Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
| 18 | |
| 19 | #include "androidfw/LoadedArsc.h" |
| 20 | |
| 21 | #include <cstddef> |
| 22 | #include <limits> |
| 23 | |
| 24 | #include "android-base/logging.h" |
| 25 | #include "android-base/stringprintf.h" |
| 26 | #include "utils/ByteOrder.h" |
| 27 | #include "utils/Trace.h" |
| 28 | |
| 29 | #ifdef _WIN32 |
| 30 | #ifdef ERROR |
| 31 | #undef ERROR |
| 32 | #endif |
| 33 | #endif |
| 34 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 35 | #include "androidfw/ByteBucketArray.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 36 | #include "androidfw/Chunk.h" |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 37 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 38 | #include "androidfw/Util.h" |
| 39 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 40 | using ::android::base::StringPrintf; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 44 | constexpr const static int kAppPackageId = 0x7f; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 45 | |
| 46 | // Element of a TypeSpec array. See TypeSpec. |
| 47 | struct Type { |
| 48 | // The configuration for which this type defines entries. |
| 49 | // This is already converted to host endianness. |
| 50 | ResTable_config configuration; |
| 51 | |
| 52 | // Pointer to the mmapped data where entry definitions are kept. |
| 53 | const ResTable_type* type; |
| 54 | }; |
| 55 | |
| 56 | // TypeSpec is going to be immediately proceeded by |
| 57 | // an array of Type structs, all in the same block of memory. |
| 58 | struct TypeSpec { |
| 59 | // Pointer to the mmapped data where flags are kept. |
| 60 | // Flags denote whether the resource entry is public |
| 61 | // and under which configurations it varies. |
| 62 | const ResTable_typeSpec* type_spec; |
| 63 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 64 | // Pointer to the mmapped data where the IDMAP mappings for this type |
| 65 | // exist. May be nullptr if no IDMAP exists. |
| 66 | const IdmapEntry_header* idmap_entries; |
| 67 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 68 | // The number of types that follow this struct. |
| 69 | // There is a type for each configuration |
| 70 | // that entries are defined for. |
| 71 | size_t type_count; |
| 72 | |
| 73 | // Trick to easily access a variable number of Type structs |
| 74 | // proceeding this struct, and to ensure their alignment. |
| 75 | const Type types[0]; |
| 76 | }; |
| 77 | |
| 78 | // TypeSpecPtr points to the block of memory that holds |
| 79 | // a TypeSpec struct, followed by an array of Type structs. |
| 80 | // TypeSpecPtr is a managed pointer that knows how to delete |
| 81 | // itself. |
| 82 | using TypeSpecPtr = util::unique_cptr<TypeSpec>; |
| 83 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 84 | namespace { |
| 85 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 86 | // Builder that helps accumulate Type structs and then create a single |
| 87 | // contiguous block of memory to store both the TypeSpec struct and |
| 88 | // the Type structs. |
| 89 | class TypeSpecPtrBuilder { |
| 90 | public: |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 91 | explicit TypeSpecPtrBuilder(const ResTable_typeSpec* header, |
| 92 | const IdmapEntry_header* idmap_header) |
| 93 | : header_(header), idmap_header_(idmap_header) { |
| 94 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 95 | |
| 96 | void AddType(const ResTable_type* type) { |
| 97 | ResTable_config config; |
| 98 | config.copyFromDtoH(type->config); |
| 99 | types_.push_back(Type{config, type}); |
| 100 | } |
| 101 | |
| 102 | TypeSpecPtr Build() { |
| 103 | // Check for overflow. |
| 104 | if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(Type) < types_.size()) { |
| 105 | return {}; |
| 106 | } |
| 107 | TypeSpec* type_spec = (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(Type))); |
| 108 | type_spec->type_spec = header_; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 109 | type_spec->idmap_entries = idmap_header_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 110 | type_spec->type_count = types_.size(); |
| 111 | memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(Type)); |
| 112 | return TypeSpecPtr(type_spec); |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder); |
| 117 | |
| 118 | const ResTable_typeSpec* header_; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 119 | const IdmapEntry_header* idmap_header_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 120 | std::vector<Type> types_; |
| 121 | }; |
| 122 | |
| 123 | } // namespace |
| 124 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 125 | LoadedPackage::LoadedPackage() = default; |
| 126 | LoadedPackage::~LoadedPackage() = default; |
| 127 | |
| 128 | // Precondition: The header passed in has already been verified, so reading any fields and trusting |
| 129 | // the ResChunk_header is safe. |
| 130 | static bool VerifyResTableType(const ResTable_type* header) { |
| 131 | const size_t entry_count = dtohl(header->entryCount); |
| 132 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
| 133 | LOG(ERROR) << "Too many entries in RES_TABLE_TYPE_TYPE."; |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | // Make sure that there is enough room for the entry offsets. |
| 138 | const size_t offsets_offset = dtohs(header->header.headerSize); |
| 139 | const size_t entries_offset = dtohl(header->entriesStart); |
| 140 | const size_t offsets_length = sizeof(uint32_t) * entry_count; |
| 141 | |
| 142 | if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) { |
| 143 | LOG(ERROR) << "Entry offsets overlap actual entry data."; |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | if (entries_offset > dtohl(header->header.size)) { |
| 148 | LOG(ERROR) << "Entry offsets extend beyond chunk."; |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | if (entries_offset & 0x03) { |
| 153 | LOG(ERROR) << "Entries start at unaligned address."; |
| 154 | return false; |
| 155 | } |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | static bool VerifyResTableEntry(const ResTable_type* type, uint32_t entry_offset, |
| 160 | size_t entry_idx) { |
| 161 | // Check that the offset is aligned. |
| 162 | if (entry_offset & 0x03) { |
| 163 | LOG(ERROR) << "Entry offset at index " << entry_idx << " is not 4-byte aligned."; |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // Check that the offset doesn't overflow. |
| 168 | if (entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart)) { |
| 169 | // Overflow in offset. |
| 170 | LOG(ERROR) << "Entry offset at index " << entry_idx << " is too large."; |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | const size_t chunk_size = dtohl(type->header.size); |
| 175 | |
| 176 | entry_offset += dtohl(type->entriesStart); |
| 177 | if (entry_offset > chunk_size - sizeof(ResTable_entry)) { |
| 178 | LOG(ERROR) << "Entry offset at index " << entry_idx |
| 179 | << " is too large. No room for ResTable_entry."; |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>( |
| 184 | reinterpret_cast<const uint8_t*>(type) + entry_offset); |
| 185 | |
| 186 | const size_t entry_size = dtohs(entry->size); |
| 187 | if (entry_size < sizeof(*entry)) { |
| 188 | LOG(ERROR) << "ResTable_entry size " << entry_size << " at index " << entry_idx |
| 189 | << " is too small."; |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | if (entry_size > chunk_size || entry_offset > chunk_size - entry_size) { |
| 194 | LOG(ERROR) << "ResTable_entry size " << entry_size << " at index " << entry_idx |
| 195 | << " is too large."; |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | if (entry_size < sizeof(ResTable_map_entry)) { |
| 200 | // There needs to be room for one Res_value struct. |
| 201 | if (entry_offset + entry_size > chunk_size - sizeof(Res_value)) { |
| 202 | LOG(ERROR) << "No room for Res_value after ResTable_entry at index " << entry_idx |
| 203 | << " for type " << (int)type->id << "."; |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | const Res_value* value = |
| 208 | reinterpret_cast<const Res_value*>(reinterpret_cast<const uint8_t*>(entry) + entry_size); |
| 209 | const size_t value_size = dtohs(value->size); |
| 210 | if (value_size < sizeof(Res_value)) { |
| 211 | LOG(ERROR) << "Res_value at index " << entry_idx << " is too small."; |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | if (value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size) { |
| 216 | LOG(ERROR) << "Res_value size " << value_size << " at index " << entry_idx |
| 217 | << " is too large."; |
| 218 | return false; |
| 219 | } |
| 220 | } else { |
| 221 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry); |
| 222 | const size_t map_entry_count = dtohl(map->count); |
| 223 | size_t map_entries_start = entry_offset + entry_size; |
| 224 | if (map_entries_start & 0x03) { |
| 225 | LOG(ERROR) << "Map entries at index " << entry_idx << " start at unaligned offset."; |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | // Each entry is sizeof(ResTable_map) big. |
| 230 | if (map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map))) { |
| 231 | LOG(ERROR) << "Too many map entries in ResTable_map_entry at index " << entry_idx << "."; |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | template <bool Verified> |
| 239 | bool LoadedPackage::FindEntry(const TypeSpecPtr& type_spec_ptr, uint16_t entry_idx, |
| 240 | const ResTable_config& config, FindEntryResult* out_entry) const { |
| 241 | const ResTable_config* best_config = nullptr; |
| 242 | const ResTable_type* best_type = nullptr; |
| 243 | uint32_t best_offset = 0; |
| 244 | |
| 245 | for (uint32_t i = 0; i < type_spec_ptr->type_count; i++) { |
| 246 | const Type* type = &type_spec_ptr->types[i]; |
| 247 | |
| 248 | if (type->configuration.match(config) && |
| 249 | (best_config == nullptr || type->configuration.isBetterThan(*best_config, &config))) { |
| 250 | // The configuration matches and is better than the previous selection. |
| 251 | // Find the entry value if it exists for this configuration. |
| 252 | const size_t entry_count = dtohl(type->type->entryCount); |
| 253 | const size_t offsets_offset = dtohs(type->type->header.headerSize); |
| 254 | if (entry_idx < entry_count) { |
| 255 | // If the package hasn't been verified, do bounds checking. |
| 256 | if (!Verified) { |
| 257 | if (!VerifyResTableType(type->type)) { |
| 258 | continue; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>( |
| 263 | reinterpret_cast<const uint8_t*>(type->type) + offsets_offset); |
| 264 | const uint32_t offset = dtohl(entry_offsets[entry_idx]); |
| 265 | if (offset != ResTable_type::NO_ENTRY) { |
| 266 | // There is an entry for this resource, record it. |
| 267 | best_config = &type->configuration; |
| 268 | best_type = type->type; |
| 269 | best_offset = offset; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (best_type == nullptr) { |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | if (!Verified) { |
| 280 | if (!VerifyResTableEntry(best_type, best_offset, entry_idx)) { |
| 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | const ResTable_entry* best_entry = reinterpret_cast<const ResTable_entry*>( |
| 286 | reinterpret_cast<const uint8_t*>(best_type) + best_offset + dtohl(best_type->entriesStart)); |
| 287 | |
| 288 | const uint32_t* flags = reinterpret_cast<const uint32_t*>(type_spec_ptr->type_spec + 1); |
| 289 | out_entry->type_flags = dtohl(flags[entry_idx]); |
| 290 | out_entry->entry = best_entry; |
| 291 | out_entry->config = best_config; |
| 292 | out_entry->type_string_ref = StringPoolRef(&type_string_pool_, best_type->id - 1); |
| 293 | out_entry->entry_string_ref = StringPoolRef(&key_string_pool_, dtohl(best_entry->key.index)); |
| 294 | return true; |
| 295 | } |
| 296 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 297 | bool LoadedPackage::FindEntry(uint8_t type_idx, uint16_t entry_idx, const ResTable_config& config, |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 298 | FindEntryResult* out_entry) const { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 299 | ATRACE_CALL(); |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 300 | |
| 301 | // If the type IDs are offset in this package, we need to take that into account when searching |
| 302 | // for a type. |
| 303 | const TypeSpecPtr& ptr = type_specs_[type_idx - type_id_offset_]; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 304 | if (ptr == nullptr) { |
| 305 | return false; |
| 306 | } |
| 307 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 308 | // If there is an IDMAP supplied with this package, translate the entry ID. |
| 309 | if (ptr->idmap_entries != nullptr) { |
| 310 | if (!LoadedIdmap::Lookup(ptr->idmap_entries, entry_idx, &entry_idx)) { |
| 311 | // There is no mapping, so the resource is not meant to be in this overlay package. |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 316 | // Don't bother checking if the entry ID is larger than the number of entries. |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 317 | if (entry_idx >= dtohl(ptr->type_spec->entryCount)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 321 | if (verified_) { |
| 322 | return FindEntry<true>(ptr, entry_idx, config, out_entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 323 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 324 | return FindEntry<false>(ptr, entry_idx, config, out_entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static bool VerifyType(const Chunk& chunk) { |
| 328 | ATRACE_CALL(); |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 329 | const ResTable_type* header = chunk.header<ResTable_type, kResTableTypeMinSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 330 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 331 | if (!VerifyResTableType(header)) { |
| 332 | return false; |
| 333 | } |
| 334 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 335 | const size_t entry_count = dtohl(header->entryCount); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 336 | const size_t offsets_offset = chunk.header_size(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 337 | |
| 338 | // Check each entry offset. |
| 339 | const uint32_t* offsets = |
| 340 | reinterpret_cast<const uint32_t*>(reinterpret_cast<const uint8_t*>(header) + offsets_offset); |
| 341 | for (size_t i = 0; i < entry_count; i++) { |
| 342 | uint32_t offset = dtohl(offsets[i]); |
| 343 | if (offset != ResTable_type::NO_ENTRY) { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 344 | if (!VerifyResTableEntry(header, offset, i)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 345 | return false; |
| 346 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | return true; |
| 350 | } |
| 351 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 352 | void LoadedPackage::CollectConfigurations(bool exclude_mipmap, |
| 353 | std::set<ResTable_config>* out_configs) const { |
| 354 | const static std::u16string kMipMap = u"mipmap"; |
| 355 | const size_t type_count = type_specs_.size(); |
| 356 | for (size_t i = 0; i < type_count; i++) { |
| 357 | const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i]; |
| 358 | if (type_spec != nullptr) { |
| 359 | if (exclude_mipmap) { |
| 360 | const int type_idx = type_spec->type_spec->id - 1; |
| 361 | size_t type_name_len; |
| 362 | const char16_t* type_name16 = type_string_pool_.stringAt(type_idx, &type_name_len); |
| 363 | if (type_name16 != nullptr) { |
| 364 | if (kMipMap.compare(0, std::u16string::npos, type_name16, type_name_len) == 0) { |
| 365 | // This is a mipmap type, skip collection. |
| 366 | continue; |
| 367 | } |
| 368 | } |
| 369 | const char* type_name = type_string_pool_.string8At(type_idx, &type_name_len); |
| 370 | if (type_name != nullptr) { |
| 371 | if (strncmp(type_name, "mipmap", type_name_len) == 0) { |
| 372 | // This is a mipmap type, skip collection. |
| 373 | continue; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | for (size_t j = 0; j < type_spec->type_count; j++) { |
| 379 | out_configs->insert(type_spec->types[j].configuration); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const { |
| 386 | char temp_locale[RESTABLE_MAX_LOCALE_LEN]; |
| 387 | const size_t type_count = type_specs_.size(); |
| 388 | for (size_t i = 0; i < type_count; i++) { |
| 389 | const util::unique_cptr<TypeSpec>& type_spec = type_specs_[i]; |
| 390 | if (type_spec != nullptr) { |
| 391 | for (size_t j = 0; j < type_spec->type_count; j++) { |
| 392 | const ResTable_config& configuration = type_spec->types[j].configuration; |
| 393 | if (configuration.locale != 0) { |
| 394 | configuration.getBcp47Locale(temp_locale, canonicalize); |
| 395 | std::string locale(temp_locale); |
| 396 | out_locales->insert(std::move(locale)); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 403 | uint32_t LoadedPackage::FindEntryByName(const std::u16string& type_name, |
| 404 | const std::u16string& entry_name) const { |
| 405 | ssize_t type_idx = type_string_pool_.indexOfString(type_name.data(), type_name.size()); |
| 406 | if (type_idx < 0) { |
| 407 | return 0u; |
| 408 | } |
| 409 | |
| 410 | ssize_t key_idx = key_string_pool_.indexOfString(entry_name.data(), entry_name.size()); |
| 411 | if (key_idx < 0) { |
| 412 | return 0u; |
| 413 | } |
| 414 | |
| 415 | const TypeSpec* type_spec = type_specs_[type_idx].get(); |
| 416 | if (type_spec == nullptr) { |
| 417 | return 0u; |
| 418 | } |
| 419 | |
| 420 | for (size_t ti = 0; ti < type_spec->type_count; ti++) { |
| 421 | const Type* type = &type_spec->types[ti]; |
| 422 | size_t entry_count = dtohl(type->type->entryCount); |
| 423 | for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) { |
| 424 | const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>( |
| 425 | reinterpret_cast<const uint8_t*>(type->type) + dtohs(type->type->header.headerSize)); |
| 426 | const uint32_t offset = dtohl(entry_offsets[entry_idx]); |
| 427 | if (offset != ResTable_type::NO_ENTRY) { |
| 428 | const ResTable_entry* entry = |
| 429 | reinterpret_cast<const ResTable_entry*>(reinterpret_cast<const uint8_t*>(type->type) + |
| 430 | dtohl(type->type->entriesStart) + offset); |
| 431 | if (dtohl(entry->key.index) == static_cast<uint32_t>(key_idx)) { |
| 432 | // The package ID will be overridden by the caller (due to runtime assignment of package |
| 433 | // IDs for shared libraries). |
| 434 | return make_resid(0x00, type_idx + type_id_offset_ + 1, entry_idx); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | return 0u; |
| 440 | } |
| 441 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 442 | const LoadedPackage* LoadedArsc::GetPackageForId(uint32_t resid) const { |
| 443 | const uint8_t package_id = get_package_id(resid); |
| 444 | for (const auto& loaded_package : packages_) { |
| 445 | if (loaded_package->GetPackageId() == package_id) { |
| 446 | return loaded_package.get(); |
| 447 | } |
| 448 | } |
| 449 | return nullptr; |
| 450 | } |
| 451 | |
| 452 | std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk, |
| 453 | const LoadedIdmap* loaded_idmap, |
| 454 | bool system, bool load_as_shared_library) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 455 | ATRACE_CALL(); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 456 | std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 457 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 458 | // typeIdOffset was added at some point, but we still must recognize apps built before this |
| 459 | // was added. |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 460 | constexpr size_t kMinPackageSize = |
| 461 | sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset); |
| 462 | const ResTable_package* header = chunk.header<ResTable_package, kMinPackageSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 463 | if (header == nullptr) { |
| 464 | LOG(ERROR) << "Chunk RES_TABLE_PACKAGE_TYPE is too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 465 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 468 | loaded_package->system_ = system; |
| 469 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 470 | loaded_package->package_id_ = dtohl(header->id); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 471 | if (loaded_package->package_id_ == 0 || |
| 472 | (loaded_package->package_id_ == kAppPackageId && load_as_shared_library)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 473 | // Package ID of 0 means this is a shared library. |
| 474 | loaded_package->dynamic_ = true; |
| 475 | } |
| 476 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 477 | if (loaded_idmap != nullptr) { |
| 478 | // This is an overlay and so it needs to pretend to be the target package. |
| 479 | loaded_package->package_id_ = loaded_idmap->TargetPackageId(); |
| 480 | loaded_package->overlay_ = true; |
| 481 | } |
| 482 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 483 | if (header->header.headerSize >= sizeof(ResTable_package)) { |
| 484 | uint32_t type_id_offset = dtohl(header->typeIdOffset); |
| 485 | if (type_id_offset > std::numeric_limits<uint8_t>::max()) { |
| 486 | LOG(ERROR) << "Type ID offset in RES_TABLE_PACKAGE_TYPE is too large."; |
| 487 | return {}; |
| 488 | } |
| 489 | loaded_package->type_id_offset_ = static_cast<int>(type_id_offset); |
| 490 | } |
| 491 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 492 | util::ReadUtf16StringFromDevice(header->name, arraysize(header->name), |
| 493 | &loaded_package->package_name_); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 494 | |
| 495 | // A TypeSpec builder. We use this to accumulate the set of Types |
| 496 | // available for a TypeSpec, and later build a single, contiguous block |
| 497 | // of memory that holds all the Types together with the TypeSpec. |
| 498 | std::unique_ptr<TypeSpecPtrBuilder> types_builder; |
| 499 | |
| 500 | // Keep track of the last seen type index. Since type IDs are 1-based, |
| 501 | // this records their index, which is 0-based (type ID - 1). |
| 502 | uint8_t last_type_idx = 0; |
| 503 | |
| 504 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 505 | while (iter.HasNext()) { |
| 506 | const Chunk child_chunk = iter.Next(); |
| 507 | switch (child_chunk.type()) { |
| 508 | case RES_STRING_POOL_TYPE: { |
| 509 | const uintptr_t pool_address = |
| 510 | reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>()); |
| 511 | const uintptr_t header_address = reinterpret_cast<uintptr_t>(header); |
| 512 | if (pool_address == header_address + dtohl(header->typeStrings)) { |
| 513 | // This string pool is the type string pool. |
| 514 | status_t err = loaded_package->type_string_pool_.setTo( |
| 515 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 516 | if (err != NO_ERROR) { |
| 517 | LOG(ERROR) << "Corrupt package type string pool."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 518 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 519 | } |
| 520 | } else if (pool_address == header_address + dtohl(header->keyStrings)) { |
| 521 | // This string pool is the key string pool. |
| 522 | status_t err = loaded_package->key_string_pool_.setTo( |
| 523 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 524 | if (err != NO_ERROR) { |
| 525 | LOG(ERROR) << "Corrupt package key string pool."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 526 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 527 | } |
| 528 | } else { |
| 529 | LOG(WARNING) << "Too many string pool chunks found in package."; |
| 530 | } |
| 531 | } break; |
| 532 | |
| 533 | case RES_TABLE_TYPE_SPEC_TYPE: { |
| 534 | ATRACE_NAME("LoadTableTypeSpec"); |
| 535 | |
| 536 | // Starting a new TypeSpec, so finish the old one if there was one. |
| 537 | if (types_builder) { |
| 538 | TypeSpecPtr type_spec_ptr = types_builder->Build(); |
| 539 | if (type_spec_ptr == nullptr) { |
| 540 | LOG(ERROR) << "Too many type configurations, overflow detected."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 541 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 542 | } |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 543 | |
| 544 | // We only add the type to the package if there is no IDMAP, or if the type is |
| 545 | // overlaying something. |
| 546 | if (loaded_idmap == nullptr || type_spec_ptr->idmap_entries != nullptr) { |
| 547 | // If this is an overlay, insert it at the target type ID. |
| 548 | if (type_spec_ptr->idmap_entries != nullptr) { |
| 549 | last_type_idx = dtohs(type_spec_ptr->idmap_entries->target_type_id) - 1; |
| 550 | } |
| 551 | loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr); |
| 552 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 553 | |
| 554 | types_builder = {}; |
| 555 | last_type_idx = 0; |
| 556 | } |
| 557 | |
| 558 | const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>(); |
| 559 | if (type_spec == nullptr) { |
| 560 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE is too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 561 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | if (type_spec->id == 0) { |
| 565 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 566 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 569 | if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) > |
| 570 | std::numeric_limits<uint8_t>::max()) { |
| 571 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_SPEC_TYPE has out of range ID."; |
| 572 | return {}; |
| 573 | } |
| 574 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 575 | // The data portion of this chunk contains entry_count 32bit entries, |
| 576 | // each one representing a set of flags. |
| 577 | // Here we only validate that the chunk is well formed. |
| 578 | const size_t entry_count = dtohl(type_spec->entryCount); |
| 579 | |
| 580 | // There can only be 2^16 entries in a type, because that is the ID |
| 581 | // space for entries (EEEE) in the resource ID 0xPPTTEEEE. |
| 582 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
| 583 | LOG(ERROR) << "Too many entries in RES_TABLE_TYPE_SPEC_TYPE: " << entry_count << "."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 584 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | if (entry_count * sizeof(uint32_t) > chunk.data_size()) { |
| 588 | LOG(ERROR) << "Chunk too small to hold entries in RES_TABLE_TYPE_SPEC_TYPE."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 589 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | last_type_idx = type_spec->id - 1; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 593 | |
| 594 | // If this is an overlay, associate the mapping of this type to the target type |
| 595 | // from the IDMAP. |
| 596 | const IdmapEntry_header* idmap_entry_header = nullptr; |
| 597 | if (loaded_idmap != nullptr) { |
| 598 | idmap_entry_header = loaded_idmap->GetEntryMapForType(type_spec->id); |
| 599 | } |
| 600 | |
| 601 | types_builder = util::make_unique<TypeSpecPtrBuilder>(type_spec, idmap_entry_header); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 602 | } break; |
| 603 | |
| 604 | case RES_TABLE_TYPE_TYPE: { |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 605 | const ResTable_type* type = child_chunk.header<ResTable_type, kResTableTypeMinSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 606 | if (type == nullptr) { |
| 607 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_TYPE is too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 608 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | if (type->id == 0) { |
| 612 | LOG(ERROR) << "Chunk RES_TABLE_TYPE_TYPE has invalid ID 0."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 613 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // Type chunks must be preceded by their TypeSpec chunks. |
| 617 | if (!types_builder || type->id - 1 != last_type_idx) { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 618 | LOG(ERROR) << "Found RES_TABLE_TYPE_TYPE chunk without RES_TABLE_TYPE_SPEC_TYPE."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 619 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 622 | // Only verify the type if we haven't already failed verification. |
| 623 | if (loaded_package->verified_) { |
| 624 | if (!VerifyType(child_chunk)) { |
| 625 | LOG(WARNING) << "Package failed verification, resource retrieval may be slower"; |
| 626 | loaded_package->verified_ = false; |
| 627 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | types_builder->AddType(type); |
| 631 | } break; |
| 632 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 633 | case RES_TABLE_LIBRARY_TYPE: { |
| 634 | const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>(); |
| 635 | if (lib == nullptr) { |
| 636 | LOG(ERROR) << "Chunk RES_TABLE_LIBRARY_TYPE is too small."; |
| 637 | return {}; |
| 638 | } |
| 639 | |
| 640 | if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) { |
| 641 | LOG(ERROR) << "Chunk too small to hold entries in RES_TABLE_LIBRARY_TYPE."; |
| 642 | return {}; |
| 643 | } |
| 644 | |
| 645 | loaded_package->dynamic_package_map_.reserve(dtohl(lib->count)); |
| 646 | |
| 647 | const ResTable_lib_entry* const entry_begin = |
| 648 | reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr()); |
| 649 | const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count); |
| 650 | for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) { |
| 651 | std::string package_name; |
| 652 | util::ReadUtf16StringFromDevice(entry_iter->packageName, |
| 653 | arraysize(entry_iter->packageName), &package_name); |
| 654 | |
| 655 | if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) { |
| 656 | LOG(ERROR) << base::StringPrintf( |
| 657 | "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.", |
| 658 | dtohl(entry_iter->packageId), package_name.c_str()); |
| 659 | return {}; |
| 660 | } |
| 661 | |
| 662 | loaded_package->dynamic_package_map_.emplace_back(std::move(package_name), |
| 663 | dtohl(entry_iter->packageId)); |
| 664 | } |
| 665 | |
| 666 | } break; |
| 667 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 668 | default: |
| 669 | LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Finish the last TypeSpec. |
| 675 | if (types_builder) { |
| 676 | TypeSpecPtr type_spec_ptr = types_builder->Build(); |
| 677 | if (type_spec_ptr == nullptr) { |
| 678 | LOG(ERROR) << "Too many type configurations, overflow detected."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 679 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 680 | } |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 681 | |
| 682 | // We only add the type to the package if there is no IDMAP, or if the type is |
| 683 | // overlaying something. |
| 684 | if (loaded_idmap == nullptr || type_spec_ptr->idmap_entries != nullptr) { |
| 685 | // If this is an overlay, insert it at the target type ID. |
| 686 | if (type_spec_ptr->idmap_entries != nullptr) { |
| 687 | last_type_idx = dtohs(type_spec_ptr->idmap_entries->target_type_id) - 1; |
| 688 | } |
| 689 | loaded_package->type_specs_.editItemAt(last_type_idx) = std::move(type_spec_ptr); |
| 690 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (iter.HadError()) { |
| 694 | LOG(ERROR) << iter.GetLastError(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 695 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 696 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 697 | return std::move(loaded_package); |
| 698 | } |
| 699 | |
| 700 | bool LoadedArsc::FindEntry(uint32_t resid, const ResTable_config& config, |
| 701 | FindEntryResult* out_entry) const { |
| 702 | ATRACE_CALL(); |
| 703 | |
| 704 | const uint8_t package_id = get_package_id(resid); |
| 705 | const uint8_t type_id = get_type_id(resid); |
| 706 | const uint16_t entry_id = get_entry_id(resid); |
| 707 | |
| 708 | if (type_id == 0) { |
| 709 | LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid); |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | for (const auto& loaded_package : packages_) { |
| 714 | if (loaded_package->GetPackageId() == package_id) { |
| 715 | return loaded_package->FindEntry(type_id - 1, entry_id, config, out_entry); |
| 716 | } |
| 717 | } |
| 718 | return false; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 721 | bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap, |
| 722 | bool load_as_shared_library) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 723 | ATRACE_CALL(); |
| 724 | const ResTable_header* header = chunk.header<ResTable_header>(); |
| 725 | if (header == nullptr) { |
| 726 | LOG(ERROR) << "Chunk RES_TABLE_TYPE is too small."; |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | const size_t package_count = dtohl(header->packageCount); |
| 731 | size_t packages_seen = 0; |
| 732 | |
| 733 | packages_.reserve(package_count); |
| 734 | |
| 735 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 736 | while (iter.HasNext()) { |
| 737 | const Chunk child_chunk = iter.Next(); |
| 738 | switch (child_chunk.type()) { |
| 739 | case RES_STRING_POOL_TYPE: |
| 740 | // Only use the first string pool. Ignore others. |
| 741 | if (global_string_pool_.getError() == NO_INIT) { |
| 742 | status_t err = global_string_pool_.setTo(child_chunk.header<ResStringPool_header>(), |
| 743 | child_chunk.size()); |
| 744 | if (err != NO_ERROR) { |
| 745 | LOG(ERROR) << "Corrupt string pool."; |
| 746 | return false; |
| 747 | } |
| 748 | } else { |
| 749 | LOG(WARNING) << "Multiple string pool chunks found in resource table."; |
| 750 | } |
| 751 | break; |
| 752 | |
| 753 | case RES_TABLE_PACKAGE_TYPE: { |
| 754 | if (packages_seen + 1 > package_count) { |
| 755 | LOG(ERROR) << "More package chunks were found than the " << package_count |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 756 | << " declared in the header."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 757 | return false; |
| 758 | } |
| 759 | packages_seen++; |
| 760 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame^] | 761 | std::unique_ptr<const LoadedPackage> loaded_package = |
| 762 | LoadedPackage::Load(child_chunk, loaded_idmap, system_, load_as_shared_library); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 763 | if (!loaded_package) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 764 | return false; |
| 765 | } |
| 766 | packages_.push_back(std::move(loaded_package)); |
| 767 | } break; |
| 768 | |
| 769 | default: |
| 770 | LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | if (iter.HadError()) { |
| 776 | LOG(ERROR) << iter.GetLastError(); |
| 777 | return false; |
| 778 | } |
| 779 | return true; |
| 780 | } |
| 781 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 782 | std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const StringPiece& data, |
| 783 | const LoadedIdmap* loaded_idmap, bool system, |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 784 | bool load_as_shared_library) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 785 | ATRACE_CALL(); |
| 786 | |
| 787 | // Not using make_unique because the constructor is private. |
| 788 | std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc()); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 789 | loaded_arsc->system_ = system; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 790 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 791 | ChunkIterator iter(data.data(), data.size()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 792 | while (iter.HasNext()) { |
| 793 | const Chunk chunk = iter.Next(); |
| 794 | switch (chunk.type()) { |
| 795 | case RES_TABLE_TYPE: |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 796 | if (!loaded_arsc->LoadTable(chunk, loaded_idmap, load_as_shared_library)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 797 | return {}; |
| 798 | } |
| 799 | break; |
| 800 | |
| 801 | default: |
| 802 | LOG(WARNING) << base::StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | if (iter.HadError()) { |
| 808 | LOG(ERROR) << iter.GetLastError(); |
| 809 | return {}; |
| 810 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 811 | |
| 812 | // Need to force a move for mingw32. |
| 813 | return std::move(loaded_arsc); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 816 | std::unique_ptr<const LoadedArsc> LoadedArsc::CreateEmpty() { |
| 817 | return std::unique_ptr<LoadedArsc>(new LoadedArsc()); |
| 818 | } |
| 819 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 820 | } // namespace android |