< prev index next >

src/java.base/share/native/libzip/zlib/trees.c

Print this page

   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* trees.c -- output deflated data using Huffman coding
  26  * Copyright (C) 1995-2021 Jean-loup Gailly
  27  * detect_data_type() function provided freely by Cosmin Truta, 2006
  28  * For conditions of distribution and use, see copyright notice in zlib.h
  29  */
  30 
  31 /*
  32  *  ALGORITHM
  33  *
  34  *      The "deflation" process uses several Huffman trees. The more
  35  *      common source values are represented by shorter bit sequences.
  36  *
  37  *      Each code tree is stored in a compressed form which is itself
  38  * a Huffman encoding of the lengths of all the code strings (in
  39  * ascending order by source values).  The actual code strings are
  40  * reconstructed from the lengths in the inflate process, as described
  41  * in the deflate specification.
  42  *
  43  *  REFERENCES
  44  *
  45  *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  46  *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc

 129 /* length code for each normalized match length (0 == MIN_MATCH) */
 130 
 131 local int base_length[LENGTH_CODES];
 132 /* First normalized length for each code (0 = MIN_MATCH) */
 133 
 134 local int base_dist[D_CODES];
 135 /* First normalized distance for each code (0 = distance of 1) */
 136 
 137 #else
 138 #  include "trees.h"
 139 #endif /* GEN_TREES_H */
 140 
 141 struct static_tree_desc_s {
 142     const ct_data *static_tree;  /* static tree or NULL */
 143     const intf *extra_bits;      /* extra bits for each code or NULL */
 144     int     extra_base;          /* base index for extra_bits */
 145     int     elems;               /* max number of elements in the tree */
 146     int     max_length;          /* max bit length for the codes */
 147 };
 148 
 149 local const static_tree_desc  static_l_desc =






 150 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
 151 
 152 local const static_tree_desc  static_d_desc =
 153 {static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS};
 154 
 155 local const static_tree_desc  static_bl_desc =
 156 {(const ct_data *)0, extra_blbits, 0,   BL_CODES, MAX_BL_BITS};
 157 
 158 /* ===========================================================================
 159  * Local (static) routines in this file.











 160  */








 161 
 162 local void tr_static_init OF((void));
 163 local void init_block     OF((deflate_state *s));
 164 local void pqdownheap     OF((deflate_state *s, ct_data *tree, int k));
 165 local void gen_bitlen     OF((deflate_state *s, tree_desc *desc));
 166 local void gen_codes      OF((ct_data *tree, int max_code, ushf *bl_count));
 167 local void build_tree     OF((deflate_state *s, tree_desc *desc));
 168 local void scan_tree      OF((deflate_state *s, ct_data *tree, int max_code));
 169 local void send_tree      OF((deflate_state *s, ct_data *tree, int max_code));
 170 local int  build_bl_tree  OF((deflate_state *s));
 171 local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
 172                               int blcodes));
 173 local void compress_block OF((deflate_state *s, const ct_data *ltree,
 174                               const ct_data *dtree));
 175 local int  detect_data_type OF((deflate_state *s));
 176 local unsigned bi_reverse OF((unsigned code, int len));
 177 local void bi_windup      OF((deflate_state *s));
 178 local void bi_flush       OF((deflate_state *s));




















































 179 
 180 #ifdef GEN_TREES_H
 181 local void gen_trees_header OF((void));
 182 #endif
 183 
 184 #ifndef ZLIB_DEBUG
 185 #  define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
 186    /* Send a code of the given tree. c and tree must not have side effects */
 187 
 188 #else /* !ZLIB_DEBUG */
 189 #  define send_code(s, c, tree) \
 190      { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
 191        send_bits(s, tree[c].Code, tree[c].Len); }
 192 #endif
 193 
 194 /* ===========================================================================
 195  * Output a short LSB first on the stream.
 196  * IN assertion: there is enough room in pendingBuf.
 197  */
 198 #define put_short(s, w) { \
 199     put_byte(s, (uch)((w) & 0xff)); \
 200     put_byte(s, (uch)((ush)(w) >> 8)); \
 201 }
 202 
 203 /* ===========================================================================
 204  * Send a value on a given number of bits.
 205  * IN assertion: length <= 16 and value fits in length bits.
 206  */
 207 #ifdef ZLIB_DEBUG
 208 local void send_bits      OF((deflate_state *s, int value, int length));
 209 
 210 local void send_bits(s, value, length)
 211     deflate_state *s;
 212     int value;  /* value to send */
 213     int length; /* number of bits */
 214 {
 215     Tracevv((stderr," l %2d v %4x ", length, value));
 216     Assert(length > 0 && length <= 15, "invalid length");
 217     s->bits_sent += (ulg)length;
 218 
 219     /* If not enough room in bi_buf, use (valid) bits from bi_buf and
 220      * (16 - bi_valid) bits from value, leaving (width - (16 - bi_valid))
 221      * unused bits in value.
 222      */
 223     if (s->bi_valid > (int)Buf_size - length) {
 224         s->bi_buf |= (ush)value << s->bi_valid;
 225         put_short(s, s->bi_buf);
 226         s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
 227         s->bi_valid += length - Buf_size;
 228     } else {
 229         s->bi_buf |= (ush)value << s->bi_valid;
 230         s->bi_valid += length;
 231     }
 232 }
 233 #else /* !ZLIB_DEBUG */
 234 

 236 { int len = length;\
 237   if (s->bi_valid > (int)Buf_size - len) {\
 238     int val = (int)value;\
 239     s->bi_buf |= (ush)val << s->bi_valid;\
 240     put_short(s, s->bi_buf);\
 241     s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
 242     s->bi_valid += len - Buf_size;\
 243   } else {\
 244     s->bi_buf |= (ush)(value) << s->bi_valid;\
 245     s->bi_valid += len;\
 246   }\
 247 }
 248 #endif /* ZLIB_DEBUG */
 249 
 250 
 251 /* the arguments must not have side effects */
 252 
 253 /* ===========================================================================
 254  * Initialize the various 'constant' tables.
 255  */
 256 local void tr_static_init()
 257 {
 258 #if defined(GEN_TREES_H) || !defined(STDC)
 259     static int static_init_done = 0;
 260     int n;        /* iterates over tree elements */
 261     int bits;     /* bit counter */
 262     int length;   /* length value */
 263     int code;     /* code value */
 264     int dist;     /* distance index */
 265     ush bl_count[MAX_BITS+1];
 266     /* number of codes at each bit length for an optimal tree */
 267 
 268     if (static_init_done) return;
 269 
 270     /* For some embedded targets, global variables are not initialized: */
 271 #ifdef NO_INIT_GLOBAL_POINTERS
 272     static_l_desc.static_tree = static_ltree;
 273     static_l_desc.extra_bits = extra_lbits;
 274     static_d_desc.static_tree = static_dtree;
 275     static_d_desc.extra_bits = extra_dbits;
 276     static_bl_desc.extra_bits = extra_blbits;
 277 #endif

 330     static_init_done = 1;
 331 
 332 #  ifdef GEN_TREES_H
 333     gen_trees_header();
 334 #  endif
 335 #endif /* defined(GEN_TREES_H) || !defined(STDC) */
 336 }
 337 
 338 /* ===========================================================================
 339  * Generate the file trees.h describing the static trees.
 340  */
 341 #ifdef GEN_TREES_H
 342 #  ifndef ZLIB_DEBUG
 343 #    include <stdio.h>
 344 #  endif
 345 
 346 #  define SEPARATOR(i, last, width) \
 347       ((i) == (last)? "\n};\n\n" :    \
 348        ((i) % (width) == (width) - 1 ? ",\n" : ", "))
 349 
 350 void gen_trees_header()
 351 {
 352     FILE *header = fopen("trees.h", "w");
 353     int i;
 354 
 355     Assert (header != NULL, "Can't open trees.h");
 356     fprintf(header,
 357             "/* header created automatically with -DGEN_TREES_H */\n\n");
 358 
 359     fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
 360     for (i = 0; i < L_CODES+2; i++) {
 361         fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
 362                 static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
 363     }
 364 
 365     fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
 366     for (i = 0; i < D_CODES; i++) {
 367         fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
 368                 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
 369     }
 370 
 371     fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");

 380         fprintf(header, "%2u%s", _length_code[i],
 381                 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
 382     }
 383 
 384     fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
 385     for (i = 0; i < LENGTH_CODES; i++) {
 386         fprintf(header, "%1u%s", base_length[i],
 387                 SEPARATOR(i, LENGTH_CODES-1, 20));
 388     }
 389 
 390     fprintf(header, "local const int base_dist[D_CODES] = {\n");
 391     for (i = 0; i < D_CODES; i++) {
 392         fprintf(header, "%5u%s", base_dist[i],
 393                 SEPARATOR(i, D_CODES-1, 10));
 394     }
 395 
 396     fclose(header);
 397 }
 398 #endif /* GEN_TREES_H */
 399 
















 400 /* ===========================================================================
 401  * Initialize the tree data structures for a new zlib stream.
 402  */
 403 void ZLIB_INTERNAL _tr_init(s)
 404     deflate_state *s;
 405 {
 406     tr_static_init();
 407 
 408     s->l_desc.dyn_tree = s->dyn_ltree;
 409     s->l_desc.stat_desc = &static_l_desc;
 410 
 411     s->d_desc.dyn_tree = s->dyn_dtree;
 412     s->d_desc.stat_desc = &static_d_desc;
 413 
 414     s->bl_desc.dyn_tree = s->bl_tree;
 415     s->bl_desc.stat_desc = &static_bl_desc;
 416 
 417     s->bi_buf = 0;
 418     s->bi_valid = 0;
 419 #ifdef ZLIB_DEBUG
 420     s->compressed_len = 0L;
 421     s->bits_sent = 0L;
 422 #endif
 423 
 424     /* Initialize the first block of the first file: */
 425     init_block(s);
 426 }
 427 
 428 /* ===========================================================================
 429  * Initialize a new block.
 430  */
 431 local void init_block(s)
 432     deflate_state *s;
 433 {
 434     int n; /* iterates over tree elements */
 435 
 436     /* Initialize the trees. */
 437     for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
 438     for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
 439     for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
 440 
 441     s->dyn_ltree[END_BLOCK].Freq = 1;
 442     s->opt_len = s->static_len = 0L;
 443     s->sym_next = s->matches = 0;
 444 }
 445 
 446 #define SMALLEST 1
 447 /* Index within the heap array of least frequent node in the Huffman tree */
 448 
 449 
 450 /* ===========================================================================
 451  * Remove the smallest element from the heap and recreate the heap with
 452  * one less element. Updates heap and heap_len.
 453  */
 454 #define pqremove(s, tree, top) \
 455 {\
 456     top = s->heap[SMALLEST]; \
 457     s->heap[SMALLEST] = s->heap[s->heap_len--]; \
 458     pqdownheap(s, tree, SMALLEST); \
 459 }
 460 
 461 /* ===========================================================================
 462  * Compares to subtrees, using the tree depth as tie breaker when
 463  * the subtrees have equal frequency. This minimizes the worst case length.
 464  */
 465 #define smaller(tree, n, m, depth) \
 466    (tree[n].Freq < tree[m].Freq || \
 467    (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
 468 
 469 /* ===========================================================================
 470  * Restore the heap property by moving down the tree starting at node k,
 471  * exchanging a node with the smallest of its two sons if necessary, stopping
 472  * when the heap property is re-established (each father smaller than its
 473  * two sons).
 474  */
 475 local void pqdownheap(s, tree, k)
 476     deflate_state *s;
 477     ct_data *tree;  /* the tree to restore */
 478     int k;               /* node to move down */
 479 {
 480     int v = s->heap[k];
 481     int j = k << 1;  /* left son of k */
 482     while (j <= s->heap_len) {
 483         /* Set j to the smallest of the two sons: */
 484         if (j < s->heap_len &&
 485             smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) {
 486             j++;
 487         }
 488         /* Exit if v is smaller than both sons */
 489         if (smaller(tree, v, s->heap[j], s->depth)) break;
 490 
 491         /* Exchange v with the smallest son */
 492         s->heap[k] = s->heap[j];  k = j;
 493 
 494         /* And continue down the tree, setting j to the left son of k */
 495         j <<= 1;
 496     }
 497     s->heap[k] = v;
 498 }
 499 
 500 /* ===========================================================================
 501  * Compute the optimal bit lengths for a tree and update the total bit length
 502  * for the current block.
 503  * IN assertion: the fields freq and dad are set, heap[heap_max] and
 504  *    above are the tree nodes sorted by increasing frequency.
 505  * OUT assertions: the field len is set to the optimal bit length, the
 506  *     array bl_count contains the frequencies for each bit length.
 507  *     The length opt_len is updated; static_len is also updated if stree is
 508  *     not null.
 509  */
 510 local void gen_bitlen(s, desc)
 511     deflate_state *s;
 512     tree_desc *desc;    /* the tree descriptor */
 513 {
 514     ct_data *tree        = desc->dyn_tree;
 515     int max_code         = desc->max_code;
 516     const ct_data *stree = desc->stat_desc->static_tree;
 517     const intf *extra    = desc->stat_desc->extra_bits;
 518     int base             = desc->stat_desc->extra_base;
 519     int max_length       = desc->stat_desc->max_length;
 520     int h;              /* heap index */
 521     int n, m;           /* iterate over the tree elements */
 522     int bits;           /* bit length */
 523     int xbits;          /* extra bits */
 524     ush f;              /* frequency */
 525     int overflow = 0;   /* number of elements with bit length too large */
 526 
 527     for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
 528 
 529     /* In a first pass, compute the optimal bit lengths (which may
 530      * overflow in the case of the bit length tree).
 531      */
 532     tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
 533 

 568     /* Now recompute all bit lengths, scanning in increasing frequency.
 569      * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
 570      * lengths instead of fixing only the wrong ones. This idea is taken
 571      * from 'ar' written by Haruhiko Okumura.)
 572      */
 573     for (bits = max_length; bits != 0; bits--) {
 574         n = s->bl_count[bits];
 575         while (n != 0) {
 576             m = s->heap[--h];
 577             if (m > max_code) continue;
 578             if ((unsigned) tree[m].Len != (unsigned) bits) {
 579                 Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
 580                 s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq;
 581                 tree[m].Len = (ush)bits;
 582             }
 583             n--;
 584         }
 585     }
 586 }
 587 
 588 /* ===========================================================================
 589  * Generate the codes for a given tree and bit counts (which need not be
 590  * optimal).
 591  * IN assertion: the array bl_count contains the bit length statistics for
 592  * the given tree and the field len is set for all tree elements.
 593  * OUT assertion: the field code is set for all tree elements of non
 594  *     zero code length.
 595  */
 596 local void gen_codes(tree, max_code, bl_count)
 597     ct_data *tree;             /* the tree to decorate */
 598     int max_code;              /* largest code with non zero frequency */
 599     ushf *bl_count;            /* number of codes at each bit length */
 600 {
 601     ush next_code[MAX_BITS+1]; /* next code value for each bit length */
 602     unsigned code = 0;         /* running code value */
 603     int bits;                  /* bit index */
 604     int n;                     /* code index */
 605 
 606     /* The distribution counts are first used to generate the code values
 607      * without bit reversal.
 608      */
 609     for (bits = 1; bits <= MAX_BITS; bits++) {
 610         code = (code + bl_count[bits - 1]) << 1;
 611         next_code[bits] = (ush)code;
 612     }
 613     /* Check that the bit counts in bl_count are consistent. The last code
 614      * must be all ones.
 615      */
 616     Assert (code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1,
 617             "inconsistent bit counts");
 618     Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
 619 
 620     for (n = 0;  n <= max_code; n++) {
 621         int len = tree[n].Len;
 622         if (len == 0) continue;
 623         /* Now reverse the bits */
 624         tree[n].Code = (ush)bi_reverse(next_code[len]++, len);
 625 
 626         Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
 627             n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1));
 628     }
 629 }
 630 
 631 /* ===========================================================================
 632  * Construct one Huffman tree and assigns the code bit strings and lengths.
 633  * Update the total bit length for the current block.
 634  * IN assertion: the field freq is set for all tree elements.
 635  * OUT assertions: the fields len and code are set to the optimal bit length
 636  *     and corresponding code. The length opt_len is updated; static_len is
 637  *     also updated if stree is not null. The field max_code is set.
 638  */
 639 local void build_tree(s, desc)
 640     deflate_state *s;
 641     tree_desc *desc; /* the tree descriptor */
 642 {
 643     ct_data *tree         = desc->dyn_tree;
 644     const ct_data *stree  = desc->stat_desc->static_tree;
 645     int elems             = desc->stat_desc->elems;
 646     int n, m;          /* iterate over heap elements */
 647     int max_code = -1; /* largest code with non zero frequency */
 648     int node;          /* new node being created */
 649 
 650     /* Construct the initial heap, with least frequent element in
 651      * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n + 1].
 652      * heap[0] is not used.
 653      */
 654     s->heap_len = 0, s->heap_max = HEAP_SIZE;
 655 
 656     for (n = 0; n < elems; n++) {
 657         if (tree[n].Freq != 0) {
 658             s->heap[++(s->heap_len)] = max_code = n;
 659             s->depth[n] = 0;
 660         } else {
 661             tree[n].Len = 0;
 662         }

 707         s->heap[SMALLEST] = node++;
 708         pqdownheap(s, tree, SMALLEST);
 709 
 710     } while (s->heap_len >= 2);
 711 
 712     s->heap[--(s->heap_max)] = s->heap[SMALLEST];
 713 
 714     /* At this point, the fields freq and dad are set. We can now
 715      * generate the bit lengths.
 716      */
 717     gen_bitlen(s, (tree_desc *)desc);
 718 
 719     /* The field len is now set, we can generate the bit codes */
 720     gen_codes ((ct_data *)tree, max_code, s->bl_count);
 721 }
 722 
 723 /* ===========================================================================
 724  * Scan a literal or distance tree to determine the frequencies of the codes
 725  * in the bit length tree.
 726  */
 727 local void scan_tree(s, tree, max_code)
 728     deflate_state *s;
 729     ct_data *tree;   /* the tree to be scanned */
 730     int max_code;    /* and its largest code of non zero frequency */
 731 {
 732     int n;                     /* iterates over all tree elements */
 733     int prevlen = -1;          /* last emitted length */
 734     int curlen;                /* length of current code */
 735     int nextlen = tree[0].Len; /* length of next code */
 736     int count = 0;             /* repeat count of the current code */
 737     int max_count = 7;         /* max repeat count */
 738     int min_count = 4;         /* min repeat count */
 739 
 740     if (nextlen == 0) max_count = 138, min_count = 3;
 741     tree[max_code + 1].Len = (ush)0xffff; /* guard */
 742 
 743     for (n = 0; n <= max_code; n++) {
 744         curlen = nextlen; nextlen = tree[n + 1].Len;
 745         if (++count < max_count && curlen == nextlen) {
 746             continue;
 747         } else if (count < min_count) {
 748             s->bl_tree[curlen].Freq += count;
 749         } else if (curlen != 0) {
 750             if (curlen != prevlen) s->bl_tree[curlen].Freq++;
 751             s->bl_tree[REP_3_6].Freq++;
 752         } else if (count <= 10) {
 753             s->bl_tree[REPZ_3_10].Freq++;
 754         } else {
 755             s->bl_tree[REPZ_11_138].Freq++;
 756         }
 757         count = 0; prevlen = curlen;
 758         if (nextlen == 0) {
 759             max_count = 138, min_count = 3;
 760         } else if (curlen == nextlen) {
 761             max_count = 6, min_count = 3;
 762         } else {
 763             max_count = 7, min_count = 4;
 764         }
 765     }
 766 }
 767 
 768 /* ===========================================================================
 769  * Send a literal or distance tree in compressed form, using the codes in
 770  * bl_tree.
 771  */
 772 local void send_tree(s, tree, max_code)
 773     deflate_state *s;
 774     ct_data *tree; /* the tree to be scanned */
 775     int max_code;       /* and its largest code of non zero frequency */
 776 {
 777     int n;                     /* iterates over all tree elements */
 778     int prevlen = -1;          /* last emitted length */
 779     int curlen;                /* length of current code */
 780     int nextlen = tree[0].Len; /* length of next code */
 781     int count = 0;             /* repeat count of the current code */
 782     int max_count = 7;         /* max repeat count */
 783     int min_count = 4;         /* min repeat count */
 784 
 785     /* tree[max_code + 1].Len = -1; */  /* guard already set */
 786     if (nextlen == 0) max_count = 138, min_count = 3;
 787 
 788     for (n = 0; n <= max_code; n++) {
 789         curlen = nextlen; nextlen = tree[n + 1].Len;
 790         if (++count < max_count && curlen == nextlen) {
 791             continue;
 792         } else if (count < min_count) {
 793             do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
 794 
 795         } else if (curlen != 0) {
 796             if (curlen != prevlen) {

 803             send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count - 3, 3);
 804 
 805         } else {
 806             send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count - 11, 7);
 807         }
 808         count = 0; prevlen = curlen;
 809         if (nextlen == 0) {
 810             max_count = 138, min_count = 3;
 811         } else if (curlen == nextlen) {
 812             max_count = 6, min_count = 3;
 813         } else {
 814             max_count = 7, min_count = 4;
 815         }
 816     }
 817 }
 818 
 819 /* ===========================================================================
 820  * Construct the Huffman tree for the bit lengths and return the index in
 821  * bl_order of the last bit length code to send.
 822  */
 823 local int build_bl_tree(s)
 824     deflate_state *s;
 825 {
 826     int max_blindex;  /* index of last bit length code of non zero freq */
 827 
 828     /* Determine the bit length frequencies for literal and distance trees */
 829     scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
 830     scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
 831 
 832     /* Build the bit length tree: */
 833     build_tree(s, (tree_desc *)(&(s->bl_desc)));
 834     /* opt_len now includes the length of the tree representations, except the
 835      * lengths of the bit lengths codes and the 5 + 5 + 4 bits for the counts.
 836      */
 837 
 838     /* Determine the number of bit length codes to send. The pkzip format
 839      * requires that at least 4 bit length codes be sent. (appnote.txt says
 840      * 3 but the actual value used is 4.)
 841      */
 842     for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
 843         if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
 844     }
 845     /* Update opt_len to include the bit length tree and counts */
 846     s->opt_len += 3*((ulg)max_blindex + 1) + 5 + 5 + 4;
 847     Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
 848             s->opt_len, s->static_len));
 849 
 850     return max_blindex;
 851 }
 852 
 853 /* ===========================================================================
 854  * Send the header for a block using dynamic Huffman trees: the counts, the
 855  * lengths of the bit length codes, the literal tree and the distance tree.
 856  * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
 857  */
 858 local void send_all_trees(s, lcodes, dcodes, blcodes)
 859     deflate_state *s;
 860     int lcodes, dcodes, blcodes; /* number of codes for each tree */
 861 {
 862     int rank;                    /* index in bl_order */
 863 
 864     Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
 865     Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
 866             "too many codes");
 867     Tracev((stderr, "\nbl counts: "));
 868     send_bits(s, lcodes - 257, 5);  /* not +255 as stated in appnote.txt */
 869     send_bits(s, dcodes - 1,   5);
 870     send_bits(s, blcodes - 4,  4);  /* not -3 as stated in appnote.txt */
 871     for (rank = 0; rank < blcodes; rank++) {
 872         Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
 873         send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
 874     }
 875     Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
 876 
 877     send_tree(s, (ct_data *)s->dyn_ltree, lcodes - 1);  /* literal tree */
 878     Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
 879 
 880     send_tree(s, (ct_data *)s->dyn_dtree, dcodes - 1);  /* distance tree */
 881     Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
 882 }
 883 
 884 /* ===========================================================================
 885  * Send a stored block
 886  */
 887 void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
 888     deflate_state *s;
 889     charf *buf;       /* input block */
 890     ulg stored_len;   /* length of input block */
 891     int last;         /* one if this is the last block for a file */
 892 {
 893     send_bits(s, (STORED_BLOCK<<1) + last, 3);  /* send block type */
 894     bi_windup(s);        /* align on byte boundary */
 895     put_short(s, (ush)stored_len);
 896     put_short(s, (ush)~stored_len);
 897     if (stored_len)
 898         zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len);
 899     s->pending += stored_len;
 900 #ifdef ZLIB_DEBUG
 901     s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
 902     s->compressed_len += (stored_len + 4) << 3;
 903     s->bits_sent += 2*16;
 904     s->bits_sent += stored_len << 3;
 905 #endif
 906 }
 907 
 908 /* ===========================================================================
 909  * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
 910  */
 911 void ZLIB_INTERNAL _tr_flush_bits(s)
 912     deflate_state *s;
 913 {
 914     bi_flush(s);
 915 }
 916 
 917 /* ===========================================================================
 918  * Send one empty static block to give enough lookahead for inflate.
 919  * This takes 10 bits, of which 7 may remain in the bit buffer.
 920  */
 921 void ZLIB_INTERNAL _tr_align(s)
 922     deflate_state *s;
 923 {
 924     send_bits(s, STATIC_TREES<<1, 3);
 925     send_code(s, END_BLOCK, static_ltree);
 926 #ifdef ZLIB_DEBUG
 927     s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
 928 #endif
 929     bi_flush(s);
 930 }
 931 
































































































 932 /* ===========================================================================
 933  * Determine the best encoding for the current block: dynamic trees, static
 934  * trees or store, and write out the encoded block.
 935  */
 936 void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
 937     deflate_state *s;
 938     charf *buf;       /* input block, or NULL if too old */
 939     ulg stored_len;   /* length of input block */
 940     int last;         /* one if this is the last block for a file */
 941 {
 942     ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
 943     int max_blindex = 0;  /* index of last bit length code of non zero freq */
 944 
 945     /* Build the Huffman trees unless a stored block is forced */
 946     if (s->level > 0) {
 947 
 948         /* Check if the file is binary or text */
 949         if (s->strm->data_type == Z_UNKNOWN)
 950             s->strm->data_type = detect_data_type(s);
 951 
 952         /* Construct the literal and distance trees */
 953         build_tree(s, (tree_desc *)(&(s->l_desc)));
 954         Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
 955                 s->static_len));
 956 
 957         build_tree(s, (tree_desc *)(&(s->d_desc)));
 958         Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
 959                 s->static_len));
 960         /* At this point, opt_len and static_len are the total bit lengths of
 961          * the compressed block data, excluding the tree representations.

1018     Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1019     /* The above check is made mod 2^32, for files larger than 512 MB
1020      * and uLong implemented on 32 bits.
1021      */
1022     init_block(s);
1023 
1024     if (last) {
1025         bi_windup(s);
1026 #ifdef ZLIB_DEBUG
1027         s->compressed_len += 7;  /* align on byte boundary */
1028 #endif
1029     }
1030     Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
1031            s->compressed_len - 7*last));
1032 }
1033 
1034 /* ===========================================================================
1035  * Save the match info and tally the frequency counts. Return true if
1036  * the current block must be flushed.
1037  */
1038 int ZLIB_INTERNAL _tr_tally(s, dist, lc)
1039     deflate_state *s;
1040     unsigned dist;  /* distance of matched string */
1041     unsigned lc;    /* match length - MIN_MATCH or unmatched char (dist==0) */
1042 {
1043     s->sym_buf[s->sym_next++] = (uch)dist;
1044     s->sym_buf[s->sym_next++] = (uch)(dist >> 8);
1045     s->sym_buf[s->sym_next++] = (uch)lc;

1046     if (dist == 0) {
1047         /* lc is the unmatched char */
1048         s->dyn_ltree[lc].Freq++;
1049     } else {
1050         s->matches++;
1051         /* Here, lc is the match length - MIN_MATCH */
1052         dist--;             /* dist = match distance - 1 */
1053         Assert((ush)dist < (ush)MAX_DIST(s) &&
1054                (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1055                (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");
1056 
1057         s->dyn_ltree[_length_code[lc] + LITERALS + 1].Freq++;
1058         s->dyn_dtree[d_code(dist)].Freq++;
1059     }
1060     return (s->sym_next == s->sym_end);
1061 }
1062 
1063 /* ===========================================================================
1064  * Send the block data compressed using the given Huffman trees
1065  */
1066 local void compress_block(s, ltree, dtree)
1067     deflate_state *s;
1068     const ct_data *ltree; /* literal tree */
1069     const ct_data *dtree; /* distance tree */
1070 {
1071     unsigned dist;      /* distance of matched string */
1072     int lc;             /* match length or unmatched char (if dist == 0) */
1073     unsigned sx = 0;    /* running index in sym_buf */
1074     unsigned code;      /* the code to send */
1075     int extra;          /* number of extra bits to send */
1076 
1077     if (s->sym_next != 0) do {
1078         dist = s->sym_buf[sx++] & 0xff;
1079         dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
1080         lc = s->sym_buf[sx++];
1081         if (dist == 0) {
1082             send_code(s, lc, ltree); /* send a literal byte */
1083             Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1084         } else {
1085             /* Here, lc is the match length - MIN_MATCH */
1086             code = _length_code[lc];
1087             send_code(s, code + LITERALS + 1, ltree);   /* send length code */
1088             extra = extra_lbits[code];
1089             if (extra != 0) {
1090                 lc -= base_length[code];
1091                 send_bits(s, lc, extra);       /* send the extra length bits */
1092             }
1093             dist--; /* dist is now the match distance - 1 */
1094             code = d_code(dist);
1095             Assert (code < D_CODES, "bad d_code");
1096 
1097             send_code(s, code, dtree);       /* send the distance code */
1098             extra = extra_dbits[code];
1099             if (extra != 0) {
1100                 dist -= (unsigned)base_dist[code];
1101                 send_bits(s, dist, extra);   /* send the extra distance bits */
1102             }
1103         } /* literal or match pair ? */
1104 
1105         /* Check that the overlay between pending_buf and sym_buf is ok: */
1106         Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
1107 
1108     } while (sx < s->sym_next);
1109 
1110     send_code(s, END_BLOCK, ltree);
1111 }
1112 
1113 /* ===========================================================================
1114  * Check if the data type is TEXT or BINARY, using the following algorithm:
1115  * - TEXT if the two conditions below are satisfied:
1116  *    a) There are no non-portable control characters belonging to the
1117  *       "block list" (0..6, 14..25, 28..31).
1118  *    b) There is at least one printable character belonging to the
1119  *       "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
1120  * - BINARY otherwise.
1121  * - The following partially-portable control characters form a
1122  *   "gray list" that is ignored in this detection algorithm:
1123  *   (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
1124  * IN assertion: the fields Freq of dyn_ltree are set.
1125  */
1126 local int detect_data_type(s)
1127     deflate_state *s;
1128 {
1129     /* block_mask is the bit mask of block-listed bytes
1130      * set bits 0..6, 14..25, and 28..31
1131      * 0xf3ffc07f = binary 11110011111111111100000001111111
1132      */
1133     unsigned long block_mask = 0xf3ffc07fUL;
1134     int n;
1135 
1136     /* Check for non-textual ("block-listed") bytes. */
1137     for (n = 0; n <= 31; n++, block_mask >>= 1)
1138         if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1139             return Z_BINARY;
1140 
1141     /* Check for textual ("allow-listed") bytes. */
1142     if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1143             || s->dyn_ltree[13].Freq != 0)
1144         return Z_TEXT;
1145     for (n = 32; n < LITERALS; n++)
1146         if (s->dyn_ltree[n].Freq != 0)
1147             return Z_TEXT;
1148 
1149     /* There are no "block-listed" or "allow-listed" bytes:
1150      * this stream either is empty or has tolerated ("gray-listed") bytes only.
1151      */
1152     return Z_BINARY;
1153 }
1154 
1155 /* ===========================================================================
1156  * Reverse the first len bits of a code, using straightforward code (a faster
1157  * method would use a table)
1158  * IN assertion: 1 <= len <= 15
1159  */
1160 local unsigned bi_reverse(code, len)
1161     unsigned code; /* the value to invert */
1162     int len;       /* its bit length */
1163 {
1164     register unsigned res = 0;
1165     do {
1166         res |= code & 1;
1167         code >>= 1, res <<= 1;
1168     } while (--len > 0);
1169     return res >> 1;
1170 }
1171 
1172 /* ===========================================================================
1173  * Flush the bit buffer, keeping at most 7 bits in it.
1174  */
1175 local void bi_flush(s)
1176     deflate_state *s;
1177 {
1178     if (s->bi_valid == 16) {
1179         put_short(s, s->bi_buf);
1180         s->bi_buf = 0;
1181         s->bi_valid = 0;
1182     } else if (s->bi_valid >= 8) {
1183         put_byte(s, (Byte)s->bi_buf);
1184         s->bi_buf >>= 8;
1185         s->bi_valid -= 8;
1186     }
1187 }
1188 
1189 /* ===========================================================================
1190  * Flush the bit buffer and align the output on a byte boundary
1191  */
1192 local void bi_windup(s)
1193     deflate_state *s;
1194 {
1195     if (s->bi_valid > 8) {
1196         put_short(s, s->bi_buf);
1197     } else if (s->bi_valid > 0) {
1198         put_byte(s, (Byte)s->bi_buf);
1199     }
1200     s->bi_buf = 0;
1201     s->bi_valid = 0;
1202 #ifdef ZLIB_DEBUG
1203     s->bits_sent = (s->bits_sent + 7) & ~7;
1204 #endif
1205 }

   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* trees.c -- output deflated data using Huffman coding
  26  * Copyright (C) 1995-2024 Jean-loup Gailly
  27  * detect_data_type() function provided freely by Cosmin Truta, 2006
  28  * For conditions of distribution and use, see copyright notice in zlib.h
  29  */
  30 
  31 /*
  32  *  ALGORITHM
  33  *
  34  *      The "deflation" process uses several Huffman trees. The more
  35  *      common source values are represented by shorter bit sequences.
  36  *
  37  *      Each code tree is stored in a compressed form which is itself
  38  * a Huffman encoding of the lengths of all the code strings (in
  39  * ascending order by source values).  The actual code strings are
  40  * reconstructed from the lengths in the inflate process, as described
  41  * in the deflate specification.
  42  *
  43  *  REFERENCES
  44  *
  45  *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  46  *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc

 129 /* length code for each normalized match length (0 == MIN_MATCH) */
 130 
 131 local int base_length[LENGTH_CODES];
 132 /* First normalized length for each code (0 = MIN_MATCH) */
 133 
 134 local int base_dist[D_CODES];
 135 /* First normalized distance for each code (0 = distance of 1) */
 136 
 137 #else
 138 #  include "trees.h"
 139 #endif /* GEN_TREES_H */
 140 
 141 struct static_tree_desc_s {
 142     const ct_data *static_tree;  /* static tree or NULL */
 143     const intf *extra_bits;      /* extra bits for each code or NULL */
 144     int     extra_base;          /* base index for extra_bits */
 145     int     elems;               /* max number of elements in the tree */
 146     int     max_length;          /* max bit length for the codes */
 147 };
 148 
 149 #ifdef NO_INIT_GLOBAL_POINTERS
 150 #  define TCONST
 151 #else
 152 #  define TCONST const
 153 #endif
 154 
 155 local TCONST static_tree_desc static_l_desc =
 156 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
 157 
 158 local TCONST static_tree_desc static_d_desc =
 159 {static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS};
 160 
 161 local TCONST static_tree_desc static_bl_desc =
 162 {(const ct_data *)0, extra_blbits, 0,   BL_CODES, MAX_BL_BITS};
 163 
 164 /* ===========================================================================
 165  * Output a short LSB first on the stream.
 166  * IN assertion: there is enough room in pendingBuf.
 167  */
 168 #define put_short(s, w) { \
 169     put_byte(s, (uch)((w) & 0xff)); \
 170     put_byte(s, (uch)((ush)(w) >> 8)); \
 171 }
 172 
 173 /* ===========================================================================
 174  * Reverse the first len bits of a code, using straightforward code (a faster
 175  * method would use a table)
 176  * IN assertion: 1 <= len <= 15
 177  */
 178 local unsigned bi_reverse(unsigned code, int len) {
 179     register unsigned res = 0;
 180     do {
 181         res |= code & 1;
 182         code >>= 1, res <<= 1;
 183     } while (--len > 0);
 184     return res >> 1;
 185 }
 186 
 187 /* ===========================================================================
 188  * Flush the bit buffer, keeping at most 7 bits in it.
 189  */
 190 local void bi_flush(deflate_state *s) {
 191     if (s->bi_valid == 16) {
 192         put_short(s, s->bi_buf);
 193         s->bi_buf = 0;
 194         s->bi_valid = 0;
 195     } else if (s->bi_valid >= 8) {
 196         put_byte(s, (Byte)s->bi_buf);
 197         s->bi_buf >>= 8;
 198         s->bi_valid -= 8;
 199     }
 200 }
 201 
 202 /* ===========================================================================
 203  * Flush the bit buffer and align the output on a byte boundary
 204  */
 205 local void bi_windup(deflate_state *s) {
 206     if (s->bi_valid > 8) {
 207         put_short(s, s->bi_buf);
 208     } else if (s->bi_valid > 0) {
 209         put_byte(s, (Byte)s->bi_buf);
 210     }
 211     s->bi_buf = 0;
 212     s->bi_valid = 0;
 213 #ifdef ZLIB_DEBUG
 214     s->bits_sent = (s->bits_sent + 7) & ~7;
 215 #endif
 216 }
 217 
 218 /* ===========================================================================
 219  * Generate the codes for a given tree and bit counts (which need not be
 220  * optimal).
 221  * IN assertion: the array bl_count contains the bit length statistics for
 222  * the given tree and the field len is set for all tree elements.
 223  * OUT assertion: the field code is set for all tree elements of non
 224  *     zero code length.
 225  */
 226 local void gen_codes(ct_data *tree, int max_code, ushf *bl_count) {
 227     ush next_code[MAX_BITS+1]; /* next code value for each bit length */
 228     unsigned code = 0;         /* running code value */
 229     int bits;                  /* bit index */
 230     int n;                     /* code index */
 231 
 232     /* The distribution counts are first used to generate the code values
 233      * without bit reversal.
 234      */
 235     for (bits = 1; bits <= MAX_BITS; bits++) {
 236         code = (code + bl_count[bits - 1]) << 1;
 237         next_code[bits] = (ush)code;
 238     }
 239     /* Check that the bit counts in bl_count are consistent. The last code
 240      * must be all ones.
 241      */
 242     Assert (code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1,
 243             "inconsistent bit counts");
 244     Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
 245 
 246     for (n = 0;  n <= max_code; n++) {
 247         int len = tree[n].Len;
 248         if (len == 0) continue;
 249         /* Now reverse the bits */
 250         tree[n].Code = (ush)bi_reverse(next_code[len]++, len);
 251 
 252         Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
 253             n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1));
 254     }
 255 }
 256 
 257 #ifdef GEN_TREES_H
 258 local void gen_trees_header(void);
 259 #endif
 260 
 261 #ifndef ZLIB_DEBUG
 262 #  define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
 263    /* Send a code of the given tree. c and tree must not have side effects */
 264 
 265 #else /* !ZLIB_DEBUG */
 266 #  define send_code(s, c, tree) \
 267      { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
 268        send_bits(s, tree[c].Code, tree[c].Len); }
 269 #endif
 270 









 271 /* ===========================================================================
 272  * Send a value on a given number of bits.
 273  * IN assertion: length <= 16 and value fits in length bits.
 274  */
 275 #ifdef ZLIB_DEBUG
 276 local void send_bits(deflate_state *s, int value, int length) {






 277     Tracevv((stderr," l %2d v %4x ", length, value));
 278     Assert(length > 0 && length <= 15, "invalid length");
 279     s->bits_sent += (ulg)length;
 280 
 281     /* If not enough room in bi_buf, use (valid) bits from bi_buf and
 282      * (16 - bi_valid) bits from value, leaving (width - (16 - bi_valid))
 283      * unused bits in value.
 284      */
 285     if (s->bi_valid > (int)Buf_size - length) {
 286         s->bi_buf |= (ush)value << s->bi_valid;
 287         put_short(s, s->bi_buf);
 288         s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
 289         s->bi_valid += length - Buf_size;
 290     } else {
 291         s->bi_buf |= (ush)value << s->bi_valid;
 292         s->bi_valid += length;
 293     }
 294 }
 295 #else /* !ZLIB_DEBUG */
 296 

 298 { int len = length;\
 299   if (s->bi_valid > (int)Buf_size - len) {\
 300     int val = (int)value;\
 301     s->bi_buf |= (ush)val << s->bi_valid;\
 302     put_short(s, s->bi_buf);\
 303     s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
 304     s->bi_valid += len - Buf_size;\
 305   } else {\
 306     s->bi_buf |= (ush)(value) << s->bi_valid;\
 307     s->bi_valid += len;\
 308   }\
 309 }
 310 #endif /* ZLIB_DEBUG */
 311 
 312 
 313 /* the arguments must not have side effects */
 314 
 315 /* ===========================================================================
 316  * Initialize the various 'constant' tables.
 317  */
 318 local void tr_static_init(void) {

 319 #if defined(GEN_TREES_H) || !defined(STDC)
 320     static int static_init_done = 0;
 321     int n;        /* iterates over tree elements */
 322     int bits;     /* bit counter */
 323     int length;   /* length value */
 324     int code;     /* code value */
 325     int dist;     /* distance index */
 326     ush bl_count[MAX_BITS+1];
 327     /* number of codes at each bit length for an optimal tree */
 328 
 329     if (static_init_done) return;
 330 
 331     /* For some embedded targets, global variables are not initialized: */
 332 #ifdef NO_INIT_GLOBAL_POINTERS
 333     static_l_desc.static_tree = static_ltree;
 334     static_l_desc.extra_bits = extra_lbits;
 335     static_d_desc.static_tree = static_dtree;
 336     static_d_desc.extra_bits = extra_dbits;
 337     static_bl_desc.extra_bits = extra_blbits;
 338 #endif

 391     static_init_done = 1;
 392 
 393 #  ifdef GEN_TREES_H
 394     gen_trees_header();
 395 #  endif
 396 #endif /* defined(GEN_TREES_H) || !defined(STDC) */
 397 }
 398 
 399 /* ===========================================================================
 400  * Generate the file trees.h describing the static trees.
 401  */
 402 #ifdef GEN_TREES_H
 403 #  ifndef ZLIB_DEBUG
 404 #    include <stdio.h>
 405 #  endif
 406 
 407 #  define SEPARATOR(i, last, width) \
 408       ((i) == (last)? "\n};\n\n" :    \
 409        ((i) % (width) == (width) - 1 ? ",\n" : ", "))
 410 
 411 void gen_trees_header(void) {

 412     FILE *header = fopen("trees.h", "w");
 413     int i;
 414 
 415     Assert (header != NULL, "Can't open trees.h");
 416     fprintf(header,
 417             "/* header created automatically with -DGEN_TREES_H */\n\n");
 418 
 419     fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
 420     for (i = 0; i < L_CODES+2; i++) {
 421         fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
 422                 static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
 423     }
 424 
 425     fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
 426     for (i = 0; i < D_CODES; i++) {
 427         fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
 428                 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
 429     }
 430 
 431     fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");

 440         fprintf(header, "%2u%s", _length_code[i],
 441                 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
 442     }
 443 
 444     fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
 445     for (i = 0; i < LENGTH_CODES; i++) {
 446         fprintf(header, "%1u%s", base_length[i],
 447                 SEPARATOR(i, LENGTH_CODES-1, 20));
 448     }
 449 
 450     fprintf(header, "local const int base_dist[D_CODES] = {\n");
 451     for (i = 0; i < D_CODES; i++) {
 452         fprintf(header, "%5u%s", base_dist[i],
 453                 SEPARATOR(i, D_CODES-1, 10));
 454     }
 455 
 456     fclose(header);
 457 }
 458 #endif /* GEN_TREES_H */
 459 
 460 /* ===========================================================================
 461  * Initialize a new block.
 462  */
 463 local void init_block(deflate_state *s) {
 464     int n; /* iterates over tree elements */
 465 
 466     /* Initialize the trees. */
 467     for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
 468     for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
 469     for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
 470 
 471     s->dyn_ltree[END_BLOCK].Freq = 1;
 472     s->opt_len = s->static_len = 0L;
 473     s->sym_next = s->matches = 0;
 474 }
 475 
 476 /* ===========================================================================
 477  * Initialize the tree data structures for a new zlib stream.
 478  */
 479 void ZLIB_INTERNAL _tr_init(deflate_state *s) {


 480     tr_static_init();
 481 
 482     s->l_desc.dyn_tree = s->dyn_ltree;
 483     s->l_desc.stat_desc = &static_l_desc;
 484 
 485     s->d_desc.dyn_tree = s->dyn_dtree;
 486     s->d_desc.stat_desc = &static_d_desc;
 487 
 488     s->bl_desc.dyn_tree = s->bl_tree;
 489     s->bl_desc.stat_desc = &static_bl_desc;
 490 
 491     s->bi_buf = 0;
 492     s->bi_valid = 0;
 493 #ifdef ZLIB_DEBUG
 494     s->compressed_len = 0L;
 495     s->bits_sent = 0L;
 496 #endif
 497 
 498     /* Initialize the first block of the first file: */
 499     init_block(s);
 500 }
 501 


















 502 #define SMALLEST 1
 503 /* Index within the heap array of least frequent node in the Huffman tree */
 504 
 505 
 506 /* ===========================================================================
 507  * Remove the smallest element from the heap and recreate the heap with
 508  * one less element. Updates heap and heap_len.
 509  */
 510 #define pqremove(s, tree, top) \
 511 {\
 512     top = s->heap[SMALLEST]; \
 513     s->heap[SMALLEST] = s->heap[s->heap_len--]; \
 514     pqdownheap(s, tree, SMALLEST); \
 515 }
 516 
 517 /* ===========================================================================
 518  * Compares to subtrees, using the tree depth as tie breaker when
 519  * the subtrees have equal frequency. This minimizes the worst case length.
 520  */
 521 #define smaller(tree, n, m, depth) \
 522    (tree[n].Freq < tree[m].Freq || \
 523    (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
 524 
 525 /* ===========================================================================
 526  * Restore the heap property by moving down the tree starting at node k,
 527  * exchanging a node with the smallest of its two sons if necessary, stopping
 528  * when the heap property is re-established (each father smaller than its
 529  * two sons).
 530  */
 531 local void pqdownheap(deflate_state *s, ct_data *tree, int k) {




 532     int v = s->heap[k];
 533     int j = k << 1;  /* left son of k */
 534     while (j <= s->heap_len) {
 535         /* Set j to the smallest of the two sons: */
 536         if (j < s->heap_len &&
 537             smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) {
 538             j++;
 539         }
 540         /* Exit if v is smaller than both sons */
 541         if (smaller(tree, v, s->heap[j], s->depth)) break;
 542 
 543         /* Exchange v with the smallest son */
 544         s->heap[k] = s->heap[j];  k = j;
 545 
 546         /* And continue down the tree, setting j to the left son of k */
 547         j <<= 1;
 548     }
 549     s->heap[k] = v;
 550 }
 551 
 552 /* ===========================================================================
 553  * Compute the optimal bit lengths for a tree and update the total bit length
 554  * for the current block.
 555  * IN assertion: the fields freq and dad are set, heap[heap_max] and
 556  *    above are the tree nodes sorted by increasing frequency.
 557  * OUT assertions: the field len is set to the optimal bit length, the
 558  *     array bl_count contains the frequencies for each bit length.
 559  *     The length opt_len is updated; static_len is also updated if stree is
 560  *     not null.
 561  */
 562 local void gen_bitlen(deflate_state *s, tree_desc *desc) {



 563     ct_data *tree        = desc->dyn_tree;
 564     int max_code         = desc->max_code;
 565     const ct_data *stree = desc->stat_desc->static_tree;
 566     const intf *extra    = desc->stat_desc->extra_bits;
 567     int base             = desc->stat_desc->extra_base;
 568     int max_length       = desc->stat_desc->max_length;
 569     int h;              /* heap index */
 570     int n, m;           /* iterate over the tree elements */
 571     int bits;           /* bit length */
 572     int xbits;          /* extra bits */
 573     ush f;              /* frequency */
 574     int overflow = 0;   /* number of elements with bit length too large */
 575 
 576     for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
 577 
 578     /* In a first pass, compute the optimal bit lengths (which may
 579      * overflow in the case of the bit length tree).
 580      */
 581     tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
 582 

 617     /* Now recompute all bit lengths, scanning in increasing frequency.
 618      * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
 619      * lengths instead of fixing only the wrong ones. This idea is taken
 620      * from 'ar' written by Haruhiko Okumura.)
 621      */
 622     for (bits = max_length; bits != 0; bits--) {
 623         n = s->bl_count[bits];
 624         while (n != 0) {
 625             m = s->heap[--h];
 626             if (m > max_code) continue;
 627             if ((unsigned) tree[m].Len != (unsigned) bits) {
 628                 Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
 629                 s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq;
 630                 tree[m].Len = (ush)bits;
 631             }
 632             n--;
 633         }
 634     }
 635 }
 636 
 637 #ifdef DUMP_BL_TREE
 638 #  include <stdio.h>
 639 #endif







































 640 
 641 /* ===========================================================================
 642  * Construct one Huffman tree and assigns the code bit strings and lengths.
 643  * Update the total bit length for the current block.
 644  * IN assertion: the field freq is set for all tree elements.
 645  * OUT assertions: the fields len and code are set to the optimal bit length
 646  *     and corresponding code. The length opt_len is updated; static_len is
 647  *     also updated if stree is not null. The field max_code is set.
 648  */
 649 local void build_tree(deflate_state *s, tree_desc *desc) {



 650     ct_data *tree         = desc->dyn_tree;
 651     const ct_data *stree  = desc->stat_desc->static_tree;
 652     int elems             = desc->stat_desc->elems;
 653     int n, m;          /* iterate over heap elements */
 654     int max_code = -1; /* largest code with non zero frequency */
 655     int node;          /* new node being created */
 656 
 657     /* Construct the initial heap, with least frequent element in
 658      * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n + 1].
 659      * heap[0] is not used.
 660      */
 661     s->heap_len = 0, s->heap_max = HEAP_SIZE;
 662 
 663     for (n = 0; n < elems; n++) {
 664         if (tree[n].Freq != 0) {
 665             s->heap[++(s->heap_len)] = max_code = n;
 666             s->depth[n] = 0;
 667         } else {
 668             tree[n].Len = 0;
 669         }

 714         s->heap[SMALLEST] = node++;
 715         pqdownheap(s, tree, SMALLEST);
 716 
 717     } while (s->heap_len >= 2);
 718 
 719     s->heap[--(s->heap_max)] = s->heap[SMALLEST];
 720 
 721     /* At this point, the fields freq and dad are set. We can now
 722      * generate the bit lengths.
 723      */
 724     gen_bitlen(s, (tree_desc *)desc);
 725 
 726     /* The field len is now set, we can generate the bit codes */
 727     gen_codes ((ct_data *)tree, max_code, s->bl_count);
 728 }
 729 
 730 /* ===========================================================================
 731  * Scan a literal or distance tree to determine the frequencies of the codes
 732  * in the bit length tree.
 733  */
 734 local void scan_tree(deflate_state *s, ct_data *tree, int max_code) {




 735     int n;                     /* iterates over all tree elements */
 736     int prevlen = -1;          /* last emitted length */
 737     int curlen;                /* length of current code */
 738     int nextlen = tree[0].Len; /* length of next code */
 739     int count = 0;             /* repeat count of the current code */
 740     int max_count = 7;         /* max repeat count */
 741     int min_count = 4;         /* min repeat count */
 742 
 743     if (nextlen == 0) max_count = 138, min_count = 3;
 744     tree[max_code + 1].Len = (ush)0xffff; /* guard */
 745 
 746     for (n = 0; n <= max_code; n++) {
 747         curlen = nextlen; nextlen = tree[n + 1].Len;
 748         if (++count < max_count && curlen == nextlen) {
 749             continue;
 750         } else if (count < min_count) {
 751             s->bl_tree[curlen].Freq += count;
 752         } else if (curlen != 0) {
 753             if (curlen != prevlen) s->bl_tree[curlen].Freq++;
 754             s->bl_tree[REP_3_6].Freq++;
 755         } else if (count <= 10) {
 756             s->bl_tree[REPZ_3_10].Freq++;
 757         } else {
 758             s->bl_tree[REPZ_11_138].Freq++;
 759         }
 760         count = 0; prevlen = curlen;
 761         if (nextlen == 0) {
 762             max_count = 138, min_count = 3;
 763         } else if (curlen == nextlen) {
 764             max_count = 6, min_count = 3;
 765         } else {
 766             max_count = 7, min_count = 4;
 767         }
 768     }
 769 }
 770 
 771 /* ===========================================================================
 772  * Send a literal or distance tree in compressed form, using the codes in
 773  * bl_tree.
 774  */
 775 local void send_tree(deflate_state *s, ct_data *tree, int max_code) {




 776     int n;                     /* iterates over all tree elements */
 777     int prevlen = -1;          /* last emitted length */
 778     int curlen;                /* length of current code */
 779     int nextlen = tree[0].Len; /* length of next code */
 780     int count = 0;             /* repeat count of the current code */
 781     int max_count = 7;         /* max repeat count */
 782     int min_count = 4;         /* min repeat count */
 783 
 784     /* tree[max_code + 1].Len = -1; */  /* guard already set */
 785     if (nextlen == 0) max_count = 138, min_count = 3;
 786 
 787     for (n = 0; n <= max_code; n++) {
 788         curlen = nextlen; nextlen = tree[n + 1].Len;
 789         if (++count < max_count && curlen == nextlen) {
 790             continue;
 791         } else if (count < min_count) {
 792             do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
 793 
 794         } else if (curlen != 0) {
 795             if (curlen != prevlen) {

 802             send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count - 3, 3);
 803 
 804         } else {
 805             send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count - 11, 7);
 806         }
 807         count = 0; prevlen = curlen;
 808         if (nextlen == 0) {
 809             max_count = 138, min_count = 3;
 810         } else if (curlen == nextlen) {
 811             max_count = 6, min_count = 3;
 812         } else {
 813             max_count = 7, min_count = 4;
 814         }
 815     }
 816 }
 817 
 818 /* ===========================================================================
 819  * Construct the Huffman tree for the bit lengths and return the index in
 820  * bl_order of the last bit length code to send.
 821  */
 822 local int build_bl_tree(deflate_state *s) {


 823     int max_blindex;  /* index of last bit length code of non zero freq */
 824 
 825     /* Determine the bit length frequencies for literal and distance trees */
 826     scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
 827     scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
 828 
 829     /* Build the bit length tree: */
 830     build_tree(s, (tree_desc *)(&(s->bl_desc)));
 831     /* opt_len now includes the length of the tree representations, except the
 832      * lengths of the bit lengths codes and the 5 + 5 + 4 bits for the counts.
 833      */
 834 
 835     /* Determine the number of bit length codes to send. The pkzip format
 836      * requires that at least 4 bit length codes be sent. (appnote.txt says
 837      * 3 but the actual value used is 4.)
 838      */
 839     for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
 840         if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
 841     }
 842     /* Update opt_len to include the bit length tree and counts */
 843     s->opt_len += 3*((ulg)max_blindex + 1) + 5 + 5 + 4;
 844     Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
 845             s->opt_len, s->static_len));
 846 
 847     return max_blindex;
 848 }
 849 
 850 /* ===========================================================================
 851  * Send the header for a block using dynamic Huffman trees: the counts, the
 852  * lengths of the bit length codes, the literal tree and the distance tree.
 853  * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
 854  */
 855 local void send_all_trees(deflate_state *s, int lcodes, int dcodes,
 856                           int blcodes) {


 857     int rank;                    /* index in bl_order */
 858 
 859     Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
 860     Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
 861             "too many codes");
 862     Tracev((stderr, "\nbl counts: "));
 863     send_bits(s, lcodes - 257, 5);  /* not +255 as stated in appnote.txt */
 864     send_bits(s, dcodes - 1,   5);
 865     send_bits(s, blcodes - 4,  4);  /* not -3 as stated in appnote.txt */
 866     for (rank = 0; rank < blcodes; rank++) {
 867         Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
 868         send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
 869     }
 870     Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
 871 
 872     send_tree(s, (ct_data *)s->dyn_ltree, lcodes - 1);  /* literal tree */
 873     Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
 874 
 875     send_tree(s, (ct_data *)s->dyn_dtree, dcodes - 1);  /* distance tree */
 876     Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
 877 }
 878 
 879 /* ===========================================================================
 880  * Send a stored block
 881  */
 882 void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
 883                                     ulg stored_len, int last) {




 884     send_bits(s, (STORED_BLOCK<<1) + last, 3);  /* send block type */
 885     bi_windup(s);        /* align on byte boundary */
 886     put_short(s, (ush)stored_len);
 887     put_short(s, (ush)~stored_len);
 888     if (stored_len)
 889         zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len);
 890     s->pending += stored_len;
 891 #ifdef ZLIB_DEBUG
 892     s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
 893     s->compressed_len += (stored_len + 4) << 3;
 894     s->bits_sent += 2*16;
 895     s->bits_sent += stored_len << 3;
 896 #endif
 897 }
 898 
 899 /* ===========================================================================
 900  * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
 901  */
 902 void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s) {


 903     bi_flush(s);
 904 }
 905 
 906 /* ===========================================================================
 907  * Send one empty static block to give enough lookahead for inflate.
 908  * This takes 10 bits, of which 7 may remain in the bit buffer.
 909  */
 910 void ZLIB_INTERNAL _tr_align(deflate_state *s) {


 911     send_bits(s, STATIC_TREES<<1, 3);
 912     send_code(s, END_BLOCK, static_ltree);
 913 #ifdef ZLIB_DEBUG
 914     s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
 915 #endif
 916     bi_flush(s);
 917 }
 918 
 919 /* ===========================================================================
 920  * Send the block data compressed using the given Huffman trees
 921  */
 922 local void compress_block(deflate_state *s, const ct_data *ltree,
 923                           const ct_data *dtree) {
 924     unsigned dist;      /* distance of matched string */
 925     int lc;             /* match length or unmatched char (if dist == 0) */
 926     unsigned sx = 0;    /* running index in symbol buffers */
 927     unsigned code;      /* the code to send */
 928     int extra;          /* number of extra bits to send */
 929 
 930     if (s->sym_next != 0) do {
 931 #ifdef LIT_MEM
 932         dist = s->d_buf[sx];
 933         lc = s->l_buf[sx++];
 934 #else
 935         dist = s->sym_buf[sx++] & 0xff;
 936         dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
 937         lc = s->sym_buf[sx++];
 938 #endif
 939         if (dist == 0) {
 940             send_code(s, lc, ltree); /* send a literal byte */
 941             Tracecv(isgraph(lc), (stderr," '%c' ", lc));
 942         } else {
 943             /* Here, lc is the match length - MIN_MATCH */
 944             code = _length_code[lc];
 945             send_code(s, code + LITERALS + 1, ltree);   /* send length code */
 946             extra = extra_lbits[code];
 947             if (extra != 0) {
 948                 lc -= base_length[code];
 949                 send_bits(s, lc, extra);       /* send the extra length bits */
 950             }
 951             dist--; /* dist is now the match distance - 1 */
 952             code = d_code(dist);
 953             Assert (code < D_CODES, "bad d_code");
 954 
 955             send_code(s, code, dtree);       /* send the distance code */
 956             extra = extra_dbits[code];
 957             if (extra != 0) {
 958                 dist -= (unsigned)base_dist[code];
 959                 send_bits(s, dist, extra);   /* send the extra distance bits */
 960             }
 961         } /* literal or match pair ? */
 962 
 963         /* Check for no overlay of pending_buf on needed symbols */
 964 #ifdef LIT_MEM
 965         Assert(s->pending < 2 * (s->lit_bufsize + sx), "pendingBuf overflow");
 966 #else
 967         Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
 968 #endif
 969 
 970     } while (sx < s->sym_next);
 971 
 972     send_code(s, END_BLOCK, ltree);
 973 }
 974 
 975 /* ===========================================================================
 976  * Check if the data type is TEXT or BINARY, using the following algorithm:
 977  * - TEXT if the two conditions below are satisfied:
 978  *    a) There are no non-portable control characters belonging to the
 979  *       "block list" (0..6, 14..25, 28..31).
 980  *    b) There is at least one printable character belonging to the
 981  *       "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
 982  * - BINARY otherwise.
 983  * - The following partially-portable control characters form a
 984  *   "gray list" that is ignored in this detection algorithm:
 985  *   (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
 986  * IN assertion: the fields Freq of dyn_ltree are set.
 987  */
 988 local int detect_data_type(deflate_state *s) {
 989     /* block_mask is the bit mask of block-listed bytes
 990      * set bits 0..6, 14..25, and 28..31
 991      * 0xf3ffc07f = binary 11110011111111111100000001111111
 992      */
 993     unsigned long block_mask = 0xf3ffc07fUL;
 994     int n;
 995 
 996     /* Check for non-textual ("block-listed") bytes. */
 997     for (n = 0; n <= 31; n++, block_mask >>= 1)
 998         if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0))
 999             return Z_BINARY;
