| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#include "inflate64.h" |
| 53 |
#include "deflate.h" |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
typedef enum { |
| 59 |
need_more, |
| 60 |
block_done, |
| 61 |
finish_started, |
| 62 |
finish_done |
| 63 |
} block_state; |
| 64 |
|
| 65 |
local int deflateStateCheck OF((z_streamp strm)); |
| 66 |
local void slide_hash OF((deflate_state *s)); |
| 67 |
local void fill_window OF((deflate_state *s)); |
| 68 |
local block_state deflate9_ OF((deflate_state *s, int flush)); |
| 69 |
local void lm_init OF((deflate_state *s)); |
| 70 |
local void flush_pending OF((z_streamp strm)); |
| 71 |
local unsigned read_buf OF((z_streamp strm, Byte FAR *buf, unsigned size)); |
| 72 |
local uInt longest_match OF((deflate_state *s, IPos cur_match)); |
| 73 |
|
| 74 |
#ifdef ZLIB_DEBUG |
| 75 |
local void check_match OF((deflate_state *s, IPos start, IPos match, int length)); |
| 76 |
#endif |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#define NIL 0 |
| 83 |
|
| 84 |
|
| 85 |
#ifndef TOO_FAR |
| 86 |
# define TOO_FAR 4096 |
| 87 |
#endif |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#define INSERT_STRING(s, str, match_head) \ |
| 116 |
(UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ |
| 117 |
match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ |
| 118 |
s->head[s->ins_h] = (Pos)(str)) |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
#define CLEAR_HASH(s) \ |
| 125 |
do { \ |
| 126 |
s->head[s->hash_size-1] = NIL; \ |
| 127 |
zmemzero((Byte FAR *)s->head, \ |
| 128 |
(unsigned)(s->hash_size-1)*sizeof(*s->head)); \ |
| 129 |
} while (0) |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
local void slide_hash(s) |
| 137 |
deflate_state *s; |
| 138 |
{ |
| 139 |
unsigned n, m; |
| 140 |
Posf *p; |
| 141 |
uInt wsize = s->w_size; |
| 142 |
|
| 143 |
n = s->hash_size; |
| 144 |
p = &s->head[n]; |
| 145 |
do { |
| 146 |
m = *--p; |
| 147 |
*p = (Pos)(m >= wsize ? m - wsize : NIL); |
| 148 |
} while (--n); |
| 149 |
n = wsize; |
| 150 |
p = &s->prev[n]; |
| 151 |
do { |
| 152 |
m = *--p; |
| 153 |
*p = (Pos)(m >= wsize ? m - wsize : NIL); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
} while (--n); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
int ZEXPORT deflate9Init2(strm) |
| 162 |
z_streamp strm; |
| 163 |
{ |
| 164 |
deflate_state *s; |
| 165 |
|
| 166 |
if (strm == Z_NULL) return Z_STREAM_ERROR; |
| 167 |
|
| 168 |
strm->msg = Z_NULL; |
| 169 |
if (strm->zalloc == (alloc_func)0) { |
| 170 |
return Z_STREAM_ERROR; |
| 171 |
} |
| 172 |
if (strm->zfree == (free_func)0) { |
| 173 |
return Z_STREAM_ERROR; |
| 174 |
} |
| 175 |
|
| 176 |
s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); |
| 177 |
if (s == Z_NULL) return Z_MEM_ERROR; |
| 178 |
strm->state = (struct internal_state FAR *)s; |
| 179 |
s->strm = strm; |
| 180 |
s->status = INIT_STATE; |
| 181 |
|
| 182 |
s->w_bits = WBITS64; |
| 183 |
s->w_size = 1 << s->w_bits; |
| 184 |
s->w_mask = s->w_size - 1; |
| 185 |
|
| 186 |
s->hash_bits = MEM_LEVEL64 + 7; |
| 187 |
s->hash_size = 1 << s->hash_bits; |
| 188 |
s->hash_mask = s->hash_size - 1; |
| 189 |
s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); |
| 190 |
|
| 191 |
s->window = (Byte FAR *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); |
| 192 |
s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); |
| 193 |
s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); |
| 194 |
|
| 195 |
s->high_water = 0; |
| 196 |
|
| 197 |
s->lit_bufsize = 1 << (s->hash_bits - 1); |
| 198 |
|
| 199 |
s->pending_buf = (unsigned char FAR *) ZALLOC(strm, s->lit_bufsize, 4); |
| 200 |
s->pending_buf_size = (unsigned long)s->lit_bufsize * 4; |
| 201 |
s->sym_buf = (unsigned char FAR *) ZALLOC(strm, s->lit_bufsize, 4); |
| 202 |
s->sym_end = s->lit_bufsize * 4; |
| 203 |
|
| 204 |
if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || |
| 205 |
s->pending_buf == Z_NULL || s->sym_buf == Z_NULL) { |
| 206 |
s->status = FINISH_STATE; |
| 207 |
strm->msg = ERR_MSG(Z_MEM_ERROR); |
| 208 |
deflate9End (strm); |
| 209 |
return Z_MEM_ERROR; |
| 210 |
} |
| 211 |
return deflate9Reset(strm); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
local int deflateStateCheck (strm) |
| 218 |
z_streamp strm; |
| 219 |
{ |
| 220 |
deflate_state *s; |
| 221 |
if (strm == Z_NULL || |
| 222 |
strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) |
| 223 |
return 1; |
| 224 |
s = strm->state; |
| 225 |
if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && |
| 226 |
s->status != BUSY_STATE && |
| 227 |
s->status != FINISH_STATE)) |
| 228 |
return 1; |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
int ZEXPORT deflate9ResetKeep (strm) |
| 234 |
z_streamp strm; |
| 235 |
{ |
| 236 |
deflate_state *s; |
| 237 |
|
| 238 |
if (deflateStateCheck(strm)) { |
| 239 |
return Z_STREAM_ERROR; |
| 240 |
} |
| 241 |
|
| 242 |
strm->total_in = strm->total_out = 0; |
| 243 |
strm->msg = Z_NULL; |
| 244 |
strm->data_type = Z_UNKNOWN; |
| 245 |
|
| 246 |
s = (deflate_state *)strm->state; |
| 247 |
s->pending = 0; |
| 248 |
s->pending_out = s->pending_buf; |
| 249 |
|
| 250 |
s->status = INIT_STATE; |
| 251 |
s->last_flush = -2; |
| 252 |
|
| 253 |
_tr_init(s); |
| 254 |
|
| 255 |
return Z_OK; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
int ZEXPORT deflate9Reset (strm) |
| 260 |
z_streamp strm; |
| 261 |
{ |
| 262 |
int ret = deflate9ResetKeep(strm); |
| 263 |
if (ret == Z_OK) { |
| 264 |
lm_init(strm->state); |
| 265 |
} |
| 266 |
return ret; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
local void flush_pending(strm) |
| 276 |
z_streamp strm; |
| 277 |
{ |
| 278 |
unsigned len; |
| 279 |
deflate_state *s = strm->state; |
| 280 |
|
| 281 |
_tr_flush_bits(s); |
| 282 |
len = s->pending; |
| 283 |
if (len > strm->avail_out) { |
| 284 |
len = strm->avail_out; |
| 285 |
} |
| 286 |
if (len == 0) { |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
zmemcpy(strm->next_out, s->pending_out, len); |
| 291 |
strm->next_out += len; |
| 292 |
s->pending_out += len; |
| 293 |
strm->total_out += len; |
| 294 |
strm->avail_out -= len; |
| 295 |
s->pending -= len; |
| 296 |
if (s->pending == 0) { |
| 297 |
s->pending_out = s->pending_buf; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
int ZEXPORT deflate9 (strm, flush) |
| 303 |
z_streamp strm; |
| 304 |
int flush; |
| 305 |
{ |
| 306 |
int old_flush; |
| 307 |
deflate_state *s; |
| 308 |
|
| 309 |
|
| 310 |
if (deflateStateCheck(strm) || (flush != Z_FINISH && flush != Z_NO_FLUSH)) { |
| 311 |
return Z_STREAM_ERROR; |
| 312 |
} |
| 313 |
s = strm->state; |
| 314 |
|
| 315 |
if (strm->next_out == Z_NULL || |
| 316 |
(strm->avail_in != 0 && strm->next_in == Z_NULL) || |
| 317 |
(s->status == FINISH_STATE && flush != Z_FINISH)) { |
| 318 |
ERR_RETURN(strm, Z_STREAM_ERROR); |
| 319 |
} |
| 320 |
if (strm->avail_out == 0) { ERR_RETURN(strm, Z_BUF_ERROR); } |
| 321 |
|
| 322 |
old_flush = s->last_flush; |
| 323 |
s->last_flush = flush; |
| 324 |
|
| 325 |
|
| 326 |
if (s->pending != 0) { |
| 327 |
flush_pending(strm); |
| 328 |
if (strm->avail_out == 0) { |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
s->last_flush = -1; |
| 336 |
return Z_OK; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
} else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && flush != Z_FINISH) { |
| 344 |
ERR_RETURN(strm, Z_BUF_ERROR); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
if (s->status == FINISH_STATE && strm->avail_in != 0) { |
| 349 |
ERR_RETURN(strm, Z_BUF_ERROR); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
if (s->status == INIT_STATE) { s->status = BUSY_STATE; } |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
if (strm->avail_in != 0 || s->lookahead != 0 || |
| 358 |
(flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { |
| 359 |
block_state bstate; |
| 360 |
|
| 361 |
bstate = deflate9_(s, flush); |
| 362 |
|
| 363 |
if (bstate == finish_started || bstate == finish_done) { |
| 364 |
s->status = FINISH_STATE; |
| 365 |
} |
| 366 |
if (bstate == need_more || bstate == finish_started) { |
| 367 |
if (strm->avail_out == 0) { |
| 368 |
s->last_flush = -1; |
| 369 |
} |
| 370 |
return Z_OK; |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
} |
| 379 |
if (bstate == block_done) { |
| 380 |
flush_pending(strm); |
| 381 |
if (strm->avail_out == 0) { |
| 382 |
s->last_flush = -1; |
| 383 |
return Z_OK; |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
if (flush != Z_FINISH) return Z_OK; |
| 389 |
return Z_STREAM_END; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
int ZEXPORT deflate9End (z_streamp strm) { |
| 394 |
int status; |
| 395 |
|
| 396 |
if (deflateStateCheck(strm)) return Z_STREAM_ERROR; |
| 397 |
|
| 398 |
status = strm->state->status; |
| 399 |
|
| 400 |
|
| 401 |
TRY_FREE(strm, strm->state->sym_buf); |
| 402 |
TRY_FREE(strm, strm->state->pending_buf); |
| 403 |
TRY_FREE(strm, strm->state->head); |
| 404 |
TRY_FREE(strm, strm->state->prev); |
| 405 |
TRY_FREE(strm, strm->state->window); |
| 406 |
|
| 407 |
ZFREE(strm, strm->state); |
| 408 |
strm->state = Z_NULL; |
| 409 |
|
| 410 |
return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
local unsigned read_buf(strm, buf, size) |
| 421 |
z_streamp strm; |
| 422 |
Byte FAR *buf; |
| 423 |
unsigned size; |
| 424 |
{ |
| 425 |
unsigned len = strm->avail_in; |
| 426 |
|
| 427 |
if (len > size) len = size; |
| 428 |
if (len == 0) return 0; |
| 429 |
|
| 430 |
strm->avail_in -= len; |
| 431 |
|
| 432 |
zmemcpy(buf, strm->next_in, len); |
| 433 |
strm->next_in += len; |
| 434 |
strm->total_in += len; |
| 435 |
|
| 436 |
return len; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
local void lm_init (s) |
| 443 |
deflate_state *s; |
| 444 |
{ |
| 445 |
s->window_size = (unsigned long) 2L * s->w_size; |
| 446 |
|
| 447 |
CLEAR_HASH(s); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
s->max_lazy_match = 258; |
| 452 |
s->good_match = 32; |
| 453 |
s->nice_match = 258; |
| 454 |
s->max_chain_length = 4096; |
| 455 |
|
| 456 |
s->strstart = 0; |
| 457 |
s->block_start = 0L; |
| 458 |
s->lookahead = 0; |
| 459 |
s->insert = 0; |
| 460 |
s->match_length = s->prev_length = MIN_MATCH - 1; |
| 461 |
s->match_available = 0; |
| 462 |
s->ins_h = 0; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
local uInt longest_match(s, cur_match) |
| 478 |
deflate_state *s; |
| 479 |
IPos cur_match; |
| 480 |
{ |
| 481 |
unsigned chain_length = s->max_chain_length; |
| 482 |
register Byte FAR *scan = s->window + s->strstart; |
| 483 |
register Byte FAR *match; |
| 484 |
register int len; |
| 485 |
int best_len = (int)s->prev_length; |
| 486 |
int nice_match = s->nice_match; |
| 487 |
IPos limit = s->strstart > (IPos)MAX_DIST(s) ? |
| 488 |
s->strstart - (IPos)MAX_DIST(s) : NIL; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
Posf *prev = s->prev; |
| 493 |
uInt wmask = s->w_mask; |
| 494 |
register Byte FAR *strend = s->window + s->strstart + MAX_MATCH; |
| 495 |
register Byte scan_end1 = scan[best_len-1]; |
| 496 |
register Byte scan_end = scan[best_len]; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
if (s->prev_length >= s->good_match) { |
| 505 |
chain_length >>= 2; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
if ((uInt) nice_match > s->lookahead) { |
| 511 |
nice_match = (int) s->lookahead; |
| 512 |
} |
| 513 |
|
| 514 |
Assert((unsigned long)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); |
| 515 |
|
| 516 |
do { |
| 517 |
Assert(cur_match < s->strstart, "no future"); |
| 518 |
match = s->window + cur_match; |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
if (match[best_len] != scan_end || |
| 529 |
match[best_len-1] != scan_end1 || |
| 530 |
*match != *scan || |
| 531 |
*++match != scan[1]) continue; |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
scan += 2, match++; |
| 540 |
Assert(*scan == *match, "match[2]?"); |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
do { |
| 546 |
} while (*++scan == *++match && *++scan == *++match && |
| 547 |
*++scan == *++match && *++scan == *++match && |
| 548 |
*++scan == *++match && *++scan == *++match && |
| 549 |
*++scan == *++match && *++scan == *++match && |
| 550 |
scan < strend); |
| 551 |
|
| 552 |
Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); |
| 553 |
|
| 554 |
len = MAX_MATCH - (int)(strend - scan); |
| 555 |
scan = strend - MAX_MATCH; |
| 556 |
|
| 557 |
if (len > best_len) { |
| 558 |
s->match_start = cur_match; |
| 559 |
best_len = len; |
| 560 |
if (len >= nice_match) { |
| 561 |
break; |
| 562 |
} |
| 563 |
scan_end1 = scan[best_len - 1]; |
| 564 |
scan_end = scan[best_len]; |
| 565 |
} |
| 566 |
} while ((cur_match = prev[cur_match & wmask]) > limit |
| 567 |
&& --chain_length != 0); |
| 568 |
|
| 569 |
if ((uInt) best_len <= s->lookahead) { |
| 570 |
return (uInt) best_len; |
| 571 |
} |
| 572 |
return s->lookahead; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
#ifdef ZLIB_DEBUG |
| 577 |
|
| 578 |
#define EQUAL 0 |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
local void check_match(s, start, match, length) |
| 585 |
deflate_state *s; |
| 586 |
IPos start, match; |
| 587 |
int length; |
| 588 |
{ |
| 589 |
|
| 590 |
if (zmemcmp(s->window + match, |
| 591 |
s->window + start, length) != EQUAL) { |
| 592 |
fprintf(stderr, " start %u, match %u, length %d\n", |
| 593 |
start, match, length); |
| 594 |
do { |
| 595 |
fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); |
| 596 |
} while (--length != 0); |
| 597 |
z_error("invalid match"); |
| 598 |
} |
| 599 |
if (z_verbose > 1) { |
| 600 |
fprintf(stderr,"\\[%d,%d]", start-match, length); |
| 601 |
do { putc(s->window[start++], stderr); } while (--length != 0); |
| 602 |
} |
| 603 |
} |
| 604 |
#else |
| 605 |
# define check_match(s, start, match, length) |
| 606 |
#endif |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
local void fill_window(s) |
| 619 |
deflate_state *s; |
| 620 |
{ |
| 621 |
unsigned n; |
| 622 |
unsigned more; |
| 623 |
uInt wsize = s->w_size; |
| 624 |
|
| 625 |
Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); |
| 626 |
|
| 627 |
do { |
| 628 |
more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); |
| 629 |
|
| 630 |
|
| 631 |
if (sizeof(int) <= 2) { |
| 632 |
if (more == 0 && s->strstart == 0 && s->lookahead == 0) { |
| 633 |
more = wsize; |
| 634 |
|
| 635 |
} else if (more == (unsigned)(-1)) { |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
more--; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
if (s->strstart >= wsize+MAX_DIST(s)) { |
| 647 |
|
| 648 |
zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more); |
| 649 |
s->match_start -= wsize; |
| 650 |
s->strstart -= wsize; |
| 651 |
s->block_start -= (long) wsize; |
| 652 |
if (s->insert > s->strstart) { |
| 653 |
s->insert = s->strstart; |
| 654 |
} |
| 655 |
slide_hash(s); |
| 656 |
more += wsize; |
| 657 |
} |
| 658 |
if (s->strm->avail_in == 0) break; |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
Assert(more >= 2, "more < 2"); |
| 672 |
|
| 673 |
n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); |
| 674 |
s->lookahead += n; |
| 675 |
|
| 676 |
|
| 677 |
if (s->lookahead + s->insert >= MIN_MATCH) { |
| 678 |
uInt str = s->strstart - s->insert; |
| 679 |
s->ins_h = s->window[str]; |
| 680 |
UPDATE_HASH(s, s->ins_h, s->window[str + 1]); |
| 681 |
#if MIN_MATCH != 3 |
| 682 |
Call UPDATE_HASH() MIN_MATCH-3 more times |
| 683 |
#endif |
| 684 |
while (s->insert) { |
| 685 |
UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH - 1]); |
| 686 |
s->prev[str & s->w_mask] = s->head[s->ins_h]; |
| 687 |
s->head[s->ins_h] = (Pos)str; |
| 688 |
str++; |
| 689 |
s->insert--; |
| 690 |
if (s->lookahead + s->insert < MIN_MATCH) |
| 691 |
break; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
} while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
if (s->high_water < s->window_size) { |
| 708 |
unsigned long curr = s->strstart + (ulg)(s->lookahead); |
| 709 |
unsigned long init; |
| 710 |
|
| 711 |
if (s->high_water < curr) { |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
init = s->window_size - curr; |
| 716 |
if (init > WIN_INIT) { |
| 717 |
init = WIN_INIT; |
| 718 |
} |
| 719 |
zmemzero(s->window + curr, (unsigned)init); |
| 720 |
s->high_water = curr + init; |
| 721 |
} |
| 722 |
else if (s->high_water < (ulg)curr + WIN_INIT) { |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
init = (unsigned long)curr + WIN_INIT - s->high_water; |
| 728 |
if (init > s->window_size - s->high_water) { |
| 729 |
init = s->window_size - s->high_water; |
| 730 |
} |
| 731 |
zmemzero(s->window + s->high_water, (unsigned)init); |
| 732 |
s->high_water += init; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
Assert((unsigned long)s->strstart <= s->window_size - MIN_LOOKAHEAD, |
| 737 |
"not enough room for search"); |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
#define FLUSH_BLOCK_ONLY(s, last) { \ |
| 745 |
_tr_flush_block(s, (s->block_start >= 0L ? \ |
| 746 |
(char FAR *)&s->window[(unsigned)s->block_start] : \ |
| 747 |
(char FAR *)Z_NULL), \ |
| 748 |
(unsigned long)((long)s->strstart - s->block_start), \ |
| 749 |
(last)); \ |
| 750 |
s->block_start = s->strstart; \ |
| 751 |
flush_pending(s->strm); \ |
| 752 |
Tracev((stderr,"[FLUSH]\n")); \ |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
local block_state deflate9_(s, flush) |
| 761 |
deflate_state *s; |
| 762 |
int flush; |
| 763 |
{ |
| 764 |
IPos hash_head; |
| 765 |
int bflush; |
| 766 |
|
| 767 |
|
| 768 |
for (;;) { |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
if (s->lookahead < MIN_LOOKAHEAD) { |
| 775 |
fill_window(s); |
| 776 |
if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { |
| 777 |
return need_more; |
| 778 |
} |
| 779 |
if (s->lookahead == 0) break; |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
hash_head = NIL; |
| 786 |
if (s->lookahead >= MIN_MATCH) { |
| 787 |
INSERT_STRING(s, s->strstart, hash_head); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
s->prev_length = s->match_length, s->prev_match = s->match_start; |
| 793 |
s->match_length = MIN_MATCH-1; |
| 794 |
|
| 795 |
if (hash_head != NIL && s->prev_length < s->max_lazy_match && |
| 796 |
s->strstart - hash_head <= MAX_DIST(s)) { |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
s->match_length = longest_match (s, hash_head); |
| 802 |
|
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { |
| 808 |
uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; |
| 809 |
|
| 810 |
|
| 811 |
check_match(s, s->strstart-1, s->prev_match, s->prev_length); |
| 812 |
|
| 813 |
bflush = _tr_tally(s, s->strstart -1 - s->prev_match, s->prev_length - MIN_MATCH); |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
s->lookahead -= s->prev_length-1; |
| 821 |
s->prev_length -= 2; |
| 822 |
do { |
| 823 |
if (++s->strstart <= max_insert) { |
| 824 |
INSERT_STRING(s, s->strstart, hash_head); |
| 825 |
} |
| 826 |
} while (--s->prev_length != 0); |
| 827 |
s->match_available = 0; |
| 828 |
s->match_length = MIN_MATCH-1; |
| 829 |
s->strstart++; |
| 830 |
|
| 831 |
if (bflush) { |
| 832 |
FLUSH_BLOCK_ONLY(s, 0); |
| 833 |
if (s->strm->avail_out == 0) { |
| 834 |
return need_more; |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
} else if (s->match_available) { |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
Tracevv((stderr,"%c", s->window[s->strstart-1])); |
| 844 |
bflush = _tr_tally(s, 0, s->window[s->strstart-1]); |
| 845 |
if (bflush) { |
| 846 |
FLUSH_BLOCK_ONLY(s, 0); |
| 847 |
} |
| 848 |
s->strstart++; |
| 849 |
s->lookahead--; |
| 850 |
if (s->strm->avail_out == 0) return need_more; |
| 851 |
} else { |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
s->match_available = 1; |
| 856 |
s->strstart++; |
| 857 |
s->lookahead--; |
| 858 |
} |
| 859 |
} |
| 860 |
Assert (flush != Z_NO_FLUSH, "no flush?"); |
| 861 |
if (s->match_available) { |
| 862 |
Tracevv((stderr,"%c", s->window[s->strstart-1])); |
| 863 |
bflush = _tr_tally(s, 0, s->window[s->strstart-1]); |
| 864 |
s->match_available = 0; |
| 865 |
} |
| 866 |
s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; |
| 867 |
if (flush == Z_FINISH) { |
| 868 |
FLUSH_BLOCK_ONLY(s, 1); |
| 869 |
if (s->strm->avail_out == 0) { |
| 870 |
return finish_started; |
| 871 |
} else { |
| 872 |
return finish_done; |
| 873 |
} |
| 874 |
} |
| 875 |
if (s->sym_next) { |
| 876 |
FLUSH_BLOCK_ONLY(s, 0); |
| 877 |
if (s->strm->avail_out == 0) { |
| 878 |
return need_more; |
| 879 |
} |
| 880 |
} |
| 881 |
return block_done; |
| 882 |
} |