zlib-streams 1.0.7 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/Makefile +21 -8
  2. package/dist/zlib-streams-dev.wasm +0 -0
  3. package/dist/zlib-streams.js +135 -62
  4. package/dist/zlib-streams.wasm +0 -0
  5. package/package.json +2 -2
  6. package/src/README.md +61 -1
  7. package/src/chunkcopy.h +565 -0
  8. package/src/compare256.h +56 -0
  9. package/src/cpu_features.h +22 -0
  10. package/src/deflate.c +2349 -0
  11. package/src/deflate.h +420 -0
  12. package/src/deflate_crc32.c +22 -0
  13. package/src/inffast.h +26 -0
  14. package/src/{inffast.c → inffast_chunk.c} +127 -71
  15. package/src/inffast_chunk.h +42 -0
  16. package/src/inflate.c +69 -33
  17. package/src/insert_string.h +80 -0
  18. package/src/wasm/api/zlib-streams.js +135 -62
  19. package/build/debug/src/inffast.d +0 -8
  20. package/build/debug/src/inffast.o +0 -0
  21. package/build/debug/src/inflate.d +0 -8
  22. package/build/debug/src/inflate.o +0 -0
  23. package/build/debug/src/inftrees.d +0 -6
  24. package/build/debug/src/inftrees.o +0 -0
  25. package/build/debug/src/zlib/adler32.d +0 -5
  26. package/build/debug/src/zlib/adler32.o +0 -0
  27. package/build/debug/src/zlib/contrib/infback9/infback9.d +0 -13
  28. package/build/debug/src/zlib/contrib/infback9/infback9.o +0 -0
  29. package/build/debug/src/zlib/contrib/infback9/inftree9.d +0 -7
  30. package/build/debug/src/zlib/contrib/infback9/inftree9.o +0 -0
  31. package/build/debug/src/zlib/crc32.d +0 -6
  32. package/build/debug/src/zlib/crc32.o +0 -0
  33. package/build/debug/src/zlib/trees.d +0 -7
  34. package/build/debug/src/zlib/trees.o +0 -0
  35. package/build/debug/src/zlib/zutil.d +0 -6
  36. package/build/debug/src/zlib/zutil.o +0 -0
  37. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.log +0 -34
  38. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.out +0 -100000
  39. package/tmp/all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.log +0 -2
  40. package/tmp/all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.out +0 -10000
  41. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.log +0 -1
  42. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.out +0 -0
  43. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.log +0 -1
  44. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.out +0 -0
  45. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.log +0 -2
  46. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.out +0 -0
  47. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.log +0 -1
  48. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.out +0 -0
  49. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.log +0 -1
  50. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.out +0 -0
  51. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.log +0 -1
  52. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.out +0 -0
  53. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.log +0 -2
  54. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.out +0 -0
  55. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.log +0 -2
  56. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.out +0 -0
  57. package/tmp/all_runs/payload_decompress_ref_debug__100k_lines.deflate64.log +0 -1
  58. package/tmp/all_runs/payload_decompress_ref_debug__100k_lines.deflate64.out +0 -100000
  59. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.log +0 -1
  60. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.out +0 -10000
  61. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.log +0 -1
  62. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.out +0 -0
  63. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.log +0 -1
  64. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.out +0 -0
  65. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.log +0 -1
  66. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.out +0 -0
  67. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.log +0 -1
  68. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.out +0 -0
  69. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.log +0 -1
  70. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.out +0 -0
  71. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.log +0 -1
  72. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.out +0 -0
  73. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.log +0 -1
  74. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.out +0 -0
  75. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.log +0 -1
  76. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.out +0 -0
  77. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.log +0 -34
  78. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.out +0 -100000
  79. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.log +0 -2
  80. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.out +0 -10000
  81. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.log +0 -1
  82. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.out +0 -0
  83. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.log +0 -1
  84. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.out +0 -0
  85. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.log +0 -2
  86. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.out +0 -0
  87. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.log +0 -1
  88. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.out +0 -0
  89. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.log +0 -1
  90. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.out +0 -0
  91. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.log +0 -1
  92. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.out +0 -0
  93. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.log +0 -2
  94. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.out +0 -0
  95. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.log +0 -2
  96. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.out +0 -0
  97. package/tmp/all_runs/wasm__100k_lines.deflate64.out +0 -100000
  98. package/tmp/all_runs/wasm__10k_lines.deflate64.out +0 -10000
  99. package/tmp/all_runs/wasm__payload_63k.deflate64.out +0 -0
  100. package/tmp/all_runs/wasm__payload_64k.deflate64.out +0 -0
  101. package/tmp/all_runs/wasm__payload_65k.deflate64.out +0 -0
  102. package/tmp/all_runs/wasm__rand_block_gap.deflate64.out +0 -0
  103. package/tmp/all_runs/wasm__repeat_63k.deflate64.out +0 -0
  104. package/tmp/all_runs/wasm__repeat_64k.deflate64.out +0 -0
  105. package/tmp/all_runs/wasm__repeat_65k.deflate64.out +0 -0
  106. package/tmp/all_runs/wasm__zeros_100k.deflate64.out +0 -0
  107. package/tmp/diagnose/diagnose_stream_mem.json +0 -4202
