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