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 | |
| 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. |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 37 | return findByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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>(); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 51 | symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value()); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 52 | symbol->isPublic = (sr.entry->symbolStatus.state == SymbolState::kPublic); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | |
| 54 | if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | const ConfigDescription kDefaultConfig; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 56 | ResourceConfigValue* configValue = sr.entry->findValue(kDefaultConfig); |
| 57 | if (configValue) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | // This resource has an Attribute. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 59 | if (Attribute* attr = valueCast<Attribute>(configValue->value.get())) { |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 60 | symbol->attribute = util::make_unique<Attribute>(*attr); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 61 | } else { |
| 62 | return {}; |
| 63 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | if (name.type == ResourceType::kAttrPrivate) { |
| 68 | // Masquerade this entry as kAttr. |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 69 | mCache.put(ResourceName(name.package, ResourceType::kAttr, name.entry), symbol); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | } else { |
| 71 | mCache.put(name, symbol); |
| 72 | } |
| 73 | return symbol.get(); |
| 74 | } |
| 75 | |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 76 | static std::shared_ptr<ISymbolTable::Symbol> lookupAttributeInTable(const android::ResTable& table, |
| 77 | ResourceId id) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | // Try as a bag. |
| 79 | const android::ResTable::bag_entry* entry; |
| 80 | ssize_t count = table.lockBag(id.id, &entry); |
| 81 | if (count < 0) { |
| 82 | table.unlockBag(entry); |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | // We found a resource. |
| 87 | std::shared_ptr<ISymbolTable::Symbol> s = std::make_shared<ISymbolTable::Symbol>(); |
| 88 | s->id = id; |
| 89 | |
| 90 | // Check to see if it is an attribute. |
| 91 | for (size_t i = 0; i < (size_t) count; i++) { |
| 92 | if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) { |
| 93 | s->attribute = util::make_unique<Attribute>(false); |
| 94 | s->attribute->typeMask = entry[i].map.value.data; |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (s->attribute) { |
| 100 | for (size_t i = 0; i < (size_t) count; i++) { |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 101 | const android::ResTable_map& mapEntry = entry[i].map; |
| 102 | if (Res_INTERNALID(mapEntry.name.ident)) { |
| 103 | switch (mapEntry.name.ident) { |
| 104 | case android::ResTable_map::ATTR_MIN: |
| 105 | s->attribute->minInt = static_cast<int32_t>(mapEntry.value.data); |
| 106 | break; |
| 107 | case android::ResTable_map::ATTR_MAX: |
| 108 | s->attribute->maxInt = static_cast<int32_t>(mapEntry.value.data); |
| 109 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 110 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 111 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 113 | |
| 114 | android::ResTable::resource_name entryName; |
| 115 | if (!table.getResourceName(mapEntry.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 = ResourceName( |
| 129 | StringPiece16(entryName.package, entryName.packageLen), |
| 130 | *parsedType, |
| 131 | StringPiece16(entryName.name, entryName.nameLen)); |
| 132 | symbol.symbol.id = ResourceId(mapEntry.name.ident); |
| 133 | symbol.value = mapEntry.value.data; |
| 134 | s->attribute->symbols.push_back(std::move(symbol)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | table.unlockBag(entry); |
| 138 | return s; |
| 139 | } |
| 140 | |
| 141 | const ISymbolTable::Symbol* AssetManagerSymbolTableBuilder::AssetManagerSymbolTable::findByName( |
| 142 | const ResourceName& name) { |
| 143 | if (const std::shared_ptr<Symbol>& s = mCache.get(name)) { |
| 144 | return s.get(); |
| 145 | } |
| 146 | |
| 147 | for (const auto& asset : mAssets) { |
| 148 | const android::ResTable& table = asset->getResources(false); |
| 149 | StringPiece16 typeStr = toString(name.type); |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 150 | uint32_t typeSpecFlags = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | ResourceId resId = table.identifierForName(name.entry.data(), name.entry.size(), |
| 152 | typeStr.data(), typeStr.size(), |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 153 | name.package.data(), name.package.size(), |
| 154 | &typeSpecFlags); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | if (!resId.isValid()) { |
| 156 | continue; |
| 157 | } |
| 158 | |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 159 | std::shared_ptr<Symbol> s; |
| 160 | if (name.type == ResourceType::kAttr) { |
| 161 | s = lookupAttributeInTable(table, resId); |
| 162 | } else { |
| 163 | s = std::make_shared<Symbol>(); |
| 164 | s->id = resId; |
| 165 | } |
| 166 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 167 | if (s) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 168 | s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | mCache.put(name, s); |
| 170 | return s.get(); |
| 171 | } |
| 172 | } |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 176 | static Maybe<ResourceName> getResourceName(const android::ResTable& table, ResourceId id) { |
| 177 | android::ResTable::resource_name resName; |
| 178 | if (!table.getResourceName(id.id, true, &resName)) { |
| 179 | return {}; |
| 180 | } |
| 181 | |
| 182 | ResourceName name; |
| 183 | if (resName.package) { |
| 184 | name.package = StringPiece16(resName.package, resName.packageLen).toString(); |
| 185 | } |
| 186 | |
| 187 | const ResourceType* type; |
| 188 | if (resName.type) { |
| 189 | type = parseResourceType(StringPiece16(resName.type, resName.typeLen)); |
| 190 | |
| 191 | } else if (resName.type8) { |
| 192 | type = parseResourceType(util::utf8ToUtf16(StringPiece(resName.type8, resName.typeLen))); |
| 193 | } else { |
| 194 | return {}; |
| 195 | } |
| 196 | |
| 197 | if (!type) { |
| 198 | return {}; |
| 199 | } |
| 200 | |
| 201 | name.type = *type; |
| 202 | |
| 203 | if (resName.name) { |
| 204 | name.entry = StringPiece16(resName.name, resName.nameLen).toString(); |
| 205 | } else if (resName.name8) { |
| 206 | name.entry = util::utf8ToUtf16(StringPiece(resName.name8, resName.nameLen)); |
| 207 | } else { |
| 208 | return {}; |
| 209 | } |
| 210 | |
| 211 | return name; |
| 212 | } |
| 213 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 214 | const ISymbolTable::Symbol* AssetManagerSymbolTableBuilder::AssetManagerSymbolTable::findById( |
| 215 | ResourceId id) { |
| 216 | if (const std::shared_ptr<Symbol>& s = mIdCache.get(id)) { |
| 217 | return s.get(); |
| 218 | } |
| 219 | |
| 220 | for (const auto& asset : mAssets) { |
| 221 | const android::ResTable& table = asset->getResources(false); |
| 222 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 223 | Maybe<ResourceName> maybeName = getResourceName(table, id); |
| 224 | if (!maybeName) { |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 225 | continue; |
| 226 | } |
| 227 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 228 | uint32_t typeSpecFlags = 0; |
| 229 | table.getResourceFlags(id.id, &typeSpecFlags); |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 230 | |
| 231 | std::shared_ptr<Symbol> s; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 232 | if (maybeName.value().type == ResourceType::kAttr) { |
Adam Lesinski | e352b99 | 2015-11-16 11:59:14 -0800 | [diff] [blame] | 233 | s = lookupAttributeInTable(table, id); |
| 234 | } else { |
| 235 | s = std::make_shared<Symbol>(); |
| 236 | s->id = id; |
| 237 | } |
| 238 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 239 | if (s) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 240 | s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 241 | mIdCache.put(id, s); |
| 242 | return s.get(); |
| 243 | } |
| 244 | } |
| 245 | return nullptr; |
| 246 | } |
| 247 | |
| 248 | const ISymbolTable::Symbol* JoinedSymbolTableBuilder::JoinedSymbolTable::findByName( |
| 249 | const ResourceName& name) { |
| 250 | for (auto& symbolTable : mSymbolTables) { |
| 251 | if (const Symbol* s = symbolTable->findByName(name)) { |
| 252 | return s; |
| 253 | } |
| 254 | } |
| 255 | return {}; |
| 256 | } |
| 257 | |
| 258 | const ISymbolTable::Symbol* JoinedSymbolTableBuilder::JoinedSymbolTable::findById(ResourceId id) { |
| 259 | for (auto& symbolTable : mSymbolTables) { |
| 260 | if (const Symbol* s = symbolTable->findById(id)) { |
| 261 | return s; |
| 262 | } |
| 263 | } |
| 264 | return {}; |
| 265 | } |
| 266 | |
| 267 | } // namespace aapt |