blob: 7d0f6b9c495aacc352a2ed8e3f822d4a2eda4dfa [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "utils/FileMap.h"
20#include "ziparchive/zip_archive.h"
21
22#include "Source.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070023#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "util/Util.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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry,
32 const Source& source)
33 : zip_handle_(handle), zip_entry_(entry), source_(source) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080034
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035std::unique_ptr<IData> ZipFile::OpenAsData() {
36 if (zip_entry_.method == kCompressStored) {
37 int fd = GetFileDescriptor(zip_handle_);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 android::FileMap file_map;
40 bool result = file_map.create(nullptr, fd, zip_entry_.offset,
41 zip_entry_.uncompressed_length, true);
42 if (!result) {
43 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080044 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 return util::make_unique<MmappedData>(std::move(file_map));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 } else {
48 std::unique_ptr<uint8_t[]> data =
49 std::unique_ptr<uint8_t[]>(new uint8_t[zip_entry_.uncompressed_length]);
50 int32_t result =
51 ExtractToMemory(zip_handle_, &zip_entry_, data.get(),
52 static_cast<uint32_t>(zip_entry_.uncompressed_length));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080053 if (result != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080055 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 return util::make_unique<MallocData>(std::move(data),
57 zip_entry_.uncompressed_length);
58 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -080059}
60
Adam Lesinski00451162017-10-03 07:44:08 -070061std::unique_ptr<io::InputStream> ZipFile::OpenInputStream() {
62 return OpenAsData();
63}
64
65const Source& ZipFile::GetSource() const {
66 return source_;
67}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068
Pierre Lecesne970732d2017-02-02 23:13:58 +000069bool ZipFile::WasCompressed() {
70 return zip_entry_.method != kCompressStored;
71}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073ZipFileCollectionIterator::ZipFileCollectionIterator(
74 ZipFileCollection* collection)
75 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
76
Adam Lesinski00451162017-10-03 07:44:08 -070077bool ZipFileCollectionIterator::HasNext() {
78 return current_ != end_;
79}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080
81IFile* ZipFileCollectionIterator::Next() {
Pierre Lecesne880d65b2017-02-02 22:33:17 +000082 IFile* result = current_->get();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 ++current_;
84 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080085}
86
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087ZipFileCollection::ZipFileCollection() : handle_(nullptr) {}
88
89std::unique_ptr<ZipFileCollection> ZipFileCollection::Create(
90 const StringPiece& path, std::string* out_error) {
91 constexpr static const int32_t kEmptyArchive = -6;
92
93 std::unique_ptr<ZipFileCollection> collection =
94 std::unique_ptr<ZipFileCollection>(new ZipFileCollection());
95
96 int32_t result = OpenArchive(path.data(), &collection->handle_);
97 if (result != 0) {
98 // If a zip is empty, result will be an error code. This is fine and we
99 // should
100 // return an empty ZipFileCollection.
101 if (result == kEmptyArchive) {
102 return collection;
103 }
104
105 if (out_error) *out_error = ErrorCodeString(result);
106 return {};
107 }
108
109 void* cookie = nullptr;
110 result = StartIteration(collection->handle_, &cookie, nullptr, nullptr);
111 if (result != 0) {
112 if (out_error) *out_error = ErrorCodeString(result);
113 return {};
114 }
115
116 using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>;
117 IterationEnder iteration_ender(cookie, EndIteration);
118
119 ZipString zip_entry_name;
120 ZipEntry zip_data;
121 while ((result = Next(cookie, &zip_data, &zip_entry_name)) == 0) {
122 std::string zip_entry_path =
123 std::string(reinterpret_cast<const char*>(zip_entry_name.name),
124 zip_entry_name.name_length);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700125
126 // Do not add folders to the file collection
127 if (util::EndsWith(zip_entry_path, "/")) {
128 continue;
129 }
130
131 std::unique_ptr<IFile> file = util::make_unique<ZipFile>(collection->handle_, zip_data,
132 Source(zip_entry_path, path.to_string()));
Pierre Lecesne880d65b2017-02-02 22:33:17 +0000133 collection->files_by_name_[zip_entry_path] = file.get();
134 collection->files_.push_back(std::move(file));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 }
136
137 if (result != -1) {
138 if (out_error) *out_error = ErrorCodeString(result);
139 return {};
140 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 return collection;
143}
144
145IFile* ZipFileCollection::FindFile(const StringPiece& path) {
Pierre Lecesne880d65b2017-02-02 22:33:17 +0000146 auto iter = files_by_name_.find(path.to_string());
147 if (iter != files_by_name_.end()) {
148 return iter->second;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 }
150 return nullptr;
151}
152
153std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() {
154 return util::make_unique<ZipFileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800155}
156
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700157char ZipFileCollection::GetDirSeparator() {
158 // According to the zip file specification, section 4.4.17.1:
159 // "All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility
160 // with Amiga and UNIX file systems etc."
161 return '/';
162}
163
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800164ZipFileCollection::~ZipFileCollection() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 if (handle_) {
166 CloseArchive(handle_);
167 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800168}
169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170} // namespace io
171} // namespace aapt