blob: 6ad2f9c10d229f394addb94cea86394824baa333 [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 Lesinski467f1712015-11-16 17:35:44 -080054 symbol->isPublic = (sr.entry->symbolStatus.state == SymbolState::kPublic);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055
56 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057 const ConfigDescription kDefaultConfig;
58 auto iter = std::lower_bound(sr.entry->values.begin(), sr.entry->values.end(),
Adam Lesinskib274e352015-11-06 15:14:35 -080059 kDefaultConfig, cmp::lessThanConfig);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060
61 if (iter != sr.entry->values.end() && iter->config == kDefaultConfig) {
62 // This resource has an Attribute.
Adam Lesinskie78fd612015-10-22 12:48:43 -070063 if (Attribute* attr = valueCast<Attribute>(iter->value.get())) {
Adam Lesinskia5870652015-11-20 15:32:30 -080064 symbol->attribute = util::make_unique<Attribute>(*attr);
Adam Lesinskie78fd612015-10-22 12:48:43 -070065 } else {
66 return {};
67 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068 }
69 }
70
71 if (name.type == ResourceType::kAttrPrivate) {
72 // Masquerade this entry as kAttr.
Adam Lesinskie78fd612015-10-22 12:48:43 -070073 mCache.put(ResourceName(name.package, ResourceType::kAttr, name.entry), symbol);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074 } else {
75 mCache.put(name, symbol);
76 }
77 return symbol.get();
78}
79
Adam Lesinskie352b992015-11-16 11:59:14 -080080static std::shared_ptr<ISymbolTable::Symbol> lookupAttributeInTable(const android::ResTable& table,
81 ResourceId id) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 // Try as a bag.
83 const android::ResTable::bag_entry* entry;
84 ssize_t count = table.lockBag(id.id, &entry);
85 if (count < 0) {
86 table.unlockBag(entry);
87 return nullptr;
88 }
89
90 // We found a resource.
91 std::shared_ptr<ISymbolTable::Symbol> s = std::make_shared<ISymbolTable::Symbol>();
92 s->id = id;
93
94 // Check to see if it is an attribute.
95 for (size_t i = 0; i < (size_t) count; i++) {
96 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) {
97 s->attribute = util::make_unique<Attribute>(false);
98 s->attribute->typeMask = entry[i].map.value.data;
99 break;
100 }
101 }
102
103 if (s->attribute) {
104 for (size_t i = 0; i < (size_t) count; i++) {
Adam Lesinskia5870652015-11-20 15:32:30 -0800105 const android::ResTable_map& mapEntry = entry[i].map;
106 if (Res_INTERNALID(mapEntry.name.ident)) {
107 switch (mapEntry.name.ident) {
108 case android::ResTable_map::ATTR_MIN:
109 s->attribute->minInt = static_cast<int32_t>(mapEntry.value.data);
110 break;
111 case android::ResTable_map::ATTR_MAX:
112 s->attribute->maxInt = static_cast<int32_t>(mapEntry.value.data);
113 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800115 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800117
118 android::ResTable::resource_name entryName;
119 if (!table.getResourceName(mapEntry.name.ident, false, &entryName)) {
120 table.unlockBag(entry);
121 return nullptr;
122 }
123
124 const ResourceType* parsedType = parseResourceType(
125 StringPiece16(entryName.type, entryName.typeLen));
126 if (!parsedType) {
127 table.unlockBag(entry);
128 return nullptr;
129 }
130
131 Attribute::Symbol symbol;
132 symbol.symbol.name = ResourceName(
133 StringPiece16(entryName.package, entryName.packageLen),
134 *parsedType,
135 StringPiece16(entryName.name, entryName.nameLen));
136 symbol.symbol.id = ResourceId(mapEntry.name.ident);
137 symbol.value = mapEntry.value.data;
138 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 }
140 }
141 table.unlockBag(entry);
142 return s;
143}
144
145const ISymbolTable::Symbol* AssetManagerSymbolTableBuilder::AssetManagerSymbolTable::findByName(
146 const ResourceName& name) {
147 if (const std::shared_ptr<Symbol>& s = mCache.get(name)) {
148 return s.get();
149 }
150
151 for (const auto& asset : mAssets) {
152 const android::ResTable& table = asset->getResources(false);
153 StringPiece16 typeStr = toString(name.type);
Adam Lesinskie352b992015-11-16 11:59:14 -0800154 uint32_t typeSpecFlags = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155 ResourceId resId = table.identifierForName(name.entry.data(), name.entry.size(),
156 typeStr.data(), typeStr.size(),
Adam Lesinskie352b992015-11-16 11:59:14 -0800157 name.package.data(), name.package.size(),
158 &typeSpecFlags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159 if (!resId.isValid()) {
160 continue;
161 }
162
Adam Lesinskie352b992015-11-16 11:59:14 -0800163 std::shared_ptr<Symbol> s;
164 if (name.type == ResourceType::kAttr) {
165 s = lookupAttributeInTable(table, resId);
166 } else {
167 s = std::make_shared<Symbol>();
168 s->id = resId;
169 }
170
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700171 if (s) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800172 s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173 mCache.put(name, s);
174 return s.get();
175 }
176 }
177 return nullptr;
178}
179
Adam Lesinski467f1712015-11-16 17:35:44 -0800180static Maybe<ResourceName> getResourceName(const android::ResTable& table, ResourceId id) {
181 android::ResTable::resource_name resName;
182 if (!table.getResourceName(id.id, true, &resName)) {
183 return {};
184 }
185
186 ResourceName name;
187 if (resName.package) {
188 name.package = StringPiece16(resName.package, resName.packageLen).toString();
189 }
190
191 const ResourceType* type;
192 if (resName.type) {
193 type = parseResourceType(StringPiece16(resName.type, resName.typeLen));
194
195 } else if (resName.type8) {
196 type = parseResourceType(util::utf8ToUtf16(StringPiece(resName.type8, resName.typeLen)));
197 } else {
198 return {};
199 }
200
201 if (!type) {
202 return {};
203 }
204
205 name.type = *type;
206
207 if (resName.name) {
208 name.entry = StringPiece16(resName.name, resName.nameLen).toString();
209 } else if (resName.name8) {
210 name.entry = util::utf8ToUtf16(StringPiece(resName.name8, resName.nameLen));
211 } else {
212 return {};
213 }
214
215 return name;
216}
217
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218const ISymbolTable::Symbol* AssetManagerSymbolTableBuilder::AssetManagerSymbolTable::findById(
219 ResourceId id) {
220 if (const std::shared_ptr<Symbol>& s = mIdCache.get(id)) {
221 return s.get();
222 }
223
224 for (const auto& asset : mAssets) {
225 const android::ResTable& table = asset->getResources(false);
226
Adam Lesinski467f1712015-11-16 17:35:44 -0800227 Maybe<ResourceName> maybeName = getResourceName(table, id);
228 if (!maybeName) {
Adam Lesinskie352b992015-11-16 11:59:14 -0800229 continue;
230 }
231
Adam Lesinski467f1712015-11-16 17:35:44 -0800232 uint32_t typeSpecFlags = 0;
233 table.getResourceFlags(id.id, &typeSpecFlags);
Adam Lesinskie352b992015-11-16 11:59:14 -0800234
235 std::shared_ptr<Symbol> s;
Adam Lesinski467f1712015-11-16 17:35:44 -0800236 if (maybeName.value().type == ResourceType::kAttr) {
Adam Lesinskie352b992015-11-16 11:59:14 -0800237 s = lookupAttributeInTable(table, id);
238 } else {
239 s = std::make_shared<Symbol>();
240 s->id = id;
241 }
242
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243 if (s) {
Adam Lesinski467f1712015-11-16 17:35:44 -0800244 s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245 mIdCache.put(id, s);
246 return s.get();
247 }
248 }
249 return nullptr;
250}
251
252const ISymbolTable::Symbol* JoinedSymbolTableBuilder::JoinedSymbolTable::findByName(
253 const ResourceName& name) {
254 for (auto& symbolTable : mSymbolTables) {
255 if (const Symbol* s = symbolTable->findByName(name)) {
256 return s;
257 }
258 }
259 return {};
260}
261
262const ISymbolTable::Symbol* JoinedSymbolTableBuilder::JoinedSymbolTable::findById(ResourceId id) {
263 for (auto& symbolTable : mSymbolTables) {
264 if (const Symbol* s = symbolTable->findById(id)) {
265 return s;
266 }
267 }
268 return {};
269}
270
271} // namespace aapt