55 " deflate 1.2.2 Copyright 1995-2004 Jean-loup Gailly ";
88 void match_init
OF((
void));
109 # define TOO_FAR 4096
113 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
160 #ifndef NO_DUMMY_DECL
170 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
184 #define INSERT_STRING(s, str, match_head) \
185 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
186 match_head = s->head[s->ins_h], \
187 s->head[s->ins_h] = (Pos)(str))
189 #define INSERT_STRING(s, str, match_head) \
190 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
191 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
192 s->head[s->ins_h] = (Pos)(str))
199 #define CLEAR_HASH(s) \
200 s->head[s->hash_size-1] = NIL; \
201 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
213 const char *
version,
int stream_size)
224 if (version ==
Z_NULL || version[0] != my_version[0] ||
231 if (strm->zalloc == (alloc_func)0) {
235 if (strm->zfree == (free_func)0) strm->zfree =
zcfree;
238 if (level != 0) level = 1;
243 if (windowBits < 0) {
245 windowBits = -windowBits;
248 else if (windowBits > 15) {
254 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
255 strategy < 0 || strategy >
Z_RLE) {
258 if (windowBits == 8) windowBits = 9;
305 uInt length = dictLength;
310 strm->state->wrap == 2 ||
311 (strm->state->wrap == 1 && strm->state->status !=
INIT_STATE))
316 strm->adler =
adler32(strm->adler, dictionary, dictLength);
321 #ifndef USE_DICT_HEAD
322 dictionary += dictLength - length;
335 for (n = 0; n <= length -
MIN_MATCH; n++) {
338 if (hash_head) hash_head = 0;
348 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
352 strm->total_in = strm->total_out = 0;
381 strm->state->bi_valid = bits;
382 strm->state->bi_buf = (
ush)(value & ((1 << bits) - 1));
397 if (level != 0) level = 1;
401 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_RLE) {
404 func = configuration_table[s->
level].func;
406 if (func != configuration_table[level].func && strm->total_in != 0) {
410 if (s->
level != level) {
444 destLen = sourceLen +
445 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
479 unsigned len = strm->state->pending;
481 if (len > strm->avail_out) len = strm->avail_out;
482 if (len == 0)
return;
484 zmemcpy(strm->next_out, strm->state->pending_out, len);
485 strm->next_out +=
len;
486 strm->state->pending_out +=
len;
487 strm->total_out +=
len;
488 strm->avail_out -=
len;
489 strm->state->pending -=
len;
490 if (strm->state->pending == 0) {
491 strm->state->pending_out = strm->state->pending_buf;
507 if (strm->next_out ==
Z_NULL ||
508 (strm->next_in ==
Z_NULL && strm->avail_in != 0) ||
545 else if (s->
level < 6)
547 else if (s->
level == 6)
551 header |= (level_flags << 6);
553 header += 31 - (header % 31);
570 if (strm->avail_out == 0) {
585 }
else if (strm->avail_in == 0 && flush <= old_flush &&
597 if (strm->avail_in != 0 || s->
lookahead != 0 ||
601 bstate = (*(configuration_table[s->
level].func))(
s, flush);
607 if (strm->avail_out == 0) {
632 if (strm->avail_out == 0) {
638 Assert(strm->avail_out > 0, (
char*)
"bug2");
676 status = strm->state->status;
683 TRY_FREE(strm, strm->state->pending_buf);
686 TRY_FREE(strm, strm->state->window);
688 ZFREE(strm, strm->state);
761 unsigned len = strm->avail_in;
763 if (len > size) len = size;
764 if (len == 0)
return 0;
766 strm->avail_in -=
len;
768 if (strm->state->wrap == 1) {
769 strm->adler =
adler32(strm->adler, strm->next_in, len);
772 else if (strm->state->wrap == 2) {
773 strm->adler =
crc32(strm->adler, strm->next_in, len);
776 zmemcpy(buf, strm->next_in, len);
777 strm->next_in +=
len;
778 strm->total_in +=
len;
828 register Bytef *match;
845 register ush scan_start = *(
ushf*)scan;
846 register ush scan_end = *(
ushf*)(scan+best_len-1);
849 register Byte scan_end1 = scan[best_len-1];
850 register Byte scan_end = scan[best_len];
871 match = s->
window + cur_match;
876 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
880 if (*(
ushf*)(match+best_len-1) != scan_end ||
881 *(
ushf*)match != scan_start)
continue;
892 Assert(scan[2] == match[2], (
char*)
"scan[2]?");
895 }
while (*(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
896 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
897 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
898 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
904 if (*scan == *match) scan++;
906 len = (
MAX_MATCH - 1) - (
int)(strend-scan);
911 if (match[best_len] != scan_end ||
912 match[best_len-1] != scan_end1 ||
914 *++match != scan[1])
continue;
923 Assert(*scan == *match, (
char*)
"match[2]?");
929 }
while (*++scan == *++match && *++scan == *++match &&
930 *++scan == *++match && *++scan == *++match &&
931 *++scan == *++match && *++scan == *++match &&
932 *++scan == *++match && *++scan == *++match &&
942 if (len > best_len) {
945 if (len >= nice_match)
break;
947 scan_end = *(
ushf*)(scan+best_len-1);
949 scan_end1 = scan[best_len-1];
950 scan_end = scan[best_len];
953 }
while ((cur_match = prev[cur_match & wmask]) > limit
954 && --chain_length != 0);
968 register Bytef *match;
981 match = s->
window + cur_match;
985 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
993 scan += 2, match += 2;
994 Assert(*scan == *match, (
char*)
"match[2]?");
1000 }
while (*++scan == *++match && *++scan == *++match &&
1001 *++scan == *++match && *++scan == *++match &&
1002 *++scan == *++match && *++scan == *++match &&
1003 *++scan == *++match && *++scan == *++match &&
1025 fprintf(stderr, (
char*)
" start %u, match %u, length %d\n",
1026 start, match, length);
1028 fprintf(stderr, (
char*)
"%c%c", s->
window[match++], s->
window[start++]);
1029 }
while (--length != 0);
1030 z_error((
char*)
"invalid match");
1032 if (z_verbose > 1) {
1033 fprintf(stderr,(
char*)
"\\[%d,%d]", start-match, length);
1034 do { putc(s->
window[start++], stderr); }
while (--length != 0);
1038 # define check_match(s, start, match, length)
1053 register unsigned n,
m;
1062 if (
sizeof(
int) <= 2) {
1066 }
else if (more == (
unsigned)(-1)) {
1094 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1102 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1110 if (s->
strm->avail_in == 0)
return;
1123 Assert(more >= 2, (
char*)
"more < 2");
1147 #define FLUSH_BLOCK_ONLY(s, eof) { \
1148 _tr_flush_block(s, (s->block_start >= 0L ? \
1149 (charf *)&s->window[(unsigned)s->block_start] : \
1151 (ulg)((long)s->strstart - s->block_start), \
1153 s->block_start = s->strstart; \
1154 flush_pending(s->strm); \
1155 Tracev((stderr,(char*)"[FLUSH]")); \
1159 #define FLUSH_BLOCK(s, eof) { \
1160 FLUSH_BLOCK_ONLY(s, eof); \
1161 if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
1178 ulg max_block_size = 0xffff;