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