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