Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 17 | #include "io/ZipArchive.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 19 | #include "utils/FileMap.h" |
| 20 | #include "ziparchive/zip_archive.h" |
| 21 | |
| 22 | #include "Source.h" |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame^] | 23 | #include "trace/TraceBuffer.h" |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 24 | #include "util/Files.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | #include "util/Util.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 26 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 27 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 29 | namespace aapt { |
| 30 | namespace io { |
| 31 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, |
| 33 | const Source& source) |
| 34 | : zip_handle_(handle), zip_entry_(entry), source_(source) {} |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | std::unique_ptr<IData> ZipFile::OpenAsData() { |
Ryan Mitchell | f6fe9b6 | 2018-09-24 12:13:31 -0700 | [diff] [blame] | 37 | // The file will fail to be mmaped if it is empty |
| 38 | if (zip_entry_.uncompressed_length == 0) { |
| 39 | return util::make_unique<EmptyData>(); |
| 40 | } |
| 41 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | if (zip_entry_.method == kCompressStored) { |
| 43 | int fd = GetFileDescriptor(zip_handle_); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | android::FileMap file_map; |
| 46 | bool result = file_map.create(nullptr, fd, zip_entry_.offset, |
| 47 | zip_entry_.uncompressed_length, true); |
| 48 | if (!result) { |
| 49 | return {}; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 50 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | return util::make_unique<MmappedData>(std::move(file_map)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 52 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | } else { |
| 54 | std::unique_ptr<uint8_t[]> data = |
| 55 | std::unique_ptr<uint8_t[]>(new uint8_t[zip_entry_.uncompressed_length]); |
| 56 | int32_t result = |
| 57 | ExtractToMemory(zip_handle_, &zip_entry_, data.get(), |
| 58 | static_cast<uint32_t>(zip_entry_.uncompressed_length)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 59 | if (result != 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | return {}; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 61 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | return util::make_unique<MallocData>(std::move(data), |
| 63 | zip_entry_.uncompressed_length); |
| 64 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 67 | std::unique_ptr<io::InputStream> ZipFile::OpenInputStream() { |
| 68 | return OpenAsData(); |
| 69 | } |
| 70 | |
| 71 | const Source& ZipFile::GetSource() const { |
| 72 | return source_; |
| 73 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | |
Pierre Lecesne | 970732d | 2017-02-02 23:13:58 +0000 | [diff] [blame] | 75 | bool ZipFile::WasCompressed() { |
| 76 | return zip_entry_.method != kCompressStored; |
| 77 | } |
| 78 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | ZipFileCollectionIterator::ZipFileCollectionIterator( |
| 80 | ZipFileCollection* collection) |
| 81 | : current_(collection->files_.begin()), end_(collection->files_.end()) {} |
| 82 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 83 | bool ZipFileCollectionIterator::HasNext() { |
| 84 | return current_ != end_; |
| 85 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | |
| 87 | IFile* ZipFileCollectionIterator::Next() { |
Pierre Lecesne | 880d65b | 2017-02-02 22:33:17 +0000 | [diff] [blame] | 88 | IFile* result = current_->get(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | ++current_; |
| 90 | return result; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | ZipFileCollection::ZipFileCollection() : handle_(nullptr) {} |
| 94 | |
| 95 | std::unique_ptr<ZipFileCollection> ZipFileCollection::Create( |
| 96 | const StringPiece& path, std::string* out_error) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame^] | 97 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | constexpr static const int32_t kEmptyArchive = -6; |
| 99 | |
| 100 | std::unique_ptr<ZipFileCollection> collection = |
| 101 | std::unique_ptr<ZipFileCollection>(new ZipFileCollection()); |
| 102 | |
| 103 | int32_t result = OpenArchive(path.data(), &collection->handle_); |
| 104 | if (result != 0) { |
| 105 | // If a zip is empty, result will be an error code. This is fine and we |
| 106 | // should |
| 107 | // return an empty ZipFileCollection. |
| 108 | if (result == kEmptyArchive) { |
| 109 | return collection; |
| 110 | } |
| 111 | |
| 112 | if (out_error) *out_error = ErrorCodeString(result); |
| 113 | return {}; |
| 114 | } |
| 115 | |
| 116 | void* cookie = nullptr; |
| 117 | result = StartIteration(collection->handle_, &cookie, nullptr, nullptr); |
| 118 | if (result != 0) { |
| 119 | if (out_error) *out_error = ErrorCodeString(result); |
| 120 | return {}; |
| 121 | } |
| 122 | |
| 123 | using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>; |
| 124 | IterationEnder iteration_ender(cookie, EndIteration); |
| 125 | |
| 126 | ZipString zip_entry_name; |
| 127 | ZipEntry zip_data; |
| 128 | while ((result = Next(cookie, &zip_data, &zip_entry_name)) == 0) { |
| 129 | std::string zip_entry_path = |
| 130 | std::string(reinterpret_cast<const char*>(zip_entry_name.name), |
| 131 | zip_entry_name.name_length); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 132 | |
| 133 | // Do not add folders to the file collection |
| 134 | if (util::EndsWith(zip_entry_path, "/")) { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | std::unique_ptr<IFile> file = util::make_unique<ZipFile>(collection->handle_, zip_data, |
| 139 | Source(zip_entry_path, path.to_string())); |
Pierre Lecesne | 880d65b | 2017-02-02 22:33:17 +0000 | [diff] [blame] | 140 | collection->files_by_name_[zip_entry_path] = file.get(); |
| 141 | collection->files_.push_back(std::move(file)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (result != -1) { |
| 145 | if (out_error) *out_error = ErrorCodeString(result); |
| 146 | return {}; |
| 147 | } |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 149 | return collection; |
| 150 | } |
| 151 | |
| 152 | IFile* ZipFileCollection::FindFile(const StringPiece& path) { |
Pierre Lecesne | 880d65b | 2017-02-02 22:33:17 +0000 | [diff] [blame] | 153 | auto iter = files_by_name_.find(path.to_string()); |
| 154 | if (iter != files_by_name_.end()) { |
| 155 | return iter->second; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | } |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
| 160 | std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() { |
| 161 | return util::make_unique<ZipFileCollectionIterator>(this); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 164 | char ZipFileCollection::GetDirSeparator() { |
| 165 | // According to the zip file specification, section 4.4.17.1: |
| 166 | // "All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility |
| 167 | // with Amiga and UNIX file systems etc." |
| 168 | return '/'; |
| 169 | } |
| 170 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 171 | ZipFileCollection::~ZipFileCollection() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | if (handle_) { |
| 173 | CloseArchive(handle_); |
| 174 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | } // namespace io |
| 178 | } // namespace aapt |