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