Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(472)

Side by Side Diff: third_party/zlib/deflate.c

Issue 1955383002: Update Zlib to version 1.2.8 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix unzip on win platform Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/zlib/deflate.h ('k') | third_party/zlib/google.patch » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
52 #include <assert.h> 51 #include <assert.h>
53
54 #include "deflate.h" 52 #include "deflate.h"
55 #include "x86.h" 53 #include "x86.h"
56 54
57 const char deflate_copyright[] = 55 const char deflate_copyright[] =
58 " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler "; 56 " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
59 /* 57 /*
60 If you use the zlib library in a product, an acknowledgment is welcome 58 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 59 in the documentation of your product. If for some reason you cannot
62 include such an acknowledgment, I would appreciate that you keep this 60 include such an acknowledgment, I would appreciate that you keep this
63 copyright string in the executable of your product. 61 copyright string in the executable of your product.
64 */ 62 */
65 63
66 /* =========================================================================== 64 /* ===========================================================================
67 * Function prototypes. 65 * Function prototypes.
68 */ 66 */
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 * meaning. 167 * meaning.
170 */ 168 */
171 169
172 #define EQUAL 0 170 #define EQUAL 0
173 /* result of memcmp for equal strings */ 171 /* result of memcmp for equal strings */
174 172
175 #ifndef NO_DUMMY_DECL 173 #ifndef NO_DUMMY_DECL
176 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ 174 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
177 #endif 175 #endif
178 176
177 /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
178 #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
179
179 /* =========================================================================== 180 /* ===========================================================================
180 * Update a hash value with the given input byte 181 * Update a hash value with the given input byte
181 * IN assertion: all calls to to UPDATE_HASH are made with consecutive 182 * 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 183 * input characters, so that a running hash key can be computed from the
183 * previous key instead of complete recalculation each time. 184 * previous key instead of complete recalculation each time.
184 */ 185 */
185 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 186 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
186 187
187 /* =========================================================================== 188 /* ===========================================================================
188 * Insert string str in the dictionary and set match_head to the previous head 189 * 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
262 x86_check_features(); 263 x86_check_features();
263 264
264 if (version == Z_NULL || version[0] != my_version[0] || 265 if (version == Z_NULL || version[0] != my_version[0] ||
265 stream_size != sizeof(z_stream)) { 266 stream_size != sizeof(z_stream)) {
266 return Z_VERSION_ERROR; 267 return Z_VERSION_ERROR;
267 } 268 }
268 if (strm == Z_NULL) return Z_STREAM_ERROR; 269 if (strm == Z_NULL) return Z_STREAM_ERROR;
269 270
270 strm->msg = Z_NULL; 271 strm->msg = Z_NULL;
271 if (strm->zalloc == (alloc_func)0) { 272 if (strm->zalloc == (alloc_func)0) {
273 #ifdef Z_SOLO
274 return Z_STREAM_ERROR;
275 #else
272 strm->zalloc = zcalloc; 276 strm->zalloc = zcalloc;
273 strm->opaque = (voidpf)0; 277 strm->opaque = (voidpf)0;
278 #endif
274 } 279 }
275 if (strm->zfree == (free_func)0) strm->zfree = zcfree; 280 if (strm->zfree == (free_func)0)
281 #ifdef Z_SOLO
282 return Z_STREAM_ERROR;
283 #else
284 strm->zfree = zcfree;
285 #endif
276 286
277 #ifdef FASTEST 287 #ifdef FASTEST
278 if (level != 0) level = 1; 288 if (level != 0) level = 1;
279 #else 289 #else
280 if (level == Z_DEFAULT_COMPRESSION) level = 6; 290 if (level == Z_DEFAULT_COMPRESSION) level = 6;
281 #endif 291 #endif
282 292
283 if (windowBits < 0) { /* suppress zlib wrapper */ 293 if (windowBits < 0) { /* suppress zlib wrapper */
284 wrap = 0; 294 wrap = 0;
285 windowBits = -windowBits; 295 windowBits = -windowBits;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 338
329 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 339 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
330 340
331 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); 341 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
332 s->pending_buf = (uchf *) overlay; 342 s->pending_buf = (uchf *) overlay;
333 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); 343 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
334 344
335 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 345 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
336 s->pending_buf == Z_NULL) { 346 s->pending_buf == Z_NULL) {
337 s->status = FINISH_STATE; 347 s->status = FINISH_STATE;
338 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR); 348 strm->msg = ERR_MSG(Z_MEM_ERROR);
339 deflateEnd (strm); 349 deflateEnd (strm);
340 return Z_MEM_ERROR; 350 return Z_MEM_ERROR;
341 } 351 }
342 s->d_buf = overlay + s->lit_bufsize/sizeof(ush); 352 s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
343 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; 353 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
344 354
345 s->level = level; 355 s->level = level;
346 s->strategy = strategy; 356 s->strategy = strategy;
347 s->method = (Byte)method; 357 s->method = (Byte)method;
348 358
349 return deflateReset(strm); 359 return deflateReset(strm);
350 } 360 }
351 361
352 /* ========================================================================= */ 362 /* ========================================================================= */
353 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) 363 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
354 z_streamp strm; 364 z_streamp strm;
355 const Bytef *dictionary; 365 const Bytef *dictionary;
356 uInt dictLength; 366 uInt dictLength;
357 { 367 {
358 deflate_state *s; 368 deflate_state *s;
359 uInt length = dictLength; 369 uInt str, n;
360 uInt n; 370 int wrap;
361 IPos hash_head = 0; 371 unsigned avail;
372 z_const unsigned char *next;
362 373
363 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL || 374 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
364 strm->state->wrap == 2 || 375 return Z_STREAM_ERROR;
365 (strm->state->wrap == 1 && strm->state->status != INIT_STATE)) 376 s = strm->state;
377 wrap = s->wrap;
378 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
366 return Z_STREAM_ERROR; 379 return Z_STREAM_ERROR;
367 380
368 s = strm->state; 381 /* when using zlib wrappers, compute Adler-32 for provided dictionary */
369 if (s->wrap) 382 if (wrap == 1)
370 strm->adler = adler32(strm->adler, dictionary, dictLength); 383 strm->adler = adler32(strm->adler, dictionary, dictLength);
384 s->wrap = 0; /* avoid computing Adler-32 in read_buf */
371 385
372 if (length < MIN_MATCH) return Z_OK; 386 /* if dictionary would fill window, just replace the history */
373 if (length > s->w_size) { 387 if (dictLength >= s->w_size) {
374 length = s->w_size; 388 if (wrap == 0) { /* already empty otherwise */
375 dictionary += dictLength - length; /* use the tail of the dictionary */ 389 CLEAR_HASH(s);
390 s->strstart = 0;
391 s->block_start = 0L;
392 s->insert = 0;
393 }
394 dictionary += dictLength - s->w_size; /* use the tail */
395 dictLength = s->w_size;
376 } 396 }
377 zmemcpy(s->window, dictionary, length);
378 s->strstart = length;
379 s->block_start = (long)length;
380 397
381 /* Insert all strings in the hash table (except for the last two bytes). 398 /* insert dictionary into window and hash */
382 * s->lookahead stays null, so s->ins_h will be recomputed at the next 399 avail = strm->avail_in;
383 * call of fill_window. 400 next = strm->next_in;
384 */ 401 strm->avail_in = dictLength;
385 s->ins_h = s->window[0]; 402 strm->next_in = (z_const Bytef *)dictionary;
386 UPDATE_HASH(s, s->ins_h, s->window[1]); 403 fill_window(s);
387 for (n = 0; n <= length - MIN_MATCH; n++) { 404 while (s->lookahead >= MIN_MATCH) {
388 insert_string(s, n); 405 str = s->strstart;
406 n = s->lookahead - (MIN_MATCH-1);
407 do {
408 insert_string(s, str);
409 str++;
410 } while (--n);
411 s->strstart = str;
412 s->lookahead = MIN_MATCH-1;
413 fill_window(s);
389 } 414 }
390 if (hash_head) hash_head = 0; /* to make compiler happy */ 415 s->strstart += s->lookahead;
416 s->block_start = (long)s->strstart;
417 s->insert = s->lookahead;
418 s->lookahead = 0;
419 s->match_length = s->prev_length = MIN_MATCH-1;
420 s->match_available = 0;
421 strm->next_in = next;
422 strm->avail_in = avail;
423 s->wrap = wrap;
391 return Z_OK; 424 return Z_OK;
392 } 425 }
393 426
394 /* ========================================================================= */ 427 /* ========================================================================= */
395 int ZEXPORT deflateReset (strm) 428 int ZEXPORT deflateResetKeep (strm)
396 z_streamp strm; 429 z_streamp strm;
397 { 430 {
398 deflate_state *s; 431 deflate_state *s;
399 432
400 if (strm == Z_NULL || strm->state == Z_NULL || 433 if (strm == Z_NULL || strm->state == Z_NULL ||
401 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { 434 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
402 return Z_STREAM_ERROR; 435 return Z_STREAM_ERROR;
403 } 436 }
404 437
405 strm->total_in = strm->total_out = 0; 438 strm->total_in = strm->total_out = 0;
(...skipping 11 matching lines...) Expand all
417 } 450 }
418 s->status = s->wrap ? INIT_STATE : BUSY_STATE; 451 s->status = s->wrap ? INIT_STATE : BUSY_STATE;
419 strm->adler = 452 strm->adler =
420 #ifdef GZIP 453 #ifdef GZIP
421 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 454 s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
422 #endif 455 #endif
423 adler32(0L, Z_NULL, 0); 456 adler32(0L, Z_NULL, 0);
424 s->last_flush = Z_NO_FLUSH; 457 s->last_flush = Z_NO_FLUSH;
425 458
426 _tr_init(s); 459 _tr_init(s);
427 lm_init(s);
428 460
429 return Z_OK; 461 return Z_OK;
430 } 462 }
431 463
432 /* ========================================================================= */ 464 /* ========================================================================= */
465 int ZEXPORT deflateReset (strm)
466 z_streamp strm;
467 {
468 int ret;
469
470 ret = deflateResetKeep(strm);
471 if (ret == Z_OK)
472 lm_init(strm->state);
473 return ret;
474 }
475
476 /* ========================================================================= */
433 int ZEXPORT deflateSetHeader (strm, head) 477 int ZEXPORT deflateSetHeader (strm, head)
434 z_streamp strm; 478 z_streamp strm;
435 gz_headerp head; 479 gz_headerp head;
436 { 480 {
437 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 481 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
438 if (strm->state->wrap != 2) return Z_STREAM_ERROR; 482 if (strm->state->wrap != 2) return Z_STREAM_ERROR;
439 strm->state->gzhead = head; 483 strm->state->gzhead = head;
440 return Z_OK; 484 return Z_OK;
441 } 485 }
442 486
443 /* ========================================================================= */ 487 /* ========================================================================= */
488 int ZEXPORT deflatePending (strm, pending, bits)
489 unsigned *pending;
490 int *bits;
491 z_streamp strm;
492 {
493 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
494 if (pending != Z_NULL)
495 *pending = strm->state->pending;
496 if (bits != Z_NULL)
497 *bits = strm->state->bi_valid;
498 return Z_OK;
499 }
500
501 /* ========================================================================= */
444 int ZEXPORT deflatePrime (strm, bits, value) 502 int ZEXPORT deflatePrime (strm, bits, value)
445 z_streamp strm; 503 z_streamp strm;
446 int bits; 504 int bits;
447 int value; 505 int value;
448 { 506 {
507 deflate_state *s;
508 int put;
509
449 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 510 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
450 strm->state->bi_valid = bits; 511 s = strm->state;
451 strm->state->bi_buf = (ush)(value & ((1 << bits) - 1)); 512 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
513 return Z_BUF_ERROR;
514 do {
515 put = Buf_size - s->bi_valid;
516 if (put > bits)
517 put = bits;
518 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
519 s->bi_valid += put;
520 _tr_flush_bits(s);
521 value >>= put;
522 bits -= put;
523 } while (bits);
452 return Z_OK; 524 return Z_OK;
453 } 525 }
454 526
455 /* ========================================================================= */ 527 /* ========================================================================= */
456 int ZEXPORT deflateParams(strm, level, strategy) 528 int ZEXPORT deflateParams(strm, level, strategy)
457 z_streamp strm; 529 z_streamp strm;
458 int level; 530 int level;
459 int strategy; 531 int strategy;
460 { 532 {
461 deflate_state *s; 533 deflate_state *s;
(...skipping 10 matching lines...) Expand all
472 #endif 544 #endif
473 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { 545 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
474 return Z_STREAM_ERROR; 546 return Z_STREAM_ERROR;
475 } 547 }
476 func = configuration_table[s->level].func; 548 func = configuration_table[s->level].func;
477 549
478 if ((strategy != s->strategy || func != configuration_table[level].func) && 550 if ((strategy != s->strategy || func != configuration_table[level].func) &&
479 strm->total_in != 0) { 551 strm->total_in != 0) {
480 /* Flush the last buffer: */ 552 /* Flush the last buffer: */
481 err = deflate(strm, Z_BLOCK); 553 err = deflate(strm, Z_BLOCK);
554 if (err == Z_BUF_ERROR && s->pending == 0)
555 err = Z_OK;
482 } 556 }
483 if (s->level != level) { 557 if (s->level != level) {
484 s->level = level; 558 s->level = level;
485 s->max_lazy_match = configuration_table[level].max_lazy; 559 s->max_lazy_match = configuration_table[level].max_lazy;
486 s->good_match = configuration_table[level].good_length; 560 s->good_match = configuration_table[level].good_length;
487 s->nice_match = configuration_table[level].nice_length; 561 s->nice_match = configuration_table[level].nice_length;
488 s->max_chain_length = configuration_table[level].max_chain; 562 s->max_chain_length = configuration_table[level].max_chain;
489 } 563 }
490 s->strategy = strategy; 564 s->strategy = strategy;
491 return err; 565 return err;
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 673
600 /* ========================================================================= 674 /* =========================================================================
601 * Flush as much pending output as possible. All deflate() output goes 675 * Flush as much pending output as possible. All deflate() output goes
602 * through this function so some applications may wish to modify it 676 * 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. 677 * to avoid allocating a large strm->next_out buffer and copying into it.
604 * (See also read_buf()). 678 * (See also read_buf()).
605 */ 679 */
606 local void flush_pending(strm) 680 local void flush_pending(strm)
607 z_streamp strm; 681 z_streamp strm;
608 { 682 {
609 unsigned len = strm->state->pending; 683 unsigned len;
684 deflate_state *s = strm->state;
610 685
686 _tr_flush_bits(s);
687 len = s->pending;
611 if (len > strm->avail_out) len = strm->avail_out; 688 if (len > strm->avail_out) len = strm->avail_out;
612 if (len == 0) return; 689 if (len == 0) return;
613 690
614 zmemcpy(strm->next_out, strm->state->pending_out, len); 691 zmemcpy(strm->next_out, s->pending_out, len);
615 strm->next_out += len; 692 strm->next_out += len;
616 strm->state->pending_out += len; 693 s->pending_out += len;
617 strm->total_out += len; 694 strm->total_out += len;
618 strm->avail_out -= len; 695 strm->avail_out -= len;
619 strm->state->pending -= len; 696 s->pending -= len;
620 if (strm->state->pending == 0) { 697 if (s->pending == 0) {
621 strm->state->pending_out = strm->state->pending_buf; 698 s->pending_out = s->pending_buf;
622 } 699 }
623 } 700 }
624 701
625 /* ========================================================================= */ 702 /* ========================================================================= */
626 int ZEXPORT deflate (strm, flush) 703 int ZEXPORT deflate (strm, flush)
627 z_streamp strm; 704 z_streamp strm;
628 int flush; 705 int flush;
629 { 706 {
630 int old_flush; /* value of flush param for previous deflate call */ 707 int old_flush; /* value of flush param for previous deflate call */
631 deflate_state *s; 708 deflate_state *s;
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 * return OK instead of BUF_ERROR at next call of deflate: 915 * return OK instead of BUF_ERROR at next call of deflate:
839 */ 916 */
840 s->last_flush = -1; 917 s->last_flush = -1;
841 return Z_OK; 918 return Z_OK;
842 } 919 }
843 920
844 /* Make sure there is something to do and avoid duplicate consecutive 921 /* Make sure there is something to do and avoid duplicate consecutive
845 * flushes. For repeated and useless calls with Z_FINISH, we keep 922 * flushes. For repeated and useless calls with Z_FINISH, we keep
846 * returning Z_STREAM_END instead of Z_BUF_ERROR. 923 * returning Z_STREAM_END instead of Z_BUF_ERROR.
847 */ 924 */
848 } else if (strm->avail_in == 0 && flush <= old_flush && 925 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
849 flush != Z_FINISH) { 926 flush != Z_FINISH) {
850 ERR_RETURN(strm, Z_BUF_ERROR); 927 ERR_RETURN(strm, Z_BUF_ERROR);
851 } 928 }
852 929
853 /* User must not provide more input after the first FINISH: */ 930 /* User must not provide more input after the first FINISH: */
854 if (s->status == FINISH_STATE && strm->avail_in != 0) { 931 if (s->status == FINISH_STATE && strm->avail_in != 0) {
855 ERR_RETURN(strm, Z_BUF_ERROR); 932 ERR_RETURN(strm, Z_BUF_ERROR);
856 } 933 }
857 934
858 /* Start a new block or continue the current one. 935 /* Start a new block or continue the current one.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ 981 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
905 _tr_stored_block(s, (char*)0, 0L, 0); 982 _tr_stored_block(s, (char*)0, 0L, 0);
906 /* For a full flush, this empty block will be recognized 983 /* For a full flush, this empty block will be recognized
907 * as a special marker by inflate_sync(). 984 * as a special marker by inflate_sync().
908 */ 985 */
909 if (flush == Z_FULL_FLUSH) { 986 if (flush == Z_FULL_FLUSH) {
910 CLEAR_HASH(s); /* forget history */ 987 CLEAR_HASH(s); /* forget history */
911 if (s->lookahead == 0) { 988 if (s->lookahead == 0) {
912 s->strstart = 0; 989 s->strstart = 0;
913 s->block_start = 0L; 990 s->block_start = 0L;
991 s->insert = 0;
914 } 992 }
915 } 993 }
916 } 994 }
917 flush_pending(strm); 995 flush_pending(strm);
918 if (strm->avail_out == 0) { 996 if (strm->avail_out == 0) {
919 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ 997 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
920 return Z_OK; 998 return Z_OK;
921 } 999 }
922 } 1000 }
923 } 1001 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1001 deflate_state *ss; 1079 deflate_state *ss;
1002 ushf *overlay; 1080 ushf *overlay;
1003 1081
1004 1082
1005 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { 1083 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
1006 return Z_STREAM_ERROR; 1084 return Z_STREAM_ERROR;
1007 } 1085 }
1008 1086
1009 ss = source->state; 1087 ss = source->state;
1010 1088
1011 zmemcpy(dest, source, sizeof(z_stream)); 1089 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1012 1090
1013 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); 1091 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1014 if (ds == Z_NULL) return Z_MEM_ERROR; 1092 if (ds == Z_NULL) return Z_MEM_ERROR;
1015 dest->state = (struct internal_state FAR *) ds; 1093 dest->state = (struct internal_state FAR *) ds;
1016 zmemcpy(ds, ss, sizeof(deflate_state)); 1094 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
1017 ds->strm = dest; 1095 ds->strm = dest;
1018 1096
1019 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); 1097 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1020 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); 1098 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1021 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); 1099 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
1022 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); 1100 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
1023 ds->pending_buf = (uchf *) overlay; 1101 ds->pending_buf = (uchf *) overlay;
1024 1102
1025 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || 1103 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1026 ds->pending_buf == Z_NULL) { 1104 ds->pending_buf == Z_NULL) {
1027 deflateEnd (dest); 1105 deflateEnd (dest);
1028 return Z_MEM_ERROR; 1106 return Z_MEM_ERROR;
1029 } 1107 }
1030 /* following zmemcpy do not work for 16-bit MSDOS */ 1108 /* following zmemcpy do not work for 16-bit MSDOS */
1031 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); 1109 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1032 zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos)); 1110 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
1033 zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos)); 1111 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); 1112 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1035 1113
1036 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 1114 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1037 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); 1115 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
1038 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; 1116 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
1039 1117
1040 ds->l_desc.dyn_tree = ds->dyn_ltree; 1118 ds->l_desc.dyn_tree = ds->dyn_ltree;
1041 ds->d_desc.dyn_tree = ds->dyn_dtree; 1119 ds->d_desc.dyn_tree = ds->dyn_dtree;
1042 ds->bl_desc.dyn_tree = ds->bl_tree; 1120 ds->bl_desc.dyn_tree = ds->bl_tree;
1043 1121
(...skipping 14 matching lines...) Expand all
1058 unsigned size; 1136 unsigned size;
1059 { 1137 {
1060 unsigned len = strm->avail_in; 1138 unsigned len = strm->avail_in;
1061 1139
1062 if (len > size) len = size; 1140 if (len > size) len = size;
1063 if (len == 0) return 0; 1141 if (len == 0) return 0;
1064 1142
1065 strm->avail_in -= len; 1143 strm->avail_in -= len;
1066 1144
1067 #ifdef GZIP 1145 #ifdef GZIP
1068 if (strm->state->wrap == 2) { 1146 if (strm->state->wrap == 2)
1069 copy_with_crc(strm, buf, len); 1147 copy_with_crc(strm, buf, len);
1070 } 1148 else
1071 else
1072 #endif 1149 #endif
1073 { 1150 {
1074 zmemcpy(buf, strm->next_in, len); 1151 zmemcpy(buf, strm->next_in, len);
1075 if (strm->state->wrap == 1) 1152 if (strm->state->wrap == 1)
1076 strm->adler = adler32(strm->adler, buf, len); 1153 strm->adler = adler32(strm->adler, buf, len);
1077 } 1154 }
1078 strm->next_in += len; 1155 strm->next_in += len;
1079 strm->total_in += len; 1156 strm->total_in += len;
1080 1157
1081 return (int)len; 1158 return (int)len;
(...skipping 12 matching lines...) Expand all
1094 /* Set the default configuration parameters: 1171 /* Set the default configuration parameters:
1095 */ 1172 */
1096 s->max_lazy_match = configuration_table[s->level].max_lazy; 1173 s->max_lazy_match = configuration_table[s->level].max_lazy;
1097 s->good_match = configuration_table[s->level].good_length; 1174 s->good_match = configuration_table[s->level].good_length;
1098 s->nice_match = configuration_table[s->level].nice_length; 1175 s->nice_match = configuration_table[s->level].nice_length;
1099 s->max_chain_length = configuration_table[s->level].max_chain; 1176 s->max_chain_length = configuration_table[s->level].max_chain;
1100 1177
1101 s->strstart = 0; 1178 s->strstart = 0;
1102 s->block_start = 0L; 1179 s->block_start = 0L;
1103 s->lookahead = 0; 1180 s->lookahead = 0;
1181 s->insert = 0;
1104 s->match_length = s->prev_length = MIN_MATCH-1; 1182 s->match_length = s->prev_length = MIN_MATCH-1;
1105 s->match_available = 0; 1183 s->match_available = 0;
1106 s->ins_h = 0; 1184 s->ins_h = 0;
1107 #ifndef FASTEST 1185 #ifndef FASTEST
1108 #ifdef ASMV 1186 #ifdef ASMV
1109 match_init(); /* initialize the asm code */ 1187 match_init(); /* initialize the asm code */
1110 #endif 1188 #endif
1111 #endif 1189 #endif
1112 } 1190 }
1113 1191
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
1365 (start - cookie_location) < MAX_DIST(s) && 1443 (start - cookie_location) < MAX_DIST(s) &&
1366 len <= s->lookahead) { 1444 len <= s->lookahead) {
1367 for (i = 0; i < len; i++) { 1445 for (i = 0; i < len; i++) {
1368 if (s->window[start+i] != s->window[cookie_location+i] || 1446 if (s->window[start+i] != s->window[cookie_location+i] ||
1369 class_at(s, cookie_location+i) != 1) { 1447 class_at(s, cookie_location+i) != 1) {
1370 return 0; 1448 return 0;
1371 } 1449 }
1372 } 1450 }
1373 /* Check that we aren't matching a prefix of another cookie by ensuring 1451 /* Check that we aren't matching a prefix of another cookie by ensuring
1374 * that the final byte is either a semicolon (which cannot appear in a 1452 * that the final byte is either a semicolon (which cannot appear in a
1375 * cookie value), or the match is followed by non-cookie data. */ 1453 * cookie value), or non-cookie data. */
1376 if (s->window[cookie_location+len-1] != ';' && 1454 if (s->window[cookie_location+len-1] != ';' &&
1377 class_at(s, cookie_location+len) != 0) { 1455 class_at(s, cookie_location+len) != 0) {
1378 return 0; 1456 return 0;
1379 } 1457 }
1380 s->match_start = cookie_location; 1458 s->match_start = cookie_location;
1381 return len; 1459 return len;
1382 } 1460 }
1383 1461
1384 return 0; 1462 return 0;
1385 } 1463 }
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1499 } 1577 }
1500 1578
1501 local void fill_window_c(s) 1579 local void fill_window_c(s)
1502 deflate_state *s; 1580 deflate_state *s;
1503 { 1581 {
1504 register unsigned n, m; 1582 register unsigned n, m;
1505 register Posf *p; 1583 register Posf *p;
1506 unsigned more; /* Amount of free space at the end of the window. */ 1584 unsigned more; /* Amount of free space at the end of the window. */
1507 uInt wsize = s->w_size; 1585 uInt wsize = s->w_size;
1508 1586
1587 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1588
1509 do { 1589 do {
1510 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); 1590 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1511 1591
1512 /* Deal with !@#$% 64K limit: */ 1592 /* Deal with !@#$% 64K limit: */
1513 if (sizeof(int) <= 2) { 1593 if (sizeof(int) <= 2) {
1514 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 1594 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1515 more = wsize; 1595 more = wsize;
1516 1596
1517 } else if (more == (unsigned)(-1)) { 1597 } else if (more == (unsigned)(-1)) {
1518 /* Very unlikely, but possible on 16 bit machine if 1598 /* Very unlikely, but possible on 16 bit machine if
(...skipping 30 matching lines...) Expand all
1549 #ifndef FASTEST 1629 #ifndef FASTEST
1550 p = &s->prev[n]; 1630 p = &s->prev[n];
1551 do { 1631 do {
1552 m = *--p; 1632 m = *--p;
1553 *p = (Pos)(m >= wsize ? m-wsize : NIL); 1633 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1554 /* If n is not on any hash chain, prev[n] is garbage but 1634 /* If n is not on any hash chain, prev[n] is garbage but
1555 * its value will never be used. 1635 * its value will never be used.
1556 */ 1636 */
1557 } while (--n); 1637 } while (--n);
1558 #endif 1638 #endif
1559
1560 for (n = 0; n < Z_COOKIE_HASH_SIZE; n++) { 1639 for (n = 0; n < Z_COOKIE_HASH_SIZE; n++) {
1561 if (s->cookie_locations[n] > wsize) { 1640 if (s->cookie_locations[n] > wsize) {
1562 s->cookie_locations[n] -= wsize; 1641 s->cookie_locations[n] -= wsize;
1563 } else { 1642 } else {
1564 s->cookie_locations[n] = 0; 1643 s->cookie_locations[n] = 0;
1565 } 1644 }
1566 } 1645 }
1567 1646
1568 if (s->class_bitmap) { 1647 if (s->class_bitmap) {
1569 zmemcpy(s->class_bitmap, s->class_bitmap + s->w_size/8, 1648 zmemcpy(s->class_bitmap, s->class_bitmap + s->w_size/8,
1570 s->w_size/8); 1649 s->w_size/8);
1571 zmemzero(s->class_bitmap + s->w_size/8, s->w_size/8); 1650 zmemzero(s->class_bitmap + s->w_size/8, s->w_size/8);
1572 } 1651 }
1573 1652
1574 more += wsize; 1653 more += wsize;
1575 } 1654 }
1576 if (s->strm->avail_in == 0) return; 1655 if (s->strm->avail_in == 0) break;
1577 1656
1578 /* If there was no sliding: 1657 /* If there was no sliding:
1579 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 1658 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1580 * more == window_size - lookahead - strstart 1659 * more == window_size - lookahead - strstart
1581 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 1660 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1582 * => more >= window_size - 2*WSIZE + 2 1661 * => more >= window_size - 2*WSIZE + 2
1583 * In the BIG_MEM or MMAP case (not yet supported), 1662 * In the BIG_MEM or MMAP case (not yet supported),
1584 * window_size == input_size + MIN_LOOKAHEAD && 1663 * window_size == input_size + MIN_LOOKAHEAD &&
1585 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 1664 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1586 * Otherwise, window_size == 2*WSIZE so more >= 2. 1665 * Otherwise, window_size == 2*WSIZE so more >= 2.
1587 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. 1666 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1588 */ 1667 */
1589 Assert(more >= 2, "more < 2"); 1668 Assert(more >= 2, "more < 2");
1590 1669
1591 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 1670 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1592 if (s->class_bitmap != NULL) { 1671 if (s->class_bitmap != NULL) {
1593 class_set(s, s->strstart + s->lookahead, n, s->strm->clas); 1672 class_set(s, s->strstart + s->lookahead, n, s->strm->clas);
1594 } 1673 }
1595 s->lookahead += n; 1674 s->lookahead += n;
1596 1675
1597 /* Initialize the hash value now that we have some input: */ 1676 /* Initialize the hash value now that we have some input: */
1598 if (s->lookahead >= MIN_MATCH) { 1677 if (s->lookahead + s->insert >= MIN_MATCH) {
1599 s->ins_h = s->window[s->strstart]; 1678 uInt str = s->strstart - s->insert;
1600 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1679 s->ins_h = s->window[str];
1680 UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
1601 #if MIN_MATCH != 3 1681 #if MIN_MATCH != 3
1602 Call UPDATE_HASH() MIN_MATCH-3 more times 1682 Call UPDATE_HASH() MIN_MATCH-3 more times
1603 #endif 1683 #endif
1684 while (s->insert) {
1685 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1686 #ifndef FASTEST
1687 s->prev[str & s->w_mask] = s->head[s->ins_h];
1688 #endif
1689 s->head[s->ins_h] = (Pos)str;
1690 str++;
1691 s->insert--;
1692 if (s->lookahead + s->insert < MIN_MATCH)
1693 break;
1694 }
1604 } 1695 }
1605 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 1696 /* 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. 1697 * but this is not important since only literal bytes will be emitted.
1607 */ 1698 */
1608 1699
1609 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); 1700 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1610 1701
1611 /* If the WIN_INIT bytes after the end of the current data have never been 1702 /* 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 1703 * 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 1704 * the use of uninitialized (or uninitialised as Julian writes) bytes by
(...skipping 20 matching lines...) Expand all
1634 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up 1725 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1635 * to end of window, whichever is less. 1726 * to end of window, whichever is less.
1636 */ 1727 */
1637 init = (ulg)curr + WIN_INIT - s->high_water; 1728 init = (ulg)curr + WIN_INIT - s->high_water;
1638 if (init > s->window_size - s->high_water) 1729 if (init > s->window_size - s->high_water)
1639 init = s->window_size - s->high_water; 1730 init = s->window_size - s->high_water;
1640 zmemzero(s->window + s->high_water, (unsigned)init); 1731 zmemzero(s->window + s->high_water, (unsigned)init);
1641 s->high_water += init; 1732 s->high_water += init;
1642 } 1733 }
1643 } 1734 }
1735
1736 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1737 "not enough room for search");
1644 } 1738 }
1645 1739
1646 /* =========================================================================== 1740 /* ===========================================================================
1647 * Flush the current block, with given end-of-file flag. 1741 * Flush the current block, with given end-of-file flag.
1648 * IN assertion: strstart is set to the end of the current match. 1742 * IN assertion: strstart is set to the end of the current match.
1649 */ 1743 */
1650 #define FLUSH_BLOCK_ONLY(s, last) { \ 1744 #define FLUSH_BLOCK_ONLY(s, last) { \
1651 _tr_flush_block(s, (s->block_start >= 0L ? \ 1745 _tr_flush_block(s, (s->block_start >= 0L ? \
1652 (charf *)&s->window[(unsigned)s->block_start] : \ 1746 (charf *)&s->window[(unsigned)s->block_start] : \
1653 (charf *)Z_NULL), \ 1747 (charf *)Z_NULL), \
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1714 s->strstart = (uInt)max_start; 1808 s->strstart = (uInt)max_start;
1715 FLUSH_BLOCK(s, 0); 1809 FLUSH_BLOCK(s, 0);
1716 } 1810 }
1717 /* Flush if we may have to slide, otherwise block_start may become 1811 /* Flush if we may have to slide, otherwise block_start may become
1718 * negative and the data will be gone: 1812 * negative and the data will be gone:
1719 */ 1813 */
1720 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { 1814 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1721 FLUSH_BLOCK(s, 0); 1815 FLUSH_BLOCK(s, 0);
1722 } 1816 }
1723 } 1817 }
1724 FLUSH_BLOCK(s, flush == Z_FINISH); 1818 s->insert = 0;
1725 return flush == Z_FINISH ? finish_done : block_done; 1819 if (flush == Z_FINISH) {
1820 FLUSH_BLOCK(s, 1);
1821 return finish_done;
1822 }
1823 if ((long)s->strstart > s->block_start)
1824 FLUSH_BLOCK(s, 0);
1825 return block_done;
1726 } 1826 }
1727 1827
1728 /* =========================================================================== 1828 /* ===========================================================================
1729 * Compress as much as possible from the input stream, return the current 1829 * Compress as much as possible from the input stream, return the current
1730 * block state. 1830 * block state.
1731 * This function does not perform lazy evaluation of matches and inserts 1831 * This function does not perform lazy evaluation of matches and inserts
1732 * new strings in the dictionary only for unmatched strings or for short 1832 * new strings in the dictionary only for unmatched strings or for short
1733 * matches. It is used only for the fast compression options. 1833 * matches. It is used only for the fast compression options.
1734 */ 1834 */
1735 local block_state deflate_fast(s, flush, clas) 1835 local block_state deflate_fast(s, flush, clas)
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
1817 } 1917 }
1818 } else { 1918 } else {
1819 /* No match, output a literal byte */ 1919 /* No match, output a literal byte */
1820 Tracevv((stderr,"%c", s->window[s->strstart])); 1920 Tracevv((stderr,"%c", s->window[s->strstart]));
1821 _tr_tally_lit (s, s->window[s->strstart], bflush); 1921 _tr_tally_lit (s, s->window[s->strstart], bflush);
1822 s->lookahead--; 1922 s->lookahead--;
1823 s->strstart++; 1923 s->strstart++;
1824 } 1924 }
1825 if (bflush) FLUSH_BLOCK(s, 0); 1925 if (bflush) FLUSH_BLOCK(s, 0);
1826 } 1926 }
1827 FLUSH_BLOCK(s, flush == Z_FINISH); 1927 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1828 return flush == Z_FINISH ? finish_done : block_done; 1928 if (flush == Z_FINISH) {
1929 FLUSH_BLOCK(s, 1);
1930 return finish_done;
1931 }
1932 if (s->last_lit)
1933 FLUSH_BLOCK(s, 0);
1934 return block_done;
1829 } 1935 }
1830 1936
1831 #ifndef FASTEST 1937 #ifndef FASTEST
1832 /* =========================================================================== 1938 /* ===========================================================================
1833 * Same as above, but achieves better compression. We use a lazy 1939 * Same as above, but achieves better compression. We use a lazy
1834 * evaluation for matches: a match is finally adopted only if there is 1940 * evaluation for matches: a match is finally adopted only if there is
1835 * no better match at the next window position. 1941 * no better match at the next window position.
1836 */ 1942 */
1837 local block_state deflate_slow(s, flush, clas) 1943 local block_state deflate_slow(s, flush, clas)
1838 deflate_state *s; 1944 deflate_state *s;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 s->strstart++; 2079 s->strstart++;
1974 s->lookahead--; 2080 s->lookahead--;
1975 } 2081 }
1976 } 2082 }
1977 Assert (flush != Z_NO_FLUSH, "no flush?"); 2083 Assert (flush != Z_NO_FLUSH, "no flush?");
1978 if (s->match_available) { 2084 if (s->match_available) {
1979 Tracevv((stderr,"%c", s->window[s->strstart-1])); 2085 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1980 _tr_tally_lit(s, s->window[s->strstart-1], bflush); 2086 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1981 s->match_available = 0; 2087 s->match_available = 0;
1982 } 2088 }
1983 FLUSH_BLOCK(s, flush == Z_FINISH); 2089 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1984 return flush == Z_FINISH ? finish_done : block_done; 2090 if (flush == Z_FINISH) {
2091 FLUSH_BLOCK(s, 1);
2092 return finish_done;
2093 }
2094 if (s->last_lit)
2095 FLUSH_BLOCK(s, 0);
2096 return block_done;
1985 } 2097 }
1986 #endif /* FASTEST */ 2098 #endif /* FASTEST */
1987 2099
1988 /* =========================================================================== 2100 /* ===========================================================================
1989 * For Z_RLE, simply look for runs of bytes, generate matches only of distance 2101 * 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 2102 * one. Do not maintain a hash table. (It will be regenerated if this run of
1991 * deflate switches away from Z_RLE.) 2103 * deflate switches away from Z_RLE.)
1992 */ 2104 */
1993 local block_state deflate_rle(s, flush) 2105 local block_state deflate_rle(s, flush)
1994 deflate_state *s; 2106 deflate_state *s;
1995 int flush; 2107 int flush;
1996 { 2108 {
1997 int bflush; /* set if current block must be flushed */ 2109 int bflush; /* set if current block must be flushed */
1998 uInt prev; /* byte at distance one to match */ 2110 uInt prev; /* byte at distance one to match */
1999 Bytef *scan, *strend; /* scan goes up to strend for length of run */ 2111 Bytef *scan, *strend; /* scan goes up to strend for length of run */
2000 2112
2001 for (;;) { 2113 for (;;) {
2002 /* Make sure that we always have enough lookahead, except 2114 /* Make sure that we always have enough lookahead, except
2003 * at the end of the input file. We need MAX_MATCH bytes 2115 * at the end of the input file. We need MAX_MATCH bytes
2004 * for the longest encodable run. 2116 * for the longest run, plus one for the unrolled loop.
2005 */ 2117 */
2006 if (s->lookahead < MAX_MATCH) { 2118 if (s->lookahead <= MAX_MATCH) {
2007 fill_window(s); 2119 fill_window(s);
2008 if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) { 2120 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
2009 return need_more; 2121 return need_more;
2010 } 2122 }
2011 if (s->lookahead == 0) break; /* flush the current block */ 2123 if (s->lookahead == 0) break; /* flush the current block */
2012 } 2124 }
2013 2125
2014 /* See how many times the previous byte repeats */ 2126 /* See how many times the previous byte repeats */
2015 s->match_length = 0; 2127 s->match_length = 0;
2016 if (s->lookahead >= MIN_MATCH && s->strstart > 0) { 2128 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
2017 scan = s->window + s->strstart - 1; 2129 scan = s->window + s->strstart - 1;
2018 prev = *scan; 2130 prev = *scan;
2019 if (prev == *++scan && prev == *++scan && prev == *++scan) { 2131 if (prev == *++scan && prev == *++scan && prev == *++scan) {
2020 strend = s->window + s->strstart + MAX_MATCH; 2132 strend = s->window + s->strstart + MAX_MATCH;
2021 do { 2133 do {
2022 } while (prev == *++scan && prev == *++scan && 2134 } while (prev == *++scan && prev == *++scan &&
2023 prev == *++scan && prev == *++scan && 2135 prev == *++scan && prev == *++scan &&
2024 prev == *++scan && prev == *++scan && 2136 prev == *++scan && prev == *++scan &&
2025 prev == *++scan && prev == *++scan && 2137 prev == *++scan && prev == *++scan &&
2026 scan < strend); 2138 scan < strend);
2027 s->match_length = MAX_MATCH - (int)(strend - scan); 2139 s->match_length = MAX_MATCH - (int)(strend - scan);
2028 if (s->match_length > s->lookahead) 2140 if (s->match_length > s->lookahead)
2029 s->match_length = s->lookahead; 2141 s->match_length = s->lookahead;
2030 } 2142 }
2143 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
2031 } 2144 }
2032 2145
2033 /* Emit match if have run of MIN_MATCH or longer, else emit literal */ 2146 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2034 if (s->match_length >= MIN_MATCH) { 2147 if (s->match_length >= MIN_MATCH) {
2035 check_match(s, s->strstart, s->strstart - 1, s->match_length); 2148 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2036 2149
2037 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); 2150 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
2038 2151
2039 s->lookahead -= s->match_length; 2152 s->lookahead -= s->match_length;
2040 s->strstart += s->match_length; 2153 s->strstart += s->match_length;
2041 s->match_length = 0; 2154 s->match_length = 0;
2042 } else { 2155 } else {
2043 /* No match, output a literal byte */ 2156 /* No match, output a literal byte */
2044 Tracevv((stderr,"%c", s->window[s->strstart])); 2157 Tracevv((stderr,"%c", s->window[s->strstart]));
2045 _tr_tally_lit (s, s->window[s->strstart], bflush); 2158 _tr_tally_lit (s, s->window[s->strstart], bflush);
2046 s->lookahead--; 2159 s->lookahead--;
2047 s->strstart++; 2160 s->strstart++;
2048 } 2161 }
2049 if (bflush) FLUSH_BLOCK(s, 0); 2162 if (bflush) FLUSH_BLOCK(s, 0);
2050 } 2163 }
2051 FLUSH_BLOCK(s, flush == Z_FINISH); 2164 s->insert = 0;
2052 return flush == Z_FINISH ? finish_done : block_done; 2165 if (flush == Z_FINISH) {
2166 FLUSH_BLOCK(s, 1);
2167 return finish_done;
2168 }
2169 if (s->last_lit)
2170 FLUSH_BLOCK(s, 0);
2171 return block_done;
2053 } 2172 }
2054 2173
2055 /* =========================================================================== 2174 /* ===========================================================================
2056 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. 2175 * 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.) 2176 * (It will be regenerated if this run of deflate switches away from Huffman.)
2058 */ 2177 */
2059 local block_state deflate_huff(s, flush) 2178 local block_state deflate_huff(s, flush)
2060 deflate_state *s; 2179 deflate_state *s;
2061 int flush; 2180 int flush;
2062 { 2181 {
(...skipping 11 matching lines...) Expand all
2074 } 2193 }
2075 2194
2076 /* Output a literal byte */ 2195 /* Output a literal byte */
2077 s->match_length = 0; 2196 s->match_length = 0;
2078 Tracevv((stderr,"%c", s->window[s->strstart])); 2197 Tracevv((stderr,"%c", s->window[s->strstart]));
2079 _tr_tally_lit (s, s->window[s->strstart], bflush); 2198 _tr_tally_lit (s, s->window[s->strstart], bflush);
2080 s->lookahead--; 2199 s->lookahead--;
2081 s->strstart++; 2200 s->strstart++;
2082 if (bflush) FLUSH_BLOCK(s, 0); 2201 if (bflush) FLUSH_BLOCK(s, 0);
2083 } 2202 }
2084 FLUSH_BLOCK(s, flush == Z_FINISH); 2203 s->insert = 0;
2085 return flush == Z_FINISH ? finish_done : block_done; 2204 if (flush == Z_FINISH) {
2205 FLUSH_BLOCK(s, 1);
2206 return finish_done;
2207 }
2208 if (s->last_lit)
2209 FLUSH_BLOCK(s, 0);
2210 return block_done;
2086 } 2211 }
2087 2212
2088 /* Safe to inline this as GCC/clang will use inline asm and Visual Studio will 2213 /* Safe to inline this as GCC/clang will use inline asm and Visual Studio will
2089 * use intrinsic without extra params 2214 * use intrinsic without extra params
2090 */ 2215 */
2091 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str) 2216 local INLINE Pos insert_string_sse(deflate_state *const s, const Pos str)
2092 { 2217 {
2093 Pos ret; 2218 Pos ret;
2094 unsigned *ip, val, h = 0; 2219 unsigned *ip, val, h = 0;
2095 2220
(...skipping 15 matching lines...) Expand all
2111 #else 2236 #else
2112 /* This should never happen */ 2237 /* This should never happen */
2113 assert(0); 2238 assert(0);
2114 #endif 2239 #endif
2115 2240
2116 ret = s->head[h & s->hash_mask]; 2241 ret = s->head[h & s->hash_mask];
2117 s->head[h & s->hash_mask] = str; 2242 s->head[h & s->hash_mask] = str;
2118 s->prev[str & s->w_mask] = ret; 2243 s->prev[str & s->w_mask] = ret;
2119 return ret; 2244 return ret;
2120 } 2245 }
OLDNEW
« no previous file with comments | « third_party/zlib/deflate.h ('k') | third_party/zlib/google.patch » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698