abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
deflate.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc/util/abc_global.h"
#include "deflate.h"

Go to the source code of this file.

Data Structures

struct  config_s
 
struct  static_tree_desc_s
 

Macros

#define NIL   0
 
#define TOO_FAR   4096
 
#define EQUAL   0
 
#define UPDATE_HASH(s, h, c)   (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
 
#define INSERT_STRING(s, str, match_head)
 
#define CLEAR_HASH(s)
 
#define check_match(s, start, match, length)
 
#define FLUSH_BLOCK_ONLY(s, last)
 
#define FLUSH_BLOCK(s, last)
 

Typedefs

typedef block_state compress_func OF ((deflate_state *s, int flush))
 
typedef struct config_s config
 

Enumerations

enum  block_state { need_more, block_done, finish_started, finish_done }
 

Functions

local void fill_window OF ((deflate_state *s))
 
local block_state deflate_stored OF ((deflate_state *s, int flush))
 
local void putShortMSB OF ((deflate_state *s, uInt b))
 
local void flush_pending OF ((z_streamp strm))
 
local int read_buf OF ((z_streamp strm, Bytef *buf, unsigned size))
 
local uInt longest_match OF ((deflate_state *s, IPos cur_match))
 
int ZEXPORT deflateInit_ (z_streamp strm, int level, const char *version, int stream_size)
 
int ZEXPORT deflateInit2_ (z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size)
 
int ZEXPORT deflateSetDictionary (z_streamp strm, const Bytef *dictionary, uInt dictLength)
 
int ZEXPORT deflateReset (z_streamp strm)
 
int ZEXPORT deflateSetHeader (z_streamp strm, gz_headerp head)
 
int ZEXPORT deflatePrime (z_streamp strm, int bits, int value)
 
int ZEXPORT deflateParams (z_streamp strm, int level, int strategy)
 
int ZEXPORT deflateTune (z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
 
uLong ZEXPORT deflateBound (z_streamp strm, uLong sourceLen)
 
local void putShortMSB (deflate_state *s, uInt b)
 
local void flush_pending (z_streamp strm)
 
int ZEXPORT deflate (z_streamp strm, int flush)
 
int ZEXPORT deflateEnd (z_streamp strm)
 
int ZEXPORT deflateCopy (z_streamp dest, z_streamp source)
 
local int read_buf (z_streamp strm, Bytef *buf, unsigned size)
 
local void lm_init (deflate_state *s)
 
local uInt longest_match (deflate_state *s, IPos cur_match)
 
local void fill_window (deflate_state *s)
 
local block_state deflate_stored (deflate_state *s, int flush)
 
local block_state deflate_fast (deflate_state *s, int flush)
 
local block_state deflate_slow (deflate_state *s, int flush)
 
local block_state deflate_rle (deflate_state *s, int flush)
 
local block_state deflate_huff (deflate_state *s, int flush)
 

Variables

ABC_NAMESPACE_IMPL_START const char deflate_copyright []
 
local const config configuration_table [10]
 

Macro Definition Documentation

#define check_match (   s,
  start,
  match,
  length 
)

Definition at line 1249 of file deflate.c.

#define CLEAR_HASH (   s)
Value:
s->head[s->hash_size-1] = NIL; \
zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
Byte FAR Bytef
Definition: zconf.h:342
#define NIL
Definition: deflate.c:111
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
Definition: zutil.c:175

Definition at line 200 of file deflate.c.

#define EQUAL   0

Definition at line 158 of file deflate.c.

#define FLUSH_BLOCK (   s,
  last 
)
Value:
{ \
if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
}
if(last==0)
Definition: sparse_int.h:34
#define FLUSH_BLOCK_ONLY(s, last)
Definition: deflate.c:1392

Definition at line 1404 of file deflate.c.

#define FLUSH_BLOCK_ONLY (   s,
  last 
)
Value:
{ \
_tr_flush_block(s, (s->block_start >= 0L ? \
(charf *)&s->window[(unsigned)s->block_start] : \
(charf *)Z_NULL), \
(ulg)((long)s->strstart - s->block_start), \
(last)); \
s->block_start = s->strstart; \
flush_pending(s->strm); \
Tracev((stderr,"[FLUSH]")); \
}
local void flush_pending(z_streamp strm)
Definition: deflate.c:536
#define Tracev(x)
Definition: zutil.h:270
unsigned long ulg
Definition: zutil.h:43
void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, ulg stored_len, int last)
Definition: trees.c:910
#define Z_NULL
Definition: zlib.h:216
char FAR charf
Definition: zconf.h:344

Definition at line 1392 of file deflate.c.

#define INSERT_STRING (   s,
  str,
  match_head 
)
Value:
(UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
s->head[s->ins_h] = (Pos)(str))
#define MIN_MATCH
Definition: zutil.h:73
ush Pos
Definition: deflate.h:88
#define UPDATE_HASH(s, h, c)
Definition: deflate.c:171

Definition at line 190 of file deflate.c.

#define NIL   0

Definition at line 111 of file deflate.c.

#define TOO_FAR   4096

Definition at line 115 of file deflate.c.

#define UPDATE_HASH (   s,
  h,
 
)    (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)

Definition at line 171 of file deflate.c.

Typedef Documentation

typedef struct config_s config

Definition at line 80 of file deflate.c.

Enumeration Type Documentation

Enumerator
need_more 
block_done 
finish_started 
finish_done 

Definition at line 73 of file deflate.c.

73  {
74  need_more, /* block not completed, need more input or more output */
75  block_done, /* block flush performed */
76  finish_started, /* finish started, need only more output at next deflate */
77  finish_done /* finish done, accept no more input or output */
78 } block_state;
block_state
Definition: deflate.c:73

Function Documentation

int ZEXPORT deflate ( z_streamp  strm,
int  flush 
)

Definition at line 555 of file deflate.c.

