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