blob: 4745cc6cdc331b833530799b25b3a4c72eba4430 [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 Mitchellbf1f45b2020-09-29 17:22:52 -0700106 uint32_t enforce_overlayable;
Mårten Kongstad02751232018-04-27 13:16:32 +0200107 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
108 !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700109 !Read32(stream, &idmap_header->fulfilled_policies_) ||
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800110 !Read32(stream, &enforce_overlayable) || !ReadString(stream, &idmap_header->target_path_) ||
111 !ReadString(stream, &idmap_header->overlay_path_) ||
112 !ReadString(stream, &idmap_header->debug_info_)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200113 return nullptr;
114 }
115
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700116 idmap_header->enforce_overlayable_ = enforce_overlayable != 0U;
Mårten Kongstad02751232018-04-27 13:16:32 +0200117 return std::move(idmap_header);
118}
119
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800120Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
121 const std::string& overlay_path,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700122 PolicyBitmask fulfilled_policies,
123 bool enforce_overlayable) const {
Ryan Mitchella7070132020-05-13 14:17:52 -0700124 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700125 if (!target_zip) {
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800126 return Error("failed to open target %s", target_path.c_str());
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700127 }
128
Ryan Mitchella7070132020-05-13 14:17:52 -0700129 const Result<uint32_t> target_crc = GetPackageCrc(*target_zip);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700130 if (!target_crc) {
131 return Error("failed to get target crc");
132 }
133
Ryan Mitchella7070132020-05-13 14:17:52 -0700134 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path);
135 if (!overlay_zip) {
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800136 return Error("failed to overlay target %s", overlay_path.c_str());
Ryan Mitchella7070132020-05-13 14:17:52 -0700137 }
138
139 const Result<uint32_t> overlay_crc = GetPackageCrc(*overlay_zip);
140 if (!overlay_crc) {
141 return Error("failed to get overlay crc");
142 }
143
144 return IsUpToDate(target_path, overlay_path, *target_crc, *overlay_crc, fulfilled_policies,
145 enforce_overlayable);
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700146}
147
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800148Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
149 const std::string& overlay_path, uint32_t target_crc,
150 uint32_t overlay_crc, PolicyBitmask fulfilled_policies,
Ryan Mitchell038a2842020-06-08 14:41:07 -0700151 bool enforce_overlayable) const {
Mårten Kongstad02751232018-04-27 13:16:32 +0200152 if (magic_ != kIdmapMagic) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100153 return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
Mårten Kongstad02751232018-04-27 13:16:32 +0200154 }
155
156 if (version_ != kIdmapCurrentVersion) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100157 return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion);
Mårten Kongstad02751232018-04-27 13:16:32 +0200158 }
159
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700160 if (target_crc_ != target_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100161 return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_,
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700162 target_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200163 }
164
Ryan Mitchella7070132020-05-13 14:17:52 -0700165 if (overlay_crc_ != overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100166 return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_,
Ryan Mitchella7070132020-05-13 14:17:52 -0700167 overlay_crc);
168 }
169
170 if (fulfilled_policies_ != fulfilled_policies) {
171 return Error("bad fulfilled policies: idmap version 0x%08x, file system version 0x%08x",
172 fulfilled_policies, fulfilled_policies_);
173 }
174
175 if (enforce_overlayable != enforce_overlayable_) {
176 return Error("bad enforce overlayable: idmap version %s, file system version %s",
Ryan Mitchell038a2842020-06-08 14:41:07 -0700177 enforce_overlayable ? "true" : "false", enforce_overlayable_ ? "true" : "false");
Ryan Mitchella7070132020-05-13 14:17:52 -0700178 }
179
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800180 if (target_path != target_path_) {
181 return Error("bad target path: idmap version %s, file system version %s", target_path.c_str(),
182 target_path_.c_str());
Ryan Mitchella7070132020-05-13 14:17:52 -0700183 }
184
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800185 if (overlay_path != overlay_path_) {
186 return Error("bad overlay path: idmap version %s, file system version %s", overlay_path.c_str(),
187 overlay_path_.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200188 }
189
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100190 return Unit{};
Mårten Kongstad02751232018-04-27 13:16:32 +0200191}
192
193std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
194 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
195
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700196 uint8_t padding;
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700197 if (!Read8(stream, &idmap_data_header->target_package_id_) ||
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700198 !Read8(stream, &idmap_data_header->overlay_package_id_) || !Read8(stream, &padding) ||
199 !Read8(stream, &padding) || !Read32(stream, &idmap_data_header->target_entry_count) ||
200 !Read32(stream, &idmap_data_header->target_entry_inline_count) ||
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700201 !Read32(stream, &idmap_data_header->overlay_entry_count) ||
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700202 !Read32(stream, &idmap_data_header->string_pool_index_offset)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200203 return nullptr;
204 }
Mårten Kongstad02751232018-04-27 13:16:32 +0200205
206 return std::move(idmap_data_header);
207}
208
Mårten Kongstad02751232018-04-27 13:16:32 +0200209std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
210 std::unique_ptr<IdmapData> data(new IdmapData());
211 data->header_ = IdmapData::Header::FromBinaryStream(stream);
212 if (!data->header_) {
213 return nullptr;
214 }
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700215
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700216 // Read the mapping of target resource id to overlay resource value.
217 for (size_t i = 0; i < data->header_->GetTargetEntryCount(); i++) {
218 TargetEntry target_entry{};
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700219 if (!Read32(stream, &target_entry.target_id) || !Read32(stream, &target_entry.overlay_id)) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200220 return nullptr;
221 }
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700222 data->target_entries_.push_back(target_entry);
223 }
224
225 // Read the mapping of target resource id to inline overlay values.
226 uint8_t unused1;
227 uint16_t unused2;
228 for (size_t i = 0; i < data->header_->GetTargetInlineEntryCount(); i++) {
229 TargetInlineEntry target_entry{};
230 if (!Read32(stream, &target_entry.target_id) || !Read16(stream, &unused2) ||
231 !Read8(stream, &unused1) || !Read8(stream, &target_entry.value.data_type) ||
232 !Read32(stream, &target_entry.value.data_value)) {
233 return nullptr;
234 }
235 data->target_inline_entries_.push_back(target_entry);
Mårten Kongstad02751232018-04-27 13:16:32 +0200236 }
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700237
238 // Read the mapping of overlay resource id to target resource id.
239 for (size_t i = 0; i < data->header_->GetOverlayEntryCount(); i++) {
240 OverlayEntry overlay_entry{};
241 if (!Read32(stream, &overlay_entry.overlay_id) || !Read32(stream, &overlay_entry.target_id)) {
242 return nullptr;
243 }
244 data->overlay_entries_.emplace_back(overlay_entry);
245 }
246
247 // Read raw string pool bytes.
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800248 if (!ReadString(stream, &data->string_pool_data_)) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700249 return nullptr;
250 }
Mårten Kongstad02751232018-04-27 13:16:32 +0200251 return std::move(data);
252}
253
254std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
255 const std::string& absolute_apk_path) {
256 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
257 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
258 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
259 replace(copy.begin(), copy.end(), '/', '@');
260 return absolute_dir + "/" + copy + "@idmap";
261}
262
Mårten Kongstadce424902019-03-01 08:35:37 +0100263Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100264 SYSTRACE << "Idmap::FromBinaryStream";
Mårten Kongstad02751232018-04-27 13:16:32 +0200265 std::unique_ptr<Idmap> idmap(new Idmap());
266
267 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
268 if (!idmap->header_) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100269 return Error("failed to parse idmap header");
Mårten Kongstad02751232018-04-27 13:16:32 +0200270 }
271
272 // idmap version 0x01 does not specify the number of data blocks that follow
273 // the idmap header; assume exactly one data block
274 for (int i = 0; i < 1; i++) {
275 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
276 if (!data) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100277 return Error("failed to parse data block %d", i);
Mårten Kongstad02751232018-04-27 13:16:32 +0200278 }
279 idmap->data_.push_back(std::move(data));
280 }
281
Mårten Kongstadce424902019-03-01 08:35:37 +0100282 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200283}
284
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700285Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping(
286 const ResourceMapping& resource_mapping) {
287 if (resource_mapping.GetTargetToOverlayMap().empty()) {
288 return Error("no resources were overlaid");
289 }
290
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700291 std::unique_ptr<IdmapData> data(new IdmapData());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700292 data->string_pool_data_ = resource_mapping.GetStringPoolData().to_string();
293 for (const auto& mapping : resource_mapping.GetTargetToOverlayMap()) {
294 if (auto overlay_resource = std::get_if<ResourceId>(&mapping.second)) {
295 data->target_entries_.push_back({mapping.first, *overlay_resource});
296 } else {
297 data->target_inline_entries_.push_back(
298 {mapping.first, std::get<TargetValue>(mapping.second)});
299 }
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800300 }
301
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700302 for (const auto& mapping : resource_mapping.GetOverlayToTargetMap()) {
303 data->overlay_entries_.emplace_back(IdmapData::OverlayEntry{mapping.first, mapping.second});
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700304 }
305
306 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
307 data_header->target_package_id_ = resource_mapping.GetTargetPackageId();
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700308 data_header->overlay_package_id_ = resource_mapping.GetOverlayPackageId();
309 data_header->target_entry_count = static_cast<uint32_t>(data->target_entries_.size());
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700310 data_header->target_entry_inline_count =
311 static_cast<uint32_t>(data->target_inline_entries_.size());
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700312 data_header->overlay_entry_count = static_cast<uint32_t>(data->overlay_entries_.size());
313 data_header->string_pool_index_offset = resource_mapping.GetStringPoolOffset();
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700314 data->header_ = std::move(data_header);
315 return {std::move(data)};
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800316}
317
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700318Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const ApkAssets& target_apk_assets,
Mårten Kongstadce424902019-03-01 08:35:37 +0100319 const ApkAssets& overlay_apk_assets,
320 const PolicyBitmask& fulfilled_policies,
321 bool enforce_overlayable) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100322 SYSTRACE << "Idmap::FromApkAssets";
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700323 const std::string& target_apk_path = target_apk_assets.GetPath();
324 const std::string& overlay_apk_path = overlay_apk_assets.GetPath();
Mårten Kongstad02751232018-04-27 13:16:32 +0200325
326 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
327 if (!target_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100328 return Error("failed to open target as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200329 }
330
331 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
332 if (!overlay_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100333 return Error("failed to open overlay as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200334 }
335
336 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
337 header->magic_ = kIdmapMagic;
338 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100339
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700340 Result<uint32_t> crc = GetPackageCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100341 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100342 return Error(crc.GetError(), "failed to get zip CRC for target");
Mårten Kongstad02751232018-04-27 13:16:32 +0200343 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100344 header->target_crc_ = *crc;
345
Ryan Mitchell7d53f192020-04-24 17:45:25 -0700346 crc = GetPackageCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100347 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100348 return Error(crc.GetError(), "failed to get zip CRC for overlay");
Mårten Kongstad02751232018-04-27 13:16:32 +0200349 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100350 header->overlay_crc_ = *crc;
Ryan Mitchella7070132020-05-13 14:17:52 -0700351 header->fulfilled_policies_ = fulfilled_policies;
352 header->enforce_overlayable_ = enforce_overlayable;
Ryan Mitchell0699f1d2020-12-03 15:41:42 -0800353 header->target_path_ = target_apk_path;
354 header->overlay_path_ = overlay_apk_path;
Mårten Kongstad02751232018-04-27 13:16:32 +0200355
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700356 auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
357 if (!overlay_info) {
358 return overlay_info.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200359 }
360
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200361 LogInfo log_info;
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700362 auto resource_mapping =
363 ResourceMapping::FromApkAssets(target_apk_assets, overlay_apk_assets, *overlay_info,
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200364 fulfilled_policies, enforce_overlayable, log_info);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700365 if (!resource_mapping) {
366 return resource_mapping.GetError();
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800367 }
368
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700369 auto idmap_data = IdmapData::FromResourceMapping(*resource_mapping);
370 if (!idmap_data) {
371 return idmap_data.GetError();
Mårten Kongstad02751232018-04-27 13:16:32 +0200372 }
373
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200374 std::unique_ptr<Idmap> idmap(new Idmap());
375 header->debug_info_ = log_info.GetString();
376 idmap->header_ = std::move(header);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -0700377 idmap->data_.push_back(std::move(*idmap_data));
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200378
Mårten Kongstadce424902019-03-01 08:35:37 +0100379 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200380}
381
382void IdmapHeader::accept(Visitor* v) const {
383 assert(v != nullptr);
384 v->visit(*this);
385}
386
387void IdmapData::Header::accept(Visitor* v) const {
388 assert(v != nullptr);
389 v->visit(*this);
390}
391
Mårten Kongstad02751232018-04-27 13:16:32 +0200392void IdmapData::accept(Visitor* v) const {
393 assert(v != nullptr);
Mårten Kongstad02751232018-04-27 13:16:32 +0200394 header_->accept(v);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700395 v->visit(*this);
Mårten Kongstad02751232018-04-27 13:16:32 +0200396}
397
398void Idmap::accept(Visitor* v) const {
399 assert(v != nullptr);
Mårten Kongstad02751232018-04-27 13:16:32 +0200400 header_->accept(v);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700401 v->visit(*this);
Mårten Kongstad02751232018-04-27 13:16:32 +0200402 auto end = data_.cend();
403 for (auto iter = data_.cbegin(); iter != end; ++iter) {
404 (*iter)->accept(v);
405 }
406}
407
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100408} // namespace android::idmap2