home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / unzip.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  42.9 KB  |  1,271 lines

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