55 " deflate 1.2.7 Copyright 1995-2012 Jean-loup Gailly and Mark Adler ";
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));
210 const char *
version,
int stream_size)
221 if (version ==
Z_NULL || version[0] != my_version[0] ||
228 if (strm->zalloc == (alloc_func)0) {
233 strm->opaque = (voidpf)0;
236 if (strm->zfree == (free_func)0)
244 if (level != 0) level = 1;
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) {
264 if (windowBits == 8) windowBits = 9;
328 strm->adler =
adler32(strm->adler, dictionary, dictLength);
332 if (dictLength >= s->
w_size) {
339 dictionary += 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;
370 strm->next_in = next;
371 strm->avail_in = avail;
382 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
386 strm->total_in = strm->total_out = 0;
426 strm->state->gzhead =
head;
435 *pending = strm->state->pending;
437 *bits = strm->state->bi_valid;
475 if (level != 0) level = 1;
479 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
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) {
534 uLong complen, wraplen;
538 complen = sourceLen +
539 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
552 wraplen = 6 + (s->
strstart ? 4 : 0);
558 wraplen += 2 + s->
gzhead->extra_len;
579 return complen + wraplen;
582 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
583 (sourceLen >> 25) + 13 - 6 + wraplen;
610 if (len > strm->avail_out) len = strm->avail_out;
611 if (len == 0)
return;
614 strm->next_out +=
len;
616 strm->total_out +=
len;
617 strm->avail_out -=
len;
631 flush >
Z_BLOCK || flush < 0) {
636 if (strm->next_out ==
Z_NULL ||
637 (strm->next_in ==
Z_NULL && strm->avail_in != 0) ||
669 (s->
gzhead->hcrc ? 2 : 0) +
701 else if (s->
level < 6)
703 else if (s->
level == 6)
707 header |= (level_flags << 6);
709 header += 31 - (header % 31);
816 put_byte(s, (Byte)(strm->adler & 0xff));
817 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
830 if (strm->avail_out == 0) {
845 }
else if (strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
857 if (strm->avail_in != 0 || s->
lookahead != 0 ||
863 (*(configuration_table[s->
level].func))(
s, flush));
869 if (strm->avail_out == 0) {
899 if (strm->avail_out == 0) {
905 Assert(strm->avail_out > 0,
"bug2");
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));
943 status = strm->state->status;
955 TRY_FREE(strm, strm->state->pending_buf);
958 TRY_FREE(strm, strm->state->window);
960 ZFREE(strm, strm->state);
995 ds->window = (Bytef *)
ZALLOC(dest, ds->w_size, 2*
sizeof(Byte));
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;
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;
1103 register Bytef *match;
1120 register ush scan_start = *(
ushf*)scan;
1121 register ush scan_end = *(
ushf*)(scan+best_len-1);
1124 register Byte scan_end1 = scan[best_len-1];
1125 register Byte scan_end = scan[best_len];
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) &&
1184 if (*scan == *match) scan++;
1186 len = (
MAX_MATCH - 1) - (
int)(strend-scan);
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 &&
1222 if (len > best_len) {
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;
1249 register Bytef *match;
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 &&
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)
1336 register unsigned n,
m;
1347 if (
sizeof(
int) <= 2) {
1351 }
else if (more == (
unsigned)(-1)) {
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");
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;
1802 Bytef *scan, *strend;
1822 if (prev == *++scan && prev == *++scan && prev == *++scan) {
1825 }
while (prev == *++scan && prev == *++scan &&
1826 prev == *++scan && prev == *++scan &&
1827 prev == *++scan && prev == *++scan &&
1828 prev == *++scan && prev == *++scan &&
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)
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
int ZEXPORT deflateResetKeep(z_streamp strm)
#define Assert(cond, msg)
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)
gz_header FAR * gz_headerp
int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
subroutine curr(MNUM, PIM1, PIM2, PIM3, PIM4, HADCUR)
int ZEXPORT deflatePrime(z_streamp strm, int bits, int value)
#define _tr_tally_lit(s, c, flush)
#define ZALLOC(strm, items, size)
#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)
#define ZFREE(strm, addr)
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)
const char deflate_copyright[]
#define _tr_tally_dist(s, distance, length, flush)
#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)
#define ERR_RETURN(strm, err)
const XML_Char int const XML_Char * value
local block_state deflate_stored(deflate_state *s, int flush)
int ZLIB_INTERNAL zmemcmp(const Bytef *s1, const Bytef *s2, uInt len)
#define Z_DEFAULT_STRATEGY
local block_state deflate_rle(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)
#define Z_DEFAULT_COMPRESSION
void wrap(std::string &str, const size_t lineLength=78, const std::string &separators=" \t")
voidpf alloc_func OF((voidpf opaque, uInt items, uInt size))