Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
deflate.c File Reference
#include "deflate.h"

Go to the source code of this file.

Classes

struct  config_s
 

Macros

#define NIL   0
 
#define TOO_FAR   4096
 
#define RANK(f)   (((f) * 2) - ((f) > 4 ? 9 : 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 HCRC_UPDATE(beg)
 
#define check_match(s, start, match, length)
 
#define FLUSH_BLOCK_ONLY(s, last)
 
#define FLUSH_BLOCK(s, last)
 
#define MAX_STORED   65535
 
#define MIN(a, b)   ((a) > (b) ? (b) : (a))
 

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

int deflateStateCheck OF ((z_streamp strm))
 
void slide_hash OF ((deflate_state *s))
 
void putShortMSB OF ((deflate_state *s, uInt b))
 
unsigned read_buf OF ((z_streamp strm, Bytef *buf, unsigned size))
 
uInt longest_match OF ((deflate_state *s, IPos cur_match))
 
void slide_hash (deflate_state *s)
 
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 deflateStateCheck (z_streamp strm)
 
int ZEXPORT deflateSetDictionary (z_streamp strm, const Bytef *dictionary, uInt dictLength)
 
int ZEXPORT deflateGetDictionary (z_streamp strm, Bytef *dictionary, uInt *dictLength)
 
int ZEXPORT deflateResetKeep (z_streamp strm)
 
int ZEXPORT deflateReset (z_streamp strm)
 
int ZEXPORT deflateSetHeader (z_streamp strm, gz_headerp head)
 
int ZEXPORT deflatePending (z_streamp strm, unsigned *pending, int *bits)
 
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)
 
void putShortMSB (deflate_state *s, uInt b)
 
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)
 
unsigned read_buf (z_streamp strm, Bytef *buf, unsigned size)
 
void lm_init (deflate_state *s)
 
uInt longest_match (deflate_state *s, IPos cur_match)
 
void fill_window (deflate_state *s)
 
block_state deflate_stored (deflate_state *s, int flush)
 
block_state deflate_fast (deflate_state *s, int flush)
 
block_state deflate_slow (deflate_state *s, int flush)
 
block_state deflate_rle (deflate_state *s, int flush)
 
block_state deflate_huff (deflate_state *s, int flush)
 

Variables

const char deflate_copyright []
 
const config configuration_table [10]
 

Macro Definition Documentation

◆ check_match

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

Definition at line 1512 of file deflate.c.

◆ CLEAR_HASH

#define CLEAR_HASH (   s)
Value:
do { \
s->head[s->hash_size-1] = NIL; \
zmemzero((Bytef *)s->head, \
(unsigned)(s->hash_size-1)*sizeof(*s->head)); \
} while (0)
#define NIL
Definition: deflate.c:106

Definition at line 191 of file deflate.c.

◆ FLUSH_BLOCK

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

Definition at line 1662 of file deflate.c.

◆ FLUSH_BLOCK_ONLY

#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]")); \
}
#define Z_NULL
Definition: zlib.h:212
unsigned long ulg
Definition: zutil.h:42

Definition at line 1650 of file deflate.c.

◆ HCRC_UPDATE

#define HCRC_UPDATE (   beg)
Value:
do { \
if (s->gzhead->hcrc && s->pending > (beg)) \
strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
s->pending - (beg)); \
} while (0)
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:1062

Definition at line 799 of file deflate.c.

◆ INSERT_STRING

#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 UPDATE_HASH(s, h, c)
Definition: deflate.c:162
ush Pos
Definition: deflate.h:91
#define MIN_MATCH
Definition: zutil.h:83

Definition at line 181 of file deflate.c.

◆ MAX_STORED

#define MAX_STORED   65535

Definition at line 1668 of file deflate.c.

◆ MIN

#define MIN (   a,
 
)    ((a) > (b) ? (b) : (a))

Definition at line 1671 of file deflate.c.

◆ NIL

#define NIL   0

Definition at line 106 of file deflate.c.

◆ RANK

#define RANK (   f)    (((f) * 2) - ((f) > 4 ? 9 : 0))

Definition at line 154 of file deflate.c.

◆ TOO_FAR

#define TOO_FAR   4096

Definition at line 110 of file deflate.c.

◆ UPDATE_HASH

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

Definition at line 162 of file deflate.c.

Typedef Documentation

◆ config

typedef struct config_s config

◆ OF

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

Definition at line 72 of file deflate.c.

Enumeration Type Documentation

◆ block_state

Enumerator
need_more 
block_done 
finish_started 
finish_done 

Definition at line 65 of file deflate.c.

