blob: 2a70f0d6a8040ce9c290aab9aae2c4eb835cc136 [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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 Lesinski73f6f9d2017-11-14 10:18:05 -080021#include <algorithm>
Adam Lesinski7ad11102016-10-28 16:39:15 -070022#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 Lesinskida431a22016-12-29 16:08:16 -050036#include "androidfw/Chunk.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080037#include "androidfw/ResourceUtils.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070038#include "androidfw/Util.h"
39
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000040using android::base::StringPrintf;
Adam Lesinski7ad11102016-10-28 16:39:15 -070041
42namespace android {
43
Adam Lesinskida431a22016-12-29 16:08:16 -050044constexpr const static int kAppPackageId = 0x7f;
Adam Lesinski7ad11102016-10-28 16:39:15 -070045
Adam Lesinskida431a22016-12-29 16:08:16 -050046namespace {
47
Adam Lesinski7ad11102016-10-28 16:39:15 -070048// Builder that helps accumulate Type structs and then create a single
49// contiguous block of memory to store both the TypeSpec struct and
50// the Type structs.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080051struct TypeSpecBuilder {
52 explicit TypeSpecBuilder(incfs::verified_map_ptr<ResTable_typeSpec> header) : header_(header) {}
Adam Lesinski7ad11102016-10-28 16:39:15 -070053
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000054 void AddType(incfs::verified_map_ptr<ResTable_type> type) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080055 TypeSpec::TypeEntry& entry = type_entries.emplace_back();
56 entry.config.copyFromDtoH(type->config);
57 entry.type = type;
Adam Lesinski7ad11102016-10-28 16:39:15 -070058 }
59
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080060 TypeSpec Build() {
61 return {header_, std::move(type_entries)};
Adam Lesinski7ad11102016-10-28 16:39:15 -070062 }
63
64 private:
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080065 DISALLOW_COPY_AND_ASSIGN(TypeSpecBuilder);
Adam Lesinski7ad11102016-10-28 16:39:15 -070066
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000067 incfs::verified_map_ptr<ResTable_typeSpec> header_;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080068 std::vector<TypeSpec::TypeEntry> type_entries;
Adam Lesinski7ad11102016-10-28 16:39:15 -070069};
70
71} // namespace
72
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070073LoadedPackage::LoadedPackage() = default;
74LoadedPackage::~LoadedPackage() = default;
75
76// Precondition: The header passed in has already been verified, so reading any fields and trusting
77// the ResChunk_header is safe.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000078static bool VerifyResTableType(incfs::map_ptr<ResTable_type> header) {
Adam Lesinski498f6052017-11-29 13:24:29 -080079 if (header->id == 0) {
80 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
81 return false;
82 }
83
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070084 const size_t entry_count = dtohl(header->entryCount);
85 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -080086 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070087 return false;
88 }
89
90 // Make sure that there is enough room for the entry offsets.
91 const size_t offsets_offset = dtohs(header->header.headerSize);
92 const size_t entries_offset = dtohl(header->entriesStart);
93 const size_t offsets_length = sizeof(uint32_t) * entry_count;
94
95 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -080096 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070097 return false;
98 }
99
100 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800101 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700102 return false;
103 }
104
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000105 if (entries_offset & 0x03U) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800106 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700107 return false;
108 }
109 return true;
110}
111
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000112static base::expected<std::monostate, NullOrIOError> VerifyResTableEntry(
113 incfs::verified_map_ptr<ResTable_type> type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700114 // Check that the offset is aligned.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000115 if (UNLIKELY(entry_offset & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800116 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000117 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700118 }
119
120 // Check that the offset doesn't overflow.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000121 if (UNLIKELY(entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart))) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700122 // Overflow in offset.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800123 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000124 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700125 }
126
127 const size_t chunk_size = dtohl(type->header.size);
128
129 entry_offset += dtohl(type->entriesStart);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000130 if (UNLIKELY(entry_offset > chunk_size - sizeof(ResTable_entry))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800131 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700132 << " is too large. No room for ResTable_entry.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000133 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700134 }
135
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000136 auto entry = type.offset(entry_offset).convert<ResTable_entry>();
137 if (UNLIKELY(!entry)) {
138 return base::unexpected(IOError::PAGES_MISSING);
139 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700140
141 const size_t entry_size = dtohs(entry->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000142 if (UNLIKELY(entry_size < sizeof(entry.value()))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800143 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700144 << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000145 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700146 }
147
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000148 if (UNLIKELY(entry_size > chunk_size || entry_offset > chunk_size - entry_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800149 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700150 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000151 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700152 }
153
154 if (entry_size < sizeof(ResTable_map_entry)) {
155 // There needs to be room for one Res_value struct.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000156 if (UNLIKELY(entry_offset + entry_size > chunk_size - sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800157 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700158 << " for type " << (int)type->id << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000159 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700160 }
161
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000162 auto value = entry.offset(entry_size).convert<Res_value>();
163 if (UNLIKELY(!value)) {
164 return base::unexpected(IOError::PAGES_MISSING);
165 }
166
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700167 const size_t value_size = dtohs(value->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000168 if (UNLIKELY(value_size < sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800169 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000170 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700171 }
172
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000173 if (UNLIKELY(value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800174 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700175 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000176 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700177 }
178 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000179 auto map = entry.convert<ResTable_map_entry>();
180 if (UNLIKELY(!map)) {
181 return base::unexpected(IOError::PAGES_MISSING);
182 }
183
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700184 const size_t map_entry_count = dtohl(map->count);
185 size_t map_entries_start = entry_offset + entry_size;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000186 if (UNLIKELY(map_entries_start & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800187 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000188 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700189 }
190
191 // Each entry is sizeof(ResTable_map) big.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000192 if (UNLIKELY(map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map)))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800193 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000194 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700195 }
196 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000197 return {};
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700198}
199
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100200LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei)
201 : loadedPackage_(lp),
202 typeIndex_(ti),
203 entryIndex_(ei),
204 typeIndexEnd_(lp->resource_ids_.size() + 1) {
205 while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) {
206 typeIndex_++;
207 }
208}
209
210LoadedPackage::iterator& LoadedPackage::iterator::operator++() {
211 while (typeIndex_ < typeIndexEnd_) {
212 if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) {
213 entryIndex_++;
214 break;
215 }
216 entryIndex_ = 0;
217 typeIndex_++;
218 if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) {
219 break;
220 }
221 }
222 return *this;
223}
224
225uint32_t LoadedPackage::iterator::operator*() const {
226 if (typeIndex_ >= typeIndexEnd_) {
227 return 0;
228 }
229 return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_,
230 entryIndex_);
231}
232
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000233base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntry(
234 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
235 base::expected<uint32_t, NullOrIOError> entry_offset = GetEntryOffset(type_chunk, entry_index);
236 if (UNLIKELY(!entry_offset.has_value())) {
237 return base::unexpected(entry_offset.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800238 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000239 return GetEntryFromOffset(type_chunk, entry_offset.value());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800240}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700241
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000242base::expected<uint32_t, NullOrIOError> LoadedPackage::GetEntryOffset(
243 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800244 // The configuration matches and is better than the previous selection.
245 // Find the entry value if it exists for this configuration.
246 const size_t entry_count = dtohl(type_chunk->entryCount);
247 const size_t offsets_offset = dtohs(type_chunk->header.headerSize);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700248
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800249 // Check if there is the desired entry in this type.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800250 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
251 // This is encoded as a sparse map, so perform a binary search.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000252 bool error = false;
253 auto sparse_indices = type_chunk.offset(offsets_offset)
254 .convert<ResTable_sparseTypeEntry>().iterator();
255 auto sparse_indices_end = sparse_indices + entry_count;
256 auto result = std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
257 [&error](const incfs::map_ptr<ResTable_sparseTypeEntry>& entry,
258 uint16_t entry_idx) {
259 if (UNLIKELY(!entry)) {
260 return error = true;
261 }
262 return dtohs(entry->idx) < entry_idx;
263 });
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800264
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000265 if (result == sparse_indices_end) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800266 // No entry found.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000267 return base::unexpected(std::nullopt);
268 }
269
270 const incfs::verified_map_ptr<ResTable_sparseTypeEntry> entry = (*result).verified();
271 if (dtohs(entry->idx) != entry_index) {
272 if (error) {
273 return base::unexpected(IOError::PAGES_MISSING);
274 }
275 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700276 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800277
278 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
279 // the real offset divided by 4.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000280 return uint32_t{dtohs(entry->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700281 }
282
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800283 // This type is encoded as a dense array.
284 if (entry_index >= entry_count) {
285 // This entry cannot be here.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000286 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700287 }
288
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000289 const auto entry_offset_ptr = type_chunk.offset(offsets_offset).convert<uint32_t>() + entry_index;
290 if (UNLIKELY(!entry_offset_ptr)) {
291 return base::unexpected(IOError::PAGES_MISSING);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700292 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000293
294 const uint32_t value = dtohl(entry_offset_ptr.value());
295 if (value == ResTable_type::NO_ENTRY) {
296 return base::unexpected(std::nullopt);
297 }
298
299 return value;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700300}
301
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000302base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntryFromOffset(
303 incfs::verified_map_ptr<ResTable_type> type_chunk, uint32_t offset) {
304 auto valid = VerifyResTableEntry(type_chunk, offset);
305 if (UNLIKELY(!valid.has_value())) {
306 return base::unexpected(valid.error());
307 }
308 return type_chunk.offset(offset + dtohl(type_chunk->entriesStart)).convert<ResTable_entry>();
309}
310
311base::expected<std::monostate, IOError> LoadedPackage::CollectConfigurations(
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800312 bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {\
313 for (const auto& type_spec : type_specs_) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000314 if (exclude_mipmap) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800315 const int type_idx = type_spec.first - 1;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000316 const auto type_name16 = type_string_pool_.stringAt(type_idx);
317 if (UNLIKELY(IsIOError(type_name16))) {
318 return base::unexpected(GetIOError(type_name16.error()));
319 }
320 if (type_name16.has_value()) {
321 if (strncmp16(type_name16->data(), u"mipmap", type_name16->size()) == 0) {
322 // This is a mipmap type, skip collection.
323 continue;
Adam Lesinski0c405242017-01-13 20:47:26 -0800324 }
325 }
326
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000327 const auto type_name = type_string_pool_.string8At(type_idx);
328 if (UNLIKELY(IsIOError(type_name))) {
329 return base::unexpected(GetIOError(type_name.error()));
330 }
331 if (type_name.has_value()) {
332 if (strncmp(type_name->data(), "mipmap", type_name->size()) == 0) {
333 // This is a mipmap type, skip collection.
334 continue;
335 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700336 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700337 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000338
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800339 for (const auto& type_entry : type_spec.second.type_entries) {
340 out_configs->insert(type_entry.config);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000341 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800342 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000343 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -0800344}
345
346void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
347 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800348 for (const auto& type_spec : type_specs_) {
349 for (const auto& type_entry : type_spec.second.type_entries) {
350 if (type_entry.config.locale != 0) {
351 type_entry.config.getBcp47Locale(temp_locale, canonicalize);
352 std::string locale(temp_locale);
353 out_locales->insert(std::move(locale));
Adam Lesinski0c405242017-01-13 20:47:26 -0800354 }
355 }
356 }
357}
358
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000359base::expected<uint32_t, NullOrIOError> LoadedPackage::FindEntryByName(
360 const std::u16string& type_name, const std::u16string& entry_name) const {
361 const base::expected<size_t, NullOrIOError> type_idx = type_string_pool_.indexOfString(
362 type_name.data(), type_name.size());
363 if (!type_idx.has_value()) {
364 return base::unexpected(type_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800365 }
366
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000367 const base::expected<size_t, NullOrIOError> key_idx = key_string_pool_.indexOfString(
368 entry_name.data(), entry_name.size());
369 if (!key_idx.has_value()) {
370 return base::unexpected(key_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800371 }
372
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800373 const TypeSpec* type_spec = GetTypeSpecByTypeIndex(*type_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800374 if (type_spec == nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000375 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800376 }
377
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800378 for (const auto& type_entry : type_spec->type_entries) {
379 const incfs::verified_map_ptr<ResTable_type>& type = type_entry.type;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000380
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800381 size_t entry_count = dtohl(type->entryCount);
Adam Lesinski929d6512017-01-16 19:11:19 -0800382 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000383 auto entry_offset_ptr = type.offset(dtohs(type->header.headerSize)).convert<uint32_t>() +
384 entry_idx;
385 if (!entry_offset_ptr) {
386 return base::unexpected(IOError::PAGES_MISSING);
387 }
388
389 auto offset = dtohl(entry_offset_ptr.value());
Adam Lesinski929d6512017-01-16 19:11:19 -0800390 if (offset != ResTable_type::NO_ENTRY) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000391 auto entry = type.offset(dtohl(type->entriesStart) + offset).convert<ResTable_entry>();
392 if (!entry) {
393 return base::unexpected(IOError::PAGES_MISSING);
394 }
395
396 if (dtohl(entry->key.index) == static_cast<uint32_t>(*key_idx)) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800397 // The package ID will be overridden by the caller (due to runtime assignment of package
398 // IDs for shared libraries).
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000399 return make_resid(0x00, *type_idx + type_id_offset_ + 1, entry_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800400 }
401 }
402 }
403 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000404 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800405}
406
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800407const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700408 for (const auto& loaded_package : packages_) {
409 if (loaded_package->GetPackageId() == package_id) {
410 return loaded_package.get();
411 }
412 }
413 return nullptr;
414}
415
416std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800417 package_property_t property_flags) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800418 ATRACE_NAME("LoadedPackage::Load");
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700419 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500420
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700421 // typeIdOffset was added at some point, but we still must recognize apps built before this
422 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700423 constexpr size_t kMinPackageSize =
424 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000425 const incfs::map_ptr<ResTable_package> header = chunk.header<ResTable_package, kMinPackageSize>();
426 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800427 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500428 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700429 }
430
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800431 if ((property_flags & PROPERTY_SYSTEM) != 0) {
432 loaded_package->property_flags_ |= PROPERTY_SYSTEM;
433 }
434
435 if ((property_flags & PROPERTY_LOADER) != 0) {
436 loaded_package->property_flags_ |= PROPERTY_LOADER;
437 }
438
439 if ((property_flags & PROPERTY_OVERLAY) != 0) {
440 // Overlay resources must have an exclusive resource id space for referencing internal
441 // resources.
442 loaded_package->property_flags_ |= PROPERTY_OVERLAY | PROPERTY_DYNAMIC;
443 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700444
Adam Lesinski7ad11102016-10-28 16:39:15 -0700445 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700446 if (loaded_package->package_id_ == 0 ||
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800447 (loaded_package->package_id_ == kAppPackageId && (property_flags & PROPERTY_DYNAMIC) != 0)) {
448 loaded_package->property_flags_ |= PROPERTY_DYNAMIC;
Winson9947f1e2019-08-16 10:20:39 -0700449 }
450
Adam Lesinskic6aada92017-01-13 15:34:14 -0800451 if (header->header.headerSize >= sizeof(ResTable_package)) {
452 uint32_t type_id_offset = dtohl(header->typeIdOffset);
453 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800454 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800455 return {};
456 }
457 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
458 }
459
Adam Lesinskida431a22016-12-29 16:08:16 -0500460 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
461 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700462
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800463 // A map of TypeSpec builders, each associated with an type index.
464 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
465 // contiguous block of memory that holds all the Types together with the TypeSpec.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800466 std::unordered_map<int, std::unique_ptr<TypeSpecBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700467
468 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
469 while (iter.HasNext()) {
470 const Chunk child_chunk = iter.Next();
471 switch (child_chunk.type()) {
472 case RES_STRING_POOL_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000473 const auto pool_address = child_chunk.header<ResChunk_header>();
474 if (!pool_address) {
475 LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation.";
476 return {};
477 }
478
479 if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700480 // This string pool is the type string pool.
481 status_t err = loaded_package->type_string_pool_.setTo(
482 child_chunk.header<ResStringPool_header>(), child_chunk.size());
483 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800484 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500485 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700486 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000487 } else if (pool_address == header.offset(dtohl(header->keyStrings))
488 .convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700489 // This string pool is the key string pool.
490 status_t err = loaded_package->key_string_pool_.setTo(
491 child_chunk.header<ResStringPool_header>(), child_chunk.size());
492 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800493 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500494 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700495 }
496 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800497 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700498 }
499 } break;
500
501 case RES_TABLE_TYPE_SPEC_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000502 const auto type_spec = child_chunk.header<ResTable_typeSpec>();
503 if (!type_spec) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800504 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500505 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700506 }
507
508 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800509 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500510 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700511 }
512
Adam Lesinskic6aada92017-01-13 15:34:14 -0800513 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
514 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800515 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800516 return {};
517 }
518
Adam Lesinski7ad11102016-10-28 16:39:15 -0700519 // The data portion of this chunk contains entry_count 32bit entries,
520 // each one representing a set of flags.
521 // Here we only validate that the chunk is well formed.
522 const size_t entry_count = dtohl(type_spec->entryCount);
523
524 // There can only be 2^16 entries in a type, because that is the ID
525 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
526 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800527 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500528 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700529 }
530
531 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800532 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500533 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700534 }
535
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800536 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type_spec->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800537 if (builder_ptr == nullptr) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800538 builder_ptr = util::make_unique<TypeSpecBuilder>(type_spec.verified());
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100539 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800540 } else {
541 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
542 type_spec->id);
543 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700544 } break;
545
546 case RES_TABLE_TYPE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000547 const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
548 if (!type) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800549 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500550 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700551 }
552
Adam Lesinski498f6052017-11-29 13:24:29 -0800553 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500554 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700555 }
556
557 // Type chunks must be preceded by their TypeSpec chunks.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800558 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800559 if (builder_ptr != nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000560 builder_ptr->AddType(type.verified());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800561 } else {
562 LOG(ERROR) << StringPrintf(
563 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
564 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500565 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700566 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700567 } break;
568
Adam Lesinskida431a22016-12-29 16:08:16 -0500569 case RES_TABLE_LIBRARY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000570 const auto lib = child_chunk.header<ResTable_lib_header>();
571 if (!lib) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800572 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500573 return {};
574 }
575
576 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800577 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500578 return {};
579 }
580
581 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
582
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000583 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>();
584 const auto entry_end = entry_begin + dtohl(lib->count);
Adam Lesinskida431a22016-12-29 16:08:16 -0500585 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000586 if (!entry_iter) {
587 return {};
588 }
589
Adam Lesinskida431a22016-12-29 16:08:16 -0500590 std::string package_name;
591 util::ReadUtf16StringFromDevice(entry_iter->packageName,
592 arraysize(entry_iter->packageName), &package_name);
593
594 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800595 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500596 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
597 dtohl(entry_iter->packageId), package_name.c_str());
598 return {};
599 }
600
601 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
602 dtohl(entry_iter->packageId));
603 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800604 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500605
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800606 case RES_TABLE_OVERLAYABLE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000607 const auto overlayable = child_chunk.header<ResTable_overlayable_header>();
608 if (!overlayable) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800609 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
610 return {};
611 }
612
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800613 std::string name;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000614 util::ReadUtf16StringFromDevice(overlayable->name, arraysize(overlayable->name), &name);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800615 std::string actor;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000616 util::ReadUtf16StringFromDevice(overlayable->actor, arraysize(overlayable->actor), &actor);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800617
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100618 if (loaded_package->overlayable_map_.find(name) !=
619 loaded_package->overlayable_map_.end()) {
620 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '" << name << "'.";
621 return {};
622 }
623 loaded_package->overlayable_map_.emplace(name, actor);
624
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800625 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800626 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
627 while (overlayable_iter.HasNext()) {
628 const Chunk overlayable_child_chunk = overlayable_iter.Next();
629
630 switch (overlayable_child_chunk.type()) {
631 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000632 const auto policy_header =
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800633 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000634 if (!policy_header) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800635 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
636 return {};
637 }
638
639 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
640 < dtohl(policy_header->entry_count)) {
641 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
642 return {};
643 }
644
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800645 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800646 std::unordered_set<uint32_t> ids;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000647 const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800648 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
649 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000650 if (!id_iter) {
651 return {};
652 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800653 ids.insert(dtohl(id_iter->ident));
654 }
655
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800656 // Add the pairing of overlayable properties and resource ids to the package
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800657 OverlayableInfo overlayable_info{};
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800658 overlayable_info.name = name;
659 overlayable_info.actor = actor;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800660 overlayable_info.policy_flags = policy_header->policy_flags;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000661 loaded_package->overlayable_infos_.emplace_back(overlayable_info, ids);
Ryan Mitchell19823452019-01-29 12:01:24 -0800662 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800663 break;
664 }
665
666 default:
667 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
668 break;
669 }
670 }
671
672 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800673 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800674 overlayable_iter.GetLastError().c_str());
675 if (overlayable_iter.HadFatalError()) {
676 return {};
677 }
678 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500679 } break;
680
Adam Lesinski7ad11102016-10-28 16:39:15 -0700681 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800682 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700683 break;
684 }
685 }
686
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800687 if (iter.HadError()) {
688 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700689 if (iter.HadFatalError()) {
690 return {};
691 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800692 }
693
694 // Flatten and construct the TypeSpecs.
695 for (auto& entry : type_builder_map) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800696 TypeSpec type_spec = entry.second->Build();
697 uint8_t type_id = static_cast<uint8_t>(entry.first);
698 loaded_package->type_specs_[type_id] = std::move(type_spec);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700699 }
700
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700701 return std::move(loaded_package);
702}
703
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700704bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800705 package_property_t property_flags) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000706 incfs::map_ptr<ResTable_header> header = chunk.header<ResTable_header>();
707 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800708 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700709 return false;
710 }
711
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700712 if (loaded_idmap != nullptr) {
713 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
714 }
715
Adam Lesinski7ad11102016-10-28 16:39:15 -0700716 const size_t package_count = dtohl(header->packageCount);
717 size_t packages_seen = 0;
718
719 packages_.reserve(package_count);
720
721 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
722 while (iter.HasNext()) {
723 const Chunk child_chunk = iter.Next();
724 switch (child_chunk.type()) {
725 case RES_STRING_POOL_TYPE:
726 // Only use the first string pool. Ignore others.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700727 if (global_string_pool_->getError() == NO_INIT) {
728 status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(),
729 child_chunk.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700730 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800731 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700732 return false;
733 }
734 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800735 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700736 }
737 break;
738
739 case RES_TABLE_PACKAGE_TYPE: {
740 if (packages_seen + 1 > package_count) {
741 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700742 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700743 return false;
744 }
745 packages_seen++;
746
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700747 std::unique_ptr<const LoadedPackage> loaded_package =
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800748 LoadedPackage::Load(child_chunk, property_flags);
Adam Lesinskida431a22016-12-29 16:08:16 -0500749 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700750 return false;
751 }
752 packages_.push_back(std::move(loaded_package));
753 } break;
754
755 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800756 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700757 break;
758 }
759 }
760
761 if (iter.HadError()) {
762 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700763 if (iter.HadFatalError()) {
764 return false;
765 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700766 }
767 return true;
768}
769
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800770std::unique_ptr<LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data,
771 const size_t length,
772 const LoadedIdmap* loaded_idmap,
773 const package_property_t property_flags) {
Winson9947f1e2019-08-16 10:20:39 -0700774 ATRACE_NAME("LoadedArsc::Load");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700775
776 // Not using make_unique because the constructor is private.
777 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
778
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000779 ChunkIterator iter(data, length);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700780 while (iter.HasNext()) {
781 const Chunk chunk = iter.Next();
782 switch (chunk.type()) {
783 case RES_TABLE_TYPE:
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800784 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, property_flags)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700785 return {};
786 }
787 break;
788
789 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800790 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700791 break;
792 }
793 }
794
795 if (iter.HadError()) {
796 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700797 if (iter.HadFatalError()) {
798 return {};
799 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700800 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800801
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800802 return loaded_arsc;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700803}
804
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800805std::unique_ptr<LoadedArsc> LoadedArsc::CreateEmpty() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700806 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
807}
808
Adam Lesinski7ad11102016-10-28 16:39:15 -0700809} // namespace android