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