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...) 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...) 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...) 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...) 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 || | |
365 (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) | |
366 return Z_STREAM_ERROR; | 377 return Z_STREAM_ERROR; |
367 | 378 |
368 s = strm->state; | 379 s = strm->state; |
369 if (s->wrap) | 380 wrap = s->wrap; |
| 381 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) |
| 382 return Z_STREAM_ERROR; |
| 383 |
| 384 /* when using zlib wrappers, compute Adler-32 for provided dictionary */ |
| 385 if (wrap == 1) |
370 strm->adler = adler32(strm->adler, dictionary, dictLength); | 386 strm->adler = adler32(strm->adler, dictionary, dictLength); |
| 387 s->wrap = 0; /* avoid computing Adler-32 in read_buf */ |
371 | 388 |
372 if (length < MIN_MATCH) return Z_OK; | 389 /* if dictionary would fill window, just replace the history */ |
373 if (length > s->w_size) { | 390 if (dictLength >= s->w_size) { |
374 length = s->w_size; | 391 if (wrap == 0) { /* already empty otherwise */ |
375 dictionary += dictLength - length; /* use the tail of the dictionary */ | 392 CLEAR_HASH(s); |
| 393 s->strstart = 0; |
| 394 s->block_start = 0L; |
| 395 s->insert = 0; |
| 396 } |
| 397 dictionary += dictLength - s->w_size; /* use the tail */ |
| 398 dictLength = s->w_size; |
376 } | 399 } |
377 zmemcpy(s->window, dictionary, length); | |
378 s->strstart = length; | |
379 s->block_start = (long)length; | |
380 | 400 |
381 /* Insert all strings in the hash table (except for the last two bytes). | 401 /* insert dictionary into window and hash */ |
382 * s->lookahead stays null, so s->ins_h will be recomputed at the next | 402 avail = strm->avail_in; |
383 * call of fill_window. | 403 next = strm->next_in; |
384 */ | 404 strm->avail_in = dictLength; |
385 s->ins_h = s->window[0]; | 405 strm->next_in = (z_const Bytef *)dictionary; |
386 UPDATE_HASH(s, s->ins_h, s->window[1]); | 406 fill_window(s); |
387 for (n = 0; n <= length - MIN_MATCH; n++) { | 407 while (s->lookahead >= MIN_MATCH) { |
388 insert_string(s, n); | 408 str = s->strstart; |
| 409 n = s->lookahead - (MIN_MATCH-1); |
| 410 do { |
| 411 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); |
| 412 #ifndef FASTEST |
| 413 s->prev[str & s->w_mask] = s->head[s->ins_h]; |
| 414 #endif |
| 415 s->head[s->ins_h] = (Pos)str; |
| 416 str++; |
| 417 } while (--n); |
| 418 s->strstart = str; |
| 419 s->lookahead = MIN_MATCH-1; |
| 420 fill_window(s); |
389 } | 421 } |
390 if (hash_head) hash_head = 0; /* to make compiler happy */ | 422 s->strstart += s->lookahead; |
| 423 s->block_start = (long)s->strstart; |
| 424 s->insert = s->lookahead; |
| 425 s->lookahead = 0; |
| 426 s->match_length = s->prev_length = MIN_MATCH-1; |
| 427 s->match_available = 0; |
| 428 strm->next_in = next; |
| 429 strm->avail_in = avail; |
| 430 s->wrap = wrap; |
391 return Z_OK; | 431 return Z_OK; |
392 } | 432 } |
393 | 433 |
394 /* ========================================================================= */ | 434 /* ========================================================================= */ |
395 int ZEXPORT deflateReset (strm) | 435 int ZEXPORT deflateResetKeep (strm) |
396 z_streamp strm; | 436 z_streamp strm; |
397 { | 437 { |
398 deflate_state *s; | 438 deflate_state *s; |
399 | 439 |
400 if (strm == Z_NULL || strm->state == Z_NULL || | 440 if (strm == Z_NULL || strm->state == Z_NULL || |
401 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { | 441 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { |
402 return Z_STREAM_ERROR; | 442 return Z_STREAM_ERROR; |
403 } | 443 } |
404 | 444 |
405 strm->total_in = strm->total_out = 0; | 445 strm->total_in = strm->total_out = 0; |
(...skipping 11 matching lines...) Loading... |
417 } | 457 } |
418 s->status = s->wrap ? INIT_STATE : BUSY_STATE; | 458 s->status = s->wrap ? INIT_STATE : BUSY_STATE; |
419 strm->adler = | 459 strm->adler = |
420 #ifdef GZIP | 460 #ifdef GZIP |
421 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : | 461 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : |
422 #endif | 462 #endif |
423 adler32(0L, Z_NULL, 0); | 463 adler32(0L, Z_NULL, 0); |
424 s->last_flush = Z_NO_FLUSH; | 464 s->last_flush = Z_NO_FLUSH; |
425 | 465 |
426 _tr_init(s); | 466 _tr_init(s); |
427 lm_init(s); | |
428 | 467 |
429 return Z_OK; | 468 return Z_OK; |
430 } | 469 } |
431 | 470 |
432 /* ========================================================================= */ | 471 /* ========================================================================= */ |
| 472 int ZEXPORT deflateReset (strm) |
| 473 z_streamp strm; |
| 474 { |
| 475 int ret; |
| 476 |
| 477 ret = deflateResetKeep(strm); |
| 478 if (ret == Z_OK) |
| 479 lm_init(strm->state); |
| 480 return ret; |
| 481 } |
| 482 |
| 483 /* ========================================================================= */ |
433 int ZEXPORT deflateSetHeader (strm, head) | 484 int ZEXPORT deflateSetHeader (strm, head) |
434 z_streamp strm; | 485 z_streamp strm; |
435 gz_headerp head; | 486 gz_headerp head; |
436 { | 487 { |
437 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 488 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
438 if (strm->state->wrap != 2) return Z_STREAM_ERROR; | 489 if (strm->state->wrap != 2) return Z_STREAM_ERROR; |
439 strm->state->gzhead = head; | 490 strm->state->gzhead = head; |
440 return Z_OK; | 491 return Z_OK; |
441 } | 492 } |
442 | 493 |
443 /* ========================================================================= */ | 494 /* ========================================================================= */ |
| 495 int ZEXPORT deflatePending (strm, pending, bits) |
| 496 unsigned *pending; |
| 497 int *bits; |
| 498 z_streamp strm; |
| 499 { |
| 500 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 501 if (pending != Z_NULL) |
| 502 *pending = strm->state->pending; |
| 503 if (bits != Z_NULL) |
| 504 *bits = strm->state->bi_valid; |
| 505 return Z_OK; |
| 506 } |
| 507 |
| 508 /* ========================================================================= */ |
444 int ZEXPORT deflatePrime (strm, bits, value) | 509 int ZEXPORT deflatePrime (strm, bits, value) |
445 z_streamp strm; | 510 z_streamp strm; |
446 int bits; | 511 int bits; |
447 int value; | 512 int value; |
448 { | 513 { |
| 514 deflate_state *s; |
| 515 int put; |
| 516 |
449 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; | 517 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
450 strm->state->bi_valid = bits; | 518 s = strm->state; |
451 strm->state->bi_buf = (ush)(value & ((1 << bits) - 1)); | 519 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) |
| 520 return Z_BUF_ERROR; |
| 521 do { |
| 522 put = Buf_size - s->bi_valid; |
| 523 if (put > bits) |
| 524 put = bits; |
| 525 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); |
| 526 s->bi_valid += put; |
| 527 _tr_flush_bits(s); |
| 528 value >>= put; |
| 529 bits -= put; |
| 530 } while (bits); |
452 return Z_OK; | 531 return Z_OK; |
453 } | 532 } |
454 | 533 |
455 /* ========================================================================= */ | 534 /* ========================================================================= */ |
456 int ZEXPORT deflateParams(strm, level, strategy) | 535 int ZEXPORT deflateParams(strm, level, strategy) |
457 z_streamp strm; | 536 z_streamp strm; |
458 int level; | 537 int level; |
459 int strategy; | 538 int strategy; |
460 { | 539 { |
461 deflate_state *s; | 540 deflate_state *s; |
(...skipping 10 matching lines...) Loading... |
472 #endif | 551 #endif |
473 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { | 552 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { |
474 return Z_STREAM_ERROR; | 553 return Z_STREAM_ERROR; |
475 } | 554 } |
476 func = configuration_table[s->level].func; | 555 func = configuration_table[s->level].func; |
477 | 556 |
478 if ((strategy != s->strategy || func != configuration_table[level].func) && | 557 if ((strategy != s->strategy || func != configuration_table[level].func) && |
479 strm->total_in != 0) { | 558 strm->total_in != 0) { |
480 /* Flush the last buffer: */ | 559 /* Flush the last buffer: */ |
481 err = deflate(strm, Z_BLOCK); | 560 err = deflate(strm, Z_BLOCK); |
| 561 if (err == Z_BUF_ERROR && s->pending == 0) |
| 562 err = Z_OK; |
482 } | 563 } |
483 if (s->level != level) { | 564 if (s->level != level) { |
484 s->level = level; | 565 s->level = level; |
485 s->max_lazy_match = configuration_table[level].max_lazy; | 566 s->max_lazy_match = configuration_table[level].max_lazy; |
486 s->good_match = configuration_table[level].good_length; | 567 s->good_match = configuration_table[level].good_length; |
487 s->nice_match = configuration_table[level].nice_length; | 568 s->nice_match = configuration_table[level].nice_length; |
488 s->max_chain_length = configuration_table[level].max_chain; | 569 s->max_chain_length = configuration_table[level].max_chain; |
489 } | 570 } |
490 s->strategy = strategy; | 571 s->strategy = strategy; |
491 return err; | 572 return err; |
(...skipping 107 matching lines...) Loading... |
599 | 680 |
600 /* ========================================================================= | 681 /* ========================================================================= |
601 * Flush as much pending output as possible. All deflate() output goes | 682 * Flush as much pending output as possible. All deflate() output goes |
602 * through this function so some applications may wish to modify it | 683 * 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. | 684 * to avoid allocating a large strm->next_out buffer and copying into it. |
604 * (See also read_buf()). | 685 * (See also read_buf()). |
605 */ | 686 */ |
606 local void flush_pending(strm) | 687 local void flush_pending(strm) |
607 z_streamp strm; | 688 z_streamp strm; |
608 { | 689 { |
609 unsigned len = strm->state->pending; | 690 unsigned len; |
| 691 deflate_state *s = strm->state; |
610 | 692 |
| 693 _tr_flush_bits(s); |
| 694 len = s->pending; |
611 if (len > strm->avail_out) len = strm->avail_out; | 695 if (len > strm->avail_out) len = strm->avail_out; |
612 if (len == 0) return; | 696 if (len == 0) return; |
613 | 697 |
614 zmemcpy(strm->next_out, strm->state->pending_out, len); | 698 zmemcpy(strm->next_out, s->pending_out, len); |
615 strm->next_out += len; | 699 strm->next_out += len; |
616 strm->state->pending_out += len; | 700 s->pending_out += len; |
617 strm->total_out += len; | 701 strm->total_out += len; |
618 strm->avail_out -= len; | 702 strm->avail_out -= len; |
619 strm->state->pending -= len; | 703 s->pending -= len; |
620 if (strm->state->pending == 0) { | 704 if (s->pending == 0) { |
621 strm->state->pending_out = strm->state->pending_buf; | 705 s->pending_out = s->pending_buf; |
622 } | 706 } |
623 } | 707 } |
624 | 708 |
625 /* ========================================================================= */ | 709 /* ========================================================================= */ |
626 int ZEXPORT deflate (strm, flush) | 710 int ZEXPORT deflate (strm, flush) |
627 z_streamp strm; | 711 z_streamp strm; |
628 int flush; | 712 int flush; |
629 { | 713 { |
630 int old_flush; /* value of flush param for previous deflate call */ | 714 int old_flush; /* value of flush param for previous deflate call */ |
631 deflate_state *s; | 715 deflate_state *s; |
(...skipping 206 matching lines...) Loading... |
838 * return OK instead of BUF_ERROR at next call of deflate: | 922 * return OK instead of BUF_ERROR at next call of deflate: |
839 */ | 923 */ |
840 s->last_flush = -1; | 924 s->last_flush = -1; |
841 return Z_OK; | 925 return Z_OK; |
842 } | 926 } |
843 | 927 |
844 /* Make sure there is something to do and avoid duplicate consecutive | 928 /* Make sure there is something to do and avoid duplicate consecutive |
845 * flushes. For repeated and useless calls with Z_FINISH, we keep | 929 * flushes. For repeated and useless calls with Z_FINISH, we keep |
846 * returning Z_STREAM_END instead of Z_BUF_ERROR. | 930 * returning Z_STREAM_END instead of Z_BUF_ERROR. |
847 */ | 931 */ |
848 } else if (strm->avail_in == 0 && flush <= old_flush && | 932 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && |
849 flush != Z_FINISH) { | 933 flush != Z_FINISH) { |
850 ERR_RETURN(strm, Z_BUF_ERROR); | 934 ERR_RETURN(strm, Z_BUF_ERROR); |
851 } | 935 } |
852 | 936 |
853 /* User must not provide more input after the first FINISH: */ | 937 /* User must not provide more input after the first FINISH: */ |
854 if (s->status == FINISH_STATE && strm->avail_in != 0) { | 938 if (s->status == FINISH_STATE && strm->avail_in != 0) { |
855 ERR_RETURN(strm, Z_BUF_ERROR); | 939 ERR_RETURN(strm, Z_BUF_ERROR); |
856 } | 940 } |
857 | 941 |
858 /* Start a new block or continue the current one. | 942 /* Start a new block or continue the current one. |
(...skipping 45 matching lines...) Loading... |
904 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ | 988 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ |
905 _tr_stored_block(s, (char*)0, 0L, 0); | 989 _tr_stored_block(s, (char*)0, 0L, 0); |
906 /* For a full flush, this empty block will be recognized | 990 /* For a full flush, this empty block will be recognized |
907 * as a special marker by inflate_sync(). | 991 * as a special marker by inflate_sync(). |
908 */ | 992 */ |
909 if (flush == Z_FULL_FLUSH) { | 993 if (flush == Z_FULL_FLUSH) { |
910 CLEAR_HASH(s); /* forget history */ | 994 CLEAR_HASH(s); /* forget history */ |
911 if (s->lookahead == 0) { | 995 if (s->lookahead == 0) { |
912 s->strstart = 0; | 996 s->strstart = 0; |
913 s->block_start = 0L; | 997 s->block_start = 0L; |
| 998 s->insert = 0; |
914 } | 999 } |
915 } | 1000 } |
916 } | 1001 } |
917 flush_pending(strm); | 1002 flush_pending(strm); |
918 if (strm->avail_out == 0) { | 1003 if (strm->avail_out == 0) { |
919 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ | 1004 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ |
920 return Z_OK; | 1005 return Z_OK; |
921 } | 1006 } |
922 } | 1007 } |
923 } | 1008 } |
(...skipping 77 matching lines...) Loading... |
1001 deflate_state *ss; | 1086 deflate_state *ss; |
1002 ushf *overlay; | 1087 ushf *overlay; |
1003 | 1088 |
1004 | 1089 |
1005 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { | 1090 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { |
1006 return Z_STREAM_ERROR; | 1091 return Z_STREAM_ERROR; |
1007 } | 1092 } |
1008 | 1093 |
1009 ss = source->state; | 1094 ss = source->state; |
1010 | 1095 |
1011 zmemcpy(dest, source, sizeof(z_stream)); | 1096 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); |
1012 | 1097 |
1013 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); | 1098 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); |
1014 if (ds == Z_NULL) return Z_MEM_ERROR; | 1099 if (ds == Z_NULL) return Z_MEM_ERROR; |
1015 dest->state = (struct internal_state FAR *) ds; | 1100 dest->state = (struct internal_state FAR *) ds; |
1016 zmemcpy(ds, ss, sizeof(deflate_state)); | 1101 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); |
1017 ds->strm = dest; | 1102 ds->strm = dest; |
1018 | 1103 |
1019 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); | 1104 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); |
1020 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); | 1105 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); |
1021 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); | 1106 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); |
1022 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); | 1107 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); |
1023 ds->pending_buf = (uchf *) overlay; | 1108 ds->pending_buf = (uchf *) overlay; |
1024 | 1109 |
1025 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || | 1110 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || |
1026 ds->pending_buf == Z_NULL) { | 1111 ds->pending_buf == Z_NULL) { |
1027 deflateEnd (dest); | 1112 deflateEnd (dest); |
1028 return Z_MEM_ERROR; | 1113 return Z_MEM_ERROR; |
1029 } | 1114 } |
1030 /* following zmemcpy do not work for 16-bit MSDOS */ | 1115 /* following zmemcpy do not work for 16-bit MSDOS */ |
1031 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); | 1116 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); |
1032 zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos)); | 1117 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); |
1033 zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos)); | 1118 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); | 1119 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); |
1035 | 1120 |
1036 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); | 1121 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); |
1037 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); | 1122 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); |
1038 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; | 1123 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; |
1039 | 1124 |
1040 ds->l_desc.dyn_tree = ds->dyn_ltree; | 1125 ds->l_desc.dyn_tree = ds->dyn_ltree; |
1041 ds->d_desc.dyn_tree = ds->dyn_dtree; | 1126 ds->d_desc.dyn_tree = ds->dyn_dtree; |
1042 ds->bl_desc.dyn_tree = ds->bl_tree; | 1127 ds->bl_desc.dyn_tree = ds->bl_tree; |
1043 | 1128 |
(...skipping 13 matching lines...) Loading... |
1057 Bytef *buf; | 1142 Bytef *buf; |
1058 unsigned size; | 1143 unsigned size; |
1059 { | 1144 { |
1060 unsigned len = strm->avail_in; | 1145 unsigned len = strm->avail_in; |
1061 | 1146 |
1062 if (len > size) len = size; | 1147 if (len > size) len = size; |
1063 if (len == 0) return 0; | 1148 if (len == 0) return 0; |
1064 | 1149 |
1065 strm->avail_in -= len; | 1150 strm->avail_in -= len; |
1066 | 1151 |
| 1152 zmemcpy(buf, strm->next_in, len); |
| 1153 if (strm->state->wrap == 1) { |
| 1154 strm->adler = adler32(strm->adler, buf, len); |
| 1155 } |
1067 #ifdef GZIP | 1156 #ifdef GZIP |
1068 if (strm->state->wrap == 2) { | 1157 if (strm->state->wrap == 2) { |
1069 copy_with_crc(strm, buf, len); | 1158 copy_with_crc(strm, buf, len); |
1070 } | 1159 } |
1071 else | |
1072 #endif | 1160 #endif |
1073 { | |
1074 zmemcpy(buf, strm->next_in, len); | |
1075 if (strm->state->wrap == 1) | |
1076 strm->adler = adler32(strm->adler, buf, len); | |
1077 } | |
1078 strm->next_in += len; | 1161 strm->next_in += len; |
1079 strm->total_in += len; | 1162 strm->total_in += len; |
1080 | 1163 |
1081 return (int)len; | 1164 return (int)len; |
1082 } | 1165 } |
1083 | 1166 |
1084 /* =========================================================================== | 1167 /* =========================================================================== |
1085 * Initialize the "longest match" routines for a new zlib stream | 1168 * Initialize the "longest match" routines for a new zlib stream |
1086 */ | 1169 */ |
1087 local void lm_init (s) | 1170 local void lm_init (s) |
1088 deflate_state *s; | 1171 deflate_state *s; |
1089 { | 1172 { |
1090 s->window_size = (ulg)2L*s->w_size; | 1173 s->window_size = (ulg)2L*s->w_size; |
1091 | 1174 |
1092 CLEAR_HASH(s); | 1175 CLEAR_HASH(s); |
1093 | 1176 |
1094 /* Set the default configuration parameters: | 1177 /* Set the default configuration parameters: |
1095 */ | 1178 */ |
1096 s->max_lazy_match = configuration_table[s->level].max_lazy; | 1179 s->max_lazy_match = configuration_table[s->level].max_lazy; |
1097 s->good_match = configuration_table[s->level].good_length; | 1180 s->good_match = configuration_table[s->level].good_length; |
1098 s->nice_match = configuration_table[s->level].nice_length; | 1181 s->nice_match = configuration_table[s->level].nice_length; |
1099 s->max_chain_length = configuration_table[s->level].max_chain; | 1182 s->max_chain_length = configuration_table[s->level].max_chain; |
1100 | 1183 |
1101 s->strstart = 0; | 1184 s->strstart = 0; |
1102 s->block_start = 0L; | 1185 s->block_start = 0L; |
1103 s->lookahead = 0; | 1186 s->lookahead = 0; |
| 1187 s->insert = 0; |
1104 s->match_length = s->prev_length = MIN_MATCH-1; | 1188 s->match_length = s->prev_length = MIN_MATCH-1; |
1105 s->match_available = 0; | 1189 s->match_available = 0; |
1106 s->ins_h = 0; | 1190 s->ins_h = 0; |
1107 #ifndef FASTEST | 1191 #ifndef FASTEST |
1108 #ifdef ASMV | 1192 #ifdef ASMV |
1109 match_init(); /* initialize the asm code */ | 1193 match_init(); /* initialize the asm code */ |
1110 #endif | 1194 #endif |
1111 #endif | 1195 #endif |
1112 } | 1196 } |
1113 | 1197 |
(...skipping 385 matching lines...) Loading... |
1499 } | 1583 } |
1500 | 1584 |
1501 local void fill_window_c(s) | 1585 local void fill_window_c(s) |
1502 deflate_state *s; | 1586 deflate_state *s; |
1503 { | 1587 { |
1504 register unsigned n, m; | 1588 register unsigned n, m; |
1505 register Posf *p; | 1589 register Posf *p; |
1506 unsigned more; /* Amount of free space at the end of the window. */ | 1590 unsigned more; /* Amount of free space at the end of the window. */ |
1507 uInt wsize = s->w_size; | 1591 uInt wsize = s->w_size; |
1508 | 1592 |
| 1593 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); |
| 1594 |
1509 do { | 1595 do { |
1510 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); | 1596 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
1511 | 1597 |
1512 /* Deal with !@#$% 64K limit: */ | 1598 /* Deal with !@#$% 64K limit: */ |
1513 if (sizeof(int) <= 2) { | 1599 if (sizeof(int) <= 2) { |
1514 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { | 1600 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { |
1515 more = wsize; | 1601 more = wsize; |
1516 | 1602 |
1517 } else if (more == (unsigned)(-1)) { | 1603 } else if (more == (unsigned)(-1)) { |
1518 /* Very unlikely, but possible on 16 bit machine if | 1604 /* Very unlikely, but possible on 16 bit machine if |
(...skipping 47 matching lines...) Loading... |
1566 } | 1652 } |
1567 | 1653 |
1568 if (s->class_bitmap) { | 1654 if (s->class_bitmap) { |
1569 zmemcpy(s->class_bitmap, s->class_bitmap + s->w_size/8, | 1655 zmemcpy(s->class_bitmap, s->class_bitmap + s->w_size/8, |
1570 s->w_size/8); | 1656 s->w_size/8); |
1571 zmemzero(s->class_bitmap + s->w_size/8, s->w_size/8); | 1657 zmemzero(s->class_bitmap + s->w_size/8, s->w_size/8); |
1572 } | 1658 } |
1573 | 1659 |
1574 more += wsize; | 1660 more += wsize; |
1575 } | 1661 } |
1576 if (s->strm->avail_in == 0) return; | 1662 if (s->strm->avail_in == 0) break; |
1577 | 1663 |
1578 /* If there was no sliding: | 1664 /* If there was no sliding: |
1579 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && | 1665 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && |
1580 * more == window_size - lookahead - strstart | 1666 * more == window_size - lookahead - strstart |
1581 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) | 1667 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) |
1582 * => more >= window_size - 2*WSIZE + 2 | 1668 * => more >= window_size - 2*WSIZE + 2 |
1583 * In the BIG_MEM or MMAP case (not yet supported), | 1669 * In the BIG_MEM or MMAP case (not yet supported), |
1584 * window_size == input_size + MIN_LOOKAHEAD && | 1670 * window_size == input_size + MIN_LOOKAHEAD && |
1585 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. | 1671 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. |
1586 * Otherwise, window_size == 2*WSIZE so more >= 2. | 1672 * Otherwise, window_size == 2*WSIZE so more >= 2. |
1587 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. | 1673 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. |
1588 */ | 1674 */ |
1589 Assert(more >= 2, "more < 2"); | 1675 Assert(more >= 2, "more < 2"); |
1590 | 1676 |
1591 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); | 1677 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); |
1592 if (s->class_bitmap != NULL) { | 1678 if (s->class_bitmap != NULL) { |
1593 class_set(s, s->strstart + s->lookahead, n, s->strm->clas); | 1679 class_set(s, s->strstart + s->lookahead, n, s->strm->clas); |
1594 } | 1680 } |
1595 s->lookahead += n; | 1681 s->lookahead += n; |
1596 | 1682 |
1597 /* Initialize the hash value now that we have some input: */ | 1683 /* Initialize the hash value now that we have some input: */ |
1598 if (s->lookahead >= MIN_MATCH) { | 1684 if (s->lookahead + s->insert >= MIN_MATCH) { |
1599 s->ins_h = s->window[s->strstart]; | 1685 uInt str = s->strstart - s->insert; |
1600 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); | 1686 s->ins_h = s->window[str]; |
| 1687 UPDATE_HASH(s, s->ins_h, s->window[str + 1]); |
1601 #if MIN_MATCH != 3 | 1688 #if MIN_MATCH != 3 |
1602 Call UPDATE_HASH() MIN_MATCH-3 more times | 1689 Call UPDATE_HASH() MIN_MATCH-3 more times |
1603 #endif | 1690 #endif |
| 1691 while (s->insert) { |
| 1692 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); |
| 1693 #ifndef FASTEST |
| 1694 s->prev[str & s->w_mask] = s->head[s->ins_h]; |
| 1695 #endif |
| 1696 s->head[s->ins_h] = (Pos)str; |
| 1697 str++; |
| 1698 s->insert--; |
| 1699 if (s->lookahead + s->insert < MIN_MATCH) |
| 1700 break; |
| 1701 } |
1604 } | 1702 } |
1605 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, | 1703 /* 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. | 1704 * but this is not important since only literal bytes will be emitted. |
1607 */ | 1705 */ |
1608 | 1706 |
1609 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); | 1707 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); |
1610 | 1708 |
1611 /* If the WIN_INIT bytes after the end of the current data have never been | 1709 /* 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 | 1710 * 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 | 1711 * the use of uninitialized (or uninitialised as Julian writes) bytes by |
(...skipping 20 matching lines...) Loading... |
1634 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up | 1732 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up |
1635 * to end of window, whichever is less. | 1733 * to end of window, whichever is less. |
1636 */ | 1734 */ |
1637 init = (ulg)curr + WIN_INIT - s->high_water; | 1735 init = (ulg)curr + WIN_INIT - s->high_water; |
1638 if (init > s->window_size - s->high_water) | 1736 if (init > s->window_size - s->high_water) |
1639 init = s->window_size - s->high_water; | 1737 init = s->window_size - s->high_water; |
1640 zmemzero(s->window + s->high_water, (unsigned)init); | 1738 zmemzero(s->window + s->high_water, (unsigned)init); |
1641 s->high_water += init; | 1739 s->high_water += init; |
1642 } | 1740 } |
1643 } | 1741 } |
| 1742 |
| 1743 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, |
| 1744 "not enough room for search"); |
1644 } | 1745 } |
1645 | 1746 |
1646 /* =========================================================================== | 1747 /* =========================================================================== |
1647 * Flush the current block, with given end-of-file flag. | 1748 * Flush the current block, with given end-of-file flag. |
1648 * IN assertion: strstart is set to the end of the current match. | 1749 * IN assertion: strstart is set to the end of the current match. |
1649 */ | 1750 */ |
1650 #define FLUSH_BLOCK_ONLY(s, last) { \ | 1751 #define FLUSH_BLOCK_ONLY(s, last) { \ |
1651 _tr_flush_block(s, (s->block_start >= 0L ? \ | 1752 _tr_flush_block(s, (s->block_start >= 0L ? \ |
1652 (charf *)&s->window[(unsigned)s->block_start] : \ | 1753 (charf *)&s->window[(unsigned)s->block_start] : \ |
1653 (charf *)Z_NULL), \ | 1754 (charf *)Z_NULL), \ |
(...skipping 60 matching lines...) Loading... |
1714 s->strstart = (uInt)max_start; | 1815 s->strstart = (uInt)max_start; |
1715 FLUSH_BLOCK(s, 0); | 1816 FLUSH_BLOCK(s, 0); |
1716 } | 1817 } |
1717 /* Flush if we may have to slide, otherwise block_start may become | 1818 /* Flush if we may have to slide, otherwise block_start may become |
1718 * negative and the data will be gone: | 1819 * negative and the data will be gone: |
1719 */ | 1820 */ |
1720 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { | 1821 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { |
1721 FLUSH_BLOCK(s, 0); | 1822 FLUSH_BLOCK(s, 0); |
1722 } | 1823 } |
1723 } | 1824 } |
1724 FLUSH_BLOCK(s, flush == Z_FINISH); | 1825 s->insert = 0; |
1725 return flush == Z_FINISH ? finish_done : block_done; | 1826 if (flush == Z_FINISH) { |
| 1827 FLUSH_BLOCK(s, 1); |
| 1828 return finish_done; |
| 1829 } |
| 1830 if ((long)s->strstart > s->block_start) |
| 1831 FLUSH_BLOCK(s, 0); |
| 1832 return block_done; |
1726 } | 1833 } |
1727 | 1834 |
1728 /* =========================================================================== | 1835 /* =========================================================================== |
1729 * Compress as much as possible from the input stream, return the current | 1836 * Compress as much as possible from the input stream, return the current |
1730 * block state. | 1837 * block state. |
1731 * This function does not perform lazy evaluation of matches and inserts | 1838 * This function does not perform lazy evaluation of matches and inserts |
1732 * new strings in the dictionary only for unmatched strings or for short | 1839 * new strings in the dictionary only for unmatched strings or for short |
1733 * matches. It is used only for the fast compression options. | 1840 * matches. It is used only for the fast compression options. |
1734 */ | 1841 */ |
1735 local block_state deflate_fast(s, flush, clas) | 1842 local block_state deflate_fast(s, flush, clas) |
(...skipping 81 matching lines...) Loading... |
1817 } | 1924 } |
1818 } else { | 1925 } else { |
1819 /* No match, output a literal byte */ | 1926 /* No match, output a literal byte */ |
1820 Tracevv((stderr,"%c", s->window[s->strstart])); | 1927 Tracevv((stderr,"%c", s->window[s->strstart])); |
1821 _tr_tally_lit (s, s->window[s->strstart], bflush); | 1928 _tr_tally_lit (s, s->window[s->strstart], bflush); |
1822 s->lookahead--; | 1929 s->lookahead--; |
1823 s->strstart++; | 1930 s->strstart++; |
1824 } | 1931 } |
1825 if (bflush) FLUSH_BLOCK(s, 0); | 1932 if (bflush) FLUSH_BLOCK(s, 0); |
1826 } | 1933 } |
1827 FLUSH_BLOCK(s, flush == Z_FINISH); | 1934 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; |
1828 return flush == Z_FINISH ? finish_done : block_done; | 1935 if (flush == Z_FINISH) { |
| 1936 FLUSH_BLOCK(s, 1); |
| 1937 return finish_done; |
| 1938 } |
| 1939 if (s->last_lit) |
| 1940 FLUSH_BLOCK(s, 0); |
| 1941 return block_done; |
1829 } | 1942 } |
1830 | 1943 |
1831 #ifndef FASTEST | 1944 #ifndef FASTEST |
1832 /* =========================================================================== | 1945 /* =========================================================================== |
1833 * Same as above, but achieves better compression. We use a lazy | 1946 * Same as above, but achieves better compression. We use a lazy |
1834 * evaluation for matches: a match is finally adopted only if there is | 1947 * evaluation for matches: a match is finally adopted only if there is |
1835 * no better match at the next window position. | 1948 * no better match at the next window position. |
1836 */ | 1949 */ |
1837 local block_state deflate_slow(s, flush, clas) | 1950 local block_state deflate_slow(s, flush, clas) |
1838 deflate_state *s; | 1951 deflate_state *s; |
(...skipping 134 matching lines...) Loading... |
1973 s->strstart++; | 2086 s->strstart++; |
1974 s->lookahead--; | 2087 s->lookahead--; |
1975 } | 2088 } |
1976 } | 2089 } |
1977 Assert (flush != Z_NO_FLUSH, "no flush?"); | 2090 Assert (flush != Z_NO_FLUSH, "no flush?"); |
1978 if (s->match_available) { | 2091 if (s->match_available) { |
1979 Tracevv((stderr,"%c", s->window[s->strstart-1])); | 2092 Tracevv((stderr,"%c", s->window[s->strstart-1])); |
1980 _tr_tally_lit(s, s->window[s->strstart-1], bflush); | 2093 _tr_tally_lit(s, s->window[s->strstart-1], bflush); |
1981 s->match_available = 0; | 2094 s->match_available = 0; |
1982 } | 2095 } |
1983 FLUSH_BLOCK(s, flush == Z_FINISH); | 2096 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; |
1984 return flush == Z_FINISH ? finish_done : block_done; | 2097 if (flush == Z_FINISH) { |
| 2098 FLUSH_BLOCK(s, 1); |
| 2099 return finish_done; |
| 2100 } |
| 2101 if (s->last_lit) |
| 2102 FLUSH_BLOCK(s, 0); |
| 2103 return block_done; |
1985 } | 2104 } |
1986 #endif /* FASTEST */ | 2105 #endif /* FASTEST */ |
1987 | 2106 |
1988 /* =========================================================================== | 2107 /* =========================================================================== |
1989 * For Z_RLE, simply look for runs of bytes, generate matches only of distance | 2108 * 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 | 2109 * one. Do not maintain a hash table. (It will be regenerated if this run of |
1991 * deflate switches away from Z_RLE.) | 2110 * deflate switches away from Z_RLE.) |
1992 */ | 2111 */ |
1993 local block_state deflate_rle(s, flush) | 2112 local block_state deflate_rle(s, flush) |
1994 deflate_state *s; | 2113 deflate_state *s; |
1995 int flush; | 2114 int flush; |
1996 { | 2115 { |
1997 int bflush; /* set if current block must be flushed */ | 2116 int bflush; /* set if current block must be flushed */ |
1998 uInt prev; /* byte at distance one to match */ | 2117 uInt prev; /* byte at distance one to match */ |
1999 Bytef *scan, *strend; /* scan goes up to strend for length of run */ | 2118 Bytef *scan, *strend; /* scan goes up to strend for length of run */ |
2000 | 2119 |
2001 for (;;) { | 2120 for (;;) { |
2002 /* Make sure that we always have enough lookahead, except | 2121 /* Make sure that we always have enough lookahead, except |
2003 * at the end of the input file. We need MAX_MATCH bytes | 2122 * at the end of the input file. We need MAX_MATCH bytes |
2004 * for the longest encodable run. | 2123 * for the longest run, plus one for the unrolled loop. |
2005 */ | 2124 */ |
2006 if (s->lookahead < MAX_MATCH) { | 2125 if (s->lookahead <= MAX_MATCH) { |
2007 fill_window(s); | 2126 fill_window(s); |
2008 if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { | 2127 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { |
2009 return need_more; | 2128 return need_more; |
2010 } | 2129 } |
2011 if (s->lookahead == 0) break; /* flush the current block */ | 2130 if (s->lookahead == 0) break; /* flush the current block */ |
2012 } | 2131 } |
2013 | 2132 |
2014 /* See how many times the previous byte repeats */ | 2133 /* See how many times the previous byte repeats */ |
2015 s->match_length = 0; | 2134 s->match_length = 0; |
2016 if (s->lookahead >= MIN_MATCH && s->strstart > 0) { | 2135 if (s->lookahead >= MIN_MATCH && s->strstart > 0) { |
2017 scan = s->window + s->strstart - 1; | 2136 scan = s->window + s->strstart - 1; |
2018 prev = *scan; | 2137 prev = *scan; |
2019 if (prev == *++scan && prev == *++scan && prev == *++scan) { | 2138 if (prev == *++scan && prev == *++scan && prev == *++scan) { |
2020 strend = s->window + s->strstart + MAX_MATCH; | 2139 strend = s->window + s->strstart + MAX_MATCH; |
2021 do { | 2140 do { |
2022 } while (prev == *++scan && prev == *++scan && | 2141 } while (prev == *++scan && prev == *++scan && |
2023 prev == *++scan && prev == *++scan && | 2142 prev == *++scan && prev == *++scan && |
2024 prev == *++scan && prev == *++scan && | 2143 prev == *++scan && prev == *++scan && |
2025 prev == *++scan && prev == *++scan && | 2144 prev == *++scan && prev == *++scan && |
2026 scan < strend); | 2145 scan < strend); |
2027 s->match_length = MAX_MATCH - (int)(strend - scan); | 2146 s->match_length = MAX_MATCH - (int)(strend - scan); |
2028 if (s->match_length > s->lookahead) | 2147 if (s->match_length > s->lookahead) |
2029 s->match_length = s->lookahead; | 2148 s->match_length = s->lookahead; |
2030 } | 2149 } |
| 2150 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); |
2031 } | 2151 } |
2032 | 2152 |
2033 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ | 2153 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ |
2034 if (s->match_length >= MIN_MATCH) { | 2154 if (s->match_length >= MIN_MATCH) { |
2035 check_match(s, s->strstart, s->strstart - 1, s->match_length); | 2155 check_match(s, s->strstart, s->strstart - 1, s->match_length); |
2036 | 2156 |
2037 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); | 2157 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); |
2038 | 2158 |
2039 s->lookahead -= s->match_length; | 2159 s->lookahead -= s->match_length; |
2040 s->strstart += s->match_length; | 2160 s->strstart += s->match_length; |
2041 s->match_length = 0; | 2161 s->match_length = 0; |
2042 } else { | 2162 } else { |
2043 /* No match, output a literal byte */ | 2163 /* No match, output a literal byte */ |
2044 Tracevv((stderr,"%c", s->window[s->strstart])); | 2164 Tracevv((stderr,"%c", s->window[s->strstart])); |
2045 _tr_tally_lit (s, s->window[s->strstart], bflush); | 2165 _tr_tally_lit (s, s->window[s->strstart], bflush); |
2046 s->lookahead--; | 2166 s->lookahead--; |
2047 s->strstart++; | 2167 s->strstart++; |
2048 } | 2168 } |
2049 if (bflush) FLUSH_BLOCK(s, 0); | 2169 if (bflush) FLUSH_BLOCK(s, 0); |
2050 } | 2170 } |
2051 FLUSH_BLOCK(s, flush == Z_FINISH); | 2171 s->insert = 0; |
2052 return flush == Z_FINISH ? finish_done : block_done; | 2172 if (flush == Z_FINISH) { |
| 2173 FLUSH_BLOCK(s, 1); |
| 2174 return finish_done; |
| 2175 } |
| 2176 if (s->last_lit) |
| 2177 FLUSH_BLOCK(s, 0); |
| 2178 return block_done; |
2053 } | 2179 } |
2054 | 2180 |
2055 /* =========================================================================== | 2181 /* =========================================================================== |
2056 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. | 2182 * 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.) | 2183 * (It will be regenerated if this run of deflate switches away from Huffman.) |
2058 */ | 2184 */ |
2059 local block_state deflate_huff(s, flush) | 2185 local block_state deflate_huff(s, flush) |
2060 deflate_state *s; | 2186 deflate_state *s; |
2061 int flush; | 2187 int flush; |
2062 { | 2188 { |
(...skipping 11 matching lines...) Loading... |
2074 } | 2200 } |
2075 | 2201 |
2076 /* Output a literal byte */ | 2202 /* Output a literal byte */ |
2077 s->match_length = 0; | 2203 s->match_length = 0; |
2078 Tracevv((stderr,"%c", s->window[s->strstart])); | 2204 Tracevv((stderr,"%c", s->window[s->strstart])); |
2079 _tr_tally_lit (s, s->window[s->strstart], bflush); | 2205 _tr_tally_lit (s, s->window[s->strstart], bflush); |
2080 s->lookahead--; | 2206 s->lookahead--; |
2081 s->strstart++; | 2207 s->strstart++; |
2082 if (bflush) FLUSH_BLOCK(s, 0); | 2208 if (bflush) FLUSH_BLOCK(s, 0); |
2083 } | 2209 } |
2084 FLUSH_BLOCK(s, flush == Z_FINISH); | 2210 s->insert = 0; |
2085 return flush == Z_FINISH ? finish_done : block_done; | 2211 if (flush == Z_FINISH) { |
| 2212 FLUSH_BLOCK(s, 1); |
| 2213 return finish_done; |
| 2214 } |
| 2215 if (s->last_lit) |
| 2216 FLUSH_BLOCK(s, 0); |
| 2217 return block_done; |
2086 } | 2218 } |
2087 | 2219 |
2088 /* Safe to inline this as GCC/clang will use inline asm and Visual Studio will | 2220 /* Safe to inline this as GCC/clang will use inline asm and Visual Studio will |
2089 * use intrinsic without extra params | 2221 * use intrinsic without extra params |
2090 */ | 2222 */ |
2091 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str) | 2223 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str) |
2092 { | 2224 { |
2093 Pos ret; | 2225 Pos ret; |
2094 unsigned *ip, val, h = 0; | 2226 unsigned *ip, val, h = 0; |
2095 | 2227 |
(...skipping 15 matching lines...) Loading... |
2111 #else | 2243 #else |
2112 /* This should never happen */ | 2244 /* This should never happen */ |
2113 assert(0); | 2245 assert(0); |
2114 #endif | 2246 #endif |
2115 | 2247 |
2116 ret = s->head[h & s->hash_mask]; | 2248 ret = s->head[h & s->hash_mask]; |
2117 s->head[h & s->hash_mask] = str; | 2249 s->head[h & s->hash_mask] = str; |
2118 s->prev[str & s->w_mask] = ret; | 2250 s->prev[str & s->w_mask] = ret; |
2119 return ret; | 2251 return ret; |
2120 } | 2252 } |
OLD | NEW |