556 {
557  int old_flush; /* value of flush param for previous deflate call */
558  deflate_state *s;
559 
560  if (strm == Z_NULL || strm->state == Z_NULL ||
561  flush > Z_BLOCK || flush < 0) {
562  return Z_STREAM_ERROR;
563  }
564  s = strm->state;
565 
566  if (strm->next_out == Z_NULL ||
567  (strm->next_in == Z_NULL && strm->avail_in != 0) ||
568  (s->status == FINISH_STATE && flush != Z_FINISH)) {
569  ERR_RETURN(strm, Z_STREAM_ERROR);
570  }
571  if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
572 
573  s->strm = strm; /* just in case */
574  old_flush = s->last_flush;
575  s->last_flush = flush;
576 
577  /* Write the header */
578  if (s->status == INIT_STATE) {
579 #ifdef GZIP
580  if (s->wrap == 2) {
581  strm->adler = crc32(0L, Z_NULL, 0);
582  put_byte(s, 31);
583  put_byte(s, 139);
584  put_byte(s, 8);
585  if (s->gzhead == Z_NULL) {
586  put_byte(s, 0);
587  put_byte(s, 0);
588  put_byte(s, 0);
589  put_byte(s, 0);
590  put_byte(s, 0);
591  put_byte(s, s->level == 9 ? 2 :
592  (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
593  4 : 0));
594  put_byte(s, OS_CODE);
595  s->status = BUSY_STATE;
596  }
597  else {
598  put_byte(s, (s->gzhead->text ? 1 : 0) +
599  (s->gzhead->hcrc ? 2 : 0) +
600  (s->gzhead->extra == Z_NULL ? 0 : 4) +
601  (s->gzhead->name == Z_NULL ? 0 : 8) +
602  (s->gzhead->comment == Z_NULL ? 0 : 16)
603  );
604  put_byte(s, (Byte)(s->gzhead->time & 0xff));
605  put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
606  put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
607  put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
608  put_byte(s, s->level == 9 ? 2 :
609  (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
610  4 : 0));
611  put_byte(s, s->gzhead->os & 0xff);
612  if (s->gzhead->extra != Z_NULL) {
613  put_byte(s, s->gzhead->extra_len & 0xff);
614  put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
615  }
616  if (s->gzhead->hcrc)
617  strm->adler = crc32(strm->adler, s->pending_buf,
618  s->pending);
619  s->gzindex = 0;
620  s->status = EXTRA_STATE;
621  }
622  }
623  else
624 #endif
625  {
626  uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
627  uInt level_flags;
628 
629  if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
630  level_flags = 0;
631  else if (s->level < 6)
632  level_flags = 1;
633  else if (s->level == 6)
634  level_flags = 2;
635  else
636  level_flags = 3;
637  header |= (level_flags << 6);
638  if (s->strstart != 0) header |= PRESET_DICT;
639  header += 31 - (header % 31);
640 
641  s->status = BUSY_STATE;
642  putShortMSB(s, header);
643 
644  /* Save the adler32 of the preset dictionary: */
645  if (s->strstart != 0) {
646  putShortMSB(s, (uInt)(strm->adler >> 16));
647  putShortMSB(s, (uInt)(strm->adler & 0xffff));
648  }
649  strm->adler = adler32(0L, Z_NULL, 0);
650  }
651  }
652 #ifdef GZIP
653  if (s->status == EXTRA_STATE) {
654  if (s->gzhead->extra != Z_NULL) {
655  uInt beg = s->pending; /* start of bytes to update crc */
656 
657  while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
658  if (s->pending == s->pending_buf_size) {
659  if (s->gzhead->hcrc && s->pending > beg)
660  strm->adler = crc32(strm->adler, s->pending_buf + beg,
661  s->pending - beg);
662  flush_pending(strm);
663  beg = s->pending;
664  if (s->pending == s->pending_buf_size)
665  break;
666  }
667  put_byte(s, s->gzhead->extra[s->gzindex]);
668  s->gzindex++;
669  }
670  if (s->gzhead->hcrc && s->pending > beg)
671  strm->adler = crc32(strm->adler, s->pending_buf + beg,
672  s->pending - beg);
673  if (s->gzindex == s->gzhead->extra_len) {
674  s->gzindex = 0;
675  s->status = NAME_STATE;
676  }
677  }
678  else
679  s->status = NAME_STATE;
680  }
681  if (s->status == NAME_STATE) {
682  if (s->gzhead->name != Z_NULL) {
683  uInt beg = s->pending; /* start of bytes to update crc */
684  int val;
685 
686  do {
687  if (s->pending == s->pending_buf_size) {
688  if (s->gzhead->hcrc && s->pending > beg)
689  strm->adler = crc32(strm->adler, s->pending_buf + beg,
690  s->pending - beg);
691  flush_pending(strm);
692  beg = s->pending;
693  if (s->pending == s->pending_buf_size) {
694  val = 1;
695  break;
696  }
697  }
698  val = s->gzhead->name[s->gzindex++];
699  put_byte(s, val);
700  } while (val != 0);
701  if (s->gzhead->hcrc && s->pending > beg)
702  strm->adler = crc32(strm->adler, s->pending_buf + beg,
703  s->pending - beg);
704  if (val == 0) {
705  s->gzindex = 0;
706  s->status = COMMENT_STATE;
707  }
708  }
709  else
710  s->status = COMMENT_STATE;
711  }
712  if (s->status == COMMENT_STATE) {
713  if (s->gzhead->comment != Z_NULL) {
714  uInt beg = s->pending; /* start of bytes to update crc */
715  int val;
716 
717  do {
718  if (s->pending == s->pending_buf_size) {
719  if (s->gzhead->hcrc && s->pending > beg)
720  strm->adler = crc32(strm->adler, s->pending_buf + beg,
721  s->pending - beg);
722  flush_pending(strm);
723  beg = s->pending;
724  if (s->pending == s->pending_buf_size) {
725  val = 1;
726  break;
727  }
728  }
729  val = s->gzhead->comment[s->gzindex++];
730  put_byte(s, val);
731  } while (val != 0);
732  if (s->gzhead->hcrc && s->pending > beg)
733  strm->adler = crc32(strm->adler, s->pending_buf + beg,
734  s->pending - beg);
735  if (val == 0)
736  s->status = HCRC_STATE;
737  }
738  else
739  s->status = HCRC_STATE;
740  }
741  if (s->status == HCRC_STATE) {
742  if (s->gzhead->hcrc) {
743  if (s->pending + 2 > s->pending_buf_size)
744  flush_pending(strm);
745  if (s->pending + 2 <= s->pending_buf_size) {
746  put_byte(s, (Byte)(strm->adler & 0xff));
747  put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
748  strm->adler = crc32(0L, Z_NULL, 0);
749  s->status = BUSY_STATE;
750  }
751  }
752  else
753  s->status = BUSY_STATE;
754  }
755 #endif
756 
757  /* Flush as much pending output as possible */
758  if (s->pending != 0) {
759  flush_pending(strm);
760  if (strm->avail_out == 0) {
761  /* Since avail_out is 0, deflate will be called again with
762  * more output space, but possibly with both pending and
763  * avail_in equal to zero. There won't be anything to do,
764  * but this is not an error situation so make sure we
765  * return OK instead of BUF_ERROR at next call of deflate:
766  */
767  s->last_flush = -1;
768  return Z_OK;
769  }
770 
771  /* Make sure there is something to do and avoid duplicate consecutive
772  * flushes. For repeated and useless calls with Z_FINISH, we keep
773  * returning Z_STREAM_END instead of Z_BUF_ERROR.
774  */
775  } else if (strm->avail_in == 0 && flush <= old_flush &&
776  flush != Z_FINISH) {
777  ERR_RETURN(strm, Z_BUF_ERROR);
778  }
779 
780  /* User must not provide more input after the first FINISH: */
781  if (s->status == FINISH_STATE && strm->avail_in != 0) {
782  ERR_RETURN(strm, Z_BUF_ERROR);
783  }
784 
785  /* Start a new block or continue the current one.
786  */
787  if (strm->avail_in != 0 || s->lookahead != 0 ||
788  (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
789  block_state bstate;
790 
791  bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
792  (s->strategy == Z_RLE ? deflate_rle(s, flush) :
793  (*(configuration_table[s->level].func))(s, flush));
794 
795  if (bstate == finish_started || bstate == finish_done) {
796  s->status = FINISH_STATE;
797  }
798  if (bstate == need_more || bstate == finish_started) {
799  if (strm->avail_out == 0) {
800  s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
801  }
802  return Z_OK;
803  /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
804  * of deflate should use the same flush parameter to make sure
805  * that the flush is complete. So we don't have to output an
806  * empty block here, this will be done at next call. This also
807  * ensures that for a very small output buffer, we emit at most
808  * one empty block.
809  */
810  }
811  if (bstate == block_done) {
812  if (flush == Z_PARTIAL_FLUSH) {
813  _tr_align(s);
814  } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
815  _tr_stored_block(s, (char*)0, 0L, 0);
816  /* For a full flush, this empty block will be recognized
817  * as a special marker by inflate_sync().
818  */
819  if (flush == Z_FULL_FLUSH) {
820  CLEAR_HASH(s); /* forget history */
821  if (s->lookahead == 0) {
822  s->strstart = 0;
823  s->block_start = 0L;
824  }
825  }
826  }
827  flush_pending(strm);
828  if (strm->avail_out == 0) {
829  s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
830  return Z_OK;
831  }
832  }
833  }
834  Assert(strm->avail_out > 0, "bug2");
835 
836  if (flush != Z_FINISH) return Z_OK;
837  if (s->wrap <= 0) return Z_STREAM_END;
838 
839  /* Write the trailer */
840 #ifdef GZIP
841  if (s->wrap == 2) {
842  put_byte(s, (Byte)(strm->adler & 0xff));
843  put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
844  put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
845  put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
846  put_byte(s, (Byte)(strm->total_in & 0xff));
847  put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
848  put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
849  put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
850  }
851  else
852 #endif
853  {
854  putShortMSB(s, (uInt)(strm->adler >> 16));
855  putShortMSB(s, (uInt)(strm->adler & 0xffff));
856  }
857  flush_pending(strm);
858  /* If avail_out is zero, the application will call deflate again
859  * to flush the rest.
860  */
861  if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
862  return s->pending != 0 ? Z_OK : Z_STREAM_END;
863 }
#define INIT_STATE
Definition: deflate.h:53
#define Z_BLOCK
Definition: zlib.h:177
local void flush_pending(z_streamp strm)
Definition: deflate.c:536
#define HCRC_STATE
Definition: deflate.h:57
unsigned char Byte
Definition: zconf.h:333
#define Z_NO_FLUSH
Definition: zlib.h:172
#define PRESET_DICT
Definition: zutil.h:77
void ZLIB_INTERNAL _tr_align(deflate_state *s)
Definition: trees.c:882
block_state
Definition: deflate.c:73
#define Z_PARTIAL_FLUSH
Definition: zlib.h:173
#define Assert(cond, msg)
Definition: zutil.h:268
#define Z_STREAM_ERROR
Definition: zlib.h:185
gz_headerp gzhead
Definition: deflate.h:104
uInt lookahead
Definition: deflate.h:160
#define BUSY_STATE
Definition: deflate.h:58
local block_state deflate_huff(deflate_state *s, int flush)
Definition: deflate.c:1755
#define Z_FINISH
Definition: zlib.h:176
z_streamp strm
Definition: deflate.h:97
#define Z_DEFLATED
Definition: zlib.h:213
#define Z_STREAM_END
Definition: zlib.h:182
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:230
#define put_byte(s, c)
Definition: deflate.h:277
#define COMMENT_STATE
Definition: deflate.h:56
Bytef * pending_buf
Definition: deflate.h:99
#define CLEAR_HASH(s)
Definition: deflate.c:200
local const config configuration_table[10]
Definition: deflate.c:138
#define NAME_STATE
Definition: deflate.h:55
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
Definition: trees.c:861
long block_start
Definition: deflate.h:150
uInt gzindex
Definition: deflate.h:105
#define FINISH_STATE
Definition: deflate.h:59
#define Z_HUFFMAN_ONLY
Definition: zlib.h:201
local block_state deflate_rle(deflate_state *s, int flush)
Definition: deflate.c:1691
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition: adler32.c:67
local void putShortMSB(deflate_state *s, uInt b)
Definition: deflate.c:524
#define Z_BUF_ERROR
Definition: zlib.h:188
uInt pending
Definition: deflate.h:102
uInt strstart
Definition: deflate.h:158
#define ERR_RETURN(strm, err)
Definition: zutil.h:50
ulg pending_buf_size
Definition: deflate.h:100
#define Z_OK
Definition: zlib.h:181
#define EXTRA_STATE
Definition: deflate.h:54
#define Z_NULL
Definition: zlib.h:216
#define Z_FULL_FLUSH
Definition: zlib.h:175
int last_flush
Definition: deflate.h:107
#define OS_CODE
Definition: zutil.h:181
unsigned int uInt
Definition: zconf.h:335
#define Z_RLE
Definition: zlib.h:202
local block_state deflate_fast ( deflate_state s,
int  flush 
)

