ApkAssets.cpp revision 0c40524953f3d36a880f91183302a2ea5c722930
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#define ATRACE_TAG ATRACE_TAG_RESOURCES
18
19#include "androidfw/ApkAssets.h"
20
21#include "android-base/logging.h"
22#include "utils/Trace.h"
23#include "ziparchive/zip_archive.h"
24
25#include "androidfw/Asset.h"
26#include "androidfw/Util.h"
27
28namespace android {
29
30std::unique_ptr<const ApkAssets> ApkAssets::Load(const std::string& path, bool system) {
31  return ApkAssets::LoadImpl(path, system, false /*load_as_shared_library*/);
32}
33
34std::unique_ptr<const ApkAssets> ApkAssets::LoadAsSharedLibrary(const std::string& path,
35                                                                bool system) {
36  return ApkAssets::LoadImpl(path, system, true /*load_as_shared_library*/);
37}
38
39std::unique_ptr<const ApkAssets> ApkAssets::LoadImpl(const std::string& path, bool system,
40                                                     bool load_as_shared_library) {
41  ATRACE_CALL();
42  ::ZipArchiveHandle unmanaged_handle;
43  int32_t result = ::OpenArchive(path.c_str(), &unmanaged_handle);
44  if (result != 0) {
45    LOG(ERROR) << ::ErrorCodeString(result);
46    return {};
47  }
48
49  // Wrap the handle in a unique_ptr so it gets automatically closed.
50  std::unique_ptr<ApkAssets> loaded_apk(new ApkAssets());
51  loaded_apk->zip_handle_.reset(unmanaged_handle);
52
53  ::ZipString entry_name("resources.arsc");
54  ::ZipEntry entry;
55  result = ::FindEntry(loaded_apk->zip_handle_.get(), entry_name, &entry);
56  if (result != 0) {
57    LOG(ERROR) << ::ErrorCodeString(result);
58    return {};
59  }
60
61  if (entry.method == kCompressDeflated) {
62    LOG(WARNING) << "resources.arsc is compressed.";
63  }
64
65  loaded_apk->resources_asset_ =
66      loaded_apk->Open("resources.arsc", Asset::AccessMode::ACCESS_BUFFER);
67  if (loaded_apk->resources_asset_ == nullptr) {
68    return {};
69  }
70
71  loaded_apk->path_ = path;
72  loaded_apk->loaded_arsc_ =
73      LoadedArsc::Load(loaded_apk->resources_asset_->getBuffer(true /*wordAligned*/),
74                       loaded_apk->resources_asset_->getLength(), system, load_as_shared_library);
75  if (loaded_apk->loaded_arsc_ == nullptr) {
76    return {};
77  }
78
79  // Need to force a move for mingw32.
80  return std::move(loaded_apk);
81}
82
83std::unique_ptr<Asset> ApkAssets::Open(const std::string& path, Asset::AccessMode /*mode*/) const {
84  ATRACE_NAME("ApkAssets::Open");
85  CHECK(zip_handle_ != nullptr);
86
87  ::ZipString name(path.c_str());
88  ::ZipEntry entry;
89  int32_t result = ::FindEntry(zip_handle_.get(), name, &entry);
90  if (result != 0) {
91    LOG(ERROR) << "No entry '" << path << "' found in APK.";
92    return {};
93  }
94
95  if (entry.method == kCompressDeflated) {
96    auto compressed_asset = util::make_unique<_CompressedAsset>();
97    if (compressed_asset->openChunk(::GetFileDescriptor(zip_handle_.get()), entry.offset,
98                                    entry.method, entry.uncompressed_length,
99                                    entry.compressed_length) != NO_ERROR) {
100      LOG(ERROR) << "Failed to decompress '" << path << "'.";
101      return {};
102    }
103    return std::move(compressed_asset);
104  } else {
105    auto uncompressed_asset = util::make_unique<_FileAsset>();
106    if (uncompressed_asset->openChunk(path.c_str(), ::GetFileDescriptor(zip_handle_.get()),
107                                      entry.offset, entry.uncompressed_length) != NO_ERROR) {
108      LOG(ERROR) << "Failed to mmap '" << path << "'.";
109      return {};
110    }
111    return std::move(uncompressed_asset);
112  }
113  return {};
114}
115
116}  // namespace android
117