Debian Codegraph @

Contrib/zip2exe/zlib/unzip.c

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