blob: f3116701056b25a1194d4d3eedab64e9d3092cde [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 Lesinski9431c472017-04-21 16:08:02 -070041using android::base::StringPrintf;
42
Adam Lesinski59e04c62016-02-04 15:59:23 -080043namespace {
44
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045/*
46 * Visitor that converts a reference's resource ID to a resource name,
47 * given a mapping from resource ID to resource name.
48 */
49class ReferenceIdToNameVisitor : public ValueVisitor {
Adam Lesinskib54ef102016-10-21 13:38:42 -070050 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 using ValueVisitor::Visit;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052
Adam Lesinskib54ef102016-10-21 13:38:42 -070053 explicit ReferenceIdToNameVisitor(
54 const std::map<ResourceId, ResourceName>* mapping)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 : mapping_(mapping) {
56 CHECK(mapping_ != nullptr);
Adam Lesinskib54ef102016-10-21 13:38:42 -070057 }
58
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 void Visit(Reference* reference) override {
60 if (!reference->id || !reference->id.value().is_valid()) {
Adam Lesinskib54ef102016-10-21 13:38:42 -070061 return;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062 }
63
Adam Lesinskib54ef102016-10-21 13:38:42 -070064 ResourceId id = reference->id.value();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 auto cache_iter = mapping_->find(id);
66 if (cache_iter != mapping_->end()) {
67 reference->name = cache_iter->second;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068 }
Adam Lesinskib54ef102016-10-21 13:38:42 -070069 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070
71 private:
72 DISALLOW_COPY_AND_ASSIGN(ReferenceIdToNameVisitor);
73
74 const std::map<ResourceId, ResourceName>* mapping_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075};
76
Adam Lesinskib54ef102016-10-21 13:38:42 -070077} // namespace
Adam Lesinski59e04c62016-02-04 15:59:23 -080078
Adam Lesinskid0f492d2017-04-03 18:12:45 -070079BinaryResourceParser::BinaryResourceParser(IAaptContext* context, ResourceTable* table,
80 const Source& source, const void* data, size_t len,
81 io::IFileCollection* files)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 : context_(context),
83 table_(table),
84 source_(source),
85 data_(data),
Adam Lesinskid0f492d2017-04-03 18:12:45 -070086 data_len_(len),
87 files_(files) {
88}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090bool BinaryResourceParser::Parse() {
91 ResChunkPullParser parser(data_, data_len_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092
Adam Lesinski9431c472017-04-21 16:08:02 -070093 if (!ResChunkPullParser::IsGoodEvent(parser.Next())) {
94 context_->GetDiagnostics()->Error(DiagMessage(source_)
95 << "corrupt resources.arsc: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -070096 return false;
97 }
Adam Lesinski9431c472017-04-21 16:08:02 -070098
99 if (parser.chunk()->type != android::RES_TABLE_TYPE) {
100 context_->GetDiagnostics()->Error(DiagMessage(source_)
101 << StringPrintf("unknown chunk of type 0x%02x",
102 (int)parser.chunk()->type));
103 return false;
104 }
105
106 if (!ParseTable(parser.chunk())) {
107 return false;
108 }
109
110 if (parser.Next() != ResChunkPullParser::Event::kEndDocument) {
111 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
112 context_->GetDiagnostics()->Warn(
113 DiagMessage(source_) << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error());
114 } else {
115 context_->GetDiagnostics()->Warn(
116 DiagMessage(source_) << StringPrintf(
117 "unexpected chunk of type 0x%02x trailing RES_TABLE_TYPE",
118 (int)parser.chunk()->type));
119 }
120 }
121 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122}
123
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124/**
Adam Lesinskib54ef102016-10-21 13:38:42 -0700125 * Parses the resource table, which contains all the packages, types, and
126 * entries.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) {
129 const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk);
130 if (!table_header) {
131 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700132 << "corrupt ResTable_header chunk");
133 return false;
134 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 ResChunkPullParser parser(GetChunkData(&table_header->header),
137 GetChunkDataLen(&table_header->header));
138 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
139 switch (util::DeviceToHost16(parser.chunk()->type)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700140 case android::RES_STRING_POOL_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 if (value_pool_.getError() == NO_INIT) {
142 status_t err = value_pool_.setTo(
143 parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700144 if (err != NO_ERROR) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 context_->GetDiagnostics()->Error(
146 DiagMessage(source_) << "corrupt string pool in ResTable: "
147 << value_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700148 return false;
149 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150
Adam Lesinskib54ef102016-10-21 13:38:42 -0700151 // Reserve some space for the strings we are going to add.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 table_->string_pool.HintWillAdd(value_pool_.size(),
153 value_pool_.styleCount());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700154 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 context_->GetDiagnostics()->Warn(
156 DiagMessage(source_) << "unexpected string pool in ResTable");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700158 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159
Adam Lesinskib54ef102016-10-21 13:38:42 -0700160 case android::RES_TABLE_PACKAGE_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 if (!ParsePackage(parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700162 return false;
163 }
164 break;
165
166 default:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 context_->GetDiagnostics()->Warn(
168 DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700169 << "unexpected chunk type "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 << (int)util::DeviceToHost16(parser.chunk()->type));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700171 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700173 }
174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
176 context_->GetDiagnostics()->Error(
177 DiagMessage(source_) << "corrupt resource table: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700178 return false;
179 }
180 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181}
182
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) {
Adam Lesinski33af6c72017-03-29 13:00:35 -0700184 constexpr size_t kMinPackageSize =
185 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
186 const ResTable_package* package_header = ConvertTo<ResTable_package, kMinPackageSize>(chunk);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 if (!package_header) {
Adam Lesinski33af6c72017-03-29 13:00:35 -0700188 context_->GetDiagnostics()->Error(DiagMessage(source_) << "corrupt ResTable_package chunk");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700189 return false;
190 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 uint32_t package_id = util::DeviceToHost32(package_header->id);
193 if (package_id > std::numeric_limits<uint8_t>::max()) {
194 context_->GetDiagnostics()->Error(
195 DiagMessage(source_) << "package ID is too big (" << package_id << ")");
Adam Lesinskib54ef102016-10-21 13:38:42 -0700196 return false;
197 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198
Adam Lesinskib54ef102016-10-21 13:38:42 -0700199 // Extract the package name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 size_t len = strnlen16((const char16_t*)package_header->name,
201 arraysize(package_header->name));
202 std::u16string package_name;
203 package_name.resize(len);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700204 for (size_t i = 0; i < len; i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 package_name[i] = util::DeviceToHost16(package_header->name[i]);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700206 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 ResourceTablePackage* package = table_->CreatePackage(
209 util::Utf16ToUtf8(package_name), static_cast<uint8_t>(package_id));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700210 if (!package) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 context_->GetDiagnostics()->Error(
212 DiagMessage(source_) << "incompatible package '" << package_name
213 << "' with ID " << package_id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700214 return false;
215 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700216
Adam Lesinskib54ef102016-10-21 13:38:42 -0700217 // There can be multiple packages in a table, so
218 // clear the type and key pool in case they were set from a previous package.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 type_pool_.uninit();
220 key_pool_.uninit();
Adam Lesinskie352b992015-11-16 11:59:14 -0800221
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 ResChunkPullParser parser(GetChunkData(&package_header->header),
223 GetChunkDataLen(&package_header->header));
224 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
225 switch (util::DeviceToHost16(parser.chunk()->type)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700226 case android::RES_STRING_POOL_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 if (type_pool_.getError() == NO_INIT) {
228 status_t err = type_pool_.setTo(
229 parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700230 if (err != NO_ERROR) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700232 << "corrupt type string pool in "
233 << "ResTable_package: "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 << type_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700235 return false;
236 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 } else if (key_pool_.getError() == NO_INIT) {
238 status_t err = key_pool_.setTo(
239 parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700240 if (err != NO_ERROR) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700242 << "corrupt key string pool in "
243 << "ResTable_package: "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 << key_pool_.getError());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700245 return false;
246 }
247 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 context_->GetDiagnostics()->Warn(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700249 << "unexpected string pool");
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700251 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252
Adam Lesinskib54ef102016-10-21 13:38:42 -0700253 case android::RES_TABLE_TYPE_SPEC_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 if (!ParseTypeSpec(parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700255 return false;
256 }
257 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258
Adam Lesinskib54ef102016-10-21 13:38:42 -0700259 case android::RES_TABLE_TYPE_TYPE:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 if (!ParseType(package, parser.chunk())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700261 return false;
262 }
263 break;
264
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800265 case android::RES_TABLE_LIBRARY_TYPE:
266 if (!ParseLibrary(parser.chunk())) {
267 return false;
268 }
269 break;
270
Adam Lesinskib54ef102016-10-21 13:38:42 -0700271 default:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 context_->GetDiagnostics()->Warn(
273 DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700274 << "unexpected chunk type "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 << (int)util::DeviceToHost16(parser.chunk()->type));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700276 break;
277 }
278 }
279
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
281 context_->GetDiagnostics()->Error(
282 DiagMessage(source_) << "corrupt ResTable_package: " << parser.error());
Adam Lesinskib54ef102016-10-21 13:38:42 -0700283 return false;
284 }
285
286 // Now go through the table and change local resource ID references to
287 // symbolic references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 ReferenceIdToNameVisitor visitor(&id_index_);
289 VisitAllValuesInTable(table_, &visitor);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700290 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700291}
292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293bool BinaryResourceParser::ParseTypeSpec(const ResChunk_header* chunk) {
294 if (type_pool_.getError() != NO_ERROR) {
295 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700296 << "missing type string pool");
297 return false;
298 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700299
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk);
301 if (!type_spec) {
302 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700303 << "corrupt ResTable_typeSpec chunk");
304 return false;
305 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 if (type_spec->id == 0) {
308 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700309 << "ResTable_typeSpec has invalid id: "
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 << type_spec->id);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700311 return false;
312 }
313 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314}
315
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316bool BinaryResourceParser::ParseType(const ResourceTablePackage* package,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700317 const ResChunk_header* chunk) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 if (type_pool_.getError() != NO_ERROR) {
319 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700320 << "missing type string pool");
321 return false;
322 }
323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 if (key_pool_.getError() != NO_ERROR) {
325 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700326 << "missing key string pool");
327 return false;
328 }
329
Adam Lesinski136fd072017-03-03 13:50:21 -0800330 // Specify a manual size, because ResTable_type contains ResTable_config, which changes
331 // a lot and has its own code to handle variable size.
332 const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700333 if (!type) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700335 << "corrupt ResTable_type chunk");
336 return false;
337 }
338
339 if (type->id == 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 context_->GetDiagnostics()->Error(DiagMessage(source_)
Adam Lesinskib54ef102016-10-21 13:38:42 -0700341 << "ResTable_type has invalid id: "
342 << (int)type->id);
343 return false;
344 }
345
346 ConfigDescription config;
347 config.copyFromDtoH(type->config);
348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 const std::string type_str = util::GetString(type_pool_, type->id - 1);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 const ResourceType* parsed_type = ParseResourceType(type_str);
352 if (!parsed_type) {
353 context_->GetDiagnostics()->Error(
354 DiagMessage(source_) << "invalid type name '" << type_str
Adam Lesinskib54ef102016-10-21 13:38:42 -0700355 << "' for type with ID " << (int)type->id);
356 return false;
357 }
358
359 TypeVariant tv(type);
360 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) {
361 const ResTable_entry* entry = *it;
362 if (!entry) {
363 continue;
364 }
365
366 const ResourceName name(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700367 package->name, *parsed_type,
368 util::GetString(key_pool_, util::DeviceToHost32(entry->key.index)));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700369
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 const ResourceId res_id(package->id.value(), type->id,
371 static_cast<uint16_t>(it.index()));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700372
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 std::unique_ptr<Value> resource_value;
Adam Lesinskib54ef102016-10-21 13:38:42 -0700374 if (entry->flags & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700375 const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700376
377 // TODO(adamlesinski): Check that the entry count is valid.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378 resource_value = ParseMapEntry(name, config, mapEntry);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700379 } else {
380 const Res_value* value =
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700381 (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size));
382 resource_value = ParseValue(name, config, *value);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700383 }
384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 if (!resource_value) {
386 context_->GetDiagnostics()->Error(
387 DiagMessage(source_) << "failed to parse value for resource " << name
388 << " (" << res_id << ") with configuration '"
Adam Lesinskib54ef102016-10-21 13:38:42 -0700389 << config << "'");
390 return false;
391 }
392
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000393 if (!table_->AddResourceAllowMangled(name, res_id, config, {}, std::move(resource_value),
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 context_->GetDiagnostics())) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700395 return false;
396 }
397
398 if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) {
399 Symbol symbol;
400 symbol.state = SymbolState::kPublic;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 symbol.source = source_.WithLine(0);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700402 if (!table_->SetSymbolStateAllowMangled(name, res_id, symbol, context_->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700403 return false;
Adam Lesinskib54ef102016-10-21 13:38:42 -0700404 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700405 }
406
Adam Lesinskib54ef102016-10-21 13:38:42 -0700407 // Add this resource name->id mapping to the index so
408 // that we can resolve all ID references to name references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 auto cache_iter = id_index_.find(res_id);
410 if (cache_iter == id_index_.end()) {
411 id_index_.insert({res_id, name});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700412 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700413 }
414 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700415}
416
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800417bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) {
418 DynamicRefTable dynamic_ref_table;
419 if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) {
420 return false;
421 }
422
423 const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries();
424 const size_t count = entries.size();
425 for (size_t i = 0; i < count; i++) {
426 table_->included_packages_[entries.valueAt(i)] =
427 util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string()));
428 }
429 return true;
430}
431
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700432std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name,
433 const ConfigDescription& config,
434 const android::Res_value& value) {
435 std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_,
436 value, &table_->string_pool);
437 if (files_ != nullptr && item != nullptr) {
438 FileReference* file_ref = ValueCast<FileReference>(item.get());
439 if (file_ref != nullptr) {
440 file_ref->file = files_->FindFile(*file_ref->path);
441 if (file_ref->file == nullptr) {
Adam Lesinski742888f2017-04-28 15:34:52 -0700442 context_->GetDiagnostics()->Warn(DiagMessage() << "resource " << name << " for config '"
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700443 << config << "' is a file reference to '"
444 << *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 Lesinskice5e56e2016-10-21 17:56:45 -0700452std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700453 const ResourceNameRef& name, const ConfigDescription& config,
454 const ResTable_map_entry* map) {
455 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:
459 // fallthrough
460 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 Lesinski33af6c72017-03-29 13:00:35 -0700472 context_->GetDiagnostics()->Error(DiagMessage() << "illegal map type '" << ToString(name.type)
473 << "' (" << (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 Lesinskice5e56e2016-10-21 17:56:45 -0700479std::unique_ptr<Style> BinaryResourceParser::ParseStyle(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700480 const ResourceNameRef& name, const ConfigDescription& config,
481 const ResTable_map_entry* map) {
482 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 Lesinskice5e56e2016-10-21 17:56:45 -0700504std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700505 const ResourceNameRef& name, const ConfigDescription& config,
506 const ResTable_map_entry* map) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 const bool is_weak =
508 (util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0;
509 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(is_weak);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700510
Adam Lesinskib54ef102016-10-21 13:38:42 -0700511 // First we must discover what type of attribute this is. Find the type mask.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 auto type_mask_iter =
Adam Lesinskib54ef102016-10-21 13:38:42 -0700513 std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514 return util::DeviceToHost32(entry.name.ident) ==
Adam Lesinskib54ef102016-10-21 13:38:42 -0700515 ResTable_map::ATTR_TYPE;
516 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700517
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 if (type_mask_iter != end(map)) {
519 attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700520 }
521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 for (const ResTable_map& map_entry : map) {
523 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
524 switch (util::DeviceToHost32(map_entry.name.ident)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700525 case ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 attr->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700527 break;
528 case ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 attr->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700530 break;
531 }
532 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700533 }
534
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 if (attr->type_mask &
536 (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700537 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 symbol.value = util::DeviceToHost32(map_entry.value.data);
539 symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700540 attr->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700542 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700543
Adam Lesinskib54ef102016-10-21 13:38:42 -0700544 // TODO(adamlesinski): Find i80n, attributes.
545 return attr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700546}
547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548std::unique_ptr<Array> BinaryResourceParser::ParseArray(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700549 const ResourceNameRef& name, const ConfigDescription& config,
550 const ResTable_map_entry* map) {
551 std::unique_ptr<Array> array = util::make_unique<Array>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 for (const ResTable_map& map_entry : map) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700553 array->items.push_back(ParseValue(name, config, map_entry.value));
Adam Lesinskib54ef102016-10-21 13:38:42 -0700554 }
555 return array;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700556}
557
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558std::unique_ptr<Plural> BinaryResourceParser::ParsePlural(
Adam Lesinskib54ef102016-10-21 13:38:42 -0700559 const ResourceNameRef& name, const ConfigDescription& config,
560 const ResTable_map_entry* map) {
561 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 for (const ResTable_map& map_entry : map) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700563 std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value);
Adam Lesinskib54ef102016-10-21 13:38:42 -0700564 if (!item) {
565 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700566 }
Adam Lesinskib54ef102016-10-21 13:38:42 -0700567
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 switch (util::DeviceToHost32(map_entry.name.ident)) {
Adam Lesinskib54ef102016-10-21 13:38:42 -0700569 case ResTable_map::ATTR_ZERO:
570 plural->values[Plural::Zero] = std::move(item);
571 break;
572 case ResTable_map::ATTR_ONE:
573 plural->values[Plural::One] = std::move(item);
574 break;
575 case ResTable_map::ATTR_TWO:
576 plural->values[Plural::Two] = std::move(item);
577 break;
578 case ResTable_map::ATTR_FEW:
579 plural->values[Plural::Few] = std::move(item);
580 break;
581 case ResTable_map::ATTR_MANY:
582 plural->values[Plural::Many] = std::move(item);
583 break;
584 case ResTable_map::ATTR_OTHER:
585 plural->values[Plural::Other] = std::move(item);
586 break;
587 }
588 }
589 return plural;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700590}
591
Adam Lesinskib54ef102016-10-21 13:38:42 -0700592} // namespace aapt