60 #define REPZ_11_138 18
64 = {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 static const int extra_dbits[
D_CODES]
67 = {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 static const int extra_blbits[
BL_CODES]
70 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
73 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
78 #define Buf_size (8 * 2*sizeof(char))
99 static uch dist_code[512];
135 static void tr_static_init (
void);
139 static void gen_codes (
ct_data *
tree,
int max_code,
ush *bl_count);
144 static void send_all_trees (
deflate_state *
s,
int lcodes,
int dcodes,
149 static unsigned bi_reverse (
unsigned value,
int length);
156 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
160 # define send_code(s, c, tree) \
161 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
162 send_bits(s, tree[c].Code, tree[c].Len); }
165 #define d_code(dist) \
166 ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
185 Tracevv((stderr,
" l %2d v %4x ", length, value));
186 Assert(length > 0 && length <= 15,
"invalid length");
187 s->bits_sent += (
ulg)length;
205 #define send_bits(s, value, length) \
207 if (s->bi_valid > (int)Buf_size - len) {\
209 s->bi_buf |= (val << s->bi_valid);\
210 put_short(s, s->bi_buf);\
211 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
212 s->bi_valid += len - Buf_size;\
214 s->bi_buf |= (value) << s->bi_valid;\
225 static void tr_static_init(
void)
227 static int static_init_done;
236 if (static_init_done)
return;
242 for (n = 0; n < (1<<extra_lbits[
code]); n++) {
243 length_code[length++] = (
uch)code;
246 Assert (length == 256,
"tr_static_init: length != 256");
251 length_code[length-1] = (
uch)code;
255 for (code = 0 ; code < 16; code++) {
257 for (n = 0; n < (1<<extra_dbits[
code]); n++) {
258 dist_code[dist++] = (
uch)code;
261 Assert (dist == 256,
"tr_static_init: dist != 256");
263 for ( ; code <
D_CODES; code++) {
264 base_dist[
code] = dist << 7;
265 for (n = 0; n < (1<<(extra_dbits[
code]-7)); n++) {
266 dist_code[256 + dist++] = (
uch)code;
269 Assert (dist == 256,
"tr_static_init: 256+dist != 512");
272 for (bits = 0; bits <=
MAX_BITS; bits++) bl_count[bits] = 0;
274 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
275 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
276 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
277 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
285 for (n = 0; n <
D_CODES; n++) {
286 static_dtree[
n].Len = 5;
287 static_dtree[
n].Code = bi_reverse((
unsigned)n, 5);
289 static_init_done = 1;
304 s->
l_desc.stat_desc = &static_l_desc;
307 s->
d_desc.stat_desc = &static_d_desc;
310 s->
bl_desc.stat_desc = &static_bl_desc;
350 #define pqremove(s, tree, top) \
352 top = s->heap[SMALLEST]; \
353 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
354 pqdownheap(s, tree, SMALLEST); \
361 #define smaller(tree, n, m, depth) \
362 (tree[n].Freq < tree[m].Freq || \
363 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
371 static void pqdownheap(
379 while (j <= s->heap_len) {
381 if (j < s->heap_len &&
407 static void gen_bitlen(
434 bits = tree[tree[
n].Dad].Len + 1;
435 if (bits > max_length) bits = max_length, overflow++;
436 tree[
n].Len = (
ush)bits;
439 if (n > max_code)
continue;
443 if (n >= base) xbits = extra[n-base];
448 if (overflow == 0)
return;
450 Trace((stderr,
"\nbit length overflow\n"));
456 while (s->
bl_count[bits] == 0) bits--;
464 }
while (overflow > 0);
471 for (bits = max_length; bits != 0; bits--) {
475 if (m > max_code)
continue;
476 if (tree[m].
Len != (
unsigned) bits) {
477 Trace((stderr,
"code %d bits %d->%d\n", m, tree[m].
Len, bits));
480 tree[
m].Len = (
ush)bits;
495 static void gen_codes(
509 for (bits = 1; bits <=
MAX_BITS; bits++) {
510 next_code[
bits] = code = (code + bl_count[bits-1]) << 1;
516 "inconsistent bit counts");
517 Tracev((stderr,
"\ngen_codes: max_code %d ", max_code));
519 for (n = 0; n <= max_code; n++) {
520 int len = tree[
n].Len;
521 if (len == 0)
continue;
523 tree[
n].Code = bi_reverse(next_code[len]++, len);
525 Tracecv(tree != static_ltree, (stderr,
"\nn %3d %c l %2d c %4x (%x) ",
526 n, (
isgraph(n) ? n :
' '), len, tree[n].
Code, next_code[len]-1));
538 static void build_tree(
556 for (n = 0; n < elems; n++) {
557 if (tree[n].
Freq != 0) {
571 node = s->
heap[++(s->
heap_len)] = (max_code < 2 ? ++max_code : 0);
582 for (n = s->
heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
596 tree[
node].Freq = tree[
n].Freq + tree[
m].Freq;
598 tree[
n].Dad = tree[
m].Dad = (
ush)node;
601 fprintf(stderr,
"\nnode %d(%d), sons %d(%d) %d(%d)",
602 node, tree[node].
Freq, n, tree[n].Freq, m, tree[m].Freq);
626 static void scan_tree(
635 int nextlen = tree[0].Len;
640 if (nextlen == 0) max_count = 138, min_count = 3;
641 tree[max_code+1].Len = (
ush)0xffff;
643 for (n = 0; n <= max_code; n++) {
644 curlen = nextlen; nextlen = tree[n+1].Len;
645 if (++count < max_count && curlen == nextlen) {
647 }
else if (count < min_count) {
649 }
else if (curlen != 0) {
650 if (curlen != prevlen) s->
bl_tree[curlen].Freq++;
652 }
else if (count <= 10) {
657 count = 0; prevlen = curlen;
659 max_count = 138, min_count = 3;
660 }
else if (curlen == nextlen) {
661 max_count = 6, min_count = 3;
663 max_count = 7, min_count = 4;
672 static void send_tree(
681 int nextlen = tree[0].Len;
687 if (nextlen == 0) max_count = 138, min_count = 3;
689 for (n = 0; n <= max_code; n++) {
690 curlen = nextlen; nextlen = tree[n+1].Len;
691 if (++count < max_count && curlen == nextlen) {
693 }
else if (count < min_count) {
696 }
else if (curlen != 0) {
697 if (curlen != prevlen) {
700 Assert(count >= 3 && count <= 6,
" 3_6?");
703 }
else if (count <= 10) {
709 count = 0; prevlen = curlen;
711 max_count = 138, min_count = 3;
712 }
else if (curlen == nextlen) {
713 max_count = 6, min_count = 3;
715 max_count = 7, min_count = 4;
724 static int build_bl_tree(
744 for (max_blindex =
BL_CODES-1; max_blindex >= 3; max_blindex--) {
745 if (s->
bl_tree[bl_order[max_blindex]].Len != 0)
break;
748 s->
opt_len += 3*(max_blindex+1) + 5+5+4;
749 Tracev((stderr,
"\ndyn trees: dyn %ld, stat %ld",
760 static void send_all_trees(
769 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4,
"not enough codes");
770 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <=
BL_CODES,
772 Tracev((stderr,
"\nbl counts: "));
776 for (rank = 0; rank < blcodes; rank++) {
777 Tracev((stderr,
"\nbl code %2d ", bl_order[rank]));
780 Tracev((stderr,
"\nbl tree: sent %ld", s->bits_sent));
783 Tracev((stderr,
"\nlit tree: sent %ld", s->bits_sent));
786 Tracev((stderr,
"\ndist tree: sent %ld", s->bits_sent));
803 copy_block(s, buf, (
unsigned)stored_len, 1);
863 ulg opt_lenb, static_lenb;
878 Tracev((stderr,
"\ndist data: dyn %ld, stat %ld", s->
opt_len,
887 max_blindex = build_bl_tree(s);
890 opt_lenb = (s->
opt_len+3+7)>>3;
893 Tracev((stderr,
"\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
897 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
900 Assert(buf != (
char*)0,
"lost buf");
901 opt_lenb = static_lenb = stored_len + 5;
908 #ifdef STORED_FILE_OK
909 # ifdef FORCE_STORED_FILE
912 if (stored_len <= opt_lenb && eof && s->compressed_len==0
L && seekable()) {
915 if (buf == (
char*)0)
error (
"block vanished");
917 copy_block(s, buf, (
unsigned)stored_len, 0);
924 if (buf != (
char*)0) {
926 if (stored_len+4 <= opt_lenb && buf != (
char*)0) {
938 }
else if (static_lenb >= 0) {
940 }
else if (static_lenb == opt_lenb) {
943 compress_block(s, (
ct_data *)static_ltree, (
ct_data *)static_dtree);
947 send_all_trees(s, s->
l_desc.max_code+1, s->
d_desc.max_code+1,
986 (
ush)
d_code(dist) < (
ush)D_CODES,
"zlib_tr_tally: bad match");
998 for (dcode = 0; dcode <
D_CODES; dcode++) {
1000 (5
L+extra_dbits[dcode]);
1003 Tracev((stderr,
"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1004 s->
last_lit, in_length, out_length,
1005 100
L - out_length*100
L/in_length));
1018 static void compress_block(
1031 dist = s->
d_buf[lx];
1032 lc = s->
l_buf[lx++];
1038 code = length_code[
lc];
1040 extra = extra_lbits[
code];
1042 lc -= base_length[
code];
1047 Assert (code < D_CODES,
"bad d_code");
1050 extra = extra_dbits[
code];
1052 dist -= base_dist[
code];
1060 }
while (lx < s->last_lit);
1072 static void set_data_type(
1077 unsigned ascii_freq = 0;
1078 unsigned bin_freq = 0;
1079 while (n < 7) bin_freq += s->
dyn_ltree[n++].Freq;
1080 while (n < 128) ascii_freq += s->
dyn_ltree[n++].Freq;
1089 static void copy_block(
1103 s->bits_sent += 2*16;
1107 s->bits_sent += (
ulg)len<<3;