Definition at line 1474 of file deflate.c.

1475 {
1476  IPos hash_head; /* head of the hash chain */
1477  int bflush; /* set if current block must be flushed */
1478 
1479  for (;;) {
1480  /* Make sure that we always have enough lookahead, except
1481  * at the end of the input file. We need MAX_MATCH bytes
1482  * for the next match, plus MIN_MATCH bytes to insert the
1483  * string following the next match.
1484  */
1485  if (s->lookahead < MIN_LOOKAHEAD) {
1486  fill_window(s);
1487  if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1488  return need_more;
1489  }
1490  if (s->lookahead == 0) break; /* flush the current block */
1491  }
1492 
1493  /* Insert the string window[strstart .. strstart+2] in the
1494  * dictionary, and set hash_head to the head of the hash chain:
1495  */
1496  hash_head = NIL;
1497  if (s->lookahead >= MIN_MATCH) {
1498  INSERT_STRING(s, s->strstart, hash_head);
1499  }
1500 
1501  /* Find the longest match, discarding those <= prev_length.
1502  * At this point we have always match_length < MIN_MATCH
1503  */
1504  if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1505  /* To simplify the code, we prevent matches with the string
1506  * of window index 0 (in particular we have to avoid a match
1507  * of the string with itself at the start of the input file).
1508  */
1509  s->match_length = longest_match (s, hash_head);
1510  /* longest_match() sets match_start */
1511  }
1512  if (s->match_length >= MIN_MATCH) {
1514 
1515  _tr_tally_dist(s, s->strstart - s->match_start,
1516  s->match_length - MIN_MATCH, bflush);
1517 
1518  s->lookahead -= s->match_length;
1519 
1520  /* Insert new strings in the hash table only if the match length
1521  * is not too large. This saves time but degrades compression.
1522  */
1523 #ifndef FASTEST
1524  if (s->match_length <= s->max_insert_length &&
1525  s->lookahead >= MIN_MATCH) {
1526  s->match_length--; /* string at strstart already in table */
1527  do {
1528  s->strstart++;
1529  INSERT_STRING(s, s->strstart, hash_head);
1530  /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1531  * always MIN_MATCH bytes ahead.
1532  */
1533  } while (--s->match_length != 0);
1534  s->strstart++;
1535  } else
1536 #endif
1537  {
1538  s->strstart += s->match_length;
1539  s->match_length = 0;
1540  s->ins_h = s->window[s->strstart];
1541  UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1542 #if MIN_MATCH != 3
1543  Call UPDATE_HASH() MIN_MATCH-3 more times
1544 #endif
1545  /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1546  * matter since it will be recomputed at next deflate call.
1547  */
1548  }
1549  } else {
1550  /* No match, output a literal byte */
1551  Tracevv((stderr,"%c", s->window[s->strstart]));
1552  _tr_tally_lit (s, s->window[s->strstart], bflush);
1553  s->lookahead--;
1554  s->strstart++;
1555  }
1556  if (bflush) FLUSH_BLOCK(s, 0);
1557  }
1558  FLUSH_BLOCK(s, flush == Z_FINISH);
1559  return flush == Z_FINISH ? finish_done : block_done;
1560 }
#define INSERT_STRING(s, str, match_head)
Definition: deflate.c:190
#define check_match(s, start, match, length)
Definition: deflate.c:1249
Bytef * window
Definition: deflate.h:115
#define Z_NO_FLUSH
Definition: zlib.h:172
#define MIN_MATCH
Definition: zutil.h:73
uInt match_length
Definition: deflate.h:155
local uInt longest_match(deflate_state *s, IPos cur_match)
Definition: deflate.c:1027
#define UPDATE_HASH(s, h, c)
Definition: deflate.c:171
uInt match_start
Definition: deflate.h:159
uInt lookahead
Definition: deflate.h:160
#define _tr_tally_lit(s, c, flush)
Definition: deflate.h:321
#define MAX_DIST(s)
Definition: deflate.h:285
#define MIN_LOOKAHEAD
Definition: deflate.h:280
#define Z_FINISH
Definition: zlib.h:176
#define NIL
Definition: deflate.c:111
#define _tr_tally_dist(s, distance, length, flush)
Definition: deflate.h:328
local void fill_window(deflate_state *s)
Definition: deflate.c:1262
uInt strstart
Definition: deflate.h:158
#define FLUSH_BLOCK(s, last)
Definition: deflate.c:1404
#define Tracevv(x)
Definition: zutil.h:271
unsigned IPos
Definition: deflate.h:90
local block_state deflate_huff ( deflate_state s,
int  flush 
)

Definition at line 1755 of file deflate.c.

1756 {
1757  int bflush; /* set if current block must be flushed */
1758 
1759  for (;;) {
1760  /* Make sure that we have a literal to write. */
1761  if (s->lookahead == 0) {
1762  fill_window(s);
1763  if (s->lookahead == 0) {
1764  if (flush == Z_NO_FLUSH)
1765  return need_more;
1766  break; /* flush the current block */
1767  }
1768  }
1769 
1770  /* Output a literal byte */
1771  s->match_length = 0;
1772  Tracevv((stderr,"%c", s->window[s->strstart]));
1773  _tr_tally_lit (s, s->window[s->strstart], bflush);
1774  s->lookahead--;
1775  s->strstart++;
1776  if (bflush) FLUSH_BLOCK(s, 0);
1777  }
1778  FLUSH_BLOCK(s, flush == Z_FINISH);
1779  return flush == Z_FINISH ? finish_done : block_done;
1780 }
Bytef * window
Definition: deflate.h:115
#define Z_NO_FLUSH
Definition: zlib.h:172
uInt match_length
Definition: deflate.h:155
uInt lookahead
Definition: deflate.h:160
#define _tr_tally_lit(s, c, flush)
Definition: deflate.h:321
#define Z_FINISH
Definition: zlib.h:176
local void fill_window(deflate_state *s)
Definition: deflate.c:1262
uInt strstart
Definition: deflate.h:158
#define FLUSH_BLOCK(s, last)
Definition: deflate.c:1404
#define Tracevv(x)
Definition: zutil.h:271
local block_state deflate_rle ( deflate_state s,
int  flush 
)

Definition at line 1691 of file deflate.c.

1692 {
1693  int bflush; /* set if current block must be flushed */
1694  uInt prev; /* byte at distance one to match */
1695  Bytef *scan, *strend; /* scan goes up to strend for length of run */
1696 
1697  for (;;) {
1698  /* Make sure that we always have enough lookahead, except
1699  * at the end of the input file. We need MAX_MATCH bytes
1700  * for the longest encodable run.
1701  */
1702  if (s->lookahead < MAX_MATCH) {
1703  fill_window(s);
1704  if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
1705  return need_more;
1706  }
1707  if (s->lookahead == 0) break; /* flush the current block */
1708  }
1709 
1710  /* See how many times the previous byte repeats */
1711  s->match_length = 0;
1712  if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1713  scan = s->window + s->strstart - 1;
1714  prev = *scan;
1715  if (prev == *++scan && prev == *++scan && prev == *++scan) {
1716  strend = s->window + s->strstart + MAX_MATCH;
1717  do {
1718  } while (prev == *++scan && prev == *++scan &&
1719  prev == *++scan && prev == *++scan &&
1720  prev == *++scan && prev == *++scan &&
1721  prev == *++scan && prev == *++scan &&
1722  scan < strend);
1723  s->match_length = MAX_MATCH - (int)(strend - scan);
1724  if (s->match_length > s->lookahead)
1725  s->match_length = s->lookahead;
1726  }
1727  }
1728 
1729  /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1730  if (s->match_length >= MIN_MATCH) {
1731  check_match(s, s->strstart, s->strstart - 1, s->match_length);
1732 
1733  _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
1734 
1735  s->lookahead -= s->match_length;
1736  s->strstart += s->match_length;
1737  s->match_length = 0;
1738  } else {
1739  /* No match, output a literal byte */
1740  Tracevv((stderr,"%c", s->window[s->strstart]));
1741  _tr_tally_lit (s, s->window[s->strstart], bflush);
1742  s->lookahead--;
1743  s->strstart++;
1744  }
1745  if (bflush) FLUSH_BLOCK(s, 0);
1746  }
1747  FLUSH_BLOCK(s, flush == Z_FINISH);
1748  return flush == Z_FINISH ? finish_done : block_done;
1749 }
#define check_match(s, start, match, length)
Definition: deflate.c:1249
Bytef * window
Definition: deflate.h:115
#define Z_NO_FLUSH
Definition: zlib.h:172
#define MIN_MATCH
Definition: zutil.h:73
uInt match_length
Definition: deflate.h:155
Byte FAR Bytef
Definition: zconf.h:342
uInt lookahead
Definition: deflate.h:160
#define _tr_tally_lit(s, c, flush)
Definition: deflate.h:321
#define Z_FINISH
Definition: zlib.h:176
#define MAX_MATCH
Definition: zutil.h:74
#define _tr_tally_dist(s, distance, length, flush)
Definition: deflate.h:328
local void fill_window(deflate_state *s)
Definition: deflate.c:1262
uInt strstart
Definition: deflate.h:158
#define FLUSH_BLOCK(s, last)
Definition: deflate.c:1404
unsigned int uInt
Definition: zconf.h:335
#define Tracevv(x)
Definition: zutil.h:271
local block_state deflate_slow ( deflate_state s,
int  flush 
)

