1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 
5 This file is part of Quake III Arena source code.
6 
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11 
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22 
23 #if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
24 /* like the STRICT of WIN32, we define a pointer that cannot be converted
25     from (void*) without cast */
26 typedef struct TagunzFile__ { int unused; } unzFile__;
27 typedef unzFile__ *unzFile;
28 #else
29 typedef void* unzFile;
30 #endif
31 
32 /* tm_unz contain date/time info */
33 typedef struct tm_unz_s
34 {
35 	unsigned int tm_sec;            /* seconds after the minute - [0,59] */
36 	unsigned int tm_min;            /* minutes after the hour - [0,59] */
37 	unsigned int tm_hour;           /* hours since midnight - [0,23] */
38 	unsigned int tm_mday;           /* day of the month - [1,31] */
39 	unsigned int tm_mon;            /* months since January - [0,11] */
40 	unsigned int tm_year;           /* years - [1980..2044] */
41 } tm_unz;
42 
43 /* unz_global_info structure contain global data about the ZIPfile
44    These data comes from the end of central dir */
45 typedef struct unz_global_info_s
46 {
47 	unsigned long number_entry;         /* total number of entries in the central dir on this disk */
48 	unsigned long size_comment;         /* size of the global comment of the zipfile */
49 } unz_global_info;
50 
51 
52 /* unz_file_info contain information about a file in the zipfile */
53 typedef struct unz_file_info_s
54 {
55     unsigned long version;              /* version made by                 2 unsigned chars */
56     unsigned long version_needed;       /* version needed to extract       2 unsigned chars */
57     unsigned long flag;                 /* general purpose bit flag        2 unsigned chars */
58     unsigned long compression_method;   /* compression method              2 unsigned chars */
59     unsigned long dosDate;              /* last mod file date in Dos fmt   4 unsigned chars */
60     unsigned long crc;                  /* crc-32                          4 unsigned chars */
61     unsigned long compressed_size;      /* compressed size                 4 unsigned chars */
62     unsigned long uncompressed_size;    /* uncompressed size               4 unsigned chars */
63     unsigned long size_filename;        /* filename length                 2 unsigned chars */
64     unsigned long size_file_extra;      /* extra field length              2 unsigned chars */
65     unsigned long size_file_comment;    /* file comment length             2 unsigned chars */
66 
67     unsigned long disk_num_start;       /* disk number start               2 unsigned chars */
68     unsigned long internal_fa;          /* internal file attributes        2 unsigned chars */
69     unsigned long external_fa;          /* external file attributes        4 unsigned chars */
70 
71     tm_unz tmu_date;
72 } unz_file_info;
73 
74 /* unz_file_info_interntal contain internal info about a file in zipfile*/
75 typedef struct unz_file_info_internal_s
76 {
77     unsigned long offset_curfile;/* relative offset of static header 4 unsigned chars */
78 } unz_file_info_internal;
79 
80 typedef void* (*alloc_func) (void* opaque, unsigned int items, unsigned int size);
81 typedef void   (*free_func) (void* opaque, void* address);
82 
83 struct internal_state;
84 
85 typedef struct z_stream_s {
86     unsigned char    *next_in;  /* next input unsigned char */
87     unsigned int     avail_in;  /* number of unsigned chars available at next_in */
88     unsigned long    total_in;  /* total nb of input unsigned chars read so */
89 
90     unsigned char    *next_out; /* next output unsigned char should be put there */
91     unsigned int     avail_out; /* remaining free space at next_out */
92     unsigned long    total_out; /* total nb of unsigned chars output so */
93 
94     char     *msg;      /* last error message, NULL if no error */
95     struct internal_state *state; /* not visible by applications */
96 
97     alloc_func zalloc;  /* used to allocate the internal state */
98     free_func  zfree;   /* used to free the internal state */
99     unsigned char*     opaque;  /* private data object passed to zalloc and zfree */
100 
101     int     data_type;  /* best guess about the data type: ascii or binary */
102     unsigned long   adler;      /* adler32 value of the uncompressed data */
103     unsigned long   reserved;   /* reserved for future use */
104 } z_stream;
105 
106 typedef z_stream *z_streamp;
107 
108 
109 /* file_in_zip_read_info_s contain internal information about a file in zipfile,
110     when reading and decompress it */
111 typedef struct
112 {
113 	char  *read_buffer;         /* internal buffer for compressed data */
114 	z_stream stream;            /* zLib stream structure for inflate */
115 
116 	unsigned long pos_in_zipfile;       /* position in unsigned char on the zipfile, for fseek*/
117 	unsigned long stream_initialised;   /* flag set if stream structure is initialised*/
118 
119 	unsigned long offset_local_extrafield;/* offset of the static extra field */
120 	unsigned int  size_local_extrafield;/* size of the static extra field */
121 	unsigned long pos_local_extrafield;   /* position in the static extra field in read*/
122 
123 	unsigned long crc32;                /* crc32 of all data uncompressed */
124 	unsigned long crc32_wait;           /* crc32 we must obtain after decompress all */
125 	unsigned long rest_read_compressed; /* number of unsigned char to be decompressed */
126 	unsigned long rest_read_uncompressed;/*number of unsigned char to be obtained after decomp*/
127 	FILE* file;                 /* io structore of the zipfile */
128 	unsigned long compression_method;   /* compression method (0==store) */
129 	unsigned long byte_before_the_zipfile;/* unsigned char before the zipfile, (>0 for sfx)*/
130 } file_in_zip_read_info_s;
131 
132 
133 /* unz_s contain internal information about the zipfile
134 */
135 typedef struct
136 {
137 	FILE* file;                 /* io structore of the zipfile */
138 	unz_global_info gi;       /* public global information */
139 	unsigned long byte_before_the_zipfile;/* unsigned char before the zipfile, (>0 for sfx)*/
140 	unsigned long num_file;             /* number of the current file in the zipfile*/
141 	unsigned long pos_in_central_dir;   /* pos of the current file in the central dir*/
142 	unsigned long current_file_ok;      /* flag about the usability of the current file*/
143 	unsigned long central_pos;          /* position of the beginning of the central dir*/
144 
145 	unsigned long size_central_dir;     /* size of the central directory  */
146 	unsigned long offset_central_dir;   /* offset of start of central directory with
147 								   respect to the starting disk number */
148 
149 	unz_file_info cur_file_info; /* public info about the current file in zip*/
150 	unz_file_info_internal cur_file_info_internal; /* private info about it*/
151     file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current
152 	                                    file if we are decompressing it */
153 	unsigned char*	tmpFile;
154 	int	tmpPos,tmpSize;
155 } unz_s;
156 
157 #define UNZ_OK                                  (0)
158 #define UNZ_END_OF_LIST_OF_FILE (-100)
159 #define UNZ_ERRNO               (Z_ERRNO)
160 #define UNZ_EOF                 (0)
161 #define UNZ_PARAMERROR                  (-102)
162 #define UNZ_BADZIPFILE                  (-103)
163 #define UNZ_INTERNALERROR               (-104)
164 #define UNZ_CRCERROR                    (-105)
165 
166 #define UNZ_CASESENSITIVE		1
167 #define UNZ_NOTCASESENSITIVE	2
168 #define UNZ_OSDEFAULTCASE		0
169 
170 extern int unzStringFileNameCompare (const char* fileName1, const char* fileName2, int iCaseSensitivity);
171 
172 /*
173    Compare two filename (fileName1,fileName2).
174    If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
175    If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
176 								or strcasecmp)
177    If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
178 	(like 1 on Unix, 2 on Windows)
179 */
180 
181 extern unzFile unzOpen (const char *path);
182 extern unzFile unzReOpen (const char* path, unzFile file);
183 
184 /*
185   Open a Zip file. path contain the full pathname (by example,
186      on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer
187 	 "zlib/zlib111.zip".
188 	 If the zipfile cannot be opened (file don't exist or in not valid), the
189 	   return value is NULL.
190      Else, the return value is a unzFile Handle, usable with other function
191 	   of this unzip package.
192 */
193 
194 extern int unzClose (unzFile file);
195 
196 /*
197   Close a ZipFile opened with unzipOpen.
198   If there is files inside the .Zip opened with unzOpenCurrentFile (see later),
199     these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
200   return UNZ_OK if there is no problem. */
201 
202 extern int unzGetGlobalInfo (unzFile file, unz_global_info *pglobal_info);
203 
204 /*
205   Write info about the ZipFile in the *pglobal_info structure.
206   No preparation of the structure is needed
207   return UNZ_OK if there is no problem. */
208 
209 
210 extern int unzGetGlobalComment (unzFile file, char *szComment, unsigned long uSizeBuf);
211 
212 /*
213   Get the global comment string of the ZipFile, in the szComment buffer.
214   uSizeBuf is the size of the szComment buffer.
215   return the number of unsigned char copied or an error code <0
216 */
217 
218 
219 /***************************************************************************/
220 /* Unzip package allow you browse the directory of the zipfile */
221 
222 extern int unzGoToFirstFile (unzFile file);
223 
224 /*
225   Set the current file of the zipfile to the first file.
226   return UNZ_OK if there is no problem
227 */
228 
229 extern int unzGoToNextFile (unzFile file);
230 
231 /*
232   Set the current file of the zipfile to the next file.
233   return UNZ_OK if there is no problem
234   return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
235 */
236 
237 extern int unzGetCurrentFileInfoPosition (unzFile file, unsigned long *pos );
238 
239 /*
240   Get the position of the info of the current file in the zip.
241   return UNZ_OK if there is no problem
242 */
243 
244 extern int unzSetCurrentFileInfoPosition (unzFile file, unsigned long pos );
245 
246 /*
247   Set the position of the info of the current file in the zip.
248   return UNZ_OK if there is no problem
249 */
250 
251 extern int unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity);
252 
253 /*
254   Try locate the file szFileName in the zipfile.
255   For the iCaseSensitivity signification, see unzStringFileNameCompare
256 
257   return value :
258   UNZ_OK if the file is found. It becomes the current file.
259   UNZ_END_OF_LIST_OF_FILE if the file is not found
260 */
261 
262 
263 extern int unzGetCurrentFileInfo (unzFile file, unz_file_info *pfile_info, char *szFileName, unsigned long fileNameBufferSize, void *extraField, unsigned long extraFieldBufferSize, char *szComment, unsigned long commentBufferSize);
264 
265 /*
266   Get Info about the current file
267   if pfile_info!=NULL, the *pfile_info structure will contain somes info about
268 	    the current file
269   if szFileName!=NULL, the filemane string will be copied in szFileName
270 			(fileNameBufferSize is the size of the buffer)
271   if extraField!=NULL, the extra field information will be copied in extraField
272 			(extraFieldBufferSize is the size of the buffer).
273 			This is the Central-header version of the extra field
274   if szComment!=NULL, the comment string of the file will be copied in szComment
275 			(commentBufferSize is the size of the buffer)
276 */
277 
278 /***************************************************************************/
279 /* for reading the content of the current zipfile, you can open it, read data
280    from it, and close it (you can close it before reading all the file)
281    */
282 
283 extern int unzOpenCurrentFile (unzFile file);
284 
285 /*
286   Open for reading data the current file in the zipfile.
287   If there is no error, the return value is UNZ_OK.
288 */
289 
290 extern int unzCloseCurrentFile (unzFile file);
291 
292 /*
293   Close the file in zip opened with unzOpenCurrentFile
294   Return UNZ_CRCERROR if all the file was read but the CRC is not good
295 */
296 
297 
298 extern int unzReadCurrentFile (unzFile file, void* buf, unsigned len);
299 
300 /*
301   Read unsigned chars from the current file (opened by unzOpenCurrentFile)
302   buf contain buffer where data must be copied
303   len the size of buf.
304 
305   return the number of unsigned char copied if somes unsigned chars are copied
306   return 0 if the end of file was reached
307   return <0 with error code if there is an error
308     (UNZ_ERRNO for IO error, or zLib error for uncompress error)
309 */
310 
311 extern long unztell(unzFile file);
312 
313 /*
314   Give the current position in uncompressed data
315 */
316 
317 extern int unzeof (unzFile file);
318 
319 /*
320   return 1 if the end of file was reached, 0 elsewhere
321 */
322 
323 extern int unzGetLocalExtrafield (unzFile file, void* buf, unsigned len);
324 
325 /*
326   Read extra field from the current file (opened by unzOpenCurrentFile)
327   This is the local-header version of the extra field (sometimes, there is
328     more info in the local-header version than in the central-header)
329 
330   if buf==NULL, it return the size of the local extra field
331 
332   if buf!=NULL, len is the size of the buffer, the extra header is copied in
333 	buf.
334   the return value is the number of unsigned chars copied in buf, or (if <0)
335 	the error code
336 */
337