blob: 052f806e3b953aa8ede6b8ef8b8caf6a64451a59 [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
Adam Lesinski46708052017-09-29 14:49:15 -070017#ifndef AAPT_FORMAT_BINARY_RESOURCEPARSER_H
18#define AAPT_FORMAT_BINARY_RESOURCEPARSER_H
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019
Adam Lesinskice5e56e22016-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
Adam Lesinski46708052017-09-29 14:49:15 -070035// Parses a binary resource table (resources.arsc) and adds the entries to a ResourceTable.
36// This is different than the libandroidfw ResTable in that it scans the table from top to bottom
37// and doesn't require support for random access.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038class BinaryResourceParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 public:
Adam Lesinski46708052017-09-29 14:49:15 -070040 // Creates a parser, which will read `len` bytes from `data`, and add any resources parsed to
41 // `table`. `source` is for logging purposes.
Adam Lesinski8780eb62017-10-31 17:44:39 -070042 BinaryResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070043 const void* data, size_t data_len, io::IFileCollection* files = nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044
Adam Lesinski46708052017-09-29 14:49:15 -070045 // Parses the binary resource table and returns true if successful.
Adam Lesinskice5e56e22016-10-21 17:56:45 -070046 bool Parse();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 private:
Adam Lesinskice5e56e22016-10-21 17:56:45 -070049 DISALLOW_COPY_AND_ASSIGN(BinaryResourceParser);
50
51 bool ParseTable(const android::ResChunk_header* chunk);
52 bool ParsePackage(const android::ResChunk_header* chunk);
53 bool ParseTypeSpec(const android::ResChunk_header* chunk);
Adam Lesinski46708052017-09-29 14:49:15 -070054 bool ParseType(const ResourceTablePackage* package, const android::ResChunk_header* chunk);
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080055 bool ParseLibrary(const android::ResChunk_header* chunk);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinskid0f492d2017-04-03 18:12:45 -070057 std::unique_ptr<Item> ParseValue(const ResourceNameRef& name, const ConfigDescription& config,
58 const android::Res_value& value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinski46708052017-09-29 14:49:15 -070060 std::unique_ptr<Value> ParseMapEntry(const ResourceNameRef& name, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinski46708052017-09-29 14:49:15 -070063 std::unique_ptr<Style> ParseStyle(const ResourceNameRef& name, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 const android::ResTable_map_entry* map);
65
Adam Lesinski46708052017-09-29 14:49:15 -070066 std::unique_ptr<Attribute> ParseAttr(const ResourceNameRef& name, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 const android::ResTable_map_entry* map);
68
Adam Lesinski46708052017-09-29 14:49:15 -070069 std::unique_ptr<Array> ParseArray(const ResourceNameRef& name, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 const android::ResTable_map_entry* map);
71
Adam Lesinski46708052017-09-29 14:49:15 -070072 std::unique_ptr<Plural> ParsePlural(const ResourceNameRef& name, const ConfigDescription& config,
Adam Lesinskie78fd612015-10-22 12:48:43 -070073 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 /**
76 * If the mapEntry is a special type that denotes meta data (source, comment),
77 * then it is
78 * read and added to the Value.
79 * Returns true if the mapEntry was meta data.
80 */
Adam Lesinskice5e56e22016-10-21 17:56:45 -070081 bool CollectMetaData(const android::ResTable_map& map_entry, Value* value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinski8780eb62017-10-31 17:44:39 -070083 IDiagnostics* diag_;
Adam Lesinskice5e56e22016-10-21 17:56:45 -070084 ResourceTable* table_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085
Adam Lesinskice5e56e22016-10-21 17:56:45 -070086 const Source source_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskice5e56e22016-10-21 17:56:45 -070088 const void* data_;
89 const size_t data_len_;
Adam Lesinski28cacf02015-11-23 14:22:47 -080090
Adam Lesinskid0f492d2017-04-03 18:12:45 -070091 // Optional file collection from which to create io::IFile objects.
92 io::IFileCollection* files_;
93
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 // The standard value string pool for resource values.
Adam Lesinskice5e56e22016-10-21 17:56:45 -070095 android::ResStringPool value_pool_;
Adam Lesinski769de982015-04-10 19:43:55 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 // The string pool that holds the names of the types defined
98 // in this table.
Adam Lesinskice5e56e22016-10-21 17:56:45 -070099 android::ResStringPool type_pool_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 // The string pool that holds the names of the entries defined
102 // in this table.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700103 android::ResStringPool key_pool_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 // A mapping of resource ID to resource name. When we finish parsing
106 // we use this to convert all resource IDs to symbolic references.
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700107 std::map<ResourceId, ResourceName> id_index_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108};
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111
112namespace android {
113
Adam Lesinski46708052017-09-29 14:49:15 -0700114// Iterator functionality for ResTable_map_entry.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
116inline const ResTable_map* begin(const ResTable_map_entry* map) {
Adam Lesinski46708052017-09-29 14:49:15 -0700117 return (const ResTable_map*)((const uint8_t*)map + ::aapt::util::DeviceToHost32(map->size));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118}
119
120inline const ResTable_map* end(const ResTable_map_entry* map) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700121 return begin(map) + aapt::util::DeviceToHost32(map->count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122}
123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124} // namespace android
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
Adam Lesinski46708052017-09-29 14:49:15 -0700126#endif // AAPT_FORMAT_BINARY_RESOURCEPARSER_H