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