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