blob: cb620cc475a979310009bf04f22ae001cafa6056 [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 -070073// Precondition: The header passed in has already been verified, so reading any fields and trusting
74// the ResChunk_header is safe.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000075static bool VerifyResTableType(incfs::map_ptr<ResTable_type> header) {
Adam Lesinski498f6052017-11-29 13:24:29 -080076 if (header->id == 0) {
77 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
78 return false;
79 }
80
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070081 const size_t entry_count = dtohl(header->entryCount);
82 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -080083 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070084 return false;
85 }
86
87 // Make sure that there is enough room for the entry offsets.
88 const size_t offsets_offset = dtohs(header->header.headerSize);
89 const size_t entries_offset = dtohl(header->entriesStart);
90 const size_t offsets_length = sizeof(uint32_t) * entry_count;
91
92 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -080093 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070094 return false;
95 }
96
97 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -080098 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070099 return false;
100 }
101
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000102 if (entries_offset & 0x03U) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800103 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700104 return false;
105 }
106 return true;
107}
108
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000109static base::expected<std::monostate, NullOrIOError> VerifyResTableEntry(
110 incfs::verified_map_ptr<ResTable_type> type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700111 // Check that the offset is aligned.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000112 if (UNLIKELY(entry_offset & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800113 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000114 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700115 }
116
117 // Check that the offset doesn't overflow.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000118 if (UNLIKELY(entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart))) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700119 // Overflow in offset.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800120 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000121 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700122 }
123
124 const size_t chunk_size = dtohl(type->header.size);
125
126 entry_offset += dtohl(type->entriesStart);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000127 if (UNLIKELY(entry_offset > chunk_size - sizeof(ResTable_entry))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800128 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700129 << " is too large. No room for ResTable_entry.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000130 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700131 }
132
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000133 auto entry = type.offset(entry_offset).convert<ResTable_entry>();
134 if (UNLIKELY(!entry)) {
135 return base::unexpected(IOError::PAGES_MISSING);
136 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700137
138 const size_t entry_size = dtohs(entry->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000139 if (UNLIKELY(entry_size < sizeof(entry.value()))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800140 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700141 << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000142 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700143 }
144
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000145 if (UNLIKELY(entry_size > chunk_size || entry_offset > chunk_size - entry_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800146 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700147 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000148 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700149 }
150
151 if (entry_size < sizeof(ResTable_map_entry)) {
152 // There needs to be room for one Res_value struct.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000153 if (UNLIKELY(entry_offset + entry_size > chunk_size - sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800154 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700155 << " for type " << (int)type->id << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000156 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700157 }
158
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000159 auto value = entry.offset(entry_size).convert<Res_value>();
160 if (UNLIKELY(!value)) {
161 return base::unexpected(IOError::PAGES_MISSING);
162 }
163
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700164 const size_t value_size = dtohs(value->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000165 if (UNLIKELY(value_size < sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800166 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000167 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700168 }
169
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000170 if (UNLIKELY(value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800171 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700172 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000173 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700174 }
175 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000176 auto map = entry.convert<ResTable_map_entry>();
177 if (UNLIKELY(!map)) {
178 return base::unexpected(IOError::PAGES_MISSING);
179 }
180
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700181 const size_t map_entry_count = dtohl(map->count);
182 size_t map_entries_start = entry_offset + entry_size;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000183 if (UNLIKELY(map_entries_start & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800184 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000185 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700186 }
187
188 // Each entry is sizeof(ResTable_map) big.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000189 if (UNLIKELY(map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map)))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800190 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000191 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700192 }
193 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000194 return {};
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700195}
196
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100197LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei)
198 : loadedPackage_(lp),
199 typeIndex_(ti),
200 entryIndex_(ei),
201 typeIndexEnd_(lp->resource_ids_.size() + 1) {
202 while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) {
203 typeIndex_++;
204 }
205}
206
207LoadedPackage::iterator& LoadedPackage::iterator::operator++() {
208 while (typeIndex_ < typeIndexEnd_) {
209 if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) {
210 entryIndex_++;
211 break;
212 }
213 entryIndex_ = 0;
214 typeIndex_++;
215 if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) {
216 break;
217 }
218 }
219 return *this;
220}
221
222uint32_t LoadedPackage::iterator::operator*() const {
223 if (typeIndex_ >= typeIndexEnd_) {
224 return 0;
225 }
226 return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_,
227 entryIndex_);
228}
229
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000230base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntry(
231 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
232 base::expected<uint32_t, NullOrIOError> entry_offset = GetEntryOffset(type_chunk, entry_index);
233 if (UNLIKELY(!entry_offset.has_value())) {
234 return base::unexpected(entry_offset.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800235 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000236 return GetEntryFromOffset(type_chunk, entry_offset.value());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800237}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700238
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000239base::expected<uint32_t, NullOrIOError> LoadedPackage::GetEntryOffset(
240 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800241 // The configuration matches and is better than the previous selection.
242 // Find the entry value if it exists for this configuration.
243 const size_t entry_count = dtohl(type_chunk->entryCount);
244 const size_t offsets_offset = dtohs(type_chunk->header.headerSize);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700245
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800246 // Check if there is the desired entry in this type.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800247 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
248 // This is encoded as a sparse map, so perform a binary search.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000249 bool error = false;
250 auto sparse_indices = type_chunk.offset(offsets_offset)
251 .convert<ResTable_sparseTypeEntry>().iterator();
252 auto sparse_indices_end = sparse_indices + entry_count;
253 auto result = std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
254 [&error](const incfs::map_ptr<ResTable_sparseTypeEntry>& entry,
255 uint16_t entry_idx) {
256 if (UNLIKELY(!entry)) {
257 return error = true;
258 }
259 return dtohs(entry->idx) < entry_idx;
260 });
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800261
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000262 if (result == sparse_indices_end) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800263 // No entry found.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000264 return base::unexpected(std::nullopt);
265 }
266
267 const incfs::verified_map_ptr<ResTable_sparseTypeEntry> entry = (*result).verified();
268 if (dtohs(entry->idx) != entry_index) {
269 if (error) {
270 return base::unexpected(IOError::PAGES_MISSING);
271 }
272 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700273 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800274
275 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
276 // the real offset divided by 4.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000277 return uint32_t{dtohs(entry->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700278 }
279
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800280 // This type is encoded as a dense array.
281 if (entry_index >= entry_count) {
282 // This entry cannot be here.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000283 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700284 }
285
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000286 const auto entry_offset_ptr = type_chunk.offset(offsets_offset).convert<uint32_t>() + entry_index;
287 if (UNLIKELY(!entry_offset_ptr)) {
288 return base::unexpected(IOError::PAGES_MISSING);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700289 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000290
291 const uint32_t value = dtohl(entry_offset_ptr.value());
292 if (value == ResTable_type::NO_ENTRY) {
293 return base::unexpected(std::nullopt);
294 }
295
296 return value;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700297}
298
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000299base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntryFromOffset(
300 incfs::verified_map_ptr<ResTable_type> type_chunk, uint32_t offset) {
301 auto valid = VerifyResTableEntry(type_chunk, offset);
302 if (UNLIKELY(!valid.has_value())) {
303 return base::unexpected(valid.error());
304 }
305 return type_chunk.offset(offset + dtohl(type_chunk->entriesStart)).convert<ResTable_entry>();
306}
307
308base::expected<std::monostate, IOError> LoadedPackage::CollectConfigurations(
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800309 bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {\
310 for (const auto& type_spec : type_specs_) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000311 if (exclude_mipmap) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800312 const int type_idx = type_spec.first - 1;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000313 const auto type_name16 = type_string_pool_.stringAt(type_idx);
314 if (UNLIKELY(IsIOError(type_name16))) {
315 return base::unexpected(GetIOError(type_name16.error()));
316 }
317 if (type_name16.has_value()) {
318 if (strncmp16(type_name16->data(), u"mipmap", type_name16->size()) == 0) {
319 // This is a mipmap type, skip collection.
320 continue;
Adam Lesinski0c405242017-01-13 20:47:26 -0800321 }
322 }
323
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000324 const auto type_name = type_string_pool_.string8At(type_idx);
325 if (UNLIKELY(IsIOError(type_name))) {
326 return base::unexpected(GetIOError(type_name.error()));
327 }
328 if (type_name.has_value()) {
329 if (strncmp(type_name->data(), "mipmap", type_name->size()) == 0) {
330 // This is a mipmap type, skip collection.
331 continue;
332 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700333 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700334 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000335
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800336 for (const auto& type_entry : type_spec.second.type_entries) {
337 out_configs->insert(type_entry.config);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000338 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800339 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000340 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -0800341}
342
343void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
344 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800345 for (const auto& type_spec : type_specs_) {
346 for (const auto& type_entry : type_spec.second.type_entries) {
347 if (type_entry.config.locale != 0) {
348 type_entry.config.getBcp47Locale(temp_locale, canonicalize);
349 std::string locale(temp_locale);
350 out_locales->insert(std::move(locale));
Adam Lesinski0c405242017-01-13 20:47:26 -0800351 }
352 }
353 }
354}
355
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000356base::expected<uint32_t, NullOrIOError> LoadedPackage::FindEntryByName(
357 const std::u16string& type_name, const std::u16string& entry_name) const {
358 const base::expected<size_t, NullOrIOError> type_idx = type_string_pool_.indexOfString(
359 type_name.data(), type_name.size());
360 if (!type_idx.has_value()) {
361 return base::unexpected(type_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800362 }
363
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000364 const base::expected<size_t, NullOrIOError> key_idx = key_string_pool_.indexOfString(
365 entry_name.data(), entry_name.size());
366 if (!key_idx.has_value()) {
367 return base::unexpected(key_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800368 }
369
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800370 const TypeSpec* type_spec = GetTypeSpecByTypeIndex(*type_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800371 if (type_spec == nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000372 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800373 }
374
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800375 for (const auto& type_entry : type_spec->type_entries) {
376 const incfs::verified_map_ptr<ResTable_type>& type = type_entry.type;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000377
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800378 size_t entry_count = dtohl(type->entryCount);
Adam Lesinski929d6512017-01-16 19:11:19 -0800379 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000380 auto entry_offset_ptr = type.offset(dtohs(type->header.headerSize)).convert<uint32_t>() +
381 entry_idx;
382 if (!entry_offset_ptr) {
383 return base::unexpected(IOError::PAGES_MISSING);
384 }
385
386 auto offset = dtohl(entry_offset_ptr.value());
Adam Lesinski929d6512017-01-16 19:11:19 -0800387 if (offset != ResTable_type::NO_ENTRY) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000388 auto entry = type.offset(dtohl(type->entriesStart) + offset).convert<ResTable_entry>();
389 if (!entry) {
390 return base::unexpected(IOError::PAGES_MISSING);
391 }
392
393 if (dtohl(entry->key.index) == static_cast<uint32_t>(*key_idx)) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800394 // The package ID will be overridden by the caller (due to runtime assignment of package
395 // IDs for shared libraries).
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000396 return make_resid(0x00, *type_idx + type_id_offset_ + 1, entry_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800397 }
398 }
399 }
400 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000401 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800402}
403
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800404const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700405 for (const auto& loaded_package : packages_) {
406 if (loaded_package->GetPackageId() == package_id) {
407 return loaded_package.get();
408 }
409 }
410 return nullptr;
411}
412
413std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800414 package_property_t property_flags) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800415 ATRACE_NAME("LoadedPackage::Load");
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700416 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500417
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700418 // typeIdOffset was added at some point, but we still must recognize apps built before this
419 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700420 constexpr size_t kMinPackageSize =
421 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000422 const incfs::map_ptr<ResTable_package> header = chunk.header<ResTable_package, kMinPackageSize>();
423 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800424 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500425 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700426 }
427
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800428 if ((property_flags & PROPERTY_SYSTEM) != 0) {
429 loaded_package->property_flags_ |= PROPERTY_SYSTEM;
430 }
431
432 if ((property_flags & PROPERTY_LOADER) != 0) {
433 loaded_package->property_flags_ |= PROPERTY_LOADER;
434 }
435
436 if ((property_flags & PROPERTY_OVERLAY) != 0) {
437 // Overlay resources must have an exclusive resource id space for referencing internal
438 // resources.
439 loaded_package->property_flags_ |= PROPERTY_OVERLAY | PROPERTY_DYNAMIC;
440 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700441
Adam Lesinski7ad11102016-10-28 16:39:15 -0700442 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700443 if (loaded_package->package_id_ == 0 ||
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800444 (loaded_package->package_id_ == kAppPackageId && (property_flags & PROPERTY_DYNAMIC) != 0)) {
445 loaded_package->property_flags_ |= PROPERTY_DYNAMIC;
Winson9947f1e2019-08-16 10:20:39 -0700446 }
447
Adam Lesinskic6aada92017-01-13 15:34:14 -0800448 if (header->header.headerSize >= sizeof(ResTable_package)) {
449 uint32_t type_id_offset = dtohl(header->typeIdOffset);
450 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800451 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800452 return {};
453 }
454 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
455 }
456
Adam Lesinskida431a22016-12-29 16:08:16 -0500457 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
458 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700459
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800460 // A map of TypeSpec builders, each associated with an type index.
461 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
462 // contiguous block of memory that holds all the Types together with the TypeSpec.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800463 std::unordered_map<int, std::unique_ptr<TypeSpecBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700464
465 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
466 while (iter.HasNext()) {
467 const Chunk child_chunk = iter.Next();
468 switch (child_chunk.type()) {
469 case RES_STRING_POOL_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000470 const auto pool_address = child_chunk.header<ResChunk_header>();
471 if (!pool_address) {
472 LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation.";
473 return {};
474 }
475
476 if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700477 // This string pool is the type string pool.
478 status_t err = loaded_package->type_string_pool_.setTo(
479 child_chunk.header<ResStringPool_header>(), child_chunk.size());
480 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800481 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500482 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700483 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000484 } else if (pool_address == header.offset(dtohl(header->keyStrings))
485 .convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700486 // This string pool is the key string pool.
487 status_t err = loaded_package->key_string_pool_.setTo(
488 child_chunk.header<ResStringPool_header>(), child_chunk.size());
489 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800490 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500491 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700492 }
493 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800494 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700495 }
496 } break;
497
498 case RES_TABLE_TYPE_SPEC_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000499 const auto type_spec = child_chunk.header<ResTable_typeSpec>();
500 if (!type_spec) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800501 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500502 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700503 }
504
505 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800506 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500507 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700508 }
509
Adam Lesinskic6aada92017-01-13 15:34:14 -0800510 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
511 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800512 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800513 return {};
514 }
515
Adam Lesinski7ad11102016-10-28 16:39:15 -0700516 // The data portion of this chunk contains entry_count 32bit entries,
517 // each one representing a set of flags.
518 // Here we only validate that the chunk is well formed.
519 const size_t entry_count = dtohl(type_spec->entryCount);
520
521 // There can only be 2^16 entries in a type, because that is the ID
522 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
523 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800524 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500525 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700526 }
527
528 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800529 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500530 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700531 }
532
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800533 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type_spec->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800534 if (builder_ptr == nullptr) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800535 builder_ptr = util::make_unique<TypeSpecBuilder>(type_spec.verified());
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100536 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800537 } else {
538 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
539 type_spec->id);
540 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700541 } break;
542
543 case RES_TABLE_TYPE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000544 const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
545 if (!type) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800546 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500547 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700548 }
549
Adam Lesinski498f6052017-11-29 13:24:29 -0800550 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500551 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700552 }
553
554 // Type chunks must be preceded by their TypeSpec chunks.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800555 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800556 if (builder_ptr != nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000557 builder_ptr->AddType(type.verified());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800558 } else {
559 LOG(ERROR) << StringPrintf(
560 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
561 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500562 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700563 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700564 } break;
565
Adam Lesinskida431a22016-12-29 16:08:16 -0500566 case RES_TABLE_LIBRARY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000567 const auto lib = child_chunk.header<ResTable_lib_header>();
568 if (!lib) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800569 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500570 return {};
571 }
572
573 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800574 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500575 return {};
576 }
577
578 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
579
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000580 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>();
581 const auto entry_end = entry_begin + dtohl(lib->count);
Adam Lesinskida431a22016-12-29 16:08:16 -0500582 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000583 if (!entry_iter) {
584 return {};
585 }
586
Adam Lesinskida431a22016-12-29 16:08:16 -0500587 std::string package_name;
588 util::ReadUtf16StringFromDevice(entry_iter->packageName,
589 arraysize(entry_iter->packageName), &package_name);
590
591 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800592 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500593 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
594 dtohl(entry_iter->packageId), package_name.c_str());
595 return {};
596 }
597
598 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
599 dtohl(entry_iter->packageId));
600 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800601 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500602
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800603 case RES_TABLE_OVERLAYABLE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000604 const auto overlayable = child_chunk.header<ResTable_overlayable_header>();
605 if (!overlayable) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800606 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
607 return {};
608 }
609
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800610 std::string name;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000611 util::ReadUtf16StringFromDevice(overlayable->name, arraysize(overlayable->name), &name);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800612 std::string actor;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000613 util::ReadUtf16StringFromDevice(overlayable->actor, arraysize(overlayable->actor), &actor);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800614
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100615 if (loaded_package->overlayable_map_.find(name) !=
616 loaded_package->overlayable_map_.end()) {
617 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '" << name << "'.";
618 return {};
619 }
620 loaded_package->overlayable_map_.emplace(name, actor);
621
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800622 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800623 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
624 while (overlayable_iter.HasNext()) {
625 const Chunk overlayable_child_chunk = overlayable_iter.Next();
626
627 switch (overlayable_child_chunk.type()) {
628 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000629 const auto policy_header =
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800630 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000631 if (!policy_header) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800632 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
633 return {};
634 }
635
636 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
637 < dtohl(policy_header->entry_count)) {
638 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
639 return {};
640 }
641
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800642 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800643 std::unordered_set<uint32_t> ids;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000644 const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800645 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
646 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000647 if (!id_iter) {
648 return {};
649 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800650 ids.insert(dtohl(id_iter->ident));
651 }
652
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800653 // Add the pairing of overlayable properties and resource ids to the package
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800654 OverlayableInfo overlayable_info{};
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800655 overlayable_info.name = name;
656 overlayable_info.actor = actor;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800657 overlayable_info.policy_flags = policy_header->policy_flags;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000658 loaded_package->overlayable_infos_.emplace_back(overlayable_info, ids);
Ryan Mitchell19823452019-01-29 12:01:24 -0800659 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800660 break;
661 }
662
663 default:
664 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
665 break;
666 }
667 }
668
669 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800670 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800671 overlayable_iter.GetLastError().c_str());
672 if (overlayable_iter.HadFatalError()) {
673 return {};
674 }
675 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500676 } break;
677
Adam Lesinski7ad11102016-10-28 16:39:15 -0700678 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800679 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700680 break;
681 }
682 }
683
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800684 if (iter.HadError()) {
685 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700686 if (iter.HadFatalError()) {
687 return {};
688 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800689 }
690
691 // Flatten and construct the TypeSpecs.
692 for (auto& entry : type_builder_map) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800693 TypeSpec type_spec = entry.second->Build();
694 uint8_t type_id = static_cast<uint8_t>(entry.first);
695 loaded_package->type_specs_[type_id] = std::move(type_spec);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700696 }
697
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700698 return std::move(loaded_package);
699}
700
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700701bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800702 package_property_t property_flags) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000703 incfs::map_ptr<ResTable_header> header = chunk.header<ResTable_header>();
704 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800705 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700706 return false;
707 }
708
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700709 if (loaded_idmap != nullptr) {
710 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
711 }
712
Adam Lesinski7ad11102016-10-28 16:39:15 -0700713 const size_t package_count = dtohl(header->packageCount);
714 size_t packages_seen = 0;
715
716 packages_.reserve(package_count);
717
718 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
719 while (iter.HasNext()) {
720 const Chunk child_chunk = iter.Next();
721 switch (child_chunk.type()) {
722 case RES_STRING_POOL_TYPE:
723 // Only use the first string pool. Ignore others.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700724 if (global_string_pool_->getError() == NO_INIT) {
725 status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(),
726 child_chunk.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700727 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800728 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700729 return false;
730 }
731 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800732 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700733 }
734 break;
735
736 case RES_TABLE_PACKAGE_TYPE: {
737 if (packages_seen + 1 > package_count) {
738 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700739 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700740 return false;
741 }
742 packages_seen++;
743
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700744 std::unique_ptr<const LoadedPackage> loaded_package =
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800745 LoadedPackage::Load(child_chunk, property_flags);
Adam Lesinskida431a22016-12-29 16:08:16 -0500746 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700747 return false;
748 }
749 packages_.push_back(std::move(loaded_package));
750 } break;
751
752 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800753 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700754 break;
755 }
756 }
757
758 if (iter.HadError()) {
759 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700760 if (iter.HadFatalError()) {
761 return false;
762 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700763 }
764 return true;
765}
766
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800767std::unique_ptr<LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data,
768 const size_t length,
769 const LoadedIdmap* loaded_idmap,
770 const package_property_t property_flags) {
Winson9947f1e2019-08-16 10:20:39 -0700771 ATRACE_NAME("LoadedArsc::Load");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700772
773 // Not using make_unique because the constructor is private.
774 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
775
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000776 ChunkIterator iter(data, length);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700777 while (iter.HasNext()) {
778 const Chunk chunk = iter.Next();
779 switch (chunk.type()) {
780 case RES_TABLE_TYPE:
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800781 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, property_flags)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700782 return {};
783 }
784 break;
785
786 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800787 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700788 break;
789 }
790 }
791
792 if (iter.HadError()) {
793 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700794 if (iter.HadFatalError()) {
795 return {};
796 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700797 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800798
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800799 return loaded_arsc;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700800}
801
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800802std::unique_ptr<LoadedArsc> LoadedArsc::CreateEmpty() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700803 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
804}
805
Adam Lesinski7ad11102016-10-28 16:39:15 -0700806} // namespace android