blob: a7d180c90307c383f850a94d4937837a282706f8 [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
17#include <algorithm>
18#include <iostream>
19#include <iterator>
20#include <limits>
21#include <map>
22#include <memory>
23#include <set>
24#include <string>
25#include <utility>
26#include <vector>
27
28#include "android-base/macros.h"
29#include "android-base/stringprintf.h"
30#include "androidfw/AssetManager2.h"
31#include "utils/String16.h"
32#include "utils/String8.h"
33
34#include "idmap2/Idmap.h"
35#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010036#include "idmap2/Result.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010037#include "idmap2/SysTrace.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020038#include "idmap2/ZipFile.h"
39
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010040namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020041
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010042namespace {
43
Mårten Kongstad02751232018-04-27 13:16:32 +020044#define EXTRACT_TYPE(resid) ((0x00ff0000 & (resid)) >> 16)
45
46#define EXTRACT_ENTRY(resid) (0x0000ffff & (resid))
47
Mårten Kongstadcf281362018-11-28 19:32:25 +010048class MatchingResources {
49 public:
Mårten Kongstad02751232018-04-27 13:16:32 +020050 void Add(ResourceId target_resid, ResourceId overlay_resid) {
51 TypeId target_typeid = EXTRACT_TYPE(target_resid);
Mårten Kongstadcf281362018-11-28 19:32:25 +010052 if (map_.find(target_typeid) == map_.end()) {
53 map_.emplace(target_typeid, std::set<std::pair<ResourceId, ResourceId>>());
Mårten Kongstad02751232018-04-27 13:16:32 +020054 }
Mårten Kongstadcf281362018-11-28 19:32:25 +010055 map_[target_typeid].insert(std::make_pair(target_resid, overlay_resid));
Mårten Kongstad02751232018-04-27 13:16:32 +020056 }
57
Yi Kong974d5162019-03-06 16:18:11 -080058 inline const std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>>& WARN_UNUSED
59 Map() const {
Mårten Kongstadcf281362018-11-28 19:32:25 +010060 return map_;
61 }
62
63 private:
Mårten Kongstad02751232018-04-27 13:16:32 +020064 // target type id -> set { pair { overlay entry id, overlay entry id } }
Mårten Kongstadcf281362018-11-28 19:32:25 +010065 std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>> map_;
Mårten Kongstad02751232018-04-27 13:16:32 +020066};
67
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010068bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) {
Mårten Kongstad02751232018-04-27 13:16:32 +020069 uint16_t value;
70 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint16_t))) {
71 *out = dtohl(value);
72 return true;
73 }
74 return false;
75}
76
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010077bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
Mårten Kongstad02751232018-04-27 13:16:32 +020078 uint32_t value;
79 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
80 *out = dtohl(value);
81 return true;
82 }
83 return false;
84}
85
86// a string is encoded as a kIdmapStringLength char array; the array is always null-terminated
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010087bool WARN_UNUSED ReadString(std::istream& stream, char out[kIdmapStringLength]) {
Mårten Kongstad02751232018-04-27 13:16:32 +020088 char buf[kIdmapStringLength];
89 memset(buf, 0, sizeof(buf));
90 if (!stream.read(buf, sizeof(buf))) {
91 return false;
92 }
93 if (buf[sizeof(buf) - 1] != '\0') {
94 return false;
95 }
96 memcpy(out, buf, sizeof(buf));
97 return true;
98}
99
Mårten Kongstad744ccfe2018-12-20 14:56:14 +0100100ResourceId NameToResid(const AssetManager2& am, const std::string& name) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200101 return am.GetResourceId(name);
102}
103
104// TODO(martenkongstad): scan for package name instead of assuming package at index 0
105//
106// idmap version 0x01 naively assumes that the package to use is always the first ResTable_package
107// in the resources.arsc blob. In most cases, there is only a single ResTable_package anyway, so
108// this assumption tends to work out. That said, the correct thing to do is to scan
109// resources.arsc for a package with a given name as read from the package manifest instead of
110// relying on a hard-coded index. This however requires storing the package name in the idmap
111// header, which in turn requires incrementing the idmap version. Because the initial version of
112// idmap2 is compatible with idmap, this will have to wait for now.
Mårten Kongstad744ccfe2018-12-20 14:56:14 +0100113const LoadedPackage* GetPackageAtIndex0(const LoadedArsc& loaded_arsc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200114 const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc.GetPackages();
115 if (packages.empty()) {
116 return nullptr;
117 }
118 int id = packages[0]->GetPackageId();
119 return loaded_arsc.GetPackageById(id);
120}
121
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100122Result<uint32_t> GetCrc(const ZipFile& zip) {
123 const Result<uint32_t> a = zip.Crc("resources.arsc");
124 const Result<uint32_t> b = zip.Crc("AndroidManifest.xml");
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100125 return a && b
126 ? Result<uint32_t>(*a ^ *b)
127 : Error("Couldn't get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc");
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100128}
129
Mårten Kongstad744ccfe2018-12-20 14:56:14 +0100130} // namespace
131
Mårten Kongstad02751232018-04-27 13:16:32 +0200132std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
133 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
134
135 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
136 !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
137 !ReadString(stream, idmap_header->target_path_) ||
138 !ReadString(stream, idmap_header->overlay_path_)) {
139 return nullptr;
140 }
141
142 return std::move(idmap_header);
143}
144
145bool IdmapHeader::IsUpToDate(std::ostream& out_error) const {
146 if (magic_ != kIdmapMagic) {
147 out_error << base::StringPrintf("error: bad magic: actual 0x%08x, expected 0x%08x", magic_,
148 kIdmapMagic)
149 << std::endl;
150 return false;
151 }
152
153 if (version_ != kIdmapCurrentVersion) {
154 out_error << base::StringPrintf("error: bad version: actual 0x%08x, expected 0x%08x", version_,
155 kIdmapCurrentVersion)
156 << std::endl;
157 return false;
158 }
159
160 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path_);
161 if (!target_zip) {
162 out_error << "error: failed to open target " << target_path_ << std::endl;
163 return false;
164 }
165
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100166 Result<uint32_t> target_crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100167 if (!target_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200168 out_error << "error: failed to get target crc" << std::endl;
169 return false;
170 }
171
Mårten Kongstad0f763112018-11-19 14:14:37 +0100172 if (target_crc_ != *target_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200173 out_error << base::StringPrintf(
174 "error: bad target crc: idmap version 0x%08x, file system version 0x%08x",
Mårten Kongstad0f763112018-11-19 14:14:37 +0100175 target_crc_, *target_crc)
Mårten Kongstad02751232018-04-27 13:16:32 +0200176 << std::endl;
177 return false;
178 }
179
180 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path_);
181 if (!overlay_zip) {
182 out_error << "error: failed to open overlay " << overlay_path_ << std::endl;
183 return false;
184 }
185
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100186 Result<uint32_t> overlay_crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100187 if (!overlay_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200188 out_error << "error: failed to get overlay crc" << std::endl;
189 return false;
190 }
191
Mårten Kongstad0f763112018-11-19 14:14:37 +0100192 if (overlay_crc_ != *overlay_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200193 out_error << base::StringPrintf(
194 "error: bad overlay crc: idmap version 0x%08x, file system version 0x%08x",
Mårten Kongstad0f763112018-11-19 14:14:37 +0100195 overlay_crc_, *overlay_crc)
Mårten Kongstad02751232018-04-27 13:16:32 +0200196 << std::endl;
197 return false;
198 }
199
200 return true;
201}
202
203std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
204 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
205
206 uint16_t target_package_id16;
207 if (!Read16(stream, &target_package_id16) || !Read16(stream, &idmap_data_header->type_count_)) {
208 return nullptr;
209 }
210 idmap_data_header->target_package_id_ = target_package_id16;
211
212 return std::move(idmap_data_header);
213}
214
215std::unique_ptr<const IdmapData::TypeEntry> IdmapData::TypeEntry::FromBinaryStream(
216 std::istream& stream) {
217 std::unique_ptr<IdmapData::TypeEntry> data(new IdmapData::TypeEntry());
Mårten Kongstadb8779022018-11-29 09:53:17 +0100218 uint16_t target_type16;
219 uint16_t overlay_type16;
220 uint16_t entry_count;
Mårten Kongstad02751232018-04-27 13:16:32 +0200221 if (!Read16(stream, &target_type16) || !Read16(stream, &overlay_type16) ||
222 !Read16(stream, &entry_count) || !Read16(stream, &data->entry_offset_)) {
223 return nullptr;
224 }
225 data->target_type_id_ = target_type16;
226 data->overlay_type_id_ = overlay_type16;
227 for (uint16_t i = 0; i < entry_count; i++) {
228 ResourceId resid;
229 if (!Read32(stream, &resid)) {
230 return nullptr;
231 }
232 data->entries_.push_back(resid);
233 }
234
235 return std::move(data);
236}
237
238std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
239 std::unique_ptr<IdmapData> data(new IdmapData());
240 data->header_ = IdmapData::Header::FromBinaryStream(stream);
241 if (!data->header_) {
242 return nullptr;
243 }
244 for (size_t type_count = 0; type_count < data->header_->GetTypeCount(); type_count++) {
245 std::unique_ptr<const TypeEntry> type = IdmapData::TypeEntry::FromBinaryStream(stream);
246 if (!type) {
247 return nullptr;
248 }
249 data->type_entries_.push_back(std::move(type));
250 }
251 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
263std::unique_ptr<const Idmap> Idmap::FromBinaryStream(std::istream& stream,
264 std::ostream& out_error) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100265 SYSTRACE << "Idmap::FromBinaryStream";
Mårten Kongstad02751232018-04-27 13:16:32 +0200266 std::unique_ptr<Idmap> idmap(new Idmap());
267
268 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
269 if (!idmap->header_) {
270 out_error << "error: failed to parse idmap header" << std::endl;
271 return nullptr;
272 }
273
274 // idmap version 0x01 does not specify the number of data blocks that follow
275 // the idmap header; assume exactly one data block
276 for (int i = 0; i < 1; i++) {
277 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
278 if (!data) {
279 out_error << "error: failed to parse data block " << i << std::endl;
280 return nullptr;
281 }
282 idmap->data_.push_back(std::move(data));
283 }
284
285 return std::move(idmap);
286}
287
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800288std::string ConcatPolicies(const std::vector<std::string>& policies) {
289 std::string message;
290 for (const std::string& policy : policies) {
291 if (message.empty()) {
292 message.append(policy);
293 } else {
294 message.append(policy);
295 message.append("|");
296 }
297 }
298
299 return message;
300}
301
302Result<Unit> CheckOverlayable(const LoadedPackage& target_package,
303 const utils::OverlayManifestInfo& overlay_info,
304 const PolicyBitmask& fulfilled_policies, const ResourceId& resid) {
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800305 static constexpr const PolicyBitmask sDefaultPolicies =
306 PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION |
307 PolicyFlags::POLICY_PRODUCT_PARTITION | PolicyFlags::POLICY_SIGNATURE;
308
309 // If the resource does not have an overlayable definition, allow the resource to be overlaid if
310 // the overlay is preinstalled or signed with the same signature as the target.
311 if (!target_package.DefinesOverlayable()) {
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800312 return (sDefaultPolicies & fulfilled_policies) != 0
313 ? Result<Unit>({})
314 : Error(
315 "overlay must be preinstalled or signed with the same signature as the "
316 "target");
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800317 }
318
Ryan Mitchella3628462019-01-14 12:19:40 -0800319 const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(resid);
320 if (overlayable_info == nullptr) {
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800321 // Do not allow non-overlayable resources to be overlaid.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800322 return Error("resource has no overlayable declaration");
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800323 }
324
Ryan Mitchell19823452019-01-29 12:01:24 -0800325 if (overlay_info.target_name != overlayable_info->name) {
Ryan Mitchella3628462019-01-14 12:19:40 -0800326 // If the overlay supplies a target overlayable name, the resource must belong to the
327 // overlayable defined with the specified name to be overlaid.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800328 return Error("<overlay> android:targetName '%s' does not match overlayable name '%s'",
329 overlay_info.target_name.c_str(), overlayable_info->name.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -0800330 }
331
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800332 // Enforce policy restrictions if the resource is declared as overlayable.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800333 if ((overlayable_info->policy_flags & fulfilled_policies) == 0) {
334 return Error("overlay with policies '%s' does not fulfill any overlayable policies '%s'",
335 ConcatPolicies(BitmaskToPolicies(fulfilled_policies)).c_str(),
336 ConcatPolicies(BitmaskToPolicies(overlayable_info->policy_flags)).c_str());
337 }
338
339 return Result<Unit>({});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800340}
341
342std::unique_ptr<const Idmap> Idmap::FromApkAssets(
343 const std::string& target_apk_path, const ApkAssets& target_apk_assets,
344 const std::string& overlay_apk_path, const ApkAssets& overlay_apk_assets,
345 const PolicyBitmask& fulfilled_policies, bool enforce_overlayable, std::ostream& out_error) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100346 SYSTRACE << "Idmap::FromApkAssets";
Mårten Kongstad02751232018-04-27 13:16:32 +0200347 AssetManager2 target_asset_manager;
348 if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true, false)) {
349 out_error << "error: failed to create target asset manager" << std::endl;
350 return nullptr;
351 }
352
353 AssetManager2 overlay_asset_manager;
354 if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true, false)) {
355 out_error << "error: failed to create overlay asset manager" << std::endl;
356 return nullptr;
357 }
358
359 const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100360 if (target_arsc == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200361 out_error << "error: failed to load target resources.arsc" << std::endl;
362 return nullptr;
363 }
364
365 const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100366 if (overlay_arsc == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200367 out_error << "error: failed to load overlay resources.arsc" << std::endl;
368 return nullptr;
369 }
370
371 const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100372 if (target_pkg == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200373 out_error << "error: failed to load target package from resources.arsc" << std::endl;
374 return nullptr;
375 }
376
377 const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100378 if (overlay_pkg == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200379 out_error << "error: failed to load overlay package from resources.arsc" << std::endl;
380 return nullptr;
381 }
382
383 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
384 if (!target_zip) {
385 out_error << "error: failed to open target as zip" << std::endl;
386 return nullptr;
387 }
388
389 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
390 if (!overlay_zip) {
391 out_error << "error: failed to open overlay as zip" << std::endl;
392 return nullptr;
393 }
394
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100395 auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
Ryan Mitchella3628462019-01-14 12:19:40 -0800396 if (!overlay_info) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100397 out_error << "error: " << overlay_info.GetErrorMessage() << std::endl;
Ryan Mitchella3628462019-01-14 12:19:40 -0800398 return nullptr;
399 }
400
Mårten Kongstad02751232018-04-27 13:16:32 +0200401 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
402 header->magic_ = kIdmapMagic;
403 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100404
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100405 Result<uint32_t> crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100406 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200407 out_error << "error: failed to get zip crc for target" << std::endl;
408 return nullptr;
409 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100410 header->target_crc_ = *crc;
411
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100412 crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100413 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200414 out_error << "error: failed to get zip crc for overlay" << std::endl;
415 return nullptr;
416 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100417 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200418
419 if (target_apk_path.size() > sizeof(header->target_path_)) {
420 out_error << "error: target apk path \"" << target_apk_path << "\" longer that maximum size "
421 << sizeof(header->target_path_) << std::endl;
422 return nullptr;
423 }
424 memset(header->target_path_, 0, sizeof(header->target_path_));
425 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
426
427 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
428 out_error << "error: overlay apk path \"" << overlay_apk_path << "\" longer that maximum size "
429 << sizeof(header->overlay_path_) << std::endl;
430 return nullptr;
431 }
432 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
433 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
434
435 std::unique_ptr<Idmap> idmap(new Idmap());
436 idmap->header_ = std::move(header);
437
438 // find the resources that exist in both packages
439 MatchingResources matching_resources;
440 const auto end = overlay_pkg->end();
441 for (auto iter = overlay_pkg->begin(); iter != end; ++iter) {
442 const ResourceId overlay_resid = *iter;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100443 Result<std::string> name = utils::ResToTypeEntryName(overlay_asset_manager, overlay_resid);
444 if (!name) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200445 continue;
446 }
447 // prepend "<package>:" to turn name into "<package>:<type>/<name>"
Mårten Kongstad0f763112018-11-19 14:14:37 +0100448 const std::string full_name =
449 base::StringPrintf("%s:%s", target_pkg->GetPackageName().c_str(), name->c_str());
450 const ResourceId target_resid = NameToResid(target_asset_manager, full_name);
Mårten Kongstad02751232018-04-27 13:16:32 +0200451 if (target_resid == 0) {
452 continue;
453 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800454
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800455 if (!enforce_overlayable) {
456 Result<Unit> success =
457 CheckOverlayable(*target_pkg, *overlay_info, fulfilled_policies, target_resid);
458 if (!success) {
459 LOG(WARNING) << "overlay \"" << overlay_apk_path
460 << "\" is not allowed to overlay resource \"" << full_name
461 << "\": " << success.GetErrorMessage();
462 continue;
463 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800464 }
465
Mårten Kongstad02751232018-04-27 13:16:32 +0200466 matching_resources.Add(target_resid, overlay_resid);
467 }
468
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800469 if (matching_resources.Map().empty()) {
470 out_error << "overlay \"" << overlay_apk_path << "\" does not successfully overlay any resource"
471 << std::endl;
472 return nullptr;
473 }
474
Mårten Kongstad02751232018-04-27 13:16:32 +0200475 // encode idmap data
476 std::unique_ptr<IdmapData> data(new IdmapData());
Mårten Kongstadcf281362018-11-28 19:32:25 +0100477 const auto types_end = matching_resources.Map().cend();
478 for (auto ti = matching_resources.Map().cbegin(); ti != types_end; ++ti) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200479 auto ei = ti->second.cbegin();
480 std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry());
481 type->target_type_id_ = EXTRACT_TYPE(ei->first);
482 type->overlay_type_id_ = EXTRACT_TYPE(ei->second);
483 type->entry_offset_ = EXTRACT_ENTRY(ei->first);
484 EntryId last_target_entry = kNoEntry;
485 for (; ei != ti->second.cend(); ++ei) {
486 if (last_target_entry != kNoEntry) {
487 int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1;
488 type->entries_.insert(type->entries_.end(), count, kNoEntry);
489 }
490 type->entries_.push_back(EXTRACT_ENTRY(ei->second));
491 last_target_entry = EXTRACT_ENTRY(ei->first);
492 }
493 data->type_entries_.push_back(std::move(type));
494 }
495
496 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
497 data_header->target_package_id_ = target_pkg->GetPackageId();
498 data_header->type_count_ = data->type_entries_.size();
499 data->header_ = std::move(data_header);
500
501 idmap->data_.push_back(std::move(data));
502
503 return std::move(idmap);
504}
505
506void IdmapHeader::accept(Visitor* v) const {
507 assert(v != nullptr);
508 v->visit(*this);
509}
510
511void IdmapData::Header::accept(Visitor* v) const {
512 assert(v != nullptr);
513 v->visit(*this);
514}
515
516void IdmapData::TypeEntry::accept(Visitor* v) const {
517 assert(v != nullptr);
518 v->visit(*this);
519}
520
521void IdmapData::accept(Visitor* v) const {
522 assert(v != nullptr);
523 v->visit(*this);
524 header_->accept(v);
525 auto end = type_entries_.cend();
526 for (auto iter = type_entries_.cbegin(); iter != end; ++iter) {
527 (*iter)->accept(v);
528 }
529}
530
531void Idmap::accept(Visitor* v) const {
532 assert(v != nullptr);
533 v->visit(*this);
534 header_->accept(v);
535 auto end = data_.cend();
536 for (auto iter = data_.cbegin(); iter != end; ++iter) {
537 (*iter)->accept(v);
538 }
539}
540
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100541} // namespace android::idmap2