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