65 {
66 need_more, /* block not completed, need more input or more output */
67 block_done, /* block flush performed */
68 finish_started, /* finish started, need only more output at next deflate */
69 finish_done /* finish done, accept no more input or output */
block_state
Definition: deflate.c:65
@ block_done
Definition: deflate.c:67
@ finish_done
Definition: deflate.c:69

Function Documentation

◆ deflate()

int ZEXPORT deflate ( z_streamp  strm,
int  flush 
)

Definition at line 807 of file deflate.c.

810{
811 int old_flush; /* value of flush param for previous deflate call */
812 deflate_state *s;
813
814 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
815 return Z_STREAM_ERROR;
816 }
817 s = strm->state;
818
819 if (strm->next_out == Z_NULL ||
820 (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
821 (s->status == FINISH_STATE && flush != Z_FINISH)) {
823 }
824 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
825
826 old_flush = s->last_flush;
827 s->last_flush = flush;
828
829 /* Flush as much pending output as possible */
830 if (s->pending != 0) {
831 flush_pending(strm);
832 if (strm->avail_out == 0) {
833 /* Since avail_out is 0, deflate will be called again with
834 * more output space, but possibly with both pending and
835 * avail_in equal to zero. There won't be anything to do,
836 * but this is not an error situation so make sure we
837 * return OK instead of BUF_ERROR at next call of deflate:
838 */
839 s->last_flush = -1;
840 return Z_OK;
841 }
842
843 /* Make sure there is something to do and avoid duplicate consecutive
844 * flushes. For repeated and useless calls with Z_FINISH, we keep
845 * returning Z_STREAM_END instead of Z_BUF_ERROR.
846 */
847 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
848 flush != Z_FINISH) {
849 ERR_RETURN(strm, Z_BUF_ERROR);
850 }
851
852 /* User must not provide more input after the first FINISH: */
853 if (s->status == FINISH_STATE && strm->avail_in != 0) {
854 ERR_RETURN(strm, Z_BUF_ERROR);
855 }
856
857 /* Write the header */
858 if (s->status == INIT_STATE && s->wrap == 0)
859 s->status = BUSY_STATE;
860 if (s->status == INIT_STATE) {
861 /* zlib header */
862 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
863 uInt level_flags;
864
865 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
866 level_flags = 0;
867 else if (s->level < 6)
868 level_flags = 1;
869 else if (s->level == 6)
870 level_flags = 2;
871 else
872 level_flags = 3;
873 header |= (level_flags << 6);
874 if (s->strstart != 0) header |= PRESET_DICT;
875 header += 31 - (header % 31);
876
877 putShortMSB(s, header);
878
879 /* Save the adler32 of the preset dictionary: */
880 if (s->strstart != 0) {
881 putShortMSB(s, (uInt)(strm->adler >> 16));
882 putShortMSB(s, (uInt)(strm->adler & 0xffff));
883 }
884 strm->adler = adler32(0L, Z_NULL, 0);
885 s->status = BUSY_STATE;
886
887 /* Compression must start with an empty pending buffer */
888 flush_pending(strm);
889 if (s->pending != 0) {
890 s->last_flush = -1;
891 return Z_OK;
892 }
893 }
894#ifdef GZIP
895 if (s->status == GZIP_STATE) {
896 /* gzip header */
897 strm->adler = crc32(0L, Z_NULL, 0);
898 put_byte(s, 31);
899 put_byte(s, 139);
900 put_byte(s, 8);
901 if (s->gzhead == Z_NULL) {
902 put_byte(s, 0);
903 put_byte(s, 0);
904 put_byte(s, 0);
905 put_byte(s, 0);
906 put_byte(s, 0);
907 put_byte(s, s->level == 9 ? 2 :
908 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
909 4 : 0));
910 put_byte(s, OS_CODE);
911 s->status = BUSY_STATE;
912
913 /* Compression must start with an empty pending buffer */
914 flush_pending(strm);
915 if (s->pending != 0) {
916 s->last_flush = -1;
917 return Z_OK;
918 }
919 }
920 else {
921 put_byte(s, (s->gzhead->text ? 1 : 0) +
922 (s->gzhead->hcrc ? 2 : 0) +
923 (s->gzhead->extra == Z_NULL ? 0 : 4) +
924 (s->gzhead->name == Z_NULL ? 0 : 8) +
925 (s->gzhead->comment == Z_NULL ? 0 : 16)
926 );
927 put_byte(s, (Byte)(s->gzhead->time & 0xff));
928 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
929 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
930 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
931 put_byte(s, s->level == 9 ? 2 :
932 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
933 4 : 0));
934 put_byte(s, s->gzhead->os & 0xff);
935 if (s->gzhead->extra != Z_NULL) {
936 put_byte(s, s->gzhead->extra_len & 0xff);
937 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
938 }
939 if (s->gzhead->hcrc)
940 strm->adler = crc32(strm->adler, s->pending_buf,
941 s->pending);
942 s->gzindex = 0;
943 s->status = EXTRA_STATE;
944 }
945 }
946 if (s->status == EXTRA_STATE) {
947 if (s->gzhead->extra != Z_NULL) {
948 ulg beg = s->pending; /* start of bytes to update crc */
949 uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
950 while (s->pending + left > s->pending_buf_size) {
951 uInt copy = s->pending_buf_size - s->pending;
953 s->gzhead->extra + s->gzindex, copy);
955 HCRC_UPDATE(beg);
956 s->gzindex += copy;
957 flush_pending(strm);
958 if (s->pending != 0) {
959 s->last_flush = -1;
960 return Z_OK;
961 }
962 beg = 0;
963 left -= copy;
964 }
966 s->gzhead->extra + s->gzindex, left);
967 s->pending += left;
968 HCRC_UPDATE(beg);
969 s->gzindex = 0;
970 }
971 s->status = NAME_STATE;
972 }
973 if (s->status == NAME_STATE) {
974 if (s->gzhead->name != Z_NULL) {
975 ulg beg = s->pending; /* start of bytes to update crc */
976 int val;
977 do {
978 if (s->pending == s->pending_buf_size) {
979 HCRC_UPDATE(beg);
980 flush_pending(strm);
981 if (s->pending != 0) {
982 s->last_flush = -1;
983 return Z_OK;
984 }
985 beg = 0;
986 }
987 val = s->gzhead->name[s->gzindex++];
988 put_byte(s, val);
989 } while (val != 0);
990 HCRC_UPDATE(beg);
991 s->gzindex = 0;
992 }
994 }
995 if (s->status == COMMENT_STATE) {
996 if (s->gzhead->comment != Z_NULL) {
997 ulg beg = s->pending; /* start of bytes to update crc */
998 int val;
999 do {
1000 if (s->pending == s->pending_buf_size) {
1001 HCRC_UPDATE(beg);
1002 flush_pending(strm);
1003 if (s->pending != 0) {
1004 s->last_flush = -1;
1005 return Z_OK;
1006 }
1007 beg = 0;
1008 }
1009 val = s->gzhead->comment[s->gzindex++];
1010 put_byte(s, val);
1011 } while (val != 0);
1012 HCRC_UPDATE(beg);
1013 }
1014 s->status = HCRC_STATE;
1015 }
1016 if (s->status == HCRC_STATE) {
1017 if (s->gzhead->hcrc) {
1018 if (s->pending + 2 > s->pending_buf_size) {
1019 flush_pending(strm);
1020 if (s->pending != 0) {
1021 s->last_flush = -1;
1022 return Z_OK;
1023 }
1024 }
1025 put_byte(s, (Byte)(strm->adler & 0xff));
1026 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1027 strm->adler = crc32(0L, Z_NULL, 0);
1028 }
1029 s->status = BUSY_STATE;
1030
1031 /* Compression must start with an empty pending buffer */
1032 flush_pending(strm);
1033 if (s->pending != 0) {
1034 s->last_flush = -1;
1035 return Z_OK;
1036 }
1037 }
1038#endif
1039
1040 /* Start a new block or continue the current one.
1041 */
1042 if (strm->avail_in != 0 || s->lookahead != 0 ||
1043 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
1044 block_state bstate;
1045
1046 bstate = s->level == 0 ? deflate_stored(s, flush) :
1047 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
1048 s->strategy == Z_RLE ? deflate_rle(s, flush) :
1049 (*(configuration_table[s->level].func))(s, flush);
1050
1051 if (bstate == finish_started || bstate == finish_done) {
1052 s->status = FINISH_STATE;
1053 }
1054 if (bstate == need_more || bstate == finish_started) {
1055 if (strm->avail_out == 0) {
1056 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
1057 }
1058 return Z_OK;
1059 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1060 * of deflate should use the same flush parameter to make sure
1061 * that the flush is complete. So we don't have to output an
1062 * empty block here, this will be done at next call. This also
1063 * ensures that for a very small output buffer, we emit at most
1064 * one empty block.
1065 */
1066 }
1067 if (bstate == block_done) {
1068 if (flush == Z_PARTIAL_FLUSH) {
1069 _tr_align(s);
1070 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
1071 _tr_stored_block(s, (char*)0, 0L, 0);
1072 /* For a full flush, this empty block will be recognized
1073 * as a special marker by inflate_sync().
1074 */
1075 if (flush == Z_FULL_FLUSH) {
1076 CLEAR_HASH(s); /* forget history */
1077 if (s->lookahead == 0) {
1078 s->strstart = 0;
1079 s->block_start = 0L;
1080 s->insert = 0;
1081 }
1082 }
1083 }
1084 flush_pending(strm);
1085 if (strm->avail_out == 0) {
1086 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1087 return Z_OK;
1088 }
1089 }
1090 }
1091
1092 if (flush != Z_FINISH) return Z_OK;
1093 if (s->wrap <= 0) return Z_STREAM_END;
1094
1095 /* Write the trailer */
1096#ifdef GZIP
1097 if (s->wrap == 2) {
1098 put_byte(s, (Byte)(strm->adler & 0xff));
1099 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1100 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
1101 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
1102 put_byte(s, (Byte)(strm->total_in & 0xff));
1103 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
1104 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
1105 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
1106 }
1107 else
1108#endif
1109 {
1110 putShortMSB(s, (uInt)(strm->adler >> 16));
1111 putShortMSB(s, (uInt)(strm->adler & 0xffff));
1112 }
1113 flush_pending(strm);
1114 /* If avail_out is zero, the application will call deflate again
1115 * to flush the rest.
1116 */
1117 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
1118 return s->pending != 0 ? Z_OK : Z_STREAM_END;
1119}
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition: adler32.c:133
const config configuration_table[10]
Definition: deflate.c:133
#define HCRC_UPDATE(beg)
Definition: deflate.c:799
block_state deflate_stored(deflate_state *s, int flush)
Definition: deflate.c:1688
void putShortMSB(deflate_state *s, uInt b)
Definition: deflate.c:760
void flush_pending(z_streamp strm)
Definition: deflate.c:774
block_state deflate_huff(deflate_state *s, int flush)
Definition: deflate.c:2181
int deflateStateCheck(z_streamp strm)
Definition: deflate.c:396
#define RANK(f)
Definition: deflate.c:154
#define CLEAR_HASH(s)
Definition: deflate.c:191
block_state deflate_rle(deflate_state *s, int flush)
Definition: deflate.c:2108
#define FINISH_STATE
Definition: deflate.h:62
#define COMMENT_STATE
Definition: deflate.h:59
#define HCRC_STATE
Definition: deflate.h:60
#define GZIP_STATE
Definition: deflate.h:55
#define BUSY_STATE
Definition: deflate.h:61
#define put_byte(s, c)
Definition: deflate.h:275
#define INIT_STATE
Definition: deflate.h:53
#define NAME_STATE
Definition: deflate.h:58
#define EXTRA_STATE
Definition: deflate.h:57
void copy(G4double dst[], const G4double src[], std::size_t size=G4FieldTrack::ncompSVEC)
Definition: G4FieldUtils.cc:98
uInt lookahead
Definition: deflate.h:163
long block_start
Definition: deflate.h:153
ulg pending_buf_size
Definition: deflate.h:103
uInt strstart
Definition: deflate.h:161
int last_flush
Definition: deflate.h:110
gz_headerp gzhead
Definition: deflate.h:107
Bytef * pending_buf
Definition: deflate.h:102
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
Definition: trees.c:862
void ZLIB_INTERNAL _tr_align(deflate_state *s)
Definition: trees.c:896
#define Z_HUFFMAN_ONLY
Definition: zlib.h:197
#define Z_DEFLATED
Definition: zlib.h:209
#define Z_BUF_ERROR
Definition: zlib.h:184
#define Z_BLOCK
Definition: zlib.h:173
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_FINISH
Definition: zlib.h:172
#define Z_OK
Definition: zlib.h:177
#define Z_STREAM_ERROR
Definition: zlib.h:181
#define Z_NO_FLUSH
Definition: zlib.h:168
#define Z_PARTIAL_FLUSH
Definition: zlib.h:169
#define Z_FULL_FLUSH
Definition: zlib.h:171
#define Z_RLE
Definition: zlib.h:198
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.c:148
#define ERR_RETURN(strm, err)
Definition: zutil.h:60
#define PRESET_DICT
Definition: zutil.h:87
#define OS_CODE
Definition: zutil.h:200