Definition at line 1568 of file deflate.c.

1569 {
1570  IPos hash_head; /* head of hash chain */
1571  int bflush; /* set if current block must be flushed */
1572 
1573  /* Process the input block. */
1574  for (;;) {
1575  /* Make sure that we always have enough lookahead, except
1576  * at the end of the input file. We need MAX_MATCH bytes
1577  * for the next match, plus MIN_MATCH bytes to insert the
1578  * string following the next match.
1579  */
1580  if (s->lookahead < MIN_LOOKAHEAD) {
1581  fill_window(s);
1582  if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1583  return need_more;
1584  }
1585  if (s->lookahead == 0) break; /* flush the current block */
1586  }
1587 
1588  /* Insert the string window[strstart .. strstart+2] in the
1589  * dictionary, and set hash_head to the head of the hash chain:
1590  */
1591  hash_head = NIL;
1592  if (s->lookahead >= MIN_MATCH) {
1593  INSERT_STRING(s, s->strstart, hash_head);
1594  }
1595 
1596  /* Find the longest match, discarding those <= prev_length.
1597  */
1599  s->match_length = MIN_MATCH-1;
1600 
1601  if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1602  s->strstart - hash_head <= MAX_DIST(s)) {
1603  /* To simplify the code, we prevent matches with the string
1604  * of window index 0 (in particular we have to avoid a match
1605  * of the string with itself at the start of the input file).
1606  */
1607  s->match_length = longest_match (s, hash_head);
1608  /* longest_match() sets match_start */
1609 
1610  if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1611 #if TOO_FAR <= 32767
1612  || (s->match_length == MIN_MATCH &&
1613  s->strstart - s->match_start > TOO_FAR)
1614 #endif
1615  )) {
1616 
1617  /* If prev_match is also MIN_MATCH, match_start is garbage
1618  * but we will ignore the current match anyway.
1619  */
1620  s->match_length = MIN_MATCH-1;
1621  }
1622  }
1623  /* If there was a match at the previous step and the current
1624  * match is not better, output the previous match:
1625  */
1626  if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1627  uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1628  /* Do not insert strings in hash table beyond this. */
1629 
1630  check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1631 
1632  _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1633  s->prev_length - MIN_MATCH, bflush);
1634 
1635  /* Insert in hash table all strings up to the end of the match.
1636  * strstart-1 and strstart are already inserted. If there is not
1637  * enough lookahead, the last two strings are not inserted in
1638  * the hash table.
1639  */
1640  s->lookahead -= s->prev_length-1;
1641  s->prev_length -= 2;
1642  do {
1643  if (++s->strstart <= max_insert) {
1644  INSERT_STRING(s, s->strstart, hash_head);
1645  }
1646  } while (--s->prev_length != 0);
1647  s->match_available = 0;
1648  s->match_length = MIN_MATCH-1;
1649  s->strstart++;
1650 
1651  if (bflush) FLUSH_BLOCK(s, 0);
1652 
1653  } else if (s->match_available) {
1654  /* If there was no match at the previous position, output a
1655  * single literal. If there was a match but the current match
1656  * is longer, truncate the previous match to a single literal.
1657  */
1658  Tracevv((stderr,"%c", s->window[s->strstart-1]));
1659  _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1660  if (bflush) {
1661  FLUSH_BLOCK_ONLY(s, 0);
1662  }
1663  s->strstart++;
1664  s->lookahead--;
1665  if (s->strm->avail_out == 0) return need_more;
1666  } else {
1667  /* There is no previous match to compare with, wait for
1668  * the next step to decide.
1669  */
1670  s->match_available = 1;
1671  s->strstart++;
1672  s->lookahead--;
1673  }
1674  }
1675  Assert (flush != Z_NO_FLUSH, "no flush?");
1676  if (s->match_available) {
1677  Tracevv((stderr,"%c", s->window[s->strstart-1]));
1678  _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1679  s->match_available = 0;
1680  }
1681  FLUSH_BLOCK(s, flush == Z_FINISH);
1682  return flush == Z_FINISH ? finish_done : block_done;
1683 }
#define INSERT_STRING(s, str, match_head)
Definition: deflate.c:190
#define check_match(s, start, match, length)
Definition: deflate.c:1249
Bytef * window
Definition: deflate.h:115
#define TOO_FAR
Definition: deflate.c:115
#define Z_NO_FLUSH
Definition: zlib.h:172
#define MIN_MATCH
Definition: zutil.h:73
uInt max_lazy_match
Definition: deflate.h:173
uInt match_length
Definition: deflate.h:155
#define Assert(cond, msg)
Definition: zutil.h:268
local uInt longest_match(deflate_state *s, IPos cur_match)
Definition: deflate.c:1027
uInt prev_length
Definition: deflate.h:162
#define Z_FILTERED
Definition: zlib.h:200
uInt match_start
Definition: deflate.h:159
uInt lookahead
Definition: deflate.h:160
#define _tr_tally_lit(s, c, flush)
Definition: deflate.h:321
#define MAX_DIST(s)
Definition: deflate.h:285
#define MIN_LOOKAHEAD
Definition: deflate.h:280
#define Z_FINISH
Definition: zlib.h:176
z_streamp strm
Definition: deflate.h:97
#define NIL
Definition: deflate.c:111
#define _tr_tally_dist(s, distance, length, flush)
Definition: deflate.h:328
int match_available
Definition: deflate.h:157
local void fill_window(deflate_state *s)
Definition: deflate.c:1262
uInt strstart
Definition: deflate.h:158
#define FLUSH_BLOCK(s, last)
Definition: deflate.c:1404
IPos prev_match
Definition: deflate.h:156
unsigned int uInt
Definition: zconf.h:335
#define FLUSH_BLOCK_ONLY(s, last)
Definition: deflate.c:1392
#define Tracevv(x)
Definition: zutil.h:271
unsigned IPos
Definition: deflate.h:90
local block_state deflate_stored ( deflate_state s,
int  flush 
)

Definition at line 1418 of file deflate.c.

1419 {
1420  /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1421  * to pending_buf_size, and each stored block has a 5 byte header:
1422  */
1423  ulg max_block_size = 0xffff;
1424  ulg max_start;
1425 
1426  if (max_block_size > s->pending_buf_size - 5) {
1427  max_block_size = s->pending_buf_size - 5;
1428  }
1429 
1430  /* Copy as much as possible from input to output: */
1431  for (;;) {
1432  /* Fill the window as much as possible: */
1433  if (s->lookahead <= 1) {
1434 
1435  Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1436  s->block_start >= (long)s->w_size, "slide too late");
1437 
1438  fill_window(s);
1439  if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1440 
1441  if (s->lookahead == 0) break; /* flush the current block */
1442  }
1443  Assert(s->block_start >= 0L, "block gone");
1444 
1445  s->strstart += s->lookahead;
1446  s->lookahead = 0;
1447 
1448  /* Emit a stored block if pending_buf will be full: */
1449  max_start = s->block_start + max_block_size;
1450  if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1451  /* strstart == 0 is possible when wraparound on 16-bit machine */
1452  s->lookahead = (uInt)(s->strstart - max_start);
1453  s->strstart = (uInt)max_start;
1454  FLUSH_BLOCK(s, 0);
1455  }
1456  /* Flush if we may have to slide, otherwise block_start may become
1457  * negative and the data will be gone:
1458  */
1459  if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1460  FLUSH_BLOCK(s, 0);
1461  }
1462  }
1463  FLUSH_BLOCK(s, flush == Z_FINISH);
1464  return flush == Z_FINISH ? finish_done : block_done;
1465 }
#define Z_NO_FLUSH
Definition: zlib.h:172
#define Assert(cond, msg)
Definition: zutil.h:268
uInt lookahead
Definition: deflate.h:160
#define MAX_DIST(s)
Definition: deflate.h:285
#define Z_FINISH
Definition: zlib.h:176
long block_start
Definition: deflate.h:150
local void fill_window(deflate_state *s)
Definition: deflate.c:1262
unsigned long ulg
Definition: zutil.h:43
uInt strstart
Definition: deflate.h:158
#define FLUSH_BLOCK(s, last)
Definition: deflate.c:1404
ulg pending_buf_size
Definition: deflate.h:100
unsigned int uInt
Definition: zconf.h:335
uLong ZEXPORT deflateBound ( z_streamp  strm,
uLong  sourceLen 
)

Definition at line 464 of file deflate.c.

