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 <optional>
22 #include <string>
23
24 #include "ResourceTable.h"
25 #include "ResourceUtils.h"
26 #include "ResourceValues.h"
27 #include "ValueVisitor.h"
28 #include "android-base/logging.h"
29 #include "android-base/macros.h"
30 #include "android-base/stringprintf.h"
31 #include "androidfw/ResourceTypes.h"
32 #include "androidfw/Source.h"
33 #include "androidfw/TypeWrappers.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] = android::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 android::Source & source,const void * data,size_t len,io::IFileCollection * files)89 BinaryResourceParser::BinaryResourceParser(IDiagnostics* diag, ResourceTable* table,
90 const android::Source& source, const void* data,
91 size_t len, 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(android::DiagMessage(source_) << "corrupt resources.arsc: " << parser.error());
100 return false;
101 }
102
103 if (parser.chunk()->type != android::RES_TABLE_TYPE) {
104 diag_->Error(android::DiagMessage(source_) << StringPrintf(
105 "unknown chunk of type 0x%02x", 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(android::DiagMessage(source_)
116 << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error());
117 } else {
118 diag_->Warn(android::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(android::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(android::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 (android::util::DeviceToHost16(parser.chunk()->type)) {
145 case android::RES_STRING_POOL_TYPE:
146 if (value_pool_.getError() == NO_INIT) {
147 status_t err = value_pool_.setTo(parser.chunk(),
148 android::util::DeviceToHost32(parser.chunk()->size));
149 if (err != NO_ERROR) {
150 diag_->Error(android::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(android::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(android::DiagMessage(source_)
170 << "unexpected chunk type "
171 << static_cast<int>(android::util::DeviceToHost16(parser.chunk()->type)));
172 break;
173 }
174 }
175
176 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
177 diag_->Error(android::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(android::DiagMessage(source_) << "corrupt ResTable_package chunk");
189 return false;
190 }
191
192 uint32_t package_id = android::util::DeviceToHost32(package_header->id);
193 if (package_id > std::numeric_limits<uint8_t>::max()) {
194 diag_->Error(android::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 =
203 table_->FindOrCreatePackage(android::util::Utf16ToUtf8(package_name));
204 if (!package) {
205 diag_->Error(android::DiagMessage(source_)
206 << "incompatible package '" << package_name << "' with ID " << package_id);
207 return false;
208 }
209
210 // There can be multiple packages in a table, so
211 // clear the type and key pool in case they were set from a previous package.
212 type_pool_.uninit();
213 key_pool_.uninit();
214
215 ResChunkPullParser parser(GetChunkData(&package_header->header),
216 GetChunkDataLen(&package_header->header));
217 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
218 switch (android::util::DeviceToHost16(parser.chunk()->type)) {
219 case android::RES_STRING_POOL_TYPE:
220 if (type_pool_.getError() == NO_INIT) {
221 status_t err =
222 type_pool_.setTo(parser.chunk(), android::util::DeviceToHost32(parser.chunk()->size));
223 if (err != NO_ERROR) {
224 diag_->Error(android::DiagMessage(source_)
225 << "corrupt type string pool in "
226 << "ResTable_package: " << type_pool_.getError());
227 return false;
228 }
229 } else if (key_pool_.getError() == NO_INIT) {
230 status_t err =
231 key_pool_.setTo(parser.chunk(), android::util::DeviceToHost32(parser.chunk()->size));
232 if (err != NO_ERROR) {
233 diag_->Error(android::DiagMessage(source_)
234 << "corrupt key string pool in "
235 << "ResTable_package: " << key_pool_.getError());
236 return false;
237 }
238 } else {
239 diag_->Warn(android::DiagMessage(source_) << "unexpected string pool");
240 }
241 break;
242
243 case android::RES_TABLE_TYPE_SPEC_TYPE:
244 if (!ParseTypeSpec(package, parser.chunk(), package_id)) {
245 return false;
246 }
247 break;
248
249 case android::RES_TABLE_TYPE_TYPE:
250 if (!ParseType(package, parser.chunk(), package_id)) {
251 return false;
252 }
253 break;
254
255 case android::RES_TABLE_LIBRARY_TYPE:
256 if (!ParseLibrary(parser.chunk())) {
257 return false;
258 }
259 break;
260
261 case android::RES_TABLE_OVERLAYABLE_TYPE:
262 if (!ParseOverlayable(parser.chunk())) {
263 return false;
264 }
265 break;
266
267 case android::RES_TABLE_STAGED_ALIAS_TYPE:
268 if (!ParseStagedAliases(parser.chunk())) {
269 return false;
270 }
271 break;
272
273 default:
274 diag_->Warn(android::DiagMessage(source_)
275 << "unexpected chunk type "
276 << static_cast<int>(android::util::DeviceToHost16(parser.chunk()->type)));
277 break;
278 }
279 }
280
281 if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
282 diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_package: " << parser.error());
283 return false;
284 }
285
286 // Now go through the table and change local resource ID references to
287 // symbolic references.
288 ReferenceIdToNameVisitor visitor(&id_index_);
289 VisitAllValuesInTable(table_, &visitor);
290 return true;
291 }
292
ParseTypeSpec(const ResourceTablePackage * package,const ResChunk_header * chunk,uint8_t package_id)293 bool BinaryResourceParser::ParseTypeSpec(const ResourceTablePackage* package,
294 const ResChunk_header* chunk, uint8_t package_id) {
295 if (type_pool_.getError() != NO_ERROR) {
296 diag_->Error(android::DiagMessage(source_) << "missing type string pool");
297 return false;
298 }
299
300 const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk);
301 if (!type_spec) {
302 diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_typeSpec chunk");
303 return false;
304 }
305
306 if (type_spec->id == 0) {
307 diag_->Error(android::DiagMessage(source_)
308 << "ResTable_typeSpec has invalid id: " << type_spec->id);
309 return false;
310 }
311
312 // The data portion of this chunk contains entry_count 32bit entries,
313 // each one representing a set of flags.
314 const size_t entry_count = dtohl(type_spec->entryCount);
315
316 // There can only be 2^16 entries in a type, because that is the ID
317 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
318 if (entry_count > std::numeric_limits<uint16_t>::max()) {
319 diag_->Error(android::DiagMessage(source_)
320 << "ResTable_typeSpec has too many entries (" << entry_count << ")");
321 return false;
322 }
323
324 const size_t data_size = android::util::DeviceToHost32(type_spec->header.size) -
325 android::util::DeviceToHost16(type_spec->header.headerSize);
326 if (entry_count * sizeof(uint32_t) > data_size) {
327 diag_->Error(android::DiagMessage(source_) << "ResTable_typeSpec too small to hold entries.");
328 return false;
329 }
330
331 // Record the type_spec_flags for later. We don't know resource names yet, and we need those
332 // to mark resources as overlayable.
333 const uint32_t* type_spec_flags = reinterpret_cast<const uint32_t*>(
334 reinterpret_cast<uintptr_t>(type_spec) +
335 android::util::DeviceToHost16(type_spec->header.headerSize));
336 for (size_t i = 0; i < entry_count; i++) {
337 ResourceId id(package_id, type_spec->id, static_cast<size_t>(i));
338 entry_type_spec_flags_[id] = android::util::DeviceToHost32(type_spec_flags[i]);
339 }
340 return true;
341 }
342
ParseType(const ResourceTablePackage * package,const ResChunk_header * chunk,uint8_t package_id)343 bool BinaryResourceParser::ParseType(const ResourceTablePackage* package,
344 const ResChunk_header* chunk, uint8_t package_id) {
345 if (type_pool_.getError() != NO_ERROR) {
346 diag_->Error(android::DiagMessage(source_) << "missing type string pool");
347 return false;
348 }
349
350 if (key_pool_.getError() != NO_ERROR) {
351 diag_->Error(android::DiagMessage(source_) << "missing key string pool");
352 return false;
353 }
354
355 // Specify a manual size, because ResTable_type contains ResTable_config, which changes
356 // a lot and has its own code to handle variable size.
357 const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk);
358 if (!type) {
359 diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_type chunk");
360 return false;
361 }
362
363 if (type->id == 0) {
364 diag_->Error(android::DiagMessage(source_)
365 << "ResTable_type has invalid id: " << (int)type->id);
366 return false;
367 }
368
369 ConfigDescription config;
370 config.copyFromDtoH(type->config);
371
372 const std::string type_str = android::util::GetString(type_pool_, type->id - 1);
373 std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(type_str);
374 if (!parsed_type) {
375 diag_->Warn(android::DiagMessage(source_)
376 << "invalid type name '" << type_str << "' for type with ID " << int(type->id));
377 return true;
378 }
379
380 TypeVariant tv(type);
381 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) {
382 const ResTable_entry* entry = *it;
383 if (!entry) {
384 continue;
385 }
386
387 const ResourceName name(package->name, *parsed_type,
388 android::util::GetString(key_pool_, entry->key()));
389 const ResourceId res_id(package_id, type->id, static_cast<uint16_t>(it.index()));
390
391 std::unique_ptr<Value> resource_value;
392 if (auto mapEntry = entry->map_entry()) {
393 // TODO(adamlesinski): Check that the entry count is valid.
394 resource_value = ParseMapEntry(name, config, mapEntry);
395 } else {
396 resource_value = ParseValue(name, config, entry->value());
397 }
398
399 if (!resource_value) {
400 diag_->Error(android::DiagMessage(source_)
401 << "failed to parse value for resource " << name << " (" << res_id
402 << ") with configuration '" << config << "'");
403 return false;
404 }
405
406 if (const auto to_remove_it = staged_entries_to_remove_.find({name, res_id});
407 to_remove_it != staged_entries_to_remove_.end()) {
408 staged_entries_to_remove_.erase(to_remove_it);
409 continue;
410 }
411
412 NewResourceBuilder res_builder(name);
413 res_builder.SetValue(std::move(resource_value), config)
414 .SetId(res_id, OnIdConflict::CREATE_ENTRY)
415 .SetAllowMangled(true);
416
417 if (entry->flags() & ResTable_entry::FLAG_PUBLIC) {
418 Visibility visibility{Visibility::Level::kPublic};
419
420 auto spec_flags = entry_type_spec_flags_.find(res_id);
421 if (spec_flags != entry_type_spec_flags_.end() &&
422 spec_flags->second & ResTable_typeSpec::SPEC_STAGED_API) {
423 visibility.staged_api = true;
424 }
425
426 res_builder.SetVisibility(visibility);
427 // Erase the ID from the map once processed, so that we don't mark the same symbol more than
428 // once.
429 entry_type_spec_flags_.erase(res_id);
430 }
431
432 // Add this resource name->id mapping to the index so
433 // that we can resolve all ID references to name references.
434 auto cache_iter = id_index_.find(res_id);
435 if (cache_iter == id_index_.end()) {
436 id_index_.insert({res_id, name});
437 }
438
439 if (!table_->AddResource(res_builder.Build(), diag_)) {
440 return false;
441 }
442 }
443 return true;
444 }
445
ParseLibrary(const ResChunk_header * chunk)446 bool BinaryResourceParser::ParseLibrary(const ResChunk_header* chunk) {
447 DynamicRefTable dynamic_ref_table;
448 if (dynamic_ref_table.load(reinterpret_cast<const ResTable_lib_header*>(chunk)) != NO_ERROR) {
449 return false;
450 }
451
452 const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table.entries();
453 const size_t count = entries.size();
454 for (size_t i = 0; i < count; i++) {
455 table_->included_packages_[entries.valueAt(i)] =
456 android::util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).c_str()));
457 }
458 return true;
459 }
460
ParseOverlayable(const ResChunk_header * chunk)461 bool BinaryResourceParser::ParseOverlayable(const ResChunk_header* chunk) {
462 const ResTable_overlayable_header* header = ConvertTo<ResTable_overlayable_header>(chunk);
463 if (!header) {
464 diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_category_header chunk");
465 return false;
466 }
467
468 auto overlayable = std::make_shared<Overlayable>();
469 overlayable->name = android::util::Utf16ToUtf8(
470 strcpy16_dtoh((const char16_t*)header->name, arraysize(header->name)));
471 overlayable->actor = android::util::Utf16ToUtf8(
472 strcpy16_dtoh((const char16_t*)header->actor, arraysize(header->name)));
473
474 ResChunkPullParser parser(GetChunkData(chunk),
475 GetChunkDataLen(chunk));
476 while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
477 if (android::util::DeviceToHost16(parser.chunk()->type) ==
478 android::RES_TABLE_OVERLAYABLE_POLICY_TYPE) {
479 const ResTable_overlayable_policy_header* policy_header =
480 ConvertTo<ResTable_overlayable_policy_header>(parser.chunk());
481
482 const ResTable_ref* const ref_begin = reinterpret_cast<const ResTable_ref*>(
483 ((uint8_t*)policy_header) +
484 android::util::DeviceToHost32(policy_header->header.headerSize));
485 const ResTable_ref* const ref_end =
486 ref_begin + android::util::DeviceToHost32(policy_header->entry_count);
487 for (auto ref_iter = ref_begin; ref_iter != ref_end; ++ref_iter) {
488 ResourceId res_id(android::util::DeviceToHost32(ref_iter->ident));
489 const auto iter = id_index_.find(res_id);
490
491 // If the overlayable chunk comes before the type chunks, the resource ids and resource name
492 // pairing will not exist at this point.
493 if (iter == id_index_.cend()) {
494 diag_->Error(android::DiagMessage(source_)
495 << "failed to find resource name for overlayable"
496 << " resource " << res_id);
497 return false;
498 }
499
500 OverlayableItem overlayable_item(overlayable);
501 overlayable_item.policies = policy_header->policy_flags;
502 if (!table_->AddResource(NewResourceBuilder(iter->second)
503 .SetId(res_id, OnIdConflict::CREATE_ENTRY)
504 .SetOverlayable(std::move(overlayable_item))
505 .SetAllowMangled(true)
506 .Build(),
507 diag_)) {
508 return false;
509 }
510 }
511 }
512 }
513
514 return true;
515 }
516
ParseStagedAliases(const ResChunk_header * chunk)517 bool BinaryResourceParser::ParseStagedAliases(const ResChunk_header* chunk) {
518 auto header = ConvertTo<ResTable_staged_alias_header>(chunk);
519 if (!header) {
520 diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_staged_alias_header chunk");
521 return false;
522 }
523
524 const auto ref_begin = reinterpret_cast<const ResTable_staged_alias_entry*>(
525 ((uint8_t*)header) + android::util::DeviceToHost32(header->header.headerSize));
526 const auto ref_end = ref_begin + android::util::DeviceToHost32(header->count);
527 for (auto ref_iter = ref_begin; ref_iter != ref_end; ++ref_iter) {
528 const auto staged_id = ResourceId(android::util::DeviceToHost32(ref_iter->stagedResId));
529 const auto finalized_id = ResourceId(android::util::DeviceToHost32(ref_iter->finalizedResId));
530
531 // If the staged alias chunk comes before the type chunks, the resource ids and resource name
532 // pairing will not exist at this point.
533 const auto iter = id_index_.find(finalized_id);
534 if (iter == id_index_.cend()) {
535 diag_->Error(android::DiagMessage(source_) << "failed to find resource name for finalized"
536 << " resource ID " << finalized_id);
537 return false;
538 }
539
540 // Set the staged id of the finalized resource.
541 const auto& resource_name = iter->second;
542 const StagedId staged_id_def{.id = staged_id};
543 if (!table_->AddResource(NewResourceBuilder(resource_name)
544 .SetId(finalized_id, OnIdConflict::CREATE_ENTRY)
545 .SetStagedId(staged_id_def)
546 .SetAllowMangled(true)
547 .Build(),
548 diag_)) {
549 return false;
550 }
551
552 // Since a the finalized resource entry is cloned and added to the resource table under the
553 // staged resource id, remove the cloned resource entry from the table.
554 if (!table_->RemoveResource(resource_name, staged_id)) {
555 // If we haven't seen this resource yet let's add a record to skip it when parsing.
556 staged_entries_to_remove_.insert({resource_name, staged_id});
557 }
558 }
559 return true;
560 }
561
ParseValue(const ResourceNameRef & name,const ConfigDescription & config,const android::Res_value & value)562 std::unique_ptr<Item> BinaryResourceParser::ParseValue(const ResourceNameRef& name,
563 const ConfigDescription& config,
564 const android::Res_value& value) {
565 std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(
566 name.type.type, config, value_pool_, value, &table_->string_pool);
567 if (files_ != nullptr) {
568 FileReference* file_ref = ValueCast<FileReference>(item.get());
569 if (file_ref != nullptr) {
570 file_ref->file = files_->FindFile(*file_ref->path);
571 if (file_ref->file == nullptr) {
572 diag_->Warn(android::DiagMessage() << "resource " << name << " for config '" << config
573 << "' is a file reference to '" << *file_ref->path
574 << "' but no such path exists");
575 }
576 }
577 }
578 return item;
579 }
580
ParseMapEntry(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)581 std::unique_ptr<Value> BinaryResourceParser::ParseMapEntry(const ResourceNameRef& name,
582 const ConfigDescription& config,
583 const ResTable_map_entry* map) {
584 switch (name.type.type) {
585 case ResourceType::kStyle:
586 // fallthrough
587 case ResourceType::kConfigVarying: // legacy thing used in tests
588 return ParseStyle(name, config, map);
589 case ResourceType::kAttrPrivate:
590 // fallthrough
591 case ResourceType::kAttr:
592 return ParseAttr(name, config, map);
593 case ResourceType::kArray:
594 return ParseArray(name, config, map);
595 case ResourceType::kPlurals:
596 return ParsePlural(name, config, map);
597 case ResourceType::kId:
598 // Special case: An ID is not a bag, but some apps have defined the auto-generated
599 // IDs that come from declaring an enum value in an attribute as an empty map...
600 // We can ignore the value here.
601 return util::make_unique<Id>();
602 default:
603 diag_->Error(android::DiagMessage()
604 << "illegal map type '" << name.type << "' (" << (int)name.type.type << ")");
605 break;
606 }
607 return {};
608 }
609
ParseStyle(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)610 std::unique_ptr<Style> BinaryResourceParser::ParseStyle(const ResourceNameRef& name,
611 const ConfigDescription& config,
612 const ResTable_map_entry* map) {
613 std::unique_ptr<Style> style = util::make_unique<Style>();
614 if (android::util::DeviceToHost32(map->parent.ident) != 0) {
615 // The parent is a regular reference to a resource.
616 style->parent = Reference(android::util::DeviceToHost32(map->parent.ident));
617 }
618
619 for (const ResTable_map& map_entry : map) {
620 if (Res_INTERNALID(android::util::DeviceToHost32(map_entry.name.ident))) {
621 continue;
622 }
623
624 Style::Entry style_entry;
625 style_entry.key = Reference(android::util::DeviceToHost32(map_entry.name.ident));
626 style_entry.value = ParseValue(name, config, map_entry.value);
627 if (!style_entry.value) {
628 return {};
629 }
630 style->entries.push_back(std::move(style_entry));
631 }
632 return style;
633 }
634
ParseAttr(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)635 std::unique_ptr<Attribute> BinaryResourceParser::ParseAttr(const ResourceNameRef& name,
636 const ConfigDescription& config,
637 const ResTable_map_entry* map) {
638 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>();
639 attr->SetWeak((android::util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0);
640
641 // First we must discover what type of attribute this is. Find the type mask.
642 auto type_mask_iter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool {
643 return android::util::DeviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE;
644 });
645
646 if (type_mask_iter != end(map)) {
647 attr->type_mask = android::util::DeviceToHost32(type_mask_iter->value.data);
648 }
649
650 for (const ResTable_map& map_entry : map) {
651 if (Res_INTERNALID(android::util::DeviceToHost32(map_entry.name.ident))) {
652 switch (android::util::DeviceToHost32(map_entry.name.ident)) {
653 case ResTable_map::ATTR_MIN:
654 attr->min_int = static_cast<int32_t>(map_entry.value.data);
655 break;
656 case ResTable_map::ATTR_MAX:
657 attr->max_int = static_cast<int32_t>(map_entry.value.data);
658 break;
659 }
660 continue;
661 }
662
663 if (attr->type_mask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) {
664 Attribute::Symbol symbol;
665 symbol.value = android::util::DeviceToHost32(map_entry.value.data);
666 symbol.type = map_entry.value.dataType;
667 symbol.symbol = Reference(android::util::DeviceToHost32(map_entry.name.ident));
668 attr->symbols.push_back(std::move(symbol));
669 }
670 }
671
672 // TODO(adamlesinski): Find i80n, attributes.
673 return attr;
674 }
675
ParseArray(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)676 std::unique_ptr<Array> BinaryResourceParser::ParseArray(const ResourceNameRef& name,
677 const ConfigDescription& config,
678 const ResTable_map_entry* map) {
679 std::unique_ptr<Array> array = util::make_unique<Array>();
680 for (const ResTable_map& map_entry : map) {
681 array->elements.push_back(ParseValue(name, config, map_entry.value));
682 }
683 return array;
684 }
685
ParsePlural(const ResourceNameRef & name,const ConfigDescription & config,const ResTable_map_entry * map)686 std::unique_ptr<Plural> BinaryResourceParser::ParsePlural(const ResourceNameRef& name,
687 const ConfigDescription& config,
688 const ResTable_map_entry* map) {
689 std::unique_ptr<Plural> plural = util::make_unique<Plural>();
690 for (const ResTable_map& map_entry : map) {
691 std::unique_ptr<Item> item = ParseValue(name, config, map_entry.value);
692 if (!item) {
693 return {};
694 }
695
696 switch (android::util::DeviceToHost32(map_entry.name.ident)) {
697 case ResTable_map::ATTR_ZERO:
698 plural->values[Plural::Zero] = std::move(item);
699 break;
700 case ResTable_map::ATTR_ONE:
701 plural->values[Plural::One] = std::move(item);
702 break;
703 case ResTable_map::ATTR_TWO:
704 plural->values[Plural::Two] = std::move(item);
705 break;
706 case ResTable_map::ATTR_FEW:
707 plural->values[Plural::Few] = std::move(item);
708 break;
709 case ResTable_map::ATTR_MANY:
710 plural->values[Plural::Many] = std::move(item);
711 break;
712 case ResTable_map::ATTR_OTHER:
713 plural->values[Plural::Other] = std::move(item);
714 break;
715 }
716 }
717 return plural;
718 }
719
720 } // namespace aapt
721