Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -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 | |
| 17 | #ifndef AAPT_IO_ZIPARCHIVE_H |
| 18 | #define AAPT_IO_ZIPARCHIVE_H |
| 19 | |
| 20 | #include "io/File.h" |
| 21 | #include "util/StringPiece.h" |
| 22 | |
| 23 | #include <utils/FileMap.h> |
| 24 | #include <ziparchive/zip_archive.h> |
| 25 | |
| 26 | namespace aapt { |
| 27 | namespace io { |
| 28 | |
| 29 | /** |
| 30 | * An IFile representing a file within a ZIP archive. If the file is compressed, it is uncompressed |
| 31 | * and copied into memory when opened. Otherwise it is mmapped from the ZIP archive. |
| 32 | */ |
| 33 | class ZipFile : public IFile { |
| 34 | public: |
| 35 | ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source) : |
| 36 | mZipHandle(handle), mZipEntry(entry), mSource(source) { |
| 37 | } |
| 38 | |
| 39 | std::unique_ptr<IData> openAsData() override { |
| 40 | if (mZipEntry.method == kCompressStored) { |
| 41 | int fd = GetFileDescriptor(mZipHandle); |
| 42 | |
| 43 | android::FileMap fileMap; |
| 44 | bool result = fileMap.create(nullptr, fd, mZipEntry.offset, |
| 45 | mZipEntry.uncompressed_length, true); |
| 46 | if (!result) { |
| 47 | return {}; |
| 48 | } |
| 49 | return util::make_unique<MmappedData>(std::move(fileMap)); |
| 50 | |
| 51 | } else { |
| 52 | std::unique_ptr<uint8_t[]> data = std::unique_ptr<uint8_t[]>( |
| 53 | new uint8_t[mZipEntry.uncompressed_length]); |
| 54 | int32_t result = ExtractToMemory(mZipHandle, &mZipEntry, data.get(), |
| 55 | static_cast<uint32_t>(mZipEntry.uncompressed_length)); |
| 56 | if (result != 0) { |
| 57 | return {}; |
| 58 | } |
| 59 | return util::make_unique<MallocData>(std::move(data), mZipEntry.uncompressed_length); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | const Source& getSource() const override { |
| 64 | return mSource; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | ZipArchiveHandle mZipHandle; |
| 69 | ZipEntry mZipEntry; |
| 70 | Source mSource; |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * An IFileCollection that represents a ZIP archive and the entries within it. |
| 75 | */ |
| 76 | class ZipFileCollection : public IFileCollection { |
| 77 | public: |
| 78 | static std::unique_ptr<ZipFileCollection> create(const StringPiece& path, |
| 79 | std::string* outError) { |
| 80 | std::unique_ptr<ZipFileCollection> collection = std::unique_ptr<ZipFileCollection>( |
| 81 | new ZipFileCollection()); |
| 82 | |
| 83 | int32_t result = OpenArchive(path.data(), &collection->mHandle); |
| 84 | if (result != 0) { |
| 85 | if (outError) *outError = ErrorCodeString(result); |
| 86 | return {}; |
| 87 | } |
| 88 | |
| 89 | ZipString suffix(".flat"); |
| 90 | void* cookie = nullptr; |
| 91 | result = StartIteration(collection->mHandle, &cookie, nullptr, &suffix); |
| 92 | if (result != 0) { |
| 93 | if (outError) *outError = ErrorCodeString(result); |
| 94 | return {}; |
| 95 | } |
| 96 | |
| 97 | using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>; |
| 98 | IterationEnder iterationEnder(cookie, EndIteration); |
| 99 | |
| 100 | ZipString zipEntryName; |
| 101 | ZipEntry zipData; |
| 102 | while ((result = Next(cookie, &zipData, &zipEntryName)) == 0) { |
| 103 | std::string nestedPath = path.toString(); |
| 104 | nestedPath += "@" + std::string(reinterpret_cast<const char*>(zipEntryName.name), |
| 105 | zipEntryName.name_length); |
| 106 | collection->mFiles.push_back(util::make_unique<ZipFile>(collection->mHandle, |
| 107 | zipData, |
| 108 | Source(nestedPath))); |
| 109 | } |
| 110 | |
| 111 | if (result != -1) { |
| 112 | if (outError) *outError = ErrorCodeString(result); |
| 113 | return {}; |
| 114 | } |
| 115 | return collection; |
| 116 | } |
| 117 | |
| 118 | const_iterator begin() const override { |
| 119 | return mFiles.begin(); |
| 120 | } |
| 121 | |
| 122 | const_iterator end() const override { |
| 123 | return mFiles.end(); |
| 124 | } |
| 125 | |
| 126 | ~ZipFileCollection() override { |
| 127 | if (mHandle) { |
| 128 | CloseArchive(mHandle); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | ZipFileCollection() : mHandle(nullptr) { |
| 134 | } |
| 135 | |
| 136 | ZipArchiveHandle mHandle; |
| 137 | std::vector<std::unique_ptr<IFile>> mFiles; |
| 138 | }; |
| 139 | |
| 140 | } // namespace io |
| 141 | } // namespace aapt |
| 142 | |
| 143 | #endif /* AAPT_IO_ZIPARCHIVE_H */ |