• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zlib-ng / zlib-ng / 30026106047

23 Jul 2026 04:40PM UTC coverage: 93.069%. First build
30026106047

Pull #2374

github

web-flow
Merge 9febd616a into a1cd2a7b2
Pull Request #2374: Add deflateUsed() function to get the used bits in the last byte.

16881 of 18599 branches covered (90.76%)

Branch coverage included in aggregate %.

8 of 12 new or added lines in 4 files covered. (66.67%)

13036 of 13546 relevant lines covered (96.24%)

388421110.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.62
/deflate.c
1
/* deflate.c -- compress data using the deflation algorithm
2
 * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5

6
/*
7
 *  ALGORITHM
8
 *
9
 *      The "deflation" process depends on being able to identify portions
10
 *      of the input text which are identical to earlier input (within a
11
 *      sliding window trailing behind the input currently being processed).
12
 *
13
 *      The most straightforward technique turns out to be the fastest for
14
 *      most input files: try all possible matches and select the longest.
15
 *      The key feature of this algorithm is that insertions into the string
16
 *      dictionary are very simple and thus fast, and deletions are avoided
17
 *      completely. Insertions are performed at each input character, whereas
18
 *      string matches are performed only when the previous match ends. So it
19
 *      is preferable to spend more time in matches to allow very fast string
20
 *      insertions and avoid deletions. The matching algorithm for small
21
 *      strings is inspired from that of Rabin & Karp. A brute force approach
22
 *      is used to find longer strings when a small match has been found.
23
 *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24
 *      (by Leonid Broukhis).
25
 *         A previous version of this file used a more sophisticated algorithm
26
 *      (by Fiala and Greene) which is guaranteed to run in linear amortized
27
 *      time, but has a larger average cost, uses more memory and is patented.
28
 *      However the F&G algorithm may be faster for some highly redundant
29
 *      files if the parameter max_chain_length (described below) is too large.
30
 *
31
 *  ACKNOWLEDGEMENTS
32
 *
33
 *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34
 *      I found it in 'freeze' written by Leonid Broukhis.
35
 *      Thanks to many people for bug reports and testing.
36
 *
37
 *  REFERENCES
38
 *
39
 *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40
 *      Available in https://tools.ietf.org/html/rfc1951
41
 *
42
 *      A description of the Rabin and Karp algorithm is given in the book
43
 *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44
 *
45
 *      Fiala,E.R., and Greene,D.H.
46
 *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47
 *
48
 */
49

50
#include "zbuild.h"
51
#include "functable.h"
52
#include "deflate.h"
53
#include "deflate_p.h"
54
#include "insert_string_p.h"
55
#include "arch_functions.h"
56

57
/* Avoid conflicts with zlib.h macros */
58
#ifdef ZLIB_COMPAT
59
# undef deflateInit
60
# undef deflateInit2
61
#endif
62

63
const char PREFIX(deflate_copyright)[] = " deflate 1.3.1 Copyright 1995-2024 Jean-loup Gailly and Mark Adler ";
64
/*
65
  If you use the zlib library in a product, an acknowledgment is welcome
66
  in the documentation of your product. If for some reason you cannot
67
  include such an acknowledgment, I would appreciate that you keep this
68
  copyright string in the executable of your product.
69
 */
70

71
/* ===========================================================================
72
 *  Function prototypes.
73
 */
74
static int deflateHeaders(deflate_state *s, PREFIX3(stream) *strm);
75
static int deflateStateCheck      (PREFIX3(stream) *strm);
76
Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush);
77
Z_INTERNAL block_state deflate_fast  (deflate_state *s, int flush);
78
Z_INTERNAL block_state deflate_quick (deflate_state *s, int flush);
79
#ifndef NO_MEDIUM_STRATEGY
80
Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush);
81
#endif
82
Z_INTERNAL block_state deflate_slow  (deflate_state *s, int flush);
83
Z_INTERNAL block_state deflate_rle   (deflate_state *s, int flush);
84
Z_INTERNAL block_state deflate_huff  (deflate_state *s, int flush);
85
static void lm_set_level         (deflate_state *s, int level);
86
static void lm_init              (deflate_state *s);
87

88
/* ===========================================================================
89
 * Local data
90
 */
91

92
/* Values for max_lazy_match, good_match and max_chain_length, depending on
93
 * the desired pack level (0..9). The values given below have been tuned to
94
 * exclude worst case performance for pathological files. Better values may be
95
 * found for specific files.
96
 */
97
typedef struct config_s {
98
    uint16_t good_length; /* reduce lazy search above this match length */
99
    uint16_t max_lazy;    /* do not perform lazy search above this match length */
100
    uint16_t nice_length; /* quit search above this match length */
101
    uint16_t max_chain;
102
    compress_func func;
103
} config;
104

105
static const config configuration_table[10] = {
106
/*      good lazy nice chain */
107
/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
108

109
#ifdef NO_QUICK_STRATEGY
110
/* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
111
/* 2 */ {4,    5, 16,    8, deflate_fast},
112
#else
113
/* 1 */ {0,    0,  0,    0, deflate_quick},
114
/* 2 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
115
#endif
116

117
#ifdef NO_MEDIUM_STRATEGY
118
/* 3 */ {4,    6, 32,   32, deflate_fast},
119
/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
120
/* 5 */ {8,   16, 32,   32, deflate_slow},
121
/* 6 */ {8,   16, 128, 128, deflate_slow},
122
#else
123
/* 3 */ {4,    6, 16,    6, deflate_medium},
124
/* 4 */ {4,   12, 32,   24, deflate_medium},  /* lazy matches */
125
/* 5 */ {8,   16, 32,   32, deflate_medium},
126
/* 6 */ {8,   16, 128, 128, deflate_medium},
127
#endif
128

129
/* 7 */ {8,   32, 128,  256, deflate_slow},
130
/* 8 */ {32, 128, 258, 1024, deflate_slow},
131
/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
132

133
/* Note: the deflate() code requires max_lazy >= STD_MIN_MATCH and max_chain >= 4
134
 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
135
 * meaning.
136
 */
137

138
/* Level 1 uses deflate_quick, which only reads the hash chain head, so it can
139
 * skip prev maintenance. When quick is compiled out level 1 falls back to
140
 * deflate_fast, which walks the chains like every other level. */
141
#ifdef NO_QUICK_STRATEGY
142
#  define HAVE_QUICK_STRATEGY 0
143
#else
144
#  define HAVE_QUICK_STRATEGY 1
145
#endif
146

147
/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
148
#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
149

150

151
/* ===========================================================================
152
 * Initialize the hash table. prev[] will be initialized on the fly.
153
 */
154
#define CLEAR_HASH(s) do { \
155
    memset((unsigned char *)s->head, 0, HASH_SIZE * sizeof(*s->head)); \
156
  } while (0)
157

158

159
#ifdef DEF_ALLOC_DEBUG
160
#  include <stdio.h>
161
#  define LOGSZ(name,size)           fprintf(stderr, "%s is %d bytes\n", name, size)
162
#  define LOGSZP(name,size,loc,pad)  fprintf(stderr, "%s is %d bytes, offset %d, padded %d\n", name, size, loc, pad)
163
#  define LOGSZPL(name,size,loc,pad) fprintf(stderr, "%s is %d bytes, offset %ld, padded %d\n", name, size, loc, pad)
164
#else
165
#  define LOGSZ(name,size)
166
#  define LOGSZP(name,size,loc,pad)
167
#  define LOGSZPL(name,size,loc,pad)
168
#endif
169

170
/* ===========================================================================
171
 * Allocate a big buffer and divide it up into the various buffers deflate needs.
172
 * Handles alignment of allocated buffer and alignment of individual buffers.
173
 */
