Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 17 | #include "unflatten/BinaryResourceParser.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "android-base/logging.h" |
| 24 | #include "android-base/macros.h" |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "androidfw/ResourceTypes.h" |
| 27 | #include "androidfw/TypeWrappers.h" |
| 28 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | #include "ResourceTable.h" |
| 30 | #include "ResourceUtils.h" |
| 31 | #include "ResourceValues.h" |
| 32 | #include "Source.h" |
| 33 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | #include "unflatten/ResChunkPullParser.h" |
| 35 | #include "util/Util.h" |
| 36 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | namespace aapt { |
| 38 | |
| 39 | using namespace android; |
| 40 | |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 41 | using android::base::StringPrintf; |
| 42 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 43 | namespace { |
| 44 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 45 | /* |
| 46 | * Visitor that converts a reference's resource ID to a resource name, |
| 47 | * given a mapping from resource ID to resource name. |
| 48 | */ |
| 49 | class ReferenceIdToNameVisitor : public ValueVisitor { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 50 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | using ValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 53 | explicit ReferenceIdToNameVisitor( |
| 54 | const std::map<ResourceId, ResourceName>* mapping) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | : mapping_(mapping) { |
| 56 | CHECK(mapping_ != nullptr); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | void Visit(Reference* reference) override { |
| 60 | if (!reference->id || !reference->id.value().is_valid()) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 61 | return; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 64 | ResourceId id = reference->id.value(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | auto cache_iter = mapping_->find(id); |
| 66 | if (cache_iter != mapping_->end()) { |
| 67 | reference->name = cache_iter->second; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 69 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | DISALLOW_COPY_AND_ASSIGN(ReferenceIdToNameVisitor); |
| 73 | |
| 74 | const std::map<ResourceId, ResourceName>* mapping_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 77 | } // namespace |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 78 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 79 | BinaryResourceParser::BinaryResourceParser(IAaptContext* context, ResourceTable* table, |
| 80 | const Source& source, const void* data, size_t len, |
| 81 | io::IFileCollection* files) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | : context_(context), |
| 83 | table_(table), |
| 84 | source_(source), |
| 85 | data_(data), |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 86 | data_len_(len), |
| 87 | files_(files) { |
| 88 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | bool BinaryResourceParser::Parse() { |
| 91 | ResChunkPullParser parser(data_, data_len_); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 93 | if (!ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 94 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
| 95 | << "corrupt resources.arsc: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
Adam Lesinski | 9431c47 | 2017-04-21 16:08:02 -0700 | [diff] [blame] | 98 | |
| 99 | if (parser.chunk()->type != android::RES_TABLE_TYPE) { |
| 100 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
| 101 | << StringPrintf("unknown chunk of type 0x%02x", |
| 102 | (int)parser.chunk()->type)); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | if (!ParseTable(parser.chunk())) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | if (parser.Next() != ResChunkPullParser::Event::kEndDocument) { |
| 111 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 112 | context_->GetDiagnostics()->Warn( |
| 113 | DiagMessage(source_) << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error()); |
| 114 | } else { |
| 115 | context_->GetDiagnostics()->Warn( |
| 116 | DiagMessage(source_) << StringPrintf( |
| 117 | "unexpected chunk of type 0x%02x trailing RES_TABLE_TYPE", |
| 118 | (int)parser.chunk()->type)); |
| 119 | } |
| 120 | } |
| 121 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | /** |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 125 | * Parses the resource table, which contains all the packages, types, and |
| 126 | * entries. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 127 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) { |
| 129 | const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk); |
| 130 | if (!table_header) { |
| 131 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 132 | << "corrupt ResTable_header chunk"); |
| 133 | return false; |
| 134 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 135 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | ResChunkPullParser parser(GetChunkData(&table_header->header), |
| 137 | GetChunkDataLen(&table_header->header)); |
| 138 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 139 | switch (util::DeviceToHost16(parser.chunk()->type)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 140 | case android::RES_STRING_POOL_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | if (value_pool_.getError() == NO_INIT) { |
| 142 | status_t err = value_pool_.setTo( |
| 143 | parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 144 | if (err != NO_ERROR) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 145 | context_->GetDiagnostics()->Error( |
| 146 | DiagMessage(source_) << "corrupt string pool in ResTable: " |
| 147 | << value_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 151 | // Reserve some space for the strings we are going to add. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | table_->string_pool.HintWillAdd(value_pool_.size(), |
| 153 | value_pool_.styleCount()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 154 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | context_->GetDiagnostics()->Warn( |
| 156 | DiagMessage(source_) << "unexpected string pool in ResTable"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 158 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 159 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 160 | case android::RES_TABLE_PACKAGE_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 161 | if (!ParsePackage(parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | break; |
| 165 | |
| 166 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | context_->GetDiagnostics()->Warn( |
| 168 | DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 169 | << "unexpected chunk type " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | << (int)util::DeviceToHost16(parser.chunk()->type)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 171 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 172 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 176 | context_->GetDiagnostics()->Error( |
| 177 | DiagMessage(source_) << "corrupt resource table: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 183 | bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) { |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 184 | constexpr size_t kMinPackageSize = |
| 185 | sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset); |
| 186 | const ResTable_package* package_header = ConvertTo<ResTable_package, kMinPackageSize>(chunk); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | if (!package_header) { |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 188 | context_->GetDiagnostics()->Error(DiagMessage(source_) << "corrupt ResTable_package chunk"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 189 | return false; |
| 190 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 191 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 192 | uint32_t package_id = util::DeviceToHost32(package_header->id); |
| 193 | if (package_id > std::numeric_limits<uint8_t>::max()) { |
| 194 | context_->GetDiagnostics()->Error( |
| 195 | DiagMessage(source_) << "package ID is too big (" << package_id << ")"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 196 | return false; |
| 197 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 199 | // Extract the package name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 200 | size_t len = strnlen16((const char16_t*)package_header->name, |
| 201 | arraysize(package_header->name)); |
| 202 | std::u16string package_name; |
| 203 | package_name.resize(len); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 204 | for (size_t i = 0; i < len; i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | package_name[i] = util::DeviceToHost16(package_header->name[i]); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 206 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 207 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 208 | ResourceTablePackage* package = table_->CreatePackage( |
| 209 | util::Utf16ToUtf8(package_name), static_cast<uint8_t>(package_id)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 210 | if (!package) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | context_->GetDiagnostics()->Error( |
| 212 | DiagMessage(source_) << "incompatible package '" << package_name |
| 213 | << "' with ID " << package_id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 214 | return false; |
| 215 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 216 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 217 | // There can be multiple packages in a table, so |
| 218 | // clear the type and key pool in case they were set from a previous package. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | type_pool_.uninit(); |
| 220 | key_pool_.uninit(); |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 221 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | ResChunkPullParser parser(GetChunkData(&package_header->header), |
| 223 | GetChunkDataLen(&package_header->header)); |
| 224 | while (ResChunkPullParser::IsGoodEvent(parser.Next())) { |
| 225 | switch (util::DeviceToHost16(parser.chunk()->type)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 226 | case android::RES_STRING_POOL_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | if (type_pool_.getError() == NO_INIT) { |
| 228 | status_t err = type_pool_.setTo( |
| 229 | parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 230 | if (err != NO_ERROR) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 231 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 232 | << "corrupt type string pool in " |
| 233 | << "ResTable_package: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | << type_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | } else if (key_pool_.getError() == NO_INIT) { |
| 238 | status_t err = key_pool_.setTo( |
| 239 | parser.chunk(), util::DeviceToHost32(parser.chunk()->size)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 240 | if (err != NO_ERROR) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 241 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 242 | << "corrupt key string pool in " |
| 243 | << "ResTable_package: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 244 | << key_pool_.getError()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 248 | context_->GetDiagnostics()->Warn(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 249 | << "unexpected string pool"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 250 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 251 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 252 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 253 | case android::RES_TABLE_TYPE_SPEC_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 254 | if (!ParseTypeSpec(parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 258 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 259 | case android::RES_TABLE_TYPE_TYPE: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | if (!ParseType(package, parser.chunk())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | break; |
| 264 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 265 | case android::RES_TABLE_LIBRARY_TYPE: |
| 266 | if (!ParseLibrary(parser.chunk())) { |
| 267 | return false; |
| 268 | } |
| 269 | break; |
| 270 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 271 | default: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | context_->GetDiagnostics()->Warn( |
| 273 | DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 274 | << "unexpected chunk type " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | << (int)util::DeviceToHost16(parser.chunk()->type)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 276 | break; |
| 277 | } |
| 278 | } |
| 279 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 280 | if (parser.event() == ResChunkPullParser::Event::kBadDocument) { |
| 281 | context_->GetDiagnostics()->Error( |
| 282 | DiagMessage(source_) << "corrupt ResTable_package: " << parser.error()); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | |
| 286 | // Now go through the table and change local resource ID references to |
| 287 | // symbolic references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | ReferenceIdToNameVisitor visitor(&id_index_); |
| 289 | VisitAllValuesInTable(table_, &visitor); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 290 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 293 | bool BinaryResourceParser::ParseTypeSpec(const ResChunk_header* chunk) { |
| 294 | if (type_pool_.getError() != NO_ERROR) { |
| 295 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 296 | << "missing type string pool"); |
| 297 | return false; |
| 298 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 299 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk); |
| 301 | if (!type_spec) { |
| 302 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 303 | << "corrupt ResTable_typeSpec chunk"); |
| 304 | return false; |
| 305 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 306 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 307 | if (type_spec->id == 0) { |
| 308 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 309 | << "ResTable_typeSpec has invalid id: " |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | << type_spec->id); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 316 | bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 317 | const ResChunk_header* chunk) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 318 | if (type_pool_.getError() != NO_ERROR) { |
| 319 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 320 | << "missing type string pool"); |
| 321 | return false; |
| 322 | } |
| 323 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | if (key_pool_.getError() != NO_ERROR) { |
| 325 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 326 | << "missing key string pool"); |
| 327 | return false; |
| 328 | } |
| 329 | |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 330 | // Specify a manual size, because ResTable_type contains ResTable_config, which changes |
| 331 | // a lot and has its own code to handle variable size. |
| 332 | const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 333 | if (!type) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 334 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 335 | << "corrupt ResTable_type chunk"); |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | if (type->id == 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 340 | context_->GetDiagnostics()->Error(DiagMessage(source_) |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 341 | << "ResTable_type has invalid id: " |
| 342 | << (int)type->id); |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | ConfigDescription config; |
| 347 | config.copyFromDtoH(type->config); |
| 348 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | const std::string type_str = util::GetString(type_pool_, type->id - 1); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 350 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 351 | const ResourceType* parsed_type = ParseResourceType(type_str); |
| 352 | if (!parsed_type) { |
| 353 | context_->GetDiagnostics()->Error( |
| 354 | DiagMessage(source_) << "invalid type name '" << type_str |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 355 | << "' for type with ID " << (int)type->id); |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | TypeVariant tv(type); |
| 360 | for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) { |
| 361 | const ResTable_entry* entry = *it; |
| 362 | if (!entry) { |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | const ResourceName name( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 367 | package->name, *parsed_type, |
| 368 | util::GetString(key_pool_, util::DeviceToHost32(entry->key.index))); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 369 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 370 | const ResourceId res_id(package->id.value(), type->id, |
| 371 | static_cast<uint16_t>(it.index())); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 372 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 373 | std::unique_ptr<Value> resource_value; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 374 | if (entry->flags & ResTable_entry::FLAG_COMPLEX) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 375 | const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 376 | |
| 377 | // TODO(adamlesinski): Check that the entry count is valid. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 378 | resource_value = ParseMapEntry(name, config, mapEntry); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 379 | } else { |
| 380 | const Res_value* value = |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 381 | (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size)); |
| 382 | resource_value = ParseValue(name, config, *value); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 385 | if (!resource_value) { |
| 386 | context_->GetDiagnostics()->Error( |
| 387 | DiagMessage(source_) << "failed to parse value for resource " << name |
| 388 | << " (" << res_id << ") with configuration '" |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 389 | << config << "'"); |
| 390 | return false; |
| 391 | } |
| 392 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 393 | if (!table_->AddResourceAllowMangled(name, res_id, config, {}, std::move(resource_value), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 394 | context_->GetDiagnostics())) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 395 | return false; |
| 396 | } |
| 397 | |
| 398 | if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) { |
| 399 | Symbol symbol; |
| 400 | symbol.state = SymbolState::kPublic; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 401 | symbol.source = source_.WithLine(0); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 402 | if (!table_->SetSymbolStateAllowMangled(name, res_id, symbol, context_->GetDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 403 | return false; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 404 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 407 | // Add this resource name->id mapping to the index so |
| 408 | // that we can resolve all ID references to name references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 409 | auto cache_iter = id_index_.find(res_id); |
| 410 | if (cache_iter == id_index_.end()) { |
| 411 | id_index_.insert({res_id, name}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 412 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 413 | } |
| 414 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 417 | bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) { |
| 418 | DynamicRefTable dynamic_ref_table; |
| 419 | if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) { |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries(); |
| 424 | const size_t count = entries.size(); |
| 425 | for (size_t i = 0; i < count; i++) { |
| 426 | table_->included_packages_[entries.valueAt(i)] = |
| 427 | util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string())); |
| 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 432 | std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name, |
| 433 | const ConfigDescription& config, |
| 434 | const android::Res_value& value) { |
| 435 | std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_, |
| 436 | value, &table_->string_pool); |
| 437 | if (files_ != nullptr && item != nullptr) { |
| 438 | FileReference* file_ref = ValueCast<FileReference>(item.get()); |
| 439 | if (file_ref != nullptr) { |
| 440 | file_ref->file = files_->FindFile(*file_ref->path); |
| 441 | if (file_ref->file == nullptr) { |
| 442 | context_->GetDiagnostics()->Error(DiagMessage() << "resource " << name << " for config '" |
| 443 | << config << "' is a file reference to '" |
| 444 | << *file_ref->path |
| 445 | << "' but no such path exists"); |
| 446 | return {}; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 447 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 450 | return item; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 453 | std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 454 | const ResourceNameRef& name, const ConfigDescription& config, |
| 455 | const ResTable_map_entry* map) { |
| 456 | switch (name.type) { |
| 457 | case ResourceType::kStyle: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 458 | return ParseStyle(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 459 | case ResourceType::kAttrPrivate: |
| 460 | // fallthrough |
| 461 | case ResourceType::kAttr: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 462 | return ParseAttr(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 463 | case ResourceType::kArray: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 464 | return ParseArray(name, config, map); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 465 | case ResourceType::kPlurals: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 466 | return ParsePlural(name, config, map); |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 467 | case ResourceType::kId: |
| 468 | // Special case: An ID is not a bag, but some apps have defined the auto-generated |
| 469 | // IDs that come from declaring an enum value in an attribute as an empty map... |
| 470 | // We can ignore the value here. |
| 471 | return util::make_unique<Id>(); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 472 | default: |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 473 | context_->GetDiagnostics()->Error(DiagMessage() << "illegal map type '" << ToString(name.type) |
| 474 | << "' (" << (int)name.type << ")"); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 480 | std::unique_ptr<Style> BinaryResourceParser::ParseStyle( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 481 | const ResourceNameRef& name, const ConfigDescription& config, |
| 482 | const ResTable_map_entry* map) { |
| 483 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 484 | if (util::DeviceToHost32(map->parent.ident) != 0) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 485 | // The parent is a regular reference to a resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 486 | style->parent = Reference(util::DeviceToHost32(map->parent.ident)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 489 | for (const ResTable_map& map_entry : map) { |
| 490 | if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 491 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 494 | Style::Entry style_entry; |
| 495 | style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident)); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 496 | style_entry.value = ParseValue(name, config, map_entry.value); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 497 | if (!style_entry.value) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 498 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 499 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 500 | style->entries.push_back(std::move(style_entry)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 501 | } |
| 502 | return style; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 505 | std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 506 | const ResourceNameRef& name, const ConfigDescription& config, |
| 507 | const ResTable_map_entry* map) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 508 | const bool is_weak = |
| 509 | (util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0; |
| 510 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(is_weak); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 511 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 512 | // First we must discover what type of attribute this is. Find the type mask. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 513 | auto type_mask_iter = |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 514 | std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 515 | return util::DeviceToHost32(entry.name.ident) == |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 516 | ResTable_map::ATTR_TYPE; |
| 517 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 518 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 519 | if (type_mask_iter != end(map)) { |
| 520 | attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 523 | for (const ResTable_map& map_entry : map) { |
| 524 | if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) { |
| 525 | switch (util::DeviceToHost32(map_entry.name.ident)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 526 | case ResTable_map::ATTR_MIN: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 527 | attr->min_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 528 | break; |
| 529 | case ResTable_map::ATTR_MAX: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | attr->max_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 531 | break; |
| 532 | } |
| 533 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 536 | if (attr->type_mask & |
| 537 | (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 538 | Attribute::Symbol symbol; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 539 | symbol.value = util::DeviceToHost32(map_entry.value.data); |
| 540 | symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 541 | attr->symbols.push_back(std::move(symbol)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 542 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 543 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 544 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 545 | // TODO(adamlesinski): Find i80n, attributes. |
| 546 | return attr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 549 | std::unique_ptr<Array> BinaryResourceParser::ParseArray( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 550 | const ResourceNameRef& name, const ConfigDescription& config, |
| 551 | const ResTable_map_entry* map) { |
| 552 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 553 | for (const ResTable_map& map_entry : map) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 554 | array->items.push_back(ParseValue(name, config, map_entry.value)); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 555 | } |
| 556 | return array; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 559 | std::unique_ptr<Plural> BinaryResourceParser::ParsePlural( |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 560 | const ResourceNameRef& name, const ConfigDescription& config, |
| 561 | const ResTable_map_entry* map) { |
| 562 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 563 | for (const ResTable_map& map_entry : map) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 564 | std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value); |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 565 | if (!item) { |
| 566 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 567 | } |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 568 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 569 | switch (util::DeviceToHost32(map_entry.name.ident)) { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 570 | case ResTable_map::ATTR_ZERO: |
| 571 | plural->values[Plural::Zero] = std::move(item); |
| 572 | break; |
| 573 | case ResTable_map::ATTR_ONE: |
| 574 | plural->values[Plural::One] = std::move(item); |
| 575 | break; |
| 576 | case ResTable_map::ATTR_TWO: |
| 577 | plural->values[Plural::Two] = std::move(item); |
| 578 | break; |
| 579 | case ResTable_map::ATTR_FEW: |
| 580 | plural->values[Plural::Few] = std::move(item); |
| 581 | break; |
| 582 | case ResTable_map::ATTR_MANY: |
| 583 | plural->values[Plural::Many] = std::move(item); |
| 584 | break; |
| 585 | case ResTable_map::ATTR_OTHER: |
| 586 | plural->values[Plural::Other] = std::move(item); |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | return plural; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 593 | } // namespace aapt |