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