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 = (
char*)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;
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 = (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) {
489 if (s->level != level) {
491 s->max_lazy_match = configuration_table[level].max_lazy;
492 s->good_match = configuration_table[level].good_length;
493 s->nice_match = configuration_table[level].nice_length;
494 s->max_chain_length = configuration_table[level].max_chain;
496 s->strategy = strategy;
501 int ZEXPORT
deflateTune(z_streamp strm,
int good_length,
int max_lazy,
int nice_length,
int max_chain)
505 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
507 s->good_match = good_length;
508 s->max_lazy_match = max_lazy;
509 s->nice_match = nice_length;
510 s->max_chain_length = max_chain;
534 uLong complen, wraplen;
538 complen = sourceLen +
539 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
542 if (strm == Z_NULL || strm->state == Z_NULL)
552 wraplen = 6 + (s->strstart ? 4 : 0);
556 if (s->gzhead != Z_NULL) {
557 if (s->gzhead->extra != Z_NULL)
558 wraplen += 2 + s->gzhead->extra_len;
559 str = s->gzhead->name;
564 str = s->gzhead->comment;
578 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
579 return complen + wraplen;
582 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
583 (sourceLen >> 25) + 13 - 6 + wraplen;
593 put_byte(s, (Byte)(b >> 8));
594 put_byte(s, (Byte)(b & 0xff));
606 deflate_state *s = strm->state;
610 if (len > strm->avail_out) len = strm->avail_out;
611 if (len == 0)
return;
613 zmemcpy(strm->next_out, s->pending_out, len);
614 strm->next_out += len;
615 s->pending_out += len;
616 strm->total_out += len;
617 strm->avail_out -= len;
619 if (s->pending == 0) {
620 s->pending_out = s->pending_buf;
625 int ZEXPORT
deflate (z_streamp strm,
int flush)
630 if (strm == Z_NULL || strm->state == Z_NULL ||
631 flush > Z_BLOCK || flush < 0) {
632 return Z_STREAM_ERROR;
636 if (strm->next_out == Z_NULL ||
637 (strm->next_in == Z_NULL && strm->avail_in != 0) ||
638 (s->status == FINISH_STATE && flush != Z_FINISH)) {
639 ERR_RETURN(strm, Z_STREAM_ERROR);
641 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
644 old_flush = s->last_flush;
645 s->last_flush = flush;
648 if (s->status == INIT_STATE) {
651 strm->adler =
crc32(0
L, Z_NULL, 0);
655 if (s->gzhead == Z_NULL) {
661 put_byte(s, s->level == 9 ? 2 :
662 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
664 put_byte(s, OS_CODE);
665 s->status = BUSY_STATE;
668 put_byte(s, (s->gzhead->text ? 1 : 0) +
669 (s->gzhead->hcrc ? 2 : 0) +
670 (s->gzhead->extra == Z_NULL ? 0 : 4) +
671 (s->gzhead->name == Z_NULL ? 0 : 8) +
672 (s->gzhead->comment == Z_NULL ? 0 : 16)
674 put_byte(s, (Byte)(s->gzhead->time & 0xff));
675 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
676 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
677 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
678 put_byte(s, s->level == 9 ? 2 :
679 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
681 put_byte(s, s->gzhead->os & 0xff);
682 if (s->gzhead->extra != Z_NULL) {
683 put_byte(s, s->gzhead->extra_len & 0xff);
684 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
687 strm->adler =
crc32(strm->adler, s->pending_buf,
690 s->status = EXTRA_STATE;
696 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
699 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
701 else if (s->level < 6)
703 else if (s->level == 6)
707 header |= (level_flags << 6);
708 if (s->strstart != 0) header |= PRESET_DICT;
709 header += 31 - (header % 31);
711 s->status = BUSY_STATE;
715 if (s->strstart != 0) {
719 strm->adler =
adler32(0
L, Z_NULL, 0);
723 if (s->status == EXTRA_STATE) {
724 if (s->gzhead->extra != Z_NULL) {
725 uInt beg = s->pending;
727 while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
728 if (s->pending == s->pending_buf_size) {
729 if (s->gzhead->hcrc && s->pending > beg)
730 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
734 if (s->pending == s->pending_buf_size)
737 put_byte(s, s->gzhead->extra[s->gzindex]);
740 if (s->gzhead->hcrc && s->pending > beg)
741 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
743 if (s->gzindex == s->gzhead->extra_len) {
745 s->status = NAME_STATE;
749 s->status = NAME_STATE;
751 if (s->status == NAME_STATE) {
752 if (s->gzhead->name != Z_NULL) {
753 uInt beg = s->pending;
757 if (s->pending == s->pending_buf_size) {
758 if (s->gzhead->hcrc && s->pending > beg)
759 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
763 if (s->pending == s->pending_buf_size) {
768 val = s->gzhead->name[s->gzindex++];
771 if (s->gzhead->hcrc && s->pending > beg)
772 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
776 s->status = COMMENT_STATE;
780 s->status = COMMENT_STATE;
782 if (s->status == COMMENT_STATE) {
783 if (s->gzhead->comment != Z_NULL) {
784 uInt beg = s->pending;
788 if (s->pending == s->pending_buf_size) {
789 if (s->gzhead->hcrc && s->pending > beg)
790 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
794 if (s->pending == s->pending_buf_size) {
799 val = s->gzhead->comment[s->gzindex++];
802 if (s->gzhead->hcrc && s->pending > beg)
803 strm->adler =
crc32(strm->adler, s->pending_buf + beg,
806 s->status = HCRC_STATE;
809 s->status = HCRC_STATE;
811 if (s->status == HCRC_STATE) {
812 if (s->gzhead->hcrc) {
813 if (s->pending + 2 > s->pending_buf_size)
815 if (s->pending + 2 <= s->pending_buf_size) {
816 put_byte(s, (Byte)(strm->adler & 0xff));
817 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
818 strm->adler =
crc32(0
L, Z_NULL, 0);
819 s->status = BUSY_STATE;
823 s->status = BUSY_STATE;
828 if (s->pending != 0) {
830 if (strm->avail_out == 0) {
845 }
else if (strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
847 ERR_RETURN(strm, Z_BUF_ERROR);
851 if (s->status == FINISH_STATE && strm->avail_in != 0) {
852 ERR_RETURN(strm, Z_BUF_ERROR);
857 if (strm->avail_in != 0 || s->lookahead != 0 ||
858 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
861 bstate = s->strategy == Z_HUFFMAN_ONLY ?
deflate_huff(s, flush) :
863 (*(configuration_table[s->level].func))(
s, flush));
866 s->status = FINISH_STATE;
869 if (strm->avail_out == 0) {
882 if (flush == Z_PARTIAL_FLUSH) {
884 }
else if (flush != Z_BLOCK) {
889 if (flush == Z_FULL_FLUSH) {
891 if (s->lookahead == 0) {
899 if (strm->avail_out == 0) {
905 Assert(strm->avail_out > 0,
"bug2");
907 if (flush != Z_FINISH)
return Z_OK;
908 if (s->wrap <= 0)
return Z_STREAM_END;
913 put_byte(s, (Byte)(strm->adler & 0xff));
914 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
915 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
916 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
917 put_byte(s, (Byte)(strm->total_in & 0xff));
918 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
919 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
920 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
932 if (s->wrap > 0) s->wrap = -s->wrap;
933 return s->pending != 0 ? Z_OK : Z_STREAM_END;
941 if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
943 status = strm->state->status;
944 if (status != INIT_STATE &&
945 status != EXTRA_STATE &&
946 status != NAME_STATE &&
947 status != COMMENT_STATE &&
948 status != HCRC_STATE &&
949 status != BUSY_STATE &&
950 status != FINISH_STATE) {
951 return Z_STREAM_ERROR;
955 TRY_FREE(strm, strm->state->pending_buf);
956 TRY_FREE(strm, strm->state->head);
957 TRY_FREE(strm, strm->state->prev);
958 TRY_FREE(strm, strm->state->window);
960 ZFREE(strm, strm->state);
961 strm->state = Z_NULL;
963 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
974 return Z_STREAM_ERROR;
981 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
982 return Z_STREAM_ERROR;
987 zmemcpy((voidpf)dest, (voidpf)source,
sizeof(z_stream));
989 ds = (deflate_state *) ZALLOC(dest, 1,
sizeof(deflate_state));
990 if (ds == Z_NULL)
return Z_MEM_ERROR;
992 zmemcpy((voidpf)ds, (voidpf)ss,
sizeof(deflate_state));
995 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*
sizeof(Byte));
996 ds->prev = (Posf *) ZALLOC(dest, ds->w_size,
sizeof(Pos));
997 ds->head = (Posf *) ZALLOC(dest, ds->hash_size,
sizeof(Pos));
998 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize,
sizeof(
ush)+2);
999 ds->pending_buf = (uchf *) overlay;
1001 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1002 ds->pending_buf == Z_NULL) {
1007 zmemcpy(ds->window, ss->window, ds->w_size * 2 *
sizeof(Byte));
1008 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size *
sizeof(Pos));
1009 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size *
sizeof(Pos));
1010 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1012 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1013 ds->d_buf = overlay + ds->lit_bufsize/
sizeof(
ush);
1014 ds->l_buf = ds->pending_buf + (1+
sizeof(
ush))*ds->lit_bufsize;
1016 ds->l_desc.dyn_tree = ds->dyn_ltree;
1017 ds->d_desc.dyn_tree = ds->dyn_dtree;
1018 ds->bl_desc.dyn_tree = ds->bl_tree;
1033 unsigned len = strm->avail_in;
1035 if (len > size) len = size;
1036 if (len == 0)
return 0;
1038 strm->avail_in -= len;
1040 zmemcpy(buf, strm->next_in, len);
1041 if (strm->state->wrap == 1) {
1042 strm->adler =
adler32(strm->adler, buf, len);
1045 else if (strm->state->wrap == 2) {
1046 strm->adler =
crc32(strm->adler, buf, len);
1049 strm->next_in += len;
1050 strm->total_in += len;
1060 s->window_size = (
ulg)2
L*s->w_size;
1066 s->max_lazy_match = configuration_table[s->level].max_lazy;
1067 s->good_match = configuration_table[s->level].good_length;
1068 s->nice_match = configuration_table[s->level].nice_length;
1069 s->max_chain_length = configuration_table[s->level].max_chain;
1072 s->block_start = 0
L;
1075 s->match_length = s->prev_length = MIN_MATCH-1;
1076 s->match_available = 0;
1101 unsigned chain_length = s->max_chain_length;
1102 Bytef *scan = s->window + s->strstart;
1105 int best_len = s->prev_length;
1106 int nice_match = s->nice_match;
1107 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1108 s->strstart - (IPos)MAX_DIST(s) :
NIL;
1112 Posf *prev = s->prev;
1113 uInt wmask = s->w_mask;
1119 Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1120 ush scan_start = *(ushf*)scan;
1121 ush scan_end = *(ushf*)(scan+best_len-1);
1123 Bytef *strend = s->window + s->strstart + MAX_MATCH;
1124 Byte scan_end1 = scan[best_len-1];
1125 Byte scan_end = scan[best_len];
1131 Assert(s->hash_bits >= 8 && MAX_MATCH == 258,
"Code too clever");
1134 if (s->prev_length >= s->good_match) {
1140 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1142 Assert((
ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
"need lookahead");
1145 Assert(cur_match < s->strstart,
"no future");
1146 match = s->window + cur_match;
1156 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1160 if (*(ushf*)(match+best_len-1) != scan_end ||
1161 *(ushf*)match != scan_start)
continue;
1172 Assert(scan[2] == match[2],
"scan[2]?");
1175 }
while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1176 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1177 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1178 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1183 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1184 if (*scan == *match) scan++;
1186 len = (MAX_MATCH - 1) - (
int)(strend-scan);
1187 scan = strend - (MAX_MATCH-1);
1191 if (match[best_len] != scan_end ||
1192 match[best_len-1] != scan_end1 ||
1194 *++match != scan[1])
continue;
1203 Assert(*scan == *match,
"match[2]?");
1209 }
while (*++scan == *++match && *++scan == *++match &&
1210 *++scan == *++match && *++scan == *++match &&
1211 *++scan == *++match && *++scan == *++match &&
1212 *++scan == *++match && *++scan == *++match &&
1215 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1217 len = MAX_MATCH - (int)(strend - scan);
1218 scan = strend - MAX_MATCH;
1222 if (len > best_len) {
1223 s->match_start = cur_match;
1225 if (len >= nice_match)
break;
1227 scan_end = *(ushf*)(scan+best_len-1);
1229 scan_end1 = scan[best_len-1];
1230 scan_end = scan[best_len];
1233 }
while ((cur_match = prev[cur_match & wmask]) > limit
1234 && --chain_length != 0);
1236 if ((uInt)best_len <= s->lookahead)
return (uInt)best_len;
1237 return s->lookahead;
1248 Bytef *scan = s->window + s->strstart;
1251 Bytef *strend = s->window + s->strstart + MAX_MATCH;
1256 Assert(s->hash_bits >= 8 && MAX_MATCH == 258,
"Code too clever");
1258 Assert((
ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
"need lookahead");
1260 Assert(cur_match < s->strstart,
"no future");
1262 match = s->window + cur_match;
1266 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1274 scan += 2, match += 2;
1275 Assert(*scan == *match,
"match[2]?");
1281 }
while (*++scan == *++match && *++scan == *++match &&
1282 *++scan == *++match && *++scan == *++match &&
1283 *++scan == *++match && *++scan == *++match &&
1284 *++scan == *++match && *++scan == *++match &&
1287 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1289 len = MAX_MATCH - (int)(strend - scan);
1291 if (len < MIN_MATCH)
return MIN_MATCH - 1;
1293 s->match_start = cur_match;
1294 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1303 local void check_match(deflate_state *s, IPos start, IPos match,
int length)
1306 if (
zmemcmp(s->window + match,
1307 s->window + start, length) !=
EQUAL) {
1308 fprintf(stderr,
" start %u, match %u, length %d\n",
1309 start, match, length);
1311 fprintf(stderr,
"%c%c", s->window[match++], s->window[start++]);
1312 }
while (--length != 0);
1313 z_error(
"invalid match");
1315 if (z_verbose > 1) {
1316 fprintf(stderr,
"\\[%d,%d]", start-match, length);
1317 do { putc(s->window[start++], stderr); }
while (--length != 0);
1321 # define check_match(s, start, match, length)
1339 uInt wsize = s->w_size;
1341 Assert(s->lookahead < MIN_LOOKAHEAD,
"already enough lookahead");
1344 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1347 if (
sizeof(
int) <= 2) {
1348 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1351 }
else if (more == (
unsigned)(-1)) {
1362 if (s->strstart >= wsize+MAX_DIST(s)) {
1364 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize);
1365 s->match_start -= wsize;
1366 s->strstart -= wsize;
1367 s->block_start -= (long) wsize;
1379 *p = (Pos)(m >= wsize ? m-wsize :
NIL);
1387 *p = (Pos)(m >= wsize ? m-wsize :
NIL);
1395 if (s->strm->avail_in == 0)
break;
1408 Assert(more >= 2,
"more < 2");
1410 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1414 if (s->lookahead + s->insert >= MIN_MATCH) {
1415 uInt str = s->strstart - s->insert;
1416 s->ins_h = s->window[str];
1422 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1424 s->prev[str & s->w_mask] = s->head[s->ins_h];
1426 s->head[s->ins_h] = (Pos)str;
1429 if (s->lookahead + s->insert < MIN_MATCH)
1437 }
while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1446 if (s->high_water < s->window_size) {
1447 ulg curr = s->strstart + (
ulg)(s->lookahead);
1450 if (s->high_water < curr) {
1454 init = s->window_size - curr;
1455 if (init > WIN_INIT)
1457 zmemzero(s->window + curr, (
unsigned)init);
1458 s->high_water = curr + init;
1460 else if (s->high_water < (
ulg)curr + WIN_INIT) {
1465 init = (
ulg)curr + WIN_INIT - s->high_water;
1466 if (init > s->window_size - s->high_water)
1467 init = s->window_size - s->high_water;
1468 zmemzero(s->window + s->high_water, (
unsigned)init);
1469 s->high_water += init;
1473 Assert((
ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1474 "not enough room for search");
1481 #define FLUSH_BLOCK_ONLY(s, last) { \
1482 _tr_flush_block(s, (s->block_start >= 0L ? \
1483 (charf *)&s->window[(unsigned)s->block_start] : \
1485 (ulg)((long)s->strstart - s->block_start), \
1487 s->block_start = s->strstart; \
1488 flush_pending(s->strm); \
1489 Tracev((stderr,"[FLUSH]")); \
1493 #define FLUSH_BLOCK(s, last) { \
1494 FLUSH_BLOCK_ONLY(s, last); \
1495 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1512 ulg max_block_size = 0xffff;
1515 if (max_block_size > s->pending_buf_size - 5) {
1516 max_block_size = s->pending_buf_size - 5;
1522 if (s->lookahead <= 1) {
1524 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1525 s->block_start >= (
long)s->w_size,
"slide too late");
1528 if (s->lookahead == 0 && flush == Z_NO_FLUSH)
return need_more;
1530 if (s->lookahead == 0)
break;
1532 Assert(s->block_start >= 0
L,
"block gone");
1534 s->strstart += s->lookahead;
1538 max_start = s->block_start + max_block_size;
1539 if (s->strstart == 0 || (
ulg)s->strstart >= max_start) {
1541 s->lookahead = (uInt)(s->strstart - max_start);
1542 s->strstart = (uInt)max_start;
1548 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1553 if (flush == Z_FINISH) {
1557 if ((
long)s->strstart > s->block_start)
1580 if (s->lookahead < MIN_LOOKAHEAD) {
1582 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1585 if (s->lookahead == 0)
break;
1592 if (s->lookahead >= MIN_MATCH) {
1599 if (hash_head !=
NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1607 if (s->match_length >= MIN_MATCH) {
1608 check_match(s, s->strstart, s->match_start, s->match_length);
1610 _tr_tally_dist(s, s->strstart - s->match_start,
1611 s->match_length - MIN_MATCH, bflush);
1613 s->lookahead -= s->match_length;
1619 if (s->match_length <= s->max_insert_length &&
1620 s->lookahead >= MIN_MATCH) {
1628 }
while (--s->match_length != 0);
1633 s->strstart += s->match_length;
1634 s->match_length = 0;
1635 s->ins_h = s->window[s->strstart];
1636 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1646 Tracevv((stderr,
"%c", s->window[s->strstart]));
1647 _tr_tally_lit (s, s->window[s->strstart], bflush);
1653 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1654 if (flush == Z_FINISH) {
1681 if (s->lookahead < MIN_LOOKAHEAD) {
1683 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1686 if (s->lookahead == 0)
break;
1693 if (s->lookahead >= MIN_MATCH) {
1699 s->prev_length = s->match_length, s->prev_match = s->match_start;
1700 s->match_length = MIN_MATCH-1;
1702 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
1703 s->strstart - hash_head <= MAX_DIST(s)) {
1711 if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1713 || (s->match_length == MIN_MATCH &&
1714 s->strstart - s->match_start >
TOO_FAR)
1721 s->match_length = MIN_MATCH-1;
1727 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1728 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1731 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1733 _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1734 s->prev_length - MIN_MATCH, bflush);
1741 s->lookahead -= s->prev_length-1;
1742 s->prev_length -= 2;
1744 if (++s->strstart <= max_insert) {
1747 }
while (--s->prev_length != 0);
1748 s->match_available = 0;
1749 s->match_length = MIN_MATCH-1;
1754 }
else if (s->match_available) {
1759 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1760 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1766 if (s->strm->avail_out == 0)
return need_more;
1771 s->match_available = 1;
1776 Assert (flush != Z_NO_FLUSH,
"no flush?");
1777 if (s->match_available) {
1778 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1779 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1780 s->match_available = 0;
1782 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1783 if (flush == Z_FINISH) {
1802 Bytef *scan, *strend;
1809 if (s->lookahead <= MAX_MATCH) {
1811 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
1814 if (s->lookahead == 0)
break;
1818 s->match_length = 0;
1819 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1820 scan = s->window + s->strstart - 1;
1822 if (prev == *++scan && prev == *++scan && prev == *++scan) {
1823 strend = s->window + s->strstart + MAX_MATCH;
1825 }
while (prev == *++scan && prev == *++scan &&
1826 prev == *++scan && prev == *++scan &&
1827 prev == *++scan && prev == *++scan &&
1828 prev == *++scan && prev == *++scan &&
1830 s->match_length = MAX_MATCH - (int)(strend - scan);
1831 if (s->match_length > s->lookahead)
1832 s->match_length = s->lookahead;
1834 Assert(scan <= s->window+(uInt)(s->window_size-1),
"wild scan");
1838 if (s->match_length >= MIN_MATCH) {
1839 check_match(s, s->strstart, s->strstart - 1, s->match_length);
1841 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
1843 s->lookahead -= s->match_length;
1844 s->strstart += s->match_length;
1845 s->match_length = 0;
1848 Tracevv((stderr,
"%c", s->window[s->strstart]));
1849 _tr_tally_lit (s, s->window[s->strstart], bflush);
1856 if (flush == Z_FINISH) {
1875 if (s->lookahead == 0) {
1877 if (s->lookahead == 0) {
1878 if (flush == Z_NO_FLUSH)
1885 s->match_length = 0;
1886 Tracevv((stderr,
"%c", s->window[s->strstart]));
1887 _tr_tally_lit (s, s->window[s->strstart], bflush);
1893 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)
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)
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)
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)