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)
157local void gen_trees_header
OF((
void));
161# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
165# define send_code(s, c, tree) \
166 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
167 send_bits(s, tree[c].Code, tree[c].Len); }
174#define put_short(s, w) { \
175 put_byte(s, (uch)((w) & 0xff)); \
176 put_byte(s, (uch)((ush)(w) >> 8)); \
191 Tracevv((stderr,
" l %2d v %4x ", length, value));
192 Assert(length > 0 && length <= 15,
"invalid length");
193 s->bits_sent += (
ulg)length;
199 if (s->bi_valid > (
int)
Buf_size - length) {
200 s->bi_buf |= (
ush)value << s->bi_valid;
202 s->bi_buf = (
ush)value >> (
Buf_size - s->bi_valid);
205 s->bi_buf |= (
ush)value << s->bi_valid;
206 s->bi_valid += length;
211#define send_bits(s, value, length) \
213 if (s->bi_valid > (int)Buf_size - len) {\
214 int val = (int)value;\
215 s->bi_buf |= (ush)val << s->bi_valid;\
216 put_short(s, s->bi_buf);\
217 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
218 s->bi_valid += len - Buf_size;\
220 s->bi_buf |= (ush)(value) << s->bi_valid;\
234#if defined(GEN_TREES_H) || !defined(STDC)
235 static int static_init_done = 0;
244 if (static_init_done)
return;
247#ifdef NO_INIT_GLOBAL_POINTERS
263 Assert (length == 256,
"tr_static_init: length != 256");
278 Assert (dist == 256,
"tr_static_init: dist != 256");
286 Assert (dist == 256,
"tr_static_init: 256 + dist != 512");
289 for (bits = 0; bits <=
MAX_BITS; bits++) bl_count[bits] = 0;
291 while (n <= 143)
static_ltree[n++].Len = 8, bl_count[8]++;
292 while (n <= 255)
static_ltree[n++].Len = 9, bl_count[9]++;
293 while (n <= 279)
static_ltree[n++].Len = 7, bl_count[7]++;
294 while (n <= 287)
static_ltree[n++].Len = 8, bl_count[8]++;
302 for (n = 0; n <
D_CODES; n++) {
306 static_init_done = 1;
322# define SEPARATOR(i, last, width) \
323 ((i) == (last)? "\n};\n\n" : \
324 ((i) % (width) == (width) - 1 ? ",\n" : ", "))
326void gen_trees_header()
328 FILE *header = fopen(
"trees.h",
"w");
331 Assert (header != NULL,
"Can't open trees.h");
333 "/* header created automatically with -DGEN_TREES_H */\n\n");
335 fprintf(header,
"local const ct_data static_ltree[L_CODES+2] = {\n");
336 for (i = 0; i <
L_CODES+2; i++) {
341 fprintf(header,
"local const ct_data static_dtree[D_CODES] = {\n");
342 for (i = 0; i <
D_CODES; i++) {
347 fprintf(header,
"const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
354 "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
360 fprintf(header,
"local const int base_length[LENGTH_CODES] = {\n");
366 fprintf(header,
"local const int base_dist[D_CODES] = {\n");
367 for (i = 0; i <
D_CODES; i++) {
384 s->l_desc.dyn_tree = s->dyn_ltree;
387 s->d_desc.dyn_tree = s->dyn_dtree;
390 s->bl_desc.dyn_tree = s->bl_tree;
396 s->compressed_len = 0L;
413 for (n = 0; n <
L_CODES; n++) s->dyn_ltree[n].Freq = 0;
414 for (n = 0; n <
D_CODES; n++) s->dyn_dtree[n].Freq = 0;
415 for (n = 0; n <
BL_CODES; n++) s->bl_tree[n].Freq = 0;
418 s->opt_len = s->static_len = 0L;
419 s->sym_next = s->matches = 0;
430#define pqremove(s, tree, top) \
432 top = s->heap[SMALLEST]; \
433 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
434 pqdownheap(s, tree, SMALLEST); \
441#define smaller(tree, n, m, depth) \
442 (tree[n].Freq < tree[m].Freq || \
443 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
458 while (j <= s->heap_len) {
460 if (j < s->heap_len &&
461 smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) {
465 if (
smaller(tree, v, s->heap[j], s->depth))
break;
468 s->heap[k] = s->heap[j]; k = j;
503 for (bits = 0; bits <=
MAX_BITS; bits++) s->bl_count[bits] = 0;
508 tree[s->heap[s->heap_max]].Len = 0;
510 for (h = s->heap_max + 1; h <
HEAP_SIZE; h++) {
512 bits = tree[tree[n].Dad].Len + 1;
513 if (bits > max_length) bits = max_length, overflow++;
514 tree[n].Len = (
ush)bits;
517 if (n > max_code)
continue;
521 if (n >= base) xbits = extra[n - base];
523 s->opt_len += (
ulg)f * (
unsigned)(bits + xbits);
524 if (stree) s->static_len += (
ulg)f * (
unsigned)(stree[n].Len + xbits);
526 if (overflow == 0)
return;
528 Tracev((stderr,
"\nbit length overflow\n"));
533 bits = max_length - 1;
534 while (s->bl_count[bits] == 0) bits--;
536 s->bl_count[bits + 1] += 2;
537 s->bl_count[max_length]--;
542 }
while (overflow > 0);
549 for (bits = max_length; bits != 0; bits--) {
550 n = s->bl_count[bits];
553 if (m > max_code)
continue;
554 if ((
unsigned) tree[m].
Len != (
unsigned) bits) {
555 Tracev((stderr,
"code %d bits %d->%d\n", m, tree[m].
Len, bits));
556 s->opt_len += ((
ulg)bits - tree[m].Len) * tree[m].
Freq;
557 tree[m].Len = (
ush)bits;
585 for (bits = 1; bits <=
MAX_BITS; bits++) {
586 code = (
code + bl_count[bits - 1]) << 1;
593 "inconsistent bit counts");
594 Tracev((stderr,
"\ngen_codes: max_code %d ", max_code));
596 for (n = 0; n <= max_code; n++) {
597 int len = tree[n].Len;
598 if (len == 0)
continue;
603 n, (isgraph(n) ? n :
' '), len, tree[n].
Code, next_code[len] - 1));
630 s->heap_len = 0, s->heap_max =
HEAP_SIZE;
632 for (n = 0; n < elems; n++) {
633 if (tree[n].
Freq != 0) {
634 s->heap[++(s->heap_len)] = max_code = n;
646 while (s->heap_len < 2) {
647 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
650 s->opt_len--;
if (stree) s->static_len -= stree[node].Len;
658 for (n = s->heap_len/2; n >= 1; n--)
pqdownheap(s, tree, n);
668 s->heap[--(s->heap_max)] = n;
669 s->heap[--(s->heap_max)] = m;
672 tree[node].Freq = tree[n].Freq + tree[m].Freq;
673 s->depth[node] = (
uch)((s->depth[n] >= s->depth[m] ?
674 s->depth[n] : s->depth[m]) + 1);
675 tree[n].Dad = tree[m].Dad = (
ush)node;
677 if (tree == s->bl_tree) {
678 fprintf(stderr,
"\nnode %d(%d), sons %d(%d) %d(%d)",
679 node, tree[node].
Freq, n, tree[n].
Freq, m, tree[m].
Freq);
686 }
while (s->heap_len >= 2);
688 s->heap[--(s->heap_max)] = s->heap[
SMALLEST];
711 int nextlen = tree[0].Len;
716 if (nextlen == 0) max_count = 138, min_count = 3;
717 tree[max_code + 1].Len = (
ush)0xffff;
719 for (n = 0; n <= max_code; n++) {
720 curlen = nextlen; nextlen = tree[n + 1].Len;
721 if (++count < max_count && curlen == nextlen) {
723 }
else if (count < min_count) {
724 s->bl_tree[curlen].Freq += count;
725 }
else if (curlen != 0) {
726 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
728 }
else if (count <= 10) {
733 count = 0; prevlen = curlen;
735 max_count = 138, min_count = 3;
736 }
else if (curlen == nextlen) {
737 max_count = 6, min_count = 3;
739 max_count = 7, min_count = 4;
756 int nextlen = tree[0].Len;
762 if (nextlen == 0) max_count = 138, min_count = 3;
764 for (n = 0; n <= max_code; n++) {
765 curlen = nextlen; nextlen = tree[n + 1].Len;
766 if (++count < max_count && curlen == nextlen) {
768 }
else if (count < min_count) {
769 do {
send_code(s, curlen, s->bl_tree); }
while (--count != 0);
771 }
else if (curlen != 0) {
772 if (curlen != prevlen) {
773 send_code(s, curlen, s->bl_tree); count--;
775 Assert(count >= 3 && count <= 6,
" 3_6?");
778 }
else if (count <= 10) {
784 count = 0; prevlen = curlen;
786 max_count = 138, min_count = 3;
787 }
else if (curlen == nextlen) {
788 max_count = 6, min_count = 3;
790 max_count = 7, min_count = 4;
818 for (max_blindex =
BL_CODES-1; max_blindex >= 3; max_blindex--) {
819 if (s->bl_tree[
bl_order[max_blindex]].Len != 0)
break;
822 s->opt_len += 3*((
ulg)max_blindex + 1) + 5 + 5 + 4;
823 Tracev((stderr,
"\ndyn trees: dyn %ld, stat %ld",
824 s->opt_len, s->static_len));
836 int lcodes, dcodes, blcodes;
840 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4,
"not enough codes");
843 Tracev((stderr,
"\nbl counts: "));
847 for (rank = 0; rank < blcodes; rank++) {
851 Tracev((stderr,
"\nbl tree: sent %ld", s->bits_sent));
854 Tracev((stderr,
"\nlit tree: sent %ld", s->bits_sent));
857 Tracev((stderr,
"\ndist tree: sent %ld", s->bits_sent));
874 zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len);
875 s->pending += stored_len;
877 s->compressed_len = (s->compressed_len + 3 + 7) & (
ulg)~7L;
878 s->compressed_len += (stored_len + 4) << 3;
879 s->bits_sent += 2*16;
880 s->bits_sent += stored_len << 3;
903 s->compressed_len += 10L;
918 ulg opt_lenb, static_lenb;
930 Tracev((stderr,
"\nlit data: dyn %ld, stat %ld", s->opt_len,
934 Tracev((stderr,
"\ndist data: dyn %ld, stat %ld", s->opt_len,
946 opt_lenb = (s->opt_len + 3 + 7) >> 3;
947 static_lenb = (s->static_len + 3 + 7) >> 3;
949 Tracev((stderr,
"\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
950 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
954 if (static_lenb <= opt_lenb || s->strategy ==
Z_FIXED)
956 opt_lenb = static_lenb;
959 Assert(buf != (
char*)0,
"lost buf");
960 opt_lenb = static_lenb = stored_len + 5;
964 if (buf != (
char*)0) {
966 if (stored_len + 4 <= opt_lenb && buf != (
char*)0) {
977 }
else if (static_lenb == opt_lenb) {
982 s->compressed_len += 3 + s->static_len;
989 (
const ct_data *)s->dyn_dtree);
991 s->compressed_len += 3 + s->opt_len;
994 Assert (s->compressed_len == s->bits_sent,
"bad compressed size");
1003 s->compressed_len += 7;
1006 Tracev((stderr,
"\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
1007 s->compressed_len - 7*last));
1019 s->sym_buf[s->sym_next++] = (
uch)dist;
1020 s->sym_buf[s->sym_next++] = (
uch)(dist >> 8);
1021 s->sym_buf[s->sym_next++] = (
uch)lc;
1024 s->dyn_ltree[lc].Freq++;
1034 s->dyn_dtree[
d_code(dist)].Freq++;
1036 return (s->sym_next == s->sym_end);
1053 if (s->sym_next != 0)
do {
1054 dist = s->sym_buf[sx++] & 0xff;
1055 dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
1056 lc = s->sym_buf[sx++];
1059 Tracecv(isgraph(lc), (stderr,
" '%c' ", lc));
1082 Assert(s->pending < s->lit_bufsize + sx,
"pendingBuf overflow");
1084 }
while (sx < s->sym_next);
1109 unsigned long block_mask = 0xf3ffc07fUL;
1113 for (n = 0; n <= 31; n++, block_mask >>= 1)
1114 if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1118 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1119 || s->dyn_ltree[13].Freq != 0)
1122 if (s->dyn_ltree[n].Freq != 0)
1140 register unsigned res = 0;
1143 code >>= 1, res <<= 1;
1144 }
while (--len > 0);
1154 if (s->bi_valid == 16) {
1158 }
else if (s->bi_valid >= 8) {
1171 if (s->bi_valid > 8) {
1173 }
else if (s->bi_valid > 0) {
1179 s->bits_sent = (s->bits_sent + 7) & ~7;
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 ct_data static_dtree[D_CODES]
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)
local const static_tree_desc static_bl_desc
void ZLIB_INTERNAL _tr_init(deflate_state *s)
local const static_tree_desc static_d_desc
local void pqdownheap(deflate_state *s, ct_data *tree, int k)
#define send_code(s, c, tree)
local ct_data static_ltree[L_CODES+2]
local const static_tree_desc static_l_desc
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]
#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 int base_length[LENGTH_CODES]
local void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes)
uch _length_code[MAX_MATCH-MIN_MATCH+1]
uch _dist_code[DIST_CODE_LEN]
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 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 void tr_static_init()
local int base_dist[D_CODES]
voidpf alloc_func OF((voidpf opaque, uInt items, uInt size))
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
#define Assert(cond, msg)