blob: 1129413584b2a1c3f928e56dc0a31d39b53aabb3 [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
Mårten Kongstad02751232018-04-27 13:16:32 +020072// a string is encoded as a kIdmapStringLength char array; the array is always null-terminated
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020073bool WARN_UNUSED ReadString256(std::istream& stream, char out[kIdmapStringLength]) {
Mårten Kongstad02751232018-04-27 13:16:32 +020074 char buf[kIdmapStringLength];
75 memset(buf, 0, sizeof(buf));
76 if (!stream.read(buf, sizeof(buf))) {
77 return false;
78 }
79 if (buf[sizeof(buf) - 1] != '\0') {
80 return false;
81 }
82 memcpy(out, buf, sizeof(buf));
83 return true;
84}
85
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020086Result<std::string> ReadString(std::istream& stream) {
87 uint32_t size;
88 if (!Read32(stream, &size)) {
89 return Error("failed to read string size");
90 }
91 if (size == 0) {
92 return std::string("");
93 }
94 std::string buf(size, '\0');
95 if (!stream.read(buf.data(), size)) {
96 return Error("failed to read string of size %u", size);
97 }
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -070098 uint32_t padding_size = CalculatePadding(size);
99 std::string padding(padding_size, '\0');
100 if (!stream.read(padding.data(), padding_size)) {
101 return Error("failed to read string padding of size %u", padding_size);
102 }
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200103 return buf;
104}
105
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700106} // namespace
107
108Result<uint32_t> GetPackageCrc(const ZipFile& zip) {
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100109 const Result<uint32_t> a = zip.Crc("resources.arsc");
110 const Result<uint32_t> b = zip.Crc("AndroidManifest.xml");
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100111 return a && b
112 ? Result<uint32_t>(*a ^ *b)
Mårten Kongstadce424902019-03-01 08:35:37 +0100113 : Error("failed to get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc");
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100114}
115
Mårten Kongstad02751232018-04-27 13:16:32 +0200116std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
117 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700118 uint32_t enforce_overlayable;
Mårten Kongstad02751232018-04-27 13:16:32 +0200119 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
120 !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700121 !Read32(stream, &idmap_header->fulfilled_policies_) ||
122 !Read32(stream, &enforce_overlayable) || !ReadString256(stream, idmap_header->target_path_) ||
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200123 !ReadString256(stream, idmap_header->overlay_path_)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200124 return nullptr;
125 }
126
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700127 idmap_header->enforce_overlayable_ = enforce_overlayable != 0U;
Ryan Mitchella7070132020-05-13 14:17:52 -0700128
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200129 auto debug_str = ReadString(stream);
130 if (!debug_str) {
131 return nullptr;
132 }
133 idmap_header->debug_info_ = std::move(*debug_str);
134
Mårten Kongstad02751232018-04-27 13:16:32 +0200135 return std::move(idmap_header);
136}
137
Ryan Mitchella7070132020-05-13 14:17:52 -0700138Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overlay_path,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700139 PolicyBitmask fulfilled_policies,
140 bool enforce_overlayable) const {
Ryan Mitchella7070132020-05-13 14:17:52 -0700141 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700142 if (!target_zip) {
Ryan Mitchella7070132020-05-13 14:17:52 -0700143 return Error("failed to open target %s", target_path);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700144 }
145
Ryan Mitchella7070132020-05-13 14:17:52 -0700146 const Result<uint32_t> target_crc = GetPackageCrc(*target_zip);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700147 if (!target_crc) {
148 return Error("failed to get target crc");
149 }
150
Ryan Mitchella7070132020-05-13 14:17:52 -0700151 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path);
152 if (!overlay_zip) {
153 return Error("failed to overlay target %s", overlay_path);
154 }
155
156 const Result<uint32_t> overlay_crc = GetPackageCrc(*overlay_zip);
157 if (!overlay_crc) {
158 return Error("failed to get overlay crc");
159 }
160
161 return IsUpToDate(target_path, overlay_path, *target_crc, *overlay_crc, fulfilled_policies,
162 enforce_overlayable);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700163}
164
Ryan Mitchella7070132020-05-13 14:17:52 -0700165Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overlay_path,
166 uint32_t target_crc, uint32_t overlay_crc,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700167 PolicyBitmask fulfilled_policies,
168 bool enforce_overlayable) const {
Mårten Kongstad02751232018-04-27 13:16:32 +0200169 if (magic_ != kIdmapMagic) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100170 return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
Mårten Kongstad02751232018-04-27 13:16:32 +0200171 }
172
173 if (version_ != kIdmapCurrentVersion) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100174 return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion);
Mårten Kongstad02751232018-04-27 13:16:32 +0200175 }
176
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700177 if (target_crc_ != target_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100178 return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_,
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700179 target_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200180 }
181
Ryan Mitchella7070132020-05-13 14:17:52 -0700182 if (overlay_crc_ != overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100183 return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_,
Ryan Mitchella7070132020-05-13 14:17:52 -0700184 overlay_crc);
185 }
186
187 if (fulfilled_policies_ != fulfilled_policies) {
188 return Error("bad fulfilled policies: idmap version 0x%08x, file system version 0x%08x",
189 fulfilled_policies, fulfilled_policies_);
190 }
191
192 if (enforce_overlayable != enforce_overlayable_) {
193 return Error("bad enforce overlayable: idmap version %s, file system version %s",
Ryan Mitchell038a2842020-06-08 14:41:07 -0700194 enforce_overlayable ? "true" : "false", enforce_overlayable_ ? "true" : "false");
Ryan Mitchella7070132020-05-13 14:17:52 -0700195 }
196
197 if (strcmp(target_path, target_path_) != 0) {
198 return Error("bad target path: idmap version %s, file system version %s", target_path,
199 target_path_);
200 }
201
202 if (strcmp(overlay_path, overlay_path_) != 0) {
203 return Error("bad overlay path: idmap version %s, file system version %s", overlay_path,
204 overlay_path_);
Mårten Kongstad02751232018-04-27 13:16:32 +0200205 }
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 Mitchellbf1f45b2020-09-29 17:22:52 -0700265 auto string_pool_data = ReadString(stream);
266 if (!string_pool_data) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700267 return nullptr;
268 }
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700269 data->string_pool_data_ = std::move(*string_pool_data);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700270
Mårten Kongstad02751232018-04-27 13:16:32 +0200271 return std::move(data);
272}
273
274std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
275 const std::string& absolute_apk_path) {
276 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
277 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
278 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
279 replace(copy.begin(), copy.end(), '/', '@');
280 return absolute_dir + "/" + copy + "@idmap";
281}
282
Mårten Kongstadce424902019-03-01 08:35:37 +0100283Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100284 SYSTRACE << "Idmap::FromBinaryStream";
Mårten Kongstad02751232018-04-27 13:16:32 +0200285 std::unique_ptr<Idmap> idmap(new Idmap());
286
287 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
288 if (!idmap->header_) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100289 return Error("failed to parse idmap header");
Mårten Kongstad02751232018-04-27 13:16:32 +0200290 }
291
292 // idmap version 0x01 does not specify the number of data blocks that follow
293 // the idmap header; assume exactly one data block
294 for (int i = 0; i < 1; i++) {
295 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
296 if (!data) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100297 return Error("failed to parse data block %d", i);
Mårten Kongstad02751232018-04-27 13:16:32 +0200298 }
299 idmap->data_.push_back(std::move(data));
300 }
301
Mårten Kongstadce424902019-03-01 08:35:37 +0100302 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200303}
304
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700305Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping(
306 const ResourceMapping& resource_mapping) {
307 if (resource_mapping.GetTargetToOverlayMap().empty()) {
308 return Error("no resources were overlaid");
309 }
310
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700311 std::unique_ptr<IdmapData> data(new IdmapData());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700312 data->string_pool_data_ = resource_mapping.GetStringPoolData().to_string();
313 for (const auto& mapping : resource_mapping.GetTargetToOverlayMap()) {
314 if (auto overlay_resource = std::get_if<ResourceId>(&mapping.second)) {
315 data->target_entries_.push_back({mapping.first, *overlay_resource});
316 } else {
317 data->target_inline_entries_.push_back(
318 {mapping.first, std::get<TargetValue>(mapping.second)});
319 }
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800320 }
321
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700322 for (const auto& mapping : resource_mapping.GetOverlayToTargetMap()) {
323 data->overlay_entries_.emplace_back(IdmapData::OverlayEntry{mapping.first, mapping.second});
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700324 }
325
326 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
327 data_header->target_package_id_ = resource_mapping.GetTargetPackageId();
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700328 data_header->overlay_package_id_ = resource_mapping.GetOverlayPackageId();
329 data_header->target_entry_count = static_cast<uint32_t>(data->target_entries_.size());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700330 data_header->target_entry_inline_count =
331 static_cast<uint32_t>(data->target_inline_entries_.size());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700332 data_header->overlay_entry_count = static_cast<uint32_t>(data->overlay_entries_.size());
333 data_header->string_pool_index_offset = resource_mapping.GetStringPoolOffset();
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700334 data->header_ = std::move(data_header);
335 return {std::move(data)};
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800336}
337
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700338Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const ApkAssets& target_apk_assets,
Mårten Kongstadce424902019-03-01 08:35:37 +0100339 const ApkAssets& overlay_apk_assets,
340 const PolicyBitmask& fulfilled_policies,
341 bool enforce_overlayable) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100342 SYSTRACE << "Idmap::FromApkAssets";
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700343 const std::string& target_apk_path = target_apk_assets.GetPath();
344 const std::string& overlay_apk_path = overlay_apk_assets.GetPath();
Mårten Kongstad02751232018-04-27 13:16:32 +0200345
346 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
347 if (!target_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100348 return Error("failed to open target as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200349 }
350
351 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
352 if (!overlay_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100353 return Error("failed to open overlay as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200354 }
355
356 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
357 header->magic_ = kIdmapMagic;
358 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100359
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700360 Result<uint32_t> crc = GetPackageCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100361 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100362 return Error(crc.GetError(), "failed to get zip CRC for target");
Mårten Kongstad02751232018-04-27 13:16:32 +0200363 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100364 header->target_crc_ = *crc;
365
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700366 crc = GetPackageCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100367 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100368 return Error(crc.GetError(), "failed to get zip CRC for overlay");
Mårten Kongstad02751232018-04-27 13:16:32 +0200369 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100370 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200371
Ryan Mitchella7070132020-05-13 14:17:52 -0700372 header->fulfilled_policies_ = fulfilled_policies;
373 header->enforce_overlayable_ = enforce_overlayable;
374
Mårten Kongstad02751232018-04-27 13:16:32 +0200375 if (target_apk_path.size() > sizeof(header->target_path_)) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100376 return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
377 sizeof(header->target_path_));
Mårten Kongstad02751232018-04-27 13:16:32 +0200378 }
379 memset(header->target_path_, 0, sizeof(header->target_path_));
380 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
381
382 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700383 return Error("overlay apk path \"%s\" longer than maximum size %zu", overlay_apk_path.c_str(),
Mårten Kongstadce424902019-03-01 08:35:37 +0100384 sizeof(header->target_path_));
Mårten Kongstad02751232018-04-27 13:16:32 +0200385 }
386 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
387 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
388
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700389 auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
390 if (!overlay_info) {
391 return overlay_info.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200392 }
393
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200394 LogInfo log_info;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700395 auto resource_mapping =
396 ResourceMapping::FromApkAssets(target_apk_assets, overlay_apk_assets, *overlay_info,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200397 fulfilled_policies, enforce_overlayable, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700398 if (!resource_mapping) {
399 return resource_mapping.GetError();
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800400 }
401
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700402 auto idmap_data = IdmapData::FromResourceMapping(*resource_mapping);
403 if (!idmap_data) {
404 return idmap_data.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200405 }
406
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200407 std::unique_ptr<Idmap> idmap(new Idmap());
408 header->debug_info_ = log_info.GetString();
409 idmap->header_ = std::move(header);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700410 idmap->data_.push_back(std::move(*idmap_data));
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200411
Mårten Kongstadce424902019-03-01 08:35:37 +0100412 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200413}
414
415void IdmapHeader::accept(Visitor* v) const {
416 assert(v != nullptr);
417 v->visit(*this);
418}
419
420void IdmapData::Header::accept(Visitor* v) const {
421 assert(v != nullptr);
422 v->visit(*this);
423}
424
Mårten Kongstad02751232018-04-27 13:16:32 +0200425void IdmapData::accept(Visitor* v) const {
426 assert(v != nullptr);
Mårten Kongstad02751232018-04-27 13:16:32 +0200427 header_->accept(v);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700428 v->visit(*this);
Mårten Kongstad02751232018-04-27 13:16:32 +0200429}
430
431void Idmap::accept(Visitor* v) const {
432 assert(v != nullptr);
Mårten Kongstad02751232018-04-27 13:16:32 +0200433 header_->accept(v);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700434 v->visit(*this);
Mårten Kongstad02751232018-04-27 13:16:32 +0200435 auto end = data_.cend();
436 for (auto iter = data_.cbegin(); iter != end; ++iter) {
437 (*iter)->accept(v);
438 }
439}
440
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100441} // namespace android::idmap2