174
Z_INTERNAL deflate_allocs* alloc_deflate(PREFIX3(stream) *strm, int windowBits, int lit_bufsize) {
110,116✔
175
    int curr_size = 0;
42,031✔
176

177
    /* Define sizes */
178
    int window_size = DEFLATE_ADJUST_WINDOW_SIZE((1 << windowBits) * 2);
110,116✔
179
    int prev_size = (1 << windowBits) * (int)sizeof(Pos);
42,031✔
180
    int head_size = HASH_SIZE * sizeof(Pos);
42,031✔
181
    int pending_size = (lit_bufsize * LIT_BUFS) + 1;
110,116✔
182
    int state_size = sizeof(deflate_state);
42,031✔
183
    int alloc_size = sizeof(deflate_allocs);
42,031✔
184

185
    /* Calculate relative buffer positions and paddings */
186
    LOGSZP("window", window_size, PAD_WINDOW(curr_size), PADSZ(curr_size,WINDOW_PAD_SIZE));
18,864✔
187
    int window_pos = PAD_WINDOW(curr_size);
42,031✔
188
    curr_size = window_pos + window_size;
42,031✔
189

190
    LOGSZP("prev", prev_size, PAD_64(curr_size), PADSZ(curr_size,64));
18,864✔
191
    int prev_pos = PAD_64(curr_size);
110,116✔
192
    curr_size = prev_pos + prev_size;
110,116✔
193

194
    LOGSZP("head", head_size, PAD_64(curr_size), PADSZ(curr_size,64));
18,864✔
195
    int head_pos = PAD_64(curr_size);
110,116✔
196
    curr_size = head_pos + head_size;
110,116✔
197

198
    LOGSZP("pending", pending_size, PAD_64(curr_size), PADSZ(curr_size,64));
18,864✔
199
    int pending_pos = PAD_64(curr_size);
110,116✔
200
    curr_size = pending_pos + pending_size;
110,116✔
201

202
    LOGSZP("state", state_size, PAD_64(curr_size), PADSZ(curr_size,64));
18,864✔
203
    int state_pos = PAD_64(curr_size);
110,116✔
204
    curr_size = state_pos + state_size;
110,116✔
205

206
    LOGSZP("alloc", alloc_size, PAD_16(curr_size), PADSZ(curr_size,16));
18,864✔
207
    int alloc_pos = PAD_16(curr_size);
110,116✔
208
    curr_size = alloc_pos + alloc_size;
110,116✔
209

210
    /* Add 64-1 or 4096-1 to allow window alignment, and round size of buffer up to multiple of 64 */
211
    int total_size = PAD_64(curr_size + (WINDOW_PAD_SIZE - 1));
110,116✔
212

213
    /* Allocate buffer, align to 64-byte cacheline, and zerofill the resulting buffer */
214
    char *original_buf = (char *)strm->zalloc(strm->opaque, 1, total_size);
110,116✔
215
    if (original_buf == NULL)
110,116✔
216
        return NULL;
217

218
    char *buff = (char *)HINT_ALIGNED_WINDOW((char *)PAD_WINDOW(original_buf));
110,116✔
219
    LOGSZPL("Buffer alloc", total_size, PADSZ((uintptr_t)original_buf,WINDOW_PAD_SIZE), PADSZ(curr_size,WINDOW_PAD_SIZE));
18,864✔
220

221
    /* Initialize alloc_bufs */
222
    deflate_allocs *alloc_bufs  = (struct deflate_allocs_s *)(buff + alloc_pos);
110,116✔
223
    alloc_bufs->buf_start = original_buf;
110,116✔
224
    alloc_bufs->zfree = strm->zfree;
110,116✔
225

226
    /* Assign buffers */
227
    alloc_bufs->window = (unsigned char *)HINT_ALIGNED_WINDOW(buff + window_pos);
110,116✔
228
    alloc_bufs->prev = (Pos *)HINT_ALIGNED_64(buff + prev_pos);
110,116✔
229
    alloc_bufs->head = (Pos *)HINT_ALIGNED_64(buff + head_pos);
110,116✔
230
    alloc_bufs->pending_buf = (unsigned char *)HINT_ALIGNED_64(buff + pending_pos);
110,116✔
231
    alloc_bufs->state = (deflate_state *)HINT_ALIGNED_16(buff + state_pos);
110,116✔
232

233
    memset(alloc_bufs->prev, 0, prev_size);
96,988✔
234

235
    return alloc_bufs;
110,116✔
236
}
21,604✔
237

238
/* ===========================================================================
239
 * Free all allocated deflate buffers
240
 */
241
static inline void free_deflate(PREFIX3(stream) *strm) {
42,031✔
242
    deflate_state *state = (deflate_state *)strm->state;
42,031✔
243

244
    if (state->alloc_bufs != NULL) {
110,116✔
245
        deflate_allocs *alloc_bufs = state->alloc_bufs;
42,031✔
246
        alloc_bufs->zfree(strm->opaque, alloc_bufs->buf_start);
110,116✔
247
        strm->state = NULL;
110,116✔
248
    }
21,604✔
249
}
32,608✔
250

251
/* ===========================================================================
252
 * Initialize deflate state and buffers.
253
 * This function is hidden in ZLIB_COMPAT builds.
254
 */
255
int32_t ZNG_CONDEXPORT PREFIX(deflateInit2)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
108,444✔
256
                                            int32_t memLevel, int32_t strategy) {
1,561✔
257
    /* Todo: ignore strm->next_in if we use it as window */
258
    deflate_state *s;
38,236✔
259
    int wrap = 1;
41,983✔
260

261
    /* Initialize functable */
262
    FUNCTABLE_INIT;
97,514✔
263

264
    if (strm == NULL)
110,005✔
265
        return Z_STREAM_ERROR;
266

267
    strm->msg = NULL;
110,005✔
268
    if (strm->zalloc == NULL) {
110,005✔
269
        strm->zalloc = PREFIX(zcalloc);
110,005✔
270
        strm->opaque = NULL;
110,005✔
271
    }
21,582✔
272
    if (strm->zfree == NULL)
110,005✔
273
        strm->zfree = PREFIX(zcfree);
110,005✔
274

275
    if (level == Z_DEFAULT_COMPRESSION)
110,005✔
276
        level = 6;
5,714✔
277

278
    if (windowBits < 0) { /* suppress zlib wrapper */
110,005✔
279
        wrap = 0;
2,280✔
280
        if (windowBits < -MAX_WBITS)
5,233✔
281
            return Z_STREAM_ERROR;
282
        windowBits = -windowBits;
5,233✔
283
#ifdef GZIP
284
    } else if (windowBits > MAX_WBITS) {
105,817✔
285
        wrap = 2;       /* write gzip wrapper instead */
8,443✔
286
        windowBits -= 16;
32,391✔
287
#endif
288
    }
6,205✔
289
    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || windowBits < MIN_WBITS ||
110,045✔
290
        windowBits > MAX_WBITS || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED ||
110,020✔
291
        (windowBits == 8 && wrap != 1)) {
80,770✔
292
        return Z_STREAM_ERROR;
293
    }
294
    if (windowBits == 8)
80,700✔
295
        windowBits = 9;  /* until 256-byte window bug fixed */
120✔
296

297
    /* Allocate buffers */
298
    int lit_bufsize = 1 << (memLevel + 6);
110,005✔
299
    deflate_allocs *alloc_bufs = alloc_deflate(strm, windowBits, lit_bufsize);
110,005✔
300
    if (alloc_bufs == NULL)
110,005✔
301
        return Z_MEM_ERROR;
302

303
    s = alloc_bufs->state;
110,005✔
304
    s->alloc_bufs = alloc_bufs;
110,005✔
305
    s->window = alloc_bufs->window;
110,005✔
306
    s->prev = alloc_bufs->prev;
110,005✔
307
    s->head = alloc_bufs->head;
110,005✔
308
    s->pending_buf = alloc_bufs->pending_buf;
110,005✔
309

310
    strm->state = (struct internal_state *)s;
110,005✔
311
    s->strm = strm;
110,005✔
312
    s->status = INIT_STATE;     /* to pass state test in deflateReset() */
110,005✔
313

314
    s->wrap = wrap;
110,005✔
315
    s->gzhead = NULL;
110,005✔
316
    s->w_size = 1 << windowBits;
110,005✔
317

318
    s->high_water = 0;      /* nothing written to s->window yet */
110,005✔
319

320
    s->lit_bufsize = lit_bufsize; /* 16K elements by default */
110,005✔
321

322
    /* We overlay pending_buf and sym_buf. This works since the average size
323
     * for length/distance pairs over any compressed block is assured to be 31
324
     * bits or less.
325
     *
326
     * Analysis: The longest fixed codes are a length code of 8 bits plus 5
327
     * extra bits, for lengths 131 to 257. The longest fixed distance codes are
328
     * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
329
     * possible fixed-codes length/distance pair is then 31 bits total.
330
     *
331
     * sym_buf starts one-fourth of the way into pending_buf. So there are
332
     * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
333
     * in sym_buf is three bytes -- two for the distance and one for the
334
     * literal/length. As each symbol is consumed, the pointer to the next
335
     * sym_buf value to read moves forward three bytes. From that symbol, up to
336
     * 31 bits are written to pending_buf. The closest the written pending_buf
337
     * bits gets to the next sym_buf symbol to read is just before the last
338
     * code is written. At that time, 31*(n-2) bits have been written, just
339
     * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
340
     * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
341
     * symbols are written.) The closest the writing gets to what is unread is
342
     * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
343
     * can range from 128 to 32768.
344
     *
345
     * Therefore, at a minimum, there are 142 bits of space between what is
346
     * written and what is read in the overlain buffers, so the symbols cannot
347
     * be overwritten by the compressed data. That space is actually 139 bits,
348
     * due to the three-bit fixed-code block header.
349
     *
350
     * That covers the case where either Z_FIXED is specified, forcing fixed
351
     * codes, or when the use of fixed codes is chosen, because that choice
352
     * results in a smaller compressed block than dynamic codes. That latter
353
     * condition then assures that the above analysis also covers all dynamic
354
     * blocks. A dynamic-code block will only be chosen to be emitted if it has
355
     * fewer bits than a fixed-code block would for the same set of symbols.
356
     * Therefore its average symbol length is assured to be less than 31. So
357
     * the compressed data for a dynamic block also cannot overwrite the
358
     * symbols from which it is being constructed.
359
     */
