blob: c41ada09e551f1ef0038796d2d58cb3cbd258082 [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 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -070048 BinaryResourceParser(IAaptContext* context, ResourceTable* table, const Source& source,
49 const void* data, size_t data_len, io::IFileCollection* files = nullptr);
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 Lesinskid0f492d2017-04-03 18:12:45 -070066 std::unique_ptr<Item> ParseValue(const ResourceNameRef& name, const ConfigDescription& config,
67 const android::Res_value& value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 std::unique_ptr<Value> ParseMapEntry(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 const ConfigDescription& config,
71 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::unique_ptr<Style> ParseStyle(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 const ConfigDescription& config,
75 const android::ResTable_map_entry* map);
76
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 std::unique_ptr<Attribute> ParseAttr(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 const ConfigDescription& config,
79 const android::ResTable_map_entry* map);
80
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 std::unique_ptr<Array> ParseArray(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 const ConfigDescription& config,
83 const android::ResTable_map_entry* map);
84
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 std::unique_ptr<Plural> ParsePlural(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 const ConfigDescription& config,
Adam Lesinskie78fd612015-10-22 12:48:43 -070087 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 /**
90 * If the mapEntry is a special type that denotes meta data (source, comment),
91 * then it is
92 * read and added to the Value.
93 * Returns true if the mapEntry was meta data.
94 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 bool CollectMetaData(const android::ResTable_map& map_entry, Value* value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 IAaptContext* context_;
98 ResourceTable* table_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 const Source source_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 const void* data_;
103 const size_t data_len_;
Adam Lesinski28cacf02015-11-23 14:22:47 -0800104
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700105 // Optional file collection from which to create io::IFile objects.
106 io::IFileCollection* files_;
107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 // The standard value string pool for resource values.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 android::ResStringPool value_pool_;
Adam Lesinski769de982015-04-10 19:43:55 -0700110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 // The string pool that holds the names of the types defined
112 // in this table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 android::ResStringPool type_pool_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 // The string pool that holds the names of the entries defined
116 // in this table.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 android::ResStringPool key_pool_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 // A mapping of resource ID to resource name. When we finish parsing
120 // we use this to convert all resource IDs to symbolic references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 std::map<ResourceId, ResourceName> id_index_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122};
123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
126namespace android {
127
128/**
129 * Iterator functionality for ResTable_map_entry.
130 */
131
132inline const ResTable_map* begin(const ResTable_map_entry* map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 return (const ResTable_map*)((const uint8_t*)map +
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 aapt::util::DeviceToHost32(map->size));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135}
136
137inline const ResTable_map* end(const ResTable_map_entry* map) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 return begin(map) + aapt::util::DeviceToHost32(map->count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139}
140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141} // namespace android
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143#endif // AAPT_BINARY_RESOURCE_PARSER_H