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