465 {
466  deflate_state *s;
467  uLong complen, wraplen;
468  Bytef *str;
469 
470  /* conservative upper bound for compressed data */
471  complen = sourceLen +
472  ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
473 
474  /* if can't get parameters, return conservative bound plus zlib wrapper */
475  if (strm == Z_NULL || strm->state == Z_NULL)
476  return complen + 6;
477 
478  /* compute wrapper length */
479  s = strm->state;
480  switch (s->wrap) {
481  case 0: /* raw deflate */
482  wraplen = 0;
483  break;
484  case 1: /* zlib wrapper */
485  wraplen = 6 + (s->strstart ? 4 : 0);
486  break;
487  case 2: /* gzip wrapper */
488  wraplen = 18;
489  if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
490  if (s->gzhead->extra != Z_NULL)
491  wraplen += 2 + s->gzhead->extra_len;
492  str = s->gzhead->name;
493  if (str != Z_NULL)
494  do {
495  wraplen++;
496  } while (*str++);
497  str = s->gzhead->comment;
498  if (str != Z_NULL)
499  do {
500  wraplen++;
501  } while (*str++);
502  if (s->gzhead->hcrc)
503  wraplen += 2;
504  }
505  break;
506  default: /* for compiler happiness */
507  wraplen = 6;
508  }
509 
510  /* if not default parameters, return conservative bound */
511  if (s->w_bits != 15 || s->hash_bits != 8 + 7)
512  return complen + wraplen;
513 
514  /* default settings: return tight bound for that case */
515  return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
516  (sourceLen >> 25) + 13 - 6 + wraplen;
517 }
unsigned long uLong
Definition: zconf.h:336
gz_headerp gzhead
Definition: deflate.h:104
Byte FAR Bytef
Definition: zconf.h:342
#define Z_NULL
Definition: zlib.h:216
int ZEXPORT deflateCopy ( z_streamp  dest,
z_streamp  source 
)

Definition at line 900 of file deflate.c.

901 {
902 #ifdef MAXSEG_64K
903  return Z_STREAM_ERROR;
904 #else
905  deflate_state *ds;
906  deflate_state *ss;
907  ushf *overlay;
908 
909 
910  if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
911  return Z_STREAM_ERROR;
912  }
913 
914  ss = source->state;
915 
916  zmemcpy(dest, source, sizeof(z_stream));
917 
918  ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
919  if (ds == Z_NULL) return Z_MEM_ERROR;
920  dest->state = (struct internal_state FAR *) ds;
921  zmemcpy(ds, ss, sizeof(deflate_state));
922  ds->strm = dest;
923 
924  ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
925  ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
926  ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
927  overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
928  ds->pending_buf = (uchf *) overlay;
929 
930  if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
931  ds->pending_buf == Z_NULL) {
932  deflateEnd (dest);
933  return Z_MEM_ERROR;
934  }
935  /* following zmemcpy do not work for 16-bit MSDOS */
936  zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
937  zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
938  zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
940 
941  ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
942  ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
943  ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
944 
945  ds->l_desc.dyn_tree = ds->dyn_ltree;
946  ds->d_desc.dyn_tree = ds->dyn_dtree;
947  ds->bl_desc.dyn_tree = ds->bl_tree;
948 
949  return Z_OK;
950 #endif /* MAXSEG_64K */
951 }
ush FAR ushf
Definition: zutil.h:42
Bytef * window
Definition: deflate.h:115
unsigned char Byte
Definition: zconf.h:333
ush Pos
Definition: deflate.h:88
Posf * prev
Definition: deflate.h:130
struct ct_data_s dyn_ltree[HEAP_SIZE]
Definition: deflate.h:194
ct_data * dyn_tree
Definition: deflate.h:83
Bytef * pending_out
Definition: deflate.h:101
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:866
#define Z_STREAM_ERROR
Definition: zlib.h:185
Byte FAR Bytef
Definition: zconf.h:342
#define ZALLOC(strm, items, size)
Definition: zutil.h:281
z_streamp strm
Definition: deflate.h:97
struct tree_desc_s bl_desc
Definition: deflate.h:200
struct tree_desc_s d_desc
Definition: deflate.h:199
Posf * head
Definition: deflate.h:136
unsigned short ush
Definition: zutil.h:41
uchf * l_buf
Definition: deflate.h:216
Pos FAR Posf
Definition: deflate.h:89
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.c:157
#define Z_MEM_ERROR
Definition: zlib.h:187
Bytef * pending_buf
Definition: deflate.h:99
uInt lit_bufsize
Definition: deflate.h:218
ushf * d_buf
Definition: deflate.h:240
struct ct_data_s dyn_dtree[2 *D_CODES+1]
Definition: deflate.h:195
struct tree_desc_s l_desc
Definition: deflate.h:198
ulg pending_buf_size
Definition: deflate.h:100
#define Z_OK
Definition: zlib.h:181
#define Z_NULL
Definition: zlib.h:216
#define FAR
Definition: zconf.h:329
uch FAR uchf
Definition: zutil.h:40
struct ct_data_s bl_tree[2 *BL_CODES+1]
Definition: deflate.h:196
uInt hash_size
Definition: deflate.h:139
unsigned int uInt
Definition: zconf.h:335
int ZEXPORT deflateEnd ( z_streamp  strm)

Definition at line 866 of file deflate.c.

867 {
868  int status;
869 
870  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
871 
872  status = strm->state->status;
873  if (status != INIT_STATE &&
874  status != EXTRA_STATE &&
875  status != NAME_STATE &&
876  status != COMMENT_STATE &&
877  status != HCRC_STATE &&
878  status != BUSY_STATE &&
879  status != FINISH_STATE) {
880  return Z_STREAM_ERROR;
881  }
882 
883  /* Deallocate in reverse order of allocations: */
884  TRY_FREE(strm, strm->state->pending_buf);
885  TRY_FREE(strm, strm->state->head);
886  TRY_FREE(strm, strm->state->prev);
887  TRY_FREE(strm, strm->state->window);
888 
889  ZFREE(strm, strm->state);
890  strm->state = Z_NULL;
891 
892  return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
893 }
#define INIT_STATE
Definition: deflate.h:53
#define HCRC_STATE
Definition: deflate.h:57
#define Z_STREAM_ERROR
Definition: zlib.h:185
#define BUSY_STATE
Definition: deflate.h:58
z_streamp strm
Definition: deflate.h:97
#define Z_DATA_ERROR
Definition: zlib.h:186
#define ZFREE(strm, addr)
Definition: zutil.h:283
#define COMMENT_STATE
Definition: deflate.h:56
#define NAME_STATE
Definition: deflate.h:55
#define TRY_FREE(s, p)
Definition: zutil.h:284
#define FINISH_STATE
Definition: deflate.h:59
#define Z_OK
Definition: zlib.h:181
#define EXTRA_STATE
Definition: deflate.h:54
#define Z_NULL
Definition: zlib.h:216
int ZEXPORT deflateInit2_ ( z_streamp  strm,
int  level,
int  method,
int  windowBits,
int  memLevel,
int  strategy,
const char *  version,
int  stream_size 
)

Definition at line 213 of file deflate.c.

215 {
216  deflate_state *s;
217  int wrap = 1;
218  static const char my_version[] = ZLIB_VERSION;
219 
220  ushf *overlay;
221  /* We overlay pending_buf and d_buf+l_buf. This works since the average
222  * output size for (length,distance) codes is <= 24 bits.
223  */
224 
225  if (version == Z_NULL || version[0] != my_version[0] ||
226  stream_size != sizeof(z_stream)) {
227  return Z_VERSION_ERROR;
228  }
229  if (strm == Z_NULL) return Z_STREAM_ERROR;
230 
231  strm->msg = Z_NULL;
232  if (strm->zalloc == (alloc_func)0) {
233  strm->zalloc = zcalloc;
234  strm->opaque = (voidpf)0;
235  }
236  if (strm->zfree == (free_func)0) strm->zfree = zcfree;
237 
238 #ifdef FASTEST
239  if (level != 0) level = 1;
240 #else
241  if (level == Z_DEFAULT_COMPRESSION) level = 6;
242 #endif
243 
244  if (windowBits < 0) { /* suppress zlib wrapper */
245  wrap = 0;
246  windowBits = -windowBits;
247  }
248 #ifdef GZIP
249  else if (windowBits > 15) {
250  wrap = 2; /* write gzip wrapper instead */
251  windowBits -= 16;
252  }
253 #endif
254  if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
255  windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
256  strategy < 0 || strategy > Z_FIXED) {
257  return Z_STREAM_ERROR;
258  }
259  if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
260  s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
261  if (s == Z_NULL) return Z_MEM_ERROR;
262  strm->state = (struct internal_state FAR *)s;
263  s->strm = strm;
264 
265  s->wrap = wrap;
266  s->gzhead = Z_NULL;
267  s->w_bits = windowBits;
268  s->w_size = 1 << s->w_bits;
269  s->w_mask = s->w_size - 1;
270 
271  s->hash_bits = memLevel + 7;
272  s->hash_size = 1 << s->hash_bits;
273  s->hash_mask = s->hash_size - 1;
274  s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
275 
276  s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
277  s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
278  s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
279 
280  s->high_water = 0; /* nothing written to s->window yet */
281 
282  s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
283 
284  overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
285  s->pending_buf = (uchf *) overlay;
286  s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
287 
288  if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
289  s->pending_buf == Z_NULL) {
290  s->status = FINISH_STATE;
291  strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
292  deflateEnd (strm);
293  return Z_MEM_ERROR;
294  }
295  s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
296  s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
297 
298  s->level = level;
299  s->strategy = strategy;
300  s->method = (Byte)method;
301 
302  return deflateReset(strm);
303 }
uInt hash_bits
Definition: deflate.h:140
Byte FAR * voidpf
Definition: zconf.h:355
ush FAR ushf
Definition: zutil.h:42
Bytef * window
Definition: deflate.h:115
unsigned char Byte
Definition: zconf.h:333
#define MIN_MATCH
Definition: zutil.h:73
uInt hash_mask
Definition: deflate.h:141
ush Pos
Definition: deflate.h:88
uInt hash_shift
Definition: deflate.h:143
#define MAX_MEM_LEVEL
Definition: zconf.h:210
Posf * prev
Definition: deflate.h:130
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:866
#define Z_STREAM_ERROR
Definition: zlib.h:185
gz_headerp gzhead
Definition: deflate.h:104
#define Z_FIXED
Definition: zlib.h:203
Byte FAR Bytef
Definition: zconf.h:342
ulg high_water
Definition: deflate.h:265
#define ERR_MSG(err)
Definition: zutil.h:48
#define ZALLOC(strm, items, size)
Definition: zutil.h:281
z_streamp strm
Definition: deflate.h:97
Posf * head
Definition: deflate.h:136
string version
Definition: setup.py:63
#define Z_DEFLATED
Definition: zlib.h:213
unsigned short ush
Definition: zutil.h:41
if(last==0)
Definition: sparse_int.h:34
uchf * l_buf
Definition: deflate.h:216
Pos FAR Posf
Definition: deflate.h:89
#define Z_MEM_ERROR
Definition: zlib.h:187
Bytef * pending_buf
Definition: deflate.h:99
uInt lit_bufsize
Definition: deflate.h:218
ushf * d_buf
Definition: deflate.h:240
#define ZLIB_VERSION
Definition: zlib.h:48
#define FINISH_STATE
Definition: deflate.h:59
#define Z_VERSION_ERROR
Definition: zlib.h:189
unsigned long ulg
Definition: zutil.h:43
ulg pending_buf_size
Definition: deflate.h:100
#define Z_NULL
Definition: zlib.h:216
#define FAR
Definition: zconf.h:329
int ZEXPORT deflateReset(z_streamp strm)
Definition: deflate.c:345
uch FAR uchf
Definition: zutil.h:40
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
Definition: zutil.c:300
uInt hash_size
Definition: deflate.h:139
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:197
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
Definition: zutil.c:307
int ZEXPORT deflateInit_ ( z_streamp  strm,
int  level,
const char *  version,
int  stream_size 
)

