blob: a8f9bfe4fb322164099f3eae33f3dd183c82a13c [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#include "process/SymbolTable.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070021#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022
23#include <androidfw/AssetManager.h>
24#include <androidfw/ResourceTypes.h>
25
26namespace aapt {
27
Adam Lesinski64587af2016-02-18 18:33:06 -080028void SymbolTable::appendSource(std::unique_ptr<ISymbolSource> source) {
29 mSources.push_back(std::move(source));
30
31 // We do not clear the cache, because sources earlier in the list take precedent.
32}
33
34void SymbolTable::prependSource(std::unique_ptr<ISymbolSource> source) {
35 mSources.insert(mSources.begin(), std::move(source));
36
37 // We must clear the cache in case we did a lookup before adding this resource.
38 mCache.clear();
39}
40
41const SymbolTable::Symbol* SymbolTable::findByName(const ResourceName& name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042 if (const std::shared_ptr<Symbol>& s = mCache.get(name)) {
43 return s.get();
44 }
45
Adam Lesinski64587af2016-02-18 18:33:06 -080046 // We did not find it in the cache, so look through the sources.
47 for (auto& symbolSource : mSources) {
48 std::unique_ptr<Symbol> symbol = symbolSource->findByName(name);
49 if (symbol) {
50 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
51 // doesn't support unique_ptr.
52 std::shared_ptr<Symbol> sharedSymbol = std::shared_ptr<Symbol>(symbol.release());
53 mCache.put(name, sharedSymbol);
54 return sharedSymbol.get();
55 }
56 }
57 return nullptr;
58}
59
60const SymbolTable::Symbol* SymbolTable::findById(ResourceId id) {
61 if (const std::shared_ptr<Symbol>& s = mIdCache.get(id)) {
62 return s.get();
63 }
64
65 // We did not find it in the cache, so look through the sources.
66 for (auto& symbolSource : mSources) {
67 std::unique_ptr<Symbol> symbol = symbolSource->findById(id);
68 if (symbol) {
69 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
70 // doesn't support unique_ptr.
71 std::shared_ptr<Symbol> sharedSymbol = std::shared_ptr<Symbol>(symbol.release());
72 mIdCache.put(id, sharedSymbol);
73 return sharedSymbol.get();
74 }
75 }
76 return nullptr;
77}
78
79std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::findByName(
80 const ResourceName& name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081 Maybe<ResourceTable::SearchResult> result = mTable->findResource(name);
82 if (!result) {
83 if (name.type == ResourceType::kAttr) {
84 // Recurse and try looking up a private attribute.
Adam Lesinskie78fd612015-10-22 12:48:43 -070085 return findByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070086 }
87 return {};
88 }
89
90 ResourceTable::SearchResult sr = result.value();
91
Adam Lesinski64587af2016-02-18 18:33:06 -080092 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>();
Adam Lesinski467f1712015-11-16 17:35:44 -080093 symbol->isPublic = (sr.entry->symbolStatus.state == SymbolState::kPublic);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094
Adam Lesinski64587af2016-02-18 18:33:06 -080095 if (sr.package->id && sr.type->id && sr.entry->id) {
96 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value());
97 }
98
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100 const ConfigDescription kDefaultConfig;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800101 ResourceConfigValue* configValue = sr.entry->findValue(kDefaultConfig);
102 if (configValue) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103 // This resource has an Attribute.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800104 if (Attribute* attr = valueCast<Attribute>(configValue->value.get())) {
Adam Lesinskia5870652015-11-20 15:32:30 -0800105 symbol->attribute = util::make_unique<Attribute>(*attr);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700106 } else {
107 return {};
108 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109 }
110 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800111 return symbol;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112}
113
Adam Lesinski64587af2016-02-18 18:33:06 -0800114bool AssetManagerSymbolSource::addAssetPath(const StringPiece& path) {
115 int32_t cookie = 0;
116 return mAssets.addAssetPath(android::String8(path.data(), path.size()), &cookie);
117}
118
119static std::unique_ptr<SymbolTable::Symbol> lookupAttributeInTable(const android::ResTable& table,
120 ResourceId id) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121 // Try as a bag.
122 const android::ResTable::bag_entry* entry;
123 ssize_t count = table.lockBag(id.id, &entry);
124 if (count < 0) {
125 table.unlockBag(entry);
126 return nullptr;
127 }
128
129 // We found a resource.
Adam Lesinski64587af2016-02-18 18:33:06 -0800130 std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131 s->id = id;
132
133 // Check to see if it is an attribute.
134 for (size_t i = 0; i < (size_t) count; i++) {
135 if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) {
136 s->attribute = util::make_unique<Attribute>(false);
137 s->attribute->typeMask = entry[i].map.value.data;
138 break;
139 }
140 }
141
142 if (s->attribute) {
143 for (size_t i = 0; i < (size_t) count; i++) {
Adam Lesinskia5870652015-11-20 15:32:30 -0800144 const android::ResTable_map& mapEntry = entry[i].map;
145 if (Res_INTERNALID(mapEntry.name.ident)) {
146 switch (mapEntry.name.ident) {
147 case android::ResTable_map::ATTR_MIN:
148 s->attribute->minInt = static_cast<int32_t>(mapEntry.value.data);
149 break;
150 case android::ResTable_map::ATTR_MAX:
151 s->attribute->maxInt = static_cast<int32_t>(mapEntry.value.data);
152 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800154 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155 }
Adam Lesinskia5870652015-11-20 15:32:30 -0800156
157 android::ResTable::resource_name entryName;
158 if (!table.getResourceName(mapEntry.name.ident, false, &entryName)) {
159 table.unlockBag(entry);
160 return nullptr;
161 }
162
163 const ResourceType* parsedType = parseResourceType(
164 StringPiece16(entryName.type, entryName.typeLen));
165 if (!parsedType) {
166 table.unlockBag(entry);
167 return nullptr;
168 }
169
170 Attribute::Symbol symbol;
171 symbol.symbol.name = ResourceName(
172 StringPiece16(entryName.package, entryName.packageLen),
173 *parsedType,
174 StringPiece16(entryName.name, entryName.nameLen));
175 symbol.symbol.id = ResourceId(mapEntry.name.ident);
176 symbol.value = mapEntry.value.data;
177 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178 }
179 }
180 table.unlockBag(entry);
181 return s;
182}
183
Adam Lesinski64587af2016-02-18 18:33:06 -0800184std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findByName(
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185 const ResourceName& name) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800186 const android::ResTable& table = mAssets.getResources(false);
187 StringPiece16 typeStr = toString(name.type);
188 uint32_t typeSpecFlags = 0;
189 ResourceId resId = table.identifierForName(name.entry.data(), name.entry.size(),
190 typeStr.data(), typeStr.size(),
191 name.package.data(), name.package.size(),
192 &typeSpecFlags);
193 if (!resId.isValid()) {
194 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195 }
196
Adam Lesinski64587af2016-02-18 18:33:06 -0800197 std::unique_ptr<SymbolTable::Symbol> s;
198 if (name.type == ResourceType::kAttr) {
199 s = lookupAttributeInTable(table, resId);
200 } else {
201 s = util::make_unique<SymbolTable::Symbol>();
202 s->id = resId;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800204
205 if (s) {
206 s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
207 return s;
208 }
209 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700210}
211
Adam Lesinski467f1712015-11-16 17:35:44 -0800212static Maybe<ResourceName> getResourceName(const android::ResTable& table, ResourceId id) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800213 android::ResTable::resource_name resName = {};
Adam Lesinski467f1712015-11-16 17:35:44 -0800214 if (!table.getResourceName(id.id, true, &resName)) {
215 return {};
216 }
217
218 ResourceName name;
219 if (resName.package) {
220 name.package = StringPiece16(resName.package, resName.packageLen).toString();
221 }
222
223 const ResourceType* type;
224 if (resName.type) {
225 type = parseResourceType(StringPiece16(resName.type, resName.typeLen));
226
227 } else if (resName.type8) {
228 type = parseResourceType(util::utf8ToUtf16(StringPiece(resName.type8, resName.typeLen)));
229 } else {
230 return {};
231 }
232
233 if (!type) {
234 return {};
235 }
236
237 name.type = *type;
238
239 if (resName.name) {
240 name.entry = StringPiece16(resName.name, resName.nameLen).toString();
241 } else if (resName.name8) {
242 name.entry = util::utf8ToUtf16(StringPiece(resName.name8, resName.nameLen));
243 } else {
244 return {};
245 }
246
247 return name;
248}
249
Adam Lesinski64587af2016-02-18 18:33:06 -0800250std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findById(ResourceId id) {
251 const android::ResTable& table = mAssets.getResources(false);
252 Maybe<ResourceName> maybeName = getResourceName(table, id);
253 if (!maybeName) {
254 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255 }
256
Adam Lesinski64587af2016-02-18 18:33:06 -0800257 uint32_t typeSpecFlags = 0;
258 table.getResourceFlags(id.id, &typeSpecFlags);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259
Adam Lesinski64587af2016-02-18 18:33:06 -0800260 std::unique_ptr<SymbolTable::Symbol> s;
261 if (maybeName.value().type == ResourceType::kAttr) {
262 s = lookupAttributeInTable(table, id);
263 } else {
264 s = util::make_unique<SymbolTable::Symbol>();
265 s->id = id;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700266 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267
Adam Lesinski64587af2016-02-18 18:33:06 -0800268 if (s) {
269 s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
270 return s;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700271 }
272 return {};
273}
274
275} // namespace aapt