Back to home page

OSCL-LXR

 
 

    


0001 /* +++ deflate.c */
0002 /* deflate.c -- compress data using the deflation algorithm
0003  * Copyright (C) 1995-1996 Jean-loup Gailly.
0004  * For conditions of distribution and use, see copyright notice in zlib.h 
0005  */
0006 
0007 /*
0008  *  ALGORITHM
0009  *
0010  *      The "deflation" process depends on being able to identify portions
0011  *      of the input text which are identical to earlier input (within a
0012  *      sliding window trailing behind the input currently being processed).
0013  *
0014  *      The most straightforward technique turns out to be the fastest for
0015  *      most input files: try all possible matches and select the longest.
0016  *      The key feature of this algorithm is that insertions into the string
0017  *      dictionary are very simple and thus fast, and deletions are avoided
0018  *      completely. Insertions are performed at each input character, whereas
0019  *      string matches are performed only when the previous match ends. So it
0020  *      is preferable to spend more time in matches to allow very fast string
0021  *      insertions and avoid deletions. The matching algorithm for small
0022  *      strings is inspired from that of Rabin & Karp. A brute force approach
0023  *      is used to find longer strings when a small match has been found.
0024  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
0025  *      (by Leonid Broukhis).
0026  *         A previous version of this file used a more sophisticated algorithm
0027  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
0028  *      time, but has a larger average cost, uses more memory and is patented.
0029  *      However the F&G algorithm may be faster for some highly redundant
0030  *      files if the parameter max_chain_length (described below) is too large.
0031  *
0032  *  ACKNOWLEDGEMENTS
0033  *
0034  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
0035  *      I found it in 'freeze' written by Leonid Broukhis.
0036  *      Thanks to many people for bug reports and testing.
0037  *
0038  *  REFERENCES
0039  *
0040  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
0041  *      Available in ftp://ds.internic.net/rfc/rfc1951.txt
0042  *
0043  *      A description of the Rabin and Karp algorithm is given in the book
0044  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
0045  *
0046  *      Fiala,E.R., and Greene,D.H.
0047  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
0048  *
0049  */
0050 
0051 #include <linux/module.h>
0052 #include <linux/zutil.h>
0053 #include "defutil.h"
0054 
0055 /* architecture-specific bits */
0056 #ifdef CONFIG_ZLIB_DFLTCC
0057 #  include "../zlib_dfltcc/dfltcc.h"
0058 #else
0059 #define DEFLATE_RESET_HOOK(strm) do {} while (0)
0060 #define DEFLATE_HOOK(strm, flush, bstate) 0
0061 #define DEFLATE_NEED_CHECKSUM(strm) 1
0062 #define DEFLATE_DFLTCC_ENABLED() 0
0063 #endif
0064 
0065 /* ===========================================================================
0066  *  Function prototypes.
0067  */
0068 
0069 typedef block_state (*compress_func) (deflate_state *s, int flush);
0070 /* Compression function. Returns the block state after the call. */
0071 
0072 static void fill_window    (deflate_state *s);
0073 static block_state deflate_stored (deflate_state *s, int flush);
0074 static block_state deflate_fast   (deflate_state *s, int flush);
0075 static block_state deflate_slow   (deflate_state *s, int flush);
0076 static void lm_init        (deflate_state *s);
0077 static void putShortMSB    (deflate_state *s, uInt b);
0078 static int read_buf        (z_streamp strm, Byte *buf, unsigned size);
0079 static uInt longest_match  (deflate_state *s, IPos cur_match);
0080 
0081 #ifdef DEBUG_ZLIB
0082 static  void check_match (deflate_state *s, IPos start, IPos match,
0083                          int length);
0084 #endif
0085 
0086 /* ===========================================================================
0087  * Local data
0088  */
0089 
0090 #define NIL 0
0091 /* Tail of hash chains */
0092 
0093 #ifndef TOO_FAR
0094 #  define TOO_FAR 4096
0095 #endif
0096 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
0097 
0098 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
0099 /* Minimum amount of lookahead, except at the end of the input file.
0100  * See deflate.c for comments about the MIN_MATCH+1.
0101  */
0102 
0103 /* Workspace to be allocated for deflate processing */
0104 typedef struct deflate_workspace {
0105     /* State memory for the deflator */
0106     deflate_state deflate_memory;
0107 #ifdef CONFIG_ZLIB_DFLTCC
0108     /* State memory for s390 hardware deflate */
0109     struct dfltcc_state dfltcc_memory;
0110 #endif
0111     Byte *window_memory;
0112     Pos *prev_memory;
0113     Pos *head_memory;
0114     char *overlay_memory;
0115 } deflate_workspace;
0116 
0117 #ifdef CONFIG_ZLIB_DFLTCC
0118 /* dfltcc_state must be doubleword aligned for DFLTCC call */
0119 static_assert(offsetof(struct deflate_workspace, dfltcc_memory) % 8 == 0);
0120 #endif
0121 
0122 /* Values for max_lazy_match, good_match and max_chain_length, depending on
0123  * the desired pack level (0..9). The values given below have been tuned to
0124  * exclude worst case performance for pathological files. Better values may be
0125  * found for specific files.
0126  */
0127 typedef struct config_s {
0128    ush good_length; /* reduce lazy search above this match length */
0129    ush max_lazy;    /* do not perform lazy search above this match length */
0130    ush nice_length; /* quit search above this match length */
0131    ush max_chain;
0132    compress_func func;
0133 } config;
0134 
0135 static const config configuration_table[10] = {
0136 /*      good lazy nice chain */
0137 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
0138 /* 1 */ {4,    4,  8,    4, deflate_fast}, /* maximum speed, no lazy matches */
0139 /* 2 */ {4,    5, 16,    8, deflate_fast},
0140 /* 3 */ {4,    6, 32,   32, deflate_fast},
0141 
0142 /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
0143 /* 5 */ {8,   16, 32,   32, deflate_slow},
0144 /* 6 */ {8,   16, 128, 128, deflate_slow},
0145 /* 7 */ {8,   32, 128, 256, deflate_slow},
0146 /* 8 */ {32, 128, 258, 1024, deflate_slow},
0147 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
0148 
0149 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
0150  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
0151  * meaning.
0152  */
0153 
0154 #define EQUAL 0
0155 /* result of memcmp for equal strings */
0156 
0157 /* ===========================================================================
0158  * Update a hash value with the given input byte
0159  * IN  assertion: all calls to UPDATE_HASH are made with consecutive
0160  *    input characters, so that a running hash key can be computed from the
0161  *    previous key instead of complete recalculation each time.
0162  */
0163 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
0164 
0165 
0166 /* ===========================================================================
0167  * Insert string str in the dictionary and set match_head to the previous head
0168  * of the hash chain (the most recent string with same hash key). Return
0169  * the previous length of the hash chain.
0170  * IN  assertion: all calls to INSERT_STRING are made with consecutive
0171  *    input characters and the first MIN_MATCH bytes of str are valid
0172  *    (except for the last MIN_MATCH-1 bytes of the input file).
0173  */
0174 #define INSERT_STRING(s, str, match_head) \
0175    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
0176     s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
0177     s->head[s->ins_h] = (Pos)(str))
0178 
0179 /* ===========================================================================
0180  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
0181  * prev[] will be initialized on the fly.
0182  */
0183 #define CLEAR_HASH(s) \
0184     s->head[s->hash_size-1] = NIL; \
0185     memset((char *)s->head, 0, (unsigned)(s->hash_size-1)*sizeof(*s->head));
0186 
0187 /* ========================================================================= */
0188 int zlib_deflateInit2(
0189     z_streamp strm,
0190     int  level,
0191     int  method,
0192     int  windowBits,
0193     int  memLevel,
0194     int  strategy
0195 )
0196 {
0197     deflate_state *s;
0198     int noheader = 0;
0199     deflate_workspace *mem;
0200     char *next;
0201 
0202     ush *overlay;
0203     /* We overlay pending_buf and d_buf+l_buf. This works since the average
0204      * output size for (length,distance) codes is <= 24 bits.
0205      */
0206 
0207     if (strm == NULL) return Z_STREAM_ERROR;
0208 
0209     strm->msg = NULL;
0210 
0211     if (level == Z_DEFAULT_COMPRESSION) level = 6;
0212 
0213     mem = (deflate_workspace *) strm->workspace;
0214 
0215     if (windowBits < 0) { /* undocumented feature: suppress zlib header */
0216         noheader = 1;
0217         windowBits = -windowBits;
0218     }
0219     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
0220         windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
0221     strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
0222         return Z_STREAM_ERROR;
0223     }
0224 
0225     /*
0226      * Direct the workspace's pointers to the chunks that were allocated
0227      * along with the deflate_workspace struct.
0228      */
0229     next = (char *) mem;
0230     next += sizeof(*mem);
0231 #ifdef CONFIG_ZLIB_DFLTCC
0232     /*
0233      *  DFLTCC requires the window to be page aligned.
0234      *  Thus, we overallocate and take the aligned portion of the buffer.
0235      */
0236     mem->window_memory = (Byte *) PTR_ALIGN(next, PAGE_SIZE);
0237 #else
0238     mem->window_memory = (Byte *) next;
0239 #endif
0240     next += zlib_deflate_window_memsize(windowBits);
0241     mem->prev_memory = (Pos *) next;
0242     next += zlib_deflate_prev_memsize(windowBits);
0243     mem->head_memory = (Pos *) next;
0244     next += zlib_deflate_head_memsize(memLevel);
0245     mem->overlay_memory = next;
0246 
0247     s = (deflate_state *) &(mem->deflate_memory);
0248     strm->state = (struct internal_state *)s;
0249     s->strm = strm;
0250 
0251     s->noheader = noheader;
0252     s->w_bits = windowBits;
0253     s->w_size = 1 << s->w_bits;
0254     s->w_mask = s->w_size - 1;
0255 
0256     s->hash_bits = memLevel + 7;
0257     s->hash_size = 1 << s->hash_bits;
0258     s->hash_mask = s->hash_size - 1;
0259     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
0260 
0261     s->window = (Byte *) mem->window_memory;
0262     s->prev   = (Pos *)  mem->prev_memory;
0263     s->head   = (Pos *)  mem->head_memory;
0264 
0265     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
0266 
0267     overlay = (ush *) mem->overlay_memory;
0268     s->pending_buf = (uch *) overlay;
0269     s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
0270 
0271     s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
0272     s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
0273 
0274     s->level = level;
0275     s->strategy = strategy;
0276     s->method = (Byte)method;
0277 
0278     return zlib_deflateReset(strm);
0279 }
0280 
0281 /* ========================================================================= */
0282 int zlib_deflateReset(
0283     z_streamp strm
0284 )
0285 {
0286     deflate_state *s;
0287     
0288     if (strm == NULL || strm->state == NULL)
0289         return Z_STREAM_ERROR;
0290 
0291     strm->total_in = strm->total_out = 0;
0292     strm->msg = NULL;
0293     strm->data_type = Z_UNKNOWN;
0294 
0295     s = (deflate_state *)strm->state;
0296     s->pending = 0;
0297     s->pending_out = s->pending_buf;
0298 
0299     if (s->noheader < 0) {
0300         s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
0301     }
0302     s->status = s->noheader ? BUSY_STATE : INIT_STATE;
0303     strm->adler = 1;
0304     s->last_flush = Z_NO_FLUSH;
0305 
0306     zlib_tr_init(s);
0307     lm_init(s);
0308 
0309     DEFLATE_RESET_HOOK(strm);
0310 
0311     return Z_OK;
0312 }
0313 
0314 /* =========================================================================
0315  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
0316  * IN assertion: the stream state is correct and there is enough room in
0317  * pending_buf.
0318  */
0319 static void putShortMSB(
0320     deflate_state *s,
0321     uInt b
0322 )
0323 {
0324     put_byte(s, (Byte)(b >> 8));
0325     put_byte(s, (Byte)(b & 0xff));
0326 }   
0327 
0328 /* ========================================================================= */
0329 int zlib_deflate(
0330     z_streamp strm,
0331     int flush
0332 )
0333 {
0334     int old_flush; /* value of flush param for previous deflate call */
0335     deflate_state *s;
0336 
0337     if (strm == NULL || strm->state == NULL ||
0338     flush > Z_FINISH || flush < 0) {
0339         return Z_STREAM_ERROR;
0340     }
0341     s = (deflate_state *) strm->state;
0342 
0343     if ((strm->next_in == NULL && strm->avail_in != 0) ||
0344     (s->status == FINISH_STATE && flush != Z_FINISH)) {
0345         return Z_STREAM_ERROR;
0346     }
0347     if (strm->avail_out == 0) return Z_BUF_ERROR;
0348 
0349     s->strm = strm; /* just in case */
0350     old_flush = s->last_flush;
0351     s->last_flush = flush;
0352 
0353     /* Write the zlib header */
0354     if (s->status == INIT_STATE) {
0355 
0356         uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
0357         uInt level_flags = (s->level-1) >> 1;
0358 
0359         if (level_flags > 3) level_flags = 3;
0360         header |= (level_flags << 6);
0361     if (s->strstart != 0) header |= PRESET_DICT;
0362         header += 31 - (header % 31);
0363 
0364         s->status = BUSY_STATE;
0365         putShortMSB(s, header);
0366 
0367     /* Save the adler32 of the preset dictionary: */
0368     if (s->strstart != 0) {
0369         putShortMSB(s, (uInt)(strm->adler >> 16));
0370         putShortMSB(s, (uInt)(strm->adler & 0xffff));
0371     }
0372     strm->adler = 1L;
0373     }
0374 
0375     /* Flush as much pending output as possible */
0376     if (s->pending != 0) {
0377         flush_pending(strm);
0378         if (strm->avail_out == 0) {
0379         /* Since avail_out is 0, deflate will be called again with
0380          * more output space, but possibly with both pending and
0381          * avail_in equal to zero. There won't be anything to do,
0382          * but this is not an error situation so make sure we
0383          * return OK instead of BUF_ERROR at next call of deflate:
0384              */
0385         s->last_flush = -1;
0386         return Z_OK;
0387     }
0388 
0389     /* Make sure there is something to do and avoid duplicate consecutive
0390      * flushes. For repeated and useless calls with Z_FINISH, we keep
0391      * returning Z_STREAM_END instead of Z_BUFF_ERROR.
0392      */
0393     } else if (strm->avail_in == 0 && flush <= old_flush &&
0394            flush != Z_FINISH) {
0395         return Z_BUF_ERROR;
0396     }
0397 
0398     /* User must not provide more input after the first FINISH: */
0399     if (s->status == FINISH_STATE && strm->avail_in != 0) {
0400         return Z_BUF_ERROR;
0401     }
0402 
0403     /* Start a new block or continue the current one.
0404      */
0405     if (strm->avail_in != 0 || s->lookahead != 0 ||
0406         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
0407         block_state bstate;
0408 
0409     bstate = DEFLATE_HOOK(strm, flush, &bstate) ? bstate :
0410          (*(configuration_table[s->level].func))(s, flush);
0411 
0412         if (bstate == finish_started || bstate == finish_done) {
0413             s->status = FINISH_STATE;
0414         }
0415         if (bstate == need_more || bstate == finish_started) {
0416         if (strm->avail_out == 0) {
0417             s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
0418         }
0419         return Z_OK;
0420         /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
0421          * of deflate should use the same flush parameter to make sure
0422          * that the flush is complete. So we don't have to output an
0423          * empty block here, this will be done at next call. This also
0424          * ensures that for a very small output buffer, we emit at most
0425          * one empty block.
0426          */
0427     }
0428         if (bstate == block_done) {
0429             if (flush == Z_PARTIAL_FLUSH) {
0430                 zlib_tr_align(s);
0431         } else if (flush == Z_PACKET_FLUSH) {
0432         /* Output just the 3-bit `stored' block type value,
0433            but not a zero length. */
0434         zlib_tr_stored_type_only(s);
0435             } else { /* FULL_FLUSH or SYNC_FLUSH */
0436                 zlib_tr_stored_block(s, (char*)0, 0L, 0);
0437                 /* For a full flush, this empty block will be recognized
0438                  * as a special marker by inflate_sync().
0439                  */
0440                 if (flush == Z_FULL_FLUSH) {
0441                     CLEAR_HASH(s);             /* forget history */
0442                 }
0443             }
0444             flush_pending(strm);
0445         if (strm->avail_out == 0) {
0446           s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
0447           return Z_OK;
0448         }
0449         }
0450     }
0451     Assert(strm->avail_out > 0, "bug2");
0452 
0453     if (flush != Z_FINISH) return Z_OK;
0454     if (s->noheader) return Z_STREAM_END;
0455 
0456     /* Write the zlib trailer (adler32) */
0457     putShortMSB(s, (uInt)(strm->adler >> 16));
0458     putShortMSB(s, (uInt)(strm->adler & 0xffff));
0459     flush_pending(strm);
0460     /* If avail_out is zero, the application will call deflate again
0461      * to flush the rest.
0462      */
0463     s->noheader = -1; /* write the trailer only once! */
0464     return s->pending != 0 ? Z_OK : Z_STREAM_END;
0465 }
0466 
0467 /* ========================================================================= */
0468 int zlib_deflateEnd(
0469     z_streamp strm
0470 )
0471 {
0472     int status;
0473     deflate_state *s;
0474 
0475     if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
0476     s = (deflate_state *) strm->state;
0477 
0478     status = s->status;
0479     if (status != INIT_STATE && status != BUSY_STATE &&
0480     status != FINISH_STATE) {
0481       return Z_STREAM_ERROR;
0482     }
0483 
0484     strm->state = NULL;
0485 
0486     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
0487 }
0488 
0489 /* ===========================================================================
0490  * Read a new buffer from the current input stream, update the adler32
0491  * and total number of bytes read.  All deflate() input goes through
0492  * this function so some applications may wish to modify it to avoid
0493  * allocating a large strm->next_in buffer and copying from it.
0494  * (See also flush_pending()).
0495  */
0496 static int read_buf(
0497     z_streamp strm,
0498     Byte *buf,
0499     unsigned size
0500 )
0501 {
0502     unsigned len = strm->avail_in;
0503 
0504     if (len > size) len = size;
0505     if (len == 0) return 0;
0506 
0507     strm->avail_in  -= len;
0508 
0509     if (!DEFLATE_NEED_CHECKSUM(strm)) {}
0510     else if (!((deflate_state *)(strm->state))->noheader) {
0511         strm->adler = zlib_adler32(strm->adler, strm->next_in, len);
0512     }
0513     memcpy(buf, strm->next_in, len);
0514     strm->next_in  += len;
0515     strm->total_in += len;
0516 
0517     return (int)len;
0518 }
0519 
0520 /* ===========================================================================
0521  * Initialize the "longest match" routines for a new zlib stream
0522  */
0523 static void lm_init(
0524     deflate_state *s
0525 )
0526 {
0527     s->window_size = (ulg)2L*s->w_size;
0528 
0529     CLEAR_HASH(s);
0530 
0531     /* Set the default configuration parameters:
0532      */
0533     s->max_lazy_match   = configuration_table[s->level].max_lazy;
0534     s->good_match       = configuration_table[s->level].good_length;
0535     s->nice_match       = configuration_table[s->level].nice_length;
0536     s->max_chain_length = configuration_table[s->level].max_chain;
0537 
0538     s->strstart = 0;
0539     s->block_start = 0L;
0540     s->lookahead = 0;
0541     s->match_length = s->prev_length = MIN_MATCH-1;
0542     s->match_available = 0;
0543     s->ins_h = 0;
0544 }
0545 
0546 /* ===========================================================================
0547  * Set match_start to the longest match starting at the given string and
0548  * return its length. Matches shorter or equal to prev_length are discarded,
0549  * in which case the result is equal to prev_length and match_start is
0550  * garbage.
0551  * IN assertions: cur_match is the head of the hash chain for the current
0552  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
0553  * OUT assertion: the match length is not greater than s->lookahead.
0554  */
0555 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
0556  * match.S. The code will be functionally equivalent.
0557  */
0558 static uInt longest_match(
0559     deflate_state *s,
0560     IPos cur_match          /* current match */
0561 )
0562 {
0563     unsigned chain_length = s->max_chain_length;/* max hash chain length */
0564     register Byte *scan = s->window + s->strstart; /* current string */
0565     register Byte *match;                       /* matched string */
0566     register int len;                           /* length of current match */
0567     int best_len = s->prev_length;              /* best match length so far */
0568     int nice_match = s->nice_match;             /* stop if match long enough */
0569     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
0570         s->strstart - (IPos)MAX_DIST(s) : NIL;
0571     /* Stop when cur_match becomes <= limit. To simplify the code,
0572      * we prevent matches with the string of window index 0.
0573      */
0574     Pos *prev = s->prev;
0575     uInt wmask = s->w_mask;
0576 
0577 #ifdef UNALIGNED_OK
0578     /* Compare two bytes at a time. Note: this is not always beneficial.
0579      * Try with and without -DUNALIGNED_OK to check.
0580      */
0581     register Byte *strend = s->window + s->strstart + MAX_MATCH - 1;
0582     register ush scan_start = *(ush*)scan;
0583     register ush scan_end   = *(ush*)(scan+best_len-1);
0584 #else
0585     register Byte *strend = s->window + s->strstart + MAX_MATCH;
0586     register Byte scan_end1  = scan[best_len-1];
0587     register Byte scan_end   = scan[best_len];
0588 #endif
0589 
0590     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
0591      * It is easy to get rid of this optimization if necessary.
0592      */
0593     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
0594 
0595     /* Do not waste too much time if we already have a good match: */
0596     if (s->prev_length >= s->good_match) {
0597         chain_length >>= 2;
0598     }
0599     /* Do not look for matches beyond the end of the input. This is necessary
0600      * to make deflate deterministic.
0601      */
0602     if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
0603 
0604     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
0605 
0606     do {
0607         Assert(cur_match < s->strstart, "no future");
0608         match = s->window + cur_match;
0609 
0610         /* Skip to next match if the match length cannot increase
0611          * or if the match length is less than 2:
0612          */
0613 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
0614         /* This code assumes sizeof(unsigned short) == 2. Do not use
0615          * UNALIGNED_OK if your compiler uses a different size.
0616          */
0617         if (*(ush*)(match+best_len-1) != scan_end ||
0618             *(ush*)match != scan_start) continue;
0619 
0620         /* It is not necessary to compare scan[2] and match[2] since they are
0621          * always equal when the other bytes match, given that the hash keys
0622          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
0623          * strstart+3, +5, ... up to strstart+257. We check for insufficient
0624          * lookahead only every 4th comparison; the 128th check will be made
0625          * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
0626          * necessary to put more guard bytes at the end of the window, or
0627          * to check more often for insufficient lookahead.
0628          */
0629         Assert(scan[2] == match[2], "scan[2]?");
0630         scan++, match++;
0631         do {
0632         } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
0633                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
0634                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
0635                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
0636                  scan < strend);
0637         /* The funny "do {}" generates better code on most compilers */
0638 
0639         /* Here, scan <= window+strstart+257 */
0640         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
0641         if (*scan == *match) scan++;
0642 
0643         len = (MAX_MATCH - 1) - (int)(strend-scan);
0644         scan = strend - (MAX_MATCH-1);
0645 
0646 #else /* UNALIGNED_OK */
0647 
0648         if (match[best_len]   != scan_end  ||
0649             match[best_len-1] != scan_end1 ||
0650             *match            != *scan     ||
0651             *++match          != scan[1])      continue;
0652 
0653         /* The check at best_len-1 can be removed because it will be made
0654          * again later. (This heuristic is not always a win.)
0655          * It is not necessary to compare scan[2] and match[2] since they
0656          * are always equal when the other bytes match, given that
0657          * the hash keys are equal and that HASH_BITS >= 8.
0658          */
0659         scan += 2, match++;
0660         Assert(*scan == *match, "match[2]?");
0661 
0662         /* We check for insufficient lookahead only every 8th comparison;
0663          * the 256th check will be made at strstart+258.
0664          */
0665         do {
0666         } while (*++scan == *++match && *++scan == *++match &&
0667                  *++scan == *++match && *++scan == *++match &&
0668                  *++scan == *++match && *++scan == *++match &&
0669                  *++scan == *++match && *++scan == *++match &&
0670                  scan < strend);
0671 
0672         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
0673 
0674         len = MAX_MATCH - (int)(strend - scan);
0675         scan = strend - MAX_MATCH;
0676 
0677 #endif /* UNALIGNED_OK */
0678 
0679         if (len > best_len) {
0680             s->match_start = cur_match;
0681             best_len = len;
0682             if (len >= nice_match) break;
0683 #ifdef UNALIGNED_OK
0684             scan_end = *(ush*)(scan+best_len-1);
0685 #else
0686             scan_end1  = scan[best_len-1];
0687             scan_end   = scan[best_len];
0688 #endif
0689         }
0690     } while ((cur_match = prev[cur_match & wmask]) > limit
0691              && --chain_length != 0);
0692 
0693     if ((uInt)best_len <= s->lookahead) return best_len;
0694     return s->lookahead;
0695 }
0696 
0697 #ifdef DEBUG_ZLIB
0698 /* ===========================================================================
0699  * Check that the match at match_start is indeed a match.
0700  */
0701 static void check_match(
0702     deflate_state *s,
0703     IPos start,
0704     IPos match,
0705     int length
0706 )
0707 {
0708     /* check that the match is indeed a match */
0709     if (memcmp((char *)s->window + match,
0710                 (char *)s->window + start, length) != EQUAL) {
0711         fprintf(stderr, " start %u, match %u, length %d\n",
0712         start, match, length);
0713         do {
0714         fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
0715     } while (--length != 0);
0716         z_error("invalid match");
0717     }
0718     if (z_verbose > 1) {
0719         fprintf(stderr,"\\[%d,%d]", start-match, length);
0720         do { putc(s->window[start++], stderr); } while (--length != 0);
0721     }
0722 }
0723 #else
0724 #  define check_match(s, start, match, length)
0725 #endif
0726 
0727 /* ===========================================================================
0728  * Fill the window when the lookahead becomes insufficient.
0729  * Updates strstart and lookahead.
0730  *
0731  * IN assertion: lookahead < MIN_LOOKAHEAD
0732  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
0733  *    At least one byte has been read, or avail_in == 0; reads are
0734  *    performed for at least two bytes (required for the zip translate_eol
0735  *    option -- not supported here).
0736  */
0737 static void fill_window(
0738     deflate_state *s
0739 )
0740 {
0741     register unsigned n, m;
0742     register Pos *p;
0743     unsigned more;    /* Amount of free space at the end of the window. */
0744     uInt wsize = s->w_size;
0745 
0746     do {
0747         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
0748 
0749         /* Deal with !@#$% 64K limit: */
0750         if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
0751             more = wsize;
0752 
0753         } else if (more == (unsigned)(-1)) {
0754             /* Very unlikely, but possible on 16 bit machine if strstart == 0
0755              * and lookahead == 1 (input done one byte at time)
0756              */
0757             more--;
0758 
0759         /* If the window is almost full and there is insufficient lookahead,
0760          * move the upper half to the lower one to make room in the upper half.
0761          */
0762         } else if (s->strstart >= wsize+MAX_DIST(s)) {
0763 
0764             memcpy((char *)s->window, (char *)s->window+wsize,
0765                    (unsigned)wsize);
0766             s->match_start -= wsize;
0767             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
0768             s->block_start -= (long) wsize;
0769 
0770             /* Slide the hash table (could be avoided with 32 bit values
0771                at the expense of memory usage). We slide even when level == 0
0772                to keep the hash table consistent if we switch back to level > 0
0773                later. (Using level 0 permanently is not an optimal usage of
0774                zlib, so we don't care about this pathological case.)
0775              */
0776             n = s->hash_size;
0777             p = &s->head[n];
0778             do {
0779                 m = *--p;
0780                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
0781             } while (--n);
0782 
0783             n = wsize;
0784             p = &s->prev[n];
0785             do {
0786                 m = *--p;
0787                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
0788                 /* If n is not on any hash chain, prev[n] is garbage but
0789                  * its value will never be used.
0790                  */
0791             } while (--n);
0792             more += wsize;
0793         }
0794         if (s->strm->avail_in == 0) return;
0795 
0796         /* If there was no sliding:
0797          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
0798          *    more == window_size - lookahead - strstart
0799          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
0800          * => more >= window_size - 2*WSIZE + 2
0801          * In the BIG_MEM or MMAP case (not yet supported),
0802          *   window_size == input_size + MIN_LOOKAHEAD  &&
0803          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
0804          * Otherwise, window_size == 2*WSIZE so more >= 2.
0805          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
0806          */
0807         Assert(more >= 2, "more < 2");
0808 
0809         n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
0810         s->lookahead += n;
0811 
0812         /* Initialize the hash value now that we have some input: */
0813         if (s->lookahead >= MIN_MATCH) {
0814             s->ins_h = s->window[s->strstart];
0815             UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
0816 #if MIN_MATCH != 3
0817             Call UPDATE_HASH() MIN_MATCH-3 more times
0818 #endif
0819         }
0820         /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
0821          * but this is not important since only literal bytes will be emitted.
0822          */
0823 
0824     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
0825 }
0826 
0827 /* ===========================================================================
0828  * Flush the current block, with given end-of-file flag.
0829  * IN assertion: strstart is set to the end of the current match.
0830  */
0831 #define FLUSH_BLOCK_ONLY(s, eof) { \
0832    zlib_tr_flush_block(s, (s->block_start >= 0L ? \
0833                    (char *)&s->window[(unsigned)s->block_start] : \
0834                    NULL), \
0835         (ulg)((long)s->strstart - s->block_start), \
0836         (eof)); \
0837    s->block_start = s->strstart; \
0838    flush_pending(s->strm); \
0839    Tracev((stderr,"[FLUSH]")); \
0840 }
0841 
0842 /* Same but force premature exit if necessary. */
0843 #define FLUSH_BLOCK(s, eof) { \
0844    FLUSH_BLOCK_ONLY(s, eof); \
0845    if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
0846 }
0847 
0848 /* ===========================================================================
0849  * Copy without compression as much as possible from the input stream, return
0850  * the current block state.
0851  * This function does not insert new strings in the dictionary since
0852  * uncompressible data is probably not useful. This function is used
0853  * only for the level=0 compression option.
0854  * NOTE: this function should be optimized to avoid extra copying from
0855  * window to pending_buf.
0856  */
0857 static block_state deflate_stored(
0858     deflate_state *s,
0859     int flush
0860 )
0861 {
0862     /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
0863      * to pending_buf_size, and each stored block has a 5 byte header:
0864      */
0865     ulg max_block_size = 0xffff;
0866     ulg max_start;
0867 
0868     if (max_block_size > s->pending_buf_size - 5) {
0869         max_block_size = s->pending_buf_size - 5;
0870     }
0871 
0872     /* Copy as much as possible from input to output: */
0873     for (;;) {
0874         /* Fill the window as much as possible: */
0875         if (s->lookahead <= 1) {
0876 
0877             Assert(s->strstart < s->w_size+MAX_DIST(s) ||
0878            s->block_start >= (long)s->w_size, "slide too late");
0879 
0880             fill_window(s);
0881             if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
0882 
0883             if (s->lookahead == 0) break; /* flush the current block */
0884         }
0885     Assert(s->block_start >= 0L, "block gone");
0886 
0887     s->strstart += s->lookahead;
0888     s->lookahead = 0;
0889 
0890     /* Emit a stored block if pending_buf will be full: */
0891     max_start = s->block_start + max_block_size;
0892         if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
0893         /* strstart == 0 is possible when wraparound on 16-bit machine */
0894         s->lookahead = (uInt)(s->strstart - max_start);
0895         s->strstart = (uInt)max_start;
0896             FLUSH_BLOCK(s, 0);
0897     }
0898     /* Flush if we may have to slide, otherwise block_start may become
0899          * negative and the data will be gone:
0900          */
0901         if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
0902             FLUSH_BLOCK(s, 0);
0903     }
0904     }
0905     FLUSH_BLOCK(s, flush == Z_FINISH);
0906     return flush == Z_FINISH ? finish_done : block_done;
0907 }
0908 
0909 /* ===========================================================================
0910  * Compress as much as possible from the input stream, return the current
0911  * block state.
0912  * This function does not perform lazy evaluation of matches and inserts
0913  * new strings in the dictionary only for unmatched strings or for short
0914  * matches. It is used only for the fast compression options.
0915  */
0916 static block_state deflate_fast(
0917     deflate_state *s,
0918     int flush
0919 )
0920 {
0921     IPos hash_head = NIL; /* head of the hash chain */
0922     int bflush;           /* set if current block must be flushed */
0923 
0924     for (;;) {
0925         /* Make sure that we always have enough lookahead, except
0926          * at the end of the input file. We need MAX_MATCH bytes
0927          * for the next match, plus MIN_MATCH bytes to insert the
0928          * string following the next match.
0929          */
0930         if (s->lookahead < MIN_LOOKAHEAD) {
0931             fill_window(s);
0932             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
0933             return need_more;
0934         }
0935             if (s->lookahead == 0) break; /* flush the current block */
0936         }
0937 
0938         /* Insert the string window[strstart .. strstart+2] in the
0939          * dictionary, and set hash_head to the head of the hash chain:
0940          */
0941         if (s->lookahead >= MIN_MATCH) {
0942             INSERT_STRING(s, s->strstart, hash_head);
0943         }
0944 
0945         /* Find the longest match, discarding those <= prev_length.
0946          * At this point we have always match_length < MIN_MATCH
0947          */
0948         if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
0949             /* To simplify the code, we prevent matches with the string
0950              * of window index 0 (in particular we have to avoid a match
0951              * of the string with itself at the start of the input file).
0952              */
0953             if (s->strategy != Z_HUFFMAN_ONLY) {
0954                 s->match_length = longest_match (s, hash_head);
0955             }
0956             /* longest_match() sets match_start */
0957         }
0958         if (s->match_length >= MIN_MATCH) {
0959             check_match(s, s->strstart, s->match_start, s->match_length);
0960 
0961             bflush = zlib_tr_tally(s, s->strstart - s->match_start,
0962                                s->match_length - MIN_MATCH);
0963 
0964             s->lookahead -= s->match_length;
0965 
0966             /* Insert new strings in the hash table only if the match length
0967              * is not too large. This saves time but degrades compression.
0968              */
0969             if (s->match_length <= s->max_insert_length &&
0970                 s->lookahead >= MIN_MATCH) {
0971                 s->match_length--; /* string at strstart already in hash table */
0972                 do {
0973                     s->strstart++;
0974                     INSERT_STRING(s, s->strstart, hash_head);
0975                     /* strstart never exceeds WSIZE-MAX_MATCH, so there are
0976                      * always MIN_MATCH bytes ahead.
0977                      */
0978                 } while (--s->match_length != 0);
0979                 s->strstart++; 
0980             } else {
0981                 s->strstart += s->match_length;
0982                 s->match_length = 0;
0983                 s->ins_h = s->window[s->strstart];
0984                 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
0985 #if MIN_MATCH != 3
0986                 Call UPDATE_HASH() MIN_MATCH-3 more times
0987 #endif
0988                 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
0989                  * matter since it will be recomputed at next deflate call.
0990                  */
0991             }
0992         } else {
0993             /* No match, output a literal byte */
0994             Tracevv((stderr,"%c", s->window[s->strstart]));
0995             bflush = zlib_tr_tally (s, 0, s->window[s->strstart]);
0996             s->lookahead--;
0997             s->strstart++; 
0998         }
0999         if (bflush) FLUSH_BLOCK(s, 0);
1000     }
1001     FLUSH_BLOCK(s, flush == Z_FINISH);
1002     return flush == Z_FINISH ? finish_done : block_done;
1003 }
1004 
1005 /* ===========================================================================
1006  * Same as above, but achieves better compression. We use a lazy
1007  * evaluation for matches: a match is finally adopted only if there is
1008  * no better match at the next window position.
1009  */
1010 static block_state deflate_slow(
1011     deflate_state *s,
1012     int flush
1013 )
1014 {
1015     IPos hash_head = NIL;    /* head of hash chain */
1016     int bflush;              /* set if current block must be flushed */
1017 
1018     /* Process the input block. */
1019     for (;;) {
1020         /* Make sure that we always have enough lookahead, except
1021          * at the end of the input file. We need MAX_MATCH bytes
1022          * for the next match, plus MIN_MATCH bytes to insert the
1023          * string following the next match.
1024          */
1025         if (s->lookahead < MIN_LOOKAHEAD) {
1026             fill_window(s);
1027             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1028             return need_more;
1029         }
1030             if (s->lookahead == 0) break; /* flush the current block */
1031         }
1032 
1033         /* Insert the string window[strstart .. strstart+2] in the
1034          * dictionary, and set hash_head to the head of the hash chain:
1035          */
1036         if (s->lookahead >= MIN_MATCH) {
1037             INSERT_STRING(s, s->strstart, hash_head);
1038         }
1039 
1040         /* Find the longest match, discarding those <= prev_length.
1041          */
1042         s->prev_length = s->match_length, s->prev_match = s->match_start;
1043         s->match_length = MIN_MATCH-1;
1044 
1045         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1046             s->strstart - hash_head <= MAX_DIST(s)) {
1047             /* To simplify the code, we prevent matches with the string
1048              * of window index 0 (in particular we have to avoid a match
1049              * of the string with itself at the start of the input file).
1050              */
1051             if (s->strategy != Z_HUFFMAN_ONLY) {
1052                 s->match_length = longest_match (s, hash_head);
1053             }
1054             /* longest_match() sets match_start */
1055 
1056             if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1057                  (s->match_length == MIN_MATCH &&
1058                   s->strstart - s->match_start > TOO_FAR))) {
1059 
1060                 /* If prev_match is also MIN_MATCH, match_start is garbage
1061                  * but we will ignore the current match anyway.
1062                  */
1063                 s->match_length = MIN_MATCH-1;
1064             }
1065         }
1066         /* If there was a match at the previous step and the current
1067          * match is not better, output the previous match:
1068          */
1069         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1070             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1071             /* Do not insert strings in hash table beyond this. */
1072 
1073             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1074 
1075             bflush = zlib_tr_tally(s, s->strstart -1 - s->prev_match,
1076                    s->prev_length - MIN_MATCH);
1077 
1078             /* Insert in hash table all strings up to the end of the match.
1079              * strstart-1 and strstart are already inserted. If there is not
1080              * enough lookahead, the last two strings are not inserted in
1081              * the hash table.
1082              */
1083             s->lookahead -= s->prev_length-1;
1084             s->prev_length -= 2;
1085             do {
1086                 if (++s->strstart <= max_insert) {
1087                     INSERT_STRING(s, s->strstart, hash_head);
1088                 }
1089             } while (--s->prev_length != 0);
1090             s->match_available = 0;
1091             s->match_length = MIN_MATCH-1;
1092             s->strstart++;
1093 
1094             if (bflush) FLUSH_BLOCK(s, 0);
1095 
1096         } else if (s->match_available) {
1097             /* If there was no match at the previous position, output a
1098              * single literal. If there was a match but the current match
1099              * is longer, truncate the previous match to a single literal.
1100              */
1101             Tracevv((stderr,"%c", s->window[s->strstart-1]));
1102             if (zlib_tr_tally (s, 0, s->window[s->strstart-1])) {
1103                 FLUSH_BLOCK_ONLY(s, 0);
1104             }
1105             s->strstart++;
1106             s->lookahead--;
1107             if (s->strm->avail_out == 0) return need_more;
1108         } else {
1109             /* There is no previous match to compare with, wait for
1110              * the next step to decide.
1111              */
1112             s->match_available = 1;
1113             s->strstart++;
1114             s->lookahead--;
1115         }
1116     }
1117     Assert (flush != Z_NO_FLUSH, "no flush?");
1118     if (s->match_available) {
1119         Tracevv((stderr,"%c", s->window[s->strstart-1]));
1120         zlib_tr_tally (s, 0, s->window[s->strstart-1]);
1121         s->match_available = 0;
1122     }
1123     FLUSH_BLOCK(s, flush == Z_FINISH);
1124     return flush == Z_FINISH ? finish_done : block_done;
1125 }
1126 
1127 int zlib_deflate_workspacesize(int windowBits, int memLevel)
1128 {
1129     if (windowBits < 0) /* undocumented feature: suppress zlib header */
1130         windowBits = -windowBits;
1131 
1132     /* Since the return value is typically passed to vmalloc() unchecked... */
1133     BUG_ON(memLevel < 1 || memLevel > MAX_MEM_LEVEL || windowBits < 9 ||
1134                             windowBits > 15);
1135 
1136     return sizeof(deflate_workspace)
1137         + zlib_deflate_window_memsize(windowBits)
1138         + zlib_deflate_prev_memsize(windowBits)
1139         + zlib_deflate_head_memsize(memLevel)
1140         + zlib_deflate_overlay_memsize(memLevel);
1141 }
1142 
1143 int zlib_deflate_dfltcc_enabled(void)
1144 {
1145     return DEFLATE_DFLTCC_ENABLED();
1146 }