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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "process/SymbolTable.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 19 | #include <iostream> |
| 20 | |
| 21 | #include "android-base/logging.h" |
| 22 | #include "android-base/stringprintf.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "androidfw/AssetManager.h" |
| 24 | #include "androidfw/ResourceTypes.h" |
| 25 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "ConfigDescription.h" |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 27 | #include "NameMangler.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "Resource.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 29 | #include "ResourceUtils.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 30 | #include "ValueVisitor.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 31 | #include "util/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 33 | using ::android::StringPiece; |
| 34 | using ::android::StringPiece16; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | namespace aapt { |
| 37 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 38 | SymbolTable::SymbolTable(NameMangler* mangler) |
| 39 | : mangler_(mangler), |
| 40 | delegate_(util::make_unique<DefaultSymbolTableDelegate>()), |
| 41 | cache_(200), |
| 42 | id_cache_(200) { |
| 43 | } |
| 44 | |
| 45 | void SymbolTable::SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate) { |
| 46 | CHECK(delegate != nullptr) << "can't set a nullptr delegate"; |
| 47 | delegate_ = std::move(delegate); |
| 48 | |
| 49 | // Clear the cache in case this delegate changes the order of lookup. |
| 50 | cache_.clear(); |
| 51 | } |
| 52 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) { |
| 54 | sources_.push_back(std::move(source)); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | // We do not clear the cache, because sources earlier in the list take |
| 57 | // precedent. |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) { |
| 61 | sources_.insert(sources_.begin(), std::move(source)); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | // We must clear the cache in case we did a lookup before adding this |
| 64 | // resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | cache_.clear(); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 69 | const ResourceName* name_with_package = &name; |
| 70 | |
| 71 | // Fill in the package name if necessary. |
| 72 | // If there is no package in `name`, we will need to copy the ResourceName |
| 73 | // and store it somewhere; we use the Maybe<> class to reserve storage. |
| 74 | Maybe<ResourceName> name_with_package_impl; |
| 75 | if (name.package.empty()) { |
| 76 | name_with_package_impl = ResourceName(mangler_->GetTargetPackageName(), name.type, name.entry); |
| 77 | name_with_package = &name_with_package_impl.value(); |
| 78 | } |
| 79 | |
| 80 | // We store the name unmangled in the cache, so look it up as-is. |
| 81 | if (const std::shared_ptr<Symbol>& s = cache_.get(*name_with_package)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | return s.get(); |
| 83 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 84 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 85 | // The name was not found in the cache. Mangle it (if necessary) and find it in our sources. |
| 86 | // Again, here we use a Maybe<> object to reserve storage if we need to mangle. |
| 87 | const ResourceName* mangled_name = name_with_package; |
| 88 | Maybe<ResourceName> mangled_name_impl; |
| 89 | if (mangler_->ShouldMangle(name_with_package->package)) { |
| 90 | mangled_name_impl = mangler_->MangleName(*name_with_package); |
| 91 | mangled_name = &mangled_name_impl.value(); |
| 92 | } |
| 93 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 94 | std::unique_ptr<Symbol> symbol = delegate_->FindByName(*mangled_name, sources_); |
| 95 | if (symbol == nullptr) { |
| 96 | return nullptr; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | } |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 98 | |
| 99 | // Take ownership of the symbol into a shared_ptr. We do this because |
| 100 | // LruCache doesn't support unique_ptr. |
| 101 | std::shared_ptr<Symbol> shared_symbol(std::move(symbol)); |
| 102 | |
| 103 | // Since we look in the cache with the unmangled, but package prefixed |
| 104 | // name, we must put the same name into the cache. |
| 105 | cache_.put(*name_with_package, shared_symbol); |
| 106 | |
| 107 | if (shared_symbol->id) { |
| 108 | // The symbol has an ID, so we can also cache this! |
| 109 | id_cache_.put(shared_symbol->id.value(), shared_symbol); |
| 110 | } |
| 111 | |
| 112 | // Returns the raw pointer. Callers are not expected to hold on to this |
| 113 | // between calls to Find*. |
| 114 | return shared_symbol.get(); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) { |
| 118 | if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | return s.get(); |
| 120 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 121 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | // We did not find it in the cache, so look through the sources. |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 123 | std::unique_ptr<Symbol> symbol = delegate_->FindById(id, sources_); |
| 124 | if (symbol == nullptr) { |
| 125 | return nullptr; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | } |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 127 | |
| 128 | // Take ownership of the symbol into a shared_ptr. We do this because LruCache |
| 129 | // doesn't support unique_ptr. |
| 130 | std::shared_ptr<Symbol> shared_symbol(std::move(symbol)); |
| 131 | id_cache_.put(id, shared_symbol); |
| 132 | |
| 133 | // Returns the raw pointer. Callers are not expected to hold on to this |
| 134 | // between calls to Find*. |
| 135 | return shared_symbol.get(); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 139 | // First try the ID. This is because when we lookup by ID, we only fill in the ID cache. |
| 140 | // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed |
| 141 | // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | // because the ID is cached too. |
| 143 | // |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 144 | // If we looked up by name first, a cache miss would mean we failed to lookup by name, then |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | // succeeded to lookup by ID. Subsequent lookups will miss then hit. |
| 146 | const SymbolTable::Symbol* symbol = nullptr; |
| 147 | if (ref.id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | symbol = FindById(ref.id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | } |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 150 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | if (ref.name && !symbol) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | symbol = FindByName(ref.name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | } |
| 154 | return symbol; |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 157 | std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindByName( |
| 158 | const ResourceName& name, const std::vector<std::unique_ptr<ISymbolSource>>& sources) { |
| 159 | for (auto& source : sources) { |
| 160 | std::unique_ptr<SymbolTable::Symbol> symbol = source->FindByName(name); |
| 161 | if (symbol) { |
| 162 | return symbol; |
| 163 | } |
| 164 | } |
| 165 | return {}; |
| 166 | } |
| 167 | |
| 168 | std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindById( |
| 169 | ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) { |
| 170 | for (auto& source : sources) { |
| 171 | std::unique_ptr<SymbolTable::Symbol> symbol = source->FindById(id); |
| 172 | if (symbol) { |
| 173 | return symbol; |
| 174 | } |
| 175 | } |
| 176 | return {}; |
| 177 | } |
| 178 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | const ResourceName& name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 181 | Maybe<ResourceTable::SearchResult> result = table_->FindResource(name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | if (!result) { |
| 183 | if (name.type == ResourceType::kAttr) { |
| 184 | // Recurse and try looking up a private attribute. |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 185 | return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | } |
| 187 | return {}; |
| 188 | } |
| 189 | |
| 190 | ResourceTable::SearchResult sr = result.value(); |
| 191 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 192 | std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 193 | symbol->is_public = (sr.entry->visibility.level == Visibility::Level::kPublic); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | |
| 195 | if (sr.package->id && sr.type->id && sr.entry->id) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 196 | symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 199 | if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | const ConfigDescription kDefaultConfig; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig); |
| 202 | if (config_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | // This resource has an Attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | symbol->attribute = std::make_shared<Attribute>(*attr); |
| 206 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 207 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 209 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | } |
| 211 | return symbol; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | int32_t cookie = 0; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 216 | return assets_.addAssetPath(android::String8(path.data(), path.size()), &cookie); |
| 217 | } |
| 218 | |
| 219 | std::map<size_t, std::string> AssetManagerSymbolSource::GetAssignedPackageIds() const { |
| 220 | std::map<size_t, std::string> package_map; |
| 221 | const android::ResTable& table = assets_.getResources(false); |
| 222 | const size_t package_count = table.getBasePackageCount(); |
| 223 | for (size_t i = 0; i < package_count; i++) { |
| 224 | package_map[table.getBasePackageId(i)] = |
| 225 | util::Utf16ToUtf8(android::StringPiece16(table.getBasePackageName(i).string())); |
| 226 | } |
| 227 | return package_map; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | const android::ResTable& table, ResourceId id) { |
| 232 | // Try as a bag. |
| 233 | const android::ResTable::bag_entry* entry; |
| 234 | ssize_t count = table.lockBag(id.id, &entry); |
| 235 | if (count < 0) { |
| 236 | table.unlockBag(entry); |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
| 240 | // We found a resource. |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 241 | std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>(id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 242 | |
| 243 | // Check to see if it is an attribute. |
| 244 | for (size_t i = 0; i < (size_t)count; i++) { |
| 245 | if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) { |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame^] | 246 | s->attribute = std::make_shared<Attribute>(entry[i].map.value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 248 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 249 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 250 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 251 | if (s->attribute) { |
| 252 | for (size_t i = 0; i < (size_t)count; i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | const android::ResTable_map& map_entry = entry[i].map; |
| 254 | if (Res_INTERNALID(map_entry.name.ident)) { |
| 255 | switch (map_entry.name.ident) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | case android::ResTable_map::ATTR_MIN: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | s->attribute->min_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | break; |
| 259 | case android::ResTable_map::ATTR_MAX: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | s->attribute->max_int = static_cast<int32_t>(map_entry.value.data); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 261 | break; |
| 262 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | android::ResTable::resource_name entry_name; |
| 267 | if (!table.getResourceName(map_entry.name.ident, false, &entry_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 268 | table.unlockBag(entry); |
| 269 | return nullptr; |
| 270 | } |
| 271 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 272 | Maybe<ResourceName> parsed_name = ResourceUtils::ToResourceName(entry_name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | if (!parsed_name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 274 | return nullptr; |
| 275 | } |
| 276 | |
| 277 | Attribute::Symbol symbol; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 278 | symbol.symbol.name = parsed_name.value(); |
| 279 | symbol.symbol.id = ResourceId(map_entry.name.ident); |
| 280 | symbol.value = map_entry.value.data; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 281 | s->attribute->symbols.push_back(std::move(symbol)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 282 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 283 | } |
| 284 | table.unlockBag(entry); |
| 285 | return s; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 289 | const ResourceName& name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | const android::ResTable& table = assets_.getResources(false); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 291 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 292 | const std::u16string package16 = util::Utf8ToUtf16(name.package); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 293 | const std::u16string type16 = util::Utf8ToUtf16(to_string(name.type)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | const std::u16string entry16 = util::Utf8ToUtf16(name.entry); |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 295 | const std::u16string mangled_entry16 = |
| 296 | util::Utf8ToUtf16(NameMangler::MangleEntry(name.package, name.entry)); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 297 | |
Adam Lesinski | dc78505 | 2017-12-07 15:58:46 -0800 | [diff] [blame] | 298 | uint32_t type_spec_flags; |
| 299 | ResourceId res_id; |
| 300 | |
| 301 | // There can be mangled resources embedded within other packages. Here we will |
| 302 | // look into each package and look-up the mangled name until we find the resource. |
| 303 | const size_t count = table.getBasePackageCount(); |
| 304 | for (size_t i = 0; i < count; i++) { |
| 305 | const android::String16 package_name = table.getBasePackageName(i); |
| 306 | StringPiece16 real_package16 = package16; |
| 307 | StringPiece16 real_entry16 = entry16; |
| 308 | std::u16string scratch_entry16; |
| 309 | if (StringPiece16(package_name) != package16) { |
| 310 | real_entry16 = mangled_entry16; |
| 311 | real_package16 = package_name.string(); |
| 312 | } |
| 313 | |
| 314 | type_spec_flags = 0; |
| 315 | res_id = table.identifierForName(real_entry16.data(), real_entry16.size(), type16.data(), |
| 316 | type16.size(), real_package16.data(), real_package16.size(), |
| 317 | &type_spec_flags); |
| 318 | if (res_id.is_valid()) { |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 323 | if (!res_id.is_valid()) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 324 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | std::unique_ptr<SymbolTable::Symbol> s; |
| 328 | if (name.type == ResourceType::kAttr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 329 | s = LookupAttributeInTable(table, res_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 330 | } else { |
| 331 | s = util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 332 | s->id = res_id; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | if (s) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 336 | s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 337 | return s; |
| 338 | } |
| 339 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | static Maybe<ResourceName> GetResourceName(const android::ResTable& table, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 343 | ResourceId id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 344 | android::ResTable::resource_name res_name = {}; |
| 345 | if (!table.getResourceName(id.id, true, &res_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 346 | return {}; |
| 347 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 348 | return ResourceUtils::ToResourceName(res_name); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 351 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 352 | ResourceId id) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 353 | if (!id.is_valid()) { |
| 354 | // Exit early and avoid the error logs from AssetManager. |
| 355 | return {}; |
| 356 | } |
| 357 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 358 | const android::ResTable& table = assets_.getResources(false); |
| 359 | Maybe<ResourceName> maybe_name = GetResourceName(table, id); |
| 360 | if (!maybe_name) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 361 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 364 | uint32_t type_spec_flags = 0; |
| 365 | table.getResourceFlags(id.id, &type_spec_flags); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 366 | |
| 367 | std::unique_ptr<SymbolTable::Symbol> s; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 | if (maybe_name.value().type == ResourceType::kAttr) { |
| 369 | s = LookupAttributeInTable(table, id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 370 | } else { |
| 371 | s = util::make_unique<SymbolTable::Symbol>(); |
| 372 | s->id = id; |
| 373 | } |
| 374 | |
| 375 | if (s) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 376 | s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 377 | return s; |
| 378 | } |
| 379 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 382 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 383 | const Reference& ref) { |
| 384 | // AssetManager always prefers IDs. |
| 385 | if (ref.id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 386 | return FindById(ref.id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 387 | } else if (ref.name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 388 | return FindByName(ref.name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 389 | } |
| 390 | return {}; |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 393 | } // namespace aapt |