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 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2/Idmap.h" |
| 18 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <iostream> |
| 21 | #include <iterator> |
| 22 | #include <limits> |
| 23 | #include <map> |
| 24 | #include <memory> |
| 25 | #include <set> |
| 26 | #include <string> |
| 27 | #include <utility> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "android-base/macros.h" |
| 31 | #include "android-base/stringprintf.h" |
| 32 | #include "androidfw/AssetManager2.h" |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 33 | #include "idmap2/ResourceMapping.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 34 | #include "idmap2/ResourceUtils.h" |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 35 | #include "idmap2/Result.h" |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 36 | #include "idmap2/SysTrace.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 37 | #include "idmap2/ZipFile.h" |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 38 | #include "utils/String16.h" |
| 39 | #include "utils/String8.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 40 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 41 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 42 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 43 | namespace { |
| 44 | |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 45 | class MatchingResources { |
| 46 | public: |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 47 | void Add(ResourceId target_resid, ResourceId overlay_resid) { |
| 48 | TypeId target_typeid = EXTRACT_TYPE(target_resid); |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 49 | if (map_.find(target_typeid) == map_.end()) { |
| 50 | map_.emplace(target_typeid, std::set<std::pair<ResourceId, ResourceId>>()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 51 | } |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 52 | map_[target_typeid].insert(std::make_pair(target_resid, overlay_resid)); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Yi Kong | 974d516 | 2019-03-06 16:18:11 -0800 | [diff] [blame] | 55 | inline const std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>>& WARN_UNUSED |
Mårten Kongstad | cf62249 | 2019-03-19 22:40:58 +0100 | [diff] [blame] | 56 | Map() const { |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 57 | return map_; |
| 58 | } |
| 59 | |
| 60 | private: |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 61 | // target type id -> set { pair { overlay entry id, overlay entry id } } |
Mårten Kongstad | cf28136 | 2018-11-28 19:32:25 +0100 | [diff] [blame] | 62 | std::map<TypeId, std::set<std::pair<ResourceId, ResourceId>>> map_; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 65 | bool WARN_UNUSED Read16(std::istream& stream, uint16_t* out) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 74 | bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 84 | bool WARN_UNUSED ReadString(std::istream& stream, char out[kIdmapStringLength]) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 97 | Result<uint32_t> GetCrc(const ZipFile& zip) { |
| 98 | const Result<uint32_t> a = zip.Crc("resources.arsc"); |
| 99 | const Result<uint32_t> b = zip.Crc("AndroidManifest.xml"); |
Mårten Kongstad | 49d835d | 2019-01-31 10:50:48 +0100 | [diff] [blame] | 100 | return a && b |
| 101 | ? Result<uint32_t>(*a ^ *b) |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 102 | : Error("failed to get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc"); |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Mårten Kongstad | 744ccfe | 2018-12-20 14:56:14 +0100 | [diff] [blame] | 105 | } // namespace |
| 106 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 107 | std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& stream) { |
| 108 | std::unique_ptr<IdmapHeader> idmap_header(new IdmapHeader()); |
| 109 | |
| 110 | if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) || |
| 111 | !Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) || |
| 112 | !ReadString(stream, idmap_header->target_path_) || |
| 113 | !ReadString(stream, idmap_header->overlay_path_)) { |
| 114 | return nullptr; |
| 115 | } |
| 116 | |
| 117 | return std::move(idmap_header); |
| 118 | } |
| 119 | |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 120 | Result<Unit> IdmapHeader::IsUpToDate() const { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 121 | if (magic_ != kIdmapMagic) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 122 | return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (version_ != kIdmapCurrentVersion) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 126 | return Error("bad version: actual 0x%08x, expected 0x%08x", version_, kIdmapCurrentVersion); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path_); |
| 130 | if (!target_zip) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 131 | return Error("failed to open target %s", GetTargetPath().to_string().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 134 | Result<uint32_t> target_crc = GetCrc(*target_zip); |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 135 | if (!target_crc) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 136 | return Error("failed to get target crc"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 139 | if (target_crc_ != *target_crc) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 140 | return Error("bad target crc: idmap version 0x%08x, file system version 0x%08x", target_crc_, |
| 141 | *target_crc); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path_); |
| 145 | if (!overlay_zip) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 146 | return Error("failed to open overlay %s", GetOverlayPath().to_string().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 149 | Result<uint32_t> overlay_crc = GetCrc(*overlay_zip); |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 150 | if (!overlay_crc) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 151 | return Error("failed to get overlay crc"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 154 | if (overlay_crc_ != *overlay_crc) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 155 | return Error("bad overlay crc: idmap version 0x%08x, file system version 0x%08x", overlay_crc_, |
| 156 | *overlay_crc); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 159 | return Unit{}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | std::unique_ptr<const IdmapData::Header> IdmapData::Header::FromBinaryStream(std::istream& stream) { |
| 163 | std::unique_ptr<IdmapData::Header> idmap_data_header(new IdmapData::Header()); |
| 164 | |
| 165 | uint16_t target_package_id16; |
| 166 | if (!Read16(stream, &target_package_id16) || !Read16(stream, &idmap_data_header->type_count_)) { |
| 167 | return nullptr; |
| 168 | } |
| 169 | idmap_data_header->target_package_id_ = target_package_id16; |
| 170 | |
| 171 | return std::move(idmap_data_header); |
| 172 | } |
| 173 | |
| 174 | std::unique_ptr<const IdmapData::TypeEntry> IdmapData::TypeEntry::FromBinaryStream( |
| 175 | std::istream& stream) { |
| 176 | std::unique_ptr<IdmapData::TypeEntry> data(new IdmapData::TypeEntry()); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 177 | uint16_t target_type16; |
| 178 | uint16_t overlay_type16; |
| 179 | uint16_t entry_count; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 180 | if (!Read16(stream, &target_type16) || !Read16(stream, &overlay_type16) || |
| 181 | !Read16(stream, &entry_count) || !Read16(stream, &data->entry_offset_)) { |
| 182 | return nullptr; |
| 183 | } |
| 184 | data->target_type_id_ = target_type16; |
| 185 | data->overlay_type_id_ = overlay_type16; |
| 186 | for (uint16_t i = 0; i < entry_count; i++) { |
| 187 | ResourceId resid; |
| 188 | if (!Read32(stream, &resid)) { |
| 189 | return nullptr; |
| 190 | } |
| 191 | data->entries_.push_back(resid); |
| 192 | } |
| 193 | |
| 194 | return std::move(data); |
| 195 | } |
| 196 | |
| 197 | std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& stream) { |
| 198 | std::unique_ptr<IdmapData> data(new IdmapData()); |
| 199 | data->header_ = IdmapData::Header::FromBinaryStream(stream); |
| 200 | if (!data->header_) { |
| 201 | return nullptr; |
| 202 | } |
| 203 | for (size_t type_count = 0; type_count < data->header_->GetTypeCount(); type_count++) { |
| 204 | std::unique_ptr<const TypeEntry> type = IdmapData::TypeEntry::FromBinaryStream(stream); |
| 205 | if (!type) { |
| 206 | return nullptr; |
| 207 | } |
| 208 | data->type_entries_.push_back(std::move(type)); |
| 209 | } |
| 210 | return std::move(data); |
| 211 | } |
| 212 | |
| 213 | std::string Idmap::CanonicalIdmapPathFor(const std::string& absolute_dir, |
| 214 | const std::string& absolute_apk_path) { |
| 215 | assert(absolute_dir.size() > 0 && absolute_dir[0] == "/"); |
| 216 | assert(absolute_apk_path.size() > 0 && absolute_apk_path[0] == "/"); |
| 217 | std::string copy(++absolute_apk_path.cbegin(), absolute_apk_path.cend()); |
| 218 | replace(copy.begin(), copy.end(), '/', '@'); |
| 219 | return absolute_dir + "/" + copy + "@idmap"; |
| 220 | } |
| 221 | |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 222 | Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) { |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 223 | SYSTRACE << "Idmap::FromBinaryStream"; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 224 | std::unique_ptr<Idmap> idmap(new Idmap()); |
| 225 | |
| 226 | idmap->header_ = IdmapHeader::FromBinaryStream(stream); |
| 227 | if (!idmap->header_) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 228 | return Error("failed to parse idmap header"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // idmap version 0x01 does not specify the number of data blocks that follow |
| 232 | // the idmap header; assume exactly one data block |
| 233 | for (int i = 0; i < 1; i++) { |
| 234 | std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream); |
| 235 | if (!data) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 236 | return Error("failed to parse data block %d", i); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 237 | } |
| 238 | idmap->data_.push_back(std::move(data)); |
| 239 | } |
| 240 | |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 241 | return {std::move(idmap)}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 244 | Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping( |
| 245 | const ResourceMapping& resource_mapping) { |
| 246 | if (resource_mapping.GetTargetToOverlayMap().empty()) { |
| 247 | return Error("no resources were overlaid"); |
| 248 | } |
| 249 | |
| 250 | MatchingResources matching_resources; |
| 251 | for (const auto mapping : resource_mapping.GetTargetToOverlayMap()) { |
| 252 | if (mapping.second.data_type != Res_value::TYPE_REFERENCE) { |
| 253 | // The idmap format must change to support non-references. |
| 254 | continue; |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 255 | } |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 256 | |
| 257 | matching_resources.Add(mapping.first, mapping.second.data_value); |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 260 | // encode idmap data |
| 261 | std::unique_ptr<IdmapData> data(new IdmapData()); |
| 262 | const auto types_end = matching_resources.Map().cend(); |
| 263 | for (auto ti = matching_resources.Map().cbegin(); ti != types_end; ++ti) { |
| 264 | auto ei = ti->second.cbegin(); |
| 265 | std::unique_ptr<IdmapData::TypeEntry> type(new IdmapData::TypeEntry()); |
| 266 | type->target_type_id_ = EXTRACT_TYPE(ei->first); |
| 267 | type->overlay_type_id_ = EXTRACT_TYPE(ei->second); |
| 268 | type->entry_offset_ = EXTRACT_ENTRY(ei->first); |
| 269 | EntryId last_target_entry = kNoEntry; |
| 270 | for (; ei != ti->second.cend(); ++ei) { |
| 271 | if (last_target_entry != kNoEntry) { |
| 272 | int count = EXTRACT_ENTRY(ei->first) - last_target_entry - 1; |
| 273 | type->entries_.insert(type->entries_.end(), count, kNoEntry); |
| 274 | } |
| 275 | type->entries_.push_back(EXTRACT_ENTRY(ei->second)); |
| 276 | last_target_entry = EXTRACT_ENTRY(ei->first); |
| 277 | } |
| 278 | data->type_entries_.push_back(std::move(type)); |
| 279 | } |
| 280 | |
| 281 | std::unique_ptr<IdmapData::Header> data_header(new IdmapData::Header()); |
| 282 | data_header->target_package_id_ = resource_mapping.GetTargetPackageId(); |
| 283 | data_header->type_count_ = data->type_entries_.size(); |
| 284 | data->header_ = std::move(data_header); |
| 285 | return {std::move(data)}; |
Ryan Mitchell | 4c09a4a | 2019-03-08 08:57:48 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 288 | Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const ApkAssets& target_apk_assets, |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 289 | const ApkAssets& overlay_apk_assets, |
| 290 | const PolicyBitmask& fulfilled_policies, |
| 291 | bool enforce_overlayable) { |
Mårten Kongstad | 4cbb007 | 2018-11-30 16:22:05 +0100 | [diff] [blame] | 292 | SYSTRACE << "Idmap::FromApkAssets"; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 293 | const std::string& target_apk_path = target_apk_assets.GetPath(); |
| 294 | const std::string& overlay_apk_path = overlay_apk_assets.GetPath(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 295 | |
| 296 | const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path); |
| 297 | if (!target_zip) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 298 | return Error("failed to open target as zip"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path); |
| 302 | if (!overlay_zip) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 303 | return Error("failed to open overlay as zip"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | std::unique_ptr<IdmapHeader> header(new IdmapHeader()); |
| 307 | header->magic_ = kIdmapMagic; |
| 308 | header->version_ = kIdmapCurrentVersion; |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 309 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 310 | Result<uint32_t> crc = GetCrc(*target_zip); |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 311 | if (!crc) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 312 | return Error(crc.GetError(), "failed to get zip CRC for target"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 313 | } |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 314 | header->target_crc_ = *crc; |
| 315 | |
Mårten Kongstad | 9371dc1 | 2019-01-22 14:35:12 +0100 | [diff] [blame] | 316 | crc = GetCrc(*overlay_zip); |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 317 | if (!crc) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 318 | return Error(crc.GetError(), "failed to get zip CRC for overlay"); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 319 | } |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 320 | header->overlay_crc_ = *crc; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 321 | |
| 322 | if (target_apk_path.size() > sizeof(header->target_path_)) { |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 323 | return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(), |
| 324 | sizeof(header->target_path_)); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 325 | } |
| 326 | memset(header->target_path_, 0, sizeof(header->target_path_)); |
| 327 | memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size()); |
| 328 | |
| 329 | if (overlay_apk_path.size() > sizeof(header->overlay_path_)) { |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 330 | return Error("overlay apk path \"%s\" longer than maximum size %zu", overlay_apk_path.c_str(), |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 331 | sizeof(header->target_path_)); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 332 | } |
| 333 | memset(header->overlay_path_, 0, sizeof(header->overlay_path_)); |
| 334 | memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size()); |
| 335 | |
| 336 | std::unique_ptr<Idmap> idmap(new Idmap()); |
| 337 | idmap->header_ = std::move(header); |
| 338 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 339 | auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path); |
| 340 | if (!overlay_info) { |
| 341 | return overlay_info.GetError(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 344 | auto resource_mapping = |
| 345 | ResourceMapping::FromApkAssets(target_apk_assets, overlay_apk_assets, *overlay_info, |
| 346 | fulfilled_policies, enforce_overlayable); |
| 347 | if (!resource_mapping) { |
| 348 | return resource_mapping.GetError(); |
Ryan Mitchell | b863ca3 | 2019-03-07 14:31:54 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 351 | auto idmap_data = IdmapData::FromResourceMapping(*resource_mapping); |
| 352 | if (!idmap_data) { |
| 353 | return idmap_data.GetError(); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame^] | 356 | idmap->data_.push_back(std::move(*idmap_data)); |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 357 | return {std::move(idmap)}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void IdmapHeader::accept(Visitor* v) const { |
| 361 | assert(v != nullptr); |
| 362 | v->visit(*this); |
| 363 | } |
| 364 | |
| 365 | void IdmapData::Header::accept(Visitor* v) const { |
| 366 | assert(v != nullptr); |
| 367 | v->visit(*this); |
| 368 | } |
| 369 | |
| 370 | void IdmapData::TypeEntry::accept(Visitor* v) const { |
| 371 | assert(v != nullptr); |
| 372 | v->visit(*this); |
| 373 | } |
| 374 | |
| 375 | void IdmapData::accept(Visitor* v) const { |
| 376 | assert(v != nullptr); |
| 377 | v->visit(*this); |
| 378 | header_->accept(v); |
| 379 | auto end = type_entries_.cend(); |
| 380 | for (auto iter = type_entries_.cbegin(); iter != end; ++iter) { |
| 381 | (*iter)->accept(v); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void Idmap::accept(Visitor* v) const { |
| 386 | assert(v != nullptr); |
| 387 | v->visit(*this); |
| 388 | header_->accept(v); |
| 389 | auto end = data_.cend(); |
| 390 | for (auto iter = data_.cbegin(); iter != end; ++iter) { |
| 391 | (*iter)->accept(v); |
| 392 | } |
| 393 | } |
| 394 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 395 | } // namespace android::idmap2 |