added generic support for listing and extracting zip files (using minizip)
[rocksndiamonds.git] / src / libgame / zip / unzip.h
1 /* unzip.h -- IO for uncompress .zip files using zlib
2    Version 1.2.0, September 16th, 2017
3    part of the MiniZip project
4
5    Copyright (C) 2012-2017 Nathan Moinvaziri
6      https://github.com/nmoinvaz/minizip
7    Copyright (C) 2009-2010 Mathias Svensson
8      Modifications for Zip64 support on both zip and unzip
9      http://result42.com
10    Copyright (C) 2007-2008 Even Rouault
11      Modifications of Unzip for Zip64
12    Copyright (C) 1998-2010 Gilles Vollant
13      http://www.winimage.com/zLibDll/minizip.html
14
15    This program is distributed under the terms of the same license as zlib.
16    See the accompanying LICENSE file for the full text of the license.
17 */
18
19 #ifndef _UNZ_H
20 #define _UNZ_H
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 #ifndef _ZLIB_H
27 #include "zlib.h"
28 #endif
29
30 #ifndef _ZLIBIOAPI_H
31 #include "ioapi.h"
32 #endif
33
34 #ifdef HAVE_BZIP2
35 #include "bzlib.h"
36 #endif
37
38 #define Z_BZIP2ED 12
39
40 #if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
41 /* like the STRICT of WIN32, we define a pointer that cannot be converted
42     from (void*) without cast */
43 typedef struct TagunzFile__ { int unused; } unz_file__;
44 typedef unz_file__ *unzFile;
45 #else
46 typedef voidp unzFile;
47 #endif
48
49 #define UNZ_OK                          (0)
50 #define UNZ_END_OF_LIST_OF_FILE         (-100)
51 #define UNZ_ERRNO                       (Z_ERRNO)
52 #define UNZ_EOF                         (0)
53 #define UNZ_PARAMERROR                  (-102)
54 #define UNZ_BADZIPFILE                  (-103)
55 #define UNZ_INTERNALERROR               (-104)
56 #define UNZ_CRCERROR                    (-105)
57 #define UNZ_BADPASSWORD                 (-106)
58
59 /* unz_global_info structure contain global data about the ZIPfile
60    These data comes from the end of central dir */
61 typedef struct unz_global_info64_s
62 {
63     uint64_t number_entry;          /* total number of entries in the central dir on this disk */
64     uint32_t number_disk_with_CD;   /* number the the disk with central dir, used for spanning ZIP*/
65     uint16_t size_comment;          /* size of the global comment of the zipfile */
66 } unz_global_info64;
67
68 typedef struct unz_global_info_s
69 {
70     uint32_t number_entry;          /* total number of entries in the central dir on this disk */
71     uint32_t number_disk_with_CD;   /* number the the disk with central dir, used for spanning ZIP*/
72     uint16_t size_comment;          /* size of the global comment of the zipfile */
73 } unz_global_info;
74
75 /* unz_file_info contain information about a file in the zipfile */
76 /* https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT */
77 typedef struct unz_file_info64_s
78 {
79     uint16_t version;               /* version made by                 2 bytes */
80     uint16_t version_needed;        /* version needed to extract       2 bytes */
81     uint16_t flag;                  /* general purpose bit flag        2 bytes */
82     uint16_t compression_method;    /* compression method              2 bytes */
83     uint32_t dos_date;              /* last mod file date in Dos fmt   4 bytes */
84     uint32_t crc;                   /* crc-32                          4 bytes */
85     uint64_t compressed_size;       /* compressed size                 8 bytes */
86     uint64_t uncompressed_size;     /* uncompressed size               8 bytes */
87     uint16_t size_filename;         /* filename length                 2 bytes */
88     uint16_t size_file_extra;       /* extra field length              2 bytes */
89     uint16_t size_file_comment;     /* file comment length             2 bytes */
90
91     uint32_t disk_num_start;        /* disk number start               4 bytes */
92     uint16_t internal_fa;           /* internal file attributes        2 bytes */
93     uint32_t external_fa;           /* external file attributes        4 bytes */
94
95     uint64_t disk_offset;
96
97     uint16_t size_file_extra_internal;
98 } unz_file_info64;
99
100 typedef struct unz_file_info_s
101 {
102     uint16_t version;               /* version made by                 2 bytes */
103     uint16_t version_needed;        /* version needed to extract       2 bytes */
104     uint16_t flag;                  /* general purpose bit flag        2 bytes */
105     uint16_t compression_method;    /* compression method              2 bytes */
106     uint32_t dos_date;              /* last mod file date in Dos fmt   4 bytes */
107     uint32_t crc;                   /* crc-32                          4 bytes */
108     uint32_t compressed_size;       /* compressed size                 4 bytes */
109     uint32_t uncompressed_size;     /* uncompressed size               4 bytes */
110     uint16_t size_filename;         /* filename length                 2 bytes */
111     uint16_t size_file_extra;       /* extra field length              2 bytes */
112     uint16_t size_file_comment;     /* file comment length             2 bytes */
113
114     uint16_t disk_num_start;        /* disk number start               2 bytes */
115     uint16_t internal_fa;           /* internal file attributes        2 bytes */
116     uint32_t external_fa;           /* external file attributes        4 bytes */
117
118     uint64_t disk_offset;
119 } unz_file_info;
120
121 /***************************************************************************/
122 /* Opening and close a zip file */
123
124 extern unzFile ZEXPORT unzOpen(const char *path);
125 extern unzFile ZEXPORT unzOpen64(const void *path);
126 /* Open a Zip file.
127
128    path should contain the full path (by example, on a Windows XP computer
129       "c:\\zlib\\zlib113.zip" or on an Unix computer "zlib/zlib113.zip".
130    return NULL if zipfile cannot be opened or doesn't exist
131    return unzFile handle if no error
132
133    NOTE: The "64" function take a const void *pointer, because  the path is just the value passed to the
134    open64_file_func callback. Under Windows, if UNICODE is defined, using fill_fopen64_filefunc, the path
135    is a pointer to a wide unicode string  (LPCTSTR is LPCWSTR), so const char *does not describe the reality */
136
137 extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def);
138 /* Open a Zip file, like unzOpen, but provide a set of file low level API for read/write operations */
139 extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def);
140 /* Open a Zip file, like unz64Open, but provide a set of file low level API for read/write 64-bit operations */
141
142 extern int ZEXPORT unzClose(unzFile file);
143 /* Close a ZipFile opened with unzOpen. If there is files inside the .Zip opened with unzOpenCurrentFile,
144    these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
145
146    return UNZ_OK if there is no error */
147
148 extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info);
149 extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
150 /* Write info about the ZipFile in the *pglobal_info structure.
151
152    return UNZ_OK if no error */
153
154 extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
155 /* Get the global comment string of the ZipFile, in the comment buffer.
156
157    uSizeBuf is the size of the szComment buffer.
158    return the number of byte copied or an error code <0 */
159
160 extern uint64_t ZEXPORT unzCountEntries(const unzFile file);
161
162 /***************************************************************************/
163 /* Reading the content of the current zipfile, you can open it, read data from it, and close it
164    (you can close it before reading all the file) */
165
166 extern int ZEXPORT unzOpenCurrentFile(unzFile file);
167 /* Open for reading data the current file in the zipfile.
168
169    return UNZ_OK if no error */
170
171 extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
172 /* Open for reading data the current file in the zipfile.
173    password is a crypting password
174
175    return UNZ_OK if no error */
176
177 extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
178 /* Same as unzOpenCurrentFile, but open for read raw the file (not uncompress)
179    if raw==1 *method will receive method of compression, *level will receive level of compression
180
181    NOTE: you can set level parameter as NULL (if you did not want known level,
182          but you CANNOT set method parameter as NULL */
183
184 extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
185 /* Same as unzOpenCurrentFile, but takes extra parameter password for encrypted files */
186
187 extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len);
188 /* Read bytes from the current file (opened by unzOpenCurrentFile)
189    buf contain buffer where data must be copied
190    len the size of buf.
191
192    return the number of byte copied if somes bytes are copied
193    return 0 if the end of file was reached
194    return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
195
196 extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
197     uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
198 extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename,
199     uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
200 /* Get Info about the current file
201
202    pfile_info if != NULL, the *pfile_info structure will contain somes info about the current file
203    filename if != NULL, the file name string will be copied in filename
204    filename_size is the size of the filename buffer
205    extrafield if != NULL, the extra field information from the central header will be copied in to
206    extrafield_size is the size of the extraField buffer
207    comment if != NULL, the comment string of the file will be copied in to
208    comment_size is the size of the comment buffer */
209
210 extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, uint32_t len);
211 /* Read extra field from the current file (opened by unzOpenCurrentFile)
212    This is the local-header version of the extra field (sometimes, there is
213    more info in the local-header version than in the central-header)
214
215    if buf == NULL, it return the size of the local extra field
216    if buf != NULL, len is the size of the buffer, the extra header is copied in buf.
217
218    return number of bytes copied in buf, or (if <0) the error code */
219
220 extern int ZEXPORT unzCloseCurrentFile(unzFile file);
221 /* Close the file in zip opened with unzOpenCurrentFile
222
223    return UNZ_CRCERROR if all the file was read but the CRC is not good */
224
225 /***************************************************************************/
226 /* Browse the directory of the zipfile */
227
228 typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
229 typedef int (*unzIteratorFunction)(unzFile file);
230 typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
231     uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
232
233 extern int ZEXPORT unzGoToFirstFile(unzFile file);
234 /* Set the current file of the zipfile to the first file.
235
236    return UNZ_OK if no error */
237
238 extern int ZEXPORT unzGoToFirstFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
239     uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
240 /* Set the current file of the zipfile to the first file and retrieves the current info on success.
241    Not as seek intensive as unzGoToFirstFile + unzGetCurrentFileInfo.
242
243    return UNZ_OK if no error */
244
245 extern int ZEXPORT unzGoToNextFile(unzFile file);
246 /* Set the current file of the zipfile to the next file.
247
248    return UNZ_OK if no error
249    return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
250
251 extern int ZEXPORT unzGoToNextFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
252     uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
253 /* Set the current file of the zipfile to the next file and retrieves the current
254    info on success. Does less seeking around than unzGotoNextFile + unzGetCurrentFileInfo.
255
256    return UNZ_OK if no error
257    return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
258
259 extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
260 /* Try locate the file szFileName in the zipfile. For custom filename comparison pass in comparison function.
261
262    return UNZ_OK if the file is found (it becomes the current file)
263    return UNZ_END_OF_LIST_OF_FILE if the file is not found */
264
265 /***************************************************************************/
266 /* Raw access to zip file */
267
268 typedef struct unz_file_pos_s
269 {
270     uint32_t pos_in_zip_directory;  /* offset in zip file directory */
271     uint32_t num_of_file;           /* # of file */
272 } unz_file_pos;
273
274 extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos);
275 extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos);
276
277 typedef struct unz64_file_pos_s
278 {
279     uint64_t pos_in_zip_directory;   /* offset in zip file directory */
280     uint64_t num_of_file;            /* # of file */
281 } unz64_file_pos;
282
283 extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos);
284 extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos);
285
286 extern int32_t ZEXPORT unzGetOffset(unzFile file);
287 extern int64_t ZEXPORT unzGetOffset64(unzFile file);
288 /* Get the current file offset */
289
290 extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos);
291 extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos);
292 /* Set the current file offset */
293
294 extern int32_t ZEXPORT unzTell(unzFile file);
295 extern int64_t ZEXPORT unzTell64(unzFile file);
296 /* return current position in uncompressed data */
297
298 extern int ZEXPORT unzSeek(unzFile file, uint32_t offset, int origin);
299 extern int ZEXPORT unzSeek64(unzFile file, uint64_t offset, int origin);
300 /* Seek within the uncompressed data if compression method is storage */
301
302 extern int ZEXPORT unzEndOfFile(unzFile file);
303 /* return 1 if the end of file was reached, 0 elsewhere */
304
305 /***************************************************************************/
306
307 #ifdef __cplusplus
308 }
309 #endif
310
311 #endif /* _UNZ_H */