• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "androidfw/ApkAssets.h"
18 
19 #include "android-base/errors.h"
20 #include "android-base/logging.h"
21 
22 namespace android {
23 
24 using base::SystemErrorCodeToString;
25 using base::unique_fd;
26 
27 constexpr const char* kResourcesArsc = "resources.arsc";
28 
ApkAssets(std::unique_ptr<Asset> resources_asset,std::unique_ptr<LoadedArsc> loaded_arsc,std::unique_ptr<AssetsProvider> assets,package_property_t property_flags,std::unique_ptr<Asset> idmap_asset,std::unique_ptr<LoadedIdmap> loaded_idmap)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 
Load(const std::string & path,package_property_t flags)42 std::unique_ptr<ApkAssets> ApkAssets::Load(const std::string& path, package_property_t flags) {
43   return Load(ZipAssetsProvider::Create(path, flags), flags);
44 }
45 
LoadFromFd(base::unique_fd fd,const std::string & debug_name,package_property_t flags,off64_t offset,off64_t len)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);
52 }
53 
Load(std::unique_ptr<AssetsProvider> assets,package_property_t flags)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 */);
57 }
58 
LoadTable(std::unique_ptr<Asset> resources_asset,std::unique_ptr<AssetsProvider> assets,package_property_t flags)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 */);
67 }
68 
LoadOverlay(const std::string & idmap_path,package_property_t flags)69 std::unique_ptr<ApkAssets> ApkAssets::LoadOverlay(const std::string& idmap_path,
70                                                   package_property_t flags) {
71   CHECK((flags & PROPERTY_LOADER) == 0U) << "Cannot load RROs through loaders";
72   auto idmap_asset = AssetsProvider::CreateAssetFromFile(idmap_path);
73   if (idmap_asset == nullptr) {
74     LOG(ERROR) << "failed to read IDMAP " << idmap_path;
75     return {};
76   }
77 
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);
81   if (loaded_idmap == nullptr) {
82     LOG(ERROR) << "failed to load IDMAP " << idmap_path;
83     return {};
84   }
85 
86   std::unique_ptr<AssetsProvider> overlay_assets;
87   const std::string overlay_path(loaded_idmap->OverlayApkPath());
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.
91     overlay_assets = EmptyAssetsProvider::Create(overlay_path);
92   } else {
93     // The overlay should be an APK.
94     overlay_assets = ZipAssetsProvider::Create(overlay_path, flags);
95   }
96   if (overlay_assets == nullptr) {
97     return {};
98   }
99 
100   return LoadImpl(std::move(overlay_assets), flags | PROPERTY_OVERLAY, std::move(idmap_asset),
101                   std::move(loaded_idmap));
102 }
103 
LoadImpl(std::unique_ptr<AssetsProvider> assets,package_property_t property_flags,std::unique_ptr<Asset> idmap_asset,std::unique_ptr<LoadedIdmap> loaded_idmap)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) {
109     return {};
110   }
111 
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;
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                << "'.";
119     return {};
120   }
121 
122   return LoadImpl(std::move(resources_asset), std::move(assets), property_flags,
123                   std::move(idmap_asset), std::move(loaded_idmap));
124 }
125 
LoadImpl(std::unique_ptr<Asset> resources_asset,std::unique_ptr<AssetsProvider> assets,package_property_t property_flags,std::unique_ptr<Asset> idmap_asset,std::unique_ptr<LoadedIdmap> loaded_idmap)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 ) {
132     return {};
133   }
134 
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() << "'.";
150     return {};
151   }
152 
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 
GetPath() const159 std::optional<std::string_view> ApkAssets::GetPath() const {
160   return assets_provider_->GetPath();
161 }
162 
GetDebugName() const163 const std::string& ApkAssets::GetDebugName() const {
164   return assets_provider_->GetDebugName();
165 }
166 
IsUpToDate() const167 bool ApkAssets::IsUpToDate() const {
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());
171 }
172 }  // namespace android
173