blob: 9a08f638f23095fb149009a01d89d14245159f51 [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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<ApkAssets> ApkAssets::Load(const std::string& path) {
Adam Lesinskida431a22016-12-29 16:08:16 -050031 return ApkAssets::LoadImpl(path, false /*load_as_shared_library*/);
32}
33
34std::unique_ptr<ApkAssets> ApkAssets::LoadAsSharedLibrary(const std::string& path) {
35 return ApkAssets::LoadImpl(path, true /*load_as_shared_library*/);
36}
37
38std::unique_ptr<ApkAssets> ApkAssets::LoadImpl(const std::string& path,
39 bool load_as_shared_library) {
40 ATRACE_CALL();
Adam Lesinski7ad11102016-10-28 16:39:15 -070041 ::ZipArchiveHandle unmanaged_handle;
42 int32_t result = ::OpenArchive(path.c_str(), &unmanaged_handle);
43 if (result != 0) {
44 LOG(ERROR) << ::ErrorCodeString(result);
45 return {};
46 }
47
48 // Wrap the handle in a unique_ptr so it gets automatically closed.
49 std::unique_ptr<ApkAssets> loaded_apk(new ApkAssets());
50 loaded_apk->zip_handle_.reset(unmanaged_handle);
51
52 ::ZipString entry_name("resources.arsc");
53 ::ZipEntry entry;
54 result = ::FindEntry(loaded_apk->zip_handle_.get(), entry_name, &entry);
55 if (result != 0) {
56 LOG(ERROR) << ::ErrorCodeString(result);
57 return {};
58 }
59
60 if (entry.method == kCompressDeflated) {
61 LOG(WARNING) << "resources.arsc is compressed.";
62 }
63
64 loaded_apk->resources_asset_ =
65 loaded_apk->Open("resources.arsc", Asset::AccessMode::ACCESS_BUFFER);
66 if (loaded_apk->resources_asset_ == nullptr) {
67 return {};
68 }
69
70 loaded_apk->path_ = path;
71 loaded_apk->loaded_arsc_ =
72 LoadedArsc::Load(loaded_apk->resources_asset_->getBuffer(true /*wordAligned*/),
Adam Lesinskida431a22016-12-29 16:08:16 -050073 loaded_apk->resources_asset_->getLength(), load_as_shared_library);
Adam Lesinski7ad11102016-10-28 16:39:15 -070074 if (loaded_apk->loaded_arsc_ == nullptr) {
75 return {};
76 }
77 return loaded_apk;
78}
79
80std::unique_ptr<Asset> ApkAssets::Open(const std::string& path, Asset::AccessMode /*mode*/) const {
81 ATRACE_NAME("ApkAssets::Open");
82 CHECK(zip_handle_ != nullptr);
83
84 ::ZipString name(path.c_str());
85 ::ZipEntry entry;
86 int32_t result = ::FindEntry(zip_handle_.get(), name, &entry);
87 if (result != 0) {
88 LOG(ERROR) << "No entry '" << path << "' found in APK.";
89 return {};
90 }
91
92 if (entry.method == kCompressDeflated) {
93 auto compressed_asset = util::make_unique<_CompressedAsset>();
94 if (compressed_asset->openChunk(::GetFileDescriptor(zip_handle_.get()), entry.offset,
95 entry.method, entry.uncompressed_length,
96 entry.compressed_length) != NO_ERROR) {
97 LOG(ERROR) << "Failed to decompress '" << path << "'.";
98 return {};
99 }
100 return std::move(compressed_asset);
101 } else {
102 auto uncompressed_asset = util::make_unique<_FileAsset>();
103 if (uncompressed_asset->openChunk(path.c_str(), ::GetFileDescriptor(zip_handle_.get()),
104 entry.offset, entry.uncompressed_length) != NO_ERROR) {
105 LOG(ERROR) << "Failed to mmap '" << path << "'.";
106 return {};
107 }
108 return std::move(uncompressed_asset);
109 }
110 return {};
111}
112
113} // namespace android