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