]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/common/unzip.c
Merge branch 'dpkdir' into 'master'
[xonotic/netradiant.git] / tools / quake3 / common / unzip.c
1 /*
2 WARNING: DO NOT UNCRUSTIFY
3 It will still compile after an uncrustify, but it will be *broken*
4 See https://github.com/TTimo/GtkRadiant/issues/33
5 */
6
7 /*
8 Copyright (C) 1999-2007 id Software, Inc. and contributors.
9 For a list of contributors, see the accompanying CONTRIBUTORS file.
10
11 This file is part of GtkRadiant.
12
13 GtkRadiant is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 GtkRadiant is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with GtkRadiant; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26 */
27
28 /*****************************************************************************
29  * name:                unzip.c
30  *
31  * desc:                IO on .zip files using portions of zlib 
32  *
33  *
34  *****************************************************************************/
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include "unzip.h"
40
41 // TTimo added for safe_malloc wrapping
42 #include "cmdlib.h"
43
44 /* unzip.h -- IO for uncompress .zip files using zlib 
45    Version 0.15 beta, Mar 19th, 1998,
46
47    Copyright (C) 1998 Gilles Vollant
48
49    This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g
50      WinZip, InfoZip tools and compatible.
51    Encryption and multi volume ZipFile (span) are not supported.
52    Old compressions used by old PKZip 1.x are not supported
53
54    THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE
55    CAN CHANGE IN FUTURE VERSION !!
56    I WAIT FEEDBACK at mail info@winimage.com
57    Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution
58
59    Condition of use and distribution are the same than zlib :
60
61   This software is provided 'as-is', without any express or implied
62   warranty.  In no event will the authors be held liable for any damages
63   arising from the use of this software.
64
65   Permission is granted to anyone to use this software for any purpose,
66   including commercial applications, and to alter it and redistribute it
67   freely, subject to the following restrictions:
68
69   1. The origin of this software must not be misrepresented; you must not
70      claim that you wrote the original software. If you use this software
71      in a product, an acknowledgment in the product documentation would be
72      appreciated but is not required.
73   2. Altered source versions must be plainly marked as such, and must not be
74      misrepresented as being the original software.
75   3. This notice may not be removed or altered from any source distribution.
76
77
78 */
79
80 // private stuff to not include cmdlib.h
81 /*
82 ============================================================================
83
84                                         BYTE ORDER FUNCTIONS
85
86 ============================================================================
87 */
88
89 #ifdef _SGI_SOURCE
90 #define __BIG_ENDIAN__
91 #endif
92
93 #ifdef __BIG_ENDIAN__
94
95 short   __LittleShort (short l)
96 {
97         byte    b1,b2;
98
99         b1 = l&255;
100         b2 = (l>>8)&255;
101
102         return (b1<<8) + b2;
103 }
104
105 short   __BigShort (short l)
106 {
107         return l;
108 }
109
110
111 int    __LittleLong (int l)
112 {
113         byte    b1,b2,b3,b4;
114
115         b1 = l&255;
116         b2 = (l>>8)&255;
117         b3 = (l>>16)&255;
118         b4 = (l>>24)&255;
119
120         return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
121 }
122
123 int    __BigLong (int l)
124 {
125         return l;
126 }
127
128
129 float   __LittleFloat (float l)
130 {
131         union {byte b[4]; float f;} in, out;
132         
133         in.f = l;
134         out.b[0] = in.b[3];
135         out.b[1] = in.b[2];
136         out.b[2] = in.b[1];
137         out.b[3] = in.b[0];
138         
139         return out.f;
140 }
141
142 float   __BigFloat (float l)
143 {
144         return l;
145 }
146
147
148 #else
149
150
151 short   __BigShort (short l)
152 {
153         byte    b1,b2;
154
155         b1 = l&255;
156         b2 = (l>>8)&255;
157
158         return (b1<<8) + b2;
159 }
160
161 short   __LittleShort (short l)
162 {
163         return l;
164 }
165
166
167 int    __BigLong (int l)
168 {
169         byte    b1,b2,b3,b4;
170
171         b1 = l&255;
172         b2 = (l>>8)&255;
173         b3 = (l>>16)&255;
174         b4 = (l>>24)&255;
175
176         return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
177 }
178
179 int    __LittleLong (int l)
180 {
181         return l;
182 }
183
184 float   __BigFloat (float l)
185 {
186         union {byte b[4]; float f;} in, out;
187         
188         in.f = l;
189         out.b[0] = in.b[3];
190         out.b[1] = in.b[2];
191         out.b[2] = in.b[1];
192         out.b[3] = in.b[0];
193         
194         return out.f;
195 }
196
197 float   __LittleFloat (float l)
198 {
199         return l;
200 }
201
202
203
204 #endif
205
206
207
208
209 #define zmemcpy memcpy
210 #define zmemcmp memcmp
211 #define zmemzero(dest, len) memset(dest, 0, len)
212
213 /* Diagnostic functions */
214 #ifdef _ZIP_DEBUG_
215    int z_verbose = 0;
216 #  define Assert(cond,msg) assert(cond);
217    //{if(!(cond)) Sys_Error(msg);}
218 #  define Trace(x) {if (z_verbose>=0) Sys_Error x ;}
219 #  define Tracev(x) {if (z_verbose>0) Sys_Error x ;}
220 #  define Tracevv(x) {if (z_verbose>1) Sys_Error x ;}
221 #  define Tracec(c,x) {if (z_verbose>0 && (c)) Sys_Error x ;}
222 #  define Tracecv(c,x) {if (z_verbose>1 && (c)) Sys_Error x ;}
223 #else
224 #  define Assert(cond,msg)
225 #  define Trace(x)
226 #  define Tracev(x)
227 #  define Tracevv(x)
228 #  define Tracec(c,x)
229 #  define Tracecv(c,x)
230 #endif
231
232
233 typedef uLong (*check_func) OF((uLong check, const Byte *buf, uInt len));
234 static voidp zcalloc OF((voidp opaque, unsigned items, unsigned size));
235 static void  zcfree  OF((voidp opaque, voidp ptr));
236
237 #define ZALLOC(strm, items, size) \
238            (*((strm)->zalloc))((strm)->opaque, (items), (size))
239 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidp)(addr))
240 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
241
242 // use real zlib
243 #ifndef DEF_WBITS
244 #  define DEF_WBITS MAX_WBITS
245 #endif
246 /* default windowBits for decompression. MAX_WBITS is for compression only */
247 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
248 #include <zlib.h>
249
250 #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
251                       !defined(CASESENSITIVITYDEFAULT_NO)
252 #define CASESENSITIVITYDEFAULT_NO
253 #endif
254
255
256 #ifndef UNZ_BUFSIZE
257 #define UNZ_BUFSIZE (65536)
258 #endif
259
260 #ifndef UNZ_MAXFILENAMEINZIP
261 #define UNZ_MAXFILENAMEINZIP (256)
262 #endif
263
264 #ifndef ALLOC
265 # define ALLOC(size) (safe_malloc(size))
266 #endif
267 #ifndef TRYFREE
268 # define TRYFREE(p) {if (p) free(p);}
269 #endif
270
271 #define SIZECENTRALDIRITEM (0x2e)
272 #define SIZEZIPLOCALHEADER (0x1e)
273
274 /* ===========================================================================
275      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
276    for end of file.
277    IN assertion: the stream s has been sucessfully opened for reading.
278 */
279
280 /*
281 static int unzlocal_getByte(FILE *fin,int *pi)
282 {
283     unsigned char c;
284         int err = fread(&c, 1, 1, fin);
285     if (err==1)
286     {
287         *pi = (int)c;
288         return UNZ_OK;
289     }
290     else
291     {
292         if (ferror(fin)) 
293             return UNZ_ERRNO;
294         else
295             return UNZ_EOF;
296     }
297 }
298 */
299
300 /* ===========================================================================
301    Reads a long in LSB order from the given gz_stream. Sets 
302 */
303 static int unzlocal_getShort (FILE* fin, uLong *pX)
304 {
305         short   v;
306
307         if ( fread( &v, sizeof( v ), 1, fin ) != 1 ) {
308                 return UNZ_EOF;
309         }
310
311         *pX = __LittleShort( v);
312         return UNZ_OK;
313
314 /*
315     uLong x ;
316     int i;
317     int err;
318
319     err = unzlocal_getByte(fin,&i);
320     x = (uLong)i;
321     
322     if (err==UNZ_OK)
323         err = unzlocal_getByte(fin,&i);
324     x += ((uLong)i)<<8;
325    
326     if (err==UNZ_OK)
327         *pX = x;
328     else
329         *pX = 0;
330     return err;
331 */
332 }
333
334 static int unzlocal_getLong (FILE *fin, uLong *pX)
335 {
336         int             v;
337
338         if ( fread( &v, sizeof( v ), 1, fin ) != 1 ) {
339                 return UNZ_EOF;
340         }
341
342         *pX = __LittleLong( v);
343         return UNZ_OK;
344
345 /*
346     uLong x ;
347     int i;
348     int err;
349
350     err = unzlocal_getByte(fin,&i);
351     x = (uLong)i;
352     
353     if (err==UNZ_OK)
354         err = unzlocal_getByte(fin,&i);
355     x += ((uLong)i)<<8;
356
357     if (err==UNZ_OK)
358         err = unzlocal_getByte(fin,&i);
359     x += ((uLong)i)<<16;
360
361     if (err==UNZ_OK)
362         err = unzlocal_getByte(fin,&i);
363     x += ((uLong)i)<<24;
364    
365     if (err==UNZ_OK)
366         *pX = x;
367     else
368         *pX = 0;
369     return err;
370 */
371 }
372
373
374 /* My own strcmpi / strcasecmp */
375 static int strcmpcasenosensitive_internal (const char* fileName1,const char* fileName2)
376 {
377         for (;;)
378         {
379                 char c1=*(fileName1++);
380                 char c2=*(fileName2++);
381                 if ((c1>='a') && (c1<='z'))
382                         c1 -= 0x20;
383                 if ((c2>='a') && (c2<='z'))
384                         c2 -= 0x20;
385                 if (c1=='\0')
386                         return ((c2=='\0') ? 0 : -1);
387                 if (c2=='\0')
388                         return 1;
389                 if (c1<c2)
390                         return -1;
391                 if (c1>c2)
392                         return 1;
393         }
394 }
395
396
397 #ifdef  CASESENSITIVITYDEFAULT_NO
398 #define CASESENSITIVITYDEFAULTVALUE 2
399 #else
400 #define CASESENSITIVITYDEFAULTVALUE 1
401 #endif
402
403 #ifndef STRCMPCASENOSENTIVEFUNCTION
404 #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
405 #endif
406
407 /* 
408    Compare two filename (fileName1,fileName2).
409    If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
410    If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
411                                                                 or strcasecmp)
412    If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
413         (like 1 on Unix, 2 on Windows)
414
415 */
416 extern int unzStringFileNameCompare (const char* fileName1,const char* fileName2,int iCaseSensitivity)
417 {
418         if (iCaseSensitivity==0)
419                 iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
420
421         if (iCaseSensitivity==1)
422                 return strcmp(fileName1,fileName2);
423
424         return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
425
426
427 #define BUFREADCOMMENT (0x400)
428
429 /*
430   Locate the Central directory of a zipfile (at the end, just before
431     the global comment)
432 */
433 static uLong unzlocal_SearchCentralDir(FILE *fin)
434 {
435         unsigned char* buf;
436         uLong uSizeFile;
437         uLong uBackRead;
438         uLong uMaxBack=0xffff; /* maximum size of global comment */
439         uLong uPosFound=0;
440         
441         if (fseek(fin,0,SEEK_END) != 0)
442                 return 0;
443
444
445         uSizeFile = ftell( fin );
446         
447         if (uMaxBack>uSizeFile)
448                 uMaxBack = uSizeFile;
449
450         buf = (unsigned char*)safe_malloc(BUFREADCOMMENT+4);
451         if (buf==NULL)
452                 return 0;
453
454         uBackRead = 4;
455         while (uBackRead<uMaxBack)
456         {
457                 uLong uReadSize,uReadPos ;
458                 int i;
459                 if (uBackRead+BUFREADCOMMENT>uMaxBack) 
460                         uBackRead = uMaxBack;
461                 else
462                         uBackRead+=BUFREADCOMMENT;
463                 uReadPos = uSizeFile-uBackRead ;
464                 
465                 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? 
466                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
467                 if (fseek(fin,uReadPos,SEEK_SET)!=0)
468                         break;
469
470                 if (fread(buf,(uInt)uReadSize,1,fin)!=1)
471                         break;
472
473                 for (i=(int)uReadSize-3; (i--)>0;)
474                         if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && 
475                                 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
476                         {
477                                 uPosFound = uReadPos+i;
478                                 break;
479                         }
480
481                 if (uPosFound!=0)
482                         break;
483         }
484         free(buf);
485         return uPosFound;
486 }
487
488 extern unzFile unzReOpen (const char* path, unzFile file)
489 {
490         unz_s *s;
491         FILE * fin;
492
493     fin=fopen(path,"rb");
494         if (fin==NULL)
495                 return NULL;
496
497         s=(unz_s*)safe_malloc(sizeof(unz_s));
498         memcpy(s, (unz_s*)file, sizeof(unz_s));
499
500         s->file = fin;
501         return (unzFile)s;      
502 }
503
504 /*
505   Open a Zip file. path contain the full pathname (by example,
506      on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer
507          "zlib/zlib109.zip".
508          If the zipfile cannot be opened (file don't exist or in not valid), the
509            return value is NULL.
510      Else, the return value is a unzFile Handle, usable with other function
511            of this unzip package.
512 */
513 extern unzFile unzOpen (const char* path)
514 {
515         unz_s us;
516         unz_s *s;
517         uLong central_pos,uL;
518         FILE * fin ;
519
520         uLong number_disk;          /* number of the current dist, used for 
521                                                                    spaning ZIP, unsupported, always 0*/
522         uLong number_disk_with_CD;  /* number the the disk with central dir, used
523                                                                    for spaning ZIP, unsupported, always 0*/
524         uLong number_entry_CD;      /* total number of entries in
525                                        the central dir 
526                                        (same than number_entry on nospan) */
527
528         int err=UNZ_OK;
529
530     fin=fopen(path,"rb");
531         if (fin==NULL)
532                 return NULL;
533
534         central_pos = unzlocal_SearchCentralDir(fin);
535         if (central_pos==0)
536                 err=UNZ_ERRNO;
537
538         if (fseek(fin,central_pos,SEEK_SET)!=0)
539                 err=UNZ_ERRNO;
540
541         /* the signature, already checked */
542         if (unzlocal_getLong(fin,&uL)!=UNZ_OK)
543                 err=UNZ_ERRNO;
544
545         /* number of this disk */
546         if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK)
547                 err=UNZ_ERRNO;
548
549         /* number of the disk with the start of the central directory */
550         if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK)
551                 err=UNZ_ERRNO;
552
553         /* total number of entries in the central dir on this disk */
554         if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK)
555                 err=UNZ_ERRNO;
556
557         /* total number of entries in the central dir */
558         if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK)
559                 err=UNZ_ERRNO;
560
561         if ((number_entry_CD!=us.gi.number_entry) ||
562                 (number_disk_with_CD!=0) ||
563                 (number_disk!=0))
564                 err=UNZ_BADZIPFILE;
565
566         /* size of the central directory */
567         if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)
568                 err=UNZ_ERRNO;
569
570         /* offset of start of central directory with respect to the 
571               starting disk number */
572         if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)
573                 err=UNZ_ERRNO;
574
575         /* zipfile comment length */
576         if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK)
577                 err=UNZ_ERRNO;
578
579         if ((central_pos<us.offset_central_dir+us.size_central_dir) && 
580                 (err==UNZ_OK))
581                 err=UNZ_BADZIPFILE;
582
583         if (err!=UNZ_OK)
584         {
585                 fclose(fin);
586                 return NULL;
587         }
588
589         us.file=fin;
590         us.byte_before_the_zipfile = central_pos -
591                                     (us.offset_central_dir+us.size_central_dir);
592         us.central_pos = central_pos;
593     us.pfile_in_zip_read = NULL;
594         
595
596         s=(unz_s*)safe_malloc(sizeof(unz_s));
597         *s=us;
598 //      unzGoToFirstFile((unzFile)s);   
599         return (unzFile)s;      
600 }
601
602
603 /*
604   Close a ZipFile opened with unzipOpen.
605   If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
606     these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
607   return UNZ_OK if there is no problem. */
608 extern int unzClose (unzFile file)
609 {
610         unz_s* s;
611         if (file==NULL)
612                 return UNZ_PARAMERROR;
613         s=(unz_s*)file;
614
615     if (s->pfile_in_zip_read!=NULL)
616         unzCloseCurrentFile(file);
617
618         fclose(s->file);
619         free(s);
620         return UNZ_OK;
621 }
622
623
624 /*
625   Write info about the ZipFile in the *pglobal_info structure.
626   No preparation of the structure is needed
627   return UNZ_OK if there is no problem. */
628 extern int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info)
629 {
630         unz_s* s;
631         if (file==NULL)
632                 return UNZ_PARAMERROR;
633         s=(unz_s*)file;
634         *pglobal_info=s->gi;
635         return UNZ_OK;
636 }
637
638
639 /*
640    Translate date/time from Dos format to tm_unz (readable more easilty)
641 */
642 static void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz* ptm)
643 {
644     uLong uDate;
645     uDate = (uLong)(ulDosDate>>16);
646     ptm->tm_mday = (uInt)(uDate&0x1f) ;
647     ptm->tm_mon =  (uInt)((((uDate)&0x1E0)/0x20)-1) ;
648     ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
649
650     ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
651     ptm->tm_min =  (uInt) ((ulDosDate&0x7E0)/0x20) ;
652     ptm->tm_sec =  (uInt) (2*(ulDosDate&0x1f)) ;
653 }
654
655 /*
656   Get Info about the current file in the zipfile, with internal only info
657 */
658 static int unzlocal_GetCurrentFileInfoInternal (unzFile file,
659                                                   unz_file_info *pfile_info,
660                                                   unz_file_info_internal 
661                                                   *pfile_info_internal,
662                                                   char *szFileName,
663                                                                                                   uLong fileNameBufferSize,
664                                                   void *extraField,
665                                                                                                   uLong extraFieldBufferSize,
666                                                   char *szComment,
667                                                                                                   uLong commentBufferSize)
668 {
669         unz_s* s;
670         unz_file_info file_info;
671         unz_file_info_internal file_info_internal;
672         int err=UNZ_OK;
673         uLong uMagic;
674         long lSeek=0;
675
676         if (file==NULL)
677                 return UNZ_PARAMERROR;
678         s=(unz_s*)file;
679         if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)
680                 err=UNZ_ERRNO;
681
682
683         /* we check the magic */
684         if (err==UNZ_OK) {
685                 if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
686                         err=UNZ_ERRNO;
687                 else if (uMagic!=0x02014b50)
688                         err=UNZ_BADZIPFILE;
689         }
690
691         if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
692                 err=UNZ_ERRNO;
693
694         if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
695                 err=UNZ_ERRNO;
696
697         if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
698                 err=UNZ_ERRNO;
699
700         if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
701                 err=UNZ_ERRNO;
702
703         if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
704                 err=UNZ_ERRNO;
705
706     unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
707
708         if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
709                 err=UNZ_ERRNO;
710
711         if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
712                 err=UNZ_ERRNO;
713
714         if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
715                 err=UNZ_ERRNO;
716
717         if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
718                 err=UNZ_ERRNO;
719
720         if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)
721                 err=UNZ_ERRNO;
722
723         if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)
724                 err=UNZ_ERRNO;
725
726         if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)
727                 err=UNZ_ERRNO;
728
729         if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)
730                 err=UNZ_ERRNO;
731
732         if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)
733                 err=UNZ_ERRNO;
734
735         if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)
736                 err=UNZ_ERRNO;
737
738         lSeek+=file_info.size_filename;
739         if ((err==UNZ_OK) && (szFileName!=NULL))
740         {
741                 uLong uSizeRead ;
742                 if (file_info.size_filename<fileNameBufferSize)
743                 {
744                         *(szFileName+file_info.size_filename)='\0';
745                         uSizeRead = file_info.size_filename;
746                 }
747                 else
748                         uSizeRead = fileNameBufferSize;
749
750                 if ((file_info.size_filename>0) && (fileNameBufferSize>0))
751                         if (fread(szFileName,(uInt)uSizeRead,1,s->file)!=1)
752                                 err=UNZ_ERRNO;
753                 lSeek -= uSizeRead;
754         }
755
756         
757         if ((err==UNZ_OK) && (extraField!=NULL))
758         {
759                 uLong uSizeRead ;
760                 if (file_info.size_file_extra<extraFieldBufferSize)
761                         uSizeRead = file_info.size_file_extra;
762                 else
763                         uSizeRead = extraFieldBufferSize;
764
765                 if (lSeek!=0) {
766                         if (fseek(s->file,lSeek,SEEK_CUR)==0)
767                                 lSeek=0;
768                         else
769                                 err=UNZ_ERRNO;
770                 }
771                 if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
772                         if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1)
773                                 err=UNZ_ERRNO;
774                 lSeek += file_info.size_file_extra - uSizeRead;
775         }
776         else
777                 lSeek+=file_info.size_file_extra; 
778
779         
780         if ((err==UNZ_OK) && (szComment!=NULL))
781         {
782                 uLong uSizeRead ;
783                 if (file_info.size_file_comment<commentBufferSize)
784                 {
785                         *(szComment+file_info.size_file_comment)='\0';
786                         uSizeRead = file_info.size_file_comment;
787                 }
788                 else
789                         uSizeRead = commentBufferSize;
790
791                 if (lSeek!=0) {
792                         if (fseek(s->file,lSeek,SEEK_CUR)==0)
793                                 lSeek=0;
794                         else
795                                 err=UNZ_ERRNO;
796                 }
797                 if ((file_info.size_file_comment>0) && (commentBufferSize>0))
798                         if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1)
799                                 err=UNZ_ERRNO;
800                 lSeek+=file_info.size_file_comment - uSizeRead;
801         }
802         else
803                 lSeek+=file_info.size_file_comment;
804
805         if ((err==UNZ_OK) && (pfile_info!=NULL))
806                 *pfile_info=file_info;
807
808         if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
809                 *pfile_info_internal=file_info_internal;
810
811         return err;
812 }
813
814
815
816 /*
817   Write info about the ZipFile in the *pglobal_info structure.
818   No preparation of the structure is needed
819   return UNZ_OK if there is no problem.
820 */
821 extern int unzGetCurrentFileInfo (      unzFile file, unz_file_info *pfile_info,
822                                                                         char *szFileName, uLong fileNameBufferSize,
823                                                                         void *extraField, uLong extraFieldBufferSize,
824                                                                         char *szComment, uLong commentBufferSize)
825 {
826         return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,
827                                                                                                 szFileName,fileNameBufferSize,
828                                                                                                 extraField,extraFieldBufferSize,
829                                                                                                 szComment,commentBufferSize);
830 }
831
832 /*
833   Set the current file of the zipfile to the first file.
834   return UNZ_OK if there is no problem
835 */
836 extern int unzGoToFirstFile (unzFile file)
837 {
838         int err=UNZ_OK;
839         unz_s* s;
840         if (file==NULL)
841                 return UNZ_PARAMERROR;
842         s=(unz_s*)file;
843         s->pos_in_central_dir=s->offset_central_dir;
844         s->num_file=0;
845         err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
846                                                                                          &s->cur_file_info_internal,
847                                                                                          NULL,0,NULL,0,NULL,0);
848         s->current_file_ok = (err == UNZ_OK);
849         return err;
850 }
851
852
853 /*
854   Set the current file of the zipfile to the next file.
855   return UNZ_OK if there is no problem
856   return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
857 */
858 extern int unzGoToNextFile (unzFile file)
859 {
860         unz_s* s;       
861         int err;
862
863         if (file==NULL)
864                 return UNZ_PARAMERROR;
865         s=(unz_s*)file;
866         if (!s->current_file_ok)
867                 return UNZ_END_OF_LIST_OF_FILE;
868         if (s->num_file+1==s->gi.number_entry)
869                 return UNZ_END_OF_LIST_OF_FILE;
870
871         s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
872                         s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
873         s->num_file++;
874         err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
875                                                                                            &s->cur_file_info_internal,
876                                                                                            NULL,0,NULL,0,NULL,0);
877         s->current_file_ok = (err == UNZ_OK);
878         return err;
879 }
880
881
882 /*
883   Try locate the file szFileName in the zipfile.
884   For the iCaseSensitivity signification, see unzipStringFileNameCompare
885
886   return value :
887   UNZ_OK if the file is found. It becomes the current file.
888   UNZ_END_OF_LIST_OF_FILE if the file is not found
889 */
890 extern int unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)
891 {
892         unz_s* s;       
893         int err;
894
895         
896         uLong num_fileSaved;
897         uLong pos_in_central_dirSaved;
898
899
900         if (file==NULL)
901                 return UNZ_PARAMERROR;
902
903     if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
904         return UNZ_PARAMERROR;
905
906         s=(unz_s*)file;
907         if (!s->current_file_ok)
908                 return UNZ_END_OF_LIST_OF_FILE;
909
910         num_fileSaved = s->num_file;
911         pos_in_central_dirSaved = s->pos_in_central_dir;
912
913         err = unzGoToFirstFile(file);
914
915         while (err == UNZ_OK)
916         {
917                 char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
918                 unzGetCurrentFileInfo(file,NULL,
919                                                                 szCurrentFileName,sizeof(szCurrentFileName)-1,
920                                                                 NULL,0,NULL,0);
921                 if (unzStringFileNameCompare(szCurrentFileName,
922                                                                                 szFileName,iCaseSensitivity)==0)
923                         return UNZ_OK;
924                 err = unzGoToNextFile(file);
925         }
926
927         s->num_file = num_fileSaved ;
928         s->pos_in_central_dir = pos_in_central_dirSaved ;
929         return err;
930 }
931
932
933 /*
934   Read the static header of the current zipfile
935   Check the coherency of the static header and info in the end of central
936         directory about this file
937   store in *piSizeVar the size of extra info in static header
938         (filename and size of extra field data)
939 */
940 static int unzlocal_CheckCurrentFileCoherencyHeader (unz_s* s, uInt* piSizeVar,
941                                                                                                         uLong *poffset_local_extrafield,
942                                                                                                         uInt *psize_local_extrafield)
943 {
944         uLong uMagic,uData,uFlags;
945         uLong size_filename;
946         uLong size_extra_field;
947         int err=UNZ_OK;
948
949         *piSizeVar = 0;
950         *poffset_local_extrafield = 0;
951         *psize_local_extrafield = 0;
952
953         if (fseek(s->file,s->cur_file_info_internal.offset_curfile +
954                                                                 s->byte_before_the_zipfile,SEEK_SET)!=0)
955                 return UNZ_ERRNO;
956
957
958         if (err==UNZ_OK) {
959                 if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
960                         err=UNZ_ERRNO;
961                 else if (uMagic!=0x04034b50)
962                         err=UNZ_BADZIPFILE;
963         }
964
965         if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
966                 err=UNZ_ERRNO;
967 /*
968         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
969                 err=UNZ_BADZIPFILE;
970 */
971         if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)
972                 err=UNZ_ERRNO;
973
974         if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
975                 err=UNZ_ERRNO;
976         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
977                 err=UNZ_BADZIPFILE;
978
979     if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
980                          (s->cur_file_info.compression_method!=Z_DEFLATED))
981         err=UNZ_BADZIPFILE;
982
983         if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */
984                 err=UNZ_ERRNO;
985
986         if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */
987                 err=UNZ_ERRNO;
988         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
989                                       ((uFlags & 8)==0))
990                 err=UNZ_BADZIPFILE;
991
992         if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */
993                 err=UNZ_ERRNO;
994         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
995                                                           ((uFlags & 8)==0))
996                 err=UNZ_BADZIPFILE;
997
998         if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */
999                 err=UNZ_ERRNO;
1000         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && 
1001                                                           ((uFlags & 8)==0))
1002                 err=UNZ_BADZIPFILE;
1003
1004
1005         if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)
1006                 err=UNZ_ERRNO;
1007         else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
1008                 err=UNZ_BADZIPFILE;
1009
1010         *piSizeVar += (uInt)size_filename;
1011
1012         if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)
1013                 err=UNZ_ERRNO;
1014         *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
1015                                                                         SIZEZIPLOCALHEADER + size_filename;
1016         *psize_local_extrafield = (uInt)size_extra_field;
1017
1018         *piSizeVar += (uInt)size_extra_field;
1019
1020         return err;
1021 }
1022                                                                                                 
1023 /*
1024   Open for reading data the current file in the zipfile.
1025   If there is no error and the file is opened, the return value is UNZ_OK.
1026 */
1027 extern int unzOpenCurrentFile (unzFile file)
1028 {
1029         int err=UNZ_OK;
1030         int Store;
1031         uInt iSizeVar;
1032         unz_s* s;
1033         file_in_zip_read_info_s* pfile_in_zip_read_info;
1034         uLong offset_local_extrafield;  /* offset of the static extra field */
1035         uInt  size_local_extrafield;    /* size of the static extra field */
1036
1037         if (file==NULL)
1038                 return UNZ_PARAMERROR;
1039         s=(unz_s*)file;
1040         if (!s->current_file_ok)
1041                 return UNZ_PARAMERROR;
1042
1043     if (s->pfile_in_zip_read != NULL)
1044         unzCloseCurrentFile(file);
1045
1046         if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
1047                                 &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
1048                 return UNZ_BADZIPFILE;
1049
1050         pfile_in_zip_read_info = (file_in_zip_read_info_s*)
1051                                                                             safe_malloc(sizeof(file_in_zip_read_info_s));
1052         if (pfile_in_zip_read_info==NULL)
1053                 return UNZ_INTERNALERROR;
1054
1055         pfile_in_zip_read_info->read_buffer=(char*)safe_malloc(UNZ_BUFSIZE);
1056         pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
1057         pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
1058         pfile_in_zip_read_info->pos_local_extrafield=0;
1059
1060         if (pfile_in_zip_read_info->read_buffer==NULL)
1061         {
1062                 free(pfile_in_zip_read_info);
1063                 return UNZ_INTERNALERROR;
1064         }
1065
1066         pfile_in_zip_read_info->stream_initialised=0;
1067         
1068         if ((s->cur_file_info.compression_method!=0) &&
1069         (s->cur_file_info.compression_method!=Z_DEFLATED))
1070                 err=UNZ_BADZIPFILE;
1071         Store = s->cur_file_info.compression_method==0;
1072
1073         pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
1074         pfile_in_zip_read_info->crc32=0;
1075         pfile_in_zip_read_info->compression_method =
1076             s->cur_file_info.compression_method;
1077         pfile_in_zip_read_info->file=s->file;
1078         pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
1079
1080     pfile_in_zip_read_info->stream.total_out = 0;
1081
1082         if (!Store)
1083         {
1084           pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
1085           pfile_in_zip_read_info->stream.zfree = (free_func)0;
1086           pfile_in_zip_read_info->stream.opaque = (voidp)0; 
1087       
1088           err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
1089           if (err == Z_OK)
1090             pfile_in_zip_read_info->stream_initialised=1;
1091         /* windowBits is passed < 0 to tell that there is no zlib header.
1092          * Note that in this case inflate *requires* an extra "dummy" byte
1093          * after the compressed stream in order to complete decompression and
1094          * return Z_STREAM_END. 
1095          * In unzip, i don't wait absolutely Z_STREAM_END because I known the 
1096          * size of both compressed and uncompressed data
1097          */
1098         }
1099         pfile_in_zip_read_info->rest_read_compressed = 
1100             s->cur_file_info.compressed_size ;
1101         pfile_in_zip_read_info->rest_read_uncompressed = 
1102             s->cur_file_info.uncompressed_size ;
1103
1104         
1105         pfile_in_zip_read_info->pos_in_zipfile = 
1106             s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + 
1107                           iSizeVar;
1108         
1109         pfile_in_zip_read_info->stream.avail_in = (uInt)0;
1110
1111
1112         s->pfile_in_zip_read = pfile_in_zip_read_info;
1113     return UNZ_OK;
1114 }
1115
1116
1117 /*
1118   Read bytes from the current file.
1119   buf contain buffer where data must be copied
1120   len the size of buf.
1121
1122   return the number of byte copied if somes bytes are copied
1123   return 0 if the end of file was reached
1124   return <0 with error code if there is an error
1125     (UNZ_ERRNO for IO error, or zLib error for uncompress error)
1126 */
1127 extern int unzReadCurrentFile  (unzFile file, void *buf, unsigned len)
1128 {
1129         int err=UNZ_OK;
1130         uInt iRead = 0;
1131         unz_s* s;
1132         file_in_zip_read_info_s* pfile_in_zip_read_info;
1133         if (file==NULL)
1134                 return UNZ_PARAMERROR;
1135         s=(unz_s*)file;
1136     pfile_in_zip_read_info=s->pfile_in_zip_read;
1137
1138         if (pfile_in_zip_read_info==NULL)
1139                 return UNZ_PARAMERROR;
1140
1141
1142         if ((pfile_in_zip_read_info->read_buffer == NULL))
1143                 return UNZ_END_OF_LIST_OF_FILE;
1144         if (len==0)
1145                 return 0;
1146
1147         pfile_in_zip_read_info->stream.next_out = (Byte*)buf;
1148
1149         pfile_in_zip_read_info->stream.avail_out = (uInt)len;
1150         
1151         if (len>pfile_in_zip_read_info->rest_read_uncompressed)
1152                 pfile_in_zip_read_info->stream.avail_out = 
1153                   (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
1154
1155         while (pfile_in_zip_read_info->stream.avail_out>0)
1156         {
1157                 if ((pfile_in_zip_read_info->stream.avail_in==0) &&
1158             (pfile_in_zip_read_info->rest_read_compressed>0))
1159                 {
1160                         uInt uReadThis = UNZ_BUFSIZE;
1161                         if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
1162                                 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
1163                         if (uReadThis == 0)
1164                                 return UNZ_EOF;
1165                         if (s->cur_file_info.compressed_size == pfile_in_zip_read_info->rest_read_compressed)
1166                                 if (fseek(pfile_in_zip_read_info->file,
1167                                                   pfile_in_zip_read_info->pos_in_zipfile + 
1168                                                          pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0)
1169                                         return UNZ_ERRNO;
1170                         if (fread(pfile_in_zip_read_info->read_buffer,uReadThis,1,
1171                          pfile_in_zip_read_info->file)!=1)
1172                                 return UNZ_ERRNO;
1173                         pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
1174
1175                         pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
1176                         
1177                         pfile_in_zip_read_info->stream.next_in = 
1178                 (Byte*)pfile_in_zip_read_info->read_buffer;
1179                         pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
1180                 }
1181
1182                 if (pfile_in_zip_read_info->compression_method==0)
1183                 {
1184                         uInt uDoCopy,i ;
1185                         if (pfile_in_zip_read_info->stream.avail_out < 
1186                             pfile_in_zip_read_info->stream.avail_in)
1187                                 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
1188                         else
1189                                 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
1190                                 
1191                         for (i=0;i<uDoCopy;i++)
1192                                 *(pfile_in_zip_read_info->stream.next_out+i) =
1193                         *(pfile_in_zip_read_info->stream.next_in+i);
1194                                         
1195                         pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
1196                                                                 pfile_in_zip_read_info->stream.next_out,
1197                                                                 uDoCopy);
1198                         pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
1199                         pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
1200                         pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
1201                         pfile_in_zip_read_info->stream.next_out += uDoCopy;
1202                         pfile_in_zip_read_info->stream.next_in += uDoCopy;
1203             pfile_in_zip_read_info->stream.total_out += uDoCopy;
1204                         iRead += uDoCopy;
1205                 }
1206                 else
1207                 {
1208                         uLong uTotalOutBefore,uTotalOutAfter;
1209                         const Byte *bufBefore;
1210                         uLong uOutThis;
1211                         int flush=Z_SYNC_FLUSH;
1212
1213                         uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
1214                         bufBefore = pfile_in_zip_read_info->stream.next_out;
1215
1216                         /*
1217                         if ((pfile_in_zip_read_info->rest_read_uncompressed ==
1218                                  pfile_in_zip_read_info->stream.avail_out) &&
1219                                 (pfile_in_zip_read_info->rest_read_compressed == 0))
1220                                 flush = Z_FINISH;
1221                         */
1222                         err=inflate(&pfile_in_zip_read_info->stream,flush);
1223
1224                         uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
1225                         uOutThis = uTotalOutAfter-uTotalOutBefore;
1226                         
1227                         pfile_in_zip_read_info->crc32 = 
1228                 crc32(pfile_in_zip_read_info->crc32,bufBefore,
1229                         (uInt)(uOutThis));
1230
1231                         pfile_in_zip_read_info->rest_read_uncompressed -=
1232                 uOutThis;
1233
1234                         iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
1235             
1236                         if (err==Z_STREAM_END)
1237                                 return (iRead==0) ? UNZ_EOF : iRead;
1238                         if (err!=Z_OK) 
1239                                 break;
1240                 }
1241         }
1242
1243         if (err==Z_OK)
1244                 return iRead;
1245         return err;
1246 }
1247
1248
1249 /*
1250   Give the current position in uncompressed data
1251 */
1252 extern long unztell (unzFile file)
1253 {
1254         unz_s* s;
1255         file_in_zip_read_info_s* pfile_in_zip_read_info;
1256         if (file==NULL)
1257                 return UNZ_PARAMERROR;
1258         s=(unz_s*)file;
1259     pfile_in_zip_read_info=s->pfile_in_zip_read;
1260
1261         if (pfile_in_zip_read_info==NULL)
1262                 return UNZ_PARAMERROR;
1263
1264         return (long)pfile_in_zip_read_info->stream.total_out;
1265 }
1266
1267
1268 /*
1269   return 1 if the end of file was reached, 0 elsewhere 
1270 */
1271 extern int unzeof (unzFile file)
1272 {
1273         unz_s* s;
1274         file_in_zip_read_info_s* pfile_in_zip_read_info;
1275         if (file==NULL)
1276                 return UNZ_PARAMERROR;
1277         s=(unz_s*)file;
1278     pfile_in_zip_read_info=s->pfile_in_zip_read;
1279
1280         if (pfile_in_zip_read_info==NULL)
1281                 return UNZ_PARAMERROR;
1282         
1283         if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
1284                 return 1;
1285         else
1286                 return 0;
1287 }
1288
1289
1290
1291 /*
1292   Read extra field from the current file (opened by unzOpenCurrentFile)
1293   This is the static-header version of the extra field (sometimes, there is
1294     more info in the static-header version than in the central-header)
1295
1296   if buf==NULL, it return the size of the static extra field that can be read
1297
1298   if buf!=NULL, len is the size of the buffer, the extra header is copied in
1299         buf.
1300   the return value is the number of bytes copied in buf, or (if <0) 
1301         the error code
1302 */
1303 extern int unzGetLocalExtrafield (unzFile file,void *buf,unsigned len)
1304 {
1305         unz_s* s;
1306         file_in_zip_read_info_s* pfile_in_zip_read_info;
1307         uInt read_now;
1308         uLong size_to_read;
1309
1310         if (file==NULL)
1311                 return UNZ_PARAMERROR;
1312         s=(unz_s*)file;
1313     pfile_in_zip_read_info=s->pfile_in_zip_read;
1314
1315         if (pfile_in_zip_read_info==NULL)
1316                 return UNZ_PARAMERROR;
1317
1318         size_to_read = (pfile_in_zip_read_info->size_local_extrafield - 
1319                                 pfile_in_zip_read_info->pos_local_extrafield);
1320
1321         if (buf==NULL)
1322                 return (int)size_to_read;
1323         
1324         if (len>size_to_read)
1325                 read_now = (uInt)size_to_read;
1326         else
1327                 read_now = (uInt)len ;
1328
1329         if (read_now==0)
1330                 return 0;
1331         
1332         if (fseek(pfile_in_zip_read_info->file,
1333               pfile_in_zip_read_info->offset_local_extrafield + 
1334                           pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)
1335                 return UNZ_ERRNO;
1336
1337         if (fread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1)
1338                 return UNZ_ERRNO;
1339
1340         return (int)read_now;
1341 }
1342
1343 /*
1344   Close the file in zip opened with unzipOpenCurrentFile
1345   Return UNZ_CRCERROR if all the file was read but the CRC is not good
1346 */
1347 extern int unzCloseCurrentFile (unzFile file)
1348 {
1349         int err=UNZ_OK;
1350
1351         unz_s* s;
1352         file_in_zip_read_info_s* pfile_in_zip_read_info;
1353         if (file==NULL)
1354                 return UNZ_PARAMERROR;
1355         s=(unz_s*)file;
1356     pfile_in_zip_read_info=s->pfile_in_zip_read;
1357
1358         if (pfile_in_zip_read_info==NULL)
1359                 return UNZ_PARAMERROR;
1360
1361
1362         if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
1363         {
1364                 if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
1365                         err=UNZ_CRCERROR;
1366         }
1367
1368
1369         free(pfile_in_zip_read_info->read_buffer);
1370         pfile_in_zip_read_info->read_buffer = NULL;
1371         if (pfile_in_zip_read_info->stream_initialised)
1372                 inflateEnd(&pfile_in_zip_read_info->stream);
1373
1374         pfile_in_zip_read_info->stream_initialised = 0;
1375         free(pfile_in_zip_read_info);
1376
1377     s->pfile_in_zip_read=NULL;
1378
1379         return err;
1380 }
1381
1382
1383 /*
1384   Get the global comment string of the ZipFile, in the szComment buffer.
1385   uSizeBuf is the size of the szComment buffer.
1386   return the number of byte copied or an error code <0
1387 */
1388 extern int unzGetGlobalComment (unzFile file, char *szComment, uLong uSizeBuf)
1389 {
1390         unz_s* s;
1391         uLong uReadThis ;
1392         if (file==NULL)
1393                 return UNZ_PARAMERROR;
1394         s=(unz_s*)file;
1395
1396         uReadThis = uSizeBuf;
1397         if (uReadThis>s->gi.size_comment)
1398                 uReadThis = s->gi.size_comment;
1399
1400         if (fseek(s->file,s->central_pos+22,SEEK_SET)!=0)
1401                 return UNZ_ERRNO;
1402
1403         if (uReadThis>0)
1404     {
1405       *szComment='\0';
1406           if (fread(szComment,(uInt)uReadThis,1,s->file)!=1)
1407                 return UNZ_ERRNO;
1408     }
1409
1410         if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
1411                 *(szComment+s->gi.size_comment)='\0';
1412         return (int)uReadThis;
1413 }
1414
1415 /* crc32.c -- compute the CRC-32 of a data stream
1416  * Copyright (C) 1995-1998 Mark Adler
1417  * For conditions of distribution and use, see copyright notice in zlib.h 
1418  */
1419
1420
1421 #ifdef DYNAMIC_CRC_TABLE
1422
1423 static int crc_table_empty = 1;
1424 static uLong crc_table[256];
1425 static void make_crc_table OF((void));
1426
1427 /*
1428   Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
1429   x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
1430
1431   Polynomials over GF(2) are represented in binary, one bit per coefficient,
1432   with the lowest powers in the most significant bit.  Then adding polynomials
1433   is just exclusive-or, and multiplying a polynomial by x is a right shift by
1434   one.  If we call the above polynomial p, and represent a byte as the
1435   polynomial q, also with the lowest power in the most significant bit (so the
1436   byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
1437   where a mod b means the remainder after dividing a by b.
1438
1439   This calculation is done using the shift-register method of multiplying and
1440   taking the remainder.  The register is initialized to zero, and for each
1441   incoming bit, x^32 is added mod p to the register if the bit is a one (where
1442   x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
1443   x (which is shifting right by one and adding x^32 mod p if the bit shifted
1444   out is a one).  We start with the highest power (least significant bit) of
1445   q and repeat for all eight bits of q.
1446
1447   The table is simply the CRC of all possible eight bit values.  This is all
1448   the information needed to generate CRC's on data a byte at a time for all
1449   combinations of CRC register values and incoming bytes.
1450 */
1451 static void make_crc_table()
1452 {
1453   uLong c;
1454   int n, k;
1455   uLong poly;            /* polynomial exclusive-or pattern */
1456   /* terms of polynomial defining this crc (except x^32): */
1457   static const Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
1458
1459   /* make exclusive-or pattern from polynomial (0xedb88320L) */
1460   poly = 0L;
1461   for (n = 0; n < sizeof(p)/sizeof(Byte); n++)
1462     poly |= 1L << (31 - p[n]);
1463  
1464   for (n = 0; n < 256; n++)
1465   {
1466     c = (uLong)n;
1467     for (k = 0; k < 8; k++)
1468       c = c & 1 ? poly ^ (c >> 1) : c >> 1;
1469     crc_table[n] = c;
1470   }
1471   crc_table_empty = 0;
1472 }
1473 #else
1474 /* ========================================================================
1475  * Table of CRC-32's of all single-byte values (made by make_crc_table)
1476  */
1477 static const uLong crc_table[256] = {
1478   0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
1479   0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
1480   0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
1481   0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
1482   0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
1483   0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
1484   0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
1485   0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
1486   0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
1487   0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
1488   0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
1489   0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
1490   0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
1491   0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
1492   0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
1493   0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
1494   0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
1495   0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
1496   0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
1497   0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
1498   0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
1499   0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
1500   0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
1501   0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
1502   0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
1503   0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
1504   0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
1505   0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
1506   0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
1507   0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
1508   0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
1509   0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
1510   0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
1511   0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
1512   0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
1513   0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
1514   0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
1515   0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
1516   0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
1517   0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
1518   0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
1519   0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
1520   0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
1521   0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
1522   0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
1523   0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
1524   0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
1525   0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
1526   0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
1527   0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
1528   0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
1529   0x2d02ef8dL
1530 };
1531 #endif
1532
1533
1534 /* ========================================================================= */
1535 #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
1536 #define DO2(buf)  DO1(buf); DO1(buf);
1537 #define DO4(buf)  DO2(buf); DO2(buf);
1538 #define DO8(buf)  DO4(buf); DO4(buf);
1539
1540 /* infblock.h -- header to use infblock.c
1541  * Copyright (C) 1995-1998 Mark Adler
1542  * For conditions of distribution and use, see copyright notice in zlib.h 
1543  */
1544
1545 /* WARNING: this file should *not* be used by applications. It is
1546    part of the implementation of the compression library and is
1547    subject to change. Applications should only use zlib.h.
1548  */
1549
1550 struct inflate_blocks_state;
1551 typedef struct inflate_blocks_state inflate_blocks_statef;
1552
1553 extern inflate_blocks_statef * inflate_blocks_new OF((
1554     z_streamp z,
1555     check_func c,               /* check function */
1556     uInt w));                   /* window size */
1557
1558 extern int inflate_blocks OF((
1559     inflate_blocks_statef *,
1560     z_streamp ,
1561     int));                      /* initial return code */
1562
1563 extern void inflate_blocks_reset OF((
1564     inflate_blocks_statef *,
1565     z_streamp ,
1566     uLong *));                  /* check value on output */
1567
1568 extern int inflate_blocks_free OF((
1569     inflate_blocks_statef *,
1570     z_streamp));
1571
1572 extern void inflate_set_dictionary OF((
1573     inflate_blocks_statef *s,
1574     const Byte *d,  /* dictionary */
1575     uInt  n));       /* dictionary length */
1576
1577 extern int inflate_blocks_sync_point OF((
1578     inflate_blocks_statef *s));
1579
1580 /* simplify the use of the inflate_huft type with some defines */
1581 #define exop word.what.Exop
1582 #define bits word.what.Bits
1583
1584 /* Table for deflate from PKZIP's appnote.txt. */
1585 static const uInt border[] = { /* Order of the bit length code lengths */
1586         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
1587
1588 /* inftrees.h -- header to use inftrees.c
1589  * Copyright (C) 1995-1998 Mark Adler
1590  * For conditions of distribution and use, see copyright notice in zlib.h 
1591  */
1592
1593 /* WARNING: this file should *not* be used by applications. It is
1594    part of the implementation of the compression library and is
1595    subject to change. Applications should only use zlib.h.
1596  */
1597
1598 /* Huffman code lookup table entry--this entry is four bytes for machines
1599    that have 16-bit pointers (e.g. PC's in the small or medium model). */
1600
1601 typedef struct inflate_huft_s inflate_huft;
1602
1603 struct inflate_huft_s {
1604   union {
1605     struct {
1606       Byte Exop;        /* number of extra bits or operation */
1607       Byte Bits;        /* number of bits in this code or subcode */
1608     } what;
1609     uInt pad;           /* pad structure to a power of 2 (4 bytes for */
1610   } word;               /*  16-bit, 8 bytes for 32-bit int's) */
1611   uInt base;            /* literal, length base, distance base,
1612                            or table offset */
1613 };
1614
1615 /* Maximum size of dynamic tree.  The maximum found in a long but non-
1616    exhaustive search was 1004 huft structures (850 for length/literals
1617    and 154 for distances, the latter actually the result of an
1618    exhaustive search).  The actual maximum is not known, but the
1619    value below is more than safe. */
1620 #define MANY 1440
1621
1622 extern int inflate_trees_bits OF((
1623     uInt *,                    /* 19 code lengths */
1624     uInt *,                    /* bits tree desired/actual depth */
1625     inflate_huft * *,       /* bits tree result */
1626     inflate_huft *,             /* space for trees */
1627     z_streamp));                /* for messages */
1628
1629 extern int inflate_trees_dynamic OF((
1630     uInt,                       /* number of literal/length codes */
1631     uInt,                       /* number of distance codes */
1632     uInt *,                    /* that many (total) code lengths */
1633     uInt *,                    /* literal desired/actual bit depth */
1634     uInt *,                    /* distance desired/actual bit depth */
1635     inflate_huft * *,       /* literal/length tree result */
1636     inflate_huft * *,       /* distance tree result */
1637     inflate_huft *,             /* space for trees */
1638     z_streamp));                /* for messages */
1639
1640 extern int inflate_trees_fixed OF((
1641     uInt *,                    /* literal desired/actual bit depth */
1642     uInt *,                    /* distance desired/actual bit depth */
1643     inflate_huft * *,       /* literal/length tree result */
1644     inflate_huft * *,       /* distance tree result */
1645     z_streamp));                /* for memory allocation */
1646
1647
1648 /* infcodes.h -- header to use infcodes.c
1649  * Copyright (C) 1995-1998 Mark Adler
1650  * For conditions of distribution and use, see copyright notice in zlib.h 
1651  */
1652
1653 /* WARNING: this file should *not* be used by applications. It is
1654    part of the implementation of the compression library and is
1655    subject to change. Applications should only use zlib.h.
1656  */
1657
1658 struct inflate_codes_state;
1659 typedef struct inflate_codes_state inflate_codes_statef;
1660
1661 extern inflate_codes_statef *inflate_codes_new OF((
1662     uInt, uInt,
1663     inflate_huft *, inflate_huft *,
1664     z_streamp ));
1665
1666 extern int inflate_codes OF((
1667     inflate_blocks_statef *,
1668     z_streamp ,
1669     int));
1670
1671 extern void inflate_codes_free OF((
1672     inflate_codes_statef *,
1673     z_streamp ));
1674
1675 /* infutil.h -- types and macros common to blocks and codes
1676  * Copyright (C) 1995-1998 Mark Adler
1677  * For conditions of distribution and use, see copyright notice in zlib.h 
1678  */
1679
1680 /* WARNING: this file should *not* be used by applications. It is
1681    part of the implementation of the compression library and is
1682    subject to change. Applications should only use zlib.h.
1683  */
1684
1685 #ifndef _INFUTIL_H
1686 #define _INFUTIL_H
1687
1688 typedef enum {
1689       TYPE,     /* get type bits (3, including end bit) */
1690       LENS,     /* get lengths for stored */
1691       STORED,   /* processing stored block */
1692       TABLE,    /* get table lengths */
1693       BTREE,    /* get bit lengths tree for a dynamic block */
1694       DTREE,    /* get length, distance trees for a dynamic block */
1695       CODES,    /* processing fixed or dynamic block */
1696       DRY,      /* output remaining window bytes */
1697       DONE,     /* finished last block, done */
1698       BAD}      /* got a data error--stuck here */
1699 inflate_block_mode;
1700
1701 /* inflate blocks semi-private state */
1702 struct inflate_blocks_state {
1703
1704   /* mode */
1705   inflate_block_mode  mode;     /* current inflate_block mode */
1706
1707   /* mode dependent information */
1708   union {
1709     uInt left;          /* if STORED, bytes left to copy */
1710     struct {
1711       uInt table;               /* table lengths (14 bits) */
1712       uInt index;               /* index into blens (or border) */
1713       uInt *blens;             /* bit lengths of codes */
1714       uInt bb;                  /* bit length tree depth */
1715       inflate_huft *tb;         /* bit length decoding tree */
1716     } trees;            /* if DTREE, decoding info for trees */
1717     struct {
1718       inflate_codes_statef 
1719          *codes;
1720     } decode;           /* if CODES, current state */
1721   } sub;                /* submode */
1722   uInt last;            /* true if this block is the last block */
1723
1724   /* mode independent information */
1725   uInt bitk;            /* bits in bit buffer */
1726   uLong bitb;           /* bit buffer */
1727   inflate_huft *hufts;  /* single safe_malloc for tree space */
1728   Byte *window;        /* sliding window */
1729   Byte *end;           /* one byte after sliding window */
1730   Byte *read;          /* window read pointer */
1731   Byte *write;         /* window write pointer */
1732   check_func checkfn;   /* check function */
1733   uLong check;          /* check on output */
1734
1735 };
1736
1737
1738 /* defines for inflate input/output */
1739 /*   update pointers and return */
1740 #define UPDBITS {s->bitb=b;s->bitk=k;}
1741 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
1742 #define UPDOUT {s->write=q;}
1743 #define UPDATE {UPDBITS UPDIN UPDOUT}
1744 #define LEAVE {UPDATE return inflate_flush(s,z,r);}
1745 /*   get bytes and bits */
1746 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
1747 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
1748 #define NEXTBYTE (n--,*p++)
1749 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
1750 #define DUMPBITS(j) {b>>=(j);k-=(j);}
1751 /*   output bytes */
1752 #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
1753 #define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
1754 #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
1755 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
1756 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
1757 #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
1758 /*   load static pointers */
1759 #define LOAD {LOADIN LOADOUT}
1760
1761 /* masks for lower bits (size given to avoid silly warnings with Visual C++) */
1762 extern uInt inflate_mask[17];
1763
1764 /* copy as much as possible from the sliding window to the output area */
1765 extern int inflate_flush OF((
1766     inflate_blocks_statef *,
1767     z_streamp ,
1768     int));
1769
1770 #endif
1771
1772                                                                 
1773 /*
1774    Notes beyond the 1.93a appnote.txt:
1775
1776    1. Distance pointers never point before the beginning of the output
1777       stream.
1778    2. Distance pointers can point back across blocks, up to 32k away.
1779    3. There is an implied maximum of 7 bits for the bit length table and
1780       15 bits for the actual data.
1781    4. If only one code exists, then it is encoded using one bit.  (Zero
1782       would be more efficient, but perhaps a little confusing.)  If two
1783       codes exist, they are coded using one bit each (0 and 1).
1784    5. There is no way of sending zero distance codes--a dummy must be
1785       sent if there are none.  (History: a pre 2.0 version of PKZIP would
1786       store blocks with no distance codes, but this was discovered to be
1787       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
1788       zero distance codes, which is sent as one code of zero bits in
1789       length.
1790    6. There are up to 286 literal/length codes.  Code 256 represents the
1791       end-of-block.  Note however that the static length tree defines
1792       288 codes just to fill out the Huffman codes.  Codes 286 and 287
1793       cannot be used though, since there is no length base or extra bits
1794       defined for them.  Similarily, there are up to 30 distance codes.
1795       However, static trees define 32 codes (all 5 bits) to fill out the
1796       Huffman codes, but the last two had better not show up in the data.
1797    7. Unzip can check dynamic Huffman blocks for complete code sets.
1798       The exception is that a single code would not be complete (see #4).
1799    8. The five bits following the block type is really the number of
1800       literal codes sent minus 257.
1801    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
1802       (1+6+6).  Therefore, to output three times the length, you output
1803       three codes (1+1+1), whereas to output four times the same length,
1804       you only need two codes (1+3).  Hmm.
1805   10. In the tree reconstruction algorithm, Code = Code + Increment
1806       only if BitLength(i) is not zero.  (Pretty obvious.)
1807   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
1808   12. Note: length code 284 can represent 227-258, but length code 285
1809       really is 258.  The last length deserves its own, short code
1810       since it gets used a lot in very redundant files.  The length
1811       258 is special since 258 - 3 (the min match length) is 255.
1812   13. The literal/length and distance code bit lengths are read as a
1813       single stream of lengths.  It is possible (and advantageous) for
1814       a repeat code (16, 17, or 18) to go across the boundary between
1815       the two sets of lengths.
1816  */
1817
1818 /* And'ing with mask[n] masks the lower n bits */
1819 uInt inflate_mask[17] = {
1820     0x0000,
1821     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
1822     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
1823 };
1824
1825 /*
1826   If you use the zlib library in a product, an acknowledgment is welcome
1827   in the documentation of your product. If for some reason you cannot
1828   include such an acknowledgment, I would appreciate that you keep this
1829   copyright string in the executable of your product.
1830  */
1831
1832 /* simplify the use of the inflate_huft type with some defines */
1833 #define exop word.what.Exop
1834 #define bits word.what.Bits
1835
1836
1837 static int huft_build OF((
1838     uInt *,                             /* code lengths in bits */
1839     uInt,               /* number of codes */
1840     uInt,               /* number of "simple" codes */
1841     const uInt *,               /* list of base values for non-simple codes */
1842     const uInt *,               /* list of extra bits for non-simple codes */
1843     inflate_huft **,    /* result: starting table */
1844     uInt *,                             /* maximum lookup bits (returns actual) */
1845     inflate_huft *,     /* space for trees */
1846     uInt *,             /* hufts used in space */
1847     uInt * ));                  /* space for values */
1848
1849 /* Tables for deflate from PKZIP's appnote.txt. */
1850 static const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
1851         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1852         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1853         /* see note #13 above about 258 */
1854 static const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
1855         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1856         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
1857 static const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
1858         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1859         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1860         8193, 12289, 16385, 24577};
1861 static const uInt cpdext[30] = { /* Extra bits for distance codes */
1862         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1863         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1864         12, 12, 13, 13};
1865
1866 /*
1867    Huffman code decoding is performed using a multi-level table lookup.
1868    The fastest way to decode is to simply build a lookup table whose
1869    size is determined by the longest code.  However, the time it takes
1870    to build this table can also be a factor if the data being decoded
1871    is not very long.  The most common codes are necessarily the
1872    shortest codes, so those codes dominate the decoding time, and hence
1873    the speed.  The idea is you can have a shorter table that decodes the
1874    shorter, more probable codes, and then point to subsidiary tables for
1875    the longer codes.  The time it costs to decode the longer codes is
1876    then traded against the time it takes to make longer tables.
1877
1878    This results of this trade are in the variables lbits and dbits
1879    below.  lbits is the number of bits the first level table for literal/
1880    length codes can decode in one step, and dbits is the same thing for
1881    the distance codes.  Subsequent tables are also less than or equal to
1882    those sizes.  These values may be adjusted either when all of the
1883    codes are shorter than that, in which case the longest code length in
1884    bits is used, or when the shortest code is *longer* than the requested
1885    table size, in which case the length of the shortest code in bits is
1886    used.
1887
1888    There are two different values for the two tables, since they code a
1889    different number of possibilities each.  The literal/length table
1890    codes 286 possible values, or in a flat code, a little over eight
1891    bits.  The distance table codes 30 possible values, or a little less
1892    than five bits, flat.  The optimum values for speed end up being
1893    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1894    The optimum values may differ though from machine to machine, and
1895    possibly even between compilers.  Your mileage may vary.
1896  */
1897
1898
1899 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
1900 #define BMAX 15         /* maximum bit length of any code */
1901
1902 static int huft_build(uInt *b, uInt n, uInt s, const uInt *d, const uInt *e, inflate_huft ** t, uInt *m, inflate_huft *hp, uInt *hn, uInt *v)
1903 //uInt *b;               /* code lengths in bits (all assumed <= BMAX) */
1904 //uInt n;                 /* number of codes (assumed <= 288) */
1905 //uInt s;                 /* number of simple-valued codes (0..s-1) */
1906 //const uInt *d;         /* list of base values for non-simple codes */
1907 //const uInt *e;         /* list of extra bits for non-simple codes */
1908 //inflate_huft ** t;            /* result: starting table */
1909 //uInt *m;               /* maximum lookup bits, returns actual */
1910 //inflate_huft *hp;       /* space for trees */
1911 //uInt *hn;               /* hufts used in space */
1912 //uInt *v;               /* working area: values in order of bit length */
1913 /* Given a list of code lengths and a maximum table size, make a set of
1914    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
1915    if the given code set is incomplete (the tables are still built in this
1916    case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
1917    lengths), or Z_MEM_ERROR if not enough memory. */
1918 {
1919
1920   uInt a;                       /* counter for codes of length k */
1921   uInt c[BMAX+1];               /* bit length count table */
1922   uInt f;                       /* i repeats in table every f entries */
1923   int g;                        /* maximum code length */
1924   int h;                        /* table level */
1925   register uInt i;              /* counter, current code */
1926   register uInt j;              /* counter */
1927   register int k;               /* number of bits in current code */
1928   int l;                        /* bits per table (returned in m) */
1929   uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
1930   register uInt *p;            /* pointer into c[], b[], or v[] */
1931   inflate_huft *q;              /* points to current table */
1932   struct inflate_huft_s r;      /* table entry for structure assignment */
1933   inflate_huft *u[BMAX];        /* table stack */
1934   register int w;               /* bits before this table == (l * h) */
1935   uInt x[BMAX+1];               /* bit offsets, then code stack */
1936   uInt *xp;                    /* pointer into x */
1937   int y;                        /* number of dummy codes added */
1938   uInt z;                       /* number of entries in current table */
1939
1940
1941   /* Generate counts for each bit length */
1942   p = c;
1943 #define C0 *p++ = 0;
1944 #define C2 C0 C0 C0 C0
1945 #define C4 C2 C2 C2 C2
1946   C4                            /* clear c[]--assume BMAX+1 is 16 */
1947   p = b;  i = n;
1948   do {
1949     c[*p++]++;                  /* assume all entries <= BMAX */
1950   } while (--i);
1951   if (c[0] == n)                /* null input--all zero length codes */
1952   {
1953     *t = (inflate_huft *)Z_NULL;
1954     *m = 0;
1955     return Z_OK;
1956   }
1957
1958
1959   /* Find minimum and maximum length, bound *m by those */
1960   l = *m;
1961   for (j = 1; j <= BMAX; j++)
1962     if (c[j])
1963       break;
1964   k = j;                        /* minimum code length */
1965   if ((uInt)l < j)
1966     l = j;
1967   for (i = BMAX; i; i--)
1968     if (c[i])
1969       break;
1970   g = i;                        /* maximum code length */
1971   if ((uInt)l > i)
1972     l = i;
1973   *m = l;
1974
1975
1976   /* Adjust last length count to fill out codes, if needed */
1977   for (y = 1 << j; j < i; j++, y <<= 1)
1978     if ((y -= c[j]) < 0)
1979       return Z_DATA_ERROR;
1980   if ((y -= c[i]) < 0)
1981     return Z_DATA_ERROR;
1982   c[i] += y;
1983
1984
1985   /* Generate starting offsets into the value table for each length */
1986   x[1] = j = 0;
1987   p = c + 1;  xp = x + 2;
1988   while (--i) {                 /* note that i == g from above */
1989     *xp++ = (j += *p++);
1990   }
1991
1992
1993   /* Make a table of values in order of bit lengths */
1994   p = b;  i = 0;
1995   do {
1996     if ((j = *p++) != 0)
1997       v[x[j]++] = i;
1998   } while (++i < n);
1999   n = x[g];                     /* set n to length of v */
2000
2001
2002   /* Generate the Huffman codes and for each, make the table entries */
2003   x[0] = i = 0;                 /* first Huffman code is zero */
2004   p = v;                        /* grab values in bit order */
2005   h = -1;                       /* no tables yet--level -1 */
2006   w = -l;                       /* bits decoded == (l * h) */
2007   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
2008   q = (inflate_huft *)Z_NULL;   /* ditto */
2009   z = 0;                        /* ditto */
2010
2011   /* go through the bit lengths (k already is bits in shortest code) */
2012   for (; k <= g; k++)
2013   {
2014     a = c[k];
2015     while (a--)
2016     {
2017       /* here i is the Huffman code of length k bits for value *p */
2018       /* make tables up to required level */
2019       while (k > w + l)
2020       {
2021         h++;
2022         w += l;                 /* previous table always l bits */
2023
2024         /* compute minimum size table less than or equal to l bits */
2025         z = g - w;
2026         z = z > (uInt)l ? (uInt)l : z;        /* table size upper limit */
2027         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
2028         {                       /* too few codes for k-w bit table */
2029           f -= a + 1;           /* deduct codes from patterns left */
2030           xp = c + k;
2031           if (j < z)
2032             while (++j < z)     /* try smaller tables up to z bits */
2033             {
2034               if ((f <<= 1) <= *++xp)
2035                 break;          /* enough codes to use up j bits */
2036               f -= *xp;         /* else deduct codes from patterns */
2037             }
2038         }
2039         z = 1 << j;             /* table entries for j-bit table */
2040
2041         /* allocate new table */
2042         if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
2043           return Z_MEM_ERROR;   /* not enough memory */
2044         u[h] = q = hp + *hn;
2045         *hn += z;
2046
2047         /* connect to last table, if there is one */
2048         if (h)
2049         {
2050           x[h] = i;             /* save pattern for backing up */
2051           r.bits = (Byte)l;     /* bits to dump before this table */
2052           r.exop = (Byte)j;     /* bits in this table */
2053           j = i >> (w - l);
2054           r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
2055           u[h-1][j] = r;        /* connect to last table */
2056         }
2057         else
2058           *t = q;               /* first table is returned result */
2059       }
2060
2061       /* set up table entry in r */
2062       r.bits = (Byte)(k - w);
2063       if (p >= v + n)
2064         r.exop = 128 + 64;      /* out of values--invalid code */
2065       else if (*p < s)
2066       {
2067         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
2068         r.base = *p++;          /* simple code is just the value */
2069       }
2070       else
2071       {
2072         r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
2073         r.base = d[*p++ - s];
2074       }
2075
2076       /* fill code-like entries with r */
2077       f = 1 << (k - w);
2078       for (j = i >> w; j < z; j += f)
2079         q[j] = r;
2080
2081       /* backwards increment the k-bit code i */
2082       for (j = 1 << (k - 1); i & j; j >>= 1)
2083         i ^= j;
2084       i ^= j;
2085
2086       /* backup over finished tables */
2087       mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
2088       while ((i & mask) != x[h])
2089       {
2090         h--;                    /* don't need to update q */
2091         w -= l;
2092         mask = (1 << w) - 1;
2093       }
2094     }
2095   }
2096
2097
2098   /* Return Z_BUF_ERROR if we were given an incomplete table */
2099   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
2100 }
2101
2102
2103 /* inffixed.h -- table for decoding fixed codes
2104  * Generated automatically by the maketree.c program
2105  */
2106
2107 /* WARNING: this file should *not* be used by applications. It is
2108    part of the implementation of the compression library and is
2109    subject to change. Applications should only use zlib.h.
2110  */
2111
2112 static uInt fixed_bl = 9;
2113 static uInt fixed_bd = 5;
2114 static inflate_huft fixed_tl[] = {
2115     {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
2116     {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
2117     {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
2118     {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
2119     {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
2120     {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
2121     {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
2122     {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
2123     {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
2124     {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
2125     {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
2126     {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
2127     {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
2128     {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
2129     {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
2130     {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
2131     {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
2132     {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
2133     {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
2134     {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
2135     {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
2136     {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
2137     {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
2138     {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
2139     {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
2140     {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
2141     {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
2142     {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
2143     {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
2144     {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
2145     {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
2146     {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
2147     {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
2148     {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
2149     {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
2150     {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
2151     {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
2152     {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
2153     {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
2154     {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
2155     {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
2156     {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
2157     {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
2158     {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
2159     {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
2160     {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
2161     {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
2162     {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
2163     {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
2164     {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
2165     {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
2166     {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
2167     {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
2168     {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
2169     {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
2170     {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
2171     {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
2172     {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
2173     {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
2174     {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
2175     {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
2176     {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
2177     {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
2178     {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
2179     {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
2180     {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
2181     {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
2182     {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
2183     {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
2184     {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
2185     {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
2186     {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
2187     {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
2188     {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
2189     {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
2190     {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
2191     {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
2192     {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
2193     {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
2194     {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
2195     {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
2196     {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
2197     {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
2198     {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
2199     {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
2200     {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
2201     {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
2202     {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
2203     {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
2204     {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
2205     {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
2206     {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
2207     {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
2208     {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
2209     {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
2210     {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
2211     {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
2212     {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
2213     {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
2214     {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
2215     {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
2216     {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
2217     {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
2218     {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
2219     {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
2220     {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
2221     {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
2222     {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
2223     {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
2224     {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
2225     {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
2226     {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
2227     {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
2228     {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
2229     {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
2230     {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
2231     {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
2232     {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
2233     {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
2234     {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
2235     {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
2236     {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
2237     {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
2238     {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
2239     {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
2240     {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
2241     {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
2242     {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
2243   };
2244 static inflate_huft fixed_td[] = {
2245     {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
2246     {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
2247     {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
2248     {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
2249     {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
2250     {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
2251     {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
2252     {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
2253   };
2254
2255 /* infcodes.c -- process literals and length/distance pairs
2256  * Copyright (C) 1995-1998 Mark Adler
2257  * For conditions of distribution and use, see copyright notice in zlib.h 
2258  */
2259
2260 /* simplify the use of the inflate_huft type with some defines */
2261 #define exop word.what.Exop
2262 #define bits word.what.Bits
2263
2264 typedef enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
2265       START,    /* x: set up for LEN */
2266       LEN,      /* i: get length/literal/eob next */
2267       LENEXT,   /* i: getting length extra (have base) */
2268       DIST,     /* i: get distance next */
2269       DISTEXT,  /* i: getting distance extra */
2270       COPY,     /* o: copying bytes in window, waiting for space */
2271       LIT,      /* o: got literal, waiting for output space */
2272       WASH,     /* o: got eob, possibly still output waiting */
2273       END,      /* x: got eob and all data flushed */
2274       BADCODE}  /* x: got error */
2275 inflate_codes_mode;
2276
2277 /* inflate codes private state */
2278 struct inflate_codes_state {
2279
2280   /* mode */
2281   inflate_codes_mode mode;      /* current inflate_codes mode */
2282
2283   /* mode dependent information */
2284   uInt len;
2285   union {
2286     struct {
2287       inflate_huft *tree;       /* pointer into tree */
2288       uInt need;                /* bits needed */
2289     } code;             /* if LEN or DIST, where in tree */
2290     uInt lit;           /* if LIT, literal */
2291     struct {
2292       uInt get;                 /* bits to get for extra */
2293       uInt dist;                /* distance back to copy from */
2294     } copy;             /* if EXT or COPY, where and how much */
2295   } sub;                /* submode */
2296
2297   /* mode independent information */
2298   Byte lbits;           /* ltree bits decoded per branch */
2299   Byte dbits;           /* dtree bits decoder per branch */
2300   inflate_huft *ltree;          /* literal/length/eob tree */
2301   inflate_huft *dtree;          /* distance tree */
2302
2303 };
2304
2305 /* infblock.h -- header to use infblock.c
2306  * Copyright (C) 1995-1998 Mark Adler
2307  * For conditions of distribution and use, see copyright notice in zlib.h 
2308  */
2309
2310 /* WARNING: this file should *not* be used by applications. It is
2311    part of the implementation of the compression library and is
2312    subject to change. Applications should only use zlib.h.
2313  */
2314
2315 extern inflate_blocks_statef * inflate_blocks_new OF((
2316     z_streamp z,
2317     check_func c,               /* check function */
2318     uInt w));                   /* window size */
2319
2320 extern int inflate_blocks OF((
2321     inflate_blocks_statef *,
2322     z_streamp ,
2323     int));                      /* initial return code */
2324
2325 extern void inflate_blocks_reset OF((
2326     inflate_blocks_statef *,
2327     z_streamp ,
2328     uLong *));                  /* check value on output */
2329
2330 extern int inflate_blocks_free OF((
2331     inflate_blocks_statef *,
2332     z_streamp));
2333
2334 extern void inflate_set_dictionary OF((
2335     inflate_blocks_statef *s,
2336     const Byte *d,  /* dictionary */
2337     uInt  n));       /* dictionary length */
2338
2339 extern int inflate_blocks_sync_point OF((
2340     inflate_blocks_statef *s));
2341
2342 typedef enum {
2343       imMETHOD,   /* waiting for method byte */
2344       imFLAG,     /* waiting for flag byte */
2345       imDICT4,    /* four dictionary check bytes to go */
2346       imDICT3,    /* three dictionary check bytes to go */
2347       imDICT2,    /* two dictionary check bytes to go */
2348       imDICT1,    /* one dictionary check byte to go */
2349       imDICT0,    /* waiting for inflateSetDictionary */
2350       imBLOCKS,   /* decompressing blocks */
2351       imCHECK4,   /* four check bytes to go */
2352       imCHECK3,   /* three check bytes to go */
2353       imCHECK2,   /* two check bytes to go */
2354       imCHECK1,   /* one check byte to go */
2355       imDONE,     /* finished check, done */
2356       imBAD}      /* got an error--stay here */
2357 inflate_mode;
2358
2359 /* inflate private state */
2360 struct internal_state {
2361
2362   /* mode */
2363   inflate_mode  mode;   /* current inflate mode */
2364
2365   /* mode dependent information */
2366   union {
2367     uInt method;        /* if FLAGS, method byte */
2368     struct {
2369       uLong was;                /* computed check value */
2370       uLong need;               /* stream check value */
2371     } check;            /* if CHECK, check values to compare */
2372     uInt marker;        /* if BAD, inflateSync's marker bytes count */
2373   } sub;        /* submode */
2374
2375   /* mode independent information */
2376   int  nowrap;          /* flag for no wrapper */
2377   uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
2378   inflate_blocks_statef 
2379     *blocks;            /* current inflate_blocks state */
2380
2381 };