package/src/deflate.c ADDED
@@ -0,0 +1,2349 @@
1
+ /* deflate.c -- compress data using the deflation algorithm
2
+ * Copyright (C) 1995-2026 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 at https://datatracker.ietf.org/doc/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
+ /* @(#) $Id$ */
51
+ #include <assert.h>
52
+ #include "deflate.h"
53
+
54
+ #include "cpu_features.h"
55
+
56
+ #if defined(DEFLATE_SLIDE_HASH_SSE2) || defined(DEFLATE_SLIDE_HASH_NEON)
57
+ #include "slide_hash_simd.h"
58
+ #endif
59
+
60
+ #include "compare256.h"
61
+
62
+ #if defined(QAT_COMPRESSION_ENABLED)
63
+ #include "contrib/qat/deflate_qat.h"
64
+ #endif
65
+
66
+ #include "insert_string.h"
67
+
68
+ #ifdef FASTEST
69
+ /* See http://crbug.com/1113596 */
70
+ #error "FASTEST is not supported in Chromium's zlib."
71
+ #endif
72
+
73
+ const char deflate_copyright[] =
74
+ " deflate 1.3.2.1 Copyright 1995-2026 Jean-loup Gailly and Mark Adler ";
75
+ /*
76
+ If you use the zlib library in a product, an acknowledgment is welcome
77
+ in the documentation of your product. If for some reason you cannot
78
+ include such an acknowledgment, I would appreciate that you keep this
79
+ copyright string in the executable of your product.
80
+ */
81
+
82
+ typedef enum {
83
+ need_more, /* block not completed, need more input or more output */
84
+ block_done, /* block flush performed */
85
+ finish_started, /* finish started, need only more output at next deflate */
86
+ finish_done /* finish done, accept no more input or output */
87
+ } block_state;
88
+
89
+ typedef block_state (*compress_func)(deflate_state *s, int flush);
90
+ /* Compression function. Returns the block state after the call. */
91
+
92
+ local block_state deflate_stored(deflate_state *s, int flush);
93
+ local block_state deflate_fast(deflate_state *s, int flush);
94
+ #ifndef FASTEST
95
+ local block_state deflate_slow(deflate_state *s, int flush);
96
+ #endif
97
+ local block_state deflate_rle(deflate_state *s, int flush);
98
+ local block_state deflate_huff(deflate_state *s, int flush);
99
+
100
+ /* From crc32.c */
101
+ extern void ZLIB_INTERNAL crc_reset(deflate_state *const s);
102
+ extern void ZLIB_INTERNAL crc_finalize(deflate_state *const s);
103
+ extern void ZLIB_INTERNAL copy_with_crc(z_streamp strm, Bytef *dst, long size);
104
+
105
+ /* ===========================================================================
106
+ * Local data
107
+ */
108
+
109
+ #define NIL 0
110
+ /* Tail of hash chains */
111
+
112
+ #ifndef TOO_FAR
113
+ # define TOO_FAR 4096
114
+ #endif
115
+ /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
116
+
117
+ /* Values for max_lazy_match, good_match and max_chain_length, depending on
118
+ * the desired pack level (0..9). The values given below have been tuned to
119
+ * exclude worst case performance for pathological files. Better values may be
120
+ * found for specific files.
121
+ */
122
+ typedef struct config_s {
123
+ ush good_length; /* reduce lazy search above this match length */
124
+ ush max_lazy; /* do not perform lazy search above this match length */
125
+ ush nice_length; /* quit search above this match length */
126
+ ush max_chain;
127
+ compress_func func;
128
+ } config;
129
+
130
+ #ifdef FASTEST
131
+ local const config configuration_table[2] = {
132
+ /* good lazy nice chain */
133
+ /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
134
+ /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
135
+ #else
136
+ local const config configuration_table[10] = {
137
+ /* good lazy nice chain */
138
+ /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
139
+ /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
140
+ /* 2 */ {4, 5, 16, 8, deflate_fast},
141
+ /* 3 */ {4, 6, 32, 32, deflate_fast},
142
+
143
+ /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
144
+ /* 5 */ {8, 16, 32, 32, deflate_slow},
145
+ /* 6 */ {8, 16, 128, 128, deflate_slow},
146
+ /* 7 */ {8, 32, 128, 256, deflate_slow},
147
+ /* 8 */ {32, 128, 258, 1024, deflate_slow},
148
+ /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
149
+ #endif
150
+
151
+ /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
152
+ * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
153
+ * meaning.
154
+ */
155
+
156
+ /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
157
+ #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
158
+
159
+ /* ===========================================================================
160
+ * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
161
+ * prev[] will be initialized on the fly.
162
+ * TODO(cavalcantii): optimization opportunity, check comments on:
163
+ * https://chromium-review.googlesource.com/c/chromium/src/+/3561506/
164
+ */
165
+ #define CLEAR_HASH(s) \
166
+ do { \
167
+ s->head[s->hash_size - 1] = NIL; \
168
+ zmemzero(s->head, (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
169
+ s->slid = 0; \
170
+ } while (0)
171
+
172
+ /* ===========================================================================
173
+ * Slide the hash table when sliding the window down (could be avoided with 32
174
+ * bit values at the expense of memory usage). We slide even when level == 0 to
175
+ * keep the hash table consistent if we switch back to level > 0 later.
176
+ */
177
+ #if defined(__has_feature)
178
+ # if __has_feature(memory_sanitizer)
179
+ __attribute__((no_sanitize("memory")))
180
+ # endif
181
+ #endif
182
+ local void slide_hash(deflate_state *s) {
183
+ #if defined(DEFLATE_SLIDE_HASH_SSE2) || defined(DEFLATE_SLIDE_HASH_NEON)
184
+ slide_hash_simd(s->head, s->prev, s->w_size, s->hash_size);
185
+ s->slid = 1;
186
+ return;
187
+ #endif
188
+
189
+ unsigned n, m;
190
+ Posf *p;
191
+ uInt wsize = s->w_size;
192
+
193
+ n = s->hash_size;
194
+ p = &s->head[n];
195
+ do {
196
+ m = *--p;
197
+ *p = (Pos)(m >= wsize ? m - wsize : NIL);
198
+ } while (--n);
199
+ #ifndef FASTEST
200
+ n = wsize;
201
+ p = &s->prev[n];
202
+ do {
203
+ m = *--p;
204
+ *p = (Pos)(m >= wsize ? m - wsize : NIL);
205
+ /* If n is not on any hash chain, prev[n] is garbage but
206
+ * its value will never be used.
207
+ */
208
+ } while (--n);
209
+ #endif
210
+ s->slid = 1;
211
+ }
212
+
213
+ /* ===========================================================================
214
+ * Read a new buffer from the current input stream, update the adler32
215
+ * and total number of bytes read. All deflate() input goes through
216
+ * this function so some applications may wish to modify it to avoid
217
+ * allocating a large strm->next_in buffer and copying from it.
218
+ * (See also flush_pending()).
219
+ */
220
+ local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size) {
221
+ unsigned len = strm->avail_in;
222
+
223
+ if (len > size) len = size;
224
+ if (len == 0) return 0;
225
+
226
+ strm->avail_in -= len;
227
+
228
+ /* TODO(cavalcantii): verify if we can remove 'copy_with_crc', it is legacy
229
+ * of the Intel optimizations dating back to 2015.
230
+ */
231
+ #ifdef GZIP
232
+ if (strm->state->wrap == 2)
233
+ copy_with_crc(strm, buf, len);
234
+ else
235
+ #endif
236
+ {
237
+ zmemcpy(buf, strm->next_in, len);
238
+ if (strm->state->wrap == 1)
239
+ strm->adler = adler32(strm->adler, buf, len);
240
+ }
241
+ strm->next_in += len;
242
+ strm->total_in += len;
243
+
244
+ return len;
245
+ }
246
+
247
+ /* ===========================================================================
248
+ * Fill the window when the lookahead becomes insufficient.
249
+ * Updates strstart and lookahead.
250
+ *
251
+ * IN assertion: lookahead < MIN_LOOKAHEAD
252
+ * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
253
+ * At least one byte has been read, or avail_in == 0; reads are
254
+ * performed for at least two bytes (required for the zip translate_eol
255
+ * option -- not supported here).
256
+ */
257
+ local void fill_window(deflate_state *s) {
258
+ unsigned n;
259
+ unsigned more; /* Amount of free space at the end of the window. */
260
+ uInt wsize = s->w_size;
261
+
262
+ Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
263
+
264
+ do {
265
+ more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
266
+
267
+ /* Deal with !@#$% 64K limit: */
268
+ #ifdef _MSC_VER
269
+ #pragma warning(push)
270
+ #pragma warning(disable: 4127)
271
+ #endif
272
+ if (sizeof(int) <= 2) {
273
+ #ifdef _MSC_VER
274
+ #pragma warning(pop)
275
+ #endif
276
+ if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
277
+ more = wsize;
278
+
279
+ } else if (more == (unsigned)(-1)) {
280
+ /* Very unlikely, but possible on 16 bit machine if
281
+ * strstart == 0 && lookahead == 1 (input done a byte at time)
282
+ */
283
+ more--;
284
+ }
285
+ }
286
+
287
+ /* If the window is almost full and there is insufficient lookahead,
288
+ * move the upper half to the lower one to make room in the upper half.
289
+ */
290
+ if (s->strstart >= wsize + MAX_DIST(s)) {
291
+
292
+ zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more);
293
+ s->match_start -= wsize;
294
+ s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
295
+ s->block_start -= (long) wsize;
296
+ if (s->insert > s->strstart)
297
+ s->insert = s->strstart;
298
+ slide_hash(s);
299
+ more += wsize;
300
+ }
301
+ if (s->strm->avail_in == 0) break;
302
+
303
+ /* If there was no sliding:
304
+ * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
305
+ * more == window_size - lookahead - strstart
306
+ * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
307
+ * => more >= window_size - 2*WSIZE + 2
308
+ * In the BIG_MEM or MMAP case (not yet supported),
309
+ * window_size == input_size + MIN_LOOKAHEAD &&
310
+ * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
311
+ * Otherwise, window_size == 2*WSIZE so more >= 2.
312
+ * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
313
+ */
314
+ Assert(more >= 2, "more < 2");
315
+
316
+ n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
317
+ s->lookahead += n;
318
+
319
+ /* Initialize the hash value now that we have some input: */
320
+ if (s->chromium_zlib_hash) {
321
+ /* chromium hash reads 4 bytes */
322
+ if (s->lookahead + s->insert > MIN_MATCH) {
323
+ uInt str = s->strstart - s->insert;
324
+ while (s->insert) {
325
+ insert_string(s, str);
326
+ str++;
327
+ s->insert--;
328
+ if (s->lookahead + s->insert <= MIN_MATCH)
329
+ break;
330
+ }
331
+ }
332
+ } else
333
+ /* Initialize the hash value now that we have some input: */
334
+ if (s->lookahead + s->insert >= MIN_MATCH) {
335
+ uInt str = s->strstart - s->insert;
336
+ s->ins_h = s->window[str];
337
+ UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
338
+ #if MIN_MATCH != 3
339
+ Call UPDATE_HASH() MIN_MATCH-3 more times
340
+ #endif
341
+ while (s->insert) {
342
+ UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
343
+ #ifndef FASTEST
344
+ s->prev[str & s->w_mask] = s->head[s->ins_h];
345
+ #endif
346
+ s->head[s->ins_h] = (Pos)str;
347
+ str++;
348
+ s->insert--;
349
+ if (s->lookahead + s->insert < MIN_MATCH)
350
+ break;
351
+ }
352
+ }
353
+ /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
354
+ * but this is not important since only literal bytes will be emitted.
355
+ */
356
+
357
+ } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
358
+
359
+ /* If the WIN_INIT bytes after the end of the current data have never been
360
+ * written, then zero those bytes in order to avoid memory check reports of
361
+ * the use of uninitialized (or uninitialised as Julian writes) bytes by
362
+ * the longest match routines. Update the high water mark for the next
363
+ * time through here. WIN_INIT is set to MAX_MATCH since the longest match
364
+ * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
365
+ */
366
+ if (s->high_water < s->window_size) {
367
+ ulg curr = s->strstart + (ulg)(s->lookahead);
368
+ ulg init;
369
+
370
+ if (s->high_water < curr) {
371
+ /* Previous high water mark below current data -- zero WIN_INIT
372
+ * bytes or up to end of window, whichever is less.
373
+ */
374
+ init = s->window_size - curr;
375
+ if (init > WIN_INIT)
376
+ init = WIN_INIT;
377
+ zmemzero(s->window + curr, (unsigned)init);
378
+ s->high_water = curr + init;
379
+ }
380
+ else if (s->high_water < (ulg)curr + WIN_INIT) {
381
+ /* High water mark at or above current data, but below current data
382
+ * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
383
+ * to end of window, whichever is less.
384
+ */
385
+ init = (ulg)curr + WIN_INIT - s->high_water;
386
+ if (init > s->window_size - s->high_water)
387
+ init = s->window_size - s->high_water;
388
+ zmemzero(s->window + s->high_water, (unsigned)init);
389
+ s->high_water += init;
390
+ }
391
+ }
392
+
393
+ Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
394
+ "not enough room for search");
395
+ }
396
+
397
+ /* ========================================================================= */
398
+ int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version,
399
+ int stream_size) {
400
+ return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
401
+ Z_DEFAULT_STRATEGY, version, stream_size);
402
+ /* To do: ignore strm->next_in if we use it as window */
403
+ }
404
+
405
+ #define WINDOW_PADDING 8
406
+
407
+ /* ========================================================================= */
408
+ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
409
+ int windowBits, int memLevel, int strategy,
410
+ const char *version, int stream_size) {
411
+ deflate_state *s;
412
+ int wrap = 1;
413
+ static const char my_version[] = ZLIB_VERSION;
414
+
415
+ // Needed to activate optimized insert_string() that helps compression
416
+ // for all wrapper formats (e.g. RAW, ZLIB, GZIP).
417
+ // Feature detection is not triggered while using RAW mode (i.e. we never
418
+ // call crc32() with a NULL buffer).
419
+ #if defined(CRC32_ARMV8_CRC32) || defined(CRC32_SIMD_SSE42_PCLMUL) \
420
+ || defined(RISCV_RVV)
421
+ cpu_check_features();
422
+ #endif
423
+
424
+ if (version == Z_NULL || version[0] != my_version[0] ||
425
+ stream_size != sizeof(z_stream)) {
426
+ return Z_VERSION_ERROR;
427
+ }
428
+ if (strm == Z_NULL) return Z_STREAM_ERROR;
429
+
430
+ strm->msg = Z_NULL;
431
+ if (strm->zalloc == (alloc_func)0) {
432
+ #ifdef Z_SOLO
433
+ return Z_STREAM_ERROR;
434
+ #else
435
+ strm->zalloc = zcalloc;
436
+ strm->opaque = (voidpf)0;
437
+ #endif
438
+ }
439
+ if (strm->zfree == (free_func)0)
440
+ #ifdef Z_SOLO
441
+ return Z_STREAM_ERROR;
442
+ #else
443
+ strm->zfree = zcfree;
444
+ #endif
445
+
446
+ #ifdef FASTEST
447
+ if (level != 0) level = 1;
448
+ #else
449
+ if (level == Z_DEFAULT_COMPRESSION) level = 6;
450
+ #endif
451
+
452
+ if (windowBits < 0) { /* suppress zlib wrapper */
453
+ wrap = 0;
454
+ if (windowBits < -15)
455
+ return Z_STREAM_ERROR;
456
+ windowBits = -windowBits;
457
+ }
458
+ #ifdef GZIP
459
+ else if (windowBits > 15) {
460
+ wrap = 2; /* write gzip wrapper instead */
461
+ windowBits -= 16;
462
+ }
463
+ #endif
464
+ if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
465
+ windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
466
+ strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
467
+ return Z_STREAM_ERROR;
468
+ }
469
+ if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
470
+ s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
471
+ if (s == Z_NULL) return Z_MEM_ERROR;
472
+ zmemzero(s, sizeof(deflate_state));
473
+ strm->state = (struct internal_state FAR *)s;
474
+ s->strm = strm;
475
+ s->status = INIT_STATE; /* to pass state test in deflateReset() */
476
+
477
+ s->wrap = wrap;
478
+ s->gzhead = Z_NULL;
479
+ s->w_bits = (uInt)windowBits;
480
+ s->w_size = 1 << s->w_bits;
481
+ s->w_mask = s->w_size - 1;
482
+
483
+ s->chromium_zlib_hash = 1;
484
+ #if defined(USE_ZLIB_RABIN_KARP_ROLLING_HASH)
485
+ s->chromium_zlib_hash = 0;
486
+ #endif
487
+
488
+ s->hash_bits = memLevel + 7;
489
+ if (s->chromium_zlib_hash && s->hash_bits < 15) {
490
+ s->hash_bits = 15;
491
+ }
492
+
493
+ s->hash_size = 1 << s->hash_bits;
494
+ s->hash_mask = s->hash_size - 1;
495
+ s->hash_shift = ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH);
496
+
497
+ s->window = (Bytef *) ZALLOC(strm,
498
+ s->w_size + WINDOW_PADDING,
499
+ 2*sizeof(Byte));
500
+ s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
501
+ s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
502
+
503
+ s->high_water = 0; /* nothing written to s->window yet */
504
+
505
+ s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
506
+
507
+ /* We overlay pending_buf and sym_buf. This works since the average size
508
+ * for length/distance pairs over any compressed block is assured to be 31
509
+ * bits or less.
510
+ *
511
+ * Analysis: The longest fixed codes are a length code of 8 bits plus 5
512
+ * extra bits, for lengths 131 to 257. The longest fixed distance codes are
513
+ * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
514
+ * possible fixed-codes length/distance pair is then 31 bits total.
515
+ *
516
+ * sym_buf starts one-fourth of the way into pending_buf. So there are
517
+ * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
518
+ * in sym_buf is three bytes -- two for the distance and one for the
519
+ * literal/length. As each symbol is consumed, the pointer to the next
520
+ * sym_buf value to read moves forward three bytes. From that symbol, up to
521
+ * 31 bits are written to pending_buf. The closest the written pending_buf
522
+ * bits gets to the next sym_buf symbol to read is just before the last
523
+ * code is written. At that time, 31*(n - 2) bits have been written, just
524
+ * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at
525
+ * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1
526
+ * symbols are written.) The closest the writing gets to what is unread is
527
+ * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and
528
+ * can range from 128 to 32768.
529
+ *
530
+ * Therefore, at a minimum, there are 142 bits of space between what is
531
+ * written and what is read in the overlain buffers, so the symbols cannot
532
+ * be overwritten by the compressed data. That space is actually 139 bits,
533
+ * due to the three-bit fixed-code block header.
534
+ *
535
+ * That covers the case where either Z_FIXED is specified, forcing fixed
536
+ * codes, or when the use of fixed codes is chosen, because that choice
537
+ * results in a smaller compressed block than dynamic codes. That latter
538
+ * condition then assures that the above analysis also covers all dynamic
539
+ * blocks. A dynamic-code block will only be chosen to be emitted if it has
540
+ * fewer bits than a fixed-code block would for the same set of symbols.
541
+ * Therefore its average symbol length is assured to be less than 31. So
542
+ * the compressed data for a dynamic block also cannot overwrite the
543
+ * symbols from which it is being constructed.
544
+ */
545
+ #ifdef LIT_MEM
546
+ s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 5);
547
+ #else
548
+ s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
549
+ #endif
550
+ s->pending_buf_size = (ulg)s->lit_bufsize * 4;
551
+
552
+ if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
553
+ s->pending_buf == Z_NULL) {
554
+ s->status = FINISH_STATE;
555
+ strm->msg = ERR_MSG(Z_MEM_ERROR);
556
+ deflateEnd (strm);
557
+ return Z_MEM_ERROR;
558
+ }
559
+ /* Avoid use of unitialized values in the window, see crbug.com/1137613 and
560
+ * crbug.com/1144420 */
561
+ zmemzero(s->window, (s->w_size + WINDOW_PADDING) * (2 * sizeof(Byte)));
562
+ /* Avoid use of uninitialized value, see:
563
+ * https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11360
564
+ */
565
+ zmemzero(s->prev, s->w_size * sizeof(Pos));
566
+ #ifdef LIT_MEM
567
+ s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
568
+ s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
569
+ s->sym_end = s->lit_bufsize - 1;
570
+ #else
571
+ s->sym_buf = s->pending_buf + s->lit_bufsize;
572
+ s->sym_end = (s->lit_bufsize - 1) * 3;
573
+ #endif
574
+ /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
575
+ * on 16 bit machines and because stored blocks are restricted to
576
+ * 64K-1 bytes.
577
+ */
578
+
579
+ s->level = level;
580
+ s->strategy = strategy;
581
+ s->method = (Byte)method;
582
+
583
+ #if defined(QAT_COMPRESSION_ENABLED)
584
+ s->qat_s = NULL;
585
+ if (s->level && qat_deflate_init() == Z_OK) {
586
+ s->qat_s = qat_deflate_state_init(s->level, s->wrap);
587
+ }
588
+ #endif
589
+
590
+ return deflateReset(strm);
591
+ }
592
+
593
+ /* =========================================================================
594
+ * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
595
+ */
596
+ local int deflateStateCheck(z_streamp strm) {
597
+ deflate_state *s;
598
+ if (strm == Z_NULL ||
599
+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
600
+ return 1;
601
+ s = strm->state;
602
+ if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
603
+ #ifdef GZIP
604
+ s->status != GZIP_STATE &&
605
+ #endif
606
+ s->status != EXTRA_STATE &&
607
+ s->status != NAME_STATE &&
608
+ s->status != COMMENT_STATE &&
609
+ s->status != HCRC_STATE &&
610
+ s->status != BUSY_STATE &&
611
+ s->status != FINISH_STATE))
612
+ return 1;
613
+ return 0;
614
+ }
615
+
616
+ /* ========================================================================= */
617
+ int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary,
618
+ uInt dictLength) {
619
+ deflate_state *s;
620
+ uInt str, n;
621
+ int wrap;
622
+ unsigned avail;
623
+ z_const unsigned char *next;
624
+
625
+ if (deflateStateCheck(strm) || dictionary == Z_NULL)
626
+ return Z_STREAM_ERROR;
627
+ s = strm->state;
628
+ wrap = s->wrap;
629
+ if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
630
+ return Z_STREAM_ERROR;
631
+
632
+ /* when using zlib wrappers, compute Adler-32 for provided dictionary */
633
+ if (wrap == 1)
634
+ strm->adler = adler32(strm->adler, dictionary, dictLength);
635
+ s->wrap = 0; /* avoid computing Adler-32 in read_buf */
636
+
637
+ /* if dictionary would fill window, just replace the history */
638
+ if (dictLength >= s->w_size) {
639
+ if (wrap == 0) { /* already empty otherwise */
640
+ CLEAR_HASH(s);
641
+ s->strstart = 0;
642
+ s->block_start = 0L;
643
+ s->insert = 0;
644
+ }
645
+ dictionary += dictLength - s->w_size; /* use the tail */
646
+ dictLength = s->w_size;
647
+ }
648
+
649
+ /* insert dictionary into window and hash */
650
+ avail = strm->avail_in;
651
+ next = strm->next_in;
652
+ strm->avail_in = dictLength;
653
+ strm->next_in = (z_const Bytef *)dictionary;
654
+ fill_window(s);
655
+ while (s->lookahead >= MIN_MATCH) {
656
+ str = s->strstart;
657
+ n = s->lookahead - (MIN_MATCH-1);
658
+ do {
659
+ insert_string(s, str);
660
+ str++;
661
+ } while (--n);
662
+ s->strstart = str;
663
+ s->lookahead = MIN_MATCH-1;
664
+ fill_window(s);
665
+ }
666
+ s->strstart += s->lookahead;
667
+ s->block_start = (long)s->strstart;
668
+ s->insert = s->lookahead;
669
+ s->lookahead = 0;
670
+ s->match_length = s->prev_length = MIN_MATCH-1;
671
+ s->match_available = 0;
672
+ strm->next_in = next;
673
+ strm->avail_in = avail;
674
+ s->wrap = wrap;
675
+ return Z_OK;
676
+ }
677
+
678
+ /* ========================================================================= */
679
+ int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary,
680
+ uInt *dictLength) {
681
+ deflate_state *s;
682
+ uInt len;
683
+
684
+ if (deflateStateCheck(strm))
685
+ return Z_STREAM_ERROR;
686
+ s = strm->state;
687
+ len = s->strstart + s->lookahead;
688
+ if (len > s->w_size)
689
+ len = s->w_size;
690
+ if (dictionary != Z_NULL && len)
691
+ zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
692
+ if (dictLength != Z_NULL)
693
+ *dictLength = len;
694
+ return Z_OK;
695
+ }
696
+
697
+ /* ========================================================================= */
698
+ int ZEXPORT deflateResetKeep(z_streamp strm) {
699
+ deflate_state *s;
700
+
701
+ if (deflateStateCheck(strm)) {
702
+ return Z_STREAM_ERROR;
703
+ }
704
+
705
+ strm->total_in = strm->total_out = 0;
706
+ strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
707
+ strm->data_type = Z_UNKNOWN;
708
+
709
+ s = (deflate_state *)strm->state;
710
+ s->pending = 0;
711
+ s->pending_out = s->pending_buf;
712
+
713
+ if (s->wrap < 0) {
714
+ s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
715
+ }
716
+ s->status =
717
+ #ifdef GZIP
718
+ s->wrap == 2 ? GZIP_STATE :
719
+ #endif
720
+ INIT_STATE;
721
+ strm->adler =
722
+ #ifdef GZIP
723
+ s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
724
+ #endif
725
+ adler32(0L, Z_NULL, 0);
726
+ s->last_flush = -2;
727
+
728
+ _tr_init(s);
729
+
730
+ return Z_OK;
731
+ }
732
+
733
+ /* ===========================================================================
734
+ * Initialize the "longest match" routines for a new zlib stream
735
+ */
736
+ local void lm_init(deflate_state *s) {
737
+ s->window_size = (ulg)2L*s->w_size;
738
+
739
+ CLEAR_HASH(s);
740
+
741
+ /* Set the default configuration parameters:
742
+ */
743
+ s->max_lazy_match = configuration_table[s->level].max_lazy;
744
+ s->good_match = configuration_table[s->level].good_length;
745
+ s->nice_match = configuration_table[s->level].nice_length;
746
+ s->max_chain_length = configuration_table[s->level].max_chain;
747
+
748
+ s->strstart = 0;
749
+ s->block_start = 0L;
750
+ s->lookahead = 0;
751
+ s->insert = 0;
752
+ s->match_length = s->prev_length = MIN_MATCH-1;
753
+ s->match_available = 0;
754
+ s->ins_h = 0;
755
+ }
756
+
757
+ /* ========================================================================= */
758
+ int ZEXPORT deflateReset(z_streamp strm) {
759
+ int ret;
760
+
761
+ ret = deflateResetKeep(strm);
762
+ if (ret == Z_OK)
763
+ lm_init(strm->state);
764
+ return ret;
765
+ }
766
+
767
+ /* ========================================================================= */
768
+ int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head) {
769
+ if (deflateStateCheck(strm) || strm->state->wrap != 2)
770
+ return Z_STREAM_ERROR;
771
+ strm->state->gzhead = head;
772
+ return Z_OK;
773
+ }
774
+
775
+ /* ========================================================================= */
776
+ int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) {
777
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
778
+ if (bits != Z_NULL)
779
+ *bits = strm->state->bi_valid;
780
+ if (pending != Z_NULL) {
781
+ *pending = (unsigned)strm->state->pending;
782
+ if (*pending != strm->state->pending) {
783
+ *pending = (unsigned)-1;
784
+ return Z_BUF_ERROR;
785
+ }
786
+ }
787
+ return Z_OK;
788
+ }
789
+
790
+ /* ========================================================================= */
791
+ int ZEXPORT deflateUsed(z_streamp strm, int *bits) {
792
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
793
+ if (bits != Z_NULL)
794
+ *bits = strm->state->bi_used;
795
+ return Z_OK;
796
+ }
797
+
798
+ /* ========================================================================= */
799
+ int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
800
+ deflate_state *s;
801
+ int put;
802
+
803
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
804
+ s = strm->state;
805
+ #ifdef LIT_MEM
806
+ if (bits < 0 || bits > 16 ||
807
+ (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3))
808
+ return Z_BUF_ERROR;
809
+ #else
810
+ if (bits < 0 || bits > 16 ||
811
+ s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
812
+ return Z_BUF_ERROR;
813
+ #endif
814
+ do {
815
+ put = Buf_size - s->bi_valid;
816
+ if (put > bits)
817
+ put = bits;
818
+ #if defined(DEFLATE_CHUNK_WRITE_64LE)
819
+ s->bi_buf |= (uint64_t)((value & ((1ULL << put) - 1)) << s->bi_valid);
820
+ #else
821
+ s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
822
+ #endif /* DEFLATE_CHUNK_WRITE_64LE */
823
+ s->bi_valid += put;
824
+ _tr_flush_bits(s);
825
+ value >>= put;
826
+ bits -= put;
827
+ } while (bits);
828
+ return Z_OK;
829
+ }
830
+
831
+ /* ========================================================================= */
832
+ int ZEXPORT deflateParams(z_streamp strm, int level, int strategy) {
833
+ deflate_state *s;
834
+ compress_func func;
835
+
836
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
837
+ s = strm->state;
838
+
839
+ #ifdef FASTEST
840
+ if (level != 0) level = 1;
841
+ #else
842
+ if (level == Z_DEFAULT_COMPRESSION) level = 6;
843
+ #endif
844
+ if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
845
+ return Z_STREAM_ERROR;
846
+ }
847
+ func = configuration_table[s->level].func;
848
+
849
+ if ((strategy != s->strategy || func != configuration_table[level].func) &&
850
+ s->last_flush != -2) {
851
+ /* Flush the last buffer: */
852
+ int err = deflate(strm, Z_BLOCK);
853
+ if (err == Z_STREAM_ERROR)
854
+ return err;
855
+ if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
856
+ return Z_BUF_ERROR;
857
+ }
858
+ if (s->level != level) {
859
+ if (s->level == 0 && s->matches != 0) {
860
+ if (s->matches == 1)
861
+ slide_hash(s);
862
+ else
863
+ CLEAR_HASH(s);
864
+ s->matches = 0;
865
+ }
866
+ s->level = level;
867
+ s->max_lazy_match = configuration_table[level].max_lazy;
868
+ s->good_match = configuration_table[level].good_length;
869
+ s->nice_match = configuration_table[level].nice_length;
870
+ s->max_chain_length = configuration_table[level].max_chain;
871
+ }
872
+ s->strategy = strategy;
873
+ return Z_OK;
874
+ }
875
+
876
+ /* ========================================================================= */
877
+ int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy,
878
+ int nice_length, int max_chain) {
879
+ deflate_state *s;
880
+
881
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
882
+ s = strm->state;
883
+ s->good_match = (uInt)good_length;
884
+ s->max_lazy_match = (uInt)max_lazy;
885
+ s->nice_match = nice_length;
886
+ s->max_chain_length = (uInt)max_chain;
887
+ return Z_OK;
888
+ }
889
+
890
+ /* =========================================================================
891
+ * For the default windowBits of 15 and memLevel of 8, this function returns a
892
+ * close to exact, as well as small, upper bound on the compressed size. This
893
+ * is an expansion of ~0.03%, plus a small constant.
894
+ *
895
+ * For any setting other than those defaults for windowBits and memLevel, one
896
+ * of two worst case bounds is returned. This is at most an expansion of ~4% or
897
+ * ~13%, plus a small constant.
898
+ *
899
+ * Both the 0.03% and 4% derive from the overhead of stored blocks. The first
900
+ * one is for stored blocks of 16383 bytes (memLevel == 8), whereas the second
901
+ * is for stored blocks of 127 bytes (the worst case memLevel == 1). The
902
+ * expansion results from five bytes of header for each stored block.
903
+ *
904
+ * The larger expansion of 13% results from a window size less than or equal to
905
+ * the symbols buffer size (windowBits <= memLevel + 7). In that case some of
906
+ * the data being compressed may have slid out of the sliding window, impeding
907
+ * a stored block from being emitted. Then the only choice is a fixed or
908
+ * dynamic block, where a fixed block limits the maximum expansion to 9 bits
909
+ * per 8-bit byte, plus 10 bits for every block. The smallest block size for
910
+ * which this can occur is 255 (memLevel == 2).
911
+ *
912
+ * Shifts are used to approximate divisions, for speed.
913
+ */
914
+ z_size_t ZEXPORT deflateBound_z(z_streamp strm, z_size_t sourceLen) {
915
+ deflate_state *s;
916
+ z_size_t fixedlen, storelen, wraplen, bound;
917
+
918
+ /* upper bound for fixed blocks with 9-bit literals and length 255
919
+ (memLevel == 2, which is the lowest that may not use stored blocks) --
920
+ ~13% overhead plus a small constant */
921
+ fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) +
922
+ (sourceLen >> 9) + 4;
923
+ if (fixedlen < sourceLen)
924
+ fixedlen = (z_size_t)-1;
925
+
926
+ /* upper bound for stored blocks with length 127 (memLevel == 1) --
927
+ ~4% overhead plus a small constant */
928
+ storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) +
929
+ (sourceLen >> 11) + 7;
930
+ if (storelen < sourceLen)
931
+ storelen = (z_size_t)-1;
932
+
933
+ /* if can't get parameters, return larger bound plus a wrapper */
934
+ if (deflateStateCheck(strm)) {
935
+ bound = fixedlen > storelen ? fixedlen : storelen;
936
+ return bound + 18 < bound ? (z_size_t)-1 : bound + 18;
937
+ }
938
+
939
+ /* compute wrapper length */
940
+ s = strm->state;
941
+ switch (s->wrap < 0 ? -s->wrap : s->wrap) {
942
+ case 0: /* raw deflate */
943
+ wraplen = 0;
944
+ break;
945
+ case 1: /* zlib wrapper */
946
+ wraplen = 6 + (s->strstart ? 4 : 0);
947
+ break;
948
+ #ifdef GZIP
949
+ case 2: /* gzip wrapper */
950
+ wraplen = 18;
951
+ if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
952
+ Bytef *str;
953
+ if (s->gzhead->extra != Z_NULL)
954
+ wraplen += 2 + s->gzhead->extra_len;
955
+ str = s->gzhead->name;
956
+ if (str != Z_NULL)
957
+ do {
958
+ wraplen++;
959
+ } while (*str++);
960
+ str = s->gzhead->comment;
961
+ if (str != Z_NULL)
962
+ do {
963
+ wraplen++;
964
+ } while (*str++);
965
+ if (s->gzhead->hcrc)
966
+ wraplen += 2;
967
+ }
968
+ break;
969
+ #endif
970
+ default: /* for compiler happiness */
971
+ wraplen = 18;
972
+ }
973
+
974
+ /* With Chromium's hashing, s->hash_bits may not correspond to the
975
+ memLevel, making the computations below incorrect. Return the
976
+ conservative bound. */
977
+ if (s->chromium_zlib_hash) {
978
+ bound = fixedlen > storelen ? fixedlen : storelen;
979
+ return bound + wraplen < bound ? (z_size_t)-1 : bound + wraplen;
980
+ }
981
+
982
+ /* if not default parameters, return one of the conservative bounds */
983
+ if (s->w_bits != 15 || s->hash_bits != 8 + 7) {
984
+ bound = s->w_bits <= s->hash_bits && s->level ? fixedlen :
985
+ storelen;
986
+ return bound + wraplen < bound ? (z_size_t)-1 : bound + wraplen;
987
+ }
988
+
989
+ /* default settings: return tight bound for that case -- ~0.03% overhead
990
+ plus a small constant */
991
+ bound = sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
992
+ (sourceLen >> 25) + 13 - 6 + wraplen;
993
+ return bound < sourceLen ? (z_size_t)-1 : bound;
994
+ }
995
+ uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) {
996
+ z_size_t bound = deflateBound_z(strm, sourceLen);
997
+ return (uLong)bound != bound ? (uLong)-1 : (uLong)bound;
998
+ }
999
+
1000
+ /* =========================================================================
1001
+ * Put a short in the pending buffer. The 16-bit value is put in MSB order.
1002
+ * IN assertion: the stream state is correct and there is enough room in
1003
+ * pending_buf.
1004
+ */
1005
+ local void putShortMSB(deflate_state *s, uInt b) {
1006
+ put_byte(s, (Byte)(b >> 8));
1007
+ put_byte(s, (Byte)(b & 0xff));
1008
+ }
1009
+
1010
+ /* =========================================================================
1011
+ * Flush as much pending output as possible. All deflate() output, except for
1012
+ * some deflate_stored() output, goes through this function so some
1013
+ * applications may wish to modify it to avoid allocating a large
1014
+ * strm->next_out buffer and copying into it. (See also read_buf()).
1015
+ */
1016
+ local void flush_pending(z_streamp strm) {
1017
+ unsigned len;
1018
+ deflate_state *s = strm->state;
1019
+
1020
+ #if defined(QAT_COMPRESSION_ENABLED)
1021
+ if (s->qat_s) {
1022
+ qat_flush_pending(s);
1023
+ }
1024
+ #endif
1025
+
1026
+ _tr_flush_bits(s);
1027
+ len = s->pending > strm->avail_out ? strm->avail_out :
1028
+ (unsigned)s->pending;
1029
+ if (len == 0) return;
1030
+
1031
+ zmemcpy(strm->next_out, s->pending_out, len);
1032
+ strm->next_out += len;
1033
+ s->pending_out += len;
1034
+ strm->total_out += len;
1035
+ strm->avail_out -= len;
1036
+ s->pending -= len;
1037
+ if (s->pending == 0) {
1038
+ s->pending_out = s->pending_buf;
1039
+ }
1040
+ }
1041
+
1042
+ /* ===========================================================================
1043
+ * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
1044
+ */
1045
+ #define HCRC_UPDATE(beg) \
1046
+ do { \
1047
+ if (s->gzhead->hcrc && s->pending > (beg)) \
1048
+ strm->adler = crc32_z(strm->adler, s->pending_buf + (beg), \
1049
+ s->pending - (beg)); \
1050
+ } while (0)
1051
+
1052
+ /* ========================================================================= */
1053
+ int ZEXPORT deflate(z_streamp strm, int flush) {
1054
+ int old_flush; /* value of flush param for previous deflate call */
1055
+ deflate_state *s;
1056
+
1057
+ if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
1058
+ return Z_STREAM_ERROR;
1059
+ }
1060
+ s = strm->state;
1061
+
1062
+ if (strm->next_out == Z_NULL ||
1063
+ (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
1064
+ (s->status == FINISH_STATE && flush != Z_FINISH)) {
1065
+ ERR_RETURN(strm, Z_STREAM_ERROR);
1066
+ }
1067
+ if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
1068
+
1069
+ old_flush = s->last_flush;
1070
+ s->last_flush = flush;
1071
+
1072
+ /* Flush as much pending output as possible */
1073
+ if (s->pending != 0) {
1074
+ flush_pending(strm);
1075
+ if (strm->avail_out == 0) {
1076
+ /* Since avail_out is 0, deflate will be called again with
1077
+ * more output space, but possibly with both pending and
1078
+ * avail_in equal to zero. There won't be anything to do,
1079
+ * but this is not an error situation so make sure we
1080
+ * return OK instead of BUF_ERROR at next call of deflate:
1081
+ */
1082
+ s->last_flush = -1;
1083
+ return Z_OK;
1084
+ }
1085
+
1086
+ /* Make sure there is something to do and avoid duplicate consecutive
1087
+ * flushes. For repeated and useless calls with Z_FINISH, we keep
1088
+ * returning Z_STREAM_END instead of Z_BUF_ERROR.
1089
+ */
1090
+ } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
1091
+ flush != Z_FINISH) {
1092
+ ERR_RETURN(strm, Z_BUF_ERROR);
1093
+ }
1094
+
1095
+ /* User must not provide more input after the first FINISH: */
1096
+ if (s->status == FINISH_STATE && strm->avail_in != 0) {
1097
+ ERR_RETURN(strm, Z_BUF_ERROR);
1098
+ }
1099
+
1100
+ /* Write the header */
1101
+ if (s->status == INIT_STATE && s->wrap == 0)
1102
+ s->status = BUSY_STATE;
1103
+ if (s->status == INIT_STATE) {
1104
+ /* zlib header */
1105
+ uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8;
1106
+ uInt level_flags;
1107
+
1108
+ if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
1109
+ level_flags = 0;
1110
+ else if (s->level < 6)
1111
+ level_flags = 1;
1112
+ else if (s->level == 6)
1113
+ level_flags = 2;
1114
+ else
1115
+ level_flags = 3;
1116
+ header |= (level_flags << 6);
1117
+ if (s->strstart != 0) header |= PRESET_DICT;
1118
+ header += 31 - (header % 31);
1119
+
1120
+ putShortMSB(s, header);
1121
+
1122
+ /* Save the adler32 of the preset dictionary: */
1123
+ if (s->strstart != 0) {
1124
+ putShortMSB(s, (uInt)(strm->adler >> 16));
1125
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
1126
+ }
1127
+ strm->adler = adler32(0L, Z_NULL, 0);
1128
+ s->status = BUSY_STATE;
1129
+
1130
+ /* Compression must start with an empty pending buffer */
1131
+ flush_pending(strm);
1132
+ if (s->pending != 0) {
1133
+ s->last_flush = -1;
1134
+ return Z_OK;
1135
+ }
1136
+ }
1137
+ #ifdef GZIP
1138
+ if (s->status == GZIP_STATE) {
1139
+ /* gzip header */
1140
+ crc_reset(s);
1141
+ put_byte(s, 31);
1142
+ put_byte(s, 139);
1143
+ put_byte(s, 8);
1144
+ if (s->gzhead == Z_NULL) {
1145
+ put_byte(s, 0);
1146
+ put_byte(s, 0);
1147
+ put_byte(s, 0);
1148
+ put_byte(s, 0);
1149
+ put_byte(s, 0);
1150
+ put_byte(s, s->level == 9 ? 2 :
1151
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
1152
+ 4 : 0));
1153
+ put_byte(s, OS_CODE);
1154
+ s->status = BUSY_STATE;
1155
+
1156
+ /* Compression must start with an empty pending buffer */
1157
+ flush_pending(strm);
1158
+ if (s->pending != 0) {
1159
+ s->last_flush = -1;
1160
+ return Z_OK;
1161
+ }
1162
+ }
1163
+ else {
1164
+ put_byte(s, (s->gzhead->text ? 1 : 0) +
1165
+ (s->gzhead->hcrc ? 2 : 0) +
1166
+ (s->gzhead->extra == Z_NULL ? 0 : 4) +
1167
+ (s->gzhead->name == Z_NULL ? 0 : 8) +
1168
+ (s->gzhead->comment == Z_NULL ? 0 : 16)
1169
+ );
1170
+ put_byte(s, (Byte)(s->gzhead->time & 0xff));
1171
+ put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
1172
+ put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
1173
+ put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
1174
+ put_byte(s, s->level == 9 ? 2 :
1175
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
1176
+ 4 : 0));
1177
+ put_byte(s, s->gzhead->os & 0xff);
1178
+ if (s->gzhead->extra != Z_NULL) {
1179
+ put_byte(s, s->gzhead->extra_len & 0xff);
1180
+ put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
1181
+ }
1182
+ if (s->gzhead->hcrc)
1183
+ strm->adler = crc32_z(strm->adler, s->pending_buf,
1184
+ s->pending);
1185
+ s->gzindex = 0;
1186
+ s->status = EXTRA_STATE;
1187
+ }
1188
+ }
1189
+ if (s->status == EXTRA_STATE) {
1190
+ if (s->gzhead->extra != Z_NULL) {
1191
+ ulg beg = s->pending; /* start of bytes to update crc */
1192
+ ulg left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
1193
+ while (s->pending + left > s->pending_buf_size) {
1194
+ ulg copy = s->pending_buf_size - s->pending;
1195
+ zmemcpy(s->pending_buf + s->pending,
1196
+ s->gzhead->extra + s->gzindex, copy);
1197
+ s->pending = s->pending_buf_size;
1198
+ HCRC_UPDATE(beg);
1199
+ s->gzindex += copy;
1200
+ flush_pending(strm);
1201
+ if (s->pending != 0) {
1202
+ s->last_flush = -1;
1203
+ return Z_OK;
1204
+ }
1205
+ beg = 0;
1206
+ left -= copy;
1207
+ }
1208
+ zmemcpy(s->pending_buf + s->pending,
1209
+ s->gzhead->extra + s->gzindex, left);
1210
+ s->pending += left;
1211
+ HCRC_UPDATE(beg);
1212
+ s->gzindex = 0;
1213
+ }
1214
+ s->status = NAME_STATE;
1215
+ }
1216
+ if (s->status == NAME_STATE) {
1217
+ if (s->gzhead->name != Z_NULL) {
1218
+ ulg beg = s->pending; /* start of bytes to update crc */
1219
+ int val;
1220
+ do {
1221
+ if (s->pending == s->pending_buf_size) {
1222
+ HCRC_UPDATE(beg);
1223
+ flush_pending(strm);
1224
+ if (s->pending != 0) {
1225
+ s->last_flush = -1;
1226
+ return Z_OK;
1227
+ }
1228
+ beg = 0;
1229
+ }
1230
+ val = s->gzhead->name[s->gzindex++];
1231
+ put_byte(s, val);
1232
+ } while (val != 0);
1233
+ HCRC_UPDATE(beg);
1234
+ s->gzindex = 0;
1235
+ }
1236
+ s->status = COMMENT_STATE;
1237
+ }
1238
+ if (s->status == COMMENT_STATE) {
1239
+ if (s->gzhead->comment != Z_NULL) {
1240
+ ulg beg = s->pending; /* start of bytes to update crc */
1241
+ int val;
1242
+ do {
1243
+ if (s->pending == s->pending_buf_size) {
1244
+ HCRC_UPDATE(beg);
1245
+ flush_pending(strm);
1246
+ if (s->pending != 0) {
1247
+ s->last_flush = -1;
1248
+ return Z_OK;
1249
+ }
1250
+ beg = 0;
1251
+ }
1252
+ val = s->gzhead->comment[s->gzindex++];
1253
+ put_byte(s, val);
1254
+ } while (val != 0);
1255
+ HCRC_UPDATE(beg);
1256
+ }
1257
+ s->status = HCRC_STATE;
1258
+ }
1259
+ if (s->status == HCRC_STATE) {
1260
+ if (s->gzhead->hcrc) {
1261
+ if (s->pending + 2 > s->pending_buf_size) {
1262
+ flush_pending(strm);
1263
+ if (s->pending != 0) {
1264
+ s->last_flush = -1;
1265
+ return Z_OK;
1266
+ }
1267
+ }
1268
+ put_byte(s, (Byte)(strm->adler & 0xff));
1269
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1270
+ strm->adler = crc32(0L, Z_NULL, 0);
1271
+ }
1272
+ s->status = BUSY_STATE;
1273
+
1274
+ /* Compression must start with an empty pending buffer */
1275
+ flush_pending(strm);
1276
+ if (s->pending != 0) {
1277
+ s->last_flush = -1;
1278
+ return Z_OK;
1279
+ }
1280
+ }
1281
+ #endif
1282
+
1283
+ /* Start a new block or continue the current one.
1284
+ */
1285
+ if (strm->avail_in != 0 || s->lookahead != 0 ||
1286
+ (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
1287
+ block_state bstate;
1288
+
1289
+ bstate = s->level == 0 ? deflate_stored(s, flush) :
1290
+ s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
1291
+ s->strategy == Z_RLE ? deflate_rle(s, flush) :
1292
+ (*(configuration_table[s->level].func))(s, flush);
1293
+
1294
+ if (bstate == finish_started || bstate == finish_done) {
1295
+ s->status = FINISH_STATE;
1296
+ }
1297
+ if (bstate == need_more || bstate == finish_started) {
1298
+ if (strm->avail_out == 0) {
1299
+ s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
1300
+ }
1301
+ return Z_OK;
1302
+ /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1303
+ * of deflate should use the same flush parameter to make sure
1304
+ * that the flush is complete. So we don't have to output an
1305
+ * empty block here, this will be done at next call. This also
1306
+ * ensures that for a very small output buffer, we emit at most
1307
+ * one empty block.
1308
+ */
1309
+ }
1310
+ if (bstate == block_done) {
1311
+ if (flush == Z_PARTIAL_FLUSH) {
1312
+ _tr_align(s);
1313
+ } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
1314
+ _tr_stored_block(s, (char*)0, 0L, 0);
1315
+ /* For a full flush, this empty block will be recognized
1316
+ * as a special marker by inflate_sync().
1317
+ */
1318
+ if (flush == Z_FULL_FLUSH) {
1319
+ CLEAR_HASH(s); /* forget history */
1320
+ if (s->lookahead == 0) {
1321
+ s->strstart = 0;
1322
+ s->block_start = 0L;
1323
+ s->insert = 0;
1324
+ }
1325
+ }
1326
+ }
1327
+ flush_pending(strm);
1328
+ if (strm->avail_out == 0) {
1329
+ s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1330
+ return Z_OK;
1331
+ }
1332
+ }
1333
+ }
1334
+
1335
+ if (flush != Z_FINISH) return Z_OK;
1336
+ if (s->wrap <= 0) return Z_STREAM_END;
1337
+
1338
+ /* Write the trailer */
1339
+ #ifdef GZIP
1340
+ if (s->wrap == 2) {
1341
+ crc_finalize(s);
1342
+ put_byte(s, (Byte)(strm->adler & 0xff));
1343
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1344
+ put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
1345
+ put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
1346
+ put_byte(s, (Byte)(strm->total_in & 0xff));
1347
+ put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
1348
+ put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
1349
+ put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
1350
+ }
1351
+ else
1352
+ #endif
1353
+ {
1354
+ putShortMSB(s, (uInt)(strm->adler >> 16));
1355
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
1356
+ }
1357
+ flush_pending(strm);
1358
+ /* If avail_out is zero, the application will call deflate again
1359
+ * to flush the rest.
1360
+ */
1361
+ if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
1362
+ return s->pending != 0 ? Z_OK : Z_STREAM_END;
1363
+ }
1364
+
1365
+ /* ========================================================================= */
1366
+ int ZEXPORT deflateEnd(z_streamp strm) {
1367
+ int status;
1368
+
1369
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
1370
+
1371
+ status = strm->state->status;
1372
+
1373
+ /* Deallocate in reverse order of allocations: */
1374
+ TRY_FREE(strm, strm->state->pending_buf);
1375
+ TRY_FREE(strm, strm->state->head);
1376
+ TRY_FREE(strm, strm->state->prev);
1377
+ TRY_FREE(strm, strm->state->window);
1378
+
1379
+ #if defined(QAT_COMPRESSION_ENABLED)
1380
+ if (strm->state->qat_s) {
1381
+ qat_deflate_state_free(strm->state);
1382
+ }
1383
+ #endif
1384
+
1385
+ ZFREE(strm, strm->state);
1386
+ strm->state = Z_NULL;
1387
+
1388
+ return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1389
+ }
1390
+
1391
+ /* =========================================================================
1392
+ * Copy the source state to the destination state.
1393
+ * To simplify the source, this is not supported for 16-bit MSDOS (which
1394
+ * doesn't have enough memory anyway to duplicate compression states).
1395
+ */
1396
+ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1397
+ #ifdef MAXSEG_64K
1398
+ (void)dest;
1399
+ (void)source;
1400
+ return Z_STREAM_ERROR;
1401
+ #else
1402
+ deflate_state *ds;
1403
+ deflate_state *ss;
1404
+
1405
+
1406
+ if (deflateStateCheck(source) || dest == Z_NULL) {
1407
+ return Z_STREAM_ERROR;
1408
+ }
1409
+
1410
+ ss = source->state;
1411
+
1412
+ zmemcpy(dest, source, sizeof(z_stream));
1413
+
1414
+ ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1415
+ if (ds == Z_NULL) return Z_MEM_ERROR;
1416
+ zmemzero(ds, sizeof(deflate_state));
1417
+ dest->state = (struct internal_state FAR *) ds;
1418
+ zmemcpy(ds, ss, sizeof(deflate_state));
1419
+ ds->strm = dest;
1420
+
1421
+ ds->window = (Bytef *) ZALLOC(dest,
1422
+ ds->w_size + WINDOW_PADDING,
1423
+ 2*sizeof(Byte));
1424
+ ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1425
+ ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
1426
+ #ifdef LIT_MEM
1427
+ ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 5);
1428
+ #else
1429
+ ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
1430
+ #endif
1431
+
1432
+ if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1433
+ ds->pending_buf == Z_NULL) {
1434
+ deflateEnd (dest);
1435
+ return Z_MEM_ERROR;
1436
+ }
1437
+ /* following zmemcpy do not work for 16-bit MSDOS */
1438
+ zmemcpy(ds->window, ss->window, ss->high_water);
1439
+ zmemcpy(ds->prev, ss->prev,
1440
+ (ss->slid || ss->strstart - ss->insert > ds->w_size ? ds->w_size :
1441
+ ss->strstart - ss->insert) * sizeof(Pos));
1442
+ zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
1443
+
1444
+ ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1445
+ zmemcpy(ds->pending_out, ss->pending_out, ss->pending);
1446
+ #ifdef LIT_MEM
1447
+ ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1448
+ ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1449
+ zmemcpy(ds->d_buf, ss->d_buf, ss->sym_next * sizeof(ush));
1450
+ zmemcpy(ds->l_buf, ss->l_buf, ss->sym_next);
1451
+ #else
1452
+ ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1453
+ zmemcpy(ds->sym_buf, ss->sym_buf, ss->sym_next);
1454
+ #endif
1455
+
1456
+ ds->l_desc.dyn_tree = ds->dyn_ltree;
1457
+ ds->d_desc.dyn_tree = ds->dyn_dtree;
1458
+ ds->bl_desc.dyn_tree = ds->bl_tree;
1459
+
1460
+ #if defined(QAT_COMPRESSION_ENABLED)
1461
+ if(ss->qat_s) {
1462
+ ds->qat_s = qat_deflate_copy(ss);
1463
+ if (!ds->qat_s)
1464
+ return Z_MEM_ERROR;
1465
+ }
1466
+ #endif
1467
+
1468
+ return Z_OK;
1469
+ #endif /* MAXSEG_64K */
1470
+ }
1471
+
1472
+ #ifndef FASTEST
1473
+ /* ===========================================================================
1474
+ * Set match_start to the longest match starting at the given string and
1475
+ * return its length. Matches shorter or equal to prev_length are discarded,
1476
+ * in which case the result is equal to prev_length and match_start is
1477
+ * garbage.
1478
+ * IN assertions: cur_match is the head of the hash chain for the current
1479
+ * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1480
+ * OUT assertion: the match length is not greater than s->lookahead.
1481
+ */
1482
+ local uInt longest_match(deflate_state *s, IPos cur_match) {
1483
+ unsigned chain_length = s->max_chain_length;/* max hash chain length */
1484
+ Bytef *scan = s->window + s->strstart; /* current string */
1485
+ Bytef *match; /* matched string */
1486
+ int len; /* length of current match */
1487
+ int best_len = (int)s->prev_length; /* best match length so far */
1488
+ int nice_match = s->nice_match; /* stop if match long enough */
1489
+ IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1490
+ s->strstart - (IPos)MAX_DIST(s) : NIL;
1491
+ /* Stop when cur_match becomes <= limit. To simplify the code,
1492
+ * we prevent matches with the string of window index 0.
1493
+ */
1494
+ Posf *prev = s->prev;
1495
+ uInt wmask = s->w_mask;
1496
+
1497
+ #if defined(DEFLATE_COMPARE256_64LE)
1498
+ /* Quickly reject non-matches with the 2-byte prefilter
1499
+ * (scan_start/scan_end); candidates that pass are extended 8 bytes at a
1500
+ * time with compare256(). */
1501
+ uInt scan_start = read16(scan);
1502
+ uInt scan_end = read16(scan + best_len - 1);
1503
+ #elif defined(UNALIGNED_OK)
1504
+ /* Compare two bytes at a time. Note: this is not always beneficial.
1505
+ * Try with and without -DUNALIGNED_OK to check.
1506
+ */
1507
+ Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1508
+ ush scan_start = *(ushf*)scan;
1509
+ ush scan_end = *(ushf*)(scan + best_len - 1);
1510
+ #else
1511
+ Bytef *strend = s->window + s->strstart + MAX_MATCH;
1512
+ Byte scan_end1 = scan[best_len - 1];
1513
+ Byte scan_end = scan[best_len];
1514
+ #endif
1515
+
1516
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1517
+ * It is easy to get rid of this optimization if necessary.
1518
+ */
1519
+ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1520
+
1521
+ /* Do not waste too much time if we already have a good match: */
1522
+ if (s->prev_length >= s->good_match) {
1523
+ chain_length >>= 2;
1524
+ }
1525
+ /* Do not look for matches beyond the end of the input. This is necessary
1526
+ * to make deflate deterministic.
1527
+ */
1528
+ if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
1529
+
1530
+ Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1531
+ "need lookahead");
1532
+
1533
+ do {
1534
+ Assert(cur_match < s->strstart, "no future");
1535
+ match = s->window + cur_match;
1536
+
1537
+ /* Skip to next match if the match length cannot increase
1538
+ * or if the match length is less than 2. Note that the checks below
1539
+ * for insufficient lookahead only occur occasionally for performance
1540
+ * reasons. Therefore uninitialized memory will be accessed, and
1541
+ * conditional jumps will be made that depend on those values.
1542
+ * However the length of the match is limited to the lookahead, so
1543
+ * the output of deflate is not affected by the uninitialized values.
1544
+ */
1545
+ #if defined(DEFLATE_COMPARE256_64LE)
1546
+ if (read16(match + best_len - 1) != scan_end ||
1547
+ read16(match) != scan_start) continue;
1548
+ /* scan_start matched, so bytes 0..1 are equal; compare the remaining
1549
+ * up to 256 bytes so the length can still reach MAX_MATCH (258). */
1550
+ len = 2 + compare256(scan + 2, match + 2);
1551
+ #elif (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1552
+ /* This code assumes sizeof(unsigned short) == 2. Do not use
1553
+ * UNALIGNED_OK if your compiler uses a different size.
1554
+ */
1555
+ if (*(ushf*)(match + best_len - 1) != scan_end ||
1556
+ *(ushf*)match != scan_start) continue;
1557
+
1558
+ /* It is not necessary to compare scan[2] and match[2] since they are
1559
+ * always equal when the other bytes match, given that the hash keys
1560
+ * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1561
+ * strstart + 3, + 5, up to strstart + 257. We check for insufficient
1562
+ * lookahead only every 4th comparison; the 128th check will be made
1563
+ * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is
1564
+ * necessary to put more guard bytes at the end of the window, or
1565
+ * to check more often for insufficient lookahead.
1566
+ */
1567
+ if (!s->chromium_zlib_hash) {
1568
+ Assert(scan[2] == match[2], "scan[2]?");
1569
+ } else {
1570
+ /* When using CRC hashing, scan[2] and match[2] may mismatch, but in
1571
+ * that case at least one of the other hashed bytes will mismatch
1572
+ * also. Bytes 0 and 1 were already checked above, and we know there
1573
+ * are at least four bytes to check otherwise the mismatch would have
1574
+ * been found by the scan_end comparison above, so: */
1575
+ Assert(scan[2] == match[2] || scan[3] != match[3], "scan[2]??");
1576
+ }
1577
+ scan++, match++;
1578
+ do {
1579
+ } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1580
+ *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1581
+ *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1582
+ *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1583
+ scan < strend);
1584
+ /* The funny "do {}" generates better code on most compilers */
1585
+
1586
+ /* Here, scan <= window + strstart + 257 */
1587
+ Assert(scan <= s->window+(unsigned)(s->window_size - 1),
1588
+ "wild scan");
1589
+ if (*scan == *match) scan++;
1590
+
1591
+ len = (MAX_MATCH - 1) - (int)(strend - scan);
1592
+ scan = strend - (MAX_MATCH-1);
1593
+
1594
+ #else /* UNALIGNED_OK */
1595
+
1596
+ if (match[best_len] != scan_end ||
1597
+ match[best_len - 1] != scan_end1 ||
1598
+ *match != *scan ||
1599
+ *++match != scan[1]) continue;
1600
+
1601
+ /* The check at best_len - 1 can be removed because it will be made
1602
+ * again later. (This heuristic is not always a win.)
1603
+ * It is not necessary to compare scan[2] and match[2] since they
1604
+ * are always equal when the other bytes match, given that
1605
+ * the hash keys are equal and that HASH_BITS >= 8.
1606
+ */
1607
+ scan += 2, match++;
1608
+ if (!s->chromium_zlib_hash) {
1609
+ Assert(*scan == *match, "match[2]?");
1610
+ } else {
1611
+ /* When using CRC hashing, scan[2] and match[2] may mismatch, but in
1612
+ * that case at least one of the other hashed bytes will mismatch
1613
+ * also. Bytes 0 and 1 were already checked above, and we know there
1614
+ * are at least four bytes to check otherwise the mismatch would have
1615
+ * been found by the scan_end comparison above, so: */
1616
+ Assert(*scan == *match || scan[1] != match[1], "match[2]??");
1617
+ }
1618
+
1619
+ /* We check for insufficient lookahead only every 8th comparison;
1620
+ * the 256th check will be made at strstart + 258.
1621
+ */
1622
+ do {
1623
+ } while (*++scan == *++match && *++scan == *++match &&
1624
+ *++scan == *++match && *++scan == *++match &&
1625
+ *++scan == *++match && *++scan == *++match &&
1626
+ *++scan == *++match && *++scan == *++match &&
1627
+ scan < strend);
1628
+
1629
+ Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1630
+ "wild scan");
1631
+
1632
+ len = MAX_MATCH - (int)(strend - scan);
1633
+ scan = strend - MAX_MATCH;
1634
+
1635
+ #endif /* UNALIGNED_OK */
1636
+
1637
+ if (len > best_len) {
1638
+ s->match_start = cur_match;
1639
+ best_len = len;
1640
+ if (len >= nice_match) break;
1641
+ #if defined(DEFLATE_COMPARE256_64LE)
1642
+ scan_end = read16(scan + best_len - 1);
1643
+ #elif defined(UNALIGNED_OK)
1644
+ scan_end = *(ushf*)(scan + best_len - 1);
1645
+ #else
1646
+ scan_end1 = scan[best_len - 1];
1647
+ scan_end = scan[best_len];
1648
+ #endif
1649
+ }
1650
+ } while ((cur_match = prev[cur_match & wmask]) > limit
1651
+ && --chain_length != 0);
1652
+
1653
+ if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1654
+ return s->lookahead;
1655
+ }
1656
+
1657
+ #else /* FASTEST */
1658
+
1659
+ /* ---------------------------------------------------------------------------
1660
+ * Optimized version for FASTEST only
1661
+ */
1662
+ local uInt longest_match(deflate_state *s, IPos cur_match) {
1663
+ Bytef *scan = s->window + s->strstart; /* current string */
1664
+ Bytef *match; /* matched string */
1665
+ int len; /* length of current match */
1666
+ Bytef *strend = s->window + s->strstart + MAX_MATCH;
1667
+
1668
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1669
+ * It is easy to get rid of this optimization if necessary.
1670
+ */
1671
+ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1672
+
1673
+ Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1674
+ "need lookahead");
1675
+
1676
+ Assert(cur_match < s->strstart, "no future");
1677
+
1678
+ match = s->window + cur_match;
1679
+
1680
+ /* Return failure if the match length is less than 2:
1681
+ */
1682
+ if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1683
+
1684
+ /* The check at best_len - 1 can be removed because it will be made
1685
+ * again later. (This heuristic is not always a win.)
1686
+ * It is not necessary to compare scan[2] and match[2] since they
1687
+ * are always equal when the other bytes match, given that
1688
+ * the hash keys are equal and that HASH_BITS >= 8.
1689
+ */
1690
+ scan += 2, match += 2;
1691
+ Assert(*scan == *match, "match[2]?");
1692
+
1693
+ /* We check for insufficient lookahead only every 8th comparison;
1694
+ * the 256th check will be made at strstart + 258.
1695
+ */
1696
+ do {
1697
+ } while (*++scan == *++match && *++scan == *++match &&
1698
+ *++scan == *++match && *++scan == *++match &&
1699
+ *++scan == *++match && *++scan == *++match &&
1700
+ *++scan == *++match && *++scan == *++match &&
1701
+ scan < strend);
1702
+
1703
+ Assert(scan <= s->window + (unsigned)(s->window_size - 1), "wild scan");
1704
+
1705
+ len = MAX_MATCH - (int)(strend - scan);
1706
+
1707
+ if (len < MIN_MATCH) return MIN_MATCH - 1;
1708
+
1709
+ s->match_start = cur_match;
1710
+ return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1711
+ }
1712
+
1713
+ #endif /* FASTEST */
1714
+
1715
+ #ifdef ZLIB_DEBUG
1716
+
1717
+ #define EQUAL 0
1718
+ /* result of memcmp for equal strings */
1719
+
1720
+ /* ===========================================================================
1721
+ * Check that the match at match_start is indeed a match.
1722
+ */
1723
+ local void check_match(deflate_state *s, IPos start, IPos match, int length) {
1724
+ /* check that the match is indeed a match */
1725
+ Bytef *back = s->window + (int)match, *here = s->window + start;
1726
+ IPos len = (IPos)length;
1727
+ if (match == (IPos)-1) {
1728
+ /* match starts one byte before the current window -- just compare the
1729
+ subsequent length-1 bytes */
1730
+ back++;
1731
+ here++;
1732
+ len--;
1733
+ }
1734
+ if (zmemcmp(back, here, len) != EQUAL) {
1735
+ fprintf(stderr, " start %u, match %d, length %d\n",
1736
+ start, (int)match, length);
1737
+ do {
1738
+ fprintf(stderr, "(%02x %02x)", *back++, *here++);
1739
+ } while (--len != 0);
1740
+ z_error("invalid match");
1741
+ }
1742
+ if (z_verbose > 1) {
1743
+ fprintf(stderr,"\\[%d,%d]", start - match, length);
1744
+ do { putc(s->window[start++], stderr); } while (--length != 0);
1745
+ }
1746
+ }
1747
+ #else
1748
+ # define check_match(s, start, match, length)
1749
+ #endif /* ZLIB_DEBUG */
1750
+
1751
+ /* ===========================================================================
1752
+ * Flush the current block, with given end-of-file flag.
1753
+ * IN assertion: strstart is set to the end of the current match.
1754
+ */
1755
+ #define FLUSH_BLOCK_ONLY(s, last) { \
1756
+ _tr_flush_block(s, (s->block_start >= 0L ? \
1757
+ (charf *)&s->window[(unsigned)s->block_start] : \
1758
+ (charf *)Z_NULL), \
1759
+ (ulg)((long)s->strstart - s->block_start), \
1760
+ (last)); \
1761
+ s->block_start = s->strstart; \
1762
+ flush_pending(s->strm); \
1763
+ Tracev((stderr,"[FLUSH]")); \
1764
+ }
1765
+
1766
+ /* Same but force premature exit if necessary. */
1767
+ #define FLUSH_BLOCK(s, last) { \
1768
+ FLUSH_BLOCK_ONLY(s, last); \
1769
+ if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1770
+ }
1771
+
1772
+ /* Maximum stored block length in deflate format (not including header). */
1773
+ #define MAX_STORED 65535
1774
+
1775
+ /* Minimum of a and b. */
1776
+ #define MIN(a, b) ((a) > (b) ? (b) : (a))
1777
+
1778
+ /* ===========================================================================
1779
+ * Copy without compression as much as possible from the input stream, return
1780
+ * the current block state.
1781
+ *
1782
+ * In case deflateParams() is used to later switch to a non-zero compression
1783
+ * level, s->matches (otherwise unused when storing) keeps track of the number
1784
+ * of hash table slides to perform. If s->matches is 1, then one hash table
1785
+ * slide will be done when switching. If s->matches is 2, the maximum value
1786
+ * allowed here, then the hash table will be cleared, since two or more slides
1787
+ * is the same as a clear.
1788
+ *
1789
+ * deflate_stored() is written to minimize the number of times an input byte is
1790
+ * copied. It is most efficient with large input and output buffers, which
1791
+ * maximizes the opportunities to have a single copy from next_in to next_out.
1792
+ */
1793
+ local block_state deflate_stored(deflate_state *s, int flush) {
1794
+ /* Smallest worthy block size when not flushing or finishing. By default
1795
+ * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1796
+ * large input and output buffers, the stored block size will be larger.
1797
+ */
1798
+ unsigned min_block = (unsigned)(MIN(s->pending_buf_size - 5, s->w_size));
1799
+
1800
+ /* Copy as many min_block or larger stored blocks directly to next_out as
1801
+ * possible. If flushing, copy the remaining available input to next_out as
1802
+ * stored blocks, if there is enough space.
1803
+ */
1804
+ int last = 0;
1805
+ unsigned len, left, have;
1806
+ unsigned used = s->strm->avail_in;
1807
+ do {
1808
+ /* Set len to the maximum size block that we can copy directly with the
1809
+ * available input data and output space. Set left to how much of that
1810
+ * would be copied from what's left in the window.
1811
+ */
1812
+ len = MAX_STORED; /* maximum deflate stored block length */
1813
+ have = ((unsigned)s->bi_valid + 42) >> 3; /* bytes in header */
1814
+ if (s->strm->avail_out < have) /* need room for header */
1815
+ break;
1816
+ /* maximum stored block length that will fit in avail_out: */
1817
+ have = s->strm->avail_out - have;
1818
+ left = (unsigned)(s->strstart - s->block_start); /* window bytes */
1819
+ if (len > (ulg)left + s->strm->avail_in)
1820
+ len = left + s->strm->avail_in; /* limit len to the input */
1821
+ if (len > have)
1822
+ len = have; /* limit len to the output */
1823
+
1824
+ /* If the stored block would be less than min_block in length, or if
1825
+ * unable to copy all of the available input when flushing, then try
1826
+ * copying to the window and the pending buffer instead. Also don't
1827
+ * write an empty block when flushing -- deflate() does that.
1828
+ */
1829
+ if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
1830
+ flush == Z_NO_FLUSH ||
1831
+ len != left + s->strm->avail_in))
1832
+ break;
1833
+
1834
+ /* Make a dummy stored block in pending to get the header bytes,
1835
+ * including any pending bits. This also updates the debugging counts.
1836
+ */
1837
+ last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1838
+ _tr_stored_block(s, (char *)0, 0L, last);
1839
+
1840
+ /* Replace the lengths in the dummy stored block with len. */
1841
+ s->pending_buf[s->pending - 4] = (Bytef)len;
1842
+ s->pending_buf[s->pending - 3] = (Bytef)(len >> 8);
1843
+ s->pending_buf[s->pending - 2] = (Bytef)~len;
1844
+ s->pending_buf[s->pending - 1] = (Bytef)(~len >> 8);
1845
+
1846
+ /* Write the stored block header bytes. */
1847
+ flush_pending(s->strm);
1848
+
1849
+ #ifdef ZLIB_DEBUG
1850
+ /* Update debugging counts for the data about to be copied. */
1851
+ s->compressed_len += len << 3;
1852
+ s->bits_sent += len << 3;
1853
+ #endif
1854
+
1855
+ /* Copy uncompressed bytes from the window to next_out. */
1856
+ if (left) {
1857
+ if (left > len)
1858
+ left = len;
1859
+ zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1860
+ s->strm->next_out += left;
1861
+ s->strm->avail_out -= left;
1862
+ s->strm->total_out += left;
1863
+ s->block_start += left;
1864
+ len -= left;
1865
+ }
1866
+
1867
+ /* Copy uncompressed bytes directly from next_in to next_out, updating
1868
+ * the check value.
1869
+ */
1870
+ if (len) {
1871
+ read_buf(s->strm, s->strm->next_out, len);
1872
+ s->strm->next_out += len;
1873
+ s->strm->avail_out -= len;
1874
+ s->strm->total_out += len;
1875
+ }
1876
+ } while (last == 0);
1877
+
1878
+ /* Update the sliding window with the last s->w_size bytes of the copied
1879
+ * data, or append all of the copied data to the existing window if less
1880
+ * than s->w_size bytes were copied. Also update the number of bytes to
1881
+ * insert in the hash tables, in the event that deflateParams() switches to
1882
+ * a non-zero compression level.
1883
+ */
1884
+ used -= s->strm->avail_in; /* number of input bytes directly copied */
1885
+ if (used) {
1886
+ /* If any input was used, then no unused input remains in the window,
1887
+ * therefore s->block_start == s->strstart.
1888
+ */
1889
+ if (used >= s->w_size) { /* supplant the previous history */
1890
+ s->matches = 2; /* clear hash */
1891
+ zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1892
+ s->strstart = s->w_size;
1893
+ s->insert = s->strstart;
1894
+ }
1895
+ else {
1896
+ if (s->window_size - s->strstart <= used) {
1897
+ /* Slide the window down. */
1898
+ s->strstart -= s->w_size;
1899
+ zmemcpy(s->window, s->window + s->w_size, s->strstart);
1900
+ if (s->matches < 2)
1901
+ s->matches++; /* add a pending slide_hash() */
1902
+ if (s->insert > s->strstart)
1903
+ s->insert = s->strstart;
1904
+ }
1905
+ zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1906
+ s->strstart += used;
1907
+ s->insert += MIN(used, s->w_size - s->insert);
1908
+ }
1909
+ s->block_start = s->strstart;
1910
+ }
1911
+ if (s->high_water < s->strstart)
1912
+ s->high_water = s->strstart;
1913
+
1914
+ /* If the last block was written to next_out, then done. */
1915
+ if (last) {
1916
+ s->bi_used = 8;
1917
+ return finish_done;
1918
+ }
1919
+
1920
+ /* If flushing and all input has been consumed, then done. */
1921
+ if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
1922
+ s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
1923
+ return block_done;
1924
+
1925
+ /* Fill the window with any remaining input. */
1926
+ have = (unsigned)(s->window_size - s->strstart);
1927
+ if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
1928
+ /* Slide the window down. */
1929
+ s->block_start -= s->w_size;
1930
+ s->strstart -= s->w_size;
1931
+ zmemcpy(s->window, s->window + s->w_size, s->strstart);
1932
+ if (s->matches < 2)
1933
+ s->matches++; /* add a pending slide_hash() */
1934
+ have += s->w_size; /* more space now */
1935
+ if (s->insert > s->strstart)
1936
+ s->insert = s->strstart;
1937
+ }
1938
+ if (have > s->strm->avail_in)
1939
+ have = s->strm->avail_in;
1940
+ if (have) {
1941
+ read_buf(s->strm, s->window + s->strstart, have);
1942
+ s->strstart += have;
1943
+ s->insert += MIN(have, s->w_size - s->insert);
1944
+ }
1945
+ if (s->high_water < s->strstart)
1946
+ s->high_water = s->strstart;
1947
+
1948
+ /* There was not enough avail_out to write a complete worthy or flushed
1949
+ * stored block to next_out. Write a stored block to pending instead, if we
1950
+ * have enough input for a worthy block, or if flushing and there is enough
1951
+ * room for the remaining input as a stored block in the pending buffer.
1952
+ */
1953
+ have = ((unsigned)s->bi_valid + 42) >> 3; /* bytes in header */
1954
+ /* maximum stored block length that will fit in pending: */
1955
+ have = (unsigned)MIN(s->pending_buf_size - have, MAX_STORED);
1956
+ min_block = MIN(have, s->w_size);
1957
+ left = (unsigned)(s->strstart - s->block_start);
1958
+ if (left >= min_block ||
1959
+ ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
1960
+ s->strm->avail_in == 0 && left <= have)) {
1961
+ len = MIN(left, have);
1962
+ last = flush == Z_FINISH && s->strm->avail_in == 0 &&
1963
+ len == left ? 1 : 0;
1964
+ _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
1965
+ s->block_start += len;
1966
+ flush_pending(s->strm);
1967
+ }
1968
+
1969
+ /* We've done all we can with the available input and output. */
1970
+ if (last)
1971
+ s->bi_used = 8;
1972
+ return last ? finish_started : need_more;
1973
+ }
1974
+
1975
+ /* ===========================================================================
1976
+ * Compress as much as possible from the input stream, return the current
1977
+ * block state.
1978
+ * This function does not perform lazy evaluation of matches and inserts
1979
+ * new strings in the dictionary only for unmatched strings or for short
1980
+ * matches. It is used only for the fast compression options.
1981
+ */
1982
+ local block_state deflate_fast(deflate_state *s, int flush) {
1983
+ IPos hash_head; /* head of the hash chain */
1984
+ int bflush; /* set if current block must be flushed */
1985
+
1986
+ #if defined(QAT_COMPRESSION_ENABLED)
1987
+ if (s->qat_s) {
1988
+ qat_block_state qat_block = qat_deflate_step(s, flush);
1989
+ switch (qat_block) {
1990
+ case qat_block_need_more:
1991
+ return need_more;
1992
+ case qat_block_done:
1993
+ return block_done;
1994
+ case qat_block_finish_started:
1995
+ return finish_started;
1996
+ case qat_block_finish_done:
1997
+ return finish_done;
1998
+ case qat_failure:
1999
+ break;
2000
+ }
2001
+ }
2002
+ #endif
2003
+
2004
+ for (;;) {
2005
+ /* Make sure that we always have enough lookahead, except
2006
+ * at the end of the input file. We need MAX_MATCH bytes
2007
+ * for the next match, plus MIN_MATCH bytes to insert the
2008
+ * string following the next match.
2009
+ */
2010
+ if (s->lookahead < MIN_LOOKAHEAD) {
2011
+ fill_window(s);
2012
+ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
2013
+ return need_more;
2014
+ }
2015
+ if (s->lookahead == 0) break; /* flush the current block */
2016
+ }
2017
+
2018
+ /* Insert the string window[strstart .. strstart + 2] in the
2019
+ * dictionary, and set hash_head to the head of the hash chain:
2020
+ */
2021
+ hash_head = NIL;
2022
+ if (s->lookahead >= MIN_MATCH) {
2023
+ hash_head = insert_string(s, s->strstart);
2024
+ }
2025
+
2026
+ /* Find the longest match, discarding those <= prev_length.
2027
+ * At this point we have always match_length < MIN_MATCH
2028
+ */
2029
+ if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
2030
+ /* To simplify the code, we prevent matches with the string
2031
+ * of window index 0 (in particular we have to avoid a match
2032
+ * of the string with itself at the start of the input file).
2033
+ */
2034
+ s->match_length = longest_match (s, hash_head);
2035
+ /* longest_match() sets match_start */
2036
+ }
2037
+ if (s->match_length >= MIN_MATCH) {
2038
+ check_match(s, s->strstart, s->match_start, (int)s->match_length);
2039
+
2040
+ _tr_tally_dist(s, s->strstart - s->match_start,
2041
+ s->match_length - MIN_MATCH, bflush);
2042
+
2043
+ s->lookahead -= s->match_length;
2044
+
2045
+ /* Insert new strings in the hash table only if the match length
2046
+ * is not too large. This saves time but degrades compression.
2047
+ */
2048
+ #ifndef FASTEST
2049
+ if (s->match_length <= s->max_insert_length &&
2050
+ s->lookahead >= MIN_MATCH) {
2051
+ s->match_length--; /* string at strstart already in table */
2052
+ do {
2053
+ s->strstart++;
2054
+ hash_head = insert_string(s, s->strstart);
2055
+ /* strstart never exceeds WSIZE-MAX_MATCH, so there are
2056
+ * always MIN_MATCH bytes ahead.
2057
+ */
2058
+ } while (--s->match_length != 0);
2059
+ s->strstart++;
2060
+ } else
2061
+ #endif
2062
+ {
2063
+ s->strstart += s->match_length;
2064
+ s->match_length = 0;
2065
+
2066
+ if (!s->chromium_zlib_hash) {
2067
+ s->ins_h = s->window[s->strstart];
2068
+ UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]);
2069
+ #if MIN_MATCH != 3
2070
+ Call UPDATE_HASH() MIN_MATCH-3 more times
2071
+ #endif
2072
+ /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
2073
+ * matter since it will be recomputed at next deflate call.
2074
+ */
2075
+ }
2076
+ }
2077
+ } else {
2078
+ /* No match, output a literal byte */
2079
+ Tracevv((stderr,"%c", s->window[s->strstart]));
2080
+ _tr_tally_lit(s, s->window[s->strstart], bflush);
2081
+ s->lookahead--;
2082
+ s->strstart++;
2083
+ }
2084
+ if (bflush) FLUSH_BLOCK(s, 0);
2085
+ }
2086
+ s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
2087
+ if (flush == Z_FINISH) {
2088
+ FLUSH_BLOCK(s, 1);
2089
+ return finish_done;
2090
+ }
2091
+ if (s->sym_next)
2092
+ FLUSH_BLOCK(s, 0);
2093
+ return block_done;
2094
+ }
2095
+
2096
+ #ifndef FASTEST
2097
+ /* ===========================================================================
2098
+ * Same as above, but achieves better compression. We use a lazy
2099
+ * evaluation for matches: a match is finally adopted only if there is
2100
+ * no better match at the next window position.
2101
+ */
2102
+ local block_state deflate_slow(deflate_state *s, int flush) {
2103
+ IPos hash_head; /* head of hash chain */
2104
+ int bflush; /* set if current block must be flushed */
2105
+
2106
+ #if defined(QAT_COMPRESSION_ENABLED)
2107
+ if (s->qat_s) {
2108
+ qat_block_state qat_block = qat_deflate_step(s, flush);
2109
+ switch (qat_block) {
2110
+ case qat_block_need_more:
2111
+ return need_more;
2112
+ case qat_block_done:
2113
+ return block_done;
2114
+ case qat_block_finish_started:
2115
+ return finish_started;
2116
+ case qat_block_finish_done:
2117
+ return finish_done;
2118
+ case qat_failure:
2119
+ break;
2120
+ }
2121
+ }
2122
+ #endif
2123
+
2124
+ /* Process the input block. */
2125
+ for (;;) {
2126
+ /* Make sure that we always have enough lookahead, except
2127
+ * at the end of the input file. We need MAX_MATCH bytes
2128
+ * for the next match, plus MIN_MATCH bytes to insert the
2129
+ * string following the next match.
2130
+ */
2131
+ if (s->lookahead < MIN_LOOKAHEAD) {
2132
+ fill_window(s);
2133
+ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
2134
+ return need_more;
2135
+ }
2136
+ if (s->lookahead == 0) break; /* flush the current block */
2137
+ }
2138
+
2139
+ /* Insert the string window[strstart .. strstart + 2] in the
2140
+ * dictionary, and set hash_head to the head of the hash chain:
2141
+ */
2142
+ hash_head = NIL;
2143
+ if (s->lookahead >= MIN_MATCH) {
2144
+ hash_head = insert_string(s, s->strstart);
2145
+ }
2146
+
2147
+ /* Find the longest match, discarding those <= prev_length.
2148
+ */
2149
+ s->prev_length = s->match_length, s->prev_match = s->match_start;
2150
+ s->match_length = MIN_MATCH-1;
2151
+
2152
+ if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
2153
+ s->strstart - hash_head <= MAX_DIST(s)) {
2154
+ /* To simplify the code, we prevent matches with the string
2155
+ * of window index 0 (in particular we have to avoid a match
2156
+ * of the string with itself at the start of the input file).
2157
+ */
2158
+ s->match_length = longest_match (s, hash_head);
2159
+ /* longest_match() sets match_start */
2160
+
2161
+ if (s->match_length <= 5 && (s->strategy == Z_FILTERED
2162
+ #if TOO_FAR <= 32767
2163
+ || (s->match_length == MIN_MATCH &&
2164
+ s->strstart - s->match_start > TOO_FAR)
2165
+ #endif
2166
+ )) {
2167
+
2168
+ /* If prev_match is also MIN_MATCH, match_start is garbage
2169
+ * but we will ignore the current match anyway.
2170
+ */
2171
+ s->match_length = MIN_MATCH-1;
2172
+ }
2173
+ }
2174
+ /* If there was a match at the previous step and the current
2175
+ * match is not better, output the previous match:
2176
+ */
2177
+ if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
2178
+ uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
2179
+ /* Do not insert strings in hash table beyond this. */
2180
+
2181
+ check_match(s, s->strstart - 1, s->prev_match, (int)s->prev_length);
2182
+
2183
+ _tr_tally_dist(s, s->strstart - 1 - s->prev_match,
2184
+ s->prev_length - MIN_MATCH, bflush);
2185
+
2186
+ /* Insert in hash table all strings up to the end of the match.
2187
+ * strstart - 1 and strstart are already inserted. If there is not
2188
+ * enough lookahead, the last two strings are not inserted in
2189
+ * the hash table.
2190
+ */
2191
+ s->lookahead -= s->prev_length - 1;
2192
+ s->prev_length -= 2;
2193
+ do {
2194
+ if (++s->strstart <= max_insert) {
2195
+ hash_head = insert_string(s, s->strstart);
2196
+ }
2197
+ } while (--s->prev_length != 0);
2198
+ s->match_available = 0;
2199
+ s->match_length = MIN_MATCH-1;
2200
+ s->strstart++;
2201
+
2202
+ if (bflush) FLUSH_BLOCK(s, 0);
2203
+
2204
+ } else if (s->match_available) {
2205
+ /* If there was no match at the previous position, output a
2206
+ * single literal. If there was a match but the current match
2207
+ * is longer, truncate the previous match to a single literal.
2208
+ */
2209
+ Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2210
+ _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2211
+ if (bflush) {
2212
+ FLUSH_BLOCK_ONLY(s, 0);
2213
+ }
2214
+ s->strstart++;
2215
+ s->lookahead--;
2216
+ if (s->strm->avail_out == 0) return need_more;
2217
+ } else {
2218
+ /* There is no previous match to compare with, wait for
2219
+ * the next step to decide.
2220
+ */
2221
+ s->match_available = 1;
2222
+ s->strstart++;
2223
+ s->lookahead--;
2224
+ }
2225
+ }
2226
+ Assert (flush != Z_NO_FLUSH, "no flush?");
2227
+ if (s->match_available) {
2228
+ Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2229
+ _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2230
+ s->match_available = 0;
2231
+ }
2232
+ s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
2233
+ if (flush == Z_FINISH) {
2234
+ FLUSH_BLOCK(s, 1);
2235
+ return finish_done;
2236
+ }
2237
+ if (s->sym_next)
2238
+ FLUSH_BLOCK(s, 0);
2239
+ return block_done;
2240
+ }
2241
+ #endif /* FASTEST */
2242
+
2243
+ /* ===========================================================================
2244
+ * For Z_RLE, simply look for runs of bytes, generate matches only of distance
2245
+ * one. Do not maintain a hash table. (It will be regenerated if this run of
2246
+ * deflate switches away from Z_RLE.)
2247
+ */
2248
+ local block_state deflate_rle(deflate_state *s, int flush) {
2249
+ int bflush; /* set if current block must be flushed */
2250
+ uInt prev; /* byte at distance one to match */
2251
+ Bytef *scan, *strend; /* scan goes up to strend for length of run */
2252
+
2253
+ for (;;) {
2254
+ /* Make sure that we always have enough lookahead, except
2255
+ * at the end of the input file. We need MAX_MATCH bytes
2256
+ * for the longest run, plus one for the unrolled loop.
2257
+ */
2258
+ if (s->lookahead <= MAX_MATCH) {
2259
+ fill_window(s);
2260
+ if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
2261
+ return need_more;
2262
+ }
2263
+ if (s->lookahead == 0) break; /* flush the current block */
2264
+ }
2265
+
2266
+ /* See how many times the previous byte repeats */
2267
+ s->match_length = 0;
2268
+ if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
2269
+ scan = s->window + s->strstart - 1;
2270
+ prev = *scan;
2271
+ if (prev == *++scan && prev == *++scan && prev == *++scan) {
2272
+ strend = s->window + s->strstart + MAX_MATCH;
2273
+ do {
2274
+ } while (prev == *++scan && prev == *++scan &&
2275
+ prev == *++scan && prev == *++scan &&
2276
+ prev == *++scan && prev == *++scan &&
2277
+ prev == *++scan && prev == *++scan &&
2278
+ scan < strend);
2279
+ s->match_length = MAX_MATCH - (uInt)(strend - scan);
2280
+ if (s->match_length > s->lookahead)
2281
+ s->match_length = s->lookahead;
2282
+ }
2283
+ Assert(scan <= s->window + (uInt)(s->window_size - 1),
2284
+ "wild scan");
2285
+ }
2286
+
2287
+ /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2288
+ if (s->match_length >= MIN_MATCH) {
2289
+ check_match(s, s->strstart, s->strstart - 1, (int)s->match_length);
2290
+
2291
+ _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
2292
+
2293
+ s->lookahead -= s->match_length;
2294
+ s->strstart += s->match_length;
2295
+ s->match_length = 0;
2296
+ } else {
2297
+ /* No match, output a literal byte */
2298
+ Tracevv((stderr,"%c", s->window[s->strstart]));
2299
+ _tr_tally_lit(s, s->window[s->strstart], bflush);
2300
+ s->lookahead--;
2301
+ s->strstart++;
2302
+ }
2303
+ if (bflush) FLUSH_BLOCK(s, 0);
2304
+ }
2305
+ s->insert = 0;
2306
+ if (flush == Z_FINISH) {
2307
+ FLUSH_BLOCK(s, 1);
2308
+ return finish_done;
2309
+ }
2310
+ if (s->sym_next)
2311
+ FLUSH_BLOCK(s, 0);
2312
+ return block_done;
2313
+ }
2314
+
2315
+ /* ===========================================================================
2316
+ * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
2317
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
2318
+ */
2319
+ local block_state deflate_huff(deflate_state *s, int flush) {
2320
+ int bflush; /* set if current block must be flushed */
2321
+
2322
+ for (;;) {
2323
+ /* Make sure that we have a literal to write. */
2324
+ if (s->lookahead == 0) {
2325
+ fill_window(s);
2326
+ if (s->lookahead == 0) {
2327
+ if (flush == Z_NO_FLUSH)
2328
+ return need_more;
2329
+ break; /* flush the current block */
2330
+ }
2331
+ }
2332
+
2333
+ /* Output a literal byte */
2334
+ s->match_length = 0;
2335
+ Tracevv((stderr,"%c", s->window[s->strstart]));
2336
+ _tr_tally_lit(s, s->window[s->strstart], bflush);
2337
+ s->lookahead--;
2338
+ s->strstart++;
2339
+ if (bflush) FLUSH_BLOCK(s, 0);
2340
+ }
2341
+ s->insert = 0;
2342
+ if (flush == Z_FINISH) {
2343
+ FLUSH_BLOCK(s, 1);
2344
+ return finish_done;
2345
+ }
2346
+ if (s->sym_next)
2347
+ FLUSH_BLOCK(s, 0);
2348
+ return block_done;
2349
+ }