blob: 6c506dfe1749b563a81b65590f230470bc4360fe [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 Lesinskid0f116b2016-07-08 15:00:32 -070019#include "ResourceUtils.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070020#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "process/SymbolTable.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070022#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023
24#include <androidfw/AssetManager.h>
25#include <androidfw/ResourceTypes.h>
26
27namespace aapt {
28
Adam Lesinski64587af2016-02-18 18:33:06 -080029void SymbolTable::appendSource(std::unique_ptr<ISymbolSource> source) {
30 mSources.push_back(std::move(source));
31
32 // We do not clear the cache, because sources earlier in the list take precedent.
33}
34
35void SymbolTable::prependSource(std::unique_ptr<ISymbolSource> source) {
36 mSources.insert(mSources.begin(), std::move(source));
37
38 // We must clear the cache in case we did a lookup before adding this resource.
39 mCache.clear();
40}
41
42const SymbolTable::Symbol* SymbolTable::findByName(const ResourceName& name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043 if (const std::shared_ptr<Symbol>& s = mCache.get(name)) {
44 return s.get();
45 }
46
Adam Lesinski64587af2016-02-18 18:33:06 -080047 // We did not find it in the cache, so look through the sources.
48 for (auto& symbolSource : mSources) {
49 std::unique_ptr<Symbol> symbol = symbolSource->findByName(name);
50 if (symbol) {
51 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
52 // doesn't support unique_ptr.
53 std::shared_ptr<Symbol> sharedSymbol = std::shared_ptr<Symbol>(symbol.release());
54 mCache.put(name, sharedSymbol);
Adam Lesinski7656554f2016-03-10 21:55:04 -080055
56 if (sharedSymbol->id) {
57 // The symbol has an ID, so we can also cache this!
58 mIdCache.put(sharedSymbol->id.value(), sharedSymbol);
59 }
Adam Lesinski64587af2016-02-18 18:33:06 -080060 return sharedSymbol.get();
61 }
62 }
63 return nullptr;
64}
65
66const SymbolTable::Symbol* SymbolTable::findById(ResourceId id) {
67 if (const std::shared_ptr<Symbol>& s = mIdCache.get(id)) {
68 return s.get();
69 }
70
71 // We did not find it in the cache, so look through the sources.
72 for (auto& symbolSource : mSources) {
73 std::unique_ptr<Symbol> symbol = symbolSource->findById(id);
74 if (symbol) {
75 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
76 // doesn't support unique_ptr.
77 std::shared_ptr<Symbol> sharedSymbol = std::shared_ptr<Symbol>(symbol.release());
78 mIdCache.put(id, sharedSymbol);
79 return sharedSymbol.get();
80 }
81 }
82 return nullptr;
83}
84
Adam Lesinski7656554f2016-03-10 21:55:04 -080085const SymbolTable::Symbol* SymbolTable::findByReference(const Reference& ref) {
86 // First try the ID. This is because when we lookup by ID, we only fill in the ID cache.
87 // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed
Adam Lesinskid0f116b2016-07-08 15:00:32 -070088 // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately
Adam Lesinski7656554f2016-03-10 21:55:04 -080089 // because the ID is cached too.
90 //
91 // If we looked up by name first, a cache miss would mean we failed to lookup by name, then
92 // succeeded to lookup by ID. Subsequent lookups will miss then hit.
93 const SymbolTable::Symbol* symbol = nullptr;
94 if (ref.id) {
95 symbol = findById(ref.id.value());
96 }
97
98 if (ref.name && !symbol) {
99 symbol = findByName(ref.name.value());
100 }
101 return symbol;
102}
103
Adam Lesinski64587af2016-02-18 18:33:06 -0800104std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::findByName(
105 const ResourceName& name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106 Maybe<ResourceTable::SearchResult> result = mTable->findResource(name);
107 if (!result) {
108 if (name.type == ResourceType::kAttr) {
109 // Recurse and try looking up a private attribute.
Adam Lesinskie78fd612015-10-22 12:48:43 -0700110 return findByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111 }
112 return {};
113 }
114
115 ResourceTable::SearchResult sr = result.value();
116
Adam Lesinski64587af2016-02-18 18:33:06 -0800117 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>();
Adam Lesinski467f1712015-11-16 17:35:44 -0800118 symbol->isPublic = (sr.entry->symbolStatus.state == SymbolState::kPublic);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119
Adam Lesinski64587af2016-02-18 18:33:06 -0800120 if (sr.package->id && sr.type->id && sr.entry->id) {
121 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value());
122 }
123
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125 const ConfigDescription kDefaultConfig;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800126 ResourceConfigValue* configValue = sr.entry->findValue(kDefaultConfig);
127 if (configValue) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128 // This resource has an Attribute.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800129 if (Attribute* attr = valueCast<Attribute>(configValue->value.get())) {
Adam Lesinski7656554f2016-03-10 21:55:04 -0800130 symbol->attribute = std::make_shared<Attribute>(*attr);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700131 } else {
132 return {};
133 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134 }
135 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800136 return symbol;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137}
138
Adam Lesinski64587af2016-02-18 18:33:06 -0800139bool AssetManagerSymbolSource::addAssetPath(const StringPiece& path) {
140 int32_t cookie = 0;
141 return mAssets.addAssetPath(android::String8(path.data(), path.size()), &cookie);
142}
143
144static std::unique_ptr<SymbolTable::Symbol> lookupAttributeInTable(const android::ResTable& table,
145 ResourceId id) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146 // Try as a bag.
147 const android::ResTable::bag_entry* entry;
148 ssize_t count = table.lockBag(id.id, &entry);
149 if (count < 0) {
150 table.unlockBag(entry);
151 return nullptr;
152 }
153
154 // We found a resource.
Adam Lesinski64587af2016-02-18 18:33:06 -0800155 std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156 s->id = id;
157
158 // Check to see if it is an attribute.
159 for (size_t i = 0; i < (size_t) count; i++) {
160 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) {
Adam Lesinski7656554f2016-03-10 21:55:04 -0800161 s->attribute = std::make_shared<Attribute>(false);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162 s->attribute->typeMask = entry[i].map.value.data;
163 break;
164 }
165 }
166
167 if (s->attribute) {
168 for (size_t i = 0; i < (size_t) count; i++) {
Adam Lesinskia5870652015-11-20 15:32:30 -0800169 const android::ResTable_map& mapEntry = entry[i].map;
170 if (Res_INTERNALID(mapEntry.name.ident)) {
171 switch (mapEntry.name.ident) {
172 case android::ResTable_map::ATTR_MIN:
173 s->attribute->minInt = static_cast<int32_t>(mapEntry.value.data);
174 break;
175 case android::ResTable_map::ATTR_MAX:
176 s->attribute->maxInt = static_cast<int32_t>(mapEntry.value.data);
177 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800179 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800181
182 android::ResTable::resource_name entryName;
183 if (!table.getResourceName(mapEntry.name.ident, false, &entryName)) {
184 table.unlockBag(entry);
185 return nullptr;
186 }
187
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700188 Maybe<ResourceName> parsedName = ResourceUtils::toResourceName(entryName);
189 if (!parsedName) {
Adam Lesinskia5870652015-11-20 15:32:30 -0800190 return nullptr;
191 }
192
193 Attribute::Symbol symbol;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700194 symbol.symbol.name = parsedName.value();
Adam Lesinskia5870652015-11-20 15:32:30 -0800195 symbol.symbol.id = ResourceId(mapEntry.name.ident);
196 symbol.value = mapEntry.value.data;
197 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198 }
199 }
200 table.unlockBag(entry);
201 return s;
202}
203
Adam Lesinski64587af2016-02-18 18:33:06 -0800204std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findByName(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700205 const ResourceName& name) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800206 const android::ResTable& table = mAssets.getResources(false);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700207
208 const std::u16string package16 = util::utf8ToUtf16(name.package);
209 const std::u16string type16 = util::utf8ToUtf16(toString(name.type));
210 const std::u16string entry16 = util::utf8ToUtf16(name.entry);
211
Adam Lesinski64587af2016-02-18 18:33:06 -0800212 uint32_t typeSpecFlags = 0;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700213 ResourceId resId = table.identifierForName(entry16.data(), entry16.size(),
214 type16.data(), type16.size(),
215 package16.data(), package16.size(),
Adam Lesinski64587af2016-02-18 18:33:06 -0800216 &typeSpecFlags);
217 if (!resId.isValid()) {
218 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219 }
220
Adam Lesinski64587af2016-02-18 18:33:06 -0800221 std::unique_ptr<SymbolTable::Symbol> s;
222 if (name.type == ResourceType::kAttr) {
223 s = lookupAttributeInTable(table, resId);
224 } else {
225 s = util::make_unique<SymbolTable::Symbol>();
226 s->id = resId;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700227 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800228
229 if (s) {
230 s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
231 return s;
232 }
233 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700234}
235
Adam Lesinski467f1712015-11-16 17:35:44 -0800236static Maybe<ResourceName> getResourceName(const android::ResTable& table, ResourceId id) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800237 android::ResTable::resource_name resName = {};
Adam Lesinski467f1712015-11-16 17:35:44 -0800238 if (!table.getResourceName(id.id, true, &resName)) {
239 return {};
240 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700241 return ResourceUtils::toResourceName(resName);
Adam Lesinski467f1712015-11-16 17:35:44 -0800242}
243
Adam Lesinski64587af2016-02-18 18:33:06 -0800244std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findById(ResourceId id) {
245 const android::ResTable& table = mAssets.getResources(false);
246 Maybe<ResourceName> maybeName = getResourceName(table, id);
247 if (!maybeName) {
248 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700249 }
250
Adam Lesinski64587af2016-02-18 18:33:06 -0800251 uint32_t typeSpecFlags = 0;
252 table.getResourceFlags(id.id, &typeSpecFlags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700253
Adam Lesinski64587af2016-02-18 18:33:06 -0800254 std::unique_ptr<SymbolTable::Symbol> s;
255 if (maybeName.value().type == ResourceType::kAttr) {
256 s = lookupAttributeInTable(table, id);
257 } else {
258 s = util::make_unique<SymbolTable::Symbol>();
259 s->id = id;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261
Adam Lesinski64587af2016-02-18 18:33:06 -0800262 if (s) {
263 s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
264 return s;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265 }
266 return {};
267}
268
Adam Lesinski7656554f2016-03-10 21:55:04 -0800269std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findByReference(
270 const Reference& ref) {
271 // AssetManager always prefers IDs.
272 if (ref.id) {
273 return findById(ref.id.value());
274 } else if (ref.name) {
275 return findByName(ref.name.value());
276 }
277 return {};
278}
279
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280} // namespace aapt