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" |
| 19 | #include "util/Util.h" |
| 20 | |
| 21 | #include "process/SymbolTable.h" |
| 22 | |
| 23 | #include <androidfw/AssetManager.h> |
| 24 | #include <androidfw/ResourceTypes.h> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | const ISymbolTable::Symbol* SymbolTableWrapper::findByName(const ResourceName& name) { |
| 29 | if (const std::shared_ptr<Symbol>& s = mCache.get(name)) { |
| 30 | return s.get(); |
| 31 | } |
| 32 | |
| 33 | Maybe<ResourceTable::SearchResult> result = mTable->findResource(name); |
| 34 | if (!result) { |
| 35 | if (name.type == ResourceType::kAttr) { |
| 36 | // Recurse and try looking up a private attribute. |
| 37 | return findByName(ResourceName{ name.package, ResourceType::kAttrPrivate, name.entry }); |
| 38 | } |
| 39 | return {}; |
| 40 | } |
| 41 | |
| 42 | ResourceTable::SearchResult sr = result.value(); |
| 43 | |
| 44 | // If no ID exists, we treat the symbol as missing. SymbolTables are used to |
| 45 | // find symbols to link. |
| 46 | if (!sr.package->id || !sr.type->id || !sr.entry->id) { |
| 47 | return {}; |
| 48 | } |
| 49 | |
| 50 | std::shared_ptr<Symbol> symbol = std::make_shared<Symbol>(); |
| 51 | symbol->id = ResourceId{ |
| 52 | sr.package->id.value(), sr.type->id.value(), sr.entry->id.value() }; |
| 53 | |
| 54 | if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) { |
| 55 | auto lt = [](ResourceConfigValue& lhs, const ConfigDescription& rhs) -> bool { |
| 56 | return lhs.config < rhs; |
| 57 | }; |
| 58 | |
| 59 | const ConfigDescription kDefaultConfig; |
| 60 | auto iter = std::lower_bound(sr.entry->values.begin(), sr.entry->values.end(), |
| 61 | kDefaultConfig, lt); |
| 62 | |
| 63 | if (iter != sr.entry->values.end() && iter->config == kDefaultConfig) { |
| 64 | // This resource has an Attribute. |
| 65 | symbol->attribute = util::make_unique<Attribute>( |
| 66 | *static_cast<Attribute*>(iter->value.get())); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (name.type == ResourceType::kAttrPrivate) { |
| 71 | // Masquerade this entry as kAttr. |
| 72 | mCache.put(ResourceName{ name.package, ResourceType::kAttr, name.entry }, symbol); |
| 73 | } else { |
| 74 | mCache.put(name, symbol); |
| 75 | } |
| 76 | return symbol.get(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | static 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 | |
| 142 | const 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 | |
| 167 | const 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 | |
| 185 | const 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 | |
| 195 | const 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 |