unzip.c
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:48k
源码类别:

通讯编程

开发平台:

Visual C++

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