Referenced by compress2(), deflateParams(), and gz_comp().

◆ deflate_fast()

block_state deflate_fast ( deflate_state s,
int  flush 
)

Definition at line 1875 of file deflate.c.

1878{
1879 IPos hash_head; /* head of the hash chain */
1880 int bflush; /* set if current block must be flushed */
1881
1882 for (;;) {
1883 /* Make sure that we always have enough lookahead, except
1884 * at the end of the input file. We need MAX_MATCH bytes
1885 * for the next match, plus MIN_MATCH bytes to insert the
1886 * string following the next match.
1887 */
1888 if (s->lookahead < MIN_LOOKAHEAD) {
1889 fill_window(s);
1890 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1891 return need_more;
1892 }
1893 if (s->lookahead == 0) break; /* flush the current block */
1894 }
1895
1896 /* Insert the string window[strstart .. strstart+2] in the
1897 * dictionary, and set hash_head to the head of the hash chain:
1898 */
1899 hash_head = NIL;
1900 if (s->lookahead >= MIN_MATCH) {
1901 INSERT_STRING(s, s->strstart, hash_head);
1902 }
1903
1904 /* Find the longest match, discarding those <= prev_length.
1905 * At this point we have always match_length < MIN_MATCH
1906 */
1907 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1908 /* To simplify the code, we prevent matches with the string
1909 * of window index 0 (in particular we have to avoid a match
1910 * of the string with itself at the start of the input file).
1911 */
1912 s->match_length = longest_match (s, hash_head);
1913 /* longest_match() sets match_start */
1914 }
1915 if (s->match_length >= MIN_MATCH) {
1917
1919 s->match_length - MIN_MATCH, bflush);
1920
1921 s->lookahead -= s->match_length;
1922
1923 /* Insert new strings in the hash table only if the match length
1924 * is not too large. This saves time but degrades compression.
1925 */
1926#ifndef FASTEST
1927 if (s->match_length <= s->max_insert_length &&
1928 s->lookahead >= MIN_MATCH) {
1929 s->match_length--; /* string at strstart already in table */
1930 do {
1931 s->strstart++;
1932 INSERT_STRING(s, s->strstart, hash_head);
1933 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1934 * always MIN_MATCH bytes ahead.
1935 */
1936 } while (--s->match_length != 0);
1937 s->strstart++;
1938 } else
1939#endif
1940 {
1941 s->strstart += s->match_length;
1942 s->match_length = 0;
1943 s->ins_h = s->window[s->strstart];
1944 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1945#if MIN_MATCH != 3
1946 Call UPDATE_HASH() MIN_MATCH-3 more times
1947#endif
1948 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1949 * matter since it will be recomputed at next deflate call.
1950 */
1951 }
1952 } else {
1953 /* No match, output a literal byte */
1954 Tracevv((stderr,"%c", s->window[s->strstart]));
1955 _tr_tally_lit (s, s->window[s->strstart], bflush);
1956 s->lookahead--;
1957 s->strstart++;
1958 }
1959 if (bflush) FLUSH_BLOCK(s, 0);
1960 }
1961 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1962 if (flush == Z_FINISH) {
1963 FLUSH_BLOCK(s, 1);
1964 return finish_done;
1965 }
1966 if (s->sym_next)
1967 FLUSH_BLOCK(s, 0);
1968 return block_done;
1969}
Definition: G4Pair.hh:151
#define check_match(s, start, match, length)
Definition: deflate.c:1512
#define INSERT_STRING(s, str, match_head)
Definition: deflate.c:181
#define FLUSH_BLOCK(s, last)
Definition: deflate.c:1662
uInt longest_match(deflate_state *s, IPos cur_match)
Definition: deflate.c:1279
void fill_window(deflate_state *s)
Definition: deflate.c:1525
#define MAX_DIST(s)
Definition: deflate.h:283
#define _tr_tally_dist(s, distance, length, flush)
Definition: deflate.h:328
#define MIN_LOOKAHEAD
Definition: deflate.h:278
unsigned IPos
Definition: deflate.h:93
#define _tr_tally_lit(s, c, flush)
Definition: deflate.h:320
uInt sym_next
Definition: deflate.h:241
Bytef * window
Definition: deflate.h:118
uInt match_length
Definition: deflate.h:158
uInt match_start
Definition: deflate.h:162
#define Tracevv(x)
Definition: zutil.h:253

◆ deflate_huff()

block_state deflate_huff ( deflate_state s,
int  flush 
)

Definition at line 2181 of file deflate.c.

2184{
2185 int bflush; /* set if current block must be flushed */
2186
2187 for (;;) {
2188 /* Make sure that we have a literal to write. */
2189 if (s->lookahead == 0) {
2190 fill_window(s);
2191 if (s->lookahead == 0) {
2192 if (flush == Z_NO_FLUSH)
2193 return need_more;
2194 break; /* flush the current block */
2195 }
2196 }
2197
2198 /* Output a literal byte */
2199 s->match_length = 0;
2200 Tracevv((stderr,"%c", s->window[s->strstart]));
2201 _tr_tally_lit (s, s->window[s->strstart], bflush);
2202 s->lookahead--;
2203 s->strstart++;
2204 if (bflush) FLUSH_BLOCK(s, 0);
2205 }
2206 s->insert = 0;
2207 if (flush == Z_FINISH) {
2208 FLUSH_BLOCK(s, 1);
2209 return finish_done;
2210 }
2211 if (s->sym_next)
2212 FLUSH_BLOCK(s, 0);
2213 return block_done;
2214}

Referenced by deflate().

◆ deflate_rle()

block_state deflate_rle ( deflate_state s,
int  flush 
)

Definition at line 2108 of file deflate.c.

2111{
2112 int bflush; /* set if current block must be flushed */
2113 uInt prev; /* byte at distance one to match */
2114 Bytef *scan, *strend; /* scan goes up to strend for length of run */
2115
2116 for (;;) {
2117 /* Make sure that we always have enough lookahead, except
2118 * at the end of the input file. We need MAX_MATCH bytes
2119 * for the longest run, plus one for the unrolled loop.
2120 */
2121 if (s->lookahead <= MAX_MATCH) {
2122 fill_window(s);
2123 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
2124 return need_more;
2125 }
2126 if (s->lookahead == 0) break; /* flush the current block */
2127 }
2128
2129 /* See how many times the previous byte repeats */
2130 s->match_length = 0;
2131 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
2132 scan = s->window + s->strstart - 1;
2133 prev = *scan;
2134 if (prev == *++scan && prev == *++scan && prev == *++scan) {
2135 strend = s->window + s->strstart + MAX_MATCH;
2136 do {
2137 } while (prev == *++scan && prev == *++scan &&
2138 prev == *++scan && prev == *++scan &&
2139 prev == *++scan && prev == *++scan &&
2140 prev == *++scan && prev == *++scan &&
2141 scan < strend);
2142 s->match_length = MAX_MATCH - (uInt)(strend - scan);
2143 if (s->match_length > s->lookahead)
2144 s->match_length = s->lookahead;
2145 }
2146 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
2147 }
2148
2149 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2150 if (s->match_length >= MIN_MATCH) {
2151 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2152
2153 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
2154
2155 s->lookahead -= s->match_length;
2156 s->strstart += s->match_length;
2157 s->match_length = 0;
2158 } else {
2159 /* No match, output a literal byte */
2160 Tracevv((stderr,"%c", s->window[s->strstart]));
2161 _tr_tally_lit (s, s->window[s->strstart], bflush);
2162 s->lookahead--;
2163 s->strstart++;
2164 }
2165 if (bflush) FLUSH_BLOCK(s, 0);
2166 }
2167 s->insert = 0;
2168 if (flush == Z_FINISH) {
2169 FLUSH_BLOCK(s, 1);
2170 return finish_done;
2171 }
2172 if (s->sym_next)
2173 FLUSH_BLOCK(s, 0);
2174 return block_done;
2175}
ulg window_size
Definition: deflate.h:128
#define Assert(cond, msg)
Definition: zutil.h:250
#define MAX_MATCH
Definition: zutil.h:84

Referenced by deflate().

◆ deflate_slow()

block_state deflate_slow ( deflate_state s,
int  flush 
)

Definition at line 1977 of file deflate.c.

