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