Debian Codegraph @

code/qcommon/unzip.c

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