blob: e3dd80212f128bb04eeb41f346c8b58c8a7ed557 [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 Lesinskiceb9b2f2017-02-16 12:05:42 -080064 bool ParseLibrary(const android::ResChunk_header* chunk);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 std::unique_ptr<Item> ParseValue(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 const ConfigDescription& config,
68 const android::Res_value* value,
69 uint16_t flags);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 std::unique_ptr<Value> ParseMapEntry(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 const ConfigDescription& config,
73 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 std::unique_ptr<Style> ParseStyle(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 const ConfigDescription& config,
77 const android::ResTable_map_entry* map);
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 std::unique_ptr<Attribute> ParseAttr(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 const ConfigDescription& config,
81 const android::ResTable_map_entry* map);
82
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 std::unique_ptr<Array> ParseArray(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 const ConfigDescription& config,
85 const android::ResTable_map_entry* map);
86
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 std::unique_ptr<Plural> ParsePlural(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 const ConfigDescription& config,
Adam Lesinskie78fd612015-10-22 12:48:43 -070089 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 /**
92 * If the mapEntry is a special type that denotes meta data (source, comment),
93 * then it is
94 * read and added to the Value.
95 * Returns true if the mapEntry was meta data.
96 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 bool CollectMetaData(const android::ResTable_map& map_entry, Value* value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 IAaptContext* context_;
100 ResourceTable* table_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 const Source source_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 const void* data_;
105 const size_t data_len_;
Adam Lesinski28cacf02015-11-23 14:22:47 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 // The standard value string pool for resource values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 android::ResStringPool value_pool_;
Adam Lesinski769de982015-04-10 19:43:55 -0700109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 // The string pool that holds the names of the types defined
111 // in this table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 android::ResStringPool type_pool_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 // The string pool that holds the names of the entries defined
115 // in this table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 android::ResStringPool key_pool_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 // A mapping of resource ID to resource name. When we finish parsing
119 // we use this to convert all resource IDs to symbolic references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 std::map<ResourceId, ResourceName> id_index_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121};
122
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124
125namespace android {
126
127/**
128 * Iterator functionality for ResTable_map_entry.
129 */
130
131inline const ResTable_map* begin(const ResTable_map_entry* map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 return (const ResTable_map*)((const uint8_t*)map +
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 aapt::util::DeviceToHost32(map->size));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134}
135
136inline const ResTable_map* end(const ResTable_map_entry* map) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 return begin(map) + aapt::util::DeviceToHost32(map->count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138}
139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140} // namespace android
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142#endif // AAPT_BINARY_RESOURCE_PARSER_H