Debian Codegraph @

thirdparty/minizip/unzip.c

raw symbols SCIP
1 /* unzip.c -- IO for uncompress .zip files using zlib
2 part of the MiniZip project - ( https://www.winimage.com/zLibDll/minizip.html )
3
4 Copyright (C) 1998-2026 Gilles Vollant (minizip) ( https://www.winimage.com/zLibDll/minizip.html )
5
6 Modifications of Unzip for Zip64
7 Copyright (C) 2007-2008 Even Rouault
8
9 Modifications for Zip64 support on both zip and unzip
10 Copyright (C) 2009-2010 Mathias Svensson ( https://result42.com )
11
12 For more info read MiniZip_info.txt
13
14
15 ------------------------------------------------------------------------------------
16 Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of
17 compatibility with older software. The following is from the original crypt.c.
18 Code woven in by Terry Thorsen 1/2003.
19
20 Copyright (c) 1990-2000 Info-ZIP. All rights reserved.
21
22 See the accompanying file LICENSE, version 2000-Apr-09 or later
23 (the contents of which are also included in zip.h) for terms of use.
24 If, for some reason, all these files are missing, the Info-ZIP license
25 also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
26
27 crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h]
28
29 The encryption/decryption parts of this source code (as opposed to the
30 non-echoing password parts) were originally written in Europe. The
31 whole source package can be freely distributed, including from the USA.
32 (Prior to January 2000, re-export from the US was a violation of US law.)
33
34 This encryption code is a direct transcription of the algorithm from
35 Roger Schlafly, described by Phil Katz in the file appnote.txt. This
36 file (appnote.txt) is distributed with the PKZIP program (even in the
37 version without encryption capabilities).
38
39 ------------------------------------------------------------------------------------
40
41 Changes in unzip.c
42
43 2007-2008 - Even Rouault - Addition of cpl_unzGetCurrentFileZStreamPos
44 2007-2008 - Even Rouault - Decoration of symbol names unz* -> cpl_unz*
45 2007-2008 - Even Rouault - Remove old C style function prototypes
46 2007-2008 - Even Rouault - Add unzip support for ZIP64
47
48 Copyright (C) 2007-2008 Even Rouault
49
50
51 Oct-2009 - Mathias Svensson - Removed cpl_* from symbol names (Even Rouault added them but since this is now moved to a new project (minizip64) I renamed them again).
52 Oct-2009 - Mathias Svensson - Fixed problem if uncompressed size was > 4G and compressed size was <4G
53 should only read the compressed/uncompressed size from the Zip64 format if
54 the size from normal header was 0xFFFFFFFF
55 Oct-2009 - Mathias Svensson - Applied some bug fixes from patches received from Gilles Vollant
56 Oct-2009 - Mathias Svensson - Applied support to unzip files with compression method BZIP2 (bzip2 lib is required)
57 Patch created by Daniel Borca
58
59 Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
60
61 Copyright (C) 1998 - 2010 Gilles Vollant, Even Rouault, Mathias Svensson
62
63 */
64
65
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69
70 #ifdef ZLIB_DLL
71 # undef ZLIB_DLL
72 #endif
73 #include "zlib.h"
74 #include "unzip.h"
75
76 #ifdef STDC
77 # include <stddef.h>
78 #endif
79 #ifdef NO_ERRNO_H
80 extern int errno;
81 #else
82 # include <errno.h>
83 #endif
84
85
86 #ifndef local
87 # define local static
88 #endif
89 /* compile with -Dlocal if your debugger can't find static symbols */
90
91
92 #ifndef CASESENSITIVITYDEFAULT_NO
93 # if (!defined(__unix__) && !defined(__unix) || defined(__CYGWIN__)) && !defined(CASESENSITIVITYDEFAULT_YES)
94 # define CASESENSITIVITYDEFAULT_NO
95 # endif
96 #endif
97
98
99 #ifndef UNZ_BUFSIZE
100 #define UNZ_BUFSIZE (16384)
101 #endif
102
103 #ifndef UNZ_MAXFILENAMEINZIP
104 #define UNZ_MAXFILENAMEINZIP (256)
105 #endif
106
107 #ifndef ALLOC
108 # define ALLOC(size) (malloc(size))
109 #endif
110
111 #define SIZECENTRALDIRITEM (0x2e)
112 #define SIZEZIPLOCALHEADER (0x1e)
113
114
115 const char unz_copyright[] =
116 " unzip 1.01 Copyright 1998-2004 Gilles Vollant - https://www.winimage.com/zLibDll/minizip.html";
117
118 /* unz_file_info64_internal contain internal info about a file in zipfile*/
119 typedef struct unz_file_info64_internal_s
120 {
121 ZPOS64_T offset_curfile;/* relative offset of local header 8 bytes */
122 } unz_file_info64_internal;
123
124
125 /* file_in_zip_read_info_s contain internal information about a file in zipfile,
126 when reading and decompress it */
127 typedef struct
128 {
129 char *read_buffer; /* internal buffer for compressed data */
130 z_stream stream; /* zLib stream structure for inflate */
131
132 #ifdef HAVE_BZIP2
133 bz_stream bstream; /* bzLib stream structure for bziped */
134 #endif
135
136 ZPOS64_T pos_in_zipfile; /* position in byte on the zipfile, for fseek*/
137 uLong stream_initialised; /* flag set if stream structure is initialised*/
138
139 ZPOS64_T offset_local_extrafield;/* offset of the local extra field */
140 uInt size_local_extrafield;/* size of the local extra field */
141 ZPOS64_T pos_local_extrafield; /* position in the local extra field in read*/
142 ZPOS64_T total_out_64;
143
144 uLong crc32; /* crc32 of all data uncompressed */
145 uLong crc32_wait; /* crc32 we must obtain after decompress all */
146 ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */
147 ZPOS64_T rest_read_uncompressed;/*number of byte to be obtained after decomp*/
148 zlib_filefunc64_32_def z_filefunc;
149 voidpf filestream; /* io structure of the zipfile */
150 uLong compression_method; /* compression method (0==store) */
151 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
152 int raw;
153 int extra_size;
154 } file_in_zip64_read_info_s;
155
156
157 /* unz64_s contain internal information about the zipfile
158 */
159 typedef struct
160 {
161 zlib_filefunc64_32_def z_filefunc;
162 int is64bitOpenFunction;
163 voidpf filestream; /* io structure of the zipfile */
164 unz_global_info64 gi; /* public global information */
165 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
166 ZPOS64_T num_file; /* number of the current file in the zipfile*/
167 ZPOS64_T pos_in_central_dir; /* pos of the current file in the central dir*/
168 ZPOS64_T current_file_ok; /* flag about the usability of the current file*/
169 ZPOS64_T central_pos; /* position of the beginning of the central dir*/
170
171 ZPOS64_T size_central_dir; /* size of the central directory */
172 ZPOS64_T offset_central_dir; /* offset of start of central directory with
173 respect to the starting disk number */
174
175 unz_file_info64 cur_file_info; /* public info about the current file in zip*/
176 unz_file_info64_internal cur_file_info_internal; /* private info about it*/
177 file_in_zip64_read_info_s* pfile_in_zip_read; /* structure about the current
178 file if we are decompressing it */
179 int encrypted;
180
181 int isZip64;
182
183 # ifndef NOUNCRYPT
184 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
185 const z_crc_t* pcrc_32_tab;
186 # endif
187 } unz64_s;
188
189
190 #ifndef NOUNCRYPT
191 #include "crypt.h"
192 #endif
193
194
195 /* ===========================================================================
196 Reads a long in LSB order from the given gz_stream. Sets
197 */
198
199 local int unz64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def,
200 voidpf filestream,
201 uLong *pX) {
202 unsigned char c[2];
203 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,2);
204 if (err==2)
205 {
206 *pX = c[0] | ((uLong)c[1] << 8);
207 return UNZ_OK;
208 }
209 else
210 {
211 *pX = 0;
212 if (ZERROR64(*pzlib_filefunc_def,filestream))
213 return UNZ_ERRNO;
214 else
215 return UNZ_EOF;
216 }
217 }
218
219 local int unz64local_getLong(const zlib_filefunc64_32_def* pzlib_filefunc_def,
220 voidpf filestream,
221 uLong *pX) {
222 unsigned char c[4];
223 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,4);
224 if (err==4)
225 {
226 *pX = c[0] | ((uLong)c[1] << 8) | ((uLong)c[2] << 16) | ((uLong)c[3] << 24);
227 return UNZ_OK;
228 }
229 else
230 {
231 *pX = 0;
232 if (ZERROR64(*pzlib_filefunc_def,filestream))
233 return UNZ_ERRNO;
234 else
235 return UNZ_EOF;
236 }
237 }
238
239
240 local int unz64local_getLong64(const zlib_filefunc64_32_def* pzlib_filefunc_def,
241 voidpf filestream,
242 ZPOS64_T *pX) {
243 unsigned char c[8];
244 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,c,8);
245 if (err==8)
246 {
247 *pX = c[0] | ((ZPOS64_T)c[1] << 8) | ((ZPOS64_T)c[2] << 16) | ((ZPOS64_T)c[3] << 24)
248 | ((ZPOS64_T)c[4] << 32) | ((ZPOS64_T)c[5] << 40) | ((ZPOS64_T)c[6] << 48) | ((ZPOS64_T)c[7] << 56);
249 return UNZ_OK;
250 }
251 else
252 {
253 *pX = 0;
254 if (ZERROR64(*pzlib_filefunc_def,filestream))
255 return UNZ_ERRNO;
256 else
257 return UNZ_EOF;
258 }
259 }
260
261 /* My own strcmpi / strcasecmp */
262 local int strcmpcasenosensitive_internal(const char* fileName1, const char* fileName2) {
263 for (;;)
264 {
265 char c1=*(fileName1++);
266 char c2=*(fileName2++);
267 if ((c1>='a') && (c1<='z'))
268 c1 -= 0x20;
269 if ((c2>='a') && (c2<='z'))
270 c2 -= 0x20;
271 if (c1=='\0')
272 return ((c2=='\0') ? 0 : -1);
273 if (c2=='\0')
274 return 1;
275 if (c1<c2)
276 return -1;
277 if (c1>c2)
278 return 1;
279 }
280 }
281
282
283 #ifdef CASESENSITIVITYDEFAULT_NO
284 #define CASESENSITIVITYDEFAULTVALUE 2
285 #else
286 #define CASESENSITIVITYDEFAULTVALUE 1
287 #endif
288
289 #ifndef STRCMPCASENOSENTIVEFUNCTION
290 #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
291 #endif
292
293 /*
294 Compare two filenames (fileName1,fileName2).
295 If iCaseSensitivity = 1, comparison is case sensitive (like strcmp)
296 If iCaseSensitivity = 2, comparison is not case sensitive (like strcmpi
297 or strcasecmp)
298 If iCaseSensitivity = 0, case sensitivity is default of your operating system
299 (like 1 on Unix, 2 on Windows)
300
301 */
302 extern int ZEXPORT unzStringFileNameCompare (const char* fileName1,
303 const char* fileName2,
304 int iCaseSensitivity) {
305 if (iCaseSensitivity==0)
306 iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
307
308 if (iCaseSensitivity==1)
309 return strcmp(fileName1,fileName2);
310
311 return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
312 }
313
314 #ifndef BUFREADCOMMENT
315 #define BUFREADCOMMENT (0x400)
316 #endif
317
318 #ifndef CENTRALDIRINVALID
319 #define CENTRALDIRINVALID ((ZPOS64_T)(-1))
320 #endif
321
322 /*
323 Locate the Central directory of a zipfile (at the end, just before
324 the global comment)
325 */
326 local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) {
327 unsigned char* buf;
328 ZPOS64_T uSizeFile;
329 ZPOS64_T uBackRead;
330 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
331 ZPOS64_T uPosFound=CENTRALDIRINVALID;
332
333 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
334 return CENTRALDIRINVALID;
335
336
337 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
338
339 if (uMaxBack>uSizeFile)
340 uMaxBack = uSizeFile;
341
342 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
343 if (buf==NULL)
344 return CENTRALDIRINVALID;
345
346 uBackRead = 4;
347 while (uBackRead<uMaxBack)
348 {
349 uLong uReadSize;
350 ZPOS64_T uReadPos ;
351 int i;
352 if (uBackRead+BUFREADCOMMENT>uMaxBack)
353 uBackRead = uMaxBack;
354 else
355 uBackRead+=BUFREADCOMMENT;
356 uReadPos = uSizeFile-uBackRead ;
357
358 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
359 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
360 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
361 break;
362
363 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
364 break;
365
366 for (i=(int)uReadSize-3; (i--)>0;)
367 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
368 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
369 {
370 uPosFound = uReadPos+(unsigned)i;
371 break;
372 }
373
374 if (uPosFound!=CENTRALDIRINVALID)
375 break;
376 }
377 free(buf);
378 return uPosFound;
379 }
380
381
382 /*
383 Locate the Central directory 64 of a zipfile (at the end, just before
384 the global comment)
385 */
386 local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def,
387 voidpf filestream) {
388 unsigned char* buf;
389 ZPOS64_T uSizeFile;
390 ZPOS64_T uBackRead;
391 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
392 ZPOS64_T uPosFound=CENTRALDIRINVALID;
393 uLong uL;
394 ZPOS64_T relativeOffset;
395
396 if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
397 return CENTRALDIRINVALID;
398
399
400 uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
401
402 if (uMaxBack>uSizeFile)
403 uMaxBack = uSizeFile;
404
405 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
406 if (buf==NULL)
407 return CENTRALDIRINVALID;
408
409 uBackRead = 4;
410 while (uBackRead<uMaxBack)
411 {
412 uLong uReadSize;
413 ZPOS64_T uReadPos;
414 int i;
415 if (uBackRead+BUFREADCOMMENT>uMaxBack)
416 uBackRead = uMaxBack;
417 else
418 uBackRead+=BUFREADCOMMENT;
419 uReadPos = uSizeFile-uBackRead ;
420
421 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
422 (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
423 if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
424 break;
425
426 if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
427 break;
428
429 for (i=(int)uReadSize-3; (i--)>0;)
430 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
431 ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
432 {
433 uPosFound = uReadPos+(unsigned)i;
434 break;
435 }
436
437 if (uPosFound!=CENTRALDIRINVALID)
438 break;
439 }
440 free(buf);
441 if (uPosFound == CENTRALDIRINVALID)
442 return CENTRALDIRINVALID;
443
444 /* Zip64 end of central directory locator */
445 if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
446 return CENTRALDIRINVALID;
447
448 /* the signature, already checked */
449 if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
450 return CENTRALDIRINVALID;
451
452 /* number of the disk with the start of the zip64 end of central directory */
453 if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
454 return CENTRALDIRINVALID;
455 if (uL != 0)
456 return CENTRALDIRINVALID;
457
458 /* relative offset of the zip64 end of central directory record */
459 if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK)
460 return CENTRALDIRINVALID;
461
462 /* total number of disks */
463 if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
464 return CENTRALDIRINVALID;
465 if (uL != 1)
466 return CENTRALDIRINVALID;
467
468 /* Goto end of central directory record */
469 if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
470 return CENTRALDIRINVALID;
471
472 /* the signature */
473 if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
474 return CENTRALDIRINVALID;
475
476 if (uL != 0x06064b50)
477 return CENTRALDIRINVALID;
478
479 return relativeOffset;
480 }
481
482 /*
483 Open a Zip file. path contain the full pathname (by example,
484 on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer
485 "zlib/zlib114.zip".
486 If the zipfile cannot be opened (file doesn't exist or in not valid), the
487 return value is NULL.
488 Else, the return value is a unzFile Handle, usable with other function
489 of this unzip package.
490 */
491 local unzFile unzOpenInternal(const void *path,
492 zlib_filefunc64_32_def* pzlib_filefunc64_32_def,
493 int is64bitOpenFunction) {
494 unz64_s us;
495 unz64_s *s;
496 ZPOS64_T central_pos;
497 uLong uL;
498
499 uLong number_disk; /* number of the current disk, used for
500 spanning ZIP, unsupported, always 0*/
501 uLong number_disk_with_CD; /* number the disk with central dir, used
502 for spanning ZIP, unsupported, always 0*/
503 ZPOS64_T number_entry_CD; /* total number of entries in
504 the central dir
505 (same than number_entry on nospan) */
506
507 int err=UNZ_OK;
508
509 if (unz_copyright[0]!=' ')
510 return NULL;
511
512 us.z_filefunc.zseek32_file = NULL;
513 us.z_filefunc.ztell32_file = NULL;
514 if (pzlib_filefunc64_32_def==NULL)
515 //fill_fopen64_filefunc(&us.z_filefunc.zfile_func64);
516 return NULL; // standard i/o not supported
517 us.z_filefunc = *pzlib_filefunc64_32_def;
518 us.is64bitOpenFunction = is64bitOpenFunction;
519
520
521
522 us.filestream = ZOPEN64(us.z_filefunc,
523 path,
524 ZLIB_FILEFUNC_MODE_READ |
525 ZLIB_FILEFUNC_MODE_EXISTING);
526 if (us.filestream==NULL)
527 return NULL;
528
529 central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream);
530 if (central_pos!=CENTRALDIRINVALID)
531 {
532 uLong uS;
533 ZPOS64_T uL64;
534
535 us.isZip64 = 1;
536
537 if (ZSEEK64(us.z_filefunc, us.filestream,
538 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
539 err=UNZ_ERRNO;
540
541 /* the signature, already checked */
542 if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
543 err=UNZ_ERRNO;
544
545 /* size of zip64 end of central directory record */
546 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&uL64)!=UNZ_OK)
547 err=UNZ_ERRNO;
548
549 /* version made by */
550 if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK)
551 err=UNZ_ERRNO;
552
553 /* version needed to extract */
554 if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK)
555 err=UNZ_ERRNO;
556
557 /* number of this disk */
558 if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
559 err=UNZ_ERRNO;
560
561 /* number of the disk with the start of the central directory */
562 if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
563 err=UNZ_ERRNO;
564
565 /* total number of entries in the central directory on this disk */
566 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK)
567 err=UNZ_ERRNO;
568
569 /* total number of entries in the central directory */
570 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK)
571 err=UNZ_ERRNO;
572
573 if ((number_entry_CD!=us.gi.number_entry) ||
574 (number_disk_with_CD!=0) ||
575 (number_disk!=0))
576 err=UNZ_BADZIPFILE;
577
578 /* size of the central directory */
579 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK)
580 err=UNZ_ERRNO;
581
582 /* offset of start of central directory with respect to the
583 starting disk number */
584 if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK)
585 err=UNZ_ERRNO;
586
587 us.gi.size_comment = 0;
588 }
589 else
590 {
591 central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream);
592 if (central_pos==CENTRALDIRINVALID)
593 err=UNZ_ERRNO;
594
595 us.isZip64 = 0;
596
597 if (ZSEEK64(us.z_filefunc, us.filestream,
598 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
599 err=UNZ_ERRNO;
600
601 /* the signature, already checked */
602 if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
603 err=UNZ_ERRNO;
604
605 /* number of this disk */
606 if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
607 err=UNZ_ERRNO;
608
609 /* number of the disk with the start of the central directory */
610 if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
611 err=UNZ_ERRNO;
612
613 /* total number of entries in the central dir on this disk */
614 if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
615 err=UNZ_ERRNO;
616 us.gi.number_entry = uL;
617
618 /* total number of entries in the central dir */
619 if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
620 err=UNZ_ERRNO;
621 number_entry_CD = uL;
622
623 if ((number_entry_CD!=us.gi.number_entry) ||
624 (number_disk_with_CD!=0) ||
625 (number_disk!=0))
626 err=UNZ_BADZIPFILE;
627
628 /* size of the central directory */
629 if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
630 err=UNZ_ERRNO;
631 us.size_central_dir = uL;
632
633 /* offset of start of central directory with respect to the
634 starting disk number */
635 if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
636 err=UNZ_ERRNO;
637 us.offset_central_dir = uL;
638
639 /* zipfile comment length */
640 if (unz64local_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK)
641 err=UNZ_ERRNO;
642 }
643
644 if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
645 (err==UNZ_OK))
646 err=UNZ_BADZIPFILE;
647
648 if (err!=UNZ_OK)
649 {
650 ZCLOSE64(us.z_filefunc, us.filestream);
651 return NULL;
652 }
653
654 us.byte_before_the_zipfile = central_pos -
655 (us.offset_central_dir+us.size_central_dir);
656 us.central_pos = central_pos;
657 us.pfile_in_zip_read = NULL;
658 us.encrypted = 0;
659
660
661 s=(unz64_s*)ALLOC(sizeof(unz64_s));
662 if( s != NULL)
663 {
664 *s=us;
665 unzGoToFirstFile((unzFile)s);
666 }
667 return (unzFile)s;
668 }
669
670
671 extern unzFile ZEXPORT unzOpen2(const char *path,
672 zlib_filefunc_def* pzlib_filefunc32_def) {
673 if (pzlib_filefunc32_def != NULL)
674 {
675 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
676 fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
677 return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 0);
678 }
679 else
680 return unzOpenInternal(path, NULL, 0);
681 }
682
683 extern unzFile ZEXPORT unzOpen2_64(const void *path,
684 zlib_filefunc64_def* pzlib_filefunc_def) {
685 if (pzlib_filefunc_def != NULL)
686 {
687 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
688 zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
689 zlib_filefunc64_32_def_fill.zopen32_file = NULL;
690 zlib_filefunc64_32_def_fill.ztell32_file = NULL;
691 zlib_filefunc64_32_def_fill.zseek32_file = NULL;
692 return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 1);
693 }
694 else
695 return unzOpenInternal(path, NULL, 1);
696 }
697
698 extern unzFile ZEXPORT unzOpen(const char *path) {
699 return unzOpenInternal(path, NULL, 0);
700 }
701
702 extern unzFile ZEXPORT unzOpen64(const void *path) {
703 return unzOpenInternal(path, NULL, 1);
704 }
705
706 extern void* unzGetOpaque(unzFile file) {
707 unz64_s* s;
708 if (file==NULL)
709 return NULL;
710 s=(unz64_s*)file;
711
712 return s->z_filefunc.zfile_func64.opaque;
713 }
714
715 /*
716 Close a ZipFile opened with unzOpen.
717 If there is files inside the .Zip opened with unzOpenCurrentFile (see later),
718 these files MUST be closed with unzCloseCurrentFile before call unzClose.
719 return UNZ_OK if there is no problem. */
720 extern int ZEXPORT unzClose(unzFile file) {
721 unz64_s* s;
722 if (file==NULL)
723 return UNZ_PARAMERROR;
724 s=(unz64_s*)file;
725
726 if (s->pfile_in_zip_read!=NULL)
727 unzCloseCurrentFile(file);
728
729 ZCLOSE64(s->z_filefunc, s->filestream);
730 free(s);
731 return UNZ_OK;
732 }
733
734
735 /*
736 Write info about the ZipFile in the *pglobal_info structure.
737 No preparation of the structure is needed
738 return UNZ_OK if there is no problem. */
739 extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64* pglobal_info) {
740 unz64_s* s;
741 if (file==NULL)
742 return UNZ_PARAMERROR;
743 s=(unz64_s*)file;
744 *pglobal_info=s->gi;
745 return UNZ_OK;
746 }
747
748 extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32) {
749 unz64_s* s;
750 if (file==NULL)
751 return UNZ_PARAMERROR;
752 s=(unz64_s*)file;
753 /* to do : check if number_entry is not truncated */
754 pglobal_info32->number_entry = (uLong)s->gi.number_entry;
755 pglobal_info32->size_comment = s->gi.size_comment;
756 return UNZ_OK;
757 }
758 /*
759 Translate date/time from Dos format to tm_unz (readable more easily)
760 */
761 local void unz64local_DosDateToTmuDate(ZPOS64_T ulDosDate, tm_unz* ptm) {
762 ZPOS64_T uDate;
763 uDate = (ZPOS64_T)(ulDosDate>>16);
764 ptm->tm_mday = (int)(uDate&0x1f) ;
765 ptm->tm_mon = (int)((((uDate)&0x1E0)/0x20)-1) ;
766 ptm->tm_year = (int)(((uDate&0x0FE00)/0x0200)+1980) ;
767
768 ptm->tm_hour = (int) ((ulDosDate &0xF800)/0x800);
769 ptm->tm_min = (int) ((ulDosDate&0x7E0)/0x20) ;
770 ptm->tm_sec = (int) (2*(ulDosDate&0x1f)) ;
771 }
772
773 /*
774 Get Info about the current file in the zipfile, with internal only info
775 */
776 local int unz64local_GetCurrentFileInfoInternal(unzFile file,
777 unz_file_info64 *pfile_info,
778 unz_file_info64_internal
779 *pfile_info_internal,
780 char *szFileName,
781 uLong fileNameBufferSize,
782 void *extraField,
783 uLong extraFieldBufferSize,
784 char *szComment,
785 uLong commentBufferSize) {
786 unz64_s* s;
787 unz_file_info64 file_info;
788 unz_file_info64_internal file_info_internal;
789 int err=UNZ_OK;
790 uLong uMagic;
791 long lSeek=0;
792 uLong uL;
793
794 if (file==NULL)
795 return UNZ_PARAMERROR;
796 s=(unz64_s*)file;
797 if (ZSEEK64(s->z_filefunc, s->filestream,
798 s->pos_in_central_dir+s->byte_before_the_zipfile,
799 ZLIB_FILEFUNC_SEEK_SET)!=0)
800 err=UNZ_ERRNO;
801
802
803 /* we check the magic */
804 if (err==UNZ_OK)
805 {
806 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
807 err=UNZ_ERRNO;
808 else if (uMagic!=0x02014b50)
809 err=UNZ_BADZIPFILE;
810 }
811
812 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK)
813 err=UNZ_ERRNO;
814
815 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK)
816 err=UNZ_ERRNO;
817
818 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK)
819 err=UNZ_ERRNO;
820
821 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK)
822 err=UNZ_ERRNO;
823
824 if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK)
825 err=UNZ_ERRNO;
826
827 unz64local_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
828
829 if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK)
830 err=UNZ_ERRNO;
831
832 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
833 err=UNZ_ERRNO;
834 file_info.compressed_size = uL;
835
836 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
837 err=UNZ_ERRNO;
838 file_info.uncompressed_size = uL;
839
840 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK)
841 err=UNZ_ERRNO;
842
843 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK)
844 err=UNZ_ERRNO;
845
846 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK)
847 err=UNZ_ERRNO;
848
849 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK)
850 err=UNZ_ERRNO;
851
852 if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK)
853 err=UNZ_ERRNO;
854
855 if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK)
856 err=UNZ_ERRNO;
857
858 /* relative offset of local header */
859 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
860 err=UNZ_ERRNO;
861 file_info_internal.offset_curfile = uL;
862
863 lSeek+=file_info.size_filename;
864 if ((err==UNZ_OK) && (szFileName!=NULL))
865 {
866 uLong uSizeRead ;
867 if (file_info.size_filename<fileNameBufferSize)
868 {
869 *(szFileName+file_info.size_filename)='\0';
870 uSizeRead = file_info.size_filename;
871 }
872 else
873 uSizeRead = fileNameBufferSize;
874
875 if ((file_info.size_filename>0) && (fileNameBufferSize>0))
876 if (ZREAD64(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead)
877 err=UNZ_ERRNO;
878 lSeek -= uSizeRead;
879 }
880
881 /* Read extrafield */
882 if ((err==UNZ_OK) && (extraField!=NULL))
883 {
884 ZPOS64_T uSizeRead ;
885 if (file_info.size_file_extra<extraFieldBufferSize)
886 uSizeRead = file_info.size_file_extra;
887 else
888 uSizeRead = extraFieldBufferSize;
889
890 if (lSeek!=0)
891 {
892 if (ZSEEK64(s->z_filefunc, s->filestream,(ZPOS64_T)lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
893 lSeek=0;
894 else
895 err=UNZ_ERRNO;
896 }
897
898 if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
899 if (ZREAD64(s->z_filefunc, s->filestream,extraField,(uLong)uSizeRead)!=uSizeRead)
900 err=UNZ_ERRNO;
901
902 lSeek += file_info.size_file_extra - (uLong)uSizeRead;
903 }
904 else
905 lSeek += file_info.size_file_extra;
906
907
908 if ((err==UNZ_OK) && (file_info.size_file_extra != 0))
909 {
910 uLong acc = 0;
911
912 /* since lSeek now points to after the extra field we need to move back */
913 lSeek -= file_info.size_file_extra;
914
915 if (lSeek!=0)
916 {
917 if (lSeek<0) {
918 // WORKAROUND for backwards seeking
919 ZPOS64_T pos = ZTELL64(s->z_filefunc, s->filestream);
920 if (ZSEEK64(s->z_filefunc, s->filestream,pos+(ZPOS64_T)lSeek,ZLIB_FILEFUNC_SEEK_SET)==0)
921 lSeek=0;
922 else
923 err=UNZ_ERRNO;
924 } else {
925 if (ZSEEK64(s->z_filefunc, s->filestream,(ZPOS64_T)lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
926 lSeek=0;
927 else
928 err=UNZ_ERRNO;
929 }
930 }
931
932 while(acc < file_info.size_file_extra)
933 {
934 uLong headerId;
935 uLong dataSize;
936
937 if (unz64local_getShort(&s->z_filefunc, s->filestream,&headerId) != UNZ_OK)
938 err=UNZ_ERRNO;
939
940 if (unz64local_getShort(&s->z_filefunc, s->filestream,&dataSize) != UNZ_OK)
941 err=UNZ_ERRNO;
942
943 /* ZIP64 extra fields */
944 if (headerId == 0x0001)
945 {
946 if(file_info.uncompressed_size == MAXU32)
947 {
948 if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK)
949 err=UNZ_ERRNO;
950 }
951
952 if(file_info.compressed_size == MAXU32)
953 {
954 if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK)
955 err=UNZ_ERRNO;
956 }
957
958 if(file_info_internal.offset_curfile == MAXU32)
959 {
960 /* Relative Header offset */
961 if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK)
962 err=UNZ_ERRNO;
963 }
964
965 if(file_info.disk_num_start == 0xffff)
966 {
967 /* Disk Start Number */
968 if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK)
969 err=UNZ_ERRNO;
970 }
971
972 }
973 else
974 {
975 if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0)
976 err=UNZ_ERRNO;
977 }
978
979 acc += 2 + 2 + dataSize;
980 }
981 }
982
983 if ((err==UNZ_OK) && (szComment!=NULL))
984 {
985 uLong uSizeRead ;
986 if (file_info.size_file_comment<commentBufferSize)
987 {
988 *(szComment+file_info.size_file_comment)='\0';
989 uSizeRead = file_info.size_file_comment;
990 }
991 else
992 uSizeRead = commentBufferSize;
993
994 if (lSeek!=0)
995 {
996 if (ZSEEK64(s->z_filefunc, s->filestream,(ZPOS64_T)lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
997 lSeek=0;
998 else
999 err=UNZ_ERRNO;
1000 }
1001
1002 if ((file_info.size_file_comment>0) && (commentBufferSize>0))
1003 if (ZREAD64(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead)
1004 err=UNZ_ERRNO;
1005 lSeek+=file_info.size_file_comment - uSizeRead;
1006 }
1007 else
1008 lSeek+=file_info.size_file_comment;
1009
1010
1011 if ((err==UNZ_OK) && (pfile_info!=NULL))
1012 *pfile_info=file_info;
1013
1014 if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
1015 *pfile_info_internal=file_info_internal;
1016
1017 return err;
1018 }
1019
1020
1021
1022 /*
1023 Write info about the ZipFile in the *pglobal_info structure.
1024 No preparation of the structure is needed
1025 return UNZ_OK if there is no problem.
1026 */
1027 extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file,
1028 unz_file_info64 * pfile_info,
1029 char * szFileName, uLong fileNameBufferSize,
1030 void *extraField, uLong extraFieldBufferSize,
1031 char* szComment, uLong commentBufferSize) {
1032 return unz64local_GetCurrentFileInfoInternal(file,pfile_info,NULL,
1033 szFileName,fileNameBufferSize,
1034 extraField,extraFieldBufferSize,
1035 szComment,commentBufferSize);
1036 }
1037
1038 extern int ZEXPORT unzGetCurrentFileInfo(unzFile file,
1039 unz_file_info * pfile_info,
1040 char * szFileName, uLong fileNameBufferSize,
1041 void *extraField, uLong extraFieldBufferSize,
1042 char* szComment, uLong commentBufferSize) {
1043 int err;
1044 unz_file_info64 file_info64;
1045 err = unz64local_GetCurrentFileInfoInternal(file,&file_info64,NULL,
1046 szFileName,fileNameBufferSize,
1047 extraField,extraFieldBufferSize,
1048 szComment,commentBufferSize);
1049 if ((err==UNZ_OK) && (pfile_info != NULL))
1050 {
1051 pfile_info->version = file_info64.version;
1052 pfile_info->version_needed = file_info64.version_needed;
1053 pfile_info->flag = file_info64.flag;
1054 pfile_info->compression_method = file_info64.compression_method;
1055 pfile_info->dosDate = file_info64.dosDate;
1056 pfile_info->crc = file_info64.crc;
1057
1058 pfile_info->size_filename = file_info64.size_filename;
1059 pfile_info->size_file_extra = file_info64.size_file_extra;
1060 pfile_info->size_file_comment = file_info64.size_file_comment;
1061
1062 pfile_info->disk_num_start = file_info64.disk_num_start;
1063 pfile_info->internal_fa = file_info64.internal_fa;
1064 pfile_info->external_fa = file_info64.external_fa;
1065
1066 pfile_info->tmu_date = file_info64.tmu_date;
1067
1068
1069 pfile_info->compressed_size = (uLong)file_info64.compressed_size;
1070 pfile_info->uncompressed_size = (uLong)file_info64.uncompressed_size;
1071
1072 }
1073 return err;
1074 }
1075 /*
1076 Set the current file of the zipfile to the first file.
1077 return UNZ_OK if there is no problem
1078 */
1079 extern int ZEXPORT unzGoToFirstFile(unzFile file) {
1080 int err=UNZ_OK;
1081 unz64_s* s;
1082 if (file==NULL)
1083 return UNZ_PARAMERROR;
1084 s=(unz64_s*)file;
1085 s->pos_in_central_dir=s->offset_central_dir;
1086 s->num_file=0;
1087 err=unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
1088 &s->cur_file_info_internal,
1089 NULL,0,NULL,0,NULL,0);
1090 s->current_file_ok = (err == UNZ_OK);
1091 return err;
1092 }
1093
1094 /*
1095 Set the current file of the zipfile to the next file.
1096 return UNZ_OK if there is no problem
1097 return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
1098 */
1099 extern int ZEXPORT unzGoToNextFile(unzFile file) {
1100 unz64_s* s;
1101 int err;
1102
1103 if (file==NULL)
1104 return UNZ_PARAMERROR;
1105 s=(unz64_s*)file;
1106 if (!s->current_file_ok)
1107 return UNZ_END_OF_LIST_OF_FILE;
1108 if (s->gi.number_entry != 0xffff) /* 2^16 files overflow hack */
1109 if (s->num_file+1==s->gi.number_entry)
1110 return UNZ_END_OF_LIST_OF_FILE;
1111
1112 s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
1113 s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
1114 s->num_file++;
1115 err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
1116 &s->cur_file_info_internal,
1117 NULL,0,NULL,0,NULL,0);
1118 s->current_file_ok = (err == UNZ_OK);
1119 return err;
1120 }
1121
1122
1123 /*
1124 Try locate the file szFileName in the zipfile.
1125 For the iCaseSensitivity signification, see unzStringFileNameCompare
1126
1127 return value :
1128 UNZ_OK if the file is found. It becomes the current file.
1129 UNZ_END_OF_LIST_OF_FILE if the file is not found
1130 */
1131 extern int ZEXPORT unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity) {
1132 unz64_s* s;
1133 int err;
1134
1135 /* We remember the 'current' position in the file so that we can jump
1136 * back there if we fail.
1137 */
1138 unz_file_info64 cur_file_infoSaved;
1139 unz_file_info64_internal cur_file_info_internalSaved;
1140 ZPOS64_T num_fileSaved;
1141 ZPOS64_T pos_in_central_dirSaved;
1142
1143
1144 if (file==NULL)
1145 return UNZ_PARAMERROR;
1146
1147 if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
1148 return UNZ_PARAMERROR;
1149
1150 s=(unz64_s*)file;
1151 if (!s->current_file_ok)
1152 return UNZ_END_OF_LIST_OF_FILE;
1153
1154 /* Save the current state */
1155 num_fileSaved = s->num_file;
1156 pos_in_central_dirSaved = s->pos_in_central_dir;
1157 cur_file_infoSaved = s->cur_file_info;
1158 cur_file_info_internalSaved = s->cur_file_info_internal;
1159
1160 err = unzGoToFirstFile(file);
1161
1162 while (err == UNZ_OK)
1163 {
1164 char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
1165 err = unzGetCurrentFileInfo64(file,NULL,
1166 szCurrentFileName,sizeof(szCurrentFileName)-1,
1167 NULL,0,NULL,0);
1168 if (err == UNZ_OK)
1169 {
1170 if (unzStringFileNameCompare(szCurrentFileName,
1171 szFileName,iCaseSensitivity)==0)
1172 return UNZ_OK;
1173 err = unzGoToNextFile(file);
1174 }
1175 }
1176
1177 /* We failed, so restore the state of the 'current file' to where we
1178 * were.
1179 */
1180 s->num_file = num_fileSaved ;
1181 s->pos_in_central_dir = pos_in_central_dirSaved ;
1182 s->cur_file_info = cur_file_infoSaved;
1183 s->cur_file_info_internal = cur_file_info_internalSaved;
1184 return err;
1185 }
1186
1187
1188 /*
1189 ///////////////////////////////////////////
1190 // Contributed by Ryan Haksi (mailto://[email protected])
1191 // I need random access
1192 //
1193 // Further optimization could be realized by adding an ability
1194 // to cache the directory in memory. The goal being a single
1195 // comprehensive file read to put the file I need in a memory.
1196 */
1197
1198 /*
1199 typedef struct unz_file_pos_s
1200 {
1201 ZPOS64_T pos_in_zip_directory; // offset in file
1202 ZPOS64_T num_of_file; // # of file
1203 } unz_file_pos;
1204 */
1205
1206 extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos* file_pos) {
1207 unz64_s* s;
1208
1209 if (file==NULL || file_pos==NULL)
1210 return UNZ_PARAMERROR;
1211 s=(unz64_s*)file;
1212 if (!s->current_file_ok)
1213 return UNZ_END_OF_LIST_OF_FILE;
1214
1215 file_pos->pos_in_zip_directory = s->pos_in_central_dir;
1216 file_pos->num_of_file = s->num_file;
1217
1218 return UNZ_OK;
1219 }
1220
1221 extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos* file_pos) {
1222 unz64_file_pos file_pos64;
1223 int err = unzGetFilePos64(file,&file_pos64);
1224 if (err==UNZ_OK)
1225 {
1226 file_pos->pos_in_zip_directory = (uLong)file_pos64.pos_in_zip_directory;
1227 file_pos->num_of_file = (uLong)file_pos64.num_of_file;
1228 }
1229 return err;
1230 }
1231
1232 extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos* file_pos) {
1233 unz64_s* s;
1234 int err;
1235
1236 if (file==NULL || file_pos==NULL)
1237 return UNZ_PARAMERROR;
1238 s=(unz64_s*)file;
1239
1240 /* jump to the right spot */
1241 s->pos_in_central_dir = file_pos->pos_in_zip_directory;
1242 s->num_file = file_pos->num_of_file;
1243
1244 /* set the current file */
1245 err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
1246 &s->cur_file_info_internal,
1247 NULL,0,NULL,0,NULL,0);
1248 /* return results */
1249 s->current_file_ok = (err == UNZ_OK);
1250 return err;
1251 }
1252
1253 extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos* file_pos) {
1254 unz64_file_pos file_pos64;
1255 if (file_pos == NULL)
1256 return UNZ_PARAMERROR;
1257
1258 file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
1259 file_pos64.num_of_file = file_pos->num_of_file;
1260 return unzGoToFilePos64(file,&file_pos64);
1261 }
1262
1263 /*
1264 // Unzip Helper Functions - should be here?
1265 ///////////////////////////////////////////
1266 */
1267
1268 /*
1269 Read the local header of the current zipfile
1270 Check the coherency of the local header and info in the end of central
1271 directory about this file
1272 store in *piSizeVar the size of extra info in local header
1273 (filename and size of extra field data)
1274 */
1275 local int unz64local_CheckCurrentFileCoherencyHeader(unz64_s* s, uInt* piSizeVar,
1276 ZPOS64_T * poffset_local_extrafield,
1277 uInt * psize_local_extrafield) {
1278 uLong uMagic,uData,uFlags;
1279 uLong size_filename;
1280 uLong size_extra_field;
1281 int err=UNZ_OK;
1282
1283 *piSizeVar = 0;
1284 *poffset_local_extrafield = 0;
1285 *psize_local_extrafield = 0;
1286
1287 if (ZSEEK64(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile +
1288 s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
1289 return UNZ_ERRNO;
1290
1291
1292 if (err==UNZ_OK)
1293 {
1294 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
1295 err=UNZ_ERRNO;
1296 else if (uMagic!=0x04034b50)
1297 err=UNZ_BADZIPFILE;
1298 }
1299
1300 if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
1301 err=UNZ_ERRNO;
1302 /*
1303 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
1304 err=UNZ_BADZIPFILE;
1305 */
1306 if (unz64local_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK)
1307 err=UNZ_ERRNO;
1308
1309 if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
1310 err=UNZ_ERRNO;
1311 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
1312 err=UNZ_BADZIPFILE;
1313
1314 if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
1315 /* #ifdef HAVE_BZIP2 */
1316 (s->cur_file_info.compression_method!=Z_BZIP2ED) &&
1317 /* #endif */
1318 (s->cur_file_info.compression_method!=Z_DEFLATED))
1319 err=UNZ_BADZIPFILE;
1320
1321 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */
1322 err=UNZ_ERRNO;
1323
1324 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */
1325 err=UNZ_ERRNO;
1326 else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && ((uFlags & 8)==0))
1327 err=UNZ_BADZIPFILE;
1328
1329 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */
1330 err=UNZ_ERRNO;
1331 else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && ((uFlags & 8)==0))
1332 err=UNZ_BADZIPFILE;
1333
1334 if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */
1335 err=UNZ_ERRNO;
1336 else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && ((uFlags & 8)==0))
1337 err=UNZ_BADZIPFILE;
1338
1339 if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK)
1340 err=UNZ_ERRNO;
1341 else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
1342 err=UNZ_BADZIPFILE;
1343
1344 *piSizeVar += (uInt)size_filename;
1345
1346 if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK)
1347 err=UNZ_ERRNO;
1348 *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
1349 SIZEZIPLOCALHEADER + size_filename;
1350 *psize_local_extrafield = (uInt)size_extra_field;
1351
1352 *piSizeVar += (uInt)size_extra_field;
1353
1354 return err;
1355 }
1356
1357 /*
1358 Open for reading data the current file in the zipfile.
1359 If there is no error and the file is opened, the return value is UNZ_OK.
1360 */
1361 extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int* method,
1362 int* level, int raw, const char* password) {
1363 int err=UNZ_OK;
1364 uInt iSizeVar;
1365 unz64_s* s;
1366 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1367 ZPOS64_T offset_local_extrafield; /* offset of the local extra field */
1368 uInt size_local_extrafield; /* size of the local extra field */
1369 # ifndef NOUNCRYPT
1370 char source[12];
1371 # else
1372 if (password != NULL)
1373 return UNZ_PARAMERROR;
1374 # endif
1375
1376 if (file==NULL)
1377 return UNZ_PARAMERROR;
1378 s=(unz64_s*)file;
1379 if (!s->current_file_ok)
1380 return UNZ_PARAMERROR;
1381
1382 if (s->pfile_in_zip_read != NULL)
1383 unzCloseCurrentFile(file);
1384
1385 if (unz64local_CheckCurrentFileCoherencyHeader(s,&iSizeVar, &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
1386 return UNZ_BADZIPFILE;
1387
1388 pfile_in_zip_read_info = (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s));
1389 if (pfile_in_zip_read_info==NULL)
1390 return UNZ_INTERNALERROR;
1391
1392 pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE);
1393 pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
1394 pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
1395 pfile_in_zip_read_info->pos_local_extrafield=0;
1396 pfile_in_zip_read_info->raw=raw;
1397
1398 if (pfile_in_zip_read_info->read_buffer==NULL)
1399 {
1400 free(pfile_in_zip_read_info);
1401 return UNZ_INTERNALERROR;
1402 }
1403
1404 pfile_in_zip_read_info->stream_initialised=0;
1405
1406 if (method!=NULL)
1407 *method = (int)s->cur_file_info.compression_method;
1408
1409 if (level!=NULL)
1410 {
1411 *level = 6;
1412 switch (s->cur_file_info.flag & 0x06)
1413 {
1414 case 6 : *level = 1; break;
1415 case 4 : *level = 2; break;
1416 case 2 : *level = 9; break;
1417 }
1418 }
1419
1420 if ((s->cur_file_info.compression_method!=0) &&
1421 /* #ifdef HAVE_BZIP2 */
1422 (s->cur_file_info.compression_method!=Z_BZIP2ED) &&
1423 /* #endif */
1424 (s->cur_file_info.compression_method!=Z_DEFLATED))
1425
1426 err=UNZ_BADZIPFILE;
1427
1428 pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
1429 pfile_in_zip_read_info->crc32=0;
1430 pfile_in_zip_read_info->total_out_64=0;
1431 pfile_in_zip_read_info->compression_method = s->cur_file_info.compression_method;
1432 pfile_in_zip_read_info->filestream=s->filestream;
1433 pfile_in_zip_read_info->z_filefunc=s->z_filefunc;
1434 pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
1435
1436 pfile_in_zip_read_info->stream.total_out = 0;
1437
1438 if ((s->cur_file_info.compression_method==Z_BZIP2ED) && (!raw))
1439 {
1440 #ifdef HAVE_BZIP2
1441 pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0;
1442 pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
1443 pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
1444 pfile_in_zip_read_info->bstream.state = (voidpf)0;
1445
1446 pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
1447 pfile_in_zip_read_info->stream.zfree = (free_func)0;
1448 pfile_in_zip_read_info->stream.opaque = (voidpf)0;
1449 pfile_in_zip_read_info->stream.next_in = (voidpf)0;
1450 pfile_in_zip_read_info->stream.avail_in = 0;
1451
1452 err=BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
1453 if (err == Z_OK)
1454 pfile_in_zip_read_info->stream_initialised=Z_BZIP2ED;
1455 else
1456 {
1457 free(pfile_in_zip_read_info->read_buffer);
1458 free(pfile_in_zip_read_info);
1459 return err;
1460 }
1461 #else
1462 pfile_in_zip_read_info->raw=1;
1463 #endif
1464 }
1465 else if ((s->cur_file_info.compression_method==Z_DEFLATED) && (!raw))
1466 {
1467 pfile_in_zip_read_info->stream.zalloc = s->z_filefunc.zfile_func64.alloc_mem;
1468 pfile_in_zip_read_info->stream.zfree = s->z_filefunc.zfile_func64.free_mem;
1469 pfile_in_zip_read_info->stream.opaque = (voidpf)0;
1470 pfile_in_zip_read_info->stream.next_in = 0;
1471 pfile_in_zip_read_info->stream.avail_in = 0;
1472
1473 err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
1474 if (err == Z_OK)
1475 pfile_in_zip_read_info->stream_initialised=Z_DEFLATED;
1476 else
1477 {
1478 free(pfile_in_zip_read_info->read_buffer);
1479 free(pfile_in_zip_read_info);
1480 return err;
1481 }
1482 /* windowBits is passed < 0 to tell that there is no zlib header.
1483 * Note that in this case inflate *requires* an extra "dummy" byte
1484 * after the compressed stream in order to complete decompression and
1485 * return Z_STREAM_END.
1486 * In unzip, i don't wait absolutely Z_STREAM_END because I known the
1487 * size of both compressed and uncompressed data
1488 */
1489 }
1490 pfile_in_zip_read_info->rest_read_compressed =
1491 s->cur_file_info.compressed_size ;
1492 pfile_in_zip_read_info->rest_read_uncompressed =
1493 s->cur_file_info.uncompressed_size ;
1494
1495
1496 pfile_in_zip_read_info->pos_in_zipfile =
1497 s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
1498 iSizeVar;
1499
1500 pfile_in_zip_read_info->stream.avail_in = (uInt)0;
1501 pfile_in_zip_read_info->extra_size = iSizeVar;
1502
1503 s->pfile_in_zip_read = pfile_in_zip_read_info;
1504 s->encrypted = 0;
1505
1506 # ifndef NOUNCRYPT
1507 if (password != NULL)
1508 {
1509 int i;
1510 s->pcrc_32_tab = get_crc_table();
1511 init_keys(password,s->keys,s->pcrc_32_tab);
1512 if (ZSEEK64(s->z_filefunc, s->filestream,
1513 s->pfile_in_zip_read->pos_in_zipfile +
1514 s->pfile_in_zip_read->byte_before_the_zipfile,
1515 SEEK_SET)!=0)
1516 return UNZ_INTERNALERROR;
1517 if(ZREAD64(s->z_filefunc, s->filestream,source, 12)<12)
1518 return UNZ_INTERNALERROR;
1519
1520 for (i = 0; i<12; i++)
1521 zdecode(s->keys,s->pcrc_32_tab,source[i]);
1522
1523 s->pfile_in_zip_read->pos_in_zipfile+=12;
1524 s->encrypted=1;
1525 }
1526 # endif
1527
1528
1529 return UNZ_OK;
1530 }
1531
1532 extern int ZEXPORT unzSeekCurrentFile(unzFile file, int pos) {
1533 unz64_s* s;
1534 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1535 if (file==NULL)
1536 return UNZ_PARAMERROR;
1537 s=(unz64_s*)file;
1538 pfile_in_zip_read_info=s->pfile_in_zip_read;
1539
1540 if (pfile_in_zip_read_info==NULL)
1541 return UNZ_PARAMERROR;
1542
1543 if (pfile_in_zip_read_info->compression_method==Z_BZIP2ED) { // don't know how to support bzip
1544 return UNZ_INTERNALERROR;
1545 }
1546
1547 if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw)) {
1548
1549 pfile_in_zip_read_info->rest_read_compressed =
1550 s->cur_file_info.compressed_size - pos;
1551 pfile_in_zip_read_info->rest_read_uncompressed =
1552 s->cur_file_info.uncompressed_size - pos;
1553
1554 pfile_in_zip_read_info->pos_in_zipfile =
1555 s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
1556 pfile_in_zip_read_info->extra_size + pos;
1557
1558 pfile_in_zip_read_info->stream.avail_in = (uInt)0;
1559 pfile_in_zip_read_info->stream.total_out = pos;
1560 pfile_in_zip_read_info->total_out_64 = pos;
1561
1562 return ZSEEK64(pfile_in_zip_read_info->z_filefunc,
1563 pfile_in_zip_read_info->filestream,
1564 pfile_in_zip_read_info->byte_before_the_zipfile + pfile_in_zip_read_info->pos_in_zipfile,
1565 ZLIB_FILEFUNC_SEEK_SET);
1566
1567 } else { // gzip
1568
1569 if (pos < pfile_in_zip_read_info->stream.total_out) { // negative seek, rewind
1570
1571 pfile_in_zip_read_info->rest_read_compressed =
1572 s->cur_file_info.compressed_size ;
1573 pfile_in_zip_read_info->rest_read_uncompressed =
1574 s->cur_file_info.uncompressed_size ;
1575
1576 pfile_in_zip_read_info->pos_in_zipfile =
1577 s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
1578 pfile_in_zip_read_info->extra_size;
1579
1580 (void)inflateReset(&pfile_in_zip_read_info->stream);
1581
1582 pfile_in_zip_read_info->stream.avail_in = (uInt)0;
1583 pfile_in_zip_read_info->stream.total_out = 0;
1584 pfile_in_zip_read_info->total_out_64 = 0;
1585 pfile_in_zip_read_info->stream.next_in = 0;
1586 }
1587
1588 // not sure where to read, so read on the stack
1589 {
1590 char buf[512];
1591 int to_read = pos - pfile_in_zip_read_info->stream.total_out;
1592 while (to_read) {
1593
1594 int len = to_read > sizeof(buf)?sizeof(buf):to_read;
1595 int read = unzReadCurrentFile(file, buf, len);
1596 if (read < 0) {
1597 return read;
1598 }
1599 to_read -= read;
1600 if (read == UNZ_EOF) {
1601 return pos;
1602 }
1603 }
1604 }
1605 }
1606
1607 return pos;
1608 }
1609
1610 extern int ZEXPORT unzOpenCurrentFile(unzFile file) {
1611 return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
1612 }
1613
1614 extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char* password) {
1615 return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
1616 }
1617
1618 extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int* method, int* level, int raw) {
1619 return unzOpenCurrentFile3(file, method, level, raw, NULL);
1620 }
1621
1622 /** Addition for GDAL : START */
1623
1624 extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64(unzFile file) {
1625 unz64_s* s;
1626 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1627 s=(unz64_s*)file;
1628 if (file==NULL)
1629 return 0; /* UNZ_PARAMERROR; */
1630 pfile_in_zip_read_info=s->pfile_in_zip_read;
1631 if (pfile_in_zip_read_info==NULL)
1632 return 0; /* UNZ_PARAMERROR; */
1633 return pfile_in_zip_read_info->pos_in_zipfile +
1634 pfile_in_zip_read_info->byte_before_the_zipfile;
1635 }
1636
1637 /** Addition for GDAL : END */
1638
1639 /*
1640 Read bytes from the current file.
1641 buf contain buffer where data must be copied
1642 len the size of buf.
1643
1644 return the number of byte copied if some bytes are copied
1645 return 0 if the end of file was reached
1646 return <0 with error code if there is an error
1647 (UNZ_ERRNO for IO error, or zLib error for uncompress error)
1648 */
1649 extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len) {
1650 int err=UNZ_OK;
1651 uInt iRead = 0;
1652 unz64_s* s;
1653 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1654 if (file==NULL)
1655 return UNZ_PARAMERROR;
1656 s=(unz64_s*)file;
1657 pfile_in_zip_read_info=s->pfile_in_zip_read;
1658
1659 if (pfile_in_zip_read_info==NULL)
1660 return UNZ_PARAMERROR;
1661
1662
1663 if (pfile_in_zip_read_info->read_buffer == NULL)
1664 return UNZ_END_OF_LIST_OF_FILE;
1665 if (len==0)
1666 return 0;
1667
1668 pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
1669
1670 pfile_in_zip_read_info->stream.avail_out = (uInt)len;
1671
1672 if ((len>pfile_in_zip_read_info->rest_read_uncompressed) &&
1673 (!(pfile_in_zip_read_info->raw)))
1674 pfile_in_zip_read_info->stream.avail_out =
1675 (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
1676
1677 if ((len>pfile_in_zip_read_info->rest_read_compressed+
1678 pfile_in_zip_read_info->stream.avail_in) &&
1679 (pfile_in_zip_read_info->raw))
1680 pfile_in_zip_read_info->stream.avail_out =
1681 (uInt)pfile_in_zip_read_info->rest_read_compressed+
1682 pfile_in_zip_read_info->stream.avail_in;
1683
1684 while (pfile_in_zip_read_info->stream.avail_out>0)
1685 {
1686 if ((pfile_in_zip_read_info->stream.avail_in==0) &&
1687 (pfile_in_zip_read_info->rest_read_compressed>0))
1688 {
1689 uInt uReadThis = UNZ_BUFSIZE;
1690 if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
1691 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
1692 if (uReadThis == 0)
1693 return UNZ_EOF;
1694 if (ZSEEK64(pfile_in_zip_read_info->z_filefunc,
1695 pfile_in_zip_read_info->filestream,
1696 pfile_in_zip_read_info->pos_in_zipfile +
1697 pfile_in_zip_read_info->byte_before_the_zipfile,
1698 ZLIB_FILEFUNC_SEEK_SET)!=0)
1699 return UNZ_ERRNO;
1700 if (ZREAD64(pfile_in_zip_read_info->z_filefunc,
1701 pfile_in_zip_read_info->filestream,
1702 pfile_in_zip_read_info->read_buffer,
1703 uReadThis)!=uReadThis)
1704 return UNZ_ERRNO;
1705
1706
1707 # ifndef NOUNCRYPT
1708 if(s->encrypted)
1709 {
1710 uInt i;
1711 for(i=0;i<uReadThis;i++)
1712 pfile_in_zip_read_info->read_buffer[i] =
1713 (char)zdecode(s->keys,s->pcrc_32_tab,
1714 pfile_in_zip_read_info->read_buffer[i]);
1715 }
1716 # endif
1717
1718
1719 pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
1720
1721 pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
1722
1723 pfile_in_zip_read_info->stream.next_in =
1724 (Bytef*)pfile_in_zip_read_info->read_buffer;
1725 pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
1726 }
1727
1728 if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw))
1729 {
1730 uInt uDoCopy,i ;
1731
1732 if ((pfile_in_zip_read_info->stream.avail_in == 0) &&
1733 (pfile_in_zip_read_info->rest_read_compressed == 0))
1734 return (iRead==0) ? UNZ_EOF : (int)iRead;
1735
1736 if (pfile_in_zip_read_info->stream.avail_out <
1737 pfile_in_zip_read_info->stream.avail_in)
1738 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
1739 else
1740 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
1741
1742 for (i=0;i<uDoCopy;i++)
1743 *(pfile_in_zip_read_info->stream.next_out+i) =
1744 *(pfile_in_zip_read_info->stream.next_in+i);
1745
1746 pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uDoCopy;
1747
1748 pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
1749 pfile_in_zip_read_info->stream.next_out,
1750 uDoCopy);
1751 pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
1752 pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
1753 pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
1754 pfile_in_zip_read_info->stream.next_out += uDoCopy;
1755 pfile_in_zip_read_info->stream.next_in += uDoCopy;
1756 pfile_in_zip_read_info->stream.total_out += uDoCopy;
1757 iRead += uDoCopy;
1758 }
1759 else if (pfile_in_zip_read_info->compression_method==Z_BZIP2ED)
1760 {
1761 #ifdef HAVE_BZIP2
1762 uLong uTotalOutBefore,uTotalOutAfter;
1763 const Bytef *bufBefore;
1764 uLong uOutThis;
1765
1766 pfile_in_zip_read_info->bstream.next_in = (char*)pfile_in_zip_read_info->stream.next_in;
1767 pfile_in_zip_read_info->bstream.avail_in = pfile_in_zip_read_info->stream.avail_in;
1768 pfile_in_zip_read_info->bstream.total_in_lo32 = pfile_in_zip_read_info->stream.total_in;
1769 pfile_in_zip_read_info->bstream.total_in_hi32 = 0;
1770 pfile_in_zip_read_info->bstream.next_out = (char*)pfile_in_zip_read_info->stream.next_out;
1771 pfile_in_zip_read_info->bstream.avail_out = pfile_in_zip_read_info->stream.avail_out;
1772 pfile_in_zip_read_info->bstream.total_out_lo32 = pfile_in_zip_read_info->stream.total_out;
1773 pfile_in_zip_read_info->bstream.total_out_hi32 = 0;
1774
1775 uTotalOutBefore = pfile_in_zip_read_info->bstream.total_out_lo32;
1776 bufBefore = (const Bytef *)pfile_in_zip_read_info->bstream.next_out;
1777
1778 err=BZ2_bzDecompress(&pfile_in_zip_read_info->bstream);
1779
1780 uTotalOutAfter = pfile_in_zip_read_info->bstream.total_out_lo32;
1781 uOutThis = uTotalOutAfter-uTotalOutBefore;
1782
1783 pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
1784
1785 pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,bufBefore, (uInt)(uOutThis));
1786 pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis;
1787 iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
1788
1789 pfile_in_zip_read_info->stream.next_in = (Bytef*)pfile_in_zip_read_info->bstream.next_in;
1790 pfile_in_zip_read_info->stream.avail_in = pfile_in_zip_read_info->bstream.avail_in;
1791 pfile_in_zip_read_info->stream.total_in = pfile_in_zip_read_info->bstream.total_in_lo32;
1792 pfile_in_zip_read_info->stream.next_out = (Bytef*)pfile_in_zip_read_info->bstream.next_out;
1793 pfile_in_zip_read_info->stream.avail_out = pfile_in_zip_read_info->bstream.avail_out;
1794 pfile_in_zip_read_info->stream.total_out = pfile_in_zip_read_info->bstream.total_out_lo32;
1795
1796 if (err==BZ_STREAM_END)
1797 return (iRead==0) ? UNZ_EOF : iRead;
1798 if (err!=BZ_OK)
1799 break;
1800 #endif
1801 } /* end Z_BZIP2ED */
1802 else
1803 {
1804 ZPOS64_T uTotalOutBefore,uTotalOutAfter;
1805 const Bytef *bufBefore;
1806 ZPOS64_T uOutThis;
1807 int flush=Z_SYNC_FLUSH;
1808
1809 uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
1810 bufBefore = pfile_in_zip_read_info->stream.next_out;
1811
1812 /*
1813 if ((pfile_in_zip_read_info->rest_read_uncompressed ==
1814 pfile_in_zip_read_info->stream.avail_out) &&
1815 (pfile_in_zip_read_info->rest_read_compressed == 0))
1816 flush = Z_FINISH;
1817 */
1818 err=inflate(&pfile_in_zip_read_info->stream,flush);
1819
1820 if ((err>=0) && (pfile_in_zip_read_info->stream.msg!=NULL))
1821 err = Z_DATA_ERROR;
1822
1823 uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
1824 /* Detect overflow, because z_stream.total_out is uLong (32 bits) */
1825 if (uTotalOutAfter<uTotalOutBefore)
1826 uTotalOutAfter += 1LL << 32; /* Add maximum value of uLong + 1 */
1827 uOutThis = uTotalOutAfter-uTotalOutBefore;
1828
1829 pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
1830
1831 pfile_in_zip_read_info->crc32 =
1832 crc32(pfile_in_zip_read_info->crc32,bufBefore,
1833 (uInt)(uOutThis));
1834
1835 pfile_in_zip_read_info->rest_read_uncompressed -=
1836 uOutThis;
1837
1838 iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
1839
1840 if (err==Z_STREAM_END)
1841 return (iRead==0) ? UNZ_EOF : (int)iRead;
1842 if (err!=Z_OK)
1843 break;
1844 }
1845 }
1846
1847 if (err==Z_OK)
1848 return (int)iRead;
1849 return err;
1850 }
1851
1852
1853 /*
1854 Give the current position in uncompressed data
1855 */
1856 extern z_off_t ZEXPORT unztell(unzFile file) {
1857 unz64_s* s;
1858 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1859 if (file==NULL)
1860 return UNZ_PARAMERROR;
1861 s=(unz64_s*)file;
1862 pfile_in_zip_read_info=s->pfile_in_zip_read;
1863
1864 if (pfile_in_zip_read_info==NULL)
1865 return UNZ_PARAMERROR;
1866
1867 return (z_off_t)pfile_in_zip_read_info->stream.total_out;
1868 }
1869
1870 extern ZPOS64_T ZEXPORT unztell64(unzFile file) {
1871
1872 unz64_s* s;
1873 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1874 if (file==NULL)
1875 return (ZPOS64_T)-1;
1876 s=(unz64_s*)file;
1877 pfile_in_zip_read_info=s->pfile_in_zip_read;
1878
1879 if (pfile_in_zip_read_info==NULL)
1880 return (ZPOS64_T)-1;
1881
1882 return pfile_in_zip_read_info->total_out_64;
1883 }
1884
1885
1886 /*
1887 return 1 if the end of file was reached, 0 elsewhere
1888 */
1889 extern int ZEXPORT unzeof(unzFile file) {
1890 unz64_s* s;
1891 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1892 if (file==NULL)
1893 return UNZ_PARAMERROR;
1894 s=(unz64_s*)file;
1895 pfile_in_zip_read_info=s->pfile_in_zip_read;
1896
1897 if (pfile_in_zip_read_info==NULL)
1898 return UNZ_PARAMERROR;
1899
1900 if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
1901 return 1;
1902 else
1903 return 0;
1904 }
1905
1906
1907
1908 /*
1909 Read extra field from the current file (opened by unzOpenCurrentFile)
1910 This is the local-header version of the extra field (sometimes, there is
1911 more info in the local-header version than in the central-header)
1912
1913 if buf==NULL, it return the size of the local extra field that can be read
1914
1915 if buf!=NULL, len is the size of the buffer, the extra header is copied in
1916 buf.
1917 the return value is the number of bytes copied in buf, or (if <0)
1918 the error code
1919 */
1920 extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len) {
1921 unz64_s* s;
1922 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1923 uInt read_now;
1924 ZPOS64_T size_to_read;
1925
1926 if (file==NULL)
1927 return UNZ_PARAMERROR;
1928 s=(unz64_s*)file;
1929 pfile_in_zip_read_info=s->pfile_in_zip_read;
1930
1931 if (pfile_in_zip_read_info==NULL)
1932 return UNZ_PARAMERROR;
1933
1934 size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
1935 pfile_in_zip_read_info->pos_local_extrafield);
1936
1937 if (buf==NULL)
1938 return (int)size_to_read;
1939
1940 if (len>size_to_read)
1941 read_now = (uInt)size_to_read;
1942 else
1943 read_now = (uInt)len ;
1944
1945 if (read_now==0)
1946 return 0;
1947
1948 if (ZSEEK64(pfile_in_zip_read_info->z_filefunc,
1949 pfile_in_zip_read_info->filestream,
1950 pfile_in_zip_read_info->offset_local_extrafield +
1951 pfile_in_zip_read_info->pos_local_extrafield,
1952 ZLIB_FILEFUNC_SEEK_SET)!=0)
1953 return UNZ_ERRNO;
1954
1955 if (ZREAD64(pfile_in_zip_read_info->z_filefunc,
1956 pfile_in_zip_read_info->filestream,
1957 buf,read_now)!=read_now)
1958 return UNZ_ERRNO;
1959
1960 return (int)read_now;
1961 }
1962
1963 /*
1964 Close the file in zip opened with unzOpenCurrentFile
1965 Return UNZ_CRCERROR if all the file was read but the CRC is not good
1966 */
1967 extern int ZEXPORT unzCloseCurrentFile(unzFile file) {
1968 int err=UNZ_OK;
1969
1970 unz64_s* s;
1971 file_in_zip64_read_info_s* pfile_in_zip_read_info;
1972 if (file==NULL)
1973 return UNZ_PARAMERROR;
1974 s=(unz64_s*)file;
1975 pfile_in_zip_read_info=s->pfile_in_zip_read;
1976
1977 if (pfile_in_zip_read_info==NULL)
1978 return UNZ_PARAMERROR;
1979
1980
1981 if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) &&
1982 (!pfile_in_zip_read_info->raw))
1983 {
1984 if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
1985 err=UNZ_CRCERROR;
1986 }
1987
1988
1989 free(pfile_in_zip_read_info->read_buffer);
1990 pfile_in_zip_read_info->read_buffer = NULL;
1991 if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
1992 inflateEnd(&pfile_in_zip_read_info->stream);
1993 #ifdef HAVE_BZIP2
1994 else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
1995 BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
1996 #endif
1997
1998
1999 pfile_in_zip_read_info->stream_initialised = 0;
2000 free(pfile_in_zip_read_info);
2001
2002 s->pfile_in_zip_read=NULL;
2003
2004 return err;
2005 }
2006
2007
2008 /*
2009 Get the global comment string of the ZipFile, in the szComment buffer.
2010 uSizeBuf is the size of the szComment buffer.
2011 return the number of byte copied or an error code <0
2012 */
2013 extern int ZEXPORT unzGetGlobalComment(unzFile file, char * szComment, uLong uSizeBuf) {
2014 unz64_s* s;
2015 uLong uReadThis ;
2016 if (file==NULL)
2017 return (int)UNZ_PARAMERROR;
2018 s=(unz64_s*)file;
2019
2020 uReadThis = uSizeBuf;
2021 if (uReadThis>s->gi.size_comment)
2022 uReadThis = s->gi.size_comment;
2023
2024 if (ZSEEK64(s->z_filefunc,s->filestream,s->central_pos+22,ZLIB_FILEFUNC_SEEK_SET)!=0)
2025 return UNZ_ERRNO;
2026
2027 if (uReadThis>0)
2028 {
2029 *szComment='\0';
2030 if (ZREAD64(s->z_filefunc,s->filestream,szComment,uReadThis)!=uReadThis)
2031 return UNZ_ERRNO;
2032 }
2033
2034 if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
2035 *(szComment+s->gi.size_comment)='\0';
2036 return (int)uReadThis;
2037 }
2038
2039 /* Additions by RX '2004 */
2040 extern ZPOS64_T ZEXPORT unzGetOffset64(unzFile file) {
2041 unz64_s* s;
2042
2043 if (file==NULL)
2044 return 0; /* UNZ_PARAMERROR; */
2045 s=(unz64_s*)file;
2046 if (!s->current_file_ok)
2047 return 0;
2048 if (s->gi.number_entry != 0 && s->gi.number_entry != 0xffff)
2049 if (s->num_file==s->gi.number_entry)
2050 return 0;
2051 return s->pos_in_central_dir;
2052 }
2053
2054 extern uLong ZEXPORT unzGetOffset(unzFile file) {
2055 ZPOS64_T offset64;
2056
2057 if (file==NULL)
2058 return 0; /* UNZ_PARAMERROR; */
2059 offset64 = unzGetOffset64(file);
2060 return (uLong)offset64;
2061 }
2062
2063 extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos) {
2064 unz64_s* s;
2065 int err;
2066
2067 if (file==NULL)
2068 return UNZ_PARAMERROR;
2069 s=(unz64_s*)file;
2070
2071 s->pos_in_central_dir = pos;
2072 s->num_file = s->gi.number_entry; /* hack */
2073 err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
2074 &s->cur_file_info_internal,
2075 NULL,0,NULL,0,NULL,0);
2076 s->current_file_ok = (err == UNZ_OK);
2077 return err;
2078 }
2079
2080 extern int ZEXPORT unzSetOffset (unzFile file, uLong pos) {
2081 return unzSetOffset64(file,pos);
2082 }