• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "process/IResourceTableConsumer.h"
25 #include "util/Util.h"
26 
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      */
47     BinaryResourceParser(IAaptContext* context, ResourceTable* table, const Source& source,
48                          const void* data, size_t dataLen);
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     bool parseTable(const android::ResChunk_header* chunk);
59     bool parsePackage(const android::ResChunk_header* chunk);
60     bool parseTypeSpec(const android::ResChunk_header* chunk);
61     bool parseType(const ResourceTablePackage* package, const android::ResChunk_header* chunk);
62 
63     std::unique_ptr<Item> parseValue(const ResourceNameRef& name, const ConfigDescription& config,
64                                      const android::Res_value* value, uint16_t flags);
65 
66     std::unique_ptr<Value> parseMapEntry(const ResourceNameRef& name,
67                                          const ConfigDescription& config,
68                                          const android::ResTable_map_entry* map);
69 
70     std::unique_ptr<Style> parseStyle(const ResourceNameRef& name, const ConfigDescription& config,
71                                       const android::ResTable_map_entry* map);
72 
73     std::unique_ptr<Attribute> parseAttr(const ResourceNameRef& name,
74                                          const ConfigDescription& config,
75                                          const android::ResTable_map_entry* map);
76 
77     std::unique_ptr<Array> parseArray(const ResourceNameRef& name, const ConfigDescription& config,
78                                       const android::ResTable_map_entry* map);
79 
80     std::unique_ptr<Plural> parsePlural(const ResourceNameRef& name,
81                                         const ConfigDescription& config,
82                                         const android::ResTable_map_entry* map);
83 
84     /**
85      * If the mapEntry is a special type that denotes meta data (source, comment), then it is
86      * read and added to the Value.
87      * Returns true if the mapEntry was meta data.
88      */
89     bool collectMetaData(const android::ResTable_map& mapEntry, Value* value);
90 
91     IAaptContext* mContext;
92     ResourceTable* mTable;
93 
94     const Source mSource;
95 
96     const void* mData;
97     const size_t mDataLen;
98 
99     // The standard value string pool for resource values.
100     android::ResStringPool mValuePool;
101 
102     // The string pool that holds the names of the types defined
103     // in this table.
104     android::ResStringPool mTypePool;
105 
106     // The string pool that holds the names of the entries defined
107     // in this table.
108     android::ResStringPool mKeyPool;
109 
110     // A mapping of resource ID to resource name. When we finish parsing
111     // we use this to convert all resource IDs to symbolic references.
112     std::map<ResourceId, ResourceName> mIdIndex;
113 };
114 
115 } // namespace aapt
116 
117 namespace android {
118 
119 /**
120  * Iterator functionality for ResTable_map_entry.
121  */
122 
begin(const ResTable_map_entry * map)123 inline const ResTable_map* begin(const ResTable_map_entry* map) {
124     return (const ResTable_map*)((const uint8_t*) map + aapt::util::deviceToHost32(map->size));
125 }
126 
end(const ResTable_map_entry * map)127 inline const ResTable_map* end(const ResTable_map_entry* map) {
128     return begin(map) + aapt::util::deviceToHost32(map->count);
129 }
130 
131 } // namespace android
132 
133 #endif // AAPT_BINARY_RESOURCE_PARSER_H
134