Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 19 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "process/SymbolTable.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 21 | #include "util/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | |
| 23 | #include <androidfw/AssetManager.h> |
| 24 | #include <androidfw/ResourceTypes.h> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 28 | void 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 | |
| 34 | void 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 | |
| 41 | const SymbolTable::Symbol* SymbolTable::findByName(const ResourceName& name) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | if (const std::shared_ptr<Symbol>& s = mCache.get(name)) { |
| 43 | return s.get(); |
| 44 | } |
| 45 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 46 | // 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 | |
| 60 | const 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 | |
| 79 | std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::findByName( |
| 80 | const ResourceName& name) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 81 | 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 Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 85 | return findByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 86 | } |
| 87 | return {}; |
| 88 | } |
| 89 | |
| 90 | ResourceTable::SearchResult sr = result.value(); |
| 91 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 92 | std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 93 | symbol->isPublic = (sr.entry->symbolStatus.state == SymbolState::kPublic); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 95 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | const ConfigDescription kDefaultConfig; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 101 | ResourceConfigValue* configValue = sr.entry->findValue(kDefaultConfig); |
| 102 | if (configValue) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 103 | // This resource has an Attribute. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 104 | if (Attribute* attr = valueCast<Attribute>(configValue->value.get())) { |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 105 | symbol->attribute = util::make_unique<Attribute>(*attr); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 106 | } else { |
| 107 | return {}; |
| 108 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 111 | return symbol; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 114 | bool AssetManagerSymbolSource::addAssetPath(const StringPiece& path) { |
| 115 | int32_t cookie = 0; |
| 116 | return mAssets.addAssetPath(android::String8(path.data(), path.size()), &cookie); |
| 117 | } |
| 118 | |
| 119 | static std::unique_ptr<SymbolTable::Symbol> lookupAttributeInTable(const android::ResTable& table, |
| 120 | ResourceId id) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 121 | // 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 Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 130 | std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | 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 Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 144 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 153 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 154 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 156 | |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | table.unlockBag(entry); |
| 181 | return s; |
| 182 | } |
| 183 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 184 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findByName( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 185 | const ResourceName& name) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 186 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 197 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 203 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 204 | |
| 205 | if (s) { |
| 206 | s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
| 207 | return s; |
| 208 | } |
| 209 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 212 | static Maybe<ResourceName> getResourceName(const android::ResTable& table, ResourceId id) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 213 | android::ResTable::resource_name resName = {}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 214 | 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 Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 250 | std::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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 257 | uint32_t typeSpecFlags = 0; |
| 258 | table.getResourceFlags(id.id, &typeSpecFlags); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 259 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 260 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 266 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 267 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame^] | 268 | if (s) { |
| 269 | s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
| 270 | return s; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 271 | } |
| 272 | return {}; |
| 273 | } |
| 274 | |
| 275 | } // namespace aapt |