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 | |
| 17 | #include "Source.h" |
| 18 | #include "io/ZipArchive.h" |
| 19 | #include "util/Util.h" |
| 20 | |
| 21 | #include <utils/FileMap.h> |
| 22 | #include <ziparchive/zip_archive.h> |
| 23 | |
| 24 | namespace aapt { |
| 25 | namespace io { |
| 26 | |
| 27 | ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source) : |
| 28 | mZipHandle(handle), mZipEntry(entry), mSource(source) { |
| 29 | } |
| 30 | |
| 31 | std::unique_ptr<IData> ZipFile::openAsData() { |
| 32 | if (mZipEntry.method == kCompressStored) { |
| 33 | int fd = GetFileDescriptor(mZipHandle); |
| 34 | |
| 35 | android::FileMap fileMap; |
| 36 | bool result = fileMap.create(nullptr, fd, mZipEntry.offset, |
| 37 | mZipEntry.uncompressed_length, true); |
| 38 | if (!result) { |
| 39 | return {}; |
| 40 | } |
| 41 | return util::make_unique<MmappedData>(std::move(fileMap)); |
| 42 | |
| 43 | } else { |
| 44 | std::unique_ptr<uint8_t[]> data = std::unique_ptr<uint8_t[]>( |
| 45 | new uint8_t[mZipEntry.uncompressed_length]); |
| 46 | int32_t result = ExtractToMemory(mZipHandle, &mZipEntry, data.get(), |
| 47 | static_cast<uint32_t>(mZipEntry.uncompressed_length)); |
| 48 | if (result != 0) { |
| 49 | return {}; |
| 50 | } |
| 51 | return util::make_unique<MallocData>(std::move(data), mZipEntry.uncompressed_length); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | const Source& ZipFile::getSource() const { |
| 56 | return mSource; |
| 57 | } |
| 58 | |
| 59 | ZipFileCollectionIterator::ZipFileCollectionIterator(ZipFileCollection* collection) : |
| 60 | mCurrent(collection->mFiles.begin()), mEnd(collection->mFiles.end()) { |
| 61 | } |
| 62 | |
| 63 | bool ZipFileCollectionIterator::hasNext() { |
| 64 | return mCurrent != mEnd; |
| 65 | } |
| 66 | |
| 67 | IFile* ZipFileCollectionIterator::next() { |
| 68 | IFile* result = mCurrent->second.get(); |
| 69 | ++mCurrent; |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | ZipFileCollection::ZipFileCollection() : mHandle(nullptr) { |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<ZipFileCollection> ZipFileCollection::create(const StringPiece& path, |
| 77 | std::string* outError) { |
| 78 | std::unique_ptr<ZipFileCollection> collection = std::unique_ptr<ZipFileCollection>( |
| 79 | new ZipFileCollection()); |
| 80 | |
| 81 | int32_t result = OpenArchive(path.data(), &collection->mHandle); |
| 82 | if (result != 0) { |
| 83 | if (outError) *outError = ErrorCodeString(result); |
| 84 | return {}; |
| 85 | } |
| 86 | |
| 87 | ZipString suffix(".flat"); |
| 88 | void* cookie = nullptr; |
| 89 | result = StartIteration(collection->mHandle, &cookie, nullptr, &suffix); |
| 90 | if (result != 0) { |
| 91 | if (outError) *outError = ErrorCodeString(result); |
| 92 | return {}; |
| 93 | } |
| 94 | |
| 95 | using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>; |
| 96 | IterationEnder iterationEnder(cookie, EndIteration); |
| 97 | |
| 98 | ZipString zipEntryName; |
| 99 | ZipEntry zipData; |
| 100 | while ((result = Next(cookie, &zipData, &zipEntryName)) == 0) { |
| 101 | std::string zipEntryPath = std::string(reinterpret_cast<const char*>(zipEntryName.name), |
| 102 | zipEntryName.name_length); |
| 103 | std::string nestedPath = path.toString() + "@" + zipEntryPath; |
| 104 | collection->mFiles[zipEntryPath] = util::make_unique<ZipFile>(collection->mHandle, |
| 105 | zipData, |
| 106 | Source(nestedPath)); |
| 107 | } |
| 108 | |
| 109 | if (result != -1) { |
| 110 | if (outError) *outError = ErrorCodeString(result); |
| 111 | return {}; |
| 112 | } |
| 113 | return collection; |
| 114 | } |
| 115 | |
| 116 | IFile* ZipFileCollection::findFile(const StringPiece& path) { |
| 117 | auto iter = mFiles.find(path.toString()); |
| 118 | if (iter != mFiles.end()) { |
| 119 | return iter->second.get(); |
| 120 | } |
| 121 | return nullptr; |
| 122 | } |
| 123 | |
| 124 | std::unique_ptr<IFileCollectionIterator> ZipFileCollection::iterator() { |
| 125 | return util::make_unique<ZipFileCollectionIterator>(this); |
| 126 | } |
| 127 | |
| 128 | ZipFileCollection::~ZipFileCollection() { |
| 129 | if (mHandle) { |
| 130 | CloseArchive(mHandle); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } // namespace io |
| 135 | } // namespace aapt |