blob: 2fc3b05011c293b6dd49624468def1c3f86cb89f [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 Lesinski7ad11102016-10-28 16:39:15 -070036#include "androidfw/ByteBucketArray.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050037#include "androidfw/Chunk.h"
Adam Lesinski929d6512017-01-16 19:11:19 -080038#include "androidfw/ResourceUtils.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070039#include "androidfw/Util.h"
40
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070041using android::base::StringPrintf;
Adam Lesinski7ad11102016-10-28 16:39:15 -070042
43namespace android {
44
Adam Lesinskida431a22016-12-29 16:08:16 -050045constexpr const static int kAppPackageId = 0x7f;
Adam Lesinski7ad11102016-10-28 16:39:15 -070046
Adam Lesinskida431a22016-12-29 16:08:16 -050047namespace {
48
Adam Lesinski7ad11102016-10-28 16:39:15 -070049// Builder that helps accumulate Type structs and then create a single
50// contiguous block of memory to store both the TypeSpec struct and
51// the Type structs.
52class TypeSpecPtrBuilder {
53 public:
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070054 explicit TypeSpecPtrBuilder(incfs::verified_map_ptr<ResTable_typeSpec> header)
Ryan Mitchell8a891d82019-07-01 09:48:23 -070055 : header_(header) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -070056 }
Adam Lesinski7ad11102016-10-28 16:39:15 -070057
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070058 void AddType(incfs::verified_map_ptr<ResTable_type> type) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -080059 types_.push_back(type);
Adam Lesinski7ad11102016-10-28 16:39:15 -070060 }
61
62 TypeSpecPtr Build() {
63 // Check for overflow.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070064 using ElementType = incfs::verified_map_ptr<ResTable_type>;
Adam Lesinskibebfcc42018-02-12 14:27:46 -080065 if ((std::numeric_limits<size_t>::max() - sizeof(TypeSpec)) / sizeof(ElementType) <
66 types_.size()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070067 return {};
68 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -080069 TypeSpec* type_spec =
70 (TypeSpec*)::malloc(sizeof(TypeSpec) + (types_.size() * sizeof(ElementType)));
Adam Lesinski7ad11102016-10-28 16:39:15 -070071 type_spec->type_spec = header_;
72 type_spec->type_count = types_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -080073 memcpy(type_spec + 1, types_.data(), types_.size() * sizeof(ElementType));
Adam Lesinski7ad11102016-10-28 16:39:15 -070074 return TypeSpecPtr(type_spec);
75 }
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(TypeSpecPtrBuilder);
79
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070080 incfs::verified_map_ptr<ResTable_typeSpec> header_;
81 std::vector<incfs::verified_map_ptr<ResTable_type>> types_;
Adam Lesinski7ad11102016-10-28 16:39:15 -070082};
83
84} // namespace
85
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070086LoadedPackage::LoadedPackage() = default;
87LoadedPackage::~LoadedPackage() = default;
88
89// Precondition: The header passed in has already been verified, so reading any fields and trusting
90// the ResChunk_header is safe.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -070091static bool VerifyResTableType(incfs::map_ptr<ResTable_type> header) {
Adam Lesinski498f6052017-11-29 13:24:29 -080092 if (header->id == 0) {
93 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
94 return false;
95 }
96
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070097 const size_t entry_count = dtohl(header->entryCount);
98 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -080099 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700100 return false;
101 }
102
103 // Make sure that there is enough room for the entry offsets.
104 const size_t offsets_offset = dtohs(header->header.headerSize);
105 const size_t entries_offset = dtohl(header->entriesStart);
106 const size_t offsets_length = sizeof(uint32_t) * entry_count;
107
108 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800109 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700110 return false;
111 }
112
113 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800114 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700115 return false;
116 }
117
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700118 if (entries_offset & 0x03U) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800119 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700120 return false;
121 }
122 return true;
123}
124
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700125static base::expected<std::monostate, NullOrIOError> VerifyResTableEntry(
126 incfs::verified_map_ptr<ResTable_type> type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700127 // Check that the offset is aligned.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700128 if (UNLIKELY(entry_offset & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800129 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700130 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700131 }
132
133 // Check that the offset doesn't overflow.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700134 if (UNLIKELY(entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart))) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700135 // Overflow in offset.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800136 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700137 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700138 }
139
140 const size_t chunk_size = dtohl(type->header.size);
141
142 entry_offset += dtohl(type->entriesStart);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700143 if (UNLIKELY(entry_offset > chunk_size - sizeof(ResTable_entry))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800144 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700145 << " is too large. No room for ResTable_entry.";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700146 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700147 }
148
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700149 auto entry = type.offset(entry_offset).convert<ResTable_entry>();
150 if (UNLIKELY(!entry)) {
151 return base::unexpected(IOError::PAGES_MISSING);
152 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700153
154 const size_t entry_size = dtohs(entry->size);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700155 if (UNLIKELY(entry_size < sizeof(entry.value()))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800156 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700157 << " is too small.";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700158 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700159 }
160
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700161 if (UNLIKELY(entry_size > chunk_size || entry_offset > chunk_size - entry_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800162 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700163 << " is too large.";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700164 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700165 }
166
167 if (entry_size < sizeof(ResTable_map_entry)) {
168 // There needs to be room for one Res_value struct.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700169 if (UNLIKELY(entry_offset + entry_size > chunk_size - sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800170 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700171 << " for type " << (int)type->id << ".";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700172 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700173 }
174
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700175 auto value = entry.offset(entry_size).convert<Res_value>();
176 if (UNLIKELY(!value)) {
177 return base::unexpected(IOError::PAGES_MISSING);
178 }
179
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700180 const size_t value_size = dtohs(value->size);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700181 if (UNLIKELY(value_size < sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800182 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700183 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700184 }
185
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700186 if (UNLIKELY(value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800187 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700188 << " is too large.";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700189 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700190 }
191 } else {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700192 auto map = entry.convert<ResTable_map_entry>();
193 if (UNLIKELY(!map)) {
194 return base::unexpected(IOError::PAGES_MISSING);
195 }
196
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700197 const size_t map_entry_count = dtohl(map->count);
198 size_t map_entries_start = entry_offset + entry_size;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700199 if (UNLIKELY(map_entries_start & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800200 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700201 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700202 }
203
204 // Each entry is sizeof(ResTable_map) big.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700205 if (UNLIKELY(map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map)))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800206 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700207 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700208 }
209 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700210 return {};
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700211}
212
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100213LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei)
214 : loadedPackage_(lp),
215 typeIndex_(ti),
216 entryIndex_(ei),
217 typeIndexEnd_(lp->resource_ids_.size() + 1) {
218 while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) {
219 typeIndex_++;
220 }
221}
222
223LoadedPackage::iterator& LoadedPackage::iterator::operator++() {
224 while (typeIndex_ < typeIndexEnd_) {
225 if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) {
226 entryIndex_++;
227 break;
228 }
229 entryIndex_ = 0;
230 typeIndex_++;
231 if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) {
232 break;
233 }
234 }
235 return *this;
236}
237
238uint32_t LoadedPackage::iterator::operator*() const {
239 if (typeIndex_ >= typeIndexEnd_) {
240 return 0;
241 }
242 return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_,
243 entryIndex_);
244}
245
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700246base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntry(
247 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
248 base::expected<uint32_t, NullOrIOError> entry_offset = GetEntryOffset(type_chunk, entry_index);
249 if (UNLIKELY(!entry_offset.has_value())) {
250 return base::unexpected(entry_offset.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800251 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700252 return GetEntryFromOffset(type_chunk, entry_offset.value());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800253}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700254
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700255base::expected<uint32_t, NullOrIOError> LoadedPackage::GetEntryOffset(
256 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800257 // The configuration matches and is better than the previous selection.
258 // Find the entry value if it exists for this configuration.
259 const size_t entry_count = dtohl(type_chunk->entryCount);
260 const size_t offsets_offset = dtohs(type_chunk->header.headerSize);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700261
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800262 // Check if there is the desired entry in this type.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800263 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
264 // This is encoded as a sparse map, so perform a binary search.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700265 bool error = false;
266 auto sparse_indices = type_chunk.offset(offsets_offset)
267 .convert<ResTable_sparseTypeEntry>().iterator();
268 auto sparse_indices_end = sparse_indices + entry_count;
269 auto result = std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
270 [&error](const incfs::map_ptr<ResTable_sparseTypeEntry>& entry,
271 uint16_t entry_idx) {
272 if (UNLIKELY(!entry)) {
273 return error = true;
274 }
275 return dtohs(entry->idx) < entry_idx;
276 });
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800277
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700278 if (result == sparse_indices_end) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800279 // No entry found.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700280 return base::unexpected(std::nullopt);
281 }
282
283 const incfs::verified_map_ptr<ResTable_sparseTypeEntry> entry = (*result).verified();
284 if (dtohs(entry->idx) != entry_index) {
285 if (error) {
286 return base::unexpected(IOError::PAGES_MISSING);
287 }
288 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700289 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800290
291 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
292 // the real offset divided by 4.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700293 return uint32_t{dtohs(entry->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700294 }
295
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800296 // This type is encoded as a dense array.
297 if (entry_index >= entry_count) {
298 // This entry cannot be here.
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700299 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700300 }
301
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700302 const auto entry_offset_ptr = type_chunk.offset(offsets_offset).convert<uint32_t>() + entry_index;
303 if (UNLIKELY(!entry_offset_ptr)) {
304 return base::unexpected(IOError::PAGES_MISSING);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700305 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700306
307 const uint32_t value = dtohl(entry_offset_ptr.value());
308 if (value == ResTable_type::NO_ENTRY) {
309 return base::unexpected(std::nullopt);
310 }
311
312 return value;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700313}
314
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700315base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntryFromOffset(
316 incfs::verified_map_ptr<ResTable_type> type_chunk, uint32_t offset) {
317 auto valid = VerifyResTableEntry(type_chunk, offset);
318 if (UNLIKELY(!valid.has_value())) {
319 return base::unexpected(valid.error());
320 }
321 return type_chunk.offset(offset + dtohl(type_chunk->entriesStart)).convert<ResTable_entry>();
322}
323
324base::expected<std::monostate, IOError> LoadedPackage::CollectConfigurations(
325 bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {
Adam Lesinski0c405242017-01-13 20:47:26 -0800326 const size_t type_count = type_specs_.size();
327 for (size_t i = 0; i < type_count; i++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800328 const TypeSpecPtr& type_spec = type_specs_[i];
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700329 if (type_spec == nullptr) {
330 continue;
331 }
332 if (exclude_mipmap) {
333 const int type_idx = type_spec->type_spec->id - 1;
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 Lesinski0c405242017-01-13 20:47:26 -0800342 }
343 }
344
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700345 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 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800354 }
355 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700356
357 const auto iter_end = type_spec->types + type_spec->type_count;
358 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
359 ResTable_config config;
360 config.copyFromDtoH((*iter)->config);
361 out_configs->insert(config);
362 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800363 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700364 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -0800365}
366
367void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
368 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
369 const size_t type_count = type_specs_.size();
370 for (size_t i = 0; i < type_count; i++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800371 const TypeSpecPtr& type_spec = type_specs_[i];
Adam Lesinski0c405242017-01-13 20:47:26 -0800372 if (type_spec != nullptr) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800373 const auto iter_end = type_spec->types + type_spec->type_count;
374 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
375 ResTable_config configuration;
376 configuration.copyFromDtoH((*iter)->config);
Adam Lesinski0c405242017-01-13 20:47:26 -0800377 if (configuration.locale != 0) {
378 configuration.getBcp47Locale(temp_locale, canonicalize);
379 std::string locale(temp_locale);
380 out_locales->insert(std::move(locale));
381 }
382 }
383 }
384 }
385}
386
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700387base::expected<uint32_t, NullOrIOError> LoadedPackage::FindEntryByName(
388 const std::u16string& type_name, const std::u16string& entry_name) const {
389 const base::expected<size_t, NullOrIOError> type_idx = type_string_pool_.indexOfString(
390 type_name.data(), type_name.size());
391 if (!type_idx.has_value()) {
392 return base::unexpected(type_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800393 }
394
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700395 const base::expected<size_t, NullOrIOError> key_idx = key_string_pool_.indexOfString(
396 entry_name.data(), entry_name.size());
397 if (!key_idx.has_value()) {
398 return base::unexpected(key_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800399 }
400
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700401 const TypeSpec* type_spec = type_specs_[*type_idx].get();
Adam Lesinski929d6512017-01-16 19:11:19 -0800402 if (type_spec == nullptr) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700403 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800404 }
405
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800406 const auto iter_end = type_spec->types + type_spec->type_count;
407 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700408 const incfs::verified_map_ptr<ResTable_type>& type = *iter;
409
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800410 size_t entry_count = dtohl(type->entryCount);
Adam Lesinski929d6512017-01-16 19:11:19 -0800411 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700412 auto entry_offset_ptr = type.offset(dtohs(type->header.headerSize)).convert<uint32_t>() +
413 entry_idx;
414 if (!entry_offset_ptr) {
415 return base::unexpected(IOError::PAGES_MISSING);
416 }
417
418 auto offset = dtohl(entry_offset_ptr.value());
Adam Lesinski929d6512017-01-16 19:11:19 -0800419 if (offset != ResTable_type::NO_ENTRY) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700420 auto entry = type.offset(dtohl(type->entriesStart) + offset).convert<ResTable_entry>();
421 if (!entry) {
422 return base::unexpected(IOError::PAGES_MISSING);
423 }
424
425 if (dtohl(entry->key.index) == static_cast<uint32_t>(*key_idx)) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800426 // The package ID will be overridden by the caller (due to runtime assignment of package
427 // IDs for shared libraries).
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700428 return make_resid(0x00, *type_idx + type_id_offset_ + 1, entry_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800429 }
430 }
431 }
432 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700433 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800434}
435
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800436const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700437 for (const auto& loaded_package : packages_) {
438 if (loaded_package->GetPackageId() == package_id) {
439 return loaded_package.get();
440 }
441 }
442 return nullptr;
443}
444
445std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800446 package_property_t property_flags) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800447 ATRACE_NAME("LoadedPackage::Load");
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700448 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500449
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700450 // typeIdOffset was added at some point, but we still must recognize apps built before this
451 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700452 constexpr size_t kMinPackageSize =
453 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700454 const incfs::map_ptr<ResTable_package> header = chunk.header<ResTable_package, kMinPackageSize>();
455 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800456 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500457 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700458 }
459
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800460 if ((property_flags & PROPERTY_SYSTEM) != 0) {
461 loaded_package->property_flags_ |= PROPERTY_SYSTEM;
462 }
463
464 if ((property_flags & PROPERTY_LOADER) != 0) {
465 loaded_package->property_flags_ |= PROPERTY_LOADER;
466 }
467
468 if ((property_flags & PROPERTY_OVERLAY) != 0) {
469 // Overlay resources must have an exclusive resource id space for referencing internal
470 // resources.
471 loaded_package->property_flags_ |= PROPERTY_OVERLAY | PROPERTY_DYNAMIC;
472 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700473
Adam Lesinski7ad11102016-10-28 16:39:15 -0700474 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700475 if (loaded_package->package_id_ == 0 ||
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800476 (loaded_package->package_id_ == kAppPackageId && (property_flags & PROPERTY_DYNAMIC) != 0)) {
477 loaded_package->property_flags_ |= PROPERTY_DYNAMIC;
Winson9947f1e2019-08-16 10:20:39 -0700478 }
479
Adam Lesinskic6aada92017-01-13 15:34:14 -0800480 if (header->header.headerSize >= sizeof(ResTable_package)) {
481 uint32_t type_id_offset = dtohl(header->typeIdOffset);
482 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800483 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800484 return {};
485 }
486 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
487 }
488
Adam Lesinskida431a22016-12-29 16:08:16 -0500489 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
490 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700491
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800492 // A map of TypeSpec builders, each associated with an type index.
493 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
494 // contiguous block of memory that holds all the Types together with the TypeSpec.
495 std::unordered_map<int, std::unique_ptr<TypeSpecPtrBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700496
497 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
498 while (iter.HasNext()) {
499 const Chunk child_chunk = iter.Next();
500 switch (child_chunk.type()) {
501 case RES_STRING_POOL_TYPE: {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700502 const auto pool_address = child_chunk.header<ResChunk_header>();
503 if (!pool_address) {
504 LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation.";
505 return {};
506 }
507
508 if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700509 // This string pool is the type string pool.
510 status_t err = loaded_package->type_string_pool_.setTo(
511 child_chunk.header<ResStringPool_header>(), child_chunk.size());
512 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800513 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500514 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700515 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700516 } else if (pool_address == header.offset(dtohl(header->keyStrings))
517 .convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700518 // This string pool is the key string pool.
519 status_t err = loaded_package->key_string_pool_.setTo(
520 child_chunk.header<ResStringPool_header>(), child_chunk.size());
521 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800522 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500523 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700524 }
525 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800526 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700527 }
528 } break;
529
530 case RES_TABLE_TYPE_SPEC_TYPE: {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700531 const auto type_spec = child_chunk.header<ResTable_typeSpec>();
532 if (!type_spec) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800533 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500534 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700535 }
536
537 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800538 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500539 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700540 }
541
Adam Lesinskic6aada92017-01-13 15:34:14 -0800542 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
543 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800544 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800545 return {};
546 }
547
Adam Lesinski7ad11102016-10-28 16:39:15 -0700548 // The data portion of this chunk contains entry_count 32bit entries,
549 // each one representing a set of flags.
550 // Here we only validate that the chunk is well formed.
551 const size_t entry_count = dtohl(type_spec->entryCount);
552
553 // There can only be 2^16 entries in a type, because that is the ID
554 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
555 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800556 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500557 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700558 }
559
560 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800561 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500562 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700563 }
564
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800565 std::unique_ptr<TypeSpecPtrBuilder>& builder_ptr = type_builder_map[type_spec->id - 1];
566 if (builder_ptr == nullptr) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700567 builder_ptr = util::make_unique<TypeSpecPtrBuilder>(type_spec.verified());
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100568 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800569 } else {
570 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
571 type_spec->id);
572 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700573 } break;
574
575 case RES_TABLE_TYPE_TYPE: {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700576 const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
577 if (!type) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800578 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500579 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700580 }
581
Adam Lesinski498f6052017-11-29 13:24:29 -0800582 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500583 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700584 }
585
586 // Type chunks must be preceded by their TypeSpec chunks.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800587 std::unique_ptr<TypeSpecPtrBuilder>& builder_ptr = type_builder_map[type->id - 1];
588 if (builder_ptr != nullptr) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700589 builder_ptr->AddType(type.verified());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800590 } else {
591 LOG(ERROR) << StringPrintf(
592 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
593 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500594 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700595 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700596 } break;
597
Adam Lesinskida431a22016-12-29 16:08:16 -0500598 case RES_TABLE_LIBRARY_TYPE: {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700599 const auto lib = child_chunk.header<ResTable_lib_header>();
600 if (!lib) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800601 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500602 return {};
603 }
604
605 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800606 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500607 return {};
608 }
609
610 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
611
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700612 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>();
613 const auto entry_end = entry_begin + dtohl(lib->count);
Adam Lesinskida431a22016-12-29 16:08:16 -0500614 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700615 if (!entry_iter) {
616 return {};
617 }
618
Adam Lesinskida431a22016-12-29 16:08:16 -0500619 std::string package_name;
620 util::ReadUtf16StringFromDevice(entry_iter->packageName,
621 arraysize(entry_iter->packageName), &package_name);
622
623 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800624 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500625 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
626 dtohl(entry_iter->packageId), package_name.c_str());
627 return {};
628 }
629
630 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
631 dtohl(entry_iter->packageId));
632 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800633 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500634
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800635 case RES_TABLE_OVERLAYABLE_TYPE: {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700636 const auto overlayable = child_chunk.header<ResTable_overlayable_header>();
637 if (!overlayable) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800638 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
639 return {};
640 }
641
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800642 std::string name;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700643 util::ReadUtf16StringFromDevice(overlayable->name, arraysize(overlayable->name), &name);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800644 std::string actor;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700645 util::ReadUtf16StringFromDevice(overlayable->actor, arraysize(overlayable->actor), &actor);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800646
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100647 if (loaded_package->overlayable_map_.find(name) !=
648 loaded_package->overlayable_map_.end()) {
649 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '" << name << "'.";
650 return {};
651 }
652 loaded_package->overlayable_map_.emplace(name, actor);
653
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800654 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800655 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
656 while (overlayable_iter.HasNext()) {
657 const Chunk overlayable_child_chunk = overlayable_iter.Next();
658
659 switch (overlayable_child_chunk.type()) {
660 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700661 const auto policy_header =
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800662 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700663 if (!policy_header) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800664 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
665 return {};
666 }
667
668 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
669 < dtohl(policy_header->entry_count)) {
670 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
671 return {};
672 }
673
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800674 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800675 std::unordered_set<uint32_t> ids;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700676 const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800677 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
678 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700679 if (!id_iter) {
680 return {};
681 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800682 ids.insert(dtohl(id_iter->ident));
683 }
684
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800685 // Add the pairing of overlayable properties and resource ids to the package
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800686 OverlayableInfo overlayable_info{};
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800687 overlayable_info.name = name;
688 overlayable_info.actor = actor;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800689 overlayable_info.policy_flags = policy_header->policy_flags;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700690 loaded_package->overlayable_infos_.emplace_back(overlayable_info, ids);
Ryan Mitchell19823452019-01-29 12:01:24 -0800691 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800692 break;
693 }
694
695 default:
696 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
697 break;
698 }
699 }
700
701 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800702 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800703 overlayable_iter.GetLastError().c_str());
704 if (overlayable_iter.HadFatalError()) {
705 return {};
706 }
707 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500708 } break;
709
Adam Lesinski7ad11102016-10-28 16:39:15 -0700710 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800711 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700712 break;
713 }
714 }
715
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800716 if (iter.HadError()) {
717 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700718 if (iter.HadFatalError()) {
719 return {};
720 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800721 }
722
723 // Flatten and construct the TypeSpecs.
724 for (auto& entry : type_builder_map) {
725 uint8_t type_idx = static_cast<uint8_t>(entry.first);
726 TypeSpecPtr type_spec_ptr = entry.second->Build();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700727 if (type_spec_ptr == nullptr) {
728 LOG(ERROR) << "Too many type configurations, overflow detected.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500729 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700730 }
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700731
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700732 loaded_package->type_specs_.editItemAt(type_idx) = std::move(type_spec_ptr);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700733 }
734
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700735 return std::move(loaded_package);
736}
737
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700738bool LoadedArsc::LoadTable(const Chunk& chunk, const LoadedIdmap* loaded_idmap,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800739 package_property_t property_flags) {
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700740 incfs::map_ptr<ResTable_header> header = chunk.header<ResTable_header>();
741 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800742 LOG(ERROR) << "RES_TABLE_TYPE too small.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700743 return false;
744 }
745
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700746 if (loaded_idmap != nullptr) {
747 global_string_pool_ = util::make_unique<OverlayStringPool>(loaded_idmap);
748 }
749
Adam Lesinski7ad11102016-10-28 16:39:15 -0700750 const size_t package_count = dtohl(header->packageCount);
751 size_t packages_seen = 0;
752
753 packages_.reserve(package_count);
754
755 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
756 while (iter.HasNext()) {
757 const Chunk child_chunk = iter.Next();
758 switch (child_chunk.type()) {
759 case RES_STRING_POOL_TYPE:
760 // Only use the first string pool. Ignore others.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700761 if (global_string_pool_->getError() == NO_INIT) {
762 status_t err = global_string_pool_->setTo(child_chunk.header<ResStringPool_header>(),
763 child_chunk.size());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700764 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800765 LOG(ERROR) << "RES_STRING_POOL_TYPE corrupt.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700766 return false;
767 }
768 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800769 LOG(WARNING) << "Multiple RES_STRING_POOL_TYPEs found in RES_TABLE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700770 }
771 break;
772
773 case RES_TABLE_PACKAGE_TYPE: {
774 if (packages_seen + 1 > package_count) {
775 LOG(ERROR) << "More package chunks were found than the " << package_count
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700776 << " declared in the header.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700777 return false;
778 }
779 packages_seen++;
780
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700781 std::unique_ptr<const LoadedPackage> loaded_package =
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800782 LoadedPackage::Load(child_chunk, property_flags);
Adam Lesinskida431a22016-12-29 16:08:16 -0500783 if (!loaded_package) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700784 return false;
785 }
786 packages_.push_back(std::move(loaded_package));
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 false;
799 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700800 }
801 return true;
802}
803
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700804std::unique_ptr<const LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data,
805 const size_t length,
Winson9947f1e2019-08-16 10:20:39 -0700806 const LoadedIdmap* loaded_idmap,
Ryan Mitchellef40d2e2020-03-11 10:26:08 -0700807 const package_property_t property_flags) {
Winson9947f1e2019-08-16 10:20:39 -0700808 ATRACE_NAME("LoadedArsc::Load");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700809
810 // Not using make_unique because the constructor is private.
811 std::unique_ptr<LoadedArsc> loaded_arsc(new LoadedArsc());
812
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700813 ChunkIterator iter(data, length);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700814 while (iter.HasNext()) {
815 const Chunk chunk = iter.Next();
816 switch (chunk.type()) {
817 case RES_TABLE_TYPE:
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800818 if (!loaded_arsc->LoadTable(chunk, loaded_idmap, property_flags)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700819 return {};
820 }
821 break;
822
823 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800824 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700825 break;
826 }
827 }
828
829 if (iter.HadError()) {
830 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700831 if (iter.HadFatalError()) {
832 return {};
833 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700834 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800835
836 // Need to force a move for mingw32.
837 return std::move(loaded_arsc);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700838}
839
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700840std::unique_ptr<const LoadedArsc> LoadedArsc::CreateEmpty() {
841 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
842}
843
Adam Lesinski7ad11102016-10-28 16:39:15 -0700844} // namespace android