Definition at line 205 of file deflate.c.

206 {
208  Z_DEFAULT_STRATEGY, version, stream_size);
209  /* To do: ignore strm->next_in if we use it as window */
210 }
int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size)
Definition: deflate.c:213
#define DEF_MEM_LEVEL
Definition: zutil.h:62
z_streamp strm
Definition: deflate.h:97
string version
Definition: setup.py:63
#define Z_DEFLATED
Definition: zlib.h:213
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:204
#define MAX_WBITS
Definition: zconf.h:220
int ZEXPORT deflateParams ( z_streamp  strm,
int  level,
int  strategy 
)

Definition at line 398 of file deflate.c.

399 {
400  deflate_state *s;
401  compress_func func;
402  int err = Z_OK;
403 
404  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
405  s = strm->state;
406 
407 #ifdef FASTEST
408  if (level != 0) level = 1;
409 #else
410  if (level == Z_DEFAULT_COMPRESSION) level = 6;
411 #endif
412  if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
413  return Z_STREAM_ERROR;
414  }
415  func = configuration_table[s->level].func;
416 
417  if ((strategy != s->strategy || func != configuration_table[level].func) &&
418  strm->total_in != 0) {
419  /* Flush the last buffer: */
420  err = deflate(strm, Z_BLOCK);
421  }
422  if (s->level != level) {
423  s->level = level;
428  }
429  s->strategy = strategy;
430  return err;
431 }
uInt good_match
Definition: deflate.h:187
#define Z_BLOCK
Definition: zlib.h:177
uInt max_lazy_match
Definition: deflate.h:173
ush good_length
Definition: deflate.c:125
#define Z_STREAM_ERROR
Definition: zlib.h:185
#define Z_FIXED
Definition: zlib.h:203
z_streamp strm
Definition: deflate.h:97
compress_func func
Definition: deflate.c:129
ush max_chain
Definition: deflate.c:128
local const config configuration_table[10]
Definition: deflate.c:138
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:555
ush max_lazy
Definition: deflate.c:126
uInt max_chain_length
Definition: deflate.h:167
int nice_match
Definition: deflate.h:190
#define Z_OK
Definition: zlib.h:181
#define Z_NULL
Definition: zlib.h:216
ush nice_length
Definition: deflate.c:127
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:197
int ZEXPORT deflatePrime ( z_streamp  strm,
int  bits,
int  value 
)

Definition at line 389 of file deflate.c.

390 {
391  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
392  strm->state->bi_valid = bits;
393  strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
394  return Z_OK;
395 }
#define Z_STREAM_ERROR
Definition: zlib.h:185
z_streamp strm
Definition: deflate.h:97
unsigned short ush
Definition: zutil.h:41
#define Z_OK
Definition: zlib.h:181
int value
#define Z_NULL
Definition: zlib.h:216
static char * bits(int n)
Definition: abcSaucy.c:201
int ZEXPORT deflateReset ( z_streamp  strm)

Definition at line 345 of file deflate.c.

346 {
347  deflate_state *s;
348 
349  if (strm == Z_NULL || strm->state == Z_NULL ||
350  strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
351  return Z_STREAM_ERROR;
352  }
353 
354  strm->total_in = strm->total_out = 0;
355  strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
356  strm->data_type = Z_UNKNOWN;
357 
358  s = (deflate_state *)strm->state;
359  s->pending = 0;
360  s->pending_out = s->pending_buf;
361 
362  if (s->wrap < 0) {
363  s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
364  }
365  s->status = s->wrap ? INIT_STATE : BUSY_STATE;
366  strm->adler =
367 #ifdef GZIP
368  s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
369 #endif
370  adler32(0L, Z_NULL, 0);
371  s->last_flush = Z_NO_FLUSH;
372 
373  _tr_init(s);
374  lm_init(s);
375 
376  return Z_OK;
377 }
#define INIT_STATE
Definition: deflate.h:53
#define Z_NO_FLUSH
Definition: zlib.h:172
#define Z_UNKNOWN
Definition: zlib.h:210
Bytef * pending_out
Definition: deflate.h:101
#define Z_STREAM_ERROR
Definition: zlib.h:185
#define BUSY_STATE
Definition: deflate.h:58
z_streamp strm
Definition: deflate.h:97
void ZLIB_INTERNAL _tr_init(deflate_state *s)
Definition: trees.c:394
if(last==0)
Definition: sparse_int.h:34
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:230
Bytef * pending_buf
Definition: deflate.h:99
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition: adler32.c:67
uInt pending
Definition: deflate.h:102
#define Z_OK
Definition: zlib.h:181
local void lm_init(deflate_state *s)
Definition: deflate.c:987
#define Z_NULL
Definition: zlib.h:216
int last_flush
Definition: deflate.h:107
int ZEXPORT deflateSetDictionary ( z_streamp  strm,
const Bytef dictionary,
uInt  dictLength 
)

Definition at line 306 of file deflate.c.

