1275793eaSopenharmony_ci/* deflate.c -- compress data using the deflation algorithm
2275793eaSopenharmony_ci * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
3275793eaSopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h
4275793eaSopenharmony_ci */
5275793eaSopenharmony_ci
6275793eaSopenharmony_ci/*
7275793eaSopenharmony_ci *  ALGORITHM
8275793eaSopenharmony_ci *
9275793eaSopenharmony_ci *      The "deflation" process depends on being able to identify portions
10275793eaSopenharmony_ci *      of the input text which are identical to earlier input (within a
11275793eaSopenharmony_ci *      sliding window trailing behind the input currently being processed).
12275793eaSopenharmony_ci *
13275793eaSopenharmony_ci *      The most straightforward technique turns out to be the fastest for
14275793eaSopenharmony_ci *      most input files: try all possible matches and select the longest.
15275793eaSopenharmony_ci *      The key feature of this algorithm is that insertions into the string
16275793eaSopenharmony_ci *      dictionary are very simple and thus fast, and deletions are avoided
17275793eaSopenharmony_ci *      completely. Insertions are performed at each input character, whereas
18275793eaSopenharmony_ci *      string matches are performed only when the previous match ends. So it
19275793eaSopenharmony_ci *      is preferable to spend more time in matches to allow very fast string
20275793eaSopenharmony_ci *      insertions and avoid deletions. The matching algorithm for small
21275793eaSopenharmony_ci *      strings is inspired from that of Rabin & Karp. A brute force approach
22275793eaSopenharmony_ci *      is used to find longer strings when a small match has been found.
23275793eaSopenharmony_ci *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24275793eaSopenharmony_ci *      (by Leonid Broukhis).
25275793eaSopenharmony_ci *         A previous version of this file used a more sophisticated algorithm
26275793eaSopenharmony_ci *      (by Fiala and Greene) which is guaranteed to run in linear amortized
27275793eaSopenharmony_ci *      time, but has a larger average cost, uses more memory and is patented.
28275793eaSopenharmony_ci *      However the F&G algorithm may be faster for some highly redundant
29275793eaSopenharmony_ci *      files if the parameter max_chain_length (described below) is too large.
30275793eaSopenharmony_ci *
31275793eaSopenharmony_ci *  ACKNOWLEDGEMENTS
32275793eaSopenharmony_ci *
33275793eaSopenharmony_ci *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34275793eaSopenharmony_ci *      I found it in 'freeze' written by Leonid Broukhis.
35275793eaSopenharmony_ci *      Thanks to many people for bug reports and testing.
36275793eaSopenharmony_ci *
37275793eaSopenharmony_ci *  REFERENCES
38275793eaSopenharmony_ci *
39275793eaSopenharmony_ci *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40275793eaSopenharmony_ci *      Available in http://tools.ietf.org/html/rfc1951
41275793eaSopenharmony_ci *
42275793eaSopenharmony_ci *      A description of the Rabin and Karp algorithm is given in the book
43275793eaSopenharmony_ci *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44275793eaSopenharmony_ci *
45275793eaSopenharmony_ci *      Fiala,E.R., and Greene,D.H.
46275793eaSopenharmony_ci *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47275793eaSopenharmony_ci *
48275793eaSopenharmony_ci */
49275793eaSopenharmony_ci
50275793eaSopenharmony_ci/* @(#) $Id$ */
51275793eaSopenharmony_ci
52275793eaSopenharmony_ci#include "deflate.h"
53275793eaSopenharmony_ci
54275793eaSopenharmony_ciconst char deflate_copyright[] =
55275793eaSopenharmony_ci   " deflate 1.3.1 Copyright 1995-2024 Jean-loup Gailly and Mark Adler ";
56275793eaSopenharmony_ci/*
57275793eaSopenharmony_ci  If you use the zlib library in a product, an acknowledgment is welcome
58275793eaSopenharmony_ci  in the documentation of your product. If for some reason you cannot
59275793eaSopenharmony_ci  include such an acknowledgment, I would appreciate that you keep this
60275793eaSopenharmony_ci  copyright string in the executable of your product.
61275793eaSopenharmony_ci */
62275793eaSopenharmony_ci
63275793eaSopenharmony_citypedef enum {
64275793eaSopenharmony_ci    need_more,      /* block not completed, need more input or more output */
65275793eaSopenharmony_ci    block_done,     /* block flush performed */
66275793eaSopenharmony_ci    finish_started, /* finish started, need only more output at next deflate */
67275793eaSopenharmony_ci    finish_done     /* finish done, accept no more input or output */
68275793eaSopenharmony_ci} block_state;
69275793eaSopenharmony_ci
70275793eaSopenharmony_citypedef block_state (*compress_func)(deflate_state *s, int flush);
71275793eaSopenharmony_ci/* Compression function. Returns the block state after the call. */
72275793eaSopenharmony_ci
73275793eaSopenharmony_cilocal block_state deflate_stored(deflate_state *s, int flush);
74275793eaSopenharmony_cilocal block_state deflate_fast(deflate_state *s, int flush);
75275793eaSopenharmony_ci#ifndef FASTEST
76275793eaSopenharmony_cilocal block_state deflate_slow(deflate_state *s, int flush);
77275793eaSopenharmony_ci#endif
78275793eaSopenharmony_cilocal block_state deflate_rle(deflate_state *s, int flush);
79275793eaSopenharmony_cilocal block_state deflate_huff(deflate_state *s, int flush);
80275793eaSopenharmony_ci
81275793eaSopenharmony_ci/* ===========================================================================
82275793eaSopenharmony_ci * Local data
83275793eaSopenharmony_ci */
84275793eaSopenharmony_ci
85275793eaSopenharmony_ci#define NIL 0
86275793eaSopenharmony_ci/* Tail of hash chains */
87275793eaSopenharmony_ci
88275793eaSopenharmony_ci#ifndef TOO_FAR
89275793eaSopenharmony_ci#  define TOO_FAR 4096
90275793eaSopenharmony_ci#endif
91275793eaSopenharmony_ci/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
92275793eaSopenharmony_ci
93275793eaSopenharmony_ci/* Values for max_lazy_match, good_match and max_chain_length, depending on
94275793eaSopenharmony_ci * the desired pack level (0..9). The values given below have been tuned to
95275793eaSopenharmony_ci * exclude worst case performance for pathological files. Better values may be
96275793eaSopenharmony_ci * found for specific files.
97275793eaSopenharmony_ci */
98275793eaSopenharmony_citypedef struct config_s {
99275793eaSopenharmony_ci   ush good_length; /* reduce lazy search above this match length */
100275793eaSopenharmony_ci   ush max_lazy;    /* do not perform lazy search above this match length */
101275793eaSopenharmony_ci   ush nice_length; /* quit search above this match length */
102275793eaSopenharmony_ci   ush max_chain;
103275793eaSopenharmony_ci   compress_func func;
104275793eaSopenharmony_ci} config;
105275793eaSopenharmony_ci
106275793eaSopenharmony_ci#ifdef FASTEST
107275793eaSopenharmony_cilocal const config configuration_table[2] = {
108275793eaSopenharmony_ci/*      good lazy nice chain */
109275793eaSopenharmony_ci/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
110275793eaSopenharmony_ci/* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
111275793eaSopenharmony_ci#else
112275793eaSopenharmony_cilocal const config configuration_table[10] = {
113275793eaSopenharmony_ci/*      good lazy nice chain */
114275793eaSopenharmony_ci/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
115275793eaSopenharmony_ci/* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
116275793eaSopenharmony_ci/* 2 */ {4,    5, 16,    8, deflate_fast},
117275793eaSopenharmony_ci/* 3 */ {4,    6, 32,   32, deflate_fast},
118275793eaSopenharmony_ci
119275793eaSopenharmony_ci/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
120275793eaSopenharmony_ci/* 5 */ {8,   16, 32,   32, deflate_slow},
121275793eaSopenharmony_ci/* 6 */ {8,   16, 128, 128, deflate_slow},
122275793eaSopenharmony_ci/* 7 */ {8,   32, 128, 256, deflate_slow},
123275793eaSopenharmony_ci/* 8 */ {32, 128, 258, 1024, deflate_slow},
124275793eaSopenharmony_ci/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
125275793eaSopenharmony_ci#endif
126275793eaSopenharmony_ci
127275793eaSopenharmony_ci/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
128275793eaSopenharmony_ci * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
129275793eaSopenharmony_ci * meaning.
130275793eaSopenharmony_ci */
131275793eaSopenharmony_ci
132275793eaSopenharmony_ci/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
133275793eaSopenharmony_ci#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
134275793eaSopenharmony_ci
135275793eaSopenharmony_ci/* ===========================================================================
136275793eaSopenharmony_ci * Update a hash value with the given input byte
137275793eaSopenharmony_ci * IN  assertion: all calls to UPDATE_HASH are made with consecutive input
138275793eaSopenharmony_ci *    characters, so that a running hash key can be computed from the previous
139275793eaSopenharmony_ci *    key instead of complete recalculation each time.
140275793eaSopenharmony_ci */
141275793eaSopenharmony_ci#define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask)
142275793eaSopenharmony_ci
143275793eaSopenharmony_ci
144275793eaSopenharmony_ci/* ===========================================================================
145275793eaSopenharmony_ci * Insert string str in the dictionary and set match_head to the previous head
146275793eaSopenharmony_ci * of the hash chain (the most recent string with same hash key). Return
147275793eaSopenharmony_ci * the previous length of the hash chain.
148275793eaSopenharmony_ci * If this file is compiled with -DFASTEST, the compression level is forced
149275793eaSopenharmony_ci * to 1, and no hash chains are maintained.
150275793eaSopenharmony_ci * IN  assertion: all calls to INSERT_STRING are made with consecutive input
151275793eaSopenharmony_ci *    characters and the first MIN_MATCH bytes of str are valid (except for
152275793eaSopenharmony_ci *    the last MIN_MATCH-1 bytes of the input file).
153275793eaSopenharmony_ci */
154275793eaSopenharmony_ci#ifdef FASTEST
155275793eaSopenharmony_ci#define INSERT_STRING(s, str, match_head) \
156275793eaSopenharmony_ci   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
157275793eaSopenharmony_ci    match_head = s->head[s->ins_h], \
158275793eaSopenharmony_ci    s->head[s->ins_h] = (Pos)(str))
159275793eaSopenharmony_ci#else
160275793eaSopenharmony_ci#define INSERT_STRING(s, str, match_head) \
161275793eaSopenharmony_ci   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
162275793eaSopenharmony_ci    match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
163275793eaSopenharmony_ci    s->head[s->ins_h] = (Pos)(str))
164275793eaSopenharmony_ci#endif
165275793eaSopenharmony_ci
166275793eaSopenharmony_ci/* ===========================================================================
167275793eaSopenharmony_ci * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
168275793eaSopenharmony_ci * prev[] will be initialized on the fly.
169275793eaSopenharmony_ci */
170275793eaSopenharmony_ci#define CLEAR_HASH(s) \
171275793eaSopenharmony_ci    do { \
172275793eaSopenharmony_ci        s->head[s->hash_size - 1] = NIL; \
173275793eaSopenharmony_ci        zmemzero((Bytef *)s->head, \
174275793eaSopenharmony_ci                 (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
175275793eaSopenharmony_ci    } while (0)
176275793eaSopenharmony_ci
177275793eaSopenharmony_ci/* ===========================================================================
178275793eaSopenharmony_ci * Slide the hash table when sliding the window down (could be avoided with 32
179275793eaSopenharmony_ci * bit values at the expense of memory usage). We slide even when level == 0 to
180275793eaSopenharmony_ci * keep the hash table consistent if we switch back to level > 0 later.
181275793eaSopenharmony_ci */
182275793eaSopenharmony_ci#if defined(__has_feature)
183275793eaSopenharmony_ci#  if __has_feature(memory_sanitizer)
184275793eaSopenharmony_ci     __attribute__((no_sanitize("memory")))
185275793eaSopenharmony_ci#  endif
186275793eaSopenharmony_ci#endif
187275793eaSopenharmony_cilocal void slide_hash(deflate_state *s)
188275793eaSopenharmony_ci{
189275793eaSopenharmony_ci    unsigned n, m;
190275793eaSopenharmony_ci    Posf *p;
191275793eaSopenharmony_ci    uInt wsize = s->w_size;
192275793eaSopenharmony_ci
193275793eaSopenharmony_ci    n = s->hash_size;
194275793eaSopenharmony_ci    p = &s->head[n];
195275793eaSopenharmony_ci    do {
196275793eaSopenharmony_ci        m = *--p;
197275793eaSopenharmony_ci        *p = (Pos)(m >= wsize ? m - wsize : NIL);
198275793eaSopenharmony_ci    } while (--n);
199275793eaSopenharmony_ci    n = wsize;
200275793eaSopenharmony_ci#ifndef FASTEST
201275793eaSopenharmony_ci    p = &s->prev[n];
202275793eaSopenharmony_ci    do {
203275793eaSopenharmony_ci        m = *--p;
204275793eaSopenharmony_ci        *p = (Pos)(m >= wsize ? m - wsize : NIL);
205275793eaSopenharmony_ci        /* If n is not on any hash chain, prev[n] is garbage but
206275793eaSopenharmony_ci         * its value will never be used.
207275793eaSopenharmony_ci         */
208275793eaSopenharmony_ci    } while (--n);
209275793eaSopenharmony_ci#endif
210275793eaSopenharmony_ci}
211275793eaSopenharmony_ci
212275793eaSopenharmony_ci/* ===========================================================================
213275793eaSopenharmony_ci * Read a new buffer from the current input stream, update the adler32
214275793eaSopenharmony_ci * and total number of bytes read.  All deflate() input goes through
215275793eaSopenharmony_ci * this function so some applications may wish to modify it to avoid
216275793eaSopenharmony_ci * allocating a large strm->next_in buffer and copying from it.
217275793eaSopenharmony_ci * (See also flush_pending()).
218275793eaSopenharmony_ci */
219275793eaSopenharmony_cilocal unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size)
220275793eaSopenharmony_ci{
221275793eaSopenharmony_ci    unsigned len = strm->avail_in;
222275793eaSopenharmony_ci
223275793eaSopenharmony_ci    if (len > size) len = size;
224275793eaSopenharmony_ci    if (len == 0) return 0;
225275793eaSopenharmony_ci
226275793eaSopenharmony_ci    strm->avail_in  -= len;
227275793eaSopenharmony_ci
228275793eaSopenharmony_ci    zmemcpy(buf, strm->next_in, len);
229275793eaSopenharmony_ci    if (strm->state->wrap == 1) {
230275793eaSopenharmony_ci        strm->adler = adler32(strm->adler, buf, len);
231275793eaSopenharmony_ci    }
232275793eaSopenharmony_ci#ifdef GZIP
233275793eaSopenharmony_ci    else if (strm->state->wrap == 2) {
234275793eaSopenharmony_ci        strm->adler = crc32(strm->adler, buf, len);
235275793eaSopenharmony_ci    }
236275793eaSopenharmony_ci#endif
237275793eaSopenharmony_ci    strm->next_in  += len;
238275793eaSopenharmony_ci    strm->total_in += len;
239275793eaSopenharmony_ci
240275793eaSopenharmony_ci    return len;
241275793eaSopenharmony_ci}
242275793eaSopenharmony_ci
243275793eaSopenharmony_ci/* ===========================================================================
244275793eaSopenharmony_ci * Fill the window when the lookahead becomes insufficient.
245275793eaSopenharmony_ci * Updates strstart and lookahead.
246275793eaSopenharmony_ci *
247275793eaSopenharmony_ci * IN assertion: lookahead < MIN_LOOKAHEAD
248275793eaSopenharmony_ci * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
249275793eaSopenharmony_ci *    At least one byte has been read, or avail_in == 0; reads are
250275793eaSopenharmony_ci *    performed for at least two bytes (required for the zip translate_eol
251275793eaSopenharmony_ci *    option -- not supported here).
252275793eaSopenharmony_ci */
253275793eaSopenharmony_cilocal void fill_window(deflate_state *s)
254275793eaSopenharmony_ci{
255275793eaSopenharmony_ci    unsigned n;
256275793eaSopenharmony_ci    unsigned more;    /* Amount of free space at the end of the window. */
257275793eaSopenharmony_ci    uInt wsize = s->w_size;
258275793eaSopenharmony_ci
259275793eaSopenharmony_ci    Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
260275793eaSopenharmony_ci
261275793eaSopenharmony_ci    do {
262275793eaSopenharmony_ci        more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
263275793eaSopenharmony_ci
264275793eaSopenharmony_ci        /* Deal with !@#$% 64K limit: */
265275793eaSopenharmony_ci        if (sizeof(int) <= 2) {
266275793eaSopenharmony_ci            if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
267275793eaSopenharmony_ci                more = wsize;
268275793eaSopenharmony_ci
269275793eaSopenharmony_ci            } else if (more == (unsigned)(-1)) {
270275793eaSopenharmony_ci                /* Very unlikely, but possible on 16 bit machine if
271275793eaSopenharmony_ci                 * strstart == 0 && lookahead == 1 (input done a byte at time)
272275793eaSopenharmony_ci                 */
273275793eaSopenharmony_ci                more--;
274275793eaSopenharmony_ci            }
275275793eaSopenharmony_ci        }
276275793eaSopenharmony_ci
277275793eaSopenharmony_ci        /* If the window is almost full and there is insufficient lookahead,
278275793eaSopenharmony_ci         * move the upper half to the lower one to make room in the upper half.
279275793eaSopenharmony_ci         */
280275793eaSopenharmony_ci        if (s->strstart >= wsize + MAX_DIST(s)) {
281275793eaSopenharmony_ci
282275793eaSopenharmony_ci            zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more);
283275793eaSopenharmony_ci            s->match_start -= wsize;
284275793eaSopenharmony_ci            s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
285275793eaSopenharmony_ci            s->block_start -= (long) wsize;
286275793eaSopenharmony_ci            if (s->insert > s->strstart)
287275793eaSopenharmony_ci                s->insert = s->strstart;
288275793eaSopenharmony_ci            slide_hash(s);
289275793eaSopenharmony_ci            more += wsize;
290275793eaSopenharmony_ci        }
291275793eaSopenharmony_ci        if (s->strm->avail_in == 0) break;
292275793eaSopenharmony_ci
293275793eaSopenharmony_ci        /* If there was no sliding:
294275793eaSopenharmony_ci         *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
295275793eaSopenharmony_ci         *    more == window_size - lookahead - strstart
296275793eaSopenharmony_ci         * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
297275793eaSopenharmony_ci         * => more >= window_size - 2*WSIZE + 2
298275793eaSopenharmony_ci         * In the BIG_MEM or MMAP case (not yet supported),
299275793eaSopenharmony_ci         *   window_size == input_size + MIN_LOOKAHEAD  &&
300275793eaSopenharmony_ci         *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
301275793eaSopenharmony_ci         * Otherwise, window_size == 2*WSIZE so more >= 2.
302275793eaSopenharmony_ci         * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
303275793eaSopenharmony_ci         */
304275793eaSopenharmony_ci        Assert(more >= 2, "more < 2");
305275793eaSopenharmony_ci
306275793eaSopenharmony_ci        n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
307275793eaSopenharmony_ci        s->lookahead += n;
308275793eaSopenharmony_ci
309275793eaSopenharmony_ci        /* Initialize the hash value now that we have some input: */
310275793eaSopenharmony_ci        if (s->lookahead + s->insert >= MIN_MATCH) {
311275793eaSopenharmony_ci            uInt str = s->strstart - s->insert;
312275793eaSopenharmony_ci            s->ins_h = s->window[str];
313275793eaSopenharmony_ci            UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
314275793eaSopenharmony_ci#if MIN_MATCH != 3
315275793eaSopenharmony_ci            Call UPDATE_HASH() MIN_MATCH-3 more times
316275793eaSopenharmony_ci#endif
317275793eaSopenharmony_ci            while (s->insert) {
318275793eaSopenharmony_ci                UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
319275793eaSopenharmony_ci#ifndef FASTEST
320275793eaSopenharmony_ci                s->prev[str & s->w_mask] = s->head[s->ins_h];
321275793eaSopenharmony_ci#endif
322275793eaSopenharmony_ci                s->head[s->ins_h] = (Pos)str;
323275793eaSopenharmony_ci                str++;
324275793eaSopenharmony_ci                s->insert--;
325275793eaSopenharmony_ci                if (s->lookahead + s->insert < MIN_MATCH)
326275793eaSopenharmony_ci                    break;
327275793eaSopenharmony_ci            }
328275793eaSopenharmony_ci        }
329275793eaSopenharmony_ci        /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
330275793eaSopenharmony_ci         * but this is not important since only literal bytes will be emitted.
331275793eaSopenharmony_ci         */
332275793eaSopenharmony_ci
333275793eaSopenharmony_ci    } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
334275793eaSopenharmony_ci
335275793eaSopenharmony_ci    /* If the WIN_INIT bytes after the end of the current data have never been
336275793eaSopenharmony_ci     * written, then zero those bytes in order to avoid memory check reports of
337275793eaSopenharmony_ci     * the use of uninitialized (or uninitialised as Julian writes) bytes by
338275793eaSopenharmony_ci     * the longest match routines.  Update the high water mark for the next
339275793eaSopenharmony_ci     * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
340275793eaSopenharmony_ci     * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
341275793eaSopenharmony_ci     */
342275793eaSopenharmony_ci    if (s->high_water < s->window_size) {
343275793eaSopenharmony_ci        ulg curr = s->strstart + (ulg)(s->lookahead);
344275793eaSopenharmony_ci        ulg init;
345275793eaSopenharmony_ci
346275793eaSopenharmony_ci        if (s->high_water < curr) {
347275793eaSopenharmony_ci            /* Previous high water mark below current data -- zero WIN_INIT
348275793eaSopenharmony_ci             * bytes or up to end of window, whichever is less.
349275793eaSopenharmony_ci             */
350275793eaSopenharmony_ci            init = s->window_size - curr;
351275793eaSopenharmony_ci            if (init > WIN_INIT)
352275793eaSopenharmony_ci                init = WIN_INIT;
353275793eaSopenharmony_ci            zmemzero(s->window + curr, (unsigned)init);
354275793eaSopenharmony_ci            s->high_water = curr + init;
355275793eaSopenharmony_ci        }
356275793eaSopenharmony_ci        else if (s->high_water < (ulg)curr + WIN_INIT) {
357275793eaSopenharmony_ci            /* High water mark at or above current data, but below current data
358275793eaSopenharmony_ci             * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
359275793eaSopenharmony_ci             * to end of window, whichever is less.
360275793eaSopenharmony_ci             */
361275793eaSopenharmony_ci            init = (ulg)curr + WIN_INIT - s->high_water;
362275793eaSopenharmony_ci            if (init > s->window_size - s->high_water)
363275793eaSopenharmony_ci                init = s->window_size - s->high_water;
364275793eaSopenharmony_ci            zmemzero(s->window + s->high_water, (unsigned)init);
365275793eaSopenharmony_ci            s->high_water += init;
366275793eaSopenharmony_ci        }
367275793eaSopenharmony_ci    }
368275793eaSopenharmony_ci
369275793eaSopenharmony_ci    Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
370275793eaSopenharmony_ci           "not enough room for search");
371275793eaSopenharmony_ci}
372275793eaSopenharmony_ci
373275793eaSopenharmony_ci/* ========================================================================= */
374275793eaSopenharmony_ciint ZEXPORT deflateInit_(z_streamp strm, int level, const char *version,
375275793eaSopenharmony_ci                         int stream_size) {
376275793eaSopenharmony_ci    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
377275793eaSopenharmony_ci                         Z_DEFAULT_STRATEGY, version, stream_size);
378275793eaSopenharmony_ci    /* To do: ignore strm->next_in if we use it as window */
379275793eaSopenharmony_ci}
380275793eaSopenharmony_ci
381275793eaSopenharmony_ci/* ========================================================================= */
382275793eaSopenharmony_ciint ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
383275793eaSopenharmony_ci                          int windowBits, int memLevel, int strategy,
384275793eaSopenharmony_ci                          const char *version, int stream_size) {
385275793eaSopenharmony_ci    deflate_state *s;
386275793eaSopenharmony_ci    int wrap = 1;
387275793eaSopenharmony_ci    static const char my_version[] = ZLIB_VERSION;
388275793eaSopenharmony_ci
389275793eaSopenharmony_ci    if (version == Z_NULL || version[0] != my_version[0] ||
390275793eaSopenharmony_ci        stream_size != sizeof(z_stream)) {
391275793eaSopenharmony_ci        return Z_VERSION_ERROR;
392275793eaSopenharmony_ci    }
393275793eaSopenharmony_ci    if (strm == Z_NULL) return Z_STREAM_ERROR;
394275793eaSopenharmony_ci
395275793eaSopenharmony_ci    strm->msg = Z_NULL;
396275793eaSopenharmony_ci    if (strm->zalloc == (alloc_func)0) {
397275793eaSopenharmony_ci#ifdef Z_SOLO
398275793eaSopenharmony_ci        return Z_STREAM_ERROR;
399275793eaSopenharmony_ci#else
400275793eaSopenharmony_ci        strm->zalloc = zcalloc;
401275793eaSopenharmony_ci        strm->opaque = (voidpf)0;
402275793eaSopenharmony_ci#endif
403275793eaSopenharmony_ci    }
404275793eaSopenharmony_ci    if (strm->zfree == (free_func)0)
405275793eaSopenharmony_ci#ifdef Z_SOLO
406275793eaSopenharmony_ci        return Z_STREAM_ERROR;
407275793eaSopenharmony_ci#else
408275793eaSopenharmony_ci        strm->zfree = zcfree;
409275793eaSopenharmony_ci#endif
410275793eaSopenharmony_ci
411275793eaSopenharmony_ci#ifdef FASTEST
412275793eaSopenharmony_ci    if (level != 0) level = 1;
413275793eaSopenharmony_ci#else
414275793eaSopenharmony_ci    if (level == Z_DEFAULT_COMPRESSION) level = 6;
415275793eaSopenharmony_ci#endif
416275793eaSopenharmony_ci
417275793eaSopenharmony_ci    if (windowBits < 0) { /* suppress zlib wrapper */
418275793eaSopenharmony_ci        wrap = 0;
419275793eaSopenharmony_ci        if (windowBits < -15)
420275793eaSopenharmony_ci            return Z_STREAM_ERROR;
421275793eaSopenharmony_ci        windowBits = -windowBits;
422275793eaSopenharmony_ci    }
423275793eaSopenharmony_ci#ifdef GZIP
424275793eaSopenharmony_ci    else if (windowBits > 15) {
425275793eaSopenharmony_ci        wrap = 2;       /* write gzip wrapper instead */
426275793eaSopenharmony_ci        windowBits -= 16;
427275793eaSopenharmony_ci    }
428275793eaSopenharmony_ci#endif
429275793eaSopenharmony_ci    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
430275793eaSopenharmony_ci        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
431275793eaSopenharmony_ci        strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
432275793eaSopenharmony_ci        return Z_STREAM_ERROR;
433275793eaSopenharmony_ci    }
434275793eaSopenharmony_ci    if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
435275793eaSopenharmony_ci    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
436275793eaSopenharmony_ci    if (s == Z_NULL) return Z_MEM_ERROR;
437275793eaSopenharmony_ci    strm->state = (struct internal_state FAR *)s;
438275793eaSopenharmony_ci    s->strm = strm;
439275793eaSopenharmony_ci    s->status = INIT_STATE;     /* to pass state test in deflateReset() */
440275793eaSopenharmony_ci
441275793eaSopenharmony_ci    s->wrap = wrap;
442275793eaSopenharmony_ci    s->gzhead = Z_NULL;
443275793eaSopenharmony_ci    s->w_bits = (uInt)windowBits;
444275793eaSopenharmony_ci    s->w_size = 1 << s->w_bits;
445275793eaSopenharmony_ci    s->w_mask = s->w_size - 1;
446275793eaSopenharmony_ci
447275793eaSopenharmony_ci    s->hash_bits = (uInt)memLevel + 7;
448275793eaSopenharmony_ci    s->hash_size = 1 << s->hash_bits;
449275793eaSopenharmony_ci    s->hash_mask = s->hash_size - 1;
450275793eaSopenharmony_ci    s->hash_shift =  ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH);
451275793eaSopenharmony_ci
452275793eaSopenharmony_ci    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
453275793eaSopenharmony_ci    s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
454275793eaSopenharmony_ci    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
455275793eaSopenharmony_ci
456275793eaSopenharmony_ci    s->high_water = 0;      /* nothing written to s->window yet */
457275793eaSopenharmony_ci
458275793eaSopenharmony_ci    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
459275793eaSopenharmony_ci
460275793eaSopenharmony_ci    /* We overlay pending_buf and sym_buf. This works since the average size
461275793eaSopenharmony_ci     * for length/distance pairs over any compressed block is assured to be 31
462275793eaSopenharmony_ci     * bits or less.
463275793eaSopenharmony_ci     *
464275793eaSopenharmony_ci     * Analysis: The longest fixed codes are a length code of 8 bits plus 5
465275793eaSopenharmony_ci     * extra bits, for lengths 131 to 257. The longest fixed distance codes are
466275793eaSopenharmony_ci     * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
467275793eaSopenharmony_ci     * possible fixed-codes length/distance pair is then 31 bits total.
468275793eaSopenharmony_ci     *
469275793eaSopenharmony_ci     * sym_buf starts one-fourth of the way into pending_buf. So there are
470275793eaSopenharmony_ci     * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
471275793eaSopenharmony_ci     * in sym_buf is three bytes -- two for the distance and one for the
472275793eaSopenharmony_ci     * literal/length. As each symbol is consumed, the pointer to the next
473275793eaSopenharmony_ci     * sym_buf value to read moves forward three bytes. From that symbol, up to
474275793eaSopenharmony_ci     * 31 bits are written to pending_buf. The closest the written pending_buf
475275793eaSopenharmony_ci     * bits gets to the next sym_buf symbol to read is just before the last
476275793eaSopenharmony_ci     * code is written. At that time, 31*(n - 2) bits have been written, just
477275793eaSopenharmony_ci     * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at
478275793eaSopenharmony_ci     * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1
479275793eaSopenharmony_ci     * symbols are written.) The closest the writing gets to what is unread is
480275793eaSopenharmony_ci     * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and
481275793eaSopenharmony_ci     * can range from 128 to 32768.
482275793eaSopenharmony_ci     *
483275793eaSopenharmony_ci     * Therefore, at a minimum, there are 142 bits of space between what is
484275793eaSopenharmony_ci     * written and what is read in the overlain buffers, so the symbols cannot
485275793eaSopenharmony_ci     * be overwritten by the compressed data. That space is actually 139 bits,
486275793eaSopenharmony_ci     * due to the three-bit fixed-code block header.
487275793eaSopenharmony_ci     *
488275793eaSopenharmony_ci     * That covers the case where either Z_FIXED is specified, forcing fixed
489275793eaSopenharmony_ci     * codes, or when the use of fixed codes is chosen, because that choice
490275793eaSopenharmony_ci     * results in a smaller compressed block than dynamic codes. That latter
491275793eaSopenharmony_ci     * condition then assures that the above analysis also covers all dynamic
492275793eaSopenharmony_ci     * blocks. A dynamic-code block will only be chosen to be emitted if it has
493275793eaSopenharmony_ci     * fewer bits than a fixed-code block would for the same set of symbols.
494275793eaSopenharmony_ci     * Therefore its average symbol length is assured to be less than 31. So
495275793eaSopenharmony_ci     * the compressed data for a dynamic block also cannot overwrite the
496275793eaSopenharmony_ci     * symbols from which it is being constructed.
497275793eaSopenharmony_ci     */
498275793eaSopenharmony_ci
499275793eaSopenharmony_ci    s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, LIT_BUFS);
500275793eaSopenharmony_ci    s->pending_buf_size = (ulg)s->lit_bufsize * 4;
501275793eaSopenharmony_ci
502275793eaSopenharmony_ci    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
503275793eaSopenharmony_ci        s->pending_buf == Z_NULL) {
504275793eaSopenharmony_ci        s->status = FINISH_STATE;
505275793eaSopenharmony_ci        strm->msg = ERR_MSG(Z_MEM_ERROR);
506275793eaSopenharmony_ci        deflateEnd (strm);
507275793eaSopenharmony_ci        return Z_MEM_ERROR;
508275793eaSopenharmony_ci    }
509275793eaSopenharmony_ci#ifdef LIT_MEM
510275793eaSopenharmony_ci    s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
511275793eaSopenharmony_ci    s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
512275793eaSopenharmony_ci    s->sym_end = s->lit_bufsize - 1;
513275793eaSopenharmony_ci#else
514275793eaSopenharmony_ci    s->sym_buf = s->pending_buf + s->lit_bufsize;
515275793eaSopenharmony_ci    s->sym_end = (s->lit_bufsize - 1) * 3;
516275793eaSopenharmony_ci#endif
517275793eaSopenharmony_ci    /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
518275793eaSopenharmony_ci     * on 16 bit machines and because stored blocks are restricted to
519275793eaSopenharmony_ci     * 64K-1 bytes.
520275793eaSopenharmony_ci     */
521275793eaSopenharmony_ci
522275793eaSopenharmony_ci    s->level = level;
523275793eaSopenharmony_ci    s->strategy = strategy;
524275793eaSopenharmony_ci    s->method = (Byte)method;
525275793eaSopenharmony_ci
526275793eaSopenharmony_ci    return deflateReset(strm);
527275793eaSopenharmony_ci}
528275793eaSopenharmony_ci
529275793eaSopenharmony_ci/* =========================================================================
530275793eaSopenharmony_ci * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
531275793eaSopenharmony_ci */
532275793eaSopenharmony_cilocal int deflateStateCheck(z_streamp strm)
533275793eaSopenharmony_ci{
534275793eaSopenharmony_ci    deflate_state *s;
535275793eaSopenharmony_ci    if (strm == Z_NULL ||
536275793eaSopenharmony_ci        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
537275793eaSopenharmony_ci        return 1;
538275793eaSopenharmony_ci    s = strm->state;
539275793eaSopenharmony_ci    if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
540275793eaSopenharmony_ci#ifdef GZIP
541275793eaSopenharmony_ci                                           s->status != GZIP_STATE &&
542275793eaSopenharmony_ci#endif
543275793eaSopenharmony_ci                                           s->status != EXTRA_STATE &&
544275793eaSopenharmony_ci                                           s->status != NAME_STATE &&
545275793eaSopenharmony_ci                                           s->status != COMMENT_STATE &&
546275793eaSopenharmony_ci                                           s->status != HCRC_STATE &&
547275793eaSopenharmony_ci                                           s->status != BUSY_STATE &&
548275793eaSopenharmony_ci                                           s->status != FINISH_STATE))
549275793eaSopenharmony_ci        return 1;
550275793eaSopenharmony_ci    return 0;
551275793eaSopenharmony_ci}
552275793eaSopenharmony_ci
553275793eaSopenharmony_ci/* ========================================================================= */
554275793eaSopenharmony_ciint ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary,
555275793eaSopenharmony_ci                                 uInt  dictLength) {
556275793eaSopenharmony_ci    deflate_state *s;
557275793eaSopenharmony_ci    uInt str, n;
558275793eaSopenharmony_ci    int wrap;
559275793eaSopenharmony_ci    unsigned avail;
560275793eaSopenharmony_ci    z_const unsigned char *next;
561275793eaSopenharmony_ci
562275793eaSopenharmony_ci    if (deflateStateCheck(strm) || dictionary == Z_NULL)
563275793eaSopenharmony_ci        return Z_STREAM_ERROR;
564275793eaSopenharmony_ci    s = strm->state;
565275793eaSopenharmony_ci    wrap = s->wrap;
566275793eaSopenharmony_ci    if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
567275793eaSopenharmony_ci        return Z_STREAM_ERROR;
568275793eaSopenharmony_ci
569275793eaSopenharmony_ci    /* when using zlib wrappers, compute Adler-32 for provided dictionary */
570275793eaSopenharmony_ci    if (wrap == 1)
571275793eaSopenharmony_ci        strm->adler = adler32(strm->adler, dictionary, dictLength);
572275793eaSopenharmony_ci    s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
573275793eaSopenharmony_ci
574275793eaSopenharmony_ci    /* if dictionary would fill window, just replace the history */
575275793eaSopenharmony_ci    if (dictLength >= s->w_size) {
576275793eaSopenharmony_ci        if (wrap == 0) {            /* already empty otherwise */
577275793eaSopenharmony_ci            CLEAR_HASH(s);
578275793eaSopenharmony_ci            s->strstart = 0;
579275793eaSopenharmony_ci            s->block_start = 0L;
580275793eaSopenharmony_ci            s->insert = 0;
581275793eaSopenharmony_ci        }
582275793eaSopenharmony_ci        dictionary += dictLength - s->w_size;  /* use the tail */
583275793eaSopenharmony_ci        dictLength = s->w_size;
584275793eaSopenharmony_ci    }
585275793eaSopenharmony_ci
586275793eaSopenharmony_ci    /* insert dictionary into window and hash */
587275793eaSopenharmony_ci    avail = strm->avail_in;
588275793eaSopenharmony_ci    next = strm->next_in;
589275793eaSopenharmony_ci    strm->avail_in = dictLength;
590275793eaSopenharmony_ci    strm->next_in = (z_const Bytef *)dictionary;
591275793eaSopenharmony_ci    fill_window(s);
592275793eaSopenharmony_ci    while (s->lookahead >= MIN_MATCH) {
593275793eaSopenharmony_ci        str = s->strstart;
594275793eaSopenharmony_ci        n = s->lookahead - (MIN_MATCH-1);
595275793eaSopenharmony_ci        do {
596275793eaSopenharmony_ci            UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
597275793eaSopenharmony_ci#ifndef FASTEST
598275793eaSopenharmony_ci            s->prev[str & s->w_mask] = s->head[s->ins_h];
599275793eaSopenharmony_ci#endif
600275793eaSopenharmony_ci            s->head[s->ins_h] = (Pos)str;
601275793eaSopenharmony_ci            str++;
602275793eaSopenharmony_ci        } while (--n);
603275793eaSopenharmony_ci        s->strstart = str;
604275793eaSopenharmony_ci        s->lookahead = MIN_MATCH-1;
605275793eaSopenharmony_ci        fill_window(s);
606275793eaSopenharmony_ci    }
607275793eaSopenharmony_ci    s->strstart += s->lookahead;
608275793eaSopenharmony_ci    s->block_start = (long)s->strstart;
609275793eaSopenharmony_ci    s->insert = s->lookahead;
610275793eaSopenharmony_ci    s->lookahead = 0;
611275793eaSopenharmony_ci    s->match_length = s->prev_length = MIN_MATCH-1;
612275793eaSopenharmony_ci    s->match_available = 0;
613275793eaSopenharmony_ci    strm->next_in = next;
614275793eaSopenharmony_ci    strm->avail_in = avail;
615275793eaSopenharmony_ci    s->wrap = wrap;
616275793eaSopenharmony_ci    return Z_OK;
617275793eaSopenharmony_ci}
618275793eaSopenharmony_ci
619275793eaSopenharmony_ci/* ========================================================================= */
620275793eaSopenharmony_ciint ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary,
621275793eaSopenharmony_ci                                 uInt *dictLength) {
622275793eaSopenharmony_ci    deflate_state *s;
623275793eaSopenharmony_ci    uInt len;
624275793eaSopenharmony_ci
625275793eaSopenharmony_ci    if (deflateStateCheck(strm))
626275793eaSopenharmony_ci        return Z_STREAM_ERROR;
627275793eaSopenharmony_ci    s = strm->state;
628275793eaSopenharmony_ci    len = s->strstart + s->lookahead;
629275793eaSopenharmony_ci    if (len > s->w_size)
630275793eaSopenharmony_ci        len = s->w_size;
631275793eaSopenharmony_ci    if (dictionary != Z_NULL && len)
632275793eaSopenharmony_ci        zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
633275793eaSopenharmony_ci    if (dictLength != Z_NULL)
634275793eaSopenharmony_ci        *dictLength = len;
635275793eaSopenharmony_ci    return Z_OK;
636275793eaSopenharmony_ci}
637275793eaSopenharmony_ci
638275793eaSopenharmony_ci/* ========================================================================= */
639275793eaSopenharmony_ciint ZEXPORT deflateResetKeep(z_streamp strm) {
640275793eaSopenharmony_ci    deflate_state *s;
641275793eaSopenharmony_ci
642275793eaSopenharmony_ci    if (deflateStateCheck(strm)) {
643275793eaSopenharmony_ci        return Z_STREAM_ERROR;
644275793eaSopenharmony_ci    }
645275793eaSopenharmony_ci
646275793eaSopenharmony_ci    strm->total_in = strm->total_out = 0;
647275793eaSopenharmony_ci    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
648275793eaSopenharmony_ci    strm->data_type = Z_UNKNOWN;
649275793eaSopenharmony_ci
650275793eaSopenharmony_ci    s = (deflate_state *)strm->state;
651275793eaSopenharmony_ci    s->pending = 0;
652275793eaSopenharmony_ci    s->pending_out = s->pending_buf;
653275793eaSopenharmony_ci
654275793eaSopenharmony_ci    if (s->wrap < 0) {
655275793eaSopenharmony_ci        s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
656275793eaSopenharmony_ci    }
657275793eaSopenharmony_ci    s->status =
658275793eaSopenharmony_ci#ifdef GZIP
659275793eaSopenharmony_ci        s->wrap == 2 ? GZIP_STATE :
660275793eaSopenharmony_ci#endif
661275793eaSopenharmony_ci        INIT_STATE;
662275793eaSopenharmony_ci    strm->adler =
663275793eaSopenharmony_ci#ifdef GZIP
664275793eaSopenharmony_ci        s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
665275793eaSopenharmony_ci#endif
666275793eaSopenharmony_ci        adler32(0L, Z_NULL, 0);
667275793eaSopenharmony_ci    s->last_flush = -2;
668275793eaSopenharmony_ci
669275793eaSopenharmony_ci    _tr_init(s);
670275793eaSopenharmony_ci
671275793eaSopenharmony_ci    return Z_OK;
672275793eaSopenharmony_ci}
673275793eaSopenharmony_ci
674275793eaSopenharmony_ci/* ===========================================================================
675275793eaSopenharmony_ci * Initialize the "longest match" routines for a new zlib stream
676275793eaSopenharmony_ci */
677275793eaSopenharmony_cilocal void lm_init(deflate_state *s)
678275793eaSopenharmony_ci{
679275793eaSopenharmony_ci    s->window_size = (ulg)2L*s->w_size;
680275793eaSopenharmony_ci
681275793eaSopenharmony_ci    CLEAR_HASH(s);
682275793eaSopenharmony_ci
683275793eaSopenharmony_ci    /* Set the default configuration parameters:
684275793eaSopenharmony_ci     */
685275793eaSopenharmony_ci    s->max_lazy_match   = configuration_table[s->level].max_lazy;
686275793eaSopenharmony_ci    s->good_match       = configuration_table[s->level].good_length;
687275793eaSopenharmony_ci    s->nice_match       = configuration_table[s->level].nice_length;
688275793eaSopenharmony_ci    s->max_chain_length = configuration_table[s->level].max_chain;
689275793eaSopenharmony_ci
690275793eaSopenharmony_ci    s->strstart = 0;
691275793eaSopenharmony_ci    s->block_start = 0L;
692275793eaSopenharmony_ci    s->lookahead = 0;
693275793eaSopenharmony_ci    s->insert = 0;
694275793eaSopenharmony_ci    s->match_length = s->prev_length = MIN_MATCH-1;
695275793eaSopenharmony_ci    s->match_available = 0;
696275793eaSopenharmony_ci    s->ins_h = 0;
697275793eaSopenharmony_ci}
698275793eaSopenharmony_ci
699275793eaSopenharmony_ci/* ========================================================================= */
700275793eaSopenharmony_ciint ZEXPORT deflateReset(z_streamp strm) {
701275793eaSopenharmony_ci    int ret;
702275793eaSopenharmony_ci
703275793eaSopenharmony_ci    ret = deflateResetKeep(strm);
704275793eaSopenharmony_ci    if (ret == Z_OK)
705275793eaSopenharmony_ci        lm_init(strm->state);
706275793eaSopenharmony_ci    return ret;
707275793eaSopenharmony_ci}
708275793eaSopenharmony_ci
709275793eaSopenharmony_ci/* ========================================================================= */
710275793eaSopenharmony_ciint ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head) {
711275793eaSopenharmony_ci    if (deflateStateCheck(strm) || strm->state->wrap != 2)
712275793eaSopenharmony_ci        return Z_STREAM_ERROR;
713275793eaSopenharmony_ci    strm->state->gzhead = head;
714275793eaSopenharmony_ci    return Z_OK;
715275793eaSopenharmony_ci}
716275793eaSopenharmony_ci
717275793eaSopenharmony_ci/* ========================================================================= */
718275793eaSopenharmony_ciint ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) {
719275793eaSopenharmony_ci    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
720275793eaSopenharmony_ci    if (pending != Z_NULL)
721275793eaSopenharmony_ci        *pending = strm->state->pending;
722275793eaSopenharmony_ci    if (bits != Z_NULL)
723275793eaSopenharmony_ci        *bits = strm->state->bi_valid;
724275793eaSopenharmony_ci    return Z_OK;
725275793eaSopenharmony_ci}
726275793eaSopenharmony_ci
727275793eaSopenharmony_ci/* ========================================================================= */
728275793eaSopenharmony_ciint ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
729275793eaSopenharmony_ci    deflate_state *s;
730275793eaSopenharmony_ci    int put;
731275793eaSopenharmony_ci
732275793eaSopenharmony_ci    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
733275793eaSopenharmony_ci    s = strm->state;
734275793eaSopenharmony_ci#ifdef LIT_MEM
735275793eaSopenharmony_ci    if (bits < 0 || bits > 16 ||
736275793eaSopenharmony_ci        (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3))
737275793eaSopenharmony_ci        return Z_BUF_ERROR;
738275793eaSopenharmony_ci#else
739275793eaSopenharmony_ci    if (bits < 0 || bits > 16 ||
740275793eaSopenharmony_ci        s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
741275793eaSopenharmony_ci        return Z_BUF_ERROR;
742275793eaSopenharmony_ci#endif
743275793eaSopenharmony_ci    do {
744275793eaSopenharmony_ci        put = Buf_size - s->bi_valid;
745275793eaSopenharmony_ci        if (put > bits)
746275793eaSopenharmony_ci            put = bits;
747275793eaSopenharmony_ci        s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
748275793eaSopenharmony_ci        s->bi_valid += put;
749275793eaSopenharmony_ci        _tr_flush_bits(s);
750275793eaSopenharmony_ci        value >>= put;
751275793eaSopenharmony_ci        bits -= put;
752275793eaSopenharmony_ci    } while (bits);
753275793eaSopenharmony_ci    return Z_OK;
754275793eaSopenharmony_ci}
755275793eaSopenharmony_ci
756275793eaSopenharmony_ci/* ========================================================================= */
757275793eaSopenharmony_ciint ZEXPORT deflateParams(z_streamp strm, int level, int strategy) {
758275793eaSopenharmony_ci    deflate_state *s;
759275793eaSopenharmony_ci    compress_func func;
760275793eaSopenharmony_ci
761275793eaSopenharmony_ci    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
762275793eaSopenharmony_ci    s = strm->state;
763275793eaSopenharmony_ci
764275793eaSopenharmony_ci#ifdef FASTEST
765275793eaSopenharmony_ci    if (level != 0) level = 1;
766275793eaSopenharmony_ci#else
767275793eaSopenharmony_ci    if (level == Z_DEFAULT_COMPRESSION) level = 6;
768275793eaSopenharmony_ci#endif
769275793eaSopenharmony_ci    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
770275793eaSopenharmony_ci        return Z_STREAM_ERROR;
771275793eaSopenharmony_ci    }
772275793eaSopenharmony_ci    func = configuration_table[s->level].func;
773275793eaSopenharmony_ci
774275793eaSopenharmony_ci    if ((strategy != s->strategy || func != configuration_table[level].func) &&
775275793eaSopenharmony_ci        s->last_flush != -2) {
776275793eaSopenharmony_ci        /* Flush the last buffer: */
777275793eaSopenharmony_ci        int err = deflate(strm, Z_BLOCK);
778275793eaSopenharmony_ci        if (err == Z_STREAM_ERROR)
779275793eaSopenharmony_ci            return err;
780275793eaSopenharmony_ci        if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
781275793eaSopenharmony_ci            return Z_BUF_ERROR;
782275793eaSopenharmony_ci    }
783275793eaSopenharmony_ci    if (s->level != level) {
784275793eaSopenharmony_ci        if (s->level == 0 && s->matches != 0) {
785275793eaSopenharmony_ci            if (s->matches == 1)
786275793eaSopenharmony_ci                slide_hash(s);
787275793eaSopenharmony_ci            else
788275793eaSopenharmony_ci                CLEAR_HASH(s);
789275793eaSopenharmony_ci            s->matches = 0;
790275793eaSopenharmony_ci        }
791275793eaSopenharmony_ci        s->level = level;
792275793eaSopenharmony_ci        s->max_lazy_match   = configuration_table[level].max_lazy;
793275793eaSopenharmony_ci        s->good_match       = configuration_table[level].good_length;
794275793eaSopenharmony_ci        s->nice_match       = configuration_table[level].nice_length;
795275793eaSopenharmony_ci        s->max_chain_length = configuration_table[level].max_chain;
796275793eaSopenharmony_ci    }
797275793eaSopenharmony_ci    s->strategy = strategy;
798275793eaSopenharmony_ci    return Z_OK;
799275793eaSopenharmony_ci}
800275793eaSopenharmony_ci
801275793eaSopenharmony_ci/* ========================================================================= */
802275793eaSopenharmony_ciint ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy,
803275793eaSopenharmony_ci                        int nice_length, int max_chain) {
804275793eaSopenharmony_ci    deflate_state *s;
805275793eaSopenharmony_ci
806275793eaSopenharmony_ci    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
807275793eaSopenharmony_ci    s = strm->state;
808275793eaSopenharmony_ci    s->good_match = (uInt)good_length;
809275793eaSopenharmony_ci    s->max_lazy_match = (uInt)max_lazy;
810275793eaSopenharmony_ci    s->nice_match = nice_length;
811275793eaSopenharmony_ci    s->max_chain_length = (uInt)max_chain;
812275793eaSopenharmony_ci    return Z_OK;
813275793eaSopenharmony_ci}
814275793eaSopenharmony_ci
815275793eaSopenharmony_ci/* =========================================================================
816275793eaSopenharmony_ci * For the default windowBits of 15 and memLevel of 8, this function returns a
817275793eaSopenharmony_ci * close to exact, as well as small, upper bound on the compressed size. This
818275793eaSopenharmony_ci * is an expansion of ~0.03%, plus a small constant.
819275793eaSopenharmony_ci *
820275793eaSopenharmony_ci * For any setting other than those defaults for windowBits and memLevel, one
821275793eaSopenharmony_ci * of two worst case bounds is returned. This is at most an expansion of ~4% or
822275793eaSopenharmony_ci * ~13%, plus a small constant.
823275793eaSopenharmony_ci *
824275793eaSopenharmony_ci * Both the 0.03% and 4% derive from the overhead of stored blocks. The first
825275793eaSopenharmony_ci * one is for stored blocks of 16383 bytes (memLevel == 8), whereas the second
826275793eaSopenharmony_ci * is for stored blocks of 127 bytes (the worst case memLevel == 1). The
827275793eaSopenharmony_ci * expansion results from five bytes of header for each stored block.
828275793eaSopenharmony_ci *
829275793eaSopenharmony_ci * The larger expansion of 13% results from a window size less than or equal to
830275793eaSopenharmony_ci * the symbols buffer size (windowBits <= memLevel + 7). In that case some of
831275793eaSopenharmony_ci * the data being compressed may have slid out of the sliding window, impeding
832275793eaSopenharmony_ci * a stored block from being emitted. Then the only choice is a fixed or
833275793eaSopenharmony_ci * dynamic block, where a fixed block limits the maximum expansion to 9 bits
834275793eaSopenharmony_ci * per 8-bit byte, plus 10 bits for every block. The smallest block size for
835275793eaSopenharmony_ci * which this can occur is 255 (memLevel == 2).
836275793eaSopenharmony_ci *
837275793eaSopenharmony_ci * Shifts are used to approximate divisions, for speed.
838275793eaSopenharmony_ci */
839275793eaSopenharmony_ciuLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen)
840275793eaSopenharmony_ci{
841275793eaSopenharmony_ci    deflate_state *s;
842275793eaSopenharmony_ci    uLong fixedlen, storelen, wraplen;
843275793eaSopenharmony_ci
844275793eaSopenharmony_ci    /* upper bound for fixed blocks with 9-bit literals and length 255
845275793eaSopenharmony_ci       (memLevel == 2, which is the lowest that may not use stored blocks) --
846275793eaSopenharmony_ci       ~13% overhead plus a small constant */
847275793eaSopenharmony_ci    fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) +
848275793eaSopenharmony_ci               (sourceLen >> 9) + 4;
849275793eaSopenharmony_ci
850275793eaSopenharmony_ci    /* upper bound for stored blocks with length 127 (memLevel == 1) --
851275793eaSopenharmony_ci       ~4% overhead plus a small constant */
852275793eaSopenharmony_ci    storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) +
853275793eaSopenharmony_ci               (sourceLen >> 11) + 7;
854275793eaSopenharmony_ci
855275793eaSopenharmony_ci    /* if can't get parameters, return larger bound plus a zlib wrapper */
856275793eaSopenharmony_ci    if (deflateStateCheck(strm))
857275793eaSopenharmony_ci        return (fixedlen > storelen ? fixedlen : storelen) + 6;
858275793eaSopenharmony_ci
859275793eaSopenharmony_ci    /* compute wrapper length */
860275793eaSopenharmony_ci    s = strm->state;
861275793eaSopenharmony_ci    switch (s->wrap) {
862275793eaSopenharmony_ci    case 0:                                 /* raw deflate */
863275793eaSopenharmony_ci        wraplen = 0;
864275793eaSopenharmony_ci        break;
865275793eaSopenharmony_ci    case 1:                                 /* zlib wrapper */
866275793eaSopenharmony_ci        wraplen = 6 + (s->strstart ? 4 : 0);
867275793eaSopenharmony_ci        break;
868275793eaSopenharmony_ci#ifdef GZIP
869275793eaSopenharmony_ci    case 2:                                 /* gzip wrapper */
870275793eaSopenharmony_ci        wraplen = 18;
871275793eaSopenharmony_ci        if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
872275793eaSopenharmony_ci            Bytef *str;
873275793eaSopenharmony_ci            if (s->gzhead->extra != Z_NULL)
874275793eaSopenharmony_ci                wraplen += 2 + s->gzhead->extra_len;
875275793eaSopenharmony_ci            str = s->gzhead->name;
876275793eaSopenharmony_ci            if (str != Z_NULL)
877275793eaSopenharmony_ci                do {
878275793eaSopenharmony_ci                    wraplen++;
879275793eaSopenharmony_ci                } while (*str++);
880275793eaSopenharmony_ci            str = s->gzhead->comment;
881275793eaSopenharmony_ci            if (str != Z_NULL)
882275793eaSopenharmony_ci                do {
883275793eaSopenharmony_ci                    wraplen++;
884275793eaSopenharmony_ci                } while (*str++);
885275793eaSopenharmony_ci            if (s->gzhead->hcrc)
886275793eaSopenharmony_ci                wraplen += 2;
887275793eaSopenharmony_ci        }
888275793eaSopenharmony_ci        break;
889275793eaSopenharmony_ci#endif
890275793eaSopenharmony_ci    default:                                /* for compiler happiness */
891275793eaSopenharmony_ci        wraplen = 6;
892275793eaSopenharmony_ci    }
893275793eaSopenharmony_ci
894275793eaSopenharmony_ci    /* if not default parameters, return one of the conservative bounds */
895275793eaSopenharmony_ci    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
896275793eaSopenharmony_ci        return (s->w_bits <= s->hash_bits && s->level ? fixedlen : storelen) +
897275793eaSopenharmony_ci               wraplen;
898275793eaSopenharmony_ci
899275793eaSopenharmony_ci    /* default settings: return tight bound for that case -- ~0.03% overhead
900275793eaSopenharmony_ci       plus a small constant */
901275793eaSopenharmony_ci    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
902275793eaSopenharmony_ci           (sourceLen >> 25) + 13 - 6 + wraplen;
903275793eaSopenharmony_ci}
904275793eaSopenharmony_ci
905275793eaSopenharmony_ci/* =========================================================================
906275793eaSopenharmony_ci * Put a short in the pending buffer. The 16-bit value is put in MSB order.
907275793eaSopenharmony_ci * IN assertion: the stream state is correct and there is enough room in
908275793eaSopenharmony_ci * pending_buf.
909275793eaSopenharmony_ci */
910275793eaSopenharmony_cilocal void putShortMSB(deflate_state *s, uInt b)
911275793eaSopenharmony_ci{
912275793eaSopenharmony_ci    put_byte(s, (Byte)(b >> 8));
913275793eaSopenharmony_ci    put_byte(s, (Byte)(b & 0xff));
914275793eaSopenharmony_ci}
915275793eaSopenharmony_ci
916275793eaSopenharmony_ci/* =========================================================================
917275793eaSopenharmony_ci * Flush as much pending output as possible. All deflate() output, except for
918275793eaSopenharmony_ci * some deflate_stored() output, goes through this function so some
919275793eaSopenharmony_ci * applications may wish to modify it to avoid allocating a large
920275793eaSopenharmony_ci * strm->next_out buffer and copying into it. (See also read_buf()).
921275793eaSopenharmony_ci */
922275793eaSopenharmony_cilocal void flush_pending(z_streamp strm)
923275793eaSopenharmony_ci{
924275793eaSopenharmony_ci    unsigned len;
925275793eaSopenharmony_ci    deflate_state *s = strm->state;
926275793eaSopenharmony_ci
927275793eaSopenharmony_ci    _tr_flush_bits(s);
928275793eaSopenharmony_ci    len = s->pending;
929275793eaSopenharmony_ci    if (len > strm->avail_out) len = strm->avail_out;
930275793eaSopenharmony_ci    if (len == 0) return;
931275793eaSopenharmony_ci
932275793eaSopenharmony_ci    zmemcpy(strm->next_out, s->pending_out, len);
933275793eaSopenharmony_ci    strm->next_out  += len;
934275793eaSopenharmony_ci    s->pending_out  += len;
935275793eaSopenharmony_ci    strm->total_out += len;
936275793eaSopenharmony_ci    strm->avail_out -= len;
937275793eaSopenharmony_ci    s->pending      -= len;
938275793eaSopenharmony_ci    if (s->pending == 0) {
939275793eaSopenharmony_ci        s->pending_out = s->pending_buf;
940275793eaSopenharmony_ci    }
941275793eaSopenharmony_ci}
942275793eaSopenharmony_ci
943275793eaSopenharmony_ci/* ===========================================================================
944275793eaSopenharmony_ci * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
945275793eaSopenharmony_ci */
946275793eaSopenharmony_ci#define HCRC_UPDATE(beg) \
947275793eaSopenharmony_ci    do { \
948275793eaSopenharmony_ci        if (s->gzhead->hcrc && s->pending > (beg)) \
949275793eaSopenharmony_ci            strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
950275793eaSopenharmony_ci                                s->pending - (beg)); \
951275793eaSopenharmony_ci    } while (0)
952275793eaSopenharmony_ci
953275793eaSopenharmony_ci/* ========================================================================= */
954275793eaSopenharmony_ciint ZEXPORT deflate(z_streamp strm, int flush) {
955275793eaSopenharmony_ci    int old_flush; /* value of flush param for previous deflate call */
956275793eaSopenharmony_ci    deflate_state *s;
957275793eaSopenharmony_ci
958275793eaSopenharmony_ci    if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
959275793eaSopenharmony_ci        return Z_STREAM_ERROR;
960275793eaSopenharmony_ci    }
961275793eaSopenharmony_ci    s = strm->state;
962275793eaSopenharmony_ci
963275793eaSopenharmony_ci    if (strm->next_out == Z_NULL ||
964275793eaSopenharmony_ci        (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
965275793eaSopenharmony_ci        (s->status == FINISH_STATE && flush != Z_FINISH)) {
966275793eaSopenharmony_ci        ERR_RETURN(strm, Z_STREAM_ERROR);
967275793eaSopenharmony_ci    }
968275793eaSopenharmony_ci    if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
969275793eaSopenharmony_ci
970275793eaSopenharmony_ci    old_flush = s->last_flush;
971275793eaSopenharmony_ci    s->last_flush = flush;
972275793eaSopenharmony_ci
973275793eaSopenharmony_ci    /* Flush as much pending output as possible */
974275793eaSopenharmony_ci    if (s->pending != 0) {
975275793eaSopenharmony_ci        flush_pending(strm);
976275793eaSopenharmony_ci        if (strm->avail_out == 0) {
977275793eaSopenharmony_ci            /* Since avail_out is 0, deflate will be called again with
978275793eaSopenharmony_ci             * more output space, but possibly with both pending and
979275793eaSopenharmony_ci             * avail_in equal to zero. There won't be anything to do,
980275793eaSopenharmony_ci             * but this is not an error situation so make sure we
981275793eaSopenharmony_ci             * return OK instead of BUF_ERROR at next call of deflate:
982275793eaSopenharmony_ci             */
983275793eaSopenharmony_ci            s->last_flush = -1;
984275793eaSopenharmony_ci            return Z_OK;
985275793eaSopenharmony_ci        }
986275793eaSopenharmony_ci
987275793eaSopenharmony_ci    /* Make sure there is something to do and avoid duplicate consecutive
988275793eaSopenharmony_ci     * flushes. For repeated and useless calls with Z_FINISH, we keep
989275793eaSopenharmony_ci     * returning Z_STREAM_END instead of Z_BUF_ERROR.
990275793eaSopenharmony_ci     */
991275793eaSopenharmony_ci    } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
992275793eaSopenharmony_ci               flush != Z_FINISH) {
993275793eaSopenharmony_ci        ERR_RETURN(strm, Z_BUF_ERROR);
994275793eaSopenharmony_ci    }
995275793eaSopenharmony_ci
996275793eaSopenharmony_ci    /* User must not provide more input after the first FINISH: */
997275793eaSopenharmony_ci    if (s->status == FINISH_STATE && strm->avail_in != 0) {
998275793eaSopenharmony_ci        ERR_RETURN(strm, Z_BUF_ERROR);
999275793eaSopenharmony_ci    }
1000275793eaSopenharmony_ci
1001275793eaSopenharmony_ci    /* Write the header */
1002275793eaSopenharmony_ci    if (s->status == INIT_STATE && s->wrap == 0)
1003275793eaSopenharmony_ci        s->status = BUSY_STATE;
1004275793eaSopenharmony_ci    if (s->status == INIT_STATE) {
1005275793eaSopenharmony_ci        /* zlib header */
1006275793eaSopenharmony_ci        uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8;
1007275793eaSopenharmony_ci        uInt level_flags;
1008275793eaSopenharmony_ci
1009275793eaSopenharmony_ci        if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
1010275793eaSopenharmony_ci            level_flags = 0;
1011275793eaSopenharmony_ci        else if (s->level < 6)
1012275793eaSopenharmony_ci            level_flags = 1;
1013275793eaSopenharmony_ci        else if (s->level == 6)
1014275793eaSopenharmony_ci            level_flags = 2;
1015275793eaSopenharmony_ci        else
1016275793eaSopenharmony_ci            level_flags = 3;
1017275793eaSopenharmony_ci        header |= (level_flags << 6);
1018275793eaSopenharmony_ci        if (s->strstart != 0) header |= PRESET_DICT;
1019275793eaSopenharmony_ci        header += 31 - (header % 31);
1020275793eaSopenharmony_ci
1021275793eaSopenharmony_ci        putShortMSB(s, header);
1022275793eaSopenharmony_ci
1023275793eaSopenharmony_ci        /* Save the adler32 of the preset dictionary: */
1024275793eaSopenharmony_ci        if (s->strstart != 0) {
1025275793eaSopenharmony_ci            putShortMSB(s, (uInt)(strm->adler >> 16));
1026275793eaSopenharmony_ci            putShortMSB(s, (uInt)(strm->adler & 0xffff));
1027275793eaSopenharmony_ci        }
1028275793eaSopenharmony_ci        strm->adler = adler32(0L, Z_NULL, 0);
1029275793eaSopenharmony_ci        s->status = BUSY_STATE;
1030275793eaSopenharmony_ci
1031275793eaSopenharmony_ci        /* Compression must start with an empty pending buffer */
1032275793eaSopenharmony_ci        flush_pending(strm);
1033275793eaSopenharmony_ci        if (s->pending != 0) {
1034275793eaSopenharmony_ci            s->last_flush = -1;
1035275793eaSopenharmony_ci            return Z_OK;
1036275793eaSopenharmony_ci        }
1037275793eaSopenharmony_ci    }
1038275793eaSopenharmony_ci#ifdef GZIP
1039275793eaSopenharmony_ci    if (s->status == GZIP_STATE) {
1040275793eaSopenharmony_ci        /* gzip header */
1041275793eaSopenharmony_ci        strm->adler = crc32(0L, Z_NULL, 0);
1042275793eaSopenharmony_ci        put_byte(s, 31);
1043275793eaSopenharmony_ci        put_byte(s, 139);
1044275793eaSopenharmony_ci        put_byte(s, 8);
1045275793eaSopenharmony_ci        if (s->gzhead == Z_NULL) {
1046275793eaSopenharmony_ci            put_byte(s, 0);
1047275793eaSopenharmony_ci            put_byte(s, 0);
1048275793eaSopenharmony_ci            put_byte(s, 0);
1049275793eaSopenharmony_ci            put_byte(s, 0);
1050275793eaSopenharmony_ci            put_byte(s, 0);
1051275793eaSopenharmony_ci            put_byte(s, s->level == 9 ? 2 :
1052275793eaSopenharmony_ci                     (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
1053275793eaSopenharmony_ci                      4 : 0));
1054275793eaSopenharmony_ci            put_byte(s, OS_CODE);
1055275793eaSopenharmony_ci            s->status = BUSY_STATE;
1056275793eaSopenharmony_ci
1057275793eaSopenharmony_ci            /* Compression must start with an empty pending buffer */
1058275793eaSopenharmony_ci            flush_pending(strm);
1059275793eaSopenharmony_ci            if (s->pending != 0) {
1060275793eaSopenharmony_ci                s->last_flush = -1;
1061275793eaSopenharmony_ci                return Z_OK;
1062275793eaSopenharmony_ci            }
1063275793eaSopenharmony_ci        }
1064275793eaSopenharmony_ci        else {
1065275793eaSopenharmony_ci            put_byte(s, (s->gzhead->text ? 1 : 0) +
1066275793eaSopenharmony_ci                     (s->gzhead->hcrc ? 2 : 0) +
1067275793eaSopenharmony_ci                     (s->gzhead->extra == Z_NULL ? 0 : 4) +
1068275793eaSopenharmony_ci                     (s->gzhead->name == Z_NULL ? 0 : 8) +
1069275793eaSopenharmony_ci                     (s->gzhead->comment == Z_NULL ? 0 : 16)
1070275793eaSopenharmony_ci                     );
1071275793eaSopenharmony_ci            put_byte(s, (Byte)(s->gzhead->time & 0xff));
1072275793eaSopenharmony_ci            put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
1073275793eaSopenharmony_ci            put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
1074275793eaSopenharmony_ci            put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
1075275793eaSopenharmony_ci            put_byte(s, s->level == 9 ? 2 :
1076275793eaSopenharmony_ci                     (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
1077275793eaSopenharmony_ci                      4 : 0));
1078275793eaSopenharmony_ci            put_byte(s, s->gzhead->os & 0xff);
1079275793eaSopenharmony_ci            if (s->gzhead->extra != Z_NULL) {
1080275793eaSopenharmony_ci                put_byte(s, s->gzhead->extra_len & 0xff);
1081275793eaSopenharmony_ci                put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
1082275793eaSopenharmony_ci            }
1083275793eaSopenharmony_ci            if (s->gzhead->hcrc)
1084275793eaSopenharmony_ci                strm->adler = crc32(strm->adler, s->pending_buf,
1085275793eaSopenharmony_ci                                    s->pending);
1086275793eaSopenharmony_ci            s->gzindex = 0;
1087275793eaSopenharmony_ci            s->status = EXTRA_STATE;
1088275793eaSopenharmony_ci        }
1089275793eaSopenharmony_ci    }
1090275793eaSopenharmony_ci    if (s->status == EXTRA_STATE) {
1091275793eaSopenharmony_ci        if (s->gzhead->extra != Z_NULL) {
1092275793eaSopenharmony_ci            ulg beg = s->pending;   /* start of bytes to update crc */
1093275793eaSopenharmony_ci            uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
1094275793eaSopenharmony_ci            while (s->pending + left > s->pending_buf_size) {
1095275793eaSopenharmony_ci                uInt copy = s->pending_buf_size - s->pending;
1096275793eaSopenharmony_ci                zmemcpy(s->pending_buf + s->pending,
1097275793eaSopenharmony_ci                        s->gzhead->extra + s->gzindex, copy);
1098275793eaSopenharmony_ci                s->pending = s->pending_buf_size;
1099275793eaSopenharmony_ci                HCRC_UPDATE(beg);
1100275793eaSopenharmony_ci                s->gzindex += copy;
1101275793eaSopenharmony_ci                flush_pending(strm);
1102275793eaSopenharmony_ci                if (s->pending != 0) {
1103275793eaSopenharmony_ci                    s->last_flush = -1;
1104275793eaSopenharmony_ci                    return Z_OK;
1105275793eaSopenharmony_ci                }
1106275793eaSopenharmony_ci                beg = 0;
1107275793eaSopenharmony_ci                left -= copy;
1108275793eaSopenharmony_ci            }
1109275793eaSopenharmony_ci            zmemcpy(s->pending_buf + s->pending,
1110275793eaSopenharmony_ci                    s->gzhead->extra + s->gzindex, left);
1111275793eaSopenharmony_ci            s->pending += left;
1112275793eaSopenharmony_ci            HCRC_UPDATE(beg);
1113275793eaSopenharmony_ci            s->gzindex = 0;
1114275793eaSopenharmony_ci        }
1115275793eaSopenharmony_ci        s->status = NAME_STATE;
1116275793eaSopenharmony_ci    }
1117275793eaSopenharmony_ci    if (s->status == NAME_STATE) {
1118275793eaSopenharmony_ci        if (s->gzhead->name != Z_NULL) {
1119275793eaSopenharmony_ci            ulg beg = s->pending;   /* start of bytes to update crc */
1120275793eaSopenharmony_ci            int val;
1121275793eaSopenharmony_ci            do {
1122275793eaSopenharmony_ci                if (s->pending == s->pending_buf_size) {
1123275793eaSopenharmony_ci                    HCRC_UPDATE(beg);
1124275793eaSopenharmony_ci                    flush_pending(strm);
1125275793eaSopenharmony_ci                    if (s->pending != 0) {
1126275793eaSopenharmony_ci                        s->last_flush = -1;
1127275793eaSopenharmony_ci                        return Z_OK;
1128275793eaSopenharmony_ci                    }
1129275793eaSopenharmony_ci                    beg = 0;
1130275793eaSopenharmony_ci                }
1131275793eaSopenharmony_ci                val = s->gzhead->name[s->gzindex++];
1132275793eaSopenharmony_ci                put_byte(s, val);
1133275793eaSopenharmony_ci            } while (val != 0);
1134275793eaSopenharmony_ci            HCRC_UPDATE(beg);
1135275793eaSopenharmony_ci            s->gzindex = 0;
1136275793eaSopenharmony_ci        }
1137275793eaSopenharmony_ci        s->status = COMMENT_STATE;
1138275793eaSopenharmony_ci    }
1139275793eaSopenharmony_ci    if (s->status == COMMENT_STATE) {
1140275793eaSopenharmony_ci        if (s->gzhead->comment != Z_NULL) {
1141275793eaSopenharmony_ci            ulg beg = s->pending;   /* start of bytes to update crc */
1142275793eaSopenharmony_ci            int val;
1143275793eaSopenharmony_ci            do {
1144275793eaSopenharmony_ci                if (s->pending == s->pending_buf_size) {
1145275793eaSopenharmony_ci                    HCRC_UPDATE(beg);
1146275793eaSopenharmony_ci                    flush_pending(strm);
1147275793eaSopenharmony_ci                    if (s->pending != 0) {
1148275793eaSopenharmony_ci                        s->last_flush = -1;
1149275793eaSopenharmony_ci                        return Z_OK;
1150275793eaSopenharmony_ci                    }
1151275793eaSopenharmony_ci                    beg = 0;
1152275793eaSopenharmony_ci                }
1153275793eaSopenharmony_ci                val = s->gzhead->comment[s->gzindex++];
1154275793eaSopenharmony_ci                put_byte(s, val);
1155275793eaSopenharmony_ci            } while (val != 0);
1156275793eaSopenharmony_ci            HCRC_UPDATE(beg);
1157275793eaSopenharmony_ci        }
1158275793eaSopenharmony_ci        s->status = HCRC_STATE;
1159275793eaSopenharmony_ci    }
1160275793eaSopenharmony_ci    if (s->status == HCRC_STATE) {
1161275793eaSopenharmony_ci        if (s->gzhead->hcrc) {
1162275793eaSopenharmony_ci            if (s->pending + 2 > s->pending_buf_size) {
1163275793eaSopenharmony_ci                flush_pending(strm);
1164275793eaSopenharmony_ci                if (s->pending != 0) {
1165275793eaSopenharmony_ci                    s->last_flush = -1;
1166275793eaSopenharmony_ci                    return Z_OK;
1167275793eaSopenharmony_ci                }
1168275793eaSopenharmony_ci            }
1169275793eaSopenharmony_ci            put_byte(s, (Byte)(strm->adler & 0xff));
1170275793eaSopenharmony_ci            put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1171275793eaSopenharmony_ci            strm->adler = crc32(0L, Z_NULL, 0);
1172275793eaSopenharmony_ci        }
1173275793eaSopenharmony_ci        s->status = BUSY_STATE;
1174275793eaSopenharmony_ci
1175275793eaSopenharmony_ci        /* Compression must start with an empty pending buffer */
1176275793eaSopenharmony_ci        flush_pending(strm);
1177275793eaSopenharmony_ci        if (s->pending != 0) {
1178275793eaSopenharmony_ci            s->last_flush = -1;
1179275793eaSopenharmony_ci            return Z_OK;
1180275793eaSopenharmony_ci        }
1181275793eaSopenharmony_ci    }
1182275793eaSopenharmony_ci#endif
1183275793eaSopenharmony_ci
1184275793eaSopenharmony_ci    /* Start a new block or continue the current one.
1185275793eaSopenharmony_ci     */
1186275793eaSopenharmony_ci    if (strm->avail_in != 0 || s->lookahead != 0 ||
1187275793eaSopenharmony_ci        (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
1188275793eaSopenharmony_ci        block_state bstate;
1189275793eaSopenharmony_ci
1190275793eaSopenharmony_ci        bstate = s->level == 0 ? deflate_stored(s, flush) :
1191275793eaSopenharmony_ci                 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
1192275793eaSopenharmony_ci                 s->strategy == Z_RLE ? deflate_rle(s, flush) :
1193275793eaSopenharmony_ci                 (*(configuration_table[s->level].func))(s, flush);
1194275793eaSopenharmony_ci
1195275793eaSopenharmony_ci        if (bstate == finish_started || bstate == finish_done) {
1196275793eaSopenharmony_ci            s->status = FINISH_STATE;
1197275793eaSopenharmony_ci        }
1198275793eaSopenharmony_ci        if (bstate == need_more || bstate == finish_started) {
1199275793eaSopenharmony_ci            if (strm->avail_out == 0) {
1200275793eaSopenharmony_ci                s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
1201275793eaSopenharmony_ci            }
1202275793eaSopenharmony_ci            return Z_OK;
1203275793eaSopenharmony_ci            /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1204275793eaSopenharmony_ci             * of deflate should use the same flush parameter to make sure
1205275793eaSopenharmony_ci             * that the flush is complete. So we don't have to output an
1206275793eaSopenharmony_ci             * empty block here, this will be done at next call. This also
1207275793eaSopenharmony_ci             * ensures that for a very small output buffer, we emit at most
1208275793eaSopenharmony_ci             * one empty block.
1209275793eaSopenharmony_ci             */
1210275793eaSopenharmony_ci        }
1211275793eaSopenharmony_ci        if (bstate == block_done) {
1212275793eaSopenharmony_ci            if (flush == Z_PARTIAL_FLUSH) {
1213275793eaSopenharmony_ci                _tr_align(s);
1214275793eaSopenharmony_ci            } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
1215275793eaSopenharmony_ci                _tr_stored_block(s, (char*)0, 0L, 0);
1216275793eaSopenharmony_ci                /* For a full flush, this empty block will be recognized
1217275793eaSopenharmony_ci                 * as a special marker by inflate_sync().
1218275793eaSopenharmony_ci                 */
1219275793eaSopenharmony_ci                if (flush == Z_FULL_FLUSH) {
1220275793eaSopenharmony_ci                    CLEAR_HASH(s);             /* forget history */
1221275793eaSopenharmony_ci                    if (s->lookahead == 0) {
1222275793eaSopenharmony_ci                        s->strstart = 0;
1223275793eaSopenharmony_ci                        s->block_start = 0L;
1224275793eaSopenharmony_ci                        s->insert = 0;
1225275793eaSopenharmony_ci                    }
1226275793eaSopenharmony_ci                }
1227275793eaSopenharmony_ci            }
1228275793eaSopenharmony_ci            flush_pending(strm);
1229275793eaSopenharmony_ci            if (strm->avail_out == 0) {
1230275793eaSopenharmony_ci              s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1231275793eaSopenharmony_ci              return Z_OK;
1232275793eaSopenharmony_ci            }
1233275793eaSopenharmony_ci        }
1234275793eaSopenharmony_ci    }
1235275793eaSopenharmony_ci
1236275793eaSopenharmony_ci    if (flush != Z_FINISH) return Z_OK;
1237275793eaSopenharmony_ci    if (s->wrap <= 0) return Z_STREAM_END;
1238275793eaSopenharmony_ci
1239275793eaSopenharmony_ci    /* Write the trailer */
1240275793eaSopenharmony_ci#ifdef GZIP
1241275793eaSopenharmony_ci    if (s->wrap == 2) {
1242275793eaSopenharmony_ci        put_byte(s, (Byte)(strm->adler & 0xff));
1243275793eaSopenharmony_ci        put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1244275793eaSopenharmony_ci        put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
1245275793eaSopenharmony_ci        put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
1246275793eaSopenharmony_ci        put_byte(s, (Byte)(strm->total_in & 0xff));
1247275793eaSopenharmony_ci        put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
1248275793eaSopenharmony_ci        put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
1249275793eaSopenharmony_ci        put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
1250275793eaSopenharmony_ci    }
1251275793eaSopenharmony_ci    else
1252275793eaSopenharmony_ci#endif
1253275793eaSopenharmony_ci    {
1254275793eaSopenharmony_ci        putShortMSB(s, (uInt)(strm->adler >> 16));
1255275793eaSopenharmony_ci        putShortMSB(s, (uInt)(strm->adler & 0xffff));
1256275793eaSopenharmony_ci    }
1257275793eaSopenharmony_ci    flush_pending(strm);
1258275793eaSopenharmony_ci    /* If avail_out is zero, the application will call deflate again
1259275793eaSopenharmony_ci     * to flush the rest.
1260275793eaSopenharmony_ci     */
1261275793eaSopenharmony_ci    if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
1262275793eaSopenharmony_ci    return s->pending != 0 ? Z_OK : Z_STREAM_END;
1263275793eaSopenharmony_ci}
1264275793eaSopenharmony_ci
1265275793eaSopenharmony_ci/* ========================================================================= */
1266275793eaSopenharmony_ciint ZEXPORT deflateEnd(z_streamp strm) {
1267275793eaSopenharmony_ci    int status;
1268275793eaSopenharmony_ci
1269275793eaSopenharmony_ci    if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
1270275793eaSopenharmony_ci
1271275793eaSopenharmony_ci    status = strm->state->status;
1272275793eaSopenharmony_ci
1273275793eaSopenharmony_ci    /* Deallocate in reverse order of allocations: */
1274275793eaSopenharmony_ci    TRY_FREE(strm, strm->state->pending_buf);
1275275793eaSopenharmony_ci    TRY_FREE(strm, strm->state->head);
1276275793eaSopenharmony_ci    TRY_FREE(strm, strm->state->prev);
1277275793eaSopenharmony_ci    TRY_FREE(strm, strm->state->window);
1278275793eaSopenharmony_ci
1279275793eaSopenharmony_ci    ZFREE(strm, strm->state);
1280275793eaSopenharmony_ci    strm->state = Z_NULL;
1281275793eaSopenharmony_ci
1282275793eaSopenharmony_ci    return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1283275793eaSopenharmony_ci}
1284275793eaSopenharmony_ci
1285275793eaSopenharmony_ci/* =========================================================================
1286275793eaSopenharmony_ci * Copy the source state to the destination state.
1287275793eaSopenharmony_ci * To simplify the source, this is not supported for 16-bit MSDOS (which
1288275793eaSopenharmony_ci * doesn't have enough memory anyway to duplicate compression states).
1289275793eaSopenharmony_ci */
1290275793eaSopenharmony_ciint ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1291275793eaSopenharmony_ci#ifdef MAXSEG_64K
1292275793eaSopenharmony_ci    (void)dest;
1293275793eaSopenharmony_ci    (void)source;
1294275793eaSopenharmony_ci    return Z_STREAM_ERROR;
1295275793eaSopenharmony_ci#else
1296275793eaSopenharmony_ci    deflate_state *ds;
1297275793eaSopenharmony_ci    deflate_state *ss;
1298275793eaSopenharmony_ci
1299275793eaSopenharmony_ci
1300275793eaSopenharmony_ci    if (deflateStateCheck(source) || dest == Z_NULL) {
1301275793eaSopenharmony_ci        return Z_STREAM_ERROR;
1302275793eaSopenharmony_ci    }
1303275793eaSopenharmony_ci
1304275793eaSopenharmony_ci    ss = source->state;
1305275793eaSopenharmony_ci
1306275793eaSopenharmony_ci    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1307275793eaSopenharmony_ci
1308275793eaSopenharmony_ci    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1309275793eaSopenharmony_ci    if (ds == Z_NULL) return Z_MEM_ERROR;
1310275793eaSopenharmony_ci    dest->state = (struct internal_state FAR *) ds;
1311275793eaSopenharmony_ci    zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
1312275793eaSopenharmony_ci    ds->strm = dest;
1313275793eaSopenharmony_ci
1314275793eaSopenharmony_ci    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1315275793eaSopenharmony_ci    ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
1316275793eaSopenharmony_ci    ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
1317275793eaSopenharmony_ci    ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS);
1318275793eaSopenharmony_ci
1319275793eaSopenharmony_ci    if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1320275793eaSopenharmony_ci        ds->pending_buf == Z_NULL) {
1321275793eaSopenharmony_ci        deflateEnd (dest);
1322275793eaSopenharmony_ci        return Z_MEM_ERROR;
1323275793eaSopenharmony_ci    }
1324275793eaSopenharmony_ci    /* following zmemcpy do not work for 16-bit MSDOS */
1325275793eaSopenharmony_ci    zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1326275793eaSopenharmony_ci    zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
1327275793eaSopenharmony_ci    zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
1328275793eaSopenharmony_ci    zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS);
1329275793eaSopenharmony_ci
1330275793eaSopenharmony_ci    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1331275793eaSopenharmony_ci#ifdef LIT_MEM
1332275793eaSopenharmony_ci    ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1333275793eaSopenharmony_ci    ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1334275793eaSopenharmony_ci#else
1335275793eaSopenharmony_ci    ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1336275793eaSopenharmony_ci#endif
1337275793eaSopenharmony_ci
1338275793eaSopenharmony_ci    ds->l_desc.dyn_tree = ds->dyn_ltree;
1339275793eaSopenharmony_ci    ds->d_desc.dyn_tree = ds->dyn_dtree;
1340275793eaSopenharmony_ci    ds->bl_desc.dyn_tree = ds->bl_tree;
1341275793eaSopenharmony_ci
1342275793eaSopenharmony_ci    return Z_OK;
1343275793eaSopenharmony_ci#endif /* MAXSEG_64K */
1344275793eaSopenharmony_ci}
1345275793eaSopenharmony_ci
1346275793eaSopenharmony_ci#ifndef FASTEST
1347275793eaSopenharmony_ci/* ===========================================================================
1348275793eaSopenharmony_ci * Set match_start to the longest match starting at the given string and
1349275793eaSopenharmony_ci * return its length. Matches shorter or equal to prev_length are discarded,
1350275793eaSopenharmony_ci * in which case the result is equal to prev_length and match_start is
1351275793eaSopenharmony_ci * garbage.
1352275793eaSopenharmony_ci * IN assertions: cur_match is the head of the hash chain for the current
1353275793eaSopenharmony_ci *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1354275793eaSopenharmony_ci * OUT assertion: the match length is not greater than s->lookahead.
1355275793eaSopenharmony_ci */
1356275793eaSopenharmony_cilocal uInt longest_match(deflate_state *s, IPos cur_match)
1357275793eaSopenharmony_ci{
1358275793eaSopenharmony_ci    unsigned chain_length = s->max_chain_length;/* max hash chain length */
1359275793eaSopenharmony_ci    register Bytef *scan = s->window + s->strstart; /* current string */
1360275793eaSopenharmony_ci    register Bytef *match;                      /* matched string */
1361275793eaSopenharmony_ci    register int len;                           /* length of current match */
1362275793eaSopenharmony_ci    int best_len = (int)s->prev_length;         /* best match length so far */
1363275793eaSopenharmony_ci    int nice_match = s->nice_match;             /* stop if match long enough */
1364275793eaSopenharmony_ci    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1365275793eaSopenharmony_ci        s->strstart - (IPos)MAX_DIST(s) : NIL;
1366275793eaSopenharmony_ci    /* Stop when cur_match becomes <= limit. To simplify the code,
1367275793eaSopenharmony_ci     * we prevent matches with the string of window index 0.
1368275793eaSopenharmony_ci     */
1369275793eaSopenharmony_ci    Posf *prev = s->prev;
1370275793eaSopenharmony_ci    uInt wmask = s->w_mask;
1371275793eaSopenharmony_ci
1372275793eaSopenharmony_ci#ifdef UNALIGNED_OK
1373275793eaSopenharmony_ci    /* Compare two bytes at a time. Note: this is not always beneficial.
1374275793eaSopenharmony_ci     * Try with and without -DUNALIGNED_OK to check.
1375275793eaSopenharmony_ci     */
1376275793eaSopenharmony_ci    register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1377275793eaSopenharmony_ci    register ush scan_start = *(ushf*)scan;
1378275793eaSopenharmony_ci    register ush scan_end   = *(ushf*)(scan + best_len - 1);
1379275793eaSopenharmony_ci#else
1380275793eaSopenharmony_ci    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1381275793eaSopenharmony_ci    register Byte scan_end1  = scan[best_len - 1];
1382275793eaSopenharmony_ci    register Byte scan_end   = scan[best_len];
1383275793eaSopenharmony_ci#endif
1384275793eaSopenharmony_ci
1385275793eaSopenharmony_ci    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1386275793eaSopenharmony_ci     * It is easy to get rid of this optimization if necessary.
1387275793eaSopenharmony_ci     */
1388275793eaSopenharmony_ci    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1389275793eaSopenharmony_ci
1390275793eaSopenharmony_ci    /* Do not waste too much time if we already have a good match: */
1391275793eaSopenharmony_ci    if (s->prev_length >= s->good_match) {
1392275793eaSopenharmony_ci        chain_length >>= 2;
1393275793eaSopenharmony_ci    }
1394275793eaSopenharmony_ci    /* Do not look for matches beyond the end of the input. This is necessary
1395275793eaSopenharmony_ci     * to make deflate deterministic.
1396275793eaSopenharmony_ci     */
1397275793eaSopenharmony_ci    if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
1398275793eaSopenharmony_ci
1399275793eaSopenharmony_ci    Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1400275793eaSopenharmony_ci           "need lookahead");
1401275793eaSopenharmony_ci
1402275793eaSopenharmony_ci    do {
1403275793eaSopenharmony_ci        Assert(cur_match < s->strstart, "no future");
1404275793eaSopenharmony_ci        match = s->window + cur_match;
1405275793eaSopenharmony_ci
1406275793eaSopenharmony_ci        /* Skip to next match if the match length cannot increase
1407275793eaSopenharmony_ci         * or if the match length is less than 2.  Note that the checks below
1408275793eaSopenharmony_ci         * for insufficient lookahead only occur occasionally for performance
1409275793eaSopenharmony_ci         * reasons.  Therefore uninitialized memory will be accessed, and
1410275793eaSopenharmony_ci         * conditional jumps will be made that depend on those values.
1411275793eaSopenharmony_ci         * However the length of the match is limited to the lookahead, so
1412275793eaSopenharmony_ci         * the output of deflate is not affected by the uninitialized values.
1413275793eaSopenharmony_ci         */
1414275793eaSopenharmony_ci#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1415275793eaSopenharmony_ci        /* This code assumes sizeof(unsigned short) == 2. Do not use
1416275793eaSopenharmony_ci         * UNALIGNED_OK if your compiler uses a different size.
1417275793eaSopenharmony_ci         */
1418275793eaSopenharmony_ci        if (*(ushf*)(match + best_len - 1) != scan_end ||
1419275793eaSopenharmony_ci            *(ushf*)match != scan_start) continue;
1420275793eaSopenharmony_ci
1421275793eaSopenharmony_ci        /* It is not necessary to compare scan[2] and match[2] since they are
1422275793eaSopenharmony_ci         * always equal when the other bytes match, given that the hash keys
1423275793eaSopenharmony_ci         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1424275793eaSopenharmony_ci         * strstart + 3, + 5, up to strstart + 257. We check for insufficient
1425275793eaSopenharmony_ci         * lookahead only every 4th comparison; the 128th check will be made
1426275793eaSopenharmony_ci         * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is
1427275793eaSopenharmony_ci         * necessary to put more guard bytes at the end of the window, or
1428275793eaSopenharmony_ci         * to check more often for insufficient lookahead.
1429275793eaSopenharmony_ci         */
1430275793eaSopenharmony_ci        Assert(scan[2] == match[2], "scan[2]?");
1431275793eaSopenharmony_ci        scan++, match++;
1432275793eaSopenharmony_ci        do {
1433275793eaSopenharmony_ci        } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1434275793eaSopenharmony_ci                 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1435275793eaSopenharmony_ci                 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1436275793eaSopenharmony_ci                 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1437275793eaSopenharmony_ci                 scan < strend);
1438275793eaSopenharmony_ci        /* The funny "do {}" generates better code on most compilers */
1439275793eaSopenharmony_ci
1440275793eaSopenharmony_ci        /* Here, scan <= window + strstart + 257 */
1441275793eaSopenharmony_ci        Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1442275793eaSopenharmony_ci               "wild scan");
1443275793eaSopenharmony_ci        if (*scan == *match) scan++;
1444275793eaSopenharmony_ci
1445275793eaSopenharmony_ci        len = (MAX_MATCH - 1) - (int)(strend - scan);
1446275793eaSopenharmony_ci        scan = strend - (MAX_MATCH-1);
1447275793eaSopenharmony_ci
1448275793eaSopenharmony_ci#else /* UNALIGNED_OK */
1449275793eaSopenharmony_ci
1450275793eaSopenharmony_ci        if (match[best_len]     != scan_end  ||
1451275793eaSopenharmony_ci            match[best_len - 1] != scan_end1 ||
1452275793eaSopenharmony_ci            *match              != *scan     ||
1453275793eaSopenharmony_ci            *++match            != scan[1])      continue;
1454275793eaSopenharmony_ci
1455275793eaSopenharmony_ci        /* The check at best_len - 1 can be removed because it will be made
1456275793eaSopenharmony_ci         * again later. (This heuristic is not always a win.)
1457275793eaSopenharmony_ci         * It is not necessary to compare scan[2] and match[2] since they
1458275793eaSopenharmony_ci         * are always equal when the other bytes match, given that
1459275793eaSopenharmony_ci         * the hash keys are equal and that HASH_BITS >= 8.
1460275793eaSopenharmony_ci         */
1461275793eaSopenharmony_ci        scan += 2, match++;
1462275793eaSopenharmony_ci        Assert(*scan == *match, "match[2]?");
1463275793eaSopenharmony_ci
1464275793eaSopenharmony_ci        /* We check for insufficient lookahead only every 8th comparison;
1465275793eaSopenharmony_ci         * the 256th check will be made at strstart + 258.
1466275793eaSopenharmony_ci         */
1467275793eaSopenharmony_ci        do {
1468275793eaSopenharmony_ci        } while (*++scan == *++match && *++scan == *++match &&
1469275793eaSopenharmony_ci                 *++scan == *++match && *++scan == *++match &&
1470275793eaSopenharmony_ci                 *++scan == *++match && *++scan == *++match &&
1471275793eaSopenharmony_ci                 *++scan == *++match && *++scan == *++match &&
1472275793eaSopenharmony_ci                 scan < strend);
1473275793eaSopenharmony_ci
1474275793eaSopenharmony_ci        Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1475275793eaSopenharmony_ci               "wild scan");
1476275793eaSopenharmony_ci
1477275793eaSopenharmony_ci        len = MAX_MATCH - (int)(strend - scan);
1478275793eaSopenharmony_ci        scan = strend - MAX_MATCH;
1479275793eaSopenharmony_ci
1480275793eaSopenharmony_ci#endif /* UNALIGNED_OK */
1481275793eaSopenharmony_ci
1482275793eaSopenharmony_ci        if (len > best_len) {
1483275793eaSopenharmony_ci            s->match_start = cur_match;
1484275793eaSopenharmony_ci            best_len = len;
1485275793eaSopenharmony_ci            if (len >= nice_match) break;
1486275793eaSopenharmony_ci#ifdef UNALIGNED_OK
1487275793eaSopenharmony_ci            scan_end = *(ushf*)(scan + best_len - 1);
1488275793eaSopenharmony_ci#else
1489275793eaSopenharmony_ci            scan_end1  = scan[best_len - 1];
1490275793eaSopenharmony_ci            scan_end   = scan[best_len];
1491275793eaSopenharmony_ci#endif
1492275793eaSopenharmony_ci        }
1493275793eaSopenharmony_ci    } while ((cur_match = prev[cur_match & wmask]) > limit
1494275793eaSopenharmony_ci             && --chain_length != 0);
1495275793eaSopenharmony_ci
1496275793eaSopenharmony_ci    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1497275793eaSopenharmony_ci    return s->lookahead;
1498275793eaSopenharmony_ci}
1499275793eaSopenharmony_ci
1500275793eaSopenharmony_ci#else /* FASTEST */
1501275793eaSopenharmony_ci
1502275793eaSopenharmony_ci/* ---------------------------------------------------------------------------
1503275793eaSopenharmony_ci * Optimized version for FASTEST only
1504275793eaSopenharmony_ci */
1505275793eaSopenharmony_cilocal uInt longest_match(deflate_state *s, IPos cur_match)
1506275793eaSopenharmony_ci{
1507275793eaSopenharmony_ci    register Bytef *scan = s->window + s->strstart; /* current string */
1508275793eaSopenharmony_ci    register Bytef *match;                       /* matched string */
1509275793eaSopenharmony_ci    register int len;                           /* length of current match */
1510275793eaSopenharmony_ci    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1511275793eaSopenharmony_ci
1512275793eaSopenharmony_ci    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1513275793eaSopenharmony_ci     * It is easy to get rid of this optimization if necessary.
1514275793eaSopenharmony_ci     */
1515275793eaSopenharmony_ci    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1516275793eaSopenharmony_ci
1517275793eaSopenharmony_ci    Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1518275793eaSopenharmony_ci           "need lookahead");
1519275793eaSopenharmony_ci
1520275793eaSopenharmony_ci    Assert(cur_match < s->strstart, "no future");
1521275793eaSopenharmony_ci
1522275793eaSopenharmony_ci    match = s->window + cur_match;
1523275793eaSopenharmony_ci
1524275793eaSopenharmony_ci    /* Return failure if the match length is less than 2:
1525275793eaSopenharmony_ci     */
1526275793eaSopenharmony_ci    if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1527275793eaSopenharmony_ci
1528275793eaSopenharmony_ci    /* The check at best_len - 1 can be removed because it will be made
1529275793eaSopenharmony_ci     * again later. (This heuristic is not always a win.)
1530275793eaSopenharmony_ci     * It is not necessary to compare scan[2] and match[2] since they
1531275793eaSopenharmony_ci     * are always equal when the other bytes match, given that
1532275793eaSopenharmony_ci     * the hash keys are equal and that HASH_BITS >= 8.
1533275793eaSopenharmony_ci     */
1534275793eaSopenharmony_ci    scan += 2, match += 2;
1535275793eaSopenharmony_ci    Assert(*scan == *match, "match[2]?");
1536275793eaSopenharmony_ci
1537275793eaSopenharmony_ci    /* We check for insufficient lookahead only every 8th comparison;
1538275793eaSopenharmony_ci     * the 256th check will be made at strstart + 258.
1539275793eaSopenharmony_ci     */
1540275793eaSopenharmony_ci    do {
1541275793eaSopenharmony_ci    } while (*++scan == *++match && *++scan == *++match &&
1542275793eaSopenharmony_ci             *++scan == *++match && *++scan == *++match &&
1543275793eaSopenharmony_ci             *++scan == *++match && *++scan == *++match &&
1544275793eaSopenharmony_ci             *++scan == *++match && *++scan == *++match &&
1545275793eaSopenharmony_ci             scan < strend);
1546275793eaSopenharmony_ci
1547275793eaSopenharmony_ci    Assert(scan <= s->window + (unsigned)(s->window_size - 1), "wild scan");
1548275793eaSopenharmony_ci
1549275793eaSopenharmony_ci    len = MAX_MATCH - (int)(strend - scan);
1550275793eaSopenharmony_ci
1551275793eaSopenharmony_ci    if (len < MIN_MATCH) return MIN_MATCH - 1;
1552275793eaSopenharmony_ci
1553275793eaSopenharmony_ci    s->match_start = cur_match;
1554275793eaSopenharmony_ci    return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1555275793eaSopenharmony_ci}
1556275793eaSopenharmony_ci
1557275793eaSopenharmony_ci#endif /* FASTEST */
1558275793eaSopenharmony_ci
1559275793eaSopenharmony_ci#ifdef ZLIB_DEBUG
1560275793eaSopenharmony_ci
1561275793eaSopenharmony_ci#define EQUAL 0
1562275793eaSopenharmony_ci/* result of memcmp for equal strings */
1563275793eaSopenharmony_ci
1564275793eaSopenharmony_ci/* ===========================================================================
1565275793eaSopenharmony_ci * Check that the match at match_start is indeed a match.
1566275793eaSopenharmony_ci */
1567275793eaSopenharmony_cilocal void check_match(deflate_state *s, IPos start, IPos match, int length)
1568275793eaSopenharmony_ci{
1569275793eaSopenharmony_ci    /* check that the match is indeed a match */
1570275793eaSopenharmony_ci    Bytef *back = s->window + (int)match, *here = s->window + start;
1571275793eaSopenharmony_ci    IPos len = length;
1572275793eaSopenharmony_ci    if (match == (IPos)-1) {
1573275793eaSopenharmony_ci        /* match starts one byte before the current window -- just compare the
1574275793eaSopenharmony_ci           subsequent length-1 bytes */
1575275793eaSopenharmony_ci        back++;
1576275793eaSopenharmony_ci        here++;
1577275793eaSopenharmony_ci        len--;
1578275793eaSopenharmony_ci    }
1579275793eaSopenharmony_ci    if (zmemcmp(back, here, len) != EQUAL) {
1580275793eaSopenharmony_ci        fprintf(stderr, " start %u, match %d, length %d\n",
1581275793eaSopenharmony_ci                start, (int)match, length);
1582275793eaSopenharmony_ci        do {
1583275793eaSopenharmony_ci            fprintf(stderr, "(%02x %02x)", *back++, *here++);
1584275793eaSopenharmony_ci        } while (--len != 0);
1585275793eaSopenharmony_ci        z_error("invalid match");
1586275793eaSopenharmony_ci    }
1587275793eaSopenharmony_ci    if (z_verbose > 1) {
1588275793eaSopenharmony_ci        fprintf(stderr,"\\[%d,%d]", start - match, length);
1589275793eaSopenharmony_ci        do { putc(s->window[start++], stderr); } while (--length != 0);
1590275793eaSopenharmony_ci    }
1591275793eaSopenharmony_ci}
1592275793eaSopenharmony_ci#else
1593275793eaSopenharmony_ci#  define check_match(s, start, match, length)
1594275793eaSopenharmony_ci#endif /* ZLIB_DEBUG */
1595275793eaSopenharmony_ci
1596275793eaSopenharmony_ci/* ===========================================================================
1597275793eaSopenharmony_ci * Flush the current block, with given end-of-file flag.
1598275793eaSopenharmony_ci * IN assertion: strstart is set to the end of the current match.
1599275793eaSopenharmony_ci */
1600275793eaSopenharmony_ci#define FLUSH_BLOCK_ONLY(s, last) { \
1601275793eaSopenharmony_ci   _tr_flush_block(s, (s->block_start >= 0L ? \
1602275793eaSopenharmony_ci                   (charf *)&s->window[(unsigned)s->block_start] : \
1603275793eaSopenharmony_ci                   (charf *)Z_NULL), \
1604275793eaSopenharmony_ci                (ulg)((long)s->strstart - s->block_start), \
1605275793eaSopenharmony_ci                (last)); \
1606275793eaSopenharmony_ci   s->block_start = s->strstart; \
1607275793eaSopenharmony_ci   flush_pending(s->strm); \
1608275793eaSopenharmony_ci   Tracev((stderr,"[FLUSH]")); \
1609275793eaSopenharmony_ci}
1610275793eaSopenharmony_ci
1611275793eaSopenharmony_ci/* Same but force premature exit if necessary. */
1612275793eaSopenharmony_ci#define FLUSH_BLOCK(s, last) { \
1613275793eaSopenharmony_ci   FLUSH_BLOCK_ONLY(s, last); \
1614275793eaSopenharmony_ci   if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1615275793eaSopenharmony_ci}
1616275793eaSopenharmony_ci
1617275793eaSopenharmony_ci/* Maximum stored block length in deflate format (not including header). */
1618275793eaSopenharmony_ci#define MAX_STORED 65535
1619275793eaSopenharmony_ci
1620275793eaSopenharmony_ci/* Minimum of a and b. */
1621275793eaSopenharmony_ci#define MIN(a, b) ((a) > (b) ? (b) : (a))
1622275793eaSopenharmony_ci
1623275793eaSopenharmony_ci/* ===========================================================================
1624275793eaSopenharmony_ci * Copy without compression as much as possible from the input stream, return
1625275793eaSopenharmony_ci * the current block state.
1626275793eaSopenharmony_ci *
1627275793eaSopenharmony_ci * In case deflateParams() is used to later switch to a non-zero compression
1628275793eaSopenharmony_ci * level, s->matches (otherwise unused when storing) keeps track of the number
1629275793eaSopenharmony_ci * of hash table slides to perform. If s->matches is 1, then one hash table
1630275793eaSopenharmony_ci * slide will be done when switching. If s->matches is 2, the maximum value
1631275793eaSopenharmony_ci * allowed here, then the hash table will be cleared, since two or more slides
1632275793eaSopenharmony_ci * is the same as a clear.
1633275793eaSopenharmony_ci *
1634275793eaSopenharmony_ci * deflate_stored() is written to minimize the number of times an input byte is
1635275793eaSopenharmony_ci * copied. It is most efficient with large input and output buffers, which
1636275793eaSopenharmony_ci * maximizes the opportunities to have a single copy from next_in to next_out.
1637275793eaSopenharmony_ci */
1638275793eaSopenharmony_cilocal block_state deflate_stored(deflate_state *s, int flush)
1639275793eaSopenharmony_ci{
1640275793eaSopenharmony_ci    /* Smallest worthy block size when not flushing or finishing. By default
1641275793eaSopenharmony_ci     * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1642275793eaSopenharmony_ci     * large input and output buffers, the stored block size will be larger.
1643275793eaSopenharmony_ci     */
1644275793eaSopenharmony_ci    unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
1645275793eaSopenharmony_ci
1646275793eaSopenharmony_ci    /* Copy as many min_block or larger stored blocks directly to next_out as
1647275793eaSopenharmony_ci     * possible. If flushing, copy the remaining available input to next_out as
1648275793eaSopenharmony_ci     * stored blocks, if there is enough space.
1649275793eaSopenharmony_ci     */
1650275793eaSopenharmony_ci    unsigned len, left, have, last = 0;
1651275793eaSopenharmony_ci    unsigned used = s->strm->avail_in;
1652275793eaSopenharmony_ci    do {
1653275793eaSopenharmony_ci        /* Set len to the maximum size block that we can copy directly with the
1654275793eaSopenharmony_ci         * available input data and output space. Set left to how much of that
1655275793eaSopenharmony_ci         * would be copied from what's left in the window.
1656275793eaSopenharmony_ci         */
1657275793eaSopenharmony_ci        len = MAX_STORED;       /* maximum deflate stored block length */
1658275793eaSopenharmony_ci        have = (s->bi_valid + 42) >> 3;         /* number of header bytes */
1659275793eaSopenharmony_ci        if (s->strm->avail_out < have)          /* need room for header */
1660275793eaSopenharmony_ci            break;
1661275793eaSopenharmony_ci            /* maximum stored block length that will fit in avail_out: */
1662275793eaSopenharmony_ci        have = s->strm->avail_out - have;
1663275793eaSopenharmony_ci        left = s->strstart - s->block_start;    /* bytes left in window */
1664275793eaSopenharmony_ci        if (len > (ulg)left + s->strm->avail_in)
1665275793eaSopenharmony_ci            len = left + s->strm->avail_in;     /* limit len to the input */
1666275793eaSopenharmony_ci        if (len > have)
1667275793eaSopenharmony_ci            len = have;                         /* limit len to the output */
1668275793eaSopenharmony_ci
1669275793eaSopenharmony_ci        /* If the stored block would be less than min_block in length, or if
1670275793eaSopenharmony_ci         * unable to copy all of the available input when flushing, then try
1671275793eaSopenharmony_ci         * copying to the window and the pending buffer instead. Also don't
1672275793eaSopenharmony_ci         * write an empty block when flushing -- deflate() does that.
1673275793eaSopenharmony_ci         */
1674275793eaSopenharmony_ci        if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
1675275793eaSopenharmony_ci                                flush == Z_NO_FLUSH ||
1676275793eaSopenharmony_ci                                len != left + s->strm->avail_in))
1677275793eaSopenharmony_ci            break;
1678275793eaSopenharmony_ci
1679275793eaSopenharmony_ci        /* Make a dummy stored block in pending to get the header bytes,
1680275793eaSopenharmony_ci         * including any pending bits. This also updates the debugging counts.
1681275793eaSopenharmony_ci         */
1682275793eaSopenharmony_ci        last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1683275793eaSopenharmony_ci        _tr_stored_block(s, (char *)0, 0L, last);
1684275793eaSopenharmony_ci
1685275793eaSopenharmony_ci        /* Replace the lengths in the dummy stored block with len. */
1686275793eaSopenharmony_ci        s->pending_buf[s->pending - 4] = len;
1687275793eaSopenharmony_ci        s->pending_buf[s->pending - 3] = len >> 8;
1688275793eaSopenharmony_ci        s->pending_buf[s->pending - 2] = ~len;
1689275793eaSopenharmony_ci        s->pending_buf[s->pending - 1] = ~len >> 8;
1690275793eaSopenharmony_ci
1691275793eaSopenharmony_ci        /* Write the stored block header bytes. */
1692275793eaSopenharmony_ci        flush_pending(s->strm);
1693275793eaSopenharmony_ci
1694275793eaSopenharmony_ci#ifdef ZLIB_DEBUG
1695275793eaSopenharmony_ci        /* Update debugging counts for the data about to be copied. */
1696275793eaSopenharmony_ci        s->compressed_len += len << 3;
1697275793eaSopenharmony_ci        s->bits_sent += len << 3;
1698275793eaSopenharmony_ci#endif
1699275793eaSopenharmony_ci
1700275793eaSopenharmony_ci        /* Copy uncompressed bytes from the window to next_out. */
1701275793eaSopenharmony_ci        if (left) {
1702275793eaSopenharmony_ci            if (left > len)
1703275793eaSopenharmony_ci                left = len;
1704275793eaSopenharmony_ci            zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1705275793eaSopenharmony_ci            s->strm->next_out += left;
1706275793eaSopenharmony_ci            s->strm->avail_out -= left;
1707275793eaSopenharmony_ci            s->strm->total_out += left;
1708275793eaSopenharmony_ci            s->block_start += left;
1709275793eaSopenharmony_ci            len -= left;
1710275793eaSopenharmony_ci        }
1711275793eaSopenharmony_ci
1712275793eaSopenharmony_ci        /* Copy uncompressed bytes directly from next_in to next_out, updating
1713275793eaSopenharmony_ci         * the check value.
1714275793eaSopenharmony_ci         */
1715275793eaSopenharmony_ci        if (len) {
1716275793eaSopenharmony_ci            read_buf(s->strm, s->strm->next_out, len);
1717275793eaSopenharmony_ci            s->strm->next_out += len;
1718275793eaSopenharmony_ci            s->strm->avail_out -= len;
1719275793eaSopenharmony_ci            s->strm->total_out += len;
1720275793eaSopenharmony_ci        }
1721275793eaSopenharmony_ci    } while (last == 0);
1722275793eaSopenharmony_ci
1723275793eaSopenharmony_ci    /* Update the sliding window with the last s->w_size bytes of the copied
1724275793eaSopenharmony_ci     * data, or append all of the copied data to the existing window if less
1725275793eaSopenharmony_ci     * than s->w_size bytes were copied. Also update the number of bytes to
1726275793eaSopenharmony_ci     * insert in the hash tables, in the event that deflateParams() switches to
1727275793eaSopenharmony_ci     * a non-zero compression level.
1728275793eaSopenharmony_ci     */
1729275793eaSopenharmony_ci    used -= s->strm->avail_in;      /* number of input bytes directly copied */
1730275793eaSopenharmony_ci    if (used) {
1731275793eaSopenharmony_ci        /* If any input was used, then no unused input remains in the window,
1732275793eaSopenharmony_ci         * therefore s->block_start == s->strstart.
1733275793eaSopenharmony_ci         */
1734275793eaSopenharmony_ci        if (used >= s->w_size) {    /* supplant the previous history */
1735275793eaSopenharmony_ci            s->matches = 2;         /* clear hash */
1736275793eaSopenharmony_ci            zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1737275793eaSopenharmony_ci            s->strstart = s->w_size;
1738275793eaSopenharmony_ci            s->insert = s->strstart;
1739275793eaSopenharmony_ci        }
1740275793eaSopenharmony_ci        else {
1741275793eaSopenharmony_ci            if (s->window_size - s->strstart <= used) {
1742275793eaSopenharmony_ci                /* Slide the window down. */
1743275793eaSopenharmony_ci                s->strstart -= s->w_size;
1744275793eaSopenharmony_ci                zmemcpy(s->window, s->window + s->w_size, s->strstart);
1745275793eaSopenharmony_ci                if (s->matches < 2)
1746275793eaSopenharmony_ci                    s->matches++;   /* add a pending slide_hash() */
1747275793eaSopenharmony_ci                if (s->insert > s->strstart)
1748275793eaSopenharmony_ci                    s->insert = s->strstart;
1749275793eaSopenharmony_ci            }
1750275793eaSopenharmony_ci            zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1751275793eaSopenharmony_ci            s->strstart += used;
1752275793eaSopenharmony_ci            s->insert += MIN(used, s->w_size - s->insert);
1753275793eaSopenharmony_ci        }
1754275793eaSopenharmony_ci        s->block_start = s->strstart;
1755275793eaSopenharmony_ci    }
1756275793eaSopenharmony_ci    if (s->high_water < s->strstart)
1757275793eaSopenharmony_ci        s->high_water = s->strstart;
1758275793eaSopenharmony_ci
1759275793eaSopenharmony_ci    /* If the last block was written to next_out, then done. */
1760275793eaSopenharmony_ci    if (last)
1761275793eaSopenharmony_ci        return finish_done;
1762275793eaSopenharmony_ci
1763275793eaSopenharmony_ci    /* If flushing and all input has been consumed, then done. */
1764275793eaSopenharmony_ci    if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
1765275793eaSopenharmony_ci        s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
1766275793eaSopenharmony_ci        return block_done;
1767275793eaSopenharmony_ci
1768275793eaSopenharmony_ci    /* Fill the window with any remaining input. */
1769275793eaSopenharmony_ci    have = s->window_size - s->strstart;
1770275793eaSopenharmony_ci    if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
1771275793eaSopenharmony_ci        /* Slide the window down. */
1772275793eaSopenharmony_ci        s->block_start -= s->w_size;
1773275793eaSopenharmony_ci        s->strstart -= s->w_size;
1774275793eaSopenharmony_ci        zmemcpy(s->window, s->window + s->w_size, s->strstart);
1775275793eaSopenharmony_ci        if (s->matches < 2)
1776275793eaSopenharmony_ci            s->matches++;           /* add a pending slide_hash() */
1777275793eaSopenharmony_ci        have += s->w_size;          /* more space now */
1778275793eaSopenharmony_ci        if (s->insert > s->strstart)
1779275793eaSopenharmony_ci            s->insert = s->strstart;
1780275793eaSopenharmony_ci    }
1781275793eaSopenharmony_ci    if (have > s->strm->avail_in)
1782275793eaSopenharmony_ci        have = s->strm->avail_in;
1783275793eaSopenharmony_ci    if (have) {
1784275793eaSopenharmony_ci        read_buf(s->strm, s->window + s->strstart, have);
1785275793eaSopenharmony_ci        s->strstart += have;
1786275793eaSopenharmony_ci        s->insert += MIN(have, s->w_size - s->insert);
1787275793eaSopenharmony_ci    }
1788275793eaSopenharmony_ci    if (s->high_water < s->strstart)
1789275793eaSopenharmony_ci        s->high_water = s->strstart;
1790275793eaSopenharmony_ci
1791275793eaSopenharmony_ci    /* There was not enough avail_out to write a complete worthy or flushed
1792275793eaSopenharmony_ci     * stored block to next_out. Write a stored block to pending instead, if we
1793275793eaSopenharmony_ci     * have enough input for a worthy block, or if flushing and there is enough
1794275793eaSopenharmony_ci     * room for the remaining input as a stored block in the pending buffer.
1795275793eaSopenharmony_ci     */
1796275793eaSopenharmony_ci    have = (s->bi_valid + 42) >> 3;         /* number of header bytes */
1797275793eaSopenharmony_ci        /* maximum stored block length that will fit in pending: */
1798275793eaSopenharmony_ci    have = MIN(s->pending_buf_size - have, MAX_STORED);
1799275793eaSopenharmony_ci    min_block = MIN(have, s->w_size);
1800275793eaSopenharmony_ci    left = s->strstart - s->block_start;
1801275793eaSopenharmony_ci    if (left >= min_block ||
1802275793eaSopenharmony_ci        ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
1803275793eaSopenharmony_ci         s->strm->avail_in == 0 && left <= have)) {
1804275793eaSopenharmony_ci        len = MIN(left, have);
1805275793eaSopenharmony_ci        last = flush == Z_FINISH && s->strm->avail_in == 0 &&
1806275793eaSopenharmony_ci               len == left ? 1 : 0;
1807275793eaSopenharmony_ci        _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
1808275793eaSopenharmony_ci        s->block_start += len;
1809275793eaSopenharmony_ci        flush_pending(s->strm);
1810275793eaSopenharmony_ci    }
1811275793eaSopenharmony_ci
1812275793eaSopenharmony_ci    /* We've done all we can with the available input and output. */
1813275793eaSopenharmony_ci    return last ? finish_started : need_more;
1814275793eaSopenharmony_ci}
1815275793eaSopenharmony_ci
1816275793eaSopenharmony_ci/* ===========================================================================
1817275793eaSopenharmony_ci * Compress as much as possible from the input stream, return the current
1818275793eaSopenharmony_ci * block state.
1819275793eaSopenharmony_ci * This function does not perform lazy evaluation of matches and inserts
1820275793eaSopenharmony_ci * new strings in the dictionary only for unmatched strings or for short
1821275793eaSopenharmony_ci * matches. It is used only for the fast compression options.
1822275793eaSopenharmony_ci */
1823275793eaSopenharmony_cilocal block_state deflate_fast(deflate_state *s, int flush)
1824275793eaSopenharmony_ci{
1825275793eaSopenharmony_ci    IPos hash_head;       /* head of the hash chain */
1826275793eaSopenharmony_ci    int bflush;           /* set if current block must be flushed */
1827275793eaSopenharmony_ci
1828275793eaSopenharmony_ci    for (;;) {
1829275793eaSopenharmony_ci        /* Make sure that we always have enough lookahead, except
1830275793eaSopenharmony_ci         * at the end of the input file. We need MAX_MATCH bytes
1831275793eaSopenharmony_ci         * for the next match, plus MIN_MATCH bytes to insert the
1832275793eaSopenharmony_ci         * string following the next match.
1833275793eaSopenharmony_ci         */
1834275793eaSopenharmony_ci        if (s->lookahead < MIN_LOOKAHEAD) {
1835275793eaSopenharmony_ci            fill_window(s);
1836275793eaSopenharmony_ci            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1837275793eaSopenharmony_ci                return need_more;
1838275793eaSopenharmony_ci            }
1839275793eaSopenharmony_ci            if (s->lookahead == 0) break; /* flush the current block */
1840275793eaSopenharmony_ci        }
1841275793eaSopenharmony_ci
1842275793eaSopenharmony_ci        /* Insert the string window[strstart .. strstart + 2] in the
1843275793eaSopenharmony_ci         * dictionary, and set hash_head to the head of the hash chain:
1844275793eaSopenharmony_ci         */
1845275793eaSopenharmony_ci        hash_head = NIL;
1846275793eaSopenharmony_ci        if (s->lookahead >= MIN_MATCH) {
1847275793eaSopenharmony_ci            INSERT_STRING(s, s->strstart, hash_head);
1848275793eaSopenharmony_ci        }
1849275793eaSopenharmony_ci
1850275793eaSopenharmony_ci        /* Find the longest match, discarding those <= prev_length.
1851275793eaSopenharmony_ci         * At this point we have always match_length < MIN_MATCH
1852275793eaSopenharmony_ci         */
1853275793eaSopenharmony_ci        if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1854275793eaSopenharmony_ci            /* To simplify the code, we prevent matches with the string
1855275793eaSopenharmony_ci             * of window index 0 (in particular we have to avoid a match
1856275793eaSopenharmony_ci             * of the string with itself at the start of the input file).
1857275793eaSopenharmony_ci             */
1858275793eaSopenharmony_ci            s->match_length = longest_match (s, hash_head);
1859275793eaSopenharmony_ci            /* longest_match() sets match_start */
1860275793eaSopenharmony_ci        }
1861275793eaSopenharmony_ci        if (s->match_length >= MIN_MATCH) {
1862275793eaSopenharmony_ci            check_match(s, s->strstart, s->match_start, s->match_length);
1863275793eaSopenharmony_ci
1864275793eaSopenharmony_ci            _tr_tally_dist(s, s->strstart - s->match_start,
1865275793eaSopenharmony_ci                           s->match_length - MIN_MATCH, bflush);
1866275793eaSopenharmony_ci
1867275793eaSopenharmony_ci            s->lookahead -= s->match_length;
1868275793eaSopenharmony_ci
1869275793eaSopenharmony_ci            /* Insert new strings in the hash table only if the match length
1870275793eaSopenharmony_ci             * is not too large. This saves time but degrades compression.
1871275793eaSopenharmony_ci             */
1872275793eaSopenharmony_ci#ifndef FASTEST
1873275793eaSopenharmony_ci            if (s->match_length <= s->max_insert_length &&
1874275793eaSopenharmony_ci                s->lookahead >= MIN_MATCH) {
1875275793eaSopenharmony_ci                s->match_length--; /* string at strstart already in table */
1876275793eaSopenharmony_ci                do {
1877275793eaSopenharmony_ci                    s->strstart++;
1878275793eaSopenharmony_ci                    INSERT_STRING(s, s->strstart, hash_head);
1879275793eaSopenharmony_ci                    /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1880275793eaSopenharmony_ci                     * always MIN_MATCH bytes ahead.
1881275793eaSopenharmony_ci                     */
1882275793eaSopenharmony_ci                } while (--s->match_length != 0);
1883275793eaSopenharmony_ci                s->strstart++;
1884275793eaSopenharmony_ci            } else
1885275793eaSopenharmony_ci#endif
1886275793eaSopenharmony_ci            {
1887275793eaSopenharmony_ci                s->strstart += s->match_length;
1888275793eaSopenharmony_ci                s->match_length = 0;
1889275793eaSopenharmony_ci                s->ins_h = s->window[s->strstart];
1890275793eaSopenharmony_ci                UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]);
1891275793eaSopenharmony_ci#if MIN_MATCH != 3
1892275793eaSopenharmony_ci                Call UPDATE_HASH() MIN_MATCH-3 more times
1893275793eaSopenharmony_ci#endif
1894275793eaSopenharmony_ci                /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1895275793eaSopenharmony_ci                 * matter since it will be recomputed at next deflate call.
1896275793eaSopenharmony_ci                 */
1897275793eaSopenharmony_ci            }
1898275793eaSopenharmony_ci        } else {
1899275793eaSopenharmony_ci            /* No match, output a literal byte */
1900275793eaSopenharmony_ci            Tracevv((stderr,"%c", s->window[s->strstart]));
1901275793eaSopenharmony_ci            _tr_tally_lit(s, s->window[s->strstart], bflush);
1902275793eaSopenharmony_ci            s->lookahead--;
1903275793eaSopenharmony_ci            s->strstart++;
1904275793eaSopenharmony_ci        }
1905275793eaSopenharmony_ci        if (bflush) FLUSH_BLOCK(s, 0);
1906275793eaSopenharmony_ci    }
1907275793eaSopenharmony_ci    s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1908275793eaSopenharmony_ci    if (flush == Z_FINISH) {
1909275793eaSopenharmony_ci        FLUSH_BLOCK(s, 1);
1910275793eaSopenharmony_ci        return finish_done;
1911275793eaSopenharmony_ci    }
1912275793eaSopenharmony_ci    if (s->sym_next)
1913275793eaSopenharmony_ci        FLUSH_BLOCK(s, 0);
1914275793eaSopenharmony_ci    return block_done;
1915275793eaSopenharmony_ci}
1916275793eaSopenharmony_ci
1917275793eaSopenharmony_ci#ifndef FASTEST
1918275793eaSopenharmony_ci/* ===========================================================================
1919275793eaSopenharmony_ci * Same as above, but achieves better compression. We use a lazy
1920275793eaSopenharmony_ci * evaluation for matches: a match is finally adopted only if there is
1921275793eaSopenharmony_ci * no better match at the next window position.
1922275793eaSopenharmony_ci */
1923275793eaSopenharmony_cilocal block_state deflate_slow(deflate_state *s, int flush)
1924275793eaSopenharmony_ci{
1925275793eaSopenharmony_ci    IPos hash_head;          /* head of hash chain */
1926275793eaSopenharmony_ci    int bflush;              /* set if current block must be flushed */
1927275793eaSopenharmony_ci
1928275793eaSopenharmony_ci    /* Process the input block. */
1929275793eaSopenharmony_ci    for (;;) {
1930275793eaSopenharmony_ci        /* Make sure that we always have enough lookahead, except
1931275793eaSopenharmony_ci         * at the end of the input file. We need MAX_MATCH bytes
1932275793eaSopenharmony_ci         * for the next match, plus MIN_MATCH bytes to insert the
1933275793eaSopenharmony_ci         * string following the next match.
1934275793eaSopenharmony_ci         */
1935275793eaSopenharmony_ci        if (s->lookahead < MIN_LOOKAHEAD) {
1936275793eaSopenharmony_ci            fill_window(s);
1937275793eaSopenharmony_ci            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1938275793eaSopenharmony_ci                return need_more;
1939275793eaSopenharmony_ci            }
1940275793eaSopenharmony_ci            if (s->lookahead == 0) break; /* flush the current block */
1941275793eaSopenharmony_ci        }
1942275793eaSopenharmony_ci
1943275793eaSopenharmony_ci        /* Insert the string window[strstart .. strstart + 2] in the
1944275793eaSopenharmony_ci         * dictionary, and set hash_head to the head of the hash chain:
1945275793eaSopenharmony_ci         */
1946275793eaSopenharmony_ci        hash_head = NIL;
1947275793eaSopenharmony_ci        if (s->lookahead >= MIN_MATCH) {
1948275793eaSopenharmony_ci            INSERT_STRING(s, s->strstart, hash_head);
1949275793eaSopenharmony_ci        }
1950275793eaSopenharmony_ci
1951275793eaSopenharmony_ci        /* Find the longest match, discarding those <= prev_length.
1952275793eaSopenharmony_ci         */
1953275793eaSopenharmony_ci        s->prev_length = s->match_length, s->prev_match = s->match_start;
1954275793eaSopenharmony_ci        s->match_length = MIN_MATCH-1;
1955275793eaSopenharmony_ci
1956275793eaSopenharmony_ci        if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1957275793eaSopenharmony_ci            s->strstart - hash_head <= MAX_DIST(s)) {
1958275793eaSopenharmony_ci            /* To simplify the code, we prevent matches with the string
1959275793eaSopenharmony_ci             * of window index 0 (in particular we have to avoid a match
1960275793eaSopenharmony_ci             * of the string with itself at the start of the input file).
1961275793eaSopenharmony_ci             */
1962275793eaSopenharmony_ci            s->match_length = longest_match (s, hash_head);
1963275793eaSopenharmony_ci            /* longest_match() sets match_start */
1964275793eaSopenharmony_ci
1965275793eaSopenharmony_ci            if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1966275793eaSopenharmony_ci#if TOO_FAR <= 32767
1967275793eaSopenharmony_ci                || (s->match_length == MIN_MATCH &&
1968275793eaSopenharmony_ci                    s->strstart - s->match_start > TOO_FAR)
1969275793eaSopenharmony_ci#endif
1970275793eaSopenharmony_ci                )) {
1971275793eaSopenharmony_ci
1972275793eaSopenharmony_ci                /* If prev_match is also MIN_MATCH, match_start is garbage
1973275793eaSopenharmony_ci                 * but we will ignore the current match anyway.
1974275793eaSopenharmony_ci                 */
1975275793eaSopenharmony_ci                s->match_length = MIN_MATCH-1;
1976275793eaSopenharmony_ci            }
1977275793eaSopenharmony_ci        }
1978275793eaSopenharmony_ci        /* If there was a match at the previous step and the current
1979275793eaSopenharmony_ci         * match is not better, output the previous match:
1980275793eaSopenharmony_ci         */
1981275793eaSopenharmony_ci        if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1982275793eaSopenharmony_ci            uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1983275793eaSopenharmony_ci            /* Do not insert strings in hash table beyond this. */
1984275793eaSopenharmony_ci
1985275793eaSopenharmony_ci            check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
1986275793eaSopenharmony_ci
1987275793eaSopenharmony_ci            _tr_tally_dist(s, s->strstart - 1 - s->prev_match,
1988275793eaSopenharmony_ci                           s->prev_length - MIN_MATCH, bflush);
1989275793eaSopenharmony_ci
1990275793eaSopenharmony_ci            /* Insert in hash table all strings up to the end of the match.
1991275793eaSopenharmony_ci             * strstart - 1 and strstart are already inserted. If there is not
1992275793eaSopenharmony_ci             * enough lookahead, the last two strings are not inserted in
1993275793eaSopenharmony_ci             * the hash table.
1994275793eaSopenharmony_ci             */
1995275793eaSopenharmony_ci            s->lookahead -= s->prev_length - 1;
1996275793eaSopenharmony_ci            s->prev_length -= 2;
1997275793eaSopenharmony_ci            do {
1998275793eaSopenharmony_ci                if (++s->strstart <= max_insert) {
1999275793eaSopenharmony_ci                    INSERT_STRING(s, s->strstart, hash_head);
2000275793eaSopenharmony_ci                }
2001275793eaSopenharmony_ci            } while (--s->prev_length != 0);
2002275793eaSopenharmony_ci            s->match_available = 0;
2003275793eaSopenharmony_ci            s->match_length = MIN_MATCH-1;
2004275793eaSopenharmony_ci            s->strstart++;
2005275793eaSopenharmony_ci
2006275793eaSopenharmony_ci            if (bflush) FLUSH_BLOCK(s, 0);
2007275793eaSopenharmony_ci
2008275793eaSopenharmony_ci        } else if (s->match_available) {
2009275793eaSopenharmony_ci            /* If there was no match at the previous position, output a
2010275793eaSopenharmony_ci             * single literal. If there was a match but the current match
2011275793eaSopenharmony_ci             * is longer, truncate the previous match to a single literal.
2012275793eaSopenharmony_ci             */
2013275793eaSopenharmony_ci            Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2014275793eaSopenharmony_ci            _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2015275793eaSopenharmony_ci            if (bflush) {
2016275793eaSopenharmony_ci                FLUSH_BLOCK_ONLY(s, 0);
2017275793eaSopenharmony_ci            }
2018275793eaSopenharmony_ci            s->strstart++;
2019275793eaSopenharmony_ci            s->lookahead--;
2020275793eaSopenharmony_ci            if (s->strm->avail_out == 0) return need_more;
2021275793eaSopenharmony_ci        } else {
2022275793eaSopenharmony_ci            /* There is no previous match to compare with, wait for
2023275793eaSopenharmony_ci             * the next step to decide.
2024275793eaSopenharmony_ci             */
2025275793eaSopenharmony_ci            s->match_available = 1;
2026275793eaSopenharmony_ci            s->strstart++;
2027275793eaSopenharmony_ci            s->lookahead--;
2028275793eaSopenharmony_ci        }
2029275793eaSopenharmony_ci    }
2030275793eaSopenharmony_ci    Assert (flush != Z_NO_FLUSH, "no flush?");
2031275793eaSopenharmony_ci    if (s->match_available) {
2032275793eaSopenharmony_ci        Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2033275793eaSopenharmony_ci        _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2034275793eaSopenharmony_ci        s->match_available = 0;
2035275793eaSopenharmony_ci    }
2036275793eaSopenharmony_ci    s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
2037275793eaSopenharmony_ci    if (flush == Z_FINISH) {
2038275793eaSopenharmony_ci        FLUSH_BLOCK(s, 1);
2039275793eaSopenharmony_ci        return finish_done;
2040275793eaSopenharmony_ci    }
2041275793eaSopenharmony_ci    if (s->sym_next)
2042275793eaSopenharmony_ci        FLUSH_BLOCK(s, 0);
2043275793eaSopenharmony_ci    return block_done;
2044275793eaSopenharmony_ci}
2045275793eaSopenharmony_ci#endif /* FASTEST */
2046275793eaSopenharmony_ci
2047275793eaSopenharmony_ci/* ===========================================================================
2048275793eaSopenharmony_ci * For Z_RLE, simply look for runs of bytes, generate matches only of distance
2049275793eaSopenharmony_ci * one.  Do not maintain a hash table.  (It will be regenerated if this run of
2050275793eaSopenharmony_ci * deflate switches away from Z_RLE.)
2051275793eaSopenharmony_ci */
2052275793eaSopenharmony_cilocal block_state deflate_rle(deflate_state *s, int flush)
2053275793eaSopenharmony_ci{
2054275793eaSopenharmony_ci    int bflush;             /* set if current block must be flushed */
2055275793eaSopenharmony_ci    uInt prev;              /* byte at distance one to match */
2056275793eaSopenharmony_ci    Bytef *scan, *strend;   /* scan goes up to strend for length of run */
2057275793eaSopenharmony_ci
2058275793eaSopenharmony_ci    for (;;) {
2059275793eaSopenharmony_ci        /* Make sure that we always have enough lookahead, except
2060275793eaSopenharmony_ci         * at the end of the input file. We need MAX_MATCH bytes
2061275793eaSopenharmony_ci         * for the longest run, plus one for the unrolled loop.
2062275793eaSopenharmony_ci         */
2063275793eaSopenharmony_ci        if (s->lookahead <= MAX_MATCH) {
2064275793eaSopenharmony_ci            fill_window(s);
2065275793eaSopenharmony_ci            if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
2066275793eaSopenharmony_ci                return need_more;
2067275793eaSopenharmony_ci            }
2068275793eaSopenharmony_ci            if (s->lookahead == 0) break; /* flush the current block */
2069275793eaSopenharmony_ci        }
2070275793eaSopenharmony_ci
2071275793eaSopenharmony_ci        /* See how many times the previous byte repeats */
2072275793eaSopenharmony_ci        s->match_length = 0;
2073275793eaSopenharmony_ci        if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
2074275793eaSopenharmony_ci            scan = s->window + s->strstart - 1;
2075275793eaSopenharmony_ci            prev = *scan;
2076275793eaSopenharmony_ci            if (prev == *++scan && prev == *++scan && prev == *++scan) {
2077275793eaSopenharmony_ci                strend = s->window + s->strstart + MAX_MATCH;
2078275793eaSopenharmony_ci                do {
2079275793eaSopenharmony_ci                } while (prev == *++scan && prev == *++scan &&
2080275793eaSopenharmony_ci                         prev == *++scan && prev == *++scan &&
2081275793eaSopenharmony_ci                         prev == *++scan && prev == *++scan &&
2082275793eaSopenharmony_ci                         prev == *++scan && prev == *++scan &&
2083275793eaSopenharmony_ci                         scan < strend);
2084275793eaSopenharmony_ci                s->match_length = MAX_MATCH - (uInt)(strend - scan);
2085275793eaSopenharmony_ci                if (s->match_length > s->lookahead)
2086275793eaSopenharmony_ci                    s->match_length = s->lookahead;
2087275793eaSopenharmony_ci            }
2088275793eaSopenharmony_ci            Assert(scan <= s->window + (uInt)(s->window_size - 1),
2089275793eaSopenharmony_ci                   "wild scan");
2090275793eaSopenharmony_ci        }
2091275793eaSopenharmony_ci
2092275793eaSopenharmony_ci        /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2093275793eaSopenharmony_ci        if (s->match_length >= MIN_MATCH) {
2094275793eaSopenharmony_ci            check_match(s, s->strstart, s->strstart - 1, s->match_length);
2095275793eaSopenharmony_ci
2096275793eaSopenharmony_ci            _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
2097275793eaSopenharmony_ci
2098275793eaSopenharmony_ci            s->lookahead -= s->match_length;
2099275793eaSopenharmony_ci            s->strstart += s->match_length;
2100275793eaSopenharmony_ci            s->match_length = 0;
2101275793eaSopenharmony_ci        } else {
2102275793eaSopenharmony_ci            /* No match, output a literal byte */
2103275793eaSopenharmony_ci            Tracevv((stderr,"%c", s->window[s->strstart]));
2104275793eaSopenharmony_ci            _tr_tally_lit(s, s->window[s->strstart], bflush);
2105275793eaSopenharmony_ci            s->lookahead--;
2106275793eaSopenharmony_ci            s->strstart++;
2107275793eaSopenharmony_ci        }
2108275793eaSopenharmony_ci        if (bflush) FLUSH_BLOCK(s, 0);
2109275793eaSopenharmony_ci    }
2110275793eaSopenharmony_ci    s->insert = 0;
2111275793eaSopenharmony_ci    if (flush == Z_FINISH) {
2112275793eaSopenharmony_ci        FLUSH_BLOCK(s, 1);
2113275793eaSopenharmony_ci        return finish_done;
2114275793eaSopenharmony_ci    }
2115275793eaSopenharmony_ci    if (s->sym_next)
2116275793eaSopenharmony_ci        FLUSH_BLOCK(s, 0);
2117275793eaSopenharmony_ci    return block_done;
2118275793eaSopenharmony_ci}
2119275793eaSopenharmony_ci
2120275793eaSopenharmony_ci/* ===========================================================================
2121275793eaSopenharmony_ci * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
2122275793eaSopenharmony_ci * (It will be regenerated if this run of deflate switches away from Huffman.)
2123275793eaSopenharmony_ci */
2124275793eaSopenharmony_cilocal block_state deflate_huff(deflate_state *s, int flush)
2125275793eaSopenharmony_ci{
2126275793eaSopenharmony_ci    int bflush;             /* set if current block must be flushed */
2127275793eaSopenharmony_ci
2128275793eaSopenharmony_ci    for (;;) {
2129275793eaSopenharmony_ci        /* Make sure that we have a literal to write. */
2130275793eaSopenharmony_ci        if (s->lookahead == 0) {
2131275793eaSopenharmony_ci            fill_window(s);
2132275793eaSopenharmony_ci            if (s->lookahead == 0) {
2133275793eaSopenharmony_ci                if (flush == Z_NO_FLUSH)
2134275793eaSopenharmony_ci                    return need_more;
2135275793eaSopenharmony_ci                break;      /* flush the current block */
2136275793eaSopenharmony_ci            }
2137275793eaSopenharmony_ci        }
2138275793eaSopenharmony_ci
2139275793eaSopenharmony_ci        /* Output a literal byte */
2140275793eaSopenharmony_ci        s->match_length = 0;
2141275793eaSopenharmony_ci        Tracevv((stderr,"%c", s->window[s->strstart]));
2142275793eaSopenharmony_ci        _tr_tally_lit(s, s->window[s->strstart], bflush);
2143275793eaSopenharmony_ci        s->lookahead--;
2144275793eaSopenharmony_ci        s->strstart++;
2145275793eaSopenharmony_ci        if (bflush) FLUSH_BLOCK(s, 0);
2146275793eaSopenharmony_ci    }
2147275793eaSopenharmony_ci    s->insert = 0;
2148275793eaSopenharmony_ci    if (flush == Z_FINISH) {
2149275793eaSopenharmony_ci        FLUSH_BLOCK(s, 1);
2150275793eaSopenharmony_ci        return finish_done;
2151275793eaSopenharmony_ci    }
2152275793eaSopenharmony_ci    if (s->sym_next)
2153275793eaSopenharmony_ci        FLUSH_BLOCK(s, 0);
2154275793eaSopenharmony_ci    return block_done;
2155275793eaSopenharmony_ci}
2156