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