307 {
308  deflate_state *s;
309  uInt length = dictLength;
310  uInt n;
311  IPos hash_head = 0;
312 
313  if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
314  strm->state->wrap == 2 ||
315  (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
316  return Z_STREAM_ERROR;
317 
318  s = strm->state;
319  if (s->wrap)
320  strm->adler = adler32(strm->adler, dictionary, dictLength);
321 
322  if (length < MIN_MATCH) return Z_OK;
323  if (length > s->w_size) {
324  length = s->w_size;
325  dictionary += dictLength - length; /* use the tail of the dictionary */
326  }
327  zmemcpy(s->window, dictionary, length);
328  s->strstart = length;
329  s->block_start = (long)length;
330 
331  /* Insert all strings in the hash table (except for the last two bytes).
332  * s->lookahead stays null, so s->ins_h will be recomputed at the next
333  * call of fill_window.
334  */
335  s->ins_h = s->window[0];
336  UPDATE_HASH(s, s->ins_h, s->window[1]);
337  for (n = 0; n <= length - MIN_MATCH; n++) {
338  INSERT_STRING(s, n, hash_head);
339  }
340  if (hash_head) hash_head = 0; /* to make compiler happy */
341  return Z_OK;
342 }
#define INIT_STATE
Definition: deflate.h:53
#define INSERT_STRING(s, str, match_head)
Definition: deflate.c:190
Bytef * window
Definition: deflate.h:115
#define MIN_MATCH
Definition: zutil.h:73
#define UPDATE_HASH(s, h, c)
Definition: deflate.c:171
#define Z_STREAM_ERROR
Definition: zlib.h:185
z_streamp strm
Definition: deflate.h:97
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.c:157
long block_start
Definition: deflate.h:150
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition: adler32.c:67
uInt strstart
Definition: deflate.h:158
#define Z_OK
Definition: zlib.h:181
#define Z_NULL
Definition: zlib.h:216
unsigned int uInt
Definition: zconf.h:335
unsigned IPos
Definition: deflate.h:90
int ZEXPORT deflateSetHeader ( z_streamp  strm,
gz_headerp  head 
)

Definition at line 380 of file deflate.c.

381 {
382  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
383  if (strm->state->wrap != 2) return Z_STREAM_ERROR;
384  strm->state->gzhead = head;
385  return Z_OK;
386 }
#define Z_STREAM_ERROR
Definition: zlib.h:185
z_streamp strm
Definition: deflate.h:97
Posf * head
Definition: deflate.h:136
#define Z_OK
Definition: zlib.h:181
#define Z_NULL
Definition: zlib.h:216
int ZEXPORT deflateTune ( z_streamp  strm,
int  good_length,
int  max_lazy,
int  nice_length,
int  max_chain 
)

Definition at line 434 of file deflate.c.

435 {
436  deflate_state *s;
437 
438  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
439  s = strm->state;
440  s->good_match = good_length;
441  s->max_lazy_match = max_lazy;
442  s->nice_match = nice_length;
443  s->max_chain_length = max_chain;
444  return Z_OK;
445 }
uInt good_match
Definition: deflate.h:187
uInt max_lazy_match
Definition: deflate.h:173
#define Z_STREAM_ERROR
Definition: zlib.h:185
z_streamp strm
Definition: deflate.h:97
uInt max_chain_length
Definition: deflate.h:167
int nice_match
Definition: deflate.h:190
#define Z_OK
Definition: zlib.h:181
#define Z_NULL
Definition: zlib.h:216
local void fill_window ( deflate_state s)

Definition at line 1262 of file deflate.c.

1263 {
1264  register unsigned n, m;
1265  register Posf *p;
1266  unsigned more; /* Amount of free space at the end of the window. */
1267  uInt wsize = s->w_size;
1268 
1269  do {
1270  more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1271 
1272  /* Deal with !@#$% 64K limit: */
1273  if (sizeof(int) <= 2) {
1274  if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1275  more = wsize;
1276 
1277  } else if (more == (unsigned)(-1)) {
1278  /* Very unlikely, but possible on 16 bit machine if
1279  * strstart == 0 && lookahead == 1 (input done a byte at time)
1280  */
1281  more--;
1282  }
1283  }
1284 
1285  /* If the window is almost full and there is insufficient lookahead,
1286  * move the upper half to the lower one to make room in the upper half.
1287  */
1288  if (s->strstart >= wsize+MAX_DIST(s)) {
1289 
1290  zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
1291  s->match_start -= wsize;
1292  s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1293  s->block_start -= (long) wsize;
1294 
1295  /* Slide the hash table (could be avoided with 32 bit values
1296  at the expense of memory usage). We slide even when level == 0
1297  to keep the hash table consistent if we switch back to level > 0
1298  later. (Using level 0 permanently is not an optimal usage of
1299  zlib, so we don't care about this pathological case.)
1300  */
1301  n = s->hash_size;
1302  p = &s->head[n];
1303  do {
1304  m = *--p;
1305  *p = (Pos)(m >= wsize ? m-wsize : NIL);
1306  } while (--n);
1307 
1308  n = wsize;
1309 #ifndef FASTEST
1310  p = &s->prev[n];
1311  do {
1312  m = *--p;
1313  *p = (Pos)(m >= wsize ? m-wsize : NIL);
1314  /* If n is not on any hash chain, prev[n] is garbage but
1315  * its value will never be used.
1316  */
1317  } while (--n);
1318 #endif
1319  more += wsize;
1320  }
1321  if (s->strm->avail_in == 0) return;
1322 
1323  /* If there was no sliding:
1324  * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1325  * more == window_size - lookahead - strstart
1326  * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1327  * => more >= window_size - 2*WSIZE + 2
1328  * In the BIG_MEM or MMAP case (not yet supported),
1329  * window_size == input_size + MIN_LOOKAHEAD &&
1330  * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1331  * Otherwise, window_size == 2*WSIZE so more >= 2.
1332  * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1333  */
1334  Assert(more >= 2, "more < 2");
1335 
1336  n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1337  s->lookahead += n;
1338 
1339  /* Initialize the hash value now that we have some input: */
1340  if (s->lookahead >= MIN_MATCH) {
1341  s->ins_h = s->window[s->strstart];
1342  UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1343 #if MIN_MATCH != 3
1344  Call UPDATE_HASH() MIN_MATCH-3 more times
1345 #endif
1346  }
1347  /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1348  * but this is not important since only literal bytes will be emitted.
1349  */
1350 
1351  } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1352 
1353  /* If the WIN_INIT bytes after the end of the current data have never been
1354  * written, then zero those bytes in order to avoid memory check reports of
1355  * the use of uninitialized (or uninitialised as Julian writes) bytes by
1356  * the longest match routines. Update the high water mark for the next
1357  * time through here. WIN_INIT is set to MAX_MATCH since the longest match
1358  * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1359  */
1360  if (s->high_water < s->window_size) {
1361  ulg curr = s->strstart + (ulg)(s->lookahead);
1362  ulg init;
1363 
1364  if (s->high_water < curr) {
1365  /* Previous high water mark below current data -- zero WIN_INIT
1366  * bytes or up to end of window, whichever is less.
1367  */
1368  init = s->window_size - curr;
1369  if (init > WIN_INIT)
1370  init = WIN_INIT;
1371  zmemzero(s->window + curr, (unsigned)init);
1372  s->high_water = curr + init;
1373  }
1374  else if (s->high_water < (ulg)curr + WIN_INIT) {
1375  /* High water mark at or above current data, but below current data
1376  * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1377  * to end of window, whichever is less.
1378  */
1379  init = (ulg)curr + WIN_INIT - s->high_water;
1380  if (init > s->window_size - s->high_water)
1381  init = s->window_size - s->high_water;
1382  zmemzero(s->window + s->high_water, (unsigned)init);
1383  s->high_water += init;
1384  }
1385  }
1386 }
Bytef * window
Definition: deflate.h:115
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define MIN_MATCH
Definition: zutil.h:73
ush Pos
Definition: deflate.h:88
#define Assert(cond, msg)
Definition: zutil.h:268
#define UPDATE_HASH(s, h, c)
Definition: deflate.c:171
Posf * prev
Definition: deflate.h:130
uInt match_start
Definition: deflate.h:159
local int read_buf(z_streamp strm, Bytef *buf, unsigned size)
Definition: deflate.c:960
ulg high_water
Definition: deflate.h:265
uInt lookahead
Definition: deflate.h:160
#define WIN_INIT
Definition: deflate.h:290
#define MAX_DIST(s)
Definition: deflate.h:285
ulg window_size
Definition: deflate.h:125
#define MIN_LOOKAHEAD
Definition: deflate.h:280
z_streamp strm
Definition: deflate.h:97
Posf * head
Definition: deflate.h:136
#define NIL
Definition: deflate.c:111
if(last==0)
Definition: sparse_int.h:34
Pos FAR Posf
Definition: deflate.h:89
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
Definition: zutil.c:175
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.c:157
long block_start
Definition: deflate.h:150
unsigned long ulg
Definition: zutil.h:43
uInt strstart
Definition: deflate.h:158
uInt hash_size
Definition: deflate.h:139
unsigned int uInt
Definition: zconf.h:335
local void flush_pending ( z_streamp  strm)

Definition at line 536 of file deflate.c.

537 {
538  unsigned len = strm->state->pending;
539 
540  if (len > strm->avail_out) len = strm->avail_out;
541  if (len == 0) return;
542 
543  zmemcpy(strm->next_out, strm->state->pending_out, len);
544  strm->next_out += len;
545  strm->state->pending_out += len;
546  strm->total_out += len;
547  strm->avail_out -= len;
548  strm->state->pending -= len;
549  if (strm->state->pending == 0) {
550  strm->state->pending_out = strm->state->pending_buf;
551  }
552 }
z_streamp strm
Definition: deflate.h:97
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.c:157
local void lm_init ( deflate_state s)

Definition at line 987 of file deflate.c.

988 {
989  s->window_size = (ulg)2L*s->w_size;
990 
991  CLEAR_HASH(s);
992 
993  /* Set the default configuration parameters:
994  */
999 
1000  s->strstart = 0;
1001  s->block_start = 0L;
1002  s->lookahead = 0;
1003  s->match_length = s->prev_length = MIN_MATCH-1;
1004  s->match_available = 0;
1005  s->ins_h = 0;
1006 #ifndef FASTEST
1007 #ifdef ASMV
1008  match_init(); /* initialize the asm code */
1009 #endif
1010 #endif
1011 }
uInt good_match
Definition: deflate.h:187
#define MIN_MATCH
Definition: zutil.h:73
uInt max_lazy_match
Definition: deflate.h:173
uInt match_length
Definition: deflate.h:155
uInt prev_length
Definition: deflate.h:162
ush good_length
Definition: deflate.c:125
uInt lookahead
Definition: deflate.h:160
ulg window_size
Definition: deflate.h:125
ush max_chain
Definition: deflate.c:128
#define CLEAR_HASH(s)
Definition: deflate.c:200
local const config configuration_table[10]
Definition: deflate.c:138
long block_start
Definition: deflate.h:150
ush max_lazy
Definition: deflate.c:126
int match_available
Definition: deflate.h:157
uInt max_chain_length
Definition: deflate.h:167
int nice_match
Definition: deflate.h:190
unsigned long ulg
Definition: zutil.h:43
uInt strstart
Definition: deflate.h:158
ush nice_length
Definition: deflate.c:127
local uInt longest_match ( deflate_state s,
IPos  cur_match 
)

