Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 | #ifndef AAPT_BINARY_RESOURCE_PARSER_H |
| 18 | #define AAPT_BINARY_RESOURCE_PARSER_H |
| 19 | |
| 20 | #include "ResourceTable.h" |
| 21 | #include "ResourceValues.h" |
| 22 | #include "Source.h" |
| 23 | |
| 24 | #include <androidfw/ResourceTypes.h> |
| 25 | #include <string> |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
| 29 | struct SymbolTable_entry; |
| 30 | |
| 31 | /* |
| 32 | * Parses a binary resource table (resources.arsc) and adds the entries |
| 33 | * to a ResourceTable. This is different than the libandroidfw ResTable |
| 34 | * in that it scans the table from top to bottom and doesn't require |
| 35 | * support for random access. It is also able to parse non-runtime |
| 36 | * chunks and types. |
| 37 | */ |
| 38 | class BinaryResourceParser { |
| 39 | public: |
| 40 | /* |
| 41 | * Creates a parser, which will read `len` bytes from `data`, and |
| 42 | * add any resources parsed to `table`. `source` is for logging purposes. |
| 43 | */ |
| 44 | BinaryResourceParser(std::shared_ptr<ResourceTable> table, const Source& source, |
| 45 | const void* data, size_t len); |
| 46 | |
| 47 | BinaryResourceParser(const BinaryResourceParser&) = delete; // No copy. |
| 48 | |
| 49 | /* |
| 50 | * Parses the binary resource table and returns true if successful. |
| 51 | */ |
| 52 | bool parse(); |
| 53 | |
| 54 | private: |
| 55 | // Helper method to retrieve the symbol name for a given table offset specified |
| 56 | // as a pointer. |
| 57 | bool getSymbol(const void* data, ResourceNameRef* outSymbol); |
| 58 | |
| 59 | bool parseTable(const android::ResChunk_header* chunk); |
| 60 | bool parseSymbolTable(const android::ResChunk_header* chunk); |
| 61 | |
| 62 | // Looks up the resource ID in the reference and converts it to a name if available. |
| 63 | bool idToName(Reference* reference); |
| 64 | |
| 65 | bool parsePackage(const android::ResChunk_header* chunk); |
| 66 | bool parseTypeSpec(const android::ResChunk_header* chunk); |
| 67 | bool parseType(const android::ResChunk_header* chunk); |
| 68 | |
| 69 | std::unique_ptr<Item> parseValue(const ResourceNameRef& name, |
| 70 | const ConfigDescription& config, const android::Res_value* value, uint16_t flags); |
| 71 | |
| 72 | std::unique_ptr<Value> parseMapEntry(const ResourceNameRef& name, |
| 73 | const ConfigDescription& config, const android::ResTable_map_entry* map); |
| 74 | |
| 75 | std::unique_ptr<Style> parseStyle(const ResourceNameRef& name, |
| 76 | const ConfigDescription& config, const android::ResTable_map_entry* map); |
| 77 | |
| 78 | std::unique_ptr<Attribute> parseAttr(const ResourceNameRef& name, |
| 79 | const ConfigDescription& config, const android::ResTable_map_entry* map); |
| 80 | |
| 81 | std::unique_ptr<Array> parseArray(const ResourceNameRef& name, |
| 82 | const ConfigDescription& config, const android::ResTable_map_entry* map); |
| 83 | |
| 84 | std::unique_ptr<Plural> parsePlural(const ResourceNameRef& name, |
| 85 | const ConfigDescription& config, const android::ResTable_map_entry* map); |
| 86 | |
| 87 | std::unique_ptr<Styleable> parseStyleable(const ResourceNameRef& name, |
| 88 | const ConfigDescription& config, const android::ResTable_map_entry* map); |
| 89 | |
| 90 | std::shared_ptr<ResourceTable> mTable; |
| 91 | |
| 92 | const Source mSource; |
| 93 | |
| 94 | const void* mData; |
| 95 | const size_t mDataLen; |
| 96 | |
| 97 | // The package name of the resource table. |
| 98 | std::u16string mPackage; |
| 99 | |
| 100 | // The array of symbol entries. Each element points to an offset |
| 101 | // in the table and an index into the symbol table string pool. |
| 102 | const SymbolTable_entry* mSymbolEntries = nullptr; |
| 103 | |
| 104 | // Number of symbol entries. |
| 105 | size_t mSymbolEntryCount = 0; |
| 106 | |
| 107 | // The symbol table string pool. Holds the names of symbols |
| 108 | // referenced in this table but not defined nor resolved to an |
| 109 | // ID. |
| 110 | android::ResStringPool mSymbolPool; |
| 111 | |
| 112 | // The source string pool. Resource entries may have an extra |
| 113 | // field that points into this string pool, which denotes where |
| 114 | // the resource was parsed from originally. |
| 115 | android::ResStringPool mSourcePool; |
| 116 | |
| 117 | // The standard value string pool for resource values. |
| 118 | android::ResStringPool mValuePool; |
| 119 | |
| 120 | // The string pool that holds the names of the types defined |
| 121 | // in this table. |
| 122 | android::ResStringPool mTypePool; |
| 123 | |
| 124 | // The string pool that holds the names of the entries defined |
| 125 | // in this table. |
| 126 | android::ResStringPool mKeyPool; |
| 127 | |
| 128 | // A mapping of resource ID to resource name. When we finish parsing |
| 129 | // we use this to convert all resource IDs to symbolic references. |
| 130 | std::map<ResourceId, ResourceName> mIdIndex; |
| 131 | }; |
| 132 | |
| 133 | } // namespace aapt |
| 134 | |
| 135 | namespace android { |
| 136 | |
| 137 | /** |
| 138 | * Iterator functionality for ResTable_map_entry. |
| 139 | */ |
| 140 | |
| 141 | inline const ResTable_map* begin(const ResTable_map_entry* map) { |
| 142 | return reinterpret_cast<const ResTable_map*>( |
| 143 | reinterpret_cast<const uint8_t*>(map) + map->size); |
| 144 | } |
| 145 | |
| 146 | inline const ResTable_map* end(const ResTable_map_entry* map) { |
| 147 | return reinterpret_cast<const ResTable_map*>( |
| 148 | reinterpret_cast<const uint8_t*>(map) + map->size) + map->count; |
| 149 | } |
| 150 | |
| 151 | } // namespace android |
| 152 | |
| 153 | #endif // AAPT_BINARY_RESOURCE_PARSER_H |