blob: 9afdd437491fd1c12f51ccc25f7055c771807714 [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
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100145Result<Unit> IdmapHeader::IsUpToDate() const {
Mårten Kongstad02751232018-04-27 13:16:32 +0200146 if (magic_ != kIdmapMagic) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100147 return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
Mårten Kongstad02751232018-04-27 13:16:32 +0200148 }
149
150 if (version_ != kIdmapCurrentVersion) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100151 return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion);
Mårten Kongstad02751232018-04-27 13:16:32 +0200152 }
153
154 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path_);
155 if (!target_zip) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100156 return Error("failed to open target %s", GetTargetPath().to_string().c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200157 }
158
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100159 Result<uint32_t> target_crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100160 if (!target_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100161 return Error("failed to get target crc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200162 }
163
Mårten Kongstad0f763112018-11-19 14:14:37 +0100164 if (target_crc_ != *target_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100165 return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_,
166 *target_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200167 }
168
169 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path_);
170 if (!overlay_zip) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100171 return Error("failed to open overlay %s", GetOverlayPath().to_string().c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200172 }
173
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100174 Result<uint32_t> overlay_crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100175 if (!overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100176 return Error("failed to get overlay crc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200177 }
178
Mårten Kongstad0f763112018-11-19 14:14:37 +0100179 if (overlay_crc_ != *overlay_crc) {
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100180 return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_,
181 *overlay_crc);
Mårten Kongstad02751232018-04-27 13:16:32 +0200182 }
183
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100184 return Unit{};
Mårten Kongstad02751232018-04-27 13:16:32 +0200185}
186
187std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
188 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
189
190 uint16_t target_package_id16;
191 if (!Read16(stream, &target_package_id16) || !Read16(stream, &idmap_data_header->type_count_)) {
192 return nullptr;
193 }
194 idmap_data_header->target_package_id_ = target_package_id16;
195
196 return std::move(idmap_data_header);
197}
198
199std::unique_ptr<const IdmapData::TypeEntry> IdmapData::TypeEntry::FromBinaryStream(
200 std::istream& stream) {
201 std::unique_ptr<IdmapData::TypeEntry> data(new IdmapData::TypeEntry());
Mårten Kongstadb8779022018-11-29 09:53:17 +0100202 uint16_t target_type16;
203 uint16_t overlay_type16;
204 uint16_t entry_count;
Mårten Kongstad02751232018-04-27 13:16:32 +0200205 if (!Read16(stream, &target_type16) || !Read16(stream, &overlay_type16) ||
206 !Read16(stream, &entry_count) || !Read16(stream, &data->entry_offset_)) {
207 return nullptr;
208 }
209 data->target_type_id_ = target_type16;
210 data->overlay_type_id_ = overlay_type16;
211 for (uint16_t i = 0; i < entry_count; i++) {
212 ResourceId resid;
213 if (!Read32(stream, &resid)) {
214 return nullptr;
215 }
216 data->entries_.push_back(resid);
217 }
218
219 return std::move(data);
220}
221
222std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
223 std::unique_ptr<IdmapData> data(new IdmapData());
224 data->header_ = IdmapData::Header::FromBinaryStream(stream);
225 if (!data->header_) {
226 return nullptr;
227 }
228 for (size_t type_count = 0; type_count < data->header_->GetTypeCount(); type_count++) {
229 std::unique_ptr<const TypeEntry> type = IdmapData::TypeEntry::FromBinaryStream(stream);
230 if (!type) {
231 return nullptr;
232 }
233 data->type_entries_.push_back(std::move(type));
234 }
235 return std::move(data);
236}
237
238std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
239 const std::string& absolute_apk_path) {
240 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
241 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
242 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
243 replace(copy.begin(), copy.end(), '/', '@');
244 return absolute_dir + "/" + copy + "@idmap";
245}
246
247std::unique_ptr<const Idmap> Idmap::FromBinaryStream(std::istream& stream,
248 std::ostream& out_error) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100249 SYSTRACE << "Idmap::FromBinaryStream";
Mårten Kongstad02751232018-04-27 13:16:32 +0200250 std::unique_ptr<Idmap> idmap(new Idmap());
251
252 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
253 if (!idmap->header_) {
254 out_error << "error: failed to parse idmap header" << std::endl;
255 return nullptr;
256 }
257
258 // idmap version 0x01 does not specify the number of data blocks that follow
259 // the idmap header; assume exactly one data block
260 for (int i = 0; i < 1; i++) {
261 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
262 if (!data) {
263 out_error << "error: failed to parse data block " << i << std::endl;
264 return nullptr;
265 }
266 idmap->data_.push_back(std::move(data));
267 }
268
269 return std::move(idmap);
270}
271
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800272std::string ConcatPolicies(const std::vector<std::string>& policies) {
273 std::string message;
274 for (const std::string& policy : policies) {
275 if (message.empty()) {
276 message.append(policy);
277 } else {
278 message.append(policy);
279 message.append("|");
280 }
281 }
282
283 return message;
284}
285
286Result<Unit> CheckOverlayable(const LoadedPackage& target_package,
287 const utils::OverlayManifestInfo& overlay_info,
288 const PolicyBitmask& fulfilled_policies, const ResourceId& resid) {
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800289 static constexpr const PolicyBitmask sDefaultPolicies =
290 PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION |
291 PolicyFlags::POLICY_PRODUCT_PARTITION | PolicyFlags::POLICY_SIGNATURE;
292
293 // If the resource does not have an overlayable definition, allow the resource to be overlaid if
294 // the overlay is preinstalled or signed with the same signature as the target.
295 if (!target_package.DefinesOverlayable()) {
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800296 return (sDefaultPolicies & fulfilled_policies) != 0
297 ? Result<Unit>({})
298 : Error(
299 "overlay must be preinstalled or signed with the same signature as the "
300 "target");
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800301 }
302
Ryan Mitchella3628462019-01-14 12:19:40 -0800303 const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(resid);
304 if (overlayable_info == nullptr) {
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800305 // Do not allow non-overlayable resources to be overlaid.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800306 return Error("resource has no overlayable declaration");
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800307 }
308
Ryan Mitchell19823452019-01-29 12:01:24 -0800309 if (overlay_info.target_name != overlayable_info->name) {
Ryan Mitchella3628462019-01-14 12:19:40 -0800310 // If the overlay supplies a target overlayable name, the resource must belong to the
311 // overlayable defined with the specified name to be overlaid.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800312 return Error("<overlay> android:targetName '%s' does not match overlayable name '%s'",
313 overlay_info.target_name.c_str(), overlayable_info->name.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -0800314 }
315
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800316 // Enforce policy restrictions if the resource is declared as overlayable.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800317 if ((overlayable_info->policy_flags & fulfilled_policies) == 0) {
318 return Error("overlay with policies '%s' does not fulfill any overlayable policies '%s'",
319 ConcatPolicies(BitmaskToPolicies(fulfilled_policies)).c_str(),
320 ConcatPolicies(BitmaskToPolicies(overlayable_info->policy_flags)).c_str());
321 }
322
323 return Result<Unit>({});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800324}
325
326std::unique_ptr<const Idmap> Idmap::FromApkAssets(
327 const std::string& target_apk_path, const ApkAssets& target_apk_assets,
328 const std::string& overlay_apk_path, const ApkAssets& overlay_apk_assets,
329 const PolicyBitmask& fulfilled_policies, bool enforce_overlayable, std::ostream& out_error) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100330 SYSTRACE << "Idmap::FromApkAssets";
Mårten Kongstad02751232018-04-27 13:16:32 +0200331 AssetManager2 target_asset_manager;
332 if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true, false)) {
333 out_error << "error: failed to create target asset manager" << std::endl;
334 return nullptr;
335 }
336
337 AssetManager2 overlay_asset_manager;
338 if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true, false)) {
339 out_error << "error: failed to create overlay asset manager" << std::endl;
340 return nullptr;
341 }
342
343 const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100344 if (target_arsc == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200345 out_error << "error: failed to load target resources.arsc" << std::endl;
346 return nullptr;
347 }
348
349 const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100350 if (overlay_arsc == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200351 out_error << "error: failed to load overlay resources.arsc" << std::endl;
352 return nullptr;
353 }
354
355 const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100356 if (target_pkg == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200357 out_error << "error: failed to load target package from resources.arsc" << std::endl;
358 return nullptr;
359 }
360
361 const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100362 if (overlay_pkg == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200363 out_error << "error: failed to load overlay package from resources.arsc" << std::endl;
364 return nullptr;
365 }
366
367 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
368 if (!target_zip) {
369 out_error << "error: failed to open target as zip" << std::endl;
370 return nullptr;
371 }
372
373 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
374 if (!overlay_zip) {
375 out_error << "error: failed to open overlay as zip" << std::endl;
376 return nullptr;
377 }
378
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100379 auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
Ryan Mitchella3628462019-01-14 12:19:40 -0800380 if (!overlay_info) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100381 out_error << "error: " << overlay_info.GetErrorMessage() << std::endl;
Ryan Mitchella3628462019-01-14 12:19:40 -0800382 return nullptr;
383 }
384
Mårten Kongstad02751232018-04-27 13:16:32 +0200385 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
386 header->magic_ = kIdmapMagic;
387 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100388
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100389 Result<uint32_t> crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100390 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200391 out_error << "error: failed to get zip crc for target" << std::endl;
392 return nullptr;
393 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100394 header->target_crc_ = *crc;
395
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100396 crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100397 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200398 out_error << "error: failed to get zip crc for overlay" << std::endl;
399 return nullptr;
400 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100401 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200402
403 if (target_apk_path.size() > sizeof(header->target_path_)) {
404 out_error << "error: target apk path \"" << target_apk_path << "\" longer that maximum size "
405 << sizeof(header->target_path_) << std::endl;
406 return nullptr;
407 }
408 memset(header->target_path_, 0, sizeof(header->target_path_));
409 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
410
411 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
412 out_error << "error: overlay apk path \"" << overlay_apk_path << "\" longer that maximum size "
413 << sizeof(header->overlay_path_) << std::endl;
414 return nullptr;
415 }
416 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
417 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
418
419 std::unique_ptr<Idmap> idmap(new Idmap());
420 idmap->header_ = std::move(header);
421
422 // find the resources that exist in both packages
423 MatchingResources matching_resources;
424 const auto end = overlay_pkg->end();
425 for (auto iter = overlay_pkg->begin(); iter != end; ++iter) {
426 const ResourceId overlay_resid = *iter;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100427 Result<std::string> name = utils::ResToTypeEntryName(overlay_asset_manager, overlay_resid);
428 if (!name) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200429 continue;
430 }
431 // prepend "<package>:" to turn name into "<package>:<type>/<name>"
Mårten Kongstad0f763112018-11-19 14:14:37 +0100432 const std::string full_name =
433 base::StringPrintf("%s:%s", target_pkg->GetPackageName().c_str(), name->c_str());
434 const ResourceId target_resid = NameToResid(target_asset_manager, full_name);
Mårten Kongstad02751232018-04-27 13:16:32 +0200435 if (target_resid == 0) {
436 continue;
437 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800438
Ryan Mitchell2c19bd02019-03-19 15:14:33 -0700439 if (enforce_overlayable) {
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800440 Result<Unit> success =
441 CheckOverlayable(*target_pkg, *overlay_info, fulfilled_policies, target_resid);
442 if (!success) {
443 LOG(WARNING) << "overlay \"" << overlay_apk_path
444 << "\" is not allowed to overlay resource \"" << full_name
445 << "\": " << success.GetErrorMessage();
446 continue;
447 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800448 }
449
Mårten Kongstad02751232018-04-27 13:16:32 +0200450 matching_resources.Add(target_resid, overlay_resid);
451 }
452
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800453 if (matching_resources.Map().empty()) {
454 out_error << "overlay \"" << overlay_apk_path << "\" does not successfully overlay any resource"
455 << std::endl;
456 return nullptr;
457 }
458
Mårten Kongstad02751232018-04-27 13:16:32 +0200459 // encode idmap data
460 std::unique_ptr<IdmapData> data(new IdmapData());
Mårten Kongstadcf281362018-11-28 19:32:25 +0100461 const auto types_end = matching_resources.Map().cend();
462 for (auto ti = matching_resources.Map().cbegin(); ti != types_end; ++ti) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200463 auto ei = ti->second.cbegin();
464 std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry());
465 type->target_type_id_ = EXTRACT_TYPE(ei->first);
466 type->overlay_type_id_ = EXTRACT_TYPE(ei->second);
467 type->entry_offset_ = EXTRACT_ENTRY(ei->first);
468 EntryId last_target_entry = kNoEntry;
469 for (; ei != ti->second.cend(); ++ei) {
470 if (last_target_entry != kNoEntry) {
471 int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1;
472 type->entries_.insert(type->entries_.end(), count, kNoEntry);
473 }
474 type->entries_.push_back(EXTRACT_ENTRY(ei->second));
475 last_target_entry = EXTRACT_ENTRY(ei->first);
476 }
477 data->type_entries_.push_back(std::move(type));
478 }
479
480 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
481 data_header->target_package_id_ = target_pkg->GetPackageId();
482 data_header->type_count_ = data->type_entries_.size();
483 data->header_ = std::move(data_header);
484
485 idmap->data_.push_back(std::move(data));
486
487 return std::move(idmap);
488}
489
490void IdmapHeader::accept(Visitor* v) const {
491 assert(v != nullptr);
492 v->visit(*this);
493}
494
495void IdmapData::Header::accept(Visitor* v) const {
496 assert(v != nullptr);
497 v->visit(*this);
498}
499
500void IdmapData::TypeEntry::accept(Visitor* v) const {
501 assert(v != nullptr);
502 v->visit(*this);
503}
504
505void IdmapData::accept(Visitor* v) const {
506 assert(v != nullptr);
507 v->visit(*this);
508 header_->accept(v);
509 auto end = type_entries_.cend();
510 for (auto iter = type_entries_.cbegin(); iter != end; ++iter) {
511 (*iter)->accept(v);
512 }
513}
514
515void Idmap::accept(Visitor* v) const {
516 assert(v != nullptr);
517 v->visit(*this);
518 header_->accept(v);
519 auto end = data_.cend();
520 for (auto iter = data_.cbegin(); iter != end; ++iter) {
521 (*iter)->accept(v);
522 }
523}
524
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100525} // namespace android::idmap2