Definition at line 1027 of file deflate.c.

1028 {
1029  unsigned chain_length = s->max_chain_length;/* max hash chain length */
1030  register Bytef *scan = s->window + s->strstart; /* current string */
1031  register Bytef *match; /* matched string */
1032  register int len; /* length of current match */
1033  int best_len = s->prev_length; /* best match length so far */
1034  int nice_match = s->nice_match; /* stop if match long enough */
1035  IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1036  s->strstart - (IPos)MAX_DIST(s) : NIL;
1037  /* Stop when cur_match becomes <= limit. To simplify the code,
1038  * we prevent matches with the string of window index 0.
1039  */
1040  Posf *prev = s->prev;
1041  uInt wmask = s->w_mask;
1042 
1043 #ifdef UNALIGNED_OK
1044  /* Compare two bytes at a time. Note: this is not always beneficial.
1045  * Try with and without -DUNALIGNED_OK to check.
1046  */
1047  register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1048  register ush scan_start = *(ushf*)scan;
1049  register ush scan_end = *(ushf*)(scan+best_len-1);
1050 #else
1051  register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1052  register Byte scan_end1 = scan[best_len-1];
1053  register Byte scan_end = scan[best_len];
1054 #endif
1055 
1056  /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1057  * It is easy to get rid of this optimization if necessary.
1058  */
1059  Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1060 
1061  /* Do not waste too much time if we already have a good match: */
1062  if (s->prev_length >= s->good_match) {
1063  chain_length >>= 2;
1064  }
1065  /* Do not look for matches beyond the end of the input. This is necessary
1066  * to make deflate deterministic.
1067  */
1068  if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1069 
1070  Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1071 
1072  do {
1073  Assert(cur_match < s->strstart, "no future");
1074  match = s->window + cur_match;
1075 
1076  /* Skip to next match if the match length cannot increase
1077  * or if the match length is less than 2. Note that the checks below
1078  * for insufficient lookahead only occur occasionally for performance
1079  * reasons. Therefore uninitialized memory will be accessed, and
1080  * conditional jumps will be made that depend on those values.
1081  * However the length of the match is limited to the lookahead, so
1082  * the output of deflate is not affected by the uninitialized values.
1083  */
1084 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1085  /* This code assumes sizeof(unsigned short) == 2. Do not use
1086  * UNALIGNED_OK if your compiler uses a different size.
1087  */
1088  if (*(ushf*)(match+best_len-1) != scan_end ||
1089  *(ushf*)match != scan_start) continue;
1090 
1091  /* It is not necessary to compare scan[2] and match[2] since they are
1092  * always equal when the other bytes match, given that the hash keys
1093  * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1094  * strstart+3, +5, ... up to strstart+257. We check for insufficient
1095  * lookahead only every 4th comparison; the 128th check will be made
1096  * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1097  * necessary to put more guard bytes at the end of the window, or
1098  * to check more often for insufficient lookahead.
1099  */
1100  Assert(scan[2] == match[2], "scan[2]?");
1101  scan++, match++;
1102  do {
1103  } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1104  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1105  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1106  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1107  scan < strend);
1108  /* The funny "do {}" generates better code on most compilers */
1109 
1110  /* Here, scan <= window+strstart+257 */
1111  Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1112  if (*scan == *match) scan++;
1113 
1114  len = (MAX_MATCH - 1) - (int)(strend-scan);
1115  scan = strend - (MAX_MATCH-1);
1116 
1117 #else /* UNALIGNED_OK */
1118 
1119  if (match[best_len] != scan_end ||
1120  match[best_len-1] != scan_end1 ||
1121  *match != *scan ||
1122  *++match != scan[1]) continue;
1123 
1124  /* The check at best_len-1 can be removed because it will be made
1125  * again later. (This heuristic is not always a win.)
1126  * It is not necessary to compare scan[2] and match[2] since they
1127  * are always equal when the other bytes match, given that
1128  * the hash keys are equal and that HASH_BITS >= 8.
1129  */
1130  scan += 2, match++;
1131  Assert(*scan == *match, "match[2]?");
1132 
1133  /* We check for insufficient lookahead only every 8th comparison;
1134  * the 256th check will be made at strstart+258.
1135  */
1136  do {
1137  } while (*++scan == *++match && *++scan == *++match &&
1138  *++scan == *++match && *++scan == *++match &&
1139  *++scan == *++match && *++scan == *++match &&
1140  *++scan == *++match && *++scan == *++match &&
1141  scan < strend);
1142 
1143  Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1144 
1145  len = MAX_MATCH - (int)(strend - scan);
1146  scan = strend - MAX_MATCH;
1147 
1148 #endif /* UNALIGNED_OK */
1149 
1150  if (len > best_len) {
1151  s->match_start = cur_match;
1152  best_len = len;
1153  if (len >= nice_match) break;
1154 #ifdef UNALIGNED_OK
1155  scan_end = *(ushf*)(scan+best_len-1);
1156 #else
1157  scan_end1 = scan[best_len-1];
1158  scan_end = scan[best_len];
1159 #endif
1160  }
1161  } while ((cur_match = prev[cur_match & wmask]) > limit
1162  && --chain_length != 0);
1163 
1164  if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1165  return s->lookahead;
1166 }
uInt good_match
Definition: deflate.h:187
uInt hash_bits
Definition: deflate.h:140
ush FAR ushf
Definition: zutil.h:42
Bytef * window
Definition: deflate.h:115
unsigned char Byte
Definition: zconf.h:333
#define Assert(cond, msg)
Definition: zutil.h:268
uInt prev_length
Definition: deflate.h:162
Posf * prev
Definition: deflate.h:130
uInt match_start
Definition: deflate.h:159
Byte FAR Bytef
Definition: zconf.h:342
uInt lookahead
Definition: deflate.h:160
#define MAX_DIST(s)
Definition: deflate.h:285
ulg window_size
Definition: deflate.h:125
#define MIN_LOOKAHEAD
Definition: deflate.h:280
#define NIL
Definition: deflate.c:111
unsigned short ush
Definition: zutil.h:41
static bool match(B &in, const char *str)
Definition: ParseUtils.h:99
#define MAX_MATCH
Definition: zutil.h:74
Pos FAR Posf
Definition: deflate.h:89
uInt max_chain_length
Definition: deflate.h:167
int nice_match
Definition: deflate.h:190
unsigned long ulg
Definition: zutil.h:43
uInt strstart
Definition: deflate.h:158
unsigned int uInt
Definition: zconf.h:335
unsigned IPos
Definition: deflate.h:90
local void fill_window OF ( (deflate_state *s)  )
local block_state deflate_stored OF ( (deflate_state *s, int flush)  )
local void putShortMSB OF ( (deflate_state *s, uInt b)  )
local void flush_pending OF ( (z_streamp strm)  )
local int read_buf OF ( (z_streamp strm, Bytef *buf, unsigned size )
local uInt longest_match OF ( (deflate_state *s, IPos cur_match)  )
local void putShortMSB ( deflate_state s,
uInt  b 
)

Definition at line 524 of file deflate.c.

525 {
526  put_byte(s, (Byte)(b >> 8));
527  put_byte(s, (Byte)(b & 0xff));
528 }
unsigned char Byte
Definition: zconf.h:333
#define put_byte(s, c)
Definition: deflate.h:277
local int read_buf ( z_streamp  strm,
Bytef buf,
unsigned  size 
)

Definition at line 960 of file deflate.c.

961 {
962  unsigned len = strm->avail_in;
963 
964  if (len > size) len = size;
965  if (len == 0) return 0;
966 
967  strm->avail_in -= len;
968 
969  if (strm->state->wrap == 1) {
970  strm->adler = adler32(strm->adler, strm->next_in, len);
971  }
972 #ifdef GZIP
973  else if (strm->state->wrap == 2) {
974  strm->adler = crc32(strm->adler, strm->next_in, len);
975  }
976 #endif
977  zmemcpy(buf, strm->next_in, len);
978  strm->next_in += len;
979  strm->total_in += len;
980 
981  return (int)len;
982 }
z_streamp strm
Definition: deflate.h:97
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:230
static int size
Definition: cuddSign.c:86
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.c:157
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition: adler32.c:67

Variable Documentation

local const config configuration_table[10]
Initial value:
= {
{0, 0, 0, 0, deflate_stored},
{4, 4, 8, 4, deflate_fast},
{4, 5, 16, 8, deflate_fast},
{4, 6, 32, 32, deflate_fast},
{4, 4, 16, 16, deflate_slow},
{8, 16, 32, 32, deflate_slow},
{8, 16, 128, 128, deflate_slow},
{8, 32, 128, 256, deflate_slow},
{32, 128, 258, 1024, deflate_slow},
{32, 258, 258, 4096, deflate_slow}}
local block_state deflate_stored(deflate_state *s, int flush)
Definition: deflate.c:1418
local block_state deflate_fast(deflate_state *s, int flush)
Definition: deflate.c:1474
local block_state deflate_slow(deflate_state *s, int flush)
Definition: deflate.c:1568

Definition at line 138 of file deflate.c.

ABC_NAMESPACE_IMPL_START const char deflate_copyright[]
Initial value:
=
" deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler "

Definition at line 61 of file deflate.c.