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