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 <linux/zutil.h>
38#include <linux/bitrev.h>
39#include "defutil.h"
40
41#ifdef DEBUG_ZLIB
42# include <ctype.h>
43#endif
44
45
46
47
48
49#define MAX_BL_BITS 7
50
51
52#define END_BLOCK 256
53
54
55#define REP_3_6 16
56
57
58#define REPZ_3_10 17
59
60
61#define REPZ_11_138 18
62
63
64static const int extra_lbits[LENGTH_CODES]
65 = {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
67static const int extra_dbits[D_CODES]
68 = {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
70static const int extra_blbits[BL_CODES]
71 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
72
73static const uch bl_order[BL_CODES]
74 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
75
76
77
78
79
80
81
82
83static ct_data static_ltree[L_CODES+2];
84
85
86
87
88
89
90static ct_data static_dtree[D_CODES];
91
92
93
94
95static uch dist_code[512];
96
97
98
99
100
101static uch length_code[MAX_MATCH-MIN_MATCH+1];
102
103
104static int base_length[LENGTH_CODES];
105
106
107static int base_dist[D_CODES];
108
109
110struct static_tree_desc_s {
111 const ct_data *static_tree;
112 const int *extra_bits;
113 int extra_base;
114 int elems;
115 int max_length;
116};
117
118static static_tree_desc static_l_desc =
119{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
120
121static static_tree_desc static_d_desc =
122{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
123
124static static_tree_desc static_bl_desc =
125{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
126
127
128
129
130
131static void tr_static_init (void);
132static void init_block (deflate_state *s);
133static void pqdownheap (deflate_state *s, ct_data *tree, int k);
134static void gen_bitlen (deflate_state *s, tree_desc *desc);
135static void gen_codes (ct_data *tree, int max_code, ush *bl_count);
136static void build_tree (deflate_state *s, tree_desc *desc);
137static void scan_tree (deflate_state *s, ct_data *tree, int max_code);
138static void send_tree (deflate_state *s, ct_data *tree, int max_code);
139static int build_bl_tree (deflate_state *s);
140static void send_all_trees (deflate_state *s, int lcodes, int dcodes,
141 int blcodes);
142static void compress_block (deflate_state *s, ct_data *ltree,
143 ct_data *dtree);
144static void set_data_type (deflate_state *s);
145static void bi_flush (deflate_state *s);
146static void copy_block (deflate_state *s, char *buf, unsigned len,
147 int header);
148
149#ifndef DEBUG_ZLIB
150# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
151
152
153#else
154# define send_code(s, c, tree) \
155 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
156 send_bits(s, tree[c].Code, tree[c].Len); }
157#endif
158
159#define d_code(dist) \
160 ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
161
162
163
164
165
166
167
168
169
170
171static void tr_static_init(void)
172{
173 static int static_init_done;
174 int n;
175 int bits;
176 int length;
177 int code;
178 int dist;
179 ush bl_count[MAX_BITS+1];
180
181
182 if (static_init_done) return;
183
184
185 length = 0;
186 for (code = 0; code < LENGTH_CODES-1; code++) {
187 base_length[code] = length;
188 for (n = 0; n < (1<<extra_lbits[code]); n++) {
189 length_code[length++] = (uch)code;
190 }
191 }
192 Assert (length == 256, "tr_static_init: length != 256");
193
194
195
196
197 length_code[length-1] = (uch)code;
198
199
200 dist = 0;
201 for (code = 0 ; code < 16; code++) {
202 base_dist[code] = dist;
203 for (n = 0; n < (1<<extra_dbits[code]); n++) {
204 dist_code[dist++] = (uch)code;
205 }
206 }
207 Assert (dist == 256, "tr_static_init: dist != 256");
208 dist >>= 7;
209 for ( ; code < D_CODES; code++) {
210 base_dist[code] = dist << 7;
211 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
212 dist_code[256 + dist++] = (uch)code;
213 }
214 }
215 Assert (dist == 256, "tr_static_init: 256+dist != 512");
216
217
218 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
219 n = 0;
220 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
221 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
222 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
223 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
224
225
226
227
228 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
229
230
231 for (n = 0; n < D_CODES; n++) {
232 static_dtree[n].Len = 5;
233 static_dtree[n].Code = bitrev32((u32)n) >> (32 - 5);
234 }
235 static_init_done = 1;
236}
237
238
239
240
241void zlib_tr_init(
242 deflate_state *s
243)
244{
245 tr_static_init();
246
247 s->compressed_len = 0L;
248
249 s->l_desc.dyn_tree = s->dyn_ltree;
250 s->l_desc.stat_desc = &static_l_desc;
251
252 s->d_desc.dyn_tree = s->dyn_dtree;
253 s->d_desc.stat_desc = &static_d_desc;
254
255 s->bl_desc.dyn_tree = s->bl_tree;
256 s->bl_desc.stat_desc = &static_bl_desc;
257
258 s->bi_buf = 0;
259 s->bi_valid = 0;
260 s->last_eob_len = 8;
261#ifdef DEBUG_ZLIB
262 s->bits_sent = 0L;
263#endif
264
265
266 init_block(s);
267}
268
269
270
271
272static void init_block(
273 deflate_state *s
274)
275{
276 int n;
277
278
279 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
280 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
281 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
282
283 s->dyn_ltree[END_BLOCK].Freq = 1;
284 s->opt_len = s->static_len = 0L;
285 s->last_lit = s->matches = 0;
286}
287
288#define SMALLEST 1
289
290
291
292
293
294
295
296#define pqremove(s, tree, top) \
297{\
298 top = s->heap[SMALLEST]; \
299 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
300 pqdownheap(s, tree, SMALLEST); \
301}
302
303
304
305
306
307#define smaller(tree, n, m, depth) \
308 (tree[n].Freq < tree[m].Freq || \
309 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
310
311
312
313
314
315
316
317static void pqdownheap(
318 deflate_state *s,
319 ct_data *tree,
320 int k
321)
322{
323 int v = s->heap[k];
324 int j = k << 1;
325 while (j <= s->heap_len) {
326
327 if (j < s->heap_len &&
328 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
329 j++;
330 }
331
332 if (smaller(tree, v, s->heap[j], s->depth)) break;
333
334
335 s->heap[k] = s->heap[j]; k = j;
336
337
338 j <<= 1;
339 }
340 s->heap[k] = v;
341}
342
343
344
345
346
347
348
349
350
351
352
353static void gen_bitlen(
354 deflate_state *s,
355 tree_desc *desc
356)
357{
358 ct_data *tree = desc->dyn_tree;
359 int max_code = desc->max_code;
360 const ct_data *stree = desc->stat_desc->static_tree;
361 const int *extra = desc->stat_desc->extra_bits;
362 int base = desc->stat_desc->extra_base;
363 int max_length = desc->stat_desc->max_length;
364 int h;
365 int n, m;
366 int bits;
367 int xbits;
368 ush f;
369 int overflow = 0;
370
371 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
372
373
374
375
376 tree[s->heap[s->heap_max]].Len = 0;
377
378 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
379 n = s->heap[h];
380 bits = tree[tree[n].Dad].Len + 1;
381 if (bits > max_length) bits = max_length, overflow++;
382 tree[n].Len = (ush)bits;
383
384
385 if (n > max_code) continue;
386
387 s->bl_count[bits]++;
388 xbits = 0;
389 if (n >= base) xbits = extra[n-base];
390 f = tree[n].Freq;
391 s->opt_len += (ulg)f * (bits + xbits);
392 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
393 }
394 if (overflow == 0) return;
395
396 Trace((stderr,"\nbit length overflow\n"));
397
398
399
400 do {
401 bits = max_length-1;
402 while (s->bl_count[bits] == 0) bits--;
403 s->bl_count[bits]--;
404 s->bl_count[bits+1] += 2;
405 s->bl_count[max_length]--;
406
407
408
409 overflow -= 2;
410 } while (overflow > 0);
411
412
413
414
415
416
417 for (bits = max_length; bits != 0; bits--) {
418 n = s->bl_count[bits];
419 while (n != 0) {
420 m = s->heap[--h];
421 if (m > max_code) continue;
422 if (tree[m].Len != (unsigned) bits) {
423 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
424 s->opt_len += ((long)bits - (long)tree[m].Len)
425 *(long)tree[m].Freq;
426 tree[m].Len = (ush)bits;
427 }
428 n--;
429 }
430 }
431}
432
433
434
435
436
437
438
439
440
441static void gen_codes(
442 ct_data *tree,
443 int max_code,
444 ush *bl_count
445)
446{
447 ush next_code[MAX_BITS+1];
448 ush code = 0;
449 int bits;
450 int n;
451
452
453
454
455 for (bits = 1; bits <= MAX_BITS; bits++) {
456 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
457 }
458
459
460
461 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
462 "inconsistent bit counts");
463 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
464
465 for (n = 0; n <= max_code; n++) {
466 int len = tree[n].Len;
467 if (len == 0) continue;
468
469 tree[n].Code = bitrev32((u32)(next_code[len]++)) >> (32 - len);
470
471 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
472 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
473 }
474}
475
476
477
478
479
480
481
482
483
484static void build_tree(
485 deflate_state *s,
486 tree_desc *desc
487)
488{
489 ct_data *tree = desc->dyn_tree;
490 const ct_data *stree = desc->stat_desc->static_tree;
491 int elems = desc->stat_desc->elems;
492 int n, m;
493 int max_code = -1;
494 int node;
495
496
497
498
499
500 s->heap_len = 0, s->heap_max = HEAP_SIZE;
501
502 for (n = 0; n < elems; n++) {
503 if (tree[n].Freq != 0) {
504 s->heap[++(s->heap_len)] = max_code = n;
505 s->depth[n] = 0;
506 } else {
507 tree[n].Len = 0;
508 }
509 }
510
511
512
513
514
515
516 while (s->heap_len < 2) {
517 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
518 tree[node].Freq = 1;
519 s->depth[node] = 0;
520 s->opt_len--; if (stree) s->static_len -= stree[node].Len;
521
522 }
523 desc->max_code = max_code;
524
525
526
527
528 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
529
530
531
532
533 node = elems;
534 do {
535 pqremove(s, tree, n);
536 m = s->heap[SMALLEST];
537
538 s->heap[--(s->heap_max)] = n;
539 s->heap[--(s->heap_max)] = m;
540
541
542 tree[node].Freq = tree[n].Freq + tree[m].Freq;
543 s->depth[node] = (uch) (max(s->depth[n], s->depth[m]) + 1);
544 tree[n].Dad = tree[m].Dad = (ush)node;
545#ifdef DUMP_BL_TREE
546 if (tree == s->bl_tree) {
547 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
548 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
549 }
550#endif
551
552 s->heap[SMALLEST] = node++;
553 pqdownheap(s, tree, SMALLEST);
554
555 } while (s->heap_len >= 2);
556
557 s->heap[--(s->heap_max)] = s->heap[SMALLEST];
558
559
560
561
562 gen_bitlen(s, (tree_desc *)desc);
563
564
565 gen_codes ((ct_data *)tree, max_code, s->bl_count);
566}
567
568
569
570
571
572static void scan_tree(
573 deflate_state *s,
574 ct_data *tree,
575 int max_code
576)
577{
578 int n;
579 int prevlen = -1;
580 int curlen;
581 int nextlen = tree[0].Len;
582 int count = 0;
583 int max_count = 7;
584 int min_count = 4;
585
586 if (nextlen == 0) max_count = 138, min_count = 3;
587 tree[max_code+1].Len = (ush)0xffff;
588
589 for (n = 0; n <= max_code; n++) {
590 curlen = nextlen; nextlen = tree[n+1].Len;
591 if (++count < max_count && curlen == nextlen) {
592 continue;
593 } else if (count < min_count) {
594 s->bl_tree[curlen].Freq += count;
595 } else if (curlen != 0) {
596 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
597 s->bl_tree[REP_3_6].Freq++;
598 } else if (count <= 10) {
599 s->bl_tree[REPZ_3_10].Freq++;
600 } else {
601 s->bl_tree[REPZ_11_138].Freq++;
602 }
603 count = 0; prevlen = curlen;
604 if (nextlen == 0) {
605 max_count = 138, min_count = 3;
606 } else if (curlen == nextlen) {
607 max_count = 6, min_count = 3;
608 } else {
609 max_count = 7, min_count = 4;
610 }
611 }
612}
613
614
615
616
617
618static void send_tree(
619 deflate_state *s,
620 ct_data *tree,
621 int max_code
622)
623{
624 int n;
625 int prevlen = -1;
626 int curlen;
627 int nextlen = tree[0].Len;
628 int count = 0;
629 int max_count = 7;
630 int min_count = 4;
631
632
633 if (nextlen == 0) max_count = 138, min_count = 3;
634
635 for (n = 0; n <= max_code; n++) {
636 curlen = nextlen; nextlen = tree[n+1].Len;
637 if (++count < max_count && curlen == nextlen) {
638 continue;
639 } else if (count < min_count) {
640 do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
641
642 } else if (curlen != 0) {
643 if (curlen != prevlen) {
644 send_code(s, curlen, s->bl_tree); count--;
645 }
646 Assert(count >= 3 && count <= 6, " 3_6?");
647 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
648
649 } else if (count <= 10) {
650 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
651
652 } else {
653 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
654 }
655 count = 0; prevlen = curlen;
656 if (nextlen == 0) {
657 max_count = 138, min_count = 3;
658 } else if (curlen == nextlen) {
659 max_count = 6, min_count = 3;
660 } else {
661 max_count = 7, min_count = 4;
662 }
663 }
664}
665
666
667
668
669
670static int build_bl_tree(
671 deflate_state *s
672)
673{
674 int max_blindex;
675
676
677 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
678 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
679
680
681 build_tree(s, (tree_desc *)(&(s->bl_desc)));
682
683
684
685
686
687
688
689
690 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
691 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
692 }
693
694 s->opt_len += 3*(max_blindex+1) + 5+5+4;
695 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
696 s->opt_len, s->static_len));
697
698 return max_blindex;
699}
700
701
702
703
704
705
706static void send_all_trees(
707 deflate_state *s,
708 int lcodes,
709 int dcodes,
710 int blcodes
711)
712{
713 int rank;
714
715 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
716 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
717 "too many codes");
718 Tracev((stderr, "\nbl counts: "));
719 send_bits(s, lcodes-257, 5);
720 send_bits(s, dcodes-1, 5);
721 send_bits(s, blcodes-4, 4);
722 for (rank = 0; rank < blcodes; rank++) {
723 Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
724 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
725 }
726 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
727
728 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1);
729 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
730
731 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1);
732 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
733}
734
735
736
737
738void zlib_tr_stored_block(
739 deflate_state *s,
740 char *buf,
741 ulg stored_len,
742 int eof
743)
744{
745 send_bits(s, (STORED_BLOCK<<1)+eof, 3);
746 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
747 s->compressed_len += (stored_len + 4) << 3;
748
749 copy_block(s, buf, (unsigned)stored_len, 1);
750}
751
752
753
754void zlib_tr_stored_type_only(
755 deflate_state *s
756)
757{
758 send_bits(s, (STORED_BLOCK << 1), 3);
759 bi_windup(s);
760 s->compressed_len = (s->compressed_len + 3) & ~7L;
761}
762
763
764
765
766
767
768
769
770
771
772
773
774
775void zlib_tr_align(
776 deflate_state *s
777)
778{
779 send_bits(s, STATIC_TREES<<1, 3);
780 send_code(s, END_BLOCK, static_ltree);
781 s->compressed_len += 10L;
782 bi_flush(s);
783
784
785
786
787
788 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
789 send_bits(s, STATIC_TREES<<1, 3);
790 send_code(s, END_BLOCK, static_ltree);
791 s->compressed_len += 10L;
792 bi_flush(s);
793 }
794 s->last_eob_len = 7;
795}
796
797
798
799
800
801
802ulg zlib_tr_flush_block(
803 deflate_state *s,
804 char *buf,
805 ulg stored_len,
806 int eof
807)
808{
809 ulg opt_lenb, static_lenb;
810 int max_blindex = 0;
811
812
813 if (s->level > 0) {
814
815
816 if (s->data_type == Z_UNKNOWN) set_data_type(s);
817
818
819 build_tree(s, (tree_desc *)(&(s->l_desc)));
820 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
821 s->static_len));
822
823 build_tree(s, (tree_desc *)(&(s->d_desc)));
824 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
825 s->static_len));
826
827
828
829
830
831
832
833 max_blindex = build_bl_tree(s);
834
835
836 opt_lenb = (s->opt_len+3+7)>>3;
837 static_lenb = (s->static_len+3+7)>>3;
838
839 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
840 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
841 s->last_lit));
842
843 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
844
845 } else {
846 Assert(buf != (char*)0, "lost buf");
847 opt_lenb = static_lenb = stored_len + 5;
848 }
849
850
851
852
853
854#ifdef STORED_FILE_OK
855# ifdef FORCE_STORED_FILE
856 if (eof && s->compressed_len == 0L) {
857# else
858 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) {
859# endif
860
861 if (buf == (char*)0) error ("block vanished");
862
863 copy_block(s, buf, (unsigned)stored_len, 0);
864 s->compressed_len = stored_len << 3;
865 s->method = STORED;
866 } else
867#endif
868
869#ifdef FORCE_STORED
870 if (buf != (char*)0) {
871#else
872 if (stored_len+4 <= opt_lenb && buf != (char*)0) {
873
874#endif
875
876
877
878
879
880
881 zlib_tr_stored_block(s, buf, stored_len, eof);
882
883#ifdef FORCE_STATIC
884 } else if (static_lenb >= 0) {
885#else
886 } else if (static_lenb == opt_lenb) {
887#endif
888 send_bits(s, (STATIC_TREES<<1)+eof, 3);
889 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
890 s->compressed_len += 3 + s->static_len;
891 } else {
892 send_bits(s, (DYN_TREES<<1)+eof, 3);
893 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
894 max_blindex+1);
895 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
896 s->compressed_len += 3 + s->opt_len;
897 }
898 Assert (s->compressed_len == s->bits_sent, "bad compressed size");
899 init_block(s);
900
901 if (eof) {
902 bi_windup(s);
903 s->compressed_len += 7;
904 }
905 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
906 s->compressed_len-7*eof));
907
908 return s->compressed_len >> 3;
909}
910
911
912
913
914
915int zlib_tr_tally(
916 deflate_state *s,
917 unsigned dist,
918 unsigned lc
919)
920{
921 s->d_buf[s->last_lit] = (ush)dist;
922 s->l_buf[s->last_lit++] = (uch)lc;
923 if (dist == 0) {
924
925 s->dyn_ltree[lc].Freq++;
926 } else {
927 s->matches++;
928
929 dist--;
930 Assert((ush)dist < (ush)MAX_DIST(s) &&
931 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
932 (ush)d_code(dist) < (ush)D_CODES, "zlib_tr_tally: bad match");
933
934 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
935 s->dyn_dtree[d_code(dist)].Freq++;
936 }
937
938
939 if ((s->last_lit & 0xfff) == 0 && s->level > 2) {
940
941 ulg out_length = (ulg)s->last_lit*8L;
942 ulg in_length = (ulg)((long)s->strstart - s->block_start);
943 int dcode;
944 for (dcode = 0; dcode < D_CODES; dcode++) {
945 out_length += (ulg)s->dyn_dtree[dcode].Freq *
946 (5L+extra_dbits[dcode]);
947 }
948 out_length >>= 3;
949 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
950 s->last_lit, in_length, out_length,
951 100L - out_length*100L/in_length));
952 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
953 }
954 return (s->last_lit == s->lit_bufsize-1);
955
956
957
958
959}
960
961
962
963
964static void compress_block(
965 deflate_state *s,
966 ct_data *ltree,
967 ct_data *dtree
968)
969{
970 unsigned dist;
971 int lc;
972 unsigned lx = 0;
973 unsigned code;
974 int extra;
975
976 if (s->last_lit != 0) do {
977 dist = s->d_buf[lx];
978 lc = s->l_buf[lx++];
979 if (dist == 0) {
980 send_code(s, lc, ltree);
981 Tracecv(isgraph(lc), (stderr," '%c' ", lc));
982 } else {
983
984 code = length_code[lc];
985 send_code(s, code+LITERALS+1, ltree);
986 extra = extra_lbits[code];
987 if (extra != 0) {
988 lc -= base_length[code];
989 send_bits(s, lc, extra);
990 }
991 dist--;
992 code = d_code(dist);
993 Assert (code < D_CODES, "bad d_code");
994
995 send_code(s, code, dtree);
996 extra = extra_dbits[code];
997 if (extra != 0) {
998 dist -= base_dist[code];
999 send_bits(s, dist, extra);
1000 }
1001 }
1002
1003
1004 Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
1005
1006 } while (lx < s->last_lit);
1007
1008 send_code(s, END_BLOCK, ltree);
1009 s->last_eob_len = ltree[END_BLOCK].Len;
1010}
1011
1012
1013
1014
1015
1016
1017
1018static void set_data_type(
1019 deflate_state *s
1020)
1021{
1022 int n = 0;
1023 unsigned ascii_freq = 0;
1024 unsigned bin_freq = 0;
1025 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
1026 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
1027 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
1028 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
1029}
1030
1031
1032
1033
1034
1035static void copy_block(
1036 deflate_state *s,
1037 char *buf,
1038 unsigned len,
1039 int header
1040)
1041{
1042 bi_windup(s);
1043 s->last_eob_len = 8;
1044
1045 if (header) {
1046 put_short(s, (ush)len);
1047 put_short(s, (ush)~len);
1048#ifdef DEBUG_ZLIB
1049 s->bits_sent += 2*16;
1050#endif
1051 }
1052#ifdef DEBUG_ZLIB
1053 s->bits_sent += (ulg)len<<3;
1054#endif
1055
1056 memcpy(&s->pending_buf[s->pending], buf, len);
1057 s->pending += len;
1058}
1059
1060