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};
81 #define DIST_CODE_LEN 512
83 #if defined(GEN_TREES_H) || !defined(STDC)
159 local void gen_trees_header
OF((
void));
163 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
167 # define send_code(s, c, tree) \
168 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
169 send_bits(s, tree[c].Code, tree[c].Len); }
176 #define put_short(s, w) { \
177 put_byte(s, (uch)((w) & 0xff)); \
178 put_byte(s, (uch)((ush)(w) >> 8)); \
193 Tracevv((stderr,
" l %2d v %4x ", length, value));
194 Assert(length > 0 && length <= 15,
"invalid length");
195 s->bits_sent += (
ulg)length;
202 s->bi_buf |= (
ush)value << s->bi_valid;
204 s->bi_buf = (
ush)value >> (
Buf_size - s->bi_valid);
207 s->bi_buf |= (
ush)value << s->bi_valid;
208 s->bi_valid += length;
213 #define send_bits(s, value, length) \
215 if (s->bi_valid > (int)Buf_size - len) {\
217 s->bi_buf |= (ush)val << s->bi_valid;\
218 put_short(s, s->bi_buf);\
219 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
220 s->bi_valid += len - Buf_size;\
222 s->bi_buf |= (ush)(value) << s->bi_valid;\
236 #if defined(GEN_TREES_H) || !defined(STDC)
237 static int static_init_done = 0;
246 if (static_init_done)
return;
249 #ifdef NO_INIT_GLOBAL_POINTERS
265 Assert (length == 256,
"tr_static_init: length != 256");
274 for (code = 0 ; code < 16; code++) {
280 Assert (dist == 256,
"tr_static_init: dist != 256");
282 for ( ; code <
D_CODES; code++) {
288 Assert (dist == 256,
"tr_static_init: 256+dist != 512");
291 for (bits = 0; bits <=
MAX_BITS; bits++) bl_count[bits] = 0;
293 while (n <= 143)
static_ltree[n++].Len = 8, bl_count[8]++;
294 while (n <= 255)
static_ltree[n++].Len = 9, bl_count[9]++;
295 while (n <= 279)
static_ltree[n++].Len = 7, bl_count[7]++;
296 while (n <= 287)
static_ltree[n++].Len = 8, bl_count[8]++;
304 for (n = 0; n <
D_CODES; n++) {
308 static_init_done = 1;
324 # define SEPARATOR(i, last, width) \
325 ((i) == (last)? "\n};\n\n" : \
326 ((i) % (width) == (width)-1 ? ",\n" : ", "))
328 void gen_trees_header()
330 FILE *header = fopen(
"trees.h",
"w");
333 Assert (header != NULL,
"Can't open trees.h");
335 "/* header created automatically with -DGEN_TREES_H */\n\n");
337 fprintf(header,
"local const ct_data static_ltree[L_CODES+2] = {\n");
338 for (i = 0; i <
L_CODES+2; i++) {
343 fprintf(header,
"local const ct_data static_dtree[D_CODES] = {\n");
344 for (i = 0; i <
D_CODES; i++) {
349 fprintf(header,
"const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
352 SEPARATOR(i, DIST_CODE_LEN-1, 20));
356 "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
362 fprintf(header,
"local const int base_length[LENGTH_CODES] = {\n");
365 SEPARATOR(i, LENGTH_CODES-1, 20));
368 fprintf(header,
"local const int base_dist[D_CODES] = {\n");
369 for (i = 0; i <
D_CODES; i++) {
371 SEPARATOR(i, D_CODES-1, 10));
386 s->l_desc.dyn_tree = s->dyn_ltree;
389 s->d_desc.dyn_tree = s->dyn_dtree;
392 s->bl_desc.dyn_tree = s->bl_tree;
398 s->compressed_len = 0L;
416 for (n = 0; n <
D_CODES; n++) s->dyn_dtree[n].Freq = 0;
417 for (n = 0; n <
BL_CODES; n++) s->bl_tree[n].Freq = 0;
420 s->opt_len = s->static_len = 0L;
421 s->last_lit = s->matches = 0;
432 #define pqremove(s, tree, top) \
434 top = s->heap[SMALLEST]; \
435 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
436 pqdownheap(s, tree, SMALLEST); \
443 #define smaller(tree, n, m, depth) \
444 (tree[n].Freq < tree[m].Freq || \
445 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
460 while (j <= s->heap_len) {
462 if (j < s->heap_len &&
463 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
467 if (
smaller(tree, v, s->heap[j], s->depth))
break;
470 s->heap[k] = s->heap[j]; k = j;
505 for (bits = 0; bits <=
MAX_BITS; bits++) s->bl_count[bits] = 0;
510 tree[s->heap[s->heap_max]].Len = 0;
514 bits = tree[tree[n].Dad].Len + 1;
515 if (bits > max_length) bits = max_length, overflow++;
516 tree[n].Len = (
ush)bits;
519 if (n > max_code)
continue;
523 if (n >= base) xbits = extra[n-base];
525 s->opt_len += (
ulg)f * (bits + xbits);
526 if (stree) s->static_len += (
ulg)f * (stree[n].
Len + xbits);
528 if (overflow == 0)
return;
530 Trace((stderr,
"\nbit length overflow\n"));
536 while (s->bl_count[bits] == 0) bits--;
538 s->bl_count[bits+1] += 2;
539 s->bl_count[max_length]--;
544 }
while (overflow > 0);
551 for (bits = max_length; bits != 0; bits--) {
552 n = s->bl_count[bits];
555 if (m > max_code)
continue;
556 if ((
unsigned) tree[m].
Len != (
unsigned) bits) {
557 Trace((stderr,
"code %d bits %d->%d\n", m, tree[m].
Len, bits));
558 s->opt_len += ((long)bits - (
long)tree[m].Len)
560 tree[m].Len = (
ush)bits;
588 for (bits = 1; bits <=
MAX_BITS; bits++) {
589 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
595 "inconsistent bit counts");
596 Tracev((stderr,
"\ngen_codes: max_code %d ", max_code));
598 for (n = 0; n <= max_code; n++) {
599 int len = tree[n].Len;
600 if (len == 0)
continue;
602 tree[n].Code =
bi_reverse(next_code[len]++, len);
605 n, (isgraph(n) ? n :
' '), len, tree[n].
Code, next_code[len]-1));
632 s->heap_len = 0, s->heap_max =
HEAP_SIZE;
634 for (n = 0; n < elems; n++) {
635 if (tree[n].
Freq != 0) {
636 s->heap[++(s->heap_len)] = max_code = n;
648 while (s->heap_len < 2) {
649 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
652 s->opt_len--;
if (stree) s->static_len -= stree[node].Len;
660 for (n = s->heap_len/2; n >= 1; n--)
pqdownheap(s, tree, n);
670 s->heap[--(s->heap_max)] = n;
671 s->heap[--(s->heap_max)] = m;
674 tree[node].Freq = tree[n].Freq + tree[m].Freq;
675 s->depth[node] = (
uch)((s->depth[n] >= s->depth[m] ?
676 s->depth[n] : s->depth[m]) + 1);
677 tree[n].Dad = tree[m].Dad = (
ush)node;
679 if (tree == s->bl_tree) {
680 fprintf(stderr,
"\nnode %d(%d), sons %d(%d) %d(%d)",
681 node, tree[node].
Freq, n, tree[n].Freq, m, tree[m].Freq);
688 }
while (s->heap_len >= 2);
690 s->heap[--(s->heap_max)] = s->heap[
SMALLEST];
713 int nextlen = tree[0].Len;
718 if (nextlen == 0) max_count = 138, min_count = 3;
719 tree[max_code+1].Len = (
ush)0xffff;
721 for (n = 0; n <= max_code; n++) {
722 curlen = nextlen; nextlen = tree[n+1].Len;
723 if (++count < max_count && curlen == nextlen) {
725 }
else if (count < min_count) {
726 s->bl_tree[curlen].Freq += count;
727 }
else if (curlen != 0) {
728 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
730 }
else if (count <= 10) {
735 count = 0; prevlen = curlen;
737 max_count = 138, min_count = 3;
738 }
else if (curlen == nextlen) {
739 max_count = 6, min_count = 3;
741 max_count = 7, min_count = 4;
758 int nextlen = tree[0].Len;
764 if (nextlen == 0) max_count = 138, min_count = 3;
766 for (n = 0; n <= max_code; n++) {
767 curlen = nextlen; nextlen = tree[n+1].Len;
768 if (++count < max_count && curlen == nextlen) {
770 }
else if (count < min_count) {
771 do {
send_code(s, curlen, s->bl_tree); }
while (--count != 0);
773 }
else if (curlen != 0) {
774 if (curlen != prevlen) {
775 send_code(s, curlen, s->bl_tree); count--;
777 Assert(count >= 3 && count <= 6,
" 3_6?");
780 }
else if (count <= 10) {
786 count = 0; prevlen = curlen;
788 max_count = 138, min_count = 3;
789 }
else if (curlen == nextlen) {
790 max_count = 6, min_count = 3;
792 max_count = 7, min_count = 4;
820 for (max_blindex =
BL_CODES-1; max_blindex >= 3; max_blindex--) {
821 if (s->bl_tree[
bl_order[max_blindex]].Len != 0)
break;
824 s->opt_len += 3*(max_blindex+1) + 5+5+4;
825 Tracev((stderr,
"\ndyn trees: dyn %ld, stat %ld",
826 s->opt_len, s->static_len));
838 int lcodes, dcodes, blcodes;
842 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4,
"not enough codes");
843 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <=
BL_CODES,
845 Tracev((stderr,
"\nbl counts: "));
849 for (rank = 0; rank < blcodes; rank++) {
853 Tracev((stderr,
"\nbl tree: sent %ld", s->bits_sent));
856 Tracev((stderr,
"\nlit tree: sent %ld", s->bits_sent));
859 Tracev((stderr,
"\ndist tree: sent %ld", s->bits_sent));
873 s->compressed_len = (s->compressed_len + 3 + 7) & (
ulg)~7L;
874 s->compressed_len += (stored_len + 4) << 3;
898 s->compressed_len += 10L;
913 ulg opt_lenb, static_lenb;
925 Tracev((stderr,
"\nlit data: dyn %ld, stat %ld", s->opt_len,
929 Tracev((stderr,
"\ndist data: dyn %ld, stat %ld", s->opt_len,
941 opt_lenb = (s->opt_len+3+7)>>3;
942 static_lenb = (s->static_len+3+7)>>3;
944 Tracev((stderr,
"\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
945 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
948 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
951 Assert(buf != (
char*)0,
"lost buf");
952 opt_lenb = static_lenb = stored_len + 5;
956 if (buf != (
char*)0) {
958 if (stored_len+4 <= opt_lenb && buf != (
char*)0) {
970 }
else if (static_lenb >= 0) {
972 }
else if (s->strategy ==
Z_FIXED || static_lenb == opt_lenb) {
977 s->compressed_len += 3 + s->static_len;
985 s->compressed_len += 3 + s->opt_len;
988 Assert (s->compressed_len == s->bits_sent,
"bad compressed size");
997 s->compressed_len += 7;
1000 Tracev((stderr,
"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1001 s->compressed_len-7*last));
1014 s->l_buf[s->last_lit++] = (
uch)lc;
1017 s->dyn_ltree[lc].Freq++;
1024 (
ush)
d_code(dist) < (
ush)D_CODES,
"_tr_tally: bad match");
1027 s->dyn_dtree[
d_code(dist)].Freq++;
1030 #ifdef TRUNCATE_BLOCK
1032 if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
1034 ulg out_length = (
ulg)s->last_lit*8L;
1035 ulg in_length = (
ulg)((long)s->strstart - s->block_start);
1037 for (dcode = 0; dcode <
D_CODES; dcode++) {
1038 out_length += (
ulg)s->dyn_dtree[dcode].Freq *
1042 Tracev((stderr,
"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1043 s->last_lit, in_length, out_length,
1044 100L - out_length*100L/in_length));
1045 if (s->matches < s->last_lit/2 && out_length < in_length/2)
return 1;
1048 return (s->last_lit == s->lit_bufsize-1);
1069 if (s->last_lit != 0)
do {
1070 dist = s->d_buf[lx];
1071 lc = s->l_buf[lx++];
1074 Tracecv(isgraph(lc), (stderr,
" '%c' ", lc));
1086 Assert (code < D_CODES,
"bad d_code");
1097 Assert((
uInt)(s->pending) < s->lit_bufsize + 2*lx,
1098 "pendingBuf overflow");
1100 }
while (lx < s->last_lit);
1125 unsigned long black_mask = 0xf3ffc07fUL;
1129 for (n = 0; n <= 31; n++, black_mask >>= 1)
1130 if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1134 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1135 || s->dyn_ltree[13].Freq != 0)
1138 if (s->dyn_ltree[n].Freq != 0)
1156 register unsigned res = 0;
1159 code >>= 1, res <<= 1;
1160 }
while (--len > 0);
1170 if (s->bi_valid == 16) {
1174 }
else if (s->bi_valid >= 8) {
1187 if (s->bi_valid > 8) {
1189 }
else if (s->bi_valid > 0) {
1195 s->bits_sent = (s->bits_sent+7) & ~7;
1215 s->bits_sent += 2*16;
1219 s->bits_sent += (
ulg)len<<3;