home *** CD-ROM | disk | FTP | other *** search
/ Freelog 11 / Freelog011.iso / Bas / Compression / ZLib / contrib / minizip / unzip.c < prev    next >
C/C++ Source or Header  |  1998-06-15  |  36KB  |  1,295 lines

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