360

361
    s->pending_buf_size = s->lit_bufsize * 4;
110,005✔
362

363
#ifdef LIT_MEM
364
    s->d_buf = (uint16_t *)(s->pending_buf + (s->lit_bufsize << 1));
4,372✔
365
    s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
4,372✔
366
    s->sym_end = s->lit_bufsize - 1;
4,372✔
367
#else
368
    s->sym_buf = s->pending_buf + s->lit_bufsize;
105,633✔
369
    s->sym_end = (s->lit_bufsize - 1) * 3;
105,633✔
370
#endif
371
    /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
372
     * on 16 bit machines and because stored blocks are restricted to
373
     * 64K-1 bytes.
374
     */
375

376
    s->level = level;
110,005✔
377
    s->strategy = strategy;
110,005✔
378
    s->block_open = 0;
110,005✔
379
    s->reproducible = 0;
110,005✔
380

381
    return PREFIX(deflateReset)(strm);
110,005✔
382
}
21,582✔
383

384
#ifndef ZLIB_COMPAT
385
int32_t Z_EXPORT PREFIX(deflateInit)(PREFIX3(stream) *strm, int32_t level) {
47,453✔
386
    return PREFIX(deflateInit2)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
47,453✔
387
}
388
#endif
389

390
/* Function used by zlib.h and zlib-ng version 2.0 macros */
391
int32_t Z_EXPORT PREFIX(deflateInit_)(PREFIX3(stream) *strm, int32_t level, const char *version, int32_t stream_size) {
7,960✔
392
    if (CHECK_VER_STSIZE(version, stream_size))
7,960!
393
        return Z_VERSION_ERROR;
394
    return PREFIX(deflateInit2)(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
7,960✔
395
}
995✔
396

397
/* Function used by zlib.h and zlib-ng version 2.0 macros */
398
int32_t Z_EXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int32_t method, int32_t windowBits,
7,677✔
399
                           int32_t memLevel, int32_t strategy, const char *version, int32_t stream_size) {
400
    if (CHECK_VER_STSIZE(version, stream_size))
7,677!
401
        return Z_VERSION_ERROR;
402
    return PREFIX(deflateInit2)(strm, level, method, windowBits, memLevel, strategy);
7,677✔
403
}
1,191✔
404

405
/* =========================================================================
406
 * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
407
 */
408
static int deflateStateCheck(PREFIX3(stream) *strm) {
141,685,576✔
409
    deflate_state *s;
52,273,145✔
410
    if (strm == NULL || strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
141,685,576✔
411
        return 1;
4,256✔
412
    s = strm->state;
141,673,256✔
413
    if (s == NULL || s->alloc_bufs == NULL || s->strm != strm || (s->status < INIT_STATE || s->status > MAX_STATE))
141,673,256✔
414
        return 1;
×
415
    return 0;
30,041,606✔
416
}
27,896,954✔
417

418
/* ========================================================================= */
419
int32_t Z_EXPORT PREFIX(deflateSetDictionary)(PREFIX3(stream) *strm, const uint8_t *dictionary, uint32_t dictLength) {
7,431✔
420
    deflate_state *s;
3,992✔
421
    insert_batch_func insert_batch;
3,992✔
422
    unsigned int str, n;
3,992✔
423
    int wrap;
3,992✔
424
    uint32_t avail;
3,992✔
425
    const unsigned char *next;
3,992✔
426

427
    if (deflateStateCheck(strm) || dictionary == NULL)
7,431✔
428
        return Z_STREAM_ERROR;
4,136✔
429
    s = strm->state;
7,431✔
430
    wrap = s->wrap;
7,431✔
431
    if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
7,431✔
432
        return Z_STREAM_ERROR;
4,136✔
433

434
    if (s->level >= 9)
5,363✔
435
        insert_batch = insert_roll_batch;
48✔
436
    else
437
        insert_batch = insert_knuth_batch;
2,207✔
438

439
    /* when using zlib wrappers, compute Adler-32 for provided dictionary */
440
    if (wrap == 1)
5,363✔
441
        strm->adler = FUNCTABLE_CALL(adler32)(strm->adler, dictionary, dictLength);
5,253✔
442
    DEFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength);  /* hook for IBM Z DFLTCC */
2,208✔
443
    s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
5,363✔
444

445
    /* if dictionary would fill window, just replace the history */
446
    if (dictLength >= s->w_size) {
5,363✔
447
        if (wrap == 0) {            /* already empty otherwise */
×
448
            CLEAR_HASH(s);
×
449
            s->strstart = 0;
×
450
            s->block_start = 0;
×
451
            s->insert = 0;
×
452
        }
453
        dictionary += dictLength - s->w_size;  /* use the tail */
×
454
        dictLength = s->w_size;
×
455
    }
456

457
    /* insert dictionary into window and hash */
458
    avail = strm->avail_in;
5,363✔
459
    next = strm->next_in;
5,363✔
460
    strm->avail_in = dictLength;
5,363✔
461
    strm->next_in = (z_const unsigned char *)dictionary;
5,363✔
462
    PREFIX(fill_window)(s);
5,363✔
463
    while (s->lookahead >= STD_MIN_MATCH) {
10,726✔
464
        str = s->strstart;
5,363✔
465
        n = s->lookahead - (STD_MIN_MATCH - 1);
5,363✔
466
        insert_batch(s, s->window, str, n);
5,363✔
467
        s->strstart = str + n;
5,363✔
468
        s->lookahead = STD_MIN_MATCH - 1;
5,363✔
469
        PREFIX(fill_window)(s);
5,363✔
470
    }
471
    s->strstart += s->lookahead;
5,363✔
472
    s->block_start = (int)s->strstart;
5,363✔
473
    s->insert = s->lookahead;
5,363✔
474
    s->lookahead = 0;
5,363✔
475
    s->prev_length = 0;
5,363✔
476
    s->match_available = 0;
5,363✔
477
    strm->next_in = (z_const unsigned char *)next;
5,363✔
478
    strm->avail_in = avail;
5,363✔
479
    s->wrap = wrap;
5,363✔
480
    return Z_OK;
5,363✔
481
}
1,056✔
482

483
/* ========================================================================= */
484
int32_t Z_EXPORT PREFIX(deflateGetDictionary)(PREFIX3(stream) *strm, uint8_t *dictionary, uint32_t *dictLength) {
111✔
485
    deflate_state *s;
44✔
486
    unsigned int len;
44✔
487

488
    if (deflateStateCheck(strm))
111✔
489
        return Z_STREAM_ERROR;
490
    DEFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength);  /* hook for IBM Z DFLTCC */
46✔
491
    s = strm->state;
111✔
492
    len = s->strstart + s->lookahead;
111✔
493
    if (len > s->w_size)
111✔
494
        len = s->w_size;
×
495
    if (dictionary != NULL && len)
111✔
496
        memcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
111✔
497
    if (dictLength != NULL)
111✔
498
        *dictLength = len;
111✔
499
    return Z_OK;
24✔
500
}
22✔
501

502
/* ========================================================================= */
503
int32_t Z_EXPORT PREFIX(deflateResetKeep)(PREFIX3(stream) *strm) {
3,627,422✔
504
    deflate_state *s;
1,369,763✔
505

506
    if (deflateStateCheck(strm))
3,627,422✔
507
        return Z_STREAM_ERROR;
508

509
    strm->total_in = strm->total_out = 0;
3,627,422✔
510
    strm->msg = NULL; /* use zfree if we ever allocate msg dynamically */
3,627,422✔
511
    strm->data_type = Z_UNKNOWN;
3,627,422✔
512

513
    s = (deflate_state *)strm->state;
3,627,422✔
514
    s->pending = 0;
3,627,422✔
515
    s->pending_out = s->pending_buf;
3,627,422✔
516

517
    if (s->wrap < 0)
3,627,422✔
518
        s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
55✔
519

520
    s->status =
3,691,890✔
521
#ifdef GZIP
522
        s->wrap == 2 ? GZIP_STATE :
3,627,422✔
523
#endif
524
        INIT_STATE;
1,451✔
525

526
#ifdef GZIP
527
    if (s->wrap == 2) {
3,627,422✔
528
        strm->adler = CRC32_INITIAL_VALUE;
30,815✔
529
    } else
6,205✔
530
#endif
531
        strm->adler = ADLER32_INITIAL_VALUE;
3,449,844✔
532
    s->last_flush = -2;
3,627,422✔
533

534
    zng_tr_init(s);
3,627,422✔
535

536
    DEFLATE_RESET_KEEP_HOOK(strm);  /* hook for IBM Z DFLTCC */
1,392,941✔
537

538
    return Z_OK;
3,627,422✔
539
}
1,175,468✔
540

541
/* ========================================================================= */
542
int32_t Z_EXPORT PREFIX(deflateReset)(PREFIX3(stream) *strm) {
3,627,422✔
543
    int ret = PREFIX(deflateResetKeep)(strm);
3,627,422✔
544
    if (ret == Z_OK)
3,627,422✔
545
        lm_init(strm->state);
3,627,422✔
546
    return ret;
4,779,712✔
547
}
1,152,290✔
548

