| OLD | NEW |
| 1 /* deflate.c -- compress data using the deflation algorithm | 1 /* deflate.c -- compress data using the deflation algorithm |
| 2 * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler | 2 * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler |
| 3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /* | 6 /* |
| 7 * ALGORITHM | 7 * ALGORITHM |
| 8 * | 8 * |
| 9 * The "deflation" process depends on being able to identify portions | 9 * The "deflation" process depends on being able to identify portions |
| 10 * of the input text which are identical to earlier input (within a | 10 * of the input text which are identical to earlier input (within a |
| 11 * sliding window trailing behind the input currently being processed). | 11 * sliding window trailing behind the input currently being processed). |
| 12 * | 12 * |
| (...skipping 17 matching lines...) Expand all Loading... |
| 30 * | 30 * |
| 31 * ACKNOWLEDGEMENTS | 31 * ACKNOWLEDGEMENTS |
| 32 * | 32 * |
| 33 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and | 33 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and |
| 34 * I found it in 'freeze' written by Leonid Broukhis. | 34 * I found it in 'freeze' written by Leonid Broukhis. |
| 35 * Thanks to many people for bug reports and testing. | 35 * Thanks to many people for bug reports and testing. |
| 36 * | 36 * |
| 37 * REFERENCES | 37 * REFERENCES |
| 38 * | 38 * |
| 39 * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". | 39 * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". |
| 40 * Available in http://www.ietf.org/rfc/rfc1951.txt | 40 * Available in http://tools.ietf.org/html/rfc1951 |
| 41 * | 41 * |
| 42 * A description of the Rabin and Karp algorithm is given in the book | 42 * A description of the Rabin and Karp algorithm is given in the book |
| 43 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. | 43 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. |
| 44 * | 44 * |
| 45 * Fiala,E.R., and Greene,D.H. | 45 * Fiala,E.R., and Greene,D.H. |
| 46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 | 46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 |
| 47 * | 47 * |
| 48 */ | 48 */ |
| 49 | 49 |
| 50 /* @(#) $Id$ */ | 50 /* @(#) $Id$ */ |
| 51 | 51 |
| 52 #include <assert.h> | 52 #include <assert.h> |
| 53 | 53 |
| 54 #include "deflate.h" | 54 #include "deflate.h" |
| 55 #include "x86.h" | 55 #include "x86.h" |
| 56 | 56 |
| 57 const char deflate_copyright[] = | 57 const char deflate_copyright[] = |
| 58 " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler "; | 58 " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler "; |
| 59 /* | 59 /* |
| 60 If you use the zlib library in a product, an acknowledgment is welcome | 60 If you use the zlib library in a product, an acknowledgment is welcome |
| 61 in the documentation of your product. If for some reason you cannot | 61 in the documentation of your product. If for some reason you cannot |
| 62 include such an acknowledgment, I would appreciate that you keep this | 62 include such an acknowledgment, I would appreciate that you keep this |
| 63 copyright string in the executable of your product. | 63 copyright string in the executable of your product. |
| 64 */ | 64 */ |
| 65 | 65 |
| 66 /* =========================================================================== | 66 /* =========================================================================== |
| 67 * Function prototypes. | 67 * Function prototypes. |
| 68 */ | 68 */ |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 * meaning. | 169 * meaning. |
| 170 */ | 170 */ |
| 171 | 171 |
| 172 #define EQUAL 0 | 172 #define EQUAL 0 |
| 173 /* result of memcmp for equal strings */ | 173 /* result of memcmp for equal strings */ |
| 174 | 174 |
| 175 #ifndef NO_DUMMY_DECL | 175 #ifndef NO_DUMMY_DECL |
| 176 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ | 176 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ |
| 177 #endif | 177 #endif |
| 178 | 178 |
| 179 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ |
| 180 #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0)) |
| 181 |
| 179 /* =========================================================================== | 182 /* =========================================================================== |
| 180 * Update a hash value with the given input byte | 183 * Update a hash value with the given input byte |
| 181 * IN assertion: all calls to to UPDATE_HASH are made with consecutive | 184 * IN assertion: all calls to to UPDATE_HASH are made with consecutive |
| 182 * input characters, so that a running hash key can be computed from the | 185 * input characters, so that a running hash key can be computed from the |
| 183 * previous key instead of complete recalculation each time. | 186 * previous key instead of complete recalculation each time. |
| 184 */ | 187 */ |
| 185 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) | 188 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) |
| 186 | 189 |
| 187 /* =========================================================================== | 190 /* =========================================================================== |
| 188 * Insert string str in the dictionary and set match_head to the previous head | 191 * Insert string str in the dictionary and set match_head to the previous head |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 x86_check_features(); | 265 x86_check_features(); |
| 263 | 266 |
| 264 if (version == Z_NULL || version[0] != my_version[0] || | 267 if (version == Z_NULL || version[0] != my_version[0] || |
| 265 stream_size != sizeof(z_stream)) { | 268 stream_size != sizeof(z_stream)) { |
| 266 return Z_VERSION_ERROR; | 269 return Z_VERSION_ERROR; |
| 267 } | 270 } |
| 268 if (strm == Z_NULL) return Z_STREAM_ERROR; | 271 if (strm == Z_NULL) return Z_STREAM_ERROR; |
| 269 | 272 |
| 270 strm->msg = Z_NULL; | 273 strm->msg = Z_NULL; |
| 271 if (strm->zalloc == (alloc_func)0) { | 274 if (strm->zalloc == (alloc_func)0) { |
| 275 #ifdef Z_SOLO |
| 276 return Z_STREAM_ERROR; |
| 277 #else |
| 272 strm->zalloc = zcalloc; | 278 strm->zalloc = zcalloc; |
| 273 strm->opaque = (voidpf)0; | 279 strm->opaque = (voidpf)0; |
| 280 #endif |
| 274 } | 281 } |
| 275 if (strm->zfree == (free_func)0) strm->zfree = zcfree; | 282 if (strm->zfree == (free_func)0) |
| 283 #ifdef Z_SOLO |
| 284 return Z_STREAM_ERROR; |
| 285 #else |
| 286 strm->zfree = zcfree; |
| 287 #endif |
| 276 | 288 |
| 277 #ifdef FASTEST | 289 #ifdef FASTEST |
| 278 if (level != 0) level = 1; | 290 if (level != 0) level = 1; |
| 279 #else | 291 #else |
| 280 if (level == Z_DEFAULT_COMPRESSION) level = 6; | 292 if (level == Z_DEFAULT_COMPRESSION) level = 6; |
| 281 #endif | 293 #endif |
| 282 | 294 |
| 283 if (windowBits < 0) { /* suppress zlib wrapper */ | 295 if (windowBits < 0) { /* suppress zlib wrapper */ |
| 284 wrap = 0; | 296 wrap = 0; |
| 285 windowBits = -windowBits; | 297 windowBits = -windowBits; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 | 340 |
| 329 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ | 341 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ |
| 330 | 342 |
| 331 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); | 343 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); |
| 332 s->pending_buf = (uchf *) overlay; | 344 s->pending_buf = (uchf *) overlay; |
| 333 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); | 345 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); |
| 334 | 346 |
| 335 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || | 347 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || |
| 336 s->pending_buf == Z_NULL) { | 348 s->pending_buf == Z_NULL) { |
| 337 s->status = FINISH_STATE; | 349 s->status = FINISH_STATE; |
| 338 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); | 350 strm->msg = ERR_MSG(Z_MEM_ERROR); |
| 339 deflateEnd (strm); | 351 deflateEnd (strm); |
| 340 return Z_MEM_ERROR; | 352 return Z_MEM_ERROR; |
| 341 } | 353 } |
| 342 s->d_buf = overlay + s->lit_bufsize/sizeof(ush); | 354 s->d_buf = overlay + s->lit_bufsize/sizeof(ush); |
| 343 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; | 355 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; |
| 344 | 356 |
| 345 s->level = level; | 357 s->level = level; |
| 346 s->strategy = strategy; | 358 s->strategy = strategy; |
| 347 s->method = (Byte)method; | 359 s->method = (Byte)method; |
| 348 | 360 |
| 349 return deflateReset(strm); | 361 return deflateReset(strm); |
| 350 } | 362 } |
| 351 | 363 |
| 352 /* ========================================================================= */ | 364 /* ========================================================================= */ |
| 353 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) | 365 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) |
| 354 z_streamp strm; | 366 z_streamp strm; |
| 355 const Bytef *dictionary; | 367 const Bytef *dictionary; |
| 356 uInt dictLength; | 368 uInt dictLength; |
| 357 { | 369 { |
| 358 deflate_state *s; | 370 deflate_state *s; |
| 359 uInt length = dictLength; | 371 uInt str, n; |
| 360 uInt n; | 372 int wrap; |
| 361 IPos hash_head = 0; | 373 unsigned avail; |
| 374 z_const unsigned char *next; |
| 362 | 375 |
| 363 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || | 376 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL) |
| 364 strm->state->wrap == 2 || | 377 return Z_STREAM_ERROR; |
| 365 (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) | 378 s = strm->state; |
| 379 wrap = s->wrap; |
| 380 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) |
| 366 return Z_STREAM_ERROR; | 381 return Z_STREAM_ERROR; |
| 367 | 382 |
| 368 s = strm->state; | 383 /* when using zlib wrappers, compute Adler-32 for provided dictionary */ |
| 369 if (s->wrap) | 384 if (wrap == 1) |
| 370 strm->adler = adler32(strm->adler, dictionary, dictLength); | 385 strm->adler = adler32(strm->adler, dictionary, dictLength); |
| 386 s->wrap = 0; /* avoid computing Adler-32 in read_buf */ |
| 371 | 387 |
| 372 if (length < MIN_MATCH) return Z_OK; | 388 /* if dictionary would fill window, just replace the history */ |
| 373 if (length > s->w_size) { | 389 if (dictLength >= s->w_size) { |
| 374 length = s->w_size; | 390 if (wrap == 0) { /* already empty otherwise */ |
| 375 dictionary += dictLength - length; /* use the tail of the dictionary */ | 391 CLEAR_HASH(s); |
| 392 s->strstart = 0; |
| 393 s->block_start = 0L; |
| 394 s->insert = 0; |
| 395 } |
| 396 dictionary += dictLength - s->w_size; /* use the tail */ |
| 397 dictLength = s->w_size; |
| 376 } | 398 } |
| 377 zmemcpy(s->window, dictionary, length); | |
| 378 s->strstart = length; | |
| 379 s->block_start = (long)length; | |
| 380 | 399 |
| 381 /* Insert all strings in the hash table (except for the last two bytes). | 400 /* insert dictionary into window and hash */ |
| 382 * s->lookahead stays null, so s->ins_h will be recomputed at the next | 401 avail = strm->avail_in; |
| 383 * call of fill_window. | 402 next = strm->next_in; |
| 384 */ | 403 strm->avail_in = dictLength; |
| 385 s->ins_h = s->window[0]; | 404 strm->next_in = (z_const Bytef *)dictionary; |
| 386 UPDATE_HASH(s, s->ins_h, s->window[1]); | 405 fill_window(s); |
| 387 for (n = 0; n <= length - MIN_MATCH; n++) { | 406 while (s->lookahead >= MIN_MATCH) { |
| 388 insert_string(s, n); | 407 str = s->strstart; |
| 408 n = s->lookahead - (MIN_MATCH-1); |
| 409 do { |
| 410 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); |
| 411 #ifndef FASTEST |
| 412 s->prev[str & s->w_mask] = s->head[s->ins_h]; |
| 413 #endif |
| 414 s->head[s->ins_h] = (Pos)str; |
| 415 str++; |
| 416 } while (--n); |
| 417 s->strstart = str; |
| 418 s->lookahead = MIN_MATCH-1; |
| 419 fill_window(s); |
| 389 } | 420 } |
| 390 if (hash_head) hash_head = 0; /* to make compiler happy */ | 421 s->strstart += s->lookahead; |
| 422 s->block_start = (long)s->strstart; |
| 423 s->insert = s->lookahead; |
| 424 s->lookahead = 0; |
| 425 s->match_length = s->prev_length = MIN_MATCH-1; |
| 426 s->match_available = 0; |
| 427 strm->next_in = next; |
| 428 strm->avail_in = avail; |
| 429 s->wrap = wrap; |
| 391 return Z_OK; | 430 return Z_OK; |
| 392 } | 431 } |
| 393 | 432 |
| 394 /* ========================================================================= */ | 433 /* ========================================================================= */ |
| 395 int ZEXPORT deflateReset (strm) | 434 int ZEXPORT deflateResetKeep (strm) |
| 396 z_streamp strm; | 435 z_streamp strm; |
| 397 { | 436 { |
| 398 deflate_state *s; | 437 deflate_state *s; |
| 399 | 438 |
| 400 if (strm == Z_NULL || strm->state == Z_NULL || | 439 if (strm == Z_NULL || strm->state == Z_NULL || |
| 401 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { | 440 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { |
| 402 return Z_STREAM_ERROR; | 441 return Z_STREAM_ERROR; |
| 403 } | 442 } |
| 404 | 443 |
| 405 strm->total_in = strm->total_out = 0; | 444 strm->total_in = strm->total_out = 0; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 417 } | 456 } |
| 418 s->status = s->wrap ? INIT_STATE : BUSY_STATE; | 457 s->status = s->wrap ? INIT_STATE : BUSY_STATE; |
| 419 strm->adler = | 458 strm->adler = |
| 420 #ifdef GZIP | 459 #ifdef GZIP |
| 421 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : | 460 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : |
| 422 #endif | 461 #endif |
| 423 adler32(0L, Z_NULL, 0); | 462 adler32(0L, Z_NULL, 0); |
| 424 s->last_flush = Z_NO_FLUSH; | 463 s->last_flush = Z_NO_FLUSH; |
| 425 | 464 |
| 426 _tr_init(s); | 465 _tr_init(s); |
| 427 lm_init(s); | |
| 428 | 466 |
| 429 return Z_OK; | 467 return Z_OK; |
| 430 } | 468 } |
| 431 | 469 |
| 432 /* ========================================================================= */ | 470 /* ========================================================================= */ |
| 471 int ZEXPORT deflateReset (strm) |
| 472 z_streamp strm; |
| 473 { |
| 474 int ret; |
| 475 |
| 476 ret = deflateResetKeep(strm); |
| 477 if (ret == Z_OK) |
| 478 lm_init(strm->state); |
| 479 return ret; |
| 480 } |
| 481 |
| 482 /* ========================================================================= */ |
| 433 int ZEXPORT deflateSetHeader (strm, head) | 483 int ZEXPORT deflateSetHeader (strm, head) |
| 434 z_streamp strm; | 484 z_streamp strm; |
| 435 gz_headerp head; | 485 gz_headerp head; |
| 436 { | 486 { |
| 437 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 487 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 438 if (strm->state->wrap != 2) return Z_STREAM_ERROR; | 488 if (strm->state->wrap != 2) return Z_STREAM_ERROR; |
| 439 strm->state->gzhead = head; | 489 strm->state->gzhead = head; |
| 440 return Z_OK; | 490 return Z_OK; |
| 441 } | 491 } |
| 442 | 492 |
| 443 /* ========================================================================= */ | 493 /* ========================================================================= */ |
| 494 int ZEXPORT deflatePending (strm, pending, bits) |
| 495 unsigned *pending; |
| 496 int *bits; |
| 497 z_streamp strm; |
| 498 { |
| 499 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 500 if (pending != Z_NULL) |
| 501 *pending = strm->state->pending; |
| 502 if (bits != Z_NULL) |
| 503 *bits = strm->state->bi_valid; |
| 504 return Z_OK; |
| 505 } |
| 506 |
| 507 /* ========================================================================= */ |
| 444 int ZEXPORT deflatePrime (strm, bits, value) | 508 int ZEXPORT deflatePrime (strm, bits, value) |
| 445 z_streamp strm; | 509 z_streamp strm; |
| 446 int bits; | 510 int bits; |
| 447 int value; | 511 int value; |
| 448 { | 512 { |
| 513 deflate_state *s; |
| 514 int put; |
| 515 |
| 449 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 516 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 450 strm->state->bi_valid = bits; | 517 s = strm->state; |
| 451 strm->state->bi_buf = (ush)(value & ((1 << bits) - 1)); | 518 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) |
| 519 return Z_BUF_ERROR; |
| 520 do { |
| 521 put = Buf_size - s->bi_valid; |
| 522 if (put > bits) |
| 523 put = bits; |
| 524 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); |
| 525 s->bi_valid += put; |
| 526 _tr_flush_bits(s); |
| 527 value >>= put; |
| 528 bits -= put; |
| 529 } while (bits); |
| 452 return Z_OK; | 530 return Z_OK; |
| 453 } | 531 } |
| 454 | 532 |
| 455 /* ========================================================================= */ | 533 /* ========================================================================= */ |
| 456 int ZEXPORT deflateParams(strm, level, strategy) | 534 int ZEXPORT deflateParams(strm, level, strategy) |
| 457 z_streamp strm; | 535 z_streamp strm; |
| 458 int level; | 536 int level; |
| 459 int strategy; | 537 int strategy; |
| 460 { | 538 { |
| 461 deflate_state *s; | 539 deflate_state *s; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 472 #endif | 550 #endif |
| 473 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { | 551 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { |
| 474 return Z_STREAM_ERROR; | 552 return Z_STREAM_ERROR; |
| 475 } | 553 } |
| 476 func = configuration_table[s->level].func; | 554 func = configuration_table[s->level].func; |
| 477 | 555 |
| 478 if ((strategy != s->strategy || func != configuration_table[level].func) && | 556 if ((strategy != s->strategy || func != configuration_table[level].func) && |
| 479 strm->total_in != 0) { | 557 strm->total_in != 0) { |
| 480 /* Flush the last buffer: */ | 558 /* Flush the last buffer: */ |
| 481 err = deflate(strm, Z_BLOCK); | 559 err = deflate(strm, Z_BLOCK); |
| 560 if (err == Z_BUF_ERROR && s->pending == 0) |
| 561 err = Z_OK; |
| 482 } | 562 } |
| 483 if (s->level != level) { | 563 if (s->level != level) { |
| 484 s->level = level; | 564 s->level = level; |
| 485 s->max_lazy_match = configuration_table[level].max_lazy; | 565 s->max_lazy_match = configuration_table[level].max_lazy; |
| 486 s->good_match = configuration_table[level].good_length; | 566 s->good_match = configuration_table[level].good_length; |
| 487 s->nice_match = configuration_table[level].nice_length; | 567 s->nice_match = configuration_table[level].nice_length; |
| 488 s->max_chain_length = configuration_table[level].max_chain; | 568 s->max_chain_length = configuration_table[level].max_chain; |
| 489 } | 569 } |
| 490 s->strategy = strategy; | 570 s->strategy = strategy; |
| 491 return err; | 571 return err; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 | 679 |
| 600 /* ========================================================================= | 680 /* ========================================================================= |
| 601 * Flush as much pending output as possible. All deflate() output goes | 681 * Flush as much pending output as possible. All deflate() output goes |
| 602 * through this function so some applications may wish to modify it | 682 * through this function so some applications may wish to modify it |
| 603 * to avoid allocating a large strm->next_out buffer and copying into it. | 683 * to avoid allocating a large strm->next_out buffer and copying into it. |
| 604 * (See also read_buf()). | 684 * (See also read_buf()). |
| 605 */ | 685 */ |
| 606 local void flush_pending(strm) | 686 local void flush_pending(strm) |
| 607 z_streamp strm; | 687 z_streamp strm; |
| 608 { | 688 { |
| 609 unsigned len = strm->state->pending; | 689 unsigned len; |
| 690 deflate_state *s = strm->state; |
| 610 | 691 |
| 692 _tr_flush_bits(s); |
| 693 len = s->pending; |
| 611 if (len > strm->avail_out) len = strm->avail_out; | 694 if (len > strm->avail_out) len = strm->avail_out; |
| 612 if (len == 0) return; | 695 if (len == 0) return; |
| 613 | 696 |
| 614 zmemcpy(strm->next_out, strm->state->pending_out, len); | 697 zmemcpy(strm->next_out, s->pending_out, len); |
| 615 strm->next_out += len; | 698 strm->next_out += len; |
| 616 strm->state->pending_out += len; | 699 s->pending_out += len; |
| 617 strm->total_out += len; | 700 strm->total_out += len; |
| 618 strm->avail_out -= len; | 701 strm->avail_out -= len; |
| 619 strm->state->pending -= len; | 702 s->pending -= len; |
| 620 if (strm->state->pending == 0) { | 703 if (s->pending == 0) { |
| 621 strm->state->pending_out = strm->state->pending_buf; | 704 s->pending_out = s->pending_buf; |
| 622 } | 705 } |
| 623 } | 706 } |
| 624 | 707 |
| 625 /* ========================================================================= */ | 708 /* ========================================================================= */ |
| 626 int ZEXPORT deflate (strm, flush) | 709 int ZEXPORT deflate (strm, flush) |
| 627 z_streamp strm; | 710 z_streamp strm; |
| 628 int flush; | 711 int flush; |
| 629 { | 712 { |
| 630 int old_flush; /* value of flush param for previous deflate call */ | 713 int old_flush; /* value of flush param for previous deflate call */ |
| 631 deflate_state *s; | 714 deflate_state *s; |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 * return OK instead of BUF_ERROR at next call of deflate: | 921 * return OK instead of BUF_ERROR at next call of deflate: |
| 839 */ | 922 */ |
| 840 s->last_flush = -1; | 923 s->last_flush = -1; |
| 841 return Z_OK; | 924 return Z_OK; |
| 842 } | 925 } |
| 843 | 926 |
| 844 /* Make sure there is something to do and avoid duplicate consecutive | 927 /* Make sure there is something to do and avoid duplicate consecutive |
| 845 * flushes. For repeated and useless calls with Z_FINISH, we keep | 928 * flushes. For repeated and useless calls with Z_FINISH, we keep |
| 846 * returning Z_STREAM_END instead of Z_BUF_ERROR. | 929 * returning Z_STREAM_END instead of Z_BUF_ERROR. |
| 847 */ | 930 */ |
| 848 } else if (strm->avail_in == 0 && flush <= old_flush && | 931 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && |
| 849 flush != Z_FINISH) { | 932 flush != Z_FINISH) { |
| 850 ERR_RETURN(strm, Z_BUF_ERROR); | 933 ERR_RETURN(strm, Z_BUF_ERROR); |
| 851 } | 934 } |
| 852 | 935 |
| 853 /* User must not provide more input after the first FINISH: */ | 936 /* User must not provide more input after the first FINISH: */ |
| 854 if (s->status == FINISH_STATE && strm->avail_in != 0) { | 937 if (s->status == FINISH_STATE && strm->avail_in != 0) { |
| 855 ERR_RETURN(strm, Z_BUF_ERROR); | 938 ERR_RETURN(strm, Z_BUF_ERROR); |
| 856 } | 939 } |
| 857 | 940 |
| 858 /* Start a new block or continue the current one. | 941 /* Start a new block or continue the current one. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ | 987 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ |
| 905 _tr_stored_block(s, (char*)0, 0L, 0); | 988 _tr_stored_block(s, (char*)0, 0L, 0); |
| 906 /* For a full flush, this empty block will be recognized | 989 /* For a full flush, this empty block will be recognized |
| 907 * as a special marker by inflate_sync(). | 990 * as a special marker by inflate_sync(). |
| 908 */ | 991 */ |
| 909 if (flush == Z_FULL_FLUSH) { | 992 if (flush == Z_FULL_FLUSH) { |
| 910 CLEAR_HASH(s); /* forget history */ | 993 CLEAR_HASH(s); /* forget history */ |
| 911 if (s->lookahead == 0) { | 994 if (s->lookahead == 0) { |
| 912 s->strstart = 0; | 995 s->strstart = 0; |
| 913 s->block_start = 0L; | 996 s->block_start = 0L; |
| 997 s->insert = 0; |
| 914 } | 998 } |
| 915 } | 999 } |
| 916 } | 1000 } |
| 917 flush_pending(strm); | 1001 flush_pending(strm); |
| 918 if (strm->avail_out == 0) { | 1002 if (strm->avail_out == 0) { |
| 919 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ | 1003 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ |
| 920 return Z_OK; | 1004 return Z_OK; |
| 921 } | 1005 } |
| 922 } | 1006 } |
| 923 } | 1007 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1001 deflate_state *ss; | 1085 deflate_state *ss; |
| 1002 ushf *overlay; | 1086 ushf *overlay; |
| 1003 | 1087 |
| 1004 | 1088 |
| 1005 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { | 1089 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { |
| 1006 return Z_STREAM_ERROR; | 1090 return Z_STREAM_ERROR; |
| 1007 } | 1091 } |
| 1008 | 1092 |
| 1009 ss = source->state; | 1093 ss = source->state; |
| 1010 | 1094 |
| 1011 zmemcpy(dest, source, sizeof(z_stream)); | 1095 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); |
| 1012 | 1096 |
| 1013 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); | 1097 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); |
| 1014 if (ds == Z_NULL) return Z_MEM_ERROR; | 1098 if (ds == Z_NULL) return Z_MEM_ERROR; |
| 1015 dest->state = (struct internal_state FAR *) ds; | 1099 dest->state = (struct internal_state FAR *) ds; |
| 1016 zmemcpy(ds, ss, sizeof(deflate_state)); | 1100 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); |
| 1017 ds->strm = dest; | 1101 ds->strm = dest; |
| 1018 | 1102 |
| 1019 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); | 1103 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); |
| 1020 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); | 1104 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); |
| 1021 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); | 1105 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); |
| 1022 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); | 1106 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); |
| 1023 ds->pending_buf = (uchf *) overlay; | 1107 ds->pending_buf = (uchf *) overlay; |
| 1024 | 1108 |
| 1025 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || | 1109 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || |
| 1026 ds->pending_buf == Z_NULL) { | 1110 ds->pending_buf == Z_NULL) { |
| 1027 deflateEnd (dest); | 1111 deflateEnd (dest); |
| 1028 return Z_MEM_ERROR; | 1112 return Z_MEM_ERROR; |
| 1029 } | 1113 } |
| 1030 /* following zmemcpy do not work for 16-bit MSDOS */ | 1114 /* following zmemcpy do not work for 16-bit MSDOS */ |
| 1031 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); | 1115 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); |
| 1032 zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos)); | 1116 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); |
| 1033 zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos)); | 1117 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); |
| 1034 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); | 1118 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); |
| 1035 | 1119 |
| 1036 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); | 1120 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); |
| 1037 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); | 1121 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); |
| 1038 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; | 1122 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; |
| 1039 | 1123 |
| 1040 ds->l_desc.dyn_tree = ds->dyn_ltree; | 1124 ds->l_desc.dyn_tree = ds->dyn_ltree; |
| 1041 ds->d_desc.dyn_tree = ds->dyn_dtree; | 1125 ds->d_desc.dyn_tree = ds->dyn_dtree; |
| 1042 ds->bl_desc.dyn_tree = ds->bl_tree; | 1126 ds->bl_desc.dyn_tree = ds->bl_tree; |
| 1043 | 1127 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1094 /* Set the default configuration parameters: | 1178 /* Set the default configuration parameters: |
| 1095 */ | 1179 */ |
| 1096 s->max_lazy_match = configuration_table[s->level].max_lazy; | 1180 s->max_lazy_match = configuration_table[s->level].max_lazy; |
| 1097 s->good_match = configuration_table[s->level].good_length; | 1181 s->good_match = configuration_table[s->level].good_length; |
| 1098 s->nice_match = configuration_table[s->level].nice_length; | 1182 s->nice_match = configuration_table[s->level].nice_length; |
| 1099 s->max_chain_length = configuration_table[s->level].max_chain; | 1183 s->max_chain_length = configuration_table[s->level].max_chain; |
| 1100 | 1184 |
| 1101 s->strstart = 0; | 1185 s->strstart = 0; |
| 1102 s->block_start = 0L; | 1186 s->block_start = 0L; |
| 1103 s->lookahead = 0; | 1187 s->lookahead = 0; |
| 1188 s->insert = 0; |
| 1104 s->match_length = s->prev_length = MIN_MATCH-1; | 1189 s->match_length = s->prev_length = MIN_MATCH-1; |
| 1105 s->match_available = 0; | 1190 s->match_available = 0; |
| 1106 s->ins_h = 0; | 1191 s->ins_h = 0; |
| 1107 #ifndef FASTEST | 1192 #ifndef FASTEST |
| 1108 #ifdef ASMV | 1193 #ifdef ASMV |
| 1109 match_init(); /* initialize the asm code */ | 1194 match_init(); /* initialize the asm code */ |
| 1110 #endif | 1195 #endif |
| 1111 #endif | 1196 #endif |
| 1112 } | 1197 } |
| 1113 | 1198 |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1499 } | 1584 } |
| 1500 | 1585 |
| 1501 local void fill_window_c(s) | 1586 local void fill_window_c(s) |
| 1502 deflate_state *s; | 1587 deflate_state *s; |
| 1503 { | 1588 { |
| 1504 register unsigned n, m; | 1589 register unsigned n, m; |
| 1505 register Posf *p; | 1590 register Posf *p; |
| 1506 unsigned more; /* Amount of free space at the end of the window. */ | 1591 unsigned more; /* Amount of free space at the end of the window. */ |
| 1507 uInt wsize = s->w_size; | 1592 uInt wsize = s->w_size; |
| 1508 | 1593 |
| 1594 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); |
| 1595 |
| 1509 do { | 1596 do { |
| 1510 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); | 1597 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
| 1511 | 1598 |
| 1512 /* Deal with !@#$% 64K limit: */ | 1599 /* Deal with !@#$% 64K limit: */ |
| 1513 if (sizeof(int) <= 2) { | 1600 if (sizeof(int) <= 2) { |
| 1514 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { | 1601 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { |
| 1515 more = wsize; | 1602 more = wsize; |
| 1516 | 1603 |
| 1517 } else if (more == (unsigned)(-1)) { | 1604 } else if (more == (unsigned)(-1)) { |
| 1518 /* Very unlikely, but possible on 16 bit machine if | 1605 /* Very unlikely, but possible on 16 bit machine if |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1566 } | 1653 } |
| 1567 | 1654 |
| 1568 if (s->class_bitmap) { | 1655 if (s->class_bitmap) { |
| 1569 zmemcpy(s->class_bitmap, s->class_bitmap + s->w_size/8, | 1656 zmemcpy(s->class_bitmap, s->class_bitmap + s->w_size/8, |
| 1570 s->w_size/8); | 1657 s->w_size/8); |
| 1571 zmemzero(s->class_bitmap + s->w_size/8, s->w_size/8); | 1658 zmemzero(s->class_bitmap + s->w_size/8, s->w_size/8); |
| 1572 } | 1659 } |
| 1573 | 1660 |
| 1574 more += wsize; | 1661 more += wsize; |
| 1575 } | 1662 } |
| 1576 if (s->strm->avail_in == 0) return; | 1663 if (s->strm->avail_in == 0) break; |
| 1577 | 1664 |
| 1578 /* If there was no sliding: | 1665 /* If there was no sliding: |
| 1579 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && | 1666 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && |
| 1580 * more == window_size - lookahead - strstart | 1667 * more == window_size - lookahead - strstart |
| 1581 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) | 1668 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) |
| 1582 * => more >= window_size - 2*WSIZE + 2 | 1669 * => more >= window_size - 2*WSIZE + 2 |
| 1583 * In the BIG_MEM or MMAP case (not yet supported), | 1670 * In the BIG_MEM or MMAP case (not yet supported), |
| 1584 * window_size == input_size + MIN_LOOKAHEAD && | 1671 * window_size == input_size + MIN_LOOKAHEAD && |
| 1585 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. | 1672 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. |
| 1586 * Otherwise, window_size == 2*WSIZE so more >= 2. | 1673 * Otherwise, window_size == 2*WSIZE so more >= 2. |
| 1587 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. | 1674 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. |
| 1588 */ | 1675 */ |
| 1589 Assert(more >= 2, "more < 2"); | 1676 Assert(more >= 2, "more < 2"); |
| 1590 | 1677 |
| 1591 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); | 1678 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); |
| 1592 if (s->class_bitmap != NULL) { | 1679 if (s->class_bitmap != NULL) { |
| 1593 class_set(s, s->strstart + s->lookahead, n, s->strm->clas); | 1680 class_set(s, s->strstart + s->lookahead, n, s->strm->clas); |
| 1594 } | 1681 } |
| 1595 s->lookahead += n; | 1682 s->lookahead += n; |
| 1596 | 1683 |
| 1597 /* Initialize the hash value now that we have some input: */ | 1684 /* Initialize the hash value now that we have some input: */ |
| 1598 if (s->lookahead >= MIN_MATCH) { | 1685 if (s->lookahead + s->insert >= MIN_MATCH) { |
| 1599 s->ins_h = s->window[s->strstart]; | 1686 uInt str = s->strstart - s->insert; |
| 1600 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); | 1687 s->ins_h = s->window[str]; |
| 1688 UPDATE_HASH(s, s->ins_h, s->window[str + 1]); |
| 1601 #if MIN_MATCH != 3 | 1689 #if MIN_MATCH != 3 |
| 1602 Call UPDATE_HASH() MIN_MATCH-3 more times | 1690 Call UPDATE_HASH() MIN_MATCH-3 more times |
| 1603 #endif | 1691 #endif |
| 1692 while (s->insert) { |
| 1693 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); |
| 1694 #ifndef FASTEST |
| 1695 s->prev[str & s->w_mask] = s->head[s->ins_h]; |
| 1696 #endif |
| 1697 s->head[s->ins_h] = (Pos)str; |
| 1698 str++; |
| 1699 s->insert--; |
| 1700 if (s->lookahead + s->insert < MIN_MATCH) |
| 1701 break; |
| 1702 } |
| 1604 } | 1703 } |
| 1605 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, | 1704 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, |
| 1606 * but this is not important since only literal bytes will be emitted. | 1705 * but this is not important since only literal bytes will be emitted. |
| 1607 */ | 1706 */ |
| 1608 | 1707 |
| 1609 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); | 1708 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); |
| 1610 | 1709 |
| 1611 /* If the WIN_INIT bytes after the end of the current data have never been | 1710 /* If the WIN_INIT bytes after the end of the current data have never been |
| 1612 * written, then zero those bytes in order to avoid memory check reports of | 1711 * written, then zero those bytes in order to avoid memory check reports of |
| 1613 * the use of uninitialized (or uninitialised as Julian writes) bytes by | 1712 * the use of uninitialized (or uninitialised as Julian writes) bytes by |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1634 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up | 1733 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up |
| 1635 * to end of window, whichever is less. | 1734 * to end of window, whichever is less. |
| 1636 */ | 1735 */ |
| 1637 init = (ulg)curr + WIN_INIT - s->high_water; | 1736 init = (ulg)curr + WIN_INIT - s->high_water; |
| 1638 if (init > s->window_size - s->high_water) | 1737 if (init > s->window_size - s->high_water) |
| 1639 init = s->window_size - s->high_water; | 1738 init = s->window_size - s->high_water; |
| 1640 zmemzero(s->window + s->high_water, (unsigned)init); | 1739 zmemzero(s->window + s->high_water, (unsigned)init); |
| 1641 s->high_water += init; | 1740 s->high_water += init; |
| 1642 } | 1741 } |
| 1643 } | 1742 } |
| 1743 |
| 1744 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, |
| 1745 "not enough room for search"); |
| 1644 } | 1746 } |
| 1645 | 1747 |
| 1646 /* =========================================================================== | 1748 /* =========================================================================== |
| 1647 * Flush the current block, with given end-of-file flag. | 1749 * Flush the current block, with given end-of-file flag. |
| 1648 * IN assertion: strstart is set to the end of the current match. | 1750 * IN assertion: strstart is set to the end of the current match. |
| 1649 */ | 1751 */ |
| 1650 #define FLUSH_BLOCK_ONLY(s, last) { \ | 1752 #define FLUSH_BLOCK_ONLY(s, last) { \ |
| 1651 _tr_flush_block(s, (s->block_start >= 0L ? \ | 1753 _tr_flush_block(s, (s->block_start >= 0L ? \ |
| 1652 (charf *)&s->window[(unsigned)s->block_start] : \ | 1754 (charf *)&s->window[(unsigned)s->block_start] : \ |
| 1653 (charf *)Z_NULL), \ | 1755 (charf *)Z_NULL), \ |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1714 s->strstart = (uInt)max_start; | 1816 s->strstart = (uInt)max_start; |
| 1715 FLUSH_BLOCK(s, 0); | 1817 FLUSH_BLOCK(s, 0); |
| 1716 } | 1818 } |
| 1717 /* Flush if we may have to slide, otherwise block_start may become | 1819 /* Flush if we may have to slide, otherwise block_start may become |
| 1718 * negative and the data will be gone: | 1820 * negative and the data will be gone: |
| 1719 */ | 1821 */ |
| 1720 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { | 1822 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { |
| 1721 FLUSH_BLOCK(s, 0); | 1823 FLUSH_BLOCK(s, 0); |
| 1722 } | 1824 } |
| 1723 } | 1825 } |
| 1724 FLUSH_BLOCK(s, flush == Z_FINISH); | 1826 s->insert = 0; |
| 1725 return flush == Z_FINISH ? finish_done : block_done; | 1827 if (flush == Z_FINISH) { |
| 1828 FLUSH_BLOCK(s, 1); |
| 1829 return finish_done; |
| 1830 } |
| 1831 if ((long)s->strstart > s->block_start) |
| 1832 FLUSH_BLOCK(s, 0); |
| 1833 return block_done; |
| 1726 } | 1834 } |
| 1727 | 1835 |
| 1728 /* =========================================================================== | 1836 /* =========================================================================== |
| 1729 * Compress as much as possible from the input stream, return the current | 1837 * Compress as much as possible from the input stream, return the current |
| 1730 * block state. | 1838 * block state. |
| 1731 * This function does not perform lazy evaluation of matches and inserts | 1839 * This function does not perform lazy evaluation of matches and inserts |
| 1732 * new strings in the dictionary only for unmatched strings or for short | 1840 * new strings in the dictionary only for unmatched strings or for short |
| 1733 * matches. It is used only for the fast compression options. | 1841 * matches. It is used only for the fast compression options. |
| 1734 */ | 1842 */ |
| 1735 local block_state deflate_fast(s, flush, clas) | 1843 local block_state deflate_fast(s, flush, clas) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1817 } | 1925 } |
| 1818 } else { | 1926 } else { |
| 1819 /* No match, output a literal byte */ | 1927 /* No match, output a literal byte */ |
| 1820 Tracevv((stderr,"%c", s->window[s->strstart])); | 1928 Tracevv((stderr,"%c", s->window[s->strstart])); |
| 1821 _tr_tally_lit (s, s->window[s->strstart], bflush); | 1929 _tr_tally_lit (s, s->window[s->strstart], bflush); |
| 1822 s->lookahead--; | 1930 s->lookahead--; |
| 1823 s->strstart++; | 1931 s->strstart++; |
| 1824 } | 1932 } |
| 1825 if (bflush) FLUSH_BLOCK(s, 0); | 1933 if (bflush) FLUSH_BLOCK(s, 0); |
| 1826 } | 1934 } |
| 1827 FLUSH_BLOCK(s, flush == Z_FINISH); | 1935 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; |
| 1828 return flush == Z_FINISH ? finish_done : block_done; | 1936 if (flush == Z_FINISH) { |
| 1937 FLUSH_BLOCK(s, 1); |
| 1938 return finish_done; |
| 1939 } |
| 1940 if (s->last_lit) |
| 1941 FLUSH_BLOCK(s, 0); |
| 1942 return block_done; |
| 1829 } | 1943 } |
| 1830 | 1944 |
| 1831 #ifndef FASTEST | 1945 #ifndef FASTEST |
| 1832 /* =========================================================================== | 1946 /* =========================================================================== |
| 1833 * Same as above, but achieves better compression. We use a lazy | 1947 * Same as above, but achieves better compression. We use a lazy |
| 1834 * evaluation for matches: a match is finally adopted only if there is | 1948 * evaluation for matches: a match is finally adopted only if there is |
| 1835 * no better match at the next window position. | 1949 * no better match at the next window position. |
| 1836 */ | 1950 */ |
| 1837 local block_state deflate_slow(s, flush, clas) | 1951 local block_state deflate_slow(s, flush, clas) |
| 1838 deflate_state *s; | 1952 deflate_state *s; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1973 s->strstart++; | 2087 s->strstart++; |
| 1974 s->lookahead--; | 2088 s->lookahead--; |
| 1975 } | 2089 } |
| 1976 } | 2090 } |
| 1977 Assert (flush != Z_NO_FLUSH, "no flush?"); | 2091 Assert (flush != Z_NO_FLUSH, "no flush?"); |
| 1978 if (s->match_available) { | 2092 if (s->match_available) { |
| 1979 Tracevv((stderr,"%c", s->window[s->strstart-1])); | 2093 Tracevv((stderr,"%c", s->window[s->strstart-1])); |
| 1980 _tr_tally_lit(s, s->window[s->strstart-1], bflush); | 2094 _tr_tally_lit(s, s->window[s->strstart-1], bflush); |
| 1981 s->match_available = 0; | 2095 s->match_available = 0; |
| 1982 } | 2096 } |
| 1983 FLUSH_BLOCK(s, flush == Z_FINISH); | 2097 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; |
| 1984 return flush == Z_FINISH ? finish_done : block_done; | 2098 if (flush == Z_FINISH) { |
| 2099 FLUSH_BLOCK(s, 1); |
| 2100 return finish_done; |
| 2101 } |
| 2102 if (s->last_lit) |
| 2103 FLUSH_BLOCK(s, 0); |
| 2104 return block_done; |
| 1985 } | 2105 } |
| 1986 #endif /* FASTEST */ | 2106 #endif /* FASTEST */ |
| 1987 | 2107 |
| 1988 /* =========================================================================== | 2108 /* =========================================================================== |
| 1989 * For Z_RLE, simply look for runs of bytes, generate matches only of distance | 2109 * For Z_RLE, simply look for runs of bytes, generate matches only of distance |
| 1990 * one. Do not maintain a hash table. (It will be regenerated if this run of | 2110 * one. Do not maintain a hash table. (It will be regenerated if this run of |
| 1991 * deflate switches away from Z_RLE.) | 2111 * deflate switches away from Z_RLE.) |
| 1992 */ | 2112 */ |
| 1993 local block_state deflate_rle(s, flush) | 2113 local block_state deflate_rle(s, flush) |
| 1994 deflate_state *s; | 2114 deflate_state *s; |
| 1995 int flush; | 2115 int flush; |
| 1996 { | 2116 { |
| 1997 int bflush; /* set if current block must be flushed */ | 2117 int bflush; /* set if current block must be flushed */ |
| 1998 uInt prev; /* byte at distance one to match */ | 2118 uInt prev; /* byte at distance one to match */ |
| 1999 Bytef *scan, *strend; /* scan goes up to strend for length of run */ | 2119 Bytef *scan, *strend; /* scan goes up to strend for length of run */ |
| 2000 | 2120 |
| 2001 for (;;) { | 2121 for (;;) { |
| 2002 /* Make sure that we always have enough lookahead, except | 2122 /* Make sure that we always have enough lookahead, except |
| 2003 * at the end of the input file. We need MAX_MATCH bytes | 2123 * at the end of the input file. We need MAX_MATCH bytes |
| 2004 * for the longest encodable run. | 2124 * for the longest run, plus one for the unrolled loop. |
| 2005 */ | 2125 */ |
| 2006 if (s->lookahead < MAX_MATCH) { | 2126 if (s->lookahead <= MAX_MATCH) { |
| 2007 fill_window(s); | 2127 fill_window(s); |
| 2008 if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { | 2128 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { |
| 2009 return need_more; | 2129 return need_more; |
| 2010 } | 2130 } |
| 2011 if (s->lookahead == 0) break; /* flush the current block */ | 2131 if (s->lookahead == 0) break; /* flush the current block */ |
| 2012 } | 2132 } |
| 2013 | 2133 |
| 2014 /* See how many times the previous byte repeats */ | 2134 /* See how many times the previous byte repeats */ |
| 2015 s->match_length = 0; | 2135 s->match_length = 0; |
| 2016 if (s->lookahead >= MIN_MATCH && s->strstart > 0) { | 2136 if (s->lookahead >= MIN_MATCH && s->strstart > 0) { |
| 2017 scan = s->window + s->strstart - 1; | 2137 scan = s->window + s->strstart - 1; |
| 2018 prev = *scan; | 2138 prev = *scan; |
| 2019 if (prev == *++scan && prev == *++scan && prev == *++scan) { | 2139 if (prev == *++scan && prev == *++scan && prev == *++scan) { |
| 2020 strend = s->window + s->strstart + MAX_MATCH; | 2140 strend = s->window + s->strstart + MAX_MATCH; |
| 2021 do { | 2141 do { |
| 2022 } while (prev == *++scan && prev == *++scan && | 2142 } while (prev == *++scan && prev == *++scan && |
| 2023 prev == *++scan && prev == *++scan && | 2143 prev == *++scan && prev == *++scan && |
| 2024 prev == *++scan && prev == *++scan && | 2144 prev == *++scan && prev == *++scan && |
| 2025 prev == *++scan && prev == *++scan && | 2145 prev == *++scan && prev == *++scan && |
| 2026 scan < strend); | 2146 scan < strend); |
| 2027 s->match_length = MAX_MATCH - (int)(strend - scan); | 2147 s->match_length = MAX_MATCH - (int)(strend - scan); |
| 2028 if (s->match_length > s->lookahead) | 2148 if (s->match_length > s->lookahead) |
| 2029 s->match_length = s->lookahead; | 2149 s->match_length = s->lookahead; |
| 2030 } | 2150 } |
| 2151 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); |
| 2031 } | 2152 } |
| 2032 | 2153 |
| 2033 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ | 2154 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ |
| 2034 if (s->match_length >= MIN_MATCH) { | 2155 if (s->match_length >= MIN_MATCH) { |
| 2035 check_match(s, s->strstart, s->strstart - 1, s->match_length); | 2156 check_match(s, s->strstart, s->strstart - 1, s->match_length); |
| 2036 | 2157 |
| 2037 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); | 2158 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); |
| 2038 | 2159 |
| 2039 s->lookahead -= s->match_length; | 2160 s->lookahead -= s->match_length; |
| 2040 s->strstart += s->match_length; | 2161 s->strstart += s->match_length; |
| 2041 s->match_length = 0; | 2162 s->match_length = 0; |
| 2042 } else { | 2163 } else { |
| 2043 /* No match, output a literal byte */ | 2164 /* No match, output a literal byte */ |
| 2044 Tracevv((stderr,"%c", s->window[s->strstart])); | 2165 Tracevv((stderr,"%c", s->window[s->strstart])); |
| 2045 _tr_tally_lit (s, s->window[s->strstart], bflush); | 2166 _tr_tally_lit (s, s->window[s->strstart], bflush); |
| 2046 s->lookahead--; | 2167 s->lookahead--; |
| 2047 s->strstart++; | 2168 s->strstart++; |
| 2048 } | 2169 } |
| 2049 if (bflush) FLUSH_BLOCK(s, 0); | 2170 if (bflush) FLUSH_BLOCK(s, 0); |
| 2050 } | 2171 } |
| 2051 FLUSH_BLOCK(s, flush == Z_FINISH); | 2172 s->insert = 0; |
| 2052 return flush == Z_FINISH ? finish_done : block_done; | 2173 if (flush == Z_FINISH) { |
| 2174 FLUSH_BLOCK(s, 1); |
| 2175 return finish_done; |
| 2176 } |
| 2177 if (s->last_lit) |
| 2178 FLUSH_BLOCK(s, 0); |
| 2179 return block_done; |
| 2053 } | 2180 } |
| 2054 | 2181 |
| 2055 /* =========================================================================== | 2182 /* =========================================================================== |
| 2056 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. | 2183 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. |
| 2057 * (It will be regenerated if this run of deflate switches away from Huffman.) | 2184 * (It will be regenerated if this run of deflate switches away from Huffman.) |
| 2058 */ | 2185 */ |
| 2059 local block_state deflate_huff(s, flush) | 2186 local block_state deflate_huff(s, flush) |
| 2060 deflate_state *s; | 2187 deflate_state *s; |
| 2061 int flush; | 2188 int flush; |
| 2062 { | 2189 { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2074 } | 2201 } |
| 2075 | 2202 |
| 2076 /* Output a literal byte */ | 2203 /* Output a literal byte */ |
| 2077 s->match_length = 0; | 2204 s->match_length = 0; |
| 2078 Tracevv((stderr,"%c", s->window[s->strstart])); | 2205 Tracevv((stderr,"%c", s->window[s->strstart])); |
| 2079 _tr_tally_lit (s, s->window[s->strstart], bflush); | 2206 _tr_tally_lit (s, s->window[s->strstart], bflush); |
| 2080 s->lookahead--; | 2207 s->lookahead--; |
| 2081 s->strstart++; | 2208 s->strstart++; |
| 2082 if (bflush) FLUSH_BLOCK(s, 0); | 2209 if (bflush) FLUSH_BLOCK(s, 0); |
| 2083 } | 2210 } |
| 2084 FLUSH_BLOCK(s, flush == Z_FINISH); | 2211 s->insert = 0; |
| 2085 return flush == Z_FINISH ? finish_done : block_done; | 2212 if (flush == Z_FINISH) { |
| 2213 FLUSH_BLOCK(s, 1); |
| 2214 return finish_done; |
| 2215 } |
| 2216 if (s->last_lit) |
| 2217 FLUSH_BLOCK(s, 0); |
| 2218 return block_done; |
| 2086 } | 2219 } |
| 2087 | 2220 |
| 2088 /* Safe to inline this as GCC/clang will use inline asm and Visual Studio will | 2221 /* Safe to inline this as GCC/clang will use inline asm and Visual Studio will |
| 2089 * use intrinsic without extra params | 2222 * use intrinsic without extra params |
| 2090 */ | 2223 */ |
| 2091 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str) | 2224 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str) |
| 2092 { | 2225 { |
| 2093 Pos ret; | 2226 Pos ret; |
| 2094 unsigned *ip, val, h = 0; | 2227 unsigned *ip, val, h = 0; |
| 2095 | 2228 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 2111 #else | 2244 #else |
| 2112 /* This should never happen */ | 2245 /* This should never happen */ |
| 2113 assert(0); | 2246 assert(0); |
| 2114 #endif | 2247 #endif |
| 2115 | 2248 |
| 2116 ret = s->head[h & s->hash_mask]; | 2249 ret = s->head[h & s->hash_mask]; |
| 2117 s->head[h & s->hash_mask] = str; | 2250 s->head[h & s->hash_mask] = str; |
| 2118 s->prev[str & s->w_mask] = ret; | 2251 s->prev[str & s->w_mask] = ret; |
| 2119 return ret; | 2252 return ret; |
| 2120 } | 2253 } |
| OLD | NEW |