blob: 1ef326793cb4ac1c7543eb4870538c7ed56bd513 [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());
199
200 uint16_t target_type16, overlay_type16, entry_count;
201 if (!Read16(stream, &target_type16) || !Read16(stream, &overlay_type16) ||
202 !Read16(stream, &entry_count) || !Read16(stream, &data->entry_offset_)) {
203 return nullptr;
204 }
205 data->target_type_id_ = target_type16;
206 data->overlay_type_id_ = overlay_type16;
207 for (uint16_t i = 0; i < entry_count; i++) {
208 ResourceId resid;
209 if (!Read32(stream, &resid)) {
210 return nullptr;
211 }
212 data->entries_.push_back(resid);
213 }
214
215 return std::move(data);
216}
217
218std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) {
219 std::unique_ptr<IdmapData> data(new IdmapData());
220 data->header_ = IdmapData::Header::FromBinaryStream(stream);
221 if (!data->header_) {
222 return nullptr;
223 }
224 for (size_t type_count = 0; type_count < data->header_->GetTypeCount(); type_count++) {
225 std::unique_ptr<const TypeEntry> type = IdmapData::TypeEntry::FromBinaryStream(stream);
226 if (!type) {
227 return nullptr;
228 }
229 data->type_entries_.push_back(std::move(type));
230 }
231 return std::move(data);
232}
233
234std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir,
235 const std::string& absolute_apk_path) {
236 assert(absolute_dir.size() > 0 && absolute_dir[0] == "/");
237 assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/");
238 std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend());
239 replace(copy.begin(), copy.end(), '/', '@');
240 return absolute_dir + "/" + copy + "@idmap";
241}
242
243std::unique_ptr<const Idmap> Idmap::FromBinaryStream(std::istream& stream,
244 std::ostream& out_error) {
245 std::unique_ptr<Idmap> idmap(new Idmap());
246
247 idmap->header_ = IdmapHeader::FromBinaryStream(stream);
248 if (!idmap->header_) {
249 out_error << "error: failed to parse idmap header" << std::endl;
250 return nullptr;
251 }
252
253 // idmap version 0x01 does not specify the number of data blocks that follow
254 // the idmap header; assume exactly one data block
255 for (int i = 0; i < 1; i++) {
256 std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
257 if (!data) {
258 out_error << "error: failed to parse data block " << i << std::endl;
259 return nullptr;
260 }
261 idmap->data_.push_back(std::move(data));
262 }
263
264 return std::move(idmap);
265}
266
267std::unique_ptr<const Idmap> Idmap::FromApkAssets(const std::string& target_apk_path,
268 const ApkAssets& target_apk_assets,
269 const std::string& overlay_apk_path,
270 const ApkAssets& overlay_apk_assets,
271 std::ostream& out_error) {
272 AssetManager2 target_asset_manager;
273 if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true, false)) {
274 out_error << "error: failed to create target asset manager" << std::endl;
275 return nullptr;
276 }
277
278 AssetManager2 overlay_asset_manager;
279 if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true, false)) {
280 out_error << "error: failed to create overlay asset manager" << std::endl;
281 return nullptr;
282 }
283
284 const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
285 if (!target_arsc) {
286 out_error << "error: failed to load target resources.arsc" << std::endl;
287 return nullptr;
288 }
289
290 const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
291 if (!overlay_arsc) {
292 out_error << "error: failed to load overlay resources.arsc" << std::endl;
293 return nullptr;
294 }
295
296 const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
297 if (!target_pkg) {
298 out_error << "error: failed to load target package from resources.arsc" << std::endl;
299 return nullptr;
300 }
301
302 const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
303 if (!overlay_pkg) {
304 out_error << "error: failed to load overlay package from resources.arsc" << std::endl;
305 return nullptr;
306 }
307
308 const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
309 if (!target_zip) {
310 out_error << "error: failed to open target as zip" << std::endl;
311 return nullptr;
312 }
313
314 const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
315 if (!overlay_zip) {
316 out_error << "error: failed to open overlay as zip" << std::endl;
317 return nullptr;
318 }
319
320 std::unique_ptr<IdmapHeader> header(new IdmapHeader());
321 header->magic_ = kIdmapMagic;
322 header->version_ = kIdmapCurrentVersion;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100323
324 Result<uint32_t> crc = target_zip->Crc("resources.arsc");
325 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200326 out_error << "error: failed to get zip crc for target" << std::endl;
327 return nullptr;
328 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100329 header->target_crc_ = *crc;
330
331 crc = overlay_zip->Crc("resources.arsc");
332 if (!crc) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200333 out_error << "error: failed to get zip crc for overlay" << std::endl;
334 return nullptr;
335 }
Mårten Kongstad0f763112018-11-19 14:14:37 +0100336 header->overlay_crc_ = *crc;
Mårten Kongstad02751232018-04-27 13:16:32 +0200337
338 if (target_apk_path.size() > sizeof(header->target_path_)) {
339 out_error << "error: target apk path \"" << target_apk_path << "\" longer that maximum size "
340 << sizeof(header->target_path_) << std::endl;
341 return nullptr;
342 }
343 memset(header->target_path_, 0, sizeof(header->target_path_));
344 memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
345
346 if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
347 out_error << "error: overlay apk path \"" << overlay_apk_path << "\" longer that maximum size "
348 << sizeof(header->overlay_path_) << std::endl;
349 return nullptr;
350 }
351 memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
352 memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
353
354 std::unique_ptr<Idmap> idmap(new Idmap());
355 idmap->header_ = std::move(header);
356
357 // find the resources that exist in both packages
358 MatchingResources matching_resources;
359 const auto end = overlay_pkg->end();
360 for (auto iter = overlay_pkg->begin(); iter != end; ++iter) {
361 const ResourceId overlay_resid = *iter;
Mårten Kongstad0f763112018-11-19 14:14:37 +0100362 Result<std::string> name = utils::ResToTypeEntryName(overlay_asset_manager, overlay_resid);
363 if (!name) {
Mårten Kongstad02751232018-04-27 13:16:32 +0200364 continue;
365 }
366 // prepend "<package>:" to turn name into "<package>:<type>/<name>"
Mårten Kongstad0f763112018-11-19 14:14:37 +0100367 const std::string full_name =
368 base::StringPrintf("%s:%s", target_pkg->GetPackageName().c_str(), name->c_str());
369 const ResourceId target_resid = NameToResid(target_asset_manager, full_name);
Mårten Kongstad02751232018-04-27 13:16:32 +0200370 if (target_resid == 0) {
371 continue;
372 }
373 matching_resources.Add(target_resid, overlay_resid);
374 }
375
376 // encode idmap data
377 std::unique_ptr<IdmapData> data(new IdmapData());
378 const auto types_end = matching_resources.map.cend();
379 for (auto ti = matching_resources.map.cbegin(); ti != types_end; ++ti) {
380 auto ei = ti->second.cbegin();
381 std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry());
382 type->target_type_id_ = EXTRACT_TYPE(ei->first);
383 type->overlay_type_id_ = EXTRACT_TYPE(ei->second);
384 type->entry_offset_ = EXTRACT_ENTRY(ei->first);
385 EntryId last_target_entry = kNoEntry;
386 for (; ei != ti->second.cend(); ++ei) {
387 if (last_target_entry != kNoEntry) {
388 int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1;
389 type->entries_.insert(type->entries_.end(), count, kNoEntry);
390 }
391 type->entries_.push_back(EXTRACT_ENTRY(ei->second));
392 last_target_entry = EXTRACT_ENTRY(ei->first);
393 }
394 data->type_entries_.push_back(std::move(type));
395 }
396
397 std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header());
398 data_header->target_package_id_ = target_pkg->GetPackageId();
399 data_header->type_count_ = data->type_entries_.size();
400 data->header_ = std::move(data_header);
401
402 idmap->data_.push_back(std::move(data));
403
404 return std::move(idmap);
405}
406
407void IdmapHeader::accept(Visitor* v) const {
408 assert(v != nullptr);
409 v->visit(*this);
410}
411
412void IdmapData::Header::accept(Visitor* v) const {
413 assert(v != nullptr);
414 v->visit(*this);
415}
416
417void IdmapData::TypeEntry::accept(Visitor* v) const {
418 assert(v != nullptr);
419 v->visit(*this);
420}
421
422void IdmapData::accept(Visitor* v) const {
423 assert(v != nullptr);
424 v->visit(*this);
425 header_->accept(v);
426 auto end = type_entries_.cend();
427 for (auto iter = type_entries_.cbegin(); iter != end; ++iter) {
428 (*iter)->accept(v);
429 }
430}
431
432void Idmap::accept(Visitor* v) const {
433 assert(v != nullptr);
434 v->visit(*this);
435 header_->accept(v);
436 auto end = data_.cend();
437 for (auto iter = data_.cbegin(); iter != end; ++iter) {
438 (*iter)->accept(v);
439 }
440}
441
442} // namespace idmap2
443} // namespace android