549
/* ========================================================================= */
550
int32_t Z_EXPORT PREFIX(deflateSetHeader)(PREFIX3(stream) *strm, PREFIX(gz_headerp) head) {
111✔
551
    if (deflateStateCheck(strm) || strm->state->wrap != 2)
111✔
552
        return Z_STREAM_ERROR;
553
    strm->state->gzhead = head;
111✔
554
    return Z_OK;
111✔
555
}
22✔
556

557
/* ========================================================================= */
558
int32_t Z_EXPORT PREFIX(deflatePending)(PREFIX3(stream) *strm, uint32_t *pending, int32_t *bits) {
111✔
559
    if (deflateStateCheck(strm))
111✔
560
        return Z_STREAM_ERROR;
561
    if (pending != NULL)
111✔
562
        *pending = strm->state->pending;
111✔
563
    if (bits != NULL)
111✔
564
        *bits = strm->state->bi_valid;
111✔
565
    return Z_OK;
24✔
566
}
22✔
567

568
/* ========================================================================= */
NEW
569
int32_t Z_EXPORT PREFIX(deflateUsed)(PREFIX3(stream) *strm, int32_t *bits) {
×
NEW
570
    if (deflateStateCheck(strm))
×
571
        return Z_STREAM_ERROR;
NEW
572
    if (bits != NULL)
×
NEW
573
        *bits = strm->state->bi_used;
×
574
    return Z_OK;
575
}
576

577
/* ========================================================================= */
578
int32_t Z_EXPORT PREFIX(deflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) {
999✔
579
    deflate_state *s;
396✔
580
    uint64_t value64 = (uint64_t)value;
999✔
581
    int32_t put;
396✔
582

583
    if (deflateStateCheck(strm))
999✔
584
        return Z_STREAM_ERROR;
585
    s = strm->state;
999✔
586

587
#ifdef LIT_MEM
588
    if (bits < 0 || bits > BIT_BUF_SIZE ||
36✔
589
        (unsigned char *)s->d_buf < s->pending_out + ((BIT_BUF_SIZE + 7) >> 3))
36✔
590
        return Z_BUF_ERROR;
591
#else
592
    if (bits < 0 || bits > BIT_BUF_SIZE || bits > (int32_t)(sizeof(value) << 3) ||
999✔
593
        s->sym_buf < s->pending_out + ((BIT_BUF_SIZE + 7) >> 3))
963✔
594
        return Z_BUF_ERROR;
595
#endif
596

597
    do {
414✔
598
        put = BIT_BUF_SIZE - s->bi_valid;
999✔
599
        put = MIN(put, bits);
999✔
600

601
        if (s->bi_valid == 0)
999✔
602
            s->bi_buf = value64;
856✔
603
        else
604
            s->bi_buf |= (value64 & ((UINT64_C(1) << put) - 1)) << s->bi_valid;
111✔
605
        s->bi_valid += put;
999✔
606
        zng_tr_flush_bits(s);
999✔
607
        value64 >>= put;
999✔
608
        bits -= put;
999✔
609
    } while (bits);
999✔
610
    return Z_OK;
216✔
611
}
198✔
612

613
/* ========================================================================= */
614
int32_t Z_EXPORT PREFIX(deflateParams)(PREFIX3(stream) *strm, int32_t level, int32_t strategy) {
11,607✔
615
    deflate_state *s;
4,570✔
616
    compress_func func;
4,570✔
617
    int hook_flush = Z_NO_FLUSH;
4,982✔
618

619
    if (deflateStateCheck(strm))
11,607✔
620
        return Z_STREAM_ERROR;
621
    s = strm->state;
11,607✔
622

623
    if (level == Z_DEFAULT_COMPRESSION)
11,607✔
624
        level = 6;
625
    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED)
11,607✔
626
        return Z_STREAM_ERROR;
627
    DEFLATE_PARAMS_HOOK(strm, level, strategy, &hook_flush);  /* hook for IBM Z DFLTCC */
4,776✔
628
    func = configuration_table[s->level].func;
11,607✔
629

630
    if (((strategy != s->strategy || func != configuration_table[level].func) && s->last_flush != -2)
11,607✔
631
        || hook_flush != Z_NO_FLUSH) {
2,291✔
632
        /* Flush the last buffer. Use Z_BLOCK mode, unless the hook requests a "stronger" one. */
633
        int flush = RANK(hook_flush) > RANK(Z_BLOCK) ? hook_flush : Z_BLOCK;
4,856✔
634
        int err = PREFIX(deflate)(strm, flush);
11,310✔
635
        if (err == Z_STREAM_ERROR)
11,310✔
636
            return err;
637
        if (strm->avail_in || ((int)s->strstart - s->block_start) + s->lookahead || !DEFLATE_DONE(strm, flush))
11,310✔
638
            return Z_BUF_ERROR;
639
    }
2,231✔
640

641
    int hashless = level == 0 || strategy == Z_HUFFMAN_ONLY || strategy == Z_RLE;
11,607✔
642
    int was_hashless = s->level == 0 || s->strategy == Z_HUFFMAN_ONLY || s->strategy == Z_RLE;
11,607✔
643

644
    /* Stale if the hash usage flipped (to/from huffman/rle/stored), the hash
645
     * function changed at level 9, or quick at level 1 left prev unmaintained. */
646
    int stale_chain = (hashless != was_hashless) || (level >= 9) != (s->level >= 9) ||
11,769✔
647
                      (HAVE_QUICK_STRATEGY && s->level == 1 && level != 1);
491✔
648

649
    /* Rebuild the hash chains when fill_window is called. */
650
    if (stale_chain && !hashless) {
11,509✔
651
        CLEAR_HASH(s);
5,906✔
652
        s->ins_h = 0;
5,906✔
653
        s->insert = MIN(s->strstart, s->w_size);
5,906✔
654
    }
1,165✔
655

656
    if (s->level != level)
11,607✔
657
        lm_set_level(s, level);
4,838✔
658
    s->strategy = strategy;
11,607✔
659
    return Z_OK;
11,607✔
660
}
2,286✔
661

662
/* ========================================================================= */
663
int32_t Z_EXPORT PREFIX(deflateTune)(PREFIX3(stream) *strm, int32_t good_length, int32_t max_lazy, int32_t nice_length, int32_t max_chain) {
111✔
664
    deflate_state *s;
44✔
665

666
    if (deflateStateCheck(strm))
111✔
667
        return Z_STREAM_ERROR;
668
    s = strm->state;
111✔
669
    s->good_match = (unsigned int)good_length;
111✔
670
    s->max_lazy_match = (unsigned int)max_lazy;
111✔
671
    s->nice_match = nice_length;
111✔
672
    s->max_chain_length = (unsigned int)max_chain;
111✔
673
    return Z_OK;
111✔
674
}
22✔
675

676
/* =========================================================================
677
 * For the default windowBits of 15 and memLevel of 8, this function returns
678
 * a close to exact, as well as small, upper bound on the compressed size.
679
 * They are coded as constants here for a reason--if the #define's are
680
 * changed, then this function needs to be changed as well.  The return
681
 * value for 15 and 8 only works for those exact settings.
682
 *
683
 * For any setting other than those defaults for windowBits and memLevel,
684
 * the value returned is a conservative worst case for the maximum expansion
685
 * resulting from using fixed blocks instead of stored blocks, which deflate
686
 * can emit on compressed data for some combinations of the parameters.
687
 *
688
 * This function could be more sophisticated to provide closer upper bounds for
689
 * every combination of windowBits and memLevel.  But even the conservative
690
 * upper bound of about 14% expansion does not seem onerous for output buffer
691
 * allocation.
692
 */
693
unsigned long Z_EXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long sourceLen) {
23,524✔
694
    deflate_state *s;
9,372✔
695
    unsigned long complen, wraplen;
9,372✔
696

697
    /* conservative upper bound for compressed data */
698
    complen = sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
23,524✔
699
    DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen);  /* hook for IBM Z DFLTCC */
9,798✔
700

701
    /* if can't get parameters, return conservative bound plus zlib wrapper */
702
    if (deflateStateCheck(strm))
23,524✔
703
        return complen + 6;
12,320✔
704

705
    /* compute wrapper length */
706
    s = strm->state;
