summaryrefslogtreecommitdiff
path: root/contrib/minizip/unzip.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/minizip/unzip.c')
-rw-r--r--contrib/minizip/unzip.c3060
1 files changed, 1531 insertions, 1529 deletions
diff --git a/contrib/minizip/unzip.c b/contrib/minizip/unzip.c
index 7244523..07d869e 100644
--- a/contrib/minizip/unzip.c
+++ b/contrib/minizip/unzip.c
@@ -1,1529 +1,1531 @@
1/* unzip.c -- IO for uncompress .zip files using zlib 1/* unzip.c -- IO for uncompress .zip files using zlib
2 Version 0.21 with encryption, March 10th, 2003 2 Version 0.22, May 19th, 2003
3 3
4 Copyright (C) 1998-2003 Gilles Vollant 4 Copyright (C) 1998-2003 Gilles Vollant
5 5
6 Read unzip.h for more info 6 Read unzip.h for more info
7*/ 7*/
8 8
9/* Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of 9/* Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of
10compatibility with older software. The following is from the original crypt.c. Code 10compatibility with older software. The following is from the original crypt.c. Code
11woven in by Terry Thorsen 1/2003. 11woven in by Terry Thorsen 1/2003.
12*/ 12*/
13/* 13/*
14 Copyright (c) 1990-2000 Info-ZIP. All rights reserved. 14 Copyright (c) 1990-2000 Info-ZIP. All rights reserved.
15 15
16 See the accompanying file LICENSE, version 2000-Apr-09 or later 16 See the accompanying file LICENSE, version 2000-Apr-09 or later
17 (the contents of which are also included in zip.h) for terms of use. 17 (the contents of which are also included in zip.h) for terms of use.
18 If, for some reason, all these files are missing, the Info-ZIP license 18 If, for some reason, all these files are missing, the Info-ZIP license
19 also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html 19 also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
20*/ 20*/
21/* 21/*
22 crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h] 22 crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h]
23 23
24 The encryption/decryption parts of this source code (as opposed to the 24 The encryption/decryption parts of this source code (as opposed to the
25 non-echoing password parts) were originally written in Europe. The 25 non-echoing password parts) were originally written in Europe. The
26 whole source package can be freely distributed, including from the USA. 26 whole source package can be freely distributed, including from the USA.
27 (Prior to January 2000, re-export from the US was a violation of US law.) 27 (Prior to January 2000, re-export from the US was a violation of US law.)
28 */ 28 */
29 29
30/* 30/*
31 This encryption code is a direct transcription of the algorithm from 31 This encryption code is a direct transcription of the algorithm from
32 Roger Schlafly, described by Phil Katz in the file appnote.txt. This 32 Roger Schlafly, described by Phil Katz in the file appnote.txt. This
33 file (appnote.txt) is distributed with the PKZIP program (even in the 33 file (appnote.txt) is distributed with the PKZIP program (even in the
34 version without encryption capabilities). 34 version without encryption capabilities).
35 */ 35 */
36 36
37 37
38#include <stdio.h> 38#include <stdio.h>
39#include <stdlib.h> 39#include <stdlib.h>
40#include <string.h> 40#include <string.h>
41#include "zlib.h" 41#include "zlib.h"
42#include "unzip.h" 42#include "unzip.h"
43 43
44#ifdef STDC 44#ifdef STDC
45# include <stddef.h> 45# include <stddef.h>
46# include <string.h> 46# include <string.h>
47# include <stdlib.h> 47# include <stdlib.h>
48#endif 48#endif
49#ifdef NO_ERRNO_H 49#ifdef NO_ERRNO_H
50 extern int errno; 50 extern int errno;
51#else 51#else
52# include <errno.h> 52# include <errno.h>
53#endif 53#endif
54 54
55 55
56#ifndef local 56#ifndef local
57# define local static 57# define local static
58#endif 58#endif
59/* compile with -Dlocal if your debugger can't find static symbols */ 59/* compile with -Dlocal if your debugger can't find static symbols */
60 60
61 61
62 62
63#if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \ 63#if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
64 !defined(CASESENSITIVITYDEFAULT_NO) 64 !defined(CASESENSITIVITYDEFAULT_NO)
65#define CASESENSITIVITYDEFAULT_NO 65#define CASESENSITIVITYDEFAULT_NO
66#endif 66#endif
67 67
68 68
69#ifndef UNZ_BUFSIZE 69#ifndef UNZ_BUFSIZE
70#define UNZ_BUFSIZE (16384) 70#define UNZ_BUFSIZE (16384)
71#endif 71#endif
72 72
73#ifndef UNZ_MAXFILENAMEINZIP 73#ifndef UNZ_MAXFILENAMEINZIP
74#define UNZ_MAXFILENAMEINZIP (256) 74#define UNZ_MAXFILENAMEINZIP (256)
75#endif 75#endif
76 76
77#ifndef ALLOC 77#ifndef ALLOC
78# define ALLOC(size) (malloc(size)) 78# define ALLOC(size) (malloc(size))
79#endif 79#endif
80#ifndef TRYFREE 80#ifndef TRYFREE
81# define TRYFREE(p) {if (p) free(p);} 81# define TRYFREE(p) {if (p) free(p);}
82#endif 82#endif
83 83
84#define SIZECENTRALDIRITEM (0x2e) 84#define SIZECENTRALDIRITEM (0x2e)
85#define SIZEZIPLOCALHEADER (0x1e) 85#define SIZEZIPLOCALHEADER (0x1e)
86 86
87 87
88 88
89 89
90const char unz_copyright[] = 90const char unz_copyright[] =
91 " unzip 0.21 Copyright 1998-2003 Gilles Vollant - http://www.winimage.com/zLibDll"; 91 " unzip 0.22 Copyright 1998-2003 Gilles Vollant - http://www.winimage.com/zLibDll";
92 92
93/* unz_file_info_interntal contain internal info about a file in zipfile*/ 93/* unz_file_info_interntal contain internal info about a file in zipfile*/
94typedef struct unz_file_info_internal_s 94typedef struct unz_file_info_internal_s
95{ 95{
96 uLong offset_curfile;/* relative offset of local header 4 bytes */ 96 uLong offset_curfile;/* relative offset of local header 4 bytes */
97} unz_file_info_internal; 97} unz_file_info_internal;
98 98
99 99
100/* file_in_zip_read_info_s contain internal information about a file in zipfile, 100/* file_in_zip_read_info_s contain internal information about a file in zipfile,
101 when reading and decompress it */ 101 when reading and decompress it */
102typedef struct 102typedef struct
103{ 103{
104 char *read_buffer; /* internal buffer for compressed data */ 104 char *read_buffer; /* internal buffer for compressed data */
105 z_stream stream; /* zLib stream structure for inflate */ 105 z_stream stream; /* zLib stream structure for inflate */
106 106
107 uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/ 107 uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/
108 uLong stream_initialised; /* flag set if stream structure is initialised*/ 108 uLong stream_initialised; /* flag set if stream structure is initialised*/
109 109
110 uLong offset_local_extrafield;/* offset of the local extra field */ 110 uLong offset_local_extrafield;/* offset of the local extra field */
111 uInt size_local_extrafield;/* size of the local extra field */ 111 uInt size_local_extrafield;/* size of the local extra field */
112 uLong pos_local_extrafield; /* position in the local extra field in read*/ 112 uLong pos_local_extrafield; /* position in the local extra field in read*/
113 113
114 uLong crc32; /* crc32 of all data uncompressed */ 114 uLong crc32; /* crc32 of all data uncompressed */
115 uLong crc32_wait; /* crc32 we must obtain after decompress all */ 115 uLong crc32_wait; /* crc32 we must obtain after decompress all */
116 uLong rest_read_compressed; /* number of byte to be decompressed */ 116 uLong rest_read_compressed; /* number of byte to be decompressed */
117 uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/ 117 uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/
118 zlib_filefunc_def z_filefunc; 118 zlib_filefunc_def z_filefunc;
119 voidpf filestream; /* io structore of the zipfile */ 119 voidpf filestream; /* io structore of the zipfile */
120 uLong compression_method; /* compression method (0==store) */ 120 uLong compression_method; /* compression method (0==store) */
121 uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ 121 uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
122 int raw; 122 int raw;
123} file_in_zip_read_info_s; 123} file_in_zip_read_info_s;
124 124
125 125
126/* unz_s contain internal information about the zipfile 126/* unz_s contain internal information about the zipfile
127*/ 127*/
128typedef struct 128typedef struct
129{ 129{
130 zlib_filefunc_def z_filefunc; 130 zlib_filefunc_def z_filefunc;
131 voidpf filestream; /* io structore of the zipfile */ 131 voidpf filestream; /* io structore of the zipfile */
132 unz_global_info gi; /* public global information */ 132 unz_global_info gi; /* public global information */
133 uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ 133 uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
134 uLong num_file; /* number of the current file in the zipfile*/ 134 uLong num_file; /* number of the current file in the zipfile*/
135 uLong pos_in_central_dir; /* pos of the current file in the central dir*/ 135 uLong pos_in_central_dir; /* pos of the current file in the central dir*/
136 uLong current_file_ok; /* flag about the usability of the current file*/ 136 uLong current_file_ok; /* flag about the usability of the current file*/
137 uLong central_pos; /* position of the beginning of the central dir*/ 137 uLong central_pos; /* position of the beginning of the central dir*/
138 138
139 uLong size_central_dir; /* size of the central directory */ 139 uLong size_central_dir; /* size of the central directory */
140 uLong offset_central_dir; /* offset of start of central directory with 140 uLong offset_central_dir; /* offset of start of central directory with
141 respect to the starting disk number */ 141 respect to the starting disk number */
142 142
143 unz_file_info cur_file_info; /* public info about the current file in zip*/ 143 unz_file_info cur_file_info; /* public info about the current file in zip*/
144 unz_file_info_internal cur_file_info_internal; /* private info about it*/ 144 unz_file_info_internal cur_file_info_internal; /* private info about it*/
145 file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current 145 file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current
146 file if we are decompressing it */ 146 file if we are decompressing it */
147 int encrypted; 147 int encrypted;
148 #ifndef NOUNCRPYT 148 #ifndef NOUNCRYPT
149 unsigned long keys[3]; /* keys defining the pseudo-random sequence */ 149 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
150 const unsigned long* pcrc_32_tab; 150 const unsigned long* pcrc_32_tab;
151 #endif 151 #endif
152} unz_s; 152} unz_s;
153 153
154 154
155#ifndef NOUNCRPYT 155#ifndef NOUNCRYPT
156#include "crypt.h" 156#include "crypt.h"
157#endif 157#endif
158 158
159/* =========================================================================== 159/* ===========================================================================
160 Read a byte from a gz_stream; update next_in and avail_in. Return EOF 160 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
161 for end of file. 161 for end of file.
162 IN assertion: the stream s has been sucessfully opened for reading. 162 IN assertion: the stream s has been sucessfully opened for reading.
163*/ 163*/
164 164
165 165
166local int unzlocal_getByte OF(( 166local int unzlocal_getByte OF((
167 const zlib_filefunc_def* pzlib_filefunc_def, 167 const zlib_filefunc_def* pzlib_filefunc_def,
168 voidpf filestream, 168 voidpf filestream,
169 int *pi)); 169 int *pi));
170 170
171local int unzlocal_getByte(pzlib_filefunc_def,filestream,pi) 171local int unzlocal_getByte(pzlib_filefunc_def,filestream,pi)
172 const zlib_filefunc_def* pzlib_filefunc_def; 172 const zlib_filefunc_def* pzlib_filefunc_def;
173 voidpf filestream; 173 voidpf filestream;
174 int *pi; 174 int *pi;
175{ 175{
176 unsigned char c; 176 unsigned char c;
177 int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1); 177 int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
178 if (err==1) 178 if (err==1)
179 { 179 {
180 *pi = (int)c; 180 *pi = (int)c;
181 return UNZ_OK; 181 return UNZ_OK;
182 } 182 }
183 else 183 else
184 { 184 {
185 if (ZERROR(*pzlib_filefunc_def,filestream)) 185 if (ZERROR(*pzlib_filefunc_def,filestream))
186 return UNZ_ERRNO; 186 return UNZ_ERRNO;
187 else 187 else
188 return UNZ_EOF; 188 return UNZ_EOF;
189 } 189 }
190} 190}
191 191
192 192
193/* =========================================================================== 193/* ===========================================================================
194 Reads a long in LSB order from the given gz_stream. Sets 194 Reads a long in LSB order from the given gz_stream. Sets
195*/ 195*/
196local int unzlocal_getShort OF(( 196local int unzlocal_getShort OF((
197 const zlib_filefunc_def* pzlib_filefunc_def, 197 const zlib_filefunc_def* pzlib_filefunc_def,
198 voidpf filestream, 198 voidpf filestream,
199 uLong *pX)); 199 uLong *pX));
200 200
201local int unzlocal_getShort (pzlib_filefunc_def,filestream,pX) 201local int unzlocal_getShort (pzlib_filefunc_def,filestream,pX)
202 const zlib_filefunc_def* pzlib_filefunc_def; 202 const zlib_filefunc_def* pzlib_filefunc_def;
203 voidpf filestream; 203 voidpf filestream;
204 uLong *pX; 204 uLong *pX;
205{ 205{
206 uLong x ; 206 uLong x ;
207 int i; 207 int i;
208 int err; 208 int err;
209 209
210 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); 210 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
211 x = (uLong)i; 211 x = (uLong)i;
212 212
213 if (err==UNZ_OK) 213 if (err==UNZ_OK)
214 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); 214 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
215 x += ((uLong)i)<<8; 215 x += ((uLong)i)<<8;
216 216
217 if (err==UNZ_OK) 217 if (err==UNZ_OK)
218 *pX = x; 218 *pX = x;
219 else 219 else
220 *pX = 0; 220 *pX = 0;
221 return err; 221 return err;
222} 222}
223 223
224local int unzlocal_getLong OF(( 224local int unzlocal_getLong OF((
225 const zlib_filefunc_def* pzlib_filefunc_def, 225 const zlib_filefunc_def* pzlib_filefunc_def,
226 voidpf filestream, 226 voidpf filestream,
227 uLong *pX)); 227 uLong *pX));
228 228
229local int unzlocal_getLong (pzlib_filefunc_def,filestream,pX) 229local int unzlocal_getLong (pzlib_filefunc_def,filestream,pX)
230 const zlib_filefunc_def* pzlib_filefunc_def; 230 const zlib_filefunc_def* pzlib_filefunc_def;
231 voidpf filestream; 231 voidpf filestream;
232 uLong *pX; 232 uLong *pX;
233{ 233{
234 uLong x ; 234 uLong x ;
235 int i; 235 int i;
236 int err; 236 int err;
237 237
238 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); 238 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
239 x = (uLong)i; 239 x = (uLong)i;
240 240
241 if (err==UNZ_OK) 241 if (err==UNZ_OK)
242 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); 242 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
243 x += ((uLong)i)<<8; 243 x += ((uLong)i)<<8;
244 244
245 if (err==UNZ_OK) 245 if (err==UNZ_OK)
246 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); 246 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
247 x += ((uLong)i)<<16; 247 x += ((uLong)i)<<16;
248 248
249 if (err==UNZ_OK) 249 if (err==UNZ_OK)
250 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i); 250 err = unzlocal_getByte(pzlib_filefunc_def,filestream,&i);
251 x += ((uLong)i)<<24; 251 x += ((uLong)i)<<24;
252 252
253 if (err==UNZ_OK) 253 if (err==UNZ_OK)
254 *pX = x; 254 *pX = x;
255 else 255 else
256 *pX = 0; 256 *pX = 0;
257 return err; 257 return err;
258} 258}
259 259
260 260
261/* My own strcmpi / strcasecmp */ 261/* My own strcmpi / strcasecmp */
262local int strcmpcasenosensitive_internal (fileName1,fileName2) 262local int strcmpcasenosensitive_internal (fileName1,fileName2)
263 const char* fileName1; 263 const char* fileName1;
264 const char* fileName2; 264 const char* fileName2;
265{ 265{
266 for (;;) 266 for (;;)
267 { 267 {
268 char c1=*(fileName1++); 268 char c1=*(fileName1++);
269 char c2=*(fileName2++); 269 char c2=*(fileName2++);
270 if ((c1>='a') && (c1<='z')) 270 if ((c1>='a') && (c1<='z'))
271 c1 -= 0x20; 271 c1 -= 0x20;
272 if ((c2>='a') && (c2<='z')) 272 if ((c2>='a') && (c2<='z'))
273 c2 -= 0x20; 273 c2 -= 0x20;
274 if (c1=='\0') 274 if (c1=='\0')
275 return ((c2=='\0') ? 0 : -1); 275 return ((c2=='\0') ? 0 : -1);
276 if (c2=='\0') 276 if (c2=='\0')
277 return 1; 277 return 1;
278 if (c1<c2) 278 if (c1<c2)
279 return -1; 279 return -1;
280 if (c1>c2) 280 if (c1>c2)
281 return 1; 281 return 1;
282 } 282 }
283} 283}
284 284
285 285
286#ifdef CASESENSITIVITYDEFAULT_NO 286#ifdef CASESENSITIVITYDEFAULT_NO
287#define CASESENSITIVITYDEFAULTVALUE 2 287#define CASESENSITIVITYDEFAULTVALUE 2
288#else 288#else
289#define CASESENSITIVITYDEFAULTVALUE 1 289#define CASESENSITIVITYDEFAULTVALUE 1
290#endif 290#endif
291 291
292#ifndef STRCMPCASENOSENTIVEFUNCTION 292#ifndef STRCMPCASENOSENTIVEFUNCTION
293#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal 293#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
294#endif 294#endif
295 295
296/* 296/*
297 Compare two filename (fileName1,fileName2). 297 Compare two filename (fileName1,fileName2).
298 If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) 298 If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
299 If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi 299 If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
300 or strcasecmp) 300 or strcasecmp)
301 If iCaseSenisivity = 0, case sensitivity is defaut of your operating system 301 If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
302 (like 1 on Unix, 2 on Windows) 302 (like 1 on Unix, 2 on Windows)
303 303
304*/ 304*/
305extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity) 305extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity)
306 const char* fileName1; 306 const char* fileName1;
307 const char* fileName2; 307 const char* fileName2;
308 int iCaseSensitivity; 308 int iCaseSensitivity;
309{ 309{
310 if (iCaseSensitivity==0) 310 if (iCaseSensitivity==0)
311 iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE; 311 iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
312 312
313 if (iCaseSensitivity==1) 313 if (iCaseSensitivity==1)
314 return strcmp(fileName1,fileName2); 314 return strcmp(fileName1,fileName2);
315 315
316 return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2); 316 return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
317} 317}
318 318
319#ifndef BUFREADCOMMENT 319#ifndef BUFREADCOMMENT
320#define BUFREADCOMMENT (0x400) 320#define BUFREADCOMMENT (0x400)
321#endif 321#endif
322 322
323/* 323/*
324 Locate the Central directory of a zipfile (at the end, just before 324 Locate the Central directory of a zipfile (at the end, just before
325 the global comment) 325 the global comment)
326*/ 326*/
327local uLong unzlocal_SearchCentralDir OF(( 327local uLong unzlocal_SearchCentralDir OF((
328 const zlib_filefunc_def* pzlib_filefunc_def, 328 const zlib_filefunc_def* pzlib_filefunc_def,
329 voidpf filestream)); 329 voidpf filestream));
330 330
331local uLong unzlocal_SearchCentralDir(pzlib_filefunc_def,filestream) 331local uLong unzlocal_SearchCentralDir(pzlib_filefunc_def,filestream)
332 const zlib_filefunc_def* pzlib_filefunc_def; 332 const zlib_filefunc_def* pzlib_filefunc_def;
333 voidpf filestream; 333 voidpf filestream;
334{ 334{
335 unsigned char* buf; 335 unsigned char* buf;
336 uLong uSizeFile; 336 uLong uSizeFile;
337 uLong uBackRead; 337 uLong uBackRead;
338 uLong uMaxBack=0xffff; /* maximum size of global comment */ 338 uLong uMaxBack=0xffff; /* maximum size of global comment */
339 uLong uPosFound=0; 339 uLong uPosFound=0;
340 340
341 if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) 341 if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
342 return 0; 342 return 0;
343 343
344 344
345 uSizeFile = ZTELL(*pzlib_filefunc_def,filestream); 345 uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
346 346
347 if (uMaxBack>uSizeFile) 347 if (uMaxBack>uSizeFile)
348 uMaxBack = uSizeFile; 348 uMaxBack = uSizeFile;
349 349
350 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); 350 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
351 if (buf==NULL) 351 if (buf==NULL)
352 return 0; 352 return 0;
353 353
354 uBackRead = 4; 354 uBackRead = 4;
355 while (uBackRead<uMaxBack) 355 while (uBackRead<uMaxBack)
356 { 356 {
357 uLong uReadSize,uReadPos ; 357 uLong uReadSize,uReadPos ;
358 int i; 358 int i;
359 if (uBackRead+BUFREADCOMMENT>uMaxBack) 359 if (uBackRead+BUFREADCOMMENT>uMaxBack)
360 uBackRead = uMaxBack; 360 uBackRead = uMaxBack;
361 else 361 else
362 uBackRead+=BUFREADCOMMENT; 362 uBackRead+=BUFREADCOMMENT;
363 uReadPos = uSizeFile-uBackRead ; 363 uReadPos = uSizeFile-uBackRead ;
364 364
365 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? 365 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
366 (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); 366 (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
367 if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) 367 if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
368 break; 368 break;
369 369
370 if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) 370 if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
371 break; 371 break;
372 372
373 for (i=(int)uReadSize-3; (i--)>0;) 373 for (i=(int)uReadSize-3; (i--)>0;)
374 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && 374 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
375 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) 375 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
376 { 376 {
377 uPosFound = uReadPos+i; 377 uPosFound = uReadPos+i;
378 break; 378 break;
379 } 379 }
380 380
381 if (uPosFound!=0) 381 if (uPosFound!=0)
382 break; 382 break;
383 } 383 }
384 TRYFREE(buf); 384 TRYFREE(buf);
385 return uPosFound; 385 return uPosFound;
386} 386}
387 387
388/* 388/*
389 Open a Zip file. path contain the full pathname (by example, 389 Open a Zip file. path contain the full pathname (by example,
390 on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer 390 on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer
391 "zlib/zlib114.zip". 391 "zlib/zlib114.zip".
392 If the zipfile cannot be opened (file doesn't exist or in not valid), the 392 If the zipfile cannot be opened (file doesn't exist or in not valid), the
393 return value is NULL. 393 return value is NULL.
394 Else, the return value is a unzFile Handle, usable with other function 394 Else, the return value is a unzFile Handle, usable with other function
395 of this unzip package. 395 of this unzip package.
396*/ 396*/
397extern unzFile ZEXPORT unzOpen2 (path, pzlib_filefunc_def) 397extern unzFile ZEXPORT unzOpen2 (path, pzlib_filefunc_def)
398 const char *path; 398 const char *path;
399 zlib_filefunc_def* pzlib_filefunc_def; 399 zlib_filefunc_def* pzlib_filefunc_def;
400{ 400{
401 unz_s us; 401 unz_s us;
402 unz_s *s; 402 unz_s *s;
403 uLong central_pos,uL; 403 uLong central_pos,uL;
404 404
405 uLong number_disk; /* number of the current dist, used for 405 uLong number_disk; /* number of the current dist, used for
406 spaning ZIP, unsupported, always 0*/ 406 spaning ZIP, unsupported, always 0*/
407 uLong number_disk_with_CD; /* number the the disk with central dir, used 407 uLong number_disk_with_CD; /* number the the disk with central dir, used
408 for spaning ZIP, unsupported, always 0*/ 408 for spaning ZIP, unsupported, always 0*/
409 uLong number_entry_CD; /* total number of entries in 409 uLong number_entry_CD; /* total number of entries in
410 the central dir 410 the central dir
411 (same than number_entry on nospan) */ 411 (same than number_entry on nospan) */
412 412
413 int err=UNZ_OK; 413 int err=UNZ_OK;
414 414
415 if (unz_copyright[0]!=' ') 415 if (unz_copyright[0]!=' ')
416 return NULL; 416 return NULL;
417 417
418 if (pzlib_filefunc_def==NULL) 418 if (pzlib_filefunc_def==NULL)
419 fill_fopen_filefunc(&us.z_filefunc); 419 fill_fopen_filefunc(&us.z_filefunc);
420 else 420 else
421 us.z_filefunc = *pzlib_filefunc_def; 421 us.z_filefunc = *pzlib_filefunc_def;
422 422
423 us.filestream= (*(us.z_filefunc.zopen_file))(us.z_filefunc.opaque, 423 us.filestream= (*(us.z_filefunc.zopen_file))(us.z_filefunc.opaque,
424 path, 424 path,
425 ZLIB_FILEFUNC_MODE_READ | 425 ZLIB_FILEFUNC_MODE_READ |
426 ZLIB_FILEFUNC_MODE_EXISTING); 426 ZLIB_FILEFUNC_MODE_EXISTING);
427 if (us.filestream==NULL) 427 if (us.filestream==NULL)
428 return NULL; 428 return NULL;
429 429
430 central_pos = unzlocal_SearchCentralDir(&us.z_filefunc,us.filestream); 430 central_pos = unzlocal_SearchCentralDir(&us.z_filefunc,us.filestream);
431 if (central_pos==0) 431 if (central_pos==0)
432 err=UNZ_ERRNO; 432 err=UNZ_ERRNO;
433 433
434 if (ZSEEK(us.z_filefunc, us.filestream, 434 if (ZSEEK(us.z_filefunc, us.filestream,
435 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0) 435 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
436 err=UNZ_ERRNO; 436 err=UNZ_ERRNO;
437 437
438 /* the signature, already checked */ 438 /* the signature, already checked */
439 if (unzlocal_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK) 439 if (unzlocal_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
440 err=UNZ_ERRNO; 440 err=UNZ_ERRNO;
441 441
442 /* number of this disk */ 442 /* number of this disk */
443 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK) 443 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
444 err=UNZ_ERRNO; 444 err=UNZ_ERRNO;
445 445
446 /* number of the disk with the start of the central directory */ 446 /* number of the disk with the start of the central directory */
447 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK) 447 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
448 err=UNZ_ERRNO; 448 err=UNZ_ERRNO;
449 449
450 /* total number of entries in the central dir on this disk */ 450 /* total number of entries in the central dir on this disk */
451 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK) 451 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK)
452 err=UNZ_ERRNO; 452 err=UNZ_ERRNO;
453 453
454 /* total number of entries in the central dir */ 454 /* total number of entries in the central dir */
455 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK) 455 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK)
456 err=UNZ_ERRNO; 456 err=UNZ_ERRNO;
457 457
458 if ((number_entry_CD!=us.gi.number_entry) || 458 if ((number_entry_CD!=us.gi.number_entry) ||
459 (number_disk_with_CD!=0) || 459 (number_disk_with_CD!=0) ||
460 (number_disk!=0)) 460 (number_disk!=0))
461 err=UNZ_BADZIPFILE; 461 err=UNZ_BADZIPFILE;
462 462
463 /* size of the central directory */ 463 /* size of the central directory */
464 if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK) 464 if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK)
465 err=UNZ_ERRNO; 465 err=UNZ_ERRNO;
466 466
467 /* offset of start of central directory with respect to the 467 /* offset of start of central directory with respect to the
468 starting disk number */ 468 starting disk number */
469 if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK) 469 if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK)
470 err=UNZ_ERRNO; 470 err=UNZ_ERRNO;
471 471
472 /* zipfile comment length */ 472 /* zipfile comment length */
473 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK) 473 if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK)
474 err=UNZ_ERRNO; 474 err=UNZ_ERRNO;
475 475
476 if ((central_pos<us.offset_central_dir+us.size_central_dir) && 476 if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
477 (err==UNZ_OK)) 477 (err==UNZ_OK))
478 err=UNZ_BADZIPFILE; 478 err=UNZ_BADZIPFILE;
479 479
480 if (err!=UNZ_OK) 480 if (err!=UNZ_OK)
481 { 481 {
482 ZCLOSE(us.z_filefunc, us.filestream); 482 ZCLOSE(us.z_filefunc, us.filestream);
483 return NULL; 483 return NULL;
484 } 484 }
485 485
486 us.byte_before_the_zipfile = central_pos - 486 us.byte_before_the_zipfile = central_pos -
487 (us.offset_central_dir+us.size_central_dir); 487 (us.offset_central_dir+us.size_central_dir);
488 us.central_pos = central_pos; 488 us.central_pos = central_pos;
489 us.pfile_in_zip_read = NULL; 489 us.pfile_in_zip_read = NULL;
490 us.encrypted = 0; 490 us.encrypted = 0;
491 491
492 492
493 s=(unz_s*)ALLOC(sizeof(unz_s)); 493 s=(unz_s*)ALLOC(sizeof(unz_s));
494 *s=us; 494 *s=us;
495 unzGoToFirstFile((unzFile)s); 495 unzGoToFirstFile((unzFile)s);
496 return (unzFile)s; 496 return (unzFile)s;
497} 497}
498 498
499 499
500extern unzFile ZEXPORT unzOpen (path) 500extern unzFile ZEXPORT unzOpen (path)
501 const char *path; 501 const char *path;
502{ 502{
503 return unzOpen2(path, NULL); 503 return unzOpen2(path, NULL);
504} 504}
505 505
506/* 506/*
507 Close a ZipFile opened with unzipOpen. 507 Close a ZipFile opened with unzipOpen.
508 If there is files inside the .Zip opened with unzipOpenCurrentFile (see later), 508 If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
509 these files MUST be closed with unzipCloseCurrentFile before call unzipClose. 509 these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
510 return UNZ_OK if there is no problem. */ 510 return UNZ_OK if there is no problem. */
511extern int ZEXPORT unzClose (file) 511extern int ZEXPORT unzClose (file)
512 unzFile file; 512 unzFile file;
513{ 513{
514 unz_s* s; 514 unz_s* s;
515 if (file==NULL) 515 if (file==NULL)
516 return UNZ_PARAMERROR; 516 return UNZ_PARAMERROR;
517 s=(unz_s*)file; 517 s=(unz_s*)file;
518 518
519 if (s->pfile_in_zip_read!=NULL) 519 if (s->pfile_in_zip_read!=NULL)
520 unzCloseCurrentFile(file); 520 unzCloseCurrentFile(file);
521 521
522 ZCLOSE(s->z_filefunc, s->filestream); 522 ZCLOSE(s->z_filefunc, s->filestream);
523 TRYFREE(s); 523 TRYFREE(s);
524 return UNZ_OK; 524 return UNZ_OK;
525} 525}
526 526
527 527
528/* 528/*
529 Write info about the ZipFile in the *pglobal_info structure. 529 Write info about the ZipFile in the *pglobal_info structure.
530 No preparation of the structure is needed 530 No preparation of the structure is needed
531 return UNZ_OK if there is no problem. */ 531 return UNZ_OK if there is no problem. */
532extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info) 532extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info)
533 unzFile file; 533 unzFile file;
534 unz_global_info *pglobal_info; 534 unz_global_info *pglobal_info;
535{ 535{
536 unz_s* s; 536 unz_s* s;
537 if (file==NULL) 537 if (file==NULL)
538 return UNZ_PARAMERROR; 538 return UNZ_PARAMERROR;
539 s=(unz_s*)file; 539 s=(unz_s*)file;
540 *pglobal_info=s->gi; 540 *pglobal_info=s->gi;
541 return UNZ_OK; 541 return UNZ_OK;
542} 542}
543 543
544 544
545/* 545/*
546 Translate date/time from Dos format to tm_unz (readable more easilty) 546 Translate date/time from Dos format to tm_unz (readable more easilty)
547*/ 547*/
548local void unzlocal_DosDateToTmuDate (ulDosDate, ptm) 548local void unzlocal_DosDateToTmuDate (ulDosDate, ptm)
549 uLong ulDosDate; 549 uLong ulDosDate;
550 tm_unz* ptm; 550 tm_unz* ptm;
551{ 551{
552 uLong uDate; 552 uLong uDate;
553 uDate = (uLong)(ulDosDate>>16); 553 uDate = (uLong)(ulDosDate>>16);
554 ptm->tm_mday = (uInt)(uDate&0x1f) ; 554 ptm->tm_mday = (uInt)(uDate&0x1f) ;
555 ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; 555 ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
556 ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; 556 ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
557 557
558 ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); 558 ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
559 ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; 559 ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
560 ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; 560 ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
561} 561}
562 562
563/* 563/*
564 Get Info about the current file in the zipfile, with internal only info 564 Get Info about the current file in the zipfile, with internal only info
565*/ 565*/
566local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file, 566local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file,
567 unz_file_info *pfile_info, 567 unz_file_info *pfile_info,
568 unz_file_info_internal 568 unz_file_info_internal
569 *pfile_info_internal, 569 *pfile_info_internal,
570 char *szFileName, 570 char *szFileName,
571 uLong fileNameBufferSize, 571 uLong fileNameBufferSize,
572 void *extraField, 572 void *extraField,
573 uLong extraFieldBufferSize, 573 uLong extraFieldBufferSize,
574 char *szComment, 574 char *szComment,
575 uLong commentBufferSize)); 575 uLong commentBufferSize));
576 576
577local int unzlocal_GetCurrentFileInfoInternal (file, 577local int unzlocal_GetCurrentFileInfoInternal (file,
578 pfile_info, 578 pfile_info,
579 pfile_info_internal, 579 pfile_info_internal,
580 szFileName, fileNameBufferSize, 580 szFileName, fileNameBufferSize,
581 extraField, extraFieldBufferSize, 581 extraField, extraFieldBufferSize,
582 szComment, commentBufferSize) 582 szComment, commentBufferSize)
583 unzFile file; 583 unzFile file;
584 unz_file_info *pfile_info; 584 unz_file_info *pfile_info;
585 unz_file_info_internal *pfile_info_internal; 585 unz_file_info_internal *pfile_info_internal;
586 char *szFileName; 586 char *szFileName;
587 uLong fileNameBufferSize; 587 uLong fileNameBufferSize;
588 void *extraField; 588 void *extraField;
589 uLong extraFieldBufferSize; 589 uLong extraFieldBufferSize;
590 char *szComment; 590 char *szComment;
591 uLong commentBufferSize; 591 uLong commentBufferSize;
592{ 592{
593 unz_s* s; 593 unz_s* s;
594 unz_file_info file_info; 594 unz_file_info file_info;
595 unz_file_info_internal file_info_internal; 595 unz_file_info_internal file_info_internal;
596 int err=UNZ_OK; 596 int err=UNZ_OK;
597 uLong uMagic; 597 uLong uMagic;
598 long lSeek=0; 598 long lSeek=0;
599 599
600 if (file==NULL) 600 if (file==NULL)
601 return UNZ_PARAMERROR; 601 return UNZ_PARAMERROR;
602 s=(unz_s*)file; 602 s=(unz_s*)file;
603 if (ZSEEK(s->z_filefunc, s->filestream, 603 if (ZSEEK(s->z_filefunc, s->filestream,
604 s->pos_in_central_dir+s->byte_before_the_zipfile, 604 s->pos_in_central_dir+s->byte_before_the_zipfile,
605 ZLIB_FILEFUNC_SEEK_SET)!=0) 605 ZLIB_FILEFUNC_SEEK_SET)!=0)
606 err=UNZ_ERRNO; 606 err=UNZ_ERRNO;
607 607
608 608
609 /* we check the magic */ 609 /* we check the magic */
610 if (err==UNZ_OK) 610 if (err==UNZ_OK)
611 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK) 611 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
612 err=UNZ_ERRNO; 612 err=UNZ_ERRNO;
613 else if (uMagic!=0x02014b50) 613 else if (uMagic!=0x02014b50)
614 err=UNZ_BADZIPFILE; 614 err=UNZ_BADZIPFILE;
615 615
616 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK) 616 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK)
617 err=UNZ_ERRNO; 617 err=UNZ_ERRNO;
618 618
619 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK) 619 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK)
620 err=UNZ_ERRNO; 620 err=UNZ_ERRNO;
621 621
622 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK) 622 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK)
623 err=UNZ_ERRNO; 623 err=UNZ_ERRNO;
624 624
625 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK) 625 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK)
626 err=UNZ_ERRNO; 626 err=UNZ_ERRNO;
627 627
628 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK) 628 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK)
629 err=UNZ_ERRNO; 629 err=UNZ_ERRNO;
630 630
631 unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date); 631 unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
632 632
633 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK) 633 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK)
634 err=UNZ_ERRNO; 634 err=UNZ_ERRNO;
635 635
636 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK) 636 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK)
637 err=UNZ_ERRNO; 637 err=UNZ_ERRNO;
638 638
639 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK) 639 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK)
640 err=UNZ_ERRNO; 640 err=UNZ_ERRNO;
641 641
642 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK) 642 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK)
643 err=UNZ_ERRNO; 643 err=UNZ_ERRNO;
644 644
645 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK) 645 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK)
646 err=UNZ_ERRNO; 646 err=UNZ_ERRNO;
647 647
648 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK) 648 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK)
649 err=UNZ_ERRNO; 649 err=UNZ_ERRNO;
650 650
651 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK) 651 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK)
652 err=UNZ_ERRNO; 652 err=UNZ_ERRNO;
653 653
654 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK) 654 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK)
655 err=UNZ_ERRNO; 655 err=UNZ_ERRNO;
656 656
657 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK) 657 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK)
658 err=UNZ_ERRNO; 658 err=UNZ_ERRNO;
659 659
660 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK) 660 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK)
661 err=UNZ_ERRNO; 661 err=UNZ_ERRNO;
662 662
663 lSeek+=file_info.size_filename; 663 lSeek+=file_info.size_filename;
664 if ((err==UNZ_OK) && (szFileName!=NULL)) 664 if ((err==UNZ_OK) && (szFileName!=NULL))
665 { 665 {
666 uLong uSizeRead ; 666 uLong uSizeRead ;
667 if (file_info.size_filename<fileNameBufferSize) 667 if (file_info.size_filename<fileNameBufferSize)
668 { 668 {
669 *(szFileName+file_info.size_filename)='\0'; 669 *(szFileName+file_info.size_filename)='\0';
670 uSizeRead = file_info.size_filename; 670 uSizeRead = file_info.size_filename;
671 } 671 }
672 else 672 else
673 uSizeRead = fileNameBufferSize; 673 uSizeRead = fileNameBufferSize;
674 674
675 if ((file_info.size_filename>0) && (fileNameBufferSize>0)) 675 if ((file_info.size_filename>0) && (fileNameBufferSize>0))
676 if (ZREAD(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead) 676 if (ZREAD(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead)
677 err=UNZ_ERRNO; 677 err=UNZ_ERRNO;
678 lSeek -= uSizeRead; 678 lSeek -= uSizeRead;
679 } 679 }
680 680
681 681
682 if ((err==UNZ_OK) && (extraField!=NULL)) 682 if ((err==UNZ_OK) && (extraField!=NULL))
683 { 683 {
684 uLong uSizeRead ; 684 uLong uSizeRead ;
685 if (file_info.size_file_extra<extraFieldBufferSize) 685 if (file_info.size_file_extra<extraFieldBufferSize)
686 uSizeRead = file_info.size_file_extra; 686 uSizeRead = file_info.size_file_extra;
687 else 687 else
688 uSizeRead = extraFieldBufferSize; 688 uSizeRead = extraFieldBufferSize;
689 689
690 if (lSeek!=0) 690 if (lSeek!=0)
691 if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) 691 if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
692 lSeek=0; 692 lSeek=0;
693 else 693 else
694 err=UNZ_ERRNO; 694 err=UNZ_ERRNO;
695 if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) 695 if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
696 if (ZREAD(s->z_filefunc, s->filestream,extraField,uSizeRead)!=uSizeRead) 696 if (ZREAD(s->z_filefunc, s->filestream,extraField,uSizeRead)!=uSizeRead)
697 err=UNZ_ERRNO; 697 err=UNZ_ERRNO;
698 lSeek += file_info.size_file_extra - uSizeRead; 698 lSeek += file_info.size_file_extra - uSizeRead;
699 } 699 }
700 else 700 else
701 lSeek+=file_info.size_file_extra; 701 lSeek+=file_info.size_file_extra;
702 702
703 703
704 if ((err==UNZ_OK) && (szComment!=NULL)) 704 if ((err==UNZ_OK) && (szComment!=NULL))
705 { 705 {
706 uLong uSizeRead ; 706 uLong uSizeRead ;
707 if (file_info.size_file_comment<commentBufferSize) 707 if (file_info.size_file_comment<commentBufferSize)
708 { 708 {
709 *(szComment+file_info.size_file_comment)='\0'; 709 *(szComment+file_info.size_file_comment)='\0';
710 uSizeRead = file_info.size_file_comment; 710 uSizeRead = file_info.size_file_comment;
711 } 711 }
712 else 712 else
713 uSizeRead = commentBufferSize; 713 uSizeRead = commentBufferSize;
714 714
715 if (lSeek!=0) 715 if (lSeek!=0)
716 if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) 716 if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
717 lSeek=0; 717 lSeek=0;
718 else 718 else
719 err=UNZ_ERRNO; 719 err=UNZ_ERRNO;
720 if ((file_info.size_file_comment>0) && (commentBufferSize>0)) 720 if ((file_info.size_file_comment>0) && (commentBufferSize>0))
721 if (ZREAD(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead) 721 if (ZREAD(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead)
722 err=UNZ_ERRNO; 722 err=UNZ_ERRNO;
723 lSeek+=file_info.size_file_comment - uSizeRead; 723 lSeek+=file_info.size_file_comment - uSizeRead;
724 } 724 }
725 else 725 else
726 lSeek+=file_info.size_file_comment; 726 lSeek+=file_info.size_file_comment;
727 727
728 if ((err==UNZ_OK) && (pfile_info!=NULL)) 728 if ((err==UNZ_OK) && (pfile_info!=NULL))
729 *pfile_info=file_info; 729 *pfile_info=file_info;
730 730
731 if ((err==UNZ_OK) && (pfile_info_internal!=NULL)) 731 if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
732 *pfile_info_internal=file_info_internal; 732 *pfile_info_internal=file_info_internal;
733 733
734 return err; 734 return err;
735} 735}
736 736
737 737
738 738
739/* 739/*
740 Write info about the ZipFile in the *pglobal_info structure. 740 Write info about the ZipFile in the *pglobal_info structure.
741 No preparation of the structure is needed 741 No preparation of the structure is needed
742 return UNZ_OK if there is no problem. 742 return UNZ_OK if there is no problem.
743*/ 743*/
744extern int ZEXPORT unzGetCurrentFileInfo (file, 744extern int ZEXPORT unzGetCurrentFileInfo (file,
745 pfile_info, 745 pfile_info,
746 szFileName, fileNameBufferSize, 746 szFileName, fileNameBufferSize,
747 extraField, extraFieldBufferSize, 747 extraField, extraFieldBufferSize,
748 szComment, commentBufferSize) 748 szComment, commentBufferSize)
749 unzFile file; 749 unzFile file;
750 unz_file_info *pfile_info; 750 unz_file_info *pfile_info;
751 char *szFileName; 751 char *szFileName;
752 uLong fileNameBufferSize; 752 uLong fileNameBufferSize;
753 void *extraField; 753 void *extraField;
754 uLong extraFieldBufferSize; 754 uLong extraFieldBufferSize;
755 char *szComment; 755 char *szComment;
756 uLong commentBufferSize; 756 uLong commentBufferSize;
757{ 757{
758 return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL, 758 return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,
759 szFileName,fileNameBufferSize, 759 szFileName,fileNameBufferSize,
760 extraField,extraFieldBufferSize, 760 extraField,extraFieldBufferSize,
761 szComment,commentBufferSize); 761 szComment,commentBufferSize);
762} 762}
763 763
764/* 764/*
765 Set the current file of the zipfile to the first file. 765 Set the current file of the zipfile to the first file.
766 return UNZ_OK if there is no problem 766 return UNZ_OK if there is no problem
767*/ 767*/
768extern int ZEXPORT unzGoToFirstFile (file) 768extern int ZEXPORT unzGoToFirstFile (file)
769 unzFile file; 769 unzFile file;
770{ 770{
771 int err=UNZ_OK; 771 int err=UNZ_OK;
772 unz_s* s; 772 unz_s* s;
773 if (file==NULL) 773 if (file==NULL)
774 return UNZ_PARAMERROR; 774 return UNZ_PARAMERROR;
775 s=(unz_s*)file; 775 s=(unz_s*)file;
776 s->pos_in_central_dir=s->offset_central_dir; 776 s->pos_in_central_dir=s->offset_central_dir;
777 s->num_file=0; 777 s->num_file=0;
778 err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, 778 err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
779 &s->cur_file_info_internal, 779 &s->cur_file_info_internal,
780 NULL,0,NULL,0,NULL,0); 780 NULL,0,NULL,0,NULL,0);
781 s->current_file_ok = (err == UNZ_OK); 781 s->current_file_ok = (err == UNZ_OK);
782 return err; 782 return err;
783} 783}
784 784
785/* 785/*
786 Set the current file of the zipfile to the next file. 786 Set the current file of the zipfile to the next file.
787 return UNZ_OK if there is no problem 787 return UNZ_OK if there is no problem
788 return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. 788 return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
789*/ 789*/
790extern int ZEXPORT unzGoToNextFile (file) 790extern int ZEXPORT unzGoToNextFile (file)
791 unzFile file; 791 unzFile file;
792{ 792{
793 unz_s* s; 793 unz_s* s;
794 int err; 794 int err;
795 795
796 if (file==NULL) 796 if (file==NULL)
797 return UNZ_PARAMERROR; 797 return UNZ_PARAMERROR;
798 s=(unz_s*)file; 798 s=(unz_s*)file;
799 if (!s->current_file_ok) 799 if (!s->current_file_ok)
800 return UNZ_END_OF_LIST_OF_FILE; 800 return UNZ_END_OF_LIST_OF_FILE;
801 if (s->num_file+1==s->gi.number_entry) 801 if (s->num_file+1==s->gi.number_entry)
802 return UNZ_END_OF_LIST_OF_FILE; 802 return UNZ_END_OF_LIST_OF_FILE;
803 803
804 s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + 804 s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
805 s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; 805 s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
806 s->num_file++; 806 s->num_file++;
807 err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, 807 err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
808 &s->cur_file_info_internal, 808 &s->cur_file_info_internal,
809 NULL,0,NULL,0,NULL,0); 809 NULL,0,NULL,0,NULL,0);
810 s->current_file_ok = (err == UNZ_OK); 810 s->current_file_ok = (err == UNZ_OK);
811 return err; 811 return err;
812} 812}
813 813
814 814
815/* 815/*
816 Try locate the file szFileName in the zipfile. 816 Try locate the file szFileName in the zipfile.
817 For the iCaseSensitivity signification, see unzipStringFileNameCompare 817 For the iCaseSensitivity signification, see unzipStringFileNameCompare
818 818
819 return value : 819 return value :
820 UNZ_OK if the file is found. It becomes the current file. 820 UNZ_OK if the file is found. It becomes the current file.
821 UNZ_END_OF_LIST_OF_FILE if the file is not found 821 UNZ_END_OF_LIST_OF_FILE if the file is not found
822*/ 822*/
823extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity) 823extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
824 unzFile file; 824 unzFile file;
825 const char *szFileName; 825 const char *szFileName;
826 int iCaseSensitivity; 826 int iCaseSensitivity;
827{ 827{
828 unz_s* s; 828 unz_s* s;
829 int err; 829 int err;
830 830
831 831
832 uLong num_fileSaved; 832 uLong num_fileSaved;
833 uLong pos_in_central_dirSaved; 833 uLong pos_in_central_dirSaved;
834 834
835 835
836 if (file==NULL) 836 if (file==NULL)
837 return UNZ_PARAMERROR; 837 return UNZ_PARAMERROR;
838 838
839 if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP) 839 if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
840 return UNZ_PARAMERROR; 840 return UNZ_PARAMERROR;
841 841
842 s=(unz_s*)file; 842 s=(unz_s*)file;
843 if (!s->current_file_ok) 843 if (!s->current_file_ok)
844 return UNZ_END_OF_LIST_OF_FILE; 844 return UNZ_END_OF_LIST_OF_FILE;
845 845
846 num_fileSaved = s->num_file; 846 num_fileSaved = s->num_file;
847 pos_in_central_dirSaved = s->pos_in_central_dir; 847 pos_in_central_dirSaved = s->pos_in_central_dir;
848 848
849 err = unzGoToFirstFile(file); 849 err = unzGoToFirstFile(file);
850 850
851 while (err == UNZ_OK) 851 while (err == UNZ_OK)
852 { 852 {
853 char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; 853 char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
854 unzGetCurrentFileInfo(file,NULL, 854 unzGetCurrentFileInfo(file,NULL,
855 szCurrentFileName,sizeof(szCurrentFileName)-1, 855 szCurrentFileName,sizeof(szCurrentFileName)-1,
856 NULL,0,NULL,0); 856 NULL,0,NULL,0);
857 if (unzStringFileNameCompare(szCurrentFileName, 857 if (unzStringFileNameCompare(szCurrentFileName,
858 szFileName,iCaseSensitivity)==0) 858 szFileName,iCaseSensitivity)==0)
859 return UNZ_OK; 859 return UNZ_OK;
860 err = unzGoToNextFile(file); 860 err = unzGoToNextFile(file);
861 } 861 }
862 862
863 s->num_file = num_fileSaved ; 863 s->num_file = num_fileSaved ;
864 s->pos_in_central_dir = pos_in_central_dirSaved ; 864 s->pos_in_central_dir = pos_in_central_dirSaved ;
865 return err; 865 return err;
866} 866}
867 867
868 868
869/* 869/*
870/////////////////////////////////////////// 870///////////////////////////////////////////
871// Contributed by Ryan Haksi (mailto://cryogen@infoserve.net) 871// Contributed by Ryan Haksi (mailto://cryogen@infoserve.net)
872// I need random access 872// I need random access
873// 873//
874// Further optimization could be realized by adding an ability 874// Further optimization could be realized by adding an ability
875// to cache the directory in memory. The goal being a single 875// to cache the directory in memory. The goal being a single
876// comprehensive file read to put the file I need in a memory. 876// comprehensive file read to put the file I need in a memory.
877*/ 877*/
878 878
879/* 879/*
880typedef struct unz_file_pos_s 880typedef struct unz_file_pos_s
881{ 881{
882 uLong pos_in_zip_directory; // offset in file 882 uLong pos_in_zip_directory; // offset in file
883 uLong num_of_file; // # of file 883 uLong num_of_file; // # of file
884} unz_file_pos; 884} unz_file_pos;
885*/ 885*/
886 886
887extern int ZEXPORT unzGetFilePos(file, file_pos) 887extern int ZEXPORT unzGetFilePos(file, file_pos)
888 unzFile file; 888 unzFile file;
889 unz_file_pos* file_pos; 889 unz_file_pos* file_pos;
890{ 890{
891 unz_s* s; 891 unz_s* s;
892 892
893 if (file==NULL || file_pos==NULL) 893 if (file==NULL || file_pos==NULL)
894 return UNZ_PARAMERROR; 894 return UNZ_PARAMERROR;
895 s=(unz_s*)file; 895 s=(unz_s*)file;
896 if (!s->current_file_ok) 896 if (!s->current_file_ok)
897 return UNZ_END_OF_LIST_OF_FILE; 897 return UNZ_END_OF_LIST_OF_FILE;
898 898
899 file_pos->pos_in_zip_directory = s->pos_in_central_dir; 899 file_pos->pos_in_zip_directory = s->pos_in_central_dir;
900 file_pos->num_of_file = s->num_file; 900 file_pos->num_of_file = s->num_file;
901 901
902 return UNZ_OK; 902 return UNZ_OK;
903} 903}
904 904
905extern int ZEXPORT unzGoToFilePos(file, file_pos) 905extern int ZEXPORT unzGoToFilePos(file, file_pos)
906 unzFile file; 906 unzFile file;
907 unz_file_pos* file_pos; 907 unz_file_pos* file_pos;
908{ 908{
909 unz_s* s; 909 unz_s* s;
910 int err; 910 int err;
911 911
912 if (file==NULL || file_pos==NULL) 912 if (file==NULL || file_pos==NULL)
913 return UNZ_PARAMERROR; 913 return UNZ_PARAMERROR;
914 s=(unz_s*)file; 914 s=(unz_s*)file;
915 915
916 /* jump to the right spot */ 916 /* jump to the right spot */
917 s->pos_in_central_dir = file_pos->pos_in_zip_directory; 917 s->pos_in_central_dir = file_pos->pos_in_zip_directory;
918 s->num_file = file_pos->num_of_file; 918 s->num_file = file_pos->num_of_file;
919 919
920 /* set the current file */ 920 /* set the current file */
921 err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, 921 err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
922 &s->cur_file_info_internal, 922 &s->cur_file_info_internal,
923 NULL,0,NULL,0,NULL,0); 923 NULL,0,NULL,0,NULL,0);
924 /* return results */ 924 /* return results */
925 s->current_file_ok = (err == UNZ_OK); 925 s->current_file_ok = (err == UNZ_OK);
926 return err; 926 return err;
927} 927}
928 928
929/* 929/*
930// Unzip Helper Functions - should be here? 930// Unzip Helper Functions - should be here?
931/////////////////////////////////////////// 931///////////////////////////////////////////
932*/ 932*/
933 933
934/* 934/*
935 Read the local header of the current zipfile 935 Read the local header of the current zipfile
936 Check the coherency of the local header and info in the end of central 936 Check the coherency of the local header and info in the end of central
937 directory about this file 937 directory about this file
938 store in *piSizeVar the size of extra info in local header 938 store in *piSizeVar the size of extra info in local header
939 (filename and size of extra field data) 939 (filename and size of extra field data)
940*/ 940*/
941local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar, 941local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
942 poffset_local_extrafield, 942 poffset_local_extrafield,
943 psize_local_extrafield) 943 psize_local_extrafield)
944 unz_s* s; 944 unz_s* s;
945 uInt* piSizeVar; 945 uInt* piSizeVar;
946 uLong *poffset_local_extrafield; 946 uLong *poffset_local_extrafield;
947 uInt *psize_local_extrafield; 947 uInt *psize_local_extrafield;
948{ 948{
949 uLong uMagic,uData,uFlags; 949 uLong uMagic,uData,uFlags;
950 uLong size_filename; 950 uLong size_filename;
951 uLong size_extra_field; 951 uLong size_extra_field;
952 int err=UNZ_OK; 952 int err=UNZ_OK;
953 953
954 *piSizeVar = 0; 954 *piSizeVar = 0;
955 *poffset_local_extrafield = 0; 955 *poffset_local_extrafield = 0;
956 *psize_local_extrafield = 0; 956 *psize_local_extrafield = 0;
957 957
958 if (ZSEEK(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile + 958 if (ZSEEK(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile +
959 s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0) 959 s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
960 return UNZ_ERRNO; 960 return UNZ_ERRNO;
961 961
962 962
963 if (err==UNZ_OK) 963 if (err==UNZ_OK)
964 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK) 964 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
965 err=UNZ_ERRNO; 965 err=UNZ_ERRNO;
966 else if (uMagic!=0x04034b50) 966 else if (uMagic!=0x04034b50)
967 err=UNZ_BADZIPFILE; 967 err=UNZ_BADZIPFILE;
968 968
969 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) 969 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
970 err=UNZ_ERRNO; 970 err=UNZ_ERRNO;
971/* 971/*
972 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) 972 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
973 err=UNZ_BADZIPFILE; 973 err=UNZ_BADZIPFILE;
974*/ 974*/
975 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK) 975 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK)
976 err=UNZ_ERRNO; 976 err=UNZ_ERRNO;
977 977
978 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) 978 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
979 err=UNZ_ERRNO; 979 err=UNZ_ERRNO;
980 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method)) 980 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
981 err=UNZ_BADZIPFILE; 981 err=UNZ_BADZIPFILE;
982 982
983 if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) && 983 if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
984 (s->cur_file_info.compression_method!=Z_DEFLATED)) 984 (s->cur_file_info.compression_method!=Z_DEFLATED))
985 err=UNZ_BADZIPFILE; 985 err=UNZ_BADZIPFILE;
986 986
987 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */ 987 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */
988 err=UNZ_ERRNO; 988 err=UNZ_ERRNO;
989 989
990 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */ 990 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */
991 err=UNZ_ERRNO; 991 err=UNZ_ERRNO;
992 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && 992 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
993 ((uFlags & 8)==0)) 993 ((uFlags & 8)==0))
994 err=UNZ_BADZIPFILE; 994 err=UNZ_BADZIPFILE;
995 995
996 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */ 996 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */
997 err=UNZ_ERRNO; 997 err=UNZ_ERRNO;
998 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && 998 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
999 ((uFlags & 8)==0)) 999 ((uFlags & 8)==0))
1000 err=UNZ_BADZIPFILE; 1000 err=UNZ_BADZIPFILE;
1001 1001
1002 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */ 1002 if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */
1003 err=UNZ_ERRNO; 1003 err=UNZ_ERRNO;
1004 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && 1004 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) &&
1005 ((uFlags & 8)==0)) 1005 ((uFlags & 8)==0))
1006 err=UNZ_BADZIPFILE; 1006 err=UNZ_BADZIPFILE;
1007 1007
1008 1008
1009 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK) 1009 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK)
1010 err=UNZ_ERRNO; 1010 err=UNZ_ERRNO;
1011 else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename)) 1011 else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
1012 err=UNZ_BADZIPFILE; 1012 err=UNZ_BADZIPFILE;
1013 1013
1014 *piSizeVar += (uInt)size_filename; 1014 *piSizeVar += (uInt)size_filename;
1015 1015
1016 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK) 1016 if (unzlocal_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK)
1017 err=UNZ_ERRNO; 1017 err=UNZ_ERRNO;
1018 *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile + 1018 *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
1019 SIZEZIPLOCALHEADER + size_filename; 1019 SIZEZIPLOCALHEADER + size_filename;
1020 *psize_local_extrafield = (uInt)size_extra_field; 1020 *psize_local_extrafield = (uInt)size_extra_field;
1021 1021
1022 *piSizeVar += (uInt)size_extra_field; 1022 *piSizeVar += (uInt)size_extra_field;
1023 1023
1024 return err; 1024 return err;
1025} 1025}
1026 1026
1027/* 1027/*
1028 Open for reading data the current file in the zipfile. 1028 Open for reading data the current file in the zipfile.
1029 If there is no error and the file is opened, the return value is UNZ_OK. 1029 If there is no error and the file is opened, the return value is UNZ_OK.
1030*/ 1030*/
1031extern int ZEXPORT unzOpenCurrentFile3 (file, method, level, raw, password) 1031extern int ZEXPORT unzOpenCurrentFile3 (file, method, level, raw, password)
1032 unzFile file; 1032 unzFile file;
1033 int* method; 1033 int* method;
1034 int* level; 1034 int* level;
1035 int raw; 1035 int raw;
1036 const char* password; 1036 const char* password;
1037{ 1037{
1038 int err=UNZ_OK; 1038 int err=UNZ_OK;
1039 uInt iSizeVar; 1039 uInt iSizeVar;
1040 unz_s* s; 1040 unz_s* s;
1041 file_in_zip_read_info_s* pfile_in_zip_read_info; 1041 file_in_zip_read_info_s* pfile_in_zip_read_info;
1042 uLong offset_local_extrafield; /* offset of the local extra field */ 1042 uLong offset_local_extrafield; /* offset of the local extra field */
1043 uInt size_local_extrafield; /* size of the local extra field */ 1043 uInt size_local_extrafield; /* size of the local extra field */
1044 #ifndef NOUNCRPYT 1044 #ifndef NOUNCRYPT
1045 char source[12]; 1045 char source[12];
1046 #else 1046 #else
1047 if (password != NULL) 1047 if (password != NULL)
1048 return UNZ_PARAMERROR; 1048 return UNZ_PARAMERROR;
1049 #endif 1049 #endif
1050 1050
1051 if (file==NULL) 1051 if (file==NULL)
1052 return UNZ_PARAMERROR; 1052 return UNZ_PARAMERROR;
1053 s=(unz_s*)file; 1053 s=(unz_s*)file;
1054 if (!s->current_file_ok) 1054 if (!s->current_file_ok)
1055 return UNZ_PARAMERROR; 1055 return UNZ_PARAMERROR;
1056 1056
1057 if (s->pfile_in_zip_read != NULL) 1057 if (s->pfile_in_zip_read != NULL)
1058 unzCloseCurrentFile(file); 1058 unzCloseCurrentFile(file);
1059 1059
1060 if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar, 1060 if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
1061 &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK) 1061 &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
1062 return UNZ_BADZIPFILE; 1062 return UNZ_BADZIPFILE;
1063 1063
1064 pfile_in_zip_read_info = (file_in_zip_read_info_s*) 1064 pfile_in_zip_read_info = (file_in_zip_read_info_s*)
1065 ALLOC(sizeof(file_in_zip_read_info_s)); 1065 ALLOC(sizeof(file_in_zip_read_info_s));
1066 if (pfile_in_zip_read_info==NULL) 1066 if (pfile_in_zip_read_info==NULL)
1067 return UNZ_INTERNALERROR; 1067 return UNZ_INTERNALERROR;
1068 1068
1069 pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE); 1069 pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE);
1070 pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; 1070 pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
1071 pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; 1071 pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
1072 pfile_in_zip_read_info->pos_local_extrafield=0; 1072 pfile_in_zip_read_info->pos_local_extrafield=0;
1073 pfile_in_zip_read_info->raw=raw; 1073 pfile_in_zip_read_info->raw=raw;
1074 1074
1075 if (pfile_in_zip_read_info->read_buffer==NULL) 1075 if (pfile_in_zip_read_info->read_buffer==NULL)
1076 { 1076 {
1077 TRYFREE(pfile_in_zip_read_info); 1077 TRYFREE(pfile_in_zip_read_info);
1078 return UNZ_INTERNALERROR; 1078 return UNZ_INTERNALERROR;
1079 } 1079 }
1080 1080
1081 pfile_in_zip_read_info->stream_initialised=0; 1081 pfile_in_zip_read_info->stream_initialised=0;
1082 1082
1083 if (method!=NULL) 1083 if (method!=NULL)
1084 *method = (int)s->cur_file_info.compression_method; 1084 *method = (int)s->cur_file_info.compression_method;
1085 1085
1086 if (level!=NULL) 1086 if (level!=NULL)
1087 { 1087 {
1088 *level = 6; 1088 *level = 6;
1089 switch (s->cur_file_info.flag & 0x06) 1089 switch (s->cur_file_info.flag & 0x06)
1090 { 1090 {
1091 case 6 : *level = 1; break; 1091 case 6 : *level = 1; break;
1092 case 4 : *level = 2; break; 1092 case 4 : *level = 2; break;
1093 case 2 : *level = 9; break; 1093 case 2 : *level = 9; break;
1094 } 1094 }
1095 } 1095 }
1096 1096
1097 if ((s->cur_file_info.compression_method!=0) && 1097 if ((s->cur_file_info.compression_method!=0) &&
1098 (s->cur_file_info.compression_method!=Z_DEFLATED)) 1098 (s->cur_file_info.compression_method!=Z_DEFLATED))
1099 err=UNZ_BADZIPFILE; 1099 err=UNZ_BADZIPFILE;
1100 1100
1101 pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc; 1101 pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
1102 pfile_in_zip_read_info->crc32=0; 1102 pfile_in_zip_read_info->crc32=0;
1103 pfile_in_zip_read_info->compression_method = 1103 pfile_in_zip_read_info->compression_method =
1104 s->cur_file_info.compression_method; 1104 s->cur_file_info.compression_method;
1105 pfile_in_zip_read_info->filestream=s->filestream; 1105 pfile_in_zip_read_info->filestream=s->filestream;
1106 pfile_in_zip_read_info->z_filefunc=s->z_filefunc; 1106 pfile_in_zip_read_info->z_filefunc=s->z_filefunc;
1107 pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile; 1107 pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
1108 1108
1109 pfile_in_zip_read_info->stream.total_out = 0; 1109 pfile_in_zip_read_info->stream.total_out = 0;
1110 1110
1111 if ((s->cur_file_info.compression_method==Z_DEFLATED) && 1111 if ((s->cur_file_info.compression_method==Z_DEFLATED) &&
1112 (!raw)) 1112 (!raw))
1113 { 1113 {
1114 pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; 1114 pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
1115 pfile_in_zip_read_info->stream.zfree = (free_func)0; 1115 pfile_in_zip_read_info->stream.zfree = (free_func)0;
1116 pfile_in_zip_read_info->stream.opaque = (voidpf)0; 1116 pfile_in_zip_read_info->stream.opaque = (voidpf)0;
1117 1117 pfile_in_zip_read_info->stream.next_in = (voidpf)0;
1118 err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); 1118 pfile_in_zip_read_info->stream.avail_in = 0;
1119 if (err == Z_OK) 1119
1120 pfile_in_zip_read_info->stream_initialised=1; 1120 err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
1121 else 1121 if (err == Z_OK)
1122 return err; 1122 pfile_in_zip_read_info->stream_initialised=1;
1123 /* windowBits is passed < 0 to tell that there is no zlib header. 1123 else
1124 * Note that in this case inflate *requires* an extra "dummy" byte 1124 return err;
1125 * after the compressed stream in order to complete decompression and 1125 /* windowBits is passed < 0 to tell that there is no zlib header.
1126 * return Z_STREAM_END. 1126 * Note that in this case inflate *requires* an extra "dummy" byte
1127 * In unzip, i don't wait absolutely Z_STREAM_END because I known the 1127 * after the compressed stream in order to complete decompression and
1128 * size of both compressed and uncompressed data 1128 * return Z_STREAM_END.
1129 */ 1129 * In unzip, i don't wait absolutely Z_STREAM_END because I known the
1130 } 1130 * size of both compressed and uncompressed data
1131 pfile_in_zip_read_info->rest_read_compressed = 1131 */
1132 s->cur_file_info.compressed_size ; 1132 }
1133 pfile_in_zip_read_info->rest_read_uncompressed = 1133 pfile_in_zip_read_info->rest_read_compressed =
1134 s->cur_file_info.uncompressed_size ; 1134 s->cur_file_info.compressed_size ;
1135 1135 pfile_in_zip_read_info->rest_read_uncompressed =
1136 1136 s->cur_file_info.uncompressed_size ;
1137 pfile_in_zip_read_info->pos_in_zipfile = 1137
1138 s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + 1138
1139 iSizeVar; 1139 pfile_in_zip_read_info->pos_in_zipfile =
1140 1140 s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
1141 pfile_in_zip_read_info->stream.avail_in = (uInt)0; 1141 iSizeVar;
1142 1142
1143 s->pfile_in_zip_read = pfile_in_zip_read_info; 1143 pfile_in_zip_read_info->stream.avail_in = (uInt)0;
1144 1144
1145 #ifndef NOUNCRPYT 1145 s->pfile_in_zip_read = pfile_in_zip_read_info;
1146 if (password != NULL) 1146
1147 { 1147 #ifndef NOUNCRYPT
1148 int i; 1148 if (password != NULL)
1149 s->pcrc_32_tab = get_crc_table(); 1149 {
1150 init_keys(password,s->keys,s->pcrc_32_tab); 1150 int i;
1151 if (ZSEEK(s->z_filefunc, s->filestream, 1151 s->pcrc_32_tab = get_crc_table();
1152 s->pfile_in_zip_read->pos_in_zipfile + 1152 init_keys(password,s->keys,s->pcrc_32_tab);
1153 s->pfile_in_zip_read->byte_before_the_zipfile, 1153 if (ZSEEK(s->z_filefunc, s->filestream,
1154 SEEK_SET)!=0) 1154 s->pfile_in_zip_read->pos_in_zipfile +
1155 return UNZ_INTERNALERROR; 1155 s->pfile_in_zip_read->byte_before_the_zipfile,
1156 if(ZREAD(s->z_filefunc, s->filestream,source, 12)<12) 1156 SEEK_SET)!=0)
1157 return UNZ_INTERNALERROR; 1157 return UNZ_INTERNALERROR;
1158 1158 if(ZREAD(s->z_filefunc, s->filestream,source, 12)<12)
1159 for (i = 0; i<12; i++) 1159 return UNZ_INTERNALERROR;
1160 zdecode(s->keys,s->pcrc_32_tab,source[i]); 1160
1161 1161 for (i = 0; i<12; i++)
1162 s->pfile_in_zip_read->pos_in_zipfile+=12; 1162 zdecode(s->keys,s->pcrc_32_tab,source[i]);
1163 s->encrypted=1; 1163
1164 } 1164 s->pfile_in_zip_read->pos_in_zipfile+=12;
1165 #endif 1165 s->encrypted=1;
1166 1166 }
1167 1167 #endif
1168 return UNZ_OK; 1168
1169} 1169
1170 1170 return UNZ_OK;
1171extern int ZEXPORT unzOpenCurrentFile (file) 1171}
1172 unzFile file; 1172
1173{ 1173extern int ZEXPORT unzOpenCurrentFile (file)
1174 return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL); 1174 unzFile file;
1175} 1175{
1176 1176 return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
1177extern int ZEXPORT unzOpenCurrentFilePassword (file, password) 1177}
1178 unzFile file; 1178
1179 const char* password; 1179extern int ZEXPORT unzOpenCurrentFilePassword (file, password)
1180{ 1180 unzFile file;
1181 return unzOpenCurrentFile3(file, NULL, NULL, 0, password); 1181 const char* password;
1182} 1182{
1183 1183 return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
1184extern int ZEXPORT unzOpenCurrentFile2 (file,method,level,raw) 1184}
1185 unzFile file; 1185
1186 int* method; 1186extern int ZEXPORT unzOpenCurrentFile2 (file,method,level,raw)
1187 int* level; 1187 unzFile file;
1188 int raw; 1188 int* method;
1189{ 1189 int* level;
1190 return unzOpenCurrentFile3(file, method, level, raw, NULL); 1190 int raw;
1191} 1191{
1192 1192 return unzOpenCurrentFile3(file, method, level, raw, NULL);
1193/* 1193}
1194 Read bytes from the current file. 1194
1195 buf contain buffer where data must be copied 1195/*
1196 len the size of buf. 1196 Read bytes from the current file.
1197 1197 buf contain buffer where data must be copied
1198 return the number of byte copied if somes bytes are copied 1198 len the size of buf.
1199 return 0 if the end of file was reached 1199
1200 return <0 with error code if there is an error 1200 return the number of byte copied if somes bytes are copied
1201 (UNZ_ERRNO for IO error, or zLib error for uncompress error) 1201 return 0 if the end of file was reached
1202*/ 1202 return <0 with error code if there is an error
1203extern int ZEXPORT unzReadCurrentFile (file, buf, len) 1203 (UNZ_ERRNO for IO error, or zLib error for uncompress error)
1204 unzFile file; 1204*/
1205 voidp buf; 1205extern int ZEXPORT unzReadCurrentFile (file, buf, len)
1206 unsigned len; 1206 unzFile file;
1207{ 1207 voidp buf;
1208 int err=UNZ_OK; 1208 unsigned len;
1209 uInt iRead = 0; 1209{
1210 unz_s* s; 1210 int err=UNZ_OK;
1211 file_in_zip_read_info_s* pfile_in_zip_read_info; 1211 uInt iRead = 0;
1212 if (file==NULL) 1212 unz_s* s;
1213 return UNZ_PARAMERROR; 1213 file_in_zip_read_info_s* pfile_in_zip_read_info;
1214 s=(unz_s*)file; 1214 if (file==NULL)
1215 pfile_in_zip_read_info=s->pfile_in_zip_read; 1215 return UNZ_PARAMERROR;
1216 1216 s=(unz_s*)file;
1217 if (pfile_in_zip_read_info==NULL) 1217 pfile_in_zip_read_info=s->pfile_in_zip_read;
1218 return UNZ_PARAMERROR; 1218
1219 1219 if (pfile_in_zip_read_info==NULL)
1220 1220 return UNZ_PARAMERROR;
1221 if ((pfile_in_zip_read_info->read_buffer == NULL)) 1221
1222 return UNZ_END_OF_LIST_OF_FILE; 1222
1223 if (len==0) 1223 if ((pfile_in_zip_read_info->read_buffer == NULL))
1224 return 0; 1224 return UNZ_END_OF_LIST_OF_FILE;
1225 1225 if (len==0)
1226 pfile_in_zip_read_info->stream.next_out = (Bytef*)buf; 1226 return 0;
1227 1227
1228 pfile_in_zip_read_info->stream.avail_out = (uInt)len; 1228 pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
1229 1229
1230 if (len>pfile_in_zip_read_info->rest_read_uncompressed) 1230 pfile_in_zip_read_info->stream.avail_out = (uInt)len;
1231 pfile_in_zip_read_info->stream.avail_out = 1231
1232 (uInt)pfile_in_zip_read_info->rest_read_uncompressed; 1232 if (len>pfile_in_zip_read_info->rest_read_uncompressed)
1233 1233 pfile_in_zip_read_info->stream.avail_out =
1234 while (pfile_in_zip_read_info->stream.avail_out>0) 1234 (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
1235 { 1235
1236 if ((pfile_in_zip_read_info->stream.avail_in==0) && 1236 while (pfile_in_zip_read_info->stream.avail_out>0)
1237 (pfile_in_zip_read_info->rest_read_compressed>0)) 1237 {
1238 { 1238 if ((pfile_in_zip_read_info->stream.avail_in==0) &&
1239 uInt uReadThis = UNZ_BUFSIZE; 1239 (pfile_in_zip_read_info->rest_read_compressed>0))
1240 if (pfile_in_zip_read_info->rest_read_compressed<uReadThis) 1240 {
1241 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed; 1241 uInt uReadThis = UNZ_BUFSIZE;
1242 if (uReadThis == 0) 1242 if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
1243 return UNZ_EOF; 1243 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
1244 if (ZSEEK(pfile_in_zip_read_info->z_filefunc, 1244 if (uReadThis == 0)
1245 pfile_in_zip_read_info->filestream, 1245 return UNZ_EOF;
1246 pfile_in_zip_read_info->pos_in_zipfile + 1246 if (ZSEEK(pfile_in_zip_read_info->z_filefunc,
1247 pfile_in_zip_read_info->byte_before_the_zipfile, 1247 pfile_in_zip_read_info->filestream,
1248 ZLIB_FILEFUNC_SEEK_SET)!=0) 1248 pfile_in_zip_read_info->pos_in_zipfile +
1249 return UNZ_ERRNO; 1249 pfile_in_zip_read_info->byte_before_the_zipfile,
1250 if (ZREAD(pfile_in_zip_read_info->z_filefunc, 1250 ZLIB_FILEFUNC_SEEK_SET)!=0)
1251 pfile_in_zip_read_info->filestream, 1251 return UNZ_ERRNO;
1252 pfile_in_zip_read_info->read_buffer, 1252 if (ZREAD(pfile_in_zip_read_info->z_filefunc,
1253 uReadThis)!=uReadThis) 1253 pfile_in_zip_read_info->filestream,
1254 return UNZ_ERRNO; 1254 pfile_in_zip_read_info->read_buffer,
1255 1255 uReadThis)!=uReadThis)
1256 1256 return UNZ_ERRNO;
1257 #ifndef NOUNCRPYT 1257
1258 if(s->encrypted) 1258
1259 { 1259 #ifndef NOUNCRYPT
1260 uInt i; 1260 if(s->encrypted)
1261 for(i=0;i<uReadThis;i++) 1261 {
1262 pfile_in_zip_read_info->read_buffer[i] = 1262 uInt i;
1263 zdecode(s->keys,s->pcrc_32_tab, 1263 for(i=0;i<uReadThis;i++)
1264 pfile_in_zip_read_info->read_buffer[i]); 1264 pfile_in_zip_read_info->read_buffer[i] =
1265 } 1265 zdecode(s->keys,s->pcrc_32_tab,
1266 #endif 1266 pfile_in_zip_read_info->read_buffer[i]);
1267 1267 }
1268 1268 #endif
1269 pfile_in_zip_read_info->pos_in_zipfile += uReadThis; 1269
1270 1270
1271 pfile_in_zip_read_info->rest_read_compressed-=uReadThis; 1271 pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
1272 1272
1273 pfile_in_zip_read_info->stream.next_in = 1273 pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
1274 (Bytef*)pfile_in_zip_read_info->read_buffer; 1274
1275 pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; 1275 pfile_in_zip_read_info->stream.next_in =
1276 } 1276 (Bytef*)pfile_in_zip_read_info->read_buffer;
1277 1277 pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
1278 if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw)) 1278 }
1279 { 1279
1280 uInt uDoCopy,i ; 1280 if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw))
1281 1281 {
1282 if ((pfile_in_zip_read_info->stream.avail_in == 0) && 1282 uInt uDoCopy,i ;
1283 (pfile_in_zip_read_info->rest_read_compressed == 0)) 1283
1284 return (iRead==0) ? UNZ_EOF : iRead; 1284 if ((pfile_in_zip_read_info->stream.avail_in == 0) &&
1285 1285 (pfile_in_zip_read_info->rest_read_compressed == 0))
1286 if (pfile_in_zip_read_info->stream.avail_out < 1286 return (iRead==0) ? UNZ_EOF : iRead;
1287 pfile_in_zip_read_info->stream.avail_in) 1287
1288 uDoCopy = pfile_in_zip_read_info->stream.avail_out ; 1288 if (pfile_in_zip_read_info->stream.avail_out <
1289 else 1289 pfile_in_zip_read_info->stream.avail_in)
1290 uDoCopy = pfile_in_zip_read_info->stream.avail_in ; 1290 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
1291 1291 else
1292 for (i=0;i<uDoCopy;i++) 1292 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
1293 *(pfile_in_zip_read_info->stream.next_out+i) = 1293
1294 *(pfile_in_zip_read_info->stream.next_in+i); 1294 for (i=0;i<uDoCopy;i++)
1295 1295 *(pfile_in_zip_read_info->stream.next_out+i) =
1296 pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, 1296 *(pfile_in_zip_read_info->stream.next_in+i);
1297 pfile_in_zip_read_info->stream.next_out, 1297
1298 uDoCopy); 1298 pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
1299 pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy; 1299 pfile_in_zip_read_info->stream.next_out,
1300 pfile_in_zip_read_info->stream.avail_in -= uDoCopy; 1300 uDoCopy);
1301 pfile_in_zip_read_info->stream.avail_out -= uDoCopy; 1301 pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
1302 pfile_in_zip_read_info->stream.next_out += uDoCopy; 1302 pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
1303 pfile_in_zip_read_info->stream.next_in += uDoCopy; 1303 pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
1304 pfile_in_zip_read_info->stream.total_out += uDoCopy; 1304 pfile_in_zip_read_info->stream.next_out += uDoCopy;
1305 iRead += uDoCopy; 1305 pfile_in_zip_read_info->stream.next_in += uDoCopy;
1306 } 1306 pfile_in_zip_read_info->stream.total_out += uDoCopy;
1307 else 1307 iRead += uDoCopy;
1308 { 1308 }
1309 uLong uTotalOutBefore,uTotalOutAfter; 1309 else
1310 const Bytef *bufBefore; 1310 {
1311 uLong uOutThis; 1311 uLong uTotalOutBefore,uTotalOutAfter;
1312 int flush=Z_SYNC_FLUSH; 1312 const Bytef *bufBefore;
1313 1313 uLong uOutThis;
1314 uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; 1314 int flush=Z_SYNC_FLUSH;
1315 bufBefore = pfile_in_zip_read_info->stream.next_out; 1315
1316 1316 uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
1317 /* 1317 bufBefore = pfile_in_zip_read_info->stream.next_out;
1318 if ((pfile_in_zip_read_info->rest_read_uncompressed == 1318
1319 pfile_in_zip_read_info->stream.avail_out) && 1319 /*
1320 (pfile_in_zip_read_info->rest_read_compressed == 0)) 1320 if ((pfile_in_zip_read_info->rest_read_uncompressed ==
1321 flush = Z_FINISH; 1321 pfile_in_zip_read_info->stream.avail_out) &&
1322 */ 1322 (pfile_in_zip_read_info->rest_read_compressed == 0))
1323 err=inflate(&pfile_in_zip_read_info->stream,flush); 1323 flush = Z_FINISH;
1324 1324 */
1325 uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; 1325 err=inflate(&pfile_in_zip_read_info->stream,flush);
1326 uOutThis = uTotalOutAfter-uTotalOutBefore; 1326
1327 1327 uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
1328 pfile_in_zip_read_info->crc32 = 1328 uOutThis = uTotalOutAfter-uTotalOutBefore;
1329 crc32(pfile_in_zip_read_info->crc32,bufBefore, 1329
1330 (uInt)(uOutThis)); 1330 pfile_in_zip_read_info->crc32 =
1331 1331 crc32(pfile_in_zip_read_info->crc32,bufBefore,
1332 pfile_in_zip_read_info->rest_read_uncompressed -= 1332 (uInt)(uOutThis));
1333 uOutThis; 1333
1334 1334 pfile_in_zip_read_info->rest_read_uncompressed -=
1335 iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); 1335 uOutThis;
1336 1336
1337 if (err==Z_STREAM_END) 1337 iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
1338 return (iRead==0) ? UNZ_EOF : iRead; 1338
1339 if (err!=Z_OK) 1339 if (err==Z_STREAM_END)
1340 break; 1340 return (iRead==0) ? UNZ_EOF : iRead;
1341 } 1341 if (err!=Z_OK)
1342 } 1342 break;
1343 1343 }
1344 if (err==Z_OK) 1344 }
1345 return iRead; 1345
1346 return err; 1346 if (err==Z_OK)
1347} 1347 return iRead;
1348 1348 return err;
1349 1349}
1350/* 1350
1351 Give the current position in uncompressed data 1351
1352*/ 1352/*
1353extern z_off_t ZEXPORT unztell (file) 1353 Give the current position in uncompressed data
1354 unzFile file; 1354*/
1355{ 1355extern z_off_t ZEXPORT unztell (file)
1356 unz_s* s; 1356 unzFile file;
1357 file_in_zip_read_info_s* pfile_in_zip_read_info; 1357{
1358 if (file==NULL) 1358 unz_s* s;
1359 return UNZ_PARAMERROR; 1359 file_in_zip_read_info_s* pfile_in_zip_read_info;
1360 s=(unz_s*)file; 1360 if (file==NULL)
1361 pfile_in_zip_read_info=s->pfile_in_zip_read; 1361 return UNZ_PARAMERROR;
1362 1362 s=(unz_s*)file;
1363 if (pfile_in_zip_read_info==NULL) 1363 pfile_in_zip_read_info=s->pfile_in_zip_read;
1364 return UNZ_PARAMERROR; 1364
1365 1365 if (pfile_in_zip_read_info==NULL)
1366 return (z_off_t)pfile_in_zip_read_info->stream.total_out; 1366 return UNZ_PARAMERROR;
1367} 1367
1368 1368 return (z_off_t)pfile_in_zip_read_info->stream.total_out;
1369 1369}
1370/* 1370
1371 return 1 if the end of file was reached, 0 elsewhere 1371
1372*/ 1372/*
1373extern int ZEXPORT unzeof (file) 1373 return 1 if the end of file was reached, 0 elsewhere
1374 unzFile file; 1374*/
1375{ 1375extern int ZEXPORT unzeof (file)
1376 unz_s* s; 1376 unzFile file;
1377 file_in_zip_read_info_s* pfile_in_zip_read_info; 1377{
1378 if (file==NULL) 1378 unz_s* s;
1379 return UNZ_PARAMERROR; 1379 file_in_zip_read_info_s* pfile_in_zip_read_info;
1380 s=(unz_s*)file; 1380 if (file==NULL)
1381 pfile_in_zip_read_info=s->pfile_in_zip_read; 1381 return UNZ_PARAMERROR;
1382 1382 s=(unz_s*)file;
1383 if (pfile_in_zip_read_info==NULL) 1383 pfile_in_zip_read_info=s->pfile_in_zip_read;
1384 return UNZ_PARAMERROR; 1384
1385 1385 if (pfile_in_zip_read_info==NULL)
1386 if (pfile_in_zip_read_info->rest_read_uncompressed == 0) 1386 return UNZ_PARAMERROR;
1387 return 1; 1387
1388 else 1388 if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
1389 return 0; 1389 return 1;
1390} 1390 else
1391 1391 return 0;
1392 1392}
1393 1393
1394/* 1394
1395 Read extra field from the current file (opened by unzOpenCurrentFile) 1395
1396 This is the local-header version of the extra field (sometimes, there is 1396/*
1397 more info in the local-header version than in the central-header) 1397 Read extra field from the current file (opened by unzOpenCurrentFile)
1398 1398 This is the local-header version of the extra field (sometimes, there is
1399 if buf==NULL, it return the size of the local extra field that can be read 1399 more info in the local-header version than in the central-header)
1400 1400
1401 if buf!=NULL, len is the size of the buffer, the extra header is copied in 1401 if buf==NULL, it return the size of the local extra field that can be read
1402 buf. 1402
1403 the return value is the number of bytes copied in buf, or (if <0) 1403 if buf!=NULL, len is the size of the buffer, the extra header is copied in
1404 the error code 1404 buf.
1405*/ 1405 the return value is the number of bytes copied in buf, or (if <0)
1406extern int ZEXPORT unzGetLocalExtrafield (file,buf,len) 1406 the error code
1407 unzFile file; 1407*/
1408 voidp buf; 1408extern int ZEXPORT unzGetLocalExtrafield (file,buf,len)
1409 unsigned len; 1409 unzFile file;
1410{ 1410 voidp buf;
1411 unz_s* s; 1411 unsigned len;
1412 file_in_zip_read_info_s* pfile_in_zip_read_info; 1412{
1413 uInt read_now; 1413 unz_s* s;
1414 uLong size_to_read; 1414 file_in_zip_read_info_s* pfile_in_zip_read_info;
1415 1415 uInt read_now;
1416 if (file==NULL) 1416 uLong size_to_read;
1417 return UNZ_PARAMERROR; 1417
1418 s=(unz_s*)file; 1418 if (file==NULL)
1419 pfile_in_zip_read_info=s->pfile_in_zip_read; 1419 return UNZ_PARAMERROR;
1420 1420 s=(unz_s*)file;
1421 if (pfile_in_zip_read_info==NULL) 1421 pfile_in_zip_read_info=s->pfile_in_zip_read;
1422 return UNZ_PARAMERROR; 1422
1423 1423 if (pfile_in_zip_read_info==NULL)
1424 size_to_read = (pfile_in_zip_read_info->size_local_extrafield - 1424 return UNZ_PARAMERROR;
1425 pfile_in_zip_read_info->pos_local_extrafield); 1425
1426 1426 size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
1427 if (buf==NULL) 1427 pfile_in_zip_read_info->pos_local_extrafield);
1428 return (int)size_to_read; 1428
1429 1429 if (buf==NULL)
1430 if (len>size_to_read) 1430 return (int)size_to_read;
1431 read_now = (uInt)size_to_read; 1431
1432 else 1432 if (len>size_to_read)
1433 read_now = (uInt)len ; 1433 read_now = (uInt)size_to_read;
1434 1434 else
1435 if (read_now==0) 1435 read_now = (uInt)len ;
1436 return 0; 1436
1437 1437 if (read_now==0)
1438 if (ZSEEK(pfile_in_zip_read_info->z_filefunc, 1438 return 0;
1439 pfile_in_zip_read_info->filestream, 1439
1440 pfile_in_zip_read_info->offset_local_extrafield + 1440 if (ZSEEK(pfile_in_zip_read_info->z_filefunc,
1441 pfile_in_zip_read_info->pos_local_extrafield, 1441 pfile_in_zip_read_info->filestream,
1442 ZLIB_FILEFUNC_SEEK_SET)!=0) 1442 pfile_in_zip_read_info->offset_local_extrafield +
1443 return UNZ_ERRNO; 1443 pfile_in_zip_read_info->pos_local_extrafield,
1444 1444 ZLIB_FILEFUNC_SEEK_SET)!=0)
1445 if (ZREAD(pfile_in_zip_read_info->z_filefunc, 1445 return UNZ_ERRNO;
1446 pfile_in_zip_read_info->filestream, 1446
1447 buf,size_to_read)!=size_to_read) 1447 if (ZREAD(pfile_in_zip_read_info->z_filefunc,
1448 return UNZ_ERRNO; 1448 pfile_in_zip_read_info->filestream,
1449 1449 buf,size_to_read)!=size_to_read)
1450 return (int)read_now; 1450 return UNZ_ERRNO;
1451} 1451
1452 1452 return (int)read_now;
1453/* 1453}
1454 Close the file in zip opened with unzipOpenCurrentFile 1454
1455 Return UNZ_CRCERROR if all the file was read but the CRC is not good 1455/*
1456*/ 1456 Close the file in zip opened with unzipOpenCurrentFile
1457extern int ZEXPORT unzCloseCurrentFile (file) 1457 Return UNZ_CRCERROR if all the file was read but the CRC is not good
1458 unzFile file; 1458*/
1459{ 1459extern int ZEXPORT unzCloseCurrentFile (file)
1460 int err=UNZ_OK; 1460 unzFile file;
1461 1461{
1462 unz_s* s; 1462 int err=UNZ_OK;
1463 file_in_zip_read_info_s* pfile_in_zip_read_info; 1463
1464 if (file==NULL) 1464 unz_s* s;
1465 return UNZ_PARAMERROR; 1465 file_in_zip_read_info_s* pfile_in_zip_read_info;
1466 s=(unz_s*)file; 1466 if (file==NULL)
1467 pfile_in_zip_read_info=s->pfile_in_zip_read; 1467 return UNZ_PARAMERROR;
1468 1468 s=(unz_s*)file;
1469 if (pfile_in_zip_read_info==NULL) 1469 pfile_in_zip_read_info=s->pfile_in_zip_read;
1470 return UNZ_PARAMERROR; 1470
1471 1471 if (pfile_in_zip_read_info==NULL)
1472 1472 return UNZ_PARAMERROR;
1473 if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) && 1473
1474 (!pfile_in_zip_read_info->raw)) 1474
1475 { 1475 if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) &&
1476 if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) 1476 (!pfile_in_zip_read_info->raw))
1477 err=UNZ_CRCERROR; 1477 {
1478 } 1478 if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
1479 1479 err=UNZ_CRCERROR;
1480 1480 }
1481 TRYFREE(pfile_in_zip_read_info->read_buffer); 1481
1482 pfile_in_zip_read_info->read_buffer = NULL; 1482
1483 if (pfile_in_zip_read_info->stream_initialised) 1483 TRYFREE(pfile_in_zip_read_info->read_buffer);
1484 inflateEnd(&pfile_in_zip_read_info->stream); 1484 pfile_in_zip_read_info->read_buffer = NULL;
1485 1485 if (pfile_in_zip_read_info->stream_initialised)
1486 pfile_in_zip_read_info->stream_initialised = 0; 1486 inflateEnd(&pfile_in_zip_read_info->stream);
1487 TRYFREE(pfile_in_zip_read_info); 1487
1488 1488 pfile_in_zip_read_info->stream_initialised = 0;
1489 s->pfile_in_zip_read=NULL; 1489 TRYFREE(pfile_in_zip_read_info);
1490 1490
1491 return err; 1491 s->pfile_in_zip_read=NULL;
1492} 1492
1493 1493 return err;
1494 1494}
1495/* 1495
1496 Get the global comment string of the ZipFile, in the szComment buffer. 1496
1497 uSizeBuf is the size of the szComment buffer. 1497/*
1498 return the number of byte copied or an error code <0 1498 Get the global comment string of the ZipFile, in the szComment buffer.
1499*/ 1499 uSizeBuf is the size of the szComment buffer.
1500extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf) 1500 return the number of byte copied or an error code <0
1501 unzFile file; 1501*/
1502 char *szComment; 1502extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf)
1503 uLong uSizeBuf; 1503 unzFile file;
1504{ 1504 char *szComment;
1505 int err=UNZ_OK; 1505 uLong uSizeBuf;
1506 unz_s* s; 1506{
1507 uLong uReadThis ; 1507 int err=UNZ_OK;
1508 if (file==NULL) 1508 unz_s* s;
1509 return UNZ_PARAMERROR; 1509 uLong uReadThis ;
1510 s=(unz_s*)file; 1510 if (file==NULL)
1511 1511 return UNZ_PARAMERROR;
1512 uReadThis = uSizeBuf; 1512 s=(unz_s*)file;
1513 if (uReadThis>s->gi.size_comment) 1513
1514 uReadThis = s->gi.size_comment; 1514 uReadThis = uSizeBuf;
1515 1515 if (uReadThis>s->gi.size_comment)
1516 if (ZSEEK(s->z_filefunc,s->filestream,s->central_pos+22,ZLIB_FILEFUNC_SEEK_SET)!=0) 1516 uReadThis = s->gi.size_comment;
1517 return UNZ_ERRNO; 1517
1518 1518 if (ZSEEK(s->z_filefunc,s->filestream,s->central_pos+22,ZLIB_FILEFUNC_SEEK_SET)!=0)
1519 if (uReadThis>0) 1519 return UNZ_ERRNO;
1520 { 1520
1521 *szComment='\0'; 1521 if (uReadThis>0)
1522 if (ZREAD(s->z_filefunc,s->filestream,szComment,uReadThis)!=uReadThis) 1522 {
1523 return UNZ_ERRNO; 1523 *szComment='\0';
1524 } 1524 if (ZREAD(s->z_filefunc,s->filestream,szComment,uReadThis)!=uReadThis)
1525 1525 return UNZ_ERRNO;
1526 if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment)) 1526 }
1527 *(szComment+s->gi.size_comment)='\0'; 1527
1528 return (int)uReadThis; 1528 if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
1529} 1529 *(szComment+s->gi.size_comment)='\0';
1530 return (int)uReadThis;
1531}