123
128
compress_func func;
132
local const config configuration_table[2] = {
133
/* good lazy nice chain */
134
/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
135
/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
126
137
local const config configuration_table[10] = {
127
138
/* good lazy nice chain */
128
139
/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
129
/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
140
/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
130
141
/* 2 */ {4, 5, 16, 8, deflate_fast},
131
142
/* 3 */ {4, 6, 32, 32, deflate_fast},
222
236
if (version == Z_NULL || version[0] != my_version[0] ||
223
237
stream_size != sizeof(z_stream)) {
224
return Z_VERSION_ERROR;
238
return Z_VERSION_ERROR;
226
240
if (strm == Z_NULL) return Z_STREAM_ERROR;
228
242
strm->msg = Z_NULL;
229
if (strm->zalloc == Z_NULL) {
230
strm->zalloc = zcalloc;
231
strm->opaque = (voidpf)0;
243
if (strm->zalloc == (alloc_func)0) {
244
strm->zalloc = zcalloc;
245
strm->opaque = (voidpf)0;
233
if (strm->zfree == Z_NULL) strm->zfree = zcfree;
247
if (strm->zfree == (free_func)0) strm->zfree = zcfree;
250
if (level != 0) level = 1;
235
252
if (level == Z_DEFAULT_COMPRESSION) level = 6;
240
if (windowBits < 0) { /* undocumented feature: suppress zlib header */
255
if (windowBits < 0) { /* suppress zlib wrapper */
242
257
windowBits = -windowBits;
260
else if (windowBits > 15) {
261
wrap = 2; /* write gzip wrapper instead */
244
265
if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
245
windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
246
strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
266
windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
267
strategy < 0 || strategy > Z_FIXED) {
247
268
return Z_STREAM_ERROR;
270
if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
249
271
s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
250
272
if (s == Z_NULL) return Z_MEM_ERROR;
251
273
strm->state = (struct internal_state FAR *)s;
254
s->noheader = noheader;
255
278
s->w_bits = windowBits;
256
279
s->w_size = 1 << s->w_bits;
257
280
s->w_mask = s->w_size - 1;
371
425
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
374
if (level == Z_DEFAULT_COMPRESSION) {
377
if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
378
return Z_STREAM_ERROR;
429
if (level != 0) level = 1;
431
if (level == Z_DEFAULT_COMPRESSION) level = 6;
433
if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
434
return Z_STREAM_ERROR;
380
436
func = configuration_table[s->level].func;
382
438
if (func != configuration_table[level].func && strm->total_in != 0) {
383
/* Flush the last buffer: */
384
err = deflate(strm, Z_PARTIAL_FLUSH);
439
/* Flush the last buffer: */
440
err = deflate(strm, Z_PARTIAL_FLUSH);
386
442
if (s->level != level) {
388
s->max_lazy_match = configuration_table[level].max_lazy;
389
s->good_match = configuration_table[level].good_length;
390
s->nice_match = configuration_table[level].nice_length;
391
s->max_chain_length = configuration_table[level].max_chain;
444
s->max_lazy_match = configuration_table[level].max_lazy;
445
s->good_match = configuration_table[level].good_length;
446
s->nice_match = configuration_table[level].nice_length;
447
s->max_chain_length = configuration_table[level].max_chain;
393
449
s->strategy = strategy;
453
/* ========================================================================= */
454
int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
463
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
465
s->good_match = good_length;
466
s->max_lazy_match = max_lazy;
467
s->nice_match = nice_length;
468
s->max_chain_length = max_chain;
472
/* =========================================================================
473
* For the default windowBits of 15 and memLevel of 8, this function returns
474
* a close to exact, as well as small, upper bound on the compressed size.
475
* They are coded as constants here for a reason--if the #define's are
476
* changed, then this function needs to be changed as well. The return
477
* value for 15 and 8 only works for those exact settings.
479
* For any setting other than those defaults for windowBits and memLevel,
480
* the value returned is a conservative worst case for the maximum expansion
481
* resulting from using fixed blocks instead of stored blocks, which deflate
482
* can emit on compressed data for some combinations of the parameters.
484
* This function could be more sophisticated to provide closer upper bounds
485
* for every combination of windowBits and memLevel, as well as wrap.
486
* But even the conservative upper bound of about 14% expansion does not
487
* seem onerous for output buffer allocation.
489
uLong ZEXPORT deflateBound(strm, sourceLen)
496
/* conservative upper bound */
497
destLen = sourceLen +
498
((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
500
/* if can't get parameters, return conservative bound */
501
if (strm == Z_NULL || strm->state == Z_NULL)
504
/* if not default parameters, return conservative bound */
506
if (s->w_bits != 15 || s->hash_bits != 8 + 7)
509
/* default settings: return tight bound for that case */
510
return compressBound(sourceLen);
397
513
/* =========================================================================
398
514
* Put a short in the pending buffer. The 16-bit value is put in MSB order.
399
515
* IN assertion: the stream state is correct and there is enough room in
457
573
old_flush = s->last_flush;
458
574
s->last_flush = flush;
460
/* Write the zlib header */
576
/* Write the header */
461
577
if (s->status == INIT_STATE) {
463
uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
464
uInt level_flags = (s->level-1) >> 1;
466
if (level_flags > 3) level_flags = 3;
467
header |= (level_flags << 6);
468
if (s->strstart != 0) header |= PRESET_DICT;
469
header += 31 - (header % 31);
471
s->status = BUSY_STATE;
472
putShortMSB(s, header);
474
/* Save the adler32 of the preset dictionary: */
475
if (s->strstart != 0) {
476
putShortMSB(s, (uInt)(strm->adler >> 16));
477
putShortMSB(s, (uInt)(strm->adler & 0xffff));
580
strm->adler = crc32(0L, Z_NULL, 0);
584
if (s->gzhead == NULL) {
590
put_byte(s, s->level == 9 ? 2 :
591
(s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
593
put_byte(s, OS_CODE);
594
s->status = BUSY_STATE;
597
put_byte(s, (s->gzhead->text ? 1 : 0) +
598
(s->gzhead->hcrc ? 2 : 0) +
599
(s->gzhead->extra == Z_NULL ? 0 : 4) +
600
(s->gzhead->name == Z_NULL ? 0 : 8) +
601
(s->gzhead->comment == Z_NULL ? 0 : 16)
603
put_byte(s, (Byte)(s->gzhead->time & 0xff));
604
put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
605
put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
606
put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
607
put_byte(s, s->level == 9 ? 2 :
608
(s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
610
put_byte(s, s->gzhead->os & 0xff);
611
if (s->gzhead->extra != NULL) {
612
put_byte(s, s->gzhead->extra_len & 0xff);
613
put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
616
strm->adler = crc32(strm->adler, s->pending_buf,
619
s->status = EXTRA_STATE;
625
uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
628
if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
630
else if (s->level < 6)
632
else if (s->level == 6)
636
header |= (level_flags << 6);
637
if (s->strstart != 0) header |= PRESET_DICT;
638
header += 31 - (header % 31);
640
s->status = BUSY_STATE;
641
putShortMSB(s, header);
643
/* Save the adler32 of the preset dictionary: */
644
if (s->strstart != 0) {
645
putShortMSB(s, (uInt)(strm->adler >> 16));
646
putShortMSB(s, (uInt)(strm->adler & 0xffff));
648
strm->adler = adler32(0L, Z_NULL, 0);
652
if (s->status == EXTRA_STATE) {
653
if (s->gzhead->extra != NULL) {
654
uInt beg = s->pending; /* start of bytes to update crc */
656
while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
657
if (s->pending == s->pending_buf_size) {
658
if (s->gzhead->hcrc && s->pending > beg)
659
strm->adler = crc32(strm->adler, s->pending_buf + beg,
663
if (s->pending == s->pending_buf_size)
666
put_byte(s, s->gzhead->extra[s->gzindex]);
669
if (s->gzhead->hcrc && s->pending > beg)
670
strm->adler = crc32(strm->adler, s->pending_buf + beg,
672
if (s->gzindex == s->gzhead->extra_len) {
674
s->status = NAME_STATE;
678
s->status = NAME_STATE;
680
if (s->status == NAME_STATE) {
681
if (s->gzhead->name != NULL) {
682
uInt beg = s->pending; /* start of bytes to update crc */
686
if (s->pending == s->pending_buf_size) {
687
if (s->gzhead->hcrc && s->pending > beg)
688
strm->adler = crc32(strm->adler, s->pending_buf + beg,
692
if (s->pending == s->pending_buf_size) {
697
val = s->gzhead->name[s->gzindex++];
700
if (s->gzhead->hcrc && s->pending > beg)
701
strm->adler = crc32(strm->adler, s->pending_buf + beg,
705
s->status = COMMENT_STATE;
709
s->status = COMMENT_STATE;
711
if (s->status == COMMENT_STATE) {
712
if (s->gzhead->comment != NULL) {
713
uInt beg = s->pending; /* start of bytes to update crc */
717
if (s->pending == s->pending_buf_size) {
718
if (s->gzhead->hcrc && s->pending > beg)
719
strm->adler = crc32(strm->adler, s->pending_buf + beg,
723
if (s->pending == s->pending_buf_size) {
728
val = s->gzhead->comment[s->gzindex++];
731
if (s->gzhead->hcrc && s->pending > beg)
732
strm->adler = crc32(strm->adler, s->pending_buf + beg,
735
s->status = HCRC_STATE;
738
s->status = HCRC_STATE;
740
if (s->status == HCRC_STATE) {
741
if (s->gzhead->hcrc) {
742
if (s->pending + 2 > s->pending_buf_size)
744
if (s->pending + 2 <= s->pending_buf_size) {
745
put_byte(s, (Byte)(strm->adler & 0xff));
746
put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
747
strm->adler = crc32(0L, Z_NULL, 0);
748
s->status = BUSY_STATE;
752
s->status = BUSY_STATE;
482
756
/* Flush as much pending output as possible */
483
757
if (s->pending != 0) {
484
758
flush_pending(strm);
485
759
if (strm->avail_out == 0) {
486
/* Since avail_out is 0, deflate will be called again with
487
* more output space, but possibly with both pending and
488
* avail_in equal to zero. There won't be anything to do,
489
* but this is not an error situation so make sure we
490
* return OK instead of BUF_ERROR at next call of deflate:
760
/* Since avail_out is 0, deflate will be called again with
761
* more output space, but possibly with both pending and
762
* avail_in equal to zero. There won't be anything to do,
763
* but this is not an error situation so make sure we
764
* return OK instead of BUF_ERROR at next call of deflate:
496
770
/* Make sure there is something to do and avoid duplicate consecutive
497
771
* flushes. For repeated and useless calls with Z_FINISH, we keep
498
* returning Z_STREAM_END instead of Z_BUFF_ERROR.
772
* returning Z_STREAM_END instead of Z_BUF_ERROR.
500
774
} else if (strm->avail_in == 0 && flush <= old_flush &&
502
776
ERR_RETURN(strm, Z_BUF_ERROR);
513
787
(flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
514
788
block_state bstate;
516
bstate = (*(configuration_table[s->level].func))(s, flush);
790
bstate = (*(configuration_table[s->level].func))(s, flush);
518
792
if (bstate == finish_started || bstate == finish_done) {
519
793
s->status = FINISH_STATE;
521
795
if (bstate == need_more || bstate == finish_started) {
522
if (strm->avail_out == 0) {
523
s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
526
/* If flush != Z_NO_FLUSH && avail_out == 0, the next call
527
* of deflate should use the same flush parameter to make sure
528
* that the flush is complete. So we don't have to output an
529
* empty block here, this will be done at next call. This also
530
* ensures that for a very small output buffer, we emit at most
796
if (strm->avail_out == 0) {
797
s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
800
/* If flush != Z_NO_FLUSH && avail_out == 0, the next call
801
* of deflate should use the same flush parameter to make sure
802
* that the flush is complete. So we don't have to output an
803
* empty block here, this will be done at next call. This also
804
* ensures that for a very small output buffer, we emit at most
534
808
if (bstate == block_done) {
535
809
if (flush == Z_PARTIAL_FLUSH) {
546
820
flush_pending(strm);
547
if (strm->avail_out == 0) {
548
s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
821
if (strm->avail_out == 0) {
822
s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
553
827
Assert(strm->avail_out > 0, "bug2");
555
829
if (flush != Z_FINISH) return Z_OK;
556
if (s->noheader) return Z_STREAM_END;
830
if (s->wrap <= 0) return Z_STREAM_END;
558
/* Write the zlib trailer (adler32) */
559
putShortMSB(s, (uInt)(strm->adler >> 16));
560
putShortMSB(s, (uInt)(strm->adler & 0xffff));
832
/* Write the trailer */
835
put_byte(s, (Byte)(strm->adler & 0xff));
836
put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
837
put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
838
put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
839
put_byte(s, (Byte)(strm->total_in & 0xff));
840
put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
841
put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
842
put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
847
putShortMSB(s, (uInt)(strm->adler >> 16));
848
putShortMSB(s, (uInt)(strm->adler & 0xffff));
561
850
flush_pending(strm);
562
851
/* If avail_out is zero, the application will call deflate again
563
852
* to flush the rest.
565
s->noheader = -1; /* write the trailer only once! */
854
if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
566
855
return s->pending != 0 ? Z_OK : Z_STREAM_END;
1098
1407
if (s->lookahead <= 1) {
1100
1409
Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1101
s->block_start >= (long)s->w_size, "slide too late");
1410
s->block_start >= (long)s->w_size, "slide too late");
1103
1412
fill_window(s);
1104
1413
if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1106
1415
if (s->lookahead == 0) break; /* flush the current block */
1108
Assert(s->block_start >= 0L, "block gone");
1110
s->strstart += s->lookahead;
1113
/* Emit a stored block if pending_buf will be full: */
1114
max_start = s->block_start + max_block_size;
1417
Assert(s->block_start >= 0L, "block gone");
1419
s->strstart += s->lookahead;
1422
/* Emit a stored block if pending_buf will be full: */
1423
max_start = s->block_start + max_block_size;
1115
1424
if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1116
/* strstart == 0 is possible when wraparound on 16-bit machine */
1117
s->lookahead = (uInt)(s->strstart - max_start);
1118
s->strstart = (uInt)max_start;
1425
/* strstart == 0 is possible when wraparound on 16-bit machine */
1426
s->lookahead = (uInt)(s->strstart - max_start);
1427
s->strstart = (uInt)max_start;
1119
1428
FLUSH_BLOCK(s, 0);
1121
/* Flush if we may have to slide, otherwise block_start may become
1430
/* Flush if we may have to slide, otherwise block_start may become
1122
1431
* negative and the data will be gone:
1124
1433
if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1125
1434
FLUSH_BLOCK(s, 0);
1128
1437
FLUSH_BLOCK(s, flush == Z_FINISH);
1129
1438
return flush == Z_FINISH ? finish_done : block_done;
1348
1672
FLUSH_BLOCK(s, flush == Z_FINISH);
1349
1673
return flush == Z_FINISH ? finish_done : block_done;
1675
#endif /* FASTEST */
1678
/* ===========================================================================
1679
* For Z_RLE, simply look for runs of bytes, generate matches only of distance
1680
* one. Do not maintain a hash table. (It will be regenerated if this run of
1681
* deflate switches away from Z_RLE.)
1683
local block_state deflate_rle(s, flush)
1687
int bflush; /* set if current block must be flushed */
1688
uInt run; /* length of run */
1689
uInt max; /* maximum length of run */
1690
uInt prev; /* byte at distance one to match */
1691
Bytef *scan; /* scan for end of run */
1694
/* Make sure that we always have enough lookahead, except
1695
* at the end of the input file. We need MAX_MATCH bytes
1696
* for the longest encodable run.
1698
if (s->lookahead < MAX_MATCH) {
1700
if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
1703
if (s->lookahead == 0) break; /* flush the current block */
1706
/* See how many times the previous byte repeats */
1708
if (s->strstart > 0) { /* if there is a previous byte, that is */
1709
max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
1710
scan = s->window + s->strstart - 1;
1713
if (*scan++ != prev)
1715
} while (++run < max);
1718
/* Emit match if have run of MIN_MATCH or longer, else emit literal */
1719
if (run >= MIN_MATCH) {
1720
check_match(s, s->strstart, s->strstart - 1, run);
1721
_tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
1722
s->lookahead -= run;
1725
/* No match, output a literal byte */
1726
Tracevv((stderr,"%c", s->window[s->strstart]));
1727
_tr_tally_lit (s, s->window[s->strstart], bflush);
1731
if (bflush) FLUSH_BLOCK(s, 0);
1733
FLUSH_BLOCK(s, flush == Z_FINISH);
1734
return flush == Z_FINISH ? finish_done : block_done;