55 " deflate 1.2.3 Copyright 1995-2005 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)); 236 if (version ==
Z_NULL || version[0] != my_version[0] ||
243 if (strm->zalloc == (alloc_func)0) {
247 if (strm->zfree == (free_func)0) strm->zfree =
zcfree;
250 if (level != 0) level = 1;
255 if (windowBits < 0) {
257 windowBits = -windowBits;
260 else if (windowBits > 15) {
266 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
267 strategy < 0 || strategy >
Z_FIXED) {
270 if (windowBits == 8) windowBits = 9;
326 strm->state->wrap == 2 ||
327 (strm->state->wrap == 1 && strm->state->status !=
INIT_STATE))
332 strm->adler =
adler32(strm->adler, dictionary, dictLength);
337 dictionary += dictLength -
length;
349 for (n = 0; n <= length -
MIN_MATCH; n++) {
352 if (hash_head) hash_head = 0;
363 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
367 strm->total_in = strm->total_out = 0;
399 strm->state->gzhead =
head;
410 strm->state->bi_valid = bits;
411 strm->state->bi_buf = (
ush)(value & ((1 << bits) - 1));
429 if (level != 0) level = 1;
433 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
436 func = configuration_table[s->
level].func;
438 if (func != configuration_table[level].func && strm->total_in != 0) {
442 if (s->
level != level) {
497 destLen = sourceLen +
498 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
535 unsigned len = strm->state->pending;
537 if (len > strm->avail_out) len = strm->avail_out;
538 if (len == 0)
return;
540 zmemcpy(strm->next_out, strm->state->pending_out, len);
541 strm->next_out += len;
542 strm->state->pending_out += len;
543 strm->total_out += len;
544 strm->avail_out -= len;
545 strm->state->pending -= len;
546 if (strm->state->pending == 0) {
547 strm->state->pending_out = strm->state->pending_buf;
565 if (strm->next_out ==
Z_NULL ||
566 (strm->next_in ==
Z_NULL && strm->avail_in != 0) ||
598 (s->
gzhead->hcrc ? 2 : 0) +
611 if (s->
gzhead->extra != NULL) {
630 else if (s->
level < 6)
632 else if (s->
level == 6)
636 header |= (level_flags << 6);
638 header += 31 - (header % 31);
653 if (s->
gzhead->extra != NULL) {
681 if (s->
gzhead->name != NULL) {
712 if (s->
gzhead->comment != NULL) {
759 if (strm->avail_out == 0) {
774 }
else if (strm->avail_in == 0 && flush <= old_flush &&
786 if (strm->avail_in != 0 || s->
lookahead != 0 ||
790 bstate = (*(configuration_table[s->
level].func))(s, flush);
796 if (strm->avail_out == 0) {
821 if (strm->avail_out == 0) {
827 Assert(strm->avail_out > 0,
"bug2");
866 status = strm->state->status;
878 TRY_FREE(strm, strm->state->pending_buf);
881 TRY_FREE(strm, strm->state->window);
883 ZFREE(strm, strm->state);
961 unsigned len = strm->avail_in;
963 if (len > size) len =
size;
964 if (len == 0)
return 0;
966 strm->avail_in -= len;
968 if (strm->state->wrap == 1) {
969 strm->adler =
adler32(strm->adler, strm->next_in, len);
972 else if (strm->state->wrap == 2) {
973 strm->adler =
crc32(strm->adler, strm->next_in, len);
976 zmemcpy(buf, strm->next_in, len);
977 strm->next_in += len;
978 strm->total_in += len;
995 s->max_lazy_match = configuration_table[s->level].max_lazy;
996 s->good_match = configuration_table[s->level].good_length;
997 s->nice_match = configuration_table[s->level].nice_length;
998 s->max_chain_length = configuration_table[s->level].max_chain;
1001 s->block_start = 0L;
1003 s->match_length = s->prev_length =
MIN_MATCH-1;
1004 s->match_available = 0;
1032 register Bytef *scan = s->window + s->strstart;
1033 register Bytef *match;
1035 int best_len = s->prev_length;
1043 uInt wmask = s->w_mask;
1049 register Bytef *strend = s->window + s->strstart +
MAX_MATCH - 1;
1050 register ush scan_start = *(
ushf*)scan;
1051 register ush scan_end = *(
ushf*)(scan+best_len-1);
1054 register Byte scan_end1 = scan[best_len-1];
1055 register Byte scan_end = scan[best_len];
1064 if (s->prev_length >= s->good_match) {
1070 if ((
uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1076 match = s->window + cur_match;
1086 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1090 if (*(
ushf*)(match+best_len-1) != scan_end ||
1091 *(
ushf*)match != scan_start)
continue;
1102 Assert(scan[2] == match[2],
"scan[2]?");
1105 }
while (*(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1106 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1107 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1108 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1113 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1114 if (*scan == *match) scan++;
1116 len = (
MAX_MATCH - 1) - (
int)(strend-scan);
1121 if (match[best_len] != scan_end ||
1122 match[best_len-1] != scan_end1 ||
1124 *++match != scan[1])
continue;
1133 Assert(*scan == *match,
"match[2]?");
1139 }
while (*++scan == *++match && *++scan == *++match &&
1140 *++scan == *++match && *++scan == *++match &&
1141 *++scan == *++match && *++scan == *++match &&
1142 *++scan == *++match && *++scan == *++match &&
1145 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1152 if (len > best_len) {
1153 s->match_start = cur_match;
1155 if (len >= nice_match)
break;
1157 scan_end = *(
ushf*)(scan+best_len-1);
1159 scan_end1 = scan[best_len-1];
1160 scan_end = scan[best_len];
1163 }
while ((cur_match = prev[cur_match & wmask]) > limit
1164 && --chain_length != 0);
1166 if ((
uInt)best_len <= s->lookahead)
return (
uInt)best_len;
1167 return s->lookahead;
1180 register Bytef *match;
1193 match = s->window + cur_match;
1197 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1205 scan += 2, match += 2;
1206 Assert(*scan == *match,
"match[2]?");
1212 }
while (*++scan == *++match && *++scan == *++match &&
1213 *++scan == *++match && *++scan == *++match &&
1214 *++scan == *++match && *++scan == *++match &&
1215 *++scan == *++match && *++scan == *++match &&
1218 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1224 s->match_start = cur_match;
1225 return (
uInt)len <= s->lookahead ? (
uInt)len : s->lookahead;
1238 if (
zmemcmp(s->window + match,
1239 s->window + start, length) !=
EQUAL) {
1240 fprintf(stderr,
" start %u, match %u, length %d\n",
1241 start, match, length);
1243 fprintf(stderr,
"%c%c", s->window[match++], s->window[start++]);
1244 }
while (--length != 0);
1245 z_error(
"invalid match");
1247 if (z_verbose > 1) {
1248 fprintf(stderr,
"\\[%d,%d]", start-match, length);
1249 do { putc(s->window[start++], stderr); }
while (--length != 0);
1253 # define check_match(s, start, match, length) 1269 register unsigned n, m;
1272 uInt wsize = s->w_size;
1275 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1278 if (
sizeof(
int) <= 2) {
1279 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1282 }
else if (more == (
unsigned)(-1)) {
1293 if (s->strstart >= wsize+
MAX_DIST(s)) {
1295 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize);
1296 s->match_start -= wsize;
1297 s->strstart -= wsize;
1298 s->block_start -= (long) wsize;
1311 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1319 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1327 if (s->strm->avail_in == 0)
return;
1340 Assert(more >= 2,
"more < 2");
1342 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1347 s->ins_h = s->window[s->strstart];
1348 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1357 }
while (s->lookahead <
MIN_LOOKAHEAD && s->strm->avail_in != 0);
1364 #define FLUSH_BLOCK_ONLY(s, eof) { \ 1365 _tr_flush_block(s, (s->block_start >= 0L ? \ 1366 (charf *)&s->window[(unsigned)s->block_start] : \ 1368 (ulg)((long)s->strstart - s->block_start), \ 1370 s->block_start = s->strstart; \ 1371 flush_pending(s->strm); \ 1372 Tracev((stderr,"[FLUSH]")); \ 1376 #define FLUSH_BLOCK(s, eof) { \ 1377 FLUSH_BLOCK_ONLY(s, eof); \ 1378 if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \ 1397 ulg max_block_size = 0xffff;
1400 if (max_block_size > s->pending_buf_size - 5) {
1407 if (s->lookahead <= 1) {
1410 s->block_start >= (
long)s->w_size,
"slide too late");
1415 if (s->lookahead == 0)
break;
1417 Assert(s->block_start >= 0L,
"block gone");
1419 s->strstart += s->lookahead;
1423 max_start = s->block_start + max_block_size;
1424 if (s->strstart == 0 || (
ulg)s->strstart >= max_start) {
1426 s->lookahead = (
uInt)(s->strstart - max_start);
1427 s->strstart = (
uInt)max_start;
1433 if (s->strstart - (
uInt)s->block_start >=
MAX_DIST(s)) {
1466 if (s->lookahead == 0)
break;
1479 if (hash_head !=
NIL && s->strstart - hash_head <=
MAX_DIST(s)) {
1486 (s->strategy ==
Z_RLE && s->strstart - hash_head == 1)) {
1492 }
else if (s->strategy ==
Z_RLE && s->strstart - hash_head == 1) {
1499 check_match(s, s->strstart, s->match_start, s->match_length);
1504 s->lookahead -= s->match_length;
1510 if (s->match_length <= s->max_insert_length &&
1519 }
while (--s->match_length != 0);
1524 s->strstart += s->match_length;
1525 s->match_length = 0;
1526 s->ins_h = s->window[s->strstart];
1527 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1537 Tracevv((stderr,
"%c", s->window[s->strstart]));
1573 if (s->lookahead == 0)
break;
1585 s->prev_length = s->match_length, s->prev_match = s->match_start;
1588 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
1589 s->strstart - hash_head <=
MAX_DIST(s)) {
1596 }
else if (s->strategy ==
Z_RLE && s->strstart - hash_head == 1) {
1601 if (s->match_length <= 5 && (s->strategy ==
Z_FILTERED 1604 s->strstart - s->match_start >
TOO_FAR)
1617 if (s->prev_length >=
MIN_MATCH && s->match_length <= s->prev_length) {
1621 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1631 s->lookahead -= s->prev_length-1;
1632 s->prev_length -= 2;
1634 if (++s->strstart <= max_insert) {
1637 }
while (--s->prev_length != 0);
1638 s->match_available = 0;
1644 }
else if (s->match_available) {
1649 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1656 if (s->strm->avail_out == 0)
return need_more;
1661 s->match_available = 1;
1667 if (s->match_available) {
1668 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1670 s->match_available = 0;
1703 if (s->lookahead == 0)
break;
1708 if (s->strstart > 0) {
1710 scan = s->window + s->strstart - 1;
1713 if (*scan++ != prev)
1715 }
while (++run < max);
1720 check_match(s, s->strstart, s->strstart - 1, run);
1722 s->lookahead -= run;
1726 Tracevv((stderr,
"%c", s->window[s->strstart]));
uLong ZEXPORT compressBound(uLong sourceLen)
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
void zcfree(voidpf opaque, voidpf ptr)
#define INSERT_STRING(s, str, match_head)
#define check_match(s, start, match, length)
local void flush_pending(z_streamp strm)
void zmemcpy(Bytef *dest, const Bytef *source, uInt len)
#define FLUSH_BLOCK(s, eof)
#define Assert(cond, msg)
local uInt longest_match(deflate_state *s, IPos cur_match)
void _tr_init(deflate_state *s)
#define UPDATE_HASH(s, h, c)
struct ct_data_s dyn_ltree[HEAP_SIZE]
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[]
png_bytep png_bytep png_size_t length
local int read_buf(z_streamp strm, Bytef *buf, unsigned size)
int zmemcmp(Bytef *s1, const Bytef *s2, uInt len) const
#define _tr_tally_lit(s, c, flush)
#define ZALLOC(strm, items, size)
void _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int eof)
local block_state deflate_stored(deflate_state *s, int flush)
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, unsigned len)
struct tree_desc_s bl_desc
struct tree_desc_s d_desc
voidpf zcalloc(voidpf opaque, unsigned items, unsigned size)
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)
void _tr_align(deflate_state *s)
local uInt longest_match_fast(deflate_state *s, IPos cur_match)
#define ZFREE(strm, addr)
local block_state deflate_fast(deflate_state *s, int flush)
block_state compress_func OF((deflate_state *s, int flush))
local const config configuration_table[10]
#define _tr_tally_dist(s, distance, length, flush)
int ZEXPORT deflate(z_streamp strm, int flush)
#define FLUSH_BLOCK_ONLY(s, eof)
struct ct_data_s dyn_dtree[2 *D_CODES+1]
local void fill_window(deflate_state *s)
struct tree_desc_s l_desc
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
local void putShortMSB(deflate_state *s, uInt b)
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)
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)
struct ct_data_s bl_tree[2 *BL_CODES+1]
static int max(int a, int b)
#define Z_DEFAULT_COMPRESSION