NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
unzip.cpp
Go to the documentation of this file.
1#include <windows.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <tchar.h>
6#include "unzip.h"
7
8// THIS FILE is almost entirely based upon code by Jean-loup Gailly
9// and Mark Adler. It has been modified by Lucian Wischik.
10// The modifications were: incorporate the bugfixes of 1.1.4, allow
11// unzipping to/from handles/pipes/files/memory, encryption, unicode,
12// a windowsish api, and putting everything into a single .cpp file.
13// The original code may be found at http://www.gzip.org/zlib/
14// The original copyright text follows.
15//
16//
17//
18// zlib.h -- interface of the 'zlib' general purpose compression library
19// version 1.1.3, July 9th, 1998
20//
21// Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
22//
23// This software is provided 'as-is', without any express or implied
24// warranty. In no event will the authors be held liable for any damages
25// arising from the use of this software.
26//
27// Permission is granted to anyone to use this software for any purpose,
28// including commercial applications, and to alter it and redistribute it
29// freely, subject to the following restrictions:
30//
31// 1. The origin of this software must not be misrepresented; you must not
32// claim that you wrote the original software. If you use this software
33// in a product, an acknowledgment in the product documentation would be
34// appreciated but is not required.
35// 2. Altered source versions must be plainly marked as such, and must not be
36// misrepresented as being the original software.
37// 3. This notice may not be removed or altered from any source distribution.
38//
39// Jean-loup Gailly Mark Adler
40// jloup@gzip.org madler@alumni.caltech.edu
41//
42//
43// The data format used by the zlib library is described by RFCs (Request for
44// Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
45// (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
46//
47//
48// The 'zlib' compression library provides in-memory compression and
49// decompression functions, including integrity checks of the uncompressed
50// data. This version of the library supports only one compression method
51// (deflation) but other algorithms will be added later and will have the same
52// stream interface.
53//
54// Compression can be done in a single step if the buffers are large
55// enough (for example if an input file is mmap'ed), or can be done by
56// repeated calls of the compression function. In the latter case, the
57// application must provide more input and/or consume the output
58// (providing more output space) before each call.
59//
60// The library also supports reading and writing files in gzip (.gz) format
61// with an interface similar to that of stdio.
62//
63// The library does not install any signal handler. The decoder checks
64// the consistency of the compressed data, so the library should never
65// crash even in case of corrupted input.
66//
67// for more info about .ZIP format, see ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip
68// PkWare has also a specification at ftp://ftp.pkware.com/probdesc.zip
69
70#define ZIP_HANDLE 1
71#define ZIP_FILENAME 2
72#define ZIP_MEMORY 3
73
74
75#define zmalloc(len) malloc(len)
76
77#define zfree(p) free(p)
78
79/*
80void *zmalloc(unsigned int len)
81{ char *buf = new char[len+32];
82 for (int i=0; i<16; i++)
83 { buf[i]=i;
84 buf[len+31-i]=i;
85 }
86 *((unsigned int*)buf) = len;
87 char c[1000]; wsprintf(c,"malloc 0x%lx - %lu",buf+16,len);
88 OutputDebugString(c);
89 return buf+16;
90}
91
92void zfree(void *buf)
93{ char c[1000]; wsprintf(c,"free 0x%lx",buf);
94 OutputDebugString(c);
95 char *p = ((char*)buf)-16;
96 unsigned int len = *((unsigned int*)p);
97 bool blown=false;
98 for (int i=0; i<16; i++)
99 { char lo = p[i];
100 char hi = p[len+31-i];
101 if (hi!=i || (lo!=i && i>4)) blown=true;
102 }
103 if (blown)
104 { OutputDebugString("BLOWN!!!");
105 }
106 delete[] p;
107}
108*/
109
110
111typedef struct tm_unz_s
112{
113 unsigned int tm_sec; // seconds after the minute - [0,59]
114 unsigned int tm_min; // minutes after the hour - [0,59]
115 unsigned int tm_hour; // hours since midnight - [0,23]
116 unsigned int tm_mday; // day of the month - [1,31]
117 unsigned int tm_mon; // months since January - [0,11]
118 unsigned int tm_year; // years - [1980..2044]
120
121
122// unz_global_info structure contain global data about the ZIPfile
123typedef struct unz_global_info_s
124{
125 unsigned long number_entry; // total number of entries in the central dir on this disk
126 unsigned long size_comment; // size of the global comment of the zipfile
128
129// unz_file_info contain information about a file in the zipfile
130typedef struct unz_file_info_s
131{
132 unsigned long version; // version made by 2 bytes
133 unsigned long version_needed; // version needed to extract 2 bytes
134 unsigned long flag; // general purpose bit flag 2 bytes
135 unsigned long compression_method; // compression method 2 bytes
136 unsigned long dosDate; // last mod file date in Dos fmt 4 bytes
137 unsigned long crc; // crc-32 4 bytes
138 unsigned long compressed_size; // compressed size 4 bytes
139 unsigned long uncompressed_size; // uncompressed size 4 bytes
140 unsigned long size_filename; // filename length 2 bytes
141 unsigned long size_file_extra; // extra field length 2 bytes
142 unsigned long size_file_comment; // file comment length 2 bytes
143 unsigned long disk_num_start; // disk number start 2 bytes
144 unsigned long internal_fa; // internal file attributes 2 bytes
145 unsigned long external_fa; // external file attributes 4 bytes
148
149
150#define UNZ_OK (0)
151#define UNZ_END_OF_LIST_OF_FILE (-100)
152#define UNZ_ERRNO (Z_ERRNO)
153#define UNZ_EOF (0)
154#define UNZ_PARAMERROR (-102)
155#define UNZ_BADZIPFILE (-103)
156#define UNZ_INTERNALERROR (-104)
157#define UNZ_CRCERROR (-105)
158#define UNZ_PASSWORD (-106)
159
160
161
162
163
164
165
166#define ZLIB_VERSION "1.1.3"
167
168
169// Allowed flush values; see deflate() for details
170#define Z_NO_FLUSH 0
171#define Z_SYNC_FLUSH 2
172#define Z_FULL_FLUSH 3
173#define Z_FINISH 4
174
175
176// compression levels
177#define Z_NO_COMPRESSION 0
178#define Z_BEST_SPEED 1
179#define Z_BEST_COMPRESSION 9
180#define Z_DEFAULT_COMPRESSION (-1)
181
182// compression strategy; see deflateInit2() for details
183#define Z_FILTERED 1
184#define Z_HUFFMAN_ONLY 2
185#define Z_DEFAULT_STRATEGY 0
186
187// Possible values of the data_type field
188#define Z_BINARY 0
189#define Z_ASCII 1
190#define Z_UNKNOWN 2
191
192// The deflate compression method (the only one supported in this version)
193#define Z_DEFLATED 8
194
195// for initializing zalloc, zfree, opaque
196#define Z_NULL 0
197
198// case sensitivity when searching for filenames
199#define CASE_SENSITIVE 1
200#define CASE_INSENSITIVE 2
201
202
203// Return codes for the compression/decompression functions. Negative
204// values are errors, positive values are used for special but normal events.
205#define Z_OK 0
206#define Z_STREAM_END 1
207#define Z_NEED_DICT 2
208#define Z_ERRNO (-1)
209#define Z_STREAM_ERROR (-2)
210#define Z_DATA_ERROR (-3)
211#define Z_MEM_ERROR (-4)
212#define Z_BUF_ERROR (-5)
213#define Z_VERSION_ERROR (-6)
214
215
216
217// Basic data types
218typedef unsigned char Byte; // 8 bits
219typedef unsigned int uInt; // 16 bits or more
220typedef unsigned long uLong; // 32 bits or more
221typedef void* voidpf;
222typedef void* voidp;
223typedef long z_off_t;
224
225
226
227
228
229
230
231
232
233
234
235
236typedef voidpf (*alloc_func) (voidpf opaque, uInt items, uInt size);
237typedef void (*free_func) (voidpf opaque, voidpf address);
238
239struct internal_state;
240
241typedef struct z_stream_s
242{
243 Byte* next_in; // next input byte
244 uInt avail_in; // number of bytes available at next_in
245 uLong total_in; // total nb of input bytes read so far
246
247 Byte* next_out; // next output byte should be put there
248 uInt avail_out; // remaining free space at next_out
249 uLong total_out; // total nb of bytes output so far
250
251 char* msg; // last error message, NULL if no error
252 struct internal_state* state; // not visible by applications
253
254 alloc_func zalloc; // used to allocate the internal state
255 free_func zfree; // used to free the internal state
256 voidpf opaque; // private data object passed to zalloc and zfree
257
258 int data_type; // best guess about the data type: ascii or binary
259 uLong adler; // adler32 value of the uncompressed data
260 uLong reserved; // reserved for future use
262
264
265
266// The application must update next_in and avail_in when avail_in has
267// dropped to zero. It must update next_out and avail_out when avail_out
268// has dropped to zero. The application must initialize zalloc, zfree and
269// opaque before calling the init function. All other fields are set by the
270// compression library and must not be updated by the application.
271//
272// The opaque value provided by the application will be passed as the first
273// parameter for calls of zalloc and zfree. This can be useful for custom
274// memory management. The compression library attaches no meaning to the
275// opaque value.
276//
277// zalloc must return Z_NULL if there is not enough memory for the object.
278// If zlib is used in a multi-threaded application, zalloc and zfree must be
279// thread safe.
280//
281// The fields total_in and total_out can be used for statistics or
282// progress reports. After compression, total_in holds the total size of
283// the uncompressed data and may be saved for use in the decompressor
284// (particularly if the decompressor wants to decompress everything in
285// a single step).
286//
287
288
289// basic functions
290
291const char* zlibVersion ();
292// The application can compare zlibVersion and ZLIB_VERSION for consistency.
293// If the first character differs, the library code actually used is
294// not compatible with the zlib.h header file used by the application.
295// This check is automatically made by inflateInit.
296
297
298
299
300
301
302int inflate (z_streamp strm, int flush);
303//
304// inflate decompresses as much data as possible, and stops when the input
305// buffer becomes empty or the output buffer becomes full. It may some
306// introduce some output latency (reading input without producing any output)
307// except when forced to flush.
308//
309// The detailed semantics are as follows. inflate performs one or both of the
310// following actions:
311//
312// - Decompress more input starting at next_in and update next_in and avail_in
313// accordingly. If not all input can be processed (because there is not
314// enough room in the output buffer), next_in is updated and processing
315// will resume at this point for the next call of inflate().
316//
317// - Provide more output starting at next_out and update next_out and avail_out
318// accordingly. inflate() provides as much output as possible, until there
319// is no more input data or no more space in the output buffer (see below
320// about the flush parameter).
321//
322// Before the call of inflate(), the application should ensure that at least
323// one of the actions is possible, by providing more input and/or consuming
324// more output, and updating the next_* and avail_* values accordingly.
325// The application can consume the uncompressed output when it wants, for
326// example when the output buffer is full (avail_out == 0), or after each
327// call of inflate(). If inflate returns Z_OK and with zero avail_out, it
328// must be called again after making room in the output buffer because there
329// might be more output pending.
330//
331// If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
332// output as possible to the output buffer. The flushing behavior of inflate is
333// not specified for values of the flush parameter other than Z_SYNC_FLUSH
334// and Z_FINISH, but the current implementation actually flushes as much output
335// as possible anyway.
336//
337// inflate() should normally be called until it returns Z_STREAM_END or an
338// error. However if all decompression is to be performed in a single step
339// (a single call of inflate), the parameter flush should be set to
340// Z_FINISH. In this case all pending input is processed and all pending
341// output is flushed; avail_out must be large enough to hold all the
342// uncompressed data. (The size of the uncompressed data may have been saved
343// by the compressor for this purpose.) The next operation on this stream must
344// be inflateEnd to deallocate the decompression state. The use of Z_FINISH
345// is never required, but can be used to inform inflate that a faster routine
346// may be used for the single inflate() call.
347//
348// If a preset dictionary is needed at this point (see inflateSetDictionary
349// below), inflate sets strm-adler to the adler32 checksum of the
350// dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
351// it sets strm->adler to the adler32 checksum of all output produced
352// so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
353// an error code as described below. At the end of the stream, inflate()
354// checks that its computed adler32 checksum is equal to that saved by the
355// compressor and returns Z_STREAM_END only if the checksum is correct.
356//
357// inflate() returns Z_OK if some progress has been made (more input processed
358// or more output produced), Z_STREAM_END if the end of the compressed data has
359// been reached and all uncompressed output has been produced, Z_NEED_DICT if a
360// preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
361// corrupted (input stream not conforming to the zlib format or incorrect
362// adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
363// (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
364// enough memory, Z_BUF_ERROR if no progress is possible or if there was not
365// enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
366// case, the application may then call inflateSync to look for a good
367// compression block.
368//
369
370
371int inflateEnd (z_streamp strm);
372//
373// All dynamically allocated data structures for this stream are freed.
374// This function discards any unprocessed input and does not flush any
375// pending output.
376//
377// inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
378// was inconsistent. In the error case, msg may be set but then points to a
379// static string (which must not be deallocated).
380
381// Advanced functions
382
383// The following functions are needed only in some special applications.
384
385
386
387
388
390 const Byte* dictionary,
391 uInt dictLength);
392//
393// Initializes the decompression dictionary from the given uncompressed byte
394// sequence. This function must be called immediately after a call of inflate
395// if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
396// can be determined from the Adler32 value returned by this call of
397// inflate. The compressor and decompressor must use exactly the same
398// dictionary.
399//
400// inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
401// parameter is invalid (such as NULL dictionary) or the stream state is
402// inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
403// expected one (incorrect Adler32 value). inflateSetDictionary does not
404// perform any decompression: this will be done by subsequent calls of
405// inflate().
406
407
409//
410// Skips invalid compressed data until a full flush point can be found, or until all
411// available input is skipped. No output is provided.
412//
413// inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
414// if no more input was provided, Z_DATA_ERROR if no flush point has been found,
415// or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
416// case, the application may save the current current value of total_in which
417// indicates where valid compressed data was found. In the error case, the
418// application may repeatedly call inflateSync, providing more input each time,
419// until success or end of the input data.
420
421
422int inflateReset (z_streamp strm);
423// This function is equivalent to inflateEnd followed by inflateInit,
424// but does not free and reallocate all the internal decompression state.
425// The stream will keep attributes that may have been set by inflateInit2.
426//
427// inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
428// stream state was inconsistent (such as zalloc or state being NULL).
429//
430
431
432
433// checksum functions
434// These functions are not related to compression but are exported
435// anyway because they might be useful in applications using the
436// compression library.
437
438uLong adler32 (uLong adler, const Byte* buf, uInt len);
439// Update a running Adler-32 checksum with the bytes buf[0..len-1] and
440// return the updated checksum. If buf is NULL, this function returns
441// the required initial value for the checksum.
442// An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
443// much faster. Usage example:
444//
445// uLong adler = adler32(0L, Z_NULL, 0);
446//
447// while (read_buffer(buffer, length) != EOF) {
448// adler = adler32(adler, buffer, length);
449// }
450// if (adler != original_adler) error();
451
452uLong ucrc32 (uLong crc, const Byte* buf, uInt len);
453// Update a running crc with the bytes buf[0..len-1] and return the updated
454// crc. If buf is NULL, this function returns the required initial value
455// for the crc. Pre- and post-conditioning (one's complement) is performed
456// within this function so it shouldn't be done by the application.
457// Usage example:
458//
459// uLong crc = crc32(0L, Z_NULL, 0);
460//
461// while (read_buffer(buffer, length) != EOF) {
462// crc = crc32(crc, buffer, length);
463// }
464// if (crc != original_crc) error();
465
466
467
468
469const char* zError (int err);
471const uLong* get_crc_table (void);
472
473
474
475typedef unsigned char uch;
476typedef uch uchf;
477typedef unsigned short ush;
478typedef ush ushf;
479typedef unsigned long ulg;
480
481
482
483const char* const z_errmsg[10] = // indexed by 2-zlib_error
484{
485 "need dictionary", // Z_NEED_DICT 2
486 "stream end", // Z_STREAM_END 1
487 "", // Z_OK 0
488 "file error", // Z_ERRNO (-1)
489 "stream error", // Z_STREAM_ERROR (-2)
490 "data error", // Z_DATA_ERROR (-3)
491 "insufficient memory", // Z_MEM_ERROR (-4)
492 "buffer error", // Z_BUF_ERROR (-5)
493 "incompatible version",// Z_VERSION_ERROR (-6)
494 ""
495};
496
497
498#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
499
500#define ERR_RETURN(strm,err) \
501 return (strm->msg = (char*)ERR_MSG(err), (err))
502// To be used only when the state is known to be valid
503
504// common constants
505
506
507#define STORED_BLOCK 0
508#define STATIC_TREES 1
509#define DYN_TREES 2
510// The three kinds of block type
511
512#define MIN_MATCH 3
513#define MAX_MATCH 258
514// The minimum and maximum match lengths
515
516#define PRESET_DICT 0x20 // preset dictionary flag in zlib header
517
518// target dependencies
519
520#define OS_CODE 0x0b // Window 95 & Windows NT
521
522
523
524// functions
525
526#define zmemzero(dest, len) memset(dest, 0, len)
527
528// Diagnostic functions
529#define LuAssert(cond,msg)
530#define LuTrace(x)
531#define LuTracev(x)
532#define LuTracevv(x)
533#define LuTracec(c,x)
534#define LuTracecv(c,x)
535
536
537typedef uLong (*check_func) (uLong check, const Byte* buf, uInt len);
538voidpf zcalloc (voidpf opaque, unsigned items, unsigned size);
539void zcfree (voidpf opaque, voidpf ptr);
540
541#define ZALLOC(strm, items, size) \
542 (*((strm)->zalloc))((strm)->opaque, (items), (size))
543#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
544
545//void ZFREE(z_streamp strm,voidpf addr)
546//{ *((strm)->zfree))((strm)->opaque, addr);
547//}
548
549#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
550
551
552
553
554// Huffman code lookup table entry--this entry is four bytes for machines
555// that have 16-bit pointers (e.g. PC's in the small or medium model).
556
557
559
561{
562 union
563 {
564 struct
565 {
566 Byte Exop; // number of extra bits or operation
567 Byte Bits; // number of bits in this code or subcode
569 uInt pad; // pad structure to a power of 2 (4 bytes for
570 } word; // 16-bit, 8 bytes for 32-bit int's)
571 uInt base; // literal, length base, distance base, or table offset
572};
573
574// Maximum size of dynamic tree. The maximum found in a long but non-
575// exhaustive search was 1004 huft structures (850 for length/literals
576// and 154 for distances, the latter actually the result of an
577// exhaustive search). The actual maximum is not known, but the
578// value below is more than safe.
579#define MANY 1440
580
582 uInt*, // 19 code lengths
583 uInt*, // bits tree desired/actual depth
584 inflate_huft **, // bits tree result
585 inflate_huft*, // space for trees
586 z_streamp); // for messages
587
589 uInt, // number of literal/length codes
590 uInt, // number of distance codes
591 uInt*, // that many (total) code lengths
592 uInt*, // literal desired/actual bit depth
593 uInt*, // distance desired/actual bit depth
594 inflate_huft **, // literal/length tree result
595 inflate_huft **, // distance tree result
596 inflate_huft*, // space for trees
597 z_streamp); // for messages
598
600 uInt*, // literal desired/actual bit depth
601 uInt*, // distance desired/actual bit depth
602 const inflate_huft **, // literal/length tree result
603 const inflate_huft **, // distance tree result
604 z_streamp); // for memory allocation
605
606
607
608
609
612
614 z_streamp z,
615 check_func c, // check function
616 uInt w); // window size
617
618int inflate_blocks (
620 z_streamp,
621 int); // initial return code
622
625 z_streamp,
626 uLong*); // check value on output
627
630 z_streamp);
631
634 const Byte* d, // dictionary
635 uInt n); // dictionary length
636
639
640
641
642
645
647 uInt, uInt,
648 const inflate_huft*, const inflate_huft*,
649 z_streamp );
650
651int inflate_codes (
653 z_streamp,
654 int);
655
658 z_streamp );
659
660
661
662
663typedef enum
664{
665 IBM_TYPE, // get type bits (3, including end bit)
666 IBM_LENS, // get lengths for stored
667 IBM_STORED, // processing stored block
668 IBM_TABLE, // get table lengths
669 IBM_BTREE, // get bit lengths tree for a dynamic block
670 IBM_DTREE, // get length, distance trees for a dynamic block
671 IBM_CODES, // processing fixed or dynamic block
672 IBM_DRY, // output remaining window bytes
673 IBM_DONE, // finished last block, done
674 IBM_BAD
675} // got a data error--stuck here
677
678// inflate blocks semi-private state
680{
681
682 // mode
683 inflate_block_mode mode; // current inflate_block mode
684
685 // mode dependent information
686 union
687 {
688 uInt left; // if STORED, bytes left to copy
689 struct
690 {
691 uInt table; // table lengths (14 bits)
692 uInt index; // index into blens (or border)
693 uInt* blens; // bit lengths of codes
694 uInt bb; // bit length tree depth
695 inflate_huft* tb; // bit length decoding tree
696 } trees; // if DTREE, decoding info for trees
697 struct
698 {
701 } decode; // if CODES, current state
702 } sub; // submode
703 uInt last; // true if this block is the last block
704
705 // mode independent information
706 uInt bitk; // bits in bit buffer
707 uLong bitb; // bit buffer
708 inflate_huft* hufts; // single malloc for tree space
709 Byte* window; // sliding window
710 Byte* end; // one byte after sliding window
711 Byte* read; // window read pointer
712 Byte* write; // window write pointer
713 check_func checkfn; // check function
714 uLong check; // check on output
715
716};
717
718
719// defines for inflate input/output
720// update pointers and return
721#define UPDBITS {s->bitb=b;s->bitk=k;}
722#define UPDIN {z->avail_in=n;z->total_in+=(uLong)(p-z->next_in);z->next_in=p;}
723#define UPDOUT {s->write=q;}
724#define UPDATE {UPDBITS UPDIN UPDOUT}
725#define LEAVE {UPDATE return inflate_flush(s,z,r);}
726// get bytes and bits
727#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
728#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
729#define NEXTBYTE (n--,*p++)
730#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
731#define DUMPBITS(j) {b>>=(j);k-=(j);}
732// output bytes
733#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
734#define LOADOUT {q=s->write;m=(uInt)WAVAIL;m;}
735#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
736#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
737#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
738#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
739// load local pointers
740#define LOAD {LOADIN LOADOUT}
741
742// masks for lower bits (size given to avoid silly warnings with Visual C++)
743// And'ing with mask[n] masks the lower n bits
745{
746 0x0000,
747 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
748 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
749};
750
751// copy as much as possible from the sliding window to the output area
753
755
756
757
758const uInt fixed_bl = 9;
759const uInt fixed_bd = 5;
761{
762 {{{96, 7}}, 256}, {{{0, 8}}, 80}, {{{0, 8}}, 16}, {{{84, 8}}, 115},
763 {{{82, 7}}, 31}, {{{0, 8}}, 112}, {{{0, 8}}, 48}, {{{0, 9}}, 192},
764 {{{80, 7}}, 10}, {{{0, 8}}, 96}, {{{0, 8}}, 32}, {{{0, 9}}, 160},
765 {{{0, 8}}, 0}, {{{0, 8}}, 128}, {{{0, 8}}, 64}, {{{0, 9}}, 224},
766 {{{80, 7}}, 6}, {{{0, 8}}, 88}, {{{0, 8}}, 24}, {{{0, 9}}, 144},
767 {{{83, 7}}, 59}, {{{0, 8}}, 120}, {{{0, 8}}, 56}, {{{0, 9}}, 208},
768 {{{81, 7}}, 17}, {{{0, 8}}, 104}, {{{0, 8}}, 40}, {{{0, 9}}, 176},
769 {{{0, 8}}, 8}, {{{0, 8}}, 136}, {{{0, 8}}, 72}, {{{0, 9}}, 240},
770 {{{80, 7}}, 4}, {{{0, 8}}, 84}, {{{0, 8}}, 20}, {{{85, 8}}, 227},
771 {{{83, 7}}, 43}, {{{0, 8}}, 116}, {{{0, 8}}, 52}, {{{0, 9}}, 200},
772 {{{81, 7}}, 13}, {{{0, 8}}, 100}, {{{0, 8}}, 36}, {{{0, 9}}, 168},
773 {{{0, 8}}, 4}, {{{0, 8}}, 132}, {{{0, 8}}, 68}, {{{0, 9}}, 232},
774 {{{80, 7}}, 8}, {{{0, 8}}, 92}, {{{0, 8}}, 28}, {{{0, 9}}, 152},
775 {{{84, 7}}, 83}, {{{0, 8}}, 124}, {{{0, 8}}, 60}, {{{0, 9}}, 216},
776 {{{82, 7}}, 23}, {{{0, 8}}, 108}, {{{0, 8}}, 44}, {{{0, 9}}, 184},
777 {{{0, 8}}, 12}, {{{0, 8}}, 140}, {{{0, 8}}, 76}, {{{0, 9}}, 248},
778 {{{80, 7}}, 3}, {{{0, 8}}, 82}, {{{0, 8}}, 18}, {{{85, 8}}, 163},
779 {{{83, 7}}, 35}, {{{0, 8}}, 114}, {{{0, 8}}, 50}, {{{0, 9}}, 196},
780 {{{81, 7}}, 11}, {{{0, 8}}, 98}, {{{0, 8}}, 34}, {{{0, 9}}, 164},
781 {{{0, 8}}, 2}, {{{0, 8}}, 130}, {{{0, 8}}, 66}, {{{0, 9}}, 228},
782 {{{80, 7}}, 7}, {{{0, 8}}, 90}, {{{0, 8}}, 26}, {{{0, 9}}, 148},
783 {{{84, 7}}, 67}, {{{0, 8}}, 122}, {{{0, 8}}, 58}, {{{0, 9}}, 212},
784 {{{82, 7}}, 19}, {{{0, 8}}, 106}, {{{0, 8}}, 42}, {{{0, 9}}, 180},
785 {{{0, 8}}, 10}, {{{0, 8}}, 138}, {{{0, 8}}, 74}, {{{0, 9}}, 244},
786 {{{80, 7}}, 5}, {{{0, 8}}, 86}, {{{0, 8}}, 22}, {{{192, 8}}, 0},
787 {{{83, 7}}, 51}, {{{0, 8}}, 118}, {{{0, 8}}, 54}, {{{0, 9}}, 204},
788 {{{81, 7}}, 15}, {{{0, 8}}, 102}, {{{0, 8}}, 38}, {{{0, 9}}, 172},
789 {{{0, 8}}, 6}, {{{0, 8}}, 134}, {{{0, 8}}, 70}, {{{0, 9}}, 236},
790 {{{80, 7}}, 9}, {{{0, 8}}, 94}, {{{0, 8}}, 30}, {{{0, 9}}, 156},
791 {{{84, 7}}, 99}, {{{0, 8}}, 126}, {{{0, 8}}, 62}, {{{0, 9}}, 220},
792 {{{82, 7}}, 27}, {{{0, 8}}, 110}, {{{0, 8}}, 46}, {{{0, 9}}, 188},
793 {{{0, 8}}, 14}, {{{0, 8}}, 142}, {{{0, 8}}, 78}, {{{0, 9}}, 252},
794 {{{96, 7}}, 256}, {{{0, 8}}, 81}, {{{0, 8}}, 17}, {{{85, 8}}, 131},
795 {{{82, 7}}, 31}, {{{0, 8}}, 113}, {{{0, 8}}, 49}, {{{0, 9}}, 194},
796 {{{80, 7}}, 10}, {{{0, 8}}, 97}, {{{0, 8}}, 33}, {{{0, 9}}, 162},
797 {{{0, 8}}, 1}, {{{0, 8}}, 129}, {{{0, 8}}, 65}, {{{0, 9}}, 226},
798 {{{80, 7}}, 6}, {{{0, 8}}, 89}, {{{0, 8}}, 25}, {{{0, 9}}, 146},
799 {{{83, 7}}, 59}, {{{0, 8}}, 121}, {{{0, 8}}, 57}, {{{0, 9}}, 210},
800 {{{81, 7}}, 17}, {{{0, 8}}, 105}, {{{0, 8}}, 41}, {{{0, 9}}, 178},
801 {{{0, 8}}, 9}, {{{0, 8}}, 137}, {{{0, 8}}, 73}, {{{0, 9}}, 242},
802 {{{80, 7}}, 4}, {{{0, 8}}, 85}, {{{0, 8}}, 21}, {{{80, 8}}, 258},
803 {{{83, 7}}, 43}, {{{0, 8}}, 117}, {{{0, 8}}, 53}, {{{0, 9}}, 202},
804 {{{81, 7}}, 13}, {{{0, 8}}, 101}, {{{0, 8}}, 37}, {{{0, 9}}, 170},
805 {{{0, 8}}, 5}, {{{0, 8}}, 133}, {{{0, 8}}, 69}, {{{0, 9}}, 234},
806 {{{80, 7}}, 8}, {{{0, 8}}, 93}, {{{0, 8}}, 29}, {{{0, 9}}, 154},
807 {{{84, 7}}, 83}, {{{0, 8}}, 125}, {{{0, 8}}, 61}, {{{0, 9}}, 218},
808 {{{82, 7}}, 23}, {{{0, 8}}, 109}, {{{0, 8}}, 45}, {{{0, 9}}, 186},
809 {{{0, 8}}, 13}, {{{0, 8}}, 141}, {{{0, 8}}, 77}, {{{0, 9}}, 250},
810 {{{80, 7}}, 3}, {{{0, 8}}, 83}, {{{0, 8}}, 19}, {{{85, 8}}, 195},
811 {{{83, 7}}, 35}, {{{0, 8}}, 115}, {{{0, 8}}, 51}, {{{0, 9}}, 198},
812 {{{81, 7}}, 11}, {{{0, 8}}, 99}, {{{0, 8}}, 35}, {{{0, 9}}, 166},
813 {{{0, 8}}, 3}, {{{0, 8}}, 131}, {{{0, 8}}, 67}, {{{0, 9}}, 230},
814 {{{80, 7}}, 7}, {{{0, 8}}, 91}, {{{0, 8}}, 27}, {{{0, 9}}, 150},
815 {{{84, 7}}, 67}, {{{0, 8}}, 123}, {{{0, 8}}, 59}, {{{0, 9}}, 214},
816 {{{82, 7}}, 19}, {{{0, 8}}, 107}, {{{0, 8}}, 43}, {{{0, 9}}, 182},
817 {{{0, 8}}, 11}, {{{0, 8}}, 139}, {{{0, 8}}, 75}, {{{0, 9}}, 246},
818 {{{80, 7}}, 5}, {{{0, 8}}, 87}, {{{0, 8}}, 23}, {{{192, 8}}, 0},
819 {{{83, 7}}, 51}, {{{0, 8}}, 119}, {{{0, 8}}, 55}, {{{0, 9}}, 206},
820 {{{81, 7}}, 15}, {{{0, 8}}, 103}, {{{0, 8}}, 39}, {{{0, 9}}, 174},
821 {{{0, 8}}, 7}, {{{0, 8}}, 135}, {{{0, 8}}, 71}, {{{0, 9}}, 238},
822 {{{80, 7}}, 9}, {{{0, 8}}, 95}, {{{0, 8}}, 31}, {{{0, 9}}, 158},
823 {{{84, 7}}, 99}, {{{0, 8}}, 127}, {{{0, 8}}, 63}, {{{0, 9}}, 222},
824 {{{82, 7}}, 27}, {{{0, 8}}, 111}, {{{0, 8}}, 47}, {{{0, 9}}, 190},
825 {{{0, 8}}, 15}, {{{0, 8}}, 143}, {{{0, 8}}, 79}, {{{0, 9}}, 254},
826 {{{96, 7}}, 256}, {{{0, 8}}, 80}, {{{0, 8}}, 16}, {{{84, 8}}, 115},
827 {{{82, 7}}, 31}, {{{0, 8}}, 112}, {{{0, 8}}, 48}, {{{0, 9}}, 193},
828 {{{80, 7}}, 10}, {{{0, 8}}, 96}, {{{0, 8}}, 32}, {{{0, 9}}, 161},
829 {{{0, 8}}, 0}, {{{0, 8}}, 128}, {{{0, 8}}, 64}, {{{0, 9}}, 225},
830 {{{80, 7}}, 6}, {{{0, 8}}, 88}, {{{0, 8}}, 24}, {{{0, 9}}, 145},
831 {{{83, 7}}, 59}, {{{0, 8}}, 120}, {{{0, 8}}, 56}, {{{0, 9}}, 209},
832 {{{81, 7}}, 17}, {{{0, 8}}, 104}, {{{0, 8}}, 40}, {{{0, 9}}, 177},
833 {{{0, 8}}, 8}, {{{0, 8}}, 136}, {{{0, 8}}, 72}, {{{0, 9}}, 241},
834 {{{80, 7}}, 4}, {{{0, 8}}, 84}, {{{0, 8}}, 20}, {{{85, 8}}, 227},
835 {{{83, 7}}, 43}, {{{0, 8}}, 116}, {{{0, 8}}, 52}, {{{0, 9}}, 201},
836 {{{81, 7}}, 13}, {{{0, 8}}, 100}, {{{0, 8}}, 36}, {{{0, 9}}, 169},
837 {{{0, 8}}, 4}, {{{0, 8}}, 132}, {{{0, 8}}, 68}, {{{0, 9}}, 233},
838 {{{80, 7}}, 8}, {{{0, 8}}, 92}, {{{0, 8}}, 28}, {{{0, 9}}, 153},
839 {{{84, 7}}, 83}, {{{0, 8}}, 124}, {{{0, 8}}, 60}, {{{0, 9}}, 217},
840 {{{82, 7}}, 23}, {{{0, 8}}, 108}, {{{0, 8}}, 44}, {{{0, 9}}, 185},
841 {{{0, 8}}, 12}, {{{0, 8}}, 140}, {{{0, 8}}, 76}, {{{0, 9}}, 249},
842 {{{80, 7}}, 3}, {{{0, 8}}, 82}, {{{0, 8}}, 18}, {{{85, 8}}, 163},
843 {{{83, 7}}, 35}, {{{0, 8}}, 114}, {{{0, 8}}, 50}, {{{0, 9}}, 197},
844 {{{81, 7}}, 11}, {{{0, 8}}, 98}, {{{0, 8}}, 34}, {{{0, 9}}, 165},
845 {{{0, 8}}, 2}, {{{0, 8}}, 130}, {{{0, 8}}, 66}, {{{0, 9}}, 229},
846 {{{80, 7}}, 7}, {{{0, 8}}, 90}, {{{0, 8}}, 26}, {{{0, 9}}, 149},
847 {{{84, 7}}, 67}, {{{0, 8}}, 122}, {{{0, 8}}, 58}, {{{0, 9}}, 213},
848 {{{82, 7}}, 19}, {{{0, 8}}, 106}, {{{0, 8}}, 42}, {{{0, 9}}, 181},
849 {{{0, 8}}, 10}, {{{0, 8}}, 138}, {{{0, 8}}, 74}, {{{0, 9}}, 245},
850 {{{80, 7}}, 5}, {{{0, 8}}, 86}, {{{0, 8}}, 22}, {{{192, 8}}, 0},
851 {{{83, 7}}, 51}, {{{0, 8}}, 118}, {{{0, 8}}, 54}, {{{0, 9}}, 205},
852 {{{81, 7}}, 15}, {{{0, 8}}, 102}, {{{0, 8}}, 38}, {{{0, 9}}, 173},
853 {{{0, 8}}, 6}, {{{0, 8}}, 134}, {{{0, 8}}, 70}, {{{0, 9}}, 237},
854 {{{80, 7}}, 9}, {{{0, 8}}, 94}, {{{0, 8}}, 30}, {{{0, 9}}, 157},
855 {{{84, 7}}, 99}, {{{0, 8}}, 126}, {{{0, 8}}, 62}, {{{0, 9}}, 221},
856 {{{82, 7}}, 27}, {{{0, 8}}, 110}, {{{0, 8}}, 46}, {{{0, 9}}, 189},
857 {{{0, 8}}, 14}, {{{0, 8}}, 142}, {{{0, 8}}, 78}, {{{0, 9}}, 253},
858 {{{96, 7}}, 256}, {{{0, 8}}, 81}, {{{0, 8}}, 17}, {{{85, 8}}, 131},
859 {{{82, 7}}, 31}, {{{0, 8}}, 113}, {{{0, 8}}, 49}, {{{0, 9}}, 195},
860 {{{80, 7}}, 10}, {{{0, 8}}, 97}, {{{0, 8}}, 33}, {{{0, 9}}, 163},
861 {{{0, 8}}, 1}, {{{0, 8}}, 129}, {{{0, 8}}, 65}, {{{0, 9}}, 227},
862 {{{80, 7}}, 6}, {{{0, 8}}, 89}, {{{0, 8}}, 25}, {{{0, 9}}, 147},
863 {{{83, 7}}, 59}, {{{0, 8}}, 121}, {{{0, 8}}, 57}, {{{0, 9}}, 211},
864 {{{81, 7}}, 17}, {{{0, 8}}, 105}, {{{0, 8}}, 41}, {{{0, 9}}, 179},
865 {{{0, 8}}, 9}, {{{0, 8}}, 137}, {{{0, 8}}, 73}, {{{0, 9}}, 243},
866 {{{80, 7}}, 4}, {{{0, 8}}, 85}, {{{0, 8}}, 21}, {{{80, 8}}, 258},
867 {{{83, 7}}, 43}, {{{0, 8}}, 117}, {{{0, 8}}, 53}, {{{0, 9}}, 203},
868 {{{81, 7}}, 13}, {{{0, 8}}, 101}, {{{0, 8}}, 37}, {{{0, 9}}, 171},
869 {{{0, 8}}, 5}, {{{0, 8}}, 133}, {{{0, 8}}, 69}, {{{0, 9}}, 235},
870 {{{80, 7}}, 8}, {{{0, 8}}, 93}, {{{0, 8}}, 29}, {{{0, 9}}, 155},
871 {{{84, 7}}, 83}, {{{0, 8}}, 125}, {{{0, 8}}, 61}, {{{0, 9}}, 219},
872 {{{82, 7}}, 23}, {{{0, 8}}, 109}, {{{0, 8}}, 45}, {{{0, 9}}, 187},
873 {{{0, 8}}, 13}, {{{0, 8}}, 141}, {{{0, 8}}, 77}, {{{0, 9}}, 251},
874 {{{80, 7}}, 3}, {{{0, 8}}, 83}, {{{0, 8}}, 19}, {{{85, 8}}, 195},
875 {{{83, 7}}, 35}, {{{0, 8}}, 115}, {{{0, 8}}, 51}, {{{0, 9}}, 199},
876 {{{81, 7}}, 11}, {{{0, 8}}, 99}, {{{0, 8}}, 35}, {{{0, 9}}, 167},
877 {{{0, 8}}, 3}, {{{0, 8}}, 131}, {{{0, 8}}, 67}, {{{0, 9}}, 231},
878 {{{80, 7}}, 7}, {{{0, 8}}, 91}, {{{0, 8}}, 27}, {{{0, 9}}, 151},
879 {{{84, 7}}, 67}, {{{0, 8}}, 123}, {{{0, 8}}, 59}, {{{0, 9}}, 215},
880 {{{82, 7}}, 19}, {{{0, 8}}, 107}, {{{0, 8}}, 43}, {{{0, 9}}, 183},
881 {{{0, 8}}, 11}, {{{0, 8}}, 139}, {{{0, 8}}, 75}, {{{0, 9}}, 247},
882 {{{80, 7}}, 5}, {{{0, 8}}, 87}, {{{0, 8}}, 23}, {{{192, 8}}, 0},
883 {{{83, 7}}, 51}, {{{0, 8}}, 119}, {{{0, 8}}, 55}, {{{0, 9}}, 207},
884 {{{81, 7}}, 15}, {{{0, 8}}, 103}, {{{0, 8}}, 39}, {{{0, 9}}, 175},
885 {{{0, 8}}, 7}, {{{0, 8}}, 135}, {{{0, 8}}, 71}, {{{0, 9}}, 239},
886 {{{80, 7}}, 9}, {{{0, 8}}, 95}, {{{0, 8}}, 31}, {{{0, 9}}, 159},
887 {{{84, 7}}, 99}, {{{0, 8}}, 127}, {{{0, 8}}, 63}, {{{0, 9}}, 223},
888 {{{82, 7}}, 27}, {{{0, 8}}, 111}, {{{0, 8}}, 47}, {{{0, 9}}, 191},
889 {{{0, 8}}, 15}, {{{0, 8}}, 143}, {{{0, 8}}, 79}, {{{0, 9}}, 255}
890};
892{
893 {{{80, 5}}, 1}, {{{87, 5}}, 257}, {{{83, 5}}, 17}, {{{91, 5}}, 4097},
894 {{{81, 5}}, 5}, {{{89, 5}}, 1025}, {{{85, 5}}, 65}, {{{93, 5}}, 16385},
895 {{{80, 5}}, 3}, {{{88, 5}}, 513}, {{{84, 5}}, 33}, {{{92, 5}}, 8193},
896 {{{82, 5}}, 9}, {{{90, 5}}, 2049}, {{{86, 5}}, 129}, {{{192, 5}}, 24577},
897 {{{80, 5}}, 2}, {{{87, 5}}, 385}, {{{83, 5}}, 25}, {{{91, 5}}, 6145},
898 {{{81, 5}}, 7}, {{{89, 5}}, 1537}, {{{85, 5}}, 97}, {{{93, 5}}, 24577},
899 {{{80, 5}}, 4}, {{{88, 5}}, 769}, {{{84, 5}}, 49}, {{{92, 5}}, 12289},
900 {{{82, 5}}, 13}, {{{90, 5}}, 3073}, {{{86, 5}}, 193}, {{{192, 5}}, 24577}
901};
902
903
904
905
906
907
908
909// copy as much as possible from the sliding window to the output area
911{
912 uInt n;
913 Byte* p;
914 Byte* q;
915
916 // local copies of source and destination pointers
917 p = z->next_out;
918 q = s->read;
919
920 // compute number of bytes to copy as far as end of window
921 n = (uInt)((q <= s->write ? s->write : s->end) - q);
922 if (n > z->avail_out)
923 n = z->avail_out;
924 if (n && r == Z_BUF_ERROR)
925 r = Z_OK;
926
927 // update counters
928 z->avail_out -= n;
929 z->total_out += n;
930
931 // update check information
932 if (s->checkfn != Z_NULL)
933 z->adler = s->check = (*s->checkfn)(s->check, q, n);
934
935 // copy as far as end of window
936 if (n != 0) // check for n!=0 to avoid waking up CodeGuard
937 {
938 memcpy(p, q, n);
939 p += n;
940 q += n;
941 }
942
943 // see if more to copy at beginning of window
944 if (q == s->end)
945 {
946 // wrap pointers
947 q = s->window;
948 if (s->write == s->end)
949 s->write = s->window;
950
951 // compute bytes to copy
952 n = (uInt)(s->write - q);
953 if (n > z->avail_out)
954 n = z->avail_out;
955 if (n && r == Z_BUF_ERROR)
956 r = Z_OK;
957
958 // update counters
959 z->avail_out -= n;
960 z->total_out += n;
961
962 // update check information
963 if (s->checkfn != Z_NULL)
964 z->adler = s->check = (*s->checkfn)(s->check, q, n);
965
966 // copy
967 if (n != 0)
968 {
969 memcpy(p, q, n);
970 p += n;
971 q += n;
972 }
973 }
974
975 // update pointers
976 z->next_out = p;
977 s->read = q;
978
979 // done
980 return r;
981}
982
983
984
985
986
987
988// simplify the use of the inflate_huft type with some defines
989#define exop word.what.Exop
990#define bits word.what.Bits
991
992typedef enum // waiting for "i:"=input, "o:"=output, "x:"=nothing
993{
994 START, // x: set up for LEN
995 LEN, // i: get length/literal/eob next
996 LENEXT, // i: getting length extra (have base)
997 DIST, // i: get distance next
998 DISTEXT, // i: getting distance extra
999 COPY, // o: copying bytes in window, waiting for space
1000 LIT, // o: got literal, waiting for output space
1001 WASH, // o: got eob, possibly still output waiting
1002 END, // x: got eob and all data flushed
1003 BADCODE
1004} // x: got error
1006
1007// inflate codes private state
1009{
1010
1011 // mode
1012 inflate_codes_mode mode; // current inflate_codes mode
1013
1014 // mode dependent information
1016 union
1017 {
1018 struct
1019 {
1020 const inflate_huft* tree; // pointer into tree
1021 uInt need; // bits needed
1022 } code; // if LEN or DIST, where in tree
1023 uInt lit; // if LIT, literal
1024 struct
1025 {
1026 uInt get; // bits to get for extra
1027 uInt dist; // distance back to copy from
1028 } copy; // if EXT or COPY, where and how much
1029 } sub; // submode
1030
1031 // mode independent information
1032 Byte lbits; // ltree bits decoded per branch
1033 Byte dbits; // dtree bits decoder per branch
1034 const inflate_huft* ltree; // literal/length/eob tree
1035 const inflate_huft* dtree; // distance tree
1036
1037};
1038
1039
1041 uInt bl, uInt bd,
1042 const inflate_huft* tl,
1043 const inflate_huft* td, // need separate declaration for Borland C++
1044 z_streamp z)
1045{
1047
1048 if ((c = (inflate_codes_statef*)
1049 ZALLOC(z, 1, sizeof(struct inflate_codes_state))) != Z_NULL)
1050 {
1051 c->mode = START;
1052 c->lbits = (Byte)bl;
1053 c->dbits = (Byte)bd;
1054 c->ltree = tl;
1055 c->dtree = td;
1056 LuTracev((stderr, "inflate: codes new\n"));
1057 }
1058 return c;
1059}
1060
1061
1063{
1064 uInt j; // temporary storage
1065 const inflate_huft* t; // temporary pointer
1066 uInt e; // extra bits or operation
1067 uLong b; // bit buffer
1068 uInt k; // bits in bit buffer
1069 Byte* p; // input data pointer
1070 uInt n; // bytes available there
1071 Byte* q; // output window write pointer
1072 uInt m; // bytes to end of window or read pointer
1073 Byte* f; // pointer to copy strings from
1074 inflate_codes_statef* c = s->sub.decode.codes; // codes state
1075
1076 // copy input/output information to locals (UPDATE macro restores)
1077 LOAD
1078
1079 // process input and output based on current state
1080 for (;;) switch (c->mode)
1081 {
1082 // waiting for "i:"=input, "o:"=output, "x:"=nothing
1083 case START: // x: set up for LEN
1084#ifndef SLOW
1085 if (m >= 258 && n >= 10)
1086 {
1087 UPDATE
1088 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
1089 LOAD
1090 if (r != Z_OK)
1091 {
1092 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
1093 break;
1094 }
1095 }
1096#endif // !SLOW
1097 c->sub.code.need = c->lbits;
1098 c->sub.code.tree = c->ltree;
1099 c->mode = LEN;
1100 case LEN: // i: get length/literal/eob next
1101 j = c->sub.code.need;
1102 NEEDBITS(j)
1103 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1104 DUMPBITS(t->bits)
1105 e = (uInt)(t->exop);
1106 if (e == 0) // literal
1107 {
1108 c->sub.lit = t->base;
1109 LuTracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
1110 "inflate: literal '%c'\n" :
1111 "inflate: literal 0x%02x\n", t->base));
1112 c->mode = LIT;
1113 break;
1114 }
1115 if (e & 16) // length
1116 {
1117 c->sub.copy.get = e & 15;
1118 c->len = t->base;
1119 c->mode = LENEXT;
1120 break;
1121 }
1122 if ((e & 64) == 0) // next table
1123 {
1124 c->sub.code.need = e;
1125 c->sub.code.tree = t + t->base;
1126 break;
1127 }
1128 if (e & 32) // end of block
1129 {
1130 LuTracevv((stderr, "inflate: end of block\n"));
1131 c->mode = WASH;
1132 break;
1133 }
1134 c->mode = BADCODE; // invalid code
1135 z->msg = (char*)"invalid literal/length code";
1136 r = Z_DATA_ERROR;
1137 LEAVE
1138 case LENEXT: // i: getting length extra (have base)
1139 j = c->sub.copy.get;
1140 NEEDBITS(j)
1141 c->len += (uInt)b & inflate_mask[j];
1142 DUMPBITS(j)
1143 c->sub.code.need = c->dbits;
1144 c->sub.code.tree = c->dtree;
1145 LuTracevv((stderr, "inflate: length %u\n", c->len));
1146 c->mode = DIST;
1147 case DIST: // i: get distance next
1148 j = c->sub.code.need;
1149 NEEDBITS(j)
1150 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1151 DUMPBITS(t->bits)
1152 e = (uInt)(t->exop);
1153 if (e & 16) // distance
1154 {
1155 c->sub.copy.get = e & 15;
1156 c->sub.copy.dist = t->base;
1157 c->mode = DISTEXT;
1158 break;
1159 }
1160 if ((e & 64) == 0) // next table
1161 {
1162 c->sub.code.need = e;
1163 c->sub.code.tree = t + t->base;
1164 break;
1165 }
1166 c->mode = BADCODE; // invalid code
1167 z->msg = (char*)"invalid distance code";
1168 r = Z_DATA_ERROR;
1169 LEAVE
1170 case DISTEXT: // i: getting distance extra
1171 j = c->sub.copy.get;
1172 NEEDBITS(j)
1173 c->sub.copy.dist += (uInt)b & inflate_mask[j];
1174 DUMPBITS(j)
1175 LuTracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
1176 c->mode = COPY;
1177 case COPY: // o: copying bytes in window, waiting for space
1178 f = q - c->sub.copy.dist;
1179 while (f < s->window) // modulo window size-"while" instead
1180 f += s->end - s->window; // of "if" handles invalid distances
1181 while (c->len)
1182 {
1183 NEEDOUT
1184 OUTBYTE(*f++)
1185 if (f == s->end)
1186 f = s->window;
1187 c->len--;
1188 }
1189 c->mode = START;
1190 break;
1191 case LIT: // o: got literal, waiting for output space
1192 NEEDOUT
1193 OUTBYTE(c->sub.lit)
1194 c->mode = START;
1195 break;
1196 case WASH: // o: got eob, possibly more output
1197 if (k > 7) // return unused byte, if any
1198 {
1199 //Assert(k < 16, "inflate_codes grabbed too many bytes")
1200 k -= 8;
1201 n++;
1202 p--; // can always return one
1203 }
1204 FLUSH
1205 if (s->read != s->write)
1206 LEAVE
1207 c->mode = END;
1208 case END:
1209 r = Z_STREAM_END;
1210 LEAVE
1211 case BADCODE: // x: got error
1212 r = Z_DATA_ERROR;
1213 LEAVE
1214 default:
1215 r = Z_STREAM_ERROR;
1216 LEAVE
1217 }
1218}
1219
1220
1222{
1223 ZFREE(z, c);
1224 LuTracev((stderr, "inflate: codes free\n"));
1225}
1226
1227
1228
1229// infblock.c -- interpret and process block types to last block
1230// Copyright (C) 1995-1998 Mark Adler
1231// For conditions of distribution and use, see copyright notice in zlib.h
1232
1233//struct inflate_codes_state {int dummy;}; // for buggy compilers
1234
1235
1236
1237// Table for deflate from PKZIP's appnote.txt.
1238const uInt border[] = // Order of the bit length code lengths
1239{
1240 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
1241};
1242
1243//
1244// Notes beyond the 1.93a appnote.txt:
1245//
1246// 1. Distance pointers never point before the beginning of the output stream.
1247// 2. Distance pointers can point back across blocks, up to 32k away.
1248// 3. There is an implied maximum of 7 bits for the bit length table and
1249// 15 bits for the actual data.
1250// 4. If only one code exists, then it is encoded using one bit. (Zero
1251// would be more efficient, but perhaps a little confusing.) If two
1252// codes exist, they are coded using one bit each (0 and 1).
1253// 5. There is no way of sending zero distance codes--a dummy must be
1254// sent if there are none. (History: a pre 2.0 version of PKZIP would
1255// store blocks with no distance codes, but this was discovered to be
1256// too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
1257// zero distance codes, which is sent as one code of zero bits in
1258// length.
1259// 6. There are up to 286 literal/length codes. Code 256 represents the
1260// end-of-block. Note however that the static length tree defines
1261// 288 codes just to fill out the Huffman codes. Codes 286 and 287
1262// cannot be used though, since there is no length base or extra bits
1263// defined for them. Similarily, there are up to 30 distance codes.
1264// However, static trees define 32 codes (all 5 bits) to fill out the
1265// Huffman codes, but the last two had better not show up in the data.
1266// 7. Unzip can check dynamic Huffman blocks for complete code sets.
1267// The exception is that a single code would not be complete (see #4).
1268// 8. The five bits following the block type is really the number of
1269// literal codes sent minus 257.
1270// 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
1271// (1+6+6). Therefore, to output three times the length, you output
1272// three codes (1+1+1), whereas to output four times the same length,
1273// you only need two codes (1+3). Hmm.
1274//10. In the tree reconstruction algorithm, Code = Code + Increment
1275// only if BitLength(i) is not zero. (Pretty obvious.)
1276//11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
1277//12. Note: length code 284 can represent 227-258, but length code 285
1278// really is 258. The last length deserves its own, short code
1279// since it gets used a lot in very redundant files. The length
1280// 258 is special since 258 - 3 (the min match length) is 255.
1281//13. The literal/length and distance code bit lengths are read as a
1282// single stream of lengths. It is possible (and advantageous) for
1283// a repeat code (16, 17, or 18) to go across the boundary between
1284// the two sets of lengths.
1285
1286
1288{
1289 if (c != Z_NULL)
1290 *c = s->check;
1291 if (s->mode == IBM_BTREE || s->mode == IBM_DTREE)
1292 ZFREE(z, s->sub.trees.blens);
1293 if (s->mode == IBM_CODES)
1295 s->mode = IBM_TYPE;
1296 s->bitk = 0;
1297 s->bitb = 0;
1298 s->read = s->write = s->window;
1299 if (s->checkfn != Z_NULL)
1300 z->adler = s->check = (*s->checkfn)(0L, (const Byte*)Z_NULL, 0);
1301 LuTracev((stderr, "inflate: blocks reset\n"));
1302}
1303
1304
1306{
1308
1309 if ((s = (inflate_blocks_statef*)ZALLOC
1310 (z, 1, sizeof(struct inflate_blocks_state))) == Z_NULL)
1311 return s;
1312 if ((s->hufts =
1313 (inflate_huft*)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
1314 {
1315 ZFREE(z, s);
1316 return Z_NULL;
1317 }
1318 if ((s->window = (Byte*)ZALLOC(z, 1, w)) == Z_NULL)
1319 {
1320 ZFREE(z, s->hufts);
1321 ZFREE(z, s);
1322 return Z_NULL;
1323 }
1324 s->end = s->window + w;
1325 s->checkfn = c;
1326 s->mode = IBM_TYPE;
1327 LuTracev((stderr, "inflate: blocks allocated\n"));
1329 return s;
1330}
1331
1332
1334{
1335 uInt t; // temporary storage
1336 uLong b; // bit buffer
1337 uInt k; // bits in bit buffer
1338 Byte* p; // input data pointer
1339 uInt n; // bytes available there
1340 Byte* q; // output window write pointer
1341 uInt m; // bytes to end of window or read pointer
1342
1343 // copy input/output information to locals (UPDATE macro restores)
1344 LOAD
1345
1346 // process input based on current state
1347 for (;;)
1348 {
1349 switch (s->mode)
1350 {
1351 case IBM_TYPE:
1352 NEEDBITS(3)
1353 t = (uInt)b & 7;
1354 s->last = t & 1;
1355 switch (t >> 1)
1356 {
1357 case 0: // stored
1358 LuTracev((stderr, "inflate: stored block%s\n",
1359 s->last ? " (last)" : ""));
1360 DUMPBITS(3)
1361 t = k & 7; // go to byte boundary
1362 DUMPBITS(t)
1363 s->mode = IBM_LENS; // get length of stored block
1364 break;
1365 case 1: // fixed
1366 LuTracev((stderr, "inflate: fixed codes block%s\n",
1367 s->last ? " (last)" : ""));
1368 {
1369 uInt bl, bd;
1370 const inflate_huft* tl, *td;
1371
1372 inflate_trees_fixed(&bl, &bd, &tl, &td, z);
1373 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
1374 if (s->sub.decode.codes == Z_NULL)
1375 {
1376 r = Z_MEM_ERROR;
1377 LEAVE
1378 }
1379 }
1380 DUMPBITS(3)
1381 s->mode = IBM_CODES;
1382 break;
1383 case 2: // dynamic
1384 LuTracev((stderr, "inflate: dynamic codes block%s\n",
1385 s->last ? " (last)" : ""));
1386 DUMPBITS(3)
1387 s->mode = IBM_TABLE;
1388 break;
1389 case 3: // illegal
1390 DUMPBITS(3)
1391 s->mode = IBM_BAD;
1392 z->msg = (char*)"invalid block type";
1393 r = Z_DATA_ERROR;
1394 LEAVE
1395 }
1396 break;
1397 case IBM_LENS:
1398 NEEDBITS(32)
1399 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
1400 {
1401 s->mode = IBM_BAD;
1402 z->msg = (char*)"invalid stored block lengths";
1403 r = Z_DATA_ERROR;
1404 LEAVE
1405 }
1406 s->sub.left = (uInt)b & 0xffff;
1407 b = k = 0; // dump bits
1408 LuTracev((stderr, "inflate: stored length %u\n", s->sub.left));
1409 s->mode = s->sub.left ? IBM_STORED : (s->last ? IBM_DRY : IBM_TYPE);
1410 break;
1411 case IBM_STORED:
1412 if (n == 0)
1413 LEAVE
1414 NEEDOUT
1415 t = s->sub.left;
1416 if (t > n) t = n;
1417 if (t > m) t = m;
1418 memcpy(q, p, t);
1419 p += t;
1420 n -= t;
1421 q += t;
1422 m -= t;
1423 if ((s->sub.left -= t) != 0)
1424 break;
1425 LuTracev((stderr, "inflate: stored end, %lu total out\n",
1426 z->total_out + (q >= s->read ? q - s->read :
1427 (s->end - s->read) + (q - s->window))));
1428 s->mode = s->last ? IBM_DRY : IBM_TYPE;
1429 break;
1430 case IBM_TABLE:
1431 NEEDBITS(14)
1432 s->sub.trees.table = t = (uInt)b & 0x3fff;
1433 // remove this section to workaround bug in pkzip
1434 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
1435 {
1436 s->mode = IBM_BAD;
1437 z->msg = (char*)"too many length or distance symbols";
1438 r = Z_DATA_ERROR;
1439 LEAVE
1440 }
1441 // end remove
1442 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
1443 if ((s->sub.trees.blens = (uInt*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
1444 {
1445 r = Z_MEM_ERROR;
1446 LEAVE
1447 }
1448 DUMPBITS(14)
1449 s->sub.trees.index = 0;
1450 LuTracev((stderr, "inflate: table sizes ok\n"));
1451 s->mode = IBM_BTREE;
1452 case IBM_BTREE:
1453 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
1454 {
1455 NEEDBITS(3)
1456 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
1457 DUMPBITS(3)
1458 }
1459 while (s->sub.trees.index < 19)
1460 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
1461 s->sub.trees.bb = 7;
1463 &s->sub.trees.tb, s->hufts, z);
1464 if (t != Z_OK)
1465 {
1466 r = t;
1467 if (r == Z_DATA_ERROR)
1468 {
1469 ZFREE(z, s->sub.trees.blens);
1470 s->mode = IBM_BAD;
1471 }
1472 LEAVE
1473 }
1474 s->sub.trees.index = 0;
1475 LuTracev((stderr, "inflate: bits tree ok\n"));
1476 s->mode = IBM_DTREE;
1477 case IBM_DTREE:
1478 while (t = s->sub.trees.table,
1479 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
1480 {
1481 inflate_huft* h;
1482 uInt i, j, c;
1483
1484 t = s->sub.trees.bb;
1485 NEEDBITS(t)
1486 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
1487 t = h->bits;
1488 c = h->base;
1489 if (c < 16)
1490 {
1491 DUMPBITS(t)
1492 s->sub.trees.blens[s->sub.trees.index++] = c;
1493 }
1494 else // c == 16..18
1495 {
1496 i = c == 18 ? 7 : c - 14;
1497 j = c == 18 ? 11 : 3;
1498 NEEDBITS(t + i)
1499 DUMPBITS(t)
1500 j += (uInt)b & inflate_mask[i];
1501 DUMPBITS(i)
1502 i = s->sub.trees.index;
1503 t = s->sub.trees.table;
1504 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
1505 (c == 16 && i < 1))
1506 {
1507 ZFREE(z, s->sub.trees.blens);
1508 s->mode = IBM_BAD;
1509 z->msg = (char*)"invalid bit length repeat";
1510 r = Z_DATA_ERROR;
1511 LEAVE
1512 }
1513 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
1514 do
1515 {
1516 s->sub.trees.blens[i++] = c;
1517 }
1518 while (--j);
1519 s->sub.trees.index = i;
1520 }
1521 }
1522 s->sub.trees.tb = Z_NULL;
1523 {
1524 uInt bl, bd;
1525 inflate_huft* tl, *td;
1527
1528 bl = 9; // must be <= 9 for lookahead assumptions
1529 bd = 6; // must be <= 9 for lookahead assumptions
1530 t = s->sub.trees.table;
1531 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
1532 s->sub.trees.blens, &bl, &bd, &tl, &td,
1533 s->hufts, z);
1534 if (t != Z_OK)
1535 {
1536 if (t == (uInt)Z_DATA_ERROR)
1537 {
1538 ZFREE(z, s->sub.trees.blens);
1539 s->mode = IBM_BAD;
1540 }
1541 r = t;
1542 LEAVE
1543 }
1544 LuTracev((stderr, "inflate: trees ok\n"));
1545 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
1546 {
1547 r = Z_MEM_ERROR;
1548 LEAVE
1549 }
1550 s->sub.decode.codes = c;
1551 }
1552 ZFREE(z, s->sub.trees.blens);
1553 s->mode = IBM_CODES;
1554 case IBM_CODES:
1555 UPDATE
1556 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
1557 return inflate_flush(s, z, r);
1558 r = Z_OK;
1560 LOAD
1561 LuTracev((stderr, "inflate: codes end, %lu total out\n",
1562 z->total_out + (q >= s->read ? q - s->read :
1563 (s->end - s->read) + (q - s->window))));
1564 if (!s->last)
1565 {
1566 s->mode = IBM_TYPE;
1567 break;
1568 }
1569 s->mode = IBM_DRY;
1570 case IBM_DRY:
1571 FLUSH
1572 if (s->read != s->write)
1573 LEAVE
1574 s->mode = IBM_DONE;
1575 case IBM_DONE:
1576 r = Z_STREAM_END;
1577 LEAVE
1578 case IBM_BAD:
1579 r = Z_DATA_ERROR;
1580 LEAVE
1581 default:
1582 r = Z_STREAM_ERROR;
1583 LEAVE
1584 }
1585 }
1586}
1587
1588
1590{
1592 ZFREE(z, s->window);
1593 ZFREE(z, s->hufts);
1594 ZFREE(z, s);
1595 LuTracev((stderr, "inflate: blocks freed\n"));
1596 return Z_OK;
1597}
1598
1599
1600
1601// inftrees.c -- generate Huffman trees for efficient decoding
1602// Copyright (C) 1995-1998 Mark Adler
1603// For conditions of distribution and use, see copyright notice in zlib.h
1604//
1605
1606
1607
1608extern const char unzip_inflate_copyright[] =
1609 " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
1610// If you use the zlib library in a product, an acknowledgment is welcome
1611// in the documentation of your product. If for some reason you cannot
1612// include such an acknowledgment, I would appreciate that you keep this
1613// copyright string in the executable of your product.
1614
1615
1616
1617int huft_build (
1618 uInt*, // code lengths in bits
1619 uInt, // number of codes
1620 uInt, // number of "simple" codes
1621 const uInt*, // list of base values for non-simple codes
1622 const uInt*, // list of extra bits for non-simple codes
1623 inflate_huft**, // result: starting table
1624 uInt*, // maximum lookup bits (returns actual)
1625 inflate_huft*, // space for trees
1626 uInt*, // hufts used in space
1627 uInt* ); // space for values
1628
1629// Tables for deflate from PKZIP's appnote.txt.
1630const uInt cplens[31] = // Copy lengths for literal codes 257..285
1631{
1632 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1633 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
1634};
1635// see note #13 above about 258
1636const uInt cplext[31] = // Extra bits for literal codes 257..285
1637{
1638 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1639 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112
1640}; // 112==invalid
1641const uInt cpdist[30] = // Copy offsets for distance codes 0..29
1642{
1643 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1644 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1645 8193, 12289, 16385, 24577
1646};
1647const uInt cpdext[30] = // Extra bits for distance codes
1648{
1649 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1650 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1651 12, 12, 13, 13
1652};
1653
1654//
1655// Huffman code decoding is performed using a multi-level table lookup.
1656// The fastest way to decode is to simply build a lookup table whose
1657// size is determined by the longest code. However, the time it takes
1658// to build this table can also be a factor if the data being decoded
1659// is not very long. The most common codes are necessarily the
1660// shortest codes, so those codes dominate the decoding time, and hence
1661// the speed. The idea is you can have a shorter table that decodes the
1662// shorter, more probable codes, and then point to subsidiary tables for
1663// the longer codes. The time it costs to decode the longer codes is
1664// then traded against the time it takes to make longer tables.
1665//
1666// This results of this trade are in the variables lbits and dbits
1667// below. lbits is the number of bits the first level table for literal/
1668// length codes can decode in one step, and dbits is the same thing for
1669// the distance codes. Subsequent tables are also less than or equal to
1670// those sizes. These values may be adjusted either when all of the
1671// codes are shorter than that, in which case the longest code length in
1672// bits is used, or when the shortest code is *longer* than the requested
1673// table size, in which case the length of the shortest code in bits is
1674// used.
1675//
1676// There are two different values for the two tables, since they code a
1677// different number of possibilities each. The literal/length table
1678// codes 286 possible values, or in a flat code, a little over eight
1679// bits. The distance table codes 30 possible values, or a little less
1680// than five bits, flat. The optimum values for speed end up being
1681// about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1682// The optimum values may differ though from machine to machine, and
1683// possibly even between compilers. Your mileage may vary.
1684//
1685
1686
1687// If BMAX needs to be larger than 16, then h and x[] should be uLong.
1688#define BMAX 15 // maximum bit length of any code
1689
1691 uInt* b, // code lengths in bits (all assumed <= BMAX)
1692 uInt n, // number of codes (assumed <= 288)
1693 uInt s, // number of simple-valued codes (0..s-1)
1694 const uInt* d, // list of base values for non-simple codes
1695 const uInt* e, // list of extra bits for non-simple codes
1696 inflate_huft * *t, // result: starting table
1697 uInt* m, // maximum lookup bits, returns actual
1698 inflate_huft* hp, // space for trees
1699 uInt* hn, // hufts used in space
1700 uInt* v) // working area: values in order of bit length
1701// Given a list of code lengths and a maximum table size, make a set of
1702// tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
1703// if the given code set is incomplete (the tables are still built in this
1704// case), or Z_DATA_ERROR if the input is invalid.
1705{
1706
1707 uInt a; // counter for codes of length k
1708 uInt c[BMAX + 1]; // bit length count table
1709 uInt f; // i repeats in table every f entries
1710 int g; // maximum code length
1711 int h; // table level
1712 register uInt i; // counter, current code
1713 register uInt j; // counter
1714 register int k; // number of bits in current code
1715 int l; // bits per table (returned in m)
1716 uInt mask; // (1 << w) - 1, to avoid cc -O bug on HP
1717 register uInt* p; // pointer into c[], b[], or v[]
1718 inflate_huft* q; // points to current table
1719 struct inflate_huft_s r; // table entry for structure assignment
1720 inflate_huft* u[BMAX]; // table stack
1721 register int w; // bits before this table == (l * h)
1722 uInt x[BMAX + 1]; // bit offsets, then code stack
1723 uInt* xp; // pointer into x
1724 int y; // number of dummy codes added
1725 uInt z; // number of entries in current table
1726
1727
1728 // Generate counts for each bit length
1729 p = c;
1730#define C0 *p++ = 0;
1731#define C2 C0 C0 C0 C0
1732#define C4 C2 C2 C2 C2
1733 C4;
1734 p; // clear c[]--assume BMAX+1 is 16
1735 p = b;
1736 i = n;
1737 do
1738 {
1739 c[*p++]++; // assume all entries <= BMAX
1740 }
1741 while (--i);
1742 if (c[0] == n) // null input--all zero length codes
1743 {
1744 *t = (inflate_huft*)Z_NULL;
1745 *m = 0;
1746 return Z_OK;
1747 }
1748
1749
1750 // Find minimum and maximum length, bound *m by those
1751 l = *m;
1752 for (j = 1; j <= BMAX; j++)
1753 if (c[j])
1754 break;
1755 k = j; // minimum code length
1756 if ((uInt)l < j)
1757 l = j;
1758 for (i = BMAX; i; i--)
1759 if (c[i])
1760 break;
1761 g = i; // maximum code length
1762 if ((uInt)l > i)
1763 l = i;
1764 *m = l;
1765
1766
1767 // Adjust last length count to fill out codes, if needed
1768 for (y = 1 << j; j < i; j++, y <<= 1)
1769 if ((y -= c[j]) < 0)
1770 return Z_DATA_ERROR;
1771 if ((y -= c[i]) < 0)
1772 return Z_DATA_ERROR;
1773 c[i] += y;
1774
1775
1776 // Generate starting offsets into the value table for each length
1777 x[1] = j = 0;
1778 p = c + 1;
1779 xp = x + 2;
1780 while (--i) // note that i == g from above
1781 {
1782 *xp++ = (j += *p++);
1783 }
1784
1785
1786 // Make a table of values in order of bit lengths
1787 p = b;
1788 i = 0;
1789 do
1790 {
1791 if ((j = *p++) != 0)
1792 v[x[j]++] = i;
1793 }
1794 while (++i < n);
1795 n = x[g]; // set n to length of v
1796
1797
1798 // Generate the Huffman codes and for each, make the table entries
1799 x[0] = i = 0; // first Huffman code is zero
1800 p = v; // grab values in bit order
1801 h = -1; // no tables yet--level -1
1802 w = -l; // bits decoded == (l * h)
1803 u[0] = (inflate_huft*)Z_NULL; // just to keep compilers happy
1804 q = (inflate_huft*)Z_NULL; // ditto
1805 z = 0; // ditto
1806
1807 // go through the bit lengths (k already is bits in shortest code)
1808 for (; k <= g; k++)
1809 {
1810 a = c[k];
1811 while (a--)
1812 {
1813 // here i is the Huffman code of length k bits for value *p
1814 // make tables up to required level
1815 while (k > w + l)
1816 {
1817 h++;
1818 w += l; // previous table always l bits
1819
1820 // compute minimum size table less than or equal to l bits
1821 z = g - w;
1822 z = z > (uInt)l ? l : z; // table size upper limit
1823 if ((f = 1 << (j = k - w)) > a + 1) // try a k-w bit table
1824 {
1825 // too few codes for k-w bit table
1826 f -= a + 1; // deduct codes from patterns left
1827 xp = c + k;
1828 if (j < z)
1829 while (++j < z) // try smaller tables up to z bits
1830 {
1831 if ((f <<= 1) <= *++xp)
1832 break; // enough codes to use up j bits
1833 f -= *xp; // else deduct codes from patterns
1834 }
1835 }
1836 z = 1 << j; // table entries for j-bit table
1837
1838 // allocate new table
1839 if (*hn + z > MANY) // (note: doesn't matter for fixed)
1840 return Z_DATA_ERROR; // overflow of MANY
1841 u[h] = q = hp + *hn;
1842 *hn += z;
1843
1844 // connect to last table, if there is one
1845 if (h)
1846 {
1847 x[h] = i; // save pattern for backing up
1848 r.bits = (Byte)l; // bits to dump before this table
1849 r.exop = (Byte)j; // bits in this table
1850 j = i >> (w - l);
1851 r.base = (uInt)(q - u[h - 1] - j); // offset to this table
1852 u[h - 1][j] = r; // connect to last table
1853 }
1854 else
1855 *t = q; // first table is returned result
1856 }
1857
1858 // set up table entry in r
1859 r.bits = (Byte)(k - w);
1860 if (p >= v + n)
1861 r.exop = 128 + 64; // out of values--invalid code
1862 else if (*p < s)
1863 {
1864 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); // 256 is end-of-block
1865 r.base = *p++; // simple code is just the value
1866 }
1867 else
1868 {
1869 r.exop = (Byte)(e[*p - s] + 16 + 64);// non-simple--look up in lists
1870 r.base = d[*p++ - s];
1871 }
1872
1873 // fill code-like entries with r
1874 f = 1 << (k - w);
1875 for (j = i >> w; j < z; j += f)
1876 q[j] = r;
1877
1878 // backwards increment the k-bit code i
1879 for (j = 1 << (k - 1); i & j; j >>= 1)
1880 i ^= j;
1881 i ^= j;
1882
1883 // backup over finished tables
1884 mask = (1 << w) - 1; // needed on HP, cc -O bug
1885 while ((i & mask) != x[h])
1886 {
1887 h--; // don't need to update q
1888 w -= l;
1889 mask = (1 << w) - 1;
1890 }
1891 }
1892 }
1893
1894
1895 // Return Z_BUF_ERROR if we were given an incomplete table
1896 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
1897}
1898
1899
1901 uInt* c, // 19 code lengths
1902 uInt* bb, // bits tree desired/actual depth
1903 inflate_huft * *tb, // bits tree result
1904 inflate_huft* hp, // space for trees
1905 z_streamp z) // for messages
1906{
1907 int r;
1908 uInt hn = 0; // hufts used in space
1909 uInt* v; // work area for huft_build
1910
1911 if ((v = (uInt*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
1912 return Z_MEM_ERROR;
1913 r = huft_build(c, 19, 19, (uInt*)Z_NULL, (uInt*)Z_NULL,
1914 tb, bb, hp, &hn, v);
1915 if (r == Z_DATA_ERROR)
1916 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
1917 else if (r == Z_BUF_ERROR || *bb == 0)
1918 {
1919 z->msg = (char*)"incomplete dynamic bit lengths tree";
1920 r = Z_DATA_ERROR;
1921 }
1922 ZFREE(z, v);
1923 return r;
1924}
1925
1926
1928 uInt nl, // number of literal/length codes
1929 uInt nd, // number of distance codes
1930 uInt* c, // that many (total) code lengths
1931 uInt* bl, // literal desired/actual bit depth
1932 uInt* bd, // distance desired/actual bit depth
1933 inflate_huft * *tl, // literal/length tree result
1934 inflate_huft * *td, // distance tree result
1935 inflate_huft* hp, // space for trees
1936 z_streamp z) // for messages
1937{
1938 int r;
1939 uInt hn = 0; // hufts used in space
1940 uInt* v; // work area for huft_build
1941
1942 // allocate work area
1943 if ((v = (uInt*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
1944 return Z_MEM_ERROR;
1945
1946 // build literal/length tree
1947 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
1948 if (r != Z_OK || *bl == 0)
1949 {
1950 if (r == Z_DATA_ERROR)
1951 z->msg = (char*)"oversubscribed literal/length tree";
1952 else if (r != Z_MEM_ERROR)
1953 {
1954 z->msg = (char*)"incomplete literal/length tree";
1955 r = Z_DATA_ERROR;
1956 }
1957 ZFREE(z, v);
1958 return r;
1959 }
1960
1961 // build distance tree
1962 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
1963 if (r != Z_OK || (*bd == 0 && nl > 257))
1964 {
1965 if (r == Z_DATA_ERROR)
1966 z->msg = (char*)"oversubscribed distance tree";
1967 else if (r == Z_BUF_ERROR)
1968 {
1969 z->msg = (char*)"incomplete distance tree";
1970 r = Z_DATA_ERROR;
1971 }
1972 else if (r != Z_MEM_ERROR)
1973 {
1974 z->msg = (char*)"empty distance tree with lengths";
1975 r = Z_DATA_ERROR;
1976 }
1977 ZFREE(z, v);
1978 return r;
1979 }
1980
1981 // done
1982 ZFREE(z, v);
1983 return Z_OK;
1984}
1985
1986
1987
1988
1989
1991 uInt* bl, // literal desired/actual bit depth
1992 uInt* bd, // distance desired/actual bit depth
1993 const inflate_huft * * tl, // literal/length tree result
1994 const inflate_huft * *td, // distance tree result
1995 z_streamp ) // for memory allocation
1996{
1997 *bl = fixed_bl;
1998 *bd = fixed_bd;
1999 *tl = fixed_tl;
2000 *td = fixed_td;
2001 return Z_OK;
2002}
2003
2004
2005// inffast.c -- process literals and length/distance pairs fast
2006// Copyright (C) 1995-1998 Mark Adler
2007// For conditions of distribution and use, see copyright notice in zlib.h
2008//
2009
2010
2011//struct inflate_codes_state {int dummy;}; // for buggy compilers
2012
2013
2014// macros for bit input with no checking and for returning unused bytes
2015#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
2016#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
2017
2018// Called with number of bytes left to write in window at least 258
2019// (the maximum string length) and number of input bytes available
2020// at least ten. The ten bytes are six bytes for the longest length/
2021// distance pair plus four bytes for overloading the bit buffer.
2022
2024 uInt bl, uInt bd,
2025 const inflate_huft* tl,
2026 const inflate_huft* td, // need separate declaration for Borland C++
2028 z_streamp z)
2029{
2030 const inflate_huft* t; // temporary pointer
2031 uInt e; // extra bits or operation
2032 uLong b; // bit buffer
2033 uInt k; // bits in bit buffer
2034 Byte* p; // input data pointer
2035 uInt n; // bytes available there
2036 Byte* q; // output window write pointer
2037 uInt m; // bytes to end of window or read pointer
2038 uInt ml; // mask for literal/length tree
2039 uInt md; // mask for distance tree
2040 uInt c; // bytes to copy
2041 uInt d; // distance back to copy from
2042 Byte* r; // copy source pointer
2043
2044 // load input, output, bit values
2045 LOAD
2046
2047 // initialize masks
2048 ml = inflate_mask[bl];
2049 md = inflate_mask[bd];
2050
2051 // do until not enough input or output space for fast loop
2052 do // assume called with m >= 258 && n >= 10
2053 {
2054 // get literal/length code
2055 GRABBITS(20) // max bits for literal/length code
2056 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
2057 {
2058 DUMPBITS(t->bits)
2059 LuTracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
2060 "inflate: * literal '%c'\n" :
2061 "inflate: * literal 0x%02x\n", t->base));
2062 *q++ = (Byte)t->base;
2063 m--;
2064 continue;
2065 }
2066 for (;;)
2067 {
2068 DUMPBITS(t->bits)
2069 if (e & 16)
2070 {
2071 // get extra bits for length
2072 e &= 15;
2073 c = t->base + ((uInt)b & inflate_mask[e]);
2074 DUMPBITS(e)
2075 LuTracevv((stderr, "inflate: * length %u\n", c));
2076
2077 // decode distance base of block to copy
2078 GRABBITS(15); // max bits for distance code
2079 e = (t = td + ((uInt)b & md))->exop;
2080 for (;;)
2081 {
2082 DUMPBITS(t->bits)
2083 if (e & 16)
2084 {
2085 // get extra bits to add to distance base
2086 e &= 15;
2087 GRABBITS(e) // get extra bits (up to 13)
2088 d = t->base + ((uInt)b & inflate_mask[e]);
2089 DUMPBITS(e)
2090 LuTracevv((stderr, "inflate: * distance %u\n", d));
2091
2092 // do the copy
2093 m -= c;
2094 r = q - d;
2095 if (r < s->window) // wrap if needed
2096 {
2097 do
2098 {
2099 r += s->end - s->window; // force pointer in window
2100 }
2101 while (r < s->window); // covers invalid distances
2102 e = (uInt) (s->end - r);
2103 if (c > e)
2104 {
2105 c -= e; // wrapped copy
2106 do
2107 {
2108 *q++ = *r++;
2109 }
2110 while (--e);
2111 r = s->window;
2112 do
2113 {
2114 *q++ = *r++;
2115 }
2116 while (--c);
2117 }
2118 else // normal copy
2119 {
2120 *q++ = *r++;
2121 c--;
2122 *q++ = *r++;
2123 c--;
2124 do
2125 {
2126 *q++ = *r++;
2127 }
2128 while (--c);
2129 }
2130 }
2131 else /* normal copy */
2132 {
2133 *q++ = *r++;
2134 c--;
2135 *q++ = *r++;
2136 c--;
2137 do
2138 {
2139 *q++ = *r++;
2140 }
2141 while (--c);
2142 }
2143 break;
2144 }
2145 else if ((e & 64) == 0)
2146 {
2147 t += t->base;
2148 e = (t += ((uInt)b & inflate_mask[e]))->exop;
2149 }
2150 else
2151 {
2152 z->msg = (char*)"invalid distance code";
2153 UNGRAB
2154 UPDATE
2155 return Z_DATA_ERROR;
2156 }
2157 };
2158 break;
2159 }
2160 if ((e & 64) == 0)
2161 {
2162 t += t->base;
2163 if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
2164 {
2165 DUMPBITS(t->bits)
2166 LuTracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
2167 "inflate: * literal '%c'\n" :
2168 "inflate: * literal 0x%02x\n", t->base));
2169 *q++ = (Byte)t->base;
2170 m--;
2171 break;
2172 }
2173 }
2174 else if (e & 32)
2175 {
2176 LuTracevv((stderr, "inflate: * end of block\n"));
2177 UNGRAB
2178 UPDATE
2179 return Z_STREAM_END;
2180 }
2181 else
2182 {
2183 z->msg = (char*)"invalid literal/length code";
2184 UNGRAB
2185 UPDATE
2186 return Z_DATA_ERROR;
2187 }
2188 };
2189 }
2190 while (m >= 258 && n >= 10);
2191
2192 // not enough input or output--restore pointers and return
2193 UNGRAB
2194 UPDATE
2195 return Z_OK;
2196}
2197
2198
2199
2200
2201
2202
2203// crc32.c -- compute the CRC-32 of a data stream
2204// Copyright (C) 1995-1998 Mark Adler
2205// For conditions of distribution and use, see copyright notice in zlib.h
2206
2207// @(#) $Id$
2208
2209
2210
2211
2212
2213
2214// Table of CRC-32's of all single-byte values (made by make_crc_table)
2215const uLong crc_table[256] =
2216{
2217 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
2218 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
2219 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
2220 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
2221 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
2222 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
2223 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
2224 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
2225 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
2226 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
2227 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
2228 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
2229 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
2230 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
2231 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
2232 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
2233 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
2234 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
2235 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
2236 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
2237 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
2238 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
2239 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
2240 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
2241 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
2242 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
2243 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
2244 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
2245 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
2246 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
2247 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
2248 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
2249 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
2250 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
2251 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
2252 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
2253 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
2254 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
2255 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
2256 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
2257 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
2258 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
2259 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
2260 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
2261 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
2262 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
2263 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
2264 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
2265 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
2266 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
2267 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
2268 0x2d02ef8dL
2269};
2270
2272{
2273 return (const uLong*)crc_table;
2274}
2275
2276#define CRC_DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
2277#define CRC_DO2(buf) CRC_DO1(buf); CRC_DO1(buf);
2278#define CRC_DO4(buf) CRC_DO2(buf); CRC_DO2(buf);
2279#define CRC_DO8(buf) CRC_DO4(buf); CRC_DO4(buf);
2280
2281uLong ucrc32(uLong crc, const Byte* buf, uInt len)
2282{
2283 if (buf == Z_NULL)
2284 return 0L;
2285 crc = crc ^ 0xffffffffL;
2286 while (len >= 8)
2287 {
2288 CRC_DO8(buf);
2289 len -= 8;
2290 }
2291 if (len) do
2292 {
2293 CRC_DO1(buf);
2294 }
2295 while (--len);
2296 return crc ^ 0xffffffffL;
2297}
2298
2299
2300
2301// =============================================================
2302// some decryption routines
2303#define CRC32(c, b) (crc_table[((int)(c)^(b))&0xff]^((c)>>8))
2304void Uupdate_keys(unsigned long* keys, char c)
2305{
2306 keys[0] = CRC32(keys[0], c);
2307 keys[1] += keys[0] & 0xFF;
2308 keys[1] = keys[1] * 134775813L + 1;
2309 keys[2] = CRC32(keys[2], keys[1] >> 24);
2310}
2311char Udecrypt_byte(unsigned long* keys)
2312{
2313 unsigned temp = ((unsigned)keys[2] & 0xffff) | 2;
2314 return (char)(((temp * (temp ^ 1)) >> 8) & 0xff);
2315}
2316char zdecode(unsigned long* keys, char c)
2317{
2318 c ^= Udecrypt_byte(keys);
2319 Uupdate_keys(keys, c);
2320 return c;
2321}
2322
2323
2324
2325// adler32.c -- compute the Adler-32 checksum of a data stream
2326// Copyright (C) 1995-1998 Mark Adler
2327// For conditions of distribution and use, see copyright notice in zlib.h
2328
2329// @(#) $Id$
2330
2331
2332#define BASE 65521L // largest prime smaller than 65536
2333#define NMAX 5552
2334// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
2335
2336#define AD_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
2337#define AD_DO2(buf,i) AD_DO1(buf,i); AD_DO1(buf,i+1);
2338#define AD_DO4(buf,i) AD_DO2(buf,i); AD_DO2(buf,i+2);
2339#define AD_DO8(buf,i) AD_DO4(buf,i); AD_DO4(buf,i+4);
2340#define AD_DO16(buf) AD_DO8(buf,0); AD_DO8(buf,8);
2341
2342// =========================================================================
2343uLong adler32(uLong adler, const Byte* buf, uInt len)
2344{
2345 unsigned long s1 = adler & 0xffff;
2346 unsigned long s2 = (adler >> 16) & 0xffff;
2347 int k;
2348
2349 if (buf == Z_NULL)
2350 return 1L;
2351
2352 while (len > 0)
2353 {
2354 k = len < NMAX ? len : NMAX;
2355 len -= k;
2356 while (k >= 16)
2357 {
2358 AD_DO16(buf);
2359 buf += 16;
2360 k -= 16;
2361 }
2362 if (k != 0)
2363 {
2364 do
2365 {
2366 s1 += *buf++;
2367 s2 += s1;
2368 }
2369 while (--k);
2370 }
2371 s1 %= BASE;
2372 s2 %= BASE;
2373 }
2374 return (s2 << 16) | s1;
2375}
2376
2377
2378
2379// zutil.c -- target dependent utility functions for the compression library
2380// Copyright (C) 1995-1998 Jean-loup Gailly.
2381// For conditions of distribution and use, see copyright notice in zlib.h
2382// @(#) $Id$
2383
2384
2385
2386
2387
2388
2389const char* zlibVersion()
2390{
2391 return ZLIB_VERSION;
2392}
2393
2394// exported to allow conversion of error code to string for compress() and
2395// uncompress()
2396const char* zError(int err)
2397{
2398 return ERR_MSG(err);
2399}
2400
2401
2402
2403
2404voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
2405{
2406 if (opaque)
2407 items += size - size; // make compiler happy
2408 return (voidpf)calloc(items, size);
2409}
2410
2411void zcfree (voidpf opaque, voidpf ptr)
2412{
2413 zfree(ptr);
2414 if (opaque)
2415 return; // make compiler happy
2416}
2417
2418
2419
2420// inflate.c -- zlib interface to inflate modules
2421// Copyright (C) 1995-1998 Mark Adler
2422// For conditions of distribution and use, see copyright notice in zlib.h
2423
2424//struct inflate_blocks_state {int dummy;}; // for buggy compilers
2425
2426typedef enum
2427{
2428 IM_METHOD, // waiting for method byte
2429 IM_FLAG, // waiting for flag byte
2430 IM_DICT4, // four dictionary check bytes to go
2431 IM_DICT3, // three dictionary check bytes to go
2432 IM_DICT2, // two dictionary check bytes to go
2433 IM_DICT1, // one dictionary check byte to go
2434 IM_DICT0, // waiting for inflateSetDictionary
2435 IM_BLOCKS, // decompressing blocks
2436 IM_CHECK4, // four check bytes to go
2437 IM_CHECK3, // three check bytes to go
2438 IM_CHECK2, // two check bytes to go
2439 IM_CHECK1, // one check byte to go
2440 IM_DONE, // finished check, done
2441 IM_BAD
2442} // got an error--stay here
2444
2445// inflate private state
2447{
2448
2449 // mode
2450 inflate_mode mode; // current inflate mode
2451
2452 // mode dependent information
2453 union
2454 {
2455 uInt method; // if IM_FLAGS, method byte
2456 struct
2457 {
2458 uLong was; // computed check value
2459 uLong need; // stream check value
2460 } check; // if CHECK, check values to compare
2461 uInt marker; // if IM_BAD, inflateSync's marker bytes count
2462 } sub; // submode
2463
2464 // mode independent information
2465 int nowrap; // flag for no wrapper
2466 uInt wbits; // log2(window size) (8..15, defaults to 15)
2468 * blocks; // current inflate_blocks state
2469
2470};
2471
2473{
2474 if (z == Z_NULL || z->state == Z_NULL)
2475 return Z_STREAM_ERROR;
2476 z->total_in = z->total_out = 0;
2477 z->msg = Z_NULL;
2478 z->state->mode = z->state->nowrap ? IM_BLOCKS : IM_METHOD;
2480 LuTracev((stderr, "inflate: reset\n"));
2481 return Z_OK;
2482}
2483
2485{
2486 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
2487 return Z_STREAM_ERROR;
2488 if (z->state->blocks != Z_NULL)
2490 ZFREE(z, z->state);
2491 z->state = Z_NULL;
2492 LuTracev((stderr, "inflate: end\n"));
2493 return Z_OK;
2494}
2495
2496
2498{
2499 const char* version = ZLIB_VERSION;
2500 int stream_size = sizeof(z_stream);
2501 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || stream_size != sizeof(z_stream))
2502 return Z_VERSION_ERROR;
2503
2504 int w = -15; // MAX_WBITS: 32K LZ77 window.
2505 // Warning: reducing MAX_WBITS makes minigzip unable to extract .gz files created by gzip.
2506 // The memory requirements for deflate are (in bytes):
2507 // (1 << (windowBits+2)) + (1 << (memLevel+9))
2508 // that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
2509 // plus a few kilobytes for small objects. For example, if you want to reduce
2510 // the default memory requirements from 256K to 128K, compile with
2511 // make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
2512 // Of course this will generally degrade compression (there's no free lunch).
2513 //
2514 // The memory requirements for inflate are (in bytes) 1 << windowBits
2515 // that is, 32K for windowBits=15 (default value) plus a few kilobytes
2516 // for small objects.
2517
2518 // initialize state
2519 if (z == Z_NULL)
2520 return Z_STREAM_ERROR;
2521 z->msg = Z_NULL;
2522 if (z->zalloc == Z_NULL)
2523 {
2524 z->zalloc = zcalloc;
2525 z->opaque = (voidpf)0;
2526 }
2527 if (z->zfree == Z_NULL)
2528 z->zfree = zcfree;
2529 if ((z->state = (struct internal_state*)
2530 ZALLOC(z, 1, sizeof(struct internal_state))) == Z_NULL)
2531 return Z_MEM_ERROR;
2532 z->state->blocks = Z_NULL;
2533
2534 // handle undocumented nowrap option (no zlib header or check)
2535 z->state->nowrap = 0;
2536 if (w < 0)
2537 {
2538 w = - w;
2539 z->state->nowrap = 1;
2540 }
2541
2542 // set window size
2543 if (w < 8 || w > 15)
2544 {
2545 inflateEnd(z);
2546 return Z_STREAM_ERROR;
2547 }
2548 z->state->wbits = (uInt)w;
2549
2550 // create inflate_blocks state
2551 if ((z->state->blocks =
2552 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
2553 == Z_NULL)
2554 {
2555 inflateEnd(z);
2556 return Z_MEM_ERROR;
2557 }
2558 LuTracev((stderr, "inflate: allocated\n"));
2559
2560 // reset state
2561 inflateReset(z);
2562 return Z_OK;
2563}
2564
2565
2566
2567#define IM_NEEDBYTE {if(z->avail_in==0)return r;r=f;}
2568#define IM_NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
2569
2570int inflate(z_streamp z, int f)
2571{
2572 int r;
2573 uInt b;
2574
2575 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
2576 return Z_STREAM_ERROR;
2577 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
2578 r = Z_BUF_ERROR;
2579 for (;;)
2580 {
2581 switch (z->state->mode)
2582 {
2583 case IM_METHOD:
2585 if (((z->state->sub.method = IM_NEXTBYTE) & 0xf) != Z_DEFLATED)
2586 {
2587 z->state->mode = IM_BAD;
2588 z->msg = (char*)"unknown compression method";
2589 z->state->sub.marker = 5; // can't try inflateSync
2590 break;
2591 }
2592 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
2593 {
2594 z->state->mode = IM_BAD;
2595 z->msg = (char*)"invalid window size";
2596 z->state->sub.marker = 5; // can't try inflateSync
2597 break;
2598 }
2599 z->state->mode = IM_FLAG;
2600 case IM_FLAG:
2602 b = IM_NEXTBYTE;
2603 if (((z->state->sub.method << 8) + b) % 31)
2604 {
2605 z->state->mode = IM_BAD;
2606 z->msg = (char*)"incorrect header check";
2607 z->state->sub.marker = 5; // can't try inflateSync
2608 break;
2609 }
2610 LuTracev((stderr, "inflate: zlib header ok\n"));
2611 if (!(b & PRESET_DICT))
2612 {
2613 z->state->mode = IM_BLOCKS;
2614 break;
2615 }
2616 z->state->mode = IM_DICT4;
2617 case IM_DICT4:
2619 z->state->sub.check.need = (uLong)IM_NEXTBYTE << 24;
2620 z->state->mode = IM_DICT3;
2621 case IM_DICT3:
2623 z->state->sub.check.need += (uLong)IM_NEXTBYTE << 16;
2624 z->state->mode = IM_DICT2;
2625 case IM_DICT2:
2627 z->state->sub.check.need += (uLong)IM_NEXTBYTE << 8;
2628 z->state->mode = IM_DICT1;
2629 case IM_DICT1:
2631 r;
2633 z->adler = z->state->sub.check.need;
2634 z->state->mode = IM_DICT0;
2635 return Z_NEED_DICT;
2636 case IM_DICT0:
2637 z->state->mode = IM_BAD;
2638 z->msg = (char*)"need dictionary";
2639 z->state->sub.marker = 0; // can try inflateSync
2640 return Z_STREAM_ERROR;
2641 case IM_BLOCKS:
2642 r = inflate_blocks(z->state->blocks, z, r);
2643 if (r == Z_DATA_ERROR)
2644 {
2645 z->state->mode = IM_BAD;
2646 z->state->sub.marker = 0; // can try inflateSync
2647 break;
2648 }
2649 if (r == Z_OK)
2650 r = f;
2651 if (r != Z_STREAM_END)
2652 return r;
2653 r = f;
2655 if (z->state->nowrap)
2656 {
2657 z->state->mode = IM_DONE;
2658 break;
2659 }
2660 z->state->mode = IM_CHECK4;
2661 case IM_CHECK4:
2663 z->state->sub.check.need = (uLong)IM_NEXTBYTE << 24;
2664 z->state->mode = IM_CHECK3;
2665 case IM_CHECK3:
2667 z->state->sub.check.need += (uLong)IM_NEXTBYTE << 16;
2668 z->state->mode = IM_CHECK2;
2669 case IM_CHECK2:
2671 z->state->sub.check.need += (uLong)IM_NEXTBYTE << 8;
2672 z->state->mode = IM_CHECK1;
2673 case IM_CHECK1:
2676
2677 if (z->state->sub.check.was != z->state->sub.check.need)
2678 {
2679 z->state->mode = IM_BAD;
2680 z->msg = (char*)"incorrect data check";
2681 z->state->sub.marker = 5; // can't try inflateSync
2682 break;
2683 }
2684 LuTracev((stderr, "inflate: zlib check ok\n"));
2685 z->state->mode = IM_DONE;
2686 case IM_DONE:
2687 return Z_STREAM_END;
2688 case IM_BAD:
2689 return Z_DATA_ERROR;
2690 default:
2691 return Z_STREAM_ERROR;
2692 }
2693 }
2694}
2695
2696
2697
2698
2699
2700// unzip.c -- IO on .zip files using zlib
2701// Version 0.15 beta, Mar 19th, 1998,
2702// Read unzip.h for more info
2703
2704
2705
2706
2707#define UNZ_BUFSIZE (16384)
2708#define UNZ_MAXFILENAMEINZIP (256)
2709#define SIZECENTRALDIRITEM (0x2e)
2710#define SIZEZIPLOCALHEADER (0x1e)
2711
2712
2713
2714
2715const char unz_copyright[] = " unzip 0.15 Copyright 1998 Gilles Vollant ";
2716
2717// unz_file_info_interntal contain internal info about a file in zipfile
2719{
2720 uLong offset_curfile;// relative offset of local header 4 bytes
2722
2723
2724typedef struct
2725{
2726 bool is_handle; // either a handle or memory
2728 // for handles:
2729 HANDLE h;
2730 bool herr;
2731 unsigned long initial_offset;
2733 // for memory:
2734 void* buf;
2735 unsigned int len, pos; // if it's a memory block
2736} LUFILE;
2737
2738
2739LUFILE* lufopen(void* z, unsigned int len, DWORD flags, ZRESULT* err)
2740{
2741 if (flags != ZIP_HANDLE && flags != ZIP_FILENAME && flags != ZIP_MEMORY)
2742 {
2743 *err = ZR_ARGS;
2744 return NULL;
2745 }
2746 //
2747 HANDLE h = 0;
2748 bool canseek = false;
2749 *err = ZR_OK;
2750 bool mustclosehandle = false;
2751 if (flags == ZIP_HANDLE || flags == ZIP_FILENAME)
2752 {
2753 if (flags == ZIP_HANDLE)
2754 {
2755 HANDLE hf = z;
2756 h = hf;
2757 mustclosehandle = false;
2758#ifdef DuplicateHandle
2759 BOOL res = DuplicateHandle(GetCurrentProcess(), hf, GetCurrentProcess(), &h, 0, FALSE, DUPLICATE_SAME_ACCESS);
2760 if (!res) mustclosehandle = true;
2761#endif
2762 }
2763 else
2764 {
2765 h = CreateFile((const TCHAR*)z, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2766 if (h == INVALID_HANDLE_VALUE)
2767 {
2768 *err = ZR_NOFILE;
2769 return NULL;
2770 }
2771 mustclosehandle = true;
2772 }
2773 // test if we can seek on it. We can't use GetFileType(h)==FILE_TYPE_DISK since it's not on CE.
2774 DWORD res = SetFilePointer(h, 0, 0, FILE_CURRENT);
2775 canseek = (res != 0xFFFFFFFF);
2776 }
2777 LUFILE* lf = new LUFILE;
2778 if (flags == ZIP_HANDLE || flags == ZIP_FILENAME)
2779 {
2780 lf->is_handle = true;
2781 lf->mustclosehandle = mustclosehandle;
2782 lf->canseek = canseek;
2783 lf->h = h;
2784 lf->herr = false;
2785 lf->initial_offset = 0;
2786 if (canseek)
2787 lf->initial_offset = SetFilePointer(h, 0, NULL, FILE_CURRENT);
2788 }
2789 else
2790 {
2791 lf->is_handle = false;
2792 lf->canseek = true;
2793 lf->mustclosehandle = false;
2794 lf->buf = z;
2795 lf->len = len;
2796 lf->pos = 0;
2797 lf->initial_offset = 0;
2798 }
2799 *err = ZR_OK;
2800 return lf;
2801}
2802
2803
2804int lufclose(LUFILE* stream)
2805{
2806 if (stream == NULL)
2807 return EOF;
2808 if (stream->mustclosehandle)
2809 CloseHandle(stream->h);
2810 delete stream;
2811 return 0;
2812}
2813
2814int luferror(LUFILE* stream)
2815{
2816 if (stream->is_handle && stream->herr)
2817 return 1;
2818 else return 0;
2819}
2820
2821long int luftell(LUFILE* stream)
2822{
2823 if (stream->is_handle && stream->canseek)
2824 return SetFilePointer(stream->h, 0, NULL, FILE_CURRENT) - stream->initial_offset;
2825 else if (stream->is_handle)
2826 return 0;
2827 else
2828 return stream->pos;
2829}
2830
2831int lufseek(LUFILE* stream, long offset, int whence)
2832{
2833 if (stream->is_handle && stream->canseek)
2834 {
2835 if (whence == SEEK_SET)
2836 SetFilePointer(stream->h, stream->initial_offset + offset, 0, FILE_BEGIN);
2837 else if (whence == SEEK_CUR)
2838 SetFilePointer(stream->h, offset, NULL, FILE_CURRENT);
2839 else if (whence == SEEK_END)
2840 SetFilePointer(stream->h, offset, NULL, FILE_END);
2841 else
2842 return 19; // EINVAL
2843 return 0;
2844 }
2845 else if (stream->is_handle)
2846 return 29; // ESPIPE
2847 else
2848 {
2849 if (whence == SEEK_SET)
2850 stream->pos = offset;
2851 else if (whence == SEEK_CUR)
2852 stream->pos += offset;
2853 else if (whence == SEEK_END)
2854 stream->pos = stream->len + offset;
2855 return 0;
2856 }
2857}
2858
2859
2860size_t lufread(void* ptr, size_t size, size_t n, LUFILE* stream)
2861{
2862 unsigned int toread = (unsigned int)(size * n);
2863 if (stream->is_handle)
2864 {
2865 DWORD red;
2866 BOOL res = ReadFile(stream->h, ptr, toread, &red, NULL);
2867 if (!res)
2868 stream->herr = true;
2869 return red / size;
2870 }
2871 if (stream->pos + toread > stream->len)
2872 toread = stream->len - stream->pos;
2873 memcpy(ptr, (char*)stream->buf + stream->pos, toread);
2874 DWORD red = toread;
2875 stream->pos += red;
2876 return red / size;
2877}
2878
2879
2880
2881
2882// file_in_zip_read_info_s contain internal information about a file in zipfile,
2883// when reading and decompress it
2884typedef struct
2885{
2886 char* read_buffer; // internal buffer for compressed data
2887 z_stream stream; // zLib stream structure for inflate
2888
2889 uLong pos_in_zipfile; // position in byte on the zipfile, for fseek
2890 uLong stream_initialised; // flag set if stream structure is initialised
2891
2892 uLong offset_local_extrafield;// offset of the local extra field
2893 uInt size_local_extrafield;// size of the local extra field
2894 uLong pos_local_extrafield; // position in the local extra field in read
2895
2896 uLong crc32; // crc32 of all data uncompressed
2897 uLong crc32_wait; // crc32 we must obtain after decompress all
2898 uLong rest_read_compressed; // number of byte to be decompressed
2899 uLong rest_read_uncompressed;//number of byte to be obtained after decomp
2900 LUFILE* file; // io structore of the zipfile
2901 uLong compression_method; // compression method (0==store)
2902 uLong byte_before_the_zipfile;// byte before the zipfile, (>0 for sfx)
2903 bool encrypted; // is it encrypted?
2904 unsigned long keys[3]; // decryption keys, initialized by unzOpenCurrentFile
2905 int encheadleft; // the first call(s) to unzReadCurrentFile will read this many encryption-header bytes first
2906 char crcenctest; // if encrypted, we'll check the encryption buffer against this
2908
2909
2910// unz_s contain internal information about the zipfile
2911typedef struct
2912{
2913 LUFILE* file; // io structore of the zipfile
2914 unz_global_info gi; // public global information
2915 uLong byte_before_the_zipfile;// byte before the zipfile, (>0 for sfx)
2916 uLong num_file; // number of the current file in the zipfile
2917 uLong pos_in_central_dir; // pos of the current file in the central dir
2918 uLong current_file_ok; // flag about the usability of the current file
2919 uLong central_pos; // position of the beginning of the central dir
2920
2921 uLong size_central_dir; // size of the central directory
2922 uLong offset_central_dir; // offset of start of central directory with respect to the starting disk number
2923
2924 unz_file_info cur_file_info; // public info about the current file in zip
2926 file_in_zip_read_info_s* pfile_in_zip_read; // structure about the current file if we are decompressing it
2928
2929
2930int unzStringFileNameCompare (const char* fileName1, const char* fileName2, int iCaseSensitivity);
2931// Compare two filename (fileName1,fileName2).
2932
2933z_off_t unztell (unzFile file);
2934// Give the current position in uncompressed data
2935
2936int unzeof (unzFile file);
2937// return 1 if the end of file was reached, 0 elsewhere
2938
2939int unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len);
2940// Read extra field from the current file (opened by unzOpenCurrentFile)
2941// This is the local-header version of the extra field (sometimes, there is
2942// more info in the local-header version than in the central-header)
2943//
2944// if buf==NULL, it return the size of the local extra field
2945//
2946// if buf!=NULL, len is the size of the buffer, the extra header is copied in
2947// buf.
2948// the return value is the number of bytes copied in buf, or (if <0)
2949// the error code
2950
2951
2952
2953// ===========================================================================
2954// Read a byte from a gz_stream; update next_in and avail_in. Return EOF
2955// for end of file.
2956// IN assertion: the stream s has been sucessfully opened for reading.
2957
2958int unzlocal_getByte(LUFILE* fin, int* pi)
2959{
2960 unsigned char c;
2961 int err = (int)lufread(&c, 1, 1, fin);
2962 if (err == 1)
2963 {
2964 *pi = (int)c;
2965 return UNZ_OK;
2966 }
2967 else
2968 {
2969 if (luferror(fin))
2970 return UNZ_ERRNO;
2971 else
2972 return UNZ_EOF;
2973 }
2974}
2975
2976
2977// ===========================================================================
2978// Reads a long in LSB order from the given gz_stream. Sets
2980{
2981 uLong x ;
2982 int i;
2983 int err;
2984
2985 err = unzlocal_getByte(fin, &i);
2986 x = (uLong)i;
2987
2988 if (err == UNZ_OK)
2989 err = unzlocal_getByte(fin, &i);
2990 x += ((uLong)i) << 8;
2991
2992 if (err == UNZ_OK)
2993 *pX = x;
2994 else
2995 *pX = 0;
2996 return err;
2997}
2998
3000{
3001 uLong x ;
3002 int i;
3003 int err;
3004
3005 err = unzlocal_getByte(fin, &i);
3006 x = (uLong)i;
3007
3008 if (err == UNZ_OK)
3009 err = unzlocal_getByte(fin, &i);
3010 x += ((uLong)i) << 8;
3011
3012 if (err == UNZ_OK)
3013 err = unzlocal_getByte(fin, &i);
3014 x += ((uLong)i) << 16;
3015
3016 if (err == UNZ_OK)
3017 err = unzlocal_getByte(fin, &i);
3018 x += ((uLong)i) << 24;
3019
3020 if (err == UNZ_OK)
3021 *pX = x;
3022 else
3023 *pX = 0;
3024 return err;
3025}
3026
3027
3028// My own strcmpi / strcasecmp
3029int strcmpcasenosensitive_internal (const char* fileName1, const char* fileName2)
3030{
3031 for (;;)
3032 {
3033 char c1 = *(fileName1++);
3034 char c2 = *(fileName2++);
3035 if ((c1 >= 'a') && (c1 <= 'z'))
3036 c1 -= (char)0x20;
3037 if ((c2 >= 'a') && (c2 <= 'z'))
3038 c2 -= (char)0x20;
3039 if (c1 == '\0')
3040 return ((c2 == '\0') ? 0 : -1);
3041 if (c2 == '\0')
3042 return 1;
3043 if (c1 < c2)
3044 return -1;
3045 if (c1 > c2)
3046 return 1;
3047 }
3048}
3049
3050
3051
3052
3053//
3054// Compare two filename (fileName1,fileName2).
3055// If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
3056// If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi or strcasecmp)
3057//
3058int unzStringFileNameCompare (const char* fileName1, const char* fileName2, int iCaseSensitivity)
3059{
3060 if (iCaseSensitivity == 1)
3061 return strcmp(fileName1, fileName2);
3062 else
3063 return strcmpcasenosensitive_internal(fileName1, fileName2);
3064}
3065
3066#define BUFREADCOMMENT (0x400)
3067
3068
3069// Locate the Central directory of a zipfile (at the end, just before
3070// the global comment). Lu bugfix 2005.07.26 - returns 0xFFFFFFFF if not found,
3071// rather than 0, since 0 is a valid central-dir-location for an empty zipfile.
3073{
3074 if (lufseek(fin, 0, SEEK_END) != 0)
3075 return 0xFFFFFFFF;
3076 uLong uSizeFile = luftell(fin);
3077
3078 uLong uMaxBack = 0xffff; // maximum size of global comment
3079 if (uMaxBack > uSizeFile)
3080 uMaxBack = uSizeFile;
3081
3082 unsigned char* buf = (unsigned char*)zmalloc(BUFREADCOMMENT + 4);
3083 if (buf == NULL)
3084 return 0xFFFFFFFF;
3085 uLong uPosFound = 0xFFFFFFFF;
3086
3087 uLong uBackRead = 4;
3088 while (uBackRead < uMaxBack)
3089 {
3090 uLong uReadSize, uReadPos ;
3091 int i;
3092 if (uBackRead + BUFREADCOMMENT > uMaxBack)
3093 uBackRead = uMaxBack;
3094 else
3095 uBackRead += BUFREADCOMMENT;
3096 uReadPos = uSizeFile - uBackRead ;
3097 uReadSize = ((BUFREADCOMMENT + 4) < (uSizeFile - uReadPos)) ? (BUFREADCOMMENT + 4) : (uSizeFile - uReadPos);
3098 if (lufseek(fin, uReadPos, SEEK_SET) != 0)
3099 break;
3100 if (lufread(buf, (uInt)uReadSize, 1, fin) != 1)
3101 break;
3102 for (i = (int)uReadSize - 3; (i--) >= 0;)
3103 {
3104 if (((*(buf + i)) == 0x50) && ((*(buf + i + 1)) == 0x4b) && ((*(buf + i + 2)) == 0x05) && ((*(buf + i + 3)) == 0x06))
3105 {
3106 uPosFound = uReadPos + i;
3107 break;
3108 }
3109 }
3110 if (uPosFound != 0)
3111 break;
3112 }
3113 if (buf) zfree(buf);
3114 return uPosFound;
3115}
3116
3117
3118int unzGoToFirstFile (unzFile file);
3119int unzCloseCurrentFile (unzFile file);
3120
3121// Open a Zip file.
3122// If the zipfile cannot be opened (file don't exist or in not valid), return NULL.
3123// Otherwise, the return value is a unzFile Handle, usable with other unzip functions
3125{
3126 if (fin == NULL)
3127 return NULL;
3128 if (unz_copyright[0] != ' ')
3129 {
3130 lufclose(fin);
3131 return NULL;
3132 }
3133
3134 int err = UNZ_OK;
3135 unz_s us;
3136 uLong central_pos, uL;
3137 central_pos = unzlocal_SearchCentralDir(fin);
3138 if (central_pos == 0xFFFFFFFF)
3139 err = UNZ_ERRNO;
3140 if (lufseek(fin, central_pos, SEEK_SET) != 0)
3141 err = UNZ_ERRNO;
3142 // the signature, already checked
3143 if (unzlocal_getLong(fin, &uL) != UNZ_OK)
3144 err = UNZ_ERRNO;
3145 // number of this disk
3146 uLong number_disk; // number of the current dist, used for spanning ZIP, unsupported, always 0
3147 if (unzlocal_getShort(fin, &number_disk) != UNZ_OK)
3148 err = UNZ_ERRNO;
3149 // number of the disk with the start of the central directory
3150 uLong number_disk_with_CD; // number the the disk with central dir, used for spaning ZIP, unsupported, always 0
3151 if (unzlocal_getShort(fin, &number_disk_with_CD) != UNZ_OK)
3152 err = UNZ_ERRNO;
3153 // total number of entries in the central dir on this disk
3154 if (unzlocal_getShort(fin, &us.gi.number_entry) != UNZ_OK)
3155 err = UNZ_ERRNO;
3156 // total number of entries in the central dir
3157 uLong number_entry_CD; // total number of entries in the central dir (same than number_entry on nospan)
3158 if (unzlocal_getShort(fin, &number_entry_CD) != UNZ_OK)
3159 err = UNZ_ERRNO;
3160 if ((number_entry_CD != us.gi.number_entry) || (number_disk_with_CD != 0) || (number_disk != 0))
3161 err = UNZ_BADZIPFILE;
3162 // size of the central directory
3163 if (unzlocal_getLong(fin, &us.size_central_dir) != UNZ_OK)
3164 err = UNZ_ERRNO;
3165 // offset of start of central directory with respect to the starting disk number
3167 err = UNZ_ERRNO;
3168 // zipfile comment length
3169 if (unzlocal_getShort(fin, &us.gi.size_comment) != UNZ_OK)
3170 err = UNZ_ERRNO;
3171 if ((central_pos + fin->initial_offset < us.offset_central_dir + us.size_central_dir) && (err == UNZ_OK))
3172 err = UNZ_BADZIPFILE;
3173 if (err != UNZ_OK)
3174 {
3175 lufclose(fin);
3176 return NULL;
3177 }
3178
3179 us.file = fin;
3181 us.central_pos = central_pos;
3182 us.pfile_in_zip_read = NULL;
3183 fin->initial_offset = 0; // since the zipfile itself is expected to handle this
3184
3185 unz_s* s = (unz_s*)zmalloc(sizeof(unz_s));
3186 *s = us;
3188 return (unzFile)s;
3189}
3190
3191
3192
3193// Close a ZipFile opened with unzipOpen.
3194// If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
3195// these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
3196// return UNZ_OK if there is no problem.
3198{
3199 unz_s* s;
3200 if (file == NULL)
3201 return UNZ_PARAMERROR;
3202 s = (unz_s*)file;
3203
3204 if (s->pfile_in_zip_read != NULL)
3205 unzCloseCurrentFile(file);
3206
3207 lufclose(s->file);
3208 if (s)
3209 zfree(s); // unused s=0;
3210 return UNZ_OK;
3211}
3212
3213
3214// Write info about the ZipFile in the *pglobal_info structure.
3215// No preparation of the structure is needed
3216// return UNZ_OK if there is no problem.
3218{
3219 unz_s* s;
3220 if (file == NULL)
3221 return UNZ_PARAMERROR;
3222 s = (unz_s*)file;
3223 *pglobal_info = s->gi;
3224 return UNZ_OK;
3225}
3226
3227
3228// Translate date/time from Dos format to tm_unz (readable more easilty)
3230{
3231 uLong uDate;
3232 uDate = (uLong)(ulDosDate >> 16);
3233 ptm->tm_mday = (uInt)(uDate & 0x1f) ;
3234 ptm->tm_mon = (uInt)((((uDate) & 0x1E0) / 0x20) - 1) ;
3235 ptm->tm_year = (uInt)(((uDate & 0x0FE00) / 0x0200) + 1980) ;
3236
3237 ptm->tm_hour = (uInt) ((ulDosDate & 0xF800) / 0x800);
3238 ptm->tm_min = (uInt) ((ulDosDate & 0x7E0) / 0x20) ;
3239 ptm->tm_sec = (uInt) (2 * (ulDosDate & 0x1f)) ;
3240}
3241
3242// Get Info about the current file in the zipfile, with internal only info
3244 unz_file_info* pfile_info,
3246 *pfile_info_internal,
3247 char* szFileName,
3248 uLong fileNameBufferSize,
3249 void* extraField,
3250 uLong extraFieldBufferSize,
3251 char* szComment,
3252 uLong commentBufferSize);
3253
3255 unz_file_info_internal* pfile_info_internal, char* szFileName,
3256 uLong fileNameBufferSize, void* extraField, uLong extraFieldBufferSize,
3257 char* szComment, uLong commentBufferSize)
3258{
3259 unz_s* s;
3260 unz_file_info file_info;
3261 unz_file_info_internal file_info_internal;
3262 int err = UNZ_OK;
3263 uLong uMagic;
3264 long lSeek = 0;
3265
3266 if (file == NULL)
3267 return UNZ_PARAMERROR;
3268 s = (unz_s*)file;
3269 if (lufseek(s->file, s->pos_in_central_dir + s->byte_before_the_zipfile, SEEK_SET) != 0)
3270 err = UNZ_ERRNO;
3271
3272
3273 // we check the magic
3274 if (err == UNZ_OK)
3275 {
3276 if (unzlocal_getLong(s->file, &uMagic) != UNZ_OK)
3277 err = UNZ_ERRNO;
3278 else if (uMagic != 0x02014b50)
3279 err = UNZ_BADZIPFILE;
3280 }
3281 if (unzlocal_getShort(s->file, &file_info.version) != UNZ_OK)
3282 err = UNZ_ERRNO;
3283
3284 if (unzlocal_getShort(s->file, &file_info.version_needed) != UNZ_OK)
3285 err = UNZ_ERRNO;
3286
3287 if (unzlocal_getShort(s->file, &file_info.flag) != UNZ_OK)
3288 err = UNZ_ERRNO;
3289
3290 if (unzlocal_getShort(s->file, &file_info.compression_method) != UNZ_OK)
3291 err = UNZ_ERRNO;
3292
3293 if (unzlocal_getLong(s->file, &file_info.dosDate) != UNZ_OK)
3294 err = UNZ_ERRNO;
3295
3296 unzlocal_DosDateToTmuDate(file_info.dosDate, &file_info.tmu_date);
3297
3298 if (unzlocal_getLong(s->file, &file_info.crc) != UNZ_OK)
3299 err = UNZ_ERRNO;
3300
3301 if (unzlocal_getLong(s->file, &file_info.compressed_size) != UNZ_OK)
3302 err = UNZ_ERRNO;
3303
3304 if (unzlocal_getLong(s->file, &file_info.uncompressed_size) != UNZ_OK)
3305 err = UNZ_ERRNO;
3306
3307 if (unzlocal_getShort(s->file, &file_info.size_filename) != UNZ_OK)
3308 err = UNZ_ERRNO;
3309
3310 if (unzlocal_getShort(s->file, &file_info.size_file_extra) != UNZ_OK)
3311 err = UNZ_ERRNO;
3312
3313 if (unzlocal_getShort(s->file, &file_info.size_file_comment) != UNZ_OK)
3314 err = UNZ_ERRNO;
3315
3316 if (unzlocal_getShort(s->file, &file_info.disk_num_start) != UNZ_OK)
3317 err = UNZ_ERRNO;
3318
3319 if (unzlocal_getShort(s->file, &file_info.internal_fa) != UNZ_OK)
3320 err = UNZ_ERRNO;
3321
3322 if (unzlocal_getLong(s->file, &file_info.external_fa) != UNZ_OK)
3323 err = UNZ_ERRNO;
3324
3325 if (unzlocal_getLong(s->file, &file_info_internal.offset_curfile) != UNZ_OK)
3326 err = UNZ_ERRNO;
3327
3328 lSeek += file_info.size_filename;
3329 if ((err == UNZ_OK) && (szFileName != NULL))
3330 {
3331 uLong uSizeRead ;
3332 if (file_info.size_filename < fileNameBufferSize)
3333 {
3334 *(szFileName + file_info.size_filename) = '\0';
3335 uSizeRead = file_info.size_filename;
3336 }
3337 else
3338 uSizeRead = fileNameBufferSize;
3339
3340 if ((file_info.size_filename > 0) && (fileNameBufferSize > 0))
3341 if (lufread(szFileName, (uInt)uSizeRead, 1, s->file) != 1)
3342 err = UNZ_ERRNO;
3343 lSeek -= uSizeRead;
3344 }
3345
3346
3347 if ((err == UNZ_OK) && (extraField != NULL))
3348 {
3349 uLong uSizeRead ;
3350 if (file_info.size_file_extra < extraFieldBufferSize)
3351 uSizeRead = file_info.size_file_extra;
3352 else
3353 uSizeRead = extraFieldBufferSize;
3354
3355 if (lSeek != 0)
3356 {
3357 if (lufseek(s->file, lSeek, SEEK_CUR) == 0)
3358 lSeek = 0;
3359 else
3360 err = UNZ_ERRNO;
3361 }
3362 if ((file_info.size_file_extra > 0) && (extraFieldBufferSize > 0))
3363 {
3364 if (lufread(extraField, (uInt)uSizeRead, 1, s->file) != 1)
3365 err = UNZ_ERRNO;
3366 }
3367 lSeek += file_info.size_file_extra - uSizeRead;
3368 }
3369 else
3370 lSeek += file_info.size_file_extra;
3371
3372
3373 if ((err == UNZ_OK) && (szComment != NULL))
3374 {
3375 uLong uSizeRead ;
3376 if (file_info.size_file_comment < commentBufferSize)
3377 {
3378 *(szComment + file_info.size_file_comment) = '\0';
3379 uSizeRead = file_info.size_file_comment;
3380 }
3381 else
3382 uSizeRead = commentBufferSize;
3383
3384 if (lSeek != 0)
3385 {
3386 if (lufseek(s->file, lSeek, SEEK_CUR) == 0)
3387 {} // unused lSeek=0;
3388 else
3389 err = UNZ_ERRNO;
3390 }
3391 if ((file_info.size_file_comment > 0) && (commentBufferSize > 0))
3392 {
3393 if (lufread(szComment, (uInt)uSizeRead, 1, s->file) != 1)
3394 err = UNZ_ERRNO;
3395 }
3396 //unused lSeek+=file_info.size_file_comment - uSizeRead;
3397 }
3398 else {} //unused lSeek+=file_info.size_file_comment;
3399
3400 if ((err == UNZ_OK) && (pfile_info != NULL))
3401 *pfile_info = file_info;
3402
3403 if ((err == UNZ_OK) && (pfile_info_internal != NULL))
3404 *pfile_info_internal = file_info_internal;
3405
3406 return err;
3407}
3408
3409
3410
3411// Write info about the ZipFile in the *pglobal_info structure.
3412// No preparation of the structure is needed
3413// return UNZ_OK if there is no problem.
3415 char* szFileName, uLong fileNameBufferSize, void* extraField, uLong extraFieldBufferSize,
3416 char* szComment, uLong commentBufferSize)
3417{
3418 return unzlocal_GetCurrentFileInfoInternal(file, pfile_info, NULL, szFileName, fileNameBufferSize,
3419 extraField, extraFieldBufferSize, szComment, commentBufferSize);
3420}
3421
3422
3423// Set the current file of the zipfile to the first file.
3424// return UNZ_OK if there is no problem
3426{
3427 int err;
3428 unz_s* s;
3429 if (file == NULL)
3430 return UNZ_PARAMERROR;
3431 s = (unz_s*)file;
3433 s->num_file = 0;
3436 NULL, 0, NULL, 0, NULL, 0);
3437 s->current_file_ok = (err == UNZ_OK);
3438 return err;
3439}
3440
3441
3442// Set the current file of the zipfile to the next file.
3443// return UNZ_OK if there is no problem
3444// return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
3446{
3447 unz_s* s;
3448 int err;
3449
3450 if (file == NULL)
3451 return UNZ_PARAMERROR;
3452 s = (unz_s*)file;
3453 if (!s->current_file_ok)
3455 if (s->num_file + 1 == s->gi.number_entry)
3457
3460 s->num_file++;
3463 NULL, 0, NULL, 0, NULL, 0);
3464 s->current_file_ok = (err == UNZ_OK);
3465 return err;
3466}
3467
3468
3469// Try locate the file szFileName in the zipfile.
3470// For the iCaseSensitivity signification, see unzStringFileNameCompare
3471// return value :
3472// UNZ_OK if the file is found. It becomes the current file.
3473// UNZ_END_OF_LIST_OF_FILE if the file is not found
3474int unzLocateFile (unzFile file, const char* szFileName, int iCaseSensitivity)
3475{
3476 unz_s* s;
3477 int err;
3478
3479
3480 uLong num_fileSaved;
3481 uLong pos_in_central_dirSaved;
3482
3483
3484 if (file == NULL)
3485 return UNZ_PARAMERROR;
3486
3487 if (strlen(szFileName) >= UNZ_MAXFILENAMEINZIP)
3488 return UNZ_PARAMERROR;
3489
3490 s = (unz_s*)file;
3491 if (!s->current_file_ok)
3493
3494 num_fileSaved = s->num_file;
3495 pos_in_central_dirSaved = s->pos_in_central_dir;
3496
3497 err = unzGoToFirstFile(file);
3498
3499 while (err == UNZ_OK)
3500 {
3501 char szCurrentFileName[UNZ_MAXFILENAMEINZIP + 1];
3502 unzGetCurrentFileInfo(file, NULL,
3503 szCurrentFileName, sizeof(szCurrentFileName) - 1,
3504 NULL, 0, NULL, 0);
3505 if (unzStringFileNameCompare(szCurrentFileName, szFileName, iCaseSensitivity) == 0)
3506 return UNZ_OK;
3507 err = unzGoToNextFile(file);
3508 }
3509
3510 s->num_file = num_fileSaved ;
3511 s->pos_in_central_dir = pos_in_central_dirSaved ;
3512 return err;
3513}
3514
3515
3516// Read the local header of the current zipfile
3517// Check the coherency of the local header and info in the end of central
3518// directory about this file
3519// store in *piSizeVar the size of extra info in local header
3520// (filename and size of extra field data)
3522 uLong* poffset_local_extrafield, uInt* psize_local_extrafield)
3523{
3524 uLong uMagic, uData, uFlags;
3525 uLong size_filename;
3526 uLong size_extra_field;
3527 int err = UNZ_OK;
3528
3529 *piSizeVar = 0;
3530 *poffset_local_extrafield = 0;
3531 *psize_local_extrafield = 0;
3532
3534 return UNZ_ERRNO;
3535
3536
3537 if (err == UNZ_OK)
3538 {
3539 if (unzlocal_getLong(s->file, &uMagic) != UNZ_OK)
3540 err = UNZ_ERRNO;
3541 else if (uMagic != 0x04034b50)
3542 err = UNZ_BADZIPFILE;
3543 }
3544 if (unzlocal_getShort(s->file, &uData) != UNZ_OK)
3545 err = UNZ_ERRNO;
3546// else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
3547// err=UNZ_BADZIPFILE;
3548 if (unzlocal_getShort(s->file, &uFlags) != UNZ_OK)
3549 err = UNZ_ERRNO;
3550
3551 if (unzlocal_getShort(s->file, &uData) != UNZ_OK)
3552 err = UNZ_ERRNO;
3553 else if ((err == UNZ_OK) && (uData != s->cur_file_info.compression_method))
3554 err = UNZ_BADZIPFILE;
3555
3556 if ((err == UNZ_OK) && (s->cur_file_info.compression_method != 0) &&
3558 err = UNZ_BADZIPFILE;
3559
3560 if (unzlocal_getLong(s->file, &uData) != UNZ_OK) // date/time
3561 err = UNZ_ERRNO;
3562
3563 if (unzlocal_getLong(s->file, &uData) != UNZ_OK) // crc
3564 err = UNZ_ERRNO;
3565 else if ((err == UNZ_OK) && (uData != s->cur_file_info.crc) &&
3566 ((uFlags & 8) == 0))
3567 err = UNZ_BADZIPFILE;
3568
3569 if (unzlocal_getLong(s->file, &uData) != UNZ_OK) // size compr
3570 err = UNZ_ERRNO;
3571 else if ((err == UNZ_OK) && (uData != s->cur_file_info.compressed_size) &&
3572 ((uFlags & 8) == 0))
3573 err = UNZ_BADZIPFILE;
3574
3575 if (unzlocal_getLong(s->file, &uData) != UNZ_OK) // size uncompr
3576 err = UNZ_ERRNO;
3577 else if ((err == UNZ_OK) && (uData != s->cur_file_info.uncompressed_size) &&
3578 ((uFlags & 8) == 0))
3579 err = UNZ_BADZIPFILE;
3580
3581
3582 if (unzlocal_getShort(s->file, &size_filename) != UNZ_OK)
3583 err = UNZ_ERRNO;
3584 else if ((err == UNZ_OK) && (size_filename != s->cur_file_info.size_filename))
3585 err = UNZ_BADZIPFILE;
3586
3587 *piSizeVar += (uInt)size_filename;
3588
3589 if (unzlocal_getShort(s->file, &size_extra_field) != UNZ_OK)
3590 err = UNZ_ERRNO;
3591 *poffset_local_extrafield = s->cur_file_info_internal.offset_curfile +
3592 SIZEZIPLOCALHEADER + size_filename;
3593 *psize_local_extrafield = (uInt)size_extra_field;
3594
3595 *piSizeVar += (uInt)size_extra_field;
3596
3597 return err;
3598}
3599
3600
3601
3602
3603
3604// Open for reading data the current file in the zipfile.
3605// If there is no error and the file is opened, the return value is UNZ_OK.
3606int unzOpenCurrentFile (unzFile file, const char* password)
3607{
3608 int err;
3609 int Store;
3610 uInt iSizeVar;
3611 unz_s* s;
3612 file_in_zip_read_info_s* pfile_in_zip_read_info;
3613 uLong offset_local_extrafield; // offset of the local extra field
3614 uInt size_local_extrafield; // size of the local extra field
3615
3616 if (file == NULL)
3617 return UNZ_PARAMERROR;
3618 s = (unz_s*)file;
3619 if (!s->current_file_ok)
3620 return UNZ_PARAMERROR;
3621
3622 if (s->pfile_in_zip_read != NULL)
3623 unzCloseCurrentFile(file);
3624
3626 &offset_local_extrafield, &size_local_extrafield) != UNZ_OK)
3627 return UNZ_BADZIPFILE;
3628
3629 pfile_in_zip_read_info = (file_in_zip_read_info_s*)zmalloc(sizeof(file_in_zip_read_info_s));
3630 if (pfile_in_zip_read_info == NULL)
3631 return UNZ_INTERNALERROR;
3632
3633 pfile_in_zip_read_info->read_buffer = (char*)zmalloc(UNZ_BUFSIZE);
3634 pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
3635 pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
3636 pfile_in_zip_read_info->pos_local_extrafield = 0;
3637
3638 if (pfile_in_zip_read_info->read_buffer == NULL)
3639 {
3640 if (pfile_in_zip_read_info != 0) zfree(pfile_in_zip_read_info); //unused pfile_in_zip_read_info=0;
3641 return UNZ_INTERNALERROR;
3642 }
3643
3644 pfile_in_zip_read_info->stream_initialised = 0;
3645
3647 {
3648 // unused err=UNZ_BADZIPFILE;
3649 }
3650 Store = s->cur_file_info.compression_method == 0;
3651
3652 pfile_in_zip_read_info->crc32_wait = s->cur_file_info.crc;
3653 pfile_in_zip_read_info->crc32 = 0;
3654 pfile_in_zip_read_info->compression_method = s->cur_file_info.compression_method;
3655 pfile_in_zip_read_info->file = s->file;
3656 pfile_in_zip_read_info->byte_before_the_zipfile = s->byte_before_the_zipfile;
3657
3658 pfile_in_zip_read_info->stream.total_out = 0;
3659
3660 if (!Store)
3661 {
3662 pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
3663 pfile_in_zip_read_info->stream.zfree = (free_func)0;
3664 pfile_in_zip_read_info->stream.opaque = (voidpf)0;
3665
3666 err = inflateInit2(&pfile_in_zip_read_info->stream);
3667 if (err == Z_OK)
3668 pfile_in_zip_read_info->stream_initialised = 1;
3669 // windowBits is passed < 0 to tell that there is no zlib header.
3670 // Note that in this case inflate *requires* an extra "dummy" byte
3671 // after the compressed stream in order to complete decompression and
3672 // return Z_STREAM_END.
3673 // In unzip, i don't wait absolutely Z_STREAM_END because I known the
3674 // size of both compressed and uncompressed data
3675 }
3676 pfile_in_zip_read_info->rest_read_compressed = s->cur_file_info.compressed_size ;
3677 pfile_in_zip_read_info->rest_read_uncompressed = s->cur_file_info.uncompressed_size ;
3678 pfile_in_zip_read_info->encrypted = (s->cur_file_info.flag & 1) != 0;
3679 bool extlochead = (s->cur_file_info.flag & 8) != 0;
3680 if (extlochead)
3681 pfile_in_zip_read_info->crcenctest = (char)((s->cur_file_info.dosDate >> 8) & 0xff);
3682 else
3683 pfile_in_zip_read_info->crcenctest = (char)(s->cur_file_info.crc >> 24);
3684 pfile_in_zip_read_info->encheadleft = (pfile_in_zip_read_info->encrypted ? 12 : 0);
3685 pfile_in_zip_read_info->keys[0] = 305419896L;
3686 pfile_in_zip_read_info->keys[1] = 591751049L;
3687 pfile_in_zip_read_info->keys[2] = 878082192L;
3688 for (const char* cp = password; cp != 0 && *cp != 0; cp++) Uupdate_keys(pfile_in_zip_read_info->keys, *cp);
3689
3690 pfile_in_zip_read_info->pos_in_zipfile =
3692 iSizeVar;
3693
3694 pfile_in_zip_read_info->stream.avail_in = (uInt)0;
3695
3696 s->pfile_in_zip_read = pfile_in_zip_read_info;
3697
3698 return UNZ_OK;
3699}
3700
3701
3702// Read bytes from the current file.
3703// buf contain buffer where data must be copied
3704// len the size of buf.
3705// return the number of byte copied if somes bytes are copied (and also sets *reached_eof)
3706// return 0 if the end of file was reached. (and also sets *reached_eof).
3707// return <0 with error code if there is an error. (in which case *reached_eof is meaningless)
3708// (UNZ_ERRNO for IO error, or zLib error for uncompress error)
3709int unzReadCurrentFile (unzFile file, voidp buf, unsigned len, bool* reached_eof)
3710{
3711 int err = UNZ_OK;
3712 uInt iRead = 0;
3713 if (reached_eof != 0) *reached_eof = false;
3714
3715 unz_s* s = (unz_s*)file;
3716 if (s == NULL)
3717 return UNZ_PARAMERROR;
3718
3719 file_in_zip_read_info_s* pfile_in_zip_read_info = s->pfile_in_zip_read;
3720 if (pfile_in_zip_read_info == NULL)
3721 return UNZ_PARAMERROR;
3722 if ((pfile_in_zip_read_info->read_buffer == NULL))
3724 if (len == 0)
3725 return 0;
3726
3727 pfile_in_zip_read_info->stream.next_out = (Byte*)buf;
3728 pfile_in_zip_read_info->stream.avail_out = (uInt)len;
3729
3730 if (len > pfile_in_zip_read_info->rest_read_uncompressed)
3731 {
3732 pfile_in_zip_read_info->stream.avail_out = (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
3733 }
3734
3735 while (pfile_in_zip_read_info->stream.avail_out > 0)
3736 {
3737 if ((pfile_in_zip_read_info->stream.avail_in == 0) && (pfile_in_zip_read_info->rest_read_compressed > 0))
3738 {
3739 uInt uReadThis = UNZ_BUFSIZE;
3740 if (pfile_in_zip_read_info->rest_read_compressed < uReadThis)
3741 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
3742 if (uReadThis == 0)
3743 {
3744 if (reached_eof != 0)
3745 *reached_eof = true;
3746 return UNZ_EOF;
3747 }
3748 if (lufseek(pfile_in_zip_read_info->file, pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile, SEEK_SET) != 0)
3749 return UNZ_ERRNO;
3750 if (lufread(pfile_in_zip_read_info->read_buffer, uReadThis, 1, pfile_in_zip_read_info->file) != 1)
3751 return UNZ_ERRNO;
3752 pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
3753 pfile_in_zip_read_info->rest_read_compressed -= uReadThis;
3754 pfile_in_zip_read_info->stream.next_in = (Byte*)pfile_in_zip_read_info->read_buffer;
3755 pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
3756 //
3757 if (pfile_in_zip_read_info->encrypted)
3758 {
3759 char* buf = (char*)pfile_in_zip_read_info->stream.next_in;
3760 for (unsigned int i = 0; i < uReadThis; i++) buf[i] = zdecode(pfile_in_zip_read_info->keys, buf[i]);
3761 }
3762 }
3763
3764 unsigned int uDoEncHead = pfile_in_zip_read_info->encheadleft;
3765 if (uDoEncHead > pfile_in_zip_read_info->stream.avail_in)
3766 uDoEncHead = pfile_in_zip_read_info->stream.avail_in;
3767 if (uDoEncHead > 0)
3768 {
3769 char bufcrc = pfile_in_zip_read_info->stream.next_in[uDoEncHead - 1];
3770 pfile_in_zip_read_info->rest_read_uncompressed -= uDoEncHead;
3771 pfile_in_zip_read_info->stream.avail_in -= uDoEncHead;
3772 pfile_in_zip_read_info->stream.next_in += uDoEncHead;
3773 pfile_in_zip_read_info->encheadleft -= uDoEncHead;
3774 if (pfile_in_zip_read_info->encheadleft == 0)
3775 {
3776 if (bufcrc != pfile_in_zip_read_info->crcenctest)
3777 return UNZ_PASSWORD;
3778 }
3779 }
3780
3781 if (pfile_in_zip_read_info->compression_method == 0)
3782 {
3783 uInt uDoCopy, i ;
3784 if (pfile_in_zip_read_info->stream.avail_out < pfile_in_zip_read_info->stream.avail_in)
3785 {
3786 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
3787 }
3788 else
3789 {
3790 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
3791 }
3792 for (i = 0; i < uDoCopy; i++) *(pfile_in_zip_read_info->stream.next_out + i) = *(pfile_in_zip_read_info->stream.next_in + i);
3793 pfile_in_zip_read_info->crc32 = ucrc32(pfile_in_zip_read_info->crc32, pfile_in_zip_read_info->stream.next_out, uDoCopy);
3794 pfile_in_zip_read_info->rest_read_uncompressed -= uDoCopy;
3795 pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
3796 pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
3797 pfile_in_zip_read_info->stream.next_out += uDoCopy;
3798 pfile_in_zip_read_info->stream.next_in += uDoCopy;
3799 pfile_in_zip_read_info->stream.total_out += uDoCopy;
3800 iRead += uDoCopy;
3801 if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
3802 {
3803 if (reached_eof != 0)
3804 *reached_eof = true;
3805 }
3806 }
3807 else
3808 {
3809 uLong uTotalOutBefore, uTotalOutAfter;
3810 const Byte* bufBefore;
3811 uLong uOutThis;
3812 int flush = Z_SYNC_FLUSH;
3813 uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
3814 bufBefore = pfile_in_zip_read_info->stream.next_out;
3815 //
3816 err = inflate(&pfile_in_zip_read_info->stream, flush);
3817 //
3818 uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
3819 uOutThis = uTotalOutAfter - uTotalOutBefore;
3820 pfile_in_zip_read_info->crc32 = ucrc32(pfile_in_zip_read_info->crc32, bufBefore, (uInt)(uOutThis));
3821 pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis;
3822 iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
3823 if (err == Z_STREAM_END || pfile_in_zip_read_info->rest_read_uncompressed == 0)
3824 {
3825 if (reached_eof != 0)
3826 *reached_eof = true;
3827 return iRead;
3828 }
3829 if (err != Z_OK)
3830 break;
3831 }
3832 }
3833
3834 if (err == Z_OK)
3835 return iRead;
3836 return err;
3837}
3838
3839
3840// Give the current position in uncompressed data
3842{
3843 unz_s* s;
3844 file_in_zip_read_info_s* pfile_in_zip_read_info;
3845 if (file == NULL)
3846 return UNZ_PARAMERROR;
3847 s = (unz_s*)file;
3848 pfile_in_zip_read_info = s->pfile_in_zip_read;
3849
3850 if (pfile_in_zip_read_info == NULL)
3851 return UNZ_PARAMERROR;
3852
3853 return (z_off_t)pfile_in_zip_read_info->stream.total_out;
3854}
3855
3856
3857// return 1 if the end of file was reached, 0 elsewhere
3858int unzeof (unzFile file)
3859{
3860 unz_s* s;
3861 file_in_zip_read_info_s* pfile_in_zip_read_info;
3862 if (file == NULL)
3863 return UNZ_PARAMERROR;
3864 s = (unz_s*)file;
3865 pfile_in_zip_read_info = s->pfile_in_zip_read;
3866
3867 if (pfile_in_zip_read_info == NULL)
3868 return UNZ_PARAMERROR;
3869
3870 if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
3871 return 1;
3872 else
3873 return 0;
3874}
3875
3876
3877
3878// Read extra field from the current file (opened by unzOpenCurrentFile)
3879// This is the local-header version of the extra field (sometimes, there is
3880// more info in the local-header version than in the central-header)
3881// if buf==NULL, it return the size of the local extra field that can be read
3882// if buf!=NULL, len is the size of the buffer, the extra header is copied in buf.
3883// the return value is the number of bytes copied in buf, or (if <0) the error code
3884int unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len)
3885{
3886 unz_s* s;
3887 file_in_zip_read_info_s* pfile_in_zip_read_info;
3888 uInt read_now;
3889 uLong size_to_read;
3890
3891 if (file == NULL)
3892 return UNZ_PARAMERROR;
3893 s = (unz_s*)file;
3894 pfile_in_zip_read_info = s->pfile_in_zip_read;
3895
3896 if (pfile_in_zip_read_info == NULL)
3897 return UNZ_PARAMERROR;
3898
3899 size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
3900 pfile_in_zip_read_info->pos_local_extrafield);
3901
3902 if (buf == NULL)
3903 return (int)size_to_read;
3904
3905 if (len > size_to_read)
3906 read_now = (uInt)size_to_read;
3907 else
3908 read_now = (uInt)len ;
3909
3910 if (read_now == 0)
3911 return 0;
3912
3913 if (lufseek(pfile_in_zip_read_info->file, pfile_in_zip_read_info->offset_local_extrafield + pfile_in_zip_read_info->pos_local_extrafield, SEEK_SET) != 0)
3914 return UNZ_ERRNO;
3915
3916 if (lufread(buf, (uInt)size_to_read, 1, pfile_in_zip_read_info->file) != 1)
3917 return UNZ_ERRNO;
3918
3919 return (int)read_now;
3920}
3921
3922// Close the file in zip opened with unzipOpenCurrentFile
3923// Return UNZ_CRCERROR if all the file was read but the CRC is not good
3925{
3926 int err = UNZ_OK;
3927
3928 unz_s* s;
3929 file_in_zip_read_info_s* pfile_in_zip_read_info;
3930 if (file == NULL)
3931 return UNZ_PARAMERROR;
3932 s = (unz_s*)file;
3933 pfile_in_zip_read_info = s->pfile_in_zip_read;
3934
3935 if (pfile_in_zip_read_info == NULL)
3936 return UNZ_PARAMERROR;
3937
3938
3939 if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
3940 {
3941 if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
3942 err = UNZ_CRCERROR;
3943 }
3944
3945
3946 if (pfile_in_zip_read_info->read_buffer != 0)
3947 {
3948 void* buf = pfile_in_zip_read_info->read_buffer;
3949 zfree(buf);
3950 pfile_in_zip_read_info->read_buffer = 0;
3951 }
3952 pfile_in_zip_read_info->read_buffer = NULL;
3953 if (pfile_in_zip_read_info->stream_initialised)
3954 inflateEnd(&pfile_in_zip_read_info->stream);
3955
3956 pfile_in_zip_read_info->stream_initialised = 0;
3957 if (pfile_in_zip_read_info != 0) zfree(pfile_in_zip_read_info); // unused pfile_in_zip_read_info=0;
3958
3959 s->pfile_in_zip_read = NULL;
3960
3961 return err;
3962}
3963
3964
3965// Get the global comment string of the ZipFile, in the szComment buffer.
3966// uSizeBuf is the size of the szComment buffer.
3967// return the number of byte copied or an error code <0
3968int unzGetGlobalComment (unzFile file, char* szComment, uLong uSizeBuf)
3969{
3970 //int err=UNZ_OK;
3971 unz_s* s;
3972 uLong uReadThis ;
3973 if (file == NULL)
3974 return UNZ_PARAMERROR;
3975 s = (unz_s*)file;
3976 uReadThis = uSizeBuf;
3977 if (uReadThis > s->gi.size_comment)
3978 uReadThis = s->gi.size_comment;
3979 if (lufseek(s->file, s->central_pos + 22, SEEK_SET) != 0)
3980 return UNZ_ERRNO;
3981 if (uReadThis > 0)
3982 {
3983 *szComment = '\0';
3984 if (lufread(szComment, (uInt)uReadThis, 1, s->file) != 1)
3985 return UNZ_ERRNO;
3986 }
3987 if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
3988 *(szComment + s->gi.size_comment) = '\0';
3989 return (int)uReadThis;
3990}
3991
3992
3993
3994
3995
3996int unzOpenCurrentFile (unzFile file, const char* password);
3997int unzReadCurrentFile (unzFile file, void* buf, unsigned len);
3998int unzCloseCurrentFile (unzFile file);
3999
4000
4001typedef unsigned __int32 lutime_t; // define it ourselves since we don't include time.h
4002
4003FILETIME timet2filetime(const lutime_t t)
4004{
4005 LONGLONG i = Int32x32To64(t, 10000000) + 116444736000000000;
4006 FILETIME ft;
4007 ft.dwLowDateTime = (DWORD) i;
4008 ft.dwHighDateTime = (DWORD)(i >> 32);
4009 return ft;
4010}
4011
4012FILETIME dosdatetime2filetime(WORD dosdate, WORD dostime)
4013{
4014 // date: bits 0-4 are day of month 1-31. Bits 5-8 are month 1..12. Bits 9-15 are year-1980
4015 // time: bits 0-4 are seconds/2, bits 5-10 are minute 0..59. Bits 11-15 are hour 0..23
4016 SYSTEMTIME st;
4017 st.wYear = (WORD)(((dosdate >> 9) & 0x7f) + 1980);
4018 st.wMonth = (WORD)((dosdate >> 5) & 0xf);
4019 st.wDay = (WORD)(dosdate & 0x1f);
4020 st.wHour = (WORD)((dostime >> 11) & 0x1f);
4021 st.wMinute = (WORD)((dostime >> 5) & 0x3f);
4022 st.wSecond = (WORD)((dostime & 0x1f) * 2);
4023 st.wMilliseconds = 0;
4024 FILETIME ft;
4025 SystemTimeToFileTime(&st, &ft);
4026 return ft;
4027}
4028
4029
4030
4032{
4033 public:
4034 TUnzip(const char* pwd) : uf(0), unzbuf(0), currentfile(-1), czei(-1), password(0)
4035 {
4036 if (pwd != 0)
4037 {
4038 password = new char[strlen(pwd) + 1];
4039 strcpy(password, pwd);
4040 }
4041 }
4043 {
4044 if (password != 0) delete[] password;
4045 password = 0;
4046 if (unzbuf != 0) delete[] unzbuf;
4047 unzbuf = 0;
4048 }
4049
4053 int czei;
4055 char* unzbuf; // lazily created and destroyed, used by Unzip
4056 TCHAR rootdir[MAX_PATH]; // includes a trailing slash
4057
4058 ZRESULT Open(void* z, unsigned int len, DWORD flags);
4059 ZRESULT Get(int index, ZIPENTRY* ze);
4060 ZRESULT Find(const TCHAR* name, bool ic, int* index, ZIPENTRY* ze);
4061 ZRESULT Unzip(int index, void* dst, unsigned int len, DWORD flags);
4062 ZRESULT SetUnzipBaseDir(const TCHAR* dir);
4063 ZRESULT Close();
4064};
4065
4066
4067ZRESULT TUnzip::Open(void* z, unsigned int len, DWORD flags)
4068{
4069 if (uf != 0 || currentfile != -1)
4070 return ZR_NOTINITED;
4071 //
4072#ifdef GetCurrentDirectory
4073 GetCurrentDirectory(MAX_PATH, rootdir);
4074#else
4075 _tcscpy(rootdir, _T("\\"));
4076#endif
4077 TCHAR lastchar = rootdir[_tcslen(rootdir) - 1];
4078 if (lastchar != '\\' && lastchar != '/')
4079 _tcscat(rootdir, _T("\\"));
4080 //
4081 if (flags == ZIP_HANDLE)
4082 {
4083 // test if we can seek on it. We can't use GetFileType(h)==FILE_TYPE_DISK since it's not on CE.
4084 DWORD res = SetFilePointer(z, 0, 0, FILE_CURRENT);
4085 bool canseek = (res != 0xFFFFFFFF);
4086 if (!canseek)
4087 return ZR_SEEK;
4088 }
4089 ZRESULT e;
4090 LUFILE* f = lufopen(z, len, flags, &e);
4091 if (f == NULL)
4092 return e;
4093 uf = unzOpenInternal(f);
4094 if (uf == 0)
4095 return ZR_NOFILE;
4096 return ZR_OK;
4097}
4098
4100{
4101 _tcscpy(rootdir, dir);
4102 TCHAR lastchar = rootdir[_tcslen(rootdir) - 1];
4103 if (lastchar != '\\' && lastchar != '/')
4104 _tcscat(rootdir, _T("\\"));
4105 return ZR_OK;
4106}
4107
4109{
4110 if (index < -1 || index >= (int)uf->gi.number_entry)
4111 return ZR_ARGS;
4112 if (currentfile != -1)
4114 currentfile = -1;
4115 if (index == czei && index != -1)
4116 {
4117 memcpy(ze, &cze, sizeof(ZIPENTRY));
4118 return ZR_OK;
4119 }
4120 if (index == -1)
4121 {
4122 ze->index = uf->gi.number_entry;
4123 ze->name[0] = 0;
4124 ze->attr = 0;
4125 ze->atime.dwLowDateTime = 0;
4126 ze->atime.dwHighDateTime = 0;
4127 ze->ctime.dwLowDateTime = 0;
4128 ze->ctime.dwHighDateTime = 0;
4129 ze->mtime.dwLowDateTime = 0;
4130 ze->mtime.dwHighDateTime = 0;
4131 ze->comp_size = 0;
4132 ze->unc_size = 0;
4133 return ZR_OK;
4134 }
4135 if (index < (int)uf->num_file)
4137 while ((int)uf->num_file < index)
4139 unz_file_info ufi;
4140 char fn[MAX_PATH];
4141 unzGetCurrentFileInfo(uf, &ufi, fn, MAX_PATH, NULL, 0, NULL, 0);
4142 // now get the extra header. We do this ourselves, instead of
4143 // calling unzOpenCurrentFile &c., to avoid allocating more than necessary.
4144 unsigned int extralen, iSizeVar;
4145 unsigned long offset;
4146 int res = unzlocal_CheckCurrentFileCoherencyHeader(uf, &iSizeVar, &offset, &extralen);
4147 if (res != UNZ_OK)
4148 return ZR_CORRUPT;
4149 if (lufseek(uf->file, offset, SEEK_SET) != 0)
4150 return ZR_READ;
4151 unsigned char* extra = new unsigned char[extralen];
4152 if (lufread(extra, 1, (uInt)extralen, uf->file) != extralen)
4153 {
4154 delete[] extra;
4155 return ZR_READ;
4156 }
4157 //
4158 ze->index = uf->num_file;
4159 TCHAR tfn[MAX_PATH];
4160#ifdef UNICODE
4161 MultiByteToWideChar(CP_UTF8, 0, fn, -1, tfn, MAX_PATH);
4162#else
4163 strcpy(tfn, fn);
4164#endif
4165 // As a safety feature: if the zip filename had sneaky stuff
4166 // like "c:\windows\file.txt" or "\windows\file.txt" or "fred\..\..\..\windows\file.txt"
4167 // then we get rid of them all. That way, when the programmer does UnzipItem(hz,i,ze.name),
4168 // it won't be a problem. (If the programmer really did want to get the full evil information,
4169 // then they can edit out this security feature from here).
4170 // In particular, we chop off any prefixes that are "c:\" or "\" or "/" or "[stuff]\.." or "[stuff]/.."
4171 const TCHAR* sfn = tfn;
4172 for (;;)
4173 {
4174 if (sfn[0] != 0 && sfn[1] == ':')
4175 {
4176 sfn += 2;
4177 continue;
4178 }
4179 if (sfn[0] == '\\')
4180 {
4181 sfn++;
4182 continue;
4183 }
4184 if (sfn[0] == '/')
4185 {
4186 sfn++;
4187 continue;
4188 }
4189 const TCHAR* c;
4190 c = _tcsstr(sfn, _T("\\..\\"));
4191 if (c != 0)
4192 {
4193 sfn = c + 4;
4194 continue;
4195 }
4196 c = _tcsstr(sfn, _T("\\../"));
4197 if (c != 0)
4198 {
4199 sfn = c + 4;
4200 continue;
4201 }
4202 c = _tcsstr(sfn, _T("/../"));
4203 if (c != 0)
4204 {
4205 sfn = c + 4;
4206 continue;
4207 }
4208 c = _tcsstr(sfn, _T("/..\\"));
4209 if (c != 0)
4210 {
4211 sfn = c + 4;
4212 continue;
4213 }
4214 break;
4215 }
4216 _tcscpy(ze->name, sfn);
4217
4218
4219 // zip has an 'attribute' 32bit value. Its lower half is windows stuff
4220 // its upper half is standard unix stat.st_mode. We'll start trying
4221 // to read it in unix mode
4222 unsigned long a = ufi.external_fa;
4223 bool isdir = (a & 0x40000000) != 0;
4224 bool readonly = (a & 0x00800000) == 0;
4225 //bool readable= (a&0x01000000)!=0; // unused
4226 //bool executable=(a&0x00400000)!=0; // unused
4227 bool hidden = false, system = false, archive = true;
4228 // but in normal hostmodes these are overridden by the lower half...
4229 int host = ufi.version >> 8;
4230 if (host == 0 || host == 7 || host == 11 || host == 14)
4231 {
4232 readonly = (a & 0x00000001) != 0;
4233 hidden = (a & 0x00000002) != 0;
4234 system = (a & 0x00000004) != 0;
4235 isdir = (a & 0x00000010) != 0;
4236 archive = (a & 0x00000020) != 0;
4237 }
4238 ze->attr = 0;
4239 if (isdir)
4240 ze->attr |= FILE_ATTRIBUTE_DIRECTORY;
4241 if (archive)
4242 ze->attr |= FILE_ATTRIBUTE_ARCHIVE;
4243 if (hidden)
4244 ze->attr |= FILE_ATTRIBUTE_HIDDEN;
4245 if (readonly)
4246 ze->attr |= FILE_ATTRIBUTE_READONLY;
4247 if (system)
4248 ze->attr |= FILE_ATTRIBUTE_SYSTEM;
4249 ze->comp_size = ufi.compressed_size;
4250 ze->unc_size = ufi.uncompressed_size;
4251 //
4252 WORD dostime = (WORD)(ufi.dosDate & 0xFFFF);
4253 WORD dosdate = (WORD)((ufi.dosDate >> 16) & 0xFFFF);
4254 FILETIME ftd = dosdatetime2filetime(dosdate, dostime);
4255 FILETIME ft;
4256 LocalFileTimeToFileTime(&ftd, &ft);
4257 ze->atime = ft;
4258 ze->ctime = ft;
4259 ze->mtime = ft;
4260 // the zip will always have at least that dostime. But if it also has
4261 // an extra header, then we'll instead get the info from that.
4262 unsigned int epos = 0;
4263 while (epos + 4 < extralen)
4264 {
4265 char etype[3];
4266 etype[0] = extra[epos + 0];
4267 etype[1] = extra[epos + 1];
4268 etype[2] = 0;
4269 int size = extra[epos + 2];
4270 if (strcmp(etype, "UT") != 0)
4271 {
4272 epos += 4 + size;
4273 continue;
4274 }
4275 int flags = extra[epos + 4];
4276 bool hasmtime = (flags & 1) != 0;
4277 bool hasatime = (flags & 2) != 0;
4278 bool hasctime = (flags & 4) != 0;
4279 epos += 5;
4280 if (hasmtime)
4281 {
4282 lutime_t mtime = ((extra[epos + 0]) << 0) | ((extra[epos + 1]) << 8) | ((extra[epos + 2]) << 16) | ((extra[epos + 3]) << 24);
4283 epos += 4;
4284 ze->mtime = timet2filetime(mtime);
4285 }
4286 if (hasatime)
4287 {
4288 lutime_t atime = ((extra[epos + 0]) << 0) | ((extra[epos + 1]) << 8) | ((extra[epos + 2]) << 16) | ((extra[epos + 3]) << 24);
4289 epos += 4;
4290 ze->atime = timet2filetime(atime);
4291 }
4292 if (hasctime)
4293 {
4294 lutime_t ctime = ((extra[epos + 0]) << 0) | ((extra[epos + 1]) << 8) | ((extra[epos + 2]) << 16) | ((extra[epos + 3]) << 24);
4295 epos += 4;
4296 ze->ctime = timet2filetime(ctime);
4297 }
4298 break;
4299 }
4300 //
4301 if (extra != 0)
4302 delete[] extra;
4303 memcpy(&cze, ze, sizeof(ZIPENTRY));
4304 czei = index;
4305 return ZR_OK;
4306}
4307
4308ZRESULT TUnzip::Find(const TCHAR* tname, bool ic, int* index, ZIPENTRY* ze)
4309{
4310 char name[MAX_PATH];
4311#ifdef UNICODE
4312 WideCharToMultiByte(CP_UTF8, 0, tname, -1, name, MAX_PATH, 0, 0);
4313#else
4314 strcpy(name, tname);
4315#endif
4317 if (res != UNZ_OK)
4318 {
4319 if (index != 0)
4320 *index = -1;
4321 if (ze != NULL)
4322 {
4323 ZeroMemory(ze, sizeof(ZIPENTRY));
4324 ze->index = -1;
4325 }
4326 return ZR_NOTFOUND;
4327 }
4328 if (currentfile != -1)
4330 currentfile = -1;
4331 int i = (int)uf->num_file;
4332 if (index != NULL)
4333 *index = i;
4334 if (ze != NULL)
4335 {
4336 ZRESULT zres = Get(i, ze);
4337 if (zres != ZR_OK)
4338 return zres;
4339 }
4340 return ZR_OK;
4341}
4342
4343void EnsureDirectory(const TCHAR* rootdir, const TCHAR* dir)
4344{
4345 if (rootdir != 0 && GetFileAttributes(rootdir) == 0xFFFFFFFF)
4346 CreateDirectory(rootdir, 0);
4347 if (*dir == 0)
4348 return;
4349 const TCHAR* lastslash = dir, *c = lastslash;
4350 while (*c != 0)
4351 {
4352 if (*c == '/' || *c == '\\') lastslash = c;
4353 c++;
4354 }
4355 const TCHAR* name = lastslash;
4356 if (lastslash != dir)
4357 {
4358 TCHAR tmp[MAX_PATH];
4359 memcpy(tmp, dir, sizeof(TCHAR) * (lastslash - dir));
4360 tmp[lastslash - dir] = 0;
4361 EnsureDirectory(rootdir, tmp);
4362 name++;
4363 }
4364 TCHAR cd[MAX_PATH];
4365 *cd = 0;
4366 if (rootdir != 0)
4367 _tcscpy(cd, rootdir);
4368 _tcscat(cd, dir);
4369 if (GetFileAttributes(cd) == 0xFFFFFFFF)
4370 CreateDirectory(cd, NULL);
4371}
4372
4373
4374
4375ZRESULT TUnzip::Unzip(int index, void* dst, unsigned int len, DWORD flags)
4376{
4377 if (flags != ZIP_MEMORY && flags != ZIP_FILENAME && flags != ZIP_HANDLE)
4378 return ZR_ARGS;
4379 if (flags == ZIP_MEMORY)
4380 {
4381 if (index != currentfile)
4382 {
4383 if (currentfile != -1)
4385 currentfile = -1;
4386 if (index >= (int)uf->gi.number_entry)
4387 return ZR_ARGS;
4388 if (index < (int)uf->num_file)
4390 while ((int)uf->num_file < index)
4393 currentfile = index;
4394 }
4395 bool reached_eof;
4396 int res = unzReadCurrentFile(uf, dst, len, &reached_eof);
4397 if (res <= 0)
4398 {
4400 currentfile = -1;
4401 }
4402 if (reached_eof)
4403 return ZR_OK;
4404 if (res > 0)
4405 return ZR_MORE;
4406 if (res == UNZ_PASSWORD)
4407 return ZR_PASSWORD;
4408 return ZR_FLATE;
4409 }
4410 // otherwise we're writing to a handle or a file
4411 if (currentfile != -1)
4413 currentfile = -1;
4414 if (index >= (int)uf->gi.number_entry)
4415 return ZR_ARGS;
4416 if (index < (int)uf->num_file)
4418 while ((int)uf->num_file < index)
4420 ZIPENTRY ze;
4421 Get(index, &ze);
4422 // zipentry=directory is handled specially
4423 if ((ze.attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
4424 {
4425 if (flags == ZIP_HANDLE)
4426 return ZR_OK; // don't do anything
4427 const TCHAR* dir = (const TCHAR*)dst;
4428 bool isabsolute = (dir[0] == '/' || dir[0] == '\\' || (dir[0] != 0 && dir[1] == ':'));
4429 if (isabsolute)
4430 EnsureDirectory(0, dir);
4431 else
4433 return ZR_OK;
4434 }
4435 // otherwise, we write the zipentry to a file/handle
4436 HANDLE h;
4437 if (flags == ZIP_HANDLE)
4438 h = dst;
4439 else
4440 {
4441 const TCHAR* ufn = (const TCHAR*)dst;
4442 // We'll qualify all relative names to our root dir, and leave absolute names as they are
4443 // ufn="zipfile.txt" dir="" name="zipfile.txt" fn="c:\\currentdir\\zipfile.txt"
4444 // ufn="dir1/dir2/subfile.txt" dir="dir1/dir2/" name="subfile.txt" fn="c:\\currentdir\\dir1/dir2/subfiles.txt"
4445 // ufn="\z\file.txt" dir="\z\" name="file.txt" fn="\z\file.txt"
4446 // This might be a security risk, in the case where we just use the zipentry's name as "ufn", where
4447 // a malicious zip could unzip itself into c:\windows. Our solution is that GetZipItem (which
4448 // is how the user retrieve's the file's name within the zip) never returns absolute paths.
4449 const TCHAR* name = ufn;
4450 const TCHAR* c = name;
4451 while (*c != 0)
4452 {
4453 if (*c == '/' || *c == '\\') name = c + 1;
4454 c++;
4455 }
4456 TCHAR dir[MAX_PATH];
4457 _tcscpy(dir, ufn);
4458 if (name == ufn)
4459 *dir = 0;
4460 else
4461 dir[name - ufn] = 0;
4462 TCHAR fn[MAX_PATH];
4463 bool isabsolute = (dir[0] == '/' || dir[0] == '\\' || (dir[0] != 0 && dir[1] == ':'));
4464 if (isabsolute)
4465 {
4466 wsprintf(fn, _T("%s%s"), dir, name);
4467 EnsureDirectory(0, dir);
4468 }
4469 else
4470 {
4471 wsprintf(fn, _T("%s%s%s"), rootdir, dir, name);
4473 }
4474 //
4475 h = CreateFile(fn, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, ze.attr, NULL);
4476 }
4477 if (h == INVALID_HANDLE_VALUE)
4478 return ZR_NOFILE;
4480 if (unzbuf == 0)
4481 unzbuf = new char[16384];
4482 DWORD haderr = 0;
4483 //
4484
4485 for (; haderr == 0;)
4486 {
4487 bool reached_eof;
4488 int res = unzReadCurrentFile(uf, unzbuf, 16384, &reached_eof);
4489 if (res == UNZ_PASSWORD)
4490 {
4491 haderr = ZR_PASSWORD;
4492 break;
4493 }
4494 if (res < 0)
4495 {
4496 haderr = ZR_FLATE;
4497 break;
4498 }
4499 if (res > 0)
4500 {
4501 DWORD writ;
4502 BOOL bres = WriteFile(h, unzbuf, res, &writ, NULL);
4503 if (!bres)
4504 {
4505 haderr = ZR_WRITE;
4506 break;
4507 }
4508 }
4509 if (reached_eof)
4510 break;
4511 if (res == 0)
4512 {
4513 haderr = ZR_FLATE;
4514 break;
4515 }
4516 }
4517 if (!haderr)
4518 SetFileTime(h, &ze.ctime, &ze.atime, &ze.mtime); // may fail if it was a pipe
4519 if (flags != ZIP_HANDLE)
4520 CloseHandle(h);
4522 if (haderr != 0)
4523 return haderr;
4524 return ZR_OK;
4525}
4526
4528{
4529 if (currentfile != -1)
4531 currentfile = -1;
4532 if (uf != 0)
4533 unzClose(uf);
4534 uf = 0;
4535 return ZR_OK;
4536}
4537
4538
4539
4540
4541
4543
4544unsigned int FormatZipMessageU(ZRESULT code, TCHAR* buf, unsigned int len)
4545{
4546 if (code == ZR_RECENT)
4547 code = lasterrorU;
4548 const TCHAR* msg = _T("unknown zip result code");
4549 switch (code)
4550 {
4551 case ZR_OK:
4552 msg = _T("Success");
4553 break;
4554 case ZR_NODUPH:
4555 msg = _T("Culdn't duplicate handle");
4556 break;
4557 case ZR_NOFILE:
4558 msg = _T("Couldn't create/open file");
4559 break;
4560 case ZR_NOALLOC:
4561 msg = _T("Failed to allocate memory");
4562 break;
4563 case ZR_WRITE:
4564 msg = _T("Error writing to file");
4565 break;
4566 case ZR_NOTFOUND:
4567 msg = _T("File not found in the zipfile");
4568 break;
4569 case ZR_MORE:
4570 msg = _T("Still more data to unzip");
4571 break;
4572 case ZR_CORRUPT:
4573 msg = _T("Zipfile is corrupt or not a zipfile");
4574 break;
4575 case ZR_READ:
4576 msg = _T("Error reading file");
4577 break;
4578 case ZR_PASSWORD:
4579 msg = _T("Correct password required");
4580 break;
4581 case ZR_ARGS:
4582 msg = _T("Caller: faulty arguments");
4583 break;
4584 case ZR_PARTIALUNZ:
4585 msg = _T("Caller: the file had already been partially unzipped");
4586 break;
4587 case ZR_NOTMMAP:
4588 msg = _T("Caller: can only get memory of a memory zipfile");
4589 break;
4590 case ZR_MEMSIZE:
4591 msg = _T("Caller: not enough space allocated for memory zipfile");
4592 break;
4593 case ZR_FAILED:
4594 msg = _T("Caller: there was a previous error");
4595 break;
4596 case ZR_ENDED:
4597 msg = _T("Caller: additions to the zip have already been ended");
4598 break;
4599 case ZR_ZMODE:
4600 msg = _T("Caller: mixing creation and opening of zip");
4601 break;
4602 case ZR_NOTINITED:
4603 msg = _T("Zip-bug: internal initialisation not completed");
4604 break;
4605 case ZR_SEEK:
4606 msg = _T("Zip-bug: trying to seek the unseekable");
4607 break;
4608 case ZR_MISSIZE:
4609 msg = _T("Zip-bug: the anticipated size turned out wrong");
4610 break;
4611 case ZR_NOCHANGE:
4612 msg = _T("Zip-bug: tried to change mind, but not allowed");
4613 break;
4614 case ZR_FLATE:
4615 msg = _T("Zip-bug: an internal error during flation");
4616 break;
4617 }
4618 unsigned int mlen = (unsigned int)_tcslen(msg);
4619 if (buf == 0 || len == 0)
4620 return mlen;
4621 unsigned int n = mlen;
4622 if (n + 1 > len)
4623 n = len - 1;
4624 _tcsncpy(buf, msg, n);
4625 buf[n] = 0;
4626 return mlen;
4627}
4628
4629
4630typedef struct
4631{
4632 DWORD flag;
4635
4636HZIP OpenZipInternal(void* z, unsigned int len, DWORD flags, const char* password)
4637{
4638 TUnzip* unz = new TUnzip(password);
4639 lasterrorU = unz->Open(z, len, flags);
4640 if (lasterrorU != ZR_OK)
4641 {
4642 delete unz;
4643 return 0;
4644 }
4646 han->flag = 1;
4647 han->unz = unz;
4648 return (HZIP)han;
4649}
4650HZIP OpenZipHandle(HANDLE h, const char* password)
4651{
4652 return OpenZipInternal((void*)h, 0, ZIP_HANDLE, password);
4653}
4654HZIP OpenZip(const TCHAR* fn, const char* password)
4655{
4656 return OpenZipInternal((void*)fn, 0, ZIP_FILENAME, password);
4657}
4658HZIP OpenZip(void* z, unsigned int len, const char* password)
4659{
4660 return OpenZipInternal(z, len, ZIP_MEMORY, password);
4661}
4662
4663
4664ZRESULT GetZipItem(HZIP hz, int index, ZIPENTRY* ze)
4665{
4666 ze->index = 0;
4667 *ze->name = 0;
4668 ze->unc_size = 0;
4669 if (hz == 0)
4670 {
4672 return ZR_ARGS;
4673 }
4675 if (han->flag != 1)
4676 {
4678 return ZR_ZMODE;
4679 }
4680 TUnzip* unz = han->unz;
4681 lasterrorU = unz->Get(index, ze);
4682 return lasterrorU;
4683}
4684
4685ZRESULT FindZipItem(HZIP hz, const TCHAR* name, bool ic, int* index, ZIPENTRY* ze)
4686{
4687 if (hz == 0)
4688 {
4690 return ZR_ARGS;
4691 }
4693 if (han->flag != 1)
4694 {
4696 return ZR_ZMODE;
4697 }
4698 TUnzip* unz = han->unz;
4699 lasterrorU = unz->Find(name, ic, index, ze);
4700 return lasterrorU;
4701}
4702
4703ZRESULT UnzipItemInternal(HZIP hz, int index, void* dst, unsigned int len, DWORD flags)
4704{
4705 if (hz == 0)
4706 {
4708 return ZR_ARGS;
4709 }
4711 if (han->flag != 1)
4712 {
4714 return ZR_ZMODE;
4715 }
4716 TUnzip* unz = han->unz;
4717 lasterrorU = unz->Unzip(index, dst, len, flags);
4718 return lasterrorU;
4719}
4720ZRESULT UnzipItemHandle(HZIP hz, int index, HANDLE h)
4721{
4722 return UnzipItemInternal(hz, index, (void*)h, 0, ZIP_HANDLE);
4723}
4724ZRESULT UnzipItem(HZIP hz, int index, const TCHAR* fn)
4725{
4726 return UnzipItemInternal(hz, index, (void*)fn, 0, ZIP_FILENAME);
4727}
4728ZRESULT UnzipItem(HZIP hz, int index, void* z, unsigned int len)
4729{
4730 return UnzipItemInternal(hz, index, z, len, ZIP_MEMORY);
4731}
4732
4733ZRESULT SetUnzipBaseDir(HZIP hz, const TCHAR* dir)
4734{
4735 if (hz == 0)
4736 {
4738 return ZR_ARGS;
4739 }
4741 if (han->flag != 1)
4742 {
4744 return ZR_ZMODE;
4745 }
4746 TUnzip* unz = han->unz;
4747 lasterrorU = unz->SetUnzipBaseDir(dir);
4748 return lasterrorU;
4749}
4750
4751
4753{
4754 if (hz == 0)
4755 {
4757 return ZR_ARGS;
4758 }
4760 if (han->flag != 1)
4761 {
4763 return ZR_ZMODE;
4764 }
4765 TUnzip* unz = han->unz;
4766 lasterrorU = unz->Close();
4767 delete unz;
4768 delete han;
4769 return lasterrorU;
4770}
4771
4772bool IsZipHandleU(HZIP hz)
4773{
4774 if (hz == 0) return false;
4776 return (han->flag == 1);
4777}
4778
4779
ZRESULT Close()
Definition: unzip.cpp:4527
int czei
Definition: unzip.cpp:4053
unzFile uf
Definition: unzip.cpp:4050
ZRESULT Get(int index, ZIPENTRY *ze)
Definition: unzip.cpp:4108
ZRESULT Find(const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
Definition: unzip.cpp:4308
ZRESULT Open(void *z, unsigned int len, DWORD flags)
Definition: unzip.cpp:4067
char * password
Definition: unzip.cpp:4054
~TUnzip()
Definition: unzip.cpp:4042
ZRESULT Unzip(int index, void *dst, unsigned int len, DWORD flags)
Definition: unzip.cpp:4375
TCHAR rootdir[MAX_PATH]
Definition: unzip.cpp:4056
ZIPENTRY cze
Definition: unzip.cpp:4052
TUnzip(const char *pwd)
Definition: unzip.cpp:4034
char * unzbuf
Definition: unzip.cpp:4055
ZRESULT SetUnzipBaseDir(const TCHAR *dir)
Definition: unzip.cpp:4099
int currentfile
Definition: unzip.cpp:4051
if(nReturnType==EIGENVALUES)
Definition: matfuncs.hpp:390
for(unsigned int i=0;i< _mMatrix.rows();i++)
Definition: matfuncs.hpp:376
#define _T(x)
Definition: muParserDef.h:72
const string version
Definition: variables.hpp:66
#define FALSE
Definition: resampler.cpp:42
char name[32]
Definition: resampler.cpp:371
unsigned int len
Definition: unzip.cpp:2735
bool canseek
Definition: unzip.cpp:2727
unsigned long initial_offset
Definition: unzip.cpp:2731
bool herr
Definition: unzip.cpp:2730
void * buf
Definition: unzip.cpp:2734
bool mustclosehandle
Definition: unzip.cpp:2732
bool is_handle
Definition: unzip.cpp:2726
unsigned int pos
Definition: unzip.cpp:2735
HANDLE h
Definition: unzip.cpp:2729
TUnzip * unz
Definition: unzip.cpp:4633
Definition: unzip.h:21
DWORD attr
Definition: unzip.h:23
int index
Definition: unzip.h:21
long comp_size
Definition: unzip.h:25
long unc_size
Definition: unzip.h:26
FILETIME ctime
Definition: unzip.h:24
TCHAR name[MAX_PATH]
Definition: unzip.h:22
FILETIME mtime
Definition: unzip.h:24
FILETIME atime
Definition: unzip.h:24
unsigned long keys[3]
Definition: unzip.cpp:2904
struct inflate_blocks_state::@33::@35 decode
inflate_block_mode mode
Definition: unzip.cpp:683
inflate_huft * hufts
Definition: unzip.cpp:708
inflate_huft * tb
Definition: unzip.cpp:695
inflate_codes_statef * codes
Definition: unzip.cpp:700
union inflate_blocks_state::@33 sub
struct inflate_blocks_state::@33::@34 trees
check_func checkfn
Definition: unzip.cpp:713
const inflate_huft * ltree
Definition: unzip.cpp:1034
struct inflate_codes_state::@36::@37 code
inflate_codes_mode mode
Definition: unzip.cpp:1012
struct inflate_codes_state::@36::@38 copy
const inflate_huft * tree
Definition: unzip.cpp:1020
union inflate_codes_state::@36 sub
const inflate_huft * dtree
Definition: unzip.cpp:1035
union inflate_huft_s::@31 word
struct inflate_huft_s::@31::@32 what
union internal_state::@39 sub
struct internal_state::@39::@40 check
inflate_blocks_statef * blocks
Definition: unzip.cpp:2468
inflate_mode mode
Definition: unzip.cpp:2450
unsigned int tm_year
Definition: unzip.cpp:118
unsigned int tm_sec
Definition: unzip.cpp:113
unsigned int tm_hour
Definition: unzip.cpp:115
unsigned int tm_mday
Definition: unzip.cpp:116
unsigned int tm_min
Definition: unzip.cpp:114
unsigned int tm_mon
Definition: unzip.cpp:117
unsigned long size_file_comment
Definition: unzip.cpp:142
unsigned long disk_num_start
Definition: unzip.cpp:143
unsigned long crc
Definition: unzip.cpp:137
unsigned long version_needed
Definition: unzip.cpp:133
unsigned long internal_fa
Definition: unzip.cpp:144
unsigned long dosDate
Definition: unzip.cpp:136
unsigned long external_fa
Definition: unzip.cpp:145
unsigned long size_filename
Definition: unzip.cpp:140
unsigned long compressed_size
Definition: unzip.cpp:138
unsigned long size_file_extra
Definition: unzip.cpp:141
unsigned long flag
Definition: unzip.cpp:134
unsigned long version
Definition: unzip.cpp:132
tm_unz tmu_date
Definition: unzip.cpp:146
unsigned long uncompressed_size
Definition: unzip.cpp:139
unsigned long compression_method
Definition: unzip.cpp:135
unsigned long size_comment
Definition: unzip.cpp:126
unsigned long number_entry
Definition: unzip.cpp:125
unz_global_info gi
Definition: unzip.cpp:2914
uLong central_pos
Definition: unzip.cpp:2919
unz_file_info_internal cur_file_info_internal
Definition: unzip.cpp:2925
uLong size_central_dir
Definition: unzip.cpp:2921
uLong pos_in_central_dir
Definition: unzip.cpp:2917
uLong num_file
Definition: unzip.cpp:2916
uLong byte_before_the_zipfile
Definition: unzip.cpp:2915
file_in_zip_read_info_s * pfile_in_zip_read
Definition: unzip.cpp:2926
LUFILE * file
Definition: unzip.cpp:2913
unz_file_info cur_file_info
Definition: unzip.cpp:2924
uLong current_file_ok
Definition: unzip.cpp:2918
uLong offset_central_dir
Definition: unzip.cpp:2922
uInt avail_in
Definition: unzip.cpp:244
alloc_func zalloc
Definition: unzip.cpp:254
Byte * next_in
Definition: unzip.cpp:243
uInt avail_out
Definition: unzip.cpp:248
free_func zfree
Definition: unzip.cpp:255
char * msg
Definition: unzip.cpp:251
int data_type
Definition: unzip.cpp:258
uLong total_in
Definition: unzip.cpp:245
voidpf opaque
Definition: unzip.cpp:256
uLong total_out
Definition: unzip.cpp:249
uLong reserved
Definition: unzip.cpp:260
uLong adler
Definition: unzip.cpp:259
Byte * next_out
Definition: unzip.cpp:247
struct internal_state * state
Definition: unzip.cpp:252
void(* free_func)(voidpf opaque, voidpf address)
Definition: unzip.cpp:237
z_stream * z_streamp
Definition: unzip.cpp:263
int inflate_codes(inflate_blocks_statef *, z_streamp, int)
Definition: unzip.cpp:1062
#define UNZ_EOF
Definition: unzip.cpp:153
#define BMAX
Definition: unzip.cpp:1688
const uLong * get_crc_table(void)
Definition: unzip.cpp:2271
int unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity)
Definition: unzip.cpp:3474
void inflate_blocks_reset(inflate_blocks_statef *, z_streamp, uLong *)
Definition: unzip.cpp:1287
#define exop
Definition: unzip.cpp:989
#define LOAD
Definition: unzip.cpp:740
struct unz_file_info_internal_s unz_file_info_internal
int inflateInit2(z_streamp z)
Definition: unzip.cpp:2497
int inflate_trees_fixed(uInt *, uInt *, const inflate_huft **, const inflate_huft **, z_streamp)
Definition: unzip.cpp:1990
#define PRESET_DICT
Definition: unzip.cpp:516
struct tm_unz_s tm_unz
#define BUFREADCOMMENT
Definition: unzip.cpp:3066
int inflate_flush(inflate_blocks_statef *, z_streamp, int)
Definition: unzip.cpp:910
inflate_mode
Definition: unzip.cpp:2427
@ IM_CHECK3
Definition: unzip.cpp:2437
@ IM_CHECK2
Definition: unzip.cpp:2438
@ IM_DICT1
Definition: unzip.cpp:2433
@ IM_DICT3
Definition: unzip.cpp:2431
@ IM_CHECK4
Definition: unzip.cpp:2436
@ IM_DICT4
Definition: unzip.cpp:2430
@ IM_BAD
Definition: unzip.cpp:2441
@ IM_METHOD
Definition: unzip.cpp:2428
@ IM_CHECK1
Definition: unzip.cpp:2439
@ IM_DICT2
Definition: unzip.cpp:2432
@ IM_DONE
Definition: unzip.cpp:2440
@ IM_FLAG
Definition: unzip.cpp:2429
@ IM_DICT0
Definition: unzip.cpp:2434
@ IM_BLOCKS
Definition: unzip.cpp:2435
#define LEAVE
Definition: unzip.cpp:725
int luferror(LUFILE *stream)
Definition: unzip.cpp:2814
const uInt cpdist[30]
Definition: unzip.cpp:1641
const uLong crc_table[256]
Definition: unzip.cpp:2215
int unzStringFileNameCompare(const char *fileName1, const char *fileName2, int iCaseSensitivity)
Definition: unzip.cpp:3058
#define FLUSH
Definition: unzip.cpp:736
#define LuTracev(x)
Definition: unzip.cpp:531
ZRESULT GetZipItem(HZIP hz, int index, ZIPENTRY *ze)
Definition: unzip.cpp:4664
FILETIME dosdatetime2filetime(WORD dosdate, WORD dostime)
Definition: unzip.cpp:4012
#define Z_DEFLATED
Definition: unzip.cpp:193
void unzlocal_DosDateToTmuDate(uLong ulDosDate, tm_unz *ptm)
Definition: unzip.cpp:3229
int unzlocal_CheckCurrentFileCoherencyHeader(unz_s *s, uInt *piSizeVar, uLong *poffset_local_extrafield, uInt *psize_local_extrafield)
Definition: unzip.cpp:3521
int inflateSyncPoint(z_streamp z)
int unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len)
Definition: unzip.cpp:3884
const char unzip_inflate_copyright[]
#define Z_NEED_DICT
Definition: unzip.cpp:207
void EnsureDirectory(const TCHAR *rootdir, const TCHAR *dir)
Definition: unzip.cpp:4343
unsigned short ush
Definition: unzip.cpp:477
const uInt cplext[31]
Definition: unzip.cpp:1636
struct unz_s * unzFile
int unzlocal_getShort(LUFILE *fin, uLong *pX)
Definition: unzip.cpp:2979
#define ZALLOC(strm, items, size)
Definition: unzip.cpp:541
#define SIZEZIPLOCALHEADER
Definition: unzip.cpp:2710
#define AD_DO16(buf)
Definition: unzip.cpp:2340
bool IsZipHandleU(HZIP hz)
Definition: unzip.cpp:4772
#define UNZ_BADZIPFILE
Definition: unzip.cpp:155
#define Z_BUF_ERROR
Definition: unzip.cpp:212
unsigned __int32 lutime_t
Definition: unzip.cpp:4001
long z_off_t
Definition: unzip.cpp:223
ZRESULT lasterrorU
Definition: unzip.cpp:4542
ZRESULT FindZipItem(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
Definition: unzip.cpp:4685
inflate_block_mode
Definition: unzip.cpp:664
@ IBM_TYPE
Definition: unzip.cpp:665
@ IBM_BTREE
Definition: unzip.cpp:669
@ IBM_LENS
Definition: unzip.cpp:666
@ IBM_DTREE
Definition: unzip.cpp:670
@ IBM_DRY
Definition: unzip.cpp:672
@ IBM_STORED
Definition: unzip.cpp:667
@ IBM_CODES
Definition: unzip.cpp:671
@ IBM_DONE
Definition: unzip.cpp:673
@ IBM_TABLE
Definition: unzip.cpp:668
@ IBM_BAD
Definition: unzip.cpp:674
voidpf(* alloc_func)(voidpf opaque, uInt items, uInt size)
Definition: unzip.cpp:236
#define ZLIB_VERSION
Definition: unzip.cpp:166
FILETIME timet2filetime(const lutime_t t)
Definition: unzip.cpp:4003
#define ZIP_HANDLE
Definition: unzip.cpp:70
#define UNZ_PASSWORD
Definition: unzip.cpp:158
char zdecode(unsigned long *keys, char c)
Definition: unzip.cpp:2316
const inflate_huft fixed_tl[]
Definition: unzip.cpp:760
#define NMAX
Definition: unzip.cpp:2333
const uInt cplens[31]
Definition: unzip.cpp:1630
#define NEEDOUT
Definition: unzip.cpp:737
void inflate_set_dictionary(inflate_blocks_statef *s, const Byte *d, uInt n)
int unzlocal_GetCurrentFileInfoInternal(unzFile file, unz_file_info *pfile_info, unz_file_info_internal *pfile_info_internal, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
Definition: unzip.cpp:3254
#define CASE_SENSITIVE
Definition: unzip.cpp:199
#define LuTracevv(x)
Definition: unzip.cpp:532
int inflateSync(z_streamp strm)
ZRESULT SetUnzipBaseDir(HZIP hz, const TCHAR *dir)
Definition: unzip.cpp:4733
#define ERR_MSG(err)
Definition: unzip.cpp:498
uLong ucrc32(uLong crc, const Byte *buf, uInt len)
Definition: unzip.cpp:2281
int inflate_blocks(inflate_blocks_statef *, z_streamp, int)
Definition: unzip.cpp:1333
#define CRC_DO1(buf)
Definition: unzip.cpp:2276
int unzlocal_getByte(LUFILE *fin, int *pi)
Definition: unzip.cpp:2958
int unzCloseCurrentFile(unzFile file)
Definition: unzip.cpp:3924
const char * zlibVersion()
Definition: unzip.cpp:2389
int inflate_fast(uInt, uInt, const inflate_huft *, const inflate_huft *, inflate_blocks_statef *, z_streamp)
Definition: unzip.cpp:2023
#define ZFREE(strm, addr)
Definition: unzip.cpp:543
const uInt inflate_mask[17]
Definition: unzip.cpp:744
#define NEEDBITS(j)
Definition: unzip.cpp:730
#define CASE_INSENSITIVE
Definition: unzip.cpp:200
#define Z_VERSION_ERROR
Definition: unzip.cpp:213
#define IM_NEEDBYTE
Definition: unzip.cpp:2567
const uInt cpdext[30]
Definition: unzip.cpp:1647
int inflate(z_streamp strm, int flush)
Definition: unzip.cpp:2570
#define BASE
Definition: unzip.cpp:2332
int lufclose(LUFILE *stream)
Definition: unzip.cpp:2804
z_off_t unztell(unzFile file)
Definition: unzip.cpp:3841
struct unz_global_info_s unz_global_info
int inflate_trees_dynamic(uInt, uInt, uInt *, uInt *, uInt *, inflate_huft **, inflate_huft **, inflate_huft *, z_streamp)
Definition: unzip.cpp:1927
#define Z_STREAM_END
Definition: unzip.cpp:206
#define DUMPBITS(j)
Definition: unzip.cpp:731
const char * zError(int err)
Definition: unzip.cpp:2396
#define UNZ_INTERNALERROR
Definition: unzip.cpp:156
#define ZIP_MEMORY
Definition: unzip.cpp:72
int inflate_trees_bits(uInt *, uInt *, inflate_huft **, inflate_huft *, z_streamp)
Definition: unzip.cpp:1900
#define Z_FINISH
Definition: unzip.cpp:173
unsigned int uInt
Definition: unzip.cpp:219
int unzeof(unzFile file)
Definition: unzip.cpp:3858
ZRESULT UnzipItemInternal(HZIP hz, int index, void *dst, unsigned int len, DWORD flags)
Definition: unzip.cpp:4703
uLong unzlocal_SearchCentralDir(LUFILE *fin)
Definition: unzip.cpp:3072
#define GRABBITS(j)
Definition: unzip.cpp:2015
#define Z_OK
Definition: unzip.cpp:205
ZRESULT CloseZipU(HZIP hz)
Definition: unzip.cpp:4752
LUFILE * lufopen(void *z, unsigned int len, DWORD flags, ZRESULT *err)
Definition: unzip.cpp:2739
uLong(* check_func)(uLong check, const Byte *buf, uInt len)
Definition: unzip.cpp:537
inflate_codes_statef * inflate_codes_new(uInt, uInt, const inflate_huft *, const inflate_huft *, z_streamp)
Definition: unzip.cpp:1040
#define UNZ_MAXFILENAMEINZIP
Definition: unzip.cpp:2708
const uInt fixed_bd
Definition: unzip.cpp:759
int inflateReset(z_streamp strm)
Definition: unzip.cpp:2472
unzFile unzOpenInternal(LUFILE *fin)
Definition: unzip.cpp:3124
#define Z_DATA_ERROR
Definition: unzip.cpp:210
ZRESULT UnzipItem(HZIP hz, int index, const TCHAR *fn)
Definition: unzip.cpp:4724
#define CRC32(c, b)
Definition: unzip.cpp:2303
int unzlocal_getLong(LUFILE *fin, uLong *pX)
Definition: unzip.cpp:2999
#define Z_SYNC_FLUSH
Definition: unzip.cpp:171
struct z_stream_s z_stream
struct unz_file_info_s unz_file_info
#define Z_STREAM_ERROR
Definition: unzip.cpp:209
#define UNZ_ERRNO
Definition: unzip.cpp:152
uLong adler32(uLong adler, const Byte *buf, uInt len)
Definition: unzip.cpp:2343
HZIP OpenZipInternal(void *z, unsigned int len, DWORD flags, const char *password)
Definition: unzip.cpp:4636
#define MANY
Definition: unzip.cpp:579
#define OUTBYTE(a)
Definition: unzip.cpp:738
unsigned int FormatZipMessageU(ZRESULT code, TCHAR *buf, unsigned int len)
Definition: unzip.cpp:4544
const uInt fixed_bl
Definition: unzip.cpp:758
int lufseek(LUFILE *stream, long offset, int whence)
Definition: unzip.cpp:2831
#define UNGRAB
Definition: unzip.cpp:2016
uch uchf
Definition: unzip.cpp:476
#define zfree(p)
Definition: unzip.cpp:77
int inflateEnd(z_streamp strm)
Definition: unzip.cpp:2484
unsigned long ulg
Definition: unzip.cpp:479
int inflateSetDictionary(z_streamp strm, const Byte *dictionary, uInt dictLength)
#define IM_NEXTBYTE
Definition: unzip.cpp:2568
inflate_blocks_statef * inflate_blocks_new(z_streamp z, check_func c, uInt w)
Definition: unzip.cpp:1305
#define UPDATE
Definition: unzip.cpp:724
const inflate_huft fixed_td[]
Definition: unzip.cpp:891
size_t lufread(void *ptr, size_t size, size_t n, LUFILE *stream)
Definition: unzip.cpp:2860
void zcfree(voidpf opaque, voidpf ptr)
Definition: unzip.cpp:2411
#define UNZ_END_OF_LIST_OF_FILE
Definition: unzip.cpp:151
int unzGetGlobalComment(unzFile file, char *szComment, uLong uSizeBuf)
Definition: unzip.cpp:3968
#define UNZ_BUFSIZE
Definition: unzip.cpp:2707
#define UNZ_PARAMERROR
Definition: unzip.cpp:154
int unzOpenCurrentFile(unzFile file, const char *password)
Definition: unzip.cpp:3606
void * voidpf
Definition: unzip.cpp:221
#define C4
ZRESULT UnzipItemHandle(HZIP hz, int index, HANDLE h)
Definition: unzip.cpp:4720
#define CRC_DO8(buf)
Definition: unzip.cpp:2279
unsigned long uLong
Definition: unzip.cpp:220
voidpf zcalloc(voidpf opaque, unsigned items, unsigned size)
Definition: unzip.cpp:2404
ush ushf
Definition: unzip.cpp:478
const char *const z_errmsg[10]
Definition: unzip.cpp:483
#define Z_NULL
Definition: unzip.cpp:196
#define ZIP_FILENAME
Definition: unzip.cpp:71
#define SIZECENTRALDIRITEM
Definition: unzip.cpp:2709
HZIP OpenZipHandle(HANDLE h, const char *password)
Definition: unzip.cpp:4650
inflate_codes_mode
Definition: unzip.cpp:993
@ BADCODE
Definition: unzip.cpp:1003
@ START
Definition: unzip.cpp:994
@ WASH
Definition: unzip.cpp:1001
@ LIT
Definition: unzip.cpp:1000
@ COPY
Definition: unzip.cpp:999
@ DIST
Definition: unzip.cpp:997
@ LENEXT
Definition: unzip.cpp:996
@ END
Definition: unzip.cpp:1002
@ DISTEXT
Definition: unzip.cpp:998
@ LEN
Definition: unzip.cpp:995
HZIP OpenZip(const TCHAR *fn, const char *password)
Definition: unzip.cpp:4654
#define UNZ_OK
Definition: unzip.cpp:150
char Udecrypt_byte(unsigned long *keys)
Definition: unzip.cpp:2311
int unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info)
Definition: unzip.cpp:3217
int inflate_blocks_sync_point(inflate_blocks_statef *s)
int huft_build(uInt *, uInt, uInt, const uInt *, const uInt *, inflate_huft **, uInt *, inflate_huft *, uInt *, uInt *)
Definition: unzip.cpp:1690
#define Z_MEM_ERROR
Definition: unzip.cpp:211
unsigned char Byte
Definition: unzip.cpp:218
void Uupdate_keys(unsigned long *keys, char c)
Definition: unzip.cpp:2304
int unzGoToNextFile(unzFile file)
Definition: unzip.cpp:3445
int strcmpcasenosensitive_internal(const char *fileName1, const char *fileName2)
Definition: unzip.cpp:3029
#define UNZ_CRCERROR
Definition: unzip.cpp:157
int inflate_blocks_free(inflate_blocks_statef *, z_streamp)
Definition: unzip.cpp:1589
#define zmalloc(len)
Definition: unzip.cpp:75
long int luftell(LUFILE *stream)
Definition: unzip.cpp:2821
int unzGoToFirstFile(unzFile file)
Definition: unzip.cpp:3425
int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
Definition: unzip.cpp:3414
const char unz_copyright[]
Definition: unzip.cpp:2715
void inflate_codes_free(inflate_codes_statef *, z_streamp)
Definition: unzip.cpp:1221
int unzClose(unzFile file)
Definition: unzip.cpp:3197
void * voidp
Definition: unzip.cpp:222
unsigned char uch
Definition: unzip.cpp:475
const uInt border[]
Definition: unzip.cpp:1238
int unzReadCurrentFile(unzFile file, voidp buf, unsigned len, bool *reached_eof)
Definition: unzip.cpp:3709
#define ZR_ENDED
Definition: unzip.h:122
#define ZR_SEEK
Definition: unzip.h:129
#define ZR_NOTINITED
Definition: unzip.h:128
#define ZR_NOTFOUND
Definition: unzip.h:111
#define ZR_WRITE
Definition: unzip.h:110
#define ZR_PARTIALUNZ
Definition: unzip.h:124
#define ZR_FLATE
Definition: unzip.h:131
#define ZR_PASSWORD
Definition: unzip.h:115
#define ZR_OK
Definition: unzip.h:103
#define ZR_FAILED
Definition: unzip.h:121
#define ZR_CORRUPT
Definition: unzip.h:113
#define ZR_NOTMMAP
Definition: unzip.h:119
DWORD ZRESULT
Definition: unzip.h:17
#define ZR_ZMODE
Definition: unzip.h:125
#define ZR_NODUPH
Definition: unzip.h:107
#define ZR_READ
Definition: unzip.h:114
#define ZR_NOALLOC
Definition: unzip.h:109
#define ZR_MISSIZE
Definition: unzip.h:123
#define ZR_RECENT
Definition: unzip.h:104
#define ZR_MEMSIZE
Definition: unzip.h:120
#define ZR_MORE
Definition: unzip.h:112
#define ZR_ARGS
Definition: unzip.h:118
#define ZR_NOCHANGE
Definition: unzip.h:130
#define ZR_NOFILE
Definition: unzip.h:108
#define EOF
Definition: zip.cpp:76