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_FORMAT_BINARY_RESOURCEPARSER_H
18 #define AAPT_FORMAT_BINARY_RESOURCEPARSER_H
19 
20 #include <string>
21 
22 #include "ResourceTable.h"
23 #include "ResourceValues.h"
24 #include "android-base/macros.h"
25 #include "androidfw/ConfigDescription.h"
26 #include "androidfw/ResourceTypes.h"
27 #include "androidfw/Source.h"
28 #include "androidfw/Util.h"
29 #include "process/IResourceTableConsumer.h"
30 #include "util/Util.h"
31 
32 namespace aapt {
33 
34 struct SymbolTable_entry;
35 
36 // Parses a binary resource table (resources.arsc) and adds the entries to a ResourceTable.
37 // This is different than the libandroidfw ResTable in that it scans the table from top to bottom
38 // and doesn't require support for random access.
39 class BinaryResourceParser {
40  public:
41   // Creates a parser, which will read `len` bytes from `data`, and add any resources parsed to
42   // `table`. `source` is for logging purposes.
43   BinaryResourceParser(android::IDiagnostics* diag, ResourceTable* table,
44                        const android::Source& source, const void* data, size_t data_len,
45                        io::IFileCollection* files = nullptr);
46 
47   // Parses the binary resource table and returns true if successful.
48   bool Parse();
49 
50  private:
51   DISALLOW_COPY_AND_ASSIGN(BinaryResourceParser);
52 
53   bool ParseTable(const android::ResChunk_header* chunk);
54   bool ParsePackage(const android::ResChunk_header* chunk);
55   bool ParseTypeSpec(const ResourceTablePackage* package, const android::ResChunk_header* chunk,
56                      uint8_t package_id);
57   bool ParseType(const ResourceTablePackage* package, const android::ResChunk_header* chunk,
58                  uint8_t package_id);
59   bool ParseLibrary(const android::ResChunk_header* chunk);
60   bool ParseOverlayable(const android::ResChunk_header* chunk);
61   bool ParseStagedAliases(const android::ResChunk_header* chunk);
62 
63   std::unique_ptr<Item> ParseValue(const ResourceNameRef& name,
64                                    const android::ConfigDescription& config,
65                                    const android::Res_value& value);
66 
67   std::unique_ptr<Value> ParseMapEntry(const ResourceNameRef& name,
68                                        const android::ConfigDescription& config,
69                                        const android::ResTable_map_entry* map);
70 
71   std::unique_ptr<Style> ParseStyle(const ResourceNameRef& name,
72                                     const android::ConfigDescription& config,
73                                     const android::ResTable_map_entry* map);
74 
75   std::unique_ptr<Attribute> ParseAttr(const ResourceNameRef& name,
76                                        const android::ConfigDescription& config,
77                                        const android::ResTable_map_entry* map);
78 
79   std::unique_ptr<Array> ParseArray(const ResourceNameRef& name,
80                                     const android::ConfigDescription& config,
81                                     const android::ResTable_map_entry* map);
82 
83   std::unique_ptr<Plural> ParsePlural(const ResourceNameRef& name,
84                                       const android::ConfigDescription& config,
85                                       const android::ResTable_map_entry* map);
86 
87   /**
88    * If the mapEntry is a special type that denotes meta data (source, comment),
89    * then it is
90    * read and added to the Value.
91    * Returns true if the mapEntry was meta data.
92    */
93   bool CollectMetaData(const android::ResTable_map& map_entry, Value* value);
94 
95   android::IDiagnostics* diag_;
96   ResourceTable* table_;
97 
98   const android::Source source_;
99 
100   const void* data_;
101   const size_t data_len_;
102 
103   // Optional file collection from which to create io::IFile objects.
104   io::IFileCollection* files_;
105 
106   // The standard value string pool for resource values.
107   android::ResStringPool value_pool_;
108 
109   // The string pool that holds the names of the types defined
110   // in this table.
111   android::ResStringPool type_pool_;
112 
113   // The string pool that holds the names of the entries defined
114   // in this table.
115   android::ResStringPool key_pool_;
116 
117   // A mapping of resource ID to resource name. When we finish parsing
118   // we use this to convert all resource IDs to symbolic references.
119   std::map<ResourceId, ResourceName> id_index_;
120 
121   // A mapping of resource ID to type spec flags.
122   std::unordered_map<ResourceId, uint32_t> entry_type_spec_flags_;
123 
124   // A collection of staged resources that got finalized already and we're supposed to prune -
125   // but the original staged resource record hasn't been parsed yet.
126   std::set<std::pair<ResourceName, ResourceId>> staged_entries_to_remove_;
127 };
128 
129 }  // namespace aapt
130 
131 namespace android {
132 
133 // Iterator functionality for ResTable_map_entry.
134 
begin(const ResTable_map_entry * map)135 inline const ResTable_map* begin(const ResTable_map_entry* map) {
136   return (const ResTable_map*)((const uint8_t*)map + android::util::DeviceToHost32(map->size));
137 }
138 
end(const ResTable_map_entry * map)139 inline const ResTable_map* end(const ResTable_map_entry* map) {
140   return begin(map) + android::util::DeviceToHost32(map->count);
141 }
142 
143 }  // namespace android
144 
145 #endif  // AAPT_FORMAT_BINARY_RESOURCEPARSER_H
146