89 void match_init
OF((
void));
108 # define TOO_FAR 4096
154 #ifndef NO_DUMMY_DECL
159 #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
167 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
181 #define INSERT_STRING(s, str, match_head) \
182 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
183 match_head = s->head[s->ins_h], \
184 s->head[s->ins_h] = (Pos)(str))
186 #define INSERT_STRING(s, str, match_head) \
187 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
188 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
189 s->head[s->ins_h] = (Pos)(str))
196 #define CLEAR_HASH(s) \
197 s->head[s->hash_size-1] = NIL; \
198 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
201 int ZEXPORT
deflateInit_(z_streamp strm,
int level,
const char *version,
int stream_size)
203 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
204 Z_DEFAULT_STRATEGY, version, stream_size);
209 int ZEXPORT
deflateInit2_(z_streamp strm,
int level,
int method,
int windowBits,
int memLevel,
int strategy,
210 const char *version,
int stream_size)
214 static const char my_version[] = ZLIB_VERSION;
221 if (version == Z_NULL || version[0] != my_version[0] ||
222 stream_size !=
sizeof(z_stream)) {
223 return Z_VERSION_ERROR;
225 if (strm == Z_NULL)
return Z_STREAM_ERROR;
228 if (strm->zalloc == (alloc_func)0) {
230 return Z_STREAM_ERROR;
233 strm->opaque = (voidpf)0;
236 if (strm->zfree == (free_func)0)
238 return Z_STREAM_ERROR;
244 if (level != 0) level = 1;
246 if (level == Z_DEFAULT_COMPRESSION) level = 6;
249 if (windowBits < 0) {
251 windowBits = -windowBits;
254 else if (windowBits > 15) {
259 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
260 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
261 strategy < 0 || strategy > Z_FIXED) {
262 return Z_STREAM_ERROR;
264 if (windowBits == 8) windowBits = 9;
265 s = (deflate_state *) ZALLOC(strm, 1,
sizeof(deflate_state));
266 if (s == Z_NULL)
return Z_MEM_ERROR;
272 s->w_bits = windowBits;
273 s->w_size = 1 << s->w_bits;
274 s->w_mask = s->w_size - 1;
276 s->hash_bits = memLevel + 7;
277 s->hash_size = 1 << s->hash_bits;
278 s->hash_mask = s->hash_size - 1;
279 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
281 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*
sizeof(Byte));
282 s->prev = (Posf *) ZALLOC(strm, s->w_size,
sizeof(Pos));
283 s->head = (Posf *) ZALLOC(strm, s->hash_size,
sizeof(Pos));
287 s->lit_bufsize = 1 << (memLevel + 6);
289 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize,
sizeof(
ush)+2);
290 s->pending_buf = (uchf *) overlay;
291 s->pending_buf_size = (
ulg)s->lit_bufsize * (
sizeof(
ush)+2
L);
293 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
294 s->pending_buf == Z_NULL) {
295 s->status = FINISH_STATE;
296 strm->msg = ERR_MSG(Z_MEM_ERROR);
300 s->d_buf = overlay + s->lit_bufsize/
sizeof(
ush);
301 s->l_buf = s->pending_buf + (1+
sizeof(
ush))*s->lit_bufsize;
304 s->strategy = strategy;
305 s->method = (Byte)method;
317 z_const
unsigned char *next;
319 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
320 return Z_STREAM_ERROR;
323 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
324 return Z_STREAM_ERROR;
328 strm->adler =
adler32(strm->adler, dictionary, dictLength);
332 if (dictLength >= s->w_size) {
339 dictionary += dictLength - s->w_size;
340 dictLength = s->w_size;
344 avail = strm->avail_in;
345 next = strm->next_in;
346 strm->avail_in = dictLength;
347 strm->next_in = (z_const Bytef *)dictionary;
349 while (s->lookahead >= MIN_MATCH) {
351 n = s->lookahead - (MIN_MATCH-1);
353 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
355 s->prev[str & s->w_mask] = s->head[s->ins_h];
357 s->head[s->ins_h] = (Pos)str;
361 s->lookahead = MIN_MATCH-1;
364 s->strstart += s->lookahead;
365 s->block_start = (long)s->strstart;
366 s->insert = s->lookahead;
368 s->match_length = s->prev_length = MIN_MATCH-1;
369 s->match_available = 0;
370 strm->next_in = next;
371 strm->avail_in = avail;
381 if (strm == Z_NULL || strm->state == Z_NULL ||
382 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
383 return Z_STREAM_ERROR;
386 strm->total_in = strm->total_out = 0;
388 strm->data_type = Z_UNKNOWN;
390 s = (deflate_state *)strm->state;
392 s->pending_out = s->pending_buf;
397 s->status = s->wrap ? INIT_STATE : BUSY_STATE;
400 s->wrap == 2 ?
crc32(0
L, Z_NULL, 0) :
403 s->last_flush = Z_NO_FLUSH;
424 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
425 if (strm->state->wrap != 2)
return Z_STREAM_ERROR;
426 strm->state->gzhead = head;
433 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
434 if (pending != Z_NULL)
435 *pending = strm->state->pending;
437 *bits = strm->state->bi_valid;
447 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
449 if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
452 put = Buf_size - s->bi_valid;
455 s->bi_buf |= (
ush)((value & ((1 << put) - 1)) << s->bi_valid);
471 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
475 if (level != 0) level = 1;
477 if (level == Z_DEFAULT_COMPRESSION) level = 6;
479 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
480 return Z_STREAM_ERROR;
482 func = configuration_table[s->level].func;
484 if ((strategy != s->strategy || func != configuration_table[level].func) &&
485 strm->total_in != 0) {
488 if (err == Z_BUF_ERROR && s->pending == 0)
491 if (s->level != level) {
493 s->max_lazy_match = configuration_table[level].max_lazy;
494 s->good_match = configuration_table[level].good_length;
495 s->nice_match = configuration_table[level].nice_length;
496 s->max_chain_length = configuration_table[level].max_chain;
498 s->strategy = strategy;
503 int ZEXPORT
deflateTune(z_streamp strm,
int good_length,
int max_lazy,
int nice_length,
int max_chain)
507 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
509 s->good_match = good_length;
510 s->max_lazy_match = max_lazy;
511 s->nice_match = nice_length;
512 s->max_chain_length = max_chain;
536 uLong complen, wraplen;
540 complen = sourceLen +
541 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
544 if (strm == Z_NULL || strm->state == Z_NULL)
554 wraplen = 6 + (s->strstart ? 4 : 0);
558 if (s->gzhead != Z_NULL) {
559 if (s->gzhead->extra != Z_NULL)
560 wraplen += 2 + s->gzhead->extra_len;
561 str = s->gzhead->name;
566 str = s->gzhead->comment;
580 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
581 return complen + wraplen;
584 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
585 (sourceLen >> 25) + 13 - 6 + wraplen;
595 put_byte(s, (Byte)(b >> 8));
596 put_byte(s, (Byte)(b & 0xff));
608 deflate_state *s = strm->state;
612 if (len > strm->avail_out) len = strm->avail_out;
613 if (len == 0)
return;
615 zmemcpy(strm->next_out, s->pending_out, len);
616 strm->next_out += len;
617 s->pending_out += len;
618 strm->total_out += len;
619 strm->avail_out -= len;
621 if (s->pending == 0) {
622 s->pending_out = s->pending_buf;
627 int ZEXPORT
deflate (z_streamp strm,
int flush)
632 if (strm == Z_NULL || strm->state == Z_NULL ||
633 flush > Z_BLOCK || flush < 0) {
634 return Z_STREAM_ERROR;
638 if (strm->next_out == Z_NULL ||
639 (strm->next_in == Z_NULL && strm->avail_in != 0) ||
640 (s->status == FINISH_STATE && flush != Z_FINISH)) {
641 ERR_RETURN(strm, Z_STREAM_ERROR);
643 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
646 old_flush = s->last_flush;
647 s->last_flush = flush;
650 if (s->status == INIT_STATE) {
653 strm->adler =
crc32(0
L, Z_NULL, 0);
657 if (s->gzhead == Z_NULL) {
663 put_byte(s, s->level == 9 ? 2 :
664 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
666 put_byte(s, OS_CODE);
667 s->status = BUSY_STATE;
670 put_byte(s, (s->gzhead->text ? 1 : 0) +
671 (s->gzhead->hcrc ? 2 : 0) +
672 (s->gzhead->extra == Z_NULL ? 0 : 4) +
673 (s->gzhead->name == Z_NULL ? 0 : 8) +
674 (s->gzhead->comment == Z_NULL ? 0 : 16)
676 put_byte(s, (Byte)(s->gzhead->time & 0xff));
677 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
678 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
679 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
680 put_byte(s, s->level == 9 ? 2 :
681 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
683 put_byte(s, s->gzhead->os & 0xff);
684 if (s->gzhead->extra != Z_NULL) {
685 put_byte(s, s->gzhead->extra_len & 0xff);
686 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
689 strm->adler =
crc32(strm->adler, s->pending_buf,
692 s->status = EXTRA_STATE;
698 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
701 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
703 else if (s->level < 6)
705 else if (s->level == 6)
709 header |= (level_flags << 6);
710 if (s->strstart != 0) header |= PRESET_DICT;
711 header += 31 - (header % 31);
713 s->status = BUSY_STATE;
717 if (s->strstart != 0) {
721 strm->adler =
adler32(0
L, Z_NULL, 0);
725 if (s->status == EXTRA_STATE) {
726 if (s->gzhead->extra != Z_NULL) {
727 uInt beg = s->pending;
729 while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
730 if (s->pending == s->pending_buf_size) {
731 if (s->gzhead->hcrc && s->pending > beg)
732 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
736 if (s->pending == s->pending_buf_size)
739 put_byte(s, s->gzhead->extra[s->gzindex]);
742 if (s->gzhead->hcrc && s->pending > beg)
743 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
745 if (s->gzindex == s->gzhead->extra_len) {
747 s->status = NAME_STATE;
751 s->status = NAME_STATE;
753 if (s->status == NAME_STATE) {
754 if (s->gzhead->name != Z_NULL) {
755 uInt beg = s->pending;
759 if (s->pending == s->pending_buf_size) {
760 if (s->gzhead->hcrc && s->pending > beg)
761 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
765 if (s->pending == s->pending_buf_size) {
770 val = s->gzhead->name[s->gzindex++];
773 if (s->gzhead->hcrc && s->pending > beg)
774 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
778 s->status = COMMENT_STATE;
782 s->status = COMMENT_STATE;
784 if (s->status == COMMENT_STATE) {
785 if (s->gzhead->comment != Z_NULL) {
786 uInt beg = s->pending;
790 if (s->pending == s->pending_buf_size) {
791 if (s->gzhead->hcrc && s->pending > beg)
792 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
796 if (s->pending == s->pending_buf_size) {
801 val = s->gzhead->comment[s->gzindex++];
804 if (s->gzhead->hcrc && s->pending > beg)
805 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
808 s->status = HCRC_STATE;
811 s->status = HCRC_STATE;
813 if (s->status == HCRC_STATE) {
814 if (s->gzhead->hcrc) {
815 if (s->pending + 2 > s->pending_buf_size)
817 if (s->pending + 2 <= s->pending_buf_size) {
818 put_byte(s, (Byte)(strm->adler & 0xff));
819 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
820 strm->adler =
crc32(0
L, Z_NULL, 0);
821 s->status = BUSY_STATE;
825 s->status = BUSY_STATE;
830 if (s->pending != 0) {
832 if (strm->avail_out == 0) {
847 }
else if (strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
849 ERR_RETURN(strm, Z_BUF_ERROR);
853 if (s->status == FINISH_STATE && strm->avail_in != 0) {
854 ERR_RETURN(strm, Z_BUF_ERROR);
859 if (strm->avail_in != 0 || s->lookahead != 0 ||
860 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
863 bstate = s->strategy == Z_HUFFMAN_ONLY ?
deflate_huff(s, flush) :
865 (*(configuration_table[s->level].func))(
s, flush));
868 s->status = FINISH_STATE;
871 if (strm->avail_out == 0) {
884 if (flush == Z_PARTIAL_FLUSH) {
886 }
else if (flush != Z_BLOCK) {
891 if (flush == Z_FULL_FLUSH) {
893 if (s->lookahead == 0) {
901 if (strm->avail_out == 0) {
907 Assert(strm->avail_out > 0,
"bug2");
909 if (flush != Z_FINISH)
return Z_OK;
910 if (s->wrap <= 0)
return Z_STREAM_END;
915 put_byte(s, (Byte)(strm->adler & 0xff));
916 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
917 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
918 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
919 put_byte(s, (Byte)(strm->total_in & 0xff));
920 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
921 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
922 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
934 if (s->wrap > 0) s->wrap = -s->wrap;
935 return s->pending != 0 ? Z_OK : Z_STREAM_END;
943 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
945 status = strm->state->status;
946 if (status != INIT_STATE &&
947 status != EXTRA_STATE &&
948 status != NAME_STATE &&
949 status != COMMENT_STATE &&
950 status != HCRC_STATE &&
951 status != BUSY_STATE &&
952 status != FINISH_STATE) {
953 return Z_STREAM_ERROR;
957 TRY_FREE(strm, strm->state->pending_buf);
958 TRY_FREE(strm, strm->state->head);
959 TRY_FREE(strm, strm->state->prev);
960 TRY_FREE(strm, strm->state->window);
962 ZFREE(strm, strm->state);
963 strm->state = Z_NULL;
965 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
976 return Z_STREAM_ERROR;
983 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
984 return Z_STREAM_ERROR;
989 zmemcpy((voidpf)dest, (voidpf)source,
sizeof(z_stream));
991 ds = (deflate_state *) ZALLOC(dest, 1,
sizeof(deflate_state));
992 if (ds == Z_NULL)
return Z_MEM_ERROR;
994 zmemcpy((voidpf)ds, (voidpf)ss,
sizeof(deflate_state));
997 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*
sizeof(Byte));
998 ds->prev = (Posf *) ZALLOC(dest, ds->w_size,
sizeof(Pos));
999 ds->head = (Posf *) ZALLOC(dest, ds->hash_size,
sizeof(Pos));
1000 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize,
sizeof(
ush)+2);
1001 ds->pending_buf = (uchf *) overlay;
1003 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1004 ds->pending_buf == Z_NULL) {
1009 zmemcpy(ds->window, ss->window, ds->w_size * 2 *
sizeof(Byte));
1010 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size *
sizeof(Pos));
1011 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size *
sizeof(Pos));
1012 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1014 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1015 ds->d_buf = overlay + ds->lit_bufsize/
sizeof(
ush);
1016 ds->l_buf = ds->pending_buf + (1+
sizeof(
ush))*ds->lit_bufsize;
1018 ds->l_desc.dyn_tree = ds->dyn_ltree;
1019 ds->d_desc.dyn_tree = ds->dyn_dtree;
1020 ds->bl_desc.dyn_tree = ds->bl_tree;
1035 unsigned len = strm->avail_in;
1037 if (len > size) len = size;
1038 if (len == 0)
return 0;
1040 strm->avail_in -= len;
1042 zmemcpy(buf, strm->next_in, len);
1043 if (strm->state->wrap == 1) {
1044 strm->adler =
adler32(strm->adler, buf, len);
1047 else if (strm->state->wrap == 2) {
1048 strm->adler =
crc32(strm->adler, buf, len);
1051 strm->next_in += len;
1052 strm->total_in += len;
1062 s->window_size = (
ulg)2
L*s->w_size;
1068 s->max_lazy_match = configuration_table[s->level].max_lazy;
1069 s->good_match = configuration_table[s->level].good_length;
1070 s->nice_match = configuration_table[s->level].nice_length;
1071 s->max_chain_length = configuration_table[s->level].max_chain;
1074 s->block_start = 0
L;
1077 s->match_length = s->prev_length = MIN_MATCH-1;
1078 s->match_available = 0;
1103 unsigned chain_length = s->max_chain_length;
1104 Bytef *scan = s->window + s->strstart;
1107 int best_len = s->prev_length;
1108 int nice_match = s->nice_match;
1109 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1110 s->strstart - (IPos)MAX_DIST(s) :
NIL;
1114 Posf *prev = s->prev;
1115 uInt wmask = s->w_mask;
1121 Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1122 ush scan_start = *(ushf*)scan;
1123 ush scan_end = *(ushf*)(scan+best_len-1);
1125 Bytef *strend = s->window + s->strstart + MAX_MATCH;
1126 Byte scan_end1 = scan[best_len-1];
1127 Byte scan_end = scan[best_len];
1133 Assert(s->hash_bits >= 8 && MAX_MATCH == 258,
"Code too clever");
1136 if (s->prev_length >= s->good_match) {
1142 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1144 Assert((
ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
"need lookahead");
1147 Assert(cur_match < s->strstart,
"no future");
1148 match = s->window + cur_match;
1158 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1162 if (*(ushf*)(match+best_len-1) != scan_end ||
1163 *(ushf*)match != scan_start)
continue;
1174 Assert(scan[2] == match[2],
"scan[2]?");
1177 }
while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1178 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1179 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1180 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1185 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1186 if (*scan == *match) scan++;
1188 len = (MAX_MATCH - 1) - (
int)(strend-scan);
1189 scan = strend - (MAX_MATCH-1);
1193 if (match[best_len] != scan_end ||
1194 match[best_len-1] != scan_end1 ||
1196 *++match != scan[1])
continue;
1205 Assert(*scan == *match,
"match[2]?");
1211 }
while (*++scan == *++match && *++scan == *++match &&
1212 *++scan == *++match && *++scan == *++match &&
1213 *++scan == *++match && *++scan == *++match &&
1214 *++scan == *++match && *++scan == *++match &&
1217 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1219 len = MAX_MATCH - (int)(strend - scan);
1220 scan = strend - MAX_MATCH;
1224 if (len > best_len) {
1225 s->match_start = cur_match;
1227 if (len >= nice_match)
break;
1229 scan_end = *(ushf*)(scan+best_len-1);
1231 scan_end1 = scan[best_len-1];
1232 scan_end = scan[best_len];
1235 }
while ((cur_match = prev[cur_match & wmask]) > limit
1236 && --chain_length != 0);
1238 if ((uInt)best_len <= s->lookahead)
return (uInt)best_len;
1239 return s->lookahead;
1250 Bytef *scan = s->window + s->strstart;
1253 Bytef *strend = s->window + s->strstart + MAX_MATCH;
1258 Assert(s->hash_bits >= 8 && MAX_MATCH == 258,
"Code too clever");
1260 Assert((
ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
"need lookahead");
1262 Assert(cur_match < s->strstart,
"no future");
1264 match = s->window + cur_match;
1268 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1276 scan += 2, match += 2;
1277 Assert(*scan == *match,
"match[2]?");
1283 }
while (*++scan == *++match && *++scan == *++match &&
1284 *++scan == *++match && *++scan == *++match &&
1285 *++scan == *++match && *++scan == *++match &&
1286 *++scan == *++match && *++scan == *++match &&
1289 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1291 len = MAX_MATCH - (int)(strend - scan);
1293 if (len < MIN_MATCH)
return MIN_MATCH - 1;
1295 s->match_start = cur_match;
1296 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1305 local void check_match(deflate_state *s, IPos start, IPos match,
int length)
1308 if (
zmemcmp(s->window + match,
1309 s->window + start, length) !=
EQUAL) {
1310 fprintf(stderr,
" start %u, match %u, length %d\n",
1311 start, match, length);
1313 fprintf(stderr,
"%c%c", s->window[match++], s->window[start++]);
1314 }
while (--length != 0);
1315 z_error(
"invalid match");
1317 if (z_verbose > 1) {
1318 fprintf(stderr,
"\\[%d,%d]", start-match, length);
1319 do { putc(s->window[start++], stderr); }
while (--length != 0);
1323 # define check_match(s, start, match, length)
1341 uInt wsize = s->w_size;
1343 Assert(s->lookahead < MIN_LOOKAHEAD,
"already enough lookahead");
1346 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1349 if (
sizeof(
int) <= 2) {
1350 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1353 }
else if (more == (
unsigned)(-1)) {
1364 if (s->strstart >= wsize+MAX_DIST(s)) {
1366 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize);
1367 s->match_start -= wsize;
1368 s->strstart -= wsize;
1369 s->block_start -= (long) wsize;
1381 *p = (Pos)(m >= wsize ? m-wsize :
NIL);
1389 *p = (Pos)(m >= wsize ? m-wsize :
NIL);
1397 if (s->strm->avail_in == 0)
break;
1410 Assert(more >= 2,
"more < 2");
1412 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1416 if (s->lookahead + s->insert >= MIN_MATCH) {
1417 uInt str = s->strstart - s->insert;
1418 s->ins_h = s->window[str];
1424 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1426 s->prev[str & s->w_mask] = s->head[s->ins_h];
1428 s->head[s->ins_h] = (Pos)str;
1431 if (s->lookahead + s->insert < MIN_MATCH)
1439 }
while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1448 if (s->high_water < s->window_size) {
1449 ulg curr = s->strstart + (
ulg)(s->lookahead);
1452 if (s->high_water < curr) {
1456 init = s->window_size - curr;
1457 if (init > WIN_INIT)
1459 zmemzero(s->window + curr, (
unsigned)init);
1460 s->high_water = curr + init;
1462 else if (s->high_water < (
ulg)curr + WIN_INIT) {
1467 init = (
ulg)curr + WIN_INIT - s->high_water;
1468 if (init > s->window_size - s->high_water)
1469 init = s->window_size - s->high_water;
1470 zmemzero(s->window + s->high_water, (
unsigned)init);
1471 s->high_water += init;
1475 Assert((
ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1476 "not enough room for search");
1483 #define FLUSH_BLOCK_ONLY(s, last) { \
1484 _tr_flush_block(s, (s->block_start >= 0L ? \
1485 (charf *)&s->window[(unsigned)s->block_start] : \
1487 (ulg)((long)s->strstart - s->block_start), \
1489 s->block_start = s->strstart; \
1490 flush_pending(s->strm); \
1491 Tracev((stderr,"[FLUSH]")); \
1495 #define FLUSH_BLOCK(s, last) { \
1496 FLUSH_BLOCK_ONLY(s, last); \
1497 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1514 ulg max_block_size = 0xffff;
1517 if (max_block_size > s->pending_buf_size - 5) {
1518 max_block_size = s->pending_buf_size - 5;
1524 if (s->lookahead <= 1) {
1526 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1527 s->block_start >= (
long)s->w_size,
"slide too late");
1530 if (s->lookahead == 0 && flush == Z_NO_FLUSH)
return need_more;
1532 if (s->lookahead == 0)
break;
1534 Assert(s->block_start >= 0
L,
"block gone");
1536 s->strstart += s->lookahead;
1540 max_start = s->block_start + max_block_size;
1541 if (s->strstart == 0 || (
ulg)s->strstart >= max_start) {
1543 s->lookahead = (uInt)(s->strstart - max_start);
1544 s->strstart = (uInt)max_start;
1550 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1555 if (flush == Z_FINISH) {
1559 if ((
long)s->strstart > s->block_start)
1582 if (s->lookahead < MIN_LOOKAHEAD) {
1584 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1587 if (s->lookahead == 0)
break;
1594 if (s->lookahead >= MIN_MATCH) {
1601 if (hash_head !=
NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1609 if (s->match_length >= MIN_MATCH) {
1610 check_match(s, s->strstart, s->match_start, s->match_length);
1612 _tr_tally_dist(s, s->strstart - s->match_start,
1613 s->match_length - MIN_MATCH, bflush);
1615 s->lookahead -= s->match_length;
1621 if (s->match_length <= s->max_insert_length &&
1622 s->lookahead >= MIN_MATCH) {
1630 }
while (--s->match_length != 0);
1635 s->strstart += s->match_length;
1636 s->match_length = 0;
1637 s->ins_h = s->window[s->strstart];
1638 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1648 Tracevv((stderr,
"%c", s->window[s->strstart]));
1649 _tr_tally_lit (s, s->window[s->strstart], bflush);
1655 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1656 if (flush == Z_FINISH) {
1683 if (s->lookahead < MIN_LOOKAHEAD) {
1685 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1688 if (s->lookahead == 0)
break;
1695 if (s->lookahead >= MIN_MATCH) {
1701 s->prev_length = s->match_length, s->prev_match = s->match_start;
1702 s->match_length = MIN_MATCH-1;
1704 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
1705 s->strstart - hash_head <= MAX_DIST(s)) {
1713 if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1715 || (s->match_length == MIN_MATCH &&
1716 s->strstart - s->match_start >
TOO_FAR)
1723 s->match_length = MIN_MATCH-1;
1729 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1730 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1733 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1735 _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1736 s->prev_length - MIN_MATCH, bflush);
1743 s->lookahead -= s->prev_length-1;
1744 s->prev_length -= 2;
1746 if (++s->strstart <= max_insert) {
1749 }
while (--s->prev_length != 0);
1750 s->match_available = 0;
1751 s->match_length = MIN_MATCH-1;
1756 }
else if (s->match_available) {
1761 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1762 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1768 if (s->strm->avail_out == 0)
return need_more;
1773 s->match_available = 1;
1778 Assert (flush != Z_NO_FLUSH,
"no flush?");
1779 if (s->match_available) {
1780 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1781 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1782 s->match_available = 0;
1784 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1785 if (flush == Z_FINISH) {
1804 Bytef *scan, *strend;
1811 if (s->lookahead <= MAX_MATCH) {
1813 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
1816 if (s->lookahead == 0)
break;
1820 s->match_length = 0;
1821 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1822 scan = s->window + s->strstart - 1;
1824 if (prev == *++scan && prev == *++scan && prev == *++scan) {
1825 strend = s->window + s->strstart + MAX_MATCH;
1827 }
while (prev == *++scan && prev == *++scan &&
1828 prev == *++scan && prev == *++scan &&
1829 prev == *++scan && prev == *++scan &&
1830 prev == *++scan && prev == *++scan &&
1832 s->match_length = MAX_MATCH - (int)(strend - scan);
1833 if (s->match_length > s->lookahead)
1834 s->match_length = s->lookahead;
1836 Assert(scan <= s->window+(uInt)(s->window_size-1),
"wild scan");
1840 if (s->match_length >= MIN_MATCH) {
1841 check_match(s, s->strstart, s->strstart - 1, s->match_length);
1843 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
1845 s->lookahead -= s->match_length;
1846 s->strstart += s->match_length;
1847 s->match_length = 0;
1850 Tracevv((stderr,
"%c", s->window[s->strstart]));
1851 _tr_tally_lit (s, s->window[s->strstart], bflush);
1858 if (flush == Z_FINISH) {
1877 if (s->lookahead == 0) {
1879 if (s->lookahead == 0) {
1880 if (flush == Z_NO_FLUSH)
1887 s->match_length = 0;
1888 Tracevv((stderr,
"%c", s->window[s->strstart]));
1889 _tr_tally_lit (s, s->window[s->strstart], bflush);
1895 if (flush == Z_FINISH) {
int ZEXPORT deflateEnd(z_streamp strm)
local int read_buf(z_streamp strm, Bytef *buf, unsigned size)
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
local void putShortMSB(deflate_state *s, uInt b)
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
local block_state deflate_huff(deflate_state *s, int flush)
int ZEXPORT deflateCopy(z_streamp dest, z_streamp source)
static constexpr double s
local void flush_pending(z_streamp strm)
void ZLIB_INTERNAL _tr_init(deflate_state *s)
#define UPDATE_HASH(s, h, c)
int ZEXPORT deflateResetKeep(z_streamp strm)
local void lm_init(deflate_state *s)
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
void ZLIB_INTERNAL _tr_align(deflate_state *s)
#define FLUSH_BLOCK_ONLY(s, last)
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
int ZEXPORT deflatePrime(z_streamp strm, int bits, int value)
#define check_match(s, start, match, length)
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head)
local void fill_window(deflate_state *s)
static constexpr double m
local block_state deflate_fast(deflate_state *s, int flush)
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
#define INSERT_STRING(s, str, match_head)
#define FLUSH_BLOCK(s, last)
void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s)
local const config configuration_table[10]
local uInt longest_match(deflate_state *s, IPos cur_match)
local block_state deflate_slow(deflate_state *s, int flush)
int ZEXPORT deflate(z_streamp strm, int flush)
local block_state deflate_stored(deflate_state *s, int flush)
int ZLIB_INTERNAL zmemcmp(const Bytef *s1, const Bytef *s2, uInt len)
local block_state deflate_rle(deflate_state *s, int flush)
static constexpr double L
block_state compress_func OF((deflate_state *s, int flush))
uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen)
int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits)
int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
int ZEXPORT deflateReset(z_streamp strm)
int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size)