blob: 32876cda04fccffe18b8f3caf844328a01e6d7be [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Adam Lesinski769de982015-04-10 19:43:55 -070020#include "Resolver.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include "ResourceTable.h"
22#include "ResourceValues.h"
23#include "Source.h"
24
25#include <androidfw/ResourceTypes.h>
26#include <string>
27
28namespace aapt {
29
30struct SymbolTable_entry;
31
32/*
33 * Parses a binary resource table (resources.arsc) and adds the entries
34 * to a ResourceTable. This is different than the libandroidfw ResTable
35 * in that it scans the table from top to bottom and doesn't require
36 * support for random access. It is also able to parse non-runtime
37 * chunks and types.
38 */
39class BinaryResourceParser {
40public:
41 /*
42 * Creates a parser, which will read `len` bytes from `data`, and
43 * add any resources parsed to `table`. `source` is for logging purposes.
44 */
Adam Lesinski769de982015-04-10 19:43:55 -070045 BinaryResourceParser(const std::shared_ptr<ResourceTable>& table,
Adam Lesinski24aad162015-04-24 19:19:30 -070046 const std::shared_ptr<IResolver>& resolver,
Adam Lesinski769de982015-04-10 19:43:55 -070047 const Source& source,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048 const void* data, size_t len);
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
57private:
58 // Helper method to retrieve the symbol name for a given table offset specified
59 // as a pointer.
60 bool getSymbol(const void* data, ResourceNameRef* outSymbol);
61
62 bool parseTable(const android::ResChunk_header* chunk);
63 bool parseSymbolTable(const android::ResChunk_header* chunk);
64
65 // Looks up the resource ID in the reference and converts it to a name if available.
66 bool idToName(Reference* reference);
67
68 bool parsePackage(const android::ResChunk_header* chunk);
Adam Lesinski6ff19662015-04-30 17:40:46 -070069 bool parsePublic(const android::ResChunk_header* chunk);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070 bool parseTypeSpec(const android::ResChunk_header* chunk);
71 bool parseType(const android::ResChunk_header* chunk);
72
73 std::unique_ptr<Item> parseValue(const ResourceNameRef& name,
74 const ConfigDescription& config, const android::Res_value* value, uint16_t flags);
75
76 std::unique_ptr<Value> parseMapEntry(const ResourceNameRef& name,
77 const ConfigDescription& config, const android::ResTable_map_entry* map);
78
79 std::unique_ptr<Style> parseStyle(const ResourceNameRef& name,
80 const ConfigDescription& config, const android::ResTable_map_entry* map);
81
82 std::unique_ptr<Attribute> parseAttr(const ResourceNameRef& name,
83 const ConfigDescription& config, const android::ResTable_map_entry* map);
84
85 std::unique_ptr<Array> parseArray(const ResourceNameRef& name,
86 const ConfigDescription& config, const android::ResTable_map_entry* map);
87
88 std::unique_ptr<Plural> parsePlural(const ResourceNameRef& name,
89 const ConfigDescription& config, const android::ResTable_map_entry* map);
90
91 std::unique_ptr<Styleable> parseStyleable(const ResourceNameRef& name,
92 const ConfigDescription& config, const android::ResTable_map_entry* map);
93
94 std::shared_ptr<ResourceTable> mTable;
95
Adam Lesinski24aad162015-04-24 19:19:30 -070096 std::shared_ptr<IResolver> mResolver;
Adam Lesinski769de982015-04-10 19:43:55 -070097
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098 const Source mSource;
99
100 const void* mData;
101 const size_t mDataLen;
102
103 // The package name of the resource table.
104 std::u16string mPackage;
105
106 // The array of symbol entries. Each element points to an offset
107 // in the table and an index into the symbol table string pool.
108 const SymbolTable_entry* mSymbolEntries = nullptr;
109
110 // Number of symbol entries.
111 size_t mSymbolEntryCount = 0;
112
113 // The symbol table string pool. Holds the names of symbols
114 // referenced in this table but not defined nor resolved to an
115 // ID.
116 android::ResStringPool mSymbolPool;
117
118 // The source string pool. Resource entries may have an extra
119 // field that points into this string pool, which denotes where
120 // the resource was parsed from originally.
121 android::ResStringPool mSourcePool;
122
123 // The standard value string pool for resource values.
124 android::ResStringPool mValuePool;
125
126 // The string pool that holds the names of the types defined
127 // in this table.
128 android::ResStringPool mTypePool;
129
130 // The string pool that holds the names of the entries defined
131 // in this table.
132 android::ResStringPool mKeyPool;
133
134 // A mapping of resource ID to resource name. When we finish parsing
135 // we use this to convert all resource IDs to symbolic references.
136 std::map<ResourceId, ResourceName> mIdIndex;
137};
138
139} // namespace aapt
140
141namespace android {
142
143/**
144 * Iterator functionality for ResTable_map_entry.
145 */
146
147inline const ResTable_map* begin(const ResTable_map_entry* map) {
148 return reinterpret_cast<const ResTable_map*>(
149 reinterpret_cast<const uint8_t*>(map) + map->size);
150}
151
152inline const ResTable_map* end(const ResTable_map_entry* map) {
153 return reinterpret_cast<const ResTable_map*>(
154 reinterpret_cast<const uint8_t*>(map) + map->size) + map->count;
155}
156
157} // namespace android
158
159#endif // AAPT_BINARY_RESOURCE_PARSER_H