11cb0ef41Sopenharmony_ci/* trees.c -- output deflated data using Huffman coding 21cb0ef41Sopenharmony_ci * Copyright (C) 1995-2021 Jean-loup Gailly 31cb0ef41Sopenharmony_ci * detect_data_type() function provided freely by Cosmin Truta, 2006 41cb0ef41Sopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h 51cb0ef41Sopenharmony_ci */ 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci/* 81cb0ef41Sopenharmony_ci * ALGORITHM 91cb0ef41Sopenharmony_ci * 101cb0ef41Sopenharmony_ci * The "deflation" process uses several Huffman trees. The more 111cb0ef41Sopenharmony_ci * common source values are represented by shorter bit sequences. 121cb0ef41Sopenharmony_ci * 131cb0ef41Sopenharmony_ci * Each code tree is stored in a compressed form which is itself 141cb0ef41Sopenharmony_ci * a Huffman encoding of the lengths of all the code strings (in 151cb0ef41Sopenharmony_ci * ascending order by source values). The actual code strings are 161cb0ef41Sopenharmony_ci * reconstructed from the lengths in the inflate process, as described 171cb0ef41Sopenharmony_ci * in the deflate specification. 181cb0ef41Sopenharmony_ci * 191cb0ef41Sopenharmony_ci * REFERENCES 201cb0ef41Sopenharmony_ci * 211cb0ef41Sopenharmony_ci * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". 221cb0ef41Sopenharmony_ci * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc 231cb0ef41Sopenharmony_ci * 241cb0ef41Sopenharmony_ci * Storer, James A. 251cb0ef41Sopenharmony_ci * Data Compression: Methods and Theory, pp. 49-50. 261cb0ef41Sopenharmony_ci * Computer Science Press, 1988. ISBN 0-7167-8156-5. 271cb0ef41Sopenharmony_ci * 281cb0ef41Sopenharmony_ci * Sedgewick, R. 291cb0ef41Sopenharmony_ci * Algorithms, p290. 301cb0ef41Sopenharmony_ci * Addison-Wesley, 1983. ISBN 0-201-06672-6. 311cb0ef41Sopenharmony_ci */ 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci/* @(#) $Id$ */ 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci/* #define GEN_TREES_H */ 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci#include "deflate.h" 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 401cb0ef41Sopenharmony_ci# include <ctype.h> 411cb0ef41Sopenharmony_ci#endif 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci/* =========================================================================== 441cb0ef41Sopenharmony_ci * Constants 451cb0ef41Sopenharmony_ci */ 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci#define MAX_BL_BITS 7 481cb0ef41Sopenharmony_ci/* Bit length codes must not exceed MAX_BL_BITS bits */ 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci#define END_BLOCK 256 511cb0ef41Sopenharmony_ci/* end of block literal code */ 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci#define REP_3_6 16 541cb0ef41Sopenharmony_ci/* repeat previous bit length 3-6 times (2 bits of repeat count) */ 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci#define REPZ_3_10 17 571cb0ef41Sopenharmony_ci/* repeat a zero length 3-10 times (3 bits of repeat count) */ 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci#define REPZ_11_138 18 601cb0ef41Sopenharmony_ci/* repeat a zero length 11-138 times (7 bits of repeat count) */ 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_cilocal const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */ 631cb0ef41Sopenharmony_ci = {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}; 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_cilocal const int extra_dbits[D_CODES] /* extra bits for each distance code */ 661cb0ef41Sopenharmony_ci = {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}; 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_cilocal const int extra_blbits[BL_CODES]/* extra bits for each bit length code */ 691cb0ef41Sopenharmony_ci = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_cilocal const uch bl_order[BL_CODES] 721cb0ef41Sopenharmony_ci = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; 731cb0ef41Sopenharmony_ci/* The lengths of the bit length codes are sent in order of decreasing 741cb0ef41Sopenharmony_ci * probability, to avoid transmitting the lengths for unused bit length codes. 751cb0ef41Sopenharmony_ci */ 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci/* =========================================================================== 781cb0ef41Sopenharmony_ci * Local data. These are initialized only once. 791cb0ef41Sopenharmony_ci */ 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci#define DIST_CODE_LEN 512 /* see definition of array dist_code below */ 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci#if defined(GEN_TREES_H) || !defined(STDC) 841cb0ef41Sopenharmony_ci/* non ANSI compilers may not accept trees.h */ 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_cilocal ct_data static_ltree[L_CODES+2]; 871cb0ef41Sopenharmony_ci/* The static literal tree. Since the bit lengths are imposed, there is no 881cb0ef41Sopenharmony_ci * need for the L_CODES extra codes used during heap construction. However 891cb0ef41Sopenharmony_ci * The codes 286 and 287 are needed to build a canonical tree (see _tr_init 901cb0ef41Sopenharmony_ci * below). 911cb0ef41Sopenharmony_ci */ 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_cilocal ct_data static_dtree[D_CODES]; 941cb0ef41Sopenharmony_ci/* The static distance tree. (Actually a trivial tree since all codes use 951cb0ef41Sopenharmony_ci * 5 bits.) 961cb0ef41Sopenharmony_ci */ 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciuch _dist_code[DIST_CODE_LEN]; 991cb0ef41Sopenharmony_ci/* Distance codes. The first 256 values correspond to the distances 1001cb0ef41Sopenharmony_ci * 3 .. 258, the last 256 values correspond to the top 8 bits of 1011cb0ef41Sopenharmony_ci * the 15 bit distances. 1021cb0ef41Sopenharmony_ci */ 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ciuch _length_code[MAX_MATCH-MIN_MATCH+1]; 1051cb0ef41Sopenharmony_ci/* length code for each normalized match length (0 == MIN_MATCH) */ 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_cilocal int base_length[LENGTH_CODES]; 1081cb0ef41Sopenharmony_ci/* First normalized length for each code (0 = MIN_MATCH) */ 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_cilocal int base_dist[D_CODES]; 1111cb0ef41Sopenharmony_ci/* First normalized distance for each code (0 = distance of 1) */ 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci#else 1141cb0ef41Sopenharmony_ci# include "trees.h" 1151cb0ef41Sopenharmony_ci#endif /* GEN_TREES_H */ 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_cistruct static_tree_desc_s { 1181cb0ef41Sopenharmony_ci const ct_data *static_tree; /* static tree or NULL */ 1191cb0ef41Sopenharmony_ci const intf *extra_bits; /* extra bits for each code or NULL */ 1201cb0ef41Sopenharmony_ci int extra_base; /* base index for extra_bits */ 1211cb0ef41Sopenharmony_ci int elems; /* max number of elements in the tree */ 1221cb0ef41Sopenharmony_ci int max_length; /* max bit length for the codes */ 1231cb0ef41Sopenharmony_ci}; 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci#ifdef NO_INIT_GLOBAL_POINTERS 1261cb0ef41Sopenharmony_ci# define TCONST 1271cb0ef41Sopenharmony_ci#else 1281cb0ef41Sopenharmony_ci# define TCONST const 1291cb0ef41Sopenharmony_ci#endif 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_cilocal TCONST static_tree_desc static_l_desc = 1321cb0ef41Sopenharmony_ci{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_cilocal TCONST static_tree_desc static_d_desc = 1351cb0ef41Sopenharmony_ci{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_cilocal TCONST static_tree_desc static_bl_desc = 1381cb0ef41Sopenharmony_ci{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci/* =========================================================================== 1411cb0ef41Sopenharmony_ci * Output a short LSB first on the stream. 1421cb0ef41Sopenharmony_ci * IN assertion: there is enough room in pendingBuf. 1431cb0ef41Sopenharmony_ci */ 1441cb0ef41Sopenharmony_ci#define put_short(s, w) { \ 1451cb0ef41Sopenharmony_ci put_byte(s, (uch)((w) & 0xff)); \ 1461cb0ef41Sopenharmony_ci put_byte(s, (uch)((ush)(w) >> 8)); \ 1471cb0ef41Sopenharmony_ci} 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci/* =========================================================================== 1501cb0ef41Sopenharmony_ci * Reverse the first len bits of a code, using straightforward code (a faster 1511cb0ef41Sopenharmony_ci * method would use a table) 1521cb0ef41Sopenharmony_ci * IN assertion: 1 <= len <= 15 1531cb0ef41Sopenharmony_ci */ 1541cb0ef41Sopenharmony_cilocal unsigned bi_reverse(unsigned code, int len) { 1551cb0ef41Sopenharmony_ci register unsigned res = 0; 1561cb0ef41Sopenharmony_ci do { 1571cb0ef41Sopenharmony_ci res |= code & 1; 1581cb0ef41Sopenharmony_ci code >>= 1, res <<= 1; 1591cb0ef41Sopenharmony_ci } while (--len > 0); 1601cb0ef41Sopenharmony_ci return res >> 1; 1611cb0ef41Sopenharmony_ci} 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci/* =========================================================================== 1641cb0ef41Sopenharmony_ci * Flush the bit buffer, keeping at most 7 bits in it. 1651cb0ef41Sopenharmony_ci */ 1661cb0ef41Sopenharmony_cilocal void bi_flush(deflate_state *s) { 1671cb0ef41Sopenharmony_ci if (s->bi_valid == 16) { 1681cb0ef41Sopenharmony_ci put_short(s, s->bi_buf); 1691cb0ef41Sopenharmony_ci s->bi_buf = 0; 1701cb0ef41Sopenharmony_ci s->bi_valid = 0; 1711cb0ef41Sopenharmony_ci } else if (s->bi_valid >= 8) { 1721cb0ef41Sopenharmony_ci put_byte(s, (Byte)s->bi_buf); 1731cb0ef41Sopenharmony_ci s->bi_buf >>= 8; 1741cb0ef41Sopenharmony_ci s->bi_valid -= 8; 1751cb0ef41Sopenharmony_ci } 1761cb0ef41Sopenharmony_ci} 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci/* =========================================================================== 1791cb0ef41Sopenharmony_ci * Flush the bit buffer and align the output on a byte boundary 1801cb0ef41Sopenharmony_ci */ 1811cb0ef41Sopenharmony_cilocal void bi_windup(deflate_state *s) { 1821cb0ef41Sopenharmony_ci if (s->bi_valid > 8) { 1831cb0ef41Sopenharmony_ci put_short(s, s->bi_buf); 1841cb0ef41Sopenharmony_ci } else if (s->bi_valid > 0) { 1851cb0ef41Sopenharmony_ci put_byte(s, (Byte)s->bi_buf); 1861cb0ef41Sopenharmony_ci } 1871cb0ef41Sopenharmony_ci s->bi_buf = 0; 1881cb0ef41Sopenharmony_ci s->bi_valid = 0; 1891cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 1901cb0ef41Sopenharmony_ci s->bits_sent = (s->bits_sent + 7) & ~7; 1911cb0ef41Sopenharmony_ci#endif 1921cb0ef41Sopenharmony_ci} 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci/* =========================================================================== 1951cb0ef41Sopenharmony_ci * Generate the codes for a given tree and bit counts (which need not be 1961cb0ef41Sopenharmony_ci * optimal). 1971cb0ef41Sopenharmony_ci * IN assertion: the array bl_count contains the bit length statistics for 1981cb0ef41Sopenharmony_ci * the given tree and the field len is set for all tree elements. 1991cb0ef41Sopenharmony_ci * OUT assertion: the field code is set for all tree elements of non 2001cb0ef41Sopenharmony_ci * zero code length. 2011cb0ef41Sopenharmony_ci */ 2021cb0ef41Sopenharmony_cilocal void gen_codes(ct_data *tree, int max_code, ushf *bl_count) { 2031cb0ef41Sopenharmony_ci ush next_code[MAX_BITS+1]; /* next code value for each bit length */ 2041cb0ef41Sopenharmony_ci unsigned code = 0; /* running code value */ 2051cb0ef41Sopenharmony_ci int bits; /* bit index */ 2061cb0ef41Sopenharmony_ci int n; /* code index */ 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci /* The distribution counts are first used to generate the code values 2091cb0ef41Sopenharmony_ci * without bit reversal. 2101cb0ef41Sopenharmony_ci */ 2111cb0ef41Sopenharmony_ci for (bits = 1; bits <= MAX_BITS; bits++) { 2121cb0ef41Sopenharmony_ci code = (code + bl_count[bits - 1]) << 1; 2131cb0ef41Sopenharmony_ci next_code[bits] = (ush)code; 2141cb0ef41Sopenharmony_ci } 2151cb0ef41Sopenharmony_ci /* Check that the bit counts in bl_count are consistent. The last code 2161cb0ef41Sopenharmony_ci * must be all ones. 2171cb0ef41Sopenharmony_ci */ 2181cb0ef41Sopenharmony_ci Assert (code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1, 2191cb0ef41Sopenharmony_ci "inconsistent bit counts"); 2201cb0ef41Sopenharmony_ci Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); 2211cb0ef41Sopenharmony_ci 2221cb0ef41Sopenharmony_ci for (n = 0; n <= max_code; n++) { 2231cb0ef41Sopenharmony_ci int len = tree[n].Len; 2241cb0ef41Sopenharmony_ci if (len == 0) continue; 2251cb0ef41Sopenharmony_ci /* Now reverse the bits */ 2261cb0ef41Sopenharmony_ci tree[n].Code = (ush)bi_reverse(next_code[len]++, len); 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ci Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", 2291cb0ef41Sopenharmony_ci n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); 2301cb0ef41Sopenharmony_ci } 2311cb0ef41Sopenharmony_ci} 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci#ifdef GEN_TREES_H 2341cb0ef41Sopenharmony_cilocal void gen_trees_header(void); 2351cb0ef41Sopenharmony_ci#endif 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci#ifndef ZLIB_DEBUG 2381cb0ef41Sopenharmony_ci# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) 2391cb0ef41Sopenharmony_ci /* Send a code of the given tree. c and tree must not have side effects */ 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ci#else /* !ZLIB_DEBUG */ 2421cb0ef41Sopenharmony_ci# define send_code(s, c, tree) \ 2431cb0ef41Sopenharmony_ci { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ 2441cb0ef41Sopenharmony_ci send_bits(s, tree[c].Code, tree[c].Len); } 2451cb0ef41Sopenharmony_ci#endif 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ci/* =========================================================================== 2481cb0ef41Sopenharmony_ci * Send a value on a given number of bits. 2491cb0ef41Sopenharmony_ci * IN assertion: length <= 16 and value fits in length bits. 2501cb0ef41Sopenharmony_ci */ 2511cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 2521cb0ef41Sopenharmony_cilocal void send_bits(deflate_state *s, int value, int length) { 2531cb0ef41Sopenharmony_ci Tracevv((stderr," l %2d v %4x ", length, value)); 2541cb0ef41Sopenharmony_ci Assert(length > 0 && length <= 15, "invalid length"); 2551cb0ef41Sopenharmony_ci s->bits_sent += (ulg)length; 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ci /* If not enough room in bi_buf, use (valid) bits from bi_buf and 2581cb0ef41Sopenharmony_ci * (16 - bi_valid) bits from value, leaving (width - (16 - bi_valid)) 2591cb0ef41Sopenharmony_ci * unused bits in value. 2601cb0ef41Sopenharmony_ci */ 2611cb0ef41Sopenharmony_ci if (s->bi_valid > (int)Buf_size - length) { 2621cb0ef41Sopenharmony_ci s->bi_buf |= (ush)value << s->bi_valid; 2631cb0ef41Sopenharmony_ci put_short(s, s->bi_buf); 2641cb0ef41Sopenharmony_ci s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); 2651cb0ef41Sopenharmony_ci s->bi_valid += length - Buf_size; 2661cb0ef41Sopenharmony_ci } else { 2671cb0ef41Sopenharmony_ci s->bi_buf |= (ush)value << s->bi_valid; 2681cb0ef41Sopenharmony_ci s->bi_valid += length; 2691cb0ef41Sopenharmony_ci } 2701cb0ef41Sopenharmony_ci} 2711cb0ef41Sopenharmony_ci#else /* !ZLIB_DEBUG */ 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci#define send_bits(s, value, length) \ 2741cb0ef41Sopenharmony_ci{ int len = length;\ 2751cb0ef41Sopenharmony_ci if (s->bi_valid > (int)Buf_size - len) {\ 2761cb0ef41Sopenharmony_ci int val = (int)value;\ 2771cb0ef41Sopenharmony_ci s->bi_buf |= (ush)val << s->bi_valid;\ 2781cb0ef41Sopenharmony_ci put_short(s, s->bi_buf);\ 2791cb0ef41Sopenharmony_ci s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ 2801cb0ef41Sopenharmony_ci s->bi_valid += len - Buf_size;\ 2811cb0ef41Sopenharmony_ci } else {\ 2821cb0ef41Sopenharmony_ci s->bi_buf |= (ush)(value) << s->bi_valid;\ 2831cb0ef41Sopenharmony_ci s->bi_valid += len;\ 2841cb0ef41Sopenharmony_ci }\ 2851cb0ef41Sopenharmony_ci} 2861cb0ef41Sopenharmony_ci#endif /* ZLIB_DEBUG */ 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ci/* the arguments must not have side effects */ 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci/* =========================================================================== 2921cb0ef41Sopenharmony_ci * Initialize the various 'constant' tables. 2931cb0ef41Sopenharmony_ci */ 2941cb0ef41Sopenharmony_cilocal void tr_static_init(void) { 2951cb0ef41Sopenharmony_ci#if defined(GEN_TREES_H) || !defined(STDC) 2961cb0ef41Sopenharmony_ci static int static_init_done = 0; 2971cb0ef41Sopenharmony_ci int n; /* iterates over tree elements */ 2981cb0ef41Sopenharmony_ci int bits; /* bit counter */ 2991cb0ef41Sopenharmony_ci int length; /* length value */ 3001cb0ef41Sopenharmony_ci int code; /* code value */ 3011cb0ef41Sopenharmony_ci int dist; /* distance index */ 3021cb0ef41Sopenharmony_ci ush bl_count[MAX_BITS+1]; 3031cb0ef41Sopenharmony_ci /* number of codes at each bit length for an optimal tree */ 3041cb0ef41Sopenharmony_ci 3051cb0ef41Sopenharmony_ci if (static_init_done) return; 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ci /* For some embedded targets, global variables are not initialized: */ 3081cb0ef41Sopenharmony_ci#ifdef NO_INIT_GLOBAL_POINTERS 3091cb0ef41Sopenharmony_ci static_l_desc.static_tree = static_ltree; 3101cb0ef41Sopenharmony_ci static_l_desc.extra_bits = extra_lbits; 3111cb0ef41Sopenharmony_ci static_d_desc.static_tree = static_dtree; 3121cb0ef41Sopenharmony_ci static_d_desc.extra_bits = extra_dbits; 3131cb0ef41Sopenharmony_ci static_bl_desc.extra_bits = extra_blbits; 3141cb0ef41Sopenharmony_ci#endif 3151cb0ef41Sopenharmony_ci 3161cb0ef41Sopenharmony_ci /* Initialize the mapping length (0..255) -> length code (0..28) */ 3171cb0ef41Sopenharmony_ci length = 0; 3181cb0ef41Sopenharmony_ci for (code = 0; code < LENGTH_CODES-1; code++) { 3191cb0ef41Sopenharmony_ci base_length[code] = length; 3201cb0ef41Sopenharmony_ci for (n = 0; n < (1 << extra_lbits[code]); n++) { 3211cb0ef41Sopenharmony_ci _length_code[length++] = (uch)code; 3221cb0ef41Sopenharmony_ci } 3231cb0ef41Sopenharmony_ci } 3241cb0ef41Sopenharmony_ci Assert (length == 256, "tr_static_init: length != 256"); 3251cb0ef41Sopenharmony_ci /* Note that the length 255 (match length 258) can be represented 3261cb0ef41Sopenharmony_ci * in two different ways: code 284 + 5 bits or code 285, so we 3271cb0ef41Sopenharmony_ci * overwrite length_code[255] to use the best encoding: 3281cb0ef41Sopenharmony_ci */ 3291cb0ef41Sopenharmony_ci _length_code[length - 1] = (uch)code; 3301cb0ef41Sopenharmony_ci 3311cb0ef41Sopenharmony_ci /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ 3321cb0ef41Sopenharmony_ci dist = 0; 3331cb0ef41Sopenharmony_ci for (code = 0 ; code < 16; code++) { 3341cb0ef41Sopenharmony_ci base_dist[code] = dist; 3351cb0ef41Sopenharmony_ci for (n = 0; n < (1 << extra_dbits[code]); n++) { 3361cb0ef41Sopenharmony_ci _dist_code[dist++] = (uch)code; 3371cb0ef41Sopenharmony_ci } 3381cb0ef41Sopenharmony_ci } 3391cb0ef41Sopenharmony_ci Assert (dist == 256, "tr_static_init: dist != 256"); 3401cb0ef41Sopenharmony_ci dist >>= 7; /* from now on, all distances are divided by 128 */ 3411cb0ef41Sopenharmony_ci for ( ; code < D_CODES; code++) { 3421cb0ef41Sopenharmony_ci base_dist[code] = dist << 7; 3431cb0ef41Sopenharmony_ci for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) { 3441cb0ef41Sopenharmony_ci _dist_code[256 + dist++] = (uch)code; 3451cb0ef41Sopenharmony_ci } 3461cb0ef41Sopenharmony_ci } 3471cb0ef41Sopenharmony_ci Assert (dist == 256, "tr_static_init: 256 + dist != 512"); 3481cb0ef41Sopenharmony_ci 3491cb0ef41Sopenharmony_ci /* Construct the codes of the static literal tree */ 3501cb0ef41Sopenharmony_ci for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; 3511cb0ef41Sopenharmony_ci n = 0; 3521cb0ef41Sopenharmony_ci while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; 3531cb0ef41Sopenharmony_ci while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; 3541cb0ef41Sopenharmony_ci while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; 3551cb0ef41Sopenharmony_ci while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; 3561cb0ef41Sopenharmony_ci /* Codes 286 and 287 do not exist, but we must include them in the 3571cb0ef41Sopenharmony_ci * tree construction to get a canonical Huffman tree (longest code 3581cb0ef41Sopenharmony_ci * all ones) 3591cb0ef41Sopenharmony_ci */ 3601cb0ef41Sopenharmony_ci gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); 3611cb0ef41Sopenharmony_ci 3621cb0ef41Sopenharmony_ci /* The static distance tree is trivial: */ 3631cb0ef41Sopenharmony_ci for (n = 0; n < D_CODES; n++) { 3641cb0ef41Sopenharmony_ci static_dtree[n].Len = 5; 3651cb0ef41Sopenharmony_ci static_dtree[n].Code = bi_reverse((unsigned)n, 5); 3661cb0ef41Sopenharmony_ci } 3671cb0ef41Sopenharmony_ci static_init_done = 1; 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ci# ifdef GEN_TREES_H 3701cb0ef41Sopenharmony_ci gen_trees_header(); 3711cb0ef41Sopenharmony_ci# endif 3721cb0ef41Sopenharmony_ci#endif /* defined(GEN_TREES_H) || !defined(STDC) */ 3731cb0ef41Sopenharmony_ci} 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_ci/* =========================================================================== 3761cb0ef41Sopenharmony_ci * Generate the file trees.h describing the static trees. 3771cb0ef41Sopenharmony_ci */ 3781cb0ef41Sopenharmony_ci#ifdef GEN_TREES_H 3791cb0ef41Sopenharmony_ci# ifndef ZLIB_DEBUG 3801cb0ef41Sopenharmony_ci# include <stdio.h> 3811cb0ef41Sopenharmony_ci# endif 3821cb0ef41Sopenharmony_ci 3831cb0ef41Sopenharmony_ci# define SEPARATOR(i, last, width) \ 3841cb0ef41Sopenharmony_ci ((i) == (last)? "\n};\n\n" : \ 3851cb0ef41Sopenharmony_ci ((i) % (width) == (width) - 1 ? ",\n" : ", ")) 3861cb0ef41Sopenharmony_ci 3871cb0ef41Sopenharmony_civoid gen_trees_header(void) { 3881cb0ef41Sopenharmony_ci FILE *header = fopen("trees.h", "w"); 3891cb0ef41Sopenharmony_ci int i; 3901cb0ef41Sopenharmony_ci 3911cb0ef41Sopenharmony_ci Assert (header != NULL, "Can't open trees.h"); 3921cb0ef41Sopenharmony_ci fprintf(header, 3931cb0ef41Sopenharmony_ci "/* header created automatically with -DGEN_TREES_H */\n\n"); 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_ci fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); 3961cb0ef41Sopenharmony_ci for (i = 0; i < L_CODES+2; i++) { 3971cb0ef41Sopenharmony_ci fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, 3981cb0ef41Sopenharmony_ci static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); 3991cb0ef41Sopenharmony_ci } 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ci fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); 4021cb0ef41Sopenharmony_ci for (i = 0; i < D_CODES; i++) { 4031cb0ef41Sopenharmony_ci fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, 4041cb0ef41Sopenharmony_ci static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); 4051cb0ef41Sopenharmony_ci } 4061cb0ef41Sopenharmony_ci 4071cb0ef41Sopenharmony_ci fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n"); 4081cb0ef41Sopenharmony_ci for (i = 0; i < DIST_CODE_LEN; i++) { 4091cb0ef41Sopenharmony_ci fprintf(header, "%2u%s", _dist_code[i], 4101cb0ef41Sopenharmony_ci SEPARATOR(i, DIST_CODE_LEN-1, 20)); 4111cb0ef41Sopenharmony_ci } 4121cb0ef41Sopenharmony_ci 4131cb0ef41Sopenharmony_ci fprintf(header, 4141cb0ef41Sopenharmony_ci "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); 4151cb0ef41Sopenharmony_ci for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { 4161cb0ef41Sopenharmony_ci fprintf(header, "%2u%s", _length_code[i], 4171cb0ef41Sopenharmony_ci SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); 4181cb0ef41Sopenharmony_ci } 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ci fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); 4211cb0ef41Sopenharmony_ci for (i = 0; i < LENGTH_CODES; i++) { 4221cb0ef41Sopenharmony_ci fprintf(header, "%1u%s", base_length[i], 4231cb0ef41Sopenharmony_ci SEPARATOR(i, LENGTH_CODES-1, 20)); 4241cb0ef41Sopenharmony_ci } 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ci fprintf(header, "local const int base_dist[D_CODES] = {\n"); 4271cb0ef41Sopenharmony_ci for (i = 0; i < D_CODES; i++) { 4281cb0ef41Sopenharmony_ci fprintf(header, "%5u%s", base_dist[i], 4291cb0ef41Sopenharmony_ci SEPARATOR(i, D_CODES-1, 10)); 4301cb0ef41Sopenharmony_ci } 4311cb0ef41Sopenharmony_ci 4321cb0ef41Sopenharmony_ci fclose(header); 4331cb0ef41Sopenharmony_ci} 4341cb0ef41Sopenharmony_ci#endif /* GEN_TREES_H */ 4351cb0ef41Sopenharmony_ci 4361cb0ef41Sopenharmony_ci/* =========================================================================== 4371cb0ef41Sopenharmony_ci * Initialize a new block. 4381cb0ef41Sopenharmony_ci */ 4391cb0ef41Sopenharmony_cilocal void init_block(deflate_state *s) { 4401cb0ef41Sopenharmony_ci int n; /* iterates over tree elements */ 4411cb0ef41Sopenharmony_ci 4421cb0ef41Sopenharmony_ci /* Initialize the trees. */ 4431cb0ef41Sopenharmony_ci for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; 4441cb0ef41Sopenharmony_ci for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; 4451cb0ef41Sopenharmony_ci for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ci s->dyn_ltree[END_BLOCK].Freq = 1; 4481cb0ef41Sopenharmony_ci s->opt_len = s->static_len = 0L; 4491cb0ef41Sopenharmony_ci s->sym_next = s->matches = 0; 4501cb0ef41Sopenharmony_ci} 4511cb0ef41Sopenharmony_ci 4521cb0ef41Sopenharmony_ci/* =========================================================================== 4531cb0ef41Sopenharmony_ci * Initialize the tree data structures for a new zlib stream. 4541cb0ef41Sopenharmony_ci */ 4551cb0ef41Sopenharmony_civoid ZLIB_INTERNAL _tr_init(deflate_state *s) { 4561cb0ef41Sopenharmony_ci tr_static_init(); 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ci s->l_desc.dyn_tree = s->dyn_ltree; 4591cb0ef41Sopenharmony_ci s->l_desc.stat_desc = &static_l_desc; 4601cb0ef41Sopenharmony_ci 4611cb0ef41Sopenharmony_ci s->d_desc.dyn_tree = s->dyn_dtree; 4621cb0ef41Sopenharmony_ci s->d_desc.stat_desc = &static_d_desc; 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ci s->bl_desc.dyn_tree = s->bl_tree; 4651cb0ef41Sopenharmony_ci s->bl_desc.stat_desc = &static_bl_desc; 4661cb0ef41Sopenharmony_ci 4671cb0ef41Sopenharmony_ci s->bi_buf = 0; 4681cb0ef41Sopenharmony_ci s->bi_valid = 0; 4691cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 4701cb0ef41Sopenharmony_ci s->compressed_len = 0L; 4711cb0ef41Sopenharmony_ci s->bits_sent = 0L; 4721cb0ef41Sopenharmony_ci#endif 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ci /* Initialize the first block of the first file: */ 4751cb0ef41Sopenharmony_ci init_block(s); 4761cb0ef41Sopenharmony_ci} 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ci#define SMALLEST 1 4791cb0ef41Sopenharmony_ci/* Index within the heap array of least frequent node in the Huffman tree */ 4801cb0ef41Sopenharmony_ci 4811cb0ef41Sopenharmony_ci 4821cb0ef41Sopenharmony_ci/* =========================================================================== 4831cb0ef41Sopenharmony_ci * Remove the smallest element from the heap and recreate the heap with 4841cb0ef41Sopenharmony_ci * one less element. Updates heap and heap_len. 4851cb0ef41Sopenharmony_ci */ 4861cb0ef41Sopenharmony_ci#define pqremove(s, tree, top) \ 4871cb0ef41Sopenharmony_ci{\ 4881cb0ef41Sopenharmony_ci top = s->heap[SMALLEST]; \ 4891cb0ef41Sopenharmony_ci s->heap[SMALLEST] = s->heap[s->heap_len--]; \ 4901cb0ef41Sopenharmony_ci pqdownheap(s, tree, SMALLEST); \ 4911cb0ef41Sopenharmony_ci} 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci/* =========================================================================== 4941cb0ef41Sopenharmony_ci * Compares to subtrees, using the tree depth as tie breaker when 4951cb0ef41Sopenharmony_ci * the subtrees have equal frequency. This minimizes the worst case length. 4961cb0ef41Sopenharmony_ci */ 4971cb0ef41Sopenharmony_ci#define smaller(tree, n, m, depth) \ 4981cb0ef41Sopenharmony_ci (tree[n].Freq < tree[m].Freq || \ 4991cb0ef41Sopenharmony_ci (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) 5001cb0ef41Sopenharmony_ci 5011cb0ef41Sopenharmony_ci/* =========================================================================== 5021cb0ef41Sopenharmony_ci * Restore the heap property by moving down the tree starting at node k, 5031cb0ef41Sopenharmony_ci * exchanging a node with the smallest of its two sons if necessary, stopping 5041cb0ef41Sopenharmony_ci * when the heap property is re-established (each father smaller than its 5051cb0ef41Sopenharmony_ci * two sons). 5061cb0ef41Sopenharmony_ci */ 5071cb0ef41Sopenharmony_cilocal void pqdownheap(deflate_state *s, ct_data *tree, int k) { 5081cb0ef41Sopenharmony_ci int v = s->heap[k]; 5091cb0ef41Sopenharmony_ci int j = k << 1; /* left son of k */ 5101cb0ef41Sopenharmony_ci while (j <= s->heap_len) { 5111cb0ef41Sopenharmony_ci /* Set j to the smallest of the two sons: */ 5121cb0ef41Sopenharmony_ci if (j < s->heap_len && 5131cb0ef41Sopenharmony_ci smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) { 5141cb0ef41Sopenharmony_ci j++; 5151cb0ef41Sopenharmony_ci } 5161cb0ef41Sopenharmony_ci /* Exit if v is smaller than both sons */ 5171cb0ef41Sopenharmony_ci if (smaller(tree, v, s->heap[j], s->depth)) break; 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci /* Exchange v with the smallest son */ 5201cb0ef41Sopenharmony_ci s->heap[k] = s->heap[j]; k = j; 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci /* And continue down the tree, setting j to the left son of k */ 5231cb0ef41Sopenharmony_ci j <<= 1; 5241cb0ef41Sopenharmony_ci } 5251cb0ef41Sopenharmony_ci s->heap[k] = v; 5261cb0ef41Sopenharmony_ci} 5271cb0ef41Sopenharmony_ci 5281cb0ef41Sopenharmony_ci/* =========================================================================== 5291cb0ef41Sopenharmony_ci * Compute the optimal bit lengths for a tree and update the total bit length 5301cb0ef41Sopenharmony_ci * for the current block. 5311cb0ef41Sopenharmony_ci * IN assertion: the fields freq and dad are set, heap[heap_max] and 5321cb0ef41Sopenharmony_ci * above are the tree nodes sorted by increasing frequency. 5331cb0ef41Sopenharmony_ci * OUT assertions: the field len is set to the optimal bit length, the 5341cb0ef41Sopenharmony_ci * array bl_count contains the frequencies for each bit length. 5351cb0ef41Sopenharmony_ci * The length opt_len is updated; static_len is also updated if stree is 5361cb0ef41Sopenharmony_ci * not null. 5371cb0ef41Sopenharmony_ci */ 5381cb0ef41Sopenharmony_cilocal void gen_bitlen(deflate_state *s, tree_desc *desc) { 5391cb0ef41Sopenharmony_ci ct_data *tree = desc->dyn_tree; 5401cb0ef41Sopenharmony_ci int max_code = desc->max_code; 5411cb0ef41Sopenharmony_ci const ct_data *stree = desc->stat_desc->static_tree; 5421cb0ef41Sopenharmony_ci const intf *extra = desc->stat_desc->extra_bits; 5431cb0ef41Sopenharmony_ci int base = desc->stat_desc->extra_base; 5441cb0ef41Sopenharmony_ci int max_length = desc->stat_desc->max_length; 5451cb0ef41Sopenharmony_ci int h; /* heap index */ 5461cb0ef41Sopenharmony_ci int n, m; /* iterate over the tree elements */ 5471cb0ef41Sopenharmony_ci int bits; /* bit length */ 5481cb0ef41Sopenharmony_ci int xbits; /* extra bits */ 5491cb0ef41Sopenharmony_ci ush f; /* frequency */ 5501cb0ef41Sopenharmony_ci int overflow = 0; /* number of elements with bit length too large */ 5511cb0ef41Sopenharmony_ci 5521cb0ef41Sopenharmony_ci for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; 5531cb0ef41Sopenharmony_ci 5541cb0ef41Sopenharmony_ci /* In a first pass, compute the optimal bit lengths (which may 5551cb0ef41Sopenharmony_ci * overflow in the case of the bit length tree). 5561cb0ef41Sopenharmony_ci */ 5571cb0ef41Sopenharmony_ci tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ 5581cb0ef41Sopenharmony_ci 5591cb0ef41Sopenharmony_ci for (h = s->heap_max + 1; h < HEAP_SIZE; h++) { 5601cb0ef41Sopenharmony_ci n = s->heap[h]; 5611cb0ef41Sopenharmony_ci bits = tree[tree[n].Dad].Len + 1; 5621cb0ef41Sopenharmony_ci if (bits > max_length) bits = max_length, overflow++; 5631cb0ef41Sopenharmony_ci tree[n].Len = (ush)bits; 5641cb0ef41Sopenharmony_ci /* We overwrite tree[n].Dad which is no longer needed */ 5651cb0ef41Sopenharmony_ci 5661cb0ef41Sopenharmony_ci if (n > max_code) continue; /* not a leaf node */ 5671cb0ef41Sopenharmony_ci 5681cb0ef41Sopenharmony_ci s->bl_count[bits]++; 5691cb0ef41Sopenharmony_ci xbits = 0; 5701cb0ef41Sopenharmony_ci if (n >= base) xbits = extra[n - base]; 5711cb0ef41Sopenharmony_ci f = tree[n].Freq; 5721cb0ef41Sopenharmony_ci s->opt_len += (ulg)f * (unsigned)(bits + xbits); 5731cb0ef41Sopenharmony_ci if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits); 5741cb0ef41Sopenharmony_ci } 5751cb0ef41Sopenharmony_ci if (overflow == 0) return; 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ci Tracev((stderr,"\nbit length overflow\n")); 5781cb0ef41Sopenharmony_ci /* This happens for example on obj2 and pic of the Calgary corpus */ 5791cb0ef41Sopenharmony_ci 5801cb0ef41Sopenharmony_ci /* Find the first bit length which could increase: */ 5811cb0ef41Sopenharmony_ci do { 5821cb0ef41Sopenharmony_ci bits = max_length - 1; 5831cb0ef41Sopenharmony_ci while (s->bl_count[bits] == 0) bits--; 5841cb0ef41Sopenharmony_ci s->bl_count[bits]--; /* move one leaf down the tree */ 5851cb0ef41Sopenharmony_ci s->bl_count[bits + 1] += 2; /* move one overflow item as its brother */ 5861cb0ef41Sopenharmony_ci s->bl_count[max_length]--; 5871cb0ef41Sopenharmony_ci /* The brother of the overflow item also moves one step up, 5881cb0ef41Sopenharmony_ci * but this does not affect bl_count[max_length] 5891cb0ef41Sopenharmony_ci */ 5901cb0ef41Sopenharmony_ci overflow -= 2; 5911cb0ef41Sopenharmony_ci } while (overflow > 0); 5921cb0ef41Sopenharmony_ci 5931cb0ef41Sopenharmony_ci /* Now recompute all bit lengths, scanning in increasing frequency. 5941cb0ef41Sopenharmony_ci * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all 5951cb0ef41Sopenharmony_ci * lengths instead of fixing only the wrong ones. This idea is taken 5961cb0ef41Sopenharmony_ci * from 'ar' written by Haruhiko Okumura.) 5971cb0ef41Sopenharmony_ci */ 5981cb0ef41Sopenharmony_ci for (bits = max_length; bits != 0; bits--) { 5991cb0ef41Sopenharmony_ci n = s->bl_count[bits]; 6001cb0ef41Sopenharmony_ci while (n != 0) { 6011cb0ef41Sopenharmony_ci m = s->heap[--h]; 6021cb0ef41Sopenharmony_ci if (m > max_code) continue; 6031cb0ef41Sopenharmony_ci if ((unsigned) tree[m].Len != (unsigned) bits) { 6041cb0ef41Sopenharmony_ci Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); 6051cb0ef41Sopenharmony_ci s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq; 6061cb0ef41Sopenharmony_ci tree[m].Len = (ush)bits; 6071cb0ef41Sopenharmony_ci } 6081cb0ef41Sopenharmony_ci n--; 6091cb0ef41Sopenharmony_ci } 6101cb0ef41Sopenharmony_ci } 6111cb0ef41Sopenharmony_ci} 6121cb0ef41Sopenharmony_ci 6131cb0ef41Sopenharmony_ci#ifdef DUMP_BL_TREE 6141cb0ef41Sopenharmony_ci# include <stdio.h> 6151cb0ef41Sopenharmony_ci#endif 6161cb0ef41Sopenharmony_ci 6171cb0ef41Sopenharmony_ci/* =========================================================================== 6181cb0ef41Sopenharmony_ci * Construct one Huffman tree and assigns the code bit strings and lengths. 6191cb0ef41Sopenharmony_ci * Update the total bit length for the current block. 6201cb0ef41Sopenharmony_ci * IN assertion: the field freq is set for all tree elements. 6211cb0ef41Sopenharmony_ci * OUT assertions: the fields len and code are set to the optimal bit length 6221cb0ef41Sopenharmony_ci * and corresponding code. The length opt_len is updated; static_len is 6231cb0ef41Sopenharmony_ci * also updated if stree is not null. The field max_code is set. 6241cb0ef41Sopenharmony_ci */ 6251cb0ef41Sopenharmony_cilocal void build_tree(deflate_state *s, tree_desc *desc) { 6261cb0ef41Sopenharmony_ci ct_data *tree = desc->dyn_tree; 6271cb0ef41Sopenharmony_ci const ct_data *stree = desc->stat_desc->static_tree; 6281cb0ef41Sopenharmony_ci int elems = desc->stat_desc->elems; 6291cb0ef41Sopenharmony_ci int n, m; /* iterate over heap elements */ 6301cb0ef41Sopenharmony_ci int max_code = -1; /* largest code with non zero frequency */ 6311cb0ef41Sopenharmony_ci int node; /* new node being created */ 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci /* Construct the initial heap, with least frequent element in 6341cb0ef41Sopenharmony_ci * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n + 1]. 6351cb0ef41Sopenharmony_ci * heap[0] is not used. 6361cb0ef41Sopenharmony_ci */ 6371cb0ef41Sopenharmony_ci s->heap_len = 0, s->heap_max = HEAP_SIZE; 6381cb0ef41Sopenharmony_ci 6391cb0ef41Sopenharmony_ci for (n = 0; n < elems; n++) { 6401cb0ef41Sopenharmony_ci if (tree[n].Freq != 0) { 6411cb0ef41Sopenharmony_ci s->heap[++(s->heap_len)] = max_code = n; 6421cb0ef41Sopenharmony_ci s->depth[n] = 0; 6431cb0ef41Sopenharmony_ci } else { 6441cb0ef41Sopenharmony_ci tree[n].Len = 0; 6451cb0ef41Sopenharmony_ci } 6461cb0ef41Sopenharmony_ci } 6471cb0ef41Sopenharmony_ci 6481cb0ef41Sopenharmony_ci /* The pkzip format requires that at least one distance code exists, 6491cb0ef41Sopenharmony_ci * and that at least one bit should be sent even if there is only one 6501cb0ef41Sopenharmony_ci * possible code. So to avoid special checks later on we force at least 6511cb0ef41Sopenharmony_ci * two codes of non zero frequency. 6521cb0ef41Sopenharmony_ci */ 6531cb0ef41Sopenharmony_ci while (s->heap_len < 2) { 6541cb0ef41Sopenharmony_ci node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); 6551cb0ef41Sopenharmony_ci tree[node].Freq = 1; 6561cb0ef41Sopenharmony_ci s->depth[node] = 0; 6571cb0ef41Sopenharmony_ci s->opt_len--; if (stree) s->static_len -= stree[node].Len; 6581cb0ef41Sopenharmony_ci /* node is 0 or 1 so it does not have extra bits */ 6591cb0ef41Sopenharmony_ci } 6601cb0ef41Sopenharmony_ci desc->max_code = max_code; 6611cb0ef41Sopenharmony_ci 6621cb0ef41Sopenharmony_ci /* The elements heap[heap_len/2 + 1 .. heap_len] are leaves of the tree, 6631cb0ef41Sopenharmony_ci * establish sub-heaps of increasing lengths: 6641cb0ef41Sopenharmony_ci */ 6651cb0ef41Sopenharmony_ci for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); 6661cb0ef41Sopenharmony_ci 6671cb0ef41Sopenharmony_ci /* Construct the Huffman tree by repeatedly combining the least two 6681cb0ef41Sopenharmony_ci * frequent nodes. 6691cb0ef41Sopenharmony_ci */ 6701cb0ef41Sopenharmony_ci node = elems; /* next internal node of the tree */ 6711cb0ef41Sopenharmony_ci do { 6721cb0ef41Sopenharmony_ci pqremove(s, tree, n); /* n = node of least frequency */ 6731cb0ef41Sopenharmony_ci m = s->heap[SMALLEST]; /* m = node of next least frequency */ 6741cb0ef41Sopenharmony_ci 6751cb0ef41Sopenharmony_ci s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ 6761cb0ef41Sopenharmony_ci s->heap[--(s->heap_max)] = m; 6771cb0ef41Sopenharmony_ci 6781cb0ef41Sopenharmony_ci /* Create a new node father of n and m */ 6791cb0ef41Sopenharmony_ci tree[node].Freq = tree[n].Freq + tree[m].Freq; 6801cb0ef41Sopenharmony_ci s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? 6811cb0ef41Sopenharmony_ci s->depth[n] : s->depth[m]) + 1); 6821cb0ef41Sopenharmony_ci tree[n].Dad = tree[m].Dad = (ush)node; 6831cb0ef41Sopenharmony_ci#ifdef DUMP_BL_TREE 6841cb0ef41Sopenharmony_ci if (tree == s->bl_tree) { 6851cb0ef41Sopenharmony_ci fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", 6861cb0ef41Sopenharmony_ci node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); 6871cb0ef41Sopenharmony_ci } 6881cb0ef41Sopenharmony_ci#endif 6891cb0ef41Sopenharmony_ci /* and insert the new node in the heap */ 6901cb0ef41Sopenharmony_ci s->heap[SMALLEST] = node++; 6911cb0ef41Sopenharmony_ci pqdownheap(s, tree, SMALLEST); 6921cb0ef41Sopenharmony_ci 6931cb0ef41Sopenharmony_ci } while (s->heap_len >= 2); 6941cb0ef41Sopenharmony_ci 6951cb0ef41Sopenharmony_ci s->heap[--(s->heap_max)] = s->heap[SMALLEST]; 6961cb0ef41Sopenharmony_ci 6971cb0ef41Sopenharmony_ci /* At this point, the fields freq and dad are set. We can now 6981cb0ef41Sopenharmony_ci * generate the bit lengths. 6991cb0ef41Sopenharmony_ci */ 7001cb0ef41Sopenharmony_ci gen_bitlen(s, (tree_desc *)desc); 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ci /* The field len is now set, we can generate the bit codes */ 7031cb0ef41Sopenharmony_ci gen_codes ((ct_data *)tree, max_code, s->bl_count); 7041cb0ef41Sopenharmony_ci} 7051cb0ef41Sopenharmony_ci 7061cb0ef41Sopenharmony_ci/* =========================================================================== 7071cb0ef41Sopenharmony_ci * Scan a literal or distance tree to determine the frequencies of the codes 7081cb0ef41Sopenharmony_ci * in the bit length tree. 7091cb0ef41Sopenharmony_ci */ 7101cb0ef41Sopenharmony_cilocal void scan_tree(deflate_state *s, ct_data *tree, int max_code) { 7111cb0ef41Sopenharmony_ci int n; /* iterates over all tree elements */ 7121cb0ef41Sopenharmony_ci int prevlen = -1; /* last emitted length */ 7131cb0ef41Sopenharmony_ci int curlen; /* length of current code */ 7141cb0ef41Sopenharmony_ci int nextlen = tree[0].Len; /* length of next code */ 7151cb0ef41Sopenharmony_ci int count = 0; /* repeat count of the current code */ 7161cb0ef41Sopenharmony_ci int max_count = 7; /* max repeat count */ 7171cb0ef41Sopenharmony_ci int min_count = 4; /* min repeat count */ 7181cb0ef41Sopenharmony_ci 7191cb0ef41Sopenharmony_ci if (nextlen == 0) max_count = 138, min_count = 3; 7201cb0ef41Sopenharmony_ci tree[max_code + 1].Len = (ush)0xffff; /* guard */ 7211cb0ef41Sopenharmony_ci 7221cb0ef41Sopenharmony_ci for (n = 0; n <= max_code; n++) { 7231cb0ef41Sopenharmony_ci curlen = nextlen; nextlen = tree[n + 1].Len; 7241cb0ef41Sopenharmony_ci if (++count < max_count && curlen == nextlen) { 7251cb0ef41Sopenharmony_ci continue; 7261cb0ef41Sopenharmony_ci } else if (count < min_count) { 7271cb0ef41Sopenharmony_ci s->bl_tree[curlen].Freq += count; 7281cb0ef41Sopenharmony_ci } else if (curlen != 0) { 7291cb0ef41Sopenharmony_ci if (curlen != prevlen) s->bl_tree[curlen].Freq++; 7301cb0ef41Sopenharmony_ci s->bl_tree[REP_3_6].Freq++; 7311cb0ef41Sopenharmony_ci } else if (count <= 10) { 7321cb0ef41Sopenharmony_ci s->bl_tree[REPZ_3_10].Freq++; 7331cb0ef41Sopenharmony_ci } else { 7341cb0ef41Sopenharmony_ci s->bl_tree[REPZ_11_138].Freq++; 7351cb0ef41Sopenharmony_ci } 7361cb0ef41Sopenharmony_ci count = 0; prevlen = curlen; 7371cb0ef41Sopenharmony_ci if (nextlen == 0) { 7381cb0ef41Sopenharmony_ci max_count = 138, min_count = 3; 7391cb0ef41Sopenharmony_ci } else if (curlen == nextlen) { 7401cb0ef41Sopenharmony_ci max_count = 6, min_count = 3; 7411cb0ef41Sopenharmony_ci } else { 7421cb0ef41Sopenharmony_ci max_count = 7, min_count = 4; 7431cb0ef41Sopenharmony_ci } 7441cb0ef41Sopenharmony_ci } 7451cb0ef41Sopenharmony_ci} 7461cb0ef41Sopenharmony_ci 7471cb0ef41Sopenharmony_ci/* =========================================================================== 7481cb0ef41Sopenharmony_ci * Send a literal or distance tree in compressed form, using the codes in 7491cb0ef41Sopenharmony_ci * bl_tree. 7501cb0ef41Sopenharmony_ci */ 7511cb0ef41Sopenharmony_cilocal void send_tree(deflate_state *s, ct_data *tree, int max_code) { 7521cb0ef41Sopenharmony_ci int n; /* iterates over all tree elements */ 7531cb0ef41Sopenharmony_ci int prevlen = -1; /* last emitted length */ 7541cb0ef41Sopenharmony_ci int curlen; /* length of current code */ 7551cb0ef41Sopenharmony_ci int nextlen = tree[0].Len; /* length of next code */ 7561cb0ef41Sopenharmony_ci int count = 0; /* repeat count of the current code */ 7571cb0ef41Sopenharmony_ci int max_count = 7; /* max repeat count */ 7581cb0ef41Sopenharmony_ci int min_count = 4; /* min repeat count */ 7591cb0ef41Sopenharmony_ci 7601cb0ef41Sopenharmony_ci /* tree[max_code + 1].Len = -1; */ /* guard already set */ 7611cb0ef41Sopenharmony_ci if (nextlen == 0) max_count = 138, min_count = 3; 7621cb0ef41Sopenharmony_ci 7631cb0ef41Sopenharmony_ci for (n = 0; n <= max_code; n++) { 7641cb0ef41Sopenharmony_ci curlen = nextlen; nextlen = tree[n + 1].Len; 7651cb0ef41Sopenharmony_ci if (++count < max_count && curlen == nextlen) { 7661cb0ef41Sopenharmony_ci continue; 7671cb0ef41Sopenharmony_ci } else if (count < min_count) { 7681cb0ef41Sopenharmony_ci do { send_code(s, curlen, s->bl_tree); } while (--count != 0); 7691cb0ef41Sopenharmony_ci 7701cb0ef41Sopenharmony_ci } else if (curlen != 0) { 7711cb0ef41Sopenharmony_ci if (curlen != prevlen) { 7721cb0ef41Sopenharmony_ci send_code(s, curlen, s->bl_tree); count--; 7731cb0ef41Sopenharmony_ci } 7741cb0ef41Sopenharmony_ci Assert(count >= 3 && count <= 6, " 3_6?"); 7751cb0ef41Sopenharmony_ci send_code(s, REP_3_6, s->bl_tree); send_bits(s, count - 3, 2); 7761cb0ef41Sopenharmony_ci 7771cb0ef41Sopenharmony_ci } else if (count <= 10) { 7781cb0ef41Sopenharmony_ci send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count - 3, 3); 7791cb0ef41Sopenharmony_ci 7801cb0ef41Sopenharmony_ci } else { 7811cb0ef41Sopenharmony_ci send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count - 11, 7); 7821cb0ef41Sopenharmony_ci } 7831cb0ef41Sopenharmony_ci count = 0; prevlen = curlen; 7841cb0ef41Sopenharmony_ci if (nextlen == 0) { 7851cb0ef41Sopenharmony_ci max_count = 138, min_count = 3; 7861cb0ef41Sopenharmony_ci } else if (curlen == nextlen) { 7871cb0ef41Sopenharmony_ci max_count = 6, min_count = 3; 7881cb0ef41Sopenharmony_ci } else { 7891cb0ef41Sopenharmony_ci max_count = 7, min_count = 4; 7901cb0ef41Sopenharmony_ci } 7911cb0ef41Sopenharmony_ci } 7921cb0ef41Sopenharmony_ci} 7931cb0ef41Sopenharmony_ci 7941cb0ef41Sopenharmony_ci/* =========================================================================== 7951cb0ef41Sopenharmony_ci * Construct the Huffman tree for the bit lengths and return the index in 7961cb0ef41Sopenharmony_ci * bl_order of the last bit length code to send. 7971cb0ef41Sopenharmony_ci */ 7981cb0ef41Sopenharmony_cilocal int build_bl_tree(deflate_state *s) { 7991cb0ef41Sopenharmony_ci int max_blindex; /* index of last bit length code of non zero freq */ 8001cb0ef41Sopenharmony_ci 8011cb0ef41Sopenharmony_ci /* Determine the bit length frequencies for literal and distance trees */ 8021cb0ef41Sopenharmony_ci scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); 8031cb0ef41Sopenharmony_ci scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ci /* Build the bit length tree: */ 8061cb0ef41Sopenharmony_ci build_tree(s, (tree_desc *)(&(s->bl_desc))); 8071cb0ef41Sopenharmony_ci /* opt_len now includes the length of the tree representations, except the 8081cb0ef41Sopenharmony_ci * lengths of the bit lengths codes and the 5 + 5 + 4 bits for the counts. 8091cb0ef41Sopenharmony_ci */ 8101cb0ef41Sopenharmony_ci 8111cb0ef41Sopenharmony_ci /* Determine the number of bit length codes to send. The pkzip format 8121cb0ef41Sopenharmony_ci * requires that at least 4 bit length codes be sent. (appnote.txt says 8131cb0ef41Sopenharmony_ci * 3 but the actual value used is 4.) 8141cb0ef41Sopenharmony_ci */ 8151cb0ef41Sopenharmony_ci for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { 8161cb0ef41Sopenharmony_ci if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; 8171cb0ef41Sopenharmony_ci } 8181cb0ef41Sopenharmony_ci /* Update opt_len to include the bit length tree and counts */ 8191cb0ef41Sopenharmony_ci s->opt_len += 3*((ulg)max_blindex + 1) + 5 + 5 + 4; 8201cb0ef41Sopenharmony_ci Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", 8211cb0ef41Sopenharmony_ci s->opt_len, s->static_len)); 8221cb0ef41Sopenharmony_ci 8231cb0ef41Sopenharmony_ci return max_blindex; 8241cb0ef41Sopenharmony_ci} 8251cb0ef41Sopenharmony_ci 8261cb0ef41Sopenharmony_ci/* =========================================================================== 8271cb0ef41Sopenharmony_ci * Send the header for a block using dynamic Huffman trees: the counts, the 8281cb0ef41Sopenharmony_ci * lengths of the bit length codes, the literal tree and the distance tree. 8291cb0ef41Sopenharmony_ci * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 8301cb0ef41Sopenharmony_ci */ 8311cb0ef41Sopenharmony_cilocal void send_all_trees(deflate_state *s, int lcodes, int dcodes, 8321cb0ef41Sopenharmony_ci int blcodes) { 8331cb0ef41Sopenharmony_ci int rank; /* index in bl_order */ 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ci Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); 8361cb0ef41Sopenharmony_ci Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, 8371cb0ef41Sopenharmony_ci "too many codes"); 8381cb0ef41Sopenharmony_ci Tracev((stderr, "\nbl counts: ")); 8391cb0ef41Sopenharmony_ci send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */ 8401cb0ef41Sopenharmony_ci send_bits(s, dcodes - 1, 5); 8411cb0ef41Sopenharmony_ci send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */ 8421cb0ef41Sopenharmony_ci for (rank = 0; rank < blcodes; rank++) { 8431cb0ef41Sopenharmony_ci Tracev((stderr, "\nbl code %2d ", bl_order[rank])); 8441cb0ef41Sopenharmony_ci send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); 8451cb0ef41Sopenharmony_ci } 8461cb0ef41Sopenharmony_ci Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); 8471cb0ef41Sopenharmony_ci 8481cb0ef41Sopenharmony_ci send_tree(s, (ct_data *)s->dyn_ltree, lcodes - 1); /* literal tree */ 8491cb0ef41Sopenharmony_ci Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); 8501cb0ef41Sopenharmony_ci 8511cb0ef41Sopenharmony_ci send_tree(s, (ct_data *)s->dyn_dtree, dcodes - 1); /* distance tree */ 8521cb0ef41Sopenharmony_ci Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); 8531cb0ef41Sopenharmony_ci} 8541cb0ef41Sopenharmony_ci 8551cb0ef41Sopenharmony_ci/* =========================================================================== 8561cb0ef41Sopenharmony_ci * Send a stored block 8571cb0ef41Sopenharmony_ci */ 8581cb0ef41Sopenharmony_civoid ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, 8591cb0ef41Sopenharmony_ci ulg stored_len, int last) { 8601cb0ef41Sopenharmony_ci send_bits(s, (STORED_BLOCK<<1) + last, 3); /* send block type */ 8611cb0ef41Sopenharmony_ci bi_windup(s); /* align on byte boundary */ 8621cb0ef41Sopenharmony_ci put_short(s, (ush)stored_len); 8631cb0ef41Sopenharmony_ci put_short(s, (ush)~stored_len); 8641cb0ef41Sopenharmony_ci if (stored_len) 8651cb0ef41Sopenharmony_ci zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len); 8661cb0ef41Sopenharmony_ci s->pending += stored_len; 8671cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 8681cb0ef41Sopenharmony_ci s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; 8691cb0ef41Sopenharmony_ci s->compressed_len += (stored_len + 4) << 3; 8701cb0ef41Sopenharmony_ci s->bits_sent += 2*16; 8711cb0ef41Sopenharmony_ci s->bits_sent += stored_len << 3; 8721cb0ef41Sopenharmony_ci#endif 8731cb0ef41Sopenharmony_ci} 8741cb0ef41Sopenharmony_ci 8751cb0ef41Sopenharmony_ci/* =========================================================================== 8761cb0ef41Sopenharmony_ci * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) 8771cb0ef41Sopenharmony_ci */ 8781cb0ef41Sopenharmony_civoid ZLIB_INTERNAL _tr_flush_bits(deflate_state *s) { 8791cb0ef41Sopenharmony_ci bi_flush(s); 8801cb0ef41Sopenharmony_ci} 8811cb0ef41Sopenharmony_ci 8821cb0ef41Sopenharmony_ci/* =========================================================================== 8831cb0ef41Sopenharmony_ci * Send one empty static block to give enough lookahead for inflate. 8841cb0ef41Sopenharmony_ci * This takes 10 bits, of which 7 may remain in the bit buffer. 8851cb0ef41Sopenharmony_ci */ 8861cb0ef41Sopenharmony_civoid ZLIB_INTERNAL _tr_align(deflate_state *s) { 8871cb0ef41Sopenharmony_ci send_bits(s, STATIC_TREES<<1, 3); 8881cb0ef41Sopenharmony_ci send_code(s, END_BLOCK, static_ltree); 8891cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 8901cb0ef41Sopenharmony_ci s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 8911cb0ef41Sopenharmony_ci#endif 8921cb0ef41Sopenharmony_ci bi_flush(s); 8931cb0ef41Sopenharmony_ci} 8941cb0ef41Sopenharmony_ci 8951cb0ef41Sopenharmony_ci/* =========================================================================== 8961cb0ef41Sopenharmony_ci * Send the block data compressed using the given Huffman trees 8971cb0ef41Sopenharmony_ci */ 8981cb0ef41Sopenharmony_cilocal void compress_block(deflate_state *s, const ct_data *ltree, 8991cb0ef41Sopenharmony_ci const ct_data *dtree) { 9001cb0ef41Sopenharmony_ci unsigned dist; /* distance of matched string */ 9011cb0ef41Sopenharmony_ci int lc; /* match length or unmatched char (if dist == 0) */ 9021cb0ef41Sopenharmony_ci unsigned sx = 0; /* running index in symbol buffers */ 9031cb0ef41Sopenharmony_ci unsigned code; /* the code to send */ 9041cb0ef41Sopenharmony_ci int extra; /* number of extra bits to send */ 9051cb0ef41Sopenharmony_ci 9061cb0ef41Sopenharmony_ci if (s->sym_next != 0) do { 9071cb0ef41Sopenharmony_ci#ifdef LIT_MEM 9081cb0ef41Sopenharmony_ci dist = s->d_buf[sx]; 9091cb0ef41Sopenharmony_ci lc = s->l_buf[sx++]; 9101cb0ef41Sopenharmony_ci#else 9111cb0ef41Sopenharmony_ci dist = s->sym_buf[sx++] & 0xff; 9121cb0ef41Sopenharmony_ci dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8; 9131cb0ef41Sopenharmony_ci lc = s->sym_buf[sx++]; 9141cb0ef41Sopenharmony_ci#endif 9151cb0ef41Sopenharmony_ci if (dist == 0) { 9161cb0ef41Sopenharmony_ci send_code(s, lc, ltree); /* send a literal byte */ 9171cb0ef41Sopenharmony_ci Tracecv(isgraph(lc), (stderr," '%c' ", lc)); 9181cb0ef41Sopenharmony_ci } else { 9191cb0ef41Sopenharmony_ci /* Here, lc is the match length - MIN_MATCH */ 9201cb0ef41Sopenharmony_ci code = _length_code[lc]; 9211cb0ef41Sopenharmony_ci send_code(s, code + LITERALS + 1, ltree); /* send length code */ 9221cb0ef41Sopenharmony_ci extra = extra_lbits[code]; 9231cb0ef41Sopenharmony_ci if (extra != 0) { 9241cb0ef41Sopenharmony_ci lc -= base_length[code]; 9251cb0ef41Sopenharmony_ci send_bits(s, lc, extra); /* send the extra length bits */ 9261cb0ef41Sopenharmony_ci } 9271cb0ef41Sopenharmony_ci dist--; /* dist is now the match distance - 1 */ 9281cb0ef41Sopenharmony_ci code = d_code(dist); 9291cb0ef41Sopenharmony_ci Assert (code < D_CODES, "bad d_code"); 9301cb0ef41Sopenharmony_ci 9311cb0ef41Sopenharmony_ci send_code(s, code, dtree); /* send the distance code */ 9321cb0ef41Sopenharmony_ci extra = extra_dbits[code]; 9331cb0ef41Sopenharmony_ci if (extra != 0) { 9341cb0ef41Sopenharmony_ci dist -= (unsigned)base_dist[code]; 9351cb0ef41Sopenharmony_ci send_bits(s, dist, extra); /* send the extra distance bits */ 9361cb0ef41Sopenharmony_ci } 9371cb0ef41Sopenharmony_ci } /* literal or match pair ? */ 9381cb0ef41Sopenharmony_ci 9391cb0ef41Sopenharmony_ci /* Check for no overlay of pending_buf on needed symbols */ 9401cb0ef41Sopenharmony_ci#ifdef LIT_MEM 9411cb0ef41Sopenharmony_ci Assert(s->pending < (s->lit_bufsize << 1) + (sx << 1), 9421cb0ef41Sopenharmony_ci "pendingBuf overflow"); 9431cb0ef41Sopenharmony_ci#else 9441cb0ef41Sopenharmony_ci Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow"); 9451cb0ef41Sopenharmony_ci#endif 9461cb0ef41Sopenharmony_ci 9471cb0ef41Sopenharmony_ci } while (sx < s->sym_next); 9481cb0ef41Sopenharmony_ci 9491cb0ef41Sopenharmony_ci send_code(s, END_BLOCK, ltree); 9501cb0ef41Sopenharmony_ci} 9511cb0ef41Sopenharmony_ci 9521cb0ef41Sopenharmony_ci/* =========================================================================== 9531cb0ef41Sopenharmony_ci * Check if the data type is TEXT or BINARY, using the following algorithm: 9541cb0ef41Sopenharmony_ci * - TEXT if the two conditions below are satisfied: 9551cb0ef41Sopenharmony_ci * a) There are no non-portable control characters belonging to the 9561cb0ef41Sopenharmony_ci * "block list" (0..6, 14..25, 28..31). 9571cb0ef41Sopenharmony_ci * b) There is at least one printable character belonging to the 9581cb0ef41Sopenharmony_ci * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). 9591cb0ef41Sopenharmony_ci * - BINARY otherwise. 9601cb0ef41Sopenharmony_ci * - The following partially-portable control characters form a 9611cb0ef41Sopenharmony_ci * "gray list" that is ignored in this detection algorithm: 9621cb0ef41Sopenharmony_ci * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). 9631cb0ef41Sopenharmony_ci * IN assertion: the fields Freq of dyn_ltree are set. 9641cb0ef41Sopenharmony_ci */ 9651cb0ef41Sopenharmony_cilocal int detect_data_type(deflate_state *s) { 9661cb0ef41Sopenharmony_ci /* block_mask is the bit mask of block-listed bytes 9671cb0ef41Sopenharmony_ci * set bits 0..6, 14..25, and 28..31 9681cb0ef41Sopenharmony_ci * 0xf3ffc07f = binary 11110011111111111100000001111111 9691cb0ef41Sopenharmony_ci */ 9701cb0ef41Sopenharmony_ci unsigned long block_mask = 0xf3ffc07fUL; 9711cb0ef41Sopenharmony_ci int n; 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ci /* Check for non-textual ("block-listed") bytes. */ 9741cb0ef41Sopenharmony_ci for (n = 0; n <= 31; n++, block_mask >>= 1) 9751cb0ef41Sopenharmony_ci if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0)) 9761cb0ef41Sopenharmony_ci return Z_BINARY; 9771cb0ef41Sopenharmony_ci 9781cb0ef41Sopenharmony_ci /* Check for textual ("allow-listed") bytes. */ 9791cb0ef41Sopenharmony_ci if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 9801cb0ef41Sopenharmony_ci || s->dyn_ltree[13].Freq != 0) 9811cb0ef41Sopenharmony_ci return Z_TEXT; 9821cb0ef41Sopenharmony_ci for (n = 32; n < LITERALS; n++) 9831cb0ef41Sopenharmony_ci if (s->dyn_ltree[n].Freq != 0) 9841cb0ef41Sopenharmony_ci return Z_TEXT; 9851cb0ef41Sopenharmony_ci 9861cb0ef41Sopenharmony_ci /* There are no "block-listed" or "allow-listed" bytes: 9871cb0ef41Sopenharmony_ci * this stream either is empty or has tolerated ("gray-listed") bytes only. 9881cb0ef41Sopenharmony_ci */ 9891cb0ef41Sopenharmony_ci return Z_BINARY; 9901cb0ef41Sopenharmony_ci} 9911cb0ef41Sopenharmony_ci 9921cb0ef41Sopenharmony_ci/* =========================================================================== 9931cb0ef41Sopenharmony_ci * Determine the best encoding for the current block: dynamic trees, static 9941cb0ef41Sopenharmony_ci * trees or store, and write out the encoded block. 9951cb0ef41Sopenharmony_ci */ 9961cb0ef41Sopenharmony_civoid ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, 9971cb0ef41Sopenharmony_ci ulg stored_len, int last) { 9981cb0ef41Sopenharmony_ci ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 9991cb0ef41Sopenharmony_ci int max_blindex = 0; /* index of last bit length code of non zero freq */ 10001cb0ef41Sopenharmony_ci 10011cb0ef41Sopenharmony_ci /* Build the Huffman trees unless a stored block is forced */ 10021cb0ef41Sopenharmony_ci if (s->level > 0) { 10031cb0ef41Sopenharmony_ci 10041cb0ef41Sopenharmony_ci /* Check if the file is binary or text */ 10051cb0ef41Sopenharmony_ci if (s->strm->data_type == Z_UNKNOWN) 10061cb0ef41Sopenharmony_ci s->strm->data_type = detect_data_type(s); 10071cb0ef41Sopenharmony_ci 10081cb0ef41Sopenharmony_ci /* Construct the literal and distance trees */ 10091cb0ef41Sopenharmony_ci build_tree(s, (tree_desc *)(&(s->l_desc))); 10101cb0ef41Sopenharmony_ci Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, 10111cb0ef41Sopenharmony_ci s->static_len)); 10121cb0ef41Sopenharmony_ci 10131cb0ef41Sopenharmony_ci build_tree(s, (tree_desc *)(&(s->d_desc))); 10141cb0ef41Sopenharmony_ci Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, 10151cb0ef41Sopenharmony_ci s->static_len)); 10161cb0ef41Sopenharmony_ci /* At this point, opt_len and static_len are the total bit lengths of 10171cb0ef41Sopenharmony_ci * the compressed block data, excluding the tree representations. 10181cb0ef41Sopenharmony_ci */ 10191cb0ef41Sopenharmony_ci 10201cb0ef41Sopenharmony_ci /* Build the bit length tree for the above two trees, and get the index 10211cb0ef41Sopenharmony_ci * in bl_order of the last bit length code to send. 10221cb0ef41Sopenharmony_ci */ 10231cb0ef41Sopenharmony_ci max_blindex = build_bl_tree(s); 10241cb0ef41Sopenharmony_ci 10251cb0ef41Sopenharmony_ci /* Determine the best encoding. Compute the block lengths in bytes. */ 10261cb0ef41Sopenharmony_ci opt_lenb = (s->opt_len + 3 + 7) >> 3; 10271cb0ef41Sopenharmony_ci static_lenb = (s->static_len + 3 + 7) >> 3; 10281cb0ef41Sopenharmony_ci 10291cb0ef41Sopenharmony_ci Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", 10301cb0ef41Sopenharmony_ci opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, 10311cb0ef41Sopenharmony_ci s->sym_next / 3)); 10321cb0ef41Sopenharmony_ci 10331cb0ef41Sopenharmony_ci#ifndef FORCE_STATIC 10341cb0ef41Sopenharmony_ci if (static_lenb <= opt_lenb || s->strategy == Z_FIXED) 10351cb0ef41Sopenharmony_ci#endif 10361cb0ef41Sopenharmony_ci opt_lenb = static_lenb; 10371cb0ef41Sopenharmony_ci 10381cb0ef41Sopenharmony_ci } else { 10391cb0ef41Sopenharmony_ci Assert(buf != (char*)0, "lost buf"); 10401cb0ef41Sopenharmony_ci opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ 10411cb0ef41Sopenharmony_ci } 10421cb0ef41Sopenharmony_ci 10431cb0ef41Sopenharmony_ci#ifdef FORCE_STORED 10441cb0ef41Sopenharmony_ci if (buf != (char*)0) { /* force stored block */ 10451cb0ef41Sopenharmony_ci#else 10461cb0ef41Sopenharmony_ci if (stored_len + 4 <= opt_lenb && buf != (char*)0) { 10471cb0ef41Sopenharmony_ci /* 4: two words for the lengths */ 10481cb0ef41Sopenharmony_ci#endif 10491cb0ef41Sopenharmony_ci /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. 10501cb0ef41Sopenharmony_ci * Otherwise we can't have processed more than WSIZE input bytes since 10511cb0ef41Sopenharmony_ci * the last block flush, because compression would have been 10521cb0ef41Sopenharmony_ci * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 10531cb0ef41Sopenharmony_ci * transform a block into a stored block. 10541cb0ef41Sopenharmony_ci */ 10551cb0ef41Sopenharmony_ci _tr_stored_block(s, buf, stored_len, last); 10561cb0ef41Sopenharmony_ci 10571cb0ef41Sopenharmony_ci } else if (static_lenb == opt_lenb) { 10581cb0ef41Sopenharmony_ci send_bits(s, (STATIC_TREES<<1) + last, 3); 10591cb0ef41Sopenharmony_ci compress_block(s, (const ct_data *)static_ltree, 10601cb0ef41Sopenharmony_ci (const ct_data *)static_dtree); 10611cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 10621cb0ef41Sopenharmony_ci s->compressed_len += 3 + s->static_len; 10631cb0ef41Sopenharmony_ci#endif 10641cb0ef41Sopenharmony_ci } else { 10651cb0ef41Sopenharmony_ci send_bits(s, (DYN_TREES<<1) + last, 3); 10661cb0ef41Sopenharmony_ci send_all_trees(s, s->l_desc.max_code + 1, s->d_desc.max_code + 1, 10671cb0ef41Sopenharmony_ci max_blindex + 1); 10681cb0ef41Sopenharmony_ci compress_block(s, (const ct_data *)s->dyn_ltree, 10691cb0ef41Sopenharmony_ci (const ct_data *)s->dyn_dtree); 10701cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 10711cb0ef41Sopenharmony_ci s->compressed_len += 3 + s->opt_len; 10721cb0ef41Sopenharmony_ci#endif 10731cb0ef41Sopenharmony_ci } 10741cb0ef41Sopenharmony_ci Assert (s->compressed_len == s->bits_sent, "bad compressed size"); 10751cb0ef41Sopenharmony_ci /* The above check is made mod 2^32, for files larger than 512 MB 10761cb0ef41Sopenharmony_ci * and uLong implemented on 32 bits. 10771cb0ef41Sopenharmony_ci */ 10781cb0ef41Sopenharmony_ci init_block(s); 10791cb0ef41Sopenharmony_ci 10801cb0ef41Sopenharmony_ci if (last) { 10811cb0ef41Sopenharmony_ci bi_windup(s); 10821cb0ef41Sopenharmony_ci#ifdef ZLIB_DEBUG 10831cb0ef41Sopenharmony_ci s->compressed_len += 7; /* align on byte boundary */ 10841cb0ef41Sopenharmony_ci#endif 10851cb0ef41Sopenharmony_ci } 10861cb0ef41Sopenharmony_ci Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len >> 3, 10871cb0ef41Sopenharmony_ci s->compressed_len - 7*last)); 10881cb0ef41Sopenharmony_ci} 10891cb0ef41Sopenharmony_ci 10901cb0ef41Sopenharmony_ci/* =========================================================================== 10911cb0ef41Sopenharmony_ci * Save the match info and tally the frequency counts. Return true if 10921cb0ef41Sopenharmony_ci * the current block must be flushed. 10931cb0ef41Sopenharmony_ci */ 10941cb0ef41Sopenharmony_ciint ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) { 10951cb0ef41Sopenharmony_ci#ifdef LIT_MEM 10961cb0ef41Sopenharmony_ci s->d_buf[s->sym_next] = (ush)dist; 10971cb0ef41Sopenharmony_ci s->l_buf[s->sym_next++] = (uch)lc; 10981cb0ef41Sopenharmony_ci#else 10991cb0ef41Sopenharmony_ci s->sym_buf[s->sym_next++] = (uch)dist; 11001cb0ef41Sopenharmony_ci s->sym_buf[s->sym_next++] = (uch)(dist >> 8); 11011cb0ef41Sopenharmony_ci s->sym_buf[s->sym_next++] = (uch)lc; 11021cb0ef41Sopenharmony_ci#endif 11031cb0ef41Sopenharmony_ci if (dist == 0) { 11041cb0ef41Sopenharmony_ci /* lc is the unmatched char */ 11051cb0ef41Sopenharmony_ci s->dyn_ltree[lc].Freq++; 11061cb0ef41Sopenharmony_ci } else { 11071cb0ef41Sopenharmony_ci s->matches++; 11081cb0ef41Sopenharmony_ci /* Here, lc is the match length - MIN_MATCH */ 11091cb0ef41Sopenharmony_ci dist--; /* dist = match distance - 1 */ 11101cb0ef41Sopenharmony_ci Assert((ush)dist < (ush)MAX_DIST(s) && 11111cb0ef41Sopenharmony_ci (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 11121cb0ef41Sopenharmony_ci (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); 11131cb0ef41Sopenharmony_ci 11141cb0ef41Sopenharmony_ci s->dyn_ltree[_length_code[lc] + LITERALS + 1].Freq++; 11151cb0ef41Sopenharmony_ci s->dyn_dtree[d_code(dist)].Freq++; 11161cb0ef41Sopenharmony_ci } 11171cb0ef41Sopenharmony_ci return (s->sym_next == s->sym_end); 11181cb0ef41Sopenharmony_ci} 1119