1980{
1981 IPos hash_head; /* head of hash chain */
1982 int bflush; /* set if current block must be flushed */
1983
1984 /* Process the input block. */
1985 for (;;) {
1986 /* Make sure that we always have enough lookahead, except
1987 * at the end of the input file. We need MAX_MATCH bytes
1988 * for the next match, plus MIN_MATCH bytes to insert the
1989 * string following the next match.
1990 */
1991 if (s->lookahead < MIN_LOOKAHEAD) {
1992 fill_window(s);
1993 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1994 return need_more;
1995 }
1996 if (s->lookahead == 0) break; /* flush the current block */
1997 }
1998
1999 /* Insert the string window[strstart .. strstart+2] in the
2000 * dictionary, and set hash_head to the head of the hash chain:
2001 */
2002 hash_head = NIL;
2003 if (s->lookahead >= MIN_MATCH) {
2004 INSERT_STRING(s, s->strstart, hash_head);
2005 }
2006
2007 /* Find the longest match, discarding those <= prev_length.
2008 */
2010 s->match_length = MIN_MATCH-1;
2011
2012 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
2013 s->strstart - hash_head <= MAX_DIST(s)) {
2014 /* To simplify the code, we prevent matches with the string
2015 * of window index 0 (in particular we have to avoid a match
2016 * of the string with itself at the start of the input file).
2017 */
2018 s->match_length = longest_match (s, hash_head);
2019 /* longest_match() sets match_start */
2020
2021 if (s->match_length <= 5 && (s->strategy == Z_FILTERED
2022#if TOO_FAR <= 32767
2023 || (s->match_length == MIN_MATCH &&
2024 s->strstart - s->match_start > TOO_FAR)
2025#endif
2026 )) {
2027
2028 /* If prev_match is also MIN_MATCH, match_start is garbage
2029 * but we will ignore the current match anyway.
2030 */
2031 s->match_length = MIN_MATCH-1;
2032 }
2033 }
2034 /* If there was a match at the previous step and the current
2035 * match is not better, output the previous match:
2036 */
2037 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
2038 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
2039 /* Do not insert strings in hash table beyond this. */
2040
2041 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
2042
2043 _tr_tally_dist(s, s->strstart -1 - s->prev_match,
2044 s->prev_length - MIN_MATCH, bflush);
2045
2046 /* Insert in hash table all strings up to the end of the match.
2047 * strstart-1 and strstart are already inserted. If there is not
2048 * enough lookahead, the last two strings are not inserted in
2049 * the hash table.
2050 */
2051 s->lookahead -= s->prev_length-1;
2052 s->prev_length -= 2;
2053 do {
2054 if (++s->strstart <= max_insert) {
2055 INSERT_STRING(s, s->strstart, hash_head);
2056 }
2057 } while (--s->prev_length != 0);
2058 s->match_available = 0;
2059 s->match_length = MIN_MATCH-1;
2060 s->strstart++;
2061
2062 if (bflush) FLUSH_BLOCK(s, 0);
2063
2064 } else if (s->match_available) {
2065 /* If there was no match at the previous position, output a
2066 * single literal. If there was a match but the current match
2067 * is longer, truncate the previous match to a single literal.
2068 */
2069 Tracevv((stderr,"%c", s->window[s->strstart-1]));
2070 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
2071 if (bflush) {
2072 FLUSH_BLOCK_ONLY(s, 0);
2073 }
2074 s->strstart++;
2075 s->lookahead--;
2076 if (s->strm->avail_out == 0) return need_more;
2077 } else {
2078 /* There is no previous match to compare with, wait for
2079 * the next step to decide.
2080 */
2081 s->match_available = 1;
2082 s->strstart++;
2083 s->lookahead--;
2084 }
2085 }
2086 Assert (flush != Z_NO_FLUSH, "no flush?");
2087 if (s->match_available) {
2088 Tracevv((stderr,"%c", s->window[s->strstart-1]));
2089 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
2090 s->match_available = 0;
2091 }
2092 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
2093 if (flush == Z_FINISH) {
2094 FLUSH_BLOCK(s, 1);
2095 return finish_done;
2096 }
2097 if (s->sym_next)
2098 FLUSH_BLOCK(s, 0);
2099 return block_done;
2100}
#define FLUSH_BLOCK_ONLY(s, last)
Definition: deflate.c:1650
#define TOO_FAR
Definition: deflate.c:110
IPos prev_match
Definition: deflate.h:159
uInt prev_length
Definition: deflate.h:165
z_streamp strm
Definition: deflate.h:100
uInt max_lazy_match
Definition: deflate.h:176
int match_available
Definition: deflate.h:160
#define Z_FILTERED
Definition: zlib.h:196

◆ deflate_stored()

block_state deflate_stored ( deflate_state s,
int  flush 
)

Definition at line 1688 of file deflate.c.

1691{
1692 /* Smallest worthy block size when not flushing or finishing. By default
1693 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1694 * large input and output buffers, the stored block size will be larger.
1695 */
1696 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
1697
1698 /* Copy as many min_block or larger stored blocks directly to next_out as
1699 * possible. If flushing, copy the remaining available input to next_out as
1700 * stored blocks, if there is enough space.
1701 */
1702 unsigned len, left, have, last = 0;
1703 unsigned used = s->strm->avail_in;
1704 do {
1705 /* Set len to the maximum size block that we can copy directly with the
1706 * available input data and output space. Set left to how much of that
1707 * would be copied from what's left in the window.
1708 */
1709 len = MAX_STORED; /* maximum deflate stored block length */
1710 have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1711 if (s->strm->avail_out < have) /* need room for header */
1712 break;
1713 /* maximum stored block length that will fit in avail_out: */
1714 have = s->strm->avail_out - have;
1715 left = s->strstart - s->block_start; /* bytes left in window */
1716 if (len > (ulg)left + s->strm->avail_in)
1717 len = left + s->strm->avail_in; /* limit len to the input */
1718 if (len > have)
1719 len = have; /* limit len to the output */
1720
1721 /* If the stored block would be less than min_block in length, or if
1722 * unable to copy all of the available input when flushing, then try
1723 * copying to the window and the pending buffer instead. Also don't
1724 * write an empty block when flushing -- deflate() does that.
1725 */
1726 if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
1727 flush == Z_NO_FLUSH ||
1728 len != left + s->strm->avail_in))
1729 break;
1730
1731 /* Make a dummy stored block in pending to get the header bytes,
1732 * including any pending bits. This also updates the debugging counts.
1733 */
1734 last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1735 _tr_stored_block(s, (char *)0, 0L, last);
1736
1737 /* Replace the lengths in the dummy stored block with len. */
1738 s->pending_buf[s->pending - 4] = len;
1739 s->pending_buf[s->pending - 3] = len >> 8;
1740 s->pending_buf[s->pending - 2] = ~len;
1741 s->pending_buf[s->pending - 1] = ~len >> 8;
1742
1743 /* Write the stored block header bytes. */
1744 flush_pending(s->strm);
1745
1746#ifdef ZLIB_DEBUG
1747 /* Update debugging counts for the data about to be copied. */
1748 s->compressed_len += len << 3;
1749 s->bits_sent += len << 3;
1750#endif
1751
1752 /* Copy uncompressed bytes from the window to next_out. */
1753 if (left) {
1754 if (left > len)
1755 left = len;
1756 zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1757 s->strm->next_out += left;
1758 s->strm->avail_out -= left;
1759 s->strm->total_out += left;
1760 s->block_start += left;
1761 len -= left;
1762 }
1763
1764 /* Copy uncompressed bytes directly from next_in to next_out, updating
1765 * the check value.
1766 */
1767 if (len) {
1768 read_buf(s->strm, s->strm->next_out, len);
1769 s->strm->next_out += len;
1770 s->strm->avail_out -= len;
1771 s->strm->total_out += len;
1772 }
1773 } while (last == 0);
1774
1775 /* Update the sliding window with the last s->w_size bytes of the copied
1776 * data, or append all of the copied data to the existing window if less
1777 * than s->w_size bytes were copied. Also update the number of bytes to
1778 * insert in the hash tables, in the event that deflateParams() switches to
1779 * a non-zero compression level.
1780 */
1781 used -= s->strm->avail_in; /* number of input bytes directly copied */
1782 if (used) {
1783 /* If any input was used, then no unused input remains in the window,
1784 * therefore s->block_start == s->strstart.
1785 */
1786 if (used >= s->w_size) { /* supplant the previous history */
1787 s->matches = 2; /* clear hash */
1788 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1789 s->strstart = s->w_size;
1790 s->insert = s->strstart;
1791 }
1792 else {
1793 if (s->window_size - s->strstart <= used) {
1794 /* Slide the window down. */
1795 s->strstart -= s->w_size;
1796 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1797 if (s->matches < 2)
1798 s->matches++; /* add a pending slide_hash() */
1799 if (s->insert > s->strstart)
1800 s->insert = s->strstart;
1801 }
1802 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1803 s->strstart += used;
1804 s->insert += MIN(used, s->w_size - s->insert);
1805 }
1806 s->block_start = s->strstart;
1807 }
1808 if (s->high_water < s->strstart)
1809 s->high_water = s->strstart;
1810
1811 /* If the last block was written to next_out, then done. */
1812 if (last)
1813 return finish_done;
1814
1815 /* If flushing and all input has been consumed, then done. */
1816 if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
1817 s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
1818 return block_done;
1819
1820 /* Fill the window with any remaining input. */
1821 have = s->window_size - s->strstart;
1822 if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
1823 /* Slide the window down. */
1824 s->block_start -= s->w_size;
1825 s->strstart -= s->w_size;
1826 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1827 if (s->matches < 2)
1828 s->matches++; /* add a pending slide_hash() */
1829 have += s->w_size; /* more space now */
1830 if (s->insert > s->strstart)
1831 s->insert = s->strstart;
1832 }
1833 if (have > s->strm->avail_in)
1834 have = s->strm->avail_in;
1835 if (have) {
1836 read_buf(s->strm, s->window + s->strstart, have);
1837 s->strstart += have;
1838 s->insert += MIN(have, s->w_size - s->insert);
1839 }
1840 if (s->high_water < s->strstart)
1841 s->high_water = s->strstart;
1842
1843 /* There was not enough avail_out to write a complete worthy or flushed
1844 * stored block to next_out. Write a stored block to pending instead, if we
1845 * have enough input for a worthy block, or if flushing and there is enough
1846 * room for the remaining input as a stored block in the pending buffer.
1847 */
1848 have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1849 /* maximum stored block length that will fit in pending: */
1850 have = MIN(s->pending_buf_size - have, MAX_STORED);
1851 min_block = MIN(have, s->w_size);
1852 left = s->strstart - s->block_start;
1853 if (left >= min_block ||
1854 ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
1855 s->strm->avail_in == 0 && left <= have)) {
1856 len = MIN(left, have);
1857 last = flush == Z_FINISH && s->strm->avail_in == 0 &&
1858 len == left ? 1 : 0;
1859 _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
1860 s->block_start += len;
1861 flush_pending(s->strm);
1862 }
1863
1864 /* We've done all we can with the available input and output. */
1865 return last ? finish_started : need_more;
1866}
#define MIN(a, b)
Definition: deflate.c:1671
unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size)
Definition: deflate.c:1207
#define MAX_STORED
Definition: deflate.c:1668
ulg high_water
Definition: deflate.h:263
uInt matches
Definition: deflate.h:246