11,204✔
707
    switch (s->wrap) {
11,204✔
708
    case 0:                                 /* raw deflate */
709
        wraplen = 0;
12✔
710
        break;
12✔
711
    case 1:                                 /* zlib wrapper */
6,835✔
712
        wraplen = ZLIB_WRAPLEN + (s->strstart ? 4 : 0);
8,718✔
713
        break;
1,883✔
714
#ifdef GZIP
715
    case 2:                                 /* gzip wrapper */
1,903✔
716
        wraplen = GZIP_WRAPLEN;
1,056✔
717
        if (s->gzhead != NULL) {            /* user-supplied gzip header */
2,431✔
718
            unsigned char *str;
44✔
719
            if (s->gzhead->extra != NULL) {
111✔
720
                wraplen += 2 + s->gzhead->extra_len;
111✔
721
            }
22✔
722
            str = s->gzhead->name;
111✔
723
            if (str != NULL) {
111✔
724
                do {
142✔
725
                    wraplen++;
555✔
726
                } while (*str++);
555✔
727
            }
22✔
728
            str = s->gzhead->comment;
111✔
729
            if (str != NULL) {
111✔
730
                do {
214✔
731
                    wraplen++;
888✔
732
                } while (*str++);
888✔
733
            }
22✔
734
            if (s->gzhead->hcrc)
111✔
735
                wraplen += 2;
111✔
736
        }
22✔
737
        break;
484✔
738
#endif
739
    default:                                /* for compiler happiness */
740
        Z_UNREACHABLE();
×
741
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 202311L
742
        wraplen = ZLIB_WRAPLEN;
743
#endif
744
    }
745

746
    /* if not default parameters, return conservative bound */
747
    if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) ||  /* hook for IBM Z DFLTCC */
11,811✔
748
            W_BITS(s) != MAX_WBITS || HASH_BITS < 15) {
11,204✔
749
        if (s->level == 0) {
5,085✔
750
            /* upper bound for stored blocks with length 127 (memLevel == 1) --
751
               ~4% overhead plus a small constant */
752
            complen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) + (sourceLen >> 11) + 7;
×
753
        }
754

755
        return complen + wraplen;
5,085✔
756
    }
757

758
#ifndef NO_QUICK_STRATEGY
759
    return sourceLen                       /* The source size itself */
3,774✔
760
      + (sourceLen == 0 ? 1 : 0)           /* Always at least one byte for any input */
5,786✔
761
      + (sourceLen < 9 ? 1 : 0)            /* One extra byte for lengths less than 9 */
5,762✔
762
      + DEFLATE_QUICK_OVERHEAD(sourceLen)  /* Source encoding overhead, padded to next full byte */
5,786✔
763
      + DEFLATE_BLOCK_OVERHEAD             /* Deflate block overhead bytes */
1,332✔
764
      + wraplen;                           /* none, zlib or gzip wrapper */
5,786✔
765
#else
766
    return sourceLen + (sourceLen >> 4) + 7 + wraplen;
333✔
767
#endif
768
}
4,686✔
769

770
/* =========================================================================
771
 * Flush as much pending output as possible. See flush_pending_inline()
772
 */
773
Z_INTERNAL void PREFIX(flush_pending)(PREFIX3(stream) *strm) {
9,978,461✔
774
    flush_pending_inline(strm);
3,353,163✔
775
}
9,972,465✔
776

777
/* ===========================================================================
778
 * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
779
 */
780
#define HCRC_UPDATE(beg) \
781
    do { \
782
        if (s->gzhead->hcrc && s->pending > (beg)) \
783
            strm->adler = crc32_small((uint32_t)strm->adler, s->pending_buf + (beg), s->pending - (beg)); \
784
    } while (0)
785

786
/* =========================================================================
787
 * Write zlib/gzip header
788
 */
