blob: 400269c41230150ed569636cfcc4f2caa53d8515 [file] [log] [blame]
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001/*
2 * Copyright (C) 2015 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 Lesinskia6fe3452015-12-09 15:20:52 -080017#include "io/ZipArchive.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080018
Jeremy Meyer56f36e82022-05-20 20:35:42 +000019#include "androidfw/Source.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080020#include "trace/TraceBuffer.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070021#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "util/Util.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000023#include "utils/FileMap.h"
24#include "ziparchive/zip_archive.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080025
Adam Lesinski00451162017-10-03 07:44:08 -070026using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080027
Adam Lesinskia6fe3452015-12-09 15:20:52 -080028namespace aapt {
29namespace io {
30
Jeremy Meyer56f36e82022-05-20 20:35:42 +000031ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const android::Source& source)
32 : zip_handle_(handle), zip_entry_(entry), source_(source) {
33}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080034
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035std::unique_ptr<IData> ZipFile::OpenAsData() {
Ryan Mitchellf6fe9b62018-09-24 12:13:31 -070036 // The file will fail to be mmaped if it is empty
37 if (zip_entry_.uncompressed_length == 0) {
38 return util::make_unique<EmptyData>();
39 }
40
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 if (zip_entry_.method == kCompressStored) {
42 int fd = GetFileDescriptor(zip_handle_);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 android::FileMap file_map;
45 bool result = file_map.create(nullptr, fd, zip_entry_.offset,
46 zip_entry_.uncompressed_length, true);
47 if (!result) {
48 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080049 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 return util::make_unique<MmappedData>(std::move(file_map));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 } else {
53 std::unique_ptr<uint8_t[]> data =
54 std::unique_ptr<uint8_t[]>(new uint8_t[zip_entry_.uncompressed_length]);
55 int32_t result =
56 ExtractToMemory(zip_handle_, &zip_entry_, data.get(),
57 static_cast<uint32_t>(zip_entry_.uncompressed_length));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080058 if (result != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080060 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 return util::make_unique<MallocData>(std::move(data),
62 zip_entry_.uncompressed_length);
63 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -080064}
65
Adam Lesinski00451162017-10-03 07:44:08 -070066std::unique_ptr<io::InputStream> ZipFile::OpenInputStream() {
67 return OpenAsData();
68}
69
Jeremy Meyer56f36e82022-05-20 20:35:42 +000070const android::Source& ZipFile::GetSource() const {
Adam Lesinski00451162017-10-03 07:44:08 -070071 return source_;
72}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073
Pierre Lecesne970732d2017-02-02 23:13:58 +000074bool ZipFile::WasCompressed() {
75 return zip_entry_.method != kCompressStored;
76}
77
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078ZipFileCollectionIterator::ZipFileCollectionIterator(
79 ZipFileCollection* collection)
80 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
81
Adam Lesinski00451162017-10-03 07:44:08 -070082bool ZipFileCollectionIterator::HasNext() {
83 return current_ != end_;
84}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085
86IFile* ZipFileCollectionIterator::Next() {
Pierre Lecesne880d65b2017-02-02 22:33:17 +000087 IFile* result = current_->get();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 ++current_;
89 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080090}
91
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092ZipFileCollection::ZipFileCollection() : handle_(nullptr) {}
93
94std::unique_ptr<ZipFileCollection> ZipFileCollection::Create(
95 const StringPiece& path, std::string* out_error) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080096 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 constexpr static const int32_t kEmptyArchive = -6;
98
99 std::unique_ptr<ZipFileCollection> collection =
100 std::unique_ptr<ZipFileCollection>(new ZipFileCollection());
101
102 int32_t result = OpenArchive(path.data(), &collection->handle_);
103 if (result != 0) {
104 // If a zip is empty, result will be an error code. This is fine and we
105 // should
106 // return an empty ZipFileCollection.
107 if (result == kEmptyArchive) {
108 return collection;
109 }
110
111 if (out_error) *out_error = ErrorCodeString(result);
112 return {};
113 }
114
115 void* cookie = nullptr;
Elliott Hughesf31f1262019-05-10 17:02:14 -0700116 result = StartIteration(collection->handle_, &cookie);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 if (result != 0) {
118 if (out_error) *out_error = ErrorCodeString(result);
119 return {};
120 }
121
122 using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>;
123 IterationEnder iteration_ender(cookie, EndIteration);
124
Elliott Hughes78de4f92019-06-14 15:28:38 -0700125 std::string zip_entry_path;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 ZipEntry zip_data;
Elliott Hughes78de4f92019-06-14 15:28:38 -0700127 while ((result = Next(cookie, &zip_data, &zip_entry_path)) == 0) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700128 // Do not add folders to the file collection
129 if (util::EndsWith(zip_entry_path, "/")) {
130 continue;
131 }
132
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000133 std::unique_ptr<IFile> file = util::make_unique<ZipFile>(
134 collection->handle_, zip_data, android::Source(zip_entry_path, path.to_string()));
Pierre Lecesne880d65b2017-02-02 22:33:17 +0000135 collection->files_by_name_[zip_entry_path] = file.get();
136 collection->files_.push_back(std::move(file));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 }
138
139 if (result != -1) {
140 if (out_error) *out_error = ErrorCodeString(result);
141 return {};
142 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 return collection;
145}
146
147IFile* ZipFileCollection::FindFile(const StringPiece& path) {
Pierre Lecesne880d65b2017-02-02 22:33:17 +0000148 auto iter = files_by_name_.find(path.to_string());
149 if (iter != files_by_name_.end()) {
150 return iter->second;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 }
152 return nullptr;
153}
154
155std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() {
156 return util::make_unique<ZipFileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800157}
158
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700159char ZipFileCollection::GetDirSeparator() {
160 // According to the zip file specification, section 4.4.17.1:
161 // "All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility
162 // with Amiga and UNIX file systems etc."
163 return '/';
164}
165
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800166ZipFileCollection::~ZipFileCollection() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 if (handle_) {
168 CloseArchive(handle_);
169 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800170}
171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172} // namespace io
173} // namespace aapt