Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 17 | #include "androidfw/ApkAssets.h" |
| 18 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 19 | #include "android-base/errors.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 20 | #include "android-base/logging.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 24 | using base::SystemErrorCodeToString; |
| 25 | using base::unique_fd; |
| 26 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 27 | constexpr const char* kResourcesArsc = "resources.arsc"; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 28 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 29 | ApkAssets::ApkAssets(std::unique_ptr<Asset> resources_asset, |
| 30 | std::unique_ptr<LoadedArsc> loaded_arsc, |
| 31 | std::unique_ptr<AssetsProvider> assets, |
| 32 | package_property_t property_flags, |
| 33 | std::unique_ptr<Asset> idmap_asset, |
| 34 | std::unique_ptr<LoadedIdmap> loaded_idmap) |
| 35 | : resources_asset_(std::move(resources_asset)), |
| 36 | loaded_arsc_(std::move(loaded_arsc)), |
| 37 | assets_provider_(std::move(assets)), |
| 38 | property_flags_(property_flags), |
| 39 | idmap_asset_(std::move(idmap_asset)), |
| 40 | loaded_idmap_(std::move(loaded_idmap)) {} |
| 41 | |
| 42 | std::unique_ptr<ApkAssets> ApkAssets::Load(const std::string& path, package_property_t flags) { |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame^] | 43 | return Load(ZipAssetsProvider::Create(path, flags), flags); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 44 | } |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 45 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 46 | std::unique_ptr<ApkAssets> ApkAssets::LoadFromFd(base::unique_fd fd, |
| 47 | const std::string& debug_name, |
| 48 | package_property_t flags, |
| 49 | off64_t offset, |
| 50 | off64_t len) { |
| 51 | return Load(ZipAssetsProvider::Create(std::move(fd), debug_name, offset, len), flags); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 54 | std::unique_ptr<ApkAssets> ApkAssets::Load(std::unique_ptr<AssetsProvider> assets, |
| 55 | package_property_t flags) { |
| 56 | return LoadImpl(std::move(assets), flags, nullptr /* idmap_asset */, nullptr /* loaded_idmap */); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 59 | std::unique_ptr<ApkAssets> ApkAssets::LoadTable(std::unique_ptr<Asset> resources_asset, |
| 60 | std::unique_ptr<AssetsProvider> assets, |
| 61 | package_property_t flags) { |
| 62 | if (resources_asset == nullptr) { |
| 63 | return {}; |
| 64 | } |
| 65 | return LoadImpl(std::move(resources_asset), std::move(assets), flags, nullptr /* idmap_asset */, |
| 66 | nullptr /* loaded_idmap */); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 69 | std::unique_ptr<ApkAssets> ApkAssets::LoadOverlay(const std::string& idmap_path, |
| 70 | package_property_t flags) { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 71 | CHECK((flags & PROPERTY_LOADER) == 0U) << "Cannot load RROs through loaders"; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 72 | auto idmap_asset = AssetsProvider::CreateAssetFromFile(idmap_path); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 73 | if (idmap_asset == nullptr) { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 74 | LOG(ERROR) << "failed to read IDMAP " << idmap_path; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 75 | return {}; |
| 76 | } |
| 77 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 78 | StringPiece idmap_data(reinterpret_cast<const char*>(idmap_asset->getBuffer(true /* aligned */)), |
| 79 | static_cast<size_t>(idmap_asset->getLength())); |
| 80 | auto loaded_idmap = LoadedIdmap::Load(idmap_path, idmap_data); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 81 | if (loaded_idmap == nullptr) { |
| 82 | LOG(ERROR) << "failed to load IDMAP " << idmap_path; |
| 83 | return {}; |
| 84 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 85 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 86 | std::unique_ptr<AssetsProvider> overlay_assets; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 87 | const std::string overlay_path(loaded_idmap->OverlayApkPath()); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 88 | if (IsFabricatedOverlay(overlay_path)) { |
| 89 | // Fabricated overlays do not contain resource definitions. All of the overlay resource values |
| 90 | // are defined inline in the idmap. |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 91 | overlay_assets = EmptyAssetsProvider::Create(overlay_path); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 92 | } else { |
| 93 | // The overlay should be an APK. |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame^] | 94 | overlay_assets = ZipAssetsProvider::Create(overlay_path, flags); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 95 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 96 | if (overlay_assets == nullptr) { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 97 | return {}; |
| 98 | } |
| 99 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 100 | return LoadImpl(std::move(overlay_assets), flags | PROPERTY_OVERLAY, std::move(idmap_asset), |
| 101 | std::move(loaded_idmap)); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 104 | std::unique_ptr<ApkAssets> ApkAssets::LoadImpl(std::unique_ptr<AssetsProvider> assets, |
| 105 | package_property_t property_flags, |
| 106 | std::unique_ptr<Asset> idmap_asset, |
| 107 | std::unique_ptr<LoadedIdmap> loaded_idmap) { |
| 108 | if (assets == nullptr) { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 109 | return {}; |
| 110 | } |
| 111 | |
Ryan Mitchell | 4ea1e42 | 2020-03-11 13:15:28 -0700 | [diff] [blame] | 112 | // Open the resource table via mmap unless it is compressed. This logic is taken care of by Open. |
| 113 | bool resources_asset_exists = false; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 114 | auto resources_asset = assets->Open(kResourcesArsc, Asset::AccessMode::ACCESS_BUFFER, |
| 115 | &resources_asset_exists); |
| 116 | if (resources_asset == nullptr && resources_asset_exists) { |
| 117 | LOG(ERROR) << "Failed to open '" << kResourcesArsc << "' in APK '" << assets->GetDebugName() |
| 118 | << "'."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 119 | return {}; |
| 120 | } |
| 121 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 122 | return LoadImpl(std::move(resources_asset), std::move(assets), property_flags, |
| 123 | std::move(idmap_asset), std::move(loaded_idmap)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 126 | std::unique_ptr<ApkAssets> ApkAssets::LoadImpl(std::unique_ptr<Asset> resources_asset, |
| 127 | std::unique_ptr<AssetsProvider> assets, |
| 128 | package_property_t property_flags, |
| 129 | std::unique_ptr<Asset> idmap_asset, |
| 130 | std::unique_ptr<LoadedIdmap> loaded_idmap) { |
| 131 | if (assets == nullptr ) { |
Ryan Mitchell | d338dfe | 2019-02-23 15:33:08 +0800 | [diff] [blame] | 132 | return {}; |
| 133 | } |
| 134 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 135 | std::unique_ptr<LoadedArsc> loaded_arsc; |
| 136 | if (resources_asset != nullptr) { |
| 137 | const auto data = resources_asset->getIncFsBuffer(true /* aligned */); |
| 138 | const size_t length = resources_asset->getLength(); |
| 139 | if (!data || length == 0) { |
| 140 | LOG(ERROR) << "Failed to read resources table in APK '" << assets->GetDebugName() << "'."; |
| 141 | return {}; |
| 142 | } |
| 143 | loaded_arsc = LoadedArsc::Load(data, length, loaded_idmap.get(), property_flags); |
| 144 | } else { |
| 145 | loaded_arsc = LoadedArsc::CreateEmpty(); |
| 146 | } |
| 147 | |
| 148 | if (loaded_arsc == nullptr) { |
| 149 | LOG(ERROR) << "Failed to load resources table in APK '" << assets->GetDebugName() << "'."; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 150 | return {}; |
| 151 | } |
| 152 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 153 | return std::unique_ptr<ApkAssets>(new ApkAssets(std::move(resources_asset), |
| 154 | std::move(loaded_arsc), std::move(assets), |
| 155 | property_flags, std::move(idmap_asset), |
| 156 | std::move(loaded_idmap))); |
| 157 | } |
| 158 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 159 | std::optional<std::string_view> ApkAssets::GetPath() const { |
| 160 | return assets_provider_->GetPath(); |
| 161 | } |
| 162 | |
| 163 | const std::string& ApkAssets::GetDebugName() const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 164 | return assets_provider_->GetDebugName(); |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 167 | bool ApkAssets::IsUpToDate() const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 168 | // Loaders are invalidated by the app, not the system, so assume they are up to date. |
| 169 | return IsLoader() || ((!loaded_idmap_ || loaded_idmap_->IsUpToDate()) |
| 170 | && assets_provider_->IsUpToDate()); |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 171 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 172 | } // namespace android |