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