1 /*
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
17 #include "io/ZipArchive.h"
18
19 #include "androidfw/Source.h"
20 #include "trace/TraceBuffer.h"
21 #include "util/Files.h"
22 #include "util/Util.h"
23 #include "utils/FileMap.h"
24 #include "ziparchive/zip_archive.h"
25
26 using ::android::StringPiece;
27
28 namespace aapt {
29 namespace io {
30
ZipFile(ZipArchiveHandle handle,const ZipEntry & entry,const android::Source & source)31 ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const android::Source& source)
32 : zip_handle_(handle), zip_entry_(entry), source_(source) {
33 }
34
OpenAsData()35 std::unique_ptr<IData> ZipFile::OpenAsData() {
36 // 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
41 if (zip_entry_.method == kCompressStored) {
42 int fd = GetFileDescriptor(zip_handle_);
43
44 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 {};
49 }
50 return util::make_unique<MmappedData>(std::move(file_map));
51
52 } 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));
58 if (result != 0) {
59 return {};
60 }
61 return util::make_unique<MallocData>(std::move(data),
62 zip_entry_.uncompressed_length);
63 }
64 }
65
OpenInputStream()66 std::unique_ptr<io::InputStream> ZipFile::OpenInputStream() {
67 return OpenAsData();
68 }
69
GetSource() const70 const android::Source& ZipFile::GetSource() const {
71 return source_;
72 }
73
WasCompressed()74 bool ZipFile::WasCompressed() {
75 return zip_entry_.method != kCompressStored;
76 }
77
ZipFileCollectionIterator(ZipFileCollection * collection)78 ZipFileCollectionIterator::ZipFileCollectionIterator(
79 ZipFileCollection* collection)
80 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
81
HasNext()82 bool ZipFileCollectionIterator::HasNext() {
83 return current_ != end_;
84 }
85
Next()86 IFile* ZipFileCollectionIterator::Next() {
87 IFile* result = current_->get();
88 ++current_;
89 return result;
90 }
91
ZipFileCollection()92 ZipFileCollection::ZipFileCollection() : handle_(nullptr) {}
93
Create(StringPiece path,std::string * out_error)94 std::unique_ptr<ZipFileCollection> ZipFileCollection::Create(StringPiece path,
95 std::string* out_error) {
96 TRACE_CALL();
97 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;
116 result = StartIteration(collection->handle_, &cookie);
117 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
125 std::string zip_entry_path;
126 ZipEntry zip_data;
127 while ((result = Next(cookie, &zip_data, &zip_entry_path)) == 0) {
128 // Do not add folders to the file collection
129 if (util::EndsWith(zip_entry_path, "/")) {
130 continue;
131 }
132
133 std::unique_ptr<IFile> file = util::make_unique<ZipFile>(collection->handle_, zip_data,
134 android::Source(zip_entry_path, path));
135 collection->files_by_name_[zip_entry_path] = file.get();
136 collection->files_.push_back(std::move(file));
137 }
138
139 if (result != -1) {
140 if (out_error) *out_error = ErrorCodeString(result);
141 return {};
142 }
143
144 return collection;
145 }
146
FindFile(StringPiece path)147 IFile* ZipFileCollection::FindFile(StringPiece path) {
148 auto iter = files_by_name_.find(path);
149 if (iter != files_by_name_.end()) {
150 return iter->second;
151 }
152 return nullptr;
153 }
154
Iterator()155 std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() {
156 return util::make_unique<ZipFileCollectionIterator>(this);
157 }
158
GetDirSeparator()159 char 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
~ZipFileCollection()166 ZipFileCollection::~ZipFileCollection() {
167 if (handle_) {
168 CloseArchive(handle_);
169 }
170 }
171
172 } // namespace io
173 } // namespace aapt
174