blob: 7309396547e6ab041da4e2c5d8502e629d9ffee9 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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#include "ConfigDescription.h"
18#include "Resource.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070019#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020
21#include "process/SymbolTable.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070022#include "util/Comparators.h"
23#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024
25#include <androidfw/AssetManager.h>
26#include <androidfw/ResourceTypes.h>
27
28namespace aapt {
29
30const ISymbolTable::Symbol* SymbolTableWrapper::findByName(const ResourceName& name) {
31 if (const std::shared_ptr<Symbol>& s = mCache.get(name)) {
32 return s.get();
33 }
34
35 Maybe<ResourceTable::SearchResult> result = mTable->findResource(name);
36 if (!result) {
37 if (name.type == ResourceType::kAttr) {
38 // Recurse and try looking up a private attribute.
Adam Lesinskie78fd612015-10-22 12:48:43 -070039 return findByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040 }
41 return {};
42 }
43
44 ResourceTable::SearchResult sr = result.value();
45
46 // If no ID exists, we treat the symbol as missing. SymbolTables are used to
47 // find symbols to link.
48 if (!sr.package->id || !sr.type->id || !sr.entry->id) {
49 return {};
50 }
51
52 std::shared_ptr<Symbol> symbol = std::make_shared<Symbol>();
Adam Lesinskie78fd612015-10-22 12:48:43 -070053 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
55 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056 const ConfigDescription kDefaultConfig;
57 auto iter = std::lower_bound(sr.entry->values.begin(), sr.entry->values.end(),
Adam Lesinskie78fd612015-10-22 12:48:43 -070058 kDefaultConfig, cmp::lessThan);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
60 if (iter != sr.entry->values.end() && iter->config == kDefaultConfig) {
61 // This resource has an Attribute.
Adam Lesinskie78fd612015-10-22 12:48:43 -070062 if (Attribute* attr = valueCast<Attribute>(iter->value.get())) {
63 symbol->attribute = std::unique_ptr<Attribute>(attr->clone(nullptr));
64 } else {
65 return {};
66 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067 }
68 }
69
70 if (name.type == ResourceType::kAttrPrivate) {
71 // Masquerade this entry as kAttr.
Adam Lesinskie78fd612015-10-22 12:48:43 -070072 mCache.put(ResourceName(name.package, ResourceType::kAttr, name.entry), symbol);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073 } else {
74 mCache.put(name, symbol);
75 }
76 return symbol.get();
77}
78
79
80static std::shared_ptr<ISymbolTable::Symbol> lookupIdInTable(const android::ResTable& table,
81 ResourceId id) {
82 android::Res_value val = {};
83 ssize_t block = table.getResource(id.id, &val, true);
84 if (block >= 0) {
85 std::shared_ptr<ISymbolTable::Symbol> s = std::make_shared<ISymbolTable::Symbol>();
86 s->id = id;
87 return s;
88 }
89
90 // Try as a bag.
91 const android::ResTable::bag_entry* entry;
92 ssize_t count = table.lockBag(id.id, &entry);
93 if (count < 0) {
94 table.unlockBag(entry);
95 return nullptr;
96 }
97
98 // We found a resource.
99 std::shared_ptr<ISymbolTable::Symbol> s = std::make_shared<ISymbolTable::Symbol>();
100 s->id = id;
101
102 // Check to see if it is an attribute.
103 for (size_t i = 0; i < (size_t) count; i++) {
104 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) {
105 s->attribute = util::make_unique<Attribute>(false);
106 s->attribute->typeMask = entry[i].map.value.data;
107 break;
108 }
109 }
110
111 if (s->attribute) {
112 for (size_t i = 0; i < (size_t) count; i++) {
113 if (!Res_INTERNALID(entry[i].map.name.ident)) {
114 android::ResTable::resource_name entryName;
115 if (!table.getResourceName(entry[i].map.name.ident, false, &entryName)) {
116 table.unlockBag(entry);
117 return nullptr;
118 }
119
120 const ResourceType* parsedType = parseResourceType(
121 StringPiece16(entryName.type, entryName.typeLen));
122 if (!parsedType) {
123 table.unlockBag(entry);
124 return nullptr;
125 }
126
127 Attribute::Symbol symbol;
128 symbol.symbol.name = ResourceNameRef(
129 StringPiece16(entryName.package, entryName.packageLen),
130 *parsedType,
131 StringPiece16(entryName.name, entryName.nameLen)).toResourceName();
132 symbol.symbol.id = ResourceId(entry[i].map.name.ident);
133 symbol.value = entry[i].map.value.data;
134 s->attribute->symbols.push_back(std::move(symbol));
135 }
136 }
137 }
138 table.unlockBag(entry);
139 return s;
140}
141
142const ISymbolTable::Symbol* AssetManagerSymbolTableBuilder::AssetManagerSymbolTable::findByName(
143 const ResourceName& name) {
144 if (const std::shared_ptr<Symbol>& s = mCache.get(name)) {
145 return s.get();
146 }
147
148 for (const auto& asset : mAssets) {
149 const android::ResTable& table = asset->getResources(false);
150 StringPiece16 typeStr = toString(name.type);
151 ResourceId resId = table.identifierForName(name.entry.data(), name.entry.size(),
152 typeStr.data(), typeStr.size(),
153 name.package.data(), name.package.size());
154 if (!resId.isValid()) {
155 continue;
156 }
157
158 std::shared_ptr<Symbol> s = lookupIdInTable(table, resId);
159 if (s) {
160 mCache.put(name, s);
161 return s.get();
162 }
163 }
164 return nullptr;
165}
166
167const ISymbolTable::Symbol* AssetManagerSymbolTableBuilder::AssetManagerSymbolTable::findById(
168 ResourceId id) {
169 if (const std::shared_ptr<Symbol>& s = mIdCache.get(id)) {
170 return s.get();
171 }
172
173 for (const auto& asset : mAssets) {
174 const android::ResTable& table = asset->getResources(false);
175
176 std::shared_ptr<Symbol> s = lookupIdInTable(table, id);
177 if (s) {
178 mIdCache.put(id, s);
179 return s.get();
180 }
181 }
182 return nullptr;
183}
184
185const ISymbolTable::Symbol* JoinedSymbolTableBuilder::JoinedSymbolTable::findByName(
186 const ResourceName& name) {
187 for (auto& symbolTable : mSymbolTables) {
188 if (const Symbol* s = symbolTable->findByName(name)) {
189 return s;
190 }
191 }
192 return {};
193}
194
195const ISymbolTable::Symbol* JoinedSymbolTableBuilder::JoinedSymbolTable::findById(ResourceId id) {
196 for (auto& symbolTable : mSymbolTables) {
197 if (const Symbol* s = symbolTable->findById(id)) {
198 return s;
199 }
200 }
201 return {};
202}
203
204} // namespace aapt