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