Referenced by deflate().

◆ deflateBound()

uLong ZEXPORT deflateBound ( z_streamp  strm,
uLong  sourceLen 
)

Definition at line 696 of file deflate.c.

699{
700 deflate_state *s;
701 uLong complen, wraplen;
702
703 /* conservative upper bound for compressed data */
704 complen = sourceLen +
705 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
706
707 /* if can't get parameters, return conservative bound plus zlib wrapper */
708 if (deflateStateCheck(strm))
709 return complen + 6;
710
711 /* compute wrapper length */
712 s = strm->state;
713 switch (s->wrap) {
714 case 0: /* raw deflate */
715 wraplen = 0;
716 break;
717 case 1: /* zlib wrapper */
718 wraplen = 6 + (s->strstart ? 4 : 0);
719 break;
720#ifdef GZIP
721 case 2: /* gzip wrapper */
722 wraplen = 18;
723 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
724 Bytef *str;
725 if (s->gzhead->extra != Z_NULL)
726 wraplen += 2 + s->gzhead->extra_len;
727 str = s->gzhead->name;
728 if (str != Z_NULL)
729 do {
730 wraplen++;
731 } while (*str++);
732 str = s->gzhead->comment;
733 if (str != Z_NULL)
734 do {
735 wraplen++;
736 } while (*str++);
737 if (s->gzhead->hcrc)
738 wraplen += 2;
739 }
740 break;
741#endif
742 default: /* for compiler happiness */
743 wraplen = 6;
744 }
745
746 /* if not default parameters, return conservative bound */
747 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
748 return complen + wraplen;
749
750 /* default settings: return tight bound for that case */
751 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
752 (sourceLen >> 25) + 13 - 6 + wraplen;
753}

◆ deflateCopy()

int ZEXPORT deflateCopy ( z_streamp  dest,
z_streamp  source 
)

Definition at line 1148 of file deflate.c.

1151{
1152#ifdef MAXSEG_64K
1153 return Z_STREAM_ERROR;
1154#else
1155 deflate_state *ds;
1156 deflate_state *ss;
1157
1158
1159 if (deflateStateCheck(source) || dest == Z_NULL) {
1160 return Z_STREAM_ERROR;
1161 }
1162
1163 ss = source->state;
1164
1165 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1166
1167 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1168 if (ds == Z_NULL) return Z_MEM_ERROR;
1169 dest->state = (struct internal_state FAR *) ds;
1170 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
1171 ds->strm = dest;
1172
1173 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1174 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1175 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
1176 ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
1177
1178 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1179 ds->pending_buf == Z_NULL) {
1180 deflateEnd (dest);
1181 return Z_MEM_ERROR;
1182 }
1183 /* following zmemcpy do not work for 16-bit MSDOS */
1184 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1185 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
1186 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
1187 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1188
1189 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1190 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1191
1192 ds->l_desc.dyn_tree = ds->dyn_ltree;
1193 ds->d_desc.dyn_tree = ds->dyn_dtree;
1194 ds->bl_desc.dyn_tree = ds->bl_tree;
1195
1196 return Z_OK;
1197#endif /* MAXSEG_64K */
1198}
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1122
Pos FAR Posf
Definition: deflate.h:92
#define Z_MEM_ERROR
Definition: zlib.h:183
#define ZALLOC(strm, items, size)
Definition: zutil.h:264
uch FAR uchf
Definition: zutil.h:39

◆ deflateEnd()

int ZEXPORT deflateEnd ( z_streamp  strm)

Definition at line 1122 of file deflate.c.

1124{
1125 int status;
1126
1127 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
1128
1129 status = strm->state->status;
1130
1131 /* Deallocate in reverse order of allocations: */
1132 TRY_FREE(strm, strm->state->pending_buf);
1133 TRY_FREE(strm, strm->state->head);
1134 TRY_FREE(strm, strm->state->prev);
1135 TRY_FREE(strm, strm->state->window);
1136
1137 ZFREE(strm, strm->state);
1138 strm->state = Z_NULL;
1139
1140 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1141}
#define Z_DATA_ERROR
Definition: zlib.h:182
#define ZFREE(strm, addr)
Definition: zutil.h:266
#define TRY_FREE(s, p)
Definition: zutil.h:267

Referenced by compress2(), deflateCopy(), deflateInit2_(), and gzclose_w().

◆ deflateGetDictionary()

int ZEXPORT deflateGetDictionary ( z_streamp  strm,
Bytef *  dictionary,
uInt *  dictLength 
)

Definition at line 488 of file deflate.c.

492{
493 deflate_state *s;
494 uInt len;
495
496 if (deflateStateCheck(strm))
497 return Z_STREAM_ERROR;
498 s = strm->state;
499 len = s->strstart + s->lookahead;
500 if (len > s->w_size)
501 len = s->w_size;
502 if (dictionary != Z_NULL && len)
503 zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
504 if (dictLength != Z_NULL)
505 *dictLength = len;
506 return Z_OK;
507}

◆ deflateInit2_()

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 242 of file deflate.c.

