blob: 0ddc224d032f3251f4217833360355af5542dd66 [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 Kongstad02751232018-04-27 13:16:32 +020037#include "idmap2/ZipFile.h"
38
39namespace android {
40namespace idmap2 {
41
42#define EXTRACT_TYPE(resid) ((0x00ff0000 & (resid)) >> 16)
43
44#define EXTRACT_ENTRY(resid) (0x0000ffff & (resid))
45
46struct MatchingResources {
47 void Add(ResourceId target_resid, ResourceId overlay_resid) {
48 TypeId target_typeid = EXTRACT_TYPE(target_resid);
49 if (map.find(target_typeid) == map.end()) {
50 map.emplace(target_typeid, std::set<std::pair<ResourceId, ResourceId>>());
51 }
52 map[target_typeid].insert(std::make_pair(target_resid, overlay_resid));
53 }
54
55 // target type id -> set { pair { overlay entry id, overlay entry id } }
56 std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>> map;
57};
58
59static bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) {
60 uint16_t value;
61 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint16_t))) {
62 *out = dtohl(value);
63 return true;
64 }
65 return false;
66}
67
68static bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
69 uint32_t value;
70 if (stream.read(reinterpret_cast<char*>(&value), sizeof(uint32_t))) {
71 *out = dtohl(value);
72 return true;
73 }
74 return false;
75}
76
77// a string is encoded as a kIdmapStringLength char array; the array is always null-terminated
78static bool WARN_UNUSED ReadString(std::istream& stream, char out[kIdmapStringLength]) {
79 char buf[kIdmapStringLength];
80 memset(buf, 0, sizeof(buf));
81 if (!stream.read(buf, sizeof(buf))) {
82 return false;
83 }
84 if (buf[sizeof(buf) - 1] != '\0') {
85 return false;
86 }
87 memcpy(out, buf, sizeof(buf));
88 return true;
89}
90
91static ResourceId NameToResid(const AssetManager2& am, const std::string& name) {
92 return am.GetResourceId(name);
93}
94
95// TODO(martenkongstad): scan for package name instead of assuming package at index 0
96//
97// idmap version 0x01 naively assumes that the package to use is always the first ResTable_package
98// in the resources.arsc blob. In most cases, there is only a single ResTable_package anyway, so
99// this assumption tends to work out. That said, the correct thing to do is to scan
100// resources.arsc for a package with a given name as read from the package manifest instead of
101// relying on a hard-coded index. This however requires storing the package name in the idmap
102// header, which in turn requires incrementing the idmap version. Because the initial version of
103// idmap2 is compatible with idmap, this will have to wait for now.
104static const LoadedPackage* GetPackageAtIndex0(const LoadedArsc& loaded_arsc) {
105 const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc.GetPackages();
106 if (packages.empty()) {
107 return nullptr;
108 }
109 int id = packages[0]->GetPackageId();
110 return loaded_arsc.GetPackageById(id);
111}
112
113std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) {
114 std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader());
115
116 if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
117 !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
118 !ReadString(stream, idmap_header->target_path_) ||
119 !ReadString(stream, idmap_header->overlay_path_)) {
120 return nullptr;
121 }
122
123 return std::move(idmap_header);
124}
125
126bool IdmapHeader::IsUpToDate(std::ostream& out_error) const {
127 if (magic_ != kIdmapMagic) {
128 out_error << base::StringPrintf("error: bad magic: actual 0x%08x, expected 0x%08x", magic_,
129 kIdmapMagic)
130 << std::endl;
131 return false;
132 }
133
134 if (version_ != kIdmapCurrentVersion) {
135 out_error << base::StringPrintf("error: bad version: actual 0x%08x, expected 0x%08x", version_,
136 kIdmapCurrentVersion)
137 << std::endl;
138 return false;
139 }
140
141 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path_);
142 if (!target_zip) {
143 out_error << "error: failed to open target " << target_path_ << std::endl;
144 return false;
145 }
146
Mårten Kongstad0f763112018-11-19 14:14:37 +0100147 Result<uint32_t> target_crc = target_zip->Crc("resources.arsc");
148 if (!target_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200149 out_error << "error: failed to get target crc" << std::endl;
150 return false;
151 }
152
Mårten Kongstad0f763112018-11-19 14:14:37 +0100153 if (target_crc_ != *target_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200154 out_error << base::StringPrintf(
155 "error: bad target crc: idmap version 0x%08x, file system version 0x%08x",
Mårten Kongstad0f763112018-11-19 14:14:37 +0100156 target_crc_, *target_crc)
Mårten Kongstad02751232018-04-27 13:16:32 +0200157 << std::endl;
158 return false;
159 }
160
161 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path_);
162 if (!overlay_zip) {
163 out_error << "error: failed to open overlay " << overlay_path_ << std::endl;
164 return false;
165 }
166
Mårten Kongstad0f763112018-11-19 14:14:37 +0100167 Result<uint32_t> overlay_crc = overlay_zip->Crc("resources.arsc");
168 if (!overlay_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200169 out_error << "error: failed to get overlay crc" << std::endl;
170 return false;
171 }
172
Mårten Kongstad0f763112018-11-19 14:14:37 +0100173 if (overlay_crc_ != *overlay_crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200174 out_error << base::StringPrintf(
175 "error: bad overlay crc: idmap version 0x%08x, file system version 0x%08x",
Mårten Kongstad0f763112018-11-19 14:14:37 +0100176 overlay_crc_, *overlay_crc)
Mårten Kongstad02751232018-04-27 13:16:32 +0200177 << std::endl;
178 return false;
179 }
180
181 return true;
182}
183
184std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) {
185 std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header());
186
187 uint16_t target_package_id16;
188 if (!Read16(stream, &target_package_id16) || !Read16(stream, &idmap_data_header->type_count_)) {
189 return nullptr;
190 }
191 idmap_data_header->target_package_id_ = target_package_id16;
192
193 return std::move(idmap_data_header);
194}
195
196std::unique_ptr<const IdmapData::TypeEntry> IdmapData::TypeEntry::FromBinaryStream(
197 std::istream& stream) {
198 std::unique_ptr<IdmapData::TypeEntry> data(new IdmapData::TypeEntry());
Mårten Kongstadb8779022018-11-29 09:53:17 +0100199 uint16_t target_type16;
200 uint16_t overlay_type16;
201 uint16_t entry_count;
Mårten Kongstad02751232018-04-27 13:16:32 +0200202 if (!Read16(stream, &target_type16) || !Read16(stream, &overlay_type16) ||
203 !Read16(stream, &entry_count) || !Read16(stream, &data->entry_offset_)) {
204 return nullptr;
205 }
206 data->target_type_id_ = target_type16;
207 data->overlay_type_id_ = overlay_type16;
208 for (uint16_t i = 0; i < entry_count; i++) {
209 ResourceId resid;
210 if (!Read32(stream, &resid)) {
211 return nullptr;
212 }
213 data->entries_.push_back(resid);
214 }
215
216 return std::move(data);
217}
218
219std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
220 std::unique_ptr<IdmapData> data(new IdmapData());
221 data->header_ = IdmapData::Header::FromBinaryStream(stream);
222 if (!data->header_) {
223 return nullptr;
224 }
225 for (size_t type_count = 0; type_count < data->header_->GetTypeCount(); type_count++) {
226 std::unique_ptr<const TypeEntry> type = IdmapData::TypeEntry::FromBinaryStream(stream);
227 if (!type) {
228 return nullptr;
229 }
230 data->type_entries_.push_back(std::move(type));
231 }
232 return std::move(data);
233}
234
235std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
236 const std::string& absolute_apk_path) {
237 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
238 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
239 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
240 replace(copy.begin(), copy.end(), '/', '@');
241 return absolute_dir + "/" + copy + "@idmap";
242}
243
244std::unique_ptr<const Idmap> Idmap::FromBinaryStream(std::istream& stream,
245 std::ostream& out_error) {
246 std::unique_ptr<Idmap> idmap(new Idmap());
247
248 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
249 if (!idmap->header_) {
250 out_error << "error: failed to parse idmap header" << std::endl;
251 return nullptr;
252 }
253
254 // idmap version 0x01 does not specify the number of data blocks that follow
255 // the idmap header; assume exactly one data block
256 for (int i = 0; i < 1; i++) {
257 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
258 if (!data) {
259 out_error << "error: failed to parse data block " << i << std::endl;
260 return nullptr;
261 }
262 idmap->data_.push_back(std::move(data));
263 }
264
265 return std::move(idmap);
266}
267
268std::unique_ptr<const Idmap> Idmap::FromApkAssets(const std::string& target_apk_path,
269 const ApkAssets& target_apk_assets,
270 const std::string& overlay_apk_path,
271 const ApkAssets& overlay_apk_assets,
272 std::ostream& out_error) {
273 AssetManager2 target_asset_manager;
274 if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true, false)) {
275 out_error << "error: failed to create target asset manager" << std::endl;
276 return nullptr;
277 }
278
279 AssetManager2 overlay_asset_manager;
280 if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true, false)) {
281 out_error << "error: failed to create overlay asset manager" << std::endl;
282 return nullptr;
283 }
284
285 const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100286 if (target_arsc == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200287 out_error << "error: failed to load target resources.arsc" << std::endl;
288 return nullptr;
289 }
290
291 const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
Mårten Kongstadb8779022018-11-29 09:53:17 +0100292 if (overlay_arsc == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200293 out_error << "error: failed to load overlay resources.arsc" << std::endl;
294 return nullptr;
295 }
296
297 const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100298 if (target_pkg == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200299 out_error << "error: failed to load target package from resources.arsc" << std::endl;
300 return nullptr;
301 }
302
303 const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100304 if (overlay_pkg == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200305 out_error << "error: failed to load overlay package from resources.arsc" << std::endl;
306 return nullptr;
307 }
308
309 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
310 if (!target_zip) {
311 out_error << "error: failed to open target as zip" << std::endl;
312 return nullptr;
313 }
314
315 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
316 if (!overlay_zip) {
317 out_error << "error: failed to open overlay as zip" << std::endl;
318 return nullptr;
319 }
320
321 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
322 header->magic_ = kIdmapMagic;
323 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100324
325 Result<uint32_t> crc = target_zip->Crc("resources.arsc");
326 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200327 out_error << "error: failed to get zip crc for target" << std::endl;
328 return nullptr;
329 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100330 header->target_crc_ = *crc;
331
332 crc = overlay_zip->Crc("resources.arsc");
333 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200334 out_error << "error: failed to get zip crc for overlay" << std::endl;
335 return nullptr;
336 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100337 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200338
339 if (target_apk_path.size() > sizeof(header->target_path_)) {
340 out_error << "error: target apk path \"" << target_apk_path << "\" longer that maximum size "
341 << sizeof(header->target_path_) << std::endl;
342 return nullptr;
343 }
344 memset(header->target_path_, 0, sizeof(header->target_path_));
345 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
346
347 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
348 out_error << "error: overlay apk path \"" << overlay_apk_path << "\" longer that maximum size "
349 << sizeof(header->overlay_path_) << std::endl;
350 return nullptr;
351 }
352 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
353 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
354
355 std::unique_ptr<Idmap> idmap(new Idmap());
356 idmap->header_ = std::move(header);
357
358 // find the resources that exist in both packages
359 MatchingResources matching_resources;
360 const auto end = overlay_pkg->end();
361 for (auto iter = overlay_pkg->begin(); iter != end; ++iter) {
362 const ResourceId overlay_resid = *iter;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100363 Result<std::string> name = utils::ResToTypeEntryName(overlay_asset_manager, overlay_resid);
364 if (!name) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200365 continue;
366 }
367 // prepend "<package>:" to turn name into "<package>:<type>/<name>"
Mårten Kongstad0f763112018-11-19 14:14:37 +0100368 const std::string full_name =
369 base::StringPrintf("%s:%s", target_pkg->GetPackageName().c_str(), name->c_str());
370 const ResourceId target_resid = NameToResid(target_asset_manager, full_name);
Mårten Kongstad02751232018-04-27 13:16:32 +0200371 if (target_resid == 0) {
372 continue;
373 }
374 matching_resources.Add(target_resid, overlay_resid);
375 }
376
377 // encode idmap data
378 std::unique_ptr<IdmapData> data(new IdmapData());
379 const auto types_end = matching_resources.map.cend();
380 for (auto ti = matching_resources.map.cbegin(); ti != types_end; ++ti) {
381 auto ei = ti->second.cbegin();
382 std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry());
383 type->target_type_id_ = EXTRACT_TYPE(ei->first);
384 type->overlay_type_id_ = EXTRACT_TYPE(ei->second);
385 type->entry_offset_ = EXTRACT_ENTRY(ei->first);
386 EntryId last_target_entry = kNoEntry;
387 for (; ei != ti->second.cend(); ++ei) {
388 if (last_target_entry != kNoEntry) {
389 int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1;
390 type->entries_.insert(type->entries_.end(), count, kNoEntry);
391 }
392 type->entries_.push_back(EXTRACT_ENTRY(ei->second));
393 last_target_entry = EXTRACT_ENTRY(ei->first);
394 }
395 data->type_entries_.push_back(std::move(type));
396 }
397
398 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
399 data_header->target_package_id_ = target_pkg->GetPackageId();
400 data_header->type_count_ = data->type_entries_.size();
401 data->header_ = std::move(data_header);
402
403 idmap->data_.push_back(std::move(data));
404
405 return std::move(idmap);
406}
407
408void IdmapHeader::accept(Visitor* v) const {
409 assert(v != nullptr);
410 v->visit(*this);
411}
412
413void IdmapData::Header::accept(Visitor* v) const {
414 assert(v != nullptr);
415 v->visit(*this);
416}
417
418void IdmapData::TypeEntry::accept(Visitor* v) const {
419 assert(v != nullptr);
420 v->visit(*this);
421}
422
423void IdmapData::accept(Visitor* v) const {
424 assert(v != nullptr);
425 v->visit(*this);
426 header_->accept(v);
427 auto end = type_entries_.cend();
428 for (auto iter = type_entries_.cbegin(); iter != end; ++iter) {
429 (*iter)->accept(v);
430 }
431}
432
433void Idmap::accept(Visitor* v) const {
434 assert(v != nullptr);
435 v->visit(*this);
436 header_->accept(v);
437 auto end = data_.cend();
438 for (auto iter = data_.cbegin(); iter != end; ++iter) {
439 (*iter)->accept(v);
440 }
441}
442
443} // namespace idmap2
444} // namespace android