blob: d17c32817994c7c176298de89db879d3e464f4d4 [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
Ryan Mitchell2fedba92021-04-23 07:47:38 -070044constexpr const static int kFrameworkPackageId = 0x01;
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.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080052struct TypeSpecBuilder {
53 explicit TypeSpecBuilder(incfs::verified_map_ptr<ResTable_typeSpec> header) : header_(header) {}
Adam Lesinski7ad11102016-10-28 16:39:15 -070054
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000055 void AddType(incfs::verified_map_ptr<ResTable_type> type) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080056 TypeSpec::TypeEntry& entry = type_entries.emplace_back();
57 entry.config.copyFromDtoH(type->config);
58 entry.type = type;
Adam Lesinski7ad11102016-10-28 16:39:15 -070059 }
60
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080061 TypeSpec Build() {
62 return {header_, std::move(type_entries)};
Adam Lesinski7ad11102016-10-28 16:39:15 -070063 }
64
65 private:
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080066 DISALLOW_COPY_AND_ASSIGN(TypeSpecBuilder);
Adam Lesinski7ad11102016-10-28 16:39:15 -070067
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000068 incfs::verified_map_ptr<ResTable_typeSpec> header_;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -080069 std::vector<TypeSpec::TypeEntry> type_entries;
Adam Lesinski7ad11102016-10-28 16:39:15 -070070};
71
72} // namespace
73
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070074// Precondition: The header passed in has already been verified, so reading any fields and trusting
75// the ResChunk_header is safe.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +000076static bool VerifyResTableType(incfs::map_ptr<ResTable_type> header) {
Adam Lesinski498f6052017-11-29 13:24:29 -080077 if (header->id == 0) {
78 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has invalid ID 0.";
79 return false;
80 }
81
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070082 const size_t entry_count = dtohl(header->entryCount);
83 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -080084 LOG(ERROR) << "RES_TABLE_TYPE_TYPE has too many entries (" << entry_count << ").";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070085 return false;
86 }
87
88 // Make sure that there is enough room for the entry offsets.
89 const size_t offsets_offset = dtohs(header->header.headerSize);
90 const size_t entries_offset = dtohl(header->entriesStart);
91 const size_t offsets_length = sizeof(uint32_t) * entry_count;
92
93 if (offsets_offset > entries_offset || entries_offset - offsets_offset < offsets_length) {
Adam Lesinski498f6052017-11-29 13:24:29 -080094 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070095 return false;
96 }
97
98 if (entries_offset > dtohl(header->header.size)) {
Adam Lesinski498f6052017-11-29 13:24:29 -080099 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entry offsets extend beyond chunk.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700100 return false;
101 }
102
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000103 if (entries_offset & 0x03U) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800104 LOG(ERROR) << "RES_TABLE_TYPE_TYPE entries start at unaligned address.";
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700105 return false;
106 }
107 return true;
108}
109
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000110static base::expected<std::monostate, NullOrIOError> VerifyResTableEntry(
111 incfs::verified_map_ptr<ResTable_type> type, uint32_t entry_offset) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700112 // Check that the offset is aligned.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000113 if (UNLIKELY(entry_offset & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800114 LOG(ERROR) << "Entry at offset " << entry_offset << " is not 4-byte aligned.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000115 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700116 }
117
118 // Check that the offset doesn't overflow.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000119 if (UNLIKELY(entry_offset > std::numeric_limits<uint32_t>::max() - dtohl(type->entriesStart))) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700120 // Overflow in offset.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800121 LOG(ERROR) << "Entry at offset " << entry_offset << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000122 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700123 }
124
125 const size_t chunk_size = dtohl(type->header.size);
126
127 entry_offset += dtohl(type->entriesStart);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000128 if (UNLIKELY(entry_offset > chunk_size - sizeof(ResTable_entry))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800129 LOG(ERROR) << "Entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700130 << " is too large. No room for ResTable_entry.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000131 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700132 }
133
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000134 auto entry = type.offset(entry_offset).convert<ResTable_entry>();
135 if (UNLIKELY(!entry)) {
136 return base::unexpected(IOError::PAGES_MISSING);
137 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700138
139 const size_t entry_size = dtohs(entry->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000140 if (UNLIKELY(entry_size < sizeof(entry.value()))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800141 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700142 << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000143 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700144 }
145
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000146 if (UNLIKELY(entry_size > chunk_size || entry_offset > chunk_size - entry_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800147 LOG(ERROR) << "ResTable_entry size " << entry_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700148 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000149 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700150 }
151
152 if (entry_size < sizeof(ResTable_map_entry)) {
153 // There needs to be room for one Res_value struct.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000154 if (UNLIKELY(entry_offset + entry_size > chunk_size - sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800155 LOG(ERROR) << "No room for Res_value after ResTable_entry at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700156 << " for type " << (int)type->id << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000157 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700158 }
159
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000160 auto value = entry.offset(entry_size).convert<Res_value>();
161 if (UNLIKELY(!value)) {
162 return base::unexpected(IOError::PAGES_MISSING);
163 }
164
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700165 const size_t value_size = dtohs(value->size);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000166 if (UNLIKELY(value_size < sizeof(Res_value))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800167 LOG(ERROR) << "Res_value at offset " << entry_offset << " is too small.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000168 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700169 }
170
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000171 if (UNLIKELY(value_size > chunk_size || entry_offset + entry_size > chunk_size - value_size)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800172 LOG(ERROR) << "Res_value size " << value_size << " at offset " << entry_offset
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700173 << " is too large.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000174 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700175 }
176 } else {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000177 auto map = entry.convert<ResTable_map_entry>();
178 if (UNLIKELY(!map)) {
179 return base::unexpected(IOError::PAGES_MISSING);
180 }
181
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700182 const size_t map_entry_count = dtohl(map->count);
183 size_t map_entries_start = entry_offset + entry_size;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000184 if (UNLIKELY(map_entries_start & 0x03U)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800185 LOG(ERROR) << "Map entries at offset " << entry_offset << " start at unaligned offset.";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000186 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700187 }
188
189 // Each entry is sizeof(ResTable_map) big.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000190 if (UNLIKELY(map_entry_count > ((chunk_size - map_entries_start) / sizeof(ResTable_map)))) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800191 LOG(ERROR) << "Too many map entries in ResTable_map_entry at offset " << entry_offset << ".";
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000192 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700193 }
194 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000195 return {};
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700196}
197
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100198LoadedPackage::iterator::iterator(const LoadedPackage* lp, size_t ti, size_t ei)
199 : loadedPackage_(lp),
200 typeIndex_(ti),
201 entryIndex_(ei),
202 typeIndexEnd_(lp->resource_ids_.size() + 1) {
203 while (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] == 0) {
204 typeIndex_++;
205 }
206}
207
208LoadedPackage::iterator& LoadedPackage::iterator::operator++() {
209 while (typeIndex_ < typeIndexEnd_) {
210 if (entryIndex_ + 1 < loadedPackage_->resource_ids_[typeIndex_]) {
211 entryIndex_++;
212 break;
213 }
214 entryIndex_ = 0;
215 typeIndex_++;
216 if (typeIndex_ < typeIndexEnd_ && loadedPackage_->resource_ids_[typeIndex_] != 0) {
217 break;
218 }
219 }
220 return *this;
221}
222
223uint32_t LoadedPackage::iterator::operator*() const {
224 if (typeIndex_ >= typeIndexEnd_) {
225 return 0;
226 }
227 return make_resid(loadedPackage_->package_id_, typeIndex_ + loadedPackage_->type_id_offset_,
228 entryIndex_);
229}
230
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000231base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntry(
232 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
233 base::expected<uint32_t, NullOrIOError> entry_offset = GetEntryOffset(type_chunk, entry_index);
234 if (UNLIKELY(!entry_offset.has_value())) {
235 return base::unexpected(entry_offset.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800236 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000237 return GetEntryFromOffset(type_chunk, entry_offset.value());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800238}
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700239
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000240base::expected<uint32_t, NullOrIOError> LoadedPackage::GetEntryOffset(
241 incfs::verified_map_ptr<ResTable_type> type_chunk, uint16_t entry_index) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800242 // The configuration matches and is better than the previous selection.
243 // Find the entry value if it exists for this configuration.
244 const size_t entry_count = dtohl(type_chunk->entryCount);
245 const size_t offsets_offset = dtohs(type_chunk->header.headerSize);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700246
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800247 // Check if there is the desired entry in this type.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800248 if (type_chunk->flags & ResTable_type::FLAG_SPARSE) {
249 // This is encoded as a sparse map, so perform a binary search.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000250 bool error = false;
251 auto sparse_indices = type_chunk.offset(offsets_offset)
252 .convert<ResTable_sparseTypeEntry>().iterator();
253 auto sparse_indices_end = sparse_indices + entry_count;
254 auto result = std::lower_bound(sparse_indices, sparse_indices_end, entry_index,
255 [&error](const incfs::map_ptr<ResTable_sparseTypeEntry>& entry,
256 uint16_t entry_idx) {
257 if (UNLIKELY(!entry)) {
258 return error = true;
259 }
260 return dtohs(entry->idx) < entry_idx;
261 });
Adam Lesinski73f6f9d2017-11-14 10:18:05 -0800262
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000263 if (result == sparse_indices_end) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800264 // No entry found.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000265 return base::unexpected(std::nullopt);
266 }
267
268 const incfs::verified_map_ptr<ResTable_sparseTypeEntry> entry = (*result).verified();
269 if (dtohs(entry->idx) != entry_index) {
270 if (error) {
271 return base::unexpected(IOError::PAGES_MISSING);
272 }
273 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700274 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800275
276 // Extract the offset from the entry. Each offset must be a multiple of 4 so we store it as
277 // the real offset divided by 4.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000278 return uint32_t{dtohs(entry->offset)} * 4u;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700279 }
280
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800281 // This type is encoded as a dense array.
282 if (entry_index >= entry_count) {
283 // This entry cannot be here.
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000284 return base::unexpected(std::nullopt);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700285 }
286
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000287 const auto entry_offset_ptr = type_chunk.offset(offsets_offset).convert<uint32_t>() + entry_index;
288 if (UNLIKELY(!entry_offset_ptr)) {
289 return base::unexpected(IOError::PAGES_MISSING);
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700290 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000291
292 const uint32_t value = dtohl(entry_offset_ptr.value());
293 if (value == ResTable_type::NO_ENTRY) {
294 return base::unexpected(std::nullopt);
295 }
296
297 return value;
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700298}
299
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000300base::expected<incfs::map_ptr<ResTable_entry>, NullOrIOError> LoadedPackage::GetEntryFromOffset(
301 incfs::verified_map_ptr<ResTable_type> type_chunk, uint32_t offset) {
302 auto valid = VerifyResTableEntry(type_chunk, offset);
303 if (UNLIKELY(!valid.has_value())) {
304 return base::unexpected(valid.error());
305 }
306 return type_chunk.offset(offset + dtohl(type_chunk->entriesStart)).convert<ResTable_entry>();
307}
308
309base::expected<std::monostate, IOError> LoadedPackage::CollectConfigurations(
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800310 bool exclude_mipmap, std::set<ResTable_config>* out_configs) const {\
311 for (const auto& type_spec : type_specs_) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000312 if (exclude_mipmap) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800313 const int type_idx = type_spec.first - 1;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000314 const auto type_name16 = type_string_pool_.stringAt(type_idx);
315 if (UNLIKELY(IsIOError(type_name16))) {
316 return base::unexpected(GetIOError(type_name16.error()));
317 }
318 if (type_name16.has_value()) {
319 if (strncmp16(type_name16->data(), u"mipmap", type_name16->size()) == 0) {
320 // This is a mipmap type, skip collection.
321 continue;
Adam Lesinski0c405242017-01-13 20:47:26 -0800322 }
323 }
324
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000325 const auto type_name = type_string_pool_.string8At(type_idx);
326 if (UNLIKELY(IsIOError(type_name))) {
327 return base::unexpected(GetIOError(type_name.error()));
328 }
329 if (type_name.has_value()) {
330 if (strncmp(type_name->data(), "mipmap", type_name->size()) == 0) {
331 // This is a mipmap type, skip collection.
332 continue;
333 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700334 }
Ryan Mitchellc75c2e02020-08-17 08:42:48 -0700335 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000336
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800337 for (const auto& type_entry : type_spec.second.type_entries) {
338 out_configs->insert(type_entry.config);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000339 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800340 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000341 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -0800342}
343
344void LoadedPackage::CollectLocales(bool canonicalize, std::set<std::string>* out_locales) const {
345 char temp_locale[RESTABLE_MAX_LOCALE_LEN];
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800346 for (const auto& type_spec : type_specs_) {
347 for (const auto& type_entry : type_spec.second.type_entries) {
348 if (type_entry.config.locale != 0) {
349 type_entry.config.getBcp47Locale(temp_locale, canonicalize);
350 std::string locale(temp_locale);
351 out_locales->insert(std::move(locale));
Adam Lesinski0c405242017-01-13 20:47:26 -0800352 }
353 }
354 }
355}
356
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000357base::expected<uint32_t, NullOrIOError> LoadedPackage::FindEntryByName(
358 const std::u16string& type_name, const std::u16string& entry_name) const {
359 const base::expected<size_t, NullOrIOError> type_idx = type_string_pool_.indexOfString(
360 type_name.data(), type_name.size());
361 if (!type_idx.has_value()) {
362 return base::unexpected(type_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800363 }
364
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000365 const base::expected<size_t, NullOrIOError> key_idx = key_string_pool_.indexOfString(
366 entry_name.data(), entry_name.size());
367 if (!key_idx.has_value()) {
368 return base::unexpected(key_idx.error());
Adam Lesinski929d6512017-01-16 19:11:19 -0800369 }
370
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800371 const TypeSpec* type_spec = GetTypeSpecByTypeIndex(*type_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800372 if (type_spec == nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000373 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800374 }
375
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800376 for (const auto& type_entry : type_spec->type_entries) {
377 const incfs::verified_map_ptr<ResTable_type>& type = type_entry.type;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000378
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800379 size_t entry_count = dtohl(type->entryCount);
Adam Lesinski929d6512017-01-16 19:11:19 -0800380 for (size_t entry_idx = 0; entry_idx < entry_count; entry_idx++) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000381 auto entry_offset_ptr = type.offset(dtohs(type->header.headerSize)).convert<uint32_t>() +
382 entry_idx;
383 if (!entry_offset_ptr) {
384 return base::unexpected(IOError::PAGES_MISSING);
385 }
386
387 auto offset = dtohl(entry_offset_ptr.value());
Adam Lesinski929d6512017-01-16 19:11:19 -0800388 if (offset != ResTable_type::NO_ENTRY) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000389 auto entry = type.offset(dtohl(type->entriesStart) + offset).convert<ResTable_entry>();
390 if (!entry) {
391 return base::unexpected(IOError::PAGES_MISSING);
392 }
393
394 if (dtohl(entry->key.index) == static_cast<uint32_t>(*key_idx)) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800395 // The package ID will be overridden by the caller (due to runtime assignment of package
396 // IDs for shared libraries).
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000397 return make_resid(0x00, *type_idx + type_id_offset_ + 1, entry_idx);
Adam Lesinski929d6512017-01-16 19:11:19 -0800398 }
399 }
400 }
401 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000402 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -0800403}
404
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800405const LoadedPackage* LoadedArsc::GetPackageById(uint8_t package_id) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700406 for (const auto& loaded_package : packages_) {
407 if (loaded_package->GetPackageId() == package_id) {
408 return loaded_package.get();
409 }
410 }
411 return nullptr;
412}
413
414std::unique_ptr<const LoadedPackage> LoadedPackage::Load(const Chunk& chunk,
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800415 package_property_t property_flags) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800416 ATRACE_NAME("LoadedPackage::Load");
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700417 std::unique_ptr<LoadedPackage> loaded_package(new LoadedPackage());
Adam Lesinskida431a22016-12-29 16:08:16 -0500418
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700419 // typeIdOffset was added at some point, but we still must recognize apps built before this
420 // was added.
Adam Lesinski33af6c72017-03-29 13:00:35 -0700421 constexpr size_t kMinPackageSize =
422 sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000423 const incfs::map_ptr<ResTable_package> header = chunk.header<ResTable_package, kMinPackageSize>();
424 if (!header) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800425 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500426 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700427 }
428
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800429 if ((property_flags & PROPERTY_SYSTEM) != 0) {
430 loaded_package->property_flags_ |= PROPERTY_SYSTEM;
431 }
432
433 if ((property_flags & PROPERTY_LOADER) != 0) {
434 loaded_package->property_flags_ |= PROPERTY_LOADER;
435 }
436
437 if ((property_flags & PROPERTY_OVERLAY) != 0) {
438 // Overlay resources must have an exclusive resource id space for referencing internal
439 // resources.
440 loaded_package->property_flags_ |= PROPERTY_OVERLAY | PROPERTY_DYNAMIC;
441 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700442
Adam Lesinski7ad11102016-10-28 16:39:15 -0700443 loaded_package->package_id_ = dtohl(header->id);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700444 if (loaded_package->package_id_ == 0 ||
Ryan Mitchell73bfe412019-11-12 16:22:04 -0800445 (loaded_package->package_id_ == kAppPackageId && (property_flags & PROPERTY_DYNAMIC) != 0)) {
446 loaded_package->property_flags_ |= PROPERTY_DYNAMIC;
Winson9947f1e2019-08-16 10:20:39 -0700447 }
448
Adam Lesinskic6aada92017-01-13 15:34:14 -0800449 if (header->header.headerSize >= sizeof(ResTable_package)) {
450 uint32_t type_id_offset = dtohl(header->typeIdOffset);
451 if (type_id_offset > std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800452 LOG(ERROR) << "RES_TABLE_PACKAGE_TYPE type ID offset too large.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800453 return {};
454 }
455 loaded_package->type_id_offset_ = static_cast<int>(type_id_offset);
456 }
457
Adam Lesinskida431a22016-12-29 16:08:16 -0500458 util::ReadUtf16StringFromDevice(header->name, arraysize(header->name),
459 &loaded_package->package_name_);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700460
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800461 // A map of TypeSpec builders, each associated with an type index.
462 // We use these to accumulate the set of Types available for a TypeSpec, and later build a single,
463 // contiguous block of memory that holds all the Types together with the TypeSpec.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800464 std::unordered_map<int, std::unique_ptr<TypeSpecBuilder>> type_builder_map;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700465
466 ChunkIterator iter(chunk.data_ptr(), chunk.data_size());
467 while (iter.HasNext()) {
468 const Chunk child_chunk = iter.Next();
469 switch (child_chunk.type()) {
470 case RES_STRING_POOL_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000471 const auto pool_address = child_chunk.header<ResChunk_header>();
472 if (!pool_address) {
473 LOG(ERROR) << "RES_STRING_POOL_TYPE is incomplete due to incremental installation.";
474 return {};
475 }
476
477 if (pool_address == header.offset(dtohl(header->typeStrings)).convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700478 // This string pool is the type string pool.
479 status_t err = loaded_package->type_string_pool_.setTo(
480 child_chunk.header<ResStringPool_header>(), child_chunk.size());
481 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800482 LOG(ERROR) << "RES_STRING_POOL_TYPE for types corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500483 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700484 }
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000485 } else if (pool_address == header.offset(dtohl(header->keyStrings))
486 .convert<ResChunk_header>()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700487 // This string pool is the key string pool.
488 status_t err = loaded_package->key_string_pool_.setTo(
489 child_chunk.header<ResStringPool_header>(), child_chunk.size());
490 if (err != NO_ERROR) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800491 LOG(ERROR) << "RES_STRING_POOL_TYPE for keys corrupt.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500492 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700493 }
494 } else {
Adam Lesinski498f6052017-11-29 13:24:29 -0800495 LOG(WARNING) << "Too many RES_STRING_POOL_TYPEs found in RES_TABLE_PACKAGE_TYPE.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700496 }
497 } break;
498
499 case RES_TABLE_TYPE_SPEC_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000500 const auto type_spec = child_chunk.header<ResTable_typeSpec>();
501 if (!type_spec) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800502 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500503 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700504 }
505
506 if (type_spec->id == 0) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800507 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has invalid ID 0.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500508 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700509 }
510
Adam Lesinskic6aada92017-01-13 15:34:14 -0800511 if (loaded_package->type_id_offset_ + static_cast<int>(type_spec->id) >
512 std::numeric_limits<uint8_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800513 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has out of range ID.";
Adam Lesinskic6aada92017-01-13 15:34:14 -0800514 return {};
515 }
516
Adam Lesinski7ad11102016-10-28 16:39:15 -0700517 // The data portion of this chunk contains entry_count 32bit entries,
518 // each one representing a set of flags.
519 // Here we only validate that the chunk is well formed.
520 const size_t entry_count = dtohl(type_spec->entryCount);
521
522 // There can only be 2^16 entries in a type, because that is the ID
523 // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
524 if (entry_count > std::numeric_limits<uint16_t>::max()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800525 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE has too many entries (" << entry_count << ").";
Adam Lesinskida431a22016-12-29 16:08:16 -0500526 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700527 }
528
529 if (entry_count * sizeof(uint32_t) > chunk.data_size()) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800530 LOG(ERROR) << "RES_TABLE_TYPE_SPEC_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500531 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700532 }
533
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800534 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type_spec->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800535 if (builder_ptr == nullptr) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800536 builder_ptr = util::make_unique<TypeSpecBuilder>(type_spec.verified());
MÃ¥rten Kongstad3f1f4fc2018-03-02 09:34:18 +0100537 loaded_package->resource_ids_.set(type_spec->id, entry_count);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800538 } else {
539 LOG(WARNING) << StringPrintf("RES_TABLE_TYPE_SPEC_TYPE already defined for ID %02x",
540 type_spec->id);
541 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700542 } break;
543
544 case RES_TABLE_TYPE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000545 const auto type = child_chunk.header<ResTable_type, kResTableTypeMinSize>();
546 if (!type) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800547 LOG(ERROR) << "RES_TABLE_TYPE_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500548 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700549 }
550
Adam Lesinski498f6052017-11-29 13:24:29 -0800551 if (!VerifyResTableType(type)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500552 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700553 }
554
555 // Type chunks must be preceded by their TypeSpec chunks.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800556 std::unique_ptr<TypeSpecBuilder>& builder_ptr = type_builder_map[type->id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800557 if (builder_ptr != nullptr) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000558 builder_ptr->AddType(type.verified());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800559 } else {
560 LOG(ERROR) << StringPrintf(
561 "RES_TABLE_TYPE_TYPE with ID %02x found without preceding RES_TABLE_TYPE_SPEC_TYPE.",
562 type->id);
Adam Lesinskida431a22016-12-29 16:08:16 -0500563 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700564 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700565 } break;
566
Adam Lesinskida431a22016-12-29 16:08:16 -0500567 case RES_TABLE_LIBRARY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000568 const auto lib = child_chunk.header<ResTable_lib_header>();
569 if (!lib) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800570 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500571 return {};
572 }
573
574 if (child_chunk.data_size() / sizeof(ResTable_lib_entry) < dtohl(lib->count)) {
Adam Lesinski498f6052017-11-29 13:24:29 -0800575 LOG(ERROR) << "RES_TABLE_LIBRARY_TYPE too small to hold entries.";
Adam Lesinskida431a22016-12-29 16:08:16 -0500576 return {};
577 }
578
579 loaded_package->dynamic_package_map_.reserve(dtohl(lib->count));
580
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000581 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_lib_entry>();
582 const auto entry_end = entry_begin + dtohl(lib->count);
Adam Lesinskida431a22016-12-29 16:08:16 -0500583 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000584 if (!entry_iter) {
585 return {};
586 }
587
Adam Lesinskida431a22016-12-29 16:08:16 -0500588 std::string package_name;
589 util::ReadUtf16StringFromDevice(entry_iter->packageName,
590 arraysize(entry_iter->packageName), &package_name);
591
592 if (dtohl(entry_iter->packageId) >= std::numeric_limits<uint8_t>::max()) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800593 LOG(ERROR) << StringPrintf(
Adam Lesinskida431a22016-12-29 16:08:16 -0500594 "Package ID %02x in RES_TABLE_LIBRARY_TYPE too large for package '%s'.",
595 dtohl(entry_iter->packageId), package_name.c_str());
596 return {};
597 }
598
599 loaded_package->dynamic_package_map_.emplace_back(std::move(package_name),
600 dtohl(entry_iter->packageId));
601 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800602 } break;
Adam Lesinskida431a22016-12-29 16:08:16 -0500603
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800604 case RES_TABLE_OVERLAYABLE_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000605 const auto overlayable = child_chunk.header<ResTable_overlayable_header>();
606 if (!overlayable) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800607 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_TYPE too small.";
608 return {};
609 }
610
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800611 std::string name;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000612 util::ReadUtf16StringFromDevice(overlayable->name, arraysize(overlayable->name), &name);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800613 std::string actor;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000614 util::ReadUtf16StringFromDevice(overlayable->actor, arraysize(overlayable->actor), &actor);
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800615
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100616 if (loaded_package->overlayable_map_.find(name) !=
617 loaded_package->overlayable_map_.end()) {
618 LOG(ERROR) << "Multiple <overlayable> blocks with the same name '" << name << "'.";
619 return {};
620 }
621 loaded_package->overlayable_map_.emplace(name, actor);
622
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800623 // Iterate over the overlayable policy chunks contained within the overlayable chunk data
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800624 ChunkIterator overlayable_iter(child_chunk.data_ptr(), child_chunk.data_size());
625 while (overlayable_iter.HasNext()) {
626 const Chunk overlayable_child_chunk = overlayable_iter.Next();
627
628 switch (overlayable_child_chunk.type()) {
629 case RES_TABLE_OVERLAYABLE_POLICY_TYPE: {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000630 const auto policy_header =
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800631 overlayable_child_chunk.header<ResTable_overlayable_policy_header>();
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000632 if (!policy_header) {
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800633 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small.";
634 return {};
635 }
636
637 if ((overlayable_child_chunk.data_size() / sizeof(ResTable_ref))
638 < dtohl(policy_header->entry_count)) {
639 LOG(ERROR) << "RES_TABLE_OVERLAYABLE_POLICY_TYPE too small to hold entries.";
640 return {};
641 }
642
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800643 // Retrieve all the resource ids belonging to this policy chunk
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800644 std::unordered_set<uint32_t> ids;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000645 const auto ids_begin = overlayable_child_chunk.data_ptr().convert<ResTable_ref>();
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800646 const auto ids_end = ids_begin + dtohl(policy_header->entry_count);
647 for (auto id_iter = ids_begin; id_iter != ids_end; ++id_iter) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000648 if (!id_iter) {
649 return {};
650 }
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800651 ids.insert(dtohl(id_iter->ident));
652 }
653
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800654 // Add the pairing of overlayable properties and resource ids to the package
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800655 OverlayableInfo overlayable_info{};
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800656 overlayable_info.name = name;
657 overlayable_info.actor = actor;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800658 overlayable_info.policy_flags = policy_header->policy_flags;
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000659 loaded_package->overlayable_infos_.emplace_back(overlayable_info, ids);
Ryan Mitchell19823452019-01-29 12:01:24 -0800660 loaded_package->defines_overlayable_ = true;
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800661 break;
662 }
663
664 default:
665 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
666 break;
667 }
668 }
669
670 if (overlayable_iter.HadError()) {
Ryan Mitchellef5673a2018-12-12 18:45:34 -0800671 LOG(ERROR) << StringPrintf("Error parsing RES_TABLE_OVERLAYABLE_TYPE: %s",
Ryan Mitchell75e20dd2018-11-06 16:39:36 -0800672 overlayable_iter.GetLastError().c_str());
673 if (overlayable_iter.HadFatalError()) {
674 return {};
675 }
676 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500677 } break;
678
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700679 case RES_TABLE_STAGED_ALIAS_TYPE: {
680 if (loaded_package->package_id_ != kFrameworkPackageId) {
681 LOG(WARNING) << "Alias chunk ignored for non-framework package '"
682 << loaded_package->package_name_ << "'";
683 break;
684 }
685
686 std::unordered_set<uint32_t> finalized_ids;
687 const auto lib_alias = child_chunk.header<ResTable_staged_alias_header>();
688 if (!lib_alias) {
689 return {};
690 }
691 const auto entry_begin = child_chunk.data_ptr().convert<ResTable_staged_alias_entry>();
692 const auto entry_end = entry_begin + dtohl(lib_alias->count);
693 for (auto entry_iter = entry_begin; entry_iter != entry_end; ++entry_iter) {
694 if (!entry_iter) {
695 return {};
696 }
697 auto finalized_id = dtohl(entry_iter->finalizedResId);
698 if (!finalized_ids.insert(finalized_id).second) {
699 LOG(ERROR) << StringPrintf("Repeated finalized resource id '%08x' in staged aliases.",
700 finalized_id);
701 return {};
702 }
703
704 auto staged_id = dtohl(entry_iter->stagedResId);
705 auto [_, success] = loaded_package->alias_id_map_.insert(std::make_pair(staged_id,
706 finalized_id));
707 if (!success) {
708 LOG(ERROR) << StringPrintf("Repeated staged resource id '%08x' in staged aliases.",
709 staged_id);
710 return {};
711 }
712 }
713 } break;
714
Adam Lesinski7ad11102016-10-28 16:39:15 -0700715 default:
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800716 LOG(WARNING) << StringPrintf("Unknown chunk type '%02x'.", chunk.type());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700717 break;
718 }
719 }
720
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800721 if (iter.HadError()) {
722 LOG(ERROR) << iter.GetLastError();
Todd Kennedy28e663c2018-07-12 13:15:54 -0700723 if (iter.HadFatalError()) {
724 return {};
725 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800726 }
727
728 // Flatten and construct the TypeSpecs.
729 for (auto& entry : type_builder_map) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800730 TypeSpec type_spec = entry.second->Build();
731 uint8_t type_id = static_cast<uint8_t>(entry.first);
732 loaded_package->type_specs_[type_id] = std::move(type_spec);
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 Mitchelldb21f09a2020-11-16 23:08:18 +0000740 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 Mitchell1a48fa62021-01-10 08:36:36 -0800804std::unique_ptr<LoadedArsc> LoadedArsc::Load(incfs::map_ptr<void> data,
805 const size_t length,
806 const LoadedIdmap* loaded_idmap,
807 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 Mitchelldb21f09a2020-11-16 23:08:18 +0000813 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
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800836 return loaded_arsc;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700837}
838
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800839std::unique_ptr<LoadedArsc> LoadedArsc::CreateEmpty() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700840 return std::unique_ptr<LoadedArsc>(new LoadedArsc());
841}
842
Adam Lesinski7ad11102016-10-28 16:39:15 -0700843} // namespace android