789
static int deflateHeaders(deflate_state *s, PREFIX3(stream) *strm) {
104,827✔
790
    if (s->status == INIT_STATE) {
104,827✔
791
        /* zlib header */
792
        unsigned int header = (Z_DEFLATED + ((W_BITS(s)-8)<<4)) << 8;
72,436✔
793
        unsigned int level_flags;
28,685✔
794

795
        if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
72,436✔
796
            level_flags = 0;
3,977✔
797
        else if (s->level < 6)
54,090✔
798
            level_flags = 1;
3,027✔
799
        else if (s->level == 6)
40,082✔
800
            level_flags = 2;
4,305✔
801
        else
802
            level_flags = 3;
8,662✔
803
        header |= (level_flags << 6);
72,436✔
804
        if (s->strstart != 0)
72,436✔
805
            header |= PRESET_DICT;
5,253✔
806
        header += 31 - (header % 31);
72,436✔
807

808
        put_short_msb(s, (uint16_t)header);
55,486✔
809

810
        /* Save the adler32 of the preset dictionary: */
811
        if (s->strstart != 0)
72,436✔
812
            put_uint32_msb(s, strm->adler);
5,253✔
813
        strm->adler = ADLER32_INITIAL_VALUE;
72,436✔
814
        s->status = BUSY_STATE;
72,436✔
815

816
        /* Compression must start with an empty pending buffer */
817
        PREFIX(flush_pending)(strm);
72,436✔
818
        if (s->pending != 0) {
72,436✔
819
            s->last_flush = -1;
5,650✔
820
            return Z_OK;
5,650✔
821
        }
822
    }
14,242✔
823
#ifdef GZIP
824
    if (s->status == GZIP_STATE) {
99,177✔
825
        /* gzip header */
826
        strm->adler = CRC32_INITIAL_VALUE;
32,391✔
827
        put_byte(s, 31);
32,391✔
828
        put_byte(s, 139);
32,391✔
829
        put_byte(s, 8);
32,391✔
830
        if (s->gzhead == NULL) {
32,391✔
831
            put_uint32(s, 0);
8,395✔
832
            put_byte(s, 0);
32,280✔
833
            put_byte(s, s->level == 9 ? 2 :
33,260✔
834
                     (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
2,043✔
835
            put_byte(s, OS_CODE);
32,280✔
836
            s->status = BUSY_STATE;
32,280✔
837

838
            /* Compression must start with an empty pending buffer */
839
            PREFIX(flush_pending)(strm);
32,280✔
840
            if (s->pending != 0) {
32,280✔
841
                s->last_flush = -1;
×
842
                return Z_OK;
×
843
            }
844
        } else {
6,183✔
845
            put_byte(s, (s->gzhead->text ? 1 : 0) +
299✔
846
                     (s->gzhead->hcrc ? 2 : 0) +
847
                     (s->gzhead->extra == NULL ? 0 : 4) +
848
                     (s->gzhead->name == NULL ? 0 : 8) +
849
                     (s->gzhead->comment == NULL ? 0 : 16)
850
                     );
24✔
851
            put_uint32(s, s->gzhead->time);
111✔
852
            put_byte(s, s->level == 9 ? 2 : (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
111✔
853
            put_byte(s, s->gzhead->os & 0xff);
111✔
854
            if (s->gzhead->extra != NULL)
111✔
855
                put_short(s, (uint16_t)s->gzhead->extra_len);
111✔
856
            if (s->gzhead->hcrc)
111✔
857
                strm->adler = crc32_small((uint32_t)strm->adler, s->pending_buf, s->pending);
111✔
858
            s->gzindex = 0;
111✔
859
            s->status = EXTRA_STATE;
111✔
860
        }
861
    }
6,205✔
862
    if (s->status == EXTRA_STATE) {
99,177✔
863
        if (s->gzhead->extra != NULL) {
111✔
864
            uint32_t beg = s->pending;   /* start of bytes to update crc */
111✔
865
            uint32_t left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
111✔
866

867
            while (s->pending + left > s->pending_buf_size) {
111✔
868
                uint32_t copy = s->pending_buf_size - s->pending;
×
869
                memcpy(s->pending_buf + s->pending, s->gzhead->extra + s->gzindex, copy);
×
870
                s->pending = s->pending_buf_size;
×
871
                HCRC_UPDATE(beg);
×
872
                s->gzindex += copy;
×
873
                PREFIX(flush_pending)(strm);
×
874
                if (s->pending != 0) {
×
875
                    s->last_flush = -1;
×
876
                    return Z_OK;
×
877
                }
878
                beg = 0;
879
                left -= copy;
×
880
            }
881
            memcpy(s->pending_buf + s->pending, s->gzhead->extra + s->gzindex, left);
111✔
882
            s->pending += left;
111✔
883
            HCRC_UPDATE(beg);
111✔
884
            s->gzindex = 0;
111✔
885
        }
22✔
886
        s->status = NAME_STATE;
111✔
887
    }
22✔
888
    if (s->status == NAME_STATE) {
99,177✔
889
        if (s->gzhead->name != NULL) {
111✔
890
            uint32_t beg = s->pending;   /* start of bytes to update crc */
111✔
891
            unsigned char val;
140✔
892

893
            do {
142✔
894
                if (s->pending == s->pending_buf_size) {
555✔
895
                    HCRC_UPDATE(beg);
×
896
                    PREFIX(flush_pending)(strm);
×
897
                    if (s->pending != 0) {
×
898
                        s->last_flush = -1;
×
899
                        return Z_OK;
×
900
                    }
901
                    beg = 0;
902
                }
903
                val = s->gzhead->name[s->gzindex++];
555✔
904
                put_byte(s, val);
555✔
905
            } while (val != 0);
555✔
906
            HCRC_UPDATE(beg);
111✔
907
            s->gzindex = 0;
111✔
908
        }
22✔
909
        s->status = COMMENT_STATE;
111✔
910
    }
22✔
911
    if (s->status == COMMENT_STATE) {
99,177✔
912
        if (s->gzhead->comment != NULL) {
111✔
913
            uint32_t beg = s->pending;  /* start of bytes to update crc */
111✔
914
            unsigned char val;
212✔
915

916
            do {
214✔
917
                if (s->pending == s->pending_buf_size) {
888✔
918
                    HCRC_UPDATE(beg);
×
919
                    PREFIX(flush_pending)(strm);
×
920
                    if (s->pending != 0) {
×
921
                        s->last_flush = -1;
×
922
                        return Z_OK;
×
923
                    }
924
                    beg = 0;
925
                }
926
                val = s->gzhead->comment[s->gzindex++];
888✔
927
                put_byte(s, val);
888✔
928
            } while (val != 0);
888✔
929
            HCRC_UPDATE(beg);
111✔
930
        }
22✔
931
        s->status = HCRC_STATE;
111✔
932
    }
22✔
933
    if (s->status == HCRC_STATE) {
99,177✔
934
        if (s->gzhead->hcrc) {
111✔
935
            if (s->pending + 2 > s->pending_buf_size) {
111✔
936
                PREFIX(flush_pending)(strm);
×
937
                if (s->pending != 0) {
×
938
                    s->last_flush = -1;
×
939
                    return Z_OK;
×
940
                }
941
            }
942
            put_short(s, (uint16_t)strm->adler);
111✔
943
            strm->adler = CRC32_INITIAL_VALUE;
111✔
944
        }
22✔
945
        s->status = BUSY_STATE;
111✔
946

947
        /* Compression must start with an empty pending buffer */
948
        flush_pending_inline(strm);
48✔
949
        if (s->pending != 0) {
111✔
950
            s->last_flush = -1;
111✔
951
            return Z_OK;
111✔
952
        }
953
    }
954
#endif
955
    return -1;
20,780✔
956
}
20,548✔
957

958
/* ========================================================================= */
959
int32_t Z_EXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int32_t flush) {
144,614,624✔
960
    int32_t old_flush; /* value of flush param for previous deflate call */
56,944,354✔
961
    deflate_state *s;
56,944,354✔
962

963
    if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0)
144,614,624✔
964
        return Z_STREAM_ERROR;
13,418,028✔
965
    s = strm->state;
144,614,624✔
966

967
    if (strm->next_out == NULL || (strm->avail_in != 0 && strm->next_in == NULL)
147,056,087✔
968
        || (s->status == FINISH_STATE && flush != Z_FINISH)) {
137,905,610✔
969
        ERR_RETURN(strm, Z_STREAM_ERROR);
13,418,028✔
970
    }
971
    if (strm->avail_out == 0) {
137,905,610✔
972
        ERR_RETURN(strm, Z_BUF_ERROR);
×
973
    }
974

975
    old_flush = s->last_flush;
137,905,610✔
976
    s->last_flush = flush;
137,905,610✔
977

978
    /* Flush as much pending output as possible */
979
    if (s->pending != 0) {
137,905,610✔
980
        flush_pending_inline(strm);
13,958,306✔
981
        if (strm->avail_out == 0) {
35,168,552✔
982
            /* Since avail_out is 0, deflate will be called again with
983
             * more output space, but possibly with both pending and
984
             * avail_in equal to zero. There won't be anything to do,
985
             * but this is not an error situation so make sure we
986
             * return OK instead of BUF_ERROR at next call of deflate:
987
             */
988
            s->last_flush = -1;
31,241,918✔
989
            return Z_OK;
31,241,918✔
990
        }
991

992
        /* Make sure there is something to do and avoid duplicate consecutive
993
         * flushes. For repeated and useless calls with Z_FINISH, we keep
994
         * returning Z_STREAM_END instead of Z_BUF_ERROR.
995
         */
996
    } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && flush != Z_FINISH) {
105,456,685✔
997
        ERR_RETURN(strm, Z_BUF_ERROR);
750,306✔
998
    }
999

1000
    /* User must not provide more input after the first FINISH: */
1001
    if (s->status == FINISH_STATE && strm->avail_in != 0)   {
105,913,386✔
1002
        ERR_RETURN(strm, Z_BUF_ERROR);
×
1003
    }
1004

1005
    /* Skip headers if raw deflate stream was requested */
1006
    if (s->status == INIT_STATE && s->wrap == 0) {
105,913,386✔
1007
        s->status = BUSY_STATE;
3,522,540✔
1008
    }
1,154,909✔
1009

1010
    if (s->status != BUSY_STATE && s->status != FINISH_STATE && s->wrap != 0) {
105,913,386✔
1011
        /* Write the header */
1012
        if (deflateHeaders(s, strm) == Z_OK) {
104,827✔
1013
            return Z_OK;
1,235✔
1014
        }
1015
    }
19,415✔
1016

1017
    /* Start a new block or continue the current one. */
1018
    if (strm->avail_in != 0 || s->lookahead != 0 || (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
105,907,625✔
1019
        block_state bstate;
39,252,321✔
1020

1021
#ifndef _MSC_VER
1022
        if (DEFLATE_HOOK(strm, flush, &bstate) == 0) /* hook for IBM Z DFLTCC */
20,233,636✔
1023
#endif
1024
            bstate = s->level == 0 ? deflate_stored(s, flush) :
209,707,530✔
1025
                 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
126,351,620✔
1026
                 s->strategy == Z_RLE ? deflate_rle(s, flush) :
37,833,158✔
1027
                 (*(configuration_table[s->level].func))(s, flush);
105,348,824✔
1028

1029
        if (bstate == finish_started || bstate == finish_done) {
105,785,839✔
1030
            s->status = FINISH_STATE;
3,627,367✔
1031
        }
1,175,457✔
1032
        if (bstate == need_more || bstate == finish_started) {
105,785,839✔
1033
            if (strm->avail_out == 0) {
102,149,660✔
1034
                s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
521,812✔
1035
            }
136,592✔
1036
            return Z_OK;
98,446,589✔
1037
            /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1038
             * of deflate should use the same flush parameter to make sure
1039
             * that the flush is complete. So we don't have to output an
1040
             * empty block here, this will be done at next call. This also
1041
             * ensures that for a very small output buffer, we emit at most
1042
             * one empty block.
1043
             */
1044
        }
1045
        if (bstate == block_done) {
3,636,179✔
1046
            if (flush == Z_PARTIAL_FLUSH) {
16,854✔
1047
                zng_tr_align(s);
×
1048
            } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
16,854✔
1049
                zng_tr_stored_block(s, NULL, 0L, 0);
5,874✔
1050
                /* For a full flush, this empty block will be recognized
1051
                 * as a special marker by inflate_sync().
1052
                 */
1053
                if (flush == Z_FULL_FLUSH) {
5,874✔
1054
                    CLEAR_HASH(s);             /* forget history */
5,372✔
1055
                    if (s->lookahead == 0) {
5,372✔
1056
                        s->strstart = 0;
5,372✔
1057
                        s->block_start = 0;
5,372✔
1058
                        s->insert = 0;
5,372✔
1059
                    }
1,056✔
1060
                }
1,056✔
1061
            }
1,155✔
1062
            PREFIX(flush_pending)(strm);
16,854✔
1063
            if (strm->avail_out == 0) {
16,854✔
1064
                s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
×
1065
                return Z_OK;
×
1066
            }
1067
        }
3,320✔
1068
    }
19,042,021✔
1069

1070
    if (flush != Z_FINISH)
3,694,122✔
1071
        return Z_OK;
89,567✔
1072

1073
    /* Write the trailer */
1074
#ifdef GZIP
1075
    if (s->wrap == 2) {
3,663,428✔
1076
        put_uint32(s, strm->adler);
32,391✔
1077
        put_uint32(s, (uint32_t)strm->total_in);
32,391✔
1078
    } else
6,205✔
1079
#endif
1080
    {
1081
        if (s->wrap == 1)
3,631,037✔
1082
            put_uint32_msb(s, strm->adler);
72,436✔
1083
    }
1084
    flush_pending_inline(strm);
1,404,543✔
1085
    /* If avail_out is zero, the application will call deflate again
1086
     * to flush the rest.
1087
     */
1088
    if (s->wrap > 0)
3,663,428✔
1089
        s->wrap = -s->wrap; /* write the trailer only once! */
104,827✔
1090
    if (s->pending == 0) {
3,663,428✔
1091
        Assert(s->bi_valid == 0, "bi_buf not flushed");
23,927✔
1092
        return Z_STREAM_END;
1,331,245✔
1093
    }
1094
    return Z_OK;
1,249✔
1095
}
26,691,466✔
1096

1097
/* ========================================================================= */
1098
int32_t Z_EXPORT PREFIX(deflateEnd)(PREFIX3(stream) *strm) {
110,116✔
1099
    if (deflateStateCheck(strm))
110,116✔
1100
        return Z_STREAM_ERROR;
1101

1102
    int32_t status = strm->state->status;
110,116✔
1103

1104
    /* Free allocated buffers */
1105
    free_deflate(strm);
42,031✔
1106

1107
    return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
110,116✔
1108
}
21,604✔
1109

1110
/* =========================================================================
1111
 * Copy the source state to the destination state.
1112
 */
1113
int32_t Z_EXPORT PREFIX(deflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *source) {
111✔
1114
    deflate_state *ds;
44✔
1115
    deflate_state *ss;
44✔
1116

1117
    if (deflateStateCheck(source) || dest == NULL)
111✔
1118
        return Z_STREAM_ERROR;
1119

1120
    ss = source->state;
111✔
1121

1122
    memcpy(dest, source, sizeof(PREFIX3(stream)));
62✔
1123

1124
    deflate_allocs *alloc_bufs = alloc_deflate(dest, W_BITS(ss), ss->lit_bufsize);
111✔
1125
    if (alloc_bufs == NULL)
111✔
1126
        return Z_MEM_ERROR;
1127

1128
    ds = alloc_bufs->state;
111✔
1129

1130
    dest->state = (struct internal_state *) ds;
111✔
1131
    memcpy(ds, ss, sizeof(deflate_state));
62✔
1132
    ds->strm = dest;
111✔
1133

1134
    ds->alloc_bufs = alloc_bufs;
111✔
1135
    ds->window = alloc_bufs->window;
111✔
1136
    ds->prev = alloc_bufs->prev;
111✔
1137
    ds->head = alloc_bufs->head;
111✔
1138
    ds->pending_buf = alloc_bufs->pending_buf;
111✔
1139

1140
    if (ds->window == NULL || ds->prev == NULL || ds->head == NULL || ds->pending_buf == NULL) {
111✔
1141
        PREFIX(deflateEnd)(dest);
×
1142
        return Z_MEM_ERROR;
×
1143
    }
1144

1145
    memcpy(ds->window, ss->window, DEFLATE_ADJUST_WINDOW_SIZE(ds->w_size * 2 * sizeof(unsigned char)));
111✔
1146
    memcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
111✔
1147
    memcpy(ds->head, ss->head, HASH_SIZE * sizeof(Pos));
111✔
1148
    memcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS);
111✔
1149

1150
    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
111✔
1151
#ifdef LIT_MEM
1152
    ds->d_buf = (uint16_t *)(ds->pending_buf + (ds->lit_bufsize << 1));
4✔
1153
    ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
4✔
1154
#else
1155
    ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
107✔
1156
#endif
1157

1158
    ds->l_desc.dyn_tree = ds->dyn_ltree;
111✔
1159
    ds->d_desc.dyn_tree = ds->dyn_dtree;
111✔
1160
    ds->bl_desc.dyn_tree = ds->bl_tree;
111✔
1161

1162
    return Z_OK;
111✔
1163
}
22✔
1164

