compressionlibs/ziplib/src/zlib/deflate.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 /* deflate.cpp -- compress data using the deflation algorithm
       
     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 depends on being able to identify portions
       
    14  *      of the input text which are identical to earlier input (within a
       
    15  *      sliding window trailing behind the input currently being processed).
       
    16  *
       
    17  *      The most straightforward technique turns out to be the fastest for
       
    18  *      most input files: try all possible matches and select the longest.
       
    19  *      The key feature of this algorithm is that insertions into the string
       
    20  *      dictionary are very simple and thus fast, and deletions are avoided
       
    21  *      completely. Insertions are performed at each input character, whereas
       
    22  *      string matches are performed only when the previous match ends. So it
       
    23  *      is preferable to spend more time in matches to allow very fast string
       
    24  *      insertions and avoid deletions. The matching algorithm for small
       
    25  *      strings is inspired from that of Rabin & Karp. A brute force approach
       
    26  *      is used to find longer strings when a small match has been found.
       
    27  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
       
    28  *      (by Leonid Broukhis).
       
    29  *         A previous version of this file used a more sophisticated algorithm
       
    30  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
       
    31  *      time, but has a larger average cost, uses more memory and is patented.
       
    32  *      However the F&G algorithm may be faster for some highly redundant
       
    33  *      files if the parameter max_chain_length (described below) is too large.
       
    34  *
       
    35  *  ACKNOWLEDGEMENTS
       
    36  *
       
    37  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
       
    38  *      I found it in 'freeze' written by Leonid Broukhis.
       
    39  *      Thanks to many people for bug reports and testing.
       
    40  *
       
    41  *  REFERENCES
       
    42  *
       
    43  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
       
    44  *      Available in http://www.ietf.org/rfc/rfc1951.txt
       
    45  *
       
    46  *      A description of the Rabin and Karp algorithm is given in the book
       
    47  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
       
    48  *
       
    49  *      Fiala,E.R., and Greene,D.H.
       
    50  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
       
    51  *
       
    52  */
       
    53 
       
    54 /* @(#) $Id$ */
       
    55 
       
    56 #include "deflate.h"
       
    57 
       
    58 const char deflate_copyright[] =
       
    59    " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
       
    60 
       
    61 /*
       
    62   If you use the zlib library in a product, an acknowledgment is welcome
       
    63   in the documentation of your product. If for some reason you cannot
       
    64   include such an acknowledgment, I would appreciate that you keep this
       
    65   copyright string in the executable of your product.
       
    66  */
       
    67 
       
    68 /* ===========================================================================
       
    69  *  Function prototypes.
       
    70  */
       
    71 typedef enum {
       
    72     need_more,      /* block not completed, need more input or more output */
       
    73     block_done,     /* block flush performed */
       
    74     finish_started, /* finish started, need only more output at next deflate */
       
    75     finish_done     /* finish done, accept no more input or output */
       
    76 } block_state;
       
    77 
       
    78 typedef block_state (*compress_func) OF((deflate_state *s, int flush));
       
    79 /* Compression function. Returns the block state after the call. */
       
    80 
       
    81 local void fill_window    OF((deflate_state *s));
       
    82 local block_state deflate_stored OF((deflate_state *s, int flush));
       
    83 local block_state deflate_fast   OF((deflate_state *s, int flush));
       
    84 #ifndef FASTEST
       
    85 local block_state deflate_slow   OF((deflate_state *s, int flush));
       
    86 #endif
       
    87 local void lm_init        OF((deflate_state *s));
       
    88 local void putShortMSB    OF((deflate_state *s, uInt b));
       
    89 local void flush_pending  OF((z_streamp strm));
       
    90 local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
       
    91 #ifndef FASTEST
       
    92 #ifdef ASMV
       
    93       void match_init OF((void)); /* asm code initialization */
       
    94       uInt longest_match  OF((deflate_state *s, IPos cur_match));
       
    95 #else
       
    96 local uInt longest_match  OF((deflate_state *s, IPos cur_match));
       
    97 #endif
       
    98 #endif
       
    99 local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
       
   100 
       
   101 #ifdef DEBUG
       
   102 local  void check_match OF((deflate_state *s, IPos start, IPos match,
       
   103                             int length));
       
   104 #endif
       
   105 
       
   106 /* ===========================================================================
       
   107  * Local data
       
   108  */
       
   109 
       
   110 #define NIL 0
       
   111 /* Tail of hash chains */
       
   112 
       
   113 #ifndef TOO_FAR
       
   114 #  define TOO_FAR 4096
       
   115 #endif
       
   116 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
       
   117 
       
   118 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
       
   119 /* Minimum amount of lookahead, except at the end of the input file.
       
   120  * See deflate.c for comments about the MIN_MATCH+1.
       
   121  */
       
   122 
       
   123 /* Values for max_lazy_match, good_match and max_chain_length, depending on
       
   124  * the desired pack level (0..9). The values given below have been tuned to
       
   125  * exclude worst case performance for pathological files. Better values may be
       
   126  * found for specific files.
       
   127  */
       
   128 typedef struct config_s {
       
   129    ush good_length; /* reduce lazy search above this match length */
       
   130    ush max_lazy;    /* do not perform lazy search above this match length */
       
   131    ush nice_length; /* quit search above this match length */
       
   132    ush max_chain;
       
   133    compress_func func;
       
   134 } config;
       
   135 
       
   136 #ifdef FASTEST
       
   137 local const config configuration_table[2] = {
       
   138 /*      good lazy nice chain */
       
   139 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
       
   140 /* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
       
   141 #else
       
   142 local const config configuration_table[10] = {
       
   143 /*      good lazy nice chain */
       
   144 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
       
   145 /* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
       
   146 /* 2 */ {4,    5, 16,    8, deflate_fast},
       
   147 /* 3 */ {4,    6, 32,   32, deflate_fast},
       
   148 
       
   149 /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
       
   150 /* 5 */ {8,   16, 32,   32, deflate_slow},
       
   151 /* 6 */ {8,   16, 128, 128, deflate_slow},
       
   152 /* 7 */ {8,   32, 128, 256, deflate_slow},
       
   153 /* 8 */ {32, 128, 258, 1024, deflate_slow},
       
   154 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
       
   155 #endif
       
   156 
       
   157 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
       
   158  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
       
   159  * meaning.
       
   160  */
       
   161 
       
   162 #define EQUAL 0
       
   163 /* result of memcmp for equal strings */
       
   164 
       
   165 #ifndef NO_DUMMY_DECL
       
   166 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
       
   167 #endif
       
   168 
       
   169 /* ===========================================================================
       
   170  * Update a hash value with the given input byte
       
   171  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
       
   172  *    input characters, so that a running hash key can be computed from the
       
   173  *    previous key instead of complete recalculation each time.
       
   174  */
       
   175 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
       
   176 
       
   177 
       
   178 /* ===========================================================================
       
   179  * Insert string str in the dictionary and set match_head to the previous head
       
   180  * of the hash chain (the most recent string with same hash key). Return
       
   181  * the previous length of the hash chain.
       
   182  * If this file is compiled with -DFASTEST, the compression level is forced
       
   183  * to 1, and no hash chains are maintained.
       
   184  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
       
   185  *    input characters and the first MIN_MATCH bytes of str are valid
       
   186  *    (except for the last MIN_MATCH-1 bytes of the input file).
       
   187  */
       
   188 #ifdef FASTEST
       
   189 #define INSERT_STRING(s, str, match_head) \
       
   190    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
       
   191     match_head = s->head[s->ins_h], \
       
   192     s->head[s->ins_h] = (Pos)(str))
       
   193 #else
       
   194 #define INSERT_STRING(s, str, match_head) \
       
   195    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
       
   196     match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
       
   197     s->head[s->ins_h] = (Pos)(str))
       
   198 #endif
       
   199 
       
   200 /* ===========================================================================
       
   201  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
       
   202  * prev[] will be initialized on the fly.
       
   203  */
       
   204 #define CLEAR_HASH(s) \
       
   205     s->head[s->hash_size-1] = NIL; \
       
   206     zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
       
   207 
       
   208 /* ========================================================================= */
       
   209 #ifdef __SYMBIAN32__
       
   210 EXPORT_C int  deflateInit__r( z_streamp strm,int  level,const char * version,int  stream_size)
       
   211 #else
       
   212 int ZEXPORT deflateInit_(strm, level, version, stream_size)
       
   213     z_streamp strm;
       
   214     int level;
       
   215     const char *version;
       
   216     int stream_size;
       
   217 #endif //__SYMBIAN32__
       
   218 {
       
   219     return deflateInit2__r(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
       
   220                          Z_DEFAULT_STRATEGY, version, stream_size);
       
   221     /* To do: ignore strm->next_in if we use it as window */
       
   222 }
       
   223 
       
   224 /* ========================================================================= */
       
   225 #ifdef __SYMBIAN32__
       
   226 EXPORT_C int  deflateInit2__r(z_streamp strm, int level,int method,int windowBits,int memLevel,int strategy,
       
   227                const char * version,int stream_size)
       
   228 #else
       
   229 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
       
   230                   version, stream_size)
       
   231     z_streamp strm;
       
   232     int  level;
       
   233     int  method;
       
   234     int  windowBits;
       
   235     int  memLevel;
       
   236     int  strategy;
       
   237     const char *version;
       
   238     int stream_size;
       
   239 #endif //__SYMBIAN32__
       
   240 {
       
   241     deflate_state *s;
       
   242     int wrap = 1;
       
   243     static const char my_version[] = ZLIB_VERSION;
       
   244 
       
   245     ushf *overlay;
       
   246     /* We overlay pending_buf and d_buf+l_buf. This works since the average
       
   247      * output size for (length,distance) codes is <= 24 bits.
       
   248      */
       
   249 
       
   250     if (version == Z_NULL || version[0] != my_version[0] ||
       
   251         stream_size != sizeof(z_stream)) {
       
   252         return Z_VERSION_ERROR;
       
   253     }
       
   254     if (strm == Z_NULL) return Z_STREAM_ERROR;
       
   255 
       
   256     strm->msg = Z_NULL;
       
   257     if (strm->zalloc == (alloc_func)0) {
       
   258         strm->zalloc = zcalloc;
       
   259         strm->opaque = (voidpf)0;
       
   260     }
       
   261     if (strm->zfree == (free_func)0) strm->zfree = zcfree;
       
   262 
       
   263 #ifdef FASTEST
       
   264     if (level != 0) level = 1;
       
   265 #else
       
   266     if (level == Z_DEFAULT_COMPRESSION) level = 6;
       
   267 #endif
       
   268 
       
   269     if (windowBits < 0) { /* suppress zlib wrapper */
       
   270         wrap = 0;
       
   271         windowBits = -windowBits;
       
   272     }
       
   273 #ifdef GZIP
       
   274     else if (windowBits > 15) {
       
   275         wrap = 2;       /* write gzip wrapper instead */
       
   276         windowBits -= 16;
       
   277     }
       
   278 #endif
       
   279     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
       
   280         windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
       
   281         strategy < 0 || strategy > Z_FIXED) {
       
   282         return Z_STREAM_ERROR;
       
   283     }
       
   284     if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
       
   285     s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
       
   286     if (s == Z_NULL) return Z_MEM_ERROR;
       
   287     strm->state = (struct internal_state FAR *)s;
       
   288     s->strm = strm;
       
   289 
       
   290     s->wrap = wrap;
       
   291     s->gzhead = Z_NULL;
       
   292     s->w_bits = windowBits;
       
   293     s->w_size = 1 << s->w_bits;
       
   294     s->w_mask = s->w_size - 1;
       
   295 
       
   296     s->hash_bits = memLevel + 7;
       
   297     s->hash_size = 1 << s->hash_bits;
       
   298     s->hash_mask = s->hash_size - 1;
       
   299     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
       
   300 
       
   301     s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
       
   302     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
       
   303     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
       
   304 
       
   305     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
       
   306 
       
   307     overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
       
   308     s->pending_buf = (uchf *) overlay;
       
   309     s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
       
   310 
       
   311     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
       
   312         s->pending_buf == Z_NULL) {
       
   313         s->status = FINISH_STATE;
       
   314         strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
       
   315 
       
   316         deflateEnd_r (strm);
       
   317         return Z_MEM_ERROR;
       
   318     }
       
   319     s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
       
   320     s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
       
   321 
       
   322     s->level = level;
       
   323     s->strategy = strategy;
       
   324     s->method = (Byte)method;
       
   325 
       
   326     return deflateReset_r (strm);
       
   327 }
       
   328 
       
   329 /* ========================================================================= */
       
   330 #ifdef __SYMBIAN32__
       
   331 EXPORT_C int  deflateSetDictionary_r (z_streamp strm, const Bytef *  dictionary,uInt   dictLength)
       
   332 #else
       
   333 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
       
   334     z_streamp strm;
       
   335     const Bytef *dictionary;
       
   336     uInt  dictLength;
       
   337 #endif //__SYMBIAN32__
       
   338 {
       
   339     deflate_state *s;
       
   340     uInt length = dictLength;
       
   341     uInt n;
       
   342     IPos hash_head = 0;
       
   343     
       
   344 	// Line to stop compiler warning about unused mandatory global variable 'deflate_copyright'
       
   345 	char dontCare = deflate_copyright[0]; dontCare = dontCare;
       
   346 	
       
   347     if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
       
   348         strm->state->wrap == 2 ||
       
   349         (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
       
   350         return Z_STREAM_ERROR;
       
   351 
       
   352     s = strm->state;
       
   353     if (s->wrap)
       
   354         strm->adler = adler32_r(strm->adler, dictionary, dictLength);
       
   355 
       
   356     if (length < MIN_MATCH) return Z_OK;
       
   357     if (length > MAX_DIST(s)) {
       
   358         length = MAX_DIST(s);
       
   359         dictionary += dictLength - length; /* use the tail of the dictionary */
       
   360     }
       
   361     zmemcpy(s->window, dictionary, length);
       
   362     s->strstart = length;
       
   363     s->block_start = (long)length;
       
   364 
       
   365     /* Insert all strings in the hash table (except for the last two bytes).
       
   366      * s->lookahead stays null, so s->ins_h will be recomputed at the next
       
   367      * call of fill_window.
       
   368      */
       
   369     s->ins_h = s->window[0];
       
   370     UPDATE_HASH(s, s->ins_h, s->window[1]);
       
   371     for (n = 0; n <= length - MIN_MATCH; n++) {
       
   372         INSERT_STRING(s, n, hash_head);
       
   373     }
       
   374     if (hash_head) hash_head = 0;  /* to make compiler happy */
       
   375     return Z_OK;
       
   376 }
       
   377 
       
   378 /* ========================================================================= */
       
   379 #ifdef __SYMBIAN32__
       
   380 EXPORT_C int  deflateReset_r (z_streamp strm)
       
   381 #else
       
   382 int ZEXPORT deflateReset (strm)
       
   383     z_streamp strm;
       
   384 #endif //__SYMBIAN32__
       
   385 {
       
   386     deflate_state *s;
       
   387 
       
   388     if (strm == Z_NULL || strm->state == Z_NULL ||
       
   389         strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
       
   390         return Z_STREAM_ERROR;
       
   391     }
       
   392 
       
   393     strm->total_in = strm->total_out = 0;
       
   394     strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
       
   395     strm->data_type = Z_UNKNOWN;
       
   396 
       
   397     s = (deflate_state *)strm->state;
       
   398     s->pending = 0;
       
   399     s->pending_out = s->pending_buf;
       
   400 
       
   401     if (s->wrap < 0) {
       
   402         s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
       
   403     }
       
   404     s->status = s->wrap ? INIT_STATE : BUSY_STATE;
       
   405     strm->adler =
       
   406 #ifdef GZIP
       
   407         s->wrap == 2 ? crc32_r(0L, Z_NULL, 0) :
       
   408 #endif
       
   409         adler32_r(0L, Z_NULL, 0);
       
   410     s->last_flush = Z_NO_FLUSH;
       
   411 
       
   412     _tr_init(s);
       
   413     lm_init(s);
       
   414 
       
   415     return Z_OK;
       
   416 }
       
   417 
       
   418 /* ========================================================================= */
       
   419 
       
   420 #ifdef __SYMBIAN32__
       
   421 EXPORT_C int deflateSetHeader_r (z_streamp strm, gz_headerp  head)
       
   422 #else
       
   423 int ZEXPORT deflateSetHeader (strm, head)
       
   424     z_streamp strm;
       
   425     gz_headerp head;
       
   426 #endif //__SYMBIAN32__
       
   427 {
       
   428     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   429     if (strm->state->wrap != 2) return Z_STREAM_ERROR;
       
   430     strm->state->gzhead = head;
       
   431     return Z_OK;
       
   432 }
       
   433 
       
   434 /* ========================================================================= */
       
   435 #ifdef __SYMBIAN32__
       
   436 EXPORT_C int  deflatePrime_r (z_streamp strm,int  bits,int  value)
       
   437 #else
       
   438 int ZEXPORT deflatePrime (strm, bits, value)
       
   439     z_streamp strm;
       
   440     int bits;
       
   441     int value;
       
   442 #endif //__SYMBIAN32__
       
   443 {
       
   444     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   445     strm->state->bi_valid = bits;
       
   446     strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
       
   447     return Z_OK;
       
   448 }
       
   449 
       
   450 /* ========================================================================= */
       
   451 
       
   452 #ifdef __SYMBIAN32__
       
   453 EXPORT_C int deflateParams_r (z_streamp strm,int  level,int  strategy)
       
   454 #else
       
   455 int ZEXPORT deflateParams(strm, level, strategy)
       
   456     z_streamp strm;
       
   457     int level;
       
   458     int strategy;
       
   459 #endif //__SYMBIAN32__
       
   460 {
       
   461     deflate_state *s;
       
   462     compress_func func;
       
   463     int err = Z_OK;
       
   464 
       
   465     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   466     s = strm->state;
       
   467 
       
   468 #ifdef FASTEST
       
   469     if (level != 0) level = 1;
       
   470 #else
       
   471     if (level == Z_DEFAULT_COMPRESSION) level = 6;
       
   472 #endif
       
   473     if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
       
   474         return Z_STREAM_ERROR;
       
   475     }
       
   476     func = configuration_table[s->level].func;
       
   477 
       
   478     if (func != configuration_table[level].func && strm->total_in != 0) {
       
   479         /* Flush the last buffer: */
       
   480         err = deflate_r(strm, Z_PARTIAL_FLUSH);
       
   481     }
       
   482     if (s->level != level) {
       
   483         s->level = level;
       
   484         s->max_lazy_match   = configuration_table[level].max_lazy;
       
   485         s->good_match       = configuration_table[level].good_length;
       
   486         s->nice_match       = configuration_table[level].nice_length;
       
   487         s->max_chain_length = configuration_table[level].max_chain;
       
   488     }
       
   489     s->strategy = strategy;
       
   490     return err;
       
   491 }
       
   492 
       
   493 /* ========================================================================= */
       
   494 
       
   495 #ifdef __SYMBIAN32__
       
   496 EXPORT_C int  deflateTune_r(z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
       
   497 #else
       
   498 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
       
   499     z_streamp strm;
       
   500     int good_length;
       
   501     int max_lazy;
       
   502     int nice_length;
       
   503     int max_chain;
       
   504 #endif //__SYMBIAN32__
       
   505 {
       
   506     deflate_state *s;
       
   507 
       
   508     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   509     s = strm->state;
       
   510     s->good_match = good_length;
       
   511     s->max_lazy_match = max_lazy;
       
   512     s->nice_match = nice_length;
       
   513     s->max_chain_length = max_chain;
       
   514     return Z_OK;
       
   515 }
       
   516 
       
   517 /* =========================================================================
       
   518  * For the default windowBits of 15 and memLevel of 8, this function returns
       
   519  * a close to exact, as well as small, upper bound on the compressed size.
       
   520  * They are coded as constants here for a reason--if the #define's are
       
   521  * changed, then this function needs to be changed as well.  The return
       
   522  * value for 15 and 8 only works for those exact settings.
       
   523  *
       
   524  * For any setting other than those defaults for windowBits and memLevel,
       
   525  * the value returned is a conservative worst case for the maximum expansion
       
   526  * resulting from using fixed blocks instead of stored blocks, which deflate
       
   527  * can emit on compressed data for some combinations of the parameters.
       
   528  *
       
   529  * This function could be more sophisticated to provide closer upper bounds
       
   530  * for every combination of windowBits and memLevel, as well as wrap.
       
   531  * But even the conservative upper bound of about 14% expansion does not
       
   532  * seem onerous for output buffer allocation.
       
   533  */
       
   534 
       
   535 #ifdef __SYMBIAN32__
       
   536 EXPORT_C  uLong deflateBound_r(z_streamp strm,uLong  sourceLen)
       
   537 #else
       
   538 uLong ZEXPORT deflateBound(strm, sourceLen)
       
   539     z_streamp strm;
       
   540     uLong sourceLen;
       
   541 #endif //__SYMBIAN32__
       
   542 {
       
   543     deflate_state *s;
       
   544     uLong destLen;
       
   545 
       
   546     /* conservative upper bound */
       
   547     destLen = sourceLen +
       
   548               ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
       
   549 
       
   550     /* if can't get parameters, return conservative bound */
       
   551     if (strm == Z_NULL || strm->state == Z_NULL)
       
   552         return destLen;
       
   553 
       
   554     /* if not default parameters, return conservative bound */
       
   555     s = strm->state;
       
   556     if (s->w_bits != 15 || s->hash_bits != 8 + 7)
       
   557         return destLen;
       
   558 
       
   559     /* default settings: return tight bound for that case */
       
   560     return compressBound_r(sourceLen);
       
   561 }
       
   562 
       
   563 /* =========================================================================
       
   564  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
       
   565  * IN assertion: the stream state is correct and there is enough room in
       
   566  * pending_buf.
       
   567  */
       
   568 
       
   569 #ifdef __SYMBIAN32__
       
   570 local void putShortMSB (deflate_state * s, uInt  b)
       
   571 #else  
       
   572 local void putShortMSB (s, b)
       
   573     deflate_state *s;
       
   574     uInt b;
       
   575 #endif //__SYMBIAN32__
       
   576 {
       
   577     put_byte(s, (Byte)(b >> 8));
       
   578     put_byte(s, (Byte)(b & 0xff));
       
   579 }
       
   580 
       
   581 /* =========================================================================
       
   582  * Flush as much pending output as possible. All deflate() output goes
       
   583  * through this function so some applications may wish to modify it
       
   584  * to avoid allocating a large strm->next_out buffer and copying into it.
       
   585  * (See also read_buf()).
       
   586  */
       
   587 #ifdef __SYMBIAN32__
       
   588 local void flush_pending(z_streamp strm)
       
   589 #else  
       
   590 local void flush_pending(strm)
       
   591     z_streamp strm;
       
   592 #endif //__SYMBIAN32__
       
   593 {
       
   594     unsigned len = strm->state->pending;
       
   595 
       
   596     if (len > strm->avail_out) len = strm->avail_out;
       
   597     if (len == 0) return;
       
   598 
       
   599     zmemcpy(strm->next_out, strm->state->pending_out, len);
       
   600     strm->next_out  += len;
       
   601     strm->state->pending_out  += len;
       
   602     strm->total_out += len;
       
   603     strm->avail_out  -= len;
       
   604     strm->state->pending -= len;
       
   605     if (strm->state->pending == 0) {
       
   606         strm->state->pending_out = strm->state->pending_buf;
       
   607     }
       
   608 }
       
   609 
       
   610 /* ========================================================================= */
       
   611 
       
   612 #ifdef __SYMBIAN32__
       
   613 EXPORT_C int  deflate_r (z_streamp strm,int  flush)
       
   614 #else
       
   615 int ZEXPORT deflate (strm, flush)
       
   616     z_streamp strm;
       
   617     int flush;
       
   618 #endif //__SYMBIAN32__
       
   619 {
       
   620     int old_flush; /* value of flush param for previous deflate call */
       
   621     deflate_state *s;
       
   622 
       
   623     if (strm == Z_NULL || strm->state == Z_NULL ||
       
   624         flush > Z_FINISH || flush < 0) {
       
   625         return Z_STREAM_ERROR;
       
   626     }
       
   627     s = strm->state;
       
   628 
       
   629     if (strm->next_out == Z_NULL ||
       
   630         (strm->next_in == Z_NULL && strm->avail_in != 0) ||
       
   631         (s->status == FINISH_STATE && flush != Z_FINISH)) {
       
   632         ERR_RETURN(strm, Z_STREAM_ERROR);
       
   633     }
       
   634     if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
       
   635 
       
   636     s->strm = strm; /* just in case */
       
   637     old_flush = s->last_flush;
       
   638     s->last_flush = flush;
       
   639 
       
   640     /* Write the header */
       
   641     if (s->status == INIT_STATE) {
       
   642 #ifdef GZIP
       
   643         if (s->wrap == 2) {
       
   644             strm->adler = crc32_r(0L, Z_NULL, 0);
       
   645             put_byte(s, 31);
       
   646             put_byte(s, 139);
       
   647             put_byte(s, 8);
       
   648             if (s->gzhead == NULL) {
       
   649                 put_byte(s, 0);
       
   650                 put_byte(s, 0);
       
   651                 put_byte(s, 0);
       
   652                 put_byte(s, 0);
       
   653                 put_byte(s, 0);
       
   654                 put_byte(s, s->level == 9 ? 2 :
       
   655                             (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
       
   656                              4 : 0));
       
   657                 put_byte(s, OS_CODE);
       
   658                 s->status = BUSY_STATE;
       
   659             }
       
   660             else {
       
   661                 put_byte(s, (s->gzhead->text ? 1 : 0) +
       
   662                             (s->gzhead->hcrc ? 2 : 0) +
       
   663                             (s->gzhead->extra == Z_NULL ? 0 : 4) +
       
   664                             (s->gzhead->name == Z_NULL ? 0 : 8) +
       
   665                             (s->gzhead->comment == Z_NULL ? 0 : 16)
       
   666                         );
       
   667                 put_byte(s, (Byte)(s->gzhead->time & 0xff));
       
   668                 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
       
   669                 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
       
   670                 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
       
   671                 put_byte(s, s->level == 9 ? 2 :
       
   672                             (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
       
   673                              4 : 0));
       
   674                 put_byte(s, s->gzhead->os & 0xff);
       
   675                 if (s->gzhead->extra != NULL) {
       
   676                     put_byte(s, s->gzhead->extra_len & 0xff);
       
   677                     put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
       
   678                 }
       
   679                 if (s->gzhead->hcrc)
       
   680                     strm->adler = crc32_r(strm->adler, s->pending_buf,
       
   681                                         s->pending);
       
   682                 s->gzindex = 0;
       
   683                 s->status = EXTRA_STATE;
       
   684             }
       
   685         }
       
   686         else
       
   687 #endif
       
   688         {
       
   689             uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
       
   690             uInt level_flags;
       
   691 
       
   692             if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
       
   693                 level_flags = 0;
       
   694             else if (s->level < 6)
       
   695                 level_flags = 1;
       
   696             else if (s->level == 6)
       
   697                 level_flags = 2;
       
   698             else
       
   699                 level_flags = 3;
       
   700             header |= (level_flags << 6);
       
   701             if (s->strstart != 0) header |= PRESET_DICT;
       
   702             header += 31 - (header % 31);
       
   703 
       
   704             s->status = BUSY_STATE;
       
   705             putShortMSB(s, header);
       
   706 
       
   707             /* Save the adler32 of the preset dictionary: */
       
   708             if (s->strstart != 0) {
       
   709                 putShortMSB(s, (uInt)(strm->adler >> 16));
       
   710                 putShortMSB(s, (uInt)(strm->adler & 0xffff));
       
   711             }
       
   712             strm->adler = adler32_r(0L, Z_NULL, 0);
       
   713         }
       
   714     }
       
   715 #ifdef GZIP
       
   716     if (s->status == EXTRA_STATE) {
       
   717         if (s->gzhead->extra != NULL) {
       
   718             uInt beg = s->pending;  /* start of bytes to update crc */
       
   719 
       
   720             while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
       
   721                 if (s->pending == s->pending_buf_size) {
       
   722                     if (s->gzhead->hcrc && s->pending > beg)
       
   723                         strm->adler = crc32_r(strm->adler, s->pending_buf + beg,
       
   724                                             s->pending - beg);
       
   725                     flush_pending(strm);
       
   726                     beg = s->pending;
       
   727                     if (s->pending == s->pending_buf_size)
       
   728                         break;
       
   729                 }
       
   730                 put_byte(s, s->gzhead->extra[s->gzindex]);
       
   731                 s->gzindex++;
       
   732             }
       
   733             if (s->gzhead->hcrc && s->pending > beg)
       
   734                 strm->adler = crc32_r(strm->adler, s->pending_buf + beg,
       
   735                                     s->pending - beg);
       
   736             if (s->gzindex == s->gzhead->extra_len) {
       
   737                 s->gzindex = 0;
       
   738                 s->status = NAME_STATE;
       
   739             }
       
   740         }
       
   741         else
       
   742             s->status = NAME_STATE;
       
   743     }
       
   744     if (s->status == NAME_STATE) {
       
   745         if (s->gzhead->name != NULL) {
       
   746             uInt beg = s->pending;  /* start of bytes to update crc */
       
   747             int val;
       
   748 
       
   749             do {
       
   750                 if (s->pending == s->pending_buf_size) {
       
   751                     if (s->gzhead->hcrc && s->pending > beg)
       
   752                         strm->adler = crc32_r(strm->adler, s->pending_buf + beg,
       
   753                                             s->pending - beg);
       
   754                     flush_pending(strm);
       
   755                     beg = s->pending;
       
   756                     if (s->pending == s->pending_buf_size) {
       
   757                         val = 1;
       
   758                         break;
       
   759                     }
       
   760                 }
       
   761                 val = s->gzhead->name[s->gzindex++];
       
   762                 put_byte(s, val);
       
   763             } while (val != 0);
       
   764             if (s->gzhead->hcrc && s->pending > beg)
       
   765                 strm->adler = crc32_r(strm->adler, s->pending_buf + beg,
       
   766                                     s->pending - beg);
       
   767             if (val == 0) {
       
   768                 s->gzindex = 0;
       
   769                 s->status = COMMENT_STATE;
       
   770             }
       
   771         }
       
   772         else
       
   773             s->status = COMMENT_STATE;
       
   774     }
       
   775     if (s->status == COMMENT_STATE) {
       
   776         if (s->gzhead->comment != NULL) {
       
   777             uInt beg = s->pending;  /* start of bytes to update crc */
       
   778             int val;
       
   779 
       
   780             do {
       
   781                 if (s->pending == s->pending_buf_size) {
       
   782                     if (s->gzhead->hcrc && s->pending > beg)
       
   783                         strm->adler = crc32_r(strm->adler, s->pending_buf + beg,
       
   784                                             s->pending - beg);
       
   785                     flush_pending(strm);
       
   786                     beg = s->pending;
       
   787                     if (s->pending == s->pending_buf_size) {
       
   788                         val = 1;
       
   789                         break;
       
   790                     }
       
   791                 }
       
   792                 val = s->gzhead->comment[s->gzindex++];
       
   793                 put_byte(s, val);
       
   794             } while (val != 0);
       
   795             if (s->gzhead->hcrc && s->pending > beg)
       
   796                 strm->adler = crc32_r(strm->adler, s->pending_buf + beg,
       
   797                                     s->pending - beg);
       
   798             if (val == 0)
       
   799                 s->status = HCRC_STATE;
       
   800         }
       
   801         else
       
   802             s->status = HCRC_STATE;
       
   803     }
       
   804     if (s->status == HCRC_STATE) {
       
   805         if (s->gzhead->hcrc) {
       
   806             if (s->pending + 2 > s->pending_buf_size)
       
   807                 flush_pending(strm);
       
   808             if (s->pending + 2 <= s->pending_buf_size) {
       
   809                 put_byte(s, (Byte)(strm->adler & 0xff));
       
   810                 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
       
   811                 strm->adler = crc32_r(0L, Z_NULL, 0);
       
   812                 s->status = BUSY_STATE;
       
   813             }
       
   814         }
       
   815         else
       
   816             s->status = BUSY_STATE;
       
   817     }
       
   818 #endif
       
   819 
       
   820     /* Flush as much pending output as possible */
       
   821     if (s->pending != 0) {
       
   822         flush_pending(strm);
       
   823         if (strm->avail_out == 0) {
       
   824             /* Since avail_out is 0, deflate will be called again with
       
   825              * more output space, but possibly with both pending and
       
   826              * avail_in equal to zero. There won't be anything to do,
       
   827              * but this is not an error situation so make sure we
       
   828              * return OK instead of BUF_ERROR at next call of deflate:
       
   829              */
       
   830             s->last_flush = -1;
       
   831             return Z_OK;
       
   832         }
       
   833 
       
   834     /* Make sure there is something to do and avoid duplicate consecutive
       
   835      * flushes. For repeated and useless calls with Z_FINISH, we keep
       
   836      * returning Z_STREAM_END instead of Z_BUF_ERROR.
       
   837      */
       
   838     } else if (strm->avail_in == 0 && flush <= old_flush &&
       
   839                flush != Z_FINISH) {
       
   840         ERR_RETURN(strm, Z_BUF_ERROR);
       
   841     }
       
   842 
       
   843     /* User must not provide more input after the first FINISH: */
       
   844     if (s->status == FINISH_STATE && strm->avail_in != 0) {
       
   845         ERR_RETURN(strm, Z_BUF_ERROR);
       
   846     }
       
   847 
       
   848     /* Start a new block or continue the current one.
       
   849      */
       
   850     if (strm->avail_in != 0 || s->lookahead != 0 ||
       
   851         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
       
   852         block_state bstate;
       
   853 
       
   854         bstate = (*(configuration_table[s->level].func))(s, flush);
       
   855 
       
   856         if (bstate == finish_started || bstate == finish_done) {
       
   857             s->status = FINISH_STATE;
       
   858         }
       
   859         if (bstate == need_more || bstate == finish_started) {
       
   860             if (strm->avail_out == 0) {
       
   861                 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
       
   862             }
       
   863             return Z_OK;
       
   864             /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
       
   865              * of deflate should use the same flush parameter to make sure
       
   866              * that the flush is complete. So we don't have to output an
       
   867              * empty block here, this will be done at next call. This also
       
   868              * ensures that for a very small output buffer, we emit at most
       
   869              * one empty block.
       
   870              */
       
   871         }
       
   872         if (bstate == block_done) {
       
   873             if (flush == Z_PARTIAL_FLUSH) {
       
   874                 _tr_align(s);
       
   875             } else { /* FULL_FLUSH or SYNC_FLUSH */
       
   876                 _tr_stored_block(s, (char*)0, 0L, 0);
       
   877                 /* For a full flush, this empty block will be recognized
       
   878                  * as a special marker by inflate_sync().
       
   879                  */
       
   880                 if (flush == Z_FULL_FLUSH) {
       
   881                     CLEAR_HASH(s);             /* forget history */
       
   882                 }
       
   883             }
       
   884             flush_pending(strm);
       
   885             if (strm->avail_out == 0) {
       
   886               s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
       
   887               return Z_OK;
       
   888             }
       
   889         }
       
   890     }
       
   891     Assert(strm->avail_out > 0, "bug2");
       
   892 
       
   893     if (flush != Z_FINISH) return Z_OK;
       
   894     if (s->wrap <= 0) return Z_STREAM_END;
       
   895 
       
   896     /* Write the trailer */
       
   897 #ifdef GZIP
       
   898     if (s->wrap == 2) {
       
   899         put_byte(s, (Byte)(strm->adler & 0xff));
       
   900         put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
       
   901         put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
       
   902         put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
       
   903         put_byte(s, (Byte)(strm->total_in & 0xff));
       
   904         put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
       
   905         put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
       
   906         put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
       
   907     }
       
   908     else
       
   909 #endif
       
   910     {
       
   911         putShortMSB(s, (uInt)(strm->adler >> 16));
       
   912         putShortMSB(s, (uInt)(strm->adler & 0xffff));
       
   913     }
       
   914     flush_pending(strm);
       
   915     /* If avail_out is zero, the application will call deflate again
       
   916      * to flush the rest.
       
   917      */
       
   918     if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
       
   919     return s->pending != 0 ? Z_OK : Z_STREAM_END;
       
   920 }
       
   921 
       
   922 /* ========================================================================= */
       
   923 #ifdef __SYMBIAN32__
       
   924 EXPORT_C int   deflateEnd_r (z_streamp strm)
       
   925 #else
       
   926 int ZEXPORT deflateEnd (strm)
       
   927     z_streamp strm;
       
   928 #endif //__SYMBIAN32__
       
   929   
       
   930 {
       
   931     int status;
       
   932 
       
   933     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
       
   934 
       
   935     status = strm->state->status;
       
   936     if (status != INIT_STATE &&
       
   937         status != EXTRA_STATE &&
       
   938         status != NAME_STATE &&
       
   939         status != COMMENT_STATE &&
       
   940         status != HCRC_STATE &&
       
   941         status != BUSY_STATE &&
       
   942         status != FINISH_STATE) {
       
   943       return Z_STREAM_ERROR;
       
   944     }
       
   945 
       
   946     /* Deallocate in reverse order of allocations: */
       
   947     TRY_FREE(strm, strm->state->pending_buf);
       
   948     TRY_FREE(strm, strm->state->head);
       
   949     TRY_FREE(strm, strm->state->prev);
       
   950     TRY_FREE(strm, strm->state->window);
       
   951 
       
   952     ZFREE(strm, strm->state);
       
   953     strm->state = Z_NULL;
       
   954 
       
   955     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
       
   956 }
       
   957 
       
   958 /* =========================================================================
       
   959  * Copy the source state to the destination state.
       
   960  * To simplify the source, this is not supported for 16-bit MSDOS (which
       
   961  * doesn't have enough memory anyway to duplicate compression states).
       
   962  */
       
   963 #ifdef __SYMBIAN32__
       
   964 EXPORT_C int deflateCopy_r (z_streamp dest, z_streamp source)
       
   965 #else
       
   966 int ZEXPORT deflateCopy (dest, source)
       
   967     z_streamp dest;
       
   968     z_streamp source;
       
   969 #endif //__SYMBIAN32__	
       
   970 {
       
   971 #ifdef MAXSEG_64K
       
   972     return Z_STREAM_ERROR;
       
   973 #else
       
   974     deflate_state *ds;
       
   975     deflate_state *ss;
       
   976     ushf *overlay;
       
   977 
       
   978 
       
   979     if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
       
   980         return Z_STREAM_ERROR;
       
   981     }
       
   982 
       
   983     ss = source->state;
       
   984 
       
   985     zmemcpy(dest, source, sizeof(z_stream));
       
   986 
       
   987     ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
       
   988     if (ds == Z_NULL) return Z_MEM_ERROR;
       
   989     dest->state = (struct internal_state FAR *) ds;
       
   990     zmemcpy(ds, ss, sizeof(deflate_state));
       
   991     ds->strm = dest;
       
   992 
       
   993     ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
       
   994     ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
       
   995     ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
       
   996     overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
       
   997     ds->pending_buf = (uchf *) overlay;
       
   998 
       
   999     if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
       
  1000         ds->pending_buf == Z_NULL) {
       
  1001         deflateEnd_r (dest);
       
  1002         return Z_MEM_ERROR;
       
  1003     }
       
  1004     /* following zmemcpy do not work for 16-bit MSDOS */
       
  1005     zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
       
  1006     zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
       
  1007     zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
       
  1008     zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
       
  1009 
       
  1010     ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
       
  1011     ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
       
  1012     ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
       
  1013 
       
  1014     ds->l_desc.dyn_tree = ds->dyn_ltree;
       
  1015     ds->d_desc.dyn_tree = ds->dyn_dtree;
       
  1016     ds->bl_desc.dyn_tree = ds->bl_tree;
       
  1017 
       
  1018     return Z_OK;
       
  1019 #endif /* MAXSEG_64K */
       
  1020 }
       
  1021 
       
  1022 /* ===========================================================================
       
  1023  * Read a new buffer from the current input stream, update the adler32
       
  1024  * and total number of bytes read.  All deflate() input goes through
       
  1025  * this function so some applications may wish to modify it to avoid
       
  1026  * allocating a large strm->next_in buffer and copying from it.
       
  1027  * (See also flush_pending()).
       
  1028  */
       
  1029 #ifdef __SYMBIAN32__
       
  1030 local int read_buf(z_streamp strm,Bytef *  buf,unsigned  size)
       
  1031 #else
       
  1032 local int read_buf(strm, buf, size)
       
  1033     z_streamp strm;
       
  1034     Bytef *buf;
       
  1035     unsigned size;
       
  1036 #endif //__SYMBIAN32__
       
  1037 {
       
  1038     unsigned len = strm->avail_in;
       
  1039 
       
  1040     if (len > size) len = size;
       
  1041     if (len == 0) return 0;
       
  1042 
       
  1043     strm->avail_in  -= len;
       
  1044 
       
  1045     if (strm->state->wrap == 1) {
       
  1046         strm->adler = adler32_r(strm->adler, strm->next_in, len);
       
  1047     }
       
  1048 #ifdef GZIP
       
  1049     else if (strm->state->wrap == 2) {
       
  1050         strm->adler = crc32_r(strm->adler, strm->next_in, len);
       
  1051     }
       
  1052 #endif
       
  1053     zmemcpy(buf, strm->next_in, len);
       
  1054     strm->next_in  += len;
       
  1055     strm->total_in += len;
       
  1056 
       
  1057     return (int)len;
       
  1058 }
       
  1059 
       
  1060 /* ===========================================================================
       
  1061  * Initialize the "longest match" routines for a new zlib stream
       
  1062  */
       
  1063 
       
  1064 #ifdef __SYMBIAN32__
       
  1065 local void lm_init (deflate_state * s)
       
  1066 #else
       
  1067 local void lm_init (s)
       
  1068     deflate_state *s;
       
  1069 #endif //__SYMBIAN32__
       
  1070 {
       
  1071     s->window_size = (ulg)2L*s->w_size;
       
  1072 
       
  1073     CLEAR_HASH(s);
       
  1074 
       
  1075     /* Set the default configuration parameters:
       
  1076      */
       
  1077     s->max_lazy_match   = configuration_table[s->level].max_lazy;
       
  1078     s->good_match       = configuration_table[s->level].good_length;
       
  1079     s->nice_match       = configuration_table[s->level].nice_length;
       
  1080     s->max_chain_length = configuration_table[s->level].max_chain;
       
  1081 
       
  1082     s->strstart = 0;
       
  1083     s->block_start = 0L;
       
  1084     s->lookahead = 0;
       
  1085     s->match_length = s->prev_length = MIN_MATCH-1;
       
  1086     s->match_available = 0;
       
  1087     s->ins_h = 0;
       
  1088 #ifndef FASTEST
       
  1089 #ifdef ASMV
       
  1090     match_init(); /* initialize the asm code */
       
  1091 #endif
       
  1092 #endif
       
  1093 }
       
  1094 
       
  1095 #ifndef FASTEST
       
  1096 /* ===========================================================================
       
  1097  * Set match_start to the longest match starting at the given string and
       
  1098  * return its length. Matches shorter or equal to prev_length are discarded,
       
  1099  * in which case the result is equal to prev_length and match_start is
       
  1100  * garbage.
       
  1101  * IN assertions: cur_match is the head of the hash chain for the current
       
  1102  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
       
  1103  * OUT assertion: the match length is not greater than s->lookahead.
       
  1104  */
       
  1105 #ifndef ASMV
       
  1106 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
       
  1107  * match.S. The code will be functionally equivalent.
       
  1108  */
       
  1109 #ifdef __SYMBIAN32__
       
  1110 local uInt longest_match(deflate_state * s,IPos  cur_match)
       
  1111 #else
       
  1112 local uInt longest_match(s, cur_match)
       
  1113     deflate_state *s;
       
  1114     IPos cur_match;                             /* current match */
       
  1115 #endif //__SYMBIAN32__
       
  1116 {
       
  1117     unsigned chain_length = s->max_chain_length;/* max hash chain length */
       
  1118     register Bytef *scan = s->window + s->strstart; /* current string */
       
  1119     register Bytef *match;                       /* matched string */
       
  1120     register int len;                           /* length of current match */
       
  1121     int best_len = s->prev_length;              /* best match length so far */
       
  1122     int nice_match = s->nice_match;             /* stop if match long enough */
       
  1123     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
       
  1124         s->strstart - (IPos)MAX_DIST(s) : NIL;
       
  1125     /* Stop when cur_match becomes <= limit. To simplify the code,
       
  1126      * we prevent matches with the string of window index 0.
       
  1127      */
       
  1128     Posf *prev = s->prev;
       
  1129     uInt wmask = s->w_mask;
       
  1130 
       
  1131 #ifdef UNALIGNED_OK
       
  1132     /* Compare two bytes at a time. Note: this is not always beneficial.
       
  1133      * Try with and without -DUNALIGNED_OK to check.
       
  1134      */
       
  1135     register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
       
  1136     register ush scan_start = *(ushf*)scan;
       
  1137     register ush scan_end   = *(ushf*)(scan+best_len-1);
       
  1138 #else
       
  1139     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
       
  1140     register Byte scan_end1  = scan[best_len-1];
       
  1141     register Byte scan_end   = scan[best_len];
       
  1142 #endif
       
  1143 
       
  1144     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
       
  1145      * It is easy to get rid of this optimization if necessary.
       
  1146      */
       
  1147     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
       
  1148 
       
  1149     /* Do not waste too much time if we already have a good match: */
       
  1150     if (s->prev_length >= s->good_match) {
       
  1151         chain_length >>= 2;
       
  1152     }
       
  1153     /* Do not look for matches beyond the end of the input. This is necessary
       
  1154      * to make deflate deterministic.
       
  1155      */
       
  1156     if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
       
  1157 
       
  1158     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
       
  1159 
       
  1160     do {
       
  1161         Assert(cur_match < s->strstart, "no future");
       
  1162         match = s->window + cur_match;
       
  1163 
       
  1164         /* Skip to next match if the match length cannot increase
       
  1165          * or if the match length is less than 2.  Note that the checks below
       
  1166          * for insufficient lookahead only occur occasionally for performance
       
  1167          * reasons.  Therefore uninitialized memory will be accessed, and
       
  1168          * conditional jumps will be made that depend on those values.
       
  1169          * However the length of the match is limited to the lookahead, so
       
  1170          * the output of deflate is not affected by the uninitialized values.
       
  1171          */
       
  1172 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
       
  1173         /* This code assumes sizeof(unsigned short) == 2. Do not use
       
  1174          * UNALIGNED_OK if your compiler uses a different size.
       
  1175          */
       
  1176         if (*(ushf*)(match+best_len-1) != scan_end ||
       
  1177             *(ushf*)match != scan_start) continue;
       
  1178 
       
  1179         /* It is not necessary to compare scan[2] and match[2] since they are
       
  1180          * always equal when the other bytes match, given that the hash keys
       
  1181          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
       
  1182          * strstart+3, +5, ... up to strstart+257. We check for insufficient
       
  1183          * lookahead only every 4th comparison; the 128th check will be made
       
  1184          * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
       
  1185          * necessary to put more guard bytes at the end of the window, or
       
  1186          * to check more often for insufficient lookahead.
       
  1187          */
       
  1188         Assert(scan[2] == match[2], "scan[2]?");
       
  1189         scan++, match++;
       
  1190         do {
       
  1191         } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
       
  1192                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
       
  1193                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
       
  1194                  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
       
  1195                  scan < strend);
       
  1196         /* The funny "do {}" generates better code on most compilers */
       
  1197 
       
  1198         /* Here, scan <= window+strstart+257 */
       
  1199         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
       
  1200         if (*scan == *match) scan++;
       
  1201 
       
  1202         len = (MAX_MATCH - 1) - (int)(strend-scan);
       
  1203         scan = strend - (MAX_MATCH-1);
       
  1204 
       
  1205 #else /* UNALIGNED_OK */
       
  1206 
       
  1207         if (match[best_len]   != scan_end  ||
       
  1208             match[best_len-1] != scan_end1 ||
       
  1209             *match            != *scan     ||
       
  1210             *++match          != scan[1])      continue;
       
  1211 
       
  1212         /* The check at best_len-1 can be removed because it will be made
       
  1213          * again later. (This heuristic is not always a win.)
       
  1214          * It is not necessary to compare scan[2] and match[2] since they
       
  1215          * are always equal when the other bytes match, given that
       
  1216          * the hash keys are equal and that HASH_BITS >= 8.
       
  1217          */
       
  1218         scan += 2, match++;
       
  1219         Assert(*scan == *match, "match[2]?");
       
  1220 
       
  1221         /* We check for insufficient lookahead only every 8th comparison;
       
  1222          * the 256th check will be made at strstart+258.
       
  1223          */
       
  1224         do {
       
  1225         } while (*++scan == *++match && *++scan == *++match &&
       
  1226                  *++scan == *++match && *++scan == *++match &&
       
  1227                  *++scan == *++match && *++scan == *++match &&
       
  1228                  *++scan == *++match && *++scan == *++match &&
       
  1229                  scan < strend);
       
  1230 
       
  1231         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
       
  1232 
       
  1233         len = MAX_MATCH - (int)(strend - scan);
       
  1234         scan = strend - MAX_MATCH;
       
  1235 
       
  1236 #endif /* UNALIGNED_OK */
       
  1237 
       
  1238         if (len > best_len) {
       
  1239             s->match_start = cur_match;
       
  1240             best_len = len;
       
  1241             if (len >= nice_match) break;
       
  1242 #ifdef UNALIGNED_OK
       
  1243             scan_end = *(ushf*)(scan+best_len-1);
       
  1244 #else
       
  1245             scan_end1  = scan[best_len-1];
       
  1246             scan_end   = scan[best_len];
       
  1247 #endif
       
  1248         }
       
  1249     } while ((cur_match = prev[cur_match & wmask]) > limit
       
  1250              && --chain_length != 0);
       
  1251 
       
  1252     if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
       
  1253     return s->lookahead;
       
  1254 }
       
  1255 #endif /* ASMV */
       
  1256 #endif /* FASTEST */
       
  1257 
       
  1258 /* ---------------------------------------------------------------------------
       
  1259  * Optimized version for level == 1 or strategy == Z_RLE only
       
  1260  */
       
  1261 #ifdef __SYMBIAN32__
       
  1262  local uInt longest_match_fast(deflate_state * s,IPos cur_match)
       
  1263 #else
       
  1264 local uInt longest_match_fast(s, cur_match)
       
  1265     deflate_state *s;
       
  1266     IPos cur_match;                             /* current match */
       
  1267 #endif //__SYMBIAN32__
       
  1268 {
       
  1269     register Bytef *scan = s->window + s->strstart; /* current string */
       
  1270     register Bytef *match;                       /* matched string */
       
  1271     register int len;                           /* length of current match */
       
  1272     register Bytef *strend = s->window + s->strstart + MAX_MATCH;
       
  1273 
       
  1274     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
       
  1275      * It is easy to get rid of this optimization if necessary.
       
  1276      */
       
  1277     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
       
  1278 
       
  1279     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
       
  1280 
       
  1281     Assert(cur_match < s->strstart, "no future");
       
  1282 
       
  1283     match = s->window + cur_match;
       
  1284 
       
  1285     /* Return failure if the match length is less than 2:
       
  1286      */
       
  1287     if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
       
  1288 
       
  1289     /* The check at best_len-1 can be removed because it will be made
       
  1290      * again later. (This heuristic is not always a win.)
       
  1291      * It is not necessary to compare scan[2] and match[2] since they
       
  1292      * are always equal when the other bytes match, given that
       
  1293      * the hash keys are equal and that HASH_BITS >= 8.
       
  1294      */
       
  1295     scan += 2, match += 2;
       
  1296     Assert(*scan == *match, "match[2]?");
       
  1297 
       
  1298     /* We check for insufficient lookahead only every 8th comparison;
       
  1299      * the 256th check will be made at strstart+258.
       
  1300      */
       
  1301     do {
       
  1302     } while (*++scan == *++match && *++scan == *++match &&
       
  1303              *++scan == *++match && *++scan == *++match &&
       
  1304              *++scan == *++match && *++scan == *++match &&
       
  1305              *++scan == *++match && *++scan == *++match &&
       
  1306              scan < strend);
       
  1307 
       
  1308     Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
       
  1309 
       
  1310     len = MAX_MATCH - (int)(strend - scan);
       
  1311 
       
  1312     if (len < MIN_MATCH) return MIN_MATCH - 1;
       
  1313 
       
  1314     s->match_start = cur_match;
       
  1315     return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
       
  1316 }
       
  1317 
       
  1318 #ifdef DEBUG
       
  1319 /* ===========================================================================
       
  1320  * Check that the match at match_start is indeed a match.
       
  1321  */
       
  1322 #ifdef __SYMBIAN32__
       
  1323 local void check_match( deflate_state * s,IPos  start, match,int length)
       
  1324 #else  
       
  1325 local void check_match(s, start, match, length)
       
  1326     deflate_state *s;
       
  1327     IPos start, match;
       
  1328     int length;
       
  1329 #endif //__SYMBIAN32__
       
  1330 {
       
  1331     /* check that the match is indeed a match */
       
  1332     if (zmemcmp(s->window + match,
       
  1333                 s->window + start, length) != EQUAL) {
       
  1334         fprintf(stderr, " start %u, match %u, length %d\n",
       
  1335                 start, match, length);
       
  1336         do {
       
  1337             fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
       
  1338         } while (--length != 0);
       
  1339         z_error("invalid match");
       
  1340     }
       
  1341     if (z_verbose > 1) {
       
  1342         fprintf(stderr,"\\[%d,%d]", start-match, length);
       
  1343         do { putc(s->window[start++], stderr); } while (--length != 0);
       
  1344     }
       
  1345 }
       
  1346 #else
       
  1347 #  define check_match(s, start, match, length)
       
  1348 #endif /* DEBUG */
       
  1349 
       
  1350 /* ===========================================================================
       
  1351  * Fill the window when the lookahead becomes insufficient.
       
  1352  * Updates strstart and lookahead.
       
  1353  *
       
  1354  * IN assertion: lookahead < MIN_LOOKAHEAD
       
  1355  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
       
  1356  *    At least one byte has been read, or avail_in == 0; reads are
       
  1357  *    performed for at least two bytes (required for the zip translate_eol
       
  1358  *    option -- not supported here).
       
  1359  */
       
  1360 #ifdef __SYMBIAN32__
       
  1361 local void fill_window(   deflate_state * s)
       
  1362 #else    
       
  1363 local void fill_window(s)
       
  1364     deflate_state *s;
       
  1365 #endif //__SYMBIAN32__
       
  1366 {
       
  1367     register unsigned n, m;
       
  1368     register Posf *p;
       
  1369     unsigned more;    /* Amount of free space at the end of the window. */
       
  1370     uInt wsize = s->w_size;
       
  1371 
       
  1372     do {
       
  1373         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
       
  1374 
       
  1375         /* Deal with !@#$% 64K limit: */
       
  1376         if (sizeof(int) <= 2) {
       
  1377             if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
       
  1378                 more = wsize;
       
  1379 
       
  1380             } else if (more == (unsigned)(-1)) {
       
  1381                 /* Very unlikely, but possible on 16 bit machine if
       
  1382                  * strstart == 0 && lookahead == 1 (input done a byte at time)
       
  1383                  */
       
  1384                 more--;
       
  1385             }
       
  1386         }
       
  1387 
       
  1388         /* If the window is almost full and there is insufficient lookahead,
       
  1389          * move the upper half to the lower one to make room in the upper half.
       
  1390          */
       
  1391         if (s->strstart >= wsize+MAX_DIST(s)) {
       
  1392 
       
  1393             zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
       
  1394             s->match_start -= wsize;
       
  1395             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
       
  1396             s->block_start -= (long) wsize;
       
  1397 
       
  1398             /* Slide the hash table (could be avoided with 32 bit values
       
  1399                at the expense of memory usage). We slide even when level == 0
       
  1400                to keep the hash table consistent if we switch back to level > 0
       
  1401                later. (Using level 0 permanently is not an optimal usage of
       
  1402                zlib, so we don't care about this pathological case.)
       
  1403              */
       
  1404             /* %%% avoid this when Z_RLE */
       
  1405             n = s->hash_size;
       
  1406             p = &s->head[n];
       
  1407             do {
       
  1408                 m = *--p;
       
  1409                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
       
  1410             } while (--n);
       
  1411 
       
  1412             n = wsize;
       
  1413 #ifndef FASTEST
       
  1414             p = &s->prev[n];
       
  1415             do {
       
  1416                 m = *--p;
       
  1417                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
       
  1418                 /* If n is not on any hash chain, prev[n] is garbage but
       
  1419                  * its value will never be used.
       
  1420                  */
       
  1421             } while (--n);
       
  1422 #endif
       
  1423             more += wsize;
       
  1424         }
       
  1425         if (s->strm->avail_in == 0) return;
       
  1426 
       
  1427         /* If there was no sliding:
       
  1428          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
       
  1429          *    more == window_size - lookahead - strstart
       
  1430          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
       
  1431          * => more >= window_size - 2*WSIZE + 2
       
  1432          * In the BIG_MEM or MMAP case (not yet supported),
       
  1433          *   window_size == input_size + MIN_LOOKAHEAD  &&
       
  1434          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
       
  1435          * Otherwise, window_size == 2*WSIZE so more >= 2.
       
  1436          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
       
  1437          */
       
  1438         Assert(more >= 2, "more < 2");
       
  1439 
       
  1440         n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
       
  1441         s->lookahead += n;
       
  1442 
       
  1443         /* Initialize the hash value now that we have some input: */
       
  1444         if (s->lookahead >= MIN_MATCH) {
       
  1445             s->ins_h = s->window[s->strstart];
       
  1446             UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
       
  1447 #if MIN_MATCH != 3
       
  1448             Call UPDATE_HASH() MIN_MATCH-3 more times
       
  1449 #endif
       
  1450         }
       
  1451         /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
       
  1452          * but this is not important since only literal bytes will be emitted.
       
  1453          */
       
  1454 
       
  1455     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
       
  1456 }
       
  1457 
       
  1458 /* ===========================================================================
       
  1459  * Flush the current block, with given end-of-file flag.
       
  1460  * IN assertion: strstart is set to the end of the current match.
       
  1461  */
       
  1462 #define FLUSH_BLOCK_ONLY(s, eof) { \
       
  1463    _tr_flush_block(s, (s->block_start >= 0L ? \
       
  1464                    (charf *)&s->window[(unsigned)s->block_start] : \
       
  1465                    (charf *)Z_NULL), \
       
  1466                 (ulg)((long)s->strstart - s->block_start), \
       
  1467                 (eof)); \
       
  1468    s->block_start = s->strstart; \
       
  1469    flush_pending(s->strm); \
       
  1470    Tracev((stderr,"[FLUSH]")); \
       
  1471 }
       
  1472 
       
  1473 /* Same but force premature exit if necessary. */
       
  1474 #define FLUSH_BLOCK(s, eof) { \
       
  1475    FLUSH_BLOCK_ONLY(s, eof); \
       
  1476    if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
       
  1477 }
       
  1478 
       
  1479 /* ===========================================================================
       
  1480  * Copy without compression as much as possible from the input stream, return
       
  1481  * the current block state.
       
  1482  * This function does not insert new strings in the dictionary since
       
  1483  * uncompressible data is probably not useful. This function is used
       
  1484  * only for the level=0 compression option.
       
  1485  * NOTE: this function should be optimized to avoid extra copying from
       
  1486  * window to pending_buf.
       
  1487  */
       
  1488  #ifdef __SYMBIAN32__
       
  1489  local block_state deflate_stored(deflate_state * s,int flush)
       
  1490 #else
       
  1491 local block_state deflate_stored(s, flush)
       
  1492     deflate_state *s;
       
  1493     int flush;
       
  1494 #endif //__SYMBIAN32__
       
  1495 {
       
  1496     /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
       
  1497      * to pending_buf_size, and each stored block has a 5 byte header:
       
  1498      */
       
  1499     ulg max_block_size = 0xffff;
       
  1500     ulg max_start;
       
  1501 
       
  1502     if (max_block_size > s->pending_buf_size - 5) {
       
  1503         max_block_size = s->pending_buf_size - 5;
       
  1504     }
       
  1505 
       
  1506     /* Copy as much as possible from input to output: */
       
  1507     for (;;) {
       
  1508         /* Fill the window as much as possible: */
       
  1509         if (s->lookahead <= 1) {
       
  1510 
       
  1511             Assert(s->strstart < s->w_size+MAX_DIST(s) ||
       
  1512                    s->block_start >= (long)s->w_size, "slide too late");
       
  1513 
       
  1514             fill_window(s);
       
  1515             if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
       
  1516 
       
  1517             if (s->lookahead == 0) break; /* flush the current block */
       
  1518         }
       
  1519         Assert(s->block_start >= 0L, "block gone");
       
  1520 
       
  1521         s->strstart += s->lookahead;
       
  1522         s->lookahead = 0;
       
  1523 
       
  1524         /* Emit a stored block if pending_buf will be full: */
       
  1525         max_start = s->block_start + max_block_size;
       
  1526         if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
       
  1527             /* strstart == 0 is possible when wraparound on 16-bit machine */
       
  1528             s->lookahead = (uInt)(s->strstart - max_start);
       
  1529             s->strstart = (uInt)max_start;
       
  1530             FLUSH_BLOCK(s, 0);
       
  1531         }
       
  1532         /* Flush if we may have to slide, otherwise block_start may become
       
  1533          * negative and the data will be gone:
       
  1534          */
       
  1535         if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
       
  1536             FLUSH_BLOCK(s, 0);
       
  1537         }
       
  1538     }
       
  1539     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1540     return flush == Z_FINISH ? finish_done : block_done;
       
  1541 }
       
  1542 
       
  1543 /* ===========================================================================
       
  1544  * Compress as much as possible from the input stream, return the current
       
  1545  * block state.
       
  1546  * This function does not perform lazy evaluation of matches and inserts
       
  1547  * new strings in the dictionary only for unmatched strings or for short
       
  1548  * matches. It is used only for the fast compression options.
       
  1549  */
       
  1550 #ifdef __SYMBIAN32__
       
  1551  local block_state deflate_fast(   deflate_state * s,int  flush)
       
  1552 #else	
       
  1553 local block_state deflate_fast(s, flush)
       
  1554     deflate_state *s;
       
  1555     int flush;
       
  1556 #endif //__SYMBIAN32__
       
  1557 {
       
  1558     IPos hash_head = NIL; /* head of the hash chain */
       
  1559     int bflush;           /* set if current block must be flushed */
       
  1560 
       
  1561     for (;;) {
       
  1562         /* Make sure that we always have enough lookahead, except
       
  1563          * at the end of the input file. We need MAX_MATCH bytes
       
  1564          * for the next match, plus MIN_MATCH bytes to insert the
       
  1565          * string following the next match.
       
  1566          */
       
  1567         if (s->lookahead < MIN_LOOKAHEAD) {
       
  1568             fill_window(s);
       
  1569             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
       
  1570                 return need_more;
       
  1571             }
       
  1572             if (s->lookahead == 0) break; /* flush the current block */
       
  1573         }
       
  1574 
       
  1575         /* Insert the string window[strstart .. strstart+2] in the
       
  1576          * dictionary, and set hash_head to the head of the hash chain:
       
  1577          */
       
  1578         if (s->lookahead >= MIN_MATCH) {
       
  1579             INSERT_STRING(s, s->strstart, hash_head);
       
  1580         }
       
  1581 
       
  1582         /* Find the longest match, discarding those <= prev_length.
       
  1583          * At this point we have always match_length < MIN_MATCH
       
  1584          */
       
  1585         if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
       
  1586             /* To simplify the code, we prevent matches with the string
       
  1587              * of window index 0 (in particular we have to avoid a match
       
  1588              * of the string with itself at the start of the input file).
       
  1589              */
       
  1590 #ifdef FASTEST
       
  1591             if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
       
  1592                 (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
       
  1593                 s->match_length = longest_match_fast (s, hash_head);
       
  1594             }
       
  1595 #else
       
  1596             if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
       
  1597                 s->match_length = longest_match (s, hash_head);
       
  1598             } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
       
  1599                 s->match_length = longest_match_fast (s, hash_head);
       
  1600             }
       
  1601 #endif
       
  1602             /* longest_match() or longest_match_fast() sets match_start */
       
  1603         }
       
  1604         if (s->match_length >= MIN_MATCH) {
       
  1605             check_match(s, s->strstart, s->match_start, s->match_length);
       
  1606 
       
  1607             _tr_tally_dist(s, s->strstart - s->match_start,
       
  1608                            s->match_length - MIN_MATCH, bflush);
       
  1609 
       
  1610             s->lookahead -= s->match_length;
       
  1611 
       
  1612             /* Insert new strings in the hash table only if the match length
       
  1613              * is not too large. This saves time but degrades compression.
       
  1614              */
       
  1615 #ifndef FASTEST
       
  1616             if (s->match_length <= s->max_insert_length &&
       
  1617                 s->lookahead >= MIN_MATCH) {
       
  1618                 s->match_length--; /* string at strstart already in table */
       
  1619                 do {
       
  1620                     s->strstart++;
       
  1621                     INSERT_STRING(s, s->strstart, hash_head);
       
  1622                     /* strstart never exceeds WSIZE-MAX_MATCH, so there are
       
  1623                      * always MIN_MATCH bytes ahead.
       
  1624                      */
       
  1625                 } while (--s->match_length != 0);
       
  1626                 s->strstart++;
       
  1627             } else
       
  1628 #endif
       
  1629             {
       
  1630                 s->strstart += s->match_length;
       
  1631                 s->match_length = 0;
       
  1632                 s->ins_h = s->window[s->strstart];
       
  1633                 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
       
  1634 #if MIN_MATCH != 3
       
  1635                 Call UPDATE_HASH() MIN_MATCH-3 more times
       
  1636 #endif
       
  1637                 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
       
  1638                  * matter since it will be recomputed at next deflate call.
       
  1639                  */
       
  1640             }
       
  1641         } else {
       
  1642             /* No match, output a literal byte */
       
  1643             Tracevv((stderr,"%c", s->window[s->strstart]));
       
  1644             _tr_tally_lit (s, s->window[s->strstart], bflush);
       
  1645             s->lookahead--;
       
  1646             s->strstart++;
       
  1647         }
       
  1648         if (bflush) FLUSH_BLOCK(s, 0);
       
  1649     }
       
  1650     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1651     return flush == Z_FINISH ? finish_done : block_done;
       
  1652 }
       
  1653 
       
  1654 #ifndef FASTEST
       
  1655 /* ===========================================================================
       
  1656  * Same as above, but achieves better compression. We use a lazy
       
  1657  * evaluation for matches: a match is finally adopted only if there is
       
  1658  * no better match at the next window position.
       
  1659  */
       
  1660 #ifdef __SYMBIAN32__
       
  1661  local block_state deflate_slow(  deflate_state * s,int  flush)
       
  1662 #else
       
  1663 local block_state deflate_slow(s, flush)
       
  1664     deflate_state *s;
       
  1665     int flush;
       
  1666 #endif //__SYMBIAN32__
       
  1667 {
       
  1668     IPos hash_head = NIL;    /* head of hash chain */
       
  1669     int bflush;              /* set if current block must be flushed */
       
  1670 
       
  1671     /* Process the input block. */
       
  1672     for (;;) {
       
  1673         /* Make sure that we always have enough lookahead, except
       
  1674          * at the end of the input file. We need MAX_MATCH bytes
       
  1675          * for the next match, plus MIN_MATCH bytes to insert the
       
  1676          * string following the next match.
       
  1677          */
       
  1678         if (s->lookahead < MIN_LOOKAHEAD) {
       
  1679             fill_window(s);
       
  1680             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
       
  1681                 return need_more;
       
  1682             }
       
  1683             if (s->lookahead == 0) break; /* flush the current block */
       
  1684         }
       
  1685 
       
  1686         /* Insert the string window[strstart .. strstart+2] in the
       
  1687          * dictionary, and set hash_head to the head of the hash chain:
       
  1688          */
       
  1689         if (s->lookahead >= MIN_MATCH) {
       
  1690             INSERT_STRING(s, s->strstart, hash_head);
       
  1691         }
       
  1692 
       
  1693         /* Find the longest match, discarding those <= prev_length.
       
  1694          */
       
  1695         s->prev_length = s->match_length, s->prev_match = s->match_start;
       
  1696         s->match_length = MIN_MATCH-1;
       
  1697 
       
  1698         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
       
  1699             s->strstart - hash_head <= MAX_DIST(s)) {
       
  1700             /* To simplify the code, we prevent matches with the string
       
  1701              * of window index 0 (in particular we have to avoid a match
       
  1702              * of the string with itself at the start of the input file).
       
  1703              */
       
  1704             if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
       
  1705                 s->match_length = longest_match (s, hash_head);
       
  1706             } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
       
  1707                 s->match_length = longest_match_fast (s, hash_head);
       
  1708             }
       
  1709             /* longest_match() or longest_match_fast() sets match_start */
       
  1710 
       
  1711             if (s->match_length <= 5 && (s->strategy == Z_FILTERED
       
  1712 #if TOO_FAR <= 32767
       
  1713                 || (s->match_length == MIN_MATCH &&
       
  1714                     s->strstart - s->match_start > TOO_FAR)
       
  1715 #endif
       
  1716                 )) {
       
  1717 
       
  1718                 /* If prev_match is also MIN_MATCH, match_start is garbage
       
  1719                  * but we will ignore the current match anyway.
       
  1720                  */
       
  1721                 s->match_length = MIN_MATCH-1;
       
  1722             }
       
  1723         }
       
  1724         /* If there was a match at the previous step and the current
       
  1725          * match is not better, output the previous match:
       
  1726          */
       
  1727         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
       
  1728             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
       
  1729             /* Do not insert strings in hash table beyond this. */
       
  1730 
       
  1731             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
       
  1732 
       
  1733             _tr_tally_dist(s, s->strstart -1 - s->prev_match,
       
  1734                            s->prev_length - MIN_MATCH, bflush);
       
  1735 
       
  1736             /* Insert in hash table all strings up to the end of the match.
       
  1737              * strstart-1 and strstart are already inserted. If there is not
       
  1738              * enough lookahead, the last two strings are not inserted in
       
  1739              * the hash table.
       
  1740              */
       
  1741             s->lookahead -= s->prev_length-1;
       
  1742             s->prev_length -= 2;
       
  1743             do {
       
  1744                 if (++s->strstart <= max_insert) {
       
  1745                     INSERT_STRING(s, s->strstart, hash_head);
       
  1746                 }
       
  1747             } while (--s->prev_length != 0);
       
  1748             s->match_available = 0;
       
  1749             s->match_length = MIN_MATCH-1;
       
  1750             s->strstart++;
       
  1751 
       
  1752             if (bflush) FLUSH_BLOCK(s, 0);
       
  1753 
       
  1754         } else if (s->match_available) {
       
  1755             /* If there was no match at the previous position, output a
       
  1756              * single literal. If there was a match but the current match
       
  1757              * is longer, truncate the previous match to a single literal.
       
  1758              */
       
  1759             Tracevv((stderr,"%c", s->window[s->strstart-1]));
       
  1760             _tr_tally_lit(s, s->window[s->strstart-1], bflush);
       
  1761             if (bflush) {
       
  1762                 FLUSH_BLOCK_ONLY(s, 0);
       
  1763             }
       
  1764             s->strstart++;
       
  1765             s->lookahead--;
       
  1766             if (s->strm->avail_out == 0) return need_more;
       
  1767         } else {
       
  1768             /* There is no previous match to compare with, wait for
       
  1769              * the next step to decide.
       
  1770              */
       
  1771             s->match_available = 1;
       
  1772             s->strstart++;
       
  1773             s->lookahead--;
       
  1774         }
       
  1775     }
       
  1776     Assert (flush != Z_NO_FLUSH, "no flush?");
       
  1777     if (s->match_available) {
       
  1778         Tracevv((stderr,"%c", s->window[s->strstart-1]));
       
  1779         _tr_tally_lit(s, s->window[s->strstart-1], bflush);
       
  1780         s->match_available = 0;
       
  1781     }
       
  1782     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1783     return flush == Z_FINISH ? finish_done : block_done;
       
  1784 }
       
  1785 #endif /* FASTEST */
       
  1786 
       
  1787 #if 0
       
  1788 /* ===========================================================================
       
  1789  * For Z_RLE, simply look for runs of bytes, generate matches only of distance
       
  1790  * one.  Do not maintain a hash table.  (It will be regenerated if this run of
       
  1791  * deflate switches away from Z_RLE.)
       
  1792  */
       
  1793 local block_state deflate_rle(s, flush)
       
  1794     deflate_state *s;
       
  1795     int flush;
       
  1796 {
       
  1797     int bflush;         /* set if current block must be flushed */
       
  1798     uInt run;           /* length of run */
       
  1799     uInt max;           /* maximum length of run */
       
  1800     uInt prev;          /* byte at distance one to match */
       
  1801     Bytef *scan;        /* scan for end of run */
       
  1802 
       
  1803     for (;;) {
       
  1804         /* Make sure that we always have enough lookahead, except
       
  1805          * at the end of the input file. We need MAX_MATCH bytes
       
  1806          * for the longest encodable run.
       
  1807          */
       
  1808         if (s->lookahead < MAX_MATCH) {
       
  1809             fill_window(s);
       
  1810             if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
       
  1811                 return need_more;
       
  1812             }
       
  1813             if (s->lookahead == 0) break; /* flush the current block */
       
  1814         }
       
  1815 
       
  1816         /* See how many times the previous byte repeats */
       
  1817         run = 0;
       
  1818         if (s->strstart > 0) {      /* if there is a previous byte, that is */
       
  1819             max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
       
  1820             scan = s->window + s->strstart - 1;
       
  1821             prev = *scan++;
       
  1822             do {
       
  1823                 if (*scan++ != prev)
       
  1824                     break;
       
  1825             } while (++run < max);
       
  1826         }
       
  1827 
       
  1828         /* Emit match if have run of MIN_MATCH or longer, else emit literal */
       
  1829         if (run >= MIN_MATCH) {
       
  1830             check_match(s, s->strstart, s->strstart - 1, run);
       
  1831             _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
       
  1832             s->lookahead -= run;
       
  1833             s->strstart += run;
       
  1834         } else {
       
  1835             /* No match, output a literal byte */
       
  1836             Tracevv((stderr,"%c", s->window[s->strstart]));
       
  1837             _tr_tally_lit (s, s->window[s->strstart], bflush);
       
  1838             s->lookahead--;
       
  1839             s->strstart++;
       
  1840         }
       
  1841         if (bflush) FLUSH_BLOCK(s, 0);
       
  1842     }
       
  1843     FLUSH_BLOCK(s, flush == Z_FINISH);
       
  1844     return flush == Z_FINISH ? finish_done : block_done;
       
  1845 }
       
  1846 #endif
       
  1847 
       
  1848 
       
  1849