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