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 | /* |
| 18 | * # idmap file format (current version) |
| 19 | * |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 20 | * idmap := header data* |
| 21 | * header := magic version target_crc overlay_crc fulfilled_policies |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 22 | * enforce_overlayable target_path overlay_path overlay_name |
| 23 | * debug_info |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 24 | * data := data_header target_entry* target_inline_entry* overlay_entry* |
| 25 | * string_pool |
| 26 | * data_header := target_package_id overlay_package_id padding(2) target_entry_count |
| 27 | * target_inline_entry_count overlay_entry_count string_pool_index |
| 28 | * target_entry := target_id overlay_id |
| 29 | * target_inline_entry := target_id Res_value::size padding(1) Res_value::type |
| 30 | * Res_value::value |
| 31 | * overlay_entry := overlay_id target_id |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 32 | * |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 33 | * debug_info := string |
| 34 | * enforce_overlayable := <uint32_t> |
| 35 | * fulfilled_policies := <uint32_t> |
| 36 | * magic := <uint32_t> |
| 37 | * overlay_crc := <uint32_t> |
| 38 | * overlay_entry_count := <uint32_t> |
| 39 | * overlay_id := <uint32_t> |
| 40 | * overlay_package_id := <uint8_t> |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 41 | * overlay_name := string |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 42 | * overlay_path := string |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 43 | * padding(n) := <uint8_t>[n] |
| 44 | * Res_value::size := <uint16_t> |
| 45 | * Res_value::type := <uint8_t> |
| 46 | * Res_value::value := <uint32_t> |
| 47 | * string := <uint32_t> <uint8_t>+ padding(n) |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 48 | * string_pool := string |
| 49 | * string_pool_index := <uint32_t> |
| 50 | * string_pool_length := <uint32_t> |
| 51 | * target_crc := <uint32_t> |
| 52 | * target_entry_count := <uint32_t> |
| 53 | * target_inline_entry_count := <uint32_t> |
| 54 | * target_id := <uint32_t> |
| 55 | * target_package_id := <uint8_t> |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 56 | * target_path := string |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 57 | * value_type := <uint8_t> |
| 58 | * value_data := <uint32_t> |
| 59 | * version := <uint32_t> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | #ifndef IDMAP2_INCLUDE_IDMAP2_IDMAP_H_ |
| 63 | #define IDMAP2_INCLUDE_IDMAP2_IDMAP_H_ |
| 64 | |
| 65 | #include <iostream> |
| 66 | #include <memory> |
| 67 | #include <string> |
| 68 | #include <vector> |
| 69 | |
| 70 | #include "android-base/macros.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 71 | #include "androidfw/ApkAssets.h" |
| 72 | #include "androidfw/ResourceTypes.h" |
| 73 | #include "androidfw/StringPiece.h" |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 74 | #include "idmap2/ResourceMapping.h" |
Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame] | 75 | #include "idmap2/ZipFile.h" |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 76 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 77 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 78 | |
| 79 | class Idmap; |
| 80 | class Visitor; |
| 81 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 82 | // magic number: all idmap files start with this |
| 83 | static constexpr const uint32_t kIdmapMagic = android::kIdmapMagic; |
| 84 | |
| 85 | // current version of the idmap binary format; must be incremented when the format is changed |
| 86 | static constexpr const uint32_t kIdmapCurrentVersion = android::kIdmapCurrentVersion; |
| 87 | |
Ryan Mitchell | 7d53f19 | 2020-04-24 17:45:25 -0700 | [diff] [blame] | 88 | // Retrieves a crc generated using all of the files within the zip that can affect idmap generation. |
| 89 | Result<uint32_t> GetPackageCrc(const ZipFile& zip_info); |
| 90 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 91 | class IdmapHeader { |
| 92 | public: |
| 93 | static std::unique_ptr<const IdmapHeader> FromBinaryStream(std::istream& stream); |
| 94 | |
| 95 | inline uint32_t GetMagic() const { |
| 96 | return magic_; |
| 97 | } |
| 98 | |
| 99 | inline uint32_t GetVersion() const { |
| 100 | return version_; |
| 101 | } |
| 102 | |
| 103 | inline uint32_t GetTargetCrc() const { |
| 104 | return target_crc_; |
| 105 | } |
| 106 | |
| 107 | inline uint32_t GetOverlayCrc() const { |
| 108 | return overlay_crc_; |
| 109 | } |
| 110 | |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 111 | inline uint32_t GetFulfilledPolicies() const { |
| 112 | return fulfilled_policies_; |
| 113 | } |
| 114 | |
| 115 | bool GetEnforceOverlayable() const { |
| 116 | return enforce_overlayable_; |
| 117 | } |
| 118 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 119 | const std::string& GetTargetPath() const { |
| 120 | return target_path_; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 123 | const std::string& GetOverlayPath() const { |
| 124 | return overlay_path_; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 127 | const std::string& GetOverlayName() const { |
| 128 | return overlay_name_; |
| 129 | } |
| 130 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 131 | const std::string& GetDebugInfo() const { |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 132 | return debug_info_; |
| 133 | } |
| 134 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 135 | // Invariant: anytime the idmap data encoding is changed, the idmap version |
| 136 | // field *must* be incremented. Because of this, we know that if the idmap |
| 137 | // header is up-to-date the entire file is up-to-date. |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 138 | Result<Unit> IsUpToDate(const std::string& target_path, const std::string& overlay_path, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 139 | const std::string& overlay_name, PolicyBitmask fulfilled_policies, |
| 140 | bool enforce_overlayable) const; |
| 141 | |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 142 | Result<Unit> IsUpToDate(const std::string& target_path, const std::string& overlay_path, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 143 | const std::string& overlay_name, uint32_t target_crc, |
| 144 | uint32_t overlay_crc, PolicyBitmask fulfilled_policies, |
| 145 | bool enforce_overlayable) const; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 146 | |
| 147 | void accept(Visitor* v) const; |
| 148 | |
| 149 | private: |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 150 | IdmapHeader() = default; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 151 | |
| 152 | uint32_t magic_; |
| 153 | uint32_t version_; |
| 154 | uint32_t target_crc_; |
| 155 | uint32_t overlay_crc_; |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 156 | uint32_t fulfilled_policies_; |
| 157 | bool enforce_overlayable_; |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 158 | std::string target_path_; |
| 159 | std::string overlay_path_; |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 160 | std::string overlay_name_; |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 161 | std::string debug_info_; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 162 | |
| 163 | friend Idmap; |
| 164 | DISALLOW_COPY_AND_ASSIGN(IdmapHeader); |
| 165 | }; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 166 | class IdmapData { |
| 167 | public: |
| 168 | class Header { |
| 169 | public: |
| 170 | static std::unique_ptr<const Header> FromBinaryStream(std::istream& stream); |
| 171 | |
| 172 | inline PackageId GetTargetPackageId() const { |
| 173 | return target_package_id_; |
| 174 | } |
| 175 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 176 | inline PackageId GetOverlayPackageId() const { |
| 177 | return overlay_package_id_; |
| 178 | } |
| 179 | |
| 180 | inline uint32_t GetTargetEntryCount() const { |
| 181 | return target_entry_count; |
| 182 | } |
| 183 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 184 | inline uint32_t GetTargetInlineEntryCount() const { |
| 185 | return target_entry_inline_count; |
| 186 | } |
| 187 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 188 | inline uint32_t GetOverlayEntryCount() const { |
| 189 | return overlay_entry_count; |
| 190 | } |
| 191 | |
| 192 | inline uint32_t GetStringPoolIndexOffset() const { |
| 193 | return string_pool_index_offset; |
| 194 | } |
| 195 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 196 | void accept(Visitor* v) const; |
| 197 | |
| 198 | private: |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 199 | PackageId target_package_id_; |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 200 | PackageId overlay_package_id_; |
| 201 | uint32_t target_entry_count; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 202 | uint32_t target_entry_inline_count; |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 203 | uint32_t overlay_entry_count; |
| 204 | uint32_t string_pool_index_offset; |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 205 | Header() = default; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 206 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 207 | friend Idmap; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 208 | friend IdmapData; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 209 | DISALLOW_COPY_AND_ASSIGN(Header); |
| 210 | }; |
| 211 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 212 | struct TargetEntry { |
| 213 | ResourceId target_id; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 214 | ResourceId overlay_id; |
| 215 | }; |
| 216 | |
| 217 | struct TargetInlineEntry { |
| 218 | ResourceId target_id; |
| 219 | TargetValue value; |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 220 | }; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 221 | |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 222 | struct OverlayEntry { |
| 223 | ResourceId overlay_id; |
| 224 | ResourceId target_id; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | static std::unique_ptr<const IdmapData> FromBinaryStream(std::istream& stream); |
| 228 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 229 | static Result<std::unique_ptr<const IdmapData>> FromResourceMapping( |
| 230 | const ResourceMapping& resource_mapping); |
| 231 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 232 | const std::unique_ptr<const Header>& GetHeader() const { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 233 | return header_; |
| 234 | } |
| 235 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 236 | const std::vector<TargetEntry>& GetTargetEntries() const { |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 237 | return target_entries_; |
| 238 | } |
| 239 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 240 | const std::vector<TargetInlineEntry>& GetTargetInlineEntries() const { |
| 241 | return target_inline_entries_; |
| 242 | } |
| 243 | |
| 244 | const std::vector<OverlayEntry>& GetOverlayEntries() const { |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 245 | return overlay_entries_; |
| 246 | } |
| 247 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 248 | const std::string& GetStringPoolData() const { |
| 249 | return string_pool_data_; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void accept(Visitor* v) const; |
| 253 | |
| 254 | private: |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 255 | IdmapData() = default; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 256 | |
| 257 | std::unique_ptr<const Header> header_; |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 258 | std::vector<TargetEntry> target_entries_; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 259 | std::vector<TargetInlineEntry> target_inline_entries_; |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 260 | std::vector<OverlayEntry> overlay_entries_; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 261 | std::string string_pool_data_; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 262 | |
| 263 | friend Idmap; |
| 264 | DISALLOW_COPY_AND_ASSIGN(IdmapData); |
| 265 | }; |
| 266 | |
| 267 | class Idmap { |
| 268 | public: |
| 269 | static std::string CanonicalIdmapPathFor(const std::string& absolute_dir, |
| 270 | const std::string& absolute_apk_path); |
| 271 | |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 272 | static Result<std::unique_ptr<const Idmap>> FromBinaryStream(std::istream& stream); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 273 | |
| 274 | // In the current version of idmap, the first package in each resources.arsc |
| 275 | // file is used; change this in the next version of idmap to use a named |
| 276 | // package instead; also update FromApkAssets to take additional parameters: |
| 277 | // the target and overlay package names |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 278 | static Result<std::unique_ptr<const Idmap>> FromApkAssets(const ApkAssets& target_apk_assets, |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 279 | const ApkAssets& overlay_apk_assets, |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 280 | const std::string& overlay_name, |
Mårten Kongstad | ce42490 | 2019-03-01 08:35:37 +0100 | [diff] [blame] | 281 | const PolicyBitmask& fulfilled_policies, |
| 282 | bool enforce_overlayable); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 283 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 284 | const std::unique_ptr<const IdmapHeader>& GetHeader() const { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 285 | return header_; |
| 286 | } |
| 287 | |
Ryan Mitchell | 30dc2e0 | 2020-12-02 11:43:18 -0800 | [diff] [blame^] | 288 | const std::vector<std::unique_ptr<const IdmapData>>& GetData() const { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 289 | return data_; |
| 290 | } |
| 291 | |
| 292 | void accept(Visitor* v) const; |
| 293 | |
| 294 | private: |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 295 | Idmap() = default; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 296 | |
| 297 | std::unique_ptr<const IdmapHeader> header_; |
| 298 | std::vector<std::unique_ptr<const IdmapData>> data_; |
| 299 | |
| 300 | DISALLOW_COPY_AND_ASSIGN(Idmap); |
| 301 | }; |
| 302 | |
| 303 | class Visitor { |
| 304 | public: |
Ryan Mitchell | 0699f1d | 2020-12-03 15:41:42 -0800 | [diff] [blame] | 305 | virtual ~Visitor() = default; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 306 | virtual void visit(const Idmap& idmap) = 0; |
| 307 | virtual void visit(const IdmapHeader& header) = 0; |
| 308 | virtual void visit(const IdmapData& data) = 0; |
| 309 | virtual void visit(const IdmapData::Header& header) = 0; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 310 | }; |
| 311 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 312 | inline size_t CalculatePadding(size_t data_length) { |
| 313 | return (4 - (data_length % 4)) % 4; |
| 314 | } |
| 315 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 316 | } // namespace android::idmap2 |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 317 | |
| 318 | #endif // IDMAP2_INCLUDE_IDMAP2_IDMAP_H_ |