1165
/* ===========================================================================
1166
 * Set longest match variables based on level configuration
1167
 */
1168
static void lm_set_level(deflate_state *s, int level) {
1,399,401✔
1169
    s->max_lazy_match   = configuration_table[level].max_lazy;
3,638,695✔
1170
    s->good_match       = configuration_table[level].good_length;
3,638,695✔
1171
    s->nice_match       = configuration_table[level].nice_length;
3,638,695✔
1172
    s->max_chain_length = configuration_table[level].max_chain;
3,638,695✔
1173
    s->level = level;
1,405,836✔
1174
}
1,339,144✔
1175

1176
/* ===========================================================================
1177
 * Initialize the "longest match" routines for a new zlib stream
1178
 */
1179
static void lm_init(deflate_state *s) {
3,627,422✔
1180
    s->window_size = 2 * s->w_size;
3,627,422✔
1181

1182
    CLEAR_HASH(s);
3,627,422✔
1183

1184
    /* Set the default configuration parameters:
1185
     */
1186
    lm_set_level(s, s->level);
3,627,422✔
1187

1188
    s->strstart = 0;
3,627,422✔
1189
    s->block_start = 0;
3,627,422✔
1190
    s->lookahead = 0;
3,627,422✔
1191
    s->insert = 0;
3,627,422✔
1192
    s->prev_length = 0;
3,627,422✔
1193
    s->match_available = 0;
3,627,422✔
1194
    s->match_start = 0;
3,627,422✔
1195
    s->ins_h = 0;
3,627,422✔
1196
}
3,625,800✔
1197

1198
/* ===========================================================================
1199
 * Fill the window when the lookahead becomes insufficient.
1200
 * Updates strstart and lookahead.
1201
 *
1202
 * IN assertion: lookahead < MIN_LOOKAHEAD
1203
 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1204
 *    At least one byte has been read, or avail_in == 0; reads are
1205
 *    performed for at least two bytes (required for the zip translate_eol
1206
 *    option -- not supported here).
1207
 */
1208

1209
void Z_INTERNAL PREFIX(fill_window)(deflate_state *s) {
149,226,828✔
1210
    PREFIX3(stream) *strm = s->strm;
149,226,828✔
1211
    insert_batch_func insert_batch;
54,510,181✔
1212
    unsigned char *window = s->window;
149,226,828✔
1213
    unsigned n;
54,510,181✔
1214
    unsigned int more;    /* Amount of free space at the end of the window. */
54,510,181✔
1215
    unsigned int wsize = s->w_size;
149,226,828✔
1216
    int level = s->level;
149,226,828✔
1217

1218
    Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
29,067,474✔
1219

1220
    if (level >= 9)
149,226,828✔
1221
        insert_batch = insert_roll_batch;
26,887,825✔
1222
    else if (HAVE_QUICK_STRATEGY && level == 1)
142,896,272✔
1223
        insert_batch = insert_knuth_batch_head;
6,050,896✔
1224
    else
1225
        insert_batch = insert_knuth_batch;
51,567,476✔
1226

1227
    do {
58,989,641✔
1228
        more = s->window_size - s->lookahead - s->strstart;
149,226,828✔
1229

1230
        /* If the window is almost full and there is insufficient lookahead,
1231
         * move the upper half to the lower one to make room in the upper half.
1232
         */
1233
        if (s->strstart >= wsize+MAX_DIST(s)) {
149,226,828✔
1234
            memcpy(window, window + wsize, (unsigned)wsize);
3,405,531✔
1235
            if (s->match_start >= wsize) {
3,405,531✔
1236
                s->match_start -= wsize;
2,388,492✔
1237
            } else {
448,609✔
1238
                s->match_start = 0;
1,017,039✔
1239
                s->prev_length = 0;
1,017,039✔
1240
            }
1241
            s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
3,405,531✔
1242
            s->block_start -= (int)wsize;
3,405,531✔
1243
            if (s->insert > s->strstart)
3,405,531✔
1244
                s->insert = s->strstart;
×
1245
            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
3,405,531✔
1246
                /* Z_HUFFMAN_ONLY and Z_RLE never read the hash chain. deflate_quick
1247
                 * reads the chain head but never walks prev, so it slides head only. */
1248
                if (HAVE_QUICK_STRATEGY && level == 1)
2,563,853✔
1249
                    FUNCTABLE_CALL(slide_hash_head)(s);
318,332✔
1250
                else
1251
                    FUNCTABLE_CALL(slide_hash)(s);
2,389,953✔
1252
            }
512,584✔
1253
            more += wsize;
3,405,531✔
1254
        }
644,800✔
1255
        if (strm->avail_in == 0)
149,226,828✔
1256
            break;
10,733,559✔
1257

1258
        /* If there was no sliding:
1259
         *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1260
         *    more == window_size - lookahead - strstart
1261
         * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1262
         * => more >= window_size - 2*WSIZE + 2
1263
         * In the BIG_MEM or MMAP case (not yet supported),
1264
         *   window_size == input_size + MIN_LOOKAHEAD  &&
1265
         *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1266
         * Otherwise, window_size == 2*WSIZE so more >= 2.
1267
         * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1268
         */
1269
        Assert(more >= 2, "more < 2");
22,044,246✔
1270

1271
        n = read_buf(strm, window + s->strstart + s->lookahead, more);
107,807,310✔
1272
        s->lookahead += n;
107,807,310✔
1273

1274
        /* Initialize the hash value now that we have some input: */
1275
        if (s->lookahead + s->insert >= STD_MIN_MATCH) {
107,807,310✔
1276
            unsigned int str = s->strstart - s->insert;
107,794,098✔
1277
            if (UNLIKELY(level >= 9)) {
107,794,098✔
1278
                s->ins_h = update_hash_roll(window[str], window[str+1]);
379,232✔
1279
            } else if (str >= 1) {
107,488,784✔
1280
                if (HAVE_QUICK_STRATEGY && level == 1)
98,913,957✔
1281
                    insert_knuth_head(s, window, str + 2 - STD_MIN_MATCH);
3,878,501✔
1282
                else
1283
                    insert_knuth(s, window, str + 2 - STD_MIN_MATCH);
98,722,297✔
1284
            }
19,580,625✔
1285
            unsigned int count = s->insert;
42,984,652✔
1286
            if (UNLIKELY(s->lookahead == 1)) {
107,794,098✔
1287
                count -= 1;
×
1288
            }
1289
            if (count > 0) {
107,794,098✔
1290
                insert_batch(s, window, str, count);
11,437✔
1291
                s->insert -= count;
11,437✔
1292
            }
2,254✔
1293
        }
21,091,591✔
1294
        /* If the whole input has less than STD_MIN_MATCH bytes, ins_h is garbage,
1295
         * but this is not important since only literal bytes will be emitted.
1296
         */
1297
    } while (s->lookahead < MIN_LOOKAHEAD && strm->avail_in != 0);
