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