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