1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "format/binary/BinaryResourceParser.h"
18
19 #include <algorithm>
20 #include <map>
21 #include <string>
22
23 #include "android-base/logging.h"
24 #include "android-base/macros.h"
25 #include "android-base/stringprintf.h"
26 #include "androidfw/ResourceTypes.h"
27 #include "androidfw/TypeWrappers.h"
28
29 #include "ResourceTable.h"
30 #include "ResourceUtils.h"
31 #include "ResourceValues.h"
32 #include "Source.h"
33 #include "ValueVisitor.h"
34 #include "format/binary/ResChunkPullParser.h"
35 #include "util/Util.h"
36
37 using namespace android;
38
39 using ::android::base::StringPrintf;
40
41 namespace aapt {
42
43 namespace {
44
strcpy16_dtoh(const char16_t * src,size_t len)45 static std::u16string strcpy16_dtoh(const char16_t* src, size_t len) {
46 size_t utf16_len = strnlen16(src, len);
47 if (utf16_len == 0) {
48 return {};
49 }
50 std::u16string dst;
51 dst.resize(utf16_len);
52 for (size_t i = 0; i < utf16_len; i++) {
53 dst[i] = util::DeviceToHost16(src[i]);
54 }
55 return dst;
56 }
57
58 // Visitor that converts a reference's resource ID to a resource name, given a mapping from
59 // resource ID to resource name.
60 class ReferenceIdToNameVisitor : public DescendingValueVisitor {
61 public:
62 using DescendingValueVisitor::Visit;
63
ReferenceIdToNameVisitor(const std::map<ResourceId,ResourceName> * mapping)64 explicit ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceName>* mapping)
65 : mapping_(mapping) {
66 CHECK(mapping_ != nullptr);
67 }
68
Visit(Reference * reference)69 void Visit(Reference* reference) override {
70 if (!reference->id || !reference->id.value().is_valid()) {
71 return;
72 }
73
74 ResourceId id = reference->id.value();
75 auto cache_iter = mapping_->find(id);
76 if (cache_iter != mapping_->end()) {
77 reference->name = cache_iter->second;
78 }
79 }
80
81 private:
82 DISALLOW_COPY_AND_ASSIGN(ReferenceIdToNameVisitor);
83
84 const std::map<ResourceId, ResourceName>* mapping_;
85 };
86
87 } // namespace
88
BinaryResourceParser(IDiagnostics * diag,ResourceTable * table,const Source & source,const void * data,size_t len,io::IFileCollection * files)89 BinaryResourceParser::BinaryResourceParser(IDiagnostics* diag, ResourceTable* table,
90 const Source& source, const void* data, size_t len,
91 io::IFileCollection* files)
92 : diag_(diag), table_(table), source_(source), data_(data), data_len_(len), files_(files) {
93 }
94
Parse()95 bool BinaryResourceParser::Parse() {
96 ResChunkPullParser parser(data_, data_len_);
97
98 if (!ResChunkPullParser::IsGoodEvent(parser.Next())) {
99 diag_->Error(DiagMessage(source_) << "corrupt resources.arsc: " << parser.error());
100 return false;
101 }
102
103 if (parser.chunk()->type != android::RES_TABLE_TYPE) {
104 diag_->Error(DiagMessage(source_) << StringPrintf("unknown chunk of type 0x%02x",
105 static_cast<int>(parser.chunk()->type)));
106 return false;
107 }
108
109 if (!ParseTable(parser.chunk())) {
110 return false;
111 }
112
113 if (parser.Next() != ResChunkPullParser::Event::kEndDocument) {
114 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
115 diag_->Warn(DiagMessage(source_)
116 << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error());
117 } else {
118 diag_->Warn(DiagMessage(source_)
119 << StringPrintf("unexpected chunk of type 0x%02x trailing RES_TABLE_TYPE",
120 static_cast<int>(parser.chunk()->type)));
121 }
122 }
123
124 if (!staged_entries_to_remove_.empty()) {
125 diag_->Error(DiagMessage(source_) << "didn't find " << staged_entries_to_remove_.size()
126 << " original staged resources");
127 return false;
128 }
129
130 return true;
131 }
132
133 // Parses the resource table, which contains all the packages, types, and entries.
ParseTable(const ResChunk_header * chunk)134 bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) {
135 const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk);
136 if (!table_header) {
137 diag_->Error(DiagMessage(source_) << "corrupt ResTable_header chunk");
138 return false;
139 }
140
141 ResChunkPullParser parser(GetChunkData(&table_header->header),
142 GetChunkDataLen(&table_header->header));
143 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
144 switch (util::DeviceToHost16(parser.chunk()->type)) {
145 case android::RES_STRING_POOL_TYPE:
146 if (value_pool_.getError() == NO_INIT) {
147 status_t err =
148 value_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
149 if (err != NO_ERROR) {
150 diag_->Error(DiagMessage(source_)
151 << "corrupt string pool in ResTable: " << value_pool_.getError());
152 return false;
153 }
154
155 // Reserve some space for the strings we are going to add.
156 table_->string_pool.HintWillAdd(value_pool_.size(), value_pool_.styleCount());
157 } else {
158 diag_->Warn(DiagMessage(source_) << "unexpected string pool in ResTable");
159 }
160 break;
161
162 case android::RES_TABLE_PACKAGE_TYPE:
163 if (!ParsePackage(parser.chunk())) {
164 return false;
165 }
166 break;
167
168 default:
169 diag_->Warn(DiagMessage(source_)
170 << "unexpected chunk type "
171 << static_cast<int>(util::DeviceToHost16(parser.chunk()->type)));
172 break;
173 }
174 }
175
176 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
177 diag_->Error(DiagMessage(source_) << "corrupt resource table: " << parser.error());
178 return false;
179 }
180 return true;
181 }
182
ParsePackage(const ResChunk_header * chunk)183 bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) {
184 constexpr size_t kMinPackageSize =
185 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
186 const ResTable_package* package_header = ConvertTo<ResTable_package, kMinPackageSize>(chunk);
187 if (!package_header) {
188 diag_->Error(DiagMessage(source_) << "corrupt ResTable_package chunk");
189 return false;
190 }
191
192 uint32_t package_id = util::DeviceToHost32(package_header->id);
193 if (package_id > std::numeric_limits<uint8_t>::max()) {
194 diag_->Error(DiagMessage(source_) << "package ID is too big (" << package_id << ")");
195 return false;
196 }
197
198 // Extract the package name.
199 std::u16string package_name = strcpy16_dtoh((const char16_t*)package_header->name,
200 arraysize(package_header->name));
201
202 ResourceTablePackage* package = table_->FindOrCreatePackage(util::Utf16ToUtf8(package_name));
203 if (!package) {
204 diag_->Error(DiagMessage(source_)
205 << "incompatible package '" << package_name << "' with ID " << package_id);
206 return false;
207 }
208
209 // There can be multiple packages in a table, so
210 // clear the type and key pool in case they were set from a previous package.
211 type_pool_.uninit();
212 key_pool_.uninit();
213
214 ResChunkPullParser parser(GetChunkData(&package_header->header),
215 GetChunkDataLen(&package_header->header));
216 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
217 switch (util::DeviceToHost16(parser.chunk()->type)) {
218 case android::RES_STRING_POOL_TYPE:
219 if (type_pool_.getError() == NO_INIT) {
220 status_t err =
221 type_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
222 if (err != NO_ERROR) {
223 diag_->Error(DiagMessage(source_) << "corrupt type string pool in "
224 << "ResTable_package: " << type_pool_.getError());
225 return false;
226 }
227 } else if (key_pool_.getError() == NO_INIT) {
228 status_t err =
229 key_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
230 if (err != NO_ERROR) {
231 diag_->Error(DiagMessage(source_) << "corrupt key string pool in "
232 << "ResTable_package: " << key_pool_.getError());
233 return false;
234 }
235 } else {
236 diag_->Warn(DiagMessage(source_) << "unexpected string pool");
237 }
238 break;
239
240 case android::RES_TABLE_TYPE_SPEC_TYPE:
241 if (!ParseTypeSpec(package, parser.chunk(), package_id)) {
242 return false;
243 }
244 break;
245
246 case android::RES_TABLE_TYPE_TYPE:
247 if (!ParseType(package, parser.chunk(), package_id)) {
248 return false;
249 }
250 break;
251
252 case android::RES_TABLE_LIBRARY_TYPE:
253 if (!ParseLibrary(parser.chunk())) {
254 return false;
255 }
256 break;
257
258 case android::RES_TABLE_OVERLAYABLE_TYPE:
259 if (!ParseOverlayable(parser.chunk())) {
260 return false;
261 }
262 break;
263
264 case android::RES_TABLE_STAGED_ALIAS_TYPE:
265 if (!ParseStagedAliases(parser.chunk())) {
266 return false;
267 }
268 break;
269
270 default:
271 diag_->Warn(DiagMessage(source_)
272 << "unexpected chunk type "
273 << static_cast<int>(util::DeviceToHost16(parser.chunk()->type)));
274 break;
275 }
276 }
277
278 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
279 diag_->Error(DiagMessage(source_) << "corrupt ResTable_package: " << parser.error());
280 return false;
281 }
282
283 // Now go through the table and change local resource ID references to
284 // symbolic references.
285 ReferenceIdToNameVisitor visitor(&id_index_);
286 VisitAllValuesInTable(table_, &visitor);
287 return true;
288 }
289
ParseTypeSpec(const ResourceTablePackage * package,const ResChunk_header * chunk,uint8_t package_id)290 bool BinaryResourceParser::ParseTypeSpec(const ResourceTablePackage* package,
291 const ResChunk_header* chunk, uint8_t package_id) {
292 if (type_pool_.getError() != NO_ERROR) {
293 diag_->Error(DiagMessage(source_) << "missing type string pool");
294 return false;
295 }
296
297 const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk);
298 if (!type_spec) {
299 diag_->Error(DiagMessage(source_) << "corrupt ResTable_typeSpec chunk");
300 return false;
301 }
302
303 if (type_spec->id == 0) {
304 diag_->Error(DiagMessage(source_) << "ResTable_typeSpec has invalid id: " << type_spec->id);
305 return false;
306 }
307
308 // The data portion of this chunk contains entry_count 32bit entries,
309 // each one representing a set of flags.
310 const size_t entry_count = dtohl(type_spec->entryCount);
311
312 // There can only be 2^16 entries in a type, because that is the ID
313 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
314 if (entry_count > std::numeric_limits<uint16_t>::max()) {
315 diag_->Error(DiagMessage(source_)
316 << "ResTable_typeSpec has too many entries (" << entry_count << ")");
317 return false;
318 }
319
320 const size_t data_size = util::DeviceToHost32(type_spec->header.size) -
321 util::DeviceToHost16(type_spec->header.headerSize);
322 if (entry_count * sizeof(uint32_t) > data_size) {
323 diag_->Error(DiagMessage(source_) << "ResTable_typeSpec too small to hold entries.");
324 return false;
325 }
326
327 // Record the type_spec_flags for later. We don't know resource names yet, and we need those
328 // to mark resources as overlayable.
329 const uint32_t* type_spec_flags = reinterpret_cast<const uint32_t*>(
330 reinterpret_cast<uintptr_t>(type_spec) + util::DeviceToHost16(type_spec->header.headerSize));
331 for (size_t i = 0; i < entry_count; i++) {
332 ResourceId id(package_id, type_spec->id, static_cast<size_t>(i));
333 entry_type_spec_flags_[id] = util::DeviceToHost32(type_spec_flags[i]);
334 }
335 return true;
336 }
337
ParseType(const ResourceTablePackage * package,const ResChunk_header * chunk,uint8_t package_id)338 bool BinaryResourceParser::ParseType(const ResourceTablePackage* package,
339 const ResChunk_header* chunk, uint8_t package_id) {
340 if (type_pool_.getError() != NO_ERROR) {
341 diag_->Error(DiagMessage(source_) << "missing type string pool");
342 return false;
343 }
344
345 if (key_pool_.getError() != NO_ERROR) {
346 diag_->Error(DiagMessage(source_) << "missing key string pool");
347 return false;
348 }
349
350 // Specify a manual size, because ResTable_type contains ResTable_config, which changes
351 // a lot and has its own code to handle variable size.
352 const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk);
353 if (!type) {
354 diag_->Error(DiagMessage(source_) << "corrupt ResTable_type chunk");
355 return false;
356 }
357
358 if (type->id == 0) {
359 diag_->Error(DiagMessage(source_) << "ResTable_type has invalid id: " << (int)type->id);
360 return false;
361 }
362
363 ConfigDescription config;
364 config.copyFromDtoH(type->config);
365
366 const std::string type_str = util::GetString(type_pool_, type->id - 1);
367 const ResourceType* parsed_type = ParseResourceType(type_str);
368 if (!parsed_type) {
369 diag_->Warn(DiagMessage(source_)
370 << "invalid type name '" << type_str << "' for type with ID " << type->id);
371 return true;
372 }
373
374 TypeVariant tv(type);
375 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) {
376 const ResTable_entry* entry = *it;
377 if (!entry) {
378 continue;
379 }
380
381 const ResourceName name(package->name, *parsed_type,
382 util::GetString(key_pool_, util::DeviceToHost32(entry->key.index)));
383 const ResourceId res_id(package_id, type->id, static_cast<uint16_t>(it.index()));
384
385 std::unique_ptr<Value> resource_value;
386 if (entry->flags & ResTable_entry::FLAG_COMPLEX) {
387 const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry);
388
389 // TODO(adamlesinski): Check that the entry count is valid.
390 resource_value = ParseMapEntry(name, config, mapEntry);
391 } else {
392 const Res_value* value =
393 (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size));
394 resource_value = ParseValue(name, config, *value);
395 }
396
397 if (!resource_value) {
398 diag_->Error(DiagMessage(source_) << "failed to parse value for resource " << name << " ("
399 << res_id << ") with configuration '" << config << "'");
400 return false;
401 }
402
403 if (const auto to_remove_it = staged_entries_to_remove_.find({name, res_id});
404 to_remove_it != staged_entries_to_remove_.end()) {
405 staged_entries_to_remove_.erase(to_remove_it);
406 continue;
407 }
408
409 NewResourceBuilder res_builder(name);
410 res_builder.SetValue(std::move(resource_value), config)
411 .SetId(res_id, OnIdConflict::CREATE_ENTRY)
412 .SetAllowMangled(true);
413
414 if (entry->flags & ResTable_entry::FLAG_PUBLIC) {
415 Visibility visibility{Visibility::Level::kPublic};
416
417 auto spec_flags = entry_type_spec_flags_.find(res_id);
418 if (spec_flags != entry_type_spec_flags_.end() &&
419 spec_flags->second & ResTable_typeSpec::SPEC_STAGED_API) {
420 visibility.staged_api = true;
421 }
422
423 res_builder.SetVisibility(visibility);
424 // Erase the ID from the map once processed, so that we don't mark the same symbol more than
425 // once.
426 entry_type_spec_flags_.erase(res_id);
427 }
428
429 // Add this resource name->id mapping to the index so
430 // that we can resolve all ID references to name references.
431 auto cache_iter = id_index_.find(res_id);
432 if (cache_iter == id_index_.end()) {
433 id_index_.insert({res_id, name});
434 }
435
436 if (!table_->AddResource(res_builder.Build(), diag_)) {
437 return false;
438 }
439 }
440 return true;
441 }
442
ParseLibrary(const ResChunk_header * chunk)443 bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) {
444 DynamicRefTable dynamic_ref_table;
445 if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) {
446 return false;
447 }
448
449 const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries();
450 const size_t count = entries.size();
451 for (size_t i = 0; i < count; i++) {
452 table_->included_packages_[entries.valueAt(i)] =
453 util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string()));
454 }
455 return true;
456 }
457
ParseOverlayable(const ResChunk_header * chunk)458 bool BinaryResourceParser::ParseOverlayable(const ResChunk_header* chunk) {
459 const ResTable_overlayable_header* header = ConvertTo<ResTable_overlayable_header>(chunk);
460 if (!header) {
461 diag_->Error(DiagMessage(source_) << "corrupt ResTable_category_header chunk");
462 return false;
463 }
464
465 auto overlayable = std::make_shared<Overlayable>();
466 overlayable->name = util::Utf16ToUtf8(strcpy16_dtoh((const char16_t*)header->name,
467 arraysize(header->name)));
468 overlayable->actor = util::Utf16ToUtf8(strcpy16_dtoh((const char16_t*)header->actor,
469 arraysize(header->name)));
470
471 ResChunkPullParser parser(GetChunkData(chunk),
472 GetChunkDataLen(chunk));
473 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
474 if (util::DeviceToHost16(parser.chunk()->type) == android::RES_TABLE_OVERLAYABLE_POLICY_TYPE) {
475 const ResTable_overlayable_policy_header* policy_header =
476 ConvertTo<ResTable_overlayable_policy_header>(parser.chunk());
477
478 const ResTable_ref* const ref_begin = reinterpret_cast<const ResTable_ref*>(
479 ((uint8_t *)policy_header) + util::DeviceToHost32(policy_header->header.headerSize));
480 const ResTable_ref* const ref_end = ref_begin
481 + util::DeviceToHost32(policy_header->entry_count);
482 for (auto ref_iter = ref_begin; ref_iter != ref_end; ++ref_iter) {
483 ResourceId res_id(util::DeviceToHost32(ref_iter->ident));
484 const auto iter = id_index_.find(res_id);
485
486 // If the overlayable chunk comes before the type chunks, the resource ids and resource name
487 // pairing will not exist at this point.
488 if (iter == id_index_.cend()) {
489 diag_->Error(DiagMessage(source_) << "failed to find resource name for overlayable"
490 << " resource " << res_id);
491 return false;
492 }
493
494 OverlayableItem overlayable_item(overlayable);
495 overlayable_item.policies = policy_header->policy_flags;
496 if (!table_->AddResource(NewResourceBuilder(iter->second)
497 .SetId(res_id, OnIdConflict::CREATE_ENTRY)
498 .SetOverlayable(std::move(overlayable_item))
499 .SetAllowMangled(true)
500 .Build(),
501 diag_)) {
502 return false;
503 }
504 }
505 }
506 }
507
508 return true;
509 }
510
ParseStagedAliases(const ResChunk_header * chunk)511 bool BinaryResourceParser::ParseStagedAliases(const ResChunk_header* chunk) {
512 auto header = ConvertTo<ResTable_staged_alias_header>(chunk);
513 if (!header) {
514 diag_->Error(DiagMessage(source_) << "corrupt ResTable_staged_alias_header chunk");
515 return false;
516 }
517
518 const auto ref_begin = reinterpret_cast<const ResTable_staged_alias_entry*>(
519 ((uint8_t*)header) + util::DeviceToHost32(header->header.headerSize));
520 const auto ref_end = ref_begin + util::DeviceToHost32(header->count);
521 for (auto ref_iter = ref_begin; ref_iter != ref_end; ++ref_iter) {
522 const auto staged_id = ResourceId(util::DeviceToHost32(ref_iter->stagedResId));
523 const auto finalized_id = ResourceId(util::DeviceToHost32(ref_iter->finalizedResId));
524
525 // If the staged alias chunk comes before the type chunks, the resource ids and resource name
526 // pairing will not exist at this point.
527 const auto iter = id_index_.find(finalized_id);
528 if (iter == id_index_.cend()) {
529 diag_->Error(DiagMessage(source_) << "failed to find resource name for finalized"
530 << " resource ID " << finalized_id);
531 return false;
532 }
533
534 // Set the staged id of the finalized resource.
535 const auto& resource_name = iter->second;
536 const StagedId staged_id_def{.id = staged_id};
537 if (!table_->AddResource(NewResourceBuilder(resource_name)
538 .SetId(finalized_id, OnIdConflict::CREATE_ENTRY)
539 .SetStagedId(staged_id_def)
540 .SetAllowMangled(true)
541 .Build(),
542 diag_)) {
543 return false;
544 }
545
546 // Since a the finalized resource entry is cloned and added to the resource table under the
547 // staged resource id, remove the cloned resource entry from the table.
548 if (!table_->RemoveResource(resource_name, staged_id)) {
549 // If we haven't seen this resource yet let's add a record to skip it when parsing.
550 staged_entries_to_remove_.insert({resource_name, staged_id});
551 }
552 }
553 return true;
554 }
555
ParseValue(const ResourceNameRef & name,const ConfigDescription & config,const android::Res_value & value)556 std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name,
557 const ConfigDescription& config,
558 const android::Res_value& value) {
559 std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(name.type, config, value_pool_,
560 value, &table_->string_pool);
561 if (files_ != nullptr) {
562 FileReference* file_ref = ValueCast<FileReference>(item.get());
563 if (file_ref != nullptr) {
564 file_ref->file = files_->FindFile(*file_ref->path);
565 if (file_ref->file == nullptr) {
566 diag_->Warn(DiagMessage() << "resource " << name << " for config '" << config
567 << "' is a file reference to '" << *file_ref->path
568 << "' but no such path exists");
569 }
570 }
571 }
572 return item;
573 }
574
ParseMapEntry(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)575 std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry(const ResourceNameRef& name,
576 const ConfigDescription& config,
577 const ResTable_map_entry* map) {
578 switch (name.type) {
579 case ResourceType::kStyle:
580 return ParseStyle(name, config, map);
581 case ResourceType::kAttrPrivate:
582 // fallthrough
583 case ResourceType::kAttr:
584 return ParseAttr(name, config, map);
585 case ResourceType::kArray:
586 return ParseArray(name, config, map);
587 case ResourceType::kPlurals:
588 return ParsePlural(name, config, map);
589 case ResourceType::kId:
590 // Special case: An ID is not a bag, but some apps have defined the auto-generated
591 // IDs that come from declaring an enum value in an attribute as an empty map...
592 // We can ignore the value here.
593 return util::make_unique<Id>();
594 default:
595 diag_->Error(DiagMessage() << "illegal map type '" << to_string(name.type) << "' ("
596 << (int)name.type << ")");
597 break;
598 }
599 return {};
600 }
601
ParseStyle(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)602 std::unique_ptr<Style> BinaryResourceParser::ParseStyle(const ResourceNameRef& name,
603 const ConfigDescription& config,
604 const ResTable_map_entry* map) {
605 std::unique_ptr<Style> style = util::make_unique<Style>();
606 if (util::DeviceToHost32(map->parent.ident) != 0) {
607 // The parent is a regular reference to a resource.
608 style->parent = Reference(util::DeviceToHost32(map->parent.ident));
609 }
610
611 for (const ResTable_map& map_entry : map) {
612 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
613 continue;
614 }
615
616 Style::Entry style_entry;
617 style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident));
618 style_entry.value = ParseValue(name, config, map_entry.value);
619 if (!style_entry.value) {
620 return {};
621 }
622 style->entries.push_back(std::move(style_entry));
623 }
624 return style;
625 }
626
ParseAttr(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)627 std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr(const ResourceNameRef& name,
628 const ConfigDescription& config,
629 const ResTable_map_entry* map) {
630 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>();
631 attr->SetWeak((util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0);
632
633 // First we must discover what type of attribute this is. Find the type mask.
634 auto type_mask_iter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool {
635 return util::DeviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE;
636 });
637
638 if (type_mask_iter != end(map)) {
639 attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data);
640 }
641
642 for (const ResTable_map& map_entry : map) {
643 if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
644 switch (util::DeviceToHost32(map_entry.name.ident)) {
645 case ResTable_map::ATTR_MIN:
646 attr->min_int = static_cast<int32_t>(map_entry.value.data);
647 break;
648 case ResTable_map::ATTR_MAX:
649 attr->max_int = static_cast<int32_t>(map_entry.value.data);
650 break;
651 }
652 continue;
653 }
654
655 if (attr->type_mask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) {
656 Attribute::Symbol symbol;
657 symbol.value = util::DeviceToHost32(map_entry.value.data);
658 symbol.type = map_entry.value.dataType;
659 symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident));
660 attr->symbols.push_back(std::move(symbol));
661 }
662 }
663
664 // TODO(adamlesinski): Find i80n, attributes.
665 return attr;
666 }
667
ParseArray(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)668 std::unique_ptr<Array> BinaryResourceParser::ParseArray(const ResourceNameRef& name,
669 const ConfigDescription& config,
670 const ResTable_map_entry* map) {
671 std::unique_ptr<Array> array = util::make_unique<Array>();
672 for (const ResTable_map& map_entry : map) {
673 array->elements.push_back(ParseValue(name, config, map_entry.value));
674 }
675 return array;
676 }
677
ParsePlural(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)678 std::unique_ptr<Plural> BinaryResourceParser::ParsePlural(const ResourceNameRef& name,
679 const ConfigDescription& config,
680 const ResTable_map_entry* map) {
681 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
682 for (const ResTable_map& map_entry : map) {
683 std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value);
684 if (!item) {
685 return {};
686 }
687
688 switch (util::DeviceToHost32(map_entry.name.ident)) {
689 case ResTable_map::ATTR_ZERO:
690 plural->values[Plural::Zero] = std::move(item);
691 break;
692 case ResTable_map::ATTR_ONE:
693 plural->values[Plural::One] = std::move(item);
694 break;
695 case ResTable_map::ATTR_TWO:
696 plural->values[Plural::Two] = std::move(item);
697 break;
698 case ResTable_map::ATTR_FEW:
699 plural->values[Plural::Few] = std::move(item);
700 break;
701 case ResTable_map::ATTR_MANY:
702 plural->values[Plural::Many] = std::move(item);
703 break;
704 case ResTable_map::ATTR_OTHER:
705 plural->values[Plural::Other] = std::move(item);
706 break;
707 }
708 }
709 return plural;
710 }
711
712 } // namespace aapt
713