blob: 5078678e08a3dd346ba25d00f3353f92ea731f0a [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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070052 : mapping_(mapping) {
53 CHECK(mapping_ != nullptr);
Adam Lesinskib54ef102016-10-21 13:38:42 -070054 }
55
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700196 type_pool_.uninit();
197 key_pool_.uninit();
Adam Lesinskie352b992015-11-16 11:59:14 -0800198
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700226 if (!ParseTypeSpec(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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -0700263bool BinaryResourceParser::ParseTypeSpec(const ResChunk_header* chunk) {
264 if (type_pool_.getError() != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700265 diag_->Error(DiagMessage(source_) << "missing type string pool");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700266 return false;
267 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700268
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700269 const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk);
270 if (!type_spec) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700271 diag_->Error(DiagMessage(source_) << "corrupt ResTable_typeSpec chunk");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700272 return false;
273 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700275 if (type_spec->id == 0) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700276 diag_->Error(DiagMessage(source_) << "ResTable_typeSpec has invalid id: " << type_spec->id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700277 return false;
278 }
279 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280}
281
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700282bool BinaryResourceParser::ParseType(const ResourceTablePackage* package,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700283 const ResChunk_header* chunk) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700284 if (type_pool_.getError() != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700285 diag_->Error(DiagMessage(source_) << "missing type string pool");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700286 return false;
287 }
288
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700289 if (key_pool_.getError() != NO_ERROR) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700290 diag_->Error(DiagMessage(source_) << "missing key string pool");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700291 return false;
292 }
293
Adam Lesinski136fd072017-03-03 13:50:21 -0800294 // Specify a manual size, because ResTable_type contains ResTable_config, which changes
295 // a lot and has its own code to handle variable size.
296 const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700297 if (!type) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700298 diag_->Error(DiagMessage(source_) << "corrupt ResTable_type chunk");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700299 return false;
300 }
301
302 if (type->id == 0) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700303 diag_->Error(DiagMessage(source_) << "ResTable_type has invalid id: " << (int)type->id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700304 return false;
305 }
306
307 ConfigDescription config;
308 config.copyFromDtoH(type->config);
309
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700310 const std::string type_str = util::GetString(type_pool_, type->id - 1);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700311
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700312 const ResourceType* parsed_type = ParseResourceType(type_str);
313 if (!parsed_type) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700314 diag_->Error(DiagMessage(source_)
315 << "invalid type name '" << type_str << "' for type with ID " << (int)type->id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700316 return false;
317 }
318
319 TypeVariant tv(type);
320 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) {
321 const ResTable_entry* entry = *it;
322 if (!entry) {
323 continue;
324 }
325
Adam Lesinski46708052017-09-29 14:49:15 -0700326 const ResourceName name(package->name, *parsed_type,
327 util::GetString(key_pool_, util::DeviceToHost32(entry->key.index)));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700328
Adam Lesinski46708052017-09-29 14:49:15 -0700329 const ResourceId res_id(package->id.value(), type->id, static_cast<uint16_t>(it.index()));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700330
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700331 std::unique_ptr<Value> resource_value;
Adam Lesinskib54ef102016-10-21 13:38:42 -0700332 if (entry->flags & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700333 const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700334
335 // TODO(adamlesinski): Check that the entry count is valid.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700336 resource_value = ParseMapEntry(name, config, mapEntry);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700337 } else {
338 const Res_value* value =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700339 (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size));
340 resource_value = ParseValue(name, config, *value);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700341 }
342
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700343 if (!resource_value) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700344 diag_->Error(DiagMessage(source_) << "failed to parse value for resource " << name << " ("
Adam Lesinski46708052017-09-29 14:49:15 -0700345 << res_id << ") with configuration '" << config << "'");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700346 return false;
347 }
348
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000349 if (!table_->AddResourceAllowMangled(name, res_id, config, {}, std::move(resource_value),
Adam Lesinski8780eb62017-10-31 17:44:39 -0700350 diag_)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700351 return false;
352 }
353
354 if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) {
355 Symbol symbol;
356 symbol.state = SymbolState::kPublic;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700357 symbol.source = source_.WithLine(0);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700358 if (!table_->SetSymbolStateAllowMangled(name, res_id, symbol, diag_)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700359 return false;
Adam Lesinskib54ef102016-10-21 13:38:42 -0700360 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700361 }
362
Adam Lesinskib54ef102016-10-21 13:38:42 -0700363 // Add this resource name->id mapping to the index so
364 // that we can resolve all ID references to name references.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700365 auto cache_iter = id_index_.find(res_id);
366 if (cache_iter == id_index_.end()) {
367 id_index_.insert({res_id, name});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700368 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700369 }
370 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700371}
372
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800373bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) {
374 DynamicRefTable dynamic_ref_table;
375 if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) {
376 return false;
377 }
378
379 const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries();
380 const size_t count = entries.size();
381 for (size_t i = 0; i < count; i++) {
382 table_->included_packages_[entries.valueAt(i)] =
383 util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string()));
384 }
385 return true;
386}
387
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700388std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name,
389 const ConfigDescription& config,
390 const android::Res_value& value) {
391 std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_,
392 value, &table_->string_pool);
Adam Lesinski8780eb62017-10-31 17:44:39 -0700393 if (files_ != nullptr) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700394 FileReference* file_ref = ValueCast<FileReference>(item.get());
395 if (file_ref != nullptr) {
396 file_ref->file = files_->FindFile(*file_ref->path);
397 if (file_ref->file == nullptr) {
Adam Lesinski8780eb62017-10-31 17:44:39 -0700398 diag_->Warn(DiagMessage() << "resource " << name << " for config '" << config
399 << "' is a file reference to '" << *file_ref->path
400 << "' but no such path exists");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700401 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700402 }
403 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700404 return item;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700405}
406
Adam Lesinski46708052017-09-29 14:49:15 -0700407std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry(const ResourceNameRef& name,
408 const ConfigDescription& config,
409 const ResTable_map_entry* map) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700410 switch (name.type) {
411 case ResourceType::kStyle:
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700412 return ParseStyle(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700413 case ResourceType::kAttrPrivate:
Adam Lesinski8780eb62017-10-31 17:44:39 -0700414 // fallthrough
Adam Lesinskib54ef102016-10-21 13:38:42 -0700415 case ResourceType::kAttr:
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700416 return ParseAttr(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700417 case ResourceType::kArray:
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700418 return ParseArray(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700419 case ResourceType::kPlurals:
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700420 return ParsePlural(name, config, map);
Adam Lesinski33af6c72017-03-29 13:00:35 -0700421 case ResourceType::kId:
422 // Special case: An ID is not a bag, but some apps have defined the auto-generated
423 // IDs that come from declaring an enum value in an attribute as an empty map...
424 // We can ignore the value here.
425 return util::make_unique<Id>();
Adam Lesinskib54ef102016-10-21 13:38:42 -0700426 default:
Adam Lesinski93190b72017-11-03 15:20:17 -0700427 diag_->Error(DiagMessage() << "illegal map type '" << to_string(name.type) << "' ("
Adam Lesinski8780eb62017-10-31 17:44:39 -0700428 << (int)name.type << ")");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700429 break;
430 }
431 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700432}
433
Adam Lesinski46708052017-09-29 14:49:15 -0700434std::unique_ptr<Style> BinaryResourceParser::ParseStyle(const ResourceNameRef& name,
435 const ConfigDescription& config,
436 const ResTable_map_entry* map) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700437 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700438 if (util::DeviceToHost32(map->parent.ident) != 0) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700439 // The parent is a regular reference to a resource.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700440 style->parent = Reference(util::DeviceToHost32(map->parent.ident));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700441 }
442
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700443 for (const ResTable_map& map_entry : map) {
444 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700445 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700446 }
447
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700448 Style::Entry style_entry;
449 style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700450 style_entry.value = ParseValue(name, config, map_entry.value);
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700451 if (!style_entry.value) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700452 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700453 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700454 style->entries.push_back(std::move(style_entry));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700455 }
456 return style;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700457}
458
Adam Lesinski46708052017-09-29 14:49:15 -0700459std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr(const ResourceNameRef& name,
460 const ConfigDescription& config,
461 const ResTable_map_entry* map) {
462 const bool is_weak = (util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700463 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(is_weak);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700464
Adam Lesinskib54ef102016-10-21 13:38:42 -0700465 // First we must discover what type of attribute this is. Find the type mask.
Adam Lesinski46708052017-09-29 14:49:15 -0700466 auto type_mask_iter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool {
467 return util::DeviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE;
468 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700469
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700470 if (type_mask_iter != end(map)) {
471 attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700472 }
473
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700474 for (const ResTable_map& map_entry : map) {
475 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
476 switch (util::DeviceToHost32(map_entry.name.ident)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700477 case ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700478 attr->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700479 break;
480 case ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700481 attr->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700482 break;
483 }
484 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700485 }
486
Adam Lesinski46708052017-09-29 14:49:15 -0700487 if (attr->type_mask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700488 Attribute::Symbol symbol;
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700489 symbol.value = util::DeviceToHost32(map_entry.value.data);
490 symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700491 attr->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700492 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700493 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700494
Adam Lesinskib54ef102016-10-21 13:38:42 -0700495 // TODO(adamlesinski): Find i80n, attributes.
496 return attr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700497}
498
Adam Lesinski46708052017-09-29 14:49:15 -0700499std::unique_ptr<Array> BinaryResourceParser::ParseArray(const ResourceNameRef& name,
500 const ConfigDescription& config,
501 const ResTable_map_entry* map) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700502 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700503 for (const ResTable_map& map_entry : map) {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700504 array->elements.push_back(ParseValue(name, config, map_entry.value));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700505 }
506 return array;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700507}
508
Adam Lesinski46708052017-09-29 14:49:15 -0700509std::unique_ptr<Plural> BinaryResourceParser::ParsePlural(const ResourceNameRef& name,
510 const ConfigDescription& config,
511 const ResTable_map_entry* map) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700512 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700513 for (const ResTable_map& map_entry : map) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700514 std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700515 if (!item) {
516 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700517 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700518
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700519 switch (util::DeviceToHost32(map_entry.name.ident)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700520 case ResTable_map::ATTR_ZERO:
521 plural->values[Plural::Zero] = std::move(item);
522 break;
523 case ResTable_map::ATTR_ONE:
524 plural->values[Plural::One] = std::move(item);
525 break;
526 case ResTable_map::ATTR_TWO:
527 plural->values[Plural::Two] = std::move(item);
528 break;
529 case ResTable_map::ATTR_FEW:
530 plural->values[Plural::Few] = std::move(item);
531 break;
532 case ResTable_map::ATTR_MANY:
533 plural->values[Plural::Many] = std::move(item);
534 break;
535 case ResTable_map::ATTR_OTHER:
536 plural->values[Plural::Other] = std::move(item);
537 break;
538 }
539 }
540 return plural;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541}
542
Adam Lesinskib54ef102016-10-21 13:38:42 -0700543} // namespace aapt