1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#include "deflate.h"
53#include <u-boot/crc.h>
54
55const char deflate_copyright[] =
56 " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
57
58
59
60
61
62
63
64
65
66
67typedef enum {
68 need_more,
69 block_done,
70 finish_started,
71 finish_done
72} block_state;
73
74typedef block_state (*compress_func) OF((deflate_state *s, int flush));
75
76
77local void fill_window OF((deflate_state *s));
78local block_state deflate_stored OF((deflate_state *s, int flush));
79local block_state deflate_fast OF((deflate_state *s, int flush));
80#ifndef FASTEST
81local block_state deflate_slow OF((deflate_state *s, int flush));
82#endif
83local block_state deflate_rle OF((deflate_state *s, int flush));
84local block_state deflate_huff OF((deflate_state *s, int flush));
85local void lm_init OF((deflate_state *s));
86local void putShortMSB OF((deflate_state *s, uInt b));
87local void flush_pending OF((z_streamp strm));
88local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
89#ifdef ASMV
90 void match_init OF((void));
91 uInt longest_match OF((deflate_state *s, IPos cur_match));
92#else
93local uInt longest_match OF((deflate_state *s, IPos cur_match));
94#endif
95
96#ifdef DEBUG
97local void check_match OF((deflate_state *s, IPos start, IPos match,
98 int length));
99#endif
100
101
102
103
104
105#define NIL 0
106
107
108#ifndef TOO_FAR
109# define TOO_FAR 4096
110#endif
111
112
113
114
115
116
117
118typedef struct config_s {
119 ush good_length;
120 ush max_lazy;
121 ush nice_length;
122 ush max_chain;
123 compress_func func;
124} config;
125
126#ifdef FASTEST
127local const config configuration_table[2] = {
128
129 {0, 0, 0, 0, deflate_stored},
130 {4, 4, 8, 4, deflate_fast}};
131#else
132local const config configuration_table[10] = {
133
134 {0, 0, 0, 0, deflate_stored},
135 {4, 4, 8, 4, deflate_fast},
136 {4, 5, 16, 8, deflate_fast},
137 {4, 6, 32, 32, deflate_fast},
138
139 {4, 4, 16, 16, deflate_slow},
140 {8, 16, 32, 32, deflate_slow},
141 {8, 16, 128, 128, deflate_slow},
142 {8, 32, 128, 256, deflate_slow},
143 {32, 128, 258, 1024, deflate_slow},
144 {32, 258, 258, 4096, deflate_slow}};
145#endif
146
147
148
149
150
151
152#define EQUAL 0
153
154
155#ifndef NO_DUMMY_DECL
156struct static_tree_desc_s {int dummy;};
157#endif
158
159
160
161
162
163
164
165#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
166
167
168
169
170
171
172
173
174
175
176
177
178#ifdef FASTEST
179#define INSERT_STRING(s, str, match_head) \
180 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
181 match_head = s->head[s->ins_h], \
182 s->head[s->ins_h] = (Pos)(str))
183#else
184#define INSERT_STRING(s, str, match_head) \
185 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
186 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
187 s->head[s->ins_h] = (Pos)(str))
188#endif
189
190
191
192
193
194#define CLEAR_HASH(s) \
195 s->head[s->hash_size-1] = NIL; \
196 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
197
198
199int ZEXPORT deflateInit_(strm, level, version, stream_size)
200 z_streamp strm;
201 int level;
202 const char *version;
203 int stream_size;
204{
205 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
206 Z_DEFAULT_STRATEGY, version, stream_size);
207
208}
209
210
211int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
212 version, stream_size)
213 z_streamp strm;
214 int level;
215 int method;
216 int windowBits;
217 int memLevel;
218 int strategy;
219 const char *version;
220 int stream_size;
221{
222 deflate_state *s;
223 int wrap = 1;
224 static const char my_version[] = ZLIB_VERSION;
225
226 if (version == Z_NULL || version[0] != my_version[0] ||
227 stream_size != sizeof(z_stream)) {
228 return Z_VERSION_ERROR;
229 }
230 if (strm == Z_NULL) return Z_STREAM_ERROR;
231
232 strm->msg = Z_NULL;
233 if (strm->zalloc == (alloc_func)0) {
234 strm->zalloc = zcalloc;
235 strm->opaque = (voidpf)0;
236 }
237 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
238
239#ifdef FASTEST
240 if (level != 0) level = 1;
241#else
242 if (level == Z_DEFAULT_COMPRESSION) level = 6;
243#endif
244
245 if (windowBits < 0) {
246 wrap = 0;
247 windowBits = -windowBits;
248 }
249#ifdef GZIP
250 else if (windowBits > 15) {
251 wrap = 2;
252 windowBits -= 16;
253 }
254#endif
255 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
256 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
257 strategy < 0 || strategy > Z_FIXED) {
258 return Z_STREAM_ERROR;
259 }
260 if (windowBits == 8) windowBits = 9;
261 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
262 if (s == Z_NULL) return Z_MEM_ERROR;
263 strm->state = (struct internal_state FAR *)s;
264 s->strm = strm;
265
266 s->wrap = wrap;
267 s->gzhead = Z_NULL;
268 s->w_bits = windowBits;
269 s->w_size = 1 << s->w_bits;
270 s->w_mask = s->w_size - 1;
271
272 s->hash_bits = memLevel + 7;
273 s->hash_size = 1 << s->hash_bits;
274 s->hash_mask = s->hash_size - 1;
275 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
276
277 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
278 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
279 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
280
281 s->high_water = 0;
282
283 s->lit_bufsize = 1 << (memLevel + 6);
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
325 s->pending_buf_size = (ulg)s->lit_bufsize * 4;
326
327 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
328 s->pending_buf == Z_NULL) {
329 s->status = FINISH_STATE;
330 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
331 deflateEnd (strm);
332 return Z_MEM_ERROR;
333 }
334 s->sym_buf = s->pending_buf + s->lit_bufsize;
335 s->sym_end = (s->lit_bufsize - 1) * 3;
336
337
338
339
340
341 s->level = level;
342 s->strategy = strategy;
343 s->method = (Byte)method;
344
345 return deflateReset(strm);
346}
347
348
349int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
350 z_streamp strm;
351 const Bytef *dictionary;
352 uInt dictLength;
353{
354 deflate_state *s;
355 uInt length = dictLength;
356 uInt n;
357 IPos hash_head = 0;
358
359 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
360 strm->state->wrap == 2 ||
361 (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
362 return Z_STREAM_ERROR;
363
364 s = strm->state;
365 if (s->wrap)
366 strm->adler = adler32(strm->adler, dictionary, dictLength);
367
368 if (length < MIN_MATCH) return Z_OK;
369 if (length > s->w_size) {
370 length = s->w_size;
371 dictionary += dictLength - length;
372 }
373 zmemcpy(s->window, dictionary, length);
374 s->strstart = length;
375 s->block_start = (long)length;
376
377
378
379
380
381 s->ins_h = s->window[0];
382 UPDATE_HASH(s, s->ins_h, s->window[1]);
383 for (n = 0; n <= length - MIN_MATCH; n++) {
384 INSERT_STRING(s, n, hash_head);
385 }
386 if (hash_head) hash_head = 0;
387 return Z_OK;
388}
389
390
391int ZEXPORT deflateReset (strm)
392 z_streamp strm;
393{
394 deflate_state *s;
395
396 if (strm == Z_NULL || strm->state == Z_NULL ||
397 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
398 return Z_STREAM_ERROR;
399 }
400
401 strm->total_in = strm->total_out = 0;
402 strm->msg = Z_NULL;
403 strm->data_type = Z_UNKNOWN;
404
405 s = (deflate_state *)strm->state;
406 s->pending = 0;
407 s->pending_out = s->pending_buf;
408
409 if (s->wrap < 0) {
410 s->wrap = -s->wrap;
411 }
412 s->status = s->wrap ? INIT_STATE : BUSY_STATE;
413 strm->adler =
414#ifdef GZIP
415 s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
416#endif
417 adler32(0L, Z_NULL, 0);
418 s->last_flush = Z_NO_FLUSH;
419
420 _tr_init(s);
421 lm_init(s);
422
423 return Z_OK;
424}
425
426
427int ZEXPORT deflateSetHeader (strm, head)
428 z_streamp strm;
429 gz_headerp head;
430{
431 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
432 if (strm->state->wrap != 2) return Z_STREAM_ERROR;
433 strm->state->gzhead = head;
434 return Z_OK;
435}
436
437
438int ZEXPORT deflatePrime (strm, bits, value)
439 z_streamp strm;
440 int bits;
441 int value;
442{
443 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
444 strm->state->bi_valid = bits;
445 strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
446 return Z_OK;
447}
448
449
450int ZEXPORT deflateParams(strm, level, strategy)
451 z_streamp strm;
452 int level;
453 int strategy;
454{
455 deflate_state *s;
456 compress_func func;
457 int err = Z_OK;
458
459 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
460 s = strm->state;
461
462#ifdef FASTEST
463 if (level != 0) level = 1;
464#else
465 if (level == Z_DEFAULT_COMPRESSION) level = 6;
466#endif
467 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
468 return Z_STREAM_ERROR;
469 }
470 func = configuration_table[s->level].func;
471
472 if ((strategy != s->strategy || func != configuration_table[level].func) &&
473 strm->total_in != 0) {
474
475 err = deflate(strm, Z_BLOCK);
476 }
477 if (s->level != level) {
478 s->level = level;
479 s->max_lazy_match = configuration_table[level].max_lazy;
480 s->good_match = configuration_table[level].good_length;
481 s->nice_match = configuration_table[level].nice_length;
482 s->max_chain_length = configuration_table[level].max_chain;
483 }
484 s->strategy = strategy;
485 return err;
486}
487
488
489int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
490 z_streamp strm;
491 int good_length;
492 int max_lazy;
493 int nice_length;
494 int max_chain;
495{
496 deflate_state *s;
497
498 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
499 s = strm->state;
500 s->good_match = good_length;
501 s->max_lazy_match = max_lazy;
502 s->nice_match = nice_length;
503 s->max_chain_length = max_chain;
504 return Z_OK;
505}
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524uLong ZEXPORT deflateBound(strm, sourceLen)
525 z_streamp strm;
526 uLong sourceLen;
527{
528 deflate_state *s;
529 uLong complen, wraplen;
530 Bytef *str;
531
532
533 complen = sourceLen +
534 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
535
536
537 if (strm == Z_NULL || strm->state == Z_NULL)
538 return complen + 6;
539
540
541 s = strm->state;
542 switch (s->wrap) {
543 case 0:
544 wraplen = 0;
545 break;
546 case 1:
547 wraplen = 6 + (s->strstart ? 4 : 0);
548 break;
549 case 2:
550 wraplen = 18;
551 if (s->gzhead != Z_NULL) {
552 if (s->gzhead->extra != Z_NULL)
553 wraplen += 2 + s->gzhead->extra_len;
554 str = s->gzhead->name;
555 if (str != Z_NULL)
556 do {
557 wraplen++;
558 } while (*str++);
559 str = s->gzhead->comment;
560 if (str != Z_NULL)
561 do {
562 wraplen++;
563 } while (*str++);
564 if (s->gzhead->hcrc)
565 wraplen += 2;
566 }
567 break;
568 default:
569 wraplen = 6;
570 }
571
572
573 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
574 return complen + wraplen;
575
576
577 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
578 (sourceLen >> 25) + 13 - 6 + wraplen;
579}
580
581
582
583
584
585
586local void putShortMSB (s, b)
587 deflate_state *s;
588 uInt b;
589{
590 put_byte(s, (Byte)(b >> 8));
591 put_byte(s, (Byte)(b & 0xff));
592}
593
594
595
596
597
598
599
600local void flush_pending(strm)
601 z_streamp strm;
602{
603 unsigned len = strm->state->pending;
604
605 if (len > strm->avail_out) len = strm->avail_out;
606 if (len == 0) return;
607
608 zmemcpy(strm->next_out, strm->state->pending_out, len);
609 strm->next_out += len;
610 strm->state->pending_out += len;
611 strm->total_out += len;
612 strm->avail_out -= len;
613 strm->state->pending -= len;
614 if (strm->state->pending == 0) {
615 strm->state->pending_out = strm->state->pending_buf;
616 }
617}
618
619
620int ZEXPORT deflate (strm, flush)
621 z_streamp strm;
622 int flush;
623{
624 int old_flush;
625 deflate_state *s;
626
627 if (strm == Z_NULL || strm->state == Z_NULL ||
628 flush > Z_BLOCK || flush < 0) {
629 return Z_STREAM_ERROR;
630 }
631 s = strm->state;
632
633 if (s->status == FINISH_STATE && flush != Z_FINISH) {
634 ERR_RETURN(strm, Z_STREAM_ERROR);
635 }
636 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
637
638 s->strm = strm;
639 old_flush = s->last_flush;
640 s->last_flush = flush;
641
642
643 if (s->status == INIT_STATE) {
644#ifdef GZIP
645 if (s->wrap == 2) {
646 strm->adler = crc32(0L, Z_NULL, 0);
647 put_byte(s, 31);
648 put_byte(s, 139);
649 put_byte(s, 8);
650 if (s->gzhead == Z_NULL) {
651 put_byte(s, 0);
652 put_byte(s, 0);
653 put_byte(s, 0);
654 put_byte(s, 0);
655 put_byte(s, 0);
656 put_byte(s, s->level == 9 ? 2 :
657 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
658 4 : 0));
659 put_byte(s, OS_CODE);
660 s->status = BUSY_STATE;
661 }
662 else {
663 put_byte(s, (s->gzhead->text ? 1 : 0) +
664 (s->gzhead->hcrc ? 2 : 0) +
665 (s->gzhead->extra == Z_NULL ? 0 : 4) +
666 (s->gzhead->name == Z_NULL ? 0 : 8) +
667 (s->gzhead->comment == Z_NULL ? 0 : 16)
668 );
669 put_byte(s, (Byte)(s->gzhead->time & 0xff));
670 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
671 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
672 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
673 put_byte(s, s->level == 9 ? 2 :
674 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
675 4 : 0));
676 put_byte(s, s->gzhead->os & 0xff);
677 if (s->gzhead->extra != Z_NULL) {
678 put_byte(s, s->gzhead->extra_len & 0xff);
679 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
680 }
681 if (s->gzhead->hcrc)
682 strm->adler = crc32(strm->adler, s->pending_buf,
683 s->pending);
684 s->gzindex = 0;
685 s->status = EXTRA_STATE;
686 }
687 }
688 else
689#endif
690 {
691 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
692 uInt level_flags;
693
694 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
695 level_flags = 0;
696 else if (s->level < 6)
697 level_flags = 1;
698 else if (s->level == 6)
699 level_flags = 2;
700 else
701 level_flags = 3;
702 header |= (level_flags << 6);
703 if (s->strstart != 0) header |= PRESET_DICT;
704 header += 31 - (header % 31);
705
706 s->status = BUSY_STATE;
707 putShortMSB(s, header);
708
709
710 if (s->strstart != 0) {
711 putShortMSB(s, (uInt)(strm->adler >> 16));
712 putShortMSB(s, (uInt)(strm->adler & 0xffff));
713 }
714 strm->adler = adler32(0L, Z_NULL, 0);
715 }
716 }
717#ifdef GZIP
718 if (s->status == EXTRA_STATE) {
719 if (s->gzhead->extra != Z_NULL) {
720 uInt beg = s->pending;
721
722 while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
723 if (s->pending == s->pending_buf_size) {
724 if (s->gzhead->hcrc && s->pending > beg)
725 strm->adler = crc32(strm->adler, s->pending_buf + beg,
726 s->pending - beg);
727 flush_pending(strm);
728 beg = s->pending;
729 if (s->pending == s->pending_buf_size)
730 break;
731 }
732 put_byte(s, s->gzhead->extra[s->gzindex]);
733 s->gzindex++;
734 }
735 if (s->gzhead->hcrc && s->pending > beg)
736 strm->adler = crc32(strm->adler, s->pending_buf + beg,
737 s->pending - beg);
738 if (s->gzindex == s->gzhead->extra_len) {
739 s->gzindex = 0;
740 s->status = NAME_STATE;
741 }
742 }
743 else
744 s->status = NAME_STATE;
745 }
746 if (s->status == NAME_STATE) {
747 if (s->gzhead->name != Z_NULL) {
748 uInt beg = s->pending;
749 int val;
750
751 do {
752 if (s->pending == s->pending_buf_size) {
753 if (s->gzhead->hcrc && s->pending > beg)
754 strm->adler = crc32(strm->adler, s->pending_buf + beg,
755 s->pending - beg);
756 flush_pending(strm);
757 beg = s->pending;
758 if (s->pending == s->pending_buf_size) {
759 val = 1;
760 break;
761 }
762 }
763 val = s->gzhead->name[s->gzindex++];
764 put_byte(s, val);
765 } while (val != 0);
766 if (s->gzhead->hcrc && s->pending > beg)
767 strm->adler = crc32(strm->adler, s->pending_buf + beg,
768 s->pending - beg);
769 if (val == 0) {
770 s->gzindex = 0;
771 s->status = COMMENT_STATE;
772 }
773 }
774 else
775 s->status = COMMENT_STATE;
776 }
777 if (s->status == COMMENT_STATE) {
778 if (s->gzhead->comment != Z_NULL) {
779 uInt beg = s->pending;
780 int val;
781
782 do {
783 if (s->pending == s->pending_buf_size) {
784 if (s->gzhead->hcrc && s->pending > beg)
785 strm->adler = crc32(strm->adler, s->pending_buf + beg,
786 s->pending - beg);
787 flush_pending(strm);
788 beg = s->pending;
789 if (s->pending == s->pending_buf_size) {
790 val = 1;
791 break;
792 }
793 }
794 val = s->gzhead->comment[s->gzindex++];
795 put_byte(s, val);
796 } while (val != 0);
797 if (s->gzhead->hcrc && s->pending > beg)
798 strm->adler = crc32(strm->adler, s->pending_buf + beg,
799 s->pending - beg);
800 if (val == 0)
801 s->status = HCRC_STATE;
802 }
803 else
804 s->status = HCRC_STATE;
805 }
806 if (s->status == HCRC_STATE) {
807 if (s->gzhead->hcrc) {
808 if (s->pending + 2 > s->pending_buf_size)
809 flush_pending(strm);
810 if (s->pending + 2 <= s->pending_buf_size) {
811 put_byte(s, (Byte)(strm->adler & 0xff));
812 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
813 strm->adler = crc32(0L, Z_NULL, 0);
814 s->status = BUSY_STATE;
815 }
816 }
817 else
818 s->status = BUSY_STATE;
819 }
820#endif
821
822
823 if (s->pending != 0) {
824 flush_pending(strm);
825 if (strm->avail_out == 0) {
826
827
828
829
830
831
832 s->last_flush = -1;
833 return Z_OK;
834 }
835
836
837
838
839
840 } else if (strm->avail_in == 0 && flush <= old_flush &&
841 flush != Z_FINISH) {
842 ERR_RETURN(strm, Z_BUF_ERROR);
843 }
844
845
846 if (s->status == FINISH_STATE && strm->avail_in != 0) {
847 ERR_RETURN(strm, Z_BUF_ERROR);
848 }
849
850
851
852 if (strm->avail_in != 0 || s->lookahead != 0 ||
853 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
854 block_state bstate;
855
856 bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
857 (s->strategy == Z_RLE ? deflate_rle(s, flush) :
858 (*(configuration_table[s->level].func))(s, flush));
859
860 if (bstate == finish_started || bstate == finish_done) {
861 s->status = FINISH_STATE;
862 }
863 if (bstate == need_more || bstate == finish_started) {
864 if (strm->avail_out == 0) {
865 s->last_flush = -1;
866 }
867 return Z_OK;
868
869
870
871
872
873
874
875 }
876 if (bstate == block_done) {
877 if (flush == Z_PARTIAL_FLUSH) {
878 _tr_align(s);
879 } else if (flush != Z_BLOCK) {
880 _tr_stored_block(s, (char*)0, 0L, 0);
881
882
883
884 if (flush == Z_FULL_FLUSH) {
885 CLEAR_HASH(s);
886 if (s->lookahead == 0) {
887 s->strstart = 0;
888 s->block_start = 0L;
889 }
890 }
891 }
892 flush_pending(strm);
893 if (strm->avail_out == 0) {
894 s->last_flush = -1;
895 return Z_OK;
896 }
897 }
898 }
899 Assert(strm->avail_out > 0, "bug2");
900
901 if (flush != Z_FINISH) return Z_OK;
902 if (s->wrap <= 0) return Z_STREAM_END;
903
904
905#ifdef GZIP
906 if (s->wrap == 2) {
907 put_byte(s, (Byte)(strm->adler & 0xff));
908 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
909 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
910 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
911 put_byte(s, (Byte)(strm->total_in & 0xff));
912 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
913 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
914 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
915 }
916 else
917#endif
918 {
919 putShortMSB(s, (uInt)(strm->adler >> 16));
920 putShortMSB(s, (uInt)(strm->adler & 0xffff));
921 }
922 flush_pending(strm);
923
924
925
926 if (s->wrap > 0) s->wrap = -s->wrap;
927 return s->pending != 0 ? Z_OK : Z_STREAM_END;
928}
929
930
931int ZEXPORT deflateEnd (strm)
932 z_streamp strm;
933{
934 int status;
935
936 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
937
938 status = strm->state->status;
939 if (status != INIT_STATE &&
940 status != EXTRA_STATE &&
941 status != NAME_STATE &&
942 status != COMMENT_STATE &&
943 status != HCRC_STATE &&
944 status != BUSY_STATE &&
945 status != FINISH_STATE) {
946 return Z_STREAM_ERROR;
947 }
948
949
950 TRY_FREE(strm, strm->state->pending_buf);
951 TRY_FREE(strm, strm->state->head);
952 TRY_FREE(strm, strm->state->prev);
953 TRY_FREE(strm, strm->state->window);
954
955 ZFREE(strm, strm->state);
956 strm->state = Z_NULL;
957
958 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
959}
960
961
962
963
964
965
966int ZEXPORT deflateCopy (dest, source)
967 z_streamp dest;
968 z_streamp source;
969{
970#ifdef MAXSEG_64K
971 return Z_STREAM_ERROR;
972#else
973 deflate_state *ds;
974 deflate_state *ss;
975
976
977 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
978 return Z_STREAM_ERROR;
979 }
980
981 ss = source->state;
982
983 zmemcpy(dest, source, sizeof(z_stream));
984
985 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
986 if (ds == Z_NULL) return Z_MEM_ERROR;
987 dest->state = (struct internal_state FAR *) ds;
988 zmemcpy(ds, ss, sizeof(deflate_state));
989 ds->strm = dest;
990
991 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
992 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
993 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
994 ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
995
996 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
997 ds->pending_buf == Z_NULL) {
998 deflateEnd (dest);
999 return Z_MEM_ERROR;
1000 }
1001
1002 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1003 zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
1004 zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
1005 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1006
1007 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1008 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1009
1010 ds->l_desc.dyn_tree = ds->dyn_ltree;
1011 ds->d_desc.dyn_tree = ds->dyn_dtree;
1012 ds->bl_desc.dyn_tree = ds->bl_tree;
1013
1014 return Z_OK;
1015#endif
1016}
1017
1018
1019
1020
1021
1022
1023
1024
1025local int read_buf(strm, buf, size)
1026 z_streamp strm;
1027 Bytef *buf;
1028 unsigned size;
1029{
1030 unsigned len = strm->avail_in;
1031
1032 if (len > size) len = size;
1033 if (len == 0) return 0;
1034
1035 strm->avail_in -= len;
1036
1037 if (strm->state->wrap == 1) {
1038 strm->adler = adler32(strm->adler, strm->next_in, len);
1039 }
1040#ifdef GZIP
1041 else if (strm->state->wrap == 2) {
1042 strm->adler = crc32(strm->adler, strm->next_in, len);
1043 }
1044#endif
1045 zmemcpy(buf, strm->next_in, len);
1046 strm->next_in += len;
1047 strm->total_in += len;
1048
1049 return (int)len;
1050}
1051
1052
1053
1054
1055local void lm_init (s)
1056 deflate_state *s;
1057{
1058 s->window_size = (ulg)2L*s->w_size;
1059
1060 CLEAR_HASH(s);
1061
1062
1063
1064 s->max_lazy_match = configuration_table[s->level].max_lazy;
1065 s->good_match = configuration_table[s->level].good_length;
1066 s->nice_match = configuration_table[s->level].nice_length;
1067 s->max_chain_length = configuration_table[s->level].max_chain;
1068
1069 s->strstart = 0;
1070 s->block_start = 0L;
1071 s->lookahead = 0;
1072 s->match_length = s->prev_length = MIN_MATCH-1;
1073 s->match_available = 0;
1074 s->ins_h = 0;
1075#ifndef FASTEST
1076#ifdef ASMV
1077 match_init();
1078#endif
1079#endif
1080}
1081
1082#ifndef FASTEST
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092#ifndef ASMV
1093
1094
1095
1096local uInt longest_match(s, cur_match)
1097 deflate_state *s;
1098 IPos cur_match;
1099{
1100 unsigned chain_length = s->max_chain_length;
1101 register Bytef *scan = s->window + s->strstart;
1102 register Bytef *match;
1103 register int len;
1104 int best_len = s->prev_length;
1105 int nice_match = s->nice_match;
1106 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1107 s->strstart - (IPos)MAX_DIST(s) : NIL;
1108
1109
1110
1111 Posf *prev = s->prev;
1112 uInt wmask = s->w_mask;
1113
1114#ifdef UNALIGNED_OK
1115
1116
1117
1118 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1119 register ush scan_start = *(ushf*)scan;
1120 register ush scan_end = *(ushf*)(scan+best_len-1);
1121#else
1122 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1123 register Byte scan_end1 = scan[best_len-1];
1124 register Byte scan_end = scan[best_len];
1125#endif
1126
1127
1128
1129
1130 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1131
1132
1133 if (s->prev_length >= s->good_match) {
1134 chain_length >>= 2;
1135 }
1136
1137
1138
1139 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1140
1141 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1142
1143 do {
1144 Assert(cur_match < s->strstart, "no future");
1145 match = s->window + cur_match;
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1156
1157
1158
1159 if (*(ushf*)(match+best_len-1) != scan_end ||
1160 *(ushf*)match != scan_start) continue;
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171 Assert(scan[2] == match[2], "scan[2]?");
1172 scan++, match++;
1173 do {
1174 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1175 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1176 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1177 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1178 scan < strend);
1179
1180
1181
1182 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1183 if (*scan == *match) scan++;
1184
1185 len = (MAX_MATCH - 1) - (int)(strend-scan);
1186 scan = strend - (MAX_MATCH-1);
1187
1188#else
1189
1190 if (match[best_len] != scan_end ||
1191 match[best_len-1] != scan_end1 ||
1192 *match != *scan ||
1193 *++match != scan[1]) continue;
1194
1195
1196
1197
1198
1199
1200
1201 scan += 2, match++;
1202 Assert(*scan == *match, "match[2]?");
1203
1204
1205
1206
1207 do {
1208 } while (*++scan == *++match && *++scan == *++match &&
1209 *++scan == *++match && *++scan == *++match &&
1210 *++scan == *++match && *++scan == *++match &&
1211 *++scan == *++match && *++scan == *++match &&
1212 scan < strend);
1213
1214 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1215
1216 len = MAX_MATCH - (int)(strend - scan);
1217 scan = strend - MAX_MATCH;
1218
1219#endif
1220
1221 if (len > best_len) {
1222 s->match_start = cur_match;
1223 best_len = len;
1224 if (len >= nice_match) break;
1225#ifdef UNALIGNED_OK
1226 scan_end = *(ushf*)(scan+best_len-1);
1227#else
1228 scan_end1 = scan[best_len-1];
1229 scan_end = scan[best_len];
1230#endif
1231 }
1232 } while ((cur_match = prev[cur_match & wmask]) > limit
1233 && --chain_length != 0);
1234
1235 if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1236 return s->lookahead;
1237}
1238#endif
1239
1240#else
1241
1242
1243
1244
1245local uInt longest_match(s, cur_match)
1246 deflate_state *s;
1247 IPos cur_match;
1248{
1249 register Bytef *scan = s->window + s->strstart;
1250 register Bytef *match;
1251 register int len;
1252 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1253
1254
1255
1256
1257 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1258
1259 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1260
1261 Assert(cur_match < s->strstart, "no future");
1262
1263 match = s->window + cur_match;
1264
1265
1266
1267 if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1268
1269
1270
1271
1272
1273
1274
1275 scan += 2, match += 2;
1276 Assert(*scan == *match, "match[2]?");
1277
1278
1279
1280
1281 do {
1282 } while (*++scan == *++match && *++scan == *++match &&
1283 *++scan == *++match && *++scan == *++match &&
1284 *++scan == *++match && *++scan == *++match &&
1285 *++scan == *++match && *++scan == *++match &&
1286 scan < strend);
1287
1288 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1289
1290 len = MAX_MATCH - (int)(strend - scan);
1291
1292 if (len < MIN_MATCH) return MIN_MATCH - 1;
1293
1294 s->match_start = cur_match;
1295 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1296}
1297
1298#endif
1299
1300#ifdef DEBUG
1301
1302
1303
1304local void check_match(s, start, match, length)
1305 deflate_state *s;
1306 IPos start, match;
1307 int length;
1308{
1309
1310 if (zmemcmp(s->window + match,
1311 s->window + start, length) != EQUAL) {
1312 fprintf(stderr, " start %u, match %u, length %d\n",
1313 start, match, length);
1314 do {
1315 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1316 } while (--length != 0);
1317 z_error("invalid match");
1318 }
1319 if (z_verbose > 1) {
1320 fprintf(stderr,"\\[%d,%d]", start-match, length);
1321 do { putc(s->window[start++]); } while (--length != 0);
1322 }
1323}
1324#else
1325# define check_match(s, start, match, length)
1326#endif
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338local void fill_window(s)
1339 deflate_state *s;
1340{
1341 register unsigned n, m;
1342 register Posf *p;
1343 unsigned more;
1344 uInt wsize = s->w_size;
1345
1346 do {
1347 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1348
1349
1350 if (sizeof(int) <= 2) {
1351 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1352 more = wsize;
1353
1354 } else if (more == (unsigned)(-1)) {
1355
1356
1357
1358 more--;
1359 }
1360 }
1361
1362
1363
1364
1365 if (s->strstart >= wsize+MAX_DIST(s)) {
1366
1367 zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
1368 s->match_start -= wsize;
1369 s->strstart -= wsize;
1370 s->block_start -= (long) wsize;
1371
1372
1373
1374
1375
1376
1377
1378 n = s->hash_size;
1379 p = &s->head[n];
1380 do {
1381 m = *--p;
1382 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1383 } while (--n);
1384
1385 n = wsize;
1386#ifndef FASTEST
1387 p = &s->prev[n];
1388 do {
1389 m = *--p;
1390 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1391
1392
1393
1394 } while (--n);
1395#endif
1396 more += wsize;
1397 }
1398 if (s->strm->avail_in == 0) return;
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411 Assert(more >= 2, "more < 2");
1412
1413 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1414 s->lookahead += n;
1415
1416
1417 if (s->lookahead >= MIN_MATCH) {
1418 s->ins_h = s->window[s->strstart];
1419 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1420#if MIN_MATCH != 3
1421 Call UPDATE_HASH() MIN_MATCH-3 more times
1422#endif
1423 }
1424
1425
1426
1427
1428 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1429
1430
1431
1432
1433
1434
1435
1436
1437 if (s->high_water < s->window_size) {
1438 ulg curr = s->strstart + (ulg)(s->lookahead);
1439 ulg init;
1440
1441 if (s->high_water < curr) {
1442
1443
1444
1445 init = s->window_size - curr;
1446 if (init > WIN_INIT)
1447 init = WIN_INIT;
1448 zmemzero(s->window + curr, (unsigned)init);
1449 s->high_water = curr + init;
1450 }
1451 else if (s->high_water < (ulg)curr + WIN_INIT) {
1452
1453
1454
1455
1456 init = (ulg)curr + WIN_INIT - s->high_water;
1457 if (init > s->window_size - s->high_water)
1458 init = s->window_size - s->high_water;
1459 zmemzero(s->window + s->high_water, (unsigned)init);
1460 s->high_water += init;
1461 }
1462 }
1463}
1464
1465
1466
1467
1468
1469#define FLUSH_BLOCK_ONLY(s, last) { \
1470 _tr_flush_block(s, (s->block_start >= 0L ? \
1471 (charf *)&s->window[(unsigned)s->block_start] : \
1472 (charf *)Z_NULL), \
1473 (ulg)((long)s->strstart - s->block_start), \
1474 (last)); \
1475 s->block_start = s->strstart; \
1476 flush_pending(s->strm); \
1477 Tracev((stderr,"[FLUSH]")); \
1478}
1479
1480
1481#define FLUSH_BLOCK(s, last) { \
1482 FLUSH_BLOCK_ONLY(s, last); \
1483 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1484}
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495local block_state deflate_stored(s, flush)
1496 deflate_state *s;
1497 int flush;
1498{
1499
1500
1501
1502 ulg max_block_size = 0xffff;
1503 ulg max_start;
1504
1505 if (max_block_size > s->pending_buf_size - 5) {
1506 max_block_size = s->pending_buf_size - 5;
1507 }
1508
1509
1510 for (;;) {
1511
1512 if (s->lookahead <= 1) {
1513
1514 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1515 s->block_start >= (long)s->w_size, "slide too late");
1516
1517 fill_window(s);
1518 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1519
1520 if (s->lookahead == 0) break;
1521 }
1522 Assert(s->block_start >= 0L, "block gone");
1523
1524 s->strstart += s->lookahead;
1525 s->lookahead = 0;
1526
1527
1528 max_start = s->block_start + max_block_size;
1529 if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1530
1531 s->lookahead = (uInt)(s->strstart - max_start);
1532 s->strstart = (uInt)max_start;
1533 FLUSH_BLOCK(s, 0);
1534 }
1535
1536
1537
1538 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1539 FLUSH_BLOCK(s, 0);
1540 }
1541 }
1542 FLUSH_BLOCK(s, flush == Z_FINISH);
1543 return flush == Z_FINISH ? finish_done : block_done;
1544}
1545
1546
1547
1548
1549
1550
1551
1552
1553local block_state deflate_fast(s, flush)
1554 deflate_state *s;
1555 int flush;
1556{
1557 IPos hash_head;
1558 int bflush;
1559
1560 for (;;) {
1561
1562
1563
1564
1565
1566 if (s->lookahead < MIN_LOOKAHEAD) {
1567 fill_window(s);
1568 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1569 return need_more;
1570 }
1571 if (s->lookahead == 0) break;
1572 }
1573
1574
1575
1576
1577 hash_head = NIL;
1578 if (s->lookahead >= MIN_MATCH) {
1579 INSERT_STRING(s, s->strstart, hash_head);
1580 }
1581
1582
1583
1584
1585 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1586
1587
1588
1589
1590 s->match_length = longest_match (s, hash_head);
1591
1592 }
1593 if (s->match_length >= MIN_MATCH) {
1594 check_match(s, s->strstart, s->match_start, s->match_length);
1595
1596 _tr_tally_dist(s, s->strstart - s->match_start,
1597 s->match_length - MIN_MATCH, bflush);
1598
1599 s->lookahead -= s->match_length;
1600
1601
1602
1603
1604#ifndef FASTEST
1605 if (s->match_length <= s->max_insert_length &&
1606 s->lookahead >= MIN_MATCH) {
1607 s->match_length--;
1608 do {
1609 s->strstart++;
1610 INSERT_STRING(s, s->strstart, hash_head);
1611
1612
1613
1614 } while (--s->match_length != 0);
1615 s->strstart++;
1616 } else
1617#endif
1618 {
1619 s->strstart += s->match_length;
1620 s->match_length = 0;
1621 s->ins_h = s->window[s->strstart];
1622 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1623#if MIN_MATCH != 3
1624 Call UPDATE_HASH() MIN_MATCH-3 more times
1625#endif
1626
1627
1628
1629 }
1630 } else {
1631
1632 Tracevv((stderr,"%c", s->window[s->strstart]));
1633 _tr_tally_lit (s, s->window[s->strstart], bflush);
1634 s->lookahead--;
1635 s->strstart++;
1636 }
1637 if (bflush) FLUSH_BLOCK(s, 0);
1638 }
1639 FLUSH_BLOCK(s, flush == Z_FINISH);
1640 return flush == Z_FINISH ? finish_done : block_done;
1641}
1642
1643#ifndef FASTEST
1644
1645
1646
1647
1648
1649local block_state deflate_slow(s, flush)
1650 deflate_state *s;
1651 int flush;
1652{
1653 IPos hash_head;
1654 int bflush;
1655
1656
1657 for (;;) {
1658
1659
1660
1661
1662
1663 if (s->lookahead < MIN_LOOKAHEAD) {
1664 fill_window(s);
1665 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1666 return need_more;
1667 }
1668 if (s->lookahead == 0) break;
1669 }
1670
1671
1672
1673
1674 hash_head = NIL;
1675 if (s->lookahead >= MIN_MATCH) {
1676 INSERT_STRING(s, s->strstart, hash_head);
1677 }
1678
1679
1680
1681 s->prev_length = s->match_length, s->prev_match = s->match_start;
1682 s->match_length = MIN_MATCH-1;
1683
1684 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1685 s->strstart - hash_head <= MAX_DIST(s)) {
1686
1687
1688
1689
1690 s->match_length = longest_match (s, hash_head);
1691
1692
1693 if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1694#if TOO_FAR <= 32767
1695 || (s->match_length == MIN_MATCH &&
1696 s->strstart - s->match_start > TOO_FAR)
1697#endif
1698 )) {
1699
1700
1701
1702
1703 s->match_length = MIN_MATCH-1;
1704 }
1705 }
1706
1707
1708
1709 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1710 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1711
1712
1713 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1714
1715 _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1716 s->prev_length - MIN_MATCH, bflush);
1717
1718
1719
1720
1721
1722
1723 s->lookahead -= s->prev_length-1;
1724 s->prev_length -= 2;
1725 do {
1726 if (++s->strstart <= max_insert) {
1727 INSERT_STRING(s, s->strstart, hash_head);
1728 }
1729 } while (--s->prev_length != 0);
1730 s->match_available = 0;
1731 s->match_length = MIN_MATCH-1;
1732 s->strstart++;
1733
1734 if (bflush) FLUSH_BLOCK(s, 0);
1735
1736 } else if (s->match_available) {
1737
1738
1739
1740
1741 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1742 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1743 if (bflush) {
1744 FLUSH_BLOCK_ONLY(s, 0);
1745 }
1746 s->strstart++;
1747 s->lookahead--;
1748 if (s->strm->avail_out == 0) return need_more;
1749 } else {
1750
1751
1752
1753 s->match_available = 1;
1754 s->strstart++;
1755 s->lookahead--;
1756 }
1757 }
1758 Assert (flush != Z_NO_FLUSH, "no flush?");
1759 if (s->match_available) {
1760 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1761 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1762 s->match_available = 0;
1763 }
1764 FLUSH_BLOCK(s, flush == Z_FINISH);
1765 return flush == Z_FINISH ? finish_done : block_done;
1766}
1767#endif
1768
1769
1770
1771
1772
1773
1774local block_state deflate_rle(s, flush)
1775 deflate_state *s;
1776 int flush;
1777{
1778 int bflush;
1779 uInt prev;
1780 Bytef *scan, *strend;
1781
1782 for (;;) {
1783
1784
1785
1786
1787 if (s->lookahead < MAX_MATCH) {
1788 fill_window(s);
1789 if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
1790 return need_more;
1791 }
1792 if (s->lookahead == 0) break;
1793 }
1794
1795
1796 s->match_length = 0;
1797 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1798 scan = s->window + s->strstart - 1;
1799 prev = *scan;
1800 if (prev == *++scan && prev == *++scan && prev == *++scan) {
1801 strend = s->window + s->strstart + MAX_MATCH;
1802 do {
1803 } while (prev == *++scan && prev == *++scan &&
1804 prev == *++scan && prev == *++scan &&
1805 prev == *++scan && prev == *++scan &&
1806 prev == *++scan && prev == *++scan &&
1807 scan < strend);
1808 s->match_length = MAX_MATCH - (int)(strend - scan);
1809 if (s->match_length > s->lookahead)
1810 s->match_length = s->lookahead;
1811 }
1812 }
1813
1814
1815 if (s->match_length >= MIN_MATCH) {
1816 check_match(s, s->strstart, s->strstart - 1, s->match_length);
1817
1818 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
1819
1820 s->lookahead -= s->match_length;
1821 s->strstart += s->match_length;
1822 s->match_length = 0;
1823 } else {
1824
1825 Tracevv((stderr,"%c", s->window[s->strstart]));
1826 _tr_tally_lit (s, s->window[s->strstart], bflush);
1827 s->lookahead--;
1828 s->strstart++;
1829 }
1830 if (bflush) FLUSH_BLOCK(s, 0);
1831 }
1832 FLUSH_BLOCK(s, flush == Z_FINISH);
1833 return flush == Z_FINISH ? finish_done : block_done;
1834}
1835
1836
1837
1838
1839
1840local block_state deflate_huff(s, flush)
1841 deflate_state *s;
1842 int flush;
1843{
1844 int bflush;
1845
1846 for (;;) {
1847
1848 if (s->lookahead == 0) {
1849 fill_window(s);
1850 if (s->lookahead == 0) {
1851 if (flush == Z_NO_FLUSH)
1852 return need_more;
1853 break;
1854 }
1855 }
1856
1857
1858 s->match_length = 0;
1859 Tracevv((stderr,"%c", s->window[s->strstart]));
1860 _tr_tally_lit (s, s->window[s->strstart], bflush);
1861 s->lookahead--;
1862 s->strstart++;
1863 if (bflush) FLUSH_BLOCK(s, 0);
1864 }
1865 FLUSH_BLOCK(s, flush == Z_FINISH);
1866 return flush == Z_FINISH ? finish_done : block_done;
1867}
1868