BinaryResourceParser.cpp revision a587065721053ad54e34f484868142407d59512d
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 "ResourceTable.h" 18#include "ResourceUtils.h" 19#include "ResourceValues.h" 20#include "Source.h" 21#include "ValueVisitor.h" 22 23#include "flatten/ResourceTypeExtensions.h" 24#include "unflatten/BinaryResourceParser.h" 25#include "unflatten/ResChunkPullParser.h" 26#include "util/Util.h" 27 28#include <androidfw/ResourceTypes.h> 29#include <androidfw/TypeWrappers.h> 30#include <base/macros.h> 31 32#include <map> 33#include <string> 34 35namespace aapt { 36 37using namespace android; 38 39/* 40 * Visitor that converts a reference's resource ID to a resource name, 41 * given a mapping from resource ID to resource name. 42 */ 43class ReferenceIdToNameVisitor : public ValueVisitor { 44private: 45 const std::map<ResourceId, ResourceName>* mMapping; 46 47public: 48 using ValueVisitor::visit; 49 50 ReferenceIdToNameVisitor(const std::map<ResourceId, ResourceName>* mapping) : 51 mMapping(mapping) { 52 assert(mMapping); 53 } 54 55 void visit(Reference* reference) override { 56 if (!reference->id || !reference->id.value().isValid()) { 57 return; 58 } 59 60 ResourceId id = reference->id.value(); 61 auto cacheIter = mMapping->find(id); 62 if (cacheIter != mMapping->end()) { 63 reference->name = cacheIter->second; 64 reference->id = {}; 65 } 66 } 67}; 68 69BinaryResourceParser::BinaryResourceParser(IAaptContext* context, ResourceTable* table, 70 const Source& source, const void* data, size_t len) : 71 mContext(context), mTable(table), mSource(source), mData(data), mDataLen(len) { 72} 73 74bool BinaryResourceParser::parse() { 75 ResChunkPullParser parser(mData, mDataLen); 76 77 bool error = false; 78 while(ResChunkPullParser::isGoodEvent(parser.next())) { 79 if (parser.getChunk()->type != android::RES_TABLE_TYPE) { 80 mContext->getDiagnostics()->warn(DiagMessage(mSource) 81 << "unknown chunk of type '" 82 << (int) parser.getChunk()->type << "'"); 83 continue; 84 } 85 86 if (!parseTable(parser.getChunk())) { 87 error = true; 88 } 89 } 90 91 if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { 92 mContext->getDiagnostics()->error(DiagMessage(mSource) 93 << "corrupt resource table: " 94 << parser.getLastError()); 95 return false; 96 } 97 return !error; 98} 99 100Maybe<Reference> BinaryResourceParser::getSymbol(const void* data) { 101 if (!mSymbolEntries || mSymbolEntryCount == 0) { 102 return {}; 103 } 104 105 if ((uintptr_t) data < (uintptr_t) mData) { 106 return {}; 107 } 108 109 // We only support 32 bit offsets right now. 110 const uintptr_t offset = (uintptr_t) data - (uintptr_t) mData; 111 if (offset > std::numeric_limits<uint32_t>::max()) { 112 return {}; 113 } 114 115 for (size_t i = 0; i < mSymbolEntryCount; i++) { 116 if (util::deviceToHost32(mSymbolEntries[i].offset) == offset) { 117 // This offset is a symbol! 118 const StringPiece16 str = util::getString( 119 mSymbolPool, util::deviceToHost32(mSymbolEntries[i].name.index)); 120 121 ResourceNameRef nameRef; 122 bool privateRef = false; 123 if (!ResourceUtils::parseResourceName(str, &nameRef, &privateRef)) { 124 return {}; 125 } 126 127 // Since we scan the symbol table in order, we can start looking for the 128 // next symbol from this point. 129 mSymbolEntryCount -= i + 1; 130 mSymbolEntries += i + 1; 131 132 Reference ref(nameRef); 133 ref.privateReference = privateRef; 134 return Maybe<Reference>(std::move(ref)); 135 } 136 } 137 return {}; 138} 139 140/** 141 * Parses the SymbolTable_header, which is present on non-final resource tables 142 * after the compile phase. 143 * 144 * | SymbolTable_header | 145 * |--------------------| 146 * |SymbolTable_entry 0 | 147 * |SymbolTable_entry 1 | 148 * | ... | 149 * |SymbolTable_entry n | 150 * |--------------------| 151 * 152 */ 153bool BinaryResourceParser::parseSymbolTable(const ResChunk_header* chunk) { 154 const SymbolTable_header* header = convertTo<SymbolTable_header>(chunk); 155 if (!header) { 156 mContext->getDiagnostics()->error(DiagMessage(mSource) 157 << "corrupt SymbolTable_header"); 158 return false; 159 } 160 161 const uint32_t entrySizeBytes = 162 util::deviceToHost32(header->count) * sizeof(SymbolTable_entry); 163 if (entrySizeBytes > getChunkDataLen(&header->header)) { 164 mContext->getDiagnostics()->error(DiagMessage(mSource) 165 << "SymbolTable_header data section too long"); 166 return false; 167 } 168 169 mSymbolEntries = (const SymbolTable_entry*) getChunkData(&header->header); 170 mSymbolEntryCount = util::deviceToHost32(header->count); 171 172 // Skip over the symbol entries and parse the StringPool chunk that should be next. 173 ResChunkPullParser parser(getChunkData(&header->header) + entrySizeBytes, 174 getChunkDataLen(&header->header) - entrySizeBytes); 175 if (!ResChunkPullParser::isGoodEvent(parser.next())) { 176 mContext->getDiagnostics()->error(DiagMessage(mSource) 177 << "failed to parse chunk in SymbolTable: " 178 << parser.getLastError()); 179 return false; 180 } 181 182 const ResChunk_header* nextChunk = parser.getChunk(); 183 if (util::deviceToHost16(nextChunk->type) != android::RES_STRING_POOL_TYPE) { 184 mContext->getDiagnostics()->error(DiagMessage(mSource) 185 << "expected string pool in SymbolTable but got " 186 << "chunk of type " 187 << (int) util::deviceToHost16(nextChunk->type)); 188 return false; 189 } 190 191 if (mSymbolPool.setTo(nextChunk, util::deviceToHost32(nextChunk->size)) != NO_ERROR) { 192 mContext->getDiagnostics()->error(DiagMessage(mSource) 193 << "corrupt string pool in SymbolTable: " 194 << mSymbolPool.getError()); 195 return false; 196 } 197 return true; 198} 199 200/** 201 * Parses the resource table, which contains all the packages, types, and entries. 202 */ 203bool BinaryResourceParser::parseTable(const ResChunk_header* chunk) { 204 const ResTable_header* tableHeader = convertTo<ResTable_header>(chunk); 205 if (!tableHeader) { 206 mContext->getDiagnostics()->error(DiagMessage(mSource) << "corrupt ResTable_header chunk"); 207 return false; 208 } 209 210 ResChunkPullParser parser(getChunkData(&tableHeader->header), 211 getChunkDataLen(&tableHeader->header)); 212 while (ResChunkPullParser::isGoodEvent(parser.next())) { 213 switch (util::deviceToHost16(parser.getChunk()->type)) { 214 case android::RES_STRING_POOL_TYPE: 215 if (mValuePool.getError() == NO_INIT) { 216 status_t err = mValuePool.setTo(parser.getChunk(), 217 util::deviceToHost32(parser.getChunk()->size)); 218 if (err != NO_ERROR) { 219 mContext->getDiagnostics()->error(DiagMessage(mSource) 220 << "corrupt string pool in ResTable: " 221 << mValuePool.getError()); 222 return false; 223 } 224 225 // Reserve some space for the strings we are going to add. 226 mTable->stringPool.hintWillAdd(mValuePool.size(), mValuePool.styleCount()); 227 } else { 228 mContext->getDiagnostics()->warn(DiagMessage(mSource) 229 << "unexpected string pool in ResTable"); 230 } 231 break; 232 233 case RES_TABLE_SYMBOL_TABLE_TYPE: 234 if (!parseSymbolTable(parser.getChunk())) { 235 return false; 236 } 237 break; 238 239 case RES_TABLE_SOURCE_POOL_TYPE: { 240 status_t err = mSourcePool.setTo(getChunkData(parser.getChunk()), 241 getChunkDataLen(parser.getChunk())); 242 if (err != NO_ERROR) { 243 mContext->getDiagnostics()->error(DiagMessage(mSource) 244 << "corrupt source string pool in ResTable: " 245 << mSourcePool.getError()); 246 return false; 247 } 248 break; 249 } 250 251 case android::RES_TABLE_PACKAGE_TYPE: 252 if (!parsePackage(parser.getChunk())) { 253 return false; 254 } 255 break; 256 257 default: 258 mContext->getDiagnostics() 259 ->warn(DiagMessage(mSource) 260 << "unexpected chunk type " 261 << (int) util::deviceToHost16(parser.getChunk()->type)); 262 break; 263 } 264 } 265 266 if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { 267 mContext->getDiagnostics()->error(DiagMessage(mSource) 268 << "corrupt resource table: " << parser.getLastError()); 269 return false; 270 } 271 return true; 272} 273 274 275bool BinaryResourceParser::parsePackage(const ResChunk_header* chunk) { 276 const ResTable_package* packageHeader = convertTo<ResTable_package>(chunk); 277 if (!packageHeader) { 278 mContext->getDiagnostics()->error(DiagMessage(mSource) 279 << "corrupt ResTable_package chunk"); 280 return false; 281 } 282 283 uint32_t packageId = util::deviceToHost32(packageHeader->id); 284 if (packageId > std::numeric_limits<uint8_t>::max()) { 285 mContext->getDiagnostics()->error(DiagMessage(mSource) 286 << "package ID is too big (" << packageId << ")"); 287 return false; 288 } 289 290 // Extract the package name. 291 size_t len = strnlen16((const char16_t*) packageHeader->name, arraysize(packageHeader->name)); 292 std::u16string packageName; 293 packageName.resize(len); 294 for (size_t i = 0; i < len; i++) { 295 packageName[i] = util::deviceToHost16(packageHeader->name[i]); 296 } 297 298 ResourceTablePackage* package = mTable->createPackage(packageName, (uint8_t) packageId); 299 if (!package) { 300 mContext->getDiagnostics()->error(DiagMessage(mSource) 301 << "incompatible package '" << packageName 302 << "' with ID " << packageId); 303 return false; 304 } 305 306 // There can be multiple packages in a table, so 307 // clear the type and key pool in case they were set from a previous package. 308 mTypePool.uninit(); 309 mKeyPool.uninit(); 310 311 ResChunkPullParser parser(getChunkData(&packageHeader->header), 312 getChunkDataLen(&packageHeader->header)); 313 while (ResChunkPullParser::isGoodEvent(parser.next())) { 314 switch (util::deviceToHost16(parser.getChunk()->type)) { 315 case android::RES_STRING_POOL_TYPE: 316 if (mTypePool.getError() == NO_INIT) { 317 status_t err = mTypePool.setTo(parser.getChunk(), 318 util::deviceToHost32(parser.getChunk()->size)); 319 if (err != NO_ERROR) { 320 mContext->getDiagnostics()->error(DiagMessage(mSource) 321 << "corrupt type string pool in " 322 << "ResTable_package: " 323 << mTypePool.getError()); 324 return false; 325 } 326 } else if (mKeyPool.getError() == NO_INIT) { 327 status_t err = mKeyPool.setTo(parser.getChunk(), 328 util::deviceToHost32(parser.getChunk()->size)); 329 if (err != NO_ERROR) { 330 mContext->getDiagnostics()->error(DiagMessage(mSource) 331 << "corrupt key string pool in " 332 << "ResTable_package: " 333 << mKeyPool.getError()); 334 return false; 335 } 336 } else { 337 mContext->getDiagnostics()->warn(DiagMessage(mSource) << "unexpected string pool"); 338 } 339 break; 340 341 case android::RES_TABLE_TYPE_SPEC_TYPE: 342 if (!parseTypeSpec(parser.getChunk())) { 343 return false; 344 } 345 break; 346 347 case android::RES_TABLE_TYPE_TYPE: 348 if (!parseType(package, parser.getChunk())) { 349 return false; 350 } 351 break; 352 353 case RES_TABLE_PUBLIC_TYPE: 354 if (!parsePublic(package, parser.getChunk())) { 355 return false; 356 } 357 break; 358 359 default: 360 mContext->getDiagnostics() 361 ->warn(DiagMessage(mSource) 362 << "unexpected chunk type " 363 << (int) util::deviceToHost16(parser.getChunk()->type)); 364 break; 365 } 366 } 367 368 if (parser.getEvent() == ResChunkPullParser::Event::BadDocument) { 369 mContext->getDiagnostics()->error(DiagMessage(mSource) 370 << "corrupt ResTable_package: " 371 << parser.getLastError()); 372 return false; 373 } 374 375 // Now go through the table and change local resource ID references to 376 // symbolic references. 377 ReferenceIdToNameVisitor visitor(&mIdIndex); 378 for (auto& package : mTable->packages) { 379 for (auto& type : package->types) { 380 for (auto& entry : type->entries) { 381 for (auto& configValue : entry->values) { 382 configValue.value->accept(&visitor); 383 } 384 } 385 } 386 } 387 return true; 388} 389 390bool BinaryResourceParser::parsePublic(const ResourceTablePackage* package, 391 const ResChunk_header* chunk) { 392 const Public_header* header = convertTo<Public_header>(chunk); 393 if (!header) { 394 mContext->getDiagnostics()->error(DiagMessage(mSource) 395 << "corrupt Public_header chunk"); 396 return false; 397 } 398 399 if (header->typeId == 0) { 400 mContext->getDiagnostics()->error(DiagMessage(mSource) 401 << "invalid type ID " 402 << (int) header->typeId); 403 return false; 404 } 405 406 StringPiece16 typeStr16 = util::getString(mTypePool, header->typeId - 1); 407 const ResourceType* parsedType = parseResourceType(typeStr16); 408 if (!parsedType) { 409 mContext->getDiagnostics()->error(DiagMessage(mSource) 410 << "invalid type '" << typeStr16 << "'"); 411 return false; 412 } 413 414 const uintptr_t chunkEnd = (uintptr_t) chunk + util::deviceToHost32(chunk->size); 415 const Public_entry* entry = (const Public_entry*) getChunkData(&header->header); 416 for (uint32_t i = 0; i < util::deviceToHost32(header->count); i++) { 417 if ((uintptr_t) entry + sizeof(*entry) > chunkEnd) { 418 mContext->getDiagnostics()->error(DiagMessage(mSource) 419 << "Public_entry data section is too long"); 420 return false; 421 } 422 423 const ResourceId resId(package->id.value(), header->typeId, 424 util::deviceToHost16(entry->entryId)); 425 426 const ResourceName name(package->name, *parsedType, 427 util::getString(mKeyPool, entry->key.index).toString()); 428 429 Symbol symbol; 430 if (mSourcePool.getError() == NO_ERROR) { 431 symbol.source.path = util::utf16ToUtf8(util::getString( 432 mSourcePool, util::deviceToHost32(entry->source.path.index))); 433 symbol.source.line = util::deviceToHost32(entry->source.line); 434 } 435 436 StringPiece16 comment = util::getString(mSourcePool, 437 util::deviceToHost32(entry->source.comment.index)); 438 if (!comment.empty()) { 439 symbol.comment = comment.toString(); 440 } 441 442 switch (util::deviceToHost16(entry->state)) { 443 case Public_entry::kPrivate: 444 symbol.state = SymbolState::kPrivate; 445 break; 446 447 case Public_entry::kPublic: 448 symbol.state = SymbolState::kPublic; 449 break; 450 } 451 452 if (!mTable->setSymbolStateAllowMangled(name, resId, symbol, mContext->getDiagnostics())) { 453 return false; 454 } 455 456 // Add this resource name->id mapping to the index so 457 // that we can resolve all ID references to name references. 458 auto cacheIter = mIdIndex.find(resId); 459 if (cacheIter == mIdIndex.end()) { 460 mIdIndex.insert({ resId, name }); 461 } 462 463 entry++; 464 } 465 return true; 466} 467 468bool BinaryResourceParser::parseTypeSpec(const ResChunk_header* chunk) { 469 if (mTypePool.getError() != NO_ERROR) { 470 mContext->getDiagnostics()->error(DiagMessage(mSource) 471 << "missing type string pool"); 472 return false; 473 } 474 475 const ResTable_typeSpec* typeSpec = convertTo<ResTable_typeSpec>(chunk); 476 if (!typeSpec) { 477 mContext->getDiagnostics()->error(DiagMessage(mSource) 478 << "corrupt ResTable_typeSpec chunk"); 479 return false; 480 } 481 482 if (typeSpec->id == 0) { 483 mContext->getDiagnostics()->error(DiagMessage(mSource) 484 << "ResTable_typeSpec has invalid id: " << typeSpec->id); 485 return false; 486 } 487 return true; 488} 489 490bool BinaryResourceParser::parseType(const ResourceTablePackage* package, 491 const ResChunk_header* chunk) { 492 if (mTypePool.getError() != NO_ERROR) { 493 mContext->getDiagnostics()->error(DiagMessage(mSource) 494 << "missing type string pool"); 495 return false; 496 } 497 498 if (mKeyPool.getError() != NO_ERROR) { 499 mContext->getDiagnostics()->error(DiagMessage(mSource) 500 << "missing key string pool"); 501 return false; 502 } 503 504 const ResTable_type* type = convertTo<ResTable_type>(chunk); 505 if (!type) { 506 mContext->getDiagnostics()->error(DiagMessage(mSource) 507 << "corrupt ResTable_type chunk"); 508 return false; 509 } 510 511 if (type->id == 0) { 512 mContext->getDiagnostics()->error(DiagMessage(mSource) 513 << "ResTable_type has invalid id: " << (int) type->id); 514 return false; 515 } 516 517 ConfigDescription config; 518 config.copyFromDtoH(type->config); 519 520 StringPiece16 typeStr16 = util::getString(mTypePool, type->id - 1); 521 522 const ResourceType* parsedType = parseResourceType(typeStr16); 523 if (!parsedType) { 524 mContext->getDiagnostics()->error(DiagMessage(mSource) 525 << "invalid type name '" << typeStr16 526 << "' for type with ID " << (int) type->id); 527 return false; 528 } 529 530 TypeVariant tv(type); 531 for (auto it = tv.beginEntries(); it != tv.endEntries(); ++it) { 532 const ResTable_entry* entry = *it; 533 if (!entry) { 534 continue; 535 } 536 537 const ResourceName name(package->name, *parsedType, 538 util::getString(mKeyPool, 539 util::deviceToHost32(entry->key.index)).toString()); 540 541 const ResourceId resId(package->id.value(), type->id, static_cast<uint16_t>(it.index())); 542 543 std::unique_ptr<Value> resourceValue; 544 const ResTable_entry_source* sourceBlock = nullptr; 545 546 if (entry->flags & ResTable_entry::FLAG_COMPLEX) { 547 const ResTable_map_entry* mapEntry = static_cast<const ResTable_map_entry*>(entry); 548 if (util::deviceToHost32(mapEntry->size) - sizeof(*mapEntry) == sizeof(*sourceBlock)) { 549 const uint8_t* data = (const uint8_t*) mapEntry; 550 data += util::deviceToHost32(mapEntry->size) - sizeof(*sourceBlock); 551 sourceBlock = (const ResTable_entry_source*) data; 552 } 553 554 // TODO(adamlesinski): Check that the entry count is valid. 555 resourceValue = parseMapEntry(name, config, mapEntry); 556 } else { 557 if (util::deviceToHost32(entry->size) - sizeof(*entry) == sizeof(*sourceBlock)) { 558 const uint8_t* data = (const uint8_t*) entry; 559 data += util::deviceToHost32(entry->size) - sizeof(*sourceBlock); 560 sourceBlock = (const ResTable_entry_source*) data; 561 } 562 563 const Res_value* value = (const Res_value*)( 564 (const uint8_t*) entry + util::deviceToHost32(entry->size)); 565 resourceValue = parseValue(name, config, value, entry->flags); 566 } 567 568 if (!resourceValue) { 569 mContext->getDiagnostics()->error(DiagMessage(mSource) 570 << "failed to parse value for resource " << name 571 << " (" << resId << ") with configuration '" 572 << config << "'"); 573 return false; 574 } 575 576 Source source = mSource; 577 if (sourceBlock) { 578 size_t len; 579 const char* str = mSourcePool.string8At(util::deviceToHost32(sourceBlock->path.index), 580 &len); 581 if (str) { 582 source.path.assign(str, len); 583 } 584 source.line = util::deviceToHost32(sourceBlock->line); 585 } 586 587 StringPiece16 comment = util::getString(mSourcePool, 588 util::deviceToHost32(sourceBlock->comment.index)); 589 if (!comment.empty()) { 590 resourceValue->setComment(comment); 591 } 592 593 resourceValue->setSource(source); 594 if (!mTable->addResourceAllowMangled(name, config, std::move(resourceValue), 595 mContext->getDiagnostics())) { 596 return false; 597 } 598 599 if ((entry->flags & ResTable_entry::FLAG_PUBLIC) != 0) { 600 Symbol symbol; 601 symbol.state = SymbolState::kPublic; 602 symbol.source = mSource.withLine(0); 603 if (!mTable->setSymbolStateAllowMangled(name, resId, symbol, 604 mContext->getDiagnostics())) { 605 return false; 606 } 607 } 608 609 // Add this resource name->id mapping to the index so 610 // that we can resolve all ID references to name references. 611 auto cacheIter = mIdIndex.find(resId); 612 if (cacheIter == mIdIndex.end()) { 613 mIdIndex.insert({ resId, name }); 614 } 615 } 616 return true; 617} 618 619std::unique_ptr<Item> BinaryResourceParser::parseValue(const ResourceNameRef& name, 620 const ConfigDescription& config, 621 const Res_value* value, 622 uint16_t flags) { 623 if (name.type == ResourceType::kId) { 624 return util::make_unique<Id>(); 625 } 626 627 const uint32_t data = util::deviceToHost32(value->data); 628 629 if (value->dataType == Res_value::TYPE_STRING) { 630 StringPiece16 str = util::getString(mValuePool, data); 631 632 const ResStringPool_span* spans = mValuePool.styleAt(data); 633 634 // Check if the string has a valid style associated with it. 635 if (spans != nullptr && spans->name.index != ResStringPool_span::END) { 636 StyleString styleStr = { str.toString() }; 637 while (spans->name.index != ResStringPool_span::END) { 638 styleStr.spans.push_back(Span{ 639 util::getString(mValuePool, spans->name.index).toString(), 640 spans->firstChar, 641 spans->lastChar 642 }); 643 spans++; 644 } 645 return util::make_unique<StyledString>(mTable->stringPool.makeRef( 646 styleStr, StringPool::Context{1, config})); 647 } else { 648 if (name.type != ResourceType::kString && 649 util::stringStartsWith<char16_t>(str, u"res/")) { 650 // This must be a FileReference. 651 return util::make_unique<FileReference>(mTable->stringPool.makeRef( 652 str, StringPool::Context{ 0, config })); 653 } 654 655 // There are no styles associated with this string, so treat it as 656 // a simple string. 657 return util::make_unique<String>(mTable->stringPool.makeRef( 658 str, StringPool::Context{1, config})); 659 } 660 } 661 662 if (value->dataType == Res_value::TYPE_REFERENCE || 663 value->dataType == Res_value::TYPE_ATTRIBUTE) { 664 const Reference::Type type = (value->dataType == Res_value::TYPE_REFERENCE) ? 665 Reference::Type::kResource : Reference::Type::kAttribute; 666 667 if (data != 0) { 668 // This is a normal reference. 669 return util::make_unique<Reference>(data, type); 670 } 671 672 // This reference has an invalid ID. Check if it is an unresolved symbol. 673 if (Maybe<Reference> ref = getSymbol(&value->data)) { 674 ref.value().referenceType = type; 675 return util::make_unique<Reference>(std::move(ref.value())); 676 } 677 678 // This is not an unresolved symbol, so it must be the magic @null reference. 679 Res_value nullType = {}; 680 nullType.dataType = Res_value::TYPE_REFERENCE; 681 return util::make_unique<BinaryPrimitive>(nullType); 682 } 683 684 if (value->dataType == ExtendedTypes::TYPE_RAW_STRING) { 685 return util::make_unique<RawString>(mTable->stringPool.makeRef( 686 util::getString(mValuePool, data), StringPool::Context{ 1, config })); 687 } 688 689 // Treat this as a raw binary primitive. 690 return util::make_unique<BinaryPrimitive>(*value); 691} 692 693std::unique_ptr<Value> BinaryResourceParser::parseMapEntry(const ResourceNameRef& name, 694 const ConfigDescription& config, 695 const ResTable_map_entry* map) { 696 switch (name.type) { 697 case ResourceType::kStyle: 698 return parseStyle(name, config, map); 699 case ResourceType::kAttrPrivate: 700 // fallthrough 701 case ResourceType::kAttr: 702 return parseAttr(name, config, map); 703 case ResourceType::kArray: 704 return parseArray(name, config, map); 705 case ResourceType::kStyleable: 706 return parseStyleable(name, config, map); 707 case ResourceType::kPlurals: 708 return parsePlural(name, config, map); 709 default: 710 assert(false && "unknown map type"); 711 break; 712 } 713 return {}; 714} 715 716std::unique_ptr<Style> BinaryResourceParser::parseStyle(const ResourceNameRef& name, 717 const ConfigDescription& config, 718 const ResTable_map_entry* map) { 719 std::unique_ptr<Style> style = util::make_unique<Style>(); 720 if (util::deviceToHost32(map->parent.ident) == 0) { 721 // The parent is either not set or it is an unresolved symbol. 722 // Check to see if it is a symbol. 723 style->parent = getSymbol(&map->parent.ident); 724 725 } else { 726 // The parent is a regular reference to a resource. 727 style->parent = Reference(util::deviceToHost32(map->parent.ident)); 728 } 729 730 for (const ResTable_map& mapEntry : map) { 731 style->entries.emplace_back(); 732 Style::Entry& styleEntry = style->entries.back(); 733 734 if (util::deviceToHost32(mapEntry.name.ident) == 0) { 735 // The map entry's key (attribute) is not set. This must be 736 // a symbol reference, so resolve it. 737 Maybe<Reference> symbol = getSymbol(&mapEntry.name.ident); 738 if (!symbol) { 739 mContext->getDiagnostics()->error(DiagMessage(mSource) 740 << "unresolved style attribute"); 741 return {}; 742 } 743 styleEntry.key = std::move(symbol.value()); 744 745 } else { 746 // The map entry's key (attribute) is a regular reference. 747 styleEntry.key.id = ResourceId(util::deviceToHost32(mapEntry.name.ident)); 748 } 749 750 // Parse the attribute's value. 751 styleEntry.value = parseValue(name, config, &mapEntry.value, 0); 752 if (!styleEntry.value) { 753 return {}; 754 } 755 } 756 return style; 757} 758 759std::unique_ptr<Attribute> BinaryResourceParser::parseAttr(const ResourceNameRef& name, 760 const ConfigDescription& config, 761 const ResTable_map_entry* map) { 762 const bool isWeak = (util::deviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0; 763 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(isWeak); 764 765 // First we must discover what type of attribute this is. Find the type mask. 766 auto typeMaskIter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool { 767 return util::deviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE; 768 }); 769 770 if (typeMaskIter != end(map)) { 771 attr->typeMask = util::deviceToHost32(typeMaskIter->value.data); 772 } 773 774 for (const ResTable_map& mapEntry : map) { 775 if (Res_INTERNALID(util::deviceToHost32(mapEntry.name.ident))) { 776 switch (util::deviceToHost32(mapEntry.name.ident)) { 777 case ResTable_map::ATTR_MIN: 778 attr->minInt = static_cast<int32_t>(mapEntry.value.data); 779 break; 780 case ResTable_map::ATTR_MAX: 781 attr->maxInt = static_cast<int32_t>(mapEntry.value.data); 782 break; 783 } 784 continue; 785 } 786 787 if (attr->typeMask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) { 788 Attribute::Symbol symbol; 789 symbol.value = util::deviceToHost32(mapEntry.value.data); 790 if (util::deviceToHost32(mapEntry.name.ident) == 0) { 791 // The map entry's key (id) is not set. This must be 792 // a symbol reference, so resolve it. 793 Maybe<Reference> ref = getSymbol(&mapEntry.name.ident); 794 if (!ref) { 795 mContext->getDiagnostics()->error(DiagMessage(mSource) 796 << "unresolved attribute symbol"); 797 return {}; 798 } 799 symbol.symbol = std::move(ref.value()); 800 801 } else { 802 // The map entry's key (id) is a regular reference. 803 symbol.symbol.id = ResourceId(util::deviceToHost32(mapEntry.name.ident)); 804 } 805 806 attr->symbols.push_back(std::move(symbol)); 807 } 808 } 809 810 // TODO(adamlesinski): Find i80n, attributes. 811 return attr; 812} 813 814std::unique_ptr<Array> BinaryResourceParser::parseArray(const ResourceNameRef& name, 815 const ConfigDescription& config, 816 const ResTable_map_entry* map) { 817 std::unique_ptr<Array> array = util::make_unique<Array>(); 818 for (const ResTable_map& mapEntry : map) { 819 array->items.push_back(parseValue(name, config, &mapEntry.value, 0)); 820 } 821 return array; 822} 823 824std::unique_ptr<Styleable> BinaryResourceParser::parseStyleable(const ResourceNameRef& name, 825 const ConfigDescription& config, 826 const ResTable_map_entry* map) { 827 std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>(); 828 for (const ResTable_map& mapEntry : map) { 829 if (util::deviceToHost32(mapEntry.name.ident) == 0) { 830 // The map entry's key (attribute) is not set. This must be 831 // a symbol reference, so resolve it. 832 Maybe<Reference> ref = getSymbol(&mapEntry.name.ident); 833 if (!ref) { 834 mContext->getDiagnostics()->error(DiagMessage(mSource) 835 << "unresolved styleable symbol"); 836 return {}; 837 } 838 styleable->entries.emplace_back(std::move(ref.value())); 839 840 } else { 841 // The map entry's key (attribute) is a regular reference. 842 styleable->entries.emplace_back(util::deviceToHost32(mapEntry.name.ident)); 843 } 844 } 845 return styleable; 846} 847 848std::unique_ptr<Plural> BinaryResourceParser::parsePlural(const ResourceNameRef& name, 849 const ConfigDescription& config, 850 const ResTable_map_entry* map) { 851 std::unique_ptr<Plural> plural = util::make_unique<Plural>(); 852 for (const ResTable_map& mapEntry : map) { 853 std::unique_ptr<Item> item = parseValue(name, config, &mapEntry.value, 0); 854 if (!item) { 855 return {}; 856 } 857 858 switch (util::deviceToHost32(mapEntry.name.ident)) { 859 case ResTable_map::ATTR_ZERO: 860 plural->values[Plural::Zero] = std::move(item); 861 break; 862 case ResTable_map::ATTR_ONE: 863 plural->values[Plural::One] = std::move(item); 864 break; 865 case ResTable_map::ATTR_TWO: 866 plural->values[Plural::Two] = std::move(item); 867 break; 868 case ResTable_map::ATTR_FEW: 869 plural->values[Plural::Few] = std::move(item); 870 break; 871 case ResTable_map::ATTR_MANY: 872 plural->values[Plural::Many] = std::move(item); 873 break; 874 case ResTable_map::ATTR_OTHER: 875 plural->values[Plural::Other] = std::move(item); 876 break; 877 } 878 } 879 return plural; 880} 881 882} // namespace aapt 883