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