252{
253 deflate_state *s;
254 int wrap = 1;
255 static const char my_version[] = ZLIB_VERSION;
256
257 if (version == Z_NULL || version[0] != my_version[0] ||
258 stream_size != sizeof(z_stream)) {
259 return Z_VERSION_ERROR;
260 }
261 if (strm == Z_NULL) return Z_STREAM_ERROR;
262
263 strm->msg = Z_NULL;
264 if (strm->zalloc == (alloc_func)0) {
265#ifdef Z_SOLO
266 return Z_STREAM_ERROR;
267#else
268 strm->zalloc = zcalloc;
269 strm->opaque = (voidpf)0;
270#endif
271 }
272 if (strm->zfree == (free_func)0)
273#ifdef Z_SOLO
274 return Z_STREAM_ERROR;
275#else
276 strm->zfree = zcfree;
277#endif
278
279#ifdef FASTEST
280 if (level != 0) level = 1;
281#else
282 if (level == Z_DEFAULT_COMPRESSION) level = 6;
283#endif
284
285 if (windowBits < 0) { /* suppress zlib wrapper */
286 wrap = 0;
287 windowBits = -windowBits;
288 }
289#ifdef GZIP
290 else if (windowBits > 15) {
291 wrap = 2; /* write gzip wrapper instead */
292 windowBits -= 16;
293 }
294#endif
295 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
296 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
297 strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
298 return Z_STREAM_ERROR;
299 }
300 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
301 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
302 if (s == Z_NULL) return Z_MEM_ERROR;
303 strm->state = (struct internal_state FAR *)s;
304 s->strm = strm;
305 s->status = INIT_STATE; /* to pass state test in deflateReset() */
306
307 s->wrap = wrap;
308 s->gzhead = Z_NULL;
309 s->w_bits = (uInt)windowBits;
310 s->w_size = 1 << s->w_bits;
311 s->w_mask = s->w_size - 1;
312
313 s->hash_bits = (uInt)memLevel + 7;
314 s->hash_size = 1 << s->hash_bits;
315 s->hash_mask = s->hash_size - 1;
317
318 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
319 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
320 /* Avoid use of uninitialized value, see:
321 * https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11360
322 */
323 zmemzero(s->prev, s->w_size * sizeof(Pos));
324 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
325
326 s->high_water = 0; /* nothing written to s->window yet */
327
328 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
329
330 /* We overlay pending_buf and sym_buf. This works since the average size
331 * for length/distance pairs over any compressed block is assured to be 31
332 * bits or less.
333 *
334 * Analysis: The longest fixed codes are a length code of 8 bits plus 5
335 * extra bits, for lengths 131 to 257. The longest fixed distance codes are
336 * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
337 * possible fixed-codes length/distance pair is then 31 bits total.
338 *
339 * sym_buf starts one-fourth of the way into pending_buf. So there are
340 * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
341 * in sym_buf is three bytes -- two for the distance and one for the
342 * literal/length. As each symbol is consumed, the pointer to the next
343 * sym_buf value to read moves forward three bytes. From that symbol, up to
344 * 31 bits are written to pending_buf. The closest the written pending_buf
345 * bits gets to the next sym_buf symbol to read is just before the last
346 * code is written. At that time, 31*(n-2) bits have been written, just
347 * after 24*(n-2) bits have been consumed from sym_buf. sym_buf starts at
348 * 8*n bits into pending_buf. (Note that the symbol buffer fills when n-1
349 * symbols are written.) The closest the writing gets to what is unread is
350 * then n+14 bits. Here n is lit_bufsize, which is 16384 by default, and
351 * can range from 128 to 32768.
352 *
353 * Therefore, at a minimum, there are 142 bits of space between what is
354 * written and what is read in the overlain buffers, so the symbols cannot
355 * be overwritten by the compressed data. That space is actually 139 bits,
356 * due to the three-bit fixed-code block header.
357 *
358 * That covers the case where either Z_FIXED is specified, forcing fixed
359 * codes, or when the use of fixed codes is chosen, because that choice
360 * results in a smaller compressed block than dynamic codes. That latter
361 * condition then assures that the above analysis also covers all dynamic
362 * blocks. A dynamic-code block will only be chosen to be emitted if it has
363 * fewer bits than a fixed-code block would for the same set of symbols.
364 * Therefore its average symbol length is assured to be less than 31. So
365 * the compressed data for a dynamic block also cannot overwrite the
366 * symbols from which it is being constructed.
367 */
368
369 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
370 s->pending_buf_size = (ulg)s->lit_bufsize * 4;
371
372 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
373 s->pending_buf == Z_NULL) {
374 s->status = FINISH_STATE;
375 strm->msg = ERR_MSG(Z_MEM_ERROR);
377 return Z_MEM_ERROR;
378 }
379 s->sym_buf = s->pending_buf + s->lit_bufsize;
380 s->sym_end = (s->lit_bufsize - 1) * 3;
381 /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
382 * on 16 bit machines and because stored blocks are restricted to
383 * 64K-1 bytes.
384 */
385
386 s->level = level;
387 s->strategy = strategy;
388 s->method = (Byte)method;
389
390 return deflateReset(strm);
391}
int ZEXPORT deflateReset(z_streamp strm)
Definition: deflate.c:548
uInt lit_bufsize
Definition: deflate.h:221
uInt sym_end
Definition: deflate.h:242
uInt hash_bits
Definition: deflate.h:143
uInt hash_mask
Definition: deflate.h:144
Posf * prev
Definition: deflate.h:133
uInt hash_size
Definition: deflate.h:142
Posf * head
Definition: deflate.h:139
uInt hash_shift
Definition: deflate.h:146
uchf * sym_buf
Definition: deflate.h:219
#define ZLIB_VERSION
Definition: zlib.h:40
#define Z_VERSION_ERROR
Definition: zlib.h:185
#define Z_FIXED
Definition: zlib.h:199
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:193
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
Definition: zutil.c:314
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
Definition: zutil.c:304
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
Definition: zutil.c:172
#define ERR_MSG(err)
Definition: zutil.h:58

Referenced by deflateInit_().

◆ deflateInit_()

int ZEXPORT deflateInit_ ( z_streamp  strm,
int  level,
const char *  version,
int  stream_size 
)

Definition at line 230 of file deflate.c.

235{
236 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
237 Z_DEFAULT_STRATEGY, version, stream_size);
238 /* To do: ignore strm->next_in if we use it as window */
239}
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:242
#define DEF_MEM_LEVEL
Definition: gzguts.h:151
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:200

◆ deflateParams()

int ZEXPORT deflateParams ( z_streamp  strm,
int  level,
int  strategy 
)

Definition at line 612 of file deflate.c.

616{
617 deflate_state *s;
618 compress_func func;
619
620 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
621 s = strm->state;
622
623#ifdef FASTEST
624 if (level != 0) level = 1;
625#else
626 if (level == Z_DEFAULT_COMPRESSION) level = 6;
627#endif
628 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
629 return Z_STREAM_ERROR;
630 }
631 func = configuration_table[s->level].func;
632
633 if ((strategy != s->strategy || func != configuration_table[level].func) &&
634 s->last_flush != -2) {
635 /* Flush the last buffer: */
636 int err = deflate(strm, Z_BLOCK);
637 if (err == Z_STREAM_ERROR)
638 return err;
639 if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
640 return Z_BUF_ERROR;
641 }
642 if (s->level != level) {
643 if (s->level == 0 && s->matches != 0) {
644 if (s->matches == 1)
645 slide_hash(s);
646 else
647 CLEAR_HASH(s);
648 s->matches = 0;
649 }
650 s->level = level;
655 }
656 s->strategy = strategy;
657 return Z_OK;
658}
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:807
void slide_hash(deflate_state *s)
Definition: deflate.c:203
ush good_length
Definition: deflate.c:120
ush max_chain
Definition: deflate.c:123
compress_func func
Definition: deflate.c:124
ush nice_length
Definition: deflate.c:122
ush max_lazy
Definition: deflate.c:121
int nice_match
Definition: deflate.h:193
uInt good_match
Definition: deflate.h:190
uInt max_chain_length
Definition: deflate.h:170

Referenced by gzsetparams().

◆ deflatePending()

int ZEXPORT deflatePending ( z_streamp  strm,
unsigned *  pending,
int *  bits 
)

Definition at line 571 of file deflate.c.

575{
576 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
577 if (pending != Z_NULL)
578 *pending = strm->state->pending;
579 if (bits != Z_NULL)
580 *bits = strm->state->bi_valid;
581 return Z_OK;
582}

◆ deflatePrime()

int ZEXPORT deflatePrime ( z_streamp  strm,
int  bits,
int  value 
)

Definition at line 585 of file deflate.c.

589{
590 deflate_state *s;
591 int put;
592
593 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
594 s = strm->state;
595 if (bits < 0 || bits > 16 ||
596 s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
597 return Z_BUF_ERROR;
598 do {
599 put = Buf_size - s->bi_valid;
600 if (put > bits)
601 put = bits;
602 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
603 s->bi_valid += put;
605 value >>= put;
606 bits -= put;
607 } while (bits);
608 return Z_OK;
609}
#define Buf_size
Definition: deflate.h:50
Bytef * pending_out
Definition: deflate.h:104
void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s)
Definition: trees.c:886
unsigned short ush
Definition: zutil.h:40

◆ deflateReset()

int ZEXPORT deflateReset ( z_streamp  strm)

Definition at line 548 of file deflate.c.

550{
551 int ret;
552
553 ret = deflateResetKeep(strm);
554 if (ret == Z_OK)
555 lm_init(strm->state);
556 return ret;
557}
int ZEXPORT deflateResetKeep(z_streamp strm)
Definition: deflate.c:510
void lm_init(deflate_state *s)
Definition: deflate.c:1237

Referenced by deflateInit2_(), and gz_comp().

◆ deflateResetKeep()

int ZEXPORT deflateResetKeep ( z_streamp  strm)

Definition at line 510 of file deflate.c.

512{
513 deflate_state *s;
514
515 if (deflateStateCheck(strm)) {
516 return Z_STREAM_ERROR;
517 }
518
519 strm->total_in = strm->total_out = 0;
520 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
521 strm->data_type = Z_UNKNOWN;
522
523 s = (deflate_state *)strm->state;
524 s->pending = 0;
525 s->pending_out = s->pending_buf;
526
527 if (s->wrap < 0) {
528 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
529 }
530 s->status =
531#ifdef GZIP
532 s->wrap == 2 ? GZIP_STATE :
533#endif
535 strm->adler =
536#ifdef GZIP
537 s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
538#endif
539 adler32(0L, Z_NULL, 0);
540 s->last_flush = -2;
541
542 _tr_init(s);
543
544 return Z_OK;
545}
void ZLIB_INTERNAL _tr_init(deflate_state *s)
Definition: trees.c:378
#define Z_UNKNOWN
Definition: zlib.h:206

