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#include <linux/module.h>
52#include <linux/zutil.h>
53#include "defutil.h"
54
55
56
57
58
59typedef enum {
60 need_more,
61 block_done,
62 finish_started,
63 finish_done
64} block_state;
65
66typedef block_state (*compress_func) (deflate_state *s, int flush);
67
68
69static void fill_window (deflate_state *s);
70static block_state deflate_stored (deflate_state *s, int flush);
71static block_state deflate_fast (deflate_state *s, int flush);
72static block_state deflate_slow (deflate_state *s, int flush);
73static void lm_init (deflate_state *s);
74static void putShortMSB (deflate_state *s, uInt b);
75static void flush_pending (z_streamp strm);
76static int read_buf (z_streamp strm, Byte *buf, unsigned size);
77static uInt longest_match (deflate_state *s, IPos cur_match);
78
79#ifdef DEBUG_ZLIB
80static void check_match (deflate_state *s, IPos start, IPos match,
81 int length);
82#endif
83
84
85
86
87
88#define NIL 0
89
90
91#ifndef TOO_FAR
92# define TOO_FAR 4096
93#endif
94
95
96#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
97
98
99
100
101
102
103
104
105
106typedef struct config_s {
107 ush good_length;
108 ush max_lazy;
109 ush nice_length;
110 ush max_chain;
111 compress_func func;
112} config;
113
114static const config configuration_table[10] = {
115
116 {0, 0, 0, 0, deflate_stored},
117 {4, 4, 8, 4, deflate_fast},
118 {4, 5, 16, 8, deflate_fast},
119 {4, 6, 32, 32, deflate_fast},
120
121 {4, 4, 16, 16, deflate_slow},
122 {8, 16, 32, 32, deflate_slow},
123 {8, 16, 128, 128, deflate_slow},
124 {8, 32, 128, 256, deflate_slow},
125 {32, 128, 258, 1024, deflate_slow},
126 {32, 258, 258, 4096, deflate_slow}};
127
128
129
130
131
132
133#define EQUAL 0
134
135
136
137
138
139
140
141
142#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
143
144
145
146
147
148
149
150
151
152
153#define INSERT_STRING(s, str, match_head) \
154 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
155 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
156 s->head[s->ins_h] = (Pos)(str))
157
158
159
160
161
162#define CLEAR_HASH(s) \
163 s->head[s->hash_size-1] = NIL; \
164 memset((char *)s->head, 0, (unsigned)(s->hash_size-1)*sizeof(*s->head));
165
166
167int zlib_deflateInit2(
168 z_streamp strm,
169 int level,
170 int method,
171 int windowBits,
172 int memLevel,
173 int strategy
174)
175{
176 deflate_state *s;
177 int noheader = 0;
178 deflate_workspace *mem;
179 char *next;
180
181 ush *overlay;
182
183
184
185
186 if (strm == NULL) return Z_STREAM_ERROR;
187
188 strm->msg = NULL;
189
190 if (level == Z_DEFAULT_COMPRESSION) level = 6;
191
192 mem = (deflate_workspace *) strm->workspace;
193
194 if (windowBits < 0) {
195 noheader = 1;
196 windowBits = -windowBits;
197 }
198 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
199 windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
200 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
201 return Z_STREAM_ERROR;
202 }
203
204
205
206
207
208 next = (char *) mem;
209 next += sizeof(*mem);
210 mem->window_memory = (Byte *) next;
211 next += zlib_deflate_window_memsize(windowBits);
212 mem->prev_memory = (Pos *) next;
213 next += zlib_deflate_prev_memsize(windowBits);
214 mem->head_memory = (Pos *) next;
215 next += zlib_deflate_head_memsize(memLevel);
216 mem->overlay_memory = next;
217
218 s = (deflate_state *) &(mem->deflate_memory);
219 strm->state = (struct internal_state *)s;
220 s->strm = strm;
221
222 s->noheader = noheader;
223 s->w_bits = windowBits;
224 s->w_size = 1 << s->w_bits;
225 s->w_mask = s->w_size - 1;
226
227 s->hash_bits = memLevel + 7;
228 s->hash_size = 1 << s->hash_bits;
229 s->hash_mask = s->hash_size - 1;
230 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
231
232 s->window = (Byte *) mem->window_memory;
233 s->prev = (Pos *) mem->prev_memory;
234 s->head = (Pos *) mem->head_memory;
235
236 s->lit_bufsize = 1 << (memLevel + 6);
237
238 overlay = (ush *) mem->overlay_memory;
239 s->pending_buf = (uch *) overlay;
240 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
241
242 s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
243 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
244
245 s->level = level;
246 s->strategy = strategy;
247 s->method = (Byte)method;
248
249 return zlib_deflateReset(strm);
250}
251
252
253#if 0
254int zlib_deflateSetDictionary(
255 z_streamp strm,
256 const Byte *dictionary,
257 uInt dictLength
258)
259{
260 deflate_state *s;
261 uInt length = dictLength;
262 uInt n;
263 IPos hash_head = 0;
264
265 if (strm == NULL || strm->state == NULL || dictionary == NULL)
266 return Z_STREAM_ERROR;
267
268 s = (deflate_state *) strm->state;
269 if (s->status != INIT_STATE) return Z_STREAM_ERROR;
270
271 strm->adler = zlib_adler32(strm->adler, dictionary, dictLength);
272
273 if (length < MIN_MATCH) return Z_OK;
274 if (length > MAX_DIST(s)) {
275 length = MAX_DIST(s);
276#ifndef USE_DICT_HEAD
277 dictionary += dictLength - length;
278#endif
279 }
280 memcpy((char *)s->window, dictionary, length);
281 s->strstart = length;
282 s->block_start = (long)length;
283
284
285
286
287
288 s->ins_h = s->window[0];
289 UPDATE_HASH(s, s->ins_h, s->window[1]);
290 for (n = 0; n <= length - MIN_MATCH; n++) {
291 INSERT_STRING(s, n, hash_head);
292 }
293 if (hash_head) hash_head = 0;
294 return Z_OK;
295}
296#endif
297
298
299int zlib_deflateReset(
300 z_streamp strm
301)
302{
303 deflate_state *s;
304
305 if (strm == NULL || strm->state == NULL)
306 return Z_STREAM_ERROR;
307
308 strm->total_in = strm->total_out = 0;
309 strm->msg = NULL;
310 strm->data_type = Z_UNKNOWN;
311
312 s = (deflate_state *)strm->state;
313 s->pending = 0;
314 s->pending_out = s->pending_buf;
315
316 if (s->noheader < 0) {
317 s->noheader = 0;
318 }
319 s->status = s->noheader ? BUSY_STATE : INIT_STATE;
320 strm->adler = 1;
321 s->last_flush = Z_NO_FLUSH;
322
323 zlib_tr_init(s);
324 lm_init(s);
325
326 return Z_OK;
327}
328
329
330#if 0
331int zlib_deflateParams(
332 z_streamp strm,
333 int level,
334 int strategy
335)
336{
337 deflate_state *s;
338 compress_func func;
339 int err = Z_OK;
340
341 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
342 s = (deflate_state *) strm->state;
343
344 if (level == Z_DEFAULT_COMPRESSION) {
345 level = 6;
346 }
347 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
348 return Z_STREAM_ERROR;
349 }
350 func = configuration_table[s->level].func;
351
352 if (func != configuration_table[level].func && strm->total_in != 0) {
353
354 err = zlib_deflate(strm, Z_PARTIAL_FLUSH);
355 }
356 if (s->level != level) {
357 s->level = level;
358 s->max_lazy_match = configuration_table[level].max_lazy;
359 s->good_match = configuration_table[level].good_length;
360 s->nice_match = configuration_table[level].nice_length;
361 s->max_chain_length = configuration_table[level].max_chain;
362 }
363 s->strategy = strategy;
364 return err;
365}
366#endif
367
368
369
370
371
372
373static void putShortMSB(
374 deflate_state *s,
375 uInt b
376)
377{
378 put_byte(s, (Byte)(b >> 8));
379 put_byte(s, (Byte)(b & 0xff));
380}
381
382
383
384
385
386
387
388static void flush_pending(
389 z_streamp strm
390)
391{
392 deflate_state *s = (deflate_state *) strm->state;
393 unsigned len = s->pending;
394
395 if (len > strm->avail_out) len = strm->avail_out;
396 if (len == 0) return;
397
398 if (strm->next_out != NULL) {
399 memcpy(strm->next_out, s->pending_out, len);
400 strm->next_out += len;
401 }
402 s->pending_out += len;
403 strm->total_out += len;
404 strm->avail_out -= len;
405 s->pending -= len;
406 if (s->pending == 0) {
407 s->pending_out = s->pending_buf;
408 }
409}
410
411
412int zlib_deflate(
413 z_streamp strm,
414 int flush
415)
416{
417 int old_flush;
418 deflate_state *s;
419
420 if (strm == NULL || strm->state == NULL ||
421 flush > Z_FINISH || flush < 0) {
422 return Z_STREAM_ERROR;
423 }
424 s = (deflate_state *) strm->state;
425
426 if ((strm->next_in == NULL && strm->avail_in != 0) ||
427 (s->status == FINISH_STATE && flush != Z_FINISH)) {
428 return Z_STREAM_ERROR;
429 }
430 if (strm->avail_out == 0) return Z_BUF_ERROR;
431
432 s->strm = strm;
433 old_flush = s->last_flush;
434 s->last_flush = flush;
435
436
437 if (s->status == INIT_STATE) {
438
439 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
440 uInt level_flags = (s->level-1) >> 1;
441
442 if (level_flags > 3) level_flags = 3;
443 header |= (level_flags << 6);
444 if (s->strstart != 0) header |= PRESET_DICT;
445 header += 31 - (header % 31);
446
447 s->status = BUSY_STATE;
448 putShortMSB(s, header);
449
450
451 if (s->strstart != 0) {
452 putShortMSB(s, (uInt)(strm->adler >> 16));
453 putShortMSB(s, (uInt)(strm->adler & 0xffff));
454 }
455 strm->adler = 1L;
456 }
457
458
459 if (s->pending != 0) {
460 flush_pending(strm);
461 if (strm->avail_out == 0) {
462
463
464
465
466
467
468 s->last_flush = -1;
469 return Z_OK;
470 }
471
472
473
474
475
476 } else if (strm->avail_in == 0 && flush <= old_flush &&
477 flush != Z_FINISH) {
478 return Z_BUF_ERROR;
479 }
480
481
482 if (s->status == FINISH_STATE && strm->avail_in != 0) {
483 return Z_BUF_ERROR;
484 }
485
486
487
488 if (strm->avail_in != 0 || s->lookahead != 0 ||
489 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
490 block_state bstate;
491
492 bstate = (*(configuration_table[s->level].func))(s, flush);
493
494 if (bstate == finish_started || bstate == finish_done) {
495 s->status = FINISH_STATE;
496 }
497 if (bstate == need_more || bstate == finish_started) {
498 if (strm->avail_out == 0) {
499 s->last_flush = -1;
500 }
501 return Z_OK;
502
503
504
505
506
507
508
509 }
510 if (bstate == block_done) {
511 if (flush == Z_PARTIAL_FLUSH) {
512 zlib_tr_align(s);
513 } else if (flush == Z_PACKET_FLUSH) {
514
515
516 zlib_tr_stored_type_only(s);
517 } else {
518 zlib_tr_stored_block(s, (char*)0, 0L, 0);
519
520
521
522 if (flush == Z_FULL_FLUSH) {
523 CLEAR_HASH(s);
524 }
525 }
526 flush_pending(strm);
527 if (strm->avail_out == 0) {
528 s->last_flush = -1;
529 return Z_OK;
530 }
531 }
532 }
533 Assert(strm->avail_out > 0, "bug2");
534
535 if (flush != Z_FINISH) return Z_OK;
536 if (s->noheader) return Z_STREAM_END;
537
538
539 putShortMSB(s, (uInt)(strm->adler >> 16));
540 putShortMSB(s, (uInt)(strm->adler & 0xffff));
541 flush_pending(strm);
542
543
544
545 s->noheader = -1;
546 return s->pending != 0 ? Z_OK : Z_STREAM_END;
547}
548
549
550int zlib_deflateEnd(
551 z_streamp strm
552)
553{
554 int status;
555 deflate_state *s;
556
557 if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
558 s = (deflate_state *) strm->state;
559
560 status = s->status;
561 if (status != INIT_STATE && status != BUSY_STATE &&
562 status != FINISH_STATE) {
563 return Z_STREAM_ERROR;
564 }
565
566 strm->state = NULL;
567
568 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
569}
570
571
572
573
574#if 0
575int zlib_deflateCopy (
576 z_streamp dest,
577 z_streamp source
578)
579{
580#ifdef MAXSEG_64K
581 return Z_STREAM_ERROR;
582#else
583 deflate_state *ds;
584 deflate_state *ss;
585 ush *overlay;
586 deflate_workspace *mem;
587
588
589 if (source == NULL || dest == NULL || source->state == NULL) {
590 return Z_STREAM_ERROR;
591 }
592
593 ss = (deflate_state *) source->state;
594
595 *dest = *source;
596
597 mem = (deflate_workspace *) dest->workspace;
598
599 ds = &(mem->deflate_memory);
600
601 dest->state = (struct internal_state *) ds;
602 *ds = *ss;
603 ds->strm = dest;
604
605 ds->window = (Byte *) mem->window_memory;
606 ds->prev = (Pos *) mem->prev_memory;
607 ds->head = (Pos *) mem->head_memory;
608 overlay = (ush *) mem->overlay_memory;
609 ds->pending_buf = (uch *) overlay;
610
611 memcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
612 memcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
613 memcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
614 memcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
615
616 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
617 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
618 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
619
620 ds->l_desc.dyn_tree = ds->dyn_ltree;
621 ds->d_desc.dyn_tree = ds->dyn_dtree;
622 ds->bl_desc.dyn_tree = ds->bl_tree;
623
624 return Z_OK;
625#endif
626}
627#endif
628
629
630
631
632
633
634
635
636static int read_buf(
637 z_streamp strm,
638 Byte *buf,
639 unsigned size
640)
641{
642 unsigned len = strm->avail_in;
643
644 if (len > size) len = size;
645 if (len == 0) return 0;
646
647 strm->avail_in -= len;
648
649 if (!((deflate_state *)(strm->state))->noheader) {
650 strm->adler = zlib_adler32(strm->adler, strm->next_in, len);
651 }
652 memcpy(buf, strm->next_in, len);
653 strm->next_in += len;
654 strm->total_in += len;
655
656 return (int)len;
657}
658
659
660
661
662static void lm_init(
663 deflate_state *s
664)
665{
666 s->window_size = (ulg)2L*s->w_size;
667
668 CLEAR_HASH(s);
669
670
671
672 s->max_lazy_match = configuration_table[s->level].max_lazy;
673 s->good_match = configuration_table[s->level].good_length;
674 s->nice_match = configuration_table[s->level].nice_length;
675 s->max_chain_length = configuration_table[s->level].max_chain;
676
677 s->strstart = 0;
678 s->block_start = 0L;
679 s->lookahead = 0;
680 s->match_length = s->prev_length = MIN_MATCH-1;
681 s->match_available = 0;
682 s->ins_h = 0;
683}
684
685
686
687
688
689
690
691
692
693
694
695
696
697static uInt longest_match(
698 deflate_state *s,
699 IPos cur_match
700)
701{
702 unsigned chain_length = s->max_chain_length;
703 register Byte *scan = s->window + s->strstart;
704 register Byte *match;
705 register int len;
706 int best_len = s->prev_length;
707 int nice_match = s->nice_match;
708 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
709 s->strstart - (IPos)MAX_DIST(s) : NIL;
710
711
712
713 Pos *prev = s->prev;
714 uInt wmask = s->w_mask;
715
716#ifdef UNALIGNED_OK
717
718
719
720 register Byte *strend = s->window + s->strstart + MAX_MATCH - 1;
721 register ush scan_start = *(ush*)scan;
722 register ush scan_end = *(ush*)(scan+best_len-1);
723#else
724 register Byte *strend = s->window + s->strstart + MAX_MATCH;
725 register Byte scan_end1 = scan[best_len-1];
726 register Byte scan_end = scan[best_len];
727#endif
728
729
730
731
732 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
733
734
735 if (s->prev_length >= s->good_match) {
736 chain_length >>= 2;
737 }
738
739
740
741 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
742
743 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
744
745 do {
746 Assert(cur_match < s->strstart, "no future");
747 match = s->window + cur_match;
748
749
750
751
752#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
753
754
755
756 if (*(ush*)(match+best_len-1) != scan_end ||
757 *(ush*)match != scan_start) continue;
758
759
760
761
762
763
764
765
766
767
768 Assert(scan[2] == match[2], "scan[2]?");
769 scan++, match++;
770 do {
771 } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
772 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
773 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
774 *(ush*)(scan+=2) == *(ush*)(match+=2) &&
775 scan < strend);
776
777
778
779 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
780 if (*scan == *match) scan++;
781
782 len = (MAX_MATCH - 1) - (int)(strend-scan);
783 scan = strend - (MAX_MATCH-1);
784
785#else
786
787 if (match[best_len] != scan_end ||
788 match[best_len-1] != scan_end1 ||
789 *match != *scan ||
790 *++match != scan[1]) continue;
791
792
793
794
795
796
797
798 scan += 2, match++;
799 Assert(*scan == *match, "match[2]?");
800
801
802
803
804 do {
805 } while (*++scan == *++match && *++scan == *++match &&
806 *++scan == *++match && *++scan == *++match &&
807 *++scan == *++match && *++scan == *++match &&
808 *++scan == *++match && *++scan == *++match &&
809 scan < strend);
810
811 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
812
813 len = MAX_MATCH - (int)(strend - scan);
814 scan = strend - MAX_MATCH;
815
816#endif
817
818 if (len > best_len) {
819 s->match_start = cur_match;
820 best_len = len;
821 if (len >= nice_match) break;
822#ifdef UNALIGNED_OK
823 scan_end = *(ush*)(scan+best_len-1);
824#else
825 scan_end1 = scan[best_len-1];
826 scan_end = scan[best_len];
827#endif
828 }
829 } while ((cur_match = prev[cur_match & wmask]) > limit
830 && --chain_length != 0);
831
832 if ((uInt)best_len <= s->lookahead) return best_len;
833 return s->lookahead;
834}
835
836#ifdef DEBUG_ZLIB
837
838
839
840static void check_match(
841 deflate_state *s,
842 IPos start,
843 IPos match,
844 int length
845)
846{
847
848 if (memcmp((char *)s->window + match,
849 (char *)s->window + start, length) != EQUAL) {
850 fprintf(stderr, " start %u, match %u, length %d\n",
851 start, match, length);
852 do {
853 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
854 } while (--length != 0);
855 z_error("invalid match");
856 }
857 if (z_verbose > 1) {
858 fprintf(stderr,"\\[%d,%d]", start-match, length);
859 do { putc(s->window[start++], stderr); } while (--length != 0);
860 }
861}
862#else
863# define check_match(s, start, match, length)
864#endif
865
866
867
868
869
870
871
872
873
874
875
876static void fill_window(
877 deflate_state *s
878)
879{
880 register unsigned n, m;
881 register Pos *p;
882 unsigned more;
883 uInt wsize = s->w_size;
884
885 do {
886 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
887
888
889 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
890 more = wsize;
891
892 } else if (more == (unsigned)(-1)) {
893
894
895
896 more--;
897
898
899
900
901 } else if (s->strstart >= wsize+MAX_DIST(s)) {
902
903 memcpy((char *)s->window, (char *)s->window+wsize,
904 (unsigned)wsize);
905 s->match_start -= wsize;
906 s->strstart -= wsize;
907 s->block_start -= (long) wsize;
908
909
910
911
912
913
914
915 n = s->hash_size;
916 p = &s->head[n];
917 do {
918 m = *--p;
919 *p = (Pos)(m >= wsize ? m-wsize : NIL);
920 } while (--n);
921
922 n = wsize;
923 p = &s->prev[n];
924 do {
925 m = *--p;
926 *p = (Pos)(m >= wsize ? m-wsize : NIL);
927
928
929
930 } while (--n);
931 more += wsize;
932 }
933 if (s->strm->avail_in == 0) return;
934
935
936
937
938
939
940
941
942
943
944
945
946 Assert(more >= 2, "more < 2");
947
948 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
949 s->lookahead += n;
950
951
952 if (s->lookahead >= MIN_MATCH) {
953 s->ins_h = s->window[s->strstart];
954 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
955#if MIN_MATCH != 3
956 Call UPDATE_HASH() MIN_MATCH-3 more times
957#endif
958 }
959
960
961
962
963 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
964}
965
966
967
968
969
970#define FLUSH_BLOCK_ONLY(s, eof) { \
971 zlib_tr_flush_block(s, (s->block_start >= 0L ? \
972 (char *)&s->window[(unsigned)s->block_start] : \
973 NULL), \
974 (ulg)((long)s->strstart - s->block_start), \
975 (eof)); \
976 s->block_start = s->strstart; \
977 flush_pending(s->strm); \
978 Tracev((stderr,"[FLUSH]")); \
979}
980
981
982#define FLUSH_BLOCK(s, eof) { \
983 FLUSH_BLOCK_ONLY(s, eof); \
984 if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
985}
986
987
988
989
990
991
992
993
994
995
996static block_state deflate_stored(
997 deflate_state *s,
998 int flush
999)
1000{
1001
1002
1003
1004 ulg max_block_size = 0xffff;
1005 ulg max_start;
1006
1007 if (max_block_size > s->pending_buf_size - 5) {
1008 max_block_size = s->pending_buf_size - 5;
1009 }
1010
1011
1012 for (;;) {
1013
1014 if (s->lookahead <= 1) {
1015
1016 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1017 s->block_start >= (long)s->w_size, "slide too late");
1018
1019 fill_window(s);
1020 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1021
1022 if (s->lookahead == 0) break;
1023 }
1024 Assert(s->block_start >= 0L, "block gone");
1025
1026 s->strstart += s->lookahead;
1027 s->lookahead = 0;
1028
1029
1030 max_start = s->block_start + max_block_size;
1031 if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1032
1033 s->lookahead = (uInt)(s->strstart - max_start);
1034 s->strstart = (uInt)max_start;
1035 FLUSH_BLOCK(s, 0);
1036 }
1037
1038
1039
1040 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1041 FLUSH_BLOCK(s, 0);
1042 }
1043 }
1044 FLUSH_BLOCK(s, flush == Z_FINISH);
1045 return flush == Z_FINISH ? finish_done : block_done;
1046}
1047
1048
1049
1050
1051
1052
1053
1054
1055static block_state deflate_fast(
1056 deflate_state *s,
1057 int flush
1058)
1059{
1060 IPos hash_head = NIL;
1061 int bflush;
1062
1063 for (;;) {
1064
1065
1066
1067
1068
1069 if (s->lookahead < MIN_LOOKAHEAD) {
1070 fill_window(s);
1071 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1072 return need_more;
1073 }
1074 if (s->lookahead == 0) break;
1075 }
1076
1077
1078
1079
1080 if (s->lookahead >= MIN_MATCH) {
1081 INSERT_STRING(s, s->strstart, hash_head);
1082 }
1083
1084
1085
1086
1087 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1088
1089
1090
1091
1092 if (s->strategy != Z_HUFFMAN_ONLY) {
1093 s->match_length = longest_match (s, hash_head);
1094 }
1095
1096 }
1097 if (s->match_length >= MIN_MATCH) {
1098 check_match(s, s->strstart, s->match_start, s->match_length);
1099
1100 bflush = zlib_tr_tally(s, s->strstart - s->match_start,
1101 s->match_length - MIN_MATCH);
1102
1103 s->lookahead -= s->match_length;
1104
1105
1106
1107
1108 if (s->match_length <= s->max_insert_length &&
1109 s->lookahead >= MIN_MATCH) {
1110 s->match_length--;
1111 do {
1112 s->strstart++;
1113 INSERT_STRING(s, s->strstart, hash_head);
1114
1115
1116
1117 } while (--s->match_length != 0);
1118 s->strstart++;
1119 } else {
1120 s->strstart += s->match_length;
1121 s->match_length = 0;
1122 s->ins_h = s->window[s->strstart];
1123 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1124#if MIN_MATCH != 3
1125 Call UPDATE_HASH() MIN_MATCH-3 more times
1126#endif
1127
1128
1129
1130 }
1131 } else {
1132
1133 Tracevv((stderr,"%c", s->window[s->strstart]));
1134 bflush = zlib_tr_tally (s, 0, s->window[s->strstart]);
1135 s->lookahead--;
1136 s->strstart++;
1137 }
1138 if (bflush) FLUSH_BLOCK(s, 0);
1139 }
1140 FLUSH_BLOCK(s, flush == Z_FINISH);
1141 return flush == Z_FINISH ? finish_done : block_done;
1142}
1143
1144
1145
1146
1147
1148
1149static block_state deflate_slow(
1150 deflate_state *s,
1151 int flush
1152)
1153{
1154 IPos hash_head = NIL;
1155 int bflush;
1156
1157
1158 for (;;) {
1159
1160
1161
1162
1163
1164 if (s->lookahead < MIN_LOOKAHEAD) {
1165 fill_window(s);
1166 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1167 return need_more;
1168 }
1169 if (s->lookahead == 0) break;
1170 }
1171
1172
1173
1174
1175 if (s->lookahead >= MIN_MATCH) {
1176 INSERT_STRING(s, s->strstart, hash_head);
1177 }
1178
1179
1180
1181 s->prev_length = s->match_length, s->prev_match = s->match_start;
1182 s->match_length = MIN_MATCH-1;
1183
1184 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1185 s->strstart - hash_head <= MAX_DIST(s)) {
1186
1187
1188
1189
1190 if (s->strategy != Z_HUFFMAN_ONLY) {
1191 s->match_length = longest_match (s, hash_head);
1192 }
1193
1194
1195 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1196 (s->match_length == MIN_MATCH &&
1197 s->strstart - s->match_start > TOO_FAR))) {
1198
1199
1200
1201
1202 s->match_length = MIN_MATCH-1;
1203 }
1204 }
1205
1206
1207
1208 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1209 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1210
1211
1212 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1213
1214 bflush = zlib_tr_tally(s, s->strstart -1 - s->prev_match,
1215 s->prev_length - MIN_MATCH);
1216
1217
1218
1219
1220
1221
1222 s->lookahead -= s->prev_length-1;
1223 s->prev_length -= 2;
1224 do {
1225 if (++s->strstart <= max_insert) {
1226 INSERT_STRING(s, s->strstart, hash_head);
1227 }
1228 } while (--s->prev_length != 0);
1229 s->match_available = 0;
1230 s->match_length = MIN_MATCH-1;
1231 s->strstart++;
1232
1233 if (bflush) FLUSH_BLOCK(s, 0);
1234
1235 } else if (s->match_available) {
1236
1237
1238
1239
1240 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1241 if (zlib_tr_tally (s, 0, s->window[s->strstart-1])) {
1242 FLUSH_BLOCK_ONLY(s, 0);
1243 }
1244 s->strstart++;
1245 s->lookahead--;
1246 if (s->strm->avail_out == 0) return need_more;
1247 } else {
1248
1249
1250
1251 s->match_available = 1;
1252 s->strstart++;
1253 s->lookahead--;
1254 }
1255 }
1256 Assert (flush != Z_NO_FLUSH, "no flush?");
1257 if (s->match_available) {
1258 Tracevv((stderr,"%c", s->window[s->strstart-1]));
1259 zlib_tr_tally (s, 0, s->window[s->strstart-1]);
1260 s->match_available = 0;
1261 }
1262 FLUSH_BLOCK(s, flush == Z_FINISH);
1263 return flush == Z_FINISH ? finish_done : block_done;
1264}
1265
1266int zlib_deflate_workspacesize(int windowBits, int memLevel)
1267{
1268 if (windowBits < 0)
1269 windowBits = -windowBits;
1270
1271
1272 BUG_ON(memLevel < 1 || memLevel > MAX_MEM_LEVEL || windowBits < 9 ||
1273 windowBits > 15);
1274
1275 return sizeof(deflate_workspace)
1276 + zlib_deflate_window_memsize(windowBits)
1277 + zlib_deflate_prev_memsize(windowBits)
1278 + zlib_deflate_head_memsize(memLevel)
1279 + zlib_deflate_overlay_memsize(memLevel);
1280}
1281