1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include "deflate.h"
38
39#ifdef DEBUG
40# include <ctype.h>
41#endif
42
43
44
45
46
47#define MAX_BL_BITS 7
48
49
50#define END_BLOCK 256
51
52
53#define REP_3_6 16
54
55
56#define REPZ_3_10 17
57
58
59#define REPZ_11_138 18
60
61
62local const int extra_lbits[LENGTH_CODES]
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};
64
65local const int extra_dbits[D_CODES]
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};
67
68local const int extra_blbits[BL_CODES]
69 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
70
71local const uch bl_order[BL_CODES]
72 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
73
74
75
76
77#define Buf_size (8 * 2*sizeof(char))
78
79
80
81
82
83
84
85
86#define DIST_CODE_LEN 512
87
88#if defined(GEN_TREES_H) || !defined(STDC)
89
90
91local ct_data static_ltree[L_CODES+2];
92
93
94
95
96
97
98local ct_data static_dtree[D_CODES];
99
100
101
102
103uch _dist_code[DIST_CODE_LEN];
104
105
106
107
108
109uch _length_code[MAX_MATCH-MIN_MATCH+1];
110
111
112local int base_length[LENGTH_CODES];
113
114
115local int base_dist[D_CODES];
116
117
118#else
119# include "trees.h"
120#endif
121
122struct static_tree_desc_s {
123 const ct_data *static_tree;
124 const intf *extra_bits;
125 int extra_base;
126 int elems;
127 int max_length;
128};
129
130local static_tree_desc static_l_desc =
131{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
132
133local static_tree_desc static_d_desc =
134{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
135
136local static_tree_desc static_bl_desc =
137{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
138
139
140
141
142
143local void tr_static_init OF((void));
144local void init_block OF((deflate_state *s));
145local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
146local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
147local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
148local void build_tree OF((deflate_state *s, tree_desc *desc));
149local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
150local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
151local int build_bl_tree OF((deflate_state *s));
152local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
153 int blcodes));
154local void compress_block OF((deflate_state *s, ct_data *ltree,
155 ct_data *dtree));
156local int detect_data_type OF((deflate_state *s));
157local unsigned bi_reverse OF((unsigned value, int length));
158local void bi_windup OF((deflate_state *s));
159local void bi_flush OF((deflate_state *s));
160local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
161 int header));
162
163#ifdef GEN_TREES_H
164local void gen_trees_header OF((void));
165#endif
166
167#ifndef DEBUG
168# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
169
170
171#else
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); }
175#endif
176
177
178
179
180
181#define put_short(s, w) { \
182 put_byte(s, (uch)((w) & 0xff)); \
183 put_byte(s, (uch)((ush)(w) >> 8)); \
184}
185
186
187
188
189
190#ifdef DEBUG
191local void send_bits OF((deflate_state *s, int value, int length));
192
193local void send_bits(s, value, length)
194 deflate_state *s;
195 int value;
196 int length;
197{
198 Tracevv((stderr," l %2d v %4x ", length, value));
199 Assert(length > 0 && length <= 15, "invalid length");
200 s->bits_sent += (ulg)length;
201
202
203
204
205
206 if (s->bi_valid > (int)Buf_size - length) {
207 s->bi_buf |= (ush)value << s->bi_valid;
208 put_short(s, s->bi_buf);
209 s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
210 s->bi_valid += length - Buf_size;
211 } else {
212 s->bi_buf |= (ush)value << s->bi_valid;
213 s->bi_valid += length;
214 }
215}
216#else
217
218#define send_bits(s, value, length) \
219{ int len = length;\
220 if (s->bi_valid > (int)Buf_size - len) {\
221 int val = value;\
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;\
226 } else {\
227 s->bi_buf |= (ush)(value) << s->bi_valid;\
228 s->bi_valid += len;\
229 }\
230}
231#endif
232
233
234
235
236
237
238
239local void tr_static_init()
240{
241#if defined(GEN_TREES_H) || !defined(STDC)
242 static int static_init_done = 0;
243 int n;
244 int bits;
245 int length;
246 int code;
247 int dist;
248 ush bl_count[MAX_BITS+1];
249
250
251 if (static_init_done) return;
252
253
254#ifdef NO_INIT_GLOBAL_POINTERS
255 static_l_desc.static_tree = static_ltree;
256 static_l_desc.extra_bits = extra_lbits;
257 static_d_desc.static_tree = static_dtree;
258 static_d_desc.extra_bits = extra_dbits;
259 static_bl_desc.extra_bits = extra_blbits;
260#endif
261
262
263 length = 0;
264 for (code = 0; code < LENGTH_CODES-1; code++) {
265 base_length[code] = length;
266 for (n = 0; n < (1<<extra_lbits[code]); n++) {
267 _length_code[length++] = (uch)code;
268 }
269 }
270 Assert (length == 256, "tr_static_init: length != 256");
271
272
273
274
275 _length_code[length-1] = (uch)code;
276
277
278 dist = 0;
279 for (code = 0 ; code < 16; code++) {
280 base_dist[code] = dist;
281 for (n = 0; n < (1<<extra_dbits[code]); n++) {
282 _dist_code[dist++] = (uch)code;
283 }
284 }
285 Assert (dist == 256, "tr_static_init: dist != 256");
286 dist >>= 7;
287 for ( ; code < D_CODES; code++) {
288 base_dist[code] = dist << 7;
289 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
290 _dist_code[256 + dist++] = (uch)code;
291 }
292 }
293 Assert (dist == 256, "tr_static_init: 256+dist != 512");
294
295
296 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
297 n = 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]++;
302
303
304
305
306 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
307
308
309 for (n = 0; n < D_CODES; n++) {
310 static_dtree[n].Len = 5;
311 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
312 }
313 static_init_done = 1;
314
315# ifdef GEN_TREES_H
316 gen_trees_header();
317# endif
318#endif
319}
320
321
322
323
324#ifdef GEN_TREES_H
325# ifndef DEBUG
326# include <stdio.h>
327# endif
328
329# define SEPARATOR(i, last, width) \
330 ((i) == (last)? "\n};\n\n" : \
331 ((i) % (width) == (width)-1 ? ",\n" : ", "))
332
333void gen_trees_header()
334{
335 FILE *header = fopen("trees.h", "w");
336 int i;
337
338 Assert (header != NULL, "Can't open trees.h");
339 fprintf(header,
340 "/* header created automatically with -DGEN_TREES_H */\n\n");
341
342 fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
343 for (i = 0; i < L_CODES+2; i++) {
344 fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
345 static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
346 }
347
348 fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
349 for (i = 0; i < D_CODES; i++) {
350 fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
351 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
352 }
353
354 fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
355 for (i = 0; i < DIST_CODE_LEN; i++) {
356 fprintf(header, "%2u%s", _dist_code[i],
357 SEPARATOR(i, DIST_CODE_LEN-1, 20));
358 }
359
360 fprintf(header,
361 "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
362 for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
363 fprintf(header, "%2u%s", _length_code[i],
364 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
365 }
366
367 fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
368 for (i = 0; i < LENGTH_CODES; i++) {
369 fprintf(header, "%1u%s", base_length[i],
370 SEPARATOR(i, LENGTH_CODES-1, 20));
371 }
372
373 fprintf(header, "local const int base_dist[D_CODES] = {\n");
374 for (i = 0; i < D_CODES; i++) {
375 fprintf(header, "%5u%s", base_dist[i],
376 SEPARATOR(i, D_CODES-1, 10));
377 }
378
379 fclose(header);
380}
381#endif
382
383
384
385
386void ZLIB_INTERNAL _tr_init(s)
387 deflate_state *s;
388{
389 tr_static_init();
390
391 s->l_desc.dyn_tree = s->dyn_ltree;
392 s->l_desc.stat_desc = &static_l_desc;
393
394 s->d_desc.dyn_tree = s->dyn_dtree;
395 s->d_desc.stat_desc = &static_d_desc;
396
397 s->bl_desc.dyn_tree = s->bl_tree;
398 s->bl_desc.stat_desc = &static_bl_desc;
399
400 s->bi_buf = 0;
401 s->bi_valid = 0;
402 s->last_eob_len = 8;
403#ifdef DEBUG
404 s->compressed_len = 0L;
405 s->bits_sent = 0L;
406#endif
407
408
409 init_block(s);
410}
411
412
413
414
415local void init_block(s)
416 deflate_state *s;
417{
418 int n;
419
420
421 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
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;
424
425 s->dyn_ltree[END_BLOCK].Freq = 1;
426 s->opt_len = s->static_len = 0L;
427 s->last_lit = s->matches = 0;
428}
429
430#define SMALLEST 1
431
432
433
434
435
436
437
438#define pqremove(s, tree, top) \
439{\
440 top = s->heap[SMALLEST]; \
441 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
442 pqdownheap(s, tree, SMALLEST); \
443}
444
445
446
447
448
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]))
452
453
454
455
456
457
458
459local void pqdownheap(s, tree, k)
460 deflate_state *s;
461 ct_data *tree;
462 int k;
463{
464 int v = s->heap[k];
465 int j = k << 1;
466 while (j <= s->heap_len) {
467
468 if (j < s->heap_len &&
469 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
470 j++;
471 }
472
473 if (smaller(tree, v, s->heap[j], s->depth)) break;
474
475
476 s->heap[k] = s->heap[j]; k = j;
477
478
479 j <<= 1;
480 }
481 s->heap[k] = v;
482}
483
484
485
486
487
488
489
490
491
492
493
494local void gen_bitlen(s, desc)
495 deflate_state *s;
496 tree_desc *desc;
497{
498 ct_data *tree = desc->dyn_tree;
499 int max_code = desc->max_code;
500 const ct_data *stree = desc->stat_desc->static_tree;
501 const intf *extra = desc->stat_desc->extra_bits;
502 int base = desc->stat_desc->extra_base;
503 int max_length = desc->stat_desc->max_length;
504 int h;
505 int n, m;
506 int bits;
507 int xbits;
508 ush f;
509 int overflow = 0;
510
511 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
512
513
514
515
516 tree[s->heap[s->heap_max]].Len = 0;
517
518 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
519 n = s->heap[h];
520 bits = tree[tree[n].Dad].Len + 1;
521 if (bits > max_length) bits = max_length, overflow++;
522 tree[n].Len = (ush)bits;
523
524
525 if (n > max_code) continue;
526
527 s->bl_count[bits]++;
528 xbits = 0;
529 if (n >= base) xbits = extra[n-base];
530 f = tree[n].Freq;
531 s->opt_len += (ulg)f * (bits + xbits);
532 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
533 }
534 if (overflow == 0) return;
535
536 Trace((stderr,"\nbit length overflow\n"));
537
538
539
540 do {
541 bits = max_length-1;
542 while (s->bl_count[bits] == 0) bits--;
543 s->bl_count[bits]--;
544 s->bl_count[bits+1] += 2;
545 s->bl_count[max_length]--;
546
547
548
549 overflow -= 2;
550 } while (overflow > 0);
551
552
553
554
555
556
557 for (bits = max_length; bits != 0; bits--) {
558 n = s->bl_count[bits];
559 while (n != 0) {
560 m = s->heap[--h];
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)
565 *(long)tree[m].Freq;
566 tree[m].Len = (ush)bits;
567 }
568 n--;
569 }
570 }
571}
572
573
574
575
576
577
578
579
580
581local void gen_codes (tree, max_code, bl_count)
582 ct_data *tree;
583 int max_code;
584 ushf *bl_count;
585{
586 ush next_code[MAX_BITS+1];
587 ush code = 0;
588 int bits;
589 int n;
590
591
592
593
594 for (bits = 1; bits <= MAX_BITS; bits++) {
595 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
596 }
597
598
599
600 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
601 "inconsistent bit counts");
602 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
603
604 for (n = 0; n <= max_code; n++) {
605 int len = tree[n].Len;
606 if (len == 0) continue;
607
608 tree[n].Code = bi_reverse(next_code[len]++, len);
609
610 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
611 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
612 }
613}
614
615
616
617
618
619
620
621
622
623local void build_tree(s, desc)
624 deflate_state *s;
625 tree_desc *desc;
626{
627 ct_data *tree = desc->dyn_tree;
628 const ct_data *stree = desc->stat_desc->static_tree;
629 int elems = desc->stat_desc->elems;
630 int n, m;
631 int max_code = -1;
632 int node;
633
634
635
636
637
638 s->heap_len = 0, s->heap_max = HEAP_SIZE;
639
640 for (n = 0; n < elems; n++) {
641 if (tree[n].Freq != 0) {
642 s->heap[++(s->heap_len)] = max_code = n;
643 s->depth[n] = 0;
644 } else {
645 tree[n].Len = 0;
646 }
647 }
648
649
650
651
652
653
654 while (s->heap_len < 2) {
655 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
656 tree[node].Freq = 1;
657 s->depth[node] = 0;
658 s->opt_len--; if (stree) s->static_len -= stree[node].Len;
659
660 }
661 desc->max_code = max_code;
662
663
664
665
666 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
667
668
669
670
671 node = elems;
672 do {
673 pqremove(s, tree, n);
674 m = s->heap[SMALLEST];
675
676 s->heap[--(s->heap_max)] = n;
677 s->heap[--(s->heap_max)] = m;
678
679
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;
684#ifdef DUMP_BL_TREE
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);
688 }
689#endif
690
691 s->heap[SMALLEST] = node++;
692 pqdownheap(s, tree, SMALLEST);
693
694 } while (s->heap_len >= 2);
695
696 s->heap[--(s->heap_max)] = s->heap[SMALLEST];
697
698
699
700
701 gen_bitlen(s, (tree_desc *)desc);
702
703
704 gen_codes ((ct_data *)tree, max_code, s->bl_count);
705}
706
707
708
709
710
711local void scan_tree (s, tree, max_code)
712 deflate_state *s;
713 ct_data *tree;
714 int max_code;
715{
716 int n;
717 int prevlen = -1;
718 int curlen;
719 int nextlen = tree[0].Len;
720 int count = 0;
721 int max_count = 7;
722 int min_count = 4;
723
724 if (nextlen == 0) max_count = 138, min_count = 3;
725 tree[max_code+1].Len = (ush)0xffff;
726
727 for (n = 0; n <= max_code; n++) {
728 curlen = nextlen; nextlen = tree[n+1].Len;
729 if (++count < max_count && curlen == nextlen) {
730 continue;
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++;
735 s->bl_tree[REP_3_6].Freq++;
736 } else if (count <= 10) {
737 s->bl_tree[REPZ_3_10].Freq++;
738 } else {
739 s->bl_tree[REPZ_11_138].Freq++;
740 }
741 count = 0; prevlen = curlen;
742 if (nextlen == 0) {
743 max_count = 138, min_count = 3;
744 } else if (curlen == nextlen) {
745 max_count = 6, min_count = 3;
746 } else {
747 max_count = 7, min_count = 4;
748 }
749 }
750}
751
752
753
754
755
756local void send_tree (s, tree, max_code)
757 deflate_state *s;
758 ct_data *tree;
759 int max_code;
760{
761 int n;
762 int prevlen = -1;
763 int curlen;
764 int nextlen = tree[0].Len;
765 int count = 0;
766 int max_count = 7;
767 int min_count = 4;
768
769
770 if (nextlen == 0) max_count = 138, min_count = 3;
771
772 for (n = 0; n <= max_code; n++) {
773 curlen = nextlen; nextlen = tree[n+1].Len;
774 if (++count < max_count && curlen == nextlen) {
775 continue;
776 } else if (count < min_count) {
777 do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
778
779 } else if (curlen != 0) {
780 if (curlen != prevlen) {
781 send_code(s, curlen, s->bl_tree); count--;
782 }
783 Assert(count >= 3 && count <= 6, " 3_6?");
784 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
785
786 } else if (count <= 10) {
787 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
788
789 } else {
790 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
791 }
792 count = 0; prevlen = curlen;
793 if (nextlen == 0) {
794 max_count = 138, min_count = 3;
795 } else if (curlen == nextlen) {
796 max_count = 6, min_count = 3;
797 } else {
798 max_count = 7, min_count = 4;
799 }
800 }
801}
802
803
804
805
806
807local int build_bl_tree(s)
808 deflate_state *s;
809{
810 int max_blindex;
811
812
813 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
814 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
815
816
817 build_tree(s, (tree_desc *)(&(s->bl_desc)));
818
819
820
821
822
823
824
825
826 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
827 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
828 }
829
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));
833
834 return max_blindex;
835}
836
837
838
839
840
841
842local void send_all_trees(s, lcodes, dcodes, blcodes)
843 deflate_state *s;
844 int lcodes, dcodes, blcodes;
845{
846 int rank;
847
848 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
849 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
850 "too many codes");
851 Tracev((stderr, "\nbl counts: "));
852 send_bits(s, lcodes-257, 5);
853 send_bits(s, dcodes-1, 5);
854 send_bits(s, blcodes-4, 4);
855 for (rank = 0; rank < blcodes; rank++) {
856 Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
857 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
858 }
859 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
860
861 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1);
862 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
863
864 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1);
865 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
866}
867
868
869
870
871void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
872 deflate_state *s;
873 charf *buf;
874 ulg stored_len;
875 int last;
876{
877 send_bits(s, (STORED_BLOCK<<1)+last, 3);
878#ifdef DEBUG
879 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
880 s->compressed_len += (stored_len + 4) << 3;
881#endif
882 copy_block(s, buf, (unsigned)stored_len, 1);
883}
884
885
886
887
888
889
890
891
892
893
894
895
896void ZLIB_INTERNAL _tr_align(s)
897 deflate_state *s;
898{
899 send_bits(s, STATIC_TREES<<1, 3);
900 send_code(s, END_BLOCK, static_ltree);
901#ifdef DEBUG
902 s->compressed_len += 10L;
903#endif
904 bi_flush(s);
905
906
907
908
909
910 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
911 send_bits(s, STATIC_TREES<<1, 3);
912 send_code(s, END_BLOCK, static_ltree);
913#ifdef DEBUG
914 s->compressed_len += 10L;
915#endif
916 bi_flush(s);
917 }
918 s->last_eob_len = 7;
919}
920
921
922
923
924
925void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
926 deflate_state *s;
927 charf *buf;
928 ulg stored_len;
929 int last;
930{
931 ulg opt_lenb, static_lenb;
932 int max_blindex = 0;
933
934
935 if (s->level > 0) {
936
937
938 if (s->strm->data_type == Z_UNKNOWN)
939 s->strm->data_type = detect_data_type(s);
940
941
942 build_tree(s, (tree_desc *)(&(s->l_desc)));
943 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
944 s->static_len));
945
946 build_tree(s, (tree_desc *)(&(s->d_desc)));
947 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
948 s->static_len));
949
950
951
952
953
954
955
956 max_blindex = build_bl_tree(s);
957
958
959 opt_lenb = (s->opt_len+3+7)>>3;
960 static_lenb = (s->static_len+3+7)>>3;
961
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,
964 s->last_lit));
965
966 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
967
968 } else {
969 Assert(buf != (char*)0, "lost buf");
970 opt_lenb = static_lenb = stored_len + 5;
971 }
972
973#ifdef FORCE_STORED
974 if (buf != (char*)0) {
975#else
976 if (stored_len+4 <= opt_lenb && buf != (char*)0) {
977
978#endif
979
980
981
982
983
984
985 _tr_stored_block(s, buf, stored_len, last);
986
987#ifdef FORCE_STATIC
988 } else if (static_lenb >= 0) {
989#else
990 } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
991#endif
992 send_bits(s, (STATIC_TREES<<1)+last, 3);
993 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
994#ifdef DEBUG
995 s->compressed_len += 3 + s->static_len;
996#endif
997 } else {
998 send_bits(s, (DYN_TREES<<1)+last, 3);
999 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
1000 max_blindex+1);
1001 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
1002#ifdef DEBUG
1003 s->compressed_len += 3 + s->opt_len;
1004#endif
1005 }
1006 Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1007
1008
1009
1010 init_block(s);
1011
1012 if (last) {
1013 bi_windup(s);
1014#ifdef DEBUG
1015 s->compressed_len += 7;
1016#endif
1017 }
1018 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1019 s->compressed_len-7*last));
1020}
1021
1022
1023
1024
1025
1026int ZLIB_INTERNAL _tr_tally (s, dist, lc)
1027 deflate_state *s;
1028 unsigned dist;
1029 unsigned lc;
1030{
1031 s->d_buf[s->last_lit] = (ush)dist;
1032 s->l_buf[s->last_lit++] = (uch)lc;
1033 if (dist == 0) {
1034
1035 s->dyn_ltree[lc].Freq++;
1036 } else {
1037 s->matches++;
1038
1039 dist--;
1040 Assert((ush)dist < (ush)MAX_DIST(s) &&
1041 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1042 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1043
1044 s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
1045 s->dyn_dtree[d_code(dist)].Freq++;
1046 }
1047
1048#ifdef TRUNCATE_BLOCK
1049
1050 if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
1051
1052 ulg out_length = (ulg)s->last_lit*8L;
1053 ulg in_length = (ulg)((long)s->strstart - s->block_start);
1054 int dcode;
1055 for (dcode = 0; dcode < D_CODES; dcode++) {
1056 out_length += (ulg)s->dyn_dtree[dcode].Freq *
1057 (5L+extra_dbits[dcode]);
1058 }
1059 out_length >>= 3;
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;
1064 }
1065#endif
1066 return (s->last_lit == s->lit_bufsize-1);
1067
1068
1069
1070
1071}
1072
1073
1074
1075
1076local void compress_block(s, ltree, dtree)
1077 deflate_state *s;
1078 ct_data *ltree;
1079 ct_data *dtree;
1080{
1081 unsigned dist;
1082 int lc;
1083 unsigned lx = 0;
1084 unsigned code;
1085 int extra;
1086
1087 if (s->last_lit != 0) do {
1088 dist = s->d_buf[lx];
1089 lc = s->l_buf[lx++];
1090 if (dist == 0) {
1091 send_code(s, lc, ltree);
1092 Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1093 } else {
1094
1095 code = _length_code[lc];
1096 send_code(s, code+LITERALS+1, ltree);
1097 extra = extra_lbits[code];
1098 if (extra != 0) {
1099 lc -= base_length[code];
1100 send_bits(s, lc, extra);
1101 }
1102 dist--;
1103 code = d_code(dist);
1104 Assert (code < D_CODES, "bad d_code");
1105
1106 send_code(s, code, dtree);
1107 extra = extra_dbits[code];
1108 if (extra != 0) {
1109 dist -= base_dist[code];
1110 send_bits(s, dist, extra);
1111 }
1112 }
1113
1114
1115 Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
1116 "pendingBuf overflow");
1117
1118 } while (lx < s->last_lit);
1119
1120 send_code(s, END_BLOCK, ltree);
1121 s->last_eob_len = ltree[END_BLOCK].Len;
1122}
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137local int detect_data_type(s)
1138 deflate_state *s;
1139{
1140
1141
1142
1143
1144 unsigned long black_mask = 0xf3ffc07fUL;
1145 int n;
1146
1147
1148 for (n = 0; n <= 31; n++, black_mask >>= 1)
1149 if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1150 return Z_BINARY;
1151
1152
1153 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1154 || s->dyn_ltree[13].Freq != 0)
1155 return Z_TEXT;
1156 for (n = 32; n < LITERALS; n++)
1157 if (s->dyn_ltree[n].Freq != 0)
1158 return Z_TEXT;
1159
1160
1161
1162
1163 return Z_BINARY;
1164}
1165
1166
1167
1168
1169
1170
1171local unsigned bi_reverse(value, len)
1172 unsigned value;
1173 int len;
1174{
1175 register unsigned res = 0;
1176 do {
1177 res |= value & 1;
1178 value >>= 1, res <<= 1;
1179 } while (--len > 0);
1180 return res >> 1;
1181}
1182
1183
1184
1185
1186local void bi_flush(s)
1187 deflate_state *s;
1188{
1189 if (s->bi_valid == 16) {
1190 put_short(s, s->bi_buf);
1191 s->bi_buf = 0;
1192 s->bi_valid = 0;
1193 } else if (s->bi_valid >= 8) {
1194 put_byte(s, (Byte)s->bi_buf);
1195 s->bi_buf >>= 8;
1196 s->bi_valid -= 8;
1197 }
1198}
1199
1200
1201
1202
1203local void bi_windup(s)
1204 deflate_state *s;
1205{
1206 if (s->bi_valid > 8) {
1207 put_short(s, s->bi_buf);
1208 } else if (s->bi_valid > 0) {
1209 put_byte(s, (Byte)s->bi_buf);
1210 }
1211 s->bi_buf = 0;
1212 s->bi_valid = 0;
1213#ifdef DEBUG
1214 s->bits_sent = (s->bits_sent+7) & ~7;
1215#endif
1216}
1217
1218
1219
1220
1221
1222local void copy_block(s, buf, len, header)
1223 deflate_state *s;
1224 charf *buf;
1225 unsigned len;
1226 int header;
1227{
1228 bi_windup(s);
1229 s->last_eob_len = 8;
1230
1231 if (header) {
1232 put_short(s, (ush)len);
1233 put_short(s, (ush)~len);
1234#ifdef DEBUG
1235 s->bits_sent += 2*16;
1236#endif
1237 }
1238#ifdef DEBUG
1239 s->bits_sent += (ulg)len<<3;
1240#endif
1241 while (len--) {
1242 put_byte(s, *buf++);
1243 }
1244}
1245