blob: a0cc407fc9484337d0bd326c69a49841422c76b2 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070017#include "idmap2/Idmap.h"
18
Mårten Kongstad02751232018-04-27 13:16:32 +020019#include <algorithm>
20#include <iostream>
21#include <iterator>
22#include <limits>
23#include <map>
24#include <memory>
25#include <set>
26#include <string>
27#include <utility>
28#include <vector>
29
30#include "android-base/macros.h"
31#include "android-base/stringprintf.h"
32#include "androidfw/AssetManager2.h"
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070033#include "idmap2/ResourceMapping.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020034#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010035#include "idmap2/Result.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010036#include "idmap2/SysTrace.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020037#include "idmap2/ZipFile.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070038#include "utils/String16.h"
39#include "utils/String8.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020040
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010041namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020042
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010043namespace {
44
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070045bool WARN_UNUSED Read8(std::istream& stream, uint8_t* out) {
46 uint8_t value;
47 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint8_t))) {
48 *out = value;
Mårten Kongstad02751232018-04-27 13:16:32 +020049 return true;
50 }
51 return false;
52}
53
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070054bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) {
55 uint16_t value;
56 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint16_t))) {
57 *out = dtohs(value);
Mårten Kongstad02751232018-04-27 13:16:32 +020058 return true;
59 }
60 return false;
61}
62
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070063bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
64 uint32_t value;
65 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
66 *out = dtohl(value);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -070067 return true;
68 }
69 return false;
70}
71
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080072bool WARN_UNUSED ReadString(std::istream& stream, std::string* out) {
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020073 uint32_t size;
74 if (!Read32(stream, &size)) {
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080075 return false;
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020076 }
77 if (size == 0) {
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080078 *out = "";
79 return true;
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020080 }
81 std::string buf(size, '\0');
82 if (!stream.read(buf.data(), size)) {
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080083 return false;
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020084 }
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070085 uint32_t padding_size = CalculatePadding(size);
86 std::string padding(padding_size, '\0');
87 if (!stream.read(padding.data(), padding_size)) {
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080088 return false;
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070089 }
Ryan Mitchell0699f1d2020-12-03 15:41:42 -080090 *out = buf;
91 return true;
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020092}
93
Ryan Mitchell7d53f192020-04-24 17:45:25 -070094} // namespace
95
96Result<uint32_t> GetPackageCrc(const ZipFile& zip) {
Mårten Kongstad9371dc12019-01-22 14:35:12 +010097 const Result<uint32_t> a = zip.Crc("resources.arsc");
98 const Result<uint32_t> b = zip.Crc("AndroidManifest.xml");
Mårten Kongstad49d835d2019-01-31 10:50:48 +010099 return a && b
100 ? Result<uint32_t>(*a ^ *b)
Mårten Kongstadce424902019-03-01 08:35:37 +0100101 : Error("failed to get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc");
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100102}
103
Mårten Kongstad02751232018-04-27 13:16:32 +0200104std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
105 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
Ryan Mitchell9289a042021-01-13 17:21:29 -0800106 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_)) {
107 return nullptr;
108 }
109
110 if (idmap_header->magic_ != kIdmapMagic ||
111 idmap_header->version_ != kIdmapCurrentVersion) {
112 // Do not continue parsing if the file is not a current version idmap.
113 return nullptr;
114 }
115
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700116 uint32_t enforce_overlayable;
Ryan Mitchell9289a042021-01-13 17:21:29 -0800117 if (!Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700118 !Read32(stream, &idmap_header->fulfilled_policies_) ||
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800119 !Read32(stream, &enforce_overlayable) || !ReadString(stream, &idmap_header->target_path_) ||
120 !ReadString(stream, &idmap_header->overlay_path_) ||
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800121 !ReadString(stream, &idmap_header->overlay_name_) ||
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800122 !ReadString(stream, &idmap_header->debug_info_)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200123 return nullptr;
124 }
125
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700126 idmap_header->enforce_overlayable_ = enforce_overlayable != 0U;
Mårten Kongstad02751232018-04-27 13:16:32 +0200127 return std::move(idmap_header);
128}
129
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800130Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
131 const std::string& overlay_path,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800132 const std::string& overlay_name,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700133 PolicyBitmask fulfilled_policies,
134 bool enforce_overlayable) const {
Ryan Mitchella7070132020-05-13 14:17:52 -0700135 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700136 if (!target_zip) {
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800137 return Error("failed to open target %s", target_path.c_str());
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700138 }
139
Ryan Mitchella7070132020-05-13 14:17:52 -0700140 const Result<uint32_t> target_crc = GetPackageCrc(*target_zip);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700141 if (!target_crc) {
142 return Error("failed to get target crc");
143 }
144
Ryan Mitchella7070132020-05-13 14:17:52 -0700145 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path);
146 if (!overlay_zip) {
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800147 return Error("failed to overlay target %s", overlay_path.c_str());
Ryan Mitchella7070132020-05-13 14:17:52 -0700148 }
149
150 const Result<uint32_t> overlay_crc = GetPackageCrc(*overlay_zip);
151 if (!overlay_crc) {
152 return Error("failed to get overlay crc");
153 }
154
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800155 return IsUpToDate(target_path, overlay_path, overlay_name, *target_crc, *overlay_crc,
156 fulfilled_policies, enforce_overlayable);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700157}
158
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800159Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800160 const std::string& overlay_path,
161 const std::string& overlay_name, uint32_t target_crc,
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800162 uint32_t overlay_crc, PolicyBitmask fulfilled_policies,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700163 bool enforce_overlayable) const {
Mårten Kongstad02751232018-04-27 13:16:32 +0200164 if (magic_ != kIdmapMagic) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100165 return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
Mårten Kongstad02751232018-04-27 13:16:32 +0200166 }
167
168 if (version_ != kIdmapCurrentVersion) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100169 return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion);
Mårten Kongstad02751232018-04-27 13:16:32 +0200170 }
171
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700172 if (target_crc_ != target_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100173 return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_,
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700174 target_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200175 }
176
Ryan Mitchella7070132020-05-13 14:17:52 -0700177 if (overlay_crc_ != overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100178 return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_,
Ryan Mitchella7070132020-05-13 14:17:52 -0700179 overlay_crc);
180 }
181
182 if (fulfilled_policies_ != fulfilled_policies) {
183 return Error("bad fulfilled policies: idmap version 0x%08x, file system version 0x%08x",
184 fulfilled_policies, fulfilled_policies_);
185 }
186
187 if (enforce_overlayable != enforce_overlayable_) {
188 return Error("bad enforce overlayable: idmap version %s, file system version %s",
Ryan Mitchell038a2842020-06-08 14:41:07 -0700189 enforce_overlayable ? "true" : "false", enforce_overlayable_ ? "true" : "false");
Ryan Mitchella7070132020-05-13 14:17:52 -0700190 }
191
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800192 if (target_path != target_path_) {
193 return Error("bad target path: idmap version %s, file system version %s", target_path.c_str(),
194 target_path_.c_str());
Ryan Mitchella7070132020-05-13 14:17:52 -0700195 }
196
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800197 if (overlay_path != overlay_path_) {
198 return Error("bad overlay path: idmap version %s, file system version %s", overlay_path.c_str(),
199 overlay_path_.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200200 }
201
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800202 if (overlay_name != overlay_name_) {
203 return Error("bad overlay name: idmap version %s, file system version %s", overlay_name.c_str(),
204 overlay_name_.c_str());
205 }
206
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100207 return Unit{};
Mårten Kongstad02751232018-04-27 13:16:32 +0200208}
209
210std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
211 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
212
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700213 uint8_t padding;
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700214 if (!Read8(stream, &idmap_data_header->target_package_id_) ||
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700215 !Read8(stream, &idmap_data_header->overlay_package_id_) || !Read8(stream, &padding) ||
216 !Read8(stream, &padding) || !Read32(stream, &idmap_data_header->target_entry_count) ||
217 !Read32(stream, &idmap_data_header->target_entry_inline_count) ||
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700218 !Read32(stream, &idmap_data_header->overlay_entry_count) ||
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700219 !Read32(stream, &idmap_data_header->string_pool_index_offset)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200220 return nullptr;
221 }
Mårten Kongstad02751232018-04-27 13:16:32 +0200222
223 return std::move(idmap_data_header);
224}
225
Mårten Kongstad02751232018-04-27 13:16:32 +0200226std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
227 std::unique_ptr<IdmapData> data(new IdmapData());
228 data->header_ = IdmapData::Header::FromBinaryStream(stream);
229 if (!data->header_) {
230 return nullptr;
231 }
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700232
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700233 // Read the mapping of target resource id to overlay resource value.
234 for (size_t i = 0; i < data->header_->GetTargetEntryCount(); i++) {
235 TargetEntry target_entry{};
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700236 if (!Read32(stream, &target_entry.target_id) || !Read32(stream, &target_entry.overlay_id)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200237 return nullptr;
238 }
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700239 data->target_entries_.push_back(target_entry);
240 }
241
242 // Read the mapping of target resource id to inline overlay values.
243 uint8_t unused1;
244 uint16_t unused2;
245 for (size_t i = 0; i < data->header_->GetTargetInlineEntryCount(); i++) {
246 TargetInlineEntry target_entry{};
247 if (!Read32(stream, &target_entry.target_id) || !Read16(stream, &unused2) ||
248 !Read8(stream, &unused1) || !Read8(stream, &target_entry.value.data_type) ||
249 !Read32(stream, &target_entry.value.data_value)) {
250 return nullptr;
251 }
252 data->target_inline_entries_.push_back(target_entry);
Mårten Kongstad02751232018-04-27 13:16:32 +0200253 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700254
255 // Read the mapping of overlay resource id to target resource id.
256 for (size_t i = 0; i < data->header_->GetOverlayEntryCount(); i++) {
257 OverlayEntry overlay_entry{};
258 if (!Read32(stream, &overlay_entry.overlay_id) || !Read32(stream, &overlay_entry.target_id)) {
259 return nullptr;
260 }
261 data->overlay_entries_.emplace_back(overlay_entry);
262 }
263
264 // Read raw string pool bytes.
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800265 if (!ReadString(stream, &data->string_pool_data_)) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700266 return nullptr;
267 }
Mårten Kongstad02751232018-04-27 13:16:32 +0200268 return std::move(data);
269}
270
271std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
272 const std::string& absolute_apk_path) {
273 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
274 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
275 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
276 replace(copy.begin(), copy.end(), '/', '@');
277 return absolute_dir + "/" + copy + "@idmap";
278}
279
Mårten Kongstadce424902019-03-01 08:35:37 +0100280Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100281 SYSTRACE << "Idmap::FromBinaryStream";
Mårten Kongstad02751232018-04-27 13:16:32 +0200282 std::unique_ptr<Idmap> idmap(new Idmap());
283
284 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
285 if (!idmap->header_) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100286 return Error("failed to parse idmap header");
Mårten Kongstad02751232018-04-27 13:16:32 +0200287 }
288
289 // idmap version 0x01 does not specify the number of data blocks that follow
290 // the idmap header; assume exactly one data block
291 for (int i = 0; i < 1; i++) {
292 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
293 if (!data) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100294 return Error("failed to parse data block %d", i);
Mårten Kongstad02751232018-04-27 13:16:32 +0200295 }
296 idmap->data_.push_back(std::move(data));
297 }
298
Mårten Kongstadce424902019-03-01 08:35:37 +0100299 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200300}
301
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700302Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping(
303 const ResourceMapping& resource_mapping) {
304 if (resource_mapping.GetTargetToOverlayMap().empty()) {
305 return Error("no resources were overlaid");
306 }
307
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700308 std::unique_ptr<IdmapData> data(new IdmapData());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700309 data->string_pool_data_ = resource_mapping.GetStringPoolData().to_string();
310 for (const auto& mapping : resource_mapping.GetTargetToOverlayMap()) {
311 if (auto overlay_resource = std::get_if<ResourceId>(&mapping.second)) {
312 data->target_entries_.push_back({mapping.first, *overlay_resource});
313 } else {
314 data->target_inline_entries_.push_back(
315 {mapping.first, std::get<TargetValue>(mapping.second)});
316 }
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800317 }
318
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700319 for (const auto& mapping : resource_mapping.GetOverlayToTargetMap()) {
320 data->overlay_entries_.emplace_back(IdmapData::OverlayEntry{mapping.first, mapping.second});
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700321 }
322
323 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
324 data_header->target_package_id_ = resource_mapping.GetTargetPackageId();
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700325 data_header->overlay_package_id_ = resource_mapping.GetOverlayPackageId();
326 data_header->target_entry_count = static_cast<uint32_t>(data->target_entries_.size());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700327 data_header->target_entry_inline_count =
328 static_cast<uint32_t>(data->target_inline_entries_.size());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700329 data_header->overlay_entry_count = static_cast<uint32_t>(data->overlay_entries_.size());
330 data_header->string_pool_index_offset = resource_mapping.GetStringPoolOffset();
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700331 data->header_ = std::move(data_header);
332 return {std::move(data)};
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800333}
334
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700335Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const ApkAssets& target_apk_assets,
Mårten Kongstadce424902019-03-01 08:35:37 +0100336 const ApkAssets& overlay_apk_assets,
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800337 const std::string& overlay_name,
Mårten Kongstadce424902019-03-01 08:35:37 +0100338 const PolicyBitmask& fulfilled_policies,
339 bool enforce_overlayable) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100340 SYSTRACE << "Idmap::FromApkAssets";
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700341 const std::string& target_apk_path = target_apk_assets.GetPath();
342 const std::string& overlay_apk_path = overlay_apk_assets.GetPath();
Mårten Kongstad02751232018-04-27 13:16:32 +0200343
344 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
345 if (!target_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100346 return Error("failed to open target as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200347 }
348
349 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
350 if (!overlay_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100351 return Error("failed to open overlay as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200352 }
353
354 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
355 header->magic_ = kIdmapMagic;
356 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100357
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700358 Result<uint32_t> crc = GetPackageCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100359 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100360 return Error(crc.GetError(), "failed to get zip CRC for target");
Mårten Kongstad02751232018-04-27 13:16:32 +0200361 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100362 header->target_crc_ = *crc;
363
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700364 crc = GetPackageCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100365 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100366 return Error(crc.GetError(), "failed to get zip CRC for overlay");
Mårten Kongstad02751232018-04-27 13:16:32 +0200367 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100368 header->overlay_crc_ = *crc;
Ryan Mitchella7070132020-05-13 14:17:52 -0700369 header->fulfilled_policies_ = fulfilled_policies;
370 header->enforce_overlayable_ = enforce_overlayable;
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800371 header->target_path_ = target_apk_path;
372 header->overlay_path_ = overlay_apk_path;
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800373 header->overlay_name_ = overlay_name;
Mårten Kongstad02751232018-04-27 13:16:32 +0200374
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800375 auto info = utils::ExtractOverlayManifestInfo(overlay_apk_path, overlay_name);
376 if (!info) {
377 return info.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200378 }
379
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200380 LogInfo log_info;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700381 auto resource_mapping =
Ryan Mitchell30dc2e02020-12-02 11:43:18 -0800382 ResourceMapping::FromApkAssets(target_apk_assets, overlay_apk_assets, *info,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200383 fulfilled_policies, enforce_overlayable, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700384 if (!resource_mapping) {
385 return resource_mapping.GetError();
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800386 }
387
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700388 auto idmap_data = IdmapData::FromResourceMapping(*resource_mapping);
389 if (!idmap_data) {
390 return idmap_data.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200391 }
392
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200393 std::unique_ptr<Idmap> idmap(new Idmap());
394 header->debug_info_ = log_info.GetString();
395 idmap->header_ = std::move(header);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700396 idmap->data_.push_back(std::move(*idmap_data));
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200397
Mårten Kongstadce424902019-03-01 08:35:37 +0100398 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200399}
400
401void IdmapHeader::accept(Visitor* v) const {
402 assert(v != nullptr);
403 v->visit(*this);
404}
405
406void IdmapData::Header::accept(Visitor* v) const {
407 assert(v != nullptr);
408 v->visit(*this);
409}
410
Mårten Kongstad02751232018-04-27 13:16:32 +0200411void IdmapData::accept(Visitor* v) const {
412 assert(v != nullptr);
Mårten Kongstad02751232018-04-27 13:16:32 +0200413 header_->accept(v);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700414 v->visit(*this);
Mårten Kongstad02751232018-04-27 13:16:32 +0200415}
416
417void Idmap::accept(Visitor* v) const {
418 assert(v != nullptr);
Mårten Kongstad02751232018-04-27 13:16:32 +0200419 header_->accept(v);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700420 v->visit(*this);
Mårten Kongstad02751232018-04-27 13:16:32 +0200421 auto end = data_.cend();
422 for (auto iter = data_.cbegin(); iter != end; ++iter) {
423 (*iter)->accept(v);
424 }
425}
426
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100427} // namespace android::idmap2