1000 
1001     /* Check for textual ("allow-listed") bytes. */
1002     if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1003             || s->dyn_ltree[13].Freq != 0)
1004         return Z_TEXT;
1005     for (n = 32; n < LITERALS; n++)
1006         if (s->dyn_ltree[n].Freq != 0)
1007             return Z_TEXT;
1008 
1009     /* There are no "block-listed" or "allow-listed" bytes:
1010      * this stream either is empty or has tolerated ("gray-listed") bytes only.
1011      */
1012     return Z_BINARY;
1013 }
1014 
1015 /* ===========================================================================
1016  * Determine the best encoding for the current block: dynamic trees, static
1017  * trees or store, and write out the encoded block.
1018  */
1019 void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf,
1020                                    ulg stored_len, int last) {




1021     ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
1022     int max_blindex = 0;  /* index of last bit length code of non zero freq */
1023 
1024     /* Build the Huffman trees unless a stored block is forced */
1025     if (s->level > 0) {
1026 
1027         /* Check if the file is binary or text */
1028         if (s->strm->data_type == Z_UNKNOWN)
1029             s->strm->data_type = detect_data_type(s);
1030 
1031         /* Construct the literal and distance trees */
1032         build_tree(s, (tree_desc *)(&(s->l_desc)));
1033         Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
1034                 s->static_len));
1035 
1036         build_tree(s, (tree_desc *)(&(s->d_desc)));
1037         Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
1038                 s->static_len));
1039         /* At this point, opt_len and static_len are the total bit lengths of
1040          * the compressed block data, excluding the tree representations.

1097     Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1098     /* The above check is made mod 2^32, for files larger than 512 MB
1099      * and uLong implemented on 32 bits.
1100      */
1101     init_block(s);
1102 
1103     if (last) {
1104         bi_windup(s);
1105 #ifdef ZLIB_DEBUG
1106         s->compressed_len += 7;  /* align on byte boundary */
1107 #endif
1108     }
1109     Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
1110            s->compressed_len - 7*last));
1111 }
1112 
1113 /* ===========================================================================
1114  * Save the match info and tally the frequency counts. Return true if
1115  * the current block must be flushed.
1116  */
1117 int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) {
1118 #ifdef LIT_MEM
1119     s->d_buf[s->sym_next] = (ush)dist;
1120     s->l_buf[s->sym_next++] = (uch)lc;
1121 #else
1122     s->sym_buf[s->sym_next++] = (uch)dist;
1123     s->sym_buf[s->sym_next++] = (uch)(dist >> 8);
1124     s->sym_buf[s->sym_next++] = (uch)lc;
1125 #endif
1126     if (dist == 0) {
1127         /* lc is the unmatched char */
1128         s->dyn_ltree[lc].Freq++;
1129     } else {
1130         s->matches++;
1131         /* Here, lc is the match length - MIN_MATCH */
1132         dist--;             /* dist = match distance - 1 */
1133         Assert((ush)dist < (ush)MAX_DIST(s) &&
1134                (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1135                (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");
1136 
1137         s->dyn_ltree[_length_code[lc] + LITERALS + 1].Freq++;
1138         s->dyn_dtree[d_code(dist)].Freq++;
1139     }
1140     return (s->sym_next == s->sym_end);
1141 }
















































































































































< prev index next >