59 #define REPZ_11_138 18 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};
77 #define Buf_size (8 * 2*sizeof(char)) 86 #define DIST_CODE_LEN 512 88 #if defined(GEN_TREES_H) || !defined(STDC) 164 local void gen_trees_header
OF((
void));
168 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) 172 # define send_code(s, c, tree) \ 173 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ 174 send_bits(s, tree[c].Code, tree[c].Len); } 181 #define put_short(s, w) { \ 182 put_byte(s, (uch)((w) & 0xff)); \ 183 put_byte(s, (uch)((ush)(w) >> 8)); \ 198 Tracevv((stderr,
" l %2d v %4x ", length, value));
199 Assert(length > 0 && length <= 15,
"invalid length");
200 s->bits_sent += (
ulg)length;
206 if (s->bi_valid > (
int)
Buf_size - length) {
207 s->bi_buf |= (
ush)value << s->bi_valid;
209 s->bi_buf = (
ush)value >> (
Buf_size - s->bi_valid);
212 s->bi_buf |= (
ush)value << s->bi_valid;
213 s->bi_valid += length;
218 #define send_bits(s, value, length) \ 220 if (s->bi_valid > (int)Buf_size - len) {\ 222 s->bi_buf |= (ush)val << s->bi_valid;\ 223 put_short(s, s->bi_buf);\ 224 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ 225 s->bi_valid += len - Buf_size;\ 227 s->bi_buf |= (ush)(value) << s->bi_valid;\ 241 #if defined(GEN_TREES_H) || !defined(STDC) 242 static int static_init_done = 0;
251 if (static_init_done)
return;
254 #ifdef NO_INIT_GLOBAL_POINTERS 270 Assert (length == 256,
"tr_static_init: length != 256");
279 for (code = 0 ; code < 16; code++) {
285 Assert (dist == 256,
"tr_static_init: dist != 256");
287 for ( ; code <
D_CODES; code++) {
293 Assert (dist == 256,
"tr_static_init: 256+dist != 512");
296 for (bits = 0; bits <=
MAX_BITS; bits++) bl_count[bits] = 0;
298 while (n <= 143)
static_ltree[n++].Len = 8, bl_count[8]++;
299 while (n <= 255)
static_ltree[n++].Len = 9, bl_count[9]++;
300 while (n <= 279)
static_ltree[n++].Len = 7, bl_count[7]++;
301 while (n <= 287)
static_ltree[n++].Len = 8, bl_count[8]++;
309 for (n = 0; n <
D_CODES; n++) {
313 static_init_done = 1;
329 # define SEPARATOR(i, last, width) \ 330 ((i) == (last)? "\n};\n\n" : \ 331 ((i) % (width) == (width)-1 ? ",\n" : ", ")) 333 void gen_trees_header()
335 FILE *header = fopen(
"trees.h",
"w");
338 Assert (header != NULL,
"Can't open trees.h");
340 "/* header created automatically with -DGEN_TREES_H */\n\n");
342 fprintf(header,
"local const ct_data static_ltree[L_CODES+2] = {\n");
343 for (i = 0; i <
L_CODES+2; i++) {
348 fprintf(header,
"local const ct_data static_dtree[D_CODES] = {\n");
349 for (i = 0; i <
D_CODES; i++) {
354 fprintf(header,
"const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
357 SEPARATOR(i, DIST_CODE_LEN-1, 20));
361 "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
367 fprintf(header,
"local const int base_length[LENGTH_CODES] = {\n");
370 SEPARATOR(i, LENGTH_CODES-1, 20));
373 fprintf(header,
"local const int base_dist[D_CODES] = {\n");
374 for (i = 0; i <
D_CODES; i++) {
376 SEPARATOR(i, D_CODES-1, 10));
391 s->l_desc.dyn_tree = s->dyn_ltree;
394 s->d_desc.dyn_tree = s->dyn_dtree;
397 s->bl_desc.dyn_tree = s->bl_tree;
404 s->compressed_len = 0L;
422 for (n = 0; n <
D_CODES; n++) s->dyn_dtree[n].Freq = 0;
423 for (n = 0; n <
BL_CODES; n++) s->bl_tree[n].Freq = 0;
426 s->opt_len = s->static_len = 0L;
427 s->last_lit = s->matches = 0;
438 #define pqremove(s, tree, top) \ 440 top = s->heap[SMALLEST]; \ 441 s->heap[SMALLEST] = s->heap[s->heap_len--]; \ 442 pqdownheap(s, tree, SMALLEST); \ 449 #define smaller(tree, n, m, depth) \ 450 (tree[n].Freq < tree[m].Freq || \ 451 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) 466 while (j <= s->heap_len) {
468 if (j < s->heap_len &&
469 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
473 if (
smaller(tree, v, s->heap[j], s->depth))
break;
476 s->heap[k] = s->heap[j]; k = j;
511 for (bits = 0; bits <=
MAX_BITS; bits++) s->bl_count[bits] = 0;
516 tree[s->heap[s->heap_max]].Len = 0;
518 for (h = s->heap_max+1; h <
HEAP_SIZE; h++) {
520 bits = tree[tree[n].Dad].Len + 1;
521 if (bits > max_length) bits =
max_length, overflow++;
522 tree[n].Len = (
ush)bits;
525 if (n > max_code)
continue;
529 if (n >= base) xbits = extra[n-base];
531 s->opt_len += (
ulg)f * (bits + xbits);
532 if (stree) s->static_len += (
ulg)f * (stree[n].
Len + xbits);
534 if (overflow == 0)
return;
536 Trace((stderr,
"\nbit length overflow\n"));
542 while (s->bl_count[bits] == 0) bits--;
544 s->bl_count[bits+1] += 2;
550 }
while (overflow > 0);
557 for (bits = max_length; bits != 0; bits--) {
558 n = s->bl_count[
bits];
561 if (m > max_code)
continue;
562 if ((
unsigned) tree[m].
Len != (
unsigned) bits) {
563 Trace((stderr,
"code %d bits %d->%d\n", m, tree[m].
Len, bits));
564 s->opt_len += ((long)bits - (
long)tree[m].Len)
566 tree[m].Len = (
ush)bits;
594 for (bits = 1; bits <=
MAX_BITS; bits++) {
595 next_code[
bits] = code = (code + bl_count[bits-1]) << 1;
601 "inconsistent bit counts");
602 Tracev((stderr,
"\ngen_codes: max_code %d ", max_code));
604 for (n = 0; n <= max_code; n++) {
605 int len = tree[n].Len;
606 if (len == 0)
continue;
608 tree[n].Code =
bi_reverse(next_code[len]++, len);
611 n, (isgraph(n) ? n :
' '), len, tree[n].
Code, next_code[len]-1));
638 s->heap_len = 0, s->heap_max =
HEAP_SIZE;
640 for (n = 0; n <
elems; n++) {
641 if (tree[n].
Freq != 0) {
642 s->heap[++(s->heap_len)] = max_code = n;
654 while (s->heap_len < 2) {
655 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
658 s->opt_len--;
if (stree) s->static_len -= stree[node].Len;
666 for (n = s->heap_len/2; n >= 1; n--)
pqdownheap(s, tree, n);
676 s->heap[--(s->heap_max)] = n;
677 s->heap[--(s->heap_max)] = m;
680 tree[node].Freq = tree[n].Freq + tree[m].Freq;
681 s->depth[node] = (
uch)((s->depth[n] >= s->depth[m] ?
682 s->depth[n] : s->depth[m]) + 1);
683 tree[n].Dad = tree[m].Dad = (
ush)node;
685 if (tree == s->bl_tree) {
686 fprintf(stderr,
"\nnode %d(%d), sons %d(%d) %d(%d)",
687 node, tree[node].
Freq, n, tree[n].Freq, m, tree[m].Freq);
694 }
while (s->heap_len >= 2);
696 s->heap[--(s->heap_max)] = s->heap[
SMALLEST];
719 int nextlen = tree[0].Len;
724 if (nextlen == 0) max_count = 138, min_count = 3;
725 tree[max_code+1].Len = (
ush)0xffff;
727 for (n = 0; n <= max_code; n++) {
728 curlen = nextlen; nextlen = tree[n+1].Len;
729 if (++count < max_count && curlen == nextlen) {
731 }
else if (count < min_count) {
732 s->bl_tree[curlen].Freq += count;
733 }
else if (curlen != 0) {
734 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
736 }
else if (count <= 10) {
741 count = 0; prevlen = curlen;
743 max_count = 138, min_count = 3;
744 }
else if (curlen == nextlen) {
745 max_count = 6, min_count = 3;
747 max_count = 7, min_count = 4;
764 int nextlen = tree[0].Len;
770 if (nextlen == 0) max_count = 138, min_count = 3;
772 for (n = 0; n <= max_code; n++) {
773 curlen = nextlen; nextlen = tree[n+1].Len;
774 if (++count < max_count && curlen == nextlen) {
776 }
else if (count < min_count) {
777 do {
send_code(s, curlen, s->bl_tree); }
while (--count != 0);
779 }
else if (curlen != 0) {
780 if (curlen != prevlen) {
781 send_code(s, curlen, s->bl_tree); count--;
783 Assert(count >= 3 && count <= 6,
" 3_6?");
786 }
else if (count <= 10) {
792 count = 0; prevlen = curlen;
794 max_count = 138, min_count = 3;
795 }
else if (curlen == nextlen) {
796 max_count = 6, min_count = 3;
798 max_count = 7, min_count = 4;
826 for (max_blindex =
BL_CODES-1; max_blindex >= 3; max_blindex--) {
827 if (s->bl_tree[
bl_order[max_blindex]].Len != 0)
break;
830 s->opt_len += 3*(max_blindex+1) + 5+5+4;
831 Tracev((stderr,
"\ndyn trees: dyn %ld, stat %ld",
832 s->opt_len, s->static_len));
844 int lcodes, dcodes, blcodes;
848 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4,
"not enough codes");
851 Tracev((stderr,
"\nbl counts: "));
855 for (rank = 0; rank < blcodes; rank++) {
859 Tracev((stderr,
"\nbl tree: sent %ld", s->bits_sent));
862 Tracev((stderr,
"\nlit tree: sent %ld", s->bits_sent));
865 Tracev((stderr,
"\ndist tree: sent %ld", s->bits_sent));
879 s->compressed_len = (s->compressed_len + 3 + 7) & (
ulg)~7L;
880 s->compressed_len += (stored_len + 4) << 3;
902 s->compressed_len += 10L;
910 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
914 s->compressed_len += 10L;
931 ulg opt_lenb, static_lenb;
943 Tracev((stderr,
"\nlit data: dyn %ld, stat %ld", s->opt_len,
947 Tracev((stderr,
"\ndist data: dyn %ld, stat %ld", s->opt_len,
959 opt_lenb = (s->opt_len+3+7)>>3;
960 static_lenb = (s->static_len+3+7)>>3;
962 Tracev((stderr,
"\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
963 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
966 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
969 Assert(buf != (
char*)0,
"lost buf");
970 opt_lenb = static_lenb = stored_len + 5;
974 if (buf != (
char*)0) {
976 if (stored_len+4 <= opt_lenb && buf != (
char*)0) {
988 }
else if (static_lenb >= 0) {
990 }
else if (s->strategy ==
Z_FIXED || static_lenb == opt_lenb) {
995 s->compressed_len += 3 + s->static_len;
1003 s->compressed_len += 3 + s->opt_len;
1006 Assert (s->compressed_len == s->bits_sent,
"bad compressed size");
1015 s->compressed_len += 7;
1018 Tracev((stderr,
"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1019 s->compressed_len-7*last));
1032 s->l_buf[s->last_lit++] = (
uch)lc;
1035 s->dyn_ltree[lc].Freq++;
1045 s->dyn_dtree[
d_code(dist)].Freq++;
1048 #ifdef TRUNCATE_BLOCK 1050 if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
1052 ulg out_length = (
ulg)s->last_lit*8L;
1053 ulg in_length = (
ulg)((long)s->strstart - s->block_start);
1055 for (dcode = 0; dcode <
D_CODES; dcode++) {
1056 out_length += (
ulg)s->dyn_dtree[dcode].Freq *
1060 Tracev((stderr,
"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1061 s->last_lit, in_length, out_length,
1062 100L - out_length*100L/in_length));
1063 if (s->matches < s->last_lit/2 && out_length < in_length/2)
return 1;
1066 return (s->last_lit == s->lit_bufsize-1);
1087 if (s->last_lit != 0)
do {
1088 dist = s->d_buf[lx];
1089 lc = s->l_buf[lx++];
1092 Tracecv(isgraph(lc), (stderr,
" '%c' ", lc));
1115 Assert((
uInt)(s->pending) < s->lit_bufsize + 2*lx,
1116 "pendingBuf overflow");
1118 }
while (lx < s->last_lit);
1144 unsigned long black_mask = 0xf3ffc07fUL;
1148 for (n = 0; n <= 31; n++, black_mask >>= 1)
1149 if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1153 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1154 || s->dyn_ltree[13].Freq != 0)
1157 if (s->dyn_ltree[n].Freq != 0)
1175 register unsigned res = 0;
1178 code >>= 1, res <<= 1;
1179 }
while (--len > 0);
1189 if (s->bi_valid == 16) {
1193 }
else if (s->bi_valid >= 8) {
1206 if (s->bi_valid > 8) {
1208 }
else if (s->bi_valid > 0) {
1214 s->bits_sent = (s->bits_sent+7) & ~7;
1229 s->last_eob_len = 8;
1235 s->bits_sent += 2*16;
1239 s->bits_sent += (
ulg)len<<3;
local const int extra_dbits[D_CODES]
local ct_data static_dtree[D_CODES]
local int detect_data_type(deflate_state *s)
void ZLIB_INTERNAL _tr_align(deflate_state *s)
#define Assert(cond, msg)
local int base_dist[D_CODES]
local void pqdownheap(deflate_state *s, ct_data *tree, int k)
local void gen_codes(ct_data *tree, int max_code, ushf *bl_count)
local void init_block(deflate_state *s)
struct ct_data_s dyn_ltree[HEAP_SIZE]
local static_tree_desc static_d_desc
static_tree_desc * stat_desc
local int base_length[LENGTH_CODES]
#define smaller(tree, n, m, depth)
local void gen_bitlen(deflate_state *s, tree_desc *desc)
local unsigned bi_reverse(unsigned code, int len)
local void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes)
local void tr_static_init OF((void))
local void compress_block(deflate_state *s, ct_data *ltree, ct_data *dtree)
void ZLIB_INTERNAL _tr_init(deflate_state *s)
#define send_bits(s, value, length)
local static_tree_desc static_bl_desc
local const int extra_lbits[LENGTH_CODES]
local void tr_static_init()
local const uch bl_order[BL_CODES]
local void send_tree(deflate_state *s, ct_data *tree, int max_code)
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
#define pqremove(s, tree, top)
uch _dist_code[DIST_CODE_LEN]
int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc)
local void scan_tree(deflate_state *s, ct_data *tree, int max_code)
uch _length_code[MAX_MATCH-MIN_MATCH+1]
local void bi_windup(deflate_state *s)
local void build_tree(deflate_state *s, tree_desc *desc)
local int build_bl_tree(deflate_state *s)
local void copy_block(deflate_state *s, charf *buf, unsigned len, int header)
void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, ulg stored_len, int last)
const ct_data * static_tree
local ct_data static_ltree[L_CODES+2]
local const int extra_blbits[BL_CODES]
#define send_code(s, c, tree)
local void bi_flush(deflate_state *s)
local static_tree_desc static_l_desc