unzip.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:48k
源码类别:

midi

开发平台:

Unix_Linux

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