63 = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
66 = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
69 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
72 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
81#define DIST_CODE_LEN 512
83#if defined(GEN_TREES_H) || !defined(STDC)
125#ifdef NO_INIT_GLOBAL_POINTERS
144#define put_short(s, w) { \
145 put_byte(s, (uch)((w) & 0xff)); \
146 put_byte(s, (uch)((ush)(w) >> 8)); \
155 register unsigned res = 0;
158 code >>= 1, res <<= 1;
167 if (s->bi_valid == 16) {
171 }
else if (s->bi_valid >= 8) {
182 if (s->bi_valid > 8) {
184 }
else if (s->bi_valid > 0) {
190 s->bits_sent = (s->bits_sent + 7) & ~7;
211 for (bits = 1; bits <=
MAX_BITS; bits++) {
212 code = (
code + bl_count[bits - 1]) << 1;
219 "inconsistent bit counts");
220 Tracev((stderr,
"\ngen_codes: max_code %d ", max_code));
222 for (n = 0; n <= max_code; n++) {
223 int len = tree[n].Len;
224 if (len == 0)
continue;
229 n, (isgraph(n) ? n :
' '), len, tree[n].
Code, next_code[len] - 1));
234local void gen_trees_header(
void);
238# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
242# define send_code(s, c, tree) \
243 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
244 send_bits(s, tree[c].Code, tree[c].Len); }
253 Tracevv((stderr,
" l %2d v %4x ", length, value));
254 Assert(length > 0 && length <= 15,
"invalid length");
255 s->bits_sent += (
ulg)length;
261 if (s->bi_valid > (
int)
Buf_size - length) {
262 s->bi_buf |= (
ush)value << s->bi_valid;
264 s->bi_buf = (
ush)value >> (
Buf_size - s->bi_valid);
267 s->bi_buf |= (
ush)value << s->bi_valid;
268 s->bi_valid += length;
273#define send_bits(s, value, length) \
275 if (s->bi_valid > (int)Buf_size - len) {\
276 int val = (int)value;\
277 s->bi_buf |= (ush)val << s->bi_valid;\
278 put_short(s, s->bi_buf);\
279 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
280 s->bi_valid += len - Buf_size;\
282 s->bi_buf |= (ush)(value) << s->bi_valid;\
295#if defined(GEN_TREES_H) || !defined(STDC)
296 static int static_init_done = 0;
305 if (static_init_done)
return;
308#ifdef NO_INIT_GLOBAL_POINTERS
324 Assert (length == 256,
"tr_static_init: length != 256");
339 Assert (dist == 256,
"tr_static_init: dist != 256");
347 Assert (dist == 256,
"tr_static_init: 256 + dist != 512");
350 for (bits = 0; bits <=
MAX_BITS; bits++) bl_count[bits] = 0;
352 while (n <= 143)
static_ltree[n++].Len = 8, bl_count[8]++;
353 while (n <= 255)
static_ltree[n++].Len = 9, bl_count[9]++;
354 while (n <= 279)
static_ltree[n++].Len = 7, bl_count[7]++;
355 while (n <= 287)
static_ltree[n++].Len = 8, bl_count[8]++;
363 for (n = 0; n <
D_CODES; n++) {
367 static_init_done = 1;
383# define SEPARATOR(i, last, width) \
384 ((i) == (last)? "\n};\n\n" : \
385 ((i) % (width) == (width) - 1 ? ",\n" : ", "))
387void gen_trees_header(
void) {
388 FILE *header = fopen(
"trees.h",
"w");
391 Assert (header != NULL,
"Can't open trees.h");
393 "/* header created automatically with -DGEN_TREES_H */\n\n");
395 fprintf(header,
"local const ct_data static_ltree[L_CODES+2] = {\n");
396 for (i = 0; i <
L_CODES+2; i++) {
401 fprintf(header,
"local const ct_data static_dtree[D_CODES] = {\n");
402 for (i = 0; i <
D_CODES; i++) {
407 fprintf(header,
"const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
414 "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
420 fprintf(header,
"local const int base_length[LENGTH_CODES] = {\n");
426 fprintf(header,
"local const int base_dist[D_CODES] = {\n");
427 for (i = 0; i <
D_CODES; i++) {
443 for (n = 0; n <
L_CODES; n++) s->dyn_ltree[n].Freq = 0;
444 for (n = 0; n <
D_CODES; n++) s->dyn_dtree[n].Freq = 0;
445 for (n = 0; n <
BL_CODES; n++) s->bl_tree[n].Freq = 0;
448 s->opt_len = s->static_len = 0L;
449 s->sym_next = s->matches = 0;
458 s->l_desc.dyn_tree = s->dyn_ltree;
461 s->d_desc.dyn_tree = s->dyn_dtree;
464 s->bl_desc.dyn_tree = s->bl_tree;
470 s->compressed_len = 0L;
486#define pqremove(s, tree, top) \
488 top = s->heap[SMALLEST]; \
489 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
490 pqdownheap(s, tree, SMALLEST); \
497#define smaller(tree, n, m, depth) \
498 (tree[n].Freq < tree[m].Freq || \
499 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
510 while (j <= s->heap_len) {
512 if (j < s->heap_len &&
513 smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) {
517 if (
smaller(tree, v, s->heap[j], s->depth))
break;
520 s->heap[k] = s->heap[j]; k = j;
552 for (bits = 0; bits <=
MAX_BITS; bits++) s->bl_count[bits] = 0;
557 tree[s->heap[s->heap_max]].Len = 0;
559 for (h = s->heap_max + 1; h <
HEAP_SIZE; h++) {
561 bits = tree[tree[n].Dad].Len + 1;
562 if (bits > max_length) bits = max_length, overflow++;
563 tree[n].Len = (
ush)bits;
566 if (n > max_code)
continue;
570 if (n >= base) xbits = extra[n - base];
572 s->opt_len += (
ulg)f * (
unsigned)(bits + xbits);
573 if (stree) s->static_len += (
ulg)f * (
unsigned)(stree[n].Len + xbits);
575 if (overflow == 0)
return;
577 Tracev((stderr,
"\nbit length overflow\n"));
582 bits = max_length - 1;
583 while (s->bl_count[bits] == 0) bits--;
585 s->bl_count[bits + 1] += 2;
586 s->bl_count[max_length]--;
591 }
while (overflow > 0);
598 for (bits = max_length; bits != 0; bits--) {
599 n = s->bl_count[bits];
602 if (m > max_code)
continue;
603 if ((
unsigned) tree[m].
Len != (
unsigned) bits) {
604 Tracev((stderr,
"code %d bits %d->%d\n", m, tree[m].
Len, bits));
605 s->opt_len += ((
ulg)bits - tree[m].Len) * tree[m].
Freq;
606 tree[m].Len = (
ush)bits;
637 s->heap_len = 0, s->heap_max =
HEAP_SIZE;
639 for (n = 0; n < elems; n++) {
640 if (tree[n].
Freq != 0) {
641 s->heap[++(s->heap_len)] = max_code = n;
653 while (s->heap_len < 2) {
654 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
657 s->opt_len--;
if (stree) s->static_len -= stree[node].Len;
665 for (n = s->heap_len/2; n >= 1; n--)
pqdownheap(s, tree, n);
675 s->heap[--(s->heap_max)] = n;
676 s->heap[--(s->heap_max)] = m;
679 tree[node].Freq = tree[n].Freq + tree[m].Freq;
680 s->depth[node] = (
uch)((s->depth[n] >= s->depth[m] ?
681 s->depth[n] : s->depth[m]) + 1);
682 tree[n].Dad = tree[m].Dad = (
ush)node;
684 if (tree == s->bl_tree) {
685 fprintf(stderr,
"\nnode %d(%d), sons %d(%d) %d(%d)",
686 node, tree[node].
Freq, n, tree[n].
Freq, m, tree[m].
Freq);
693 }
while (s->heap_len >= 2);
695 s->heap[--(s->heap_max)] = s->heap[
SMALLEST];
714 int nextlen = tree[0].Len;
719 if (nextlen == 0) max_count = 138, min_count = 3;
720 tree[max_code + 1].Len = (
ush)0xffff;
722 for (n = 0; n <= max_code; n++) {
723 curlen = nextlen; nextlen = tree[n + 1].Len;
724 if (++count < max_count && curlen == nextlen) {
726 }
else if (count < min_count) {
727 s->bl_tree[curlen].Freq += count;
728 }
else if (curlen != 0) {
729 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
731 }
else if (count <= 10) {
736 count = 0; prevlen = curlen;
738 max_count = 138, min_count = 3;
739 }
else if (curlen == nextlen) {
740 max_count = 6, min_count = 3;
742 max_count = 7, min_count = 4;
755 int nextlen = tree[0].Len;
761 if (nextlen == 0) max_count = 138, min_count = 3;
763 for (n = 0; n <= max_code; n++) {
764 curlen = nextlen; nextlen = tree[n + 1].Len;
765 if (++count < max_count && curlen == nextlen) {
767 }
else if (count < min_count) {
768 do {
send_code(s, curlen, s->bl_tree); }
while (--count != 0);
770 }
else if (curlen != 0) {
771 if (curlen != prevlen) {
772 send_code(s, curlen, s->bl_tree); count--;
774 Assert(count >= 3 && count <= 6,
" 3_6?");
777 }
else if (count <= 10) {
783 count = 0; prevlen = curlen;
785 max_count = 138, min_count = 3;
786 }
else if (curlen == nextlen) {
787 max_count = 6, min_count = 3;
789 max_count = 7, min_count = 4;
815 for (max_blindex =
BL_CODES-1; max_blindex >= 3; max_blindex--) {
816 if (s->bl_tree[
bl_order[max_blindex]].Len != 0)
break;
819 s->opt_len += 3*((
ulg)max_blindex + 1) + 5 + 5 + 4;
820 Tracev((stderr,
"\ndyn trees: dyn %ld, stat %ld",
821 s->opt_len, s->static_len));
835 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4,
"not enough codes");
838 Tracev((stderr,
"\nbl counts: "));
842 for (rank = 0; rank < blcodes; rank++) {
846 Tracev((stderr,
"\nbl tree: sent %ld", s->bits_sent));
849 Tracev((stderr,
"\nlit tree: sent %ld", s->bits_sent));
852 Tracev((stderr,
"\ndist tree: sent %ld", s->bits_sent));
859 ulg stored_len,
int last) {
865 zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len);
866 s->pending += stored_len;
868 s->compressed_len = (s->compressed_len + 3 + 7) & (
ulg)~7L;
869 s->compressed_len += (stored_len + 4) << 3;
870 s->bits_sent += 2*16;
871 s->bits_sent += stored_len << 3;
890 s->compressed_len += 10L;
906 if (s->sym_next != 0)
do {
911 dist = s->sym_buf[sx++] & 0xff;
912 dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
913 lc = s->sym_buf[sx++];
917 Tracecv(isgraph(lc), (stderr,
" '%c' ", lc));
941 Assert(s->pending < 2 * (s->lit_bufsize + sx),
"pendingBuf overflow");
943 Assert(s->pending < s->lit_bufsize + sx,
"pendingBuf overflow");
946 }
while (sx < s->sym_next);
969 unsigned long block_mask = 0xf3ffc07fUL;
973 for (n = 0; n <= 31; n++, block_mask >>= 1)
974 if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0))
978 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
979 || s->dyn_ltree[13].Freq != 0)
982 if (s->dyn_ltree[n].Freq != 0)
996 ulg stored_len,
int last) {
997 ulg opt_lenb, static_lenb;
1009 Tracev((stderr,
"\nlit data: dyn %ld, stat %ld", s->opt_len,
1013 Tracev((stderr,
"\ndist data: dyn %ld, stat %ld", s->opt_len,
1025 opt_lenb = (s->opt_len + 3 + 7) >> 3;
1026 static_lenb = (s->static_len + 3 + 7) >> 3;
1028 Tracev((stderr,
"\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
1029 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
1033 if (static_lenb <= opt_lenb || s->strategy ==
Z_FIXED)
1035 opt_lenb = static_lenb;
1038 Assert(buf != (
char*)0,
"lost buf");
1039 opt_lenb = static_lenb = stored_len + 5;
1043 if (buf != (
char*)0) {
1045 if (stored_len + 4 <= opt_lenb && buf != (
char*)0) {
1056 }
else if (static_lenb == opt_lenb) {
1061 s->compressed_len += 3 + s->static_len;
1065 send_all_trees(s, s->l_desc.max_code + 1, s->d_desc.max_code + 1,
1068 (
const ct_data *)s->dyn_dtree);
1070 s->compressed_len += 3 + s->opt_len;
1073 Assert (s->compressed_len == s->bits_sent,
"bad compressed size");
1082 s->compressed_len += 7;
1085 Tracev((stderr,
"\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
1086 s->compressed_len - 7*last));
1095 s->d_buf[s->sym_next] = (
ush)dist;
1096 s->l_buf[s->sym_next++] = (
uch)lc;
1098 s->sym_buf[s->sym_next++] = (
uch)dist;
1099 s->sym_buf[s->sym_next++] = (
uch)(dist >> 8);
1100 s->sym_buf[s->sym_next++] = (
uch)lc;
1104 s->dyn_ltree[lc].Freq++;
1114 s->dyn_dtree[
d_code(dist)].Freq++;
1116 return (s->sym_next == s->sym_end);
struct static_tree_desc_s static_tree_desc
uch ZLIB_INTERNAL _length_code[]
struct tree_desc_s tree_desc
struct internal_state deflate_state
uch ZLIB_INTERNAL _dist_code[]
const ct_data * static_tree
const static_tree_desc * stat_desc
local void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree)
local void gen_bitlen(deflate_state *s, tree_desc *desc)
local const int extra_dbits[D_CODES]
int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc)
void ZLIB_INTERNAL _tr_init(deflate_state *s)
local TCONST static_tree_desc static_d_desc
local void pqdownheap(deflate_state *s, ct_data *tree, int k)
#define send_code(s, c, tree)
local void gen_codes(ct_data *tree, int max_code, ushf *bl_count)
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
local void send_tree(deflate_state *s, ct_data *tree, int max_code)
void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s)
local const uch bl_order[BL_CODES]
local void tr_static_init(void)
#define smaller(tree, n, m, depth)
local void scan_tree(deflate_state *s, ct_data *tree, int max_code)
local const int extra_lbits[LENGTH_CODES]
local void bi_windup(deflate_state *s)
local void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes)
void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, ulg stored_len, int last)
local void bi_flush(deflate_state *s)
local int detect_data_type(deflate_state *s)
void ZLIB_INTERNAL _tr_align(deflate_state *s)
#define pqremove(s, tree, top)
local void init_block(deflate_state *s)
local unsigned bi_reverse(unsigned code, int len)
local TCONST static_tree_desc static_l_desc
local const int extra_blbits[BL_CODES]
local void build_tree(deflate_state *s, tree_desc *desc)
#define send_bits(s, value, length)
local int build_bl_tree(deflate_state *s)
local TCONST static_tree_desc static_bl_desc
local const ct_data static_dtree[D_CODES]
local const ct_data static_ltree[L_CODES+2]
local const int base_dist[D_CODES]
local const int base_length[LENGTH_CODES]
#define Assert(cond, msg)
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)