blob: 8215ddf0461cd0ab974c8ccb25f9504962c69858 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski46708052017-09-29 14:49:15 -070017#include "format/binary/BinaryResourceParser.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <algorithm>
20#include <map>
21#include <string>
22
23#include "android-base/logging.h"
24#include "android-base/macros.h"
Adam Lesinski9431c472017-04-21 16:08:02 -070025#include "android-base/stringprintf.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "androidfw/ResourceTypes.h"
27#include "androidfw/TypeWrappers.h"
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "ResourceTable.h"
30#include "ResourceUtils.h"
31#include "ResourceValues.h"
32#include "Source.h"
33#include "ValueVisitor.h"
Adam Lesinski46708052017-09-29 14:49:15 -070034#include "format/binary/ResChunkPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "util/Util.h"
36
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037using namespace android;
38
Adam Lesinski060b53d2017-07-28 17:10:35 -070039using ::android::base::StringPrintf;
Adam Lesinski9431c472017-04-21 16:08:02 -070040
Adam Lesinski46708052017-09-29 14:49:15 -070041namespace aapt {
42
Adam Lesinski59e04c62016-02-04 15:59:23 -080043namespace {
44
Adam Lesinski060b53d2017-07-28 17:10:35 -070045// Visitor that converts a reference's resource ID to a resource name, given a mapping from
46// resource ID to resource name.
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070047class ReferenceIdToNameVisitor : public DescendingValueVisitor {
Adam Lesinskib54ef102016-10-21 13:38:42 -070048 public:
Adam Lesinskid3ffa8442017-09-28 13:34:35 -070049 using DescendingValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinski060b53d2017-07-28 17:10:35 -070051 explicit ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceName>* mapping)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 : mapping_(mapping) {
53 CHECK(mapping_ != nullptr);
Adam Lesinskib54ef102016-10-21 13:38:42 -070054 }
55
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 void Visit(Reference* reference) override {
57 if (!reference->id || !reference->id.value().is_valid()) {
Adam Lesinskib54ef102016-10-21 13:38:42 -070058 return;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059 }
60
Adam Lesinskib54ef102016-10-21 13:38:42 -070061 ResourceId id = reference->id.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 auto cache_iter = mapping_->find(id);
63 if (cache_iter != mapping_->end()) {
64 reference->name = cache_iter->second;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065 }
Adam Lesinskib54ef102016-10-21 13:38:42 -070066 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067
68 private:
69 DISALLOW_COPY_AND_ASSIGN(ReferenceIdToNameVisitor);
70
71 const std::map<ResourceId, ResourceName>* mapping_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072};
73
Adam Lesinskib54ef102016-10-21 13:38:42 -070074} // namespace
Adam Lesinski59e04c62016-02-04 15:59:23 -080075
Adam Lesinski8780eb62017-10-31 17:44:39 -070076BinaryResourceParser::BinaryResourceParser(IDiagnostics* diag, ResourceTable* table,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070077 const Source& source, const void* data, size_t len,
78 io::IFileCollection* files)
Adam Lesinski8780eb62017-10-31 17:44:39 -070079 : diag_(diag), table_(table), source_(source), data_(data), data_len_(len), files_(files) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -070080}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082bool BinaryResourceParser::Parse() {
83 ResChunkPullParser parser(data_, data_len_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084
Adam Lesinski9431c472017-04-21 16:08:02 -070085 if (!ResChunkPullParser::IsGoodEvent(parser.Next())) {
Adam Lesinski8780eb62017-10-31 17:44:39 -070086 diag_->Error(DiagMessage(source_) << "corrupt resources.arsc: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -070087 return false;
88 }
Adam Lesinski9431c472017-04-21 16:08:02 -070089
90 if (parser.chunk()->type != android::RES_TABLE_TYPE) {
Adam Lesinski8780eb62017-10-31 17:44:39 -070091 diag_->Error(DiagMessage(source_) << StringPrintf("unknown chunk of type 0x%02x",
Adam Lesinski060b53d2017-07-28 17:10:35 -070092 static_cast<int>(parser.chunk()->type)));
Adam Lesinski9431c472017-04-21 16:08:02 -070093 return false;
94 }
95
96 if (!ParseTable(parser.chunk())) {
97 return false;
98 }
99
100 if (parser.Next() != ResChunkPullParser::Event::kEndDocument) {
101 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700102 diag_->Warn(DiagMessage(source_)
103 << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error());
Adam Lesinski9431c472017-04-21 16:08:02 -0700104 } else {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700105 diag_->Warn(DiagMessage(source_)
106 << StringPrintf("unexpected chunk of type 0x%02x trailing RES_TABLE_TYPE",
107 static_cast<int>(parser.chunk()->type)));
Adam Lesinski9431c472017-04-21 16:08:02 -0700108 }
109 }
110 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111}
112
Adam Lesinski46708052017-09-29 14:49:15 -0700113// Parses the resource table, which contains all the packages, types, and entries.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) {
115 const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk);
116 if (!table_header) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700117 diag_->Error(DiagMessage(source_) << "corrupt ResTable_header chunk");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700118 return false;
119 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 ResChunkPullParser parser(GetChunkData(&table_header->header),
122 GetChunkDataLen(&table_header->header));
123 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
124 switch (util::DeviceToHost16(parser.chunk()->type)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700125 case android::RES_STRING_POOL_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 if (value_pool_.getError() == NO_INIT) {
Adam Lesinski46708052017-09-29 14:49:15 -0700127 status_t err =
128 value_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700129 if (err != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700130 diag_->Error(DiagMessage(source_)
131 << "corrupt string pool in ResTable: " << value_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700132 return false;
133 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134
Adam Lesinskib54ef102016-10-21 13:38:42 -0700135 // Reserve some space for the strings we are going to add.
Adam Lesinski46708052017-09-29 14:49:15 -0700136 table_->string_pool.HintWillAdd(value_pool_.size(), value_pool_.styleCount());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700137 } else {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700138 diag_->Warn(DiagMessage(source_) << "unexpected string pool in ResTable");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700140 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141
Adam Lesinskib54ef102016-10-21 13:38:42 -0700142 case android::RES_TABLE_PACKAGE_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 if (!ParsePackage(parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700144 return false;
145 }
146 break;
147
148 default:
Adam Lesinski8780eb62017-10-31 17:44:39 -0700149 diag_->Warn(DiagMessage(source_)
150 << "unexpected chunk type "
151 << static_cast<int>(util::DeviceToHost16(parser.chunk()->type)));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700152 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700154 }
155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700157 diag_->Error(DiagMessage(source_) << "corrupt resource table: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700158 return false;
159 }
160 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161}
162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) {
Adam Lesinski33af6c72017-03-29 13:00:35 -0700164 constexpr size_t kMinPackageSize =
165 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
166 const ResTable_package* package_header = ConvertTo<ResTable_package, kMinPackageSize>(chunk);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 if (!package_header) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700168 diag_->Error(DiagMessage(source_) << "corrupt ResTable_package chunk");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700169 return false;
170 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 uint32_t package_id = util::DeviceToHost32(package_header->id);
173 if (package_id > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700174 diag_->Error(DiagMessage(source_) << "package ID is too big (" << package_id << ")");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700175 return false;
176 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177
Adam Lesinskib54ef102016-10-21 13:38:42 -0700178 // Extract the package name.
Adam Lesinski46708052017-09-29 14:49:15 -0700179 size_t len = strnlen16((const char16_t*)package_header->name, arraysize(package_header->name));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 std::u16string package_name;
181 package_name.resize(len);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700182 for (size_t i = 0; i < len; i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 package_name[i] = util::DeviceToHost16(package_header->name[i]);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700184 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185
Adam Lesinski46708052017-09-29 14:49:15 -0700186 ResourceTablePackage* package =
187 table_->CreatePackage(util::Utf16ToUtf8(package_name), static_cast<uint8_t>(package_id));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700188 if (!package) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700189 diag_->Error(DiagMessage(source_)
190 << "incompatible package '" << package_name << "' with ID " << package_id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700191 return false;
192 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700193
Adam Lesinskib54ef102016-10-21 13:38:42 -0700194 // There can be multiple packages in a table, so
195 // clear the type and key pool in case they were set from a previous package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 type_pool_.uninit();
197 key_pool_.uninit();
Adam Lesinskie352b992015-11-16 11:59:14 -0800198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 ResChunkPullParser parser(GetChunkData(&package_header->header),
200 GetChunkDataLen(&package_header->header));
201 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
202 switch (util::DeviceToHost16(parser.chunk()->type)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700203 case android::RES_STRING_POOL_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 if (type_pool_.getError() == NO_INIT) {
Adam Lesinski46708052017-09-29 14:49:15 -0700205 status_t err =
206 type_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700207 if (err != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700208 diag_->Error(DiagMessage(source_) << "corrupt type string pool in "
Adam Lesinski46708052017-09-29 14:49:15 -0700209 << "ResTable_package: " << type_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700210 return false;
211 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 } else if (key_pool_.getError() == NO_INIT) {
Adam Lesinski46708052017-09-29 14:49:15 -0700213 status_t err =
214 key_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700215 if (err != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700216 diag_->Error(DiagMessage(source_) << "corrupt key string pool in "
Adam Lesinski46708052017-09-29 14:49:15 -0700217 << "ResTable_package: " << key_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700218 return false;
219 }
220 } else {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700221 diag_->Warn(DiagMessage(source_) << "unexpected string pool");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700223 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700224
Adam Lesinskib54ef102016-10-21 13:38:42 -0700225 case android::RES_TABLE_TYPE_SPEC_TYPE:
Adam Lesinski71be7052017-12-12 16:48:07 -0800226 if (!ParseTypeSpec(package, parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700227 return false;
228 }
229 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700230
Adam Lesinskib54ef102016-10-21 13:38:42 -0700231 case android::RES_TABLE_TYPE_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 if (!ParseType(package, parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700233 return false;
234 }
235 break;
236
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800237 case android::RES_TABLE_LIBRARY_TYPE:
238 if (!ParseLibrary(parser.chunk())) {
239 return false;
240 }
241 break;
242
Adam Lesinskib54ef102016-10-21 13:38:42 -0700243 default:
Adam Lesinski8780eb62017-10-31 17:44:39 -0700244 diag_->Warn(DiagMessage(source_)
245 << "unexpected chunk type "
246 << static_cast<int>(util::DeviceToHost16(parser.chunk()->type)));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700247 break;
248 }
249 }
250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700252 diag_->Error(DiagMessage(source_) << "corrupt ResTable_package: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700253 return false;
254 }
255
256 // Now go through the table and change local resource ID references to
257 // symbolic references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 ReferenceIdToNameVisitor visitor(&id_index_);
259 VisitAllValuesInTable(table_, &visitor);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700260 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261}
262
Adam Lesinski71be7052017-12-12 16:48:07 -0800263bool BinaryResourceParser::ParseTypeSpec(const ResourceTablePackage* package,
264 const ResChunk_header* chunk) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 if (type_pool_.getError() != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700266 diag_->Error(DiagMessage(source_) << "missing type string pool");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700267 return false;
268 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk);
271 if (!type_spec) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700272 diag_->Error(DiagMessage(source_) << "corrupt ResTable_typeSpec chunk");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700273 return false;
274 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700275
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 if (type_spec->id == 0) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700277 diag_->Error(DiagMessage(source_) << "ResTable_typeSpec has invalid id: " << type_spec->id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700278 return false;
279 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800280
281 // The data portion of this chunk contains entry_count 32bit entries,
282 // each one representing a set of flags.
283 const size_t entry_count = dtohl(type_spec->entryCount);
284
285 // There can only be 2^16 entries in a type, because that is the ID
286 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
287 if (entry_count > std::numeric_limits<uint16_t>::max()) {
288 diag_->Error(DiagMessage(source_)
289 << "ResTable_typeSpec has too many entries (" << entry_count << ")");
290 return false;
291 }
292
293 const size_t data_size = util::DeviceToHost32(type_spec->header.size) -
294 util::DeviceToHost16(type_spec->header.headerSize);
295 if (entry_count * sizeof(uint32_t) > data_size) {
296 diag_->Error(DiagMessage(source_) << "ResTable_typeSpec too small to hold entries.");
297 return false;
298 }
299
300 // Record the type_spec_flags for later. We don't know resource names yet, and we need those
301 // to mark resources as overlayable.
302 const uint32_t* type_spec_flags = reinterpret_cast<const uint32_t*>(
303 reinterpret_cast<uintptr_t>(type_spec) + util::DeviceToHost16(type_spec->header.headerSize));
304 for (size_t i = 0; i < entry_count; i++) {
305 ResourceId id(package->id.value_or_default(0x0), type_spec->id, static_cast<size_t>(i));
306 entry_type_spec_flags_[id] = util::DeviceToHost32(type_spec_flags[i]);
307 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700308 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700309}
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311bool BinaryResourceParser::ParseType(const ResourceTablePackage* package,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700312 const ResChunk_header* chunk) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 if (type_pool_.getError() != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700314 diag_->Error(DiagMessage(source_) << "missing type string pool");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700315 return false;
316 }
317
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 if (key_pool_.getError() != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700319 diag_->Error(DiagMessage(source_) << "missing key string pool");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700320 return false;
321 }
322
Adam Lesinski136fd072017-03-03 13:50:21 -0800323 // Specify a manual size, because ResTable_type contains ResTable_config, which changes
324 // a lot and has its own code to handle variable size.
325 const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700326 if (!type) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700327 diag_->Error(DiagMessage(source_) << "corrupt ResTable_type chunk");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700328 return false;
329 }
330
331 if (type->id == 0) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700332 diag_->Error(DiagMessage(source_) << "ResTable_type has invalid id: " << (int)type->id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700333 return false;
334 }
335
336 ConfigDescription config;
337 config.copyFromDtoH(type->config);
338
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 const std::string type_str = util::GetString(type_pool_, type->id - 1);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 const ResourceType* parsed_type = ParseResourceType(type_str);
342 if (!parsed_type) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700343 diag_->Error(DiagMessage(source_)
344 << "invalid type name '" << type_str << "' for type with ID " << (int)type->id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700345 return false;
346 }
347
348 TypeVariant tv(type);
349 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) {
350 const ResTable_entry* entry = *it;
351 if (!entry) {
352 continue;
353 }
354
Adam Lesinski46708052017-09-29 14:49:15 -0700355 const ResourceName name(package->name, *parsed_type,
356 util::GetString(key_pool_, util::DeviceToHost32(entry->key.index)));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700357
Adam Lesinski46708052017-09-29 14:49:15 -0700358 const ResourceId res_id(package->id.value(), type->id, static_cast<uint16_t>(it.index()));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 std::unique_ptr<Value> resource_value;
Adam Lesinskib54ef102016-10-21 13:38:42 -0700361 if (entry->flags & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700362 const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700363
364 // TODO(adamlesinski): Check that the entry count is valid.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 resource_value = ParseMapEntry(name, config, mapEntry);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700366 } else {
367 const Res_value* value =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700368 (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size));
369 resource_value = ParseValue(name, config, *value);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700370 }
371
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 if (!resource_value) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700373 diag_->Error(DiagMessage(source_) << "failed to parse value for resource " << name << " ("
Adam Lesinski46708052017-09-29 14:49:15 -0700374 << res_id << ") with configuration '" << config << "'");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700375 return false;
376 }
377
Adam Lesinski71be7052017-12-12 16:48:07 -0800378 if (!table_->AddResourceWithIdMangled(name, res_id, config, {}, std::move(resource_value),
379 diag_)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700380 return false;
381 }
382
Adam Lesinski71be7052017-12-12 16:48:07 -0800383 const uint32_t type_spec_flags = entry_type_spec_flags_[res_id];
384 if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0 ||
385 (type_spec_flags & ResTable_typeSpec::SPEC_OVERLAYABLE) != 0) {
386 if (entry->flags & ResTable_entry::FLAG_PUBLIC) {
387 Visibility visibility;
388 visibility.level = Visibility::Level::kPublic;
389 visibility.source = source_.WithLine(0);
390 if (!table_->SetVisibilityWithIdMangled(name, visibility, res_id, diag_)) {
391 return false;
392 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700393 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800394
395 if (type_spec_flags & ResTable_typeSpec::SPEC_OVERLAYABLE) {
396 Overlayable overlayable;
397 overlayable.source = source_.WithLine(0);
398 if (!table_->SetOverlayableMangled(name, overlayable, diag_)) {
399 return false;
400 }
401 }
402
403 // Erase the ID from the map once processed, so that we don't mark the same symbol more than
404 // once.
405 entry_type_spec_flags_.erase(res_id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700406 }
407
Adam Lesinskib54ef102016-10-21 13:38:42 -0700408 // Add this resource name->id mapping to the index so
409 // that we can resolve all ID references to name references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 auto cache_iter = id_index_.find(res_id);
411 if (cache_iter == id_index_.end()) {
412 id_index_.insert({res_id, name});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700413 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700414 }
415 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700416}
417
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800418bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) {
419 DynamicRefTable dynamic_ref_table;
420 if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) {
421 return false;
422 }
423
424 const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries();
425 const size_t count = entries.size();
426 for (size_t i = 0; i < count; i++) {
427 table_->included_packages_[entries.valueAt(i)] =
428 util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string()));
429 }
430 return true;
431}
432
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700433std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name,
434 const ConfigDescription& config,
435 const android::Res_value& value) {
436 std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_,
437 value, &table_->string_pool);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700438 if (files_ != nullptr) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700439 FileReference* file_ref = ValueCast<FileReference>(item.get());
440 if (file_ref != nullptr) {
441 file_ref->file = files_->FindFile(*file_ref->path);
442 if (file_ref->file == nullptr) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700443 diag_->Warn(DiagMessage() << "resource " << name << " for config '" << config
444 << "' is a file reference to '" << *file_ref->path
445 << "' but no such path exists");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700446 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700447 }
448 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700449 return item;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700450}
451
Adam Lesinski46708052017-09-29 14:49:15 -0700452std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry(const ResourceNameRef& name,
453 const ConfigDescription& config,
454 const ResTable_map_entry* map) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700455 switch (name.type) {
456 case ResourceType::kStyle:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 return ParseStyle(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700458 case ResourceType::kAttrPrivate:
Adam Lesinski8780eb62017-10-31 17:44:39 -0700459 // fallthrough
Adam Lesinskib54ef102016-10-21 13:38:42 -0700460 case ResourceType::kAttr:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 return ParseAttr(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700462 case ResourceType::kArray:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 return ParseArray(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700464 case ResourceType::kPlurals:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 return ParsePlural(name, config, map);
Adam Lesinski33af6c72017-03-29 13:00:35 -0700466 case ResourceType::kId:
467 // Special case: An ID is not a bag, but some apps have defined the auto-generated
468 // IDs that come from declaring an enum value in an attribute as an empty map...
469 // We can ignore the value here.
470 return util::make_unique<Id>();
Adam Lesinskib54ef102016-10-21 13:38:42 -0700471 default:
Adam Lesinski93190b72017-11-03 15:20:17 -0700472 diag_->Error(DiagMessage() << "illegal map type '" << to_string(name.type) << "' ("
Adam Lesinski8780eb62017-10-31 17:44:39 -0700473 << (int)name.type << ")");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700474 break;
475 }
476 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700477}
478
Adam Lesinski46708052017-09-29 14:49:15 -0700479std::unique_ptr<Style> BinaryResourceParser::ParseStyle(const ResourceNameRef& name,
480 const ConfigDescription& config,
481 const ResTable_map_entry* map) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700482 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 if (util::DeviceToHost32(map->parent.ident) != 0) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700484 // The parent is a regular reference to a resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 style->parent = Reference(util::DeviceToHost32(map->parent.ident));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700486 }
487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 for (const ResTable_map& map_entry : map) {
489 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700490 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700491 }
492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 Style::Entry style_entry;
494 style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700495 style_entry.value = ParseValue(name, config, map_entry.value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if (!style_entry.value) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700497 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700498 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 style->entries.push_back(std::move(style_entry));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700500 }
501 return style;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700502}
503
Adam Lesinski46708052017-09-29 14:49:15 -0700504std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr(const ResourceNameRef& name,
505 const ConfigDescription& config,
506 const ResTable_map_entry* map) {
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800507 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>();
508 attr->SetWeak((util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700509
Adam Lesinskib54ef102016-10-21 13:38:42 -0700510 // First we must discover what type of attribute this is. Find the type mask.
Adam Lesinski46708052017-09-29 14:49:15 -0700511 auto type_mask_iter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool {
512 return util::DeviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE;
513 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700514
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515 if (type_mask_iter != end(map)) {
516 attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700517 }
518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 for (const ResTable_map& map_entry : map) {
520 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
521 switch (util::DeviceToHost32(map_entry.name.ident)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700522 case ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 attr->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700524 break;
525 case ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 attr->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700527 break;
528 }
529 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700530 }
531
Adam Lesinski46708052017-09-29 14:49:15 -0700532 if (attr->type_mask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700533 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 symbol.value = util::DeviceToHost32(map_entry.value.data);
535 symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700536 attr->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700538 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700539
Adam Lesinskib54ef102016-10-21 13:38:42 -0700540 // TODO(adamlesinski): Find i80n, attributes.
541 return attr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700542}
543
Adam Lesinski46708052017-09-29 14:49:15 -0700544std::unique_ptr<Array> BinaryResourceParser::ParseArray(const ResourceNameRef& name,
545 const ConfigDescription& config,
546 const ResTable_map_entry* map) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700547 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 for (const ResTable_map& map_entry : map) {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700549 array->elements.push_back(ParseValue(name, config, map_entry.value));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700550 }
551 return array;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700552}
553
Adam Lesinski46708052017-09-29 14:49:15 -0700554std::unique_ptr<Plural> BinaryResourceParser::ParsePlural(const ResourceNameRef& name,
555 const ConfigDescription& config,
556 const ResTable_map_entry* map) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700557 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 for (const ResTable_map& map_entry : map) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700559 std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700560 if (!item) {
561 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700562 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 switch (util::DeviceToHost32(map_entry.name.ident)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700565 case ResTable_map::ATTR_ZERO:
566 plural->values[Plural::Zero] = std::move(item);
567 break;
568 case ResTable_map::ATTR_ONE:
569 plural->values[Plural::One] = std::move(item);
570 break;
571 case ResTable_map::ATTR_TWO:
572 plural->values[Plural::Two] = std::move(item);
573 break;
574 case ResTable_map::ATTR_FEW:
575 plural->values[Plural::Few] = std::move(item);
576 break;
577 case ResTable_map::ATTR_MANY:
578 plural->values[Plural::Many] = std::move(item);
579 break;
580 case ResTable_map::ATTR_OTHER:
581 plural->values[Plural::Other] = std::move(item);
582 break;
583 }
584 }
585 return plural;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700586}
587
Adam Lesinskib54ef102016-10-21 13:38:42 -0700588} // namespace aapt