66 #define REPZ_11_138 18
70 = {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};
73 = {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};
76 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
79 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
84 #define Buf_size (8 * 2*sizeof(char))
93 #define DIST_CODE_LEN 512
95 #if defined(GEN_TREES_H) || !defined(STDC)
173 local void gen_trees_header
OF((
void));
177 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
181 # define send_code(s, c, tree) \
182 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
183 send_bits(s, tree[c].Code, tree[c].Len); }
190 #define put_short(s, w) { \
191 put_byte(s, (uch)((w) & 0xff)); \
192 put_byte(s, (uch)((ush)(w) >> 8)); \
204 Tracevv((stderr,
" l %2d v %4x ", length, value));
205 Assert(length > 0 && length <= 15,
"invalid length");
206 s->bits_sent += (
ulg)length;
224 #define send_bits(s, value, length) \
226 if (s->bi_valid > (int)Buf_size - len) {\
228 s->bi_buf |= (ush)val << s->bi_valid;\
229 put_short(s, s->bi_buf);\
230 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
231 s->bi_valid += len - Buf_size;\
233 s->bi_buf |= (ush)(value) << s->bi_valid;\
247 #if defined(GEN_TREES_H) || !defined(STDC)
248 static int static_init_done = 0;
257 if (static_init_done)
return;
260 #ifdef NO_INIT_GLOBAL_POINTERS
276 Assert (length == 256,
"tr_static_init: length != 256");
285 for (code = 0 ; code < 16; code++) {
291 Assert (dist == 256,
"tr_static_init: dist != 256");
293 for ( ; code <
D_CODES; code++) {
299 Assert (dist == 256,
"tr_static_init: 256+dist != 512");
302 for (bits = 0; bits <=
MAX_BITS; bits++) bl_count[bits] = 0;
304 while (n <= 143)
static_ltree[n++].Len = 8, bl_count[8]++;
305 while (n <= 255)
static_ltree[n++].Len = 9, bl_count[9]++;
306 while (n <= 279)
static_ltree[n++].Len = 7, bl_count[7]++;
307 while (n <= 287)
static_ltree[n++].Len = 8, bl_count[8]++;
315 for (n = 0; n <
D_CODES; n++) {
319 static_init_done = 1;
337 # define SEPARATOR(i, last, width) \
338 ((i) == (last)? "\n};\n\n" : \
339 ((i) % (width) == (width)-1 ? ",\n" : ", "))
341 void gen_trees_header()
343 FILE *header = fopen(
"trees.h",
"w");
346 Assert (header != NULL,
"Can't open trees.h");
348 "/* header created automatically with -DGEN_TREES_H */\n\n");
350 fprintf(header,
"local const ct_data static_ltree[L_CODES+2] = {\n");
351 for (i = 0; i <
L_CODES+2; i++) {
356 fprintf(header,
"local const ct_data static_dtree[D_CODES] = {\n");
357 for (i = 0; i <
D_CODES; i++) {
362 fprintf(header,
"const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
365 SEPARATOR(i, DIST_CODE_LEN-1, 20));
369 "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
375 fprintf(header,
"local const int base_length[LENGTH_CODES] = {\n");
378 SEPARATOR(i, LENGTH_CODES-1, 20));
381 fprintf(header,
"local const int base_dist[D_CODES] = {\n");
382 for (i = 0; i <
D_CODES; i++) {
384 SEPARATOR(i, D_CODES-1, 10));
411 s->compressed_len = 0L;
444 #define pqremove(s, tree, top) \
446 top = s->heap[SMALLEST]; \
447 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
448 pqdownheap(s, tree, SMALLEST); \
455 #define smaller(tree, n, m, depth) \
456 (tree[n].Freq < tree[m].Freq || \
457 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
469 while (j <= s->heap_len) {
471 if (j < s->heap_len &&
521 bits = tree[tree[n].Dad].Len + 1;
522 if (bits > max_length) bits = max_length, overflow++;
523 tree[n].Len = (
ush)bits;
526 if (n > max_code)
continue;
530 if (n >= base) xbits = extra[n-base];
535 if (overflow == 0)
return;
537 Trace((stderr,
"\nbit length overflow\n"));
543 while (s->
bl_count[bits] == 0) bits--;
551 }
while (overflow > 0);
558 for (bits = max_length; bits != 0; bits--) {
562 if (m > max_code)
continue;
563 if ((
unsigned) tree[m].
Len != (
unsigned) bits) {
564 Trace((stderr,
"code %d bits %d->%d\n", m, tree[m].
Len, bits));
565 s->
opt_len += ((long)bits - (
long)tree[m].Len)
567 tree[m].Len = (
ush)bits;
595 for (bits = 1; bits <=
MAX_BITS; bits++) {
596 next_code[
bits] = code = (code + bl_count[bits-1]) << 1;
602 "inconsistent bit counts");
603 Tracev((stderr,
"\ngen_codes: max_code %d ", max_code));
605 for (n = 0; n <= max_code; n++) {
606 int len = tree[n].Len;
607 if (len == 0)
continue;
609 tree[n].Code =
bi_reverse(next_code[len]++, len);
612 n, (isgraph(n) ? n :
' '), len, tree[n].
Code, next_code[len]-1));
639 for (n = 0; n < elems; n++) {
640 if (tree[n].
Freq != 0) {
654 node = s->
heap[++(s->
heap_len)] = (max_code < 2 ? ++max_code : 0);
679 tree[node].Freq = tree[n].Freq + tree[m].Freq;
682 tree[n].Dad = tree[m].Dad = (
ush)node;
685 fprintf(stderr,
"\nnode %d(%d), sons %d(%d) %d(%d)",
686 node, tree[node].
Freq, n, tree[n].Freq, m, tree[m].Freq);
715 int nextlen = tree[0].Len;
720 if (nextlen == 0) max_count = 138, min_count = 3;
721 tree[max_code+1].Len = (
ush)0xffff;
723 for (n = 0; n <= max_code; n++) {
724 curlen = nextlen; nextlen = tree[n+1].Len;
725 if (++count < max_count && curlen == nextlen) {
727 }
else if (count < min_count) {
728 s->
bl_tree[curlen].Freq += count;
729 }
else if (curlen != 0) {
730 if (curlen != prevlen) s->
bl_tree[curlen].Freq++;
732 }
else if (count <= 10) {
737 count = 0; prevlen = curlen;
739 max_count = 138, min_count = 3;
740 }
else if (curlen == nextlen) {
741 max_count = 6, min_count = 3;
743 max_count = 7, min_count = 4;
757 int nextlen = tree[0].Len;
763 if (nextlen == 0) max_count = 138, min_count = 3;
765 for (n = 0; n <= max_code; n++) {
766 curlen = nextlen; nextlen = tree[n+1].Len;
767 if (++count < max_count && curlen == nextlen) {
769 }
else if (count < min_count) {
772 }
else if (curlen != 0) {
773 if (curlen != prevlen) {
776 Assert(count >= 3 && count <= 6,
" 3_6?");
779 }
else if (count <= 10) {
785 count = 0; prevlen = curlen;
787 max_count = 138, min_count = 3;
788 }
else if (curlen == nextlen) {
789 max_count = 6, min_count = 3;
791 max_count = 7, min_count = 4;
818 for (max_blindex =
BL_CODES-1; max_blindex >= 3; max_blindex--) {
822 s->
opt_len += 3*(max_blindex+1) + 5+5+4;
823 Tracev((stderr,
"\ndyn trees: dyn %ld, stat %ld",
838 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4,
"not enough codes");
839 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <=
BL_CODES,
841 Tracev((stderr,
"\nbl counts: "));
845 for (rank = 0; rank < blcodes; rank++) {
849 Tracev((stderr,
"\nbl tree: sent %ld", s->bits_sent));
852 Tracev((stderr,
"\nlit tree: sent %ld", s->bits_sent));
855 Tracev((stderr,
"\ndist tree: sent %ld", s->bits_sent));
865 s->compressed_len = (s->compressed_len + 3 + 7) & (
ulg)~7L;
866 s->compressed_len += (stored_len + 4) << 3;
887 s->compressed_len += 10L;
899 s->compressed_len += 10L;
912 ulg opt_lenb, static_lenb;
928 Tracev((stderr,
"\ndist data: dyn %ld, stat %ld", s->
opt_len,
940 opt_lenb = (s->
opt_len+3+7)>>3;
943 Tracev((stderr,
"\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
947 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
950 Assert(buf != (
char*)0,
"lost buf");
951 opt_lenb = static_lenb = stored_len + 5;
955 if (buf != (
char*)0) {
957 if (stored_len+4 <= opt_lenb && buf != (
char*)0) {
969 }
else if (static_lenb >= 0) {
984 s->compressed_len += 3 + s->
opt_len;
987 Assert (s->compressed_len == s->bits_sent,
"bad compressed size");
996 s->compressed_len += 7;
999 Tracev((stderr,
"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1000 s->compressed_len-7*last));
1020 (
ush)
d_code(dist) < (
ush)D_CODES,
"_tr_tally: bad match");
1026 #ifdef TRUNCATE_BLOCK
1033 for (dcode = 0; dcode <
D_CODES; dcode++) {
1038 Tracev((stderr,
"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1039 s->
last_lit, in_length, out_length,
1040 100L - out_length*100L/in_length));
1063 dist = s->
d_buf[lx];
1064 lc = s->
l_buf[lx++];
1067 Tracecv(isgraph(lc), (stderr,
" '%c' ", lc));
1079 Assert (code < D_CODES,
"bad d_code");
1091 "pendingBuf overflow");
1093 }
while (lx < s->last_lit);
1118 unsigned long black_mask = 0xf3ffc07fUL;
1122 for (n = 0; n <= 31; n++, black_mask >>= 1)
1123 if ((black_mask & 1) && (s->
dyn_ltree[n].Freq != 0))
1147 register unsigned res = 0;
1150 code >>= 1, res <<= 1;
1151 }
while (--len > 0);
1184 s->bits_sent = (s->bits_sent+7) & ~7;
1201 s->bits_sent += 2*16;
1205 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 void copy_block(deflate_state *s, charf *buf, unsigned len, int header)
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)
for(p=first;p->value< newval;p=p->next)
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)
struct tree_desc_s bl_desc
struct tree_desc_s d_desc
void ZLIB_INTERNAL _tr_init(deflate_state *s)
#define send_bits(s, value, length)
#define ABC_NAMESPACE_IMPL_END
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)
#define ABC_NAMESPACE_IMPL_START
struct ct_data_s dyn_dtree[2 *D_CODES+1]
uch _dist_code[DIST_CODE_LEN]
struct tree_desc_s l_desc
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)
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]
static char * bits(int n)
local const int extra_blbits[BL_CODES]
struct ct_data_s bl_tree[2 *BL_CODES+1]
#define send_code(s, c, tree)
local void bi_flush(deflate_state *s)
local static_tree_desc static_l_desc