trees.c (206905) | trees.c (230837) |
---|---|
1/* trees.c -- output deflated data using Huffman coding | 1/* trees.c -- output deflated data using Huffman coding |
2 * Copyright (C) 1995-2010 Jean-loup Gailly | 2 * Copyright (C) 1995-2012 Jean-loup Gailly |
3 * detect_data_type() function provided freely by Cosmin Truta, 2006 4 * For conditions of distribution and use, see copyright notice in zlib.h 5 */ 6 7/* 8 * ALGORITHM 9 * 10 * The "deflation" process uses several Huffman trees. The more --- 58 unchanged lines hidden (view full) --- 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/* The lengths of the bit length codes are sent in order of decreasing 74 * probability, to avoid transmitting the lengths for unused bit length codes. 75 */ 76 | 3 * detect_data_type() function provided freely by Cosmin Truta, 2006 4 * For conditions of distribution and use, see copyright notice in zlib.h 5 */ 6 7/* 8 * ALGORITHM 9 * 10 * The "deflation" process uses several Huffman trees. The more --- 58 unchanged lines hidden (view full) --- 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/* The lengths of the bit length codes are sent in order of decreasing 74 * probability, to avoid transmitting the lengths for unused bit length codes. 75 */ 76 |
77#define Buf_size (8 * 2*sizeof(char)) 78/* Number of bits used within bi_buf. (bi_buf might be implemented on 79 * more than 16 bits on some systems.) 80 */ 81 | |
82/* =========================================================================== 83 * Local data. These are initialized only once. 84 */ 85 86#define DIST_CODE_LEN 512 /* see definition of array dist_code below */ 87 88#if defined(GEN_TREES_H) || !defined(STDC) 89/* non ANSI compilers may not accept trees.h */ --- 304 unchanged lines hidden (view full) --- 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; | 77/* =========================================================================== 78 * Local data. These are initialized only once. 79 */ 80 81#define DIST_CODE_LEN 512 /* see definition of array dist_code below */ 82 83#if defined(GEN_TREES_H) || !defined(STDC) 84/* non ANSI compilers may not accept trees.h */ --- 304 unchanged lines hidden (view full) --- 389 s->d_desc.dyn_tree = s->dyn_dtree; 390 s->d_desc.stat_desc = &static_d_desc; 391 392 s->bl_desc.dyn_tree = s->bl_tree; 393 s->bl_desc.stat_desc = &static_bl_desc; 394 395 s->bi_buf = 0; 396 s->bi_valid = 0; |
402 s->last_eob_len = 8; /* enough lookahead for inflate */ | |
403#ifdef DEBUG 404 s->compressed_len = 0L; 405 s->bits_sent = 0L; 406#endif 407 408 /* Initialize the first block of the first file: */ 409 init_block(s); 410} --- 467 unchanged lines hidden (view full) --- 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); /* with header */ 883} 884 885/* =========================================================================== | 397#ifdef DEBUG 398 s->compressed_len = 0L; 399 s->bits_sent = 0L; 400#endif 401 402 /* Initialize the first block of the first file: */ 403 init_block(s); 404} --- 467 unchanged lines hidden (view full) --- 872#ifdef DEBUG 873 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; 874 s->compressed_len += (stored_len + 4) << 3; 875#endif 876 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ 877} 878 879/* =========================================================================== |
880 * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) 881 */ 882void ZLIB_INTERNAL _tr_flush_bits(s) 883 deflate_state *s; 884{ 885 bi_flush(s); 886} 887 888/* =========================================================================== |
|
886 * Send one empty static block to give enough lookahead for inflate. 887 * This takes 10 bits, of which 7 may remain in the bit buffer. | 889 * Send one empty static block to give enough lookahead for inflate. 890 * This takes 10 bits, of which 7 may remain in the bit buffer. |
888 * The current inflate code requires 9 bits of lookahead. If the 889 * last two codes for the previous block (real code plus EOB) were coded 890 * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode 891 * the last real code. In this case we send two empty static blocks instead 892 * of one. (There are no problems if the previous block is stored or fixed.) 893 * To simplify the code, we assume the worst case of last real code encoded 894 * on one bit only. | |
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; /* 3 for block type, 7 for EOB */ 903#endif 904 bi_flush(s); | 891 */ 892void ZLIB_INTERNAL _tr_align(s) 893 deflate_state *s; 894{ 895 send_bits(s, STATIC_TREES<<1, 3); 896 send_code(s, END_BLOCK, static_ltree); 897#ifdef DEBUG 898 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 899#endif 900 bi_flush(s); |
905 /* Of the 10 bits for the empty block, we have already sent 906 * (10 - bi_valid) bits. The lookahead for the last real code (before 907 * the EOB of the previous block) was thus at least one plus the length 908 * of the EOB plus what we have just sent of the empty static block. 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 * Determine the best encoding for the current block: dynamic trees, static 923 * trees or store, and output the encoded block to the zip file. 924 */ 925void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) 926 deflate_state *s; --- 186 unchanged lines hidden (view full) --- 1113 1114 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ 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); | 901} 902 903/* =========================================================================== 904 * Determine the best encoding for the current block: dynamic trees, static 905 * trees or store, and output the encoded block to the zip file. 906 */ 907void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) 908 deflate_state *s; --- 186 unchanged lines hidden (view full) --- 1095 1096 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ 1097 Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, 1098 "pendingBuf overflow"); 1099 1100 } while (lx < s->last_lit); 1101 1102 send_code(s, END_BLOCK, ltree); |
1121 s->last_eob_len = ltree[END_BLOCK].Len; | |
1122} 1123 1124/* =========================================================================== 1125 * Check if the data type is TEXT or BINARY, using the following algorithm: 1126 * - TEXT if the two conditions below are satisfied: 1127 * a) There are no non-portable control characters belonging to the 1128 * "black list" (0..6, 14..25, 28..31). 1129 * b) There is at least one printable character belonging to the --- 91 unchanged lines hidden (view full) --- 1221 */ 1222local void copy_block(s, buf, len, header) 1223 deflate_state *s; 1224 charf *buf; /* the input data */ 1225 unsigned len; /* its length */ 1226 int header; /* true if block header must be written */ 1227{ 1228 bi_windup(s); /* align on byte boundary */ | 1103} 1104 1105/* =========================================================================== 1106 * Check if the data type is TEXT or BINARY, using the following algorithm: 1107 * - TEXT if the two conditions below are satisfied: 1108 * a) There are no non-portable control characters belonging to the 1109 * "black list" (0..6, 14..25, 28..31). 1110 * b) There is at least one printable character belonging to the --- 91 unchanged lines hidden (view full) --- 1202 */ 1203local void copy_block(s, buf, len, header) 1204 deflate_state *s; 1205 charf *buf; /* the input data */ 1206 unsigned len; /* its length */ 1207 int header; /* true if block header must be written */ 1208{ 1209 bi_windup(s); /* align on byte boundary */ |
1229 s->last_eob_len = 8; /* enough lookahead for inflate */ | |
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} | 1210 1211 if (header) { 1212 put_short(s, (ush)len); 1213 put_short(s, (ush)~len); 1214#ifdef DEBUG 1215 s->bits_sent += 2*16; 1216#endif 1217 } 1218#ifdef DEBUG 1219 s->bits_sent += (ulg)len<<3; 1220#endif 1221 while (len--) { 1222 put_byte(s, *buf++); 1223 } 1224} |