58 #define REPZ_11_138 18
62 = {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};
65 = {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};
68 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
71 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
76 #define Buf_size (8 * 2*sizeof(char))
85 #define DIST_CODE_LEN 512
87 #if defined(GEN_TREES_H) || !defined(STDC)
163 local void gen_trees_header
OF((
void));
167 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
171 # define send_code(s, c, tree) \
172 { if (z_verbose>2) fprintf(stderr,(char*)"\ncd %3d ",(c)); \
173 send_bits(s, tree[c].Code, tree[c].Len); }
180 #define put_short(s, w) { \
181 put_byte(s, (uch)((w) & 0xff)); \
182 put_byte(s, (uch)((ush)(w) >> 8)); \
194 Tracevv((stderr,(
char*)
" l %2d v %4x ", length, value));
195 Assert(length > 0 && length <= 15, (
char*)
"invalid length");
196 s->bits_sent += (
ulg)length;
214 #define send_bits(s, value, length) \
216 if (s->bi_valid > (int)Buf_size - len) {\
218 s->bi_buf |= (val << s->bi_valid);\
219 put_short(s, s->bi_buf);\
220 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
221 s->bi_valid += len - Buf_size;\
223 s->bi_buf |= (value) << s->bi_valid;\
237 #if defined(GEN_TREES_H) || !defined(STDC)
238 static int static_init_done = 0;
247 if (static_init_done)
return;
264 Assert (length == 256, (
char*)
"tr_static_init: length != 256");
273 for (code = 0 ; code < 16; code++) {
279 Assert (dist == 256, (
char*)
"tr_static_init: dist != 256");
281 for ( ; code <
D_CODES; code++) {
287 Assert (dist == 256, (
char*)
"tr_static_init: 256+dist != 512");
290 for (bits = 0; bits <=
MAX_BITS; bits++) bl_count[bits] = 0;
292 while (n <= 143)
static_ltree[n++].Len = 8, bl_count[8]++;
293 while (n <= 255)
static_ltree[n++].Len = 9, bl_count[9]++;
294 while (n <= 279)
static_ltree[n++].Len = 7, bl_count[7]++;
295 while (n <= 287)
static_ltree[n++].Len = 8, bl_count[8]++;
303 for (n = 0; n <
D_CODES; n++) {
307 static_init_done = 1;
323 # define SEPARATOR(i, last, width) \
324 ((i) == (last)? (char*)"\n};\n\n" : \
325 ((i) % (width) == (width)-1 ? (char*)",\n" : ", "))
327 void gen_trees_header()
329 FILE *header = fopen((
char*)
"trees.h", (
char*)
"w");
332 Assert (header != NULL, (
char*)
"Can't open trees.h");
334 (
char*)
"/* header created automatically with -DGEN_TREES_H */\n\n");
336 fprintf(header, (
char*)
"local const ct_data static_ltree[L_CODES+2] = {\n");
337 for (i = 0; i <
L_CODES+2; i++) {
342 fprintf(header, (
char*)
"local const ct_data static_dtree[D_CODES] = {\n");
343 for (i = 0; i <
D_CODES; i++) {
348 fprintf(header, (
char*)
"const uch _dist_code[DIST_CODE_LEN] = {\n");
350 fprintf(header, (
char*)
"%2u%s",
_dist_code[i],
351 SEPARATOR(i, DIST_CODE_LEN-1, 20));
354 fprintf(header, (
char*)
"const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
360 fprintf(header, (
char*)
"local const int base_length[LENGTH_CODES] = {\n");
363 SEPARATOR(i, LENGTH_CODES-1, 20));
366 fprintf(header, (
char*)
"local const int base_dist[D_CODES] = {\n");
367 for (i = 0; i <
D_CODES; i++) {
368 fprintf(header, (
char*)
"%5u%s",
base_dist[i],
369 SEPARATOR(i, D_CODES-1, 10));
396 s->compressed_len = 0L;
429 #define pqremove(s, tree, top) \
431 top = s->heap[SMALLEST]; \
432 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
433 pqdownheap(s, tree, SMALLEST); \
440 #define smaller(tree, n, m, depth) \
441 (tree[n].Freq < tree[m].Freq || \
442 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
454 while (j <= s->heap_len) {
456 if (j < s->heap_len &&
506 bits = tree[tree[
n].Dad].Len + 1;
507 if (bits > max_length) bits = max_length, overflow++;
508 tree[
n].Len = (
ush)bits;
511 if (n > max_code)
continue;
515 if (n >= base) xbits = extra[n-
base];
520 if (overflow == 0)
return;
522 Trace((stderr,(
char*)
"\nbit length overflow\n"));
528 while (s->
bl_count[bits] == 0) bits--;
536 }
while (overflow > 0);
543 for (bits = max_length; bits != 0; bits--) {
547 if (m > max_code)
continue;
548 if (tree[m].
Len != (
unsigned) bits) {
549 Trace((stderr,(
char*)
"code %d bits %d->%d\n", m, tree[m].
Len, bits));
550 s->
opt_len += ((long)bits - (
long)tree[
m].Len)
552 tree[
m].Len = (
ush)bits;
577 for (bits = 1; bits <=
MAX_BITS; bits++) {
578 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
584 (
char*)
"inconsistent bit counts");
585 Tracev((stderr,(
char*)
"\ngen_codes: max_code %d ", max_code));
587 for (n = 0; n <= max_code; n++) {
588 int len = tree[
n].Len;
589 if (len == 0)
continue;
594 n, (isgraph(n) ? n :
' '), len, tree[n].
Code, next_code[len]-1));
621 for (n = 0; n < elems; n++) {
622 if (tree[n].
Freq != 0) {
636 node = s->
heap[++(s->
heap_len)] = (max_code < 2 ? ++max_code : 0);
661 tree[node].Freq = tree[
n].Freq + tree[
m].Freq;
664 tree[
n].Dad = tree[
m].Dad = (
ush)node;
667 fprintf(stderr,(
char*)
"\nnode %d(%d), sons %d(%d) %d(%d)",
668 node, tree[node].
Freq, n, tree[n].Freq, m, tree[m].Freq);
697 int nextlen = tree[0].Len;
702 if (nextlen == 0) max_count = 138, min_count = 3;
703 tree[max_code+1].Len = (
ush)0xffff;
705 for (n = 0; n <= max_code; n++) {
706 curlen = nextlen; nextlen = tree[n+1].Len;
707 if (++count < max_count && curlen == nextlen) {
709 }
else if (count < min_count) {
710 s->
bl_tree[curlen].Freq += count;
711 }
else if (curlen != 0) {
712 if (curlen != prevlen) s->
bl_tree[curlen].Freq++;
714 }
else if (count <= 10) {
719 count = 0; prevlen = curlen;
721 max_count = 138, min_count = 3;
722 }
else if (curlen == nextlen) {
723 max_count = 6, min_count = 3;
725 max_count = 7, min_count = 4;
739 int nextlen = tree[0].Len;
745 if (nextlen == 0) max_count = 138, min_count = 3;
747 for (n = 0; n <= max_code; n++) {
748 curlen = nextlen; nextlen = tree[n+1].Len;
749 if (++count < max_count && curlen == nextlen) {
751 }
else if (count < min_count) {
754 }
else if (curlen != 0) {
755 if (curlen != prevlen) {
758 Assert(count >= 3 && count <= 6, (
char*)
" 3_6?");
761 }
else if (count <= 10) {
767 count = 0; prevlen = curlen;
769 max_count = 138, min_count = 3;
770 }
else if (curlen == nextlen) {
771 max_count = 6, min_count = 3;
773 max_count = 7, min_count = 4;
800 for (max_blindex =
BL_CODES-1; max_blindex >= 3; max_blindex--) {
804 s->
opt_len += 3*(max_blindex+1) + 5+5+4;
805 Tracev((stderr, (
char*)
"\ndyn trees: dyn %ld, stat %ld",
820 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, (
char*)
"not enough codes");
821 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <=
BL_CODES,
822 (
char*)
"too many codes");
823 Tracev((stderr, (
char*)
"\nbl counts: "));
827 for (rank = 0; rank < blcodes; rank++) {
831 Tracev((stderr, (
char*)
"\nbl tree: sent %ld", s->bits_sent));
834 Tracev((stderr, (
char*)
"\nlit tree: sent %ld", s->bits_sent));
837 Tracev((stderr, (
char*)
"\ndist tree: sent %ld", s->bits_sent));
847 s->compressed_len = (s->compressed_len + 3 + 7) & (
ulg)~7L;
848 s->compressed_len += (stored_len + 4) << 3;
869 s->compressed_len += 10L;
881 s->compressed_len += 10L;
894 ulg opt_lenb, static_lenb;
905 Tracev((stderr, (
char*)
"\nlit data: dyn %ld, stat %ld", s->
opt_len,
909 Tracev((stderr, (
char*)
"\ndist data: dyn %ld, stat %ld", s->
opt_len,
921 opt_lenb = (s->
opt_len+3+7)>>3;
924 Tracev((stderr, (
char*)
"\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
928 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
931 Assert(buf != (
char*)0, (
char*)
"lost buf");
932 opt_lenb = static_lenb = stored_len + 5;
936 if (buf != (
char*)0) {
938 if (stored_len+4 <= opt_lenb && buf != (
char*)0) {
950 }
else if (static_lenb >= 0) {
952 }
else if (static_lenb == opt_lenb) {
965 s->compressed_len += 3 + s->
opt_len;
968 Assert (s->compressed_len == s->bits_sent, (
char*)
"bad compressed size");
977 s->compressed_len += 7;
980 Tracev((stderr,(
char*)
"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
981 s->compressed_len-7*eof));
1001 (
ush)
d_code(dist) < (
ush)D_CODES, (
char*)
"_tr_tally: bad match");
1007 #ifdef TRUNCATE_BLOCK
1014 for (dcode = 0; dcode <
D_CODES; dcode++) {
1019 Tracev((stderr,(
char*)
"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1020 s->
last_lit, in_length, out_length,
1021 100L - out_length*100L/in_length));
1044 dist = s->
d_buf[lx];
1045 lc = s->
l_buf[lx++];
1048 Tracecv(isgraph(lc), (stderr,
" '%c' ", lc));
1060 Assert (code < D_CODES, (
char*)
"bad d_code");
1072 (
char*)
"pendingBuf overflow");
1074 }
while (lx < s->last_lit);
1089 unsigned ascii_freq = 0;
1090 unsigned bin_freq = 0;
1091 while (n < 7) bin_freq += s->
dyn_ltree[n++].Freq;
1092 while (n < 128) ascii_freq += s->
dyn_ltree[n++].Freq;
1104 register unsigned res = 0;
1107 code >>= 1, res <<= 1;
1108 }
while (--len > 0);
1141 s->bits_sent = (s->bits_sent+7) & ~7;
1158 s->bits_sent += 2*16;
1162 s->bits_sent += (
ulg)len<<3;