1/* 2 3PKZIP Kernels for Hashcat (c) 2018, European Union 4 5PKZIP Kernels for Hashcat has been developed by the Joint Research Centre of the European Commission. 6It is released as open source software under the MIT License. 7 8PKZIP Kernels for Hashcat makes use of two primary external components, which continue to be subject 9to the terms and conditions stipulated in the respective licences they have been released under. These 10external components include, but are not necessarily limited to, the following: 11 12----- 13 141. Hashcat: MIT License 15 16Copyright (c) 2015-2018 Jens Steube 17 18Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 19associated documentation files (the "Software"), to deal in the Software without restriction, including 20without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 21copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to 22the following conditions: 23 24The above copyright notice and this permission notice shall be included in all copies or substantial 25portions of the Software. 26 27THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 28LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 29NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 30WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 31SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 33----- 34 352. Miniz: MIT License 36 37Copyright 2013-2014 RAD Game Tools and Valve Software 38Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC 39 40All Rights Reserved. 41 42Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 43documentation files (the "Software"), to deal in the Software without restriction, including without 44limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 45the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 46conditions: 47 48The above copyright notice and this permission notice shall be included in all copies or substantial 49portions of the Software. 50 51THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 52LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 53NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 54WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 55SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 56 57----- 58 59The European Union disclaims all liability related to or arising out of the use made by third parties of 60any external components and dependencies which may be included with PKZIP Kernels for Hashcat. 61 62*/ 63 64enum{ 65 MZ_OK = 0, 66 MZ_STREAM_END = 1, 67 MZ_NEED_DICT = 2, 68 MZ_ERRNO = -1, 69 MZ_STREAM_ERROR = -2, 70 MZ_DATA_ERROR = -3, 71 MZ_MEM_ERROR = -4, 72 MZ_BUF_ERROR = -5, 73 MZ_VERSION_ERROR = -6, 74 MZ_PARAM_ERROR = -10000 75}; 76typedef ullong mz_ulong; 77 78#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES 79typedef unsigned char Byte; 80typedef unsigned int uInt; 81typedef Byte Bytef; 82typedef uInt uIntf; 83typedef char charf; 84typedef int intf; 85typedef void *voidpf; 86typedef void *voidp; 87typedef void *const voidpc; 88#define Z_NULL 0 89#define Z_NO_FLUSH MZ_NO_FLUSH 90#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH 91#define Z_SYNC_FLUSH MZ_SYNC_FLUSH 92#define Z_FULL_FLUSH MZ_FULL_FLUSH 93#define Z_FINISH MZ_FINISH 94#define Z_BLOCK MZ_BLOCK 95#define Z_OK MZ_OK 96#define Z_STREAM_END MZ_STREAM_END 97#define Z_NEED_DICT MZ_NEED_DICT 98#define Z_ERRNO MZ_ERRNO 99#define Z_STREAM_ERROR MZ_STREAM_ERROR 100#define Z_DATA_ERROR MZ_DATA_ERROR 101#define Z_MEM_ERROR MZ_MEM_ERROR 102#define Z_BUF_ERROR MZ_BUF_ERROR 103#define Z_VERSION_ERROR MZ_VERSION_ERROR 104#define Z_PARAM_ERROR MZ_PARAM_ERROR 105#define Z_NO_COMPRESSION MZ_NO_COMPRESSION 106#define Z_BEST_SPEED MZ_BEST_SPEED 107#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION 108#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION 109#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY 110#define Z_FILTERED MZ_FILTERED 111#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY 112#define Z_RLE MZ_RLE 113#define Z_FIXED MZ_FIXED 114#define Z_DEFLATED MZ_DEFLATED 115#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS 116#define z_stream mz_stream 117#define deflateInit mz_deflateInit 118#define deflateInit2 mz_deflateInit2 119#define deflateReset mz_deflateReset 120#define deflate mz_deflate 121#define deflateEnd mz_deflateEnd 122#define deflateBound mz_deflateBound 123#define compress mz_compress 124#define compress2 mz_compress2 125#define compressBound mz_compressBound 126#define inflateInit mz_inflateInit 127#define inflateInit2 mz_inflateInit2 128#define inflate mz_inflate 129#define inflateEnd mz_inflateEnd 130#define uncompress mz_uncompress 131#define adler32 mz_adler32 132#define MAX_WBITS 15 133#define MAX_MEM_LEVEL 9 134#define ZLIB_VERSION MZ_VERSION 135#define ZLIB_VERNUM MZ_VERNUM 136#define ZLIB_VER_MAJOR MZ_VER_MAJOR 137#define ZLIB_VER_MINOR MZ_VER_MINOR 138#define ZLIB_VER_REVISION MZ_VER_REVISION 139#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION 140#define zlibVersion mz_version 141#define zlib_version mz_version() 142#endif /* #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ 143 144#define MZ_MACRO_END while (0) 145 146#define tinfl_init(r) \ 147 do \ 148 { \ 149 (r)->m_state = 0; \ 150 } \ 151 MZ_MACRO_END 152 153enum 154{ 155 TINFL_FLAG_PARSE_ZLIB_HEADER = 1, 156 TINFL_FLAG_HAS_MORE_INPUT = 2, 157 TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, 158 TINFL_FLAG_COMPUTE_ADLER32 = 8 159}; 160 161enum 162{ 163 TINFL_MAX_HUFF_TABLES = 3, 164 TINFL_MAX_HUFF_SYMBOLS_0 = 288, 165 TINFL_MAX_HUFF_SYMBOLS_1 = 32, 166 TINFL_MAX_HUFF_SYMBOLS_2 = 19, 167 TINFL_FAST_LOOKUP_BITS = 10, 168 TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS 169}; 170 171typedef unsigned char mz_uint8; 172typedef signed short mz_int16; 173typedef unsigned short mz_uint16; 174typedef unsigned int mz_uint32; 175typedef unsigned int mz_uint; 176typedef u64 mz_uint64; 177typedef int mz_bool; 178 179typedef mz_uint64 tinfl_bit_buf_t; 180 181DECLSPEC void memcpy (void *dest, const void *src, u32 n) 182{ 183 char *csrc = (char *) src; 184 char *cdest = (char *) dest; 185 186 for (int i = 0; i < n; i++) 187 { 188 cdest[i] = csrc[i]; 189 } 190} 191 192DECLSPEC void *memset (u8 *s, const int c, u32 len) 193{ 194 const u8 c8 = (const u8) c; 195 196 u8 *dst = s; 197 198 while (len > 0) 199 { 200 *dst = c8; 201 202 dst++; 203 len--; 204 } 205 206 return s; 207} 208 209#define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b)) 210#define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b)) 211#define MZ_DEFAULT_WINDOW_BITS 15 212#define TINFL_LZ_DICT_SIZE 32768 213 214// hashcat-patched/hashcat-specific: 215#ifdef CRC32_IN_INFLATE 216#define M_DICT_SIZE 1 217#define MAYBE_GLOBAL GLOBAL_AS 218#else 219#define M_DICT_SIZE TINFL_LZ_DICT_SIZE 220#define MAYBE_GLOBAL 221#endif 222 223#define TINFL_CR_FINISH } 224#define TINFL_CR_BEGIN \ 225 switch (r->m_state) \ 226 { \ 227 case 0: 228 229enum 230{ 231 MZ_NO_FLUSH = 0, 232 MZ_PARTIAL_FLUSH = 1, 233 MZ_SYNC_FLUSH = 2, 234 MZ_FULL_FLUSH = 3, 235 MZ_FINISH = 4, 236 MZ_BLOCK = 5 237}; 238 239#define MZ_READ_LE16(p) *((const mz_uint16 *)(p)) 240//#define MZ_READ_LE32(p) *((const mz_uint32 *)(p)) 241#define MZ_READ_LE32(p) *((mz_uint32 *)(p)) 242 243#define TINFL_NEED_BITS(state_index, n) \ 244 do \ 245 { \ 246 mz_uint c; \ 247 TINFL_GET_BYTE(state_index, c); \ 248 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ 249 num_bits += 8; \ 250 } while (num_bits < (mz_uint)(n)) 251 252#define TINFL_SKIP_BITS(state_index, n) \ 253 do \ 254 { \ 255 if (num_bits < (mz_uint)(n)) \ 256 { \ 257 TINFL_NEED_BITS(state_index, n); \ 258 } \ 259 bit_buf >>= (n); \ 260 num_bits -= (n); \ 261 } \ 262 MZ_MACRO_END 263 264#define TINFL_GET_BITS(state_index, b, n) \ 265 do \ 266 { \ 267 if (num_bits < (mz_uint)(n)) \ 268 { \ 269 TINFL_NEED_BITS(state_index, n); \ 270 } \ 271 b = bit_buf & ((1 << (n)) - 1); \ 272 bit_buf >>= (n); \ 273 num_bits -= (n); \ 274 } \ 275 MZ_MACRO_END 276 277#define TINFL_CR_RETURN(state_index, result) \ 278 do \ 279 { \ 280 status = result; \ 281 r->m_state = state_index; \ 282 goto common_exit; \ 283 case state_index:; \ 284 } \ 285 MZ_MACRO_END 286 287#define TINFL_CR_RETURN_FOREVER(state_index, result) \ 288 do \ 289 { \ 290 for (;;) \ 291 { \ 292 TINFL_CR_RETURN(state_index, result); \ 293 } \ 294 } \ 295 MZ_MACRO_END 296 297#define TINFL_GET_BYTE(state_index, c) \ 298 do \ 299 { \ 300 while (pIn_buf_cur >= pIn_buf_end) \ 301 { \ 302 TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \ 303 } \ 304 c = pIn_xor_byte (*pIn_buf_cur++, pStream); \ 305 } \ 306 MZ_MACRO_END 307 308#define TINFL_HUFF_DECODE(state_index, sym, pHuff) \ 309 do \ 310 { \ 311 int temp; \ 312 mz_uint code_len, c; \ 313 if (num_bits < 15) \ 314 { \ 315 if ((pIn_buf_end - pIn_buf_cur) < 2) \ 316 { \ 317 TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \ 318 } \ 319 else \ 320 { \ 321 bit_buf |= (((tinfl_bit_buf_t) pIn_xor_byte (pIn_buf_cur[0], pStream)) << num_bits) | (((tinfl_bit_buf_t) pIn_xor_byte (pIn_buf_cur[1], pStream)) << (num_bits + 8)); \ 322 pIn_buf_cur += 2; \ 323 num_bits += 16; \ 324 } \ 325 } \ 326 if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \ 327 code_len = temp >> 9, temp &= 511; \ 328 else \ 329 { \ 330 code_len = TINFL_FAST_LOOKUP_BITS; \ 331 do \ 332 { \ 333 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ 334 } while (temp < 0); \ 335 } \ 336 sym = temp; \ 337 bit_buf >>= code_len; \ 338 num_bits -= code_len; \ 339 } \ 340 MZ_MACRO_END 341 342#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \ 343 do \ 344 { \ 345 temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \ 346 if (temp >= 0) \ 347 { \ 348 code_len = temp >> 9; \ 349 if ((code_len) && (num_bits >= code_len)) \ 350 break; \ 351 } \ 352 else if (num_bits > TINFL_FAST_LOOKUP_BITS) \ 353 { \ 354 code_len = TINFL_FAST_LOOKUP_BITS; \ 355 do \ 356 { \ 357 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \ 358 } while ((temp < 0) && (num_bits >= (code_len + 1))); \ 359 if (temp >= 0) \ 360 break; \ 361 } \ 362 TINFL_GET_BYTE(state_index, c); \ 363 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \ 364 num_bits += 8; \ 365 } while (num_bits < 15); 366 367typedef struct 368{ 369 mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0]; 370 mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; 371} tinfl_huff_table; 372 373typedef enum { 374 /* This flags indicates the inflator needs 1 or more input bytes to make forward progress, but the caller is indicating that no more are available. The compressed data */ 375 /* is probably corrupted. If you call the inflator again with more bytes it'll try to continue processing the input but this is a BAD sign (either the data is corrupted or you called it incorrectly). */ 376 /* If you call it again with no input you'll just get TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */ 377 TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4, 378 379 /* This flag indicates that one or more of the input parameters was obviously bogus. (You can try calling it again, but if you get this error the calling code is wrong.) */ 380 TINFL_STATUS_BAD_PARAM = -3, 381 382 /* This flags indicate the inflator is finished but the adler32 check of the uncompressed data didn't match. If you call it again it'll return TINFL_STATUS_DONE. */ 383 TINFL_STATUS_ADLER32_MISMATCH = -2, 384 385 /* This flags indicate the inflator has somehow failed (bad code, corrupted input, etc.). If you call it again without resetting via tinfl_init() it it'll just keep on returning the same status failure code. */ 386 TINFL_STATUS_FAILED = -1, 387 388 /* Any status code less than TINFL_STATUS_DONE must indicate a failure. */ 389 390 /* This flag indicates the inflator has returned every byte of uncompressed data that it can, has consumed every byte that it needed, has successfully reached the end of the deflate stream, and */ 391 /* if zlib headers and adler32 checking enabled that it has successfully checked the uncompressed data's adler32. If you call it again you'll just get TINFL_STATUS_DONE over and over again. */ 392 TINFL_STATUS_DONE = 0, 393 394 /* This flag indicates the inflator MUST have more input data (even 1 byte) before it can make any more forward progress, or you need to clear the TINFL_FLAG_HAS_MORE_INPUT */ 395 /* flag on the next call if you don't have any more source data. If the source data was somehow corrupted it's also possible (but unlikely) for the inflator to keep on demanding input to */ 396 /* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */ 397 TINFL_STATUS_NEEDS_MORE_INPUT = 1, 398 399 /* This flag indicates the inflator definitely has 1 or more bytes of uncompressed data available, but it cannot write this data into the output buffer. */ 400 /* Note if the source compressed data was corrupted it's possible for the inflator to return a lot of uncompressed data to the caller. I've been assuming you know how much uncompressed data to expect */ 401 /* (either exact or worst case) and will stop calling the inflator and fail after receiving too much. In pure streaming scenarios where you have no idea how many bytes to expect this may not be possible */ 402 /* so I may need to add some code to address this. */ 403 TINFL_STATUS_HAS_MORE_OUTPUT = 2 404} tinfl_status; 405 406#define tinfl_get_adler32(r) (r)->m_check_adler32 407 408 409 410struct tinfl_decompressor_tag 411{ 412 mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES]; 413 tinfl_bit_buf_t m_bit_buf; 414 size_t m_dist_from_out_buf_start; 415 tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES]; 416 mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; 417}; 418typedef struct tinfl_decompressor_tag tinfl_decompressor; 419 420typedef struct 421{ 422 tinfl_decompressor m_decomp; 423 mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; 424 int m_window_bits; 425 // hashcat-patched: we do not need m_dict in case of CRC32 checksums, 426 // because we have our own output buffer: 427 mz_uint8 m_dict[M_DICT_SIZE]; 428 tinfl_status m_last_status; 429 430} inflate_state; 431 432typedef struct mz_stream_s 433{ 434 MAYBE_GLOBAL const unsigned char *next_in; /* pointer to next byte to read */ 435 unsigned int avail_in; /* number of bytes available at next_in */ 436 mz_ulong total_in; /* total number of bytes consumed so far */ 437 438 unsigned char *next_out; /* pointer to next byte to write */ 439 unsigned int avail_out; /* number of bytes that can be written to next_out */ 440 mz_ulong total_out; /* total number of bytes produced so far */ 441 442 char *msg; /* error msg (unused) */ 443 inflate_state *state; /* internal state, allocated by zalloc/zfree */ 444 445 void *opaque; /* heap alloc function user pointer */ 446 447 int data_type; /* data_type (unused) */ 448 mz_ulong adler; /* adler32 of the source or uncompressed data */ 449 mz_ulong reserved; /* not used */ 450 451 #ifdef CRC32_IN_INFLATE 452 u32 key0; 453 u32 key1; 454 u32 key2; 455 u32 crc32; 456 LOCAL_AS u32 *crc32tab; 457 #endif 458 459} mz_stream; 460 461typedef mz_stream *mz_streamp; 462 463// hashcat-patched: not needed functions: 464// void miniz_def_free_func(void *opaque, void *address); 465// void *miniz_def_alloc_func(void *opaque, size_t items, size_t size); 466DECLSPEC int mz_inflate(mz_streamp pStream, int flush); 467DECLSPEC int mz_inflateEnd(mz_streamp pStream); 468 469 470 471DECLSPEC int mz_inflateInit2(mz_streamp pStream, int window_bits, inflate_state*); 472 473// hashcat-patched/hashcat-specific: 474DECLSPEC const mz_uint8 pIn_xor_byte (const mz_uint8 c, mz_streamp pStream) 475{ 476 #ifdef CRC32_IN_INFLATE 477 mz_uint8 r = c; 478 479 u32 key3; 480 481 update_key3 (pStream->key2, key3); 482 u32 plain = c ^ key3; 483 update_key012 (pStream->key0, pStream->key1, pStream->key2, plain, pStream->crc32tab); 484 485 return (mz_uint8) plain; 486 #else 487 return c; 488 #endif 489} 490 491 492DECLSPEC void memcpy_g(void *dest, MAYBE_GLOBAL const void *src, size_t n, mz_streamp pStream){ 493 MAYBE_GLOBAL char *csrc = (MAYBE_GLOBAL char *)src; 494 char *cdest = (char *)dest; 495 for (int i=0; i<n; i++){ 496 cdest[i] = pIn_xor_byte (csrc[i], pStream); 497 } 498} 499 500DECLSPEC tinfl_status tinfl_decompress(tinfl_decompressor *r, MAYBE_GLOBAL const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags, mz_streamp pStream) 501{ 502 503 const int s_length_base[31] = { 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 }; 504 const int s_length_extra[31] = { 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, 0, 0 }; 505 const int s_dist_base[32] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0 }; 506 const int s_dist_extra[32] = { 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 }; 507 const mz_uint8 s_length_dezigzag[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; 508 const int s_min_table_sizes[3] = { 257, 1, 4 }; 509 510 tinfl_status status = TINFL_STATUS_FAILED; 511 mz_uint32 num_bits, dist, counter, num_extra; 512 tinfl_bit_buf_t bit_buf; 513 MAYBE_GLOBAL const mz_uint8 *pIn_buf_cur = pIn_buf_next; 514 MAYBE_GLOBAL const mz_uint8 *pIn_buf_end = pIn_buf_next + *pIn_buf_size; 515 mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size; 516 size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start; 517 518 /* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). */ 519 if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) 520 { 521 *pIn_buf_size = *pOut_buf_size = 0; 522 return TINFL_STATUS_BAD_PARAM; 523 } 524 525 num_bits = r->m_num_bits; 526 bit_buf = r->m_bit_buf; 527 dist = r->m_dist; 528 counter = r->m_counter; 529 num_extra = r->m_num_extra; 530 dist_from_out_buf_start = r->m_dist_from_out_buf_start; 531 TINFL_CR_BEGIN 532 533 bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; 534 r->m_z_adler32 = r->m_check_adler32 = 1; 535 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) 536 { 537 TINFL_GET_BYTE(1, r->m_zhdr0); 538 TINFL_GET_BYTE(2, r->m_zhdr1); 539 counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); 540 if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 541 counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4))))); 542 if (counter) 543 { 544 TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); 545 } 546 } 547 548 do 549 { 550 TINFL_GET_BITS(3, r->m_final, 3); 551 r->m_type = r->m_final >> 1; 552 if (r->m_type == 0) 553 { 554 TINFL_SKIP_BITS(5, num_bits & 7); 555 for (counter = 0; counter < 4; ++counter) 556 { 557 if (num_bits) 558 TINFL_GET_BITS(6, r->m_raw_header[counter], 8); 559 else 560 TINFL_GET_BYTE(7, r->m_raw_header[counter]); 561 } 562 if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) 563 { 564 TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); 565 } 566 while ((counter) && (num_bits)) 567 { 568 TINFL_GET_BITS(51, dist, 8); 569 while (pOut_buf_cur >= pOut_buf_end) 570 { 571 TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); 572 } 573 *pOut_buf_cur++ = (mz_uint8)dist; 574 counter--; 575 } 576 while (counter) 577 { 578 size_t n; 579 while (pOut_buf_cur >= pOut_buf_end) 580 { 581 TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); 582 } 583 while (pIn_buf_cur >= pIn_buf_end) 584 { 585 TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); 586 } 587 n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter); 588 memcpy_g(pOut_buf_cur, pIn_buf_cur, n, pStream); 589 pIn_buf_cur += n; 590 pOut_buf_cur += n; 591 counter -= (mz_uint)n; 592 } 593 } 594 else if (r->m_type == 3) 595 { 596 TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED); 597 } 598 else 599 { 600 if (r->m_type == 1) 601 { 602 mz_uint8 *p = r->m_tables[0].m_code_size; 603 mz_uint i; 604 r->m_table_sizes[0] = 288; 605 r->m_table_sizes[1] = 32; 606 memset(r->m_tables[1].m_code_size, 5, 32); 607 for (i = 0; i <= 143; ++i) 608 *p++ = 8; 609 for (; i <= 255; ++i) 610 *p++ = 9; 611 for (; i <= 279; ++i) 612 *p++ = 7; 613 for (; i <= 287; ++i) 614 *p++ = 8; 615 } 616 else 617 { 618 for (counter = 0; counter < 3; counter++) 619 { 620 TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); 621 r->m_table_sizes[counter] += s_min_table_sizes[counter]; 622 } 623 memset(r->m_tables[2].m_code_size, 0, TINFL_MAX_HUFF_SYMBOLS_0); 624 625 for (counter = 0; counter < r->m_table_sizes[2]; counter++) 626 { 627 mz_uint s; 628 TINFL_GET_BITS(14, s, 3); 629 r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; 630 } 631 r->m_table_sizes[2] = 19; 632 } 633 for (; (int)r->m_type >= 0; r->m_type--) 634 { 635 int tree_next, tree_cur; 636 tinfl_huff_table *pTable; 637 mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; 638 pTable = &r->m_tables[r->m_type]; 639 640 memset((u8 *) total_syms, 0, 64); 641 memset((u8 *) pTable->m_look_up, 0, TINFL_FAST_LOOKUP_SIZE * 2); 642 memset((u8 *) pTable->m_tree, 0, TINFL_MAX_HUFF_SYMBOLS_0 * 2 * 2); 643 644 for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) 645 total_syms[pTable->m_code_size[i]]++; 646 used_syms = 0, total = 0; 647 next_code[0] = next_code[1] = 0; 648 for (i = 1; i <= 15; ++i) 649 { 650 used_syms += total_syms[i]; 651 next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); 652 } 653 if ((65536 != total) && (used_syms > 1)) 654 { 655 TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED); 656 } 657 for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index) 658 { 659 mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; 660 if (!code_size) 661 continue; 662 cur_code = next_code[code_size]++; 663 for (l = code_size; l > 0; l--, cur_code >>= 1) 664 rev_code = (rev_code << 1) | (cur_code & 1); 665 if (code_size <= TINFL_FAST_LOOKUP_BITS) 666 { 667 mz_int16 k = (mz_int16)((code_size << 9) | sym_index); 668 while (rev_code < TINFL_FAST_LOOKUP_SIZE) 669 { 670 pTable->m_look_up[rev_code] = k; 671 rev_code += (1 << code_size); 672 } 673 continue; 674 } 675 if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) 676 { 677 pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; 678 tree_cur = tree_next; 679 tree_next -= 2; 680 } 681 rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1); 682 for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) 683 { 684 tree_cur -= ((rev_code >>= 1) & 1); 685 if (!pTable->m_tree[-tree_cur - 1]) 686 { 687 pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; 688 tree_cur = tree_next; 689 tree_next -= 2; 690 } 691 else 692 tree_cur = pTable->m_tree[-tree_cur - 1]; 693 } 694 tree_cur -= ((rev_code >>= 1) & 1); 695 pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index; 696 } 697 if (r->m_type == 2) 698 { 699 for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) 700 { 701 mz_uint s; 702 TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); 703 if (dist < 16) 704 { 705 r->m_len_codes[counter++] = (mz_uint8)dist; 706 continue; 707 } 708 if ((dist == 16) && (!counter)) 709 { 710 TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED); 711 } 712 num_extra = "\02\03\07"[dist - 16]; 713 TINFL_GET_BITS(18, s, num_extra); 714 s += "\03\03\013"[dist - 16]; 715 716 memset(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); 717 718 719 counter += s; 720 } 721 if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) 722 { 723 TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED); 724 } 725 memcpy(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]); 726 memcpy(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]); 727 } 728 } 729 for (;;) 730 { 731 mz_uint8 *pSrc; 732 for (;;) 733 { 734 if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) 735 { 736 TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]); 737 if (counter >= 256) 738 break; 739 while (pOut_buf_cur >= pOut_buf_end) 740 { 741 TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); 742 } 743 *pOut_buf_cur++ = (mz_uint8)counter; 744 } 745 else 746 { 747 int sym2; 748 mz_uint code_len; 749 if (num_bits < 30) 750 { 751 u32 num = pIn_xor_byte (*pIn_buf_cur, pStream); 752 pIn_buf_cur++; 753 for(int i=1;i<4;i++){ 754 num |= pIn_xor_byte (*pIn_buf_cur, pStream) << 8*i; 755 pIn_buf_cur++; 756 } 757 //bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); 758 bit_buf |= ((tinfl_bit_buf_t)num) << num_bits; 759 //pIn_buf_cur += 4; 760 num_bits += 32; 761 } 762 if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 763 code_len = sym2 >> 9; 764 else 765 { 766 code_len = TINFL_FAST_LOOKUP_BITS; 767 do 768 { 769 sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; 770 } while (sym2 < 0); 771 } 772 counter = sym2; 773 bit_buf >>= code_len; 774 num_bits -= code_len; 775 if (counter & 256) 776 break; 777 778 if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 779 code_len = sym2 >> 9; 780 else 781 { 782 code_len = TINFL_FAST_LOOKUP_BITS; 783 do 784 { 785 sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; 786 } while (sym2 < 0); 787 } 788 bit_buf >>= code_len; 789 num_bits -= code_len; 790 791 pOut_buf_cur[0] = (mz_uint8)counter; 792 if (sym2 & 256) 793 { 794 pOut_buf_cur++; 795 counter = sym2; 796 break; 797 } 798 pOut_buf_cur[1] = (mz_uint8)sym2; 799 pOut_buf_cur += 2; 800 } 801 } 802 if ((counter &= 511) == 256) 803 break; 804 805 num_extra = s_length_extra[counter - 257]; 806 counter = s_length_base[counter - 257]; 807 if (num_extra) 808 { 809 mz_uint extra_bits; 810 TINFL_GET_BITS(25, extra_bits, num_extra); 811 counter += extra_bits; 812 } 813 814 TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]); 815 num_extra = s_dist_extra[dist]; 816 dist = s_dist_base[dist]; 817 if (num_extra) 818 { 819 mz_uint extra_bits; 820 TINFL_GET_BITS(27, extra_bits, num_extra); 821 dist += extra_bits; 822 } 823 824 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start; 825 if ((dist > dist_from_out_buf_start) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 826 { 827 TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED); 828 } 829 830 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask); 831 832 if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) 833 { 834 while (counter--) 835 { 836 while (pOut_buf_cur >= pOut_buf_end) 837 { 838 TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); 839 } 840 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask]; 841 } 842 continue; 843 } 844 else if ((counter >= 9) && (counter <= dist)) 845 { 846 const mz_uint8 *pSrc_end = pSrc + (counter & ~7); 847 do 848 { 849 //((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0]; 850 //((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1]; 851 852 for(int i=0;i<8;i++){ 853 pOut_buf_cur[i] = pSrc[i]; 854 } 855 856 pOut_buf_cur += 8; 857 } while ((pSrc += 8) < pSrc_end); 858 if ((counter &= 7) < 3) 859 { 860 if (counter) 861 { 862 pOut_buf_cur[0] = pSrc[0]; 863 if (counter > 1) 864 pOut_buf_cur[1] = pSrc[1]; 865 pOut_buf_cur += counter; 866 } 867 continue; 868 } 869 } 870 while(counter>2) 871 { 872 pOut_buf_cur[0] = pSrc[0]; 873 pOut_buf_cur[1] = pSrc[1]; 874 pOut_buf_cur[2] = pSrc[2]; 875 pOut_buf_cur += 3; 876 pSrc += 3; 877 counter -= 3; 878 } 879 if (counter > 0) 880 { 881 pOut_buf_cur[0] = pSrc[0]; 882 if (counter > 1) 883 pOut_buf_cur[1] = pSrc[1]; 884 pOut_buf_cur += counter; 885 } 886 } 887 } 888 } while (!(r->m_final & 1)); 889 890 /* Ensure byte alignment and put back any bytes from the bitbuf if we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */ 891 /* I'm being super conservative here. A number of simplifications can be made to the byte alignment part, and the Adler32 check shouldn't ever need to worry about reading from the bitbuf now. */ 892 TINFL_SKIP_BITS(32, num_bits & 7); 893 while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) 894 { 895 --pIn_buf_cur; 896 num_bits -= 8; 897 } 898 bit_buf &= (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1); 899 900 901 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) 902 { 903 for (counter = 0; counter < 4; ++counter) 904 { 905 mz_uint s; 906 if (num_bits) 907 TINFL_GET_BITS(41, s, 8); 908 else 909 TINFL_GET_BYTE(42, s); 910 r->m_z_adler32 = (r->m_z_adler32 << 8) | s; 911 } 912 } 913 TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE); 914 915 TINFL_CR_FINISH 916 917common_exit: 918 /* As long as we aren't telling the caller that we NEED more input to make forward progress: */ 919 /* Put back any bytes from the bitbuf in case we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */ 920 /* We need to be very careful here to NOT push back any bytes we definitely know we need to make forward progress, though, or we'll lock the caller up into an inf loop. */ 921 if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS)) 922 { 923 while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) 924 { 925 --pIn_buf_cur; 926 num_bits -= 8; 927 } 928 } 929 r->m_num_bits = num_bits; 930 r->m_bit_buf = bit_buf & (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1); 931 r->m_dist = dist; 932 r->m_counter = counter; 933 r->m_num_extra = num_extra; 934 r->m_dist_from_out_buf_start = dist_from_out_buf_start; 935 *pIn_buf_size = pIn_buf_cur - pIn_buf_next; 936 *pOut_buf_size = pOut_buf_cur - pOut_buf_next; 937 if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0)) 938 { 939 const mz_uint8 *ptr = pOut_buf_next; 940 size_t buf_len = *pOut_buf_size; 941 mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16; 942 size_t block_len = buf_len % 5552; 943 while (buf_len) 944 { 945 for (i = 0; i + 7 < block_len; i += 8, ptr += 8) 946 { 947 s1 += ptr[0], s2 += s1; 948 s1 += ptr[1], s2 += s1; 949 s1 += ptr[2], s2 += s1; 950 s1 += ptr[3], s2 += s1; 951 s1 += ptr[4], s2 += s1; 952 s1 += ptr[5], s2 += s1; 953 s1 += ptr[6], s2 += s1; 954 s1 += ptr[7], s2 += s1; 955 } 956 for (; i < block_len; ++i) 957 s1 += *ptr++, s2 += s1; 958 s1 %= 65521U, s2 %= 65521U; 959 buf_len -= block_len; 960 block_len = 5552; 961 } 962 r->m_check_adler32 = (s2 << 16) + s1; 963 if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) 964 status = TINFL_STATUS_ADLER32_MISMATCH; 965 } 966 return status; 967} 968 969 970DECLSPEC int mz_inflateInit2(mz_streamp pStream, int window_bits, inflate_state *pDecomp) 971{ 972 if (pStream == 0) 973 return MZ_STREAM_ERROR; 974 if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) 975 return MZ_PARAM_ERROR; 976 977 pStream->data_type = 0; 978 pStream->adler = 0; 979 pStream->msg = Z_NULL; 980 pStream->total_in = 0; 981 pStream->total_out = 0; 982 pStream->reserved = 0; 983 984 //pStream->state = (struct mz_internal_state *)pDecomp; 985 pStream->state = (inflate_state *) pDecomp; 986 987 tinfl_init(&pDecomp->m_decomp); 988 pDecomp->m_dict_ofs = 0; 989 pDecomp->m_dict_avail = 0; 990 pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; 991 pDecomp->m_first_call = 1; 992 pDecomp->m_has_flushed = 0; 993 pDecomp->m_window_bits = window_bits; 994 995 return MZ_OK; 996} 997 998DECLSPEC int mz_inflate(mz_streamp pStream, int flush) 999{ 1000 inflate_state *pState; 1001 mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32; 1002 size_t in_bytes, out_bytes, orig_avail_in; 1003 tinfl_status status; 1004 1005 if ((pStream == 0) || (pStream->state == 0)) 1006 return MZ_STREAM_ERROR; 1007 if (flush == MZ_PARTIAL_FLUSH) 1008 flush = MZ_SYNC_FLUSH; 1009 if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) 1010 return MZ_STREAM_ERROR; 1011 1012 pState = (inflate_state *)pStream->state; 1013 if (pState->m_window_bits > 0) 1014 decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER; 1015 orig_avail_in = pStream->avail_in; 1016 1017 first_call = pState->m_first_call; 1018 pState->m_first_call = 0; 1019 if (pState->m_last_status < 0) 1020 return MZ_DATA_ERROR; 1021 1022 if (pState->m_has_flushed && (flush != MZ_FINISH)) 1023 return MZ_STREAM_ERROR; 1024 pState->m_has_flushed |= (flush == MZ_FINISH); 1025 1026 if ((flush == MZ_FINISH) && (first_call)) 1027 { 1028 /* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. */ 1029 decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; 1030 in_bytes = pStream->avail_in; 1031 out_bytes = pStream->avail_out; 1032 status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags, pStream); 1033 1034 #ifdef CRC32_IN_INFLATE 1035 for (int i = 0; i < out_bytes; i++) 1036 { 1037 pStream->crc32 = CRC32 (pStream->crc32, pStream->next_out[i], pStream->crc32tab); 1038 } 1039 #endif 1040 1041 pState->m_last_status = status; 1042 pStream->next_in += (mz_uint)in_bytes; 1043 pStream->avail_in -= (mz_uint)in_bytes; 1044 pStream->total_in += (mz_uint)in_bytes; 1045 pStream->adler = tinfl_get_adler32(&pState->m_decomp); 1046 //pStream->next_out += (mz_uint)out_bytes; 1047 //pStream->avail_out -= (mz_uint)out_bytes; 1048 pStream->total_out += (mz_uint)out_bytes; 1049 1050 if (status < 0) 1051 return MZ_DATA_ERROR; 1052 else if (status != TINFL_STATUS_DONE) 1053 { 1054 pState->m_last_status = TINFL_STATUS_FAILED; 1055 return MZ_BUF_ERROR; 1056 } 1057 return MZ_STREAM_END; 1058 } 1059 /* flush != MZ_FINISH then we must assume there's more input. */ 1060 if (flush != MZ_FINISH) 1061 decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT; 1062 1063 if (pState->m_dict_avail) 1064 { 1065 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); 1066 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); 1067 1068 #ifdef CRC32_IN_INFLATE 1069 for (int i = 0; i < n; i++) 1070 { 1071 pStream->crc32 = CRC32 (pStream->crc32, pStream->next_out[i], pStream->crc32tab); 1072 } 1073 #endif 1074 1075 //pStream->next_out += n; 1076 //pStream->avail_out -= n; 1077 pStream->total_out += n; 1078 pState->m_dict_avail -= n; 1079 pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); 1080 return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; 1081 } 1082 1083 1084 for (;;) 1085 { 1086 in_bytes = pStream->avail_in; 1087 out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs; 1088 1089 status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags, pStream); 1090 pState->m_last_status = status; 1091 1092 pStream->next_in += (mz_uint)in_bytes; 1093 pStream->avail_in -= (mz_uint)in_bytes; 1094 pStream->total_in += (mz_uint)in_bytes; 1095 pStream->adler = tinfl_get_adler32(&pState->m_decomp); 1096 1097 pState->m_dict_avail = (mz_uint)out_bytes; 1098 1099 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); 1100 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); 1101 1102 #ifdef CRC32_IN_INFLATE 1103 for (int i = 0; i < n; i++) 1104 { 1105 pStream->crc32 = CRC32 (pStream->crc32, pStream->next_out[i], pStream->crc32tab); 1106 } 1107 #endif 1108 1109 //pStream->next_out += n; 1110 //pStream->avail_out -= n; 1111 pStream->total_out += n; 1112 pState->m_dict_avail -= n; 1113 pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); 1114 1115 if (status < 0) 1116 return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). */ 1117 else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) 1118 return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. */ 1119 else if (flush == MZ_FINISH) 1120 { 1121 /* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */ 1122 if (status == TINFL_STATUS_DONE) 1123 return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END; 1124 /* status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong. */ 1125 else if (!pStream->avail_out) 1126 return MZ_BUF_ERROR; 1127 } 1128 else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) 1129 break; 1130 } 1131 1132 return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; 1133} 1134 1135// hashcat-patched: helper function for shifted u32 1136 1137DECLSPEC u32 GETSHIFTEDINT (u32 *a, const int n) 1138{ 1139 const int d = n / 4; 1140 const int m = n & 3; 1141 1142 u64 tmp = hl32_to_64_S (a[d + 1], a[d + 0]); 1143 1144 tmp >>= m * 8; 1145 1146 return l32_from_64_S (tmp); 1147} 1148 1149// hashcat-patched: faster memcpy for our large (TINFL_LZ_DICT_SIZE) move of bytes from the old output to the window/lookup table 1150 1151DECLSPEC void hc_shift_inflate_dict (u8 *buf, const u32 offset, const u32 len) 1152{ 1153 u32 *ptr = (u32 *) buf; 1154 1155 // we need to use len - 4 here to avoid buffer overflows caused by the u64 type in GETSHIFTEDINT 1156 1157 u32 i, j; 1158 1159 for (i = 0, j = 0; i < len - 4; i += 4, j++) 1160 { 1161 ptr[j] = GETSHIFTEDINT (ptr, offset + i); 1162 } 1163 1164 // final step (last 4 bytes are special): 1165 1166 ptr[j] = (buf[offset + i + 3] << 24) | (buf[offset + i + 2] << 16) | (buf[offset + i + 1] << 8) | buf[offset + i]; 1167} 1168 1169// hashcat-patched: the mz_inflate () function from above doesn't work for us because we need to allow larger input/output and 1170// we only need the crc32 checksum actually 1171 1172DECLSPEC int hc_inflate (mz_streamp pStream) 1173{ 1174 // we can't use the full TINFL_LZ_DICT_SIZE buffer because even with only 16 input bytes 1175 // we can get pretty close to our max available output buffer: 1176 1177 // size_t in_bytes = MZ_MIN (TINFL_LZ_DICT_SIZE, pStream->avail_in); 1178 size_t in_bytes = MZ_MIN (16, pStream->avail_in); 1179 1180 mz_uint decomp_flags = ((pStream->avail_in - in_bytes) > 0) ? TINFL_FLAG_HAS_MORE_INPUT : 0; 1181 1182 decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; 1183 1184 inflate_state *pState = pStream->state; 1185 1186 size_t out_bytes = pStream->avail_out; 1187 1188 tinfl_status status = tinfl_decompress (&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out + pStream->total_out, &out_bytes, decomp_flags, pStream); 1189 1190 #ifdef CRC32_IN_INFLATE 1191 for (int i = 0; i < out_bytes; i++) 1192 { 1193 pStream->crc32 = CRC32 (pStream->crc32, pStream->next_out[pStream->total_out + i], pStream->crc32tab); 1194 } 1195 #endif 1196 1197 pStream->next_in += (mz_uint) in_bytes; 1198 pStream->avail_in -= (mz_uint) in_bytes; 1199 pStream->total_in += (mz_uint) in_bytes; 1200 1201 pStream->avail_out -= out_bytes; 1202 pStream->total_out += out_bytes; 1203 1204 if (pStream->avail_out < TINFL_LZ_DICT_SIZE) 1205 { 1206 // reset: 1207 1208 // move the last TINFL_LZ_DICT_SIZE bytes to the start of the output buffer 1209 1210 // memcpy (pStream->next_out, pStream->next_out + pStream->total_out - TINFL_LZ_DICT_SIZE, TINFL_LZ_DICT_SIZE); 1211 hc_shift_inflate_dict (pStream->next_out, pStream->total_out - TINFL_LZ_DICT_SIZE, TINFL_LZ_DICT_SIZE); 1212 1213 pStream->avail_out = TINFL_LZ_DICT_SIZE; 1214 pStream->total_out = TINFL_LZ_DICT_SIZE; 1215 } 1216 1217 if (status < 0) 1218 { 1219 return MZ_DATA_ERROR; 1220 } 1221 1222 if (in_bytes == 0) 1223 { 1224 return MZ_STREAM_END; 1225 } 1226 1227 if (pStream->avail_in == 0) 1228 { 1229 return MZ_STREAM_END; 1230 } 1231 1232 return (status == TINFL_STATUS_DONE) ? MZ_STREAM_END : MZ_OK; 1233} 1234