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 <string>
21
22 #include "android-base/macros.h"
23 #include "androidfw/ResourceTypes.h"
24
25 #include "ResourceTable.h"
26 #include "ResourceValues.h"
27 #include "Source.h"
28 #include "process/IResourceTableConsumer.h"
29 #include "util/Util.h"
30
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 {
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, const Source& source,
49 const void* data, size_t data_len, io::IFileCollection* files = nullptr);
50
51 /*
52 * Parses the binary resource table and returns true if successful.
53 */
54 bool Parse();
55
56 private:
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,
63 const android::ResChunk_header* chunk);
64 bool ParseLibrary(const android::ResChunk_header* chunk);
65
66 std::unique_ptr<Item> ParseValue(const ResourceNameRef& name, const ConfigDescription& config,
67 const android::Res_value& value);
68
69 std::unique_ptr<Value> ParseMapEntry(const ResourceNameRef& name,
70 const ConfigDescription& config,
71 const android::ResTable_map_entry* map);
72
73 std::unique_ptr<Style> ParseStyle(const ResourceNameRef& name,
74 const ConfigDescription& config,
75 const android::ResTable_map_entry* map);
76
77 std::unique_ptr<Attribute> ParseAttr(const ResourceNameRef& name,
78 const ConfigDescription& config,
79 const android::ResTable_map_entry* map);
80
81 std::unique_ptr<Array> ParseArray(const ResourceNameRef& name,
82 const ConfigDescription& config,
83 const android::ResTable_map_entry* map);
84
85 std::unique_ptr<Plural> ParsePlural(const ResourceNameRef& name,
86 const ConfigDescription& config,
87 const android::ResTable_map_entry* map);
88
89 /**
90 * If the mapEntry is a special type that denotes meta data (source, comment),
91 * then it is
92 * read and added to the Value.
93 * Returns true if the mapEntry was meta data.
94 */
95 bool CollectMetaData(const android::ResTable_map& map_entry, Value* value);
96
97 IAaptContext* context_;
98 ResourceTable* table_;
99
100 const Source source_;
101
102 const void* data_;
103 const size_t data_len_;
104
105 // Optional file collection from which to create io::IFile objects.
106 io::IFileCollection* files_;
107
108 // The standard value string pool for resource values.
109 android::ResStringPool value_pool_;
110
111 // The string pool that holds the names of the types defined
112 // in this table.
113 android::ResStringPool type_pool_;
114
115 // The string pool that holds the names of the entries defined
116 // in this table.
117 android::ResStringPool key_pool_;
118
119 // A mapping of resource ID to resource name. When we finish parsing
120 // we use this to convert all resource IDs to symbolic references.
121 std::map<ResourceId, ResourceName> id_index_;
122 };
123
124 } // namespace aapt
125
126 namespace android {
127
128 /**
129 * Iterator functionality for ResTable_map_entry.
130 */
131
begin(const ResTable_map_entry * map)132 inline const ResTable_map* begin(const ResTable_map_entry* map) {
133 return (const ResTable_map*)((const uint8_t*)map +
134 aapt::util::DeviceToHost32(map->size));
135 }
136
end(const ResTable_map_entry * map)137 inline const ResTable_map* end(const ResTable_map_entry* map) {
138 return begin(map) + aapt::util::DeviceToHost32(map->count);
139 }
140
141 } // namespace android
142
143 #endif // AAPT_BINARY_RESOURCE_PARSER_H
144