Update contrib.
     1 /* Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
 
     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
 
    13  *      The "deflation" process uses several Huffman trees. The more
 
    14  *      common source values are represented by shorter bit sequences.
 
    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.
 
    24  *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
 
    25  *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
 
    28  *          Data Compression:  Methods and Theory, pp. 49-50.
 
    29  *          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
 
    33  *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
 
    38 /* #define GEN_TREES_H */
 
    46 /* ===========================================================================
 
    51 /* Bit length codes must not exceed MAX_BL_BITS bits */
 
    54 /* end of block literal code */
 
    57 /* repeat previous bit length 3-6 times (2 bits of repeat count) */
 
    60 /* repeat a zero length 3-10 times  (3 bits of repeat count) */
 
    62 #define REPZ_11_138  18
 
    63 /* repeat a zero length 11-138 times  (7 bits of repeat count) */
 
    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};
 
    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};
 
    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};
 
    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.
 
    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.)
 
    85 /* ===========================================================================
 
    86  * Local data. These are initialized only once.
 
    89 #define DIST_CODE_LEN  512 /* see definition of array dist_code below */
 
    91 #if defined(GEN_TREES_H) || !defined(STDC)
 
    92 /* non ANSI compilers may not accept trees.h */
 
    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
 
   101 local ct_data static_dtree[D_CODES];
 
   102 /* The static distance tree. (Actually a trivial tree since all codes use
 
   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.
 
   112 uch _length_code[MAX_MATCH-MIN_MATCH+1];
 
   113 /* length code for each normalized match length (0 == MIN_MATCH) */
 
   115 local int base_length[LENGTH_CODES];
 
   116 /* First normalized length for each code (0 = MIN_MATCH) */
 
   118 local int base_dist[D_CODES];
 
   119 /* First normalized distance for each code (0 = distance of 1) */
 
   123 #endif /* GEN_TREES_H */
 
   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 */
 
   132 #ifndef SYMBIAN_EZLIB_DEVICE
 
   133 local static_tree_desc  static_l_desc =
 
   134 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
 
   136 local static_tree_desc  static_d_desc =
 
   137 {static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS};
 
   139 local static_tree_desc  static_bl_desc =
 
   140 {(const ct_data *)0, extra_blbits, 0,   BL_CODES, MAX_BL_BITS};
 
   144 local const static_tree_desc  static_l_desc =
 
   145 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
 
   147 local const static_tree_desc  static_d_desc =
 
   148 {static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS};
 
   150 local const static_tree_desc  static_bl_desc =
 
   151 {(const ct_data *)0, extra_blbits, 0,   BL_CODES, MAX_BL_BITS};
 
   153 #endif //SYMBIAN_EZLIB_DEVICE
 
   156 /* ===========================================================================
 
   157  * Local (static) routines in this file.
 
   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,
 
   171 local void compress_block OF((deflate_state *s, ct_data *ltree,
 
   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,
 
   181 local void gen_trees_header OF((void));
 
   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 */
 
   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); }
 
   194 /* ===========================================================================
 
   195  * Output a short LSB first on the stream.
 
   196  * IN assertion: there is enough room in pendingBuf.
 
   198 #define put_short(s, w) { \
 
   199     put_byte(s, (uch)((w) & 0xff)); \
 
   200     put_byte(s, (uch)((ush)(w) >> 8)); \
 
   203 /* ===========================================================================
 
   204  * Send a value on a given number of bits.
 
   205  * IN assertion: length <= 16 and value fits in length bits.
 
   208 local void send_bits      OF((deflate_state *s, int value, int length));
 
   211 local void send_bits(deflate_state * s, int value,int  length)
 
   213 local void send_bits(s, value, length)
 
   215     int value;  /* value to send */
 
   216     int length; /* number of bits */
 
   217 #endif //__SYMBIAN32__
 
   219     Tracevv((stderr," l %2d v %4x ", length, value));
 
   220     Assert(length > 0 && length <= 15, "invalid length");
 
   221     s->bits_sent += (ulg)length;
 
   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.
 
   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;
 
   233         s->bi_buf |= value << s->bi_valid;
 
   234         s->bi_valid += length;
 
   239 #define send_bits(s, value, length) \
 
   241   if (s->bi_valid > (int)Buf_size - len) {\
 
   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;\
 
   248     s->bi_buf |= (value) << s->bi_valid;\
 
   255 /* the arguments must not have side effects */
 
   257 /* ===========================================================================
 
   258  * Initialize the various 'constant' tables.
 
   260 local void tr_static_init()
 
   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 */
 
   272     if (static_init_done) return;
 
   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;
 
   281     /* Initialize the mapping length (0..255) -> length code (0..28) */
 
   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;
 
   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:
 
   294     _length_code[length-1] = (uch)code;
 
   296     /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
 
   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;
 
   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;
 
   312     Assert (dist == 256, "tr_static_init: 256+dist != 512");
 
   314     /* Construct the codes of the static literal tree */
 
   315     for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 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
 
   325     gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
 
   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);
 
   332     static_init_done = 1;
 
   337 #endif /* defined(GEN_TREES_H) || !defined(STDC) */
 
   339 /* ===========================================================================
 
   340  * Genererate the file trees.h describing the static trees.
 
   347 #  define SEPARATOR(i, last, width) \
 
   348       ((i) == (last)? "\n};\n\n" :    \
 
   349        ((i) % (width) == (width)-1 ? ",\n" : ", "))
 
   351 void gen_trees_header()
 
   353     FILE *header = fopen("trees.h", "w");
 
   356     Assert (header != NULL, "Can't open trees.h");
 
   358             "/* header created automatically with -DGEN_TREES_H */\n\n");
 
   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));
 
   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));
 
   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));
 
   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));
 
   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));
 
   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));
 
   398 #endif /* GEN_TREES_H */
 
   400 /* ===========================================================================
 
   401  * Initialize the tree data structures for a new zlib stream.
 
   404 void _tr_init(   deflate_state * s)
 
   408 #endif //__SYMBIAN32__
 
   412     s->l_desc.dyn_tree = s->dyn_ltree;
 
   413     s->l_desc.stat_desc = &static_l_desc;
 
   415     s->d_desc.dyn_tree = s->dyn_dtree;
 
   416     s->d_desc.stat_desc = &static_d_desc;
 
   418     s->bl_desc.dyn_tree = s->bl_tree;
 
   419     s->bl_desc.stat_desc = &static_bl_desc;
 
   423     s->last_eob_len = 8; /* enough lookahead for inflate */
 
   425     s->compressed_len = 0L;
 
   429     /* Initialize the first block of the first file: */
 
   433 /* ===========================================================================
 
   434  * Initialize a new block.
 
   437 local void init_block(    deflate_state * s)
 
   439 local void init_block(s)
 
   441 #endif //__SYMBIAN32__
 
   443     int n; /* iterates over tree elements */
 
   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;
 
   450     s->dyn_ltree[END_BLOCK].Freq = 1;
 
   451     s->opt_len = s->static_len = 0L;
 
   452     s->last_lit = s->matches = 0;
 
   456 /* Index within the heap array of least frequent node in the Huffman tree */
 
   459 /* ===========================================================================
 
   460  * Remove the smallest element from the heap and recreate the heap with
 
   461  * one less element. Updates heap and heap_len.
 
   463 #define pqremove(s, tree, top) \
 
   465     top = s->heap[SMALLEST]; \
 
   466     s->heap[SMALLEST] = s->heap[s->heap_len--]; \
 
   467     pqdownheap(s, tree, SMALLEST); \
 
   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.
 
   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]))
 
   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
 
   485 local void pqdownheap(  deflate_state * s,ct_data *  tree,int k)
 
   487 local void pqdownheap(s, tree, k)
 
   489     ct_data *tree;  /* the tree to restore */
 
   490     int k;               /* node to move down */
 
   491 #endif //__SYMBIAN32__	
 
   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)) {
 
   501         /* Exit if v is smaller than both sons */
 
   502         if (smaller(tree, v, s->heap[j], s->depth)) break;
 
   504         /* Exchange v with the smallest son */
 
   505         s->heap[k] = s->heap[j];  k = j;
 
   507         /* And continue down the tree, setting j to the left son of k */
 
   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
 
   524 local void gen_bitlen(    deflate_state * s,     tree_desc * desc)
 
   526 local void gen_bitlen(s, desc)
 
   528     tree_desc *desc;    /* the tree descriptor */
 
   529 #endif //__SYMBIAN32__
 
   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 */
 
   544     for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
 
   546     /* In a first pass, compute the optimal bit lengths (which may
 
   547      * overflow in the case of the bit length tree).
 
   549     tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
 
   551     for (h = s->heap_max+1; h < HEAP_SIZE; 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 */
 
   558         if (n > max_code) continue; /* not a leaf node */
 
   562         if (n >= base) xbits = extra[n-base];
 
   564         s->opt_len += (ulg)f * (bits + xbits);
 
   565         if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
 
   567     if (overflow == 0) return;
 
   569     Trace((stderr,"\nbit length overflow\n"));
 
   570     /* This happens for example on obj2 and pic of the Calgary corpus */
 
   572     /* Find the first bit length which could increase: */
 
   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]
 
   583     } while (overflow > 0);
 
   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.)
 
   590     for (bits = max_length; bits != 0; bits--) {
 
   591         n = s->bl_count[bits];
 
   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)
 
   599                 tree[m].Len = (ush)bits;
 
   606 /* ===========================================================================
 
   607  * Generate the codes for a given tree and bit counts (which need not be
 
   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
 
   615 local void gen_codes (    ct_data * tree, int max_code,    ushf *  bl_count)
 
   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__
 
   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 */
 
   628     /* The distribution counts are first used to generate the code values
 
   629      * without bit reversal.
 
   631     for (bits = 1; bits <= MAX_BITS; bits++) {
 
   632         next_code[bits] = code = (code + bl_count[bits-1]) << 1;
 
   634     /* Check that the bit counts in bl_count are consistent. The last code
 
   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));
 
   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);
 
   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));
 
   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.
 
   661 local void build_tree(    deflate_state * s,    tree_desc *  desc)
 
   663 local void build_tree(s, desc)
 
   665     tree_desc *desc; /* the tree descriptor */
 
   666 #endif //__SYMBIAN32__
 
   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 */
 
   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.
 
   679     s->heap_len = 0, s->heap_max = HEAP_SIZE;
 
   681     for (n = 0; n < elems; n++) {
 
   682         if (tree[n].Freq != 0) {
 
   683             s->heap[++(s->heap_len)] = max_code = n;
 
   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.
 
   695     while (s->heap_len < 2) {
 
   696         node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 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 */
 
   702     desc->max_code = max_code;
 
   704     /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
 
   705      * establish sub-heaps of increasing lengths:
 
   707     for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
 
   709     /* Construct the Huffman tree by repeatedly combining the least two
 
   712     node = elems;              /* next internal node of the tree */
 
   714         pqremove(s, tree, n);  /* n = node of least frequency */
 
   715         m = s->heap[SMALLEST]; /* m = node of next least frequency */
 
   717         s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
 
   718         s->heap[--(s->heap_max)] = m;
 
   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;
 
   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);
 
   731         /* and insert the new node in the heap */
 
   732         s->heap[SMALLEST] = node++;
 
   733         pqdownheap(s, tree, SMALLEST);
 
   735     } while (s->heap_len >= 2);
 
   737     s->heap[--(s->heap_max)] = s->heap[SMALLEST];
 
   739     /* At this point, the fields freq and dad are set. We can now
 
   740      * generate the bit lengths.
 
   742     gen_bitlen(s, (tree_desc *)desc);
 
   744     /* The field len is now set, we can generate the bit codes */
 
   745     gen_codes ((ct_data *)tree, max_code, s->bl_count);
 
   748 /* ===========================================================================
 
   749  * Scan a literal or distance tree to determine the frequencies of the codes
 
   750  * in the bit length tree.
 
   753  local void scan_tree (   deflate_state * s,   ct_data *  tree,int  max_code)
 
   755 local void scan_tree (s, tree, max_code)
 
   757     ct_data *tree;   /* the tree to be scanned */
 
   758     int max_code;    /* and its largest code of non zero frequency */
 
   759 #endif //__SYMBIAN32__
 
   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 */
 
   769     if (nextlen == 0) max_count = 138, min_count = 3;
 
   770     tree[max_code+1].Len = (ush)0xffff; /* guard */
 
   772     for (n = 0; n <= max_code; n++) {
 
   773         curlen = nextlen; nextlen = tree[n+1].Len;
 
   774         if (++count < max_count && curlen == nextlen) {
 
   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++;
 
   784             s->bl_tree[REPZ_11_138].Freq++;
 
   786         count = 0; prevlen = curlen;
 
   788             max_count = 138, min_count = 3;
 
   789         } else if (curlen == nextlen) {
 
   790             max_count = 6, min_count = 3;
 
   792             max_count = 7, min_count = 4;
 
   797 /* ===========================================================================
 
   798  * Send a literal or distance tree in compressed form, using the codes in
 
   802 local void send_tree (    deflate_state * s,    ct_data *  tree, int max_code)
 
   804 local void send_tree (s, tree, max_code)
 
   806     ct_data *tree; /* the tree to be scanned */
 
   807     int max_code;       /* and its largest code of non zero frequency */
 
   808 #endif //__SYMBIAN32__
 
   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 */
 
   818     /* tree[max_code+1].Len = -1; */  /* guard already set */
 
   819     if (nextlen == 0) max_count = 138, min_count = 3;
 
   821     for (n = 0; n <= max_code; n++) {
 
   822         curlen = nextlen; nextlen = tree[n+1].Len;
 
   823         if (++count < max_count && curlen == nextlen) {
 
   825         } else if (count < min_count) {
 
   826             do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
 
   828         } else if (curlen != 0) {
 
   829             if (curlen != prevlen) {
 
   830                 send_code(s, curlen, s->bl_tree); count--;
 
   832             Assert(count >= 3 && count <= 6, " 3_6?");
 
   833             send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
 
   835         } else if (count <= 10) {
 
   836             send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
 
   839             send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
 
   841         count = 0; prevlen = curlen;
 
   843             max_count = 138, min_count = 3;
 
   844         } else if (curlen == nextlen) {
 
   845             max_count = 6, min_count = 3;
 
   847             max_count = 7, min_count = 4;
 
   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.
 
   857 local int build_bl_tree(  deflate_state * s)
 
   859 local int build_bl_tree(s)
 
   861 #endif //__SYMBIAN32__
 
   863     int max_blindex;  /* index of last bit length code of non zero freq */
 
   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);
 
   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.
 
   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.)
 
   879     for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
 
   880         if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
 
   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));
 
   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.
 
   896 local void send_all_trees(   deflate_state * s, int lcodes, int dcodes, int blcodes)
 
   898 local void send_all_trees(s, lcodes, dcodes, blcodes)
 
   900     int lcodes, dcodes, blcodes; /* number of codes for each tree */
 
   901 #endif //__SYMBIAN32__
 
   903     int rank;                    /* index in bl_order */
 
   905     Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
 
   906     Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_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);
 
   916     Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
 
   918     send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
 
   919     Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
 
   921     send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
 
   922     Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
 
   925 /* ===========================================================================
 
   926  * Send a stored block
 
   929 void _tr_stored_block(   deflate_state * s,    charf *  buf,ulg  stored_len, int eof)
 
   931 void _tr_stored_block(s, buf, stored_len, eof)
 
   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__	
 
   938     send_bits(s, (STORED_BLOCK<<1)+eof, 3);  /* send block type */
 
   940     s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
 
   941     s->compressed_len += (stored_len + 4) << 3;
 
   943     copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
 
   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
 
   958 void _tr_align(    deflate_state * s)
 
   962 #endif //__SYMBIAN32__
 
   964     send_bits(s, STATIC_TREES<<1, 3);
 
   965     send_code(s, END_BLOCK, static_ltree);
 
   967     s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
 
   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.
 
   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);
 
   979         s->compressed_len += 10L;
 
   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.
 
   991 void _tr_flush_block(  deflate_state * s,    charf *  buf,ulg  stored_len,int  eof)
 
   993 void _tr_flush_block(s, buf, stored_len, eof)
 
   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__	
 
  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 */
 
  1003     /* Build the Huffman trees unless a stored block is forced */
 
  1006         /* Check if the file is binary or text */
 
  1007         if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
 
  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,
 
  1015         build_tree(s, (tree_desc *)(&(s->d_desc)));
 
  1016         Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_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.
 
  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.
 
  1025         max_blindex = build_bl_tree(s);
 
  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;
 
  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,
 
  1035         if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
 
  1038         Assert(buf != (char*)0, "lost buf");
 
  1039         opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
 
  1043     if (buf != (char*)0) { /* force stored block */
 
  1045     if (stored_len+4 <= opt_lenb && buf != (char*)0) {
 
  1046                        /* 4: two words for the lengths */
 
  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.
 
  1054         _tr_stored_block(s, buf, stored_len, eof);
 
  1057     } else if (static_lenb >= 0) { /* force static trees */
 
  1059     } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
 
  1061         send_bits(s, (STATIC_TREES<<1)+eof, 3);
 
  1062         compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
 
  1064         s->compressed_len += 3 + s->static_len;
 
  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,
 
  1070         compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
 
  1072         s->compressed_len += 3 + s->opt_len;
 
  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.
 
  1084         s->compressed_len += 7;  /* align on byte boundary */
 
  1087     Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
 
  1088            s->compressed_len-7*eof));
 
  1091 /* ===========================================================================
 
  1092  * Save the match info and tally the frequency counts. Return true if
 
  1093  * the current block must be flushed.
 
  1095 #ifndef SYMBIAN_EZLIB_DEVICE
 
  1097 #ifdef __SYMBIAN32__
 
  1098 int _tr_tally (   deflate_state * s,unsigned  dist,unsigned  lc)
 
  1100 int _tr_tally (s, dist, lc)
 
  1102     unsigned dist;  /* distance of matched string */
 
  1103     unsigned lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
 
  1104 #endif //__SYMBIAN32__
 
  1106     s->d_buf[s->last_lit] = (ush)dist;
 
  1107     s->l_buf[s->last_lit++] = (uch)lc;
 
  1109         /* lc is the unmatched char */
 
  1110         s->dyn_ltree[lc].Freq++;
 
  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");
 
  1119         s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
 
  1120         s->dyn_dtree[d_code(dist)].Freq++;
 
  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);
 
  1130         for (dcode = 0; dcode < D_CODES; dcode++) {
 
  1131             out_length += (ulg)s->dyn_dtree[dcode].Freq *
 
  1132                 (5L+extra_dbits[dcode]);
 
  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;
 
  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
 
  1147 #endif //SYMBIAN_EZLIB_DEVICE
 
  1148 /* ===========================================================================
 
  1149  * Send the block data compressed using the given Huffman trees
 
  1151 #ifdef __SYMBIAN32__
 
  1152 local void compress_block(  deflate_state * s,    ct_data *  ltree,     ct_data * dtree)
 
  1154 local void compress_block(s, ltree, dtree)
 
  1156     ct_data *ltree; /* literal tree */
 
  1157     ct_data *dtree; /* distance tree */
 
  1158 #endif //__SYMBIAN32__
 
  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 */
 
  1166     if (s->last_lit != 0) do {
 
  1167         dist = s->d_buf[lx];
 
  1168         lc = s->l_buf[lx++];
 
  1170             send_code(s, lc, ltree); /* send a literal byte */
 
  1171             Tracecv(isgraph(lc), (stderr," '%c' ", lc));
 
  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];
 
  1178                 lc -= base_length[code];
 
  1179                 send_bits(s, lc, extra);       /* send the extra length bits */
 
  1181             dist--; /* dist is now the match distance - 1 */
 
  1182             code = d_code(dist);
 
  1183             Assert (code < D_CODES, "bad d_code");
 
  1185             send_code(s, code, dtree);       /* send the distance code */
 
  1186             extra = extra_dbits[code];
 
  1188                 dist -= base_dist[code];
 
  1189                 send_bits(s, dist, extra);   /* send the extra distance bits */
 
  1191         } /* literal or match pair ? */
 
  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");
 
  1197     } while (lx < s->last_lit);
 
  1199     send_code(s, END_BLOCK, ltree);
 
  1200     s->last_eob_len = ltree[END_BLOCK].Len;
 
  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.
 
  1209 #ifdef __SYMBIAN32__
 
  1210 local void set_data_type(    deflate_state * s)
 
  1212 local void set_data_type(s)
 
  1214 #endif //__SYMBIAN32__
 
  1218     for (n = 0; n < 9; n++)
 
  1219         if (s->dyn_ltree[n].Freq != 0)
 
  1222         for (n = 14; n < 32; n++)
 
  1223             if (s->dyn_ltree[n].Freq != 0)
 
  1225     s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
 
  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
 
  1233 #ifdef __SYMBIAN32__
 
  1234 local unsigned bi_reverse(unsigned code,int  len)
 
  1236 local unsigned bi_reverse(code, len)
 
  1237     unsigned code; /* the value to invert */
 
  1238     int len;       /* its bit length */
 
  1239 #endif //__SYMBIAN32__
 
  1241     register unsigned res = 0;
 
  1244         code >>= 1, res <<= 1;
 
  1245     } while (--len > 0);
 
  1249 /* ===========================================================================
 
  1250  * Flush the bit buffer, keeping at most 7 bits in it.
 
  1252 #ifdef __SYMBIAN32__
 
  1253 local void bi_flush(  deflate_state * s)
 
  1255 local void bi_flush(s)
 
  1257 #endif //__SYMBIAN32__
 
  1259     if (s->bi_valid == 16) {
 
  1260         put_short(s, s->bi_buf);
 
  1263     } else if (s->bi_valid >= 8) {
 
  1264         put_byte(s, (Byte)s->bi_buf);
 
  1270 /* ===========================================================================
 
  1271  * Flush the bit buffer and align the output on a byte boundary
 
  1273 #ifdef __SYMBIAN32__
 
  1274 local void bi_windup(    deflate_state * s)
 
  1276 local void bi_windup(s)
 
  1278 #endif //__SYMBIAN32__
 
  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);
 
  1288     s->bits_sent = (s->bits_sent+7) & ~7;
 
  1292 /* ===========================================================================
 
  1293  * Copy a stored block, storing first the length and its
 
  1294  * one's complement if requested.
 
  1296 #ifdef __SYMBIAN32__
 
  1297 local void copy_block(    deflate_state * s,    charf    * buf,unsigned  len,int  header)
 
  1299 local void copy_block(s, buf, len, header)
 
  1301     charf    *buf;    /* the input data */
 
  1302     unsigned len;     /* its length */
 
  1303     int      header;  /* true if block header must be written */
 
  1304 #endif //__SYMBIAN32__	
 
  1306     bi_windup(s);        /* align on byte boundary */
 
  1307     s->last_eob_len = 8; /* enough lookahead for inflate */
 
  1310         put_short(s, (ush)len);
 
  1311         put_short(s, (ush)~len);
 
  1313         s->bits_sent += 2*16;
 
  1317     s->bits_sent += (ulg)len<<3;
 
  1320         put_byte(s, *buf++);