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

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

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