diff options
Diffstat (limited to 'include')
l--------- | include/ziparchive | 1 | ||||
-rw-r--r-- | include/ziparchive/zip_archive.h | 233 | ||||
-rw-r--r-- | include/ziparchive/zip_archive_stream_entry.h | 46 | ||||
-rw-r--r-- | include/ziparchive/zip_writer.h | 185 |
4 files changed, 1 insertions, 464 deletions
diff --git a/include/ziparchive b/include/ziparchive new file mode 120000 index 000000000..d8fa4246b --- /dev/null +++ b/include/ziparchive | |||
@@ -0,0 +1 @@ | |||
../libziparchive/include/ziparchive \ No newline at end of file | |||
diff --git a/include/ziparchive/zip_archive.h b/include/ziparchive/zip_archive.h deleted file mode 100644 index 73ae68d3f..000000000 --- a/include/ziparchive/zip_archive.h +++ /dev/null | |||
@@ -1,233 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2013 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 | /* | ||
18 | * Read-only access to Zip archives, with minimal heap allocation. | ||
19 | */ | ||
20 | #ifndef LIBZIPARCHIVE_ZIPARCHIVE_H_ | ||
21 | #define LIBZIPARCHIVE_ZIPARCHIVE_H_ | ||
22 | |||
23 | #include <stdint.h> | ||
24 | #include <string.h> | ||
25 | #include <sys/cdefs.h> | ||
26 | #include <sys/types.h> | ||
27 | #include <utils/Compat.h> | ||
28 | |||
29 | /* Zip compression methods we support */ | ||
30 | enum { | ||
31 | kCompressStored = 0, // no compression | ||
32 | kCompressDeflated = 8, // standard deflate | ||
33 | }; | ||
34 | |||
35 | struct ZipString { | ||
36 | const uint8_t* name; | ||
37 | uint16_t name_length; | ||
38 | |||
39 | ZipString() {} | ||
40 | |||
41 | /* | ||
42 | * entry_name has to be an c-style string with only ASCII characters. | ||
43 | */ | ||
44 | explicit ZipString(const char* entry_name); | ||
45 | |||
46 | bool operator==(const ZipString& rhs) const { | ||
47 | return name && (name_length == rhs.name_length) && (memcmp(name, rhs.name, name_length) == 0); | ||
48 | } | ||
49 | |||
50 | bool StartsWith(const ZipString& prefix) const { | ||
51 | return name && (name_length >= prefix.name_length) && | ||
52 | (memcmp(name, prefix.name, prefix.name_length) == 0); | ||
53 | } | ||
54 | |||
55 | bool EndsWith(const ZipString& suffix) const { | ||
56 | return name && (name_length >= suffix.name_length) && | ||
57 | (memcmp(name + name_length - suffix.name_length, suffix.name, suffix.name_length) == 0); | ||
58 | } | ||
59 | }; | ||
60 | |||
61 | /* | ||
62 | * Represents information about a zip entry in a zip file. | ||
63 | */ | ||
64 | struct ZipEntry { | ||
65 | // Compression method: One of kCompressStored or | ||
66 | // kCompressDeflated. | ||
67 | uint16_t method; | ||
68 | |||
69 | // Modification time. The zipfile format specifies | ||
70 | // that the first two little endian bytes contain the time | ||
71 | // and the last two little endian bytes contain the date. | ||
72 | // See `GetModificationTime`. | ||
73 | // TODO: should be overridden by extra time field, if present. | ||
74 | uint32_t mod_time; | ||
75 | |||
76 | // Returns `mod_time` as a broken-down struct tm. | ||
77 | struct tm GetModificationTime() const; | ||
78 | |||
79 | // Suggested Unix mode for this entry, from the zip archive if created on | ||
80 | // Unix, or a default otherwise. | ||
81 | mode_t unix_mode; | ||
82 | |||
83 | // 1 if this entry contains a data descriptor segment, 0 | ||
84 | // otherwise. | ||
85 | uint8_t has_data_descriptor; | ||
86 | |||
87 | // Crc32 value of this ZipEntry. This information might | ||
88 | // either be stored in the local file header or in a special | ||
89 | // Data descriptor footer at the end of the file entry. | ||
90 | uint32_t crc32; | ||
91 | |||
92 | // Compressed length of this ZipEntry. Might be present | ||
93 | // either in the local file header or in the data descriptor | ||
94 | // footer. | ||
95 | uint32_t compressed_length; | ||
96 | |||
97 | // Uncompressed length of this ZipEntry. Might be present | ||
98 | // either in the local file header or in the data descriptor | ||
99 | // footer. | ||
100 | uint32_t uncompressed_length; | ||
101 | |||
102 | // The offset to the start of data for this ZipEntry. | ||
103 | off64_t offset; | ||
104 | }; | ||
105 | |||
106 | typedef void* ZipArchiveHandle; | ||
107 | |||
108 | /* | ||
109 | * Open a Zip archive, and sets handle to the value of the opaque | ||
110 | * handle for the file. This handle must be released by calling | ||
111 | * CloseArchive with this handle. | ||
112 | * | ||
113 | * Returns 0 on success, and negative values on failure. | ||
114 | */ | ||
115 | int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle); | ||
116 | |||
117 | /* | ||
118 | * Like OpenArchive, but takes a file descriptor open for reading | ||
119 | * at the start of the file. The descriptor must be mappable (this does | ||
120 | * not allow access to a stream). | ||
121 | * | ||
122 | * Sets handle to the value of the opaque handle for this file descriptor. | ||
123 | * This handle must be released by calling CloseArchive with this handle. | ||
124 | * | ||
125 | * If assume_ownership parameter is 'true' calling CloseArchive will close | ||
126 | * the file. | ||
127 | * | ||
128 | * This function maps and scans the central directory and builds a table | ||
129 | * of entries for future lookups. | ||
130 | * | ||
131 | * "debugFileName" will appear in error messages, but is not otherwise used. | ||
132 | * | ||
133 | * Returns 0 on success, and negative values on failure. | ||
134 | */ | ||
135 | int32_t OpenArchiveFd(const int fd, const char* debugFileName, ZipArchiveHandle* handle, | ||
136 | bool assume_ownership = true); | ||
137 | |||
138 | int32_t OpenArchiveFromMemory(void* address, size_t length, const char* debugFileName, | ||
139 | ZipArchiveHandle* handle); | ||
140 | /* | ||
141 | * Close archive, releasing resources associated with it. This will | ||
142 | * unmap the central directory of the zipfile and free all internal | ||
143 | * data structures associated with the file. It is an error to use | ||
144 | * this handle for any further operations without an intervening | ||
145 | * call to one of the OpenArchive variants. | ||
146 | */ | ||
147 | void CloseArchive(ZipArchiveHandle handle); | ||
148 | |||
149 | /* | ||
150 | * Find an entry in the Zip archive, by name. |entryName| must be a null | ||
151 | * terminated string, and |data| must point to a writeable memory location. | ||
152 | * | ||
153 | * Returns 0 if an entry is found, and populates |data| with information | ||
154 | * about this entry. Returns negative values otherwise. | ||
155 | * | ||
156 | * It's important to note that |data->crc32|, |data->compLen| and | ||
157 | * |data->uncompLen| might be set to values from the central directory | ||
158 | * if this file entry contains a data descriptor footer. To verify crc32s | ||
159 | * and length, a call to VerifyCrcAndLengths must be made after entry data | ||
160 | * has been processed. | ||
161 | * | ||
162 | * On non-Windows platforms this method does not modify internal state and | ||
163 | * can be called concurrently. | ||
164 | */ | ||
165 | int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName, ZipEntry* data); | ||
166 | |||
167 | /* | ||
168 | * Start iterating over all entries of a zip file. The order of iteration | ||
169 | * is not guaranteed to be the same as the order of elements | ||
170 | * in the central directory but is stable for a given zip file. |cookie| will | ||
171 | * contain the value of an opaque cookie which can be used to make one or more | ||
172 | * calls to Next. All calls to StartIteration must be matched by a call to | ||
173 | * EndIteration to free any allocated memory. | ||
174 | * | ||
175 | * This method also accepts optional prefix and suffix to restrict iteration to | ||
176 | * entry names that start with |optional_prefix| or end with |optional_suffix|. | ||
177 | * | ||
178 | * Returns 0 on success and negative values on failure. | ||
179 | */ | ||
180 | int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, const ZipString* optional_prefix, | ||
181 | const ZipString* optional_suffix); | ||
182 | |||
183 | /* | ||
184 | * Advance to the next element in the zipfile in iteration order. | ||
185 | * | ||
186 | * Returns 0 on success, -1 if there are no more elements in this | ||
187 | * archive and lower negative values on failure. | ||
188 | */ | ||
189 | int32_t Next(void* cookie, ZipEntry* data, ZipString* name); | ||
190 | |||
191 | /* | ||
192 | * End iteration over all entries of a zip file and frees the memory allocated | ||
193 | * in StartIteration. | ||
194 | */ | ||
195 | void EndIteration(void* cookie); | ||
196 | |||
197 | /* | ||
198 | * Uncompress and write an entry to an open file identified by |fd|. | ||
199 | * |entry->uncompressed_length| bytes will be written to the file at | ||
200 | * its current offset, and the file will be truncated at the end of | ||
201 | * the uncompressed data (no truncation if |fd| references a block | ||
202 | * device). | ||
203 | * | ||
204 | * Returns 0 on success and negative values on failure. | ||
205 | */ | ||
206 | int32_t ExtractEntryToFile(ZipArchiveHandle handle, ZipEntry* entry, int fd); | ||
207 | |||
208 | /** | ||
209 | * Uncompress a given zip entry to the memory region at |begin| and of | ||
210 | * size |size|. This size is expected to be the same as the *declared* | ||
211 | * uncompressed length of the zip entry. It is an error if the *actual* | ||
212 | * number of uncompressed bytes differs from this number. | ||
213 | * | ||
214 | * Returns 0 on success and negative values on failure. | ||
215 | */ | ||
216 | int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, uint8_t* begin, uint32_t size); | ||
217 | |||
218 | int GetFileDescriptor(const ZipArchiveHandle handle); | ||
219 | |||
220 | const char* ErrorCodeString(int32_t error_code); | ||
221 | |||
222 | #if !defined(_WIN32) | ||
223 | typedef bool (*ProcessZipEntryFunction)(const uint8_t* buf, size_t buf_size, void* cookie); | ||
224 | |||
225 | /* | ||
226 | * Stream the uncompressed data through the supplied function, | ||
227 | * passing cookie to it each time it gets called. | ||
228 | */ | ||
229 | int32_t ProcessZipEntryContents(ZipArchiveHandle handle, ZipEntry* entry, | ||
230 | ProcessZipEntryFunction func, void* cookie); | ||
231 | #endif | ||
232 | |||
233 | #endif // LIBZIPARCHIVE_ZIPARCHIVE_H_ | ||
diff --git a/include/ziparchive/zip_archive_stream_entry.h b/include/ziparchive/zip_archive_stream_entry.h deleted file mode 100644 index a40b799cf..000000000 --- a/include/ziparchive/zip_archive_stream_entry.h +++ /dev/null | |||
@@ -1,46 +0,0 @@ | |||
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 | // Read-only stream access to Zip archives entries. | ||
18 | #ifndef LIBZIPARCHIVE_ZIPARCHIVESTREAMENTRY_H_ | ||
19 | #define LIBZIPARCHIVE_ZIPARCHIVESTREAMENTRY_H_ | ||
20 | |||
21 | #include <vector> | ||
22 | |||
23 | #include <ziparchive/zip_archive.h> | ||
24 | |||
25 | class ZipArchiveStreamEntry { | ||
26 | public: | ||
27 | virtual ~ZipArchiveStreamEntry() {} | ||
28 | |||
29 | virtual const std::vector<uint8_t>* Read() = 0; | ||
30 | |||
31 | virtual bool Verify() = 0; | ||
32 | |||
33 | static ZipArchiveStreamEntry* Create(ZipArchiveHandle handle, const ZipEntry& entry); | ||
34 | static ZipArchiveStreamEntry* CreateRaw(ZipArchiveHandle handle, const ZipEntry& entry); | ||
35 | |||
36 | protected: | ||
37 | ZipArchiveStreamEntry(ZipArchiveHandle handle) : handle_(handle) {} | ||
38 | |||
39 | virtual bool Init(const ZipEntry& entry); | ||
40 | |||
41 | ZipArchiveHandle handle_; | ||
42 | |||
43 | uint32_t crc32_; | ||
44 | }; | ||
45 | |||
46 | #endif // LIBZIPARCHIVE_ZIPARCHIVESTREAMENTRY_H_ | ||
diff --git a/include/ziparchive/zip_writer.h b/include/ziparchive/zip_writer.h deleted file mode 100644 index 94595143d..000000000 --- a/include/ziparchive/zip_writer.h +++ /dev/null | |||
@@ -1,185 +0,0 @@ | |||
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 LIBZIPARCHIVE_ZIPWRITER_H_ | ||
18 | #define LIBZIPARCHIVE_ZIPWRITER_H_ | ||
19 | |||
20 | #include <zlib.h> | ||
21 | #include <cstdio> | ||
22 | #include <ctime> | ||
23 | |||
24 | #include <memory> | ||
25 | #include <string> | ||
26 | #include <vector> | ||
27 | |||
28 | #include "android-base/macros.h" | ||
29 | #include "utils/Compat.h" | ||
30 | |||
31 | /** | ||
32 | * Writes a Zip file via a stateful interface. | ||
33 | * | ||
34 | * Example: | ||
35 | * | ||
36 | * FILE* file = fopen("path/to/zip.zip", "wb"); | ||
37 | * | ||
38 | * ZipWriter writer(file); | ||
39 | * | ||
40 | * writer.StartEntry("test.txt", ZipWriter::kCompress | ZipWriter::kAlign); | ||
41 | * writer.WriteBytes(buffer, bufferLen); | ||
42 | * writer.WriteBytes(buffer2, bufferLen2); | ||
43 | * writer.FinishEntry(); | ||
44 | * | ||
45 | * writer.StartEntry("empty.txt", 0); | ||
46 | * writer.FinishEntry(); | ||
47 | * | ||
48 | * writer.Finish(); | ||
49 | * | ||
50 | * fclose(file); | ||
51 | */ | ||
52 | class ZipWriter { | ||
53 | public: | ||
54 | enum { | ||
55 | /** | ||
56 | * Flag to compress the zip entry using deflate. | ||
57 | */ | ||
58 | kCompress = 0x01, | ||
59 | |||
60 | /** | ||
61 | * Flag to align the zip entry data on a 32bit boundary. Useful for | ||
62 | * mmapping the data at runtime. | ||
63 | */ | ||
64 | kAlign32 = 0x02, | ||
65 | }; | ||
66 | |||
67 | /** | ||
68 | * A struct representing a zip file entry. | ||
69 | */ | ||
70 | struct FileEntry { | ||
71 | std::string path; | ||
72 | uint16_t compression_method; | ||
73 | uint32_t crc32; | ||
74 | uint32_t compressed_size; | ||
75 | uint32_t uncompressed_size; | ||
76 | uint16_t last_mod_time; | ||
77 | uint16_t last_mod_date; | ||
78 | uint32_t padding_length; | ||
79 | off64_t local_file_header_offset; | ||
80 | }; | ||
81 | |||
82 | static const char* ErrorCodeString(int32_t error_code); | ||
83 | |||
84 | /** | ||
85 | * Create a ZipWriter that will write into a FILE stream. The file should be opened with | ||
86 | * open mode of "wb" or "w+b". ZipWriter does not take ownership of the file stream. The | ||
87 | * caller is responsible for closing the file. | ||
88 | */ | ||
89 | explicit ZipWriter(FILE* f); | ||
90 | |||
91 | // Move constructor. | ||
92 | ZipWriter(ZipWriter&& zipWriter); | ||
93 | |||
94 | // Move assignment. | ||
95 | ZipWriter& operator=(ZipWriter&& zipWriter); | ||
96 | |||
97 | /** | ||
98 | * Starts a new zip entry with the given path and flags. | ||
99 | * Flags can be a bitwise OR of ZipWriter::kCompress and ZipWriter::kAlign. | ||
100 | * Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry. | ||
101 | * Returns 0 on success, and an error value < 0 on failure. | ||
102 | */ | ||
103 | int32_t StartEntry(const char* path, size_t flags); | ||
104 | |||
105 | /** | ||
106 | * Starts a new zip entry with the given path and flags, where the | ||
107 | * entry will be aligned to the given alignment. | ||
108 | * Flags can only be ZipWriter::kCompress. Using the flag ZipWriter::kAlign32 | ||
109 | * will result in an error. | ||
110 | * Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry. | ||
111 | * Returns 0 on success, and an error value < 0 on failure. | ||
112 | */ | ||
113 | int32_t StartAlignedEntry(const char* path, size_t flags, uint32_t alignment); | ||
114 | |||
115 | /** | ||
116 | * Same as StartEntry(const char*, size_t), but sets a last modified time for the entry. | ||
117 | */ | ||
118 | int32_t StartEntryWithTime(const char* path, size_t flags, time_t time); | ||
119 | |||
120 | /** | ||
121 | * Same as StartAlignedEntry(const char*, size_t), but sets a last modified time for the entry. | ||
122 | */ | ||
123 | int32_t StartAlignedEntryWithTime(const char* path, size_t flags, time_t time, uint32_t alignment); | ||
124 | |||
125 | /** | ||
126 | * Writes bytes to the zip file for the previously started zip entry. | ||
127 | * Returns 0 on success, and an error value < 0 on failure. | ||
128 | */ | ||
129 | int32_t WriteBytes(const void* data, size_t len); | ||
130 | |||
131 | /** | ||
132 | * Finish a zip entry started with StartEntry(const char*, size_t) or | ||
133 | * StartEntryWithTime(const char*, size_t, time_t). This must be called before | ||
134 | * any new zip entries are started, or before Finish() is called. | ||
135 | * Returns 0 on success, and an error value < 0 on failure. | ||
136 | */ | ||
137 | int32_t FinishEntry(); | ||
138 | |||
139 | /** | ||
140 | * Discards the last-written entry. Can only be called after an entry has been written using | ||
141 | * FinishEntry(). | ||
142 | * Returns 0 on success, and an error value < 0 on failure. | ||
143 | */ | ||
144 | int32_t DiscardLastEntry(); | ||
145 | |||
146 | /** | ||
147 | * Sets `out_entry` to the last entry written after a call to FinishEntry(). | ||
148 | * Returns 0 on success, and an error value < 0 if no entries have been written. | ||
149 | */ | ||
150 | int32_t GetLastEntry(FileEntry* out_entry); | ||
151 | |||
152 | /** | ||
153 | * Writes the Central Directory Headers and flushes the zip file stream. | ||
154 | * Returns 0 on success, and an error value < 0 on failure. | ||
155 | */ | ||
156 | int32_t Finish(); | ||
157 | |||
158 | private: | ||
159 | DISALLOW_COPY_AND_ASSIGN(ZipWriter); | ||
160 | |||
161 | int32_t HandleError(int32_t error_code); | ||
162 | int32_t PrepareDeflate(); | ||
163 | int32_t StoreBytes(FileEntry* file, const void* data, size_t len); | ||
164 | int32_t CompressBytes(FileEntry* file, const void* data, size_t len); | ||
165 | int32_t FlushCompressedBytes(FileEntry* file); | ||
166 | |||
167 | enum class State { | ||
168 | kWritingZip, | ||
169 | kWritingEntry, | ||
170 | kDone, | ||
171 | kError, | ||
172 | }; | ||
173 | |||
174 | FILE* file_; | ||
175 | bool seekable_; | ||
176 | off64_t current_offset_; | ||
177 | State state_; | ||
178 | std::vector<FileEntry> files_; | ||
179 | FileEntry current_file_entry_; | ||
180 | |||
181 | std::unique_ptr<z_stream, void (*)(z_stream*)> z_stream_; | ||
182 | std::vector<uint8_t> buffer_; | ||
183 | }; | ||
184 | |||
185 | #endif /* LIBZIPARCHIVE_ZIPWRITER_H_ */ | ||