blob: a927a69b2585ba96e8f70a4b6397dab076dea8bd [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
Mårten Kongstadcf281362018-11-28 19:32:25 +010058 inline const std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>>& Map() const {
59 return map_;
60 }
61
62 private:
Mårten Kongstad02751232018-04-27 13:16:32 +020063 // target type id -> set { pair { overlay entry id, overlay entry id } }
Mårten Kongstadcf281362018-11-28 19:32:25 +010064 std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>> map_;
Mårten Kongstad02751232018-04-27 13:16:32 +020065};
66
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010067bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) {
Mårten Kongstad02751232018-04-27 13:16:32 +020068 uint16_t value;
69 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint16_t))) {
70 *out = dtohl(value);
71 return true;
72 }
73 return false;
74}
75
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010076bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
Mårten Kongstad02751232018-04-27 13:16:32 +020077 uint32_t value;
78 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
79 *out = dtohl(value);
80 return true;
81 }
82 return false;
83}
84
85// a string is encoded as a kIdmapStringLength char array; the array is always null-terminated
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010086bool WARN_UNUSED ReadString(std::istream& stream, char out[kIdmapStringLength]) {
Mårten Kongstad02751232018-04-27 13:16:32 +020087 char buf[kIdmapStringLength];
88 memset(buf, 0, sizeof(buf));
89 if (!stream.read(buf, sizeof(buf))) {
90 return false;
91 }
92 if (buf[sizeof(buf) - 1] != '\0') {
93 return false;
94 }
95 memcpy(out, buf, sizeof(buf));
96 return true;
97}
98
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010099ResourceId NameToResid(const AssetManager2& am, const std::string& name) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200100 return am.GetResourceId(name);
101}
102
103// TODO(martenkongstad): scan for package name instead of assuming package at index 0
104//
105// idmap version 0x01 naively assumes that the package to use is always the first ResTable_package
106// in the resources.arsc blob. In most cases, there is only a single ResTable_package anyway, so
107// this assumption tends to work out. That said, the correct thing to do is to scan
108// resources.arsc for a package with a given name as read from the package manifest instead of
109// relying on a hard-coded index. This however requires storing the package name in the idmap
110// header, which in turn requires incrementing the idmap version. Because the initial version of
111// idmap2 is compatible with idmap, this will have to wait for now.
Mårten Kongstad744ccfe2018-12-20 14:56:14 +0100112const LoadedPackage* GetPackageAtIndex0(const LoadedArsc& loaded_arsc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200113 const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc.GetPackages();
114 if (packages.empty()) {
115 return nullptr;
116 }
117 int id = packages[0]->GetPackageId();
118 return loaded_arsc.GetPackageById(id);
119}
120
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100121Result<uint32_t> GetCrc(const ZipFile& zip) {
122 const Result<uint32_t> a = zip.Crc("resources.arsc");
123 const Result<uint32_t> b = zip.Crc("AndroidManifest.xml");
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100124 return a && b
125 ? Result<uint32_t>(*a ^ *b)
126 : Error("Couldn't get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc");
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100127}
128
Mårten Kongstad744ccfe2018-12-20 14:56:14 +0100129} // namespace
130
Mårten Kongstad02751232018-04-27 13:16:32 +0200131std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
132 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
133
134 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
135 !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
136 !ReadString(stream, idmap_header->target_path_) ||
137 !ReadString(stream, idmap_header->overlay_path_)) {
138 return nullptr;
139 }
140
141 return std::move(idmap_header);
142}
143
144bool IdmapHeader::IsUpToDate(std::ostream& out_error) const {
145 if (magic_ != kIdmapMagic) {
146 out_error << base::StringPrintf("error: bad magic: actual 0x%08x, expected 0x%08x", magic_,
147 kIdmapMagic)
148 << std::endl;
149 return false;
150 }
151
152 if (version_ != kIdmapCurrentVersion) {
153 out_error << base::StringPrintf("error: bad version: actual 0x%08x, expected 0x%08x", version_,
154 kIdmapCurrentVersion)
155 << std::endl;
156 return false;
157 }
158
159 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path_);
160 if (!target_zip) {
161 out_error << "error: failed to open target " << target_path_ << std::endl;
162 return false;
163 }
164
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100165 Result<uint32_t> target_crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100166 if (!target_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200167 out_error << "error: failed to get target crc" << std::endl;
168 return false;
169 }
170
Mårten Kongstad0f763112018-11-19 14:14:37 +0100171 if (target_crc_ != *target_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200172 out_error << base::StringPrintf(
173 "error: bad target crc: idmap version 0x%08x, file system version 0x%08x",
Mårten Kongstad0f763112018-11-19 14:14:37 +0100174 target_crc_, *target_crc)
Mårten Kongstad02751232018-04-27 13:16:32 +0200175 << std::endl;
176 return false;
177 }
178
179 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path_);
180 if (!overlay_zip) {
181 out_error << "error: failed to open overlay " << overlay_path_ << std::endl;
182 return false;
183 }
184
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100185 Result<uint32_t> overlay_crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100186 if (!overlay_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200187 out_error << "error: failed to get overlay crc" << std::endl;
188 return false;
189 }
190
Mårten Kongstad0f763112018-11-19 14:14:37 +0100191 if (overlay_crc_ != *overlay_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200192 out_error << base::StringPrintf(
193 "error: bad overlay crc: idmap version 0x%08x, file system version 0x%08x",
Mårten Kongstad0f763112018-11-19 14:14:37 +0100194 overlay_crc_, *overlay_crc)
Mårten Kongstad02751232018-04-27 13:16:32 +0200195 << std::endl;
196 return false;
197 }
198
199 return true;
200}
201
202std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
203 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
204
205 uint16_t target_package_id16;
206 if (!Read16(stream, &target_package_id16) || !Read16(stream, &idmap_data_header->type_count_)) {
207 return nullptr;
208 }
209 idmap_data_header->target_package_id_ = target_package_id16;
210
211 return std::move(idmap_data_header);
212}
213
214std::unique_ptr<const IdmapData::TypeEntry> IdmapData::TypeEntry::FromBinaryStream(
215 std::istream& stream) {
216 std::unique_ptr<IdmapData::TypeEntry> data(new IdmapData::TypeEntry());
Mårten Kongstadb8779022018-11-29 09:53:17 +0100217 uint16_t target_type16;
218 uint16_t overlay_type16;
219 uint16_t entry_count;
Mårten Kongstad02751232018-04-27 13:16:32 +0200220 if (!Read16(stream, &target_type16) || !Read16(stream, &overlay_type16) ||
221 !Read16(stream, &entry_count) || !Read16(stream, &data->entry_offset_)) {
222 return nullptr;
223 }
224 data->target_type_id_ = target_type16;
225 data->overlay_type_id_ = overlay_type16;
226 for (uint16_t i = 0; i < entry_count; i++) {
227 ResourceId resid;
228 if (!Read32(stream, &resid)) {
229 return nullptr;
230 }
231 data->entries_.push_back(resid);
232 }
233
234 return std::move(data);
235}
236
237std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
238 std::unique_ptr<IdmapData> data(new IdmapData());
239 data->header_ = IdmapData::Header::FromBinaryStream(stream);
240 if (!data->header_) {
241 return nullptr;
242 }
243 for (size_t type_count = 0; type_count < data->header_->GetTypeCount(); type_count++) {
244 std::unique_ptr<const TypeEntry> type = IdmapData::TypeEntry::FromBinaryStream(stream);
245 if (!type) {
246 return nullptr;
247 }
248 data->type_entries_.push_back(std::move(type));
249 }
250 return std::move(data);
251}
252
253std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
254 const std::string& absolute_apk_path) {
255 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
256 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
257 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
258 replace(copy.begin(), copy.end(), '/', '@');
259 return absolute_dir + "/" + copy + "@idmap";
260}
261
262std::unique_ptr<const Idmap> Idmap::FromBinaryStream(std::istream& stream,
263 std::ostream& out_error) {
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_) {
269 out_error << "error: failed to parse idmap header" << std::endl;
270 return nullptr;
271 }
272
273 // idmap version 0x01 does not specify the number of data blocks that follow
274 // the idmap header; assume exactly one data block
275 for (int i = 0; i < 1; i++) {
276 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
277 if (!data) {
278 out_error << "error: failed to parse data block " << i << std::endl;
279 return nullptr;
280 }
281 idmap->data_.push_back(std::move(data));
282 }
283
284 return std::move(idmap);
285}
286
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800287std::string ConcatPolicies(const std::vector<std::string>& policies) {
288 std::string message;
289 for (const std::string& policy : policies) {
290 if (message.empty()) {
291 message.append(policy);
292 } else {
293 message.append(policy);
294 message.append("|");
295 }
296 }
297
298 return message;
299}
300
301Result<Unit> CheckOverlayable(const LoadedPackage& target_package,
302 const utils::OverlayManifestInfo& overlay_info,
303 const PolicyBitmask& fulfilled_policies, const ResourceId& resid) {
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800304 static constexpr const PolicyBitmask sDefaultPolicies =
305 PolicyFlags::POLICY_SYSTEM_PARTITION | PolicyFlags::POLICY_VENDOR_PARTITION |
306 PolicyFlags::POLICY_PRODUCT_PARTITION | PolicyFlags::POLICY_SIGNATURE;
307
308 // If the resource does not have an overlayable definition, allow the resource to be overlaid if
309 // the overlay is preinstalled or signed with the same signature as the target.
310 if (!target_package.DefinesOverlayable()) {
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800311 return (sDefaultPolicies & fulfilled_policies) != 0
312 ? Result<Unit>({})
313 : Error(
314 "overlay must be preinstalled or signed with the same signature as the "
315 "target");
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800316 }
317
Ryan Mitchella3628462019-01-14 12:19:40 -0800318 const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(resid);
319 if (overlayable_info == nullptr) {
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800320 // Do not allow non-overlayable resources to be overlaid.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800321 return Error("resource has no overlayable declaration");
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800322 }
323
Ryan Mitchell19823452019-01-29 12:01:24 -0800324 if (overlay_info.target_name != overlayable_info->name) {
Ryan Mitchella3628462019-01-14 12:19:40 -0800325 // If the overlay supplies a target overlayable name, the resource must belong to the
326 // overlayable defined with the specified name to be overlaid.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800327 return Error("<overlay> android:targetName '%s' does not match overlayable name '%s'",
328 overlay_info.target_name.c_str(), overlayable_info->name.c_str());
Ryan Mitchella3628462019-01-14 12:19:40 -0800329 }
330
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800331 // Enforce policy restrictions if the resource is declared as overlayable.
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800332 if ((overlayable_info->policy_flags & fulfilled_policies) == 0) {
333 return Error("overlay with policies '%s' does not fulfill any overlayable policies '%s'",
334 ConcatPolicies(BitmaskToPolicies(fulfilled_policies)).c_str(),
335 ConcatPolicies(BitmaskToPolicies(overlayable_info->policy_flags)).c_str());
336 }
337
338 return Result<Unit>({});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800339}
340
341std::unique_ptr<const Idmap> Idmap::FromApkAssets(
342 const std::string& target_apk_path, const ApkAssets& target_apk_assets,
343 const std::string& overlay_apk_path, const ApkAssets& overlay_apk_assets,
344 const PolicyBitmask& fulfilled_policies, bool enforce_overlayable, std::ostream& out_error) {
Mårten Kongstad4cbb0072018-11-30 16:22:05 +0100345 SYSTRACE << "Idmap::FromApkAssets";
Mårten Kongstad02751232018-04-27 13:16:32 +0200346 AssetManager2 target_asset_manager;
347 if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true, false)) {
348 out_error << "error: failed to create target asset manager" << std::endl;
349 return nullptr;
350 }
351
352 AssetManager2 overlay_asset_manager;
353 if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true, false)) {
354 out_error << "error: failed to create overlay asset manager" << std::endl;
355 return nullptr;
356 }
357
358 const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100359 if (target_arsc == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200360 out_error << "error: failed to load target resources.arsc" << std::endl;
361 return nullptr;
362 }
363
364 const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100365 if (overlay_arsc == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200366 out_error << "error: failed to load overlay resources.arsc" << std::endl;
367 return nullptr;
368 }
369
370 const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100371 if (target_pkg == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200372 out_error << "error: failed to load target package from resources.arsc" << std::endl;
373 return nullptr;
374 }
375
376 const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100377 if (overlay_pkg == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200378 out_error << "error: failed to load overlay package from resources.arsc" << std::endl;
379 return nullptr;
380 }
381
382 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
383 if (!target_zip) {
384 out_error << "error: failed to open target as zip" << std::endl;
385 return nullptr;
386 }
387
388 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
389 if (!overlay_zip) {
390 out_error << "error: failed to open overlay as zip" << std::endl;
391 return nullptr;
392 }
393
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100394 auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
Ryan Mitchella3628462019-01-14 12:19:40 -0800395 if (!overlay_info) {
Mårten Kongstad49d835d2019-01-31 10:50:48 +0100396 out_error << "error: " << overlay_info.GetErrorMessage() << std::endl;
Ryan Mitchella3628462019-01-14 12:19:40 -0800397 return nullptr;
398 }
399
Mårten Kongstad02751232018-04-27 13:16:32 +0200400 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
401 header->magic_ = kIdmapMagic;
402 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100403
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100404 Result<uint32_t> crc = GetCrc(*target_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100405 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200406 out_error << "error: failed to get zip crc for target" << std::endl;
407 return nullptr;
408 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100409 header->target_crc_ = *crc;
410
Mårten Kongstad9371dc12019-01-22 14:35:12 +0100411 crc = GetCrc(*overlay_zip);
Mårten Kongstad0f763112018-11-19 14:14:37 +0100412 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200413 out_error << "error: failed to get zip crc for overlay" << std::endl;
414 return nullptr;
415 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100416 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200417
418 if (target_apk_path.size() > sizeof(header->target_path_)) {
419 out_error << "error: target apk path \"" << target_apk_path << "\" longer that maximum size "
420 << sizeof(header->target_path_) << std::endl;
421 return nullptr;
422 }
423 memset(header->target_path_, 0, sizeof(header->target_path_));
424 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
425
426 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
427 out_error << "error: overlay apk path \"" << overlay_apk_path << "\" longer that maximum size "
428 << sizeof(header->overlay_path_) << std::endl;
429 return nullptr;
430 }
431 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
432 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
433
434 std::unique_ptr<Idmap> idmap(new Idmap());
435 idmap->header_ = std::move(header);
436
437 // find the resources that exist in both packages
438 MatchingResources matching_resources;
439 const auto end = overlay_pkg->end();
440 for (auto iter = overlay_pkg->begin(); iter != end; ++iter) {
441 const ResourceId overlay_resid = *iter;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100442 Result<std::string> name = utils::ResToTypeEntryName(overlay_asset_manager, overlay_resid);
443 if (!name) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200444 continue;
445 }
446 // prepend "<package>:" to turn name into "<package>:<type>/<name>"
Mårten Kongstad0f763112018-11-19 14:14:37 +0100447 const std::string full_name =
448 base::StringPrintf("%s:%s", target_pkg->GetPackageName().c_str(), name->c_str());
449 const ResourceId target_resid = NameToResid(target_asset_manager, full_name);
Mårten Kongstad02751232018-04-27 13:16:32 +0200450 if (target_resid == 0) {
451 continue;
452 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800453
Ryan Mitchell4c09a4a2019-03-08 08:57:48 -0800454 if (!enforce_overlayable) {
455 Result<Unit> success =
456 CheckOverlayable(*target_pkg, *overlay_info, fulfilled_policies, target_resid);
457 if (!success) {
458 LOG(WARNING) << "overlay \"" << overlay_apk_path
459 << "\" is not allowed to overlay resource \"" << full_name
460 << "\": " << success.GetErrorMessage();
461 continue;
462 }
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800463 }
464
Mårten Kongstad02751232018-04-27 13:16:32 +0200465 matching_resources.Add(target_resid, overlay_resid);
466 }
467
Ryan Mitchellb863ca32019-03-07 14:31:54 -0800468 if (matching_resources.Map().empty()) {
469 out_error << "overlay \"" << overlay_apk_path << "\" does not successfully overlay any resource"
470 << std::endl;
471 return nullptr;
472 }
473
Mårten Kongstad02751232018-04-27 13:16:32 +0200474 // encode idmap data
475 std::unique_ptr<IdmapData> data(new IdmapData());
Mårten Kongstadcf281362018-11-28 19:32:25 +0100476 const auto types_end = matching_resources.Map().cend();
477 for (auto ti = matching_resources.Map().cbegin(); ti != types_end; ++ti) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200478 auto ei = ti->second.cbegin();
479 std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry());
480 type->target_type_id_ = EXTRACT_TYPE(ei->first);
481 type->overlay_type_id_ = EXTRACT_TYPE(ei->second);
482 type->entry_offset_ = EXTRACT_ENTRY(ei->first);
483 EntryId last_target_entry = kNoEntry;
484 for (; ei != ti->second.cend(); ++ei) {
485 if (last_target_entry != kNoEntry) {
486 int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1;
487 type->entries_.insert(type->entries_.end(), count, kNoEntry);
488 }
489 type->entries_.push_back(EXTRACT_ENTRY(ei->second));
490 last_target_entry = EXTRACT_ENTRY(ei->first);
491 }
492 data->type_entries_.push_back(std::move(type));
493 }
494
495 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
496 data_header->target_package_id_ = target_pkg->GetPackageId();
497 data_header->type_count_ = data->type_entries_.size();
498 data->header_ = std::move(data_header);
499
500 idmap->data_.push_back(std::move(data));
501
502 return std::move(idmap);
503}
504
505void IdmapHeader::accept(Visitor* v) const {
506 assert(v != nullptr);
507 v->visit(*this);
508}
509
510void IdmapData::Header::accept(Visitor* v) const {
511 assert(v != nullptr);
512 v->visit(*this);
513}
514
515void IdmapData::TypeEntry::accept(Visitor* v) const {
516 assert(v != nullptr);
517 v->visit(*this);
518}
519
520void IdmapData::accept(Visitor* v) const {
521 assert(v != nullptr);
522 v->visit(*this);
523 header_->accept(v);
524 auto end = type_entries_.cend();
525 for (auto iter = type_entries_.cbegin(); iter != end; ++iter) {
526 (*iter)->accept(v);
527 }
528}
529
530void Idmap::accept(Visitor* v) const {
531 assert(v != nullptr);
532 v->visit(*this);
533 header_->accept(v);
534 auto end = data_.cend();
535 for (auto iter = data_.cbegin(); iter != end; ++iter) {
536 (*iter)->accept(v);
537 }
538}
539
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100540} // namespace android::idmap2