blob: 99f2bd4e7e311f1744b307d0b5fb3a9bd409449a [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
20#include "ResourceTable.h"
21#include "ResourceValues.h"
22#include "Source.h"
23
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024#include "process/IResourceTableConsumer.h"
25#include "util/Util.h"
26
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include <androidfw/ResourceTypes.h>
28#include <string>
29
30namespace aapt {
31
32struct SymbolTable_entry;
33
34/*
35 * Parses a binary resource table (resources.arsc) and adds the entries
36 * to a ResourceTable. This is different than the libandroidfw ResTable
37 * in that it scans the table from top to bottom and doesn't require
38 * support for random access. It is also able to parse non-runtime
39 * chunks and types.
40 */
41class BinaryResourceParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
43 /*
44 * Creates a parser, which will read `len` bytes from `data`, and
45 * add any resources parsed to `table`. `source` is for logging purposes.
46 */
47 BinaryResourceParser(IAaptContext* context, ResourceTable* table,
48 const Source& source, const void* data, size_t dataLen);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 BinaryResourceParser(const BinaryResourceParser&) = delete; // No copy.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 /*
53 * Parses the binary resource table and returns true if successful.
54 */
55 bool parse();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 private:
58 bool parseTable(const android::ResChunk_header* chunk);
59 bool parsePackage(const android::ResChunk_header* chunk);
60 bool parseTypeSpec(const android::ResChunk_header* chunk);
61 bool parseType(const ResourceTablePackage* package,
62 const android::ResChunk_header* chunk);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 std::unique_ptr<Item> parseValue(const ResourceNameRef& name,
65 const ConfigDescription& config,
66 const android::Res_value* value,
67 uint16_t flags);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 std::unique_ptr<Value> parseMapEntry(const ResourceNameRef& name,
70 const ConfigDescription& config,
71 const android::ResTable_map_entry* map);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 std::unique_ptr<Style> parseStyle(const ResourceNameRef& name,
74 const ConfigDescription& config,
75 const android::ResTable_map_entry* map);
76
77 std::unique_ptr<Attribute> parseAttr(const ResourceNameRef& name,
78 const ConfigDescription& config,
79 const android::ResTable_map_entry* map);
80
81 std::unique_ptr<Array> parseArray(const ResourceNameRef& name,
82 const ConfigDescription& config,
83 const android::ResTable_map_entry* map);
84
85 std::unique_ptr<Plural> parsePlural(const ResourceNameRef& name,
86 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 */
95 bool collectMetaData(const android::ResTable_map& mapEntry, Value* value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 IAaptContext* mContext;
98 ResourceTable* mTable;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 const Source mSource;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 const void* mData;
103 const size_t mDataLen;
Adam Lesinski28cacf02015-11-23 14:22:47 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 // The standard value string pool for resource values.
106 android::ResStringPool mValuePool;
Adam Lesinski769de982015-04-10 19:43:55 -0700107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 // The string pool that holds the names of the types defined
109 // in this table.
110 android::ResStringPool mTypePool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 // The string pool that holds the names of the entries defined
113 // in this table.
114 android::ResStringPool mKeyPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 // A mapping of resource ID to resource name. When we finish parsing
117 // we use this to convert all resource IDs to symbolic references.
118 std::map<ResourceId, ResourceName> mIdIndex;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119};
120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122
123namespace android {
124
125/**
126 * Iterator functionality for ResTable_map_entry.
127 */
128
129inline const ResTable_map* begin(const ResTable_map_entry* map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 return (const ResTable_map*)((const uint8_t*)map +
131 aapt::util::deviceToHost32(map->size));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132}
133
134inline const ResTable_map* end(const ResTable_map_entry* map) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 return begin(map) + aapt::util::deviceToHost32(map->count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136}
137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138} // namespace android
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140#endif // AAPT_BINARY_RESOURCE_PARSER_H