blob: 4649675965dbead47fab2751b168cdfd56cf2f24 [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"
Mårten Kongstad02751232018-04-27 13:16:32 +020033#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010034#include "idmap2/Result.h"
Mårten Kongstad4cbb0072018-11-30 16:22:05 +010035#include "idmap2/SysTrace.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020036#include "idmap2/ZipFile.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070037#include "utils/String16.h"
38#include "utils/String8.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020039
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
Mårten Kongstadcf622492019-03-19 22:40:58 +010059 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)
Mårten Kongstadce424902019-03-01 08:35:37 +0100127 : Error("failed to 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
Mårten Kongstadce424902019-03-01 08:35:37 +0100247Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100248 SYSTRACE << "Idmap::FromBinaryStream";
Mårten Kongstad02751232018-04-27 13:16:32 +0200249 std::unique_ptr<Idmap> idmap(new Idmap());
250
251 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
252 if (!idmap->header_) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100253 return Error("failed to parse idmap header");
Mårten Kongstad02751232018-04-27 13:16:32 +0200254 }
255
256 // idmap version 0x01 does not specify the number of data blocks that follow
257 // the idmap header; assume exactly one data block
258 for (int i = 0; i < 1; i++) {
259 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
260 if (!data) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100261 return Error("failed to parse data block %d", i);
Mårten Kongstad02751232018-04-27 13:16:32 +0200262 }
263 idmap->data_.push_back(std::move(data));
264 }
265
Mårten Kongstadce424902019-03-01 08:35:37 +0100266 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200267}
268
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800269std::string ConcatPolicies(const std::vector<std::string>& policies) {
270 std::string message;
271 for (const std::string& policy : policies) {
Ryan Mitchellac791e62019-04-03 11:02:39 -0700272 if (!message.empty()) {
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800273 message.append("|");
274 }
Ryan Mitchellac791e62019-04-03 11:02:39 -0700275 message.append(policy);
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800276 }
277
278 return message;
279}
280
281Result<Unit> CheckOverlayable(const LoadedPackage& target_package,
282 const utils::OverlayManifestInfo& overlay_info,
283 const PolicyBitmask& fulfilled_policies, const ResourceId& resid) {
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800284 static constexpr const PolicyBitmask sDefaultPolicies =
Ryan Mitchell939df092019-04-09 17:13:50 -0700285 PolicyFlags::POLICY_ODM_PARTITION | PolicyFlags::POLICY_OEM_PARTITION |
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800286 PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION |
287 PolicyFlags::POLICY_PRODUCT_PARTITION | PolicyFlags::POLICY_SIGNATURE;
288
289 // If the resource does not have an overlayable definition, allow the resource to be overlaid if
290 // the overlay is preinstalled or signed with the same signature as the target.
291 if (!target_package.DefinesOverlayable()) {
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800292 return (sDefaultPolicies & fulfilled_policies) != 0
293 ? Result<Unit>({})
294 : Error(
295 "overlay must be preinstalled or signed with the same signature as the "
296 "target");
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800297 }
298
Ryan Mitchella3628462019-01-14 12:19:40 -0800299 const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(resid);
300 if (overlayable_info == nullptr) {
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800301 // Do not allow non-overlayable resources to be overlaid.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800302 return Error("resource has no overlayable declaration");
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800303 }
304
Ryan Mitchell19823452019-01-29 12:01:24 -0800305 if (overlay_info.target_name != overlayable_info->name) {
Ryan Mitchella3628462019-01-14 12:19:40 -0800306 // If the overlay supplies a target overlayable name, the resource must belong to the
307 // overlayable defined with the specified name to be overlaid.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800308 return Error("<overlay> android:targetName '%s' does not match overlayable name '%s'",
309 overlay_info.target_name.c_str(), overlayable_info->name.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -0800310 }
311
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800312 // Enforce policy restrictions if the resource is declared as overlayable.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800313 if ((overlayable_info->policy_flags & fulfilled_policies) == 0) {
314 return Error("overlay with policies '%s' does not fulfill any overlayable policies '%s'",
315 ConcatPolicies(BitmaskToPolicies(fulfilled_policies)).c_str(),
316 ConcatPolicies(BitmaskToPolicies(overlayable_info->policy_flags)).c_str());
317 }
318
319 return Result<Unit>({});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800320}
321
Mårten Kongstadce424902019-03-01 08:35:37 +0100322Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const std::string& target_apk_path,
323 const ApkAssets& target_apk_assets,
324 const std::string& overlay_apk_path,
325 const ApkAssets& overlay_apk_assets,
326 const PolicyBitmask& fulfilled_policies,
327 bool enforce_overlayable) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100328 SYSTRACE << "Idmap::FromApkAssets";
Mårten Kongstad02751232018-04-27 13:16:32 +0200329 AssetManager2 target_asset_manager;
330 if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true, false)) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100331 return Error("failed to create target asset manager");
Mårten Kongstad02751232018-04-27 13:16:32 +0200332 }
333
334 AssetManager2 overlay_asset_manager;
335 if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true, false)) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100336 return Error("failed to create overlay asset manager");
Mårten Kongstad02751232018-04-27 13:16:32 +0200337 }
338
339 const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100340 if (target_arsc == nullptr) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100341 return Error("failed to load target resources.arsc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200342 }
343
344 const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100345 if (overlay_arsc == nullptr) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100346 return Error("failed to load overlay resources.arsc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200347 }
348
349 const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100350 if (target_pkg == nullptr) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100351 return Error("failed to load target package from resources.arsc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200352 }
353
354 const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100355 if (overlay_pkg == nullptr) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100356 return Error("failed to load overlay package from resources.arsc");
Mårten Kongstad02751232018-04-27 13:16:32 +0200357 }
358
359 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
360 if (!target_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100361 return Error("failed to open target as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200362 }
363
364 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
365 if (!overlay_zip) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100366 return Error("failed to open overlay as zip");
Mårten Kongstad02751232018-04-27 13:16:32 +0200367 }
368
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100369 auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
Ryan Mitchella3628462019-01-14 12:19:40 -0800370 if (!overlay_info) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100371 return overlay_info.GetError();
Ryan Mitchella3628462019-01-14 12:19:40 -0800372 }
373
Mårten Kongstad02751232018-04-27 13:16:32 +0200374 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
375 header->magic_ = kIdmapMagic;
376 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100377
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100378 Result<uint32_t> crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100379 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100380 return Error(crc.GetError(), "failed to get zip CRC for target");
Mårten Kongstad02751232018-04-27 13:16:32 +0200381 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100382 header->target_crc_ = *crc;
383
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100384 crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100385 if (!crc) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100386 return Error(crc.GetError(), "failed to get zip CRC for overlay");
Mårten Kongstad02751232018-04-27 13:16:32 +0200387 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100388 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200389
390 if (target_apk_path.size() > sizeof(header->target_path_)) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100391 return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
392 sizeof(header->target_path_));
Mårten Kongstad02751232018-04-27 13:16:32 +0200393 }
394 memset(header->target_path_, 0, sizeof(header->target_path_));
395 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
396
397 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100398 return Error("overlay apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
399 sizeof(header->target_path_));
Mårten Kongstad02751232018-04-27 13:16:32 +0200400 }
401 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
402 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
403
404 std::unique_ptr<Idmap> idmap(new Idmap());
405 idmap->header_ = std::move(header);
406
407 // find the resources that exist in both packages
408 MatchingResources matching_resources;
409 const auto end = overlay_pkg->end();
410 for (auto iter = overlay_pkg->begin(); iter != end; ++iter) {
411 const ResourceId overlay_resid = *iter;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100412 Result<std::string> name = utils::ResToTypeEntryName(overlay_asset_manager, overlay_resid);
413 if (!name) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200414 continue;
415 }
416 // prepend "<package>:" to turn name into "<package>:<type>/<name>"
Mårten Kongstad0f763112018-11-19 14:14:37 +0100417 const std::string full_name =
418 base::StringPrintf("%s:%s", target_pkg->GetPackageName().c_str(), name->c_str());
419 const ResourceId target_resid = NameToResid(target_asset_manager, full_name);
Mårten Kongstad02751232018-04-27 13:16:32 +0200420 if (target_resid == 0) {
421 continue;
422 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800423
Ryan Mitchell2c19bd02019-03-19 15:14:33 -0700424 if (enforce_overlayable) {
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800425 Result<Unit> success =
426 CheckOverlayable(*target_pkg, *overlay_info, fulfilled_policies, target_resid);
427 if (!success) {
428 LOG(WARNING) << "overlay \"" << overlay_apk_path
429 << "\" is not allowed to overlay resource \"" << full_name
430 << "\": " << success.GetErrorMessage();
431 continue;
432 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800433 }
434
Mårten Kongstad02751232018-04-27 13:16:32 +0200435 matching_resources.Add(target_resid, overlay_resid);
436 }
437
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800438 if (matching_resources.Map().empty()) {
Mårten Kongstadce424902019-03-01 08:35:37 +0100439 return Error("overlay \"%s\" does not successfully overlay any resource",
440 overlay_apk_path.c_str());
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800441 }
442
Mårten Kongstad02751232018-04-27 13:16:32 +0200443 // encode idmap data
444 std::unique_ptr<IdmapData> data(new IdmapData());
Mårten Kongstadcf281362018-11-28 19:32:25 +0100445 const auto types_end = matching_resources.Map().cend();
446 for (auto ti = matching_resources.Map().cbegin(); ti != types_end; ++ti) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200447 auto ei = ti->second.cbegin();
448 std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry());
449 type->target_type_id_ = EXTRACT_TYPE(ei->first);
450 type->overlay_type_id_ = EXTRACT_TYPE(ei->second);
451 type->entry_offset_ = EXTRACT_ENTRY(ei->first);
452 EntryId last_target_entry = kNoEntry;
453 for (; ei != ti->second.cend(); ++ei) {
454 if (last_target_entry != kNoEntry) {
455 int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1;
456 type->entries_.insert(type->entries_.end(), count, kNoEntry);
457 }
458 type->entries_.push_back(EXTRACT_ENTRY(ei->second));
459 last_target_entry = EXTRACT_ENTRY(ei->first);
460 }
461 data->type_entries_.push_back(std::move(type));
462 }
463
464 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
465 data_header->target_package_id_ = target_pkg->GetPackageId();
466 data_header->type_count_ = data->type_entries_.size();
467 data->header_ = std::move(data_header);
468
469 idmap->data_.push_back(std::move(data));
470
Mårten Kongstadce424902019-03-01 08:35:37 +0100471 return {std::move(idmap)};
Mårten Kongstad02751232018-04-27 13:16:32 +0200472}
473
474void IdmapHeader::accept(Visitor* v) const {
475 assert(v != nullptr);
476 v->visit(*this);
477}
478
479void IdmapData::Header::accept(Visitor* v) const {
480 assert(v != nullptr);
481 v->visit(*this);
482}
483
484void IdmapData::TypeEntry::accept(Visitor* v) const {
485 assert(v != nullptr);
486 v->visit(*this);
487}
488
489void IdmapData::accept(Visitor* v) const {
490 assert(v != nullptr);
491 v->visit(*this);
492 header_->accept(v);
493 auto end = type_entries_.cend();
494 for (auto iter = type_entries_.cbegin(); iter != end; ++iter) {
495 (*iter)->accept(v);
496 }
497}
498
499void Idmap::accept(Visitor* v) const {
500 assert(v != nullptr);
501 v->visit(*this);
502 header_->accept(v);
503 auto end = data_.cend();
504 for (auto iter = data_.cbegin(); iter != end; ++iter) {
505 (*iter)->accept(v);
506 }
507}
508
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100509} // namespace android::idmap2