blob: 892aee6fadcbfd78ece7dc62bb6189fdef489853 [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 Lesinskib54ef102016-10-21 13:38:42 -070017#include "unflatten/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 Lesinski1ab598f2015-08-14 14:26:04 -070034#include "unflatten/ResChunkPullParser.h"
35#include "util/Util.h"
36
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037namespace aapt {
38
39using namespace android;
40
Adam Lesinski060b53d2017-07-28 17:10:35 -070041using ::android::base::StringPrintf;
Adam Lesinski9431c472017-04-21 16:08:02 -070042
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 Lesinski1ab598f2015-08-14 14:26:04 -070047class ReferenceIdToNameVisitor : public ValueVisitor {
Adam Lesinskib54ef102016-10-21 13:38:42 -070048 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 using ValueVisitor::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 Lesinskid0f492d2017-04-03 18:12:45 -070076BinaryResourceParser::BinaryResourceParser(IAaptContext* context, ResourceTable* table,
77 const Source& source, const void* data, size_t len,
78 io::IFileCollection* files)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 : context_(context),
80 table_(table),
81 source_(source),
82 data_(data),
Adam Lesinskid0f492d2017-04-03 18:12:45 -070083 data_len_(len),
84 files_(files) {
85}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070086
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087bool BinaryResourceParser::Parse() {
88 ResChunkPullParser parser(data_, data_len_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089
Adam Lesinski9431c472017-04-21 16:08:02 -070090 if (!ResChunkPullParser::IsGoodEvent(parser.Next())) {
91 context_->GetDiagnostics()->Error(DiagMessage(source_)
92 << "corrupt resources.arsc: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -070093 return false;
94 }
Adam Lesinski9431c472017-04-21 16:08:02 -070095
96 if (parser.chunk()->type != android::RES_TABLE_TYPE) {
97 context_->GetDiagnostics()->Error(DiagMessage(source_)
98 << StringPrintf("unknown chunk of type 0x%02x",
Adam Lesinski060b53d2017-07-28 17:10:35 -070099 static_cast<int>(parser.chunk()->type)));
Adam Lesinski9431c472017-04-21 16:08:02 -0700100 return false;
101 }
102
103 if (!ParseTable(parser.chunk())) {
104 return false;
105 }
106
107 if (parser.Next() != ResChunkPullParser::Event::kEndDocument) {
108 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
109 context_->GetDiagnostics()->Warn(
110 DiagMessage(source_) << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error());
111 } else {
112 context_->GetDiagnostics()->Warn(
113 DiagMessage(source_) << StringPrintf(
114 "unexpected chunk of type 0x%02x trailing RES_TABLE_TYPE",
Adam Lesinski060b53d2017-07-28 17:10:35 -0700115 static_cast<int>(parser.chunk()->type)));
Adam Lesinski9431c472017-04-21 16:08:02 -0700116 }
117 }
118 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119}
120
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121/**
Adam Lesinskib54ef102016-10-21 13:38:42 -0700122 * Parses the resource table, which contains all the packages, types, and
123 * entries.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) {
126 const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk);
127 if (!table_header) {
128 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700129 << "corrupt ResTable_header chunk");
130 return false;
131 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 ResChunkPullParser parser(GetChunkData(&table_header->header),
134 GetChunkDataLen(&table_header->header));
135 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
136 switch (util::DeviceToHost16(parser.chunk()->type)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700137 case android::RES_STRING_POOL_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 if (value_pool_.getError() == NO_INIT) {
139 status_t err = value_pool_.setTo(
140 parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700141 if (err != NO_ERROR) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 context_->GetDiagnostics()->Error(
143 DiagMessage(source_) << "corrupt string pool in ResTable: "
144 << value_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700145 return false;
146 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
Adam Lesinskib54ef102016-10-21 13:38:42 -0700148 // Reserve some space for the strings we are going to add.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 table_->string_pool.HintWillAdd(value_pool_.size(),
150 value_pool_.styleCount());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700151 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 context_->GetDiagnostics()->Warn(
153 DiagMessage(source_) << "unexpected string pool in ResTable");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700155 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
Adam Lesinskib54ef102016-10-21 13:38:42 -0700157 case android::RES_TABLE_PACKAGE_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 if (!ParsePackage(parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700159 return false;
160 }
161 break;
162
163 default:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 context_->GetDiagnostics()->Warn(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700165 DiagMessage(source_) << "unexpected chunk type "
166 << static_cast<int>(util::DeviceToHost16(parser.chunk()->type)));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700167 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700169 }
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
172 context_->GetDiagnostics()->Error(
173 DiagMessage(source_) << "corrupt resource table: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700174 return false;
175 }
176 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700177}
178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) {
Adam Lesinski33af6c72017-03-29 13:00:35 -0700180 constexpr size_t kMinPackageSize =
181 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
182 const ResTable_package* package_header = ConvertTo<ResTable_package, kMinPackageSize>(chunk);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 if (!package_header) {
Adam Lesinski33af6c72017-03-29 13:00:35 -0700184 context_->GetDiagnostics()->Error(DiagMessage(source_) << "corrupt ResTable_package chunk");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700185 return false;
186 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 uint32_t package_id = util::DeviceToHost32(package_header->id);
189 if (package_id > std::numeric_limits<uint8_t>::max()) {
190 context_->GetDiagnostics()->Error(
191 DiagMessage(source_) << "package ID is too big (" << package_id << ")");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700192 return false;
193 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194
Adam Lesinskib54ef102016-10-21 13:38:42 -0700195 // Extract the package name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 size_t len = strnlen16((const char16_t*)package_header->name,
197 arraysize(package_header->name));
198 std::u16string package_name;
199 package_name.resize(len);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700200 for (size_t i = 0; i < len; i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 package_name[i] = util::DeviceToHost16(package_header->name[i]);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700202 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 ResourceTablePackage* package = table_->CreatePackage(
205 util::Utf16ToUtf8(package_name), static_cast<uint8_t>(package_id));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700206 if (!package) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 context_->GetDiagnostics()->Error(
208 DiagMessage(source_) << "incompatible package '" << package_name
209 << "' with ID " << package_id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700210 return false;
211 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212
Adam Lesinskib54ef102016-10-21 13:38:42 -0700213 // There can be multiple packages in a table, so
214 // clear the type and key pool in case they were set from a previous package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 type_pool_.uninit();
216 key_pool_.uninit();
Adam Lesinskie352b992015-11-16 11:59:14 -0800217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 ResChunkPullParser parser(GetChunkData(&package_header->header),
219 GetChunkDataLen(&package_header->header));
220 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
221 switch (util::DeviceToHost16(parser.chunk()->type)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700222 case android::RES_STRING_POOL_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 if (type_pool_.getError() == NO_INIT) {
224 status_t err = type_pool_.setTo(
225 parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700226 if (err != NO_ERROR) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700228 << "corrupt type string pool in "
229 << "ResTable_package: "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 << type_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700231 return false;
232 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 } else if (key_pool_.getError() == NO_INIT) {
234 status_t err = key_pool_.setTo(
235 parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700236 if (err != NO_ERROR) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700238 << "corrupt key string pool in "
239 << "ResTable_package: "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 << key_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700241 return false;
242 }
243 } else {
Adam Lesinski060b53d2017-07-28 17:10:35 -0700244 context_->GetDiagnostics()->Warn(DiagMessage(source_) << "unexpected string pool");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700246 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247
Adam Lesinskib54ef102016-10-21 13:38:42 -0700248 case android::RES_TABLE_TYPE_SPEC_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 if (!ParseTypeSpec(parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700250 return false;
251 }
252 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253
Adam Lesinskib54ef102016-10-21 13:38:42 -0700254 case android::RES_TABLE_TYPE_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 if (!ParseType(package, parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700256 return false;
257 }
258 break;
259
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800260 case android::RES_TABLE_LIBRARY_TYPE:
261 if (!ParseLibrary(parser.chunk())) {
262 return false;
263 }
264 break;
265
Adam Lesinskib54ef102016-10-21 13:38:42 -0700266 default:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 context_->GetDiagnostics()->Warn(
Adam Lesinski060b53d2017-07-28 17:10:35 -0700268 DiagMessage(source_) << "unexpected chunk type "
269 << static_cast<int>(util::DeviceToHost16(parser.chunk()->type)));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700270 break;
271 }
272 }
273
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
275 context_->GetDiagnostics()->Error(
276 DiagMessage(source_) << "corrupt ResTable_package: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700277 return false;
278 }
279
280 // Now go through the table and change local resource ID references to
281 // symbolic references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282 ReferenceIdToNameVisitor visitor(&id_index_);
283 VisitAllValuesInTable(table_, &visitor);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700284 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700285}
286
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287bool BinaryResourceParser::ParseTypeSpec(const ResChunk_header* chunk) {
288 if (type_pool_.getError() != NO_ERROR) {
289 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700290 << "missing type string pool");
291 return false;
292 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk);
295 if (!type_spec) {
296 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700297 << "corrupt ResTable_typeSpec chunk");
298 return false;
299 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700300
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 if (type_spec->id == 0) {
302 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700303 << "ResTable_typeSpec has invalid id: "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 << type_spec->id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700305 return false;
306 }
307 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700308}
309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310bool BinaryResourceParser::ParseType(const ResourceTablePackage* package,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700311 const ResChunk_header* chunk) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312 if (type_pool_.getError() != NO_ERROR) {
313 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700314 << "missing type string pool");
315 return false;
316 }
317
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 if (key_pool_.getError() != NO_ERROR) {
319 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700320 << "missing key string pool");
321 return false;
322 }
323
Adam Lesinski136fd072017-03-03 13:50:21 -0800324 // Specify a manual size, because ResTable_type contains ResTable_config, which changes
325 // a lot and has its own code to handle variable size.
326 const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700327 if (!type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700329 << "corrupt ResTable_type chunk");
330 return false;
331 }
332
333 if (type->id == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700335 << "ResTable_type has invalid id: "
336 << (int)type->id);
337 return false;
338 }
339
340 ConfigDescription config;
341 config.copyFromDtoH(type->config);
342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 const std::string type_str = util::GetString(type_pool_, type->id - 1);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700344
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700345 const ResourceType* parsed_type = ParseResourceType(type_str);
346 if (!parsed_type) {
347 context_->GetDiagnostics()->Error(
348 DiagMessage(source_) << "invalid type name '" << type_str
Adam Lesinskib54ef102016-10-21 13:38:42 -0700349 << "' for type with ID " << (int)type->id);
350 return false;
351 }
352
353 TypeVariant tv(type);
354 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) {
355 const ResTable_entry* entry = *it;
356 if (!entry) {
357 continue;
358 }
359
360 const ResourceName name(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361 package->name, *parsed_type,
362 util::GetString(key_pool_, util::DeviceToHost32(entry->key.index)));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 const ResourceId res_id(package->id.value(), type->id,
365 static_cast<uint16_t>(it.index()));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700366
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 std::unique_ptr<Value> resource_value;
Adam Lesinskib54ef102016-10-21 13:38:42 -0700368 if (entry->flags & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700369 const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700370
371 // TODO(adamlesinski): Check that the entry count is valid.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700372 resource_value = ParseMapEntry(name, config, mapEntry);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700373 } else {
374 const Res_value* value =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700375 (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size));
376 resource_value = ParseValue(name, config, *value);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700377 }
378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 if (!resource_value) {
380 context_->GetDiagnostics()->Error(
381 DiagMessage(source_) << "failed to parse value for resource " << name
382 << " (" << res_id << ") with configuration '"
Adam Lesinskib54ef102016-10-21 13:38:42 -0700383 << config << "'");
384 return false;
385 }
386
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000387 if (!table_->AddResourceAllowMangled(name, res_id, config, {}, std::move(resource_value),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700388 context_->GetDiagnostics())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700389 return false;
390 }
391
392 if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) {
393 Symbol symbol;
394 symbol.state = SymbolState::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 symbol.source = source_.WithLine(0);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700396 if (!table_->SetSymbolStateAllowMangled(name, res_id, symbol, context_->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700397 return false;
Adam Lesinskib54ef102016-10-21 13:38:42 -0700398 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399 }
400
Adam Lesinskib54ef102016-10-21 13:38:42 -0700401 // Add this resource name->id mapping to the index so
402 // that we can resolve all ID references to name references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 auto cache_iter = id_index_.find(res_id);
404 if (cache_iter == id_index_.end()) {
405 id_index_.insert({res_id, name});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700406 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700407 }
408 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700409}
410
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800411bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) {
412 DynamicRefTable dynamic_ref_table;
413 if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) {
414 return false;
415 }
416
417 const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries();
418 const size_t count = entries.size();
419 for (size_t i = 0; i < count; i++) {
420 table_->included_packages_[entries.valueAt(i)] =
421 util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string()));
422 }
423 return true;
424}
425
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700426std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name,
427 const ConfigDescription& config,
428 const android::Res_value& value) {
429 std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_,
430 value, &table_->string_pool);
431 if (files_ != nullptr && item != nullptr) {
432 FileReference* file_ref = ValueCast<FileReference>(item.get());
433 if (file_ref != nullptr) {
434 file_ref->file = files_->FindFile(*file_ref->path);
435 if (file_ref->file == nullptr) {
Adam Lesinski742888f2017-04-28 15:34:52 -0700436 context_->GetDiagnostics()->Warn(DiagMessage() << "resource " << name << " for config '"
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700437 << config << "' is a file reference to '"
438 << *file_ref->path
439 << "' but no such path exists");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700440 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700441 }
442 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700443 return item;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700444}
445
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700447 const ResourceNameRef& name, const ConfigDescription& config,
448 const ResTable_map_entry* map) {
449 switch (name.type) {
450 case ResourceType::kStyle:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 return ParseStyle(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700452 case ResourceType::kAttrPrivate:
453 // fallthrough
454 case ResourceType::kAttr:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 return ParseAttr(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700456 case ResourceType::kArray:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 return ParseArray(name, config, map);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700458 case ResourceType::kPlurals:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 return ParsePlural(name, config, map);
Adam Lesinski33af6c72017-03-29 13:00:35 -0700460 case ResourceType::kId:
461 // Special case: An ID is not a bag, but some apps have defined the auto-generated
462 // IDs that come from declaring an enum value in an attribute as an empty map...
463 // We can ignore the value here.
464 return util::make_unique<Id>();
Adam Lesinskib54ef102016-10-21 13:38:42 -0700465 default:
Adam Lesinski33af6c72017-03-29 13:00:35 -0700466 context_->GetDiagnostics()->Error(DiagMessage() << "illegal map type '" << ToString(name.type)
467 << "' (" << (int)name.type << ")");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700468 break;
469 }
470 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700471}
472
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473std::unique_ptr<Style> BinaryResourceParser::ParseStyle(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700474 const ResourceNameRef& name, const ConfigDescription& config,
475 const ResTable_map_entry* map) {
476 std::unique_ptr<Style> style = util::make_unique<Style>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 if (util::DeviceToHost32(map->parent.ident) != 0) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700478 // The parent is a regular reference to a resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 style->parent = Reference(util::DeviceToHost32(map->parent.ident));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700480 }
481
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 for (const ResTable_map& map_entry : map) {
483 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700484 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700485 }
486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 Style::Entry style_entry;
488 style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident));
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700489 style_entry.value = ParseValue(name, config, map_entry.value);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 if (!style_entry.value) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700491 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700492 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 style->entries.push_back(std::move(style_entry));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700494 }
495 return style;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700496}
497
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700498std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700499 const ResourceNameRef& name, const ConfigDescription& config,
500 const ResTable_map_entry* map) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 const bool is_weak =
502 (util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0;
503 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(is_weak);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700504
Adam Lesinskib54ef102016-10-21 13:38:42 -0700505 // First we must discover what type of attribute this is. Find the type mask.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 auto type_mask_iter =
Adam Lesinskib54ef102016-10-21 13:38:42 -0700507 std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700508 return util::DeviceToHost32(entry.name.ident) ==
Adam Lesinskib54ef102016-10-21 13:38:42 -0700509 ResTable_map::ATTR_TYPE;
510 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 if (type_mask_iter != end(map)) {
513 attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700514 }
515
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 for (const ResTable_map& map_entry : map) {
517 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
518 switch (util::DeviceToHost32(map_entry.name.ident)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700519 case ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 attr->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700521 break;
522 case ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 attr->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700524 break;
525 }
526 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700527 }
528
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 if (attr->type_mask &
530 (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700531 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 symbol.value = util::DeviceToHost32(map_entry.value.data);
533 symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700534 attr->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700535 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700536 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537
Adam Lesinskib54ef102016-10-21 13:38:42 -0700538 // TODO(adamlesinski): Find i80n, attributes.
539 return attr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700540}
541
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542std::unique_ptr<Array> BinaryResourceParser::ParseArray(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700543 const ResourceNameRef& name, const ConfigDescription& config,
544 const ResTable_map_entry* map) {
545 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 for (const ResTable_map& map_entry : map) {
Adam Lesinski4ffea042017-08-10 15:37:28 -0700547 array->elements.push_back(ParseValue(name, config, map_entry.value));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700548 }
549 return array;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700550}
551
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552std::unique_ptr<Plural> BinaryResourceParser::ParsePlural(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700553 const ResourceNameRef& name, const ConfigDescription& config,
554 const ResTable_map_entry* map) {
555 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 for (const ResTable_map& map_entry : map) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700557 std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700558 if (!item) {
559 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700560 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700561
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 switch (util::DeviceToHost32(map_entry.name.ident)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700563 case ResTable_map::ATTR_ZERO:
564 plural->values[Plural::Zero] = std::move(item);
565 break;
566 case ResTable_map::ATTR_ONE:
567 plural->values[Plural::One] = std::move(item);
568 break;
569 case ResTable_map::ATTR_TWO:
570 plural->values[Plural::Two] = std::move(item);
571 break;
572 case ResTable_map::ATTR_FEW:
573 plural->values[Plural::Few] = std::move(item);
574 break;
575 case ResTable_map::ATTR_MANY:
576 plural->values[Plural::Many] = std::move(item);
577 break;
578 case ResTable_map::ATTR_OTHER:
579 plural->values[Plural::Other] = std::move(item);
580 break;
581 }
582 }
583 return plural;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700584}
585
Adam Lesinskib54ef102016-10-21 13:38:42 -0700586} // namespace aapt