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 | 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 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 40 | using android::base::StringPrintf; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 44 | constexpr const static int kFrameworkPackageId = 0x01; |
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. |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 52 | struct TypeSpecBuilder { |
| 53 | explicit TypeSpecBuilder(incfs::verified_map_ptr<ResTable_typeSpec> header) : header_(header) {} |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 54 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 55 | void AddType(incfs::verified_map_ptr<ResTable_type> type) { |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 56 | TypeSpec::TypeEntry& entry = type_entries.emplace_back(); |
| 57 | entry.config.copyFromDtoH(type->config); |
| 58 | entry.type = type; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 61 | TypeSpec Build() { |
| 62 | return {header_, std::move(type_entries)}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private: |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 66 | DISALLOW_COPY_AND_ASSIGN(TypeSpecBuilder); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 67 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 68 | incfs::verified_map_ptr<ResTable_typeSpec> header_; |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 69 | std::vector<TypeSpec::TypeEntry> type_entries; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | } // namespace |
| 73 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 74 | // Precondition: The header passed in has already been verified, so reading any fields and trusting |
| 75 | // the ResChunk_header is safe. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 76 | static bool VerifyResTableType(incfs::map_ptr<ResTable_type> header) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 77 | if (header->id == 0) { |
| 78 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0."; |
| 79 | return false; |
| 80 | } |
| 81 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 82 | const size_t entry_count = dtohl(header->entryCount); |
| 83 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 84 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ")."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
| 88 | // Make sure that there is enough room for the entry offsets. |
| 89 | const size_t offsets_offset = dtohs(header->header.headerSize); |
| 90 | const size_t entries_offset = dtohl(header->entriesStart); |
| 91 | const size_t offsets_length = sizeof(uint32_t) * entry_count; |
| 92 | |
| 93 | if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 94 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
| 98 | if (entries_offset > dtohl(header->header.size)) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 99 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 103 | if (entries_offset & 0x03U) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 104 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address."; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 110 | static base::expected<std::monostate, NullOrIOError> VerifyResTableEntry( |
| 111 | incfs::verified_map_ptr<ResTable_type> type, uint32_t entry_offset) { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 112 | // Check that the offset is aligned. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 113 | if (UNLIKELY(entry_offset & 0x03U)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 114 | LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 115 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Check that the offset doesn't overflow. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 119 | if (UNLIKELY(entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart))) { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 120 | // Overflow in offset. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 121 | LOG(ERROR) << "Entry at offset " << entry_offset << " is too large."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 122 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | const size_t chunk_size = dtohl(type->header.size); |
| 126 | |
| 127 | entry_offset += dtohl(type->entriesStart); |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 128 | if (UNLIKELY(entry_offset > chunk_size - sizeof(ResTable_entry))) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 129 | LOG(ERROR) << "Entry at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 130 | << " is too large. No room for ResTable_entry."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 131 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 134 | auto entry = type.offset(entry_offset).convert<ResTable_entry>(); |
| 135 | if (UNLIKELY(!entry)) { |
| 136 | return base::unexpected(IOError::PAGES_MISSING); |
| 137 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 138 | |
| 139 | const size_t entry_size = dtohs(entry->size); |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 140 | if (UNLIKELY(entry_size < sizeof(entry.value()))) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 141 | LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 142 | << " is too small."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 143 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 146 | if (UNLIKELY(entry_size > chunk_size || entry_offset > chunk_size - entry_size)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 147 | LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 148 | << " is too large."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 149 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (entry_size < sizeof(ResTable_map_entry)) { |
| 153 | // There needs to be room for one Res_value struct. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 154 | if (UNLIKELY(entry_offset + entry_size > chunk_size - sizeof(Res_value))) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 155 | 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] | 156 | << " for type " << (int)type->id << "."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 157 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 160 | auto value = entry.offset(entry_size).convert<Res_value>(); |
| 161 | if (UNLIKELY(!value)) { |
| 162 | return base::unexpected(IOError::PAGES_MISSING); |
| 163 | } |
| 164 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 165 | const size_t value_size = dtohs(value->size); |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 166 | if (UNLIKELY(value_size < sizeof(Res_value))) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 167 | LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 168 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 171 | if (UNLIKELY(value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 172 | LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 173 | << " is too large."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 174 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 175 | } |
| 176 | } else { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 177 | auto map = entry.convert<ResTable_map_entry>(); |
| 178 | if (UNLIKELY(!map)) { |
| 179 | return base::unexpected(IOError::PAGES_MISSING); |
| 180 | } |
| 181 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 182 | const size_t map_entry_count = dtohl(map->count); |
| 183 | size_t map_entries_start = entry_offset + entry_size; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 184 | if (UNLIKELY(map_entries_start & 0x03U)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 185 | LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 186 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Each entry is sizeof(ResTable_map) big. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 190 | if (UNLIKELY(map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map)))) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 191 | LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << "."; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 192 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 195 | return {}; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 196 | } |
| 197 | |
MÃ¥rten Kongstad | 3f1f4fc | 2018-03-02 09:34:18 +0100 | [diff] [blame] | 198 | LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei) |
| 199 | : loadedPackage_(lp), |
| 200 | typeIndex_(ti), |
| 201 | entryIndex_(ei), |
| 202 | typeIndexEnd_(lp->resource_ids_.size() + 1) { |
| 203 | while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) { |
| 204 | typeIndex_++; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | LoadedPackage::iterator& LoadedPackage::iterator::operator++() { |
| 209 | while (typeIndex_ < typeIndexEnd_) { |
| 210 | if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) { |
| 211 | entryIndex_++; |
| 212 | break; |
| 213 | } |
| 214 | entryIndex_ = 0; |
| 215 | typeIndex_++; |
| 216 | if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) { |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | return *this; |
| 221 | } |
| 222 | |
| 223 | uint32_t LoadedPackage::iterator::operator*() const { |
| 224 | if (typeIndex_ >= typeIndexEnd_) { |
| 225 | return 0; |
| 226 | } |
| 227 | return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_, |
| 228 | entryIndex_); |
| 229 | } |
| 230 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 231 | base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntry( |
| 232 | incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) { |
| 233 | base::expected<uint32_t, NullOrIOError> entry_offset = GetEntryOffset(type_chunk, entry_index); |
| 234 | if (UNLIKELY(!entry_offset.has_value())) { |
| 235 | return base::unexpected(entry_offset.error()); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 236 | } |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 237 | return GetEntryFromOffset(type_chunk, entry_offset.value()); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 238 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 239 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 240 | base::expected<uint32_t, NullOrIOError> LoadedPackage::GetEntryOffset( |
| 241 | incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 242 | // The configuration matches and is better than the previous selection. |
| 243 | // Find the entry value if it exists for this configuration. |
| 244 | const size_t entry_count = dtohl(type_chunk->entryCount); |
| 245 | const size_t offsets_offset = dtohs(type_chunk->header.headerSize); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 246 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 247 | // Check if there is the desired entry in this type. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 248 | if (type_chunk->flags & ResTable_type::FLAG_SPARSE) { |
| 249 | // This is encoded as a sparse map, so perform a binary search. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 250 | bool error = false; |
| 251 | auto sparse_indices = type_chunk.offset(offsets_offset) |
| 252 | .convert<ResTable_sparseTypeEntry>().iterator(); |
| 253 | auto sparse_indices_end = sparse_indices + entry_count; |
| 254 | auto result = std::lower_bound(sparse_indices, sparse_indices_end, entry_index, |
| 255 | [&error](const incfs::map_ptr<ResTable_sparseTypeEntry>& entry, |
| 256 | uint16_t entry_idx) { |
| 257 | if (UNLIKELY(!entry)) { |
| 258 | return error = true; |
| 259 | } |
| 260 | return dtohs(entry->idx) < entry_idx; |
| 261 | }); |
Adam Lesinski | 73f6f9d | 2017-11-14 10:18:05 -0800 | [diff] [blame] | 262 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 263 | if (result == sparse_indices_end) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 264 | // No entry found. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 265 | return base::unexpected(std::nullopt); |
| 266 | } |
| 267 | |
| 268 | const incfs::verified_map_ptr<ResTable_sparseTypeEntry> entry = (*result).verified(); |
| 269 | if (dtohs(entry->idx) != entry_index) { |
| 270 | if (error) { |
| 271 | return base::unexpected(IOError::PAGES_MISSING); |
| 272 | } |
| 273 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 274 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 275 | |
| 276 | // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as |
| 277 | // the real offset divided by 4. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 278 | return uint32_t{dtohs(entry->offset)} * 4u; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 281 | // This type is encoded as a dense array. |
| 282 | if (entry_index >= entry_count) { |
| 283 | // This entry cannot be here. |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 284 | return base::unexpected(std::nullopt); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 287 | const auto entry_offset_ptr = type_chunk.offset(offsets_offset).convert<uint32_t>() + entry_index; |
| 288 | if (UNLIKELY(!entry_offset_ptr)) { |
| 289 | return base::unexpected(IOError::PAGES_MISSING); |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 290 | } |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 291 | |
| 292 | const uint32_t value = dtohl(entry_offset_ptr.value()); |
| 293 | if (value == ResTable_type::NO_ENTRY) { |
| 294 | return base::unexpected(std::nullopt); |
| 295 | } |
| 296 | |
| 297 | return value; |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 300 | base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntryFromOffset( |
| 301 | incfs::verified_map_ptr<ResTable_type> type_chunk, uint32_t offset) { |
| 302 | auto valid = VerifyResTableEntry(type_chunk, offset); |
| 303 | if (UNLIKELY(!valid.has_value())) { |
| 304 | return base::unexpected(valid.error()); |
| 305 | } |
| 306 | return type_chunk.offset(offset + dtohl(type_chunk->entriesStart)).convert<ResTable_entry>(); |
| 307 | } |
| 308 | |
| 309 | base::expected<std::monostate, IOError> LoadedPackage::CollectConfigurations( |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 310 | bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {\ |
| 311 | for (const auto& type_spec : type_specs_) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 312 | if (exclude_mipmap) { |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 313 | const int type_idx = type_spec.first - 1; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 314 | const auto type_name16 = type_string_pool_.stringAt(type_idx); |
| 315 | if (UNLIKELY(IsIOError(type_name16))) { |
| 316 | return base::unexpected(GetIOError(type_name16.error())); |
| 317 | } |
| 318 | if (type_name16.has_value()) { |
| 319 | if (strncmp16(type_name16->data(), u"mipmap", type_name16->size()) == 0) { |
| 320 | // This is a mipmap type, skip collection. |
| 321 | continue; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 325 | const auto type_name = type_string_pool_.string8At(type_idx); |
| 326 | if (UNLIKELY(IsIOError(type_name))) { |
| 327 | return base::unexpected(GetIOError(type_name.error())); |
| 328 | } |
| 329 | if (type_name.has_value()) { |
| 330 | if (strncmp(type_name->data(), "mipmap", type_name->size()) == 0) { |
| 331 | // This is a mipmap type, skip collection. |
| 332 | continue; |
| 333 | } |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 334 | } |
Ryan Mitchell | c75c2e0 | 2020-08-17 08:42:48 -0700 | [diff] [blame] | 335 | } |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 336 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 337 | for (const auto& type_entry : type_spec.second.type_entries) { |
| 338 | out_configs->insert(type_entry.config); |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 339 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 340 | } |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 341 | return {}; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const { |
| 345 | char temp_locale[RESTABLE_MAX_LOCALE_LEN]; |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 346 | for (const auto& type_spec : type_specs_) { |
| 347 | for (const auto& type_entry : type_spec.second.type_entries) { |
| 348 | if (type_entry.config.locale != 0) { |
| 349 | type_entry.config.getBcp47Locale(temp_locale, canonicalize); |
| 350 | std::string locale(temp_locale); |
| 351 | out_locales->insert(std::move(locale)); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 357 | base::expected<uint32_t, NullOrIOError> LoadedPackage::FindEntryByName( |
| 358 | const std::u16string& type_name, const std::u16string& entry_name) const { |
| 359 | const base::expected<size_t, NullOrIOError> type_idx = type_string_pool_.indexOfString( |
| 360 | type_name.data(), type_name.size()); |
| 361 | if (!type_idx.has_value()) { |
| 362 | return base::unexpected(type_idx.error()); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 365 | const base::expected<size_t, NullOrIOError> key_idx = key_string_pool_.indexOfString( |
| 366 | entry_name.data(), entry_name.size()); |
| 367 | if (!key_idx.has_value()) { |
| 368 | return base::unexpected(key_idx.error()); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 371 | const TypeSpec* type_spec = GetTypeSpecByTypeIndex(*type_idx); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 372 | if (type_spec == nullptr) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 373 | return base::unexpected(std::nullopt); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 376 | for (const auto& type_entry : type_spec->type_entries) { |
| 377 | const incfs::verified_map_ptr<ResTable_type>& type = type_entry.type; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 378 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 379 | size_t entry_count = dtohl(type->entryCount); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 380 | for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 381 | auto entry_offset_ptr = type.offset(dtohs(type->header.headerSize)).convert<uint32_t>() + |
| 382 | entry_idx; |
| 383 | if (!entry_offset_ptr) { |
| 384 | return base::unexpected(IOError::PAGES_MISSING); |
| 385 | } |
| 386 | |
| 387 | auto offset = dtohl(entry_offset_ptr.value()); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 388 | if (offset != ResTable_type::NO_ENTRY) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 389 | auto entry = type.offset(dtohl(type->entriesStart) + offset).convert<ResTable_entry>(); |
| 390 | if (!entry) { |
| 391 | return base::unexpected(IOError::PAGES_MISSING); |
| 392 | } |
| 393 | |
| 394 | if (dtohl(entry->key.index) == static_cast<uint32_t>(*key_idx)) { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 395 | // The package ID will be overridden by the caller (due to runtime assignment of package |
| 396 | // IDs for shared libraries). |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 397 | return make_resid(0x00, *type_idx + type_id_offset_ + 1, entry_idx); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 402 | return base::unexpected(std::nullopt); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 405 | const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 406 | for (const auto& loaded_package : packages_) { |
| 407 | if (loaded_package->GetPackageId() == package_id) { |
| 408 | return loaded_package.get(); |
| 409 | } |
| 410 | } |
| 411 | return nullptr; |
| 412 | } |
| 413 | |
| 414 | std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk, |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 415 | package_property_t property_flags) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 416 | ATRACE_NAME("LoadedPackage::Load"); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 417 | std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 418 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 419 | // typeIdOffset was added at some point, but we still must recognize apps built before this |
| 420 | // was added. |
Adam Lesinski | 33af6c7 | 2017-03-29 13:00:35 -0700 | [diff] [blame] | 421 | constexpr size_t kMinPackageSize = |
| 422 | sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset); |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 423 | const incfs::map_ptr<ResTable_package> header = chunk.header<ResTable_package, kMinPackageSize>(); |
| 424 | if (!header) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 425 | LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 426 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 429 | if ((property_flags & PROPERTY_SYSTEM) != 0) { |
| 430 | loaded_package->property_flags_ |= PROPERTY_SYSTEM; |
| 431 | } |
| 432 | |
| 433 | if ((property_flags & PROPERTY_LOADER) != 0) { |
| 434 | loaded_package->property_flags_ |= PROPERTY_LOADER; |
| 435 | } |
| 436 | |
| 437 | if ((property_flags & PROPERTY_OVERLAY) != 0) { |
| 438 | // Overlay resources must have an exclusive resource id space for referencing internal |
| 439 | // resources. |
| 440 | loaded_package->property_flags_ |= PROPERTY_OVERLAY | PROPERTY_DYNAMIC; |
| 441 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 442 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 443 | loaded_package->package_id_ = dtohl(header->id); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 444 | if (loaded_package->package_id_ == 0 || |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 445 | (loaded_package->package_id_ == kAppPackageId && (property_flags & PROPERTY_DYNAMIC) != 0)) { |
| 446 | loaded_package->property_flags_ |= PROPERTY_DYNAMIC; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 449 | if (header->header.headerSize >= sizeof(ResTable_package)) { |
| 450 | uint32_t type_id_offset = dtohl(header->typeIdOffset); |
| 451 | if (type_id_offset > std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 452 | LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large."; |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 453 | return {}; |
| 454 | } |
| 455 | loaded_package->type_id_offset_ = static_cast<int>(type_id_offset); |
| 456 | } |
| 457 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 458 | util::ReadUtf16StringFromDevice(header->name, arraysize(header->name), |
| 459 | &loaded_package->package_name_); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 460 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 461 | // A map of TypeSpec builders, each associated with an type index. |
| 462 | // We use these to accumulate the set of Types available for a TypeSpec, and later build a single, |
| 463 | // contiguous block of memory that holds all the Types together with the TypeSpec. |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 464 | std::unordered_map<int, std::unique_ptr<TypeSpecBuilder>> type_builder_map; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 465 | |
| 466 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 467 | while (iter.HasNext()) { |
| 468 | const Chunk child_chunk = iter.Next(); |
| 469 | switch (child_chunk.type()) { |
| 470 | case RES_STRING_POOL_TYPE: { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 471 | const auto pool_address = child_chunk.header<ResChunk_header>(); |
| 472 | if (!pool_address) { |
| 473 | LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation."; |
| 474 | return {}; |
| 475 | } |
| 476 | |
| 477 | if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 478 | // This string pool is the type string pool. |
| 479 | status_t err = loaded_package->type_string_pool_.setTo( |
| 480 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 481 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 482 | LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 483 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 484 | } |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 485 | } else if (pool_address == header.offset(dtohl(header->keyStrings)) |
| 486 | .convert<ResChunk_header>()) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 487 | // This string pool is the key string pool. |
| 488 | status_t err = loaded_package->key_string_pool_.setTo( |
| 489 | child_chunk.header<ResStringPool_header>(), child_chunk.size()); |
| 490 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 491 | LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 492 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 493 | } |
| 494 | } else { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 495 | 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] | 496 | } |
| 497 | } break; |
| 498 | |
| 499 | case RES_TABLE_TYPE_SPEC_TYPE: { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 500 | const auto type_spec = child_chunk.header<ResTable_typeSpec>(); |
| 501 | if (!type_spec) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 502 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 503 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (type_spec->id == 0) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 507 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 508 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 511 | if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) > |
| 512 | std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 513 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID."; |
Adam Lesinski | c6aada9 | 2017-01-13 15:34:14 -0800 | [diff] [blame] | 514 | return {}; |
| 515 | } |
| 516 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 517 | // The data portion of this chunk contains entry_count 32bit entries, |
| 518 | // each one representing a set of flags. |
| 519 | // Here we only validate that the chunk is well formed. |
| 520 | const size_t entry_count = dtohl(type_spec->entryCount); |
| 521 | |
| 522 | // There can only be 2^16 entries in a type, because that is the ID |
| 523 | // space for entries (EEEE) in the resource ID 0xPPTTEEEE. |
| 524 | if (entry_count > std::numeric_limits<uint16_t>::max()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 525 | 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] | 526 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | if (entry_count * sizeof(uint32_t) > chunk.data_size()) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 530 | LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 531 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 534 | std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type_spec->id]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 535 | if (builder_ptr == nullptr) { |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 536 | builder_ptr = util::make_unique<TypeSpecBuilder>(type_spec.verified()); |
MÃ¥rten Kongstad | 3f1f4fc | 2018-03-02 09:34:18 +0100 | [diff] [blame] | 537 | loaded_package->resource_ids_.set(type_spec->id, entry_count); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 538 | } else { |
| 539 | LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x", |
| 540 | type_spec->id); |
| 541 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 542 | } break; |
| 543 | |
| 544 | case RES_TABLE_TYPE_TYPE: { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 545 | const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>(); |
| 546 | if (!type) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 547 | LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 548 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 551 | if (!VerifyResTableType(type)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 552 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | // Type chunks must be preceded by their TypeSpec chunks. |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 556 | std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type->id]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 557 | if (builder_ptr != nullptr) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 558 | builder_ptr->AddType(type.verified()); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 559 | } else { |
| 560 | LOG(ERROR) << StringPrintf( |
| 561 | "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.", |
| 562 | type->id); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 563 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 564 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 565 | } break; |
| 566 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 567 | case RES_TABLE_LIBRARY_TYPE: { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 568 | const auto lib = child_chunk.header<ResTable_lib_header>(); |
| 569 | if (!lib) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 570 | LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 571 | return {}; |
| 572 | } |
| 573 | |
| 574 | if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 575 | LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries."; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 576 | return {}; |
| 577 | } |
| 578 | |
| 579 | loaded_package->dynamic_package_map_.reserve(dtohl(lib->count)); |
| 580 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 581 | const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>(); |
| 582 | const auto entry_end = entry_begin + dtohl(lib->count); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 583 | for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 584 | if (!entry_iter) { |
| 585 | return {}; |
| 586 | } |
| 587 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 588 | std::string package_name; |
| 589 | util::ReadUtf16StringFromDevice(entry_iter->packageName, |
| 590 | arraysize(entry_iter->packageName), &package_name); |
| 591 | |
| 592 | if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 593 | LOG(ERROR) << StringPrintf( |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 594 | "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.", |
| 595 | dtohl(entry_iter->packageId), package_name.c_str()); |
| 596 | return {}; |
| 597 | } |
| 598 | |
| 599 | loaded_package->dynamic_package_map_.emplace_back(std::move(package_name), |
| 600 | dtohl(entry_iter->packageId)); |
| 601 | } |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 602 | } break; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 603 | |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 604 | case RES_TABLE_OVERLAYABLE_TYPE: { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 605 | const auto overlayable = child_chunk.header<ResTable_overlayable_header>(); |
| 606 | if (!overlayable) { |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 607 | LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small."; |
| 608 | return {}; |
| 609 | } |
| 610 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 611 | std::string name; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 612 | util::ReadUtf16StringFromDevice(overlayable->name, arraysize(overlayable->name), &name); |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 613 | std::string actor; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 614 | util::ReadUtf16StringFromDevice(overlayable->actor, arraysize(overlayable->actor), &actor); |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 615 | |
MÃ¥rten Kongstad | c92c4dd | 2019-02-05 01:29:59 +0100 | [diff] [blame] | 616 | if (loaded_package->overlayable_map_.find(name) != |
| 617 | loaded_package->overlayable_map_.end()) { |
| 618 | LOG(ERROR) << "Multiple <overlayable> blocks with the same name '" << name << "'."; |
| 619 | return {}; |
| 620 | } |
| 621 | loaded_package->overlayable_map_.emplace(name, actor); |
| 622 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 623 | // Iterate over the overlayable policy chunks contained within the overlayable chunk data |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 624 | ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size()); |
| 625 | while (overlayable_iter.HasNext()) { |
| 626 | const Chunk overlayable_child_chunk = overlayable_iter.Next(); |
| 627 | |
| 628 | switch (overlayable_child_chunk.type()) { |
| 629 | case RES_TABLE_OVERLAYABLE_POLICY_TYPE: { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 630 | const auto policy_header = |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 631 | overlayable_child_chunk.header<ResTable_overlayable_policy_header>(); |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 632 | if (!policy_header) { |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 633 | LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small."; |
| 634 | return {}; |
| 635 | } |
| 636 | |
| 637 | if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref)) |
| 638 | < dtohl(policy_header->entry_count)) { |
| 639 | LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries."; |
| 640 | return {}; |
| 641 | } |
| 642 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 643 | // Retrieve all the resource ids belonging to this policy chunk |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 644 | std::unordered_set<uint32_t> ids; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 645 | const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>(); |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 646 | const auto ids_end = ids_begin + dtohl(policy_header->entry_count); |
| 647 | for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 648 | if (!id_iter) { |
| 649 | return {}; |
| 650 | } |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 651 | ids.insert(dtohl(id_iter->ident)); |
| 652 | } |
| 653 | |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 654 | // Add the pairing of overlayable properties and resource ids to the package |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 655 | OverlayableInfo overlayable_info{}; |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 656 | overlayable_info.name = name; |
| 657 | overlayable_info.actor = actor; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 658 | overlayable_info.policy_flags = policy_header->policy_flags; |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 659 | loaded_package->overlayable_infos_.emplace_back(overlayable_info, ids); |
Ryan Mitchell | 1982345 | 2019-01-29 12:01:24 -0800 | [diff] [blame] | 660 | loaded_package->defines_overlayable_ = true; |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | |
| 664 | default: |
| 665 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
| 666 | break; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | if (overlayable_iter.HadError()) { |
Ryan Mitchell | ef5673a | 2018-12-12 18:45:34 -0800 | [diff] [blame] | 671 | LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s", |
Ryan Mitchell | 75e20dd | 2018-11-06 16:39:36 -0800 | [diff] [blame] | 672 | overlayable_iter.GetLastError().c_str()); |
| 673 | if (overlayable_iter.HadFatalError()) { |
| 674 | return {}; |
| 675 | } |
| 676 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 677 | } break; |
| 678 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 679 | case RES_TABLE_STAGED_ALIAS_TYPE: { |
| 680 | if (loaded_package->package_id_ != kFrameworkPackageId) { |
| 681 | LOG(WARNING) << "Alias chunk ignored for non-framework package '" |
| 682 | << loaded_package->package_name_ << "'"; |
| 683 | break; |
| 684 | } |
| 685 | |
| 686 | std::unordered_set<uint32_t> finalized_ids; |
| 687 | const auto lib_alias = child_chunk.header<ResTable_staged_alias_header>(); |
| 688 | if (!lib_alias) { |
Yurii Zubrytskyi | 8002034 | 2021-11-29 23:55:24 -0800 | [diff] [blame^] | 689 | LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small."; |
| 690 | return {}; |
| 691 | } |
| 692 | if ((child_chunk.data_size() / sizeof(ResTable_staged_alias_entry)) |
| 693 | < dtohl(lib_alias->count)) { |
| 694 | LOG(ERROR) << "RES_TABLE_STAGED_ALIAS_TYPE is too small to hold entries."; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 695 | return {}; |
| 696 | } |
| 697 | const auto entry_begin = child_chunk.data_ptr().convert<ResTable_staged_alias_entry>(); |
| 698 | const auto entry_end = entry_begin + dtohl(lib_alias->count); |
| 699 | for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) { |
| 700 | if (!entry_iter) { |
| 701 | return {}; |
| 702 | } |
| 703 | auto finalized_id = dtohl(entry_iter->finalizedResId); |
| 704 | if (!finalized_ids.insert(finalized_id).second) { |
| 705 | LOG(ERROR) << StringPrintf("Repeated finalized resource id '%08x' in staged aliases.", |
| 706 | finalized_id); |
| 707 | return {}; |
| 708 | } |
| 709 | |
| 710 | auto staged_id = dtohl(entry_iter->stagedResId); |
| 711 | auto [_, success] = loaded_package->alias_id_map_.insert(std::make_pair(staged_id, |
| 712 | finalized_id)); |
| 713 | if (!success) { |
| 714 | LOG(ERROR) << StringPrintf("Repeated staged resource id '%08x' in staged aliases.", |
| 715 | staged_id); |
| 716 | return {}; |
| 717 | } |
| 718 | } |
| 719 | } break; |
| 720 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 721 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 722 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | } |
| 726 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 727 | if (iter.HadError()) { |
| 728 | LOG(ERROR) << iter.GetLastError(); |
Todd Kennedy | 28e663c | 2018-07-12 13:15:54 -0700 | [diff] [blame] | 729 | if (iter.HadFatalError()) { |
| 730 | return {}; |
| 731 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // Flatten and construct the TypeSpecs. |
| 735 | for (auto& entry : type_builder_map) { |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 736 | TypeSpec type_spec = entry.second->Build(); |
| 737 | uint8_t type_id = static_cast<uint8_t>(entry.first); |
| 738 | loaded_package->type_specs_[type_id] = std::move(type_spec); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 741 | return std::move(loaded_package); |
| 742 | } |
| 743 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 744 | bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap, |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 745 | package_property_t property_flags) { |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 746 | incfs::map_ptr<ResTable_header> header = chunk.header<ResTable_header>(); |
| 747 | if (!header) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 748 | LOG(ERROR) << "RES_TABLE_TYPE too small."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 749 | return false; |
| 750 | } |
| 751 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 752 | if (loaded_idmap != nullptr) { |
| 753 | global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap); |
| 754 | } |
| 755 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 756 | const size_t package_count = dtohl(header->packageCount); |
| 757 | size_t packages_seen = 0; |
| 758 | |
| 759 | packages_.reserve(package_count); |
| 760 | |
| 761 | ChunkIterator iter(chunk.data_ptr(), chunk.data_size()); |
| 762 | while (iter.HasNext()) { |
| 763 | const Chunk child_chunk = iter.Next(); |
| 764 | switch (child_chunk.type()) { |
| 765 | case RES_STRING_POOL_TYPE: |
| 766 | // Only use the first string pool. Ignore others. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 767 | if (global_string_pool_->getError() == NO_INIT) { |
| 768 | status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(), |
| 769 | child_chunk.size()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 770 | if (err != NO_ERROR) { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 771 | LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 772 | return false; |
| 773 | } |
| 774 | } else { |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 775 | LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 776 | } |
| 777 | break; |
| 778 | |
| 779 | case RES_TABLE_PACKAGE_TYPE: { |
| 780 | if (packages_seen + 1 > package_count) { |
| 781 | LOG(ERROR) << "More package chunks were found than the " << package_count |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 782 | << " declared in the header."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 783 | return false; |
| 784 | } |
| 785 | packages_seen++; |
| 786 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 787 | std::unique_ptr<const LoadedPackage> loaded_package = |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 788 | LoadedPackage::Load(child_chunk, property_flags); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 789 | if (!loaded_package) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 790 | return false; |
| 791 | } |
| 792 | packages_.push_back(std::move(loaded_package)); |
| 793 | } break; |
| 794 | |
| 795 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 796 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | if (iter.HadError()) { |
| 802 | LOG(ERROR) << iter.GetLastError(); |
Todd Kennedy | 28e663c | 2018-07-12 13:15:54 -0700 | [diff] [blame] | 803 | if (iter.HadFatalError()) { |
| 804 | return false; |
| 805 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 806 | } |
| 807 | return true; |
| 808 | } |
| 809 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 810 | std::unique_ptr<LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data, |
| 811 | const size_t length, |
| 812 | const LoadedIdmap* loaded_idmap, |
| 813 | const package_property_t property_flags) { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 814 | ATRACE_NAME("LoadedArsc::Load"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 815 | |
| 816 | // Not using make_unique because the constructor is private. |
| 817 | std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc()); |
| 818 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 819 | ChunkIterator iter(data, length); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 820 | while (iter.HasNext()) { |
| 821 | const Chunk chunk = iter.Next(); |
| 822 | switch (chunk.type()) { |
| 823 | case RES_TABLE_TYPE: |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 824 | if (!loaded_arsc->LoadTable(chunk, loaded_idmap, property_flags)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 825 | return {}; |
| 826 | } |
| 827 | break; |
| 828 | |
| 829 | default: |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 830 | LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 831 | break; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | if (iter.HadError()) { |
| 836 | LOG(ERROR) << iter.GetLastError(); |
Todd Kennedy | 28e663c | 2018-07-12 13:15:54 -0700 | [diff] [blame] | 837 | if (iter.HadFatalError()) { |
| 838 | return {}; |
| 839 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 840 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 841 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 842 | return loaded_arsc; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 845 | std::unique_ptr<LoadedArsc> LoadedArsc::CreateEmpty() { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 846 | return std::unique_ptr<LoadedArsc>(new LoadedArsc()); |
| 847 | } |
| 848 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 849 | } // namespace android |