blob: dc668fdab7b687da8028a99dc066eb6ef1bc4ba7 [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 Lesinskice5e56e2016-10-21 17:56:45 -070020#include <string>
21
22#include "android-base/macros.h"
23#include "androidfw/ResourceTypes.h"
24
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include "ResourceTable.h"
26#include "ResourceValues.h"
27#include "Source.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "process/IResourceTableConsumer.h"
29#include "util/Util.h"
30
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031namespace aapt {
32
33struct 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 */
42class BinaryResourceParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 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,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 const Source& source, const void* data, size_t data_len);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 /*
52 * Parses the binary resource table and returns true if successful.
53 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 bool Parse();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 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,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 const android::ResChunk_header* chunk);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 std::unique_ptr<Item> ParseValue(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 const ConfigDescription& config,
67 const android::Res_value* value,
68 uint16_t flags);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 std::unique_ptr<Value> ParseMapEntry(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 const ConfigDescription& config,
72 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 std::unique_ptr<Style> ParseStyle(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 const ConfigDescription& config,
76 const android::ResTable_map_entry* map);
77
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::unique_ptr<Attribute> ParseAttr(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 const ConfigDescription& config,
80 const android::ResTable_map_entry* map);
81
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 std::unique_ptr<Array> ParseArray(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 const ConfigDescription& config,
84 const android::ResTable_map_entry* map);
85
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 std::unique_ptr<Plural> ParsePlural(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 const ConfigDescription& config,
Adam Lesinskie78fd612015-10-22 12:48:43 -070088 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 /**
91 * If the mapEntry is a special type that denotes meta data (source, comment),
92 * then it is
93 * read and added to the Value.
94 * Returns true if the mapEntry was meta data.
95 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 bool CollectMetaData(const android::ResTable_map& map_entry, Value* value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 IAaptContext* context_;
99 ResourceTable* table_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 const Source source_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 const void* data_;
104 const size_t data_len_;
Adam Lesinski28cacf02015-11-23 14:22:47 -0800105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 // The standard value string pool for resource values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 android::ResStringPool value_pool_;
Adam Lesinski769de982015-04-10 19:43:55 -0700108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 // The string pool that holds the names of the types defined
110 // in this table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 android::ResStringPool type_pool_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 // The string pool that holds the names of the entries defined
114 // in this table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 android::ResStringPool key_pool_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 // A mapping of resource ID to resource name. When we finish parsing
118 // we use this to convert all resource IDs to symbolic references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 std::map<ResourceId, ResourceName> id_index_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120};
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123
124namespace android {
125
126/**
127 * Iterator functionality for ResTable_map_entry.
128 */
129
130inline const ResTable_map* begin(const ResTable_map_entry* map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 return (const ResTable_map*)((const uint8_t*)map +
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 aapt::util::DeviceToHost32(map->size));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133}
134
135inline const ResTable_map* end(const ResTable_map_entry* map) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 return begin(map) + aapt::util::DeviceToHost32(map->count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137}
138
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139} // namespace android
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141#endif // AAPT_BINARY_RESOURCE_PARSER_H