Referenced by deflateReset().

◆ deflateSetDictionary()

int ZEXPORT deflateSetDictionary ( z_streamp  strm,
const Bytef *  dictionary,
uInt  dictLength 
)

Definition at line 419 of file deflate.c.

423{
424 deflate_state *s;
425 uInt str, n;
426 int wrap;
427 unsigned avail;
428 z_const unsigned char *next;
429
430 if (deflateStateCheck(strm) || dictionary == Z_NULL)
431 return Z_STREAM_ERROR;
432 s = strm->state;
433 wrap = s->wrap;
434 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
435 return Z_STREAM_ERROR;
436
437 /* when using zlib wrappers, compute Adler-32 for provided dictionary */
438 if (wrap == 1)
439 strm->adler = adler32(strm->adler, dictionary, dictLength);
440 s->wrap = 0; /* avoid computing Adler-32 in read_buf */
441
442 /* if dictionary would fill window, just replace the history */
443 if (dictLength >= s->w_size) {
444 if (wrap == 0) { /* already empty otherwise */
445 CLEAR_HASH(s);
446 s->strstart = 0;
447 s->block_start = 0L;
448 s->insert = 0;
449 }
450 dictionary += dictLength - s->w_size; /* use the tail */
451 dictLength = s->w_size;
452 }
453
454 /* insert dictionary into window and hash */
455 avail = strm->avail_in;
456 next = strm->next_in;
457 strm->avail_in = dictLength;
458 strm->next_in = (z_const Bytef *)dictionary;
459 fill_window(s);
460 while (s->lookahead >= MIN_MATCH) {
461 str = s->strstart;
462 n = s->lookahead - (MIN_MATCH-1);
463 do {
464 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
465#ifndef FASTEST
466 s->prev[str & s->w_mask] = s->head[s->ins_h];
467#endif
468 s->head[s->ins_h] = (Pos)str;
469 str++;
470 } while (--n);
471 s->strstart = str;
472 s->lookahead = MIN_MATCH-1;
473 fill_window(s);
474 }
475 s->strstart += s->lookahead;
476 s->block_start = (long)s->strstart;
477 s->insert = s->lookahead;
478 s->lookahead = 0;
480 s->match_available = 0;
481 strm->next_in = next;
482 strm->avail_in = avail;
483 s->wrap = wrap;
484 return Z_OK;
485}

◆ deflateSetHeader()

int ZEXPORT deflateSetHeader ( z_streamp  strm,
gz_headerp  head 
)

Definition at line 560 of file deflate.c.

563{
564 if (deflateStateCheck(strm) || strm->state->wrap != 2)
565 return Z_STREAM_ERROR;
566 strm->state->gzhead = head;
567 return Z_OK;
568}

◆ deflateStateCheck()

int deflateStateCheck ( z_streamp  strm)

Definition at line 396 of file deflate.c.

398{
399 deflate_state *s;
400 if (strm == Z_NULL ||
401 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
402 return 1;
403 s = strm->state;
404 if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
405#ifdef GZIP
406 s->status != GZIP_STATE &&
407#endif
408 s->status != EXTRA_STATE &&
409 s->status != NAME_STATE &&
410 s->status != COMMENT_STATE &&
411 s->status != HCRC_STATE &&
412 s->status != BUSY_STATE &&
413 s->status != FINISH_STATE))
414 return 1;
415 return 0;
416}
#define GZIP
Definition: deflate.h:22

Referenced by deflate(), deflateBound(), deflateCopy(), deflateEnd(), deflateGetDictionary(), deflateParams(), deflatePending(), deflatePrime(), deflateResetKeep(), deflateSetDictionary(), deflateSetHeader(), and deflateTune().

◆ deflateTune()

int ZEXPORT deflateTune ( z_streamp  strm,
int  good_length,
int  max_lazy,
int  nice_length,
int  max_chain 
)

Definition at line 661 of file deflate.c.

667{
668 deflate_state *s;
669
670 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
671 s = strm->state;
672 s->good_match = (uInt)good_length;
673 s->max_lazy_match = (uInt)max_lazy;
674 s->nice_match = nice_length;
675 s->max_chain_length = (uInt)max_chain;
676 return Z_OK;
677}

◆ fill_window()

void fill_window ( deflate_state s)

Definition at line 1525 of file deflate.c.

1527{
1528 unsigned n;
1529 unsigned more; /* Amount of free space at the end of the window. */
1530 uInt wsize = s->w_size;
1531
1532 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1533
1534 do {
1535 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1536
1537 /* Deal with !@#$% 64K limit: */
1538 if (sizeof(int) <= 2) {
1539 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1540 more = wsize;
1541
1542 } else if (more == (unsigned)(-1)) {
1543 /* Very unlikely, but possible on 16 bit machine if
1544 * strstart == 0 && lookahead == 1 (input done a byte at time)
1545 */
1546 more--;
1547 }
1548 }
1549
1550 /* If the window is almost full and there is insufficient lookahead,
1551 * move the upper half to the lower one to make room in the upper half.
1552 */
1553 if (s->strstart >= wsize+MAX_DIST(s)) {
1554
1555 zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more);
1556 s->match_start -= wsize;
1557 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1558 s->block_start -= (long) wsize;
1559 if (s->insert > s->strstart)
1560 s->insert = s->strstart;
1561 slide_hash(s);
1562 more += wsize;
1563 }
1564 if (s->strm->avail_in == 0) break;
1565
1566 /* If there was no sliding:
1567 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1568 * more == window_size - lookahead - strstart
1569 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1570 * => more >= window_size - 2*WSIZE + 2
1571 * In the BIG_MEM or MMAP case (not yet supported),
1572 * window_size == input_size + MIN_LOOKAHEAD &&
1573 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1574 * Otherwise, window_size == 2*WSIZE so more >= 2.
1575 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1576 */
1577 Assert(more >= 2, "more < 2");
1578
1579 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1580 s->lookahead += n;
1581
1582 /* Initialize the hash value now that we have some input: */
1583 if (s->lookahead + s->insert >= MIN_MATCH) {
1584 uInt str = s->strstart - s->insert;
1585 s->ins_h = s->window[str];
1586 UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
1587#if MIN_MATCH != 3
1588 Call UPDATE_HASH() MIN_MATCH-3 more times
1589#endif
1590 while (s->insert) {
1591 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1592#ifndef FASTEST
1593 s->prev[str & s->w_mask] = s->head[s->ins_h];
1594#endif
1595 s->head[s->ins_h] = (Pos)str;
1596 str++;
1597 s->insert--;
1598 if (s->lookahead + s->insert < MIN_MATCH)
1599 break;
1600 }
1601 }
1602 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1603 * but this is not important since only literal bytes will be emitted.
1604 */
1605
1606 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1607
1608 /* If the WIN_INIT bytes after the end of the current data have never been
1609 * written, then zero those bytes in order to avoid memory check reports of
1610 * the use of uninitialized (or uninitialised as Julian writes) bytes by
1611 * the longest match routines. Update the high water mark for the next
1612 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
1613 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1614 */
1615 if (s->high_water < s->window_size) {
1616 ulg curr = s->strstart + (ulg)(s->lookahead);
1617 ulg init;
1618
1619 if (s->high_water < curr) {
1620 /* Previous high water mark below current data -- zero WIN_INIT
1621 * bytes or up to end of window, whichever is less.
1622 */
1623 init = s->window_size - curr;
1624 if (init > WIN_INIT)
1625 init = WIN_INIT;
1626 zmemzero(s->window + curr, (unsigned)init);
1627 s->high_water = curr + init;
1628 }
1629 else if (s->high_water < (ulg)curr + WIN_INIT) {
1630 /* High water mark at or above current data, but below current data
1631 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1632 * to end of window, whichever is less.
1633 */
1634 init = (ulg)curr + WIN_INIT - s->high_water;
1635 if (init > s->window_size - s->high_water)
1636 init = s->window_size - s->high_water;
1637 zmemzero(s->window + s->high_water, (unsigned)init);
1638 s->high_water += init;
1639 }
1640 }
1641
1643 "not enough room for search");
1644}
#define WIN_INIT
Definition: deflate.h:288

Referenced by deflate_fast(), deflate_huff(), deflate_rle(), deflate_slow(), and deflateSetDictionary().

◆ flush_pending()

void flush_pending ( z_streamp  strm)

Definition at line 774 of file deflate.c.

776{
777 unsigned len;
778 deflate_state *s = strm->state;
779
781 len = s->pending;
782 if (len > strm->avail_out) len = strm->avail_out;
783 if (len == 0) return;
784
785 zmemcpy(strm->next_out, s->pending_out, len);
786 strm->next_out += len;
787 s->pending_out += len;
788 strm->total_out += len;
789 strm->avail_out -= len;
790 s->pending -= len;
791 if (s->pending == 0) {
792 s->pending_out = s->pending_buf;
793 }
794}

