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" |
| 23 | #include "util/Util.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 24 | |
| 25 | namespace aapt { |
| 26 | namespace io { |
| 27 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 28 | ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, |
| 29 | const Source& source) |
| 30 | : zip_handle_(handle), zip_entry_(entry), source_(source) {} |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 32 | std::unique_ptr<IData> ZipFile::OpenAsData() { |
| 33 | if (zip_entry_.method == kCompressStored) { |
| 34 | int fd = GetFileDescriptor(zip_handle_); |
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 | android::FileMap file_map; |
| 37 | bool result = file_map.create(nullptr, fd, zip_entry_.offset, |
| 38 | zip_entry_.uncompressed_length, true); |
| 39 | if (!result) { |
| 40 | return {}; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 41 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 42 | return util::make_unique<MmappedData>(std::move(file_map)); |
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 | } else { |
| 45 | std::unique_ptr<uint8_t[]> data = |
| 46 | std::unique_ptr<uint8_t[]>(new uint8_t[zip_entry_.uncompressed_length]); |
| 47 | int32_t result = |
| 48 | ExtractToMemory(zip_handle_, &zip_entry_, data.get(), |
| 49 | static_cast<uint32_t>(zip_entry_.uncompressed_length)); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 50 | if (result != 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 51 | return {}; |
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 | return util::make_unique<MallocData>(std::move(data), |
| 54 | zip_entry_.uncompressed_length); |
| 55 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 58 | const Source& ZipFile::GetSource() const { return source_; } |
| 59 | |
| 60 | ZipFileCollectionIterator::ZipFileCollectionIterator( |
| 61 | ZipFileCollection* collection) |
| 62 | : current_(collection->files_.begin()), end_(collection->files_.end()) {} |
| 63 | |
| 64 | bool ZipFileCollectionIterator::HasNext() { return current_ != end_; } |
| 65 | |
| 66 | IFile* ZipFileCollectionIterator::Next() { |
| 67 | IFile* result = current_->second.get(); |
| 68 | ++current_; |
| 69 | return result; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 72 | ZipFileCollection::ZipFileCollection() : handle_(nullptr) {} |
| 73 | |
| 74 | std::unique_ptr<ZipFileCollection> ZipFileCollection::Create( |
| 75 | const StringPiece& path, std::string* out_error) { |
| 76 | constexpr static const int32_t kEmptyArchive = -6; |
| 77 | |
| 78 | std::unique_ptr<ZipFileCollection> collection = |
| 79 | std::unique_ptr<ZipFileCollection>(new ZipFileCollection()); |
| 80 | |
| 81 | int32_t result = OpenArchive(path.data(), &collection->handle_); |
| 82 | if (result != 0) { |
| 83 | // If a zip is empty, result will be an error code. This is fine and we |
| 84 | // should |
| 85 | // return an empty ZipFileCollection. |
| 86 | if (result == kEmptyArchive) { |
| 87 | return collection; |
| 88 | } |
| 89 | |
| 90 | if (out_error) *out_error = ErrorCodeString(result); |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | void* cookie = nullptr; |
| 95 | result = StartIteration(collection->handle_, &cookie, nullptr, nullptr); |
| 96 | if (result != 0) { |
| 97 | if (out_error) *out_error = ErrorCodeString(result); |
| 98 | return {}; |
| 99 | } |
| 100 | |
| 101 | using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>; |
| 102 | IterationEnder iteration_ender(cookie, EndIteration); |
| 103 | |
| 104 | ZipString zip_entry_name; |
| 105 | ZipEntry zip_data; |
| 106 | while ((result = Next(cookie, &zip_data, &zip_entry_name)) == 0) { |
| 107 | std::string zip_entry_path = |
| 108 | std::string(reinterpret_cast<const char*>(zip_entry_name.name), |
| 109 | zip_entry_name.name_length); |
| 110 | std::string nested_path = path.ToString() + "@" + zip_entry_path; |
| 111 | collection->files_[zip_entry_path] = util::make_unique<ZipFile>( |
| 112 | collection->handle_, zip_data, Source(nested_path)); |
| 113 | } |
| 114 | |
| 115 | if (result != -1) { |
| 116 | if (out_error) *out_error = ErrorCodeString(result); |
| 117 | return {}; |
| 118 | } |
| 119 | return collection; |
| 120 | } |
| 121 | |
| 122 | IFile* ZipFileCollection::FindFile(const StringPiece& path) { |
| 123 | auto iter = files_.find(path.ToString()); |
| 124 | if (iter != files_.end()) { |
| 125 | return iter->second.get(); |
| 126 | } |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
| 130 | std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() { |
| 131 | return util::make_unique<ZipFileCollectionIterator>(this); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | ZipFileCollection::~ZipFileCollection() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 135 | if (handle_) { |
| 136 | CloseArchive(handle_); |
| 137 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 140 | } // namespace io |
| 141 | } // namespace aapt |