blob: 329dac9163ce9d0aab7a5c36fec61a9f35b4c915 [file] [log] [blame]
Adam Lesinskia6fe3452015-12-09 15:20:52 -08001/*
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
24namespace aapt {
25namespace io {
26
27ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const Source& source) :
28 mZipHandle(handle), mZipEntry(entry), mSource(source) {
29}
30
31std::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
55const Source& ZipFile::getSource() const {
56 return mSource;
57}
58
59ZipFileCollectionIterator::ZipFileCollectionIterator(ZipFileCollection* collection) :
60 mCurrent(collection->mFiles.begin()), mEnd(collection->mFiles.end()) {
61}
62
63bool ZipFileCollectionIterator::hasNext() {
64 return mCurrent != mEnd;
65}
66
67IFile* ZipFileCollectionIterator::next() {
68 IFile* result = mCurrent->second.get();
69 ++mCurrent;
70 return result;
71}
72
73ZipFileCollection::ZipFileCollection() : mHandle(nullptr) {
74}
75
76std::unique_ptr<ZipFileCollection> ZipFileCollection::create(const StringPiece& path,
77 std::string* outError) {
Adam Lesinskic446a732016-01-21 11:04:46 -080078 constexpr static const int32_t kEmptyArchive = -6;
79
Adam Lesinskia6fe3452015-12-09 15:20:52 -080080 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) {
Adam Lesinskic446a732016-01-21 11:04:46 -080085 // If a zip is empty, result will be an error code. This is fine and we should
86 // return an empty ZipFileCollection.
87 if (result == kEmptyArchive) {
88 return collection;
89 }
90
Adam Lesinskia6fe3452015-12-09 15:20:52 -080091 if (outError) *outError = ErrorCodeString(result);
92 return {};
93 }
94
95 ZipString suffix(".flat");
96 void* cookie = nullptr;
97 result = StartIteration(collection->mHandle, &cookie, nullptr, &suffix);
98 if (result != 0) {
99 if (outError) *outError = ErrorCodeString(result);
100 return {};
101 }
102
103 using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>;
104 IterationEnder iterationEnder(cookie, EndIteration);
105
106 ZipString zipEntryName;
107 ZipEntry zipData;
108 while ((result = Next(cookie, &zipData, &zipEntryName)) == 0) {
109 std::string zipEntryPath = std::string(reinterpret_cast<const char*>(zipEntryName.name),
110 zipEntryName.name_length);
111 std::string nestedPath = path.toString() + "@" + zipEntryPath;
112 collection->mFiles[zipEntryPath] = util::make_unique<ZipFile>(collection->mHandle,
113 zipData,
114 Source(nestedPath));
115 }
116
117 if (result != -1) {
118 if (outError) *outError = ErrorCodeString(result);
119 return {};
120 }
121 return collection;
122}
123
124IFile* ZipFileCollection::findFile(const StringPiece& path) {
125 auto iter = mFiles.find(path.toString());
126 if (iter != mFiles.end()) {
127 return iter->second.get();
128 }
129 return nullptr;
130}
131
132std::unique_ptr<IFileCollectionIterator> ZipFileCollection::iterator() {
133 return util::make_unique<ZipFileCollectionIterator>(this);
134}
135
136ZipFileCollection::~ZipFileCollection() {
137 if (mHandle) {
138 CloseArchive(mHandle);
139 }
140}
141
142} // namespace io
143} // namespace aapt