Referenced by deflate(), and deflate_stored().

◆ lm_init()

void lm_init ( deflate_state s)

Definition at line 1237 of file deflate.c.

1239{
1240 s->window_size = (ulg)2L*s->w_size;
1241
1242 CLEAR_HASH(s);
1243
1244 /* Set the default configuration parameters:
1245 */
1250
1251 s->strstart = 0;
1252 s->block_start = 0L;
1253 s->lookahead = 0;
1254 s->insert = 0;
1256 s->match_available = 0;
1257 s->ins_h = 0;
1258#ifndef FASTEST
1259#ifdef ASMV
1260 match_init(); /* initialize the asm code */
1261#endif
1262#endif
1263}

Referenced by deflateReset().

◆ longest_match()

uInt longest_match ( deflate_state s,
IPos  cur_match 
)

Definition at line 1279 of file deflate.c.

1282{
1283 unsigned chain_length = s->max_chain_length;/* max hash chain length */
1284 register Bytef *scan = s->window + s->strstart; /* current string */
1285 register Bytef *match; /* matched string */
1286 register int len; /* length of current match */
1287 int best_len = (int)s->prev_length; /* best match length so far */
1288 int nice_match = s->nice_match; /* stop if match long enough */
1289 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1290 s->strstart - (IPos)MAX_DIST(s) : NIL;
1291 /* Stop when cur_match becomes <= limit. To simplify the code,
1292 * we prevent matches with the string of window index 0.
1293 */
1294 Posf *prev = s->prev;
1295 uInt wmask = s->w_mask;
1296
1297#ifdef UNALIGNED_OK
1298 /* Compare two bytes at a time. Note: this is not always beneficial.
1299 * Try with and without -DUNALIGNED_OK to check.
1300 */
1301 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1302 register ush scan_start = *(ushf*)scan;
1303 register ush scan_end = *(ushf*)(scan+best_len-1);
1304#else
1305 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1306 register Byte scan_end1 = scan[best_len-1];
1307 register Byte scan_end = scan[best_len];
1308#endif
1309
1310 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1311 * It is easy to get rid of this optimization if necessary.
1312 */
1313 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1314
1315 /* Do not waste too much time if we already have a good match: */
1316 if (s->prev_length >= s->good_match) {
1317 chain_length >>= 2;
1318 }
1319 /* Do not look for matches beyond the end of the input. This is necessary
1320 * to make deflate deterministic.
1321 */
1322 if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
1323
1324 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1325
1326 do {
1327 Assert(cur_match < s->strstart, "no future");
1328 match = s->window + cur_match;
1329
1330 /* Skip to next match if the match length cannot increase
1331 * or if the match length is less than 2. Note that the checks below
1332 * for insufficient lookahead only occur occasionally for performance
1333 * reasons. Therefore uninitialized memory will be accessed, and
1334 * conditional jumps will be made that depend on those values.
1335 * However the length of the match is limited to the lookahead, so
1336 * the output of deflate is not affected by the uninitialized values.
1337 */
1338#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1339 /* This code assumes sizeof(unsigned short) == 2. Do not use
1340 * UNALIGNED_OK if your compiler uses a different size.
1341 */
1342 if (*(ushf*)(match+best_len-1) != scan_end ||
1343 *(ushf*)match != scan_start) continue;
1344
1345 /* It is not necessary to compare scan[2] and match[2] since they are
1346 * always equal when the other bytes match, given that the hash keys
1347 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1348 * strstart+3, +5, ... up to strstart+257. We check for insufficient
1349 * lookahead only every 4th comparison; the 128th check will be made
1350 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1351 * necessary to put more guard bytes at the end of the window, or
1352 * to check more often for insufficient lookahead.
1353 */
1354 Assert(scan[2] == match[2], "scan[2]?");
1355 scan++, match++;
1356 do {
1357 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1358 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1359 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1360 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1361 scan < strend);
1362 /* The funny "do {}" generates better code on most compilers */
1363
1364 /* Here, scan <= window+strstart+257 */
1365 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1366 if (*scan == *match) scan++;
1367
1368 len = (MAX_MATCH - 1) - (int)(strend-scan);
1369 scan = strend - (MAX_MATCH-1);
1370
1371#else /* UNALIGNED_OK */
1372
1373 if (match[best_len] != scan_end ||
1374 match[best_len-1] != scan_end1 ||
1375 *match != *scan ||
1376 *++match != scan[1]) continue;
1377
1378 /* The check at best_len-1 can be removed because it will be made
1379 * again later. (This heuristic is not always a win.)
1380 * It is not necessary to compare scan[2] and match[2] since they
1381 * are always equal when the other bytes match, given that
1382 * the hash keys are equal and that HASH_BITS >= 8.
1383 */
1384 scan += 2, match++;
1385 Assert(*scan == *match, "match[2]?");
1386
1387 /* We check for insufficient lookahead only every 8th comparison;
1388 * the 256th check will be made at strstart+258.
1389 */
1390 do {
1391 } while (*++scan == *++match && *++scan == *++match &&
1392 *++scan == *++match && *++scan == *++match &&
1393 *++scan == *++match && *++scan == *++match &&
1394 *++scan == *++match && *++scan == *++match &&
1395 scan < strend);
1396
1397 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1398
1399 len = MAX_MATCH - (int)(strend - scan);
1400 scan = strend - MAX_MATCH;
1401
1402#endif /* UNALIGNED_OK */
1403
1404 if (len > best_len) {
1405 s->match_start = cur_match;
1406 best_len = len;
1407 if (len >= nice_match) break;
1408#ifdef UNALIGNED_OK
1409 scan_end = *(ushf*)(scan+best_len-1);
1410#else
1411 scan_end1 = scan[best_len-1];
1412 scan_end = scan[best_len];
1413#endif
1414 }
1415 } while ((cur_match = prev[cur_match & wmask]) > limit
1416 && --chain_length != 0);
1417
1418 if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1419 return s->lookahead;
1420}
ush FAR ushf
Definition: zutil.h:41

Referenced by deflate_fast(), and deflate_slow().

◆ OF() [1/5]

void slide_hash OF ( (deflate_state *s)  )

◆ OF() [2/5]

uInt longest_match OF ( (deflate_state *s, IPos cur_match)  )

◆ OF() [3/5]

void putShortMSB OF ( (deflate_state *s, uInt b)  )

◆ OF() [4/5]

int deflateStateCheck OF ( (z_streamp strm)  )

◆ OF() [5/5]

unsigned read_buf OF ( (z_streamp strm, Bytef *buf, unsigned size)  )

◆ putShortMSB()

void putShortMSB ( deflate_state s,
uInt  b 
)

Definition at line 760 of file deflate.c.

763{
764 put_byte(s, (Byte)(b >> 8));
765 put_byte(s, (Byte)(b & 0xff));
766}

Referenced by deflate().

◆ read_buf()

unsigned read_buf ( z_streamp  strm,
Bytef *  buf,
unsigned  size 
)

Definition at line 1207 of file deflate.c.

1211{
1212 unsigned len = strm->avail_in;
1213
1214 if (len > size) len = size;
1215 if (len == 0) return 0;
1216
1217 strm->avail_in -= len;
1218
1219 zmemcpy(buf, strm->next_in, len);
1220 if (strm->state->wrap == 1) {
1221 strm->adler = adler32(strm->adler, buf, len);
1222 }
1223#ifdef GZIP
1224 else if (strm->state->wrap == 2) {
1225 strm->adler = crc32(strm->adler, buf, len);
1226 }
1227#endif
1228 strm->next_in += len;
1229 strm->total_in += len;
1230
1231 return len;
1232}

Referenced by deflate_stored(), and fill_window().

◆ slide_hash()

void slide_hash ( deflate_state s)

Definition at line 203 of file deflate.c.

205{
206 unsigned n, m;
207 Posf *p;
208 uInt wsize = s->w_size;
209
210 n = s->hash_size;
211 p = &s->head[n];
212 do {
213 m = *--p;
214 *p = (Pos)(m >= wsize ? m - wsize : NIL);
215 } while (--n);
216 n = wsize;
217#ifndef FASTEST
218 p = &s->prev[n];
219 do {
220 m = *--p;
221 *p = (Pos)(m >= wsize ? m - wsize : NIL);
222 /* If n is not on any hash chain, prev[n] is garbage but
223 * its value will never be used.
224 */
225 } while (--n);
226#endif
227}

Referenced by deflateParams(), and fill_window().

Variable Documentation

◆ configuration_table

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}}
block_state deflate_fast(deflate_state *s, int flush)
Definition: deflate.c:1875
block_state deflate_slow(deflate_state *s, int flush)
Definition: deflate.c:1977

Definition at line 133 of file deflate.c.

Referenced by deflate(), deflateParams(), and lm_init().

◆ deflate_copyright

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

Definition at line 53 of file deflate.c.