v2 / thirdparty / zip / zip.h
594 lines · 545 sloc · 19.24 KB · 79bedff1766a86163e3412cd74fc4a9d92c27148
Raw
1/*
2 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
5 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
6 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
7 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
8 * OTHER DEALINGS IN THE SOFTWARE.
9 */
10
11#pragma once
12#ifndef ZIP_H
13#define ZIP_H
14
15#include <stdint.h>
16#include <stdio.h>
17#include <string.h>
18#include <sys/types.h>
19
20#ifndef ZIP_SHARED
21#define ZIP_EXPORT
22#else
23#ifdef _WIN32
24#ifdef ZIP_BUILD_SHARED
25#define ZIP_EXPORT __declspec(dllexport)
26#else
27#define ZIP_EXPORT __declspec(dllimport)
28#endif
29#else
30#define ZIP_EXPORT __attribute__((visibility("default")))
31#endif
32#endif
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38#if !defined(_POSIX_C_SOURCE) && defined(_MSC_VER)
39// 64-bit Windows is the only mainstream platform
40// where sizeof(long) != sizeof(void*)
41#ifdef _WIN64
42typedef long long ssize_t; /* byte count or error */
43#else
44typedef long ssize_t; /* byte count or error */
45#endif
46#endif
47
48/**
49 * @mainpage
50 *
51 * Documentation for @ref zip.
52 */
53
54/**
55 * @addtogroup zip
56 * @{
57 */
58
59/**
60 * Default zip compression level.
61 */
62#define ZIP_DEFAULT_COMPRESSION_LEVEL 6
63
64/**
65 * Error codes
66 */
67#define ZIP_ENOINIT -1 // not initialized
68#define ZIP_EINVENTNAME -2 // invalid entry name
69#define ZIP_ENOENT -3 // entry not found
70#define ZIP_EINVMODE -4 // invalid zip mode
71#define ZIP_EINVLVL -5 // invalid compression level
72#define ZIP_ENOSUP64 -6 // no zip 64 support
73#define ZIP_EMEMSET -7 // memset error
74#define ZIP_EWRTENT -8 // cannot write data to entry
75#define ZIP_ETDEFLINIT -9 // cannot initialize tdefl compressor
76#define ZIP_EINVIDX -10 // invalid index
77#define ZIP_ENOHDR -11 // header not found
78#define ZIP_ETDEFLBUF -12 // cannot flush tdefl buffer
79#define ZIP_ECRTHDR -13 // cannot create entry header
80#define ZIP_EWRTHDR -14 // cannot write entry header
81#define ZIP_EWRTDIR -15 // cannot write to central dir
82#define ZIP_EOPNFILE -16 // cannot open file
83#define ZIP_EINVENTTYPE -17 // invalid entry type
84#define ZIP_EMEMNOALLOC -18 // extracting data using no memory allocation
85#define ZIP_ENOFILE -19 // file not found
86#define ZIP_ENOPERM -20 // no permission
87#define ZIP_EOOMEM -21 // out of memory
88#define ZIP_EINVZIPNAME -22 // invalid zip archive name
89#define ZIP_EMKDIR -23 // make dir error
90#define ZIP_ESYMLINK -24 // symlink error
91#define ZIP_ECLSZIP -25 // close archive error
92#define ZIP_ECAPSIZE -26 // capacity size too small
93#define ZIP_EFSEEK -27 // fseek error
94#define ZIP_EFREAD -28 // fread error
95#define ZIP_EFWRITE -29 // fwrite error
96#define ZIP_ERINIT -30 // cannot initialize reader
97#define ZIP_EWINIT -31 // cannot initialize writer
98#define ZIP_EWRINIT -32 // cannot initialize writer from reader
99
100/**
101 * Looks up the error message string corresponding to an error number.
102 * @param errnum error number
103 * @return error message string corresponding to errnum or NULL if error is not
104 * found.
105 */
106extern ZIP_EXPORT const char *zip_strerror(int errnum);
107
108/**
109 * @struct zip_t
110 *
111 * This data structure is used throughout the library to represent zip archive -
112 * forward declaration.
113 */
114struct zip_t;
115
116/**
117 * Opens zip archive with compression level using the given mode.
118 *
119 * @param zipname zip archive file name.
120 * @param level compression level (0-9 are the standard zlib-style levels).
121 * @param mode file access mode.
122 * - 'r': opens a file for reading/extracting (the file must exists).
123 * - 'w': creates an empty file for writing.
124 * - 'a': appends to an existing archive.
125 *
126 * @return the zip archive handler or NULL on error
127 */
128extern ZIP_EXPORT struct zip_t *zip_open(const char *zipname, int level,
129 char mode);
130
131/**
132 * Opens zip archive with compression level using the given mode.
133 * The function additionally returns @param errnum -
134 *
135 * @param zipname zip archive file name.
136 * @param level compression level (0-9 are the standard zlib-style levels).
137 * @param mode file access mode.
138 * - 'r': opens a file for reading/extracting (the file must exists).
139 * - 'w': creates an empty file for writing.
140 * - 'a': appends to an existing archive.
141 * @param errnum 0 on success, negative number (< 0) on error.
142 *
143 * @return the zip archive handler or NULL on error
144 */
145extern ZIP_EXPORT struct zip_t *
146zip_openwitherror(const char *zipname, int level, char mode, int *errnum);
147
148/**
149 * Closes the zip archive, releases resources - always finalize.
150 *
151 * @param zip zip archive handler.
152 */
153extern ZIP_EXPORT void zip_close(struct zip_t *zip);
154
155/**
156 * Determines if the archive has a zip64 end of central directory headers.
157 *
158 * @param zip zip archive handler.
159 *
160 * @return the return code - 1 (true), 0 (false), negative number (< 0) on
161 * error.
162 */
163extern ZIP_EXPORT int zip_is64(struct zip_t *zip);
164
165/**
166 * Returns the offset in the stream where the zip header is located.
167 *
168 * @param zip zip archive handler.
169 * @param offset zip header offset.
170 *
171 * @return the return code - 0 if successful, negative number (< 0) on error.
172 */
173extern ZIP_EXPORT int zip_offset(struct zip_t *zip, uint64_t *offset);
174
175/**
176 * Opens an entry by name in the zip archive.
177 *
178 * For zip archive opened in 'w' or 'a' mode the function will append
179 * a new entry. In readonly mode the function tries to locate the entry
180 * in global dictionary.
181 *
182 * @param zip zip archive handler.
183 * @param entryname an entry name in local dictionary.
184 *
185 * @return the return code - 0 on success, negative number (< 0) on error.
186 */
187extern ZIP_EXPORT int zip_entry_open(struct zip_t *zip, const char *entryname);
188
189/**
190 * Opens an entry by name in the zip archive.
191 *
192 * For zip archive opened in 'w' or 'a' mode the function will append
193 * a new entry. In readonly mode the function tries to locate the entry
194 * in global dictionary (case sensitive).
195 *
196 * @param zip zip archive handler.
197 * @param entryname an entry name in local dictionary (case sensitive).
198 *
199 * @return the return code - 0 on success, negative number (< 0) on error.
200 */
201extern ZIP_EXPORT int zip_entry_opencasesensitive(struct zip_t *zip,
202 const char *entryname);
203
204/**
205 * Opens a new entry by index in the zip archive.
206 *
207 * This function is only valid if zip archive was opened in 'r' (readonly) mode.
208 *
209 * @param zip zip archive handler.
210 * @param index index in local dictionary.
211 *
212 * @return the return code - 0 on success, negative number (< 0) on error.
213 */
214extern ZIP_EXPORT int zip_entry_openbyindex(struct zip_t *zip, size_t index);
215
216/**
217 * Closes a zip entry, flushes buffer and releases resources.
218 *
219 * @param zip zip archive handler.
220 *
221 * @return the return code - 0 on success, negative number (< 0) on error.
222 */
223extern ZIP_EXPORT int zip_entry_close(struct zip_t *zip);
224
225/**
226 * Returns a local name of the current zip entry.
227 *
228 * The main difference between user's entry name and local entry name
229 * is optional relative path.
230 * Following .ZIP File Format Specification - the path stored MUST not contain
231 * a drive or device letter, or a leading slash.
232 * All slashes MUST be forward slashes '/' as opposed to backwards slashes '\'
233 * for compatibility with Amiga and UNIX file systems etc.
234 *
235 * @param zip: zip archive handler.
236 *
237 * @return the pointer to the current zip entry name, or NULL on error.
238 */
239extern ZIP_EXPORT const char *zip_entry_name(struct zip_t *zip);
240
241/**
242 * Returns an index of the current zip entry.
243 *
244 * @param zip zip archive handler.
245 *
246 * @return the index on success, negative number (< 0) on error.
247 */
248extern ZIP_EXPORT ssize_t zip_entry_index(struct zip_t *zip);
249
250/**
251 * Determines if the current zip entry is a directory entry.
252 *
253 * @param zip zip archive handler.
254 *
255 * @return the return code - 1 (true), 0 (false), negative number (< 0) on
256 * error.
257 */
258extern ZIP_EXPORT int zip_entry_isdir(struct zip_t *zip);
259
260/**
261 * Returns the uncompressed size of the current zip entry.
262 * Alias for zip_entry_uncomp_size (for backward compatibility).
263 *
264 * @param zip zip archive handler.
265 *
266 * @return the uncompressed size in bytes.
267 */
268extern ZIP_EXPORT unsigned long long zip_entry_size(struct zip_t *zip);
269
270/**
271 * Returns the uncompressed size of the current zip entry.
272 *
273 * @param zip zip archive handler.
274 *
275 * @return the uncompressed size in bytes.
276 */
277extern ZIP_EXPORT unsigned long long zip_entry_uncomp_size(struct zip_t *zip);
278
279/**
280 * Returns the compressed size of the current zip entry.
281 *
282 * @param zip zip archive handler.
283 *
284 * @return the compressed size in bytes.
285 */
286extern ZIP_EXPORT unsigned long long zip_entry_comp_size(struct zip_t *zip);
287
288/**
289 * Returns CRC-32 checksum of the current zip entry.
290 *
291 * @param zip zip archive handler.
292 *
293 * @return the CRC-32 checksum.
294 */
295extern ZIP_EXPORT unsigned int zip_entry_crc32(struct zip_t *zip);
296
297/**
298 * Returns byte offset of the current zip entry
299 * in the archive's central directory.
300 *
301 * @param zip zip archive handler.
302 *
303 * @return the offset in bytes.
304 */
305extern ZIP_EXPORT unsigned long long zip_entry_dir_offset(struct zip_t *zip);
306
307/**
308 * Returns the current zip entry's local header file offset in bytes.
309 *
310 * @param zip zip archive handler.
311 *
312 * @return the entry's local header file offset in bytes.
313 */
314extern ZIP_EXPORT unsigned long long zip_entry_header_offset(struct zip_t *zip);
315
316/**
317 * Compresses an input buffer for the current zip entry.
318 *
319 * @param zip zip archive handler.
320 * @param buf input buffer.
321 * @param bufsize input buffer size (in bytes).
322 *
323 * @return the return code - 0 on success, negative number (< 0) on error.
324 */
325extern ZIP_EXPORT int zip_entry_write(struct zip_t *zip, const void *buf,
326 size_t bufsize);
327
328/**
329 * Compresses a file for the current zip entry.
330 *
331 * @param zip zip archive handler.
332 * @param filename input file.
333 *
334 * @return the return code - 0 on success, negative number (< 0) on error.
335 */
336extern ZIP_EXPORT int zip_entry_fwrite(struct zip_t *zip, const char *filename);
337
338/**
339 * Extracts the current zip entry into output buffer.
340 *
341 * The function allocates sufficient memory for a output buffer.
342 *
343 * @param zip zip archive handler.
344 * @param buf output buffer.
345 * @param bufsize output buffer size (in bytes).
346 *
347 * @note remember to release memory allocated for a output buffer.
348 * for large entries, please take a look at zip_entry_extract function.
349 *
350 * @return the return code - the number of bytes actually read on success.
351 * Otherwise a negative number (< 0) on error.
352 */
353extern ZIP_EXPORT ssize_t zip_entry_read(struct zip_t *zip, void **buf,
354 size_t *bufsize);
355
356/**
357 * Extracts the current zip entry into a memory buffer using no memory
358 * allocation.
359 *
360 * @param zip zip archive handler.
361 * @param buf preallocated output buffer.
362 * @param bufsize output buffer size (in bytes).
363 *
364 * @note ensure supplied output buffer is large enough.
365 * zip_entry_size function (returns uncompressed size for the current
366 * entry) can be handy to estimate how big buffer is needed.
367 * For large entries, please take a look at zip_entry_extract function.
368 *
369 * @return the return code - the number of bytes actually read on success.
370 * Otherwise a negative number (< 0) on error (e.g. bufsize is not large
371 * enough).
372 */
373extern ZIP_EXPORT ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf,
374 size_t bufsize);
375
376/**
377 * Extracts the current zip entry into output file.
378 *
379 * @param zip zip archive handler.
380 * @param filename output file.
381 *
382 * @return the return code - 0 on success, negative number (< 0) on error.
383 */
384extern ZIP_EXPORT int zip_entry_fread(struct zip_t *zip, const char *filename);
385
386/**
387 * Extracts the current zip entry using a callback function (on_extract).
388 *
389 * @param zip zip archive handler.
390 * @param on_extract callback function.
391 * @param arg opaque pointer (optional argument, which you can pass to the
392 * on_extract callback)
393 *
394 * @return the return code - 0 on success, negative number (< 0) on error.
395 */
396extern ZIP_EXPORT int
397zip_entry_extract(struct zip_t *zip,
398 size_t (*on_extract)(void *arg, uint64_t offset,
399 const void *data, size_t size),
400 void *arg);
401
402/**
403 * Returns the number of all entries (files and directories) in the zip archive.
404 *
405 * @param zip zip archive handler.
406 *
407 * @return the return code - the number of entries on success, negative number
408 * (< 0) on error.
409 */
410extern ZIP_EXPORT ssize_t zip_entries_total(struct zip_t *zip);
411
412/**
413 * Deletes zip archive entries.
414 *
415 * @param zip zip archive handler.
416 * @param entries array of zip archive entries to be deleted.
417 * @param len the number of entries to be deleted.
418 * @return the number of deleted entries, or negative number (< 0) on error.
419 */
420extern ZIP_EXPORT ssize_t zip_entries_delete(struct zip_t *zip,
421 char *const entries[], size_t len);
422
423/**
424 * Deletes zip archive entries.
425 *
426 * @param zip zip archive handler.
427 * @param entries array of zip archive entries indices to be deleted.
428 * @param len the number of entries to be deleted.
429 * @return the number of deleted entries, or negative number (< 0) on error.
430 */
431extern ZIP_EXPORT ssize_t zip_entries_deletebyindex(struct zip_t *zip,
432 size_t entries[],
433 size_t len);
434
435/**
436 * Extracts a zip archive stream into directory.
437 *
438 * If on_extract is not NULL, the callback will be called after
439 * successfully extracted each zip entry.
440 * Returning a negative value from the callback will cause abort and return an
441 * error. The last argument (void *arg) is optional, which you can use to pass
442 * data to the on_extract callback.
443 *
444 * @param stream zip archive stream.
445 * @param size stream size.
446 * @param dir output directory.
447 * @param on_extract on extract callback.
448 * @param arg opaque pointer.
449 *
450 * @return the return code - 0 on success, negative number (< 0) on error.
451 */
452extern ZIP_EXPORT int
453zip_stream_extract(const char *stream, size_t size, const char *dir,
454 int (*on_extract)(const char *filename, void *arg),
455 void *arg);
456
457/**
458 * Opens zip archive stream into memory.
459 *
460 * @param stream zip archive stream.
461 * @param size stream size.
462 * @param level compression level (0-9 are the standard zlib-style levels).
463 * @param mode file access mode.
464 * - 'r': opens a file for reading/extracting (the file must exists).
465 * - 'w': creates an empty file for writing.
466 * - 'a': appends to an existing archive.
467 *
468 * @return the zip archive handler or NULL on error
469 */
470extern ZIP_EXPORT struct zip_t *zip_stream_open(const char *stream, size_t size,
471 int level, char mode);
472
473/**
474 * Opens zip archive stream into memory.
475 * The function additionally returns @param errnum -
476 *
477 * @param stream zip archive stream.
478 * @param size stream size.*
479 * @param level compression level (0-9 are the standard zlib-style levels).
480 * @param mode file access mode.
481 * - 'r': opens a file for reading/extracting (the file must exists).
482 * - 'w': creates an empty file for writing.
483 * - 'a': appends to an existing archive.
484 * @param errnum 0 on success, negative number (< 0) on error.
485 *
486 * @return the zip archive handler or NULL on error
487 */
488extern ZIP_EXPORT struct zip_t *zip_stream_openwitherror(const char *stream,
489 size_t size, int level,
490 char mode,
491 int *errnum);
492
493/**
494 * Copy zip archive stream output buffer.
495 *
496 * @param zip zip archive handler.
497 * @param buf output buffer. User should free buf.
498 * @param bufsize output buffer size (in bytes).
499 *
500 * @return copy size
501 */
502extern ZIP_EXPORT ssize_t zip_stream_copy(struct zip_t *zip, void **buf,
503 size_t *bufsize);
504
505/**
506 * Close zip archive releases resources.
507 *
508 * @param zip zip archive handler.
509 *
510 * @return
511 */
512extern ZIP_EXPORT void zip_stream_close(struct zip_t *zip);
513
514/**
515 * Opens zip archive from existing FILE stream with compression level using the
516 * given mode. The stream will not be closed when calling zip_close.
517 *
518 * @param stream C FILE stream.
519 * @param level compression level (0-9 are the standard zlib-style levels).
520 * @param mode file access mode. This mode should be equivalent to the mode
521 * provided when opening the file.
522 * - 'r': opens a file for reading/extracting (the file must exists).
523 * - 'w': creates an empty file for writing.
524 * - 'a': appends to an existing archive.
525 *
526 * @return the zip archive handler or NULL on error
527 */
528extern ZIP_EXPORT struct zip_t *zip_cstream_open(FILE *stream, int level,
529 char mode);
530
531/**
532 * Opens zip archive from existing FILE stream with compression level using the
533 * given mode. The function additionally returns @param errnum - The stream will
534 * not be closed when calling zip_close.
535 *
536 * @param stream C FILE stream.
537 * @param level compression level (0-9 are the standard zlib-style levels).
538 * @param mode file access mode.
539 * - 'r': opens a file for reading/extracting (the file must exists).
540 * - 'w': creates an empty file for writing.
541 * - 'a': appends to an existing archive.
542 * @param errnum 0 on success, negative number (< 0) on error.
543 *
544 * @return the zip archive handler or NULL on error
545 */
546extern ZIP_EXPORT struct zip_t *
547zip_cstream_openwitherror(FILE *stream, int level, char mode, int *errnum);
548
549/**
550 * Closes the zip archive, releases resources - always finalize.
551 * This function is an alias for zip_close function.
552 *
553 * @param zip zip archive handler.
554 */
555extern ZIP_EXPORT void zip_cstream_close(struct zip_t *zip);
556
557/**
558 * Creates a new archive and puts files into a single zip archive.
559 *
560 * @param zipname zip archive file.
561 * @param filenames input files.
562 * @param len: number of input files.
563 *
564 * @return the return code - 0 on success, negative number (< 0) on error.
565 */
566extern ZIP_EXPORT int zip_create(const char *zipname, const char *filenames[],
567 size_t len);
568
569/**
570 * Extracts a zip archive file into directory.
571 *
572 * If on_extract_entry is not NULL, the callback will be called after
573 * successfully extracted each zip entry.
574 * Returning a negative value from the callback will cause abort and return an
575 * error. The last argument (void *arg) is optional, which you can use to pass
576 * data to the on_extract_entry callback.
577 *
578 * @param zipname zip archive file.
579 * @param dir output directory.
580 * @param on_extract_entry on extract callback.
581 * @param arg opaque pointer.
582 *
583 * @return the return code - 0 on success, negative number (< 0) on error.
584 */
585extern ZIP_EXPORT int zip_extract(const char *zipname, const char *dir,
586 int (*on_extract_entry)(const char *filename,
587 void *arg),
588 void *arg);
589/** @} */
590#ifdef __cplusplus
591}
592#endif
593
594#endif
595