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 | |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 21 | #include <algorithm> |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 22 | #include <cstddef> |
| 23 | #include <limits> |
| 24 | |
| 25 | #include "android-base/logging.h" |
| 26 | #include "android-base/stringprintf.h" |
| 27 | #include "utils/ByteOrder.h" |
| 28 | #include "utils/Trace.h" |
| 29 | |
| 30 | #ifdef _WIN32 |
| 31 | #ifdef ERROR |
| 32 | #undef ERROR |
| 33 | #endif |
| 34 | #endif |
| 35 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 36 | #include "androidfw/ByteBucketArray.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 37 | #include "androidfw/Chunk.h" |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 38 | #include "androidfw/ResourceUtils.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 39 | #include "androidfw/Util.h" |
| 40 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 41 | using ::android::base::StringPrintf; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 42 | |
| 43 | namespace android { |
| 44 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 45 | constexpr const static int kAppPackageId = 0x7f; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 47 | namespace { |
| 48 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 49 | // Builder that helps accumulate Type structs and then create a single |
| 50 | // contiguous block of memory to store both the TypeSpec struct and |
| 51 | // the Type structs. |
| 52 | class TypeSpecPtrBuilder { |
| 53 | public: |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 54 | explicit TypeSpecPtrBuilder(const ResTable_typeSpec* header, |
| 55 | const IdmapEntry_header* idmap_header) |
| 56 | : header_(header), idmap_header_(idmap_header) { |
| 57 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 58 | |
| 59 | void AddType(const ResTable_type* type) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 60 | types_.push_back(type); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TypeSpecPtr Build() { |
| 64 | // Check for overflow. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 65 | using ElementType = const ResTable_type*; |
| 66 | if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(ElementType) < |
| 67 | types_.size()) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 68 | return {}; |
| 69 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 70 | TypeSpec* type_spec = |
| 71 | (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(ElementType))); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 72 | type_spec->type_spec = header_; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 73 | type_spec->idmap_entries = idmap_header_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 74 | type_spec->type_count = types_.size(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 75 | memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(ElementType)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 76 | return TypeSpecPtr(type_spec); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder); |
| 81 | |
| 82 | const ResTable_typeSpec* header_; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 83 | const IdmapEntry_header* idmap_header_; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 84 | std::vector<const ResTable_type*> types_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | } // namespace |
| 88 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 89 | LoadedPackage::LoadedPackage() = default; |
| 90 | LoadedPackage::~LoadedPackage() = default; |
| 91 | |
| 92 | // Precondition: The header passed in has already been verified, so reading any fields and trusting |
| 93 | // the ResChunk_header is safe. |
| 94 | static bool VerifyResTableType(const ResTable_type* header) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 95 | if (header->id == 0) { |
| 96 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0."; |
| 97 | return false; |
| 98 | } |
| 99 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 100 | const size_t entry_count = dtohl(header->entryCount); |
| 101 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 102 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ")."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // Make sure that there is enough room for the entry offsets. |
| 107 | const size_t offsets_offset = dtohs(header->header.headerSize); |
| 108 | const size_t entries_offset = dtohl(header->entriesStart); |
| 109 | const size_t offsets_length = sizeof(uint32_t) * entry_count; |
| 110 | |
| 111 | if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 112 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if (entries_offset > dtohl(header->header.size)) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 117 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if (entries_offset & 0x03) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 122 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 128 | static bool VerifyResTableEntry(const ResTable_type* type, uint32_t entry_offset) { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 129 | // Check that the offset is aligned. |
| 130 | if (entry_offset & 0x03) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 131 | LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | |
| 135 | // Check that the offset doesn't overflow. |
| 136 | if (entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart)) { |
| 137 | // Overflow in offset. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 138 | LOG(ERROR) << "Entry at offset " << entry_offset << " is too large."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | |
| 142 | const size_t chunk_size = dtohl(type->header.size); |
| 143 | |
| 144 | entry_offset += dtohl(type->entriesStart); |
| 145 | if (entry_offset > chunk_size - sizeof(ResTable_entry)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 146 | LOG(ERROR) << "Entry at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 147 | << " is too large. No room for ResTable_entry."; |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>( |
| 152 | reinterpret_cast<const uint8_t*>(type) + entry_offset); |
| 153 | |
| 154 | const size_t entry_size = dtohs(entry->size); |
| 155 | if (entry_size < sizeof(*entry)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 156 | LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 157 | << " is too small."; |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | if (entry_size > chunk_size || entry_offset > chunk_size - entry_size) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 162 | LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 163 | << " is too large."; |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | if (entry_size < sizeof(ResTable_map_entry)) { |
| 168 | // There needs to be room for one Res_value struct. |
| 169 | if (entry_offset + entry_size > chunk_size - sizeof(Res_value)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 170 | LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 171 | << " for type " << (int)type->id << "."; |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | const Res_value* value = |
| 176 | reinterpret_cast<const Res_value*>(reinterpret_cast<const uint8_t*>(entry) + entry_size); |
| 177 | const size_t value_size = dtohs(value->size); |
| 178 | if (value_size < sizeof(Res_value)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 179 | LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
| 183 | if (value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 184 | LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 185 | << " is too large."; |
| 186 | return false; |
| 187 | } |
| 188 | } else { |
| 189 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry); |
| 190 | const size_t map_entry_count = dtohl(map->count); |
| 191 | size_t map_entries_start = entry_offset + entry_size; |
| 192 | if (map_entries_start & 0x03) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 193 | LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 194 | return false; |
| 195 | } |
| 196 | |
| 197 | // Each entry is sizeof(ResTable_map) big. |
| 198 | if (map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map))) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 199 | LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << "."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 206 | const ResTable_entry* LoadedPackage::GetEntry(const ResTable_type* type_chunk, |
| 207 | uint16_t entry_index) { |
| 208 | uint32_t entry_offset = GetEntryOffset(type_chunk, entry_index); |
| 209 | if (entry_offset == ResTable_type::NO_ENTRY) { |
| 210 | return nullptr; |
| 211 | } |
| 212 | return GetEntryFromOffset(type_chunk, entry_offset); |
| 213 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 214 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 215 | uint32_t LoadedPackage::GetEntryOffset(const ResTable_type* type_chunk, uint16_t entry_index) { |
| 216 | // The configuration matches and is better than the previous selection. |
| 217 | // Find the entry value if it exists for this configuration. |
| 218 | const size_t entry_count = dtohl(type_chunk->entryCount); |
| 219 | const size_t offsets_offset = dtohs(type_chunk->header.headerSize); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 220 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 221 | // Check if there is the desired entry in this type. |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 222 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 223 | if (type_chunk->flags & ResTable_type::FLAG_SPARSE) { |
| 224 | // This is encoded as a sparse map, so perform a binary search. |
| 225 | const ResTable_sparseTypeEntry* sparse_indices = |
| 226 | reinterpret_cast<const ResTable_sparseTypeEntry*>( |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 227 | reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 228 | const ResTable_sparseTypeEntry* sparse_indices_end = sparse_indices + entry_count; |
| 229 | const ResTable_sparseTypeEntry* result = |
| 230 | std::lower_bound(sparse_indices, sparse_indices_end, entry_index, |
| 231 | [](const ResTable_sparseTypeEntry& entry, uint16_t entry_idx) { |
| 232 | return dtohs(entry.idx) < entry_idx; |
| 233 | }); |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 234 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 235 | if (result == sparse_indices_end || dtohs(result->idx) != entry_index) { |
| 236 | // No entry found. |
| 237 | return ResTable_type::NO_ENTRY; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 238 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 239 | |
| 240 | // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as |
| 241 | // the real offset divided by 4. |
| 242 | return uint32_t{dtohs(result->offset)} * 4u; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 245 | // This type is encoded as a dense array. |
| 246 | if (entry_index >= entry_count) { |
| 247 | // This entry cannot be here. |
| 248 | return ResTable_type::NO_ENTRY; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 251 | const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>( |
| 252 | reinterpret_cast<const uint8_t*>(type_chunk) + offsets_offset); |
| 253 | return dtohl(entry_offsets[entry_index]); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 256 | const ResTable_entry* LoadedPackage::GetEntryFromOffset(const ResTable_type* type_chunk, |
| 257 | uint32_t offset) { |
| 258 | if (UNLIKELY(!VerifyResTableEntry(type_chunk, offset))) { |
| 259 | return nullptr; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 260 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 261 | return reinterpret_cast<const ResTable_entry*>(reinterpret_cast<const uint8_t*>(type_chunk) + |
| 262 | offset + dtohl(type_chunk->entriesStart)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 265 | void LoadedPackage::CollectConfigurations(bool exclude_mipmap, |
| 266 | std::set<ResTable_config>* out_configs) const { |
| 267 | const static std::u16string kMipMap = u"mipmap"; |
| 268 | const size_t type_count = type_specs_.size(); |
| 269 | for (size_t i = 0; i < type_count; i++) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 270 | const TypeSpecPtr& type_spec = type_specs_[i]; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 271 | if (type_spec != nullptr) { |
| 272 | if (exclude_mipmap) { |
| 273 | const int type_idx = type_spec->type_spec->id - 1; |
| 274 | size_t type_name_len; |
| 275 | const char16_t* type_name16 = type_string_pool_.stringAt(type_idx, &type_name_len); |
| 276 | if (type_name16 != nullptr) { |
| 277 | if (kMipMap.compare(0, std::u16string::npos, type_name16, type_name_len) == 0) { |
| 278 | // This is a mipmap type, skip collection. |
| 279 | continue; |
| 280 | } |
| 281 | } |
| 282 | const char* type_name = type_string_pool_.string8At(type_idx, &type_name_len); |
| 283 | if (type_name != nullptr) { |
| 284 | if (strncmp(type_name, "mipmap", type_name_len) == 0) { |
| 285 | // This is a mipmap type, skip collection. |
| 286 | continue; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 291 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 292 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
| 293 | ResTable_config config; |
| 294 | config.copyFromDtoH((*iter)->config); |
| 295 | out_configs->insert(config); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const { |
| 302 | char temp_locale[RESTABLE_MAX_LOCALE_LEN]; |
| 303 | const size_t type_count = type_specs_.size(); |
| 304 | for (size_t i = 0; i < type_count; i++) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 305 | const TypeSpecPtr& type_spec = type_specs_[i]; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 306 | if (type_spec != nullptr) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 307 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 308 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
| 309 | ResTable_config configuration; |
| 310 | configuration.copyFromDtoH((*iter)->config); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 311 | if (configuration.locale != 0) { |
| 312 | configuration.getBcp47Locale(temp_locale, canonicalize); |
| 313 | std::string locale(temp_locale); |
| 314 | out_locales->insert(std::move(locale)); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 321 | uint32_t LoadedPackage::FindEntryByName(const std::u16string& type_name, |
| 322 | const std::u16string& entry_name) const { |
| 323 | ssize_t type_idx = type_string_pool_.indexOfString(type_name.data(), type_name.size()); |
| 324 | if (type_idx < 0) { |
| 325 | return 0u; |
| 326 | } |
| 327 | |
| 328 | ssize_t key_idx = key_string_pool_.indexOfString(entry_name.data(), entry_name.size()); |
| 329 | if (key_idx < 0) { |
| 330 | return 0u; |
| 331 | } |
| 332 | |
| 333 | const TypeSpec* type_spec = type_specs_[type_idx].get(); |
| 334 | if (type_spec == nullptr) { |
| 335 | return 0u; |
| 336 | } |
| 337 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 338 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 339 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
| 340 | const ResTable_type* type = *iter; |
| 341 | size_t entry_count = dtohl(type->entryCount); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 342 | for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) { |
| 343 | const uint32_t* entry_offsets = reinterpret_cast<const uint32_t*>( |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 344 | reinterpret_cast<const uint8_t*>(type) + dtohs(type->header.headerSize)); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 345 | const uint32_t offset = dtohl(entry_offsets[entry_idx]); |
| 346 | if (offset != ResTable_type::NO_ENTRY) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 347 | const ResTable_entry* entry = reinterpret_cast<const ResTable_entry*>( |
| 348 | reinterpret_cast<const uint8_t*>(type) + dtohl(type->entriesStart) + offset); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 349 | if (dtohl(entry->key.index) == static_cast<uint32_t>(key_idx)) { |
| 350 | // The package ID will be overridden by the caller (due to runtime assignment of package |
| 351 | // IDs for shared libraries). |
| 352 | return make_resid(0x00, type_idx + type_id_offset_ + 1, entry_idx); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | return 0u; |
| 358 | } |
| 359 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 360 | const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 361 | for (const auto& loaded_package : packages_) { |
| 362 | if (loaded_package->GetPackageId() == package_id) { |
| 363 | return loaded_package.get(); |
| 364 | } |
| 365 | } |
| 366 | return nullptr; |
| 367 | } |
| 368 | |
| 369 | std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk, |
| 370 | const LoadedIdmap* loaded_idmap, |
| 371 | bool system, bool load_as_shared_library) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 372 | ATRACE_NAME("LoadedPackage::Load"); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 373 | std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 374 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 375 | // typeIdOffset was added at some point, but we still must recognize apps built before this |
| 376 | // was added. |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 377 | constexpr size_t kMinPackageSize = |
| 378 | sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset); |
| 379 | const ResTable_package* header = chunk.header<ResTable_package, kMinPackageSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 380 | if (header == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 381 | LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 382 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 385 | loaded_package->system_ = system; |
| 386 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 387 | loaded_package->package_id_ = dtohl(header->id); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 388 | if (loaded_package->package_id_ == 0 || |
| 389 | (loaded_package->package_id_ == kAppPackageId && load_as_shared_library)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 390 | // Package ID of 0 means this is a shared library. |
| 391 | loaded_package->dynamic_ = true; |
| 392 | } |
| 393 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 394 | if (loaded_idmap != nullptr) { |
| 395 | // This is an overlay and so it needs to pretend to be the target package. |
| 396 | loaded_package->package_id_ = loaded_idmap->TargetPackageId(); |
| 397 | loaded_package->overlay_ = true; |
| 398 | } |
| 399 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 400 | if (header->header.headerSize >= sizeof(ResTable_package)) { |
| 401 | uint32_t type_id_offset = dtohl(header->typeIdOffset); |
| 402 | if (type_id_offset > std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 403 | LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large."; |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 404 | return {}; |
| 405 | } |
| 406 | loaded_package->type_id_offset_ = static_cast<int>(type_id_offset); |
| 407 | } |
| 408 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 409 | util::ReadUtf16StringFromDevice(header->name, arraysize(header->name), |
| 410 | &loaded_package->package_name_); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 411 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 412 | // A map of TypeSpec builders, each associated with an type index. |
| 413 | // We use these to accumulate the set of Types available for a TypeSpec, and later build a single, |
| 414 | // contiguous block of memory that holds all the Types together with the TypeSpec. |
| 415 | std::unordered_map<int, std::unique_ptr<TypeSpecPtrBuilder>> type_builder_map; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 416 | |
| 417 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 418 | while (iter.HasNext()) { |
| 419 | const Chunk child_chunk = iter.Next(); |
| 420 | switch (child_chunk.type()) { |
| 421 | case RES_STRING_POOL_TYPE: { |
| 422 | const uintptr_t pool_address = |
| 423 | reinterpret_cast<uintptr_t>(child_chunk.header<ResChunk_header>()); |
| 424 | const uintptr_t header_address = reinterpret_cast<uintptr_t>(header); |
| 425 | if (pool_address == header_address + dtohl(header->typeStrings)) { |
| 426 | // This string pool is the type string pool. |
| 427 | status_t err = loaded_package->type_string_pool_.setTo( |
| 428 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 429 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 430 | LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 431 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 432 | } |
| 433 | } else if (pool_address == header_address + dtohl(header->keyStrings)) { |
| 434 | // This string pool is the key string pool. |
| 435 | status_t err = loaded_package->key_string_pool_.setTo( |
| 436 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 437 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 438 | LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 439 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 440 | } |
| 441 | } else { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 442 | LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 443 | } |
| 444 | } break; |
| 445 | |
| 446 | case RES_TABLE_TYPE_SPEC_TYPE: { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 447 | const ResTable_typeSpec* type_spec = child_chunk.header<ResTable_typeSpec>(); |
| 448 | if (type_spec == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 449 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 450 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | if (type_spec->id == 0) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 454 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 455 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 458 | if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) > |
| 459 | std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 460 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID."; |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 461 | return {}; |
| 462 | } |
| 463 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 464 | // The data portion of this chunk contains entry_count 32bit entries, |
| 465 | // each one representing a set of flags. |
| 466 | // Here we only validate that the chunk is well formed. |
| 467 | const size_t entry_count = dtohl(type_spec->entryCount); |
| 468 | |
| 469 | // There can only be 2^16 entries in a type, because that is the ID |
| 470 | // space for entries (EEEE) in the resource ID 0xPPTTEEEE. |
| 471 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 472 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ")."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 473 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | if (entry_count * sizeof(uint32_t) > chunk.data_size()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 477 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 478 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 481 | // If this is an overlay, associate the mapping of this type to the target type |
| 482 | // from the IDMAP. |
| 483 | const IdmapEntry_header* idmap_entry_header = nullptr; |
| 484 | if (loaded_idmap != nullptr) { |
| 485 | idmap_entry_header = loaded_idmap->GetEntryMapForType(type_spec->id); |
| 486 | } |
| 487 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 488 | std::unique_ptr<TypeSpecPtrBuilder>& builder_ptr = type_builder_map[type_spec->id - 1]; |
| 489 | if (builder_ptr == nullptr) { |
| 490 | builder_ptr = util::make_unique<TypeSpecPtrBuilder>(type_spec, idmap_entry_header); |
| 491 | } else { |
| 492 | LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x", |
| 493 | type_spec->id); |
| 494 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 495 | } break; |
| 496 | |
| 497 | case RES_TABLE_TYPE_TYPE: { |
Adam Lesinski | 136fd07 | 2017-03-03 13:50:21 -0800 | [diff] [blame] | 498 | const ResTable_type* type = child_chunk.header<ResTable_type, kResTableTypeMinSize>(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 499 | if (type == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 500 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 501 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 504 | if (!VerifyResTableType(type)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 505 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // Type chunks must be preceded by their TypeSpec chunks. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 509 | std::unique_ptr<TypeSpecPtrBuilder>& builder_ptr = type_builder_map[type->id - 1]; |
| 510 | if (builder_ptr != nullptr) { |
| 511 | builder_ptr->AddType(type); |
| 512 | } else { |
| 513 | LOG(ERROR) << StringPrintf( |
| 514 | "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.", |
| 515 | type->id); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 516 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 517 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 518 | } break; |
| 519 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 520 | case RES_TABLE_LIBRARY_TYPE: { |
| 521 | const ResTable_lib_header* lib = child_chunk.header<ResTable_lib_header>(); |
| 522 | if (lib == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 523 | LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 524 | return {}; |
| 525 | } |
| 526 | |
| 527 | if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 528 | LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 529 | return {}; |
| 530 | } |
| 531 | |
| 532 | loaded_package->dynamic_package_map_.reserve(dtohl(lib->count)); |
| 533 | |
| 534 | const ResTable_lib_entry* const entry_begin = |
| 535 | reinterpret_cast<const ResTable_lib_entry*>(child_chunk.data_ptr()); |
| 536 | const ResTable_lib_entry* const entry_end = entry_begin + dtohl(lib->count); |
| 537 | for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) { |
| 538 | std::string package_name; |
| 539 | util::ReadUtf16StringFromDevice(entry_iter->packageName, |
| 540 | arraysize(entry_iter->packageName), &package_name); |
| 541 | |
| 542 | if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 543 | LOG(ERROR) << StringPrintf( |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 544 | "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.", |
| 545 | dtohl(entry_iter->packageId), package_name.c_str()); |
| 546 | return {}; |
| 547 | } |
| 548 | |
| 549 | loaded_package->dynamic_package_map_.emplace_back(std::move(package_name), |
| 550 | dtohl(entry_iter->packageId)); |
| 551 | } |
| 552 | |
| 553 | } break; |
| 554 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 555 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 556 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 557 | break; |
| 558 | } |
| 559 | } |
| 560 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 561 | if (iter.HadError()) { |
| 562 | LOG(ERROR) << iter.GetLastError(); |
| 563 | return {}; |
| 564 | } |
| 565 | |
| 566 | // Flatten and construct the TypeSpecs. |
| 567 | for (auto& entry : type_builder_map) { |
| 568 | uint8_t type_idx = static_cast<uint8_t>(entry.first); |
| 569 | TypeSpecPtr type_spec_ptr = entry.second->Build(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 570 | if (type_spec_ptr == nullptr) { |
| 571 | LOG(ERROR) << "Too many type configurations, overflow detected."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 572 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 573 | } |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 574 | |
| 575 | // We only add the type to the package if there is no IDMAP, or if the type is |
| 576 | // overlaying something. |
| 577 | if (loaded_idmap == nullptr || type_spec_ptr->idmap_entries != nullptr) { |
| 578 | // If this is an overlay, insert it at the target type ID. |
| 579 | if (type_spec_ptr->idmap_entries != nullptr) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 580 | type_idx = dtohs(type_spec_ptr->idmap_entries->target_type_id) - 1; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 581 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 582 | loaded_package->type_specs_.editItemAt(type_idx) = std::move(type_spec_ptr); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 583 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 586 | return std::move(loaded_package); |
| 587 | } |
| 588 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 589 | bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap, |
| 590 | bool load_as_shared_library) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 591 | const ResTable_header* header = chunk.header<ResTable_header>(); |
| 592 | if (header == nullptr) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 593 | LOG(ERROR) << "RES_TABLE_TYPE too small."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 594 | return false; |
| 595 | } |
| 596 | |
| 597 | const size_t package_count = dtohl(header->packageCount); |
| 598 | size_t packages_seen = 0; |
| 599 | |
| 600 | packages_.reserve(package_count); |
| 601 | |
| 602 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 603 | while (iter.HasNext()) { |
| 604 | const Chunk child_chunk = iter.Next(); |
| 605 | switch (child_chunk.type()) { |
| 606 | case RES_STRING_POOL_TYPE: |
| 607 | // Only use the first string pool. Ignore others. |
| 608 | if (global_string_pool_.getError() == NO_INIT) { |
| 609 | status_t err = global_string_pool_.setTo(child_chunk.header<ResStringPool_header>(), |
| 610 | child_chunk.size()); |
| 611 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 612 | LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 613 | return false; |
| 614 | } |
| 615 | } else { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 616 | LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 617 | } |
| 618 | break; |
| 619 | |
| 620 | case RES_TABLE_PACKAGE_TYPE: { |
| 621 | if (packages_seen + 1 > package_count) { |
| 622 | LOG(ERROR) << "More package chunks were found than the " << package_count |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 623 | << " declared in the header."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 624 | return false; |
| 625 | } |
| 626 | packages_seen++; |
| 627 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 628 | std::unique_ptr<const LoadedPackage> loaded_package = |
| 629 | LoadedPackage::Load(child_chunk, loaded_idmap, system_, load_as_shared_library); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 630 | if (!loaded_package) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | packages_.push_back(std::move(loaded_package)); |
| 634 | } break; |
| 635 | |
| 636 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 637 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 638 | break; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (iter.HadError()) { |
| 643 | LOG(ERROR) << iter.GetLastError(); |
| 644 | return false; |
| 645 | } |
| 646 | return true; |
| 647 | } |
| 648 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 649 | std::unique_ptr<const LoadedArsc> LoadedArsc::Load(const StringPiece& data, |
| 650 | const LoadedIdmap* loaded_idmap, bool system, |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 651 | bool load_as_shared_library) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 652 | ATRACE_NAME("LoadedArsc::LoadTable"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 653 | |
| 654 | // Not using make_unique because the constructor is private. |
| 655 | std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc()); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 656 | loaded_arsc->system_ = system; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 657 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 658 | ChunkIterator iter(data.data(), data.size()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 659 | while (iter.HasNext()) { |
| 660 | const Chunk chunk = iter.Next(); |
| 661 | switch (chunk.type()) { |
| 662 | case RES_TABLE_TYPE: |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 663 | if (!loaded_arsc->LoadTable(chunk, loaded_idmap, load_as_shared_library)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 664 | return {}; |
| 665 | } |
| 666 | break; |
| 667 | |
| 668 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 669 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 670 | break; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (iter.HadError()) { |
| 675 | LOG(ERROR) << iter.GetLastError(); |
| 676 | return {}; |
| 677 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 678 | |
| 679 | // Need to force a move for mingw32. |
| 680 | return std::move(loaded_arsc); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 683 | std::unique_ptr<const LoadedArsc> LoadedArsc::CreateEmpty() { |
| 684 | return std::unique_ptr<LoadedArsc>(new LoadedArsc()); |
| 685 | } |
| 686 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 687 | } // namespace android |