^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* +++ deflate.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* deflate.c -- compress data using the deflation algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 1995-1996 Jean-loup Gailly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * For conditions of distribution and use, see copyright notice in zlib.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * ALGORITHM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * The "deflation" process depends on being able to identify portions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * of the input text which are identical to earlier input (within a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * sliding window trailing behind the input currently being processed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * The most straightforward technique turns out to be the fastest for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * most input files: try all possible matches and select the longest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * The key feature of this algorithm is that insertions into the string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * dictionary are very simple and thus fast, and deletions are avoided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * completely. Insertions are performed at each input character, whereas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * string matches are performed only when the previous match ends. So it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * is preferable to spend more time in matches to allow very fast string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * insertions and avoid deletions. The matching algorithm for small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * strings is inspired from that of Rabin & Karp. A brute force approach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * is used to find longer strings when a small match has been found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * (by Leonid Broukhis).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * A previous version of this file used a more sophisticated algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * (by Fiala and Greene) which is guaranteed to run in linear amortized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * time, but has a larger average cost, uses more memory and is patented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * However the F&G algorithm may be faster for some highly redundant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * files if the parameter max_chain_length (described below) is too large.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * ACKNOWLEDGEMENTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * I found it in 'freeze' written by Leonid Broukhis.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Thanks to many people for bug reports and testing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * REFERENCES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Available in ftp://ds.internic.net/rfc/rfc1951.txt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * A description of the Rabin and Karp algorithm is given in the book
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Fiala,E.R., and Greene,D.H.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/zutil.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include "defutil.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* architecture-specific bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #ifdef CONFIG_ZLIB_DFLTCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) # include "../zlib_dfltcc/dfltcc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define DEFLATE_RESET_HOOK(strm) do {} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define DEFLATE_HOOK(strm, flush, bstate) 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define DEFLATE_NEED_CHECKSUM(strm) 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define DEFLATE_DFLTCC_ENABLED() 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * Function prototypes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) typedef block_state (*compress_func) (deflate_state *s, int flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Compression function. Returns the block state after the call. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static void fill_window (deflate_state *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static block_state deflate_stored (deflate_state *s, int flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static block_state deflate_fast (deflate_state *s, int flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static block_state deflate_slow (deflate_state *s, int flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static void lm_init (deflate_state *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static void putShortMSB (deflate_state *s, uInt b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static int read_buf (z_streamp strm, Byte *buf, unsigned size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static uInt longest_match (deflate_state *s, IPos cur_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #ifdef DEBUG_ZLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static void check_match (deflate_state *s, IPos start, IPos match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * Local data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define NIL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* Tail of hash chains */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #ifndef TOO_FAR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) # define TOO_FAR 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* Minimum amount of lookahead, except at the end of the input file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * See deflate.c for comments about the MIN_MATCH+1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* Workspace to be allocated for deflate processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) typedef struct deflate_workspace {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /* State memory for the deflator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) deflate_state deflate_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #ifdef CONFIG_ZLIB_DFLTCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* State memory for s390 hardware deflate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct dfltcc_state dfltcc_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) Byte *window_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) Pos *prev_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) Pos *head_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) char *overlay_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) } deflate_workspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #ifdef CONFIG_ZLIB_DFLTCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* dfltcc_state must be doubleword aligned for DFLTCC call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static_assert(offsetof(struct deflate_workspace, dfltcc_memory) % 8 == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Values for max_lazy_match, good_match and max_chain_length, depending on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * the desired pack level (0..9). The values given below have been tuned to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * exclude worst case performance for pathological files. Better values may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * found for specific files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) typedef struct config_s {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ush good_length; /* reduce lazy search above this match length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ush max_lazy; /* do not perform lazy search above this match length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ush nice_length; /* quit search above this match length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ush max_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) compress_func func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) } config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static const config configuration_table[10] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* good lazy nice chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* 2 */ {4, 5, 16, 8, deflate_fast},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* 3 */ {4, 6, 32, 32, deflate_fast},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* 5 */ {8, 16, 32, 32, deflate_slow},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* 6 */ {8, 16, 128, 128, deflate_slow},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* 7 */ {8, 32, 128, 256, deflate_slow},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* 8 */ {32, 128, 258, 1024, deflate_slow},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * meaning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define EQUAL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* result of memcmp for equal strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * Update a hash value with the given input byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * IN assertion: all calls to UPDATE_HASH are made with consecutive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * input characters, so that a running hash key can be computed from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * previous key instead of complete recalculation each time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * Insert string str in the dictionary and set match_head to the previous head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * of the hash chain (the most recent string with same hash key). Return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * the previous length of the hash chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * IN assertion: all calls to INSERT_STRING are made with consecutive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * input characters and the first MIN_MATCH bytes of str are valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * (except for the last MIN_MATCH-1 bytes of the input file).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #define INSERT_STRING(s, str, match_head) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) s->head[s->ins_h] = (Pos)(str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * prev[] will be initialized on the fly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #define CLEAR_HASH(s) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) s->head[s->hash_size-1] = NIL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) memset((char *)s->head, 0, (unsigned)(s->hash_size-1)*sizeof(*s->head));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* ========================================================================= */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int zlib_deflateInit2(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) z_streamp strm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int method,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int windowBits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int memLevel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int strategy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) deflate_state *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int noheader = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) deflate_workspace *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) char *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ush *overlay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* We overlay pending_buf and d_buf+l_buf. This works since the average
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * output size for (length,distance) codes is <= 24 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (strm == NULL) return Z_STREAM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) strm->msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (level == Z_DEFAULT_COMPRESSION) level = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) mem = (deflate_workspace *) strm->workspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (windowBits < 0) { /* undocumented feature: suppress zlib header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) noheader = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) windowBits = -windowBits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return Z_STREAM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * Direct the workspace's pointers to the chunks that were allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * along with the deflate_workspace struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) next = (char *) mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) next += sizeof(*mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) #ifdef CONFIG_ZLIB_DFLTCC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * DFLTCC requires the window to be page aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * Thus, we overallocate and take the aligned portion of the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) mem->window_memory = (Byte *) PTR_ALIGN(next, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) mem->window_memory = (Byte *) next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) next += zlib_deflate_window_memsize(windowBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) mem->prev_memory = (Pos *) next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) next += zlib_deflate_prev_memsize(windowBits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) mem->head_memory = (Pos *) next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) next += zlib_deflate_head_memsize(memLevel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) mem->overlay_memory = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) s = (deflate_state *) &(mem->deflate_memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) strm->state = (struct internal_state *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) s->strm = strm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) s->noheader = noheader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) s->w_bits = windowBits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) s->w_size = 1 << s->w_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) s->w_mask = s->w_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) s->hash_bits = memLevel + 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) s->hash_size = 1 << s->hash_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) s->hash_mask = s->hash_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) s->window = (Byte *) mem->window_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) s->prev = (Pos *) mem->prev_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) s->head = (Pos *) mem->head_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) overlay = (ush *) mem->overlay_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) s->pending_buf = (uch *) overlay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) s->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) s->strategy = strategy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) s->method = (Byte)method;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return zlib_deflateReset(strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* ========================================================================= */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int zlib_deflateReset(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) z_streamp strm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) deflate_state *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (strm == NULL || strm->state == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return Z_STREAM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) strm->total_in = strm->total_out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) strm->msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) strm->data_type = Z_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) s = (deflate_state *)strm->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) s->pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) s->pending_out = s->pending_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (s->noheader < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) s->status = s->noheader ? BUSY_STATE : INIT_STATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) strm->adler = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) s->last_flush = Z_NO_FLUSH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) zlib_tr_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) lm_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) DEFLATE_RESET_HOOK(strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return Z_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* =========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * Put a short in the pending buffer. The 16-bit value is put in MSB order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * IN assertion: the stream state is correct and there is enough room in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * pending_buf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static void putShortMSB(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) deflate_state *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) uInt b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) put_byte(s, (Byte)(b >> 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) put_byte(s, (Byte)(b & 0xff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /* ========================================================================= */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) int zlib_deflate(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) z_streamp strm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int old_flush; /* value of flush param for previous deflate call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) deflate_state *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (strm == NULL || strm->state == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) flush > Z_FINISH || flush < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return Z_STREAM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) s = (deflate_state *) strm->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if ((strm->next_in == NULL && strm->avail_in != 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) (s->status == FINISH_STATE && flush != Z_FINISH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return Z_STREAM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (strm->avail_out == 0) return Z_BUF_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) s->strm = strm; /* just in case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) old_flush = s->last_flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) s->last_flush = flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* Write the zlib header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (s->status == INIT_STATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) uInt level_flags = (s->level-1) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (level_flags > 3) level_flags = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) header |= (level_flags << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (s->strstart != 0) header |= PRESET_DICT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) header += 31 - (header % 31);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) s->status = BUSY_STATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) putShortMSB(s, header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* Save the adler32 of the preset dictionary: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (s->strstart != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) putShortMSB(s, (uInt)(strm->adler >> 16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) putShortMSB(s, (uInt)(strm->adler & 0xffff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) strm->adler = 1L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* Flush as much pending output as possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (s->pending != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) flush_pending(strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (strm->avail_out == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* Since avail_out is 0, deflate will be called again with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * more output space, but possibly with both pending and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * avail_in equal to zero. There won't be anything to do,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * but this is not an error situation so make sure we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * return OK instead of BUF_ERROR at next call of deflate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) s->last_flush = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return Z_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* Make sure there is something to do and avoid duplicate consecutive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * flushes. For repeated and useless calls with Z_FINISH, we keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * returning Z_STREAM_END instead of Z_BUFF_ERROR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) } else if (strm->avail_in == 0 && flush <= old_flush &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) flush != Z_FINISH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return Z_BUF_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /* User must not provide more input after the first FINISH: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (s->status == FINISH_STATE && strm->avail_in != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return Z_BUF_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* Start a new block or continue the current one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (strm->avail_in != 0 || s->lookahead != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) block_state bstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) bstate = DEFLATE_HOOK(strm, flush, &bstate) ? bstate :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) (*(configuration_table[s->level].func))(s, flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (bstate == finish_started || bstate == finish_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) s->status = FINISH_STATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (bstate == need_more || bstate == finish_started) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (strm->avail_out == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return Z_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * of deflate should use the same flush parameter to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * that the flush is complete. So we don't have to output an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * empty block here, this will be done at next call. This also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * ensures that for a very small output buffer, we emit at most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * one empty block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (bstate == block_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (flush == Z_PARTIAL_FLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) zlib_tr_align(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) } else if (flush == Z_PACKET_FLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /* Output just the 3-bit `stored' block type value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) but not a zero length. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) zlib_tr_stored_type_only(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) } else { /* FULL_FLUSH or SYNC_FLUSH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) zlib_tr_stored_block(s, (char*)0, 0L, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /* For a full flush, this empty block will be recognized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * as a special marker by inflate_sync().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (flush == Z_FULL_FLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) CLEAR_HASH(s); /* forget history */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) flush_pending(strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (strm->avail_out == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return Z_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) Assert(strm->avail_out > 0, "bug2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (flush != Z_FINISH) return Z_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (s->noheader) return Z_STREAM_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /* Write the zlib trailer (adler32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) putShortMSB(s, (uInt)(strm->adler >> 16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) putShortMSB(s, (uInt)(strm->adler & 0xffff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) flush_pending(strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* If avail_out is zero, the application will call deflate again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * to flush the rest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) s->noheader = -1; /* write the trailer only once! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return s->pending != 0 ? Z_OK : Z_STREAM_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /* ========================================================================= */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int zlib_deflateEnd(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) z_streamp strm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) deflate_state *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) s = (deflate_state *) strm->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) status = s->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (status != INIT_STATE && status != BUSY_STATE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) status != FINISH_STATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return Z_STREAM_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) strm->state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * Read a new buffer from the current input stream, update the adler32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * and total number of bytes read. All deflate() input goes through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * this function so some applications may wish to modify it to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * allocating a large strm->next_in buffer and copying from it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * (See also flush_pending()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static int read_buf(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) z_streamp strm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) Byte *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) unsigned size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) unsigned len = strm->avail_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (len > size) len = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (len == 0) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) strm->avail_in -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (!DEFLATE_NEED_CHECKSUM(strm)) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) else if (!((deflate_state *)(strm->state))->noheader) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) strm->adler = zlib_adler32(strm->adler, strm->next_in, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) memcpy(buf, strm->next_in, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) strm->next_in += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) strm->total_in += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return (int)len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * Initialize the "longest match" routines for a new zlib stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static void lm_init(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) deflate_state *s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) s->window_size = (ulg)2L*s->w_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) CLEAR_HASH(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) /* Set the default configuration parameters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) s->max_lazy_match = configuration_table[s->level].max_lazy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) s->good_match = configuration_table[s->level].good_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) s->nice_match = configuration_table[s->level].nice_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) s->max_chain_length = configuration_table[s->level].max_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) s->strstart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) s->block_start = 0L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) s->lookahead = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) s->match_length = s->prev_length = MIN_MATCH-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) s->match_available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) s->ins_h = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * Set match_start to the longest match starting at the given string and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * return its length. Matches shorter or equal to prev_length are discarded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * in which case the result is equal to prev_length and match_start is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * garbage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * IN assertions: cur_match is the head of the hash chain for the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * OUT assertion: the match length is not greater than s->lookahead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * match.S. The code will be functionally equivalent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static uInt longest_match(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) deflate_state *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) IPos cur_match /* current match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) unsigned chain_length = s->max_chain_length;/* max hash chain length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) register Byte *scan = s->window + s->strstart; /* current string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) register Byte *match; /* matched string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) register int len; /* length of current match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) int best_len = s->prev_length; /* best match length so far */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) int nice_match = s->nice_match; /* stop if match long enough */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) s->strstart - (IPos)MAX_DIST(s) : NIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /* Stop when cur_match becomes <= limit. To simplify the code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * we prevent matches with the string of window index 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) Pos *prev = s->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) uInt wmask = s->w_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) #ifdef UNALIGNED_OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* Compare two bytes at a time. Note: this is not always beneficial.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * Try with and without -DUNALIGNED_OK to check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) register Byte *strend = s->window + s->strstart + MAX_MATCH - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) register ush scan_start = *(ush*)scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) register ush scan_end = *(ush*)(scan+best_len-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) register Byte *strend = s->window + s->strstart + MAX_MATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) register Byte scan_end1 = scan[best_len-1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) register Byte scan_end = scan[best_len];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * It is easy to get rid of this optimization if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) /* Do not waste too much time if we already have a good match: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (s->prev_length >= s->good_match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) chain_length >>= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) /* Do not look for matches beyond the end of the input. This is necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * to make deflate deterministic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) Assert(cur_match < s->strstart, "no future");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) match = s->window + cur_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /* Skip to next match if the match length cannot increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * or if the match length is less than 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) /* This code assumes sizeof(unsigned short) == 2. Do not use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * UNALIGNED_OK if your compiler uses a different size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (*(ush*)(match+best_len-1) != scan_end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) *(ush*)match != scan_start) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) /* It is not necessary to compare scan[2] and match[2] since they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * always equal when the other bytes match, given that the hash keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * strstart+3, +5, ... up to strstart+257. We check for insufficient
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * lookahead only every 4th comparison; the 128th check will be made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * necessary to put more guard bytes at the end of the window, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * to check more often for insufficient lookahead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) Assert(scan[2] == match[2], "scan[2]?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) scan++, match++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) *(ush*)(scan+=2) == *(ush*)(match+=2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) *(ush*)(scan+=2) == *(ush*)(match+=2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) *(ush*)(scan+=2) == *(ush*)(match+=2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) scan < strend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) /* The funny "do {}" generates better code on most compilers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /* Here, scan <= window+strstart+257 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (*scan == *match) scan++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) len = (MAX_MATCH - 1) - (int)(strend-scan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) scan = strend - (MAX_MATCH-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) #else /* UNALIGNED_OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (match[best_len] != scan_end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) match[best_len-1] != scan_end1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) *match != *scan ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) *++match != scan[1]) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) /* The check at best_len-1 can be removed because it will be made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * again later. (This heuristic is not always a win.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * It is not necessary to compare scan[2] and match[2] since they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * are always equal when the other bytes match, given that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * the hash keys are equal and that HASH_BITS >= 8.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) scan += 2, match++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) Assert(*scan == *match, "match[2]?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) /* We check for insufficient lookahead only every 8th comparison;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * the 256th check will be made at strstart+258.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) } while (*++scan == *++match && *++scan == *++match &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) *++scan == *++match && *++scan == *++match &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) *++scan == *++match && *++scan == *++match &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) *++scan == *++match && *++scan == *++match &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) scan < strend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) len = MAX_MATCH - (int)(strend - scan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) scan = strend - MAX_MATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) #endif /* UNALIGNED_OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (len > best_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) s->match_start = cur_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) best_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (len >= nice_match) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) #ifdef UNALIGNED_OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) scan_end = *(ush*)(scan+best_len-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) scan_end1 = scan[best_len-1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) scan_end = scan[best_len];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) } while ((cur_match = prev[cur_match & wmask]) > limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) && --chain_length != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if ((uInt)best_len <= s->lookahead) return best_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) return s->lookahead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) #ifdef DEBUG_ZLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * Check that the match at match_start is indeed a match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) static void check_match(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) deflate_state *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) IPos start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) IPos match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) int length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /* check that the match is indeed a match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (memcmp((char *)s->window + match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) (char *)s->window + start, length) != EQUAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) fprintf(stderr, " start %u, match %u, length %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) start, match, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) } while (--length != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) z_error("invalid match");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (z_verbose > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) fprintf(stderr,"\\[%d,%d]", start-match, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) do { putc(s->window[start++], stderr); } while (--length != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) # define check_match(s, start, match, length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * Fill the window when the lookahead becomes insufficient.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * Updates strstart and lookahead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * IN assertion: lookahead < MIN_LOOKAHEAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * At least one byte has been read, or avail_in == 0; reads are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * performed for at least two bytes (required for the zip translate_eol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) * option -- not supported here).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) static void fill_window(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) deflate_state *s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) register unsigned n, m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) register Pos *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) unsigned more; /* Amount of free space at the end of the window. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) uInt wsize = s->w_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) /* Deal with !@#$% 64K limit: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) more = wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) } else if (more == (unsigned)(-1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) /* Very unlikely, but possible on 16 bit machine if strstart == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * and lookahead == 1 (input done one byte at time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) more--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) /* If the window is almost full and there is insufficient lookahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * move the upper half to the lower one to make room in the upper half.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) } else if (s->strstart >= wsize+MAX_DIST(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) memcpy((char *)s->window, (char *)s->window+wsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) (unsigned)wsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) s->match_start -= wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) s->block_start -= (long) wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) /* Slide the hash table (could be avoided with 32 bit values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) at the expense of memory usage). We slide even when level == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) to keep the hash table consistent if we switch back to level > 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) later. (Using level 0 permanently is not an optimal usage of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) zlib, so we don't care about this pathological case.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) n = s->hash_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) p = &s->head[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) m = *--p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) *p = (Pos)(m >= wsize ? m-wsize : NIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) } while (--n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) n = wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) p = &s->prev[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) m = *--p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) *p = (Pos)(m >= wsize ? m-wsize : NIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) /* If n is not on any hash chain, prev[n] is garbage but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * its value will never be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) } while (--n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) more += wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (s->strm->avail_in == 0) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) /* If there was no sliding:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * more == window_size - lookahead - strstart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * => more >= window_size - 2*WSIZE + 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * In the BIG_MEM or MMAP case (not yet supported),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * window_size == input_size + MIN_LOOKAHEAD &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) * Otherwise, window_size == 2*WSIZE so more >= 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) Assert(more >= 2, "more < 2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) s->lookahead += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) /* Initialize the hash value now that we have some input: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (s->lookahead >= MIN_MATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) s->ins_h = s->window[s->strstart];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) #if MIN_MATCH != 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) Call UPDATE_HASH() MIN_MATCH-3 more times
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) * but this is not important since only literal bytes will be emitted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * Flush the current block, with given end-of-file flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * IN assertion: strstart is set to the end of the current match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) #define FLUSH_BLOCK_ONLY(s, eof) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) zlib_tr_flush_block(s, (s->block_start >= 0L ? \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) (char *)&s->window[(unsigned)s->block_start] : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) NULL), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) (ulg)((long)s->strstart - s->block_start), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) (eof)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) s->block_start = s->strstart; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) flush_pending(s->strm); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) Tracev((stderr,"[FLUSH]")); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) /* Same but force premature exit if necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) #define FLUSH_BLOCK(s, eof) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) FLUSH_BLOCK_ONLY(s, eof); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) * Copy without compression as much as possible from the input stream, return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) * the current block state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * This function does not insert new strings in the dictionary since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) * uncompressible data is probably not useful. This function is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) * only for the level=0 compression option.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * NOTE: this function should be optimized to avoid extra copying from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * window to pending_buf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) static block_state deflate_stored(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) deflate_state *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) int flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * to pending_buf_size, and each stored block has a 5 byte header:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) ulg max_block_size = 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) ulg max_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (max_block_size > s->pending_buf_size - 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) max_block_size = s->pending_buf_size - 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) /* Copy as much as possible from input to output: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) /* Fill the window as much as possible: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (s->lookahead <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) Assert(s->strstart < s->w_size+MAX_DIST(s) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) s->block_start >= (long)s->w_size, "slide too late");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) fill_window(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (s->lookahead == 0) break; /* flush the current block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) Assert(s->block_start >= 0L, "block gone");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) s->strstart += s->lookahead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) s->lookahead = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) /* Emit a stored block if pending_buf will be full: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) max_start = s->block_start + max_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) /* strstart == 0 is possible when wraparound on 16-bit machine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) s->lookahead = (uInt)(s->strstart - max_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) s->strstart = (uInt)max_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) FLUSH_BLOCK(s, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) /* Flush if we may have to slide, otherwise block_start may become
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * negative and the data will be gone:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) FLUSH_BLOCK(s, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) FLUSH_BLOCK(s, flush == Z_FINISH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return flush == Z_FINISH ? finish_done : block_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) * Compress as much as possible from the input stream, return the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) * block state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) * This function does not perform lazy evaluation of matches and inserts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) * new strings in the dictionary only for unmatched strings or for short
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) * matches. It is used only for the fast compression options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) static block_state deflate_fast(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) deflate_state *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) int flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) IPos hash_head = NIL; /* head of the hash chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) int bflush; /* set if current block must be flushed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) /* Make sure that we always have enough lookahead, except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * at the end of the input file. We need MAX_MATCH bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * for the next match, plus MIN_MATCH bytes to insert the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * string following the next match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (s->lookahead < MIN_LOOKAHEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) fill_window(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) return need_more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (s->lookahead == 0) break; /* flush the current block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) /* Insert the string window[strstart .. strstart+2] in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * dictionary, and set hash_head to the head of the hash chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (s->lookahead >= MIN_MATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) INSERT_STRING(s, s->strstart, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) /* Find the longest match, discarding those <= prev_length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) * At this point we have always match_length < MIN_MATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) /* To simplify the code, we prevent matches with the string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * of window index 0 (in particular we have to avoid a match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * of the string with itself at the start of the input file).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (s->strategy != Z_HUFFMAN_ONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) s->match_length = longest_match (s, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) /* longest_match() sets match_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) if (s->match_length >= MIN_MATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) check_match(s, s->strstart, s->match_start, s->match_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) bflush = zlib_tr_tally(s, s->strstart - s->match_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) s->match_length - MIN_MATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) s->lookahead -= s->match_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) /* Insert new strings in the hash table only if the match length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * is not too large. This saves time but degrades compression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (s->match_length <= s->max_insert_length &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) s->lookahead >= MIN_MATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) s->match_length--; /* string at strstart already in hash table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) s->strstart++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) INSERT_STRING(s, s->strstart, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) /* strstart never exceeds WSIZE-MAX_MATCH, so there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) * always MIN_MATCH bytes ahead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) } while (--s->match_length != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) s->strstart++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) s->strstart += s->match_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) s->match_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) s->ins_h = s->window[s->strstart];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) #if MIN_MATCH != 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) Call UPDATE_HASH() MIN_MATCH-3 more times
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * matter since it will be recomputed at next deflate call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) /* No match, output a literal byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) Tracevv((stderr,"%c", s->window[s->strstart]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) bflush = zlib_tr_tally (s, 0, s->window[s->strstart]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) s->lookahead--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) s->strstart++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (bflush) FLUSH_BLOCK(s, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) FLUSH_BLOCK(s, flush == Z_FINISH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return flush == Z_FINISH ? finish_done : block_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) /* ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * Same as above, but achieves better compression. We use a lazy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * evaluation for matches: a match is finally adopted only if there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * no better match at the next window position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) static block_state deflate_slow(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) deflate_state *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) int flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) IPos hash_head = NIL; /* head of hash chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) int bflush; /* set if current block must be flushed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) /* Process the input block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) /* Make sure that we always have enough lookahead, except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * at the end of the input file. We need MAX_MATCH bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * for the next match, plus MIN_MATCH bytes to insert the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * string following the next match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) if (s->lookahead < MIN_LOOKAHEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) fill_window(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) return need_more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (s->lookahead == 0) break; /* flush the current block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) /* Insert the string window[strstart .. strstart+2] in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) * dictionary, and set hash_head to the head of the hash chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (s->lookahead >= MIN_MATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) INSERT_STRING(s, s->strstart, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) /* Find the longest match, discarding those <= prev_length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) s->prev_length = s->match_length, s->prev_match = s->match_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) s->match_length = MIN_MATCH-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) s->strstart - hash_head <= MAX_DIST(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) /* To simplify the code, we prevent matches with the string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * of window index 0 (in particular we have to avoid a match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * of the string with itself at the start of the input file).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) if (s->strategy != Z_HUFFMAN_ONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) s->match_length = longest_match (s, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) /* longest_match() sets match_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) (s->match_length == MIN_MATCH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) s->strstart - s->match_start > TOO_FAR))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) /* If prev_match is also MIN_MATCH, match_start is garbage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * but we will ignore the current match anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) s->match_length = MIN_MATCH-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) /* If there was a match at the previous step and the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) * match is not better, output the previous match:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) /* Do not insert strings in hash table beyond this. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) check_match(s, s->strstart-1, s->prev_match, s->prev_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) bflush = zlib_tr_tally(s, s->strstart -1 - s->prev_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) s->prev_length - MIN_MATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) /* Insert in hash table all strings up to the end of the match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) * strstart-1 and strstart are already inserted. If there is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * enough lookahead, the last two strings are not inserted in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) * the hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) s->lookahead -= s->prev_length-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) s->prev_length -= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (++s->strstart <= max_insert) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) INSERT_STRING(s, s->strstart, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) } while (--s->prev_length != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) s->match_available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) s->match_length = MIN_MATCH-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) s->strstart++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (bflush) FLUSH_BLOCK(s, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) } else if (s->match_available) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) /* If there was no match at the previous position, output a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * single literal. If there was a match but the current match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * is longer, truncate the previous match to a single literal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) Tracevv((stderr,"%c", s->window[s->strstart-1]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) if (zlib_tr_tally (s, 0, s->window[s->strstart-1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) FLUSH_BLOCK_ONLY(s, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) s->strstart++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) s->lookahead--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) if (s->strm->avail_out == 0) return need_more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) /* There is no previous match to compare with, wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) * the next step to decide.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) s->match_available = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) s->strstart++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) s->lookahead--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) Assert (flush != Z_NO_FLUSH, "no flush?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) if (s->match_available) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) Tracevv((stderr,"%c", s->window[s->strstart-1]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) zlib_tr_tally (s, 0, s->window[s->strstart-1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) s->match_available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) FLUSH_BLOCK(s, flush == Z_FINISH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) return flush == Z_FINISH ? finish_done : block_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) int zlib_deflate_workspacesize(int windowBits, int memLevel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (windowBits < 0) /* undocumented feature: suppress zlib header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) windowBits = -windowBits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) /* Since the return value is typically passed to vmalloc() unchecked... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) BUG_ON(memLevel < 1 || memLevel > MAX_MEM_LEVEL || windowBits < 9 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) windowBits > 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) return sizeof(deflate_workspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) + zlib_deflate_window_memsize(windowBits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) + zlib_deflate_prev_memsize(windowBits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) + zlib_deflate_head_memsize(memLevel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) + zlib_deflate_overlay_memsize(memLevel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) int zlib_deflate_dfltcc_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) return DEFLATE_DFLTCC_ENABLED();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) }