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