Debian Codegraph @

src/cdogs/cwolfmap/zip/zip.h

raw symbols SCIP
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 <string.h>
17 #include <sys/types.h>
18
19 #ifndef ZIP_SHARED
20 #define ZIP_EXPORT
21 #else
22 #ifdef _WIN32
23 #ifdef ZIP_BUILD_SHARED
24 #define ZIP_EXPORT __declspec(dllexport)
25 #else
26 #define ZIP_EXPORT __declspec(dllimport)
27 #endif
28 #else
29 #define ZIP_EXPORT __attribute__((visibility("default")))
30 #endif
31 #endif
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #if !defined(_POSIX_C_SOURCE) && defined(_MSC_VER)
38 // 64-bit Windows is the only mainstream platform
39 // where sizeof(long) != sizeof(void*)
40 #ifdef _WIN64
41 typedef long long ssize_t; /* byte count or error */
42 #else
43 typedef long ssize_t; /* byte count or error */
44 #endif
45 #endif
46
47 /**
48 * @mainpage
49 *
50 * Documenation for @ref zip.
51 */
52
53 /**
54 * @addtogroup zip
55 * @{
56 */
57
58 /**
59 * Default zip compression level.
60 */
61 #define ZIP_DEFAULT_COMPRESSION_LEVEL 6
62
63 /**
64 * Error codes
65 */
66 #define ZIP_ENOINIT -1 // not initialized
67 #define ZIP_EINVENTNAME -2 // invalid entry name
68 #define ZIP_ENOENT -3 // entry not found
69 #define ZIP_EINVMODE -4 // invalid zip mode
70 #define ZIP_EINVLVL -5 // invalid compression level
71 #define ZIP_ENOSUP64 -6 // no zip 64 support
72 #define ZIP_EMEMSET -7 // memset error
73 #define ZIP_EWRTENT -8 // cannot write data to entry
74 #define ZIP_ETDEFLINIT -9 // cannot initialize tdefl compressor
75 #define ZIP_EINVIDX -10 // invalid index
76 #define ZIP_ENOHDR -11 // header not found
77 #define ZIP_ETDEFLBUF -12 // cannot flush tdefl buffer
78 #define ZIP_ECRTHDR -13 // cannot create entry header
79 #define ZIP_EWRTHDR -14 // cannot write entry header
80 #define ZIP_EWRTDIR -15 // cannot write to central dir
81 #define ZIP_EOPNFILE -16 // cannot open file
82 #define ZIP_EINVENTTYPE -17 // invalid entry type
83 #define ZIP_EMEMNOALLOC -18 // extracting data using no memory allocation
84 #define ZIP_ENOFILE -19 // file not found
85 #define ZIP_ENOPERM -20 // no permission
86 #define ZIP_EOOMEM -21 // out of memory
87 #define ZIP_EINVZIPNAME -22 // invalid zip archive name
88 #define ZIP_EMKDIR -23 // make dir error
89 #define ZIP_ESYMLINK -24 // symlink error
90 #define ZIP_ECLSZIP -25 // close archive error
91 #define ZIP_ECAPSIZE -26 // capacity size too small
92 #define ZIP_EFSEEK -27 // fseek error
93 #define ZIP_EFREAD -28 // fread error
94 #define ZIP_EFWRITE -29 // fwrite error
95
96 /**
97 * Looks up the error message string coresponding to an error number.
98 * @param errnum error number
99 * @return error message string coresponding to errnum or NULL if error is not
100 * found.
101 */
102 extern ZIP_EXPORT const char *zip_strerror(int errnum);
103
104 /**
105 * @struct zip_t
106 *
107 * This data structure is used throughout the library to represent zip archive -
108 * forward declaration.
109 */
110 struct zip_t;
111
112 /**
113 * Opens zip archive with compression level using the given mode.
114 *
115 * @param zipname zip archive file name.
116 * @param level compression level (0-9 are the standard zlib-style levels).
117 * @param mode file access mode.
118 * - 'r': opens a file for reading/extracting (the file must exists).
119 * - 'w': creates an empty file for writing.
120 * - 'a': appends to an existing archive.
121 *
122 * @return the zip archive handler or NULL on error
123 */
124 extern ZIP_EXPORT struct zip_t *zip_open(const char *zipname, int level,
125 char mode);
126
127 /**
128 * Closes the zip archive, releases resources - always finalize.
129 *
130 * @param zip zip archive handler.
131 */
132 extern ZIP_EXPORT void zip_close(struct zip_t *zip);
133
134 /**
135 * Determines if the archive has a zip64 end of central directory headers.
136 *
137 * @param zip zip archive handler.
138 *
139 * @return the return code - 1 (true), 0 (false), negative number (< 0) on
140 * error.
141 */
142 extern ZIP_EXPORT int zip_is64(struct zip_t *zip);
143
144 /**
145 * Opens an entry by name in the zip archive.
146 *
147 * For zip archive opened in 'w' or 'a' mode the function will append
148 * a new entry. In readonly mode the function tries to locate the entry
149 * in global dictionary.
150 *
151 * @param zip zip archive handler.
152 * @param entryname an entry name in local dictionary.
153 *
154 * @return the return code - 0 on success, negative number (< 0) on error.
155 */
156 extern ZIP_EXPORT int zip_entry_open(struct zip_t *zip, const char *entryname);
157
158 /**
159 * Opens an entry by name in the zip archive.
160 *
161 * For zip archive opened in 'w' or 'a' mode the function will append
162 * a new entry. In readonly mode the function tries to locate the entry
163 * in global dictionary (case sensitive).
164 *
165 * @param zip zip archive handler.
166 * @param entryname an entry name in local dictionary (case sensitive).
167 *
168 * @return the return code - 0 on success, negative number (< 0) on error.
169 */
170 extern ZIP_EXPORT int zip_entry_opencasesensitive(struct zip_t *zip,
171 const char *entryname);
172
173 /**
174 * Opens a new entry by index in the zip archive.
175 *
176 * This function is only valid if zip archive was opened in 'r' (readonly) mode.
177 *
178 * @param zip zip archive handler.
179 * @param index index in local dictionary.
180 *
181 * @return the return code - 0 on success, negative number (< 0) on error.
182 */
183 extern ZIP_EXPORT int zip_entry_openbyindex(struct zip_t *zip, size_t index);
184
185 /**
186 * Closes a zip entry, flushes buffer and releases resources.
187 *
188 * @param zip zip archive handler.
189 *
190 * @return the return code - 0 on success, negative number (< 0) on error.
191 */
192 extern ZIP_EXPORT int zip_entry_close(struct zip_t *zip);
193
194 /**
195 * Returns a local name of the current zip entry.
196 *
197 * The main difference between user's entry name and local entry name
198 * is optional relative path.
199 * Following .ZIP File Format Specification - the path stored MUST not contain
200 * a drive or device letter, or a leading slash.
201 * All slashes MUST be forward slashes '/' as opposed to backwards slashes '\'
202 * for compatibility with Amiga and UNIX file systems etc.
203 *
204 * @param zip: zip archive handler.
205 *
206 * @return the pointer to the current zip entry name, or NULL on error.
207 */
208 extern ZIP_EXPORT const char *zip_entry_name(struct zip_t *zip);
209
210 /**
211 * Returns an index of the current zip entry.
212 *
213 * @param zip zip archive handler.
214 *
215 * @return the index on success, negative number (< 0) on error.
216 */
217 extern ZIP_EXPORT ssize_t zip_entry_index(struct zip_t *zip);
218
219 /**
220 * Determines if the current zip entry is a directory entry.
221 *
222 * @param zip zip archive handler.
223 *
224 * @return the return code - 1 (true), 0 (false), negative number (< 0) on
225 * error.
226 */
227 extern ZIP_EXPORT int zip_entry_isdir(struct zip_t *zip);
228
229 /**
230 * Returns the uncompressed size of the current zip entry.
231 * Alias for zip_entry_uncomp_size (for backward compatibility).
232 *
233 * @param zip zip archive handler.
234 *
235 * @return the uncompressed size in bytes.
236 */
237 extern ZIP_EXPORT unsigned long long zip_entry_size(struct zip_t *zip);
238
239 /**
240 * Returns the uncompressed size of the current zip entry.
241 *
242 * @param zip zip archive handler.
243 *
244 * @return the uncompressed size in bytes.
245 */
246 extern ZIP_EXPORT unsigned long long zip_entry_uncomp_size(struct zip_t *zip);
247
248 /**
249 * Returns the compressed size of the current zip entry.
250 *
251 * @param zip zip archive handler.
252 *
253 * @return the compressed size in bytes.
254 */
255 extern ZIP_EXPORT unsigned long long zip_entry_comp_size(struct zip_t *zip);
256
257 /**
258 * Returns CRC-32 checksum of the current zip entry.
259 *
260 * @param zip zip archive handler.
261 *
262 * @return the CRC-32 checksum.
263 */
264 extern ZIP_EXPORT unsigned int zip_entry_crc32(struct zip_t *zip);
265
266 /**
267 * Compresses an input buffer for the current zip entry.
268 *
269 * @param zip zip archive handler.
270 * @param buf input buffer.
271 * @param bufsize input buffer size (in bytes).
272 *
273 * @return the return code - 0 on success, negative number (< 0) on error.
274 */
275 extern ZIP_EXPORT int zip_entry_write(struct zip_t *zip, const void *buf,
276 size_t bufsize);
277
278 /**
279 * Compresses a file for the current zip entry.
280 *
281 * @param zip zip archive handler.
282 * @param filename input file.
283 *
284 * @return the return code - 0 on success, negative number (< 0) on error.
285 */
286 extern ZIP_EXPORT int zip_entry_fwrite(struct zip_t *zip, const char *filename);
287
288 /**
289 * Extracts the current zip entry into output buffer.
290 *
291 * The function allocates sufficient memory for a output buffer.
292 *
293 * @param zip zip archive handler.
294 * @param buf output buffer.
295 * @param bufsize output buffer size (in bytes).
296 *
297 * @note remember to release memory allocated for a output buffer.
298 * for large entries, please take a look at zip_entry_extract function.
299 *
300 * @return the return code - the number of bytes actually read on success.
301 * Otherwise a negative number (< 0) on error.
302 */
303 extern ZIP_EXPORT ssize_t zip_entry_read(struct zip_t *zip, void **buf,
304 size_t *bufsize);
305
306 /**
307 * Extracts the current zip entry into a memory buffer using no memory
308 * allocation.
309 *
310 * @param zip zip archive handler.
311 * @param buf preallocated output buffer.
312 * @param bufsize output buffer size (in bytes).
313 *
314 * @note ensure supplied output buffer is large enough.
315 * zip_entry_size function (returns uncompressed size for the current
316 * entry) can be handy to estimate how big buffer is needed.
317 * For large entries, please take a look at zip_entry_extract function.
318 *
319 * @return the return code - the number of bytes actually read on success.
320 * Otherwise a negative number (< 0) on error (e.g. bufsize is not large
321 * enough).
322 */
323 extern ZIP_EXPORT ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf,
324 size_t bufsize);
325
326 /**
327 * Extracts the current zip entry into output file.
328 *
329 * @param zip zip archive handler.
330 * @param filename output file.
331 *
332 * @return the return code - 0 on success, negative number (< 0) on error.
333 */
334 extern ZIP_EXPORT int zip_entry_fread(struct zip_t *zip, const char *filename);
335
336 /**
337 * Extracts the current zip entry using a callback function (on_extract).
338 *
339 * @param zip zip archive handler.
340 * @param on_extract callback function.
341 * @param arg opaque pointer (optional argument, which you can pass to the
342 * on_extract callback)
343 *
344 * @return the return code - 0 on success, negative number (< 0) on error.
345 */
346 extern ZIP_EXPORT int
347 zip_entry_extract(struct zip_t *zip,
348 size_t (*on_extract)(void *arg, uint64_t offset,
349 const void *data, size_t size),
350 void *arg);
351
352 /**
353 * Returns the number of all entries (files and directories) in the zip archive.
354 *
355 * @param zip zip archive handler.
356 *
357 * @return the return code - the number of entries on success, negative number
358 * (< 0) on error.
359 */
360 extern ZIP_EXPORT ssize_t zip_entries_total(struct zip_t *zip);
361
362 /**
363 * Deletes zip archive entries.
364 *
365 * @param zip zip archive handler.
366 * @param entries array of zip archive entries to be deleted.
367 * @param len the number of entries to be deleted.
368 * @return the number of deleted entries, or negative number (< 0) on error.
369 */
370 extern ZIP_EXPORT ssize_t zip_entries_delete(struct zip_t *zip,
371 char *const entries[], size_t len);
372
373 /**
374 * Extracts a zip archive stream into directory.
375 *
376 * If on_extract is not NULL, the callback will be called after
377 * successfully extracted each zip entry.
378 * Returning a negative value from the callback will cause abort and return an
379 * error. The last argument (void *arg) is optional, which you can use to pass
380 * data to the on_extract callback.
381 *
382 * @param stream zip archive stream.
383 * @param size stream size.
384 * @param dir output directory.
385 * @param on_extract on extract callback.
386 * @param arg opaque pointer.
387 *
388 * @return the return code - 0 on success, negative number (< 0) on error.
389 */
390 extern ZIP_EXPORT int
391 zip_stream_extract(const char *stream, size_t size, const char *dir,
392 int (*on_extract)(const char *filename, void *arg),
393 void *arg);
394
395 /**
396 * Opens zip archive stream into memory.
397 *
398 * @param stream zip archive stream.
399 * @param size stream size.
400 *
401 * @return the zip archive handler or NULL on error
402 */
403 extern ZIP_EXPORT struct zip_t *zip_stream_open(const char *stream, size_t size,
404 int level, char mode);
405
406 /**
407 * Copy zip archive stream output buffer.
408 *
409 * @param zip zip archive handler.
410 * @param buf output buffer. User should free buf.
411 * @param bufsize output buffer size (in bytes).
412 *
413 * @return copy size
414 */
415 extern ZIP_EXPORT ssize_t zip_stream_copy(struct zip_t *zip, void **buf,
416 size_t *bufsize);
417
418 /**
419 * Close zip archive releases resources.
420 *
421 * @param zip zip archive handler.
422 *
423 * @return
424 */
425 extern ZIP_EXPORT void zip_stream_close(struct zip_t *zip);
426
427 /**
428 * Creates a new archive and puts files into a single zip archive.
429 *
430 * @param zipname zip archive file.
431 * @param filenames input files.
432 * @param len: number of input files.
433 *
434 * @return the return code - 0 on success, negative number (< 0) on error.
435 */
436 extern ZIP_EXPORT int zip_create(const char *zipname, const char *filenames[],
437 size_t len);
438
439 /**
440 * Extracts a zip archive file into directory.
441 *
442 * If on_extract_entry is not NULL, the callback will be called after
443 * successfully extracted each zip entry.
444 * Returning a negative value from the callback will cause abort and return an
445 * error. The last argument (void *arg) is optional, which you can use to pass
446 * data to the on_extract_entry callback.
447 *
448 * @param zipname zip archive file.
449 * @param dir output directory.
450 * @param on_extract_entry on extract callback.
451 * @param arg opaque pointer.
452 *
453 * @return the return code - 0 on success, negative number (< 0) on error.
454 */
455 extern ZIP_EXPORT int zip_extract(const char *zipname, const char *dir,
456 int (*on_extract_entry)(const char *filename,
457 void *arg),
458 void *arg);
459 /** @} */
460 #ifdef __cplusplus
461 }
462 #endif
463
464 #endif