55 " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler ";
73 typedef block_state (*compress_func)
OF((deflate_state *s,
int flush));
77 local void slide_hash
OF((deflate_state *s));
78 local void fill_window
OF((deflate_state *s));
79 local block_state deflate_stored
OF((deflate_state *s,
int flush));
80 local block_state deflate_fast
OF((deflate_state *s,
int flush));
82 local block_state deflate_slow
OF((deflate_state *s,
int flush));
84 local block_state deflate_rle
OF((deflate_state *s,
int flush));
85 local block_state deflate_huff
OF((deflate_state *s,
int flush));
86 local void lm_init
OF((deflate_state *s));
87 local void putShortMSB
OF((deflate_state *s,
uInt b));
91 # pragma message("Assembler code may have bugs -- use at your own risk") 92 void match_init OF((
void));
93 uInt longest_match OF((deflate_state *s, IPos cur_match));
99 local
void check_match OF((deflate_state *s, IPos start, IPos match,
111 # define TOO_FAR 4096
129 local
const config configuration_table[2] = {
131 {0, 0, 0, 0, deflate_stored},
132 {4, 4, 8, 4, deflate_fast}};
136 {0, 0, 0, 0, deflate_stored},
137 {4, 4, 8, 4, deflate_fast},
138 {4, 5, 16, 8, deflate_fast},
139 {4, 6, 32, 32, deflate_fast},
141 {4, 4, 16, 16, deflate_slow},
142 {8, 16, 32, 32, deflate_slow},
143 {8, 16, 128, 128, deflate_slow},
144 {8, 32, 128, 256, deflate_slow},
145 {32, 128, 258, 1024, deflate_slow},
146 {32, 258, 258, 4096, deflate_slow}};
155 #define RANK(f) (((f) * 2
) - ((f) > 4
? 9
: 0
)) 163 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 177 #define INSERT_STRING(s, str, match_head) 178 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1
)]), 179 match_head = s->head[s->ins_h], 180 s->head[s->ins_h] = (Pos)(str)) 182 #define INSERT_STRING(s, str, match_head) 184 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], 185 s->head[s->ins_h] = (Pos)(str)) 192 #define CLEAR_HASH(s) 193 s->head[s->hash_size-1
] = NIL; 194 zmemzero
((Bytef *)s->head, (unsigned)(s->hash_size-1
)*sizeof(*s->head)); 206 uInt wsize = s->w_size;
212 *p = (Pos)(m >= wsize ? m - wsize :
NIL);
219 *p = (Pos)(m >= wsize ? m - wsize :
NIL);
241 version, stream_size)
260 if (version ==
Z_NULL || version[0] != my_version[0] ||
261 stream_size !=
sizeof(z_stream)) {
267 if (strm->zalloc == (alloc_func)0) {
269 return Z_STREAM_ERROR;
271 strm->zalloc = zcalloc;
272 strm->opaque = (voidpf)0;
275 if (strm->zfree == (free_func)0)
277 return Z_STREAM_ERROR;
279 strm->zfree = zcfree;
283 if (level != 0) level = 1;
288 if (windowBits < 0) {
290 windowBits = -windowBits;
293 else if (windowBits > 15) {
299 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
300 strategy < 0 || strategy >
Z_FIXED || (windowBits == 8 && wrap != 1)) {
303 if (windowBits == 8) windowBits = 9;
304 s = (deflate_state *)
ZALLOC(strm, 1,
sizeof(deflate_state));
306 strm->state = (
struct internal_state
FAR *)s;
312 s->w_bits = (uInt)windowBits;
313 s->w_size = 1 << s->w_bits;
314 s->w_mask = s->w_size - 1;
316 s->hash_bits = (uInt)memLevel + 7;
317 s->hash_size = 1 << s->hash_bits;
318 s->hash_mask = s->hash_size - 1;
321 s->window = (Bytef *)
ZALLOC(strm, s->w_size, 2*
sizeof(Byte));
322 s->prev = (Posf *)
ZALLOC(strm, s->w_size,
sizeof(Pos));
323 s->head = (Posf *)
ZALLOC(strm, s->hash_size,
sizeof(Pos));
327 s->lit_bufsize = 1 << (memLevel + 6);
329 overlay = (ushf *)
ZALLOC(strm, s->lit_bufsize,
sizeof(ush)+2);
330 s->pending_buf = (uchf *) overlay;
331 s->pending_buf_size = (ulg)s->lit_bufsize * (
sizeof(ush)+2L);
334 s->pending_buf ==
Z_NULL) {
340 s->d_buf = overlay + s->lit_bufsize/
sizeof(ush);
341 s->l_buf = s->pending_buf + (1+
sizeof(ush))*s->lit_bufsize;
344 s->strategy = strategy;
345 s->method = (Byte)method;
347 return deflateReset(strm);
358 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
378 const Bytef *dictionary;
387 if (deflateStateCheck(strm) || dictionary ==
Z_NULL)
391 if (wrap == 2 || (wrap == 1 && s->status !=
INIT_STATE) || s->lookahead)
396 strm->adler = adler32(strm->adler, dictionary, dictLength);
400 if (dictLength >= s->w_size) {
407 dictionary += dictLength - s->w_size;
408 dictLength = s->w_size;
412 avail = strm->avail_in;
413 next = strm->next_in;
414 strm->avail_in = dictLength;
415 strm->next_in = (
z_const Bytef *)dictionary;
423 s->prev[str & s->w_mask] = s->head[s->ins_h];
425 s->head[s->ins_h] = (Pos)str;
432 s->strstart += s->lookahead;
433 s->block_start = (
long)s->strstart;
434 s->insert = s->lookahead;
436 s->match_length = s->prev_length =
MIN_MATCH-1;
437 s->match_available = 0;
438 strm->next_in = next;
439 strm->avail_in = avail;
453 if (deflateStateCheck(strm))
456 len = s->strstart + s->lookahead;
459 if (dictionary !=
Z_NULL && len)
460 zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
472 if (deflateStateCheck(strm)) {
476 strm->total_in = strm->total_out = 0;
480 s = (deflate_state *)strm->state;
482 s->pending_out = s->pending_buf;
494 s->wrap == 2 ? crc32(0L,
Z_NULL, 0) :
510 ret = deflateResetKeep(strm);
512 lm_init(strm->state);
521 if (deflateStateCheck(strm) || strm->state->wrap != 2)
523 strm->state->gzhead = head;
535 *pending = strm->state->pending;
537 *bits = strm->state->bi_valid;
552 if ((Bytef *)(s->d_buf) < s->pending_out + ((
Buf_size + 7) >> 3))
558 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
580 if (level != 0) level = 1;
584 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
587 func = configuration_table[s->level].func;
589 if ((strategy != s->strategy || func != configuration_table[level].func) &&
592 int err = deflate(strm,
Z_BLOCK);
595 if (strm->avail_out == 0)
598 if (s->level != level) {
599 if (s->level == 0 && s->matches != 0) {
607 s->max_lazy_match = configuration_table[level].max_lazy;
608 s->good_match = configuration_table[level].good_length;
609 s->nice_match = configuration_table[level].nice_length;
610 s->max_chain_length = configuration_table[level].max_chain;
612 s->strategy = strategy;
628 s->good_match = (uInt)good_length;
629 s->max_lazy_match = (uInt)max_lazy;
630 s->nice_match = nice_length;
631 s->max_chain_length = (uInt)max_chain;
657 uLong complen, wraplen;
660 complen = sourceLen +
661 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
664 if (deflateStateCheck(strm))
674 wraplen = 6 + (s->strstart ? 4 : 0);
679 if (s->gzhead !=
Z_NULL) {
681 if (s->gzhead->extra !=
Z_NULL)
682 wraplen += 2 + s->gzhead->extra_len;
683 str = s->gzhead->name;
688 str = s->gzhead->comment;
703 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
704 return complen + wraplen;
707 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
708 (sourceLen >> 25) + 13 - 6 + wraplen;
734 deflate_state *s = strm->state;
738 if (len > strm->avail_out) len = strm->avail_out;
739 if (len == 0)
return;
741 zmemcpy(strm->next_out, s->pending_out, len);
742 strm->next_out += len;
743 s->pending_out += len;
744 strm->total_out += len;
745 strm->avail_out -= len;
747 if (s->pending == 0) {
748 s->pending_out = s->pending_buf;
755 #define HCRC_UPDATE(beg) 757 if (s->gzhead->hcrc && s->pending > (beg)) 758 strm->adler = crc32(strm->adler, s->pending_buf + (beg), 770 if (deflateStateCheck(strm) || flush >
Z_BLOCK || flush < 0) {
775 if (strm->next_out ==
Z_NULL ||
776 (strm->avail_in != 0 && strm->next_in ==
Z_NULL) ||
782 old_flush = s->last_flush;
783 s->last_flush = flush;
786 if (s->pending != 0) {
788 if (strm->avail_out == 0) {
803 }
else if (strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
816 uInt header = (
Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
821 else if (s->level < 6)
823 else if (s->level == 6)
827 header |= (level_flags << 6);
829 header += 31 - (header % 31);
831 putShortMSB(s, header);
834 if (s->strstart != 0) {
835 putShortMSB(s, (uInt)(strm->adler >> 16));
836 putShortMSB(s, (uInt)(strm->adler & 0xffff));
838 strm->adler = adler32(0L,
Z_NULL, 0);
843 if (s->pending != 0) {
851 strm->adler = crc32(0L,
Z_NULL, 0);
855 if (s->gzhead ==
Z_NULL) {
869 if (s->pending != 0) {
875 put_byte(s, (s->gzhead->text ? 1 : 0) +
876 (s->gzhead->hcrc ? 2 : 0) +
877 (s->gzhead->extra ==
Z_NULL ? 0 : 4) +
878 (s->gzhead->name ==
Z_NULL ? 0 : 8) +
879 (s->gzhead->comment ==
Z_NULL ? 0 : 16)
881 put_byte(s, (Byte)(s->gzhead->time & 0xff));
882 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
883 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
884 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
889 if (s->gzhead->extra !=
Z_NULL) {
890 put_byte(s, s->gzhead->extra_len & 0xff);
891 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
894 strm->adler = crc32(strm->adler, s->pending_buf,
901 if (s->gzhead->extra !=
Z_NULL) {
902 ulg beg = s->pending;
903 uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
904 while (s->pending + left > s->pending_buf_size) {
905 uInt copy = s->pending_buf_size - s->pending;
906 zmemcpy(s->pending_buf + s->pending,
907 s->gzhead->extra + s->gzindex, copy);
908 s->pending = s->pending_buf_size;
912 if (s->pending != 0) {
919 zmemcpy(s->pending_buf + s->pending,
920 s->gzhead->extra + s->gzindex, left);
928 if (s->gzhead->name !=
Z_NULL) {
929 ulg beg = s->pending;
932 if (s->pending == s->pending_buf_size) {
935 if (s->pending != 0) {
941 val = s->gzhead->name[s->gzindex++];
950 if (s->gzhead->comment !=
Z_NULL) {
951 ulg beg = s->pending;
954 if (s->pending == s->pending_buf_size) {
957 if (s->pending != 0) {
963 val = s->gzhead->comment[s->gzindex++];
971 if (s->gzhead->hcrc) {
972 if (s->pending + 2 > s->pending_buf_size) {
974 if (s->pending != 0) {
979 put_byte(s, (Byte)(strm->adler & 0xff));
980 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
981 strm->adler = crc32(0L,
Z_NULL, 0);
987 if (s->pending != 0) {
996 if (strm->avail_in != 0 || s->lookahead != 0 ||
1000 bstate = s->level == 0 ? deflate_stored(s, flush) :
1002 s->strategy ==
Z_RLE ? deflate_rle(s, flush) :
1003 (*(configuration_table[s->level].func))(s, flush);
1005 if (bstate == finish_started || bstate == finish_done) {
1008 if (bstate == need_more || bstate == finish_started) {
1009 if (strm->avail_out == 0) {
1021 if (bstate == block_done) {
1024 }
else if (flush !=
Z_BLOCK) {
1025 _tr_stored_block(s, (
char*)0, 0L, 0);
1031 if (s->lookahead == 0) {
1033 s->block_start = 0L;
1038 flush_pending(strm);
1039 if (strm->avail_out == 0) {
1052 put_byte(s, (Byte)(strm->adler & 0xff));
1053 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1054 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
1055 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
1056 put_byte(s, (Byte)(strm->total_in & 0xff));
1057 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
1058 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
1059 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
1064 putShortMSB(s, (uInt)(strm->adler >> 16));
1065 putShortMSB(s, (uInt)(strm->adler & 0xffff));
1067 flush_pending(strm);
1071 if (s->wrap > 0) s->wrap = -s->wrap;
1083 status = strm->state->status;
1086 TRY_FREE(strm, strm->state->pending_buf);
1089 TRY_FREE(strm, strm->state->window);
1091 ZFREE(strm, strm->state);
1107 return Z_STREAM_ERROR;
1114 if (deflateStateCheck(source) || dest ==
Z_NULL) {
1120 zmemcpy((voidpf)dest, (voidpf)source,
sizeof(z_stream));
1122 ds = (deflate_state *)
ZALLOC(dest, 1,
sizeof(deflate_state));
1124 dest->state = (
struct internal_state
FAR *) ds;
1125 zmemcpy((voidpf)ds, (voidpf)ss,
sizeof(deflate_state));
1128 ds->window = (Bytef *)
ZALLOC(dest, ds->w_size, 2*
sizeof(Byte));
1129 ds->prev = (Posf *)
ZALLOC(dest, ds->w_size,
sizeof(Pos));
1130 ds->head = (Posf *)
ZALLOC(dest, ds->hash_size,
sizeof(Pos));
1131 overlay = (ushf *)
ZALLOC(dest, ds->lit_bufsize,
sizeof(ush)+2);
1132 ds->pending_buf = (uchf *) overlay;
1135 ds->pending_buf ==
Z_NULL) {
1140 zmemcpy(ds->window, ss->window, ds->w_size * 2 *
sizeof(Byte));
1141 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size *
sizeof(Pos));
1142 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size *
sizeof(Pos));
1143 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1145 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1146 ds->d_buf = overlay + ds->lit_bufsize/
sizeof(ush);
1147 ds->l_buf = ds->pending_buf + (1+
sizeof(ush))*ds->lit_bufsize;
1149 ds->l_desc.dyn_tree = ds->dyn_ltree;
1150 ds->d_desc.dyn_tree = ds->dyn_dtree;
1151 ds->bl_desc.dyn_tree = ds->bl_tree;
1169 unsigned len = strm->avail_in;
1171 if (len > size) len = size;
1172 if (len == 0)
return 0;
1174 strm->avail_in -= len;
1176 zmemcpy(buf, strm->next_in, len);
1177 if (strm->state->wrap == 1) {
1178 strm->adler = adler32(strm->adler, buf, len);
1181 else if (strm->state->wrap == 2) {
1182 strm->adler = crc32(strm->adler, buf, len);
1185 strm->next_in += len;
1186 strm->total_in += len;
1197 s->window_size = (ulg)2L*s->w_size;
1203 s->max_lazy_match = configuration_table[s->level].max_lazy;
1204 s->good_match = configuration_table[s->level].good_length;
1205 s->nice_match = configuration_table[s->level].nice_length;
1206 s->max_chain_length = configuration_table[s->level].max_chain;
1209 s->block_start = 0L;
1212 s->match_length = s->prev_length =
MIN_MATCH-1;
1213 s->match_available = 0;
1240 unsigned chain_length = s->max_chain_length;
1241 register Bytef *scan = s->window + s->strstart;
1242 register Bytef *match;
1244 int best_len = (
int)s->prev_length;
1245 int nice_match = s->nice_match;
1246 IPos limit = s->strstart > (IPos)
MAX_DIST(s) ?
1251 Posf *prev = s->prev;
1252 uInt wmask = s->w_mask;
1258 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1259 register ush scan_start = *(ushf*)scan;
1260 register ush scan_end = *(ushf*)(scan+best_len-1);
1262 register Bytef *strend = s->window + s->strstart +
MAX_MATCH;
1263 register Byte scan_end1 = scan[best_len-1];
1264 register Byte scan_end = scan[best_len];
1270 Assert(s->hash_bits >= 8 && MAX_MATCH == 258,
"Code too clever");
1273 if (s->prev_length >= s->good_match) {
1279 if ((uInt)nice_match > s->lookahead) nice_match = (
int)s->lookahead;
1281 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
"need lookahead");
1284 Assert(cur_match < s->strstart,
"no future");
1285 match = s->window + cur_match;
1295 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258
) 1299 if (*(ushf*)(match+best_len-1) != scan_end ||
1300 *(ushf*)match != scan_start)
continue;
1311 Assert(scan[2] == match[2],
"scan[2]?");
1314 }
while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1315 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1316 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1317 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1322 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1323 if (*scan == *match) scan++;
1325 len = (MAX_MATCH - 1) - (
int)(strend-scan);
1326 scan = strend - (MAX_MATCH-1);
1330 if (match[best_len] != scan_end ||
1331 match[best_len-1] != scan_end1 ||
1333 *++match != scan[1])
continue;
1342 Assert(*scan == *match,
"match[2]?");
1348 }
while (*++scan == *++match && *++scan == *++match &&
1349 *++scan == *++match && *++scan == *++match &&
1350 *++scan == *++match && *++scan == *++match &&
1351 *++scan == *++match && *++scan == *++match &&
1354 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1361 if (len > best_len) {
1362 s->match_start = cur_match;
1364 if (len >= nice_match)
break;
1366 scan_end = *(ushf*)(scan+best_len-1);
1368 scan_end1 = scan[best_len-1];
1369 scan_end = scan[best_len];
1372 }
while ((cur_match = prev[cur_match & wmask]) > limit
1373 && --chain_length != 0);
1375 if ((uInt)best_len <= s->lookahead)
return (uInt)best_len;
1376 return s->lookahead;
1385 local uInt longest_match(s, cur_match)
1389 register Bytef *scan = s->window + s->strstart;
1390 register Bytef *match;
1392 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1397 Assert(s->hash_bits >= 8 && MAX_MATCH == 258,
"Code too clever");
1399 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
"need lookahead");
1401 Assert(cur_match < s->strstart,
"no future");
1403 match = s->window + cur_match;
1407 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1415 scan += 2, match += 2;
1416 Assert(*scan == *match,
"match[2]?");
1422 }
while (*++scan == *++match && *++scan == *++match &&
1423 *++scan == *++match && *++scan == *++match &&
1424 *++scan == *++match && *++scan == *++match &&
1425 *++scan == *++match && *++scan == *++match &&
1428 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1430 len = MAX_MATCH - (
int)(strend - scan);
1432 if (len < MIN_MATCH)
return MIN_MATCH - 1;
1434 s->match_start = cur_match;
1435 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1448 local
void check_match(s, start, match, length)
1454 if (zmemcmp(s->window + match,
1455 s->window + start, length) != EQUAL) {
1456 fprintf(stderr,
" start %u, match %u, length %d\n",
1457 start, match, length);
1459 fprintf(stderr,
"%c%c", s->window[match++], s->window[start++]);
1460 }
while (--length != 0);
1461 z_error(
"invalid match");
1463 if (z_verbose > 1) {
1464 fprintf(stderr,
"\\[%d,%d]", start-match, length);
1465 do { putc(s->window[start++], stderr); }
while (--length != 0);
1469 # define check_match(s, start, match, length) 1487 uInt wsize = s->w_size;
1489 Assert(s->lookahead < MIN_LOOKAHEAD,
"already enough lookahead");
1492 more = (
unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1495 if (
sizeof(
int) <= 2) {
1496 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1499 }
else if (more == (
unsigned)(-1)) {
1510 if (s->strstart >= wsize+
MAX_DIST(s)) {
1512 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize - more);
1513 s->match_start -= wsize;
1514 s->strstart -= wsize;
1515 s->block_start -= (
long) wsize;
1519 if (s->strm->avail_in == 0)
break;
1532 Assert(more >= 2,
"more < 2");
1534 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1538 if (s->lookahead + s->insert >=
MIN_MATCH) {
1539 uInt str = s->strstart - s->insert;
1540 s->ins_h = s->window[str];
1543 Call UPDATE_HASH() MIN_MATCH-3 more times
1548 s->prev[str & s->w_mask] = s->head[s->ins_h];
1550 s->head[s->ins_h] = (Pos)str;
1553 if (s->lookahead + s->insert <
MIN_MATCH)
1561 }
while (s->lookahead <
MIN_LOOKAHEAD && s->strm->avail_in != 0);
1570 if (s->high_water < s->window_size) {
1571 ulg curr = s->strstart + (ulg)(s->lookahead);
1574 if (s->high_water < curr) {
1578 init = s->window_size - curr;
1581 zmemzero(s->window + curr, (
unsigned)init);
1582 s->high_water = curr + init;
1584 else if (s->high_water < (ulg)curr +
WIN_INIT) {
1589 init = (ulg)curr +
WIN_INIT - s->high_water;
1590 if (init > s->window_size - s->high_water)
1591 init = s->window_size - s->high_water;
1592 zmemzero(s->window + s->high_water, (
unsigned)init);
1593 s->high_water += init;
1597 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1598 "not enough room for search");
1605 #define FLUSH_BLOCK_ONLY(s, last) { 1606 _tr_flush_block(s, (s->block_start >= 0L
? 1607 (charf *)&s->window[(unsigned)s->block_start] : 1609 (ulg)((long)s->strstart - s->block_start), 1611 s->block_start = s->strstart; 1612 flush_pending(s->strm); 1613 Tracev((stderr,"[FLUSH]")); \ 1617 #define FLUSH_BLOCK(s, last) { 1619 if (s->strm->avail_out == 0
) return (last) ? finish_started : need_more; \ 1623 #define MAX_STORED 65535
1626 #define MIN(a, b) ((a) > (b) ? (b) : (a)) 1651 unsigned min_block =
MIN(s->pending_buf_size - 5, s->w_size);
1657 unsigned len, left, have, last = 0;
1658 unsigned used = s->strm->avail_in;
1665 have = (s->bi_valid + 42) >> 3;
1666 if (s->strm->avail_out < have)
1669 have = s->strm->avail_out - have;
1670 left = s->strstart - s->block_start;
1671 if (len > (ulg)left + s->strm->avail_in)
1672 len = left + s->strm->avail_in;
1681 if (len < min_block && ((len == 0 && flush !=
Z_FINISH) ||
1683 len != left + s->strm->avail_in))
1689 last = flush ==
Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1690 _tr_stored_block(s, (
char *)0, 0L, last);
1693 s->pending_buf[s->pending - 4] = len;
1694 s->pending_buf[s->pending - 3] = len >> 8;
1695 s->pending_buf[s->pending - 2] = ~len;
1696 s->pending_buf[s->pending - 1] = ~len >> 8;
1699 flush_pending(s->strm);
1703 s->compressed_len += len << 3;
1704 s->bits_sent += len << 3;
1711 zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1712 s->strm->next_out += left;
1713 s->strm->avail_out -= left;
1714 s->strm->total_out += left;
1715 s->block_start += left;
1723 read_buf(s->strm, s->strm->next_out, len);
1724 s->strm->next_out += len;
1725 s->strm->avail_out -= len;
1726 s->strm->total_out += len;
1728 }
while (last == 0);
1736 used -= s->strm->avail_in;
1741 if (used >= s->w_size) {
1743 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1744 s->strstart = s->w_size;
1747 if (s->window_size - s->strstart <= used) {
1749 s->strstart -= s->w_size;
1750 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1754 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1755 s->strstart += used;
1757 s->block_start = s->strstart;
1758 s->insert +=
MIN(used, s->w_size - s->insert);
1760 if (s->high_water < s->strstart)
1761 s->high_water = s->strstart;
1769 s->strm->avail_in == 0 && (
long)s->strstart == s->block_start)
1773 have = s->window_size - s->strstart - 1;
1774 if (s->strm->avail_in > have && s->block_start >= (
long)s->w_size) {
1776 s->block_start -= s->w_size;
1777 s->strstart -= s->w_size;
1778 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1783 if (have > s->strm->avail_in)
1784 have = s->strm->avail_in;
1786 read_buf(s->strm, s->window + s->strstart, have);
1787 s->strstart += have;
1789 if (s->high_water < s->strstart)
1790 s->high_water = s->strstart;
1797 have = (s->bi_valid + 42) >> 3;
1800 min_block =
MIN(have, s->w_size);
1801 left = s->strstart - s->block_start;
1802 if (left >= min_block ||
1804 s->strm->avail_in == 0 && left <= have)) {
1805 len =
MIN(left, have);
1806 last = flush ==
Z_FINISH && s->strm->avail_in == 0 &&
1807 len == left ? 1 : 0;
1808 _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
1809 s->block_start += len;
1810 flush_pending(s->strm);
1814 return last ? finish_started : need_more;
1842 if (s->lookahead == 0)
break;
1856 if (hash_head !=
NIL && s->strstart - hash_head <=
MAX_DIST(s)) {
1861 s->match_length = longest_match (s, hash_head);
1865 check_match(s, s->strstart, s->match_start, s->match_length);
1870 s->lookahead -= s->match_length;
1885 }
while (--s->match_length != 0);
1890 s->strstart += s->match_length;
1891 s->match_length = 0;
1892 s->ins_h = s->window[s->strstart];
1893 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1895 Call UPDATE_HASH() MIN_MATCH-3 more times
1903 Tracevv((stderr,
"%c", s->window[s->strstart]));
1945 if (s->lookahead == 0)
break;
1958 s->prev_length = s->match_length, s->prev_match = s->match_start;
1961 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
1962 s->strstart - hash_head <=
MAX_DIST(s)) {
1967 s->match_length = longest_match (s, hash_head);
1970 if (s->match_length <= 5 && (s->strategy ==
Z_FILTERED 1973 s->strstart - s->match_start >
TOO_FAR)
1986 if (s->prev_length >=
MIN_MATCH && s->match_length <= s->prev_length) {
1987 uInt max_insert = s->strstart + s->lookahead -
MIN_MATCH;
1990 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
2000 s->lookahead -= s->prev_length-1;
2001 s->prev_length -= 2;
2003 if (++s->strstart <= max_insert) {
2006 }
while (--s->prev_length != 0);
2007 s->match_available = 0;
2013 }
else if (s->match_available) {
2018 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
2025 if (s->strm->avail_out == 0)
return need_more;
2030 s->match_available = 1;
2035 Assert (flush != Z_NO_FLUSH,
"no flush?");
2036 if (s->match_available) {
2037 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
2039 s->match_available = 0;
2063 Bytef *scan, *strend;
2075 if (s->lookahead == 0)
break;
2079 s->match_length = 0;
2080 if (s->lookahead >=
MIN_MATCH && s->strstart > 0) {
2081 scan = s->window + s->strstart - 1;
2083 if (prev == *++scan && prev == *++scan && prev == *++scan) {
2084 strend = s->window + s->strstart +
MAX_MATCH;
2086 }
while (prev == *++scan && prev == *++scan &&
2087 prev == *++scan && prev == *++scan &&
2088 prev == *++scan && prev == *++scan &&
2089 prev == *++scan && prev == *++scan &&
2091 s->match_length =
MAX_MATCH - (uInt)(strend - scan);
2092 if (s->match_length > s->lookahead)
2093 s->match_length = s->lookahead;
2095 Assert(scan <= s->window+(uInt)(s->window_size-1),
"wild scan");
2100 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2104 s->lookahead -= s->match_length;
2105 s->strstart += s->match_length;
2106 s->match_length = 0;
2109 Tracevv((stderr,
"%c", s->window[s->strstart]));
2138 if (s->lookahead == 0) {
2140 if (s->lookahead == 0) {
2148 s->match_length = 0;
2149 Tracevv((stderr,
"%c", s->window[s->strstart]));
int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary, uInt *dictLength)
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
#define INSERT_STRING(s, str, match_head)
#define check_match(s, start, match, length)
local void flush_pending(z_streamp strm)
#define Assert(cond, msg)
local uInt longest_match(deflate_state *s, IPos cur_match)
#define UPDATE_HASH(s, h, c)
int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size)
uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen)
int ZEXPORT deflateEnd(z_streamp strm)
int ZEXPORT deflatePrime(z_streamp strm, int bits, int value)
gz_header FAR * gz_headerp
const char deflate_copyright[]
#define _tr_tally_lit(s, c, flush)
#define ZALLOC(strm, items, size)
local int deflateStateCheck(z_streamp strm)
local block_state deflate_huff(deflate_state *s, int flush)
local block_state deflate_stored(deflate_state *s, int flush)
int ZEXPORT deflateCopy(z_streamp dest, z_streamp source)
int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
#define ZFREE(strm, addr)
local block_state deflate_fast(deflate_state *s, int flush)
local void slide_hash(deflate_state *s)
local const config configuration_table[10]
#define _tr_tally_dist(s, distance, length, flush)
int ZEXPORT deflate(z_streamp strm, int flush)
local void fill_window(deflate_state *s)
local block_state deflate_rle(deflate_state *s, int flush)
local void putShortMSB(deflate_state *s, uInt b)
local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size)
int ZEXPORT deflateResetKeep(z_streamp strm)
int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
#define ERR_RETURN(strm, err)
int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head)
#define FLUSH_BLOCK(s, last)
local void lm_init(deflate_state *s)
int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
local block_state deflate_slow(deflate_state *s, int flush)
#define Z_DEFAULT_STRATEGY
int ZEXPORT deflateReset(z_streamp strm)
int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits)
#define max_insert_length
#define Z_DEFAULT_COMPRESSION
#define FLUSH_BLOCK_ONLY(s, last)