]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.10/Modules/zlib/trees.c
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 2/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Modules / zlib / trees.c
CommitLineData
7eb75bcc
DM
1/* trees.c -- output deflated data using Huffman coding\r
2 * Copyright (C) 1995-2012 Jean-loup Gailly\r
3 * detect_data_type() function provided freely by Cosmin Truta, 2006\r
4 * For conditions of distribution and use, see copyright notice in zlib.h\r
5 */\r
6\r
7/*\r
8 * ALGORITHM\r
9 *\r
10 * The "deflation" process uses several Huffman trees. The more\r
11 * common source values are represented by shorter bit sequences.\r
12 *\r
13 * Each code tree is stored in a compressed form which is itself\r
14 * a Huffman encoding of the lengths of all the code strings (in\r
15 * ascending order by source values). The actual code strings are\r
16 * reconstructed from the lengths in the inflate process, as described\r
17 * in the deflate specification.\r
18 *\r
19 * REFERENCES\r
20 *\r
21 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".\r
22 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc\r
23 *\r
24 * Storer, James A.\r
25 * Data Compression: Methods and Theory, pp. 49-50.\r
26 * Computer Science Press, 1988. ISBN 0-7167-8156-5.\r
27 *\r
28 * Sedgewick, R.\r
29 * Algorithms, p290.\r
30 * Addison-Wesley, 1983. ISBN 0-201-06672-6.\r
31 */\r
32\r
33/* @(#) $Id$ */\r
34\r
35/* #define GEN_TREES_H */\r
36\r
37#include "deflate.h"\r
38\r
39#ifdef DEBUG\r
40# include <ctype.h>\r
41#endif\r
42\r
43/* ===========================================================================\r
44 * Constants\r
45 */\r
46\r
47#define MAX_BL_BITS 7\r
48/* Bit length codes must not exceed MAX_BL_BITS bits */\r
49\r
50#define END_BLOCK 256\r
51/* end of block literal code */\r
52\r
53#define REP_3_6 16\r
54/* repeat previous bit length 3-6 times (2 bits of repeat count) */\r
55\r
56#define REPZ_3_10 17\r
57/* repeat a zero length 3-10 times (3 bits of repeat count) */\r
58\r
59#define REPZ_11_138 18\r
60/* repeat a zero length 11-138 times (7 bits of repeat count) */\r
61\r
62local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */\r
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};\r
64\r
65local const int extra_dbits[D_CODES] /* extra bits for each distance code */\r
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};\r
67\r
68local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */\r
69 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};\r
70\r
71local const uch bl_order[BL_CODES]\r
72 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};\r
73/* The lengths of the bit length codes are sent in order of decreasing\r
74 * probability, to avoid transmitting the lengths for unused bit length codes.\r
75 */\r
76\r
77/* ===========================================================================\r
78 * Local data. These are initialized only once.\r
79 */\r
80\r
81#define DIST_CODE_LEN 512 /* see definition of array dist_code below */\r
82\r
83#if defined(GEN_TREES_H) || !defined(STDC)\r
84/* non ANSI compilers may not accept trees.h */\r
85\r
86local ct_data static_ltree[L_CODES+2];\r
87/* The static literal tree. Since the bit lengths are imposed, there is no\r
88 * need for the L_CODES extra codes used during heap construction. However\r
89 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init\r
90 * below).\r
91 */\r
92\r
93local ct_data static_dtree[D_CODES];\r
94/* The static distance tree. (Actually a trivial tree since all codes use\r
95 * 5 bits.)\r
96 */\r
97\r
98uch _dist_code[DIST_CODE_LEN];\r
99/* Distance codes. The first 256 values correspond to the distances\r
100 * 3 .. 258, the last 256 values correspond to the top 8 bits of\r
101 * the 15 bit distances.\r
102 */\r
103\r
104uch _length_code[MAX_MATCH-MIN_MATCH+1];\r
105/* length code for each normalized match length (0 == MIN_MATCH) */\r
106\r
107local int base_length[LENGTH_CODES];\r
108/* First normalized length for each code (0 = MIN_MATCH) */\r
109\r
110local int base_dist[D_CODES];\r
111/* First normalized distance for each code (0 = distance of 1) */\r
112\r
113#else\r
114# include "trees.h"\r
115#endif /* GEN_TREES_H */\r
116\r
117struct static_tree_desc_s {\r
118 const ct_data *static_tree; /* static tree or NULL */\r
119 const intf *extra_bits; /* extra bits for each code or NULL */\r
120 int extra_base; /* base index for extra_bits */\r
121 int elems; /* max number of elements in the tree */\r
122 int max_length; /* max bit length for the codes */\r
123};\r
124\r
125local static_tree_desc static_l_desc =\r
126{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};\r
127\r
128local static_tree_desc static_d_desc =\r
129{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};\r
130\r
131local static_tree_desc static_bl_desc =\r
132{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};\r
133\r
134/* ===========================================================================\r
135 * Local (static) routines in this file.\r
136 */\r
137\r
138local void tr_static_init OF((void));\r
139local void init_block OF((deflate_state *s));\r
140local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));\r
141local void gen_bitlen OF((deflate_state *s, tree_desc *desc));\r
142local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));\r
143local void build_tree OF((deflate_state *s, tree_desc *desc));\r
144local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));\r
145local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));\r
146local int build_bl_tree OF((deflate_state *s));\r
147local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,\r
148 int blcodes));\r
149local void compress_block OF((deflate_state *s, const ct_data *ltree,\r
150 const ct_data *dtree));\r
151local int detect_data_type OF((deflate_state *s));\r
152local unsigned bi_reverse OF((unsigned value, int length));\r
153local void bi_windup OF((deflate_state *s));\r
154local void bi_flush OF((deflate_state *s));\r
155local void copy_block OF((deflate_state *s, charf *buf, unsigned len,\r
156 int header));\r
157\r
158#ifdef GEN_TREES_H\r
159local void gen_trees_header OF((void));\r
160#endif\r
161\r
162#ifndef DEBUG\r
163# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)\r
164 /* Send a code of the given tree. c and tree must not have side effects */\r
165\r
166#else /* DEBUG */\r
167# define send_code(s, c, tree) \\r
168 { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \\r
169 send_bits(s, tree[c].Code, tree[c].Len); }\r
170#endif\r
171\r
172/* ===========================================================================\r
173 * Output a short LSB first on the stream.\r
174 * IN assertion: there is enough room in pendingBuf.\r
175 */\r
176#define put_short(s, w) { \\r
177 put_byte(s, (uch)((w) & 0xff)); \\r
178 put_byte(s, (uch)((ush)(w) >> 8)); \\r
179}\r
180\r
181/* ===========================================================================\r
182 * Send a value on a given number of bits.\r
183 * IN assertion: length <= 16 and value fits in length bits.\r
184 */\r
185#ifdef DEBUG\r
186local void send_bits OF((deflate_state *s, int value, int length));\r
187\r
188local void send_bits(s, value, length)\r
189 deflate_state *s;\r
190 int value; /* value to send */\r
191 int length; /* number of bits */\r
192{\r
193 Tracevv((stderr," l %2d v %4x ", length, value));\r
194 Assert(length > 0 && length <= 15, "invalid length");\r
195 s->bits_sent += (ulg)length;\r
196\r
197 /* If not enough room in bi_buf, use (valid) bits from bi_buf and\r
198 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))\r
199 * unused bits in value.\r
200 */\r
201 if (s->bi_valid > (int)Buf_size - length) {\r
202 s->bi_buf |= (ush)value << s->bi_valid;\r
203 put_short(s, s->bi_buf);\r
204 s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);\r
205 s->bi_valid += length - Buf_size;\r
206 } else {\r
207 s->bi_buf |= (ush)value << s->bi_valid;\r
208 s->bi_valid += length;\r
209 }\r
210}\r
211#else /* !DEBUG */\r
212\r
213#define send_bits(s, value, length) \\r
214{ int len = length;\\r
215 if (s->bi_valid > (int)Buf_size - len) {\\r
216 int val = value;\\r
217 s->bi_buf |= (ush)val << s->bi_valid;\\r
218 put_short(s, s->bi_buf);\\r
219 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\\r
220 s->bi_valid += len - Buf_size;\\r
221 } else {\\r
222 s->bi_buf |= (ush)(value) << s->bi_valid;\\r
223 s->bi_valid += len;\\r
224 }\\r
225}\r
226#endif /* DEBUG */\r
227\r
228\r
229/* the arguments must not have side effects */\r
230\r
231/* ===========================================================================\r
232 * Initialize the various 'constant' tables.\r
233 */\r
234local void tr_static_init()\r
235{\r
236#if defined(GEN_TREES_H) || !defined(STDC)\r
237 static int static_init_done = 0;\r
238 int n; /* iterates over tree elements */\r
239 int bits; /* bit counter */\r
240 int length; /* length value */\r
241 int code; /* code value */\r
242 int dist; /* distance index */\r
243 ush bl_count[MAX_BITS+1];\r
244 /* number of codes at each bit length for an optimal tree */\r
245\r
246 if (static_init_done) return;\r
247\r
248 /* For some embedded targets, global variables are not initialized: */\r
249#ifdef NO_INIT_GLOBAL_POINTERS\r
250 static_l_desc.static_tree = static_ltree;\r
251 static_l_desc.extra_bits = extra_lbits;\r
252 static_d_desc.static_tree = static_dtree;\r
253 static_d_desc.extra_bits = extra_dbits;\r
254 static_bl_desc.extra_bits = extra_blbits;\r
255#endif\r
256\r
257 /* Initialize the mapping length (0..255) -> length code (0..28) */\r
258 length = 0;\r
259 for (code = 0; code < LENGTH_CODES-1; code++) {\r
260 base_length[code] = length;\r
261 for (n = 0; n < (1<<extra_lbits[code]); n++) {\r
262 _length_code[length++] = (uch)code;\r
263 }\r
264 }\r
265 Assert (length == 256, "tr_static_init: length != 256");\r
266 /* Note that the length 255 (match length 258) can be represented\r
267 * in two different ways: code 284 + 5 bits or code 285, so we\r
268 * overwrite length_code[255] to use the best encoding:\r
269 */\r
270 _length_code[length-1] = (uch)code;\r
271\r
272 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */\r
273 dist = 0;\r
274 for (code = 0 ; code < 16; code++) {\r
275 base_dist[code] = dist;\r
276 for (n = 0; n < (1<<extra_dbits[code]); n++) {\r
277 _dist_code[dist++] = (uch)code;\r
278 }\r
279 }\r
280 Assert (dist == 256, "tr_static_init: dist != 256");\r
281 dist >>= 7; /* from now on, all distances are divided by 128 */\r
282 for ( ; code < D_CODES; code++) {\r
283 base_dist[code] = dist << 7;\r
284 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {\r
285 _dist_code[256 + dist++] = (uch)code;\r
286 }\r
287 }\r
288 Assert (dist == 256, "tr_static_init: 256+dist != 512");\r
289\r
290 /* Construct the codes of the static literal tree */\r
291 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;\r
292 n = 0;\r
293 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;\r
294 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;\r
295 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;\r
296 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;\r
297 /* Codes 286 and 287 do not exist, but we must include them in the\r
298 * tree construction to get a canonical Huffman tree (longest code\r
299 * all ones)\r
300 */\r
301 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);\r
302\r
303 /* The static distance tree is trivial: */\r
304 for (n = 0; n < D_CODES; n++) {\r
305 static_dtree[n].Len = 5;\r
306 static_dtree[n].Code = bi_reverse((unsigned)n, 5);\r
307 }\r
308 static_init_done = 1;\r
309\r
310# ifdef GEN_TREES_H\r
311 gen_trees_header();\r
312# endif\r
313#endif /* defined(GEN_TREES_H) || !defined(STDC) */\r
314}\r
315\r
316/* ===========================================================================\r
317 * Genererate the file trees.h describing the static trees.\r
318 */\r
319#ifdef GEN_TREES_H\r
320# ifndef DEBUG\r
321# include <stdio.h>\r
322# endif\r
323\r
324# define SEPARATOR(i, last, width) \\r
325 ((i) == (last)? "\n};\n\n" : \\r
326 ((i) % (width) == (width)-1 ? ",\n" : ", "))\r
327\r
328void gen_trees_header()\r
329{\r
330 FILE *header = fopen("trees.h", "w");\r
331 int i;\r
332\r
333 Assert (header != NULL, "Can't open trees.h");\r
334 fprintf(header,\r
335 "/* header created automatically with -DGEN_TREES_H */\n\n");\r
336\r
337 fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");\r
338 for (i = 0; i < L_CODES+2; i++) {\r
339 fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,\r
340 static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));\r
341 }\r
342\r
343 fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");\r
344 for (i = 0; i < D_CODES; i++) {\r
345 fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,\r
346 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));\r
347 }\r
348\r
349 fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");\r
350 for (i = 0; i < DIST_CODE_LEN; i++) {\r
351 fprintf(header, "%2u%s", _dist_code[i],\r
352 SEPARATOR(i, DIST_CODE_LEN-1, 20));\r
353 }\r
354\r
355 fprintf(header,\r
356 "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");\r
357 for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {\r
358 fprintf(header, "%2u%s", _length_code[i],\r
359 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));\r
360 }\r
361\r
362 fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");\r
363 for (i = 0; i < LENGTH_CODES; i++) {\r
364 fprintf(header, "%1u%s", base_length[i],\r
365 SEPARATOR(i, LENGTH_CODES-1, 20));\r
366 }\r
367\r
368 fprintf(header, "local const int base_dist[D_CODES] = {\n");\r
369 for (i = 0; i < D_CODES; i++) {\r
370 fprintf(header, "%5u%s", base_dist[i],\r
371 SEPARATOR(i, D_CODES-1, 10));\r
372 }\r
373\r
374 fclose(header);\r
375}\r
376#endif /* GEN_TREES_H */\r
377\r
378/* ===========================================================================\r
379 * Initialize the tree data structures for a new zlib stream.\r
380 */\r
381void ZLIB_INTERNAL _tr_init(s)\r
382 deflate_state *s;\r
383{\r
384 tr_static_init();\r
385\r
386 s->l_desc.dyn_tree = s->dyn_ltree;\r
387 s->l_desc.stat_desc = &static_l_desc;\r
388\r
389 s->d_desc.dyn_tree = s->dyn_dtree;\r
390 s->d_desc.stat_desc = &static_d_desc;\r
391\r
392 s->bl_desc.dyn_tree = s->bl_tree;\r
393 s->bl_desc.stat_desc = &static_bl_desc;\r
394\r
395 s->bi_buf = 0;\r
396 s->bi_valid = 0;\r
397#ifdef DEBUG\r
398 s->compressed_len = 0L;\r
399 s->bits_sent = 0L;\r
400#endif\r
401\r
402 /* Initialize the first block of the first file: */\r
403 init_block(s);\r
404}\r
405\r
406/* ===========================================================================\r
407 * Initialize a new block.\r
408 */\r
409local void init_block(s)\r
410 deflate_state *s;\r
411{\r
412 int n; /* iterates over tree elements */\r
413\r
414 /* Initialize the trees. */\r
415 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;\r
416 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;\r
417 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;\r
418\r
419 s->dyn_ltree[END_BLOCK].Freq = 1;\r
420 s->opt_len = s->static_len = 0L;\r
421 s->last_lit = s->matches = 0;\r
422}\r
423\r
424#define SMALLEST 1\r
425/* Index within the heap array of least frequent node in the Huffman tree */\r
426\r
427\r
428/* ===========================================================================\r
429 * Remove the smallest element from the heap and recreate the heap with\r
430 * one less element. Updates heap and heap_len.\r
431 */\r
432#define pqremove(s, tree, top) \\r
433{\\r
434 top = s->heap[SMALLEST]; \\r
435 s->heap[SMALLEST] = s->heap[s->heap_len--]; \\r
436 pqdownheap(s, tree, SMALLEST); \\r
437}\r
438\r
439/* ===========================================================================\r
440 * Compares to subtrees, using the tree depth as tie breaker when\r
441 * the subtrees have equal frequency. This minimizes the worst case length.\r
442 */\r
443#define smaller(tree, n, m, depth) \\r
444 (tree[n].Freq < tree[m].Freq || \\r
445 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))\r
446\r
447/* ===========================================================================\r
448 * Restore the heap property by moving down the tree starting at node k,\r
449 * exchanging a node with the smallest of its two sons if necessary, stopping\r
450 * when the heap property is re-established (each father smaller than its\r
451 * two sons).\r
452 */\r
453local void pqdownheap(s, tree, k)\r
454 deflate_state *s;\r
455 ct_data *tree; /* the tree to restore */\r
456 int k; /* node to move down */\r
457{\r
458 int v = s->heap[k];\r
459 int j = k << 1; /* left son of k */\r
460 while (j <= s->heap_len) {\r
461 /* Set j to the smallest of the two sons: */\r
462 if (j < s->heap_len &&\r
463 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {\r
464 j++;\r
465 }\r
466 /* Exit if v is smaller than both sons */\r
467 if (smaller(tree, v, s->heap[j], s->depth)) break;\r
468\r
469 /* Exchange v with the smallest son */\r
470 s->heap[k] = s->heap[j]; k = j;\r
471\r
472 /* And continue down the tree, setting j to the left son of k */\r
473 j <<= 1;\r
474 }\r
475 s->heap[k] = v;\r
476}\r
477\r
478/* ===========================================================================\r
479 * Compute the optimal bit lengths for a tree and update the total bit length\r
480 * for the current block.\r
481 * IN assertion: the fields freq and dad are set, heap[heap_max] and\r
482 * above are the tree nodes sorted by increasing frequency.\r
483 * OUT assertions: the field len is set to the optimal bit length, the\r
484 * array bl_count contains the frequencies for each bit length.\r
485 * The length opt_len is updated; static_len is also updated if stree is\r
486 * not null.\r
487 */\r
488local void gen_bitlen(s, desc)\r
489 deflate_state *s;\r
490 tree_desc *desc; /* the tree descriptor */\r
491{\r
492 ct_data *tree = desc->dyn_tree;\r
493 int max_code = desc->max_code;\r
494 const ct_data *stree = desc->stat_desc->static_tree;\r
495 const intf *extra = desc->stat_desc->extra_bits;\r
496 int base = desc->stat_desc->extra_base;\r
497 int max_length = desc->stat_desc->max_length;\r
498 int h; /* heap index */\r
499 int n, m; /* iterate over the tree elements */\r
500 int bits; /* bit length */\r
501 int xbits; /* extra bits */\r
502 ush f; /* frequency */\r
503 int overflow = 0; /* number of elements with bit length too large */\r
504\r
505 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;\r
506\r
507 /* In a first pass, compute the optimal bit lengths (which may\r
508 * overflow in the case of the bit length tree).\r
509 */\r
510 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */\r
511\r
512 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {\r
513 n = s->heap[h];\r
514 bits = tree[tree[n].Dad].Len + 1;\r
515 if (bits > max_length) bits = max_length, overflow++;\r
516 tree[n].Len = (ush)bits;\r
517 /* We overwrite tree[n].Dad which is no longer needed */\r
518\r
519 if (n > max_code) continue; /* not a leaf node */\r
520\r
521 s->bl_count[bits]++;\r
522 xbits = 0;\r
523 if (n >= base) xbits = extra[n-base];\r
524 f = tree[n].Freq;\r
525 s->opt_len += (ulg)f * (bits + xbits);\r
526 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);\r
527 }\r
528 if (overflow == 0) return;\r
529\r
530 Trace((stderr,"\nbit length overflow\n"));\r
531 /* This happens for example on obj2 and pic of the Calgary corpus */\r
532\r
533 /* Find the first bit length which could increase: */\r
534 do {\r
535 bits = max_length-1;\r
536 while (s->bl_count[bits] == 0) bits--;\r
537 s->bl_count[bits]--; /* move one leaf down the tree */\r
538 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */\r
539 s->bl_count[max_length]--;\r
540 /* The brother of the overflow item also moves one step up,\r
541 * but this does not affect bl_count[max_length]\r
542 */\r
543 overflow -= 2;\r
544 } while (overflow > 0);\r
545\r
546 /* Now recompute all bit lengths, scanning in increasing frequency.\r
547 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all\r
548 * lengths instead of fixing only the wrong ones. This idea is taken\r
549 * from 'ar' written by Haruhiko Okumura.)\r
550 */\r
551 for (bits = max_length; bits != 0; bits--) {\r
552 n = s->bl_count[bits];\r
553 while (n != 0) {\r
554 m = s->heap[--h];\r
555 if (m > max_code) continue;\r
556 if ((unsigned) tree[m].Len != (unsigned) bits) {\r
557 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));\r
558 s->opt_len += ((long)bits - (long)tree[m].Len)\r
559 *(long)tree[m].Freq;\r
560 tree[m].Len = (ush)bits;\r
561 }\r
562 n--;\r
563 }\r
564 }\r
565}\r
566\r
567/* ===========================================================================\r
568 * Generate the codes for a given tree and bit counts (which need not be\r
569 * optimal).\r
570 * IN assertion: the array bl_count contains the bit length statistics for\r
571 * the given tree and the field len is set for all tree elements.\r
572 * OUT assertion: the field code is set for all tree elements of non\r
573 * zero code length.\r
574 */\r
575local void gen_codes (tree, max_code, bl_count)\r
576 ct_data *tree; /* the tree to decorate */\r
577 int max_code; /* largest code with non zero frequency */\r
578 ushf *bl_count; /* number of codes at each bit length */\r
579{\r
580 ush next_code[MAX_BITS+1]; /* next code value for each bit length */\r
581 ush code = 0; /* running code value */\r
582 int bits; /* bit index */\r
583 int n; /* code index */\r
584\r
585 /* The distribution counts are first used to generate the code values\r
586 * without bit reversal.\r
587 */\r
588 for (bits = 1; bits <= MAX_BITS; bits++) {\r
589 next_code[bits] = code = (code + bl_count[bits-1]) << 1;\r
590 }\r
591 /* Check that the bit counts in bl_count are consistent. The last code\r
592 * must be all ones.\r
593 */\r
594 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,\r
595 "inconsistent bit counts");\r
596 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));\r
597\r
598 for (n = 0; n <= max_code; n++) {\r
599 int len = tree[n].Len;\r
600 if (len == 0) continue;\r
601 /* Now reverse the bits */\r
602 tree[n].Code = bi_reverse(next_code[len]++, len);\r
603\r
604 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",\r
605 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));\r
606 }\r
607}\r
608\r
609/* ===========================================================================\r
610 * Construct one Huffman tree and assigns the code bit strings and lengths.\r
611 * Update the total bit length for the current block.\r
612 * IN assertion: the field freq is set for all tree elements.\r
613 * OUT assertions: the fields len and code are set to the optimal bit length\r
614 * and corresponding code. The length opt_len is updated; static_len is\r
615 * also updated if stree is not null. The field max_code is set.\r
616 */\r
617local void build_tree(s, desc)\r
618 deflate_state *s;\r
619 tree_desc *desc; /* the tree descriptor */\r
620{\r
621 ct_data *tree = desc->dyn_tree;\r
622 const ct_data *stree = desc->stat_desc->static_tree;\r
623 int elems = desc->stat_desc->elems;\r
624 int n, m; /* iterate over heap elements */\r
625 int max_code = -1; /* largest code with non zero frequency */\r
626 int node; /* new node being created */\r
627\r
628 /* Construct the initial heap, with least frequent element in\r
629 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].\r
630 * heap[0] is not used.\r
631 */\r
632 s->heap_len = 0, s->heap_max = HEAP_SIZE;\r
633\r
634 for (n = 0; n < elems; n++) {\r
635 if (tree[n].Freq != 0) {\r
636 s->heap[++(s->heap_len)] = max_code = n;\r
637 s->depth[n] = 0;\r
638 } else {\r
639 tree[n].Len = 0;\r
640 }\r
641 }\r
642\r
643 /* The pkzip format requires that at least one distance code exists,\r
644 * and that at least one bit should be sent even if there is only one\r
645 * possible code. So to avoid special checks later on we force at least\r
646 * two codes of non zero frequency.\r
647 */\r
648 while (s->heap_len < 2) {\r
649 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);\r
650 tree[node].Freq = 1;\r
651 s->depth[node] = 0;\r
652 s->opt_len--; if (stree) s->static_len -= stree[node].Len;\r
653 /* node is 0 or 1 so it does not have extra bits */\r
654 }\r
655 desc->max_code = max_code;\r
656\r
657 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,\r
658 * establish sub-heaps of increasing lengths:\r
659 */\r
660 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);\r
661\r
662 /* Construct the Huffman tree by repeatedly combining the least two\r
663 * frequent nodes.\r
664 */\r
665 node = elems; /* next internal node of the tree */\r
666 do {\r
667 pqremove(s, tree, n); /* n = node of least frequency */\r
668 m = s->heap[SMALLEST]; /* m = node of next least frequency */\r
669\r
670 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */\r
671 s->heap[--(s->heap_max)] = m;\r
672\r
673 /* Create a new node father of n and m */\r
674 tree[node].Freq = tree[n].Freq + tree[m].Freq;\r
675 s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?\r
676 s->depth[n] : s->depth[m]) + 1);\r
677 tree[n].Dad = tree[m].Dad = (ush)node;\r
678#ifdef DUMP_BL_TREE\r
679 if (tree == s->bl_tree) {\r
680 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",\r
681 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);\r
682 }\r
683#endif\r
684 /* and insert the new node in the heap */\r
685 s->heap[SMALLEST] = node++;\r
686 pqdownheap(s, tree, SMALLEST);\r
687\r
688 } while (s->heap_len >= 2);\r
689\r
690 s->heap[--(s->heap_max)] = s->heap[SMALLEST];\r
691\r
692 /* At this point, the fields freq and dad are set. We can now\r
693 * generate the bit lengths.\r
694 */\r
695 gen_bitlen(s, (tree_desc *)desc);\r
696\r
697 /* The field len is now set, we can generate the bit codes */\r
698 gen_codes ((ct_data *)tree, max_code, s->bl_count);\r
699}\r
700\r
701/* ===========================================================================\r
702 * Scan a literal or distance tree to determine the frequencies of the codes\r
703 * in the bit length tree.\r
704 */\r
705local void scan_tree (s, tree, max_code)\r
706 deflate_state *s;\r
707 ct_data *tree; /* the tree to be scanned */\r
708 int max_code; /* and its largest code of non zero frequency */\r
709{\r
710 int n; /* iterates over all tree elements */\r
711 int prevlen = -1; /* last emitted length */\r
712 int curlen; /* length of current code */\r
713 int nextlen = tree[0].Len; /* length of next code */\r
714 int count = 0; /* repeat count of the current code */\r
715 int max_count = 7; /* max repeat count */\r
716 int min_count = 4; /* min repeat count */\r
717\r
718 if (nextlen == 0) max_count = 138, min_count = 3;\r
719 tree[max_code+1].Len = (ush)0xffff; /* guard */\r
720\r
721 for (n = 0; n <= max_code; n++) {\r
722 curlen = nextlen; nextlen = tree[n+1].Len;\r
723 if (++count < max_count && curlen == nextlen) {\r
724 continue;\r
725 } else if (count < min_count) {\r
726 s->bl_tree[curlen].Freq += count;\r
727 } else if (curlen != 0) {\r
728 if (curlen != prevlen) s->bl_tree[curlen].Freq++;\r
729 s->bl_tree[REP_3_6].Freq++;\r
730 } else if (count <= 10) {\r
731 s->bl_tree[REPZ_3_10].Freq++;\r
732 } else {\r
733 s->bl_tree[REPZ_11_138].Freq++;\r
734 }\r
735 count = 0; prevlen = curlen;\r
736 if (nextlen == 0) {\r
737 max_count = 138, min_count = 3;\r
738 } else if (curlen == nextlen) {\r
739 max_count = 6, min_count = 3;\r
740 } else {\r
741 max_count = 7, min_count = 4;\r
742 }\r
743 }\r
744}\r
745\r
746/* ===========================================================================\r
747 * Send a literal or distance tree in compressed form, using the codes in\r
748 * bl_tree.\r
749 */\r
750local void send_tree (s, tree, max_code)\r
751 deflate_state *s;\r
752 ct_data *tree; /* the tree to be scanned */\r
753 int max_code; /* and its largest code of non zero frequency */\r
754{\r
755 int n; /* iterates over all tree elements */\r
756 int prevlen = -1; /* last emitted length */\r
757 int curlen; /* length of current code */\r
758 int nextlen = tree[0].Len; /* length of next code */\r
759 int count = 0; /* repeat count of the current code */\r
760 int max_count = 7; /* max repeat count */\r
761 int min_count = 4; /* min repeat count */\r
762\r
763 /* tree[max_code+1].Len = -1; */ /* guard already set */\r
764 if (nextlen == 0) max_count = 138, min_count = 3;\r
765\r
766 for (n = 0; n <= max_code; n++) {\r
767 curlen = nextlen; nextlen = tree[n+1].Len;\r
768 if (++count < max_count && curlen == nextlen) {\r
769 continue;\r
770 } else if (count < min_count) {\r
771 do { send_code(s, curlen, s->bl_tree); } while (--count != 0);\r
772\r
773 } else if (curlen != 0) {\r
774 if (curlen != prevlen) {\r
775 send_code(s, curlen, s->bl_tree); count--;\r
776 }\r
777 Assert(count >= 3 && count <= 6, " 3_6?");\r
778 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);\r
779\r
780 } else if (count <= 10) {\r
781 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);\r
782\r
783 } else {\r
784 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);\r
785 }\r
786 count = 0; prevlen = curlen;\r
787 if (nextlen == 0) {\r
788 max_count = 138, min_count = 3;\r
789 } else if (curlen == nextlen) {\r
790 max_count = 6, min_count = 3;\r
791 } else {\r
792 max_count = 7, min_count = 4;\r
793 }\r
794 }\r
795}\r
796\r
797/* ===========================================================================\r
798 * Construct the Huffman tree for the bit lengths and return the index in\r
799 * bl_order of the last bit length code to send.\r
800 */\r
801local int build_bl_tree(s)\r
802 deflate_state *s;\r
803{\r
804 int max_blindex; /* index of last bit length code of non zero freq */\r
805\r
806 /* Determine the bit length frequencies for literal and distance trees */\r
807 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);\r
808 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);\r
809\r
810 /* Build the bit length tree: */\r
811 build_tree(s, (tree_desc *)(&(s->bl_desc)));\r
812 /* opt_len now includes the length of the tree representations, except\r
813 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.\r
814 */\r
815\r
816 /* Determine the number of bit length codes to send. The pkzip format\r
817 * requires that at least 4 bit length codes be sent. (appnote.txt says\r
818 * 3 but the actual value used is 4.)\r
819 */\r
820 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {\r
821 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;\r
822 }\r
823 /* Update opt_len to include the bit length tree and counts */\r
824 s->opt_len += 3*(max_blindex+1) + 5+5+4;\r
825 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",\r
826 s->opt_len, s->static_len));\r
827\r
828 return max_blindex;\r
829}\r
830\r
831/* ===========================================================================\r
832 * Send the header for a block using dynamic Huffman trees: the counts, the\r
833 * lengths of the bit length codes, the literal tree and the distance tree.\r
834 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.\r
835 */\r
836local void send_all_trees(s, lcodes, dcodes, blcodes)\r
837 deflate_state *s;\r
838 int lcodes, dcodes, blcodes; /* number of codes for each tree */\r
839{\r
840 int rank; /* index in bl_order */\r
841\r
842 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");\r
843 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,\r
844 "too many codes");\r
845 Tracev((stderr, "\nbl counts: "));\r
846 send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */\r
847 send_bits(s, dcodes-1, 5);\r
848 send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */\r
849 for (rank = 0; rank < blcodes; rank++) {\r
850 Tracev((stderr, "\nbl code %2d ", bl_order[rank]));\r
851 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);\r
852 }\r
853 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));\r
854\r
855 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */\r
856 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));\r
857\r
858 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */\r
859 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));\r
860}\r
861\r
862/* ===========================================================================\r
863 * Send a stored block\r
864 */\r
865void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)\r
866 deflate_state *s;\r
867 charf *buf; /* input block */\r
868 ulg stored_len; /* length of input block */\r
869 int last; /* one if this is the last block for a file */\r
870{\r
871 send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */\r
872#ifdef DEBUG\r
873 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;\r
874 s->compressed_len += (stored_len + 4) << 3;\r
875#endif\r
876 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */\r
877}\r
878\r
879/* ===========================================================================\r
880 * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)\r
881 */\r
882void ZLIB_INTERNAL _tr_flush_bits(s)\r
883 deflate_state *s;\r
884{\r
885 bi_flush(s);\r
886}\r
887\r
888/* ===========================================================================\r
889 * Send one empty static block to give enough lookahead for inflate.\r
890 * This takes 10 bits, of which 7 may remain in the bit buffer.\r
891 */\r
892void ZLIB_INTERNAL _tr_align(s)\r
893 deflate_state *s;\r
894{\r
895 send_bits(s, STATIC_TREES<<1, 3);\r
896 send_code(s, END_BLOCK, static_ltree);\r
897#ifdef DEBUG\r
898 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */\r
899#endif\r
900 bi_flush(s);\r
901}\r
902\r
903/* ===========================================================================\r
904 * Determine the best encoding for the current block: dynamic trees, static\r
905 * trees or store, and output the encoded block to the zip file.\r
906 */\r
907void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)\r
908 deflate_state *s;\r
909 charf *buf; /* input block, or NULL if too old */\r
910 ulg stored_len; /* length of input block */\r
911 int last; /* one if this is the last block for a file */\r
912{\r
913 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */\r
914 int max_blindex = 0; /* index of last bit length code of non zero freq */\r
915\r
916 /* Build the Huffman trees unless a stored block is forced */\r
917 if (s->level > 0) {\r
918\r
919 /* Check if the file is binary or text */\r
920 if (s->strm->data_type == Z_UNKNOWN)\r
921 s->strm->data_type = detect_data_type(s);\r
922\r
923 /* Construct the literal and distance trees */\r
924 build_tree(s, (tree_desc *)(&(s->l_desc)));\r
925 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,\r
926 s->static_len));\r
927\r
928 build_tree(s, (tree_desc *)(&(s->d_desc)));\r
929 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,\r
930 s->static_len));\r
931 /* At this point, opt_len and static_len are the total bit lengths of\r
932 * the compressed block data, excluding the tree representations.\r
933 */\r
934\r
935 /* Build the bit length tree for the above two trees, and get the index\r
936 * in bl_order of the last bit length code to send.\r
937 */\r
938 max_blindex = build_bl_tree(s);\r
939\r
940 /* Determine the best encoding. Compute the block lengths in bytes. */\r
941 opt_lenb = (s->opt_len+3+7)>>3;\r
942 static_lenb = (s->static_len+3+7)>>3;\r
943\r
944 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",\r
945 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,\r
946 s->last_lit));\r
947\r
948 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;\r
949\r
950 } else {\r
951 Assert(buf != (char*)0, "lost buf");\r
952 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */\r
953 }\r
954\r
955#ifdef FORCE_STORED\r
956 if (buf != (char*)0) { /* force stored block */\r
957#else\r
958 if (stored_len+4 <= opt_lenb && buf != (char*)0) {\r
959 /* 4: two words for the lengths */\r
960#endif\r
961 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.\r
962 * Otherwise we can't have processed more than WSIZE input bytes since\r
963 * the last block flush, because compression would have been\r
964 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to\r
965 * transform a block into a stored block.\r
966 */\r
967 _tr_stored_block(s, buf, stored_len, last);\r
968\r
969#ifdef FORCE_STATIC\r
970 } else if (static_lenb >= 0) { /* force static trees */\r
971#else\r
972 } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {\r
973#endif\r
974 send_bits(s, (STATIC_TREES<<1)+last, 3);\r
975 compress_block(s, (const ct_data *)static_ltree,\r
976 (const ct_data *)static_dtree);\r
977#ifdef DEBUG\r
978 s->compressed_len += 3 + s->static_len;\r
979#endif\r
980 } else {\r
981 send_bits(s, (DYN_TREES<<1)+last, 3);\r
982 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,\r
983 max_blindex+1);\r
984 compress_block(s, (const ct_data *)s->dyn_ltree,\r
985 (const ct_data *)s->dyn_dtree);\r
986#ifdef DEBUG\r
987 s->compressed_len += 3 + s->opt_len;\r
988#endif\r
989 }\r
990 Assert (s->compressed_len == s->bits_sent, "bad compressed size");\r
991 /* The above check is made mod 2^32, for files larger than 512 MB\r
992 * and uLong implemented on 32 bits.\r
993 */\r
994 init_block(s);\r
995\r
996 if (last) {\r
997 bi_windup(s);\r
998#ifdef DEBUG\r
999 s->compressed_len += 7; /* align on byte boundary */\r
1000#endif\r
1001 }\r
1002 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,\r
1003 s->compressed_len-7*last));\r
1004}\r
1005\r
1006/* ===========================================================================\r
1007 * Save the match info and tally the frequency counts. Return true if\r
1008 * the current block must be flushed.\r
1009 */\r
1010int ZLIB_INTERNAL _tr_tally (s, dist, lc)\r
1011 deflate_state *s;\r
1012 unsigned dist; /* distance of matched string */\r
1013 unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */\r
1014{\r
1015 s->d_buf[s->last_lit] = (ush)dist;\r
1016 s->l_buf[s->last_lit++] = (uch)lc;\r
1017 if (dist == 0) {\r
1018 /* lc is the unmatched char */\r
1019 s->dyn_ltree[lc].Freq++;\r
1020 } else {\r
1021 s->matches++;\r
1022 /* Here, lc is the match length - MIN_MATCH */\r
1023 dist--; /* dist = match distance - 1 */\r
1024 Assert((ush)dist < (ush)MAX_DIST(s) &&\r
1025 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&\r
1026 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");\r
1027\r
1028 s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;\r
1029 s->dyn_dtree[d_code(dist)].Freq++;\r
1030 }\r
1031\r
1032#ifdef TRUNCATE_BLOCK\r
1033 /* Try to guess if it is profitable to stop the current block here */\r
1034 if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {\r
1035 /* Compute an upper bound for the compressed length */\r
1036 ulg out_length = (ulg)s->last_lit*8L;\r
1037 ulg in_length = (ulg)((long)s->strstart - s->block_start);\r
1038 int dcode;\r
1039 for (dcode = 0; dcode < D_CODES; dcode++) {\r
1040 out_length += (ulg)s->dyn_dtree[dcode].Freq *\r
1041 (5L+extra_dbits[dcode]);\r
1042 }\r
1043 out_length >>= 3;\r
1044 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",\r
1045 s->last_lit, in_length, out_length,\r
1046 100L - out_length*100L/in_length));\r
1047 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;\r
1048 }\r
1049#endif\r
1050 return (s->last_lit == s->lit_bufsize-1);\r
1051 /* We avoid equality with lit_bufsize because of wraparound at 64K\r
1052 * on 16 bit machines and because stored blocks are restricted to\r
1053 * 64K-1 bytes.\r
1054 */\r
1055}\r
1056\r
1057/* ===========================================================================\r
1058 * Send the block data compressed using the given Huffman trees\r
1059 */\r
1060local void compress_block(s, ltree, dtree)\r
1061 deflate_state *s;\r
1062 const ct_data *ltree; /* literal tree */\r
1063 const ct_data *dtree; /* distance tree */\r
1064{\r
1065 unsigned dist; /* distance of matched string */\r
1066 int lc; /* match length or unmatched char (if dist == 0) */\r
1067 unsigned lx = 0; /* running index in l_buf */\r
1068 unsigned code; /* the code to send */\r
1069 int extra; /* number of extra bits to send */\r
1070\r
1071 if (s->last_lit != 0) do {\r
1072 dist = s->d_buf[lx];\r
1073 lc = s->l_buf[lx++];\r
1074 if (dist == 0) {\r
1075 send_code(s, lc, ltree); /* send a literal byte */\r
1076 Tracecv(isgraph(lc), (stderr," '%c' ", lc));\r
1077 } else {\r
1078 /* Here, lc is the match length - MIN_MATCH */\r
1079 code = _length_code[lc];\r
1080 send_code(s, code+LITERALS+1, ltree); /* send the length code */\r
1081 extra = extra_lbits[code];\r
1082 if (extra != 0) {\r
1083 lc -= base_length[code];\r
1084 send_bits(s, lc, extra); /* send the extra length bits */\r
1085 }\r
1086 dist--; /* dist is now the match distance - 1 */\r
1087 code = d_code(dist);\r
1088 Assert (code < D_CODES, "bad d_code");\r
1089\r
1090 send_code(s, code, dtree); /* send the distance code */\r
1091 extra = extra_dbits[code];\r
1092 if (extra != 0) {\r
1093 dist -= base_dist[code];\r
1094 send_bits(s, dist, extra); /* send the extra distance bits */\r
1095 }\r
1096 } /* literal or match pair ? */\r
1097\r
1098 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */\r
1099 Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,\r
1100 "pendingBuf overflow");\r
1101\r
1102 } while (lx < s->last_lit);\r
1103\r
1104 send_code(s, END_BLOCK, ltree);\r
1105}\r
1106\r
1107/* ===========================================================================\r
1108 * Check if the data type is TEXT or BINARY, using the following algorithm:\r
1109 * - TEXT if the two conditions below are satisfied:\r
1110 * a) There are no non-portable control characters belonging to the\r
1111 * "black list" (0..6, 14..25, 28..31).\r
1112 * b) There is at least one printable character belonging to the\r
1113 * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).\r
1114 * - BINARY otherwise.\r
1115 * - The following partially-portable control characters form a\r
1116 * "gray list" that is ignored in this detection algorithm:\r
1117 * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).\r
1118 * IN assertion: the fields Freq of dyn_ltree are set.\r
1119 */\r
1120local int detect_data_type(s)\r
1121 deflate_state *s;\r
1122{\r
1123 /* black_mask is the bit mask of black-listed bytes\r
1124 * set bits 0..6, 14..25, and 28..31\r
1125 * 0xf3ffc07f = binary 11110011111111111100000001111111\r
1126 */\r
1127 unsigned long black_mask = 0xf3ffc07fUL;\r
1128 int n;\r
1129\r
1130 /* Check for non-textual ("black-listed") bytes. */\r
1131 for (n = 0; n <= 31; n++, black_mask >>= 1)\r
1132 if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))\r
1133 return Z_BINARY;\r
1134\r
1135 /* Check for textual ("white-listed") bytes. */\r
1136 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0\r
1137 || s->dyn_ltree[13].Freq != 0)\r
1138 return Z_TEXT;\r
1139 for (n = 32; n < LITERALS; n++)\r
1140 if (s->dyn_ltree[n].Freq != 0)\r
1141 return Z_TEXT;\r
1142\r
1143 /* There are no "black-listed" or "white-listed" bytes:\r
1144 * this stream either is empty or has tolerated ("gray-listed") bytes only.\r
1145 */\r
1146 return Z_BINARY;\r
1147}\r
1148\r
1149/* ===========================================================================\r
1150 * Reverse the first len bits of a code, using straightforward code (a faster\r
1151 * method would use a table)\r
1152 * IN assertion: 1 <= len <= 15\r
1153 */\r
1154local unsigned bi_reverse(code, len)\r
1155 unsigned code; /* the value to invert */\r
1156 int len; /* its bit length */\r
1157{\r
1158 register unsigned res = 0;\r
1159 do {\r
1160 res |= code & 1;\r
1161 code >>= 1, res <<= 1;\r
1162 } while (--len > 0);\r
1163 return res >> 1;\r
1164}\r
1165\r
1166/* ===========================================================================\r
1167 * Flush the bit buffer, keeping at most 7 bits in it.\r
1168 */\r
1169local void bi_flush(s)\r
1170 deflate_state *s;\r
1171{\r
1172 if (s->bi_valid == 16) {\r
1173 put_short(s, s->bi_buf);\r
1174 s->bi_buf = 0;\r
1175 s->bi_valid = 0;\r
1176 } else if (s->bi_valid >= 8) {\r
1177 put_byte(s, (Byte)s->bi_buf);\r
1178 s->bi_buf >>= 8;\r
1179 s->bi_valid -= 8;\r
1180 }\r
1181}\r
1182\r
1183/* ===========================================================================\r
1184 * Flush the bit buffer and align the output on a byte boundary\r
1185 */\r
1186local void bi_windup(s)\r
1187 deflate_state *s;\r
1188{\r
1189 if (s->bi_valid > 8) {\r
1190 put_short(s, s->bi_buf);\r
1191 } else if (s->bi_valid > 0) {\r
1192 put_byte(s, (Byte)s->bi_buf);\r
1193 }\r
1194 s->bi_buf = 0;\r
1195 s->bi_valid = 0;\r
1196#ifdef DEBUG\r
1197 s->bits_sent = (s->bits_sent+7) & ~7;\r
1198#endif\r
1199}\r
1200\r
1201/* ===========================================================================\r
1202 * Copy a stored block, storing first the length and its\r
1203 * one's complement if requested.\r
1204 */\r
1205local void copy_block(s, buf, len, header)\r
1206 deflate_state *s;\r
1207 charf *buf; /* the input data */\r
1208 unsigned len; /* its length */\r
1209 int header; /* true if block header must be written */\r
1210{\r
1211 bi_windup(s); /* align on byte boundary */\r
1212\r
1213 if (header) {\r
1214 put_short(s, (ush)len);\r
1215 put_short(s, (ush)~len);\r
1216#ifdef DEBUG\r
1217 s->bits_sent += 2*16;\r
1218#endif\r
1219 }\r
1220#ifdef DEBUG\r
1221 s->bits_sent += (ulg)len<<3;\r
1222#endif\r
1223 while (len--) {\r
1224 put_byte(s, *buf++);\r
1225 }\r
1226}\r