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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
| 22 | #include "android-base/macros.h" |
| 23 | #include "androidfw/ResourceTypes.h" |
| 24 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 25 | #include "ResourceTable.h" |
| 26 | #include "ResourceValues.h" |
| 27 | #include "Source.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "process/IResourceTableConsumer.h" |
| 29 | #include "util/Util.h" |
| 30 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 31 | namespace aapt { |
| 32 | |
| 33 | struct SymbolTable_entry; |
| 34 | |
| 35 | /* |
| 36 | * Parses a binary resource table (resources.arsc) and adds the entries |
| 37 | * to a ResourceTable. This is different than the libandroidfw ResTable |
| 38 | * in that it scans the table from top to bottom and doesn't require |
| 39 | * support for random access. It is also able to parse non-runtime |
| 40 | * chunks and types. |
| 41 | */ |
| 42 | class BinaryResourceParser { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | public: |
| 44 | /* |
| 45 | * Creates a parser, which will read `len` bytes from `data`, and |
| 46 | * add any resources parsed to `table`. `source` is for logging purposes. |
| 47 | */ |
| 48 | BinaryResourceParser(IAaptContext* context, ResourceTable* table, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | const Source& source, const void* data, size_t data_len); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Parses the binary resource table and returns true if successful. |
| 53 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | bool Parse(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | DISALLOW_COPY_AND_ASSIGN(BinaryResourceParser); |
| 58 | |
| 59 | bool ParseTable(const android::ResChunk_header* chunk); |
| 60 | bool ParsePackage(const android::ResChunk_header* chunk); |
| 61 | bool ParseTypeSpec(const android::ResChunk_header* chunk); |
| 62 | bool ParseType(const ResourceTablePackage* package, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | const android::ResChunk_header* chunk); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 64 | bool ParseLibrary(const android::ResChunk_header* chunk); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 65 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | std::unique_ptr<Item> ParseValue(const ResourceNameRef& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | const ConfigDescription& config, |
| 68 | const android::Res_value* value, |
| 69 | uint16_t flags); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | std::unique_ptr<Value> ParseMapEntry(const ResourceNameRef& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 | const ConfigDescription& config, |
| 73 | const android::ResTable_map_entry* map); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | std::unique_ptr<Style> ParseStyle(const ResourceNameRef& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | const ConfigDescription& config, |
| 77 | const android::ResTable_map_entry* map); |
| 78 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | std::unique_ptr<Attribute> ParseAttr(const ResourceNameRef& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | const ConfigDescription& config, |
| 81 | const android::ResTable_map_entry* map); |
| 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | std::unique_ptr<Array> ParseArray(const ResourceNameRef& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | const ConfigDescription& config, |
| 85 | const android::ResTable_map_entry* map); |
| 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | std::unique_ptr<Plural> ParsePlural(const ResourceNameRef& name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | const ConfigDescription& config, |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 89 | const android::ResTable_map_entry* map); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | /** |
| 92 | * If the mapEntry is a special type that denotes meta data (source, comment), |
| 93 | * then it is |
| 94 | * read and added to the Value. |
| 95 | * Returns true if the mapEntry was meta data. |
| 96 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | bool CollectMetaData(const android::ResTable_map& map_entry, Value* value); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | IAaptContext* context_; |
| 100 | ResourceTable* table_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | const Source source_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | const void* data_; |
| 105 | const size_t data_len_; |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | // The standard value string pool for resource values. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | android::ResStringPool value_pool_; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 109 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | // The string pool that holds the names of the types defined |
| 111 | // in this table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | android::ResStringPool type_pool_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 113 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | // The string pool that holds the names of the entries defined |
| 115 | // in this table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | android::ResStringPool key_pool_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 117 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | // A mapping of resource ID to resource name. When we finish parsing |
| 119 | // we use this to convert all resource IDs to symbolic references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | std::map<ResourceId, ResourceName> id_index_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | |
| 125 | namespace android { |
| 126 | |
| 127 | /** |
| 128 | * Iterator functionality for ResTable_map_entry. |
| 129 | */ |
| 130 | |
| 131 | inline const ResTable_map* begin(const ResTable_map_entry* map) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | return (const ResTable_map*)((const uint8_t*)map + |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | aapt::util::DeviceToHost32(map->size)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | inline const ResTable_map* end(const ResTable_map_entry* map) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | return begin(map) + aapt::util::DeviceToHost32(map->count); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | } // namespace android |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 141 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | #endif // AAPT_BINARY_RESOURCE_PARSER_H |