107,807,310✔
1298

1299
    /* If the WIN_INIT bytes after the end of the current data have never been
1300
     * written, then zero those bytes in order to avoid memory check reports of
1301
     * the use of uninitialized (or uninitialised as Julian writes) bytes by
1302
     * the longest match routines.  Update the high water mark for the next
1303
     * time through here.  WIN_INIT is set to STD_MAX_MATCH since the longest match
1304
     * routines allow scanning to strstart + STD_MAX_MATCH, ignoring lookahead.
1305
     */
1306
    if (s->high_water < s->window_size) {
149,226,828✔
1307
        unsigned int curr = s->strstart + s->lookahead;
90,414,919✔
1308
        unsigned int init;
34,783,516✔
1309

1310
        if (s->high_water < curr) {
90,414,919✔
1311
            /* Previous high water mark below current data -- zero WIN_INIT
1312
             * bytes or up to end of window, whichever is less.
1313
             */
1314
            init = s->window_size - curr;
119,645✔
1315
            if (init > WIN_INIT)
119,645✔
1316
                init = WIN_INIT;
19,660✔
1317
            memset(window + curr, 0, init);
119,645✔
1318
            s->high_water = curr + init;
119,645✔
1319
        } else if (s->high_water < curr + WIN_INIT) {
90,319,056✔
1320
            /* High water mark at or above current data, but below current data
1321
             * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1322
             * to end of window, whichever is less.
1323
             */
1324
            init = curr + WIN_INIT - s->high_water;
57,069,903✔
1325
            if (init > s->window_size - s->high_water)
57,069,903✔
1326
                init = s->window_size - s->high_water;
×
1327
            memset(window + s->high_water, 0, init);
57,069,903✔
1328
            s->high_water += init;
58,187,427✔
1329
        }
11,030,584✔
1330
    }
19,798,097✔
1331

1332
    Assert((unsigned long)s->strstart <= s->window_size - MIN_LOOKAHEAD,
2,376,518✔
1333
           "not enough room for search");
26,690,956✔
1334
}
147,123,886✔
1335

1336
#ifndef ZLIB_COMPAT
1337
/* =========================================================================
1338
 * Checks whether buffer size is sufficient and whether this parameter is a duplicate.
1339
 */
1340
static int32_t deflateSetParamPre(zng_deflate_param_value **out, size_t min_size, zng_deflate_param_value *param) {
160✔
1341
    int32_t buf_error = param->size < min_size;
380✔
1342

1343
    if (*out != NULL) {
380✔
1344
        (*out)->status = Z_BUF_ERROR;
×
1345
        buf_error = 1;
1346
    }
1347
    *out = param;
128✔
1348
    return buf_error;
208✔
1349
}
80✔
1350

1351
/* ========================================================================= */
1352
int32_t Z_EXPORT zng_deflateSetParams(zng_stream *strm, zng_deflate_param_value *params, size_t count) {
190✔
1353
    size_t i;
76✔
1354
    deflate_state *s;
76✔
1355
    zng_deflate_param_value *new_level = NULL;
80✔
1356
    zng_deflate_param_value *new_strategy = NULL;
80✔
1357
    zng_deflate_param_value *new_reproducible = NULL;
80✔
1358
    int param_buf_error;
76✔
1359
    int version_error = 0;
80✔
1360
    int buf_error = 0;
80✔
1361
    int stream_error = 0;
80✔
1362

1363
    /* Initialize the statuses. */
1364
    for (i = 0; i < count; i++)
562✔
1365
        params[i].status = Z_OK;
380✔
1366

1367
    /* Check whether the stream state is consistent. */
1368
    if (deflateStateCheck(strm))
190✔
1369
        return Z_STREAM_ERROR;
1370
    s = strm->state;
190✔
1371

1372
    /* Check buffer sizes and detect duplicates. */
1373
    for (i = 0; i < count; i++) {
570✔
1374
        switch (params[i].param) {
380✔
1375
            case Z_DEFLATE_LEVEL:
150✔
1376
                param_buf_error = deflateSetParamPre(&new_level, sizeof(int), &params[i]);
80✔
1377
                break;
64✔
1378
            case Z_DEFLATE_STRATEGY:
150✔
1379
                param_buf_error = deflateSetParamPre(&new_strategy, sizeof(int), &params[i]);
80✔
1380
                break;
64✔
1381
            case Z_DEFLATE_REPRODUCIBLE:
×
1382
                param_buf_error = deflateSetParamPre(&new_reproducible, sizeof(int), &params[i]);
×
1383
                break;
1384
            default:
1385
                params[i].status = Z_VERSION_ERROR;
×
1386
                version_error = 1;
1387
                param_buf_error = 0;
1388
                break;
1389
        }
1390
        if (param_buf_error) {
380✔
1391
            params[i].status = Z_BUF_ERROR;
×
1392
            buf_error = 1;
1393
        }
1394
    }
80✔
1395
    /* Exit early if small buffers or duplicates are detected. */
1396
    if (buf_error)
190✔
1397
        return Z_BUF_ERROR;
1398

1399
    /* Apply changes, remember if there were errors. */
1400
    if (new_level != NULL || new_strategy != NULL) {
190✔
1401
        int ret = PREFIX(deflateParams)(strm, new_level == NULL ? s->level : *(int *)new_level->buf,
356✔
1402
                                        new_strategy == NULL ? s->strategy : *(int *)new_strategy->buf);
190✔
1403
        if (ret != Z_OK) {
190✔
1404
            if (new_level != NULL)
×
1405
                new_level->status = Z_STREAM_ERROR;
×
1406
            if (new_strategy != NULL)
×
1407
                new_strategy->status = Z_STREAM_ERROR;
×
1408
            stream_error = 1;
1409
        }
1410
    }
40✔
1411
    if (new_reproducible != NULL) {
190✔
1412
        int val = *(int *)new_reproducible->buf;
×
1413
        if (DEFLATE_CAN_SET_REPRODUCIBLE(strm, val)) {
1414
            s->reproducible = val;
×
1415
        } else {
1416
            new_reproducible->status = Z_STREAM_ERROR;
1417
            stream_error = 1;
1418
        }
1419
    }
1420

1421
    /* Report version errors only if there are no real errors. */
1422
    return stream_error ? Z_STREAM_ERROR : (version_error ? Z_VERSION_ERROR : Z_OK);
190✔
1423
}
40✔
1424

1425
/* ========================================================================= */
1426
int32_t Z_EXPORT zng_deflateGetParams(zng_stream *strm, zng_deflate_param_value *params, size_t count) {
190✔
1427
    deflate_state *s;
76✔
1428
    size_t i;
76✔
1429
    int32_t buf_error = 0;
80✔
1430
    int32_t version_error = 0;
80✔
1431

1432
    /* Initialize the statuses. */
1433
    for (i = 0; i < count; i++)
562✔
1434
        params[i].status = Z_OK;
380✔
1435

1436
    /* Check whether the stream state is consistent. */
1437
    if (deflateStateCheck(strm))
190✔
1438
        return Z_STREAM_ERROR;
1439
    s = strm->state;
190✔
1440

1441
    for (i = 0; i < count; i++) {
570✔
1442
        switch (params[i].param) {
380✔
1443
            case Z_DEFLATE_LEVEL:
150✔
1444
                if (params[i].size < sizeof(int))
190✔
1445
                    params[i].status = Z_BUF_ERROR;
×
1446
                else
1447
                    *(int *)params[i].buf = s->level;
190✔
1448
                break;
40✔
1449
            case Z_DEFLATE_STRATEGY:
150✔
1450
                if (params[i].size < sizeof(int))
190✔
1451
                    params[i].status = Z_BUF_ERROR;
×
1452
                else
1453
                    *(int *)params[i].buf = s->strategy;
190✔
1454
                break;
40✔
1455
            case Z_DEFLATE_REPRODUCIBLE:
1456
                if (params[i].size < sizeof(int))
×
1457
                    params[i].status = Z_BUF_ERROR;
×
1458
                else
1459
                    *(int *)params[i].buf = s->reproducible;
×
1460
                break;
1461
            default:
1462
                params[i].status = Z_VERSION_ERROR;
×
1463
                version_error = 1;
1464
                break;
1465
        }
1466
        if (params[i].status == Z_BUF_ERROR)
380✔
1467
            buf_error = 1;
1468
    }
80✔
1469
    return buf_error ? Z_BUF_ERROR : (version_error ? Z_VERSION_ERROR : Z_OK);
190✔
1470
}
40✔
1471
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc