Geant4 11.4.0
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)
#define UPDATE_HASH(s, h, c)
#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)

Typedefs

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

Enumerations

enum  block_state { need_more , block_done , finish_started , finish_done }

Functions

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)
local void slide_hash (deflate_state *s)
local unsigned read_buf (z_streamp strm, Bytef *buf, unsigned size)
local void fill_window (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)
local 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)
local void lm_init (deflate_state *s)
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)
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 uInt longest_match (deflate_state *s, IPos cur_match)

Variables

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

Macro Definition Documentation

◆ check_match

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

Definition at line 1586 of file deflate.c.

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

◆ 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:85

Definition at line 170 of file deflate.c.

170#define CLEAR_HASH(s) \
171 do { \
172 s->head[s->hash_size - 1] = NIL; \
173 zmemzero((Bytef *)s->head, \
174 (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
175 } while (0)

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

◆ 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:66
@ need_more
Definition deflate.c:64

Definition at line 1605 of file deflate.c.

1605#define FLUSH_BLOCK(s, last) { \
1606 FLUSH_BLOCK_ONLY(s, last); \
1607 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1608}

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

◆ 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:43

Definition at line 1593 of file deflate.c.

1593#define FLUSH_BLOCK_ONLY(s, last) { \
1594 _tr_flush_block(s, (s->block_start >= 0L ? \
1595 (charf *)&s->window[(unsigned)s->block_start] : \
1596 (charf *)Z_NULL), \
1597 (ulg)((long)s->strstart - s->block_start), \
1598 (last)); \
1599 s->block_start = s->strstart; \
1600 flush_pending(s->strm); \
1601 Tracev((stderr,"[FLUSH]")); \
1602}

Referenced by deflate_slow().

◆ 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)
ZEXTERN uLong ZEXPORT crc32(uLong crc, const Bytef *buf, uInt len)

Definition at line 942 of file deflate.c.

942#define HCRC_UPDATE(beg) \
943 do { \
944 if (s->gzhead->hcrc && s->pending > (beg)) \
945 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
946 s->pending - (beg)); \
947 } while (0)

Referenced by deflate().

◆ 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:141
ush Pos
Definition deflate.h:96
#define MIN_MATCH
Definition zutil.h:84

Definition at line 160 of file deflate.c.

160#define INSERT_STRING(s, str, match_head) \
161 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
162 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
163 s->head[s->ins_h] = (Pos)(str))

Referenced by deflate_fast(), and deflate_slow().

◆ MAX_STORED

#define MAX_STORED   65535

Definition at line 1611 of file deflate.c.

Referenced by deflate_stored().

◆ MIN

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

Definition at line 1614 of file deflate.c.

Referenced by deflate_stored().

◆ NIL

#define NIL   0

Definition at line 85 of file deflate.c.

Referenced by deflate_fast(), deflate_slow(), longest_match(), and slide_hash().

◆ RANK

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

Definition at line 133 of file deflate.c.

Referenced by deflate().

◆ TOO_FAR

#define TOO_FAR   4096

Definition at line 89 of file deflate.c.

Referenced by deflate_slow().

◆ UPDATE_HASH

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

Definition at line 141 of file deflate.c.

Referenced by deflate_fast(), deflateSetDictionary(), and fill_window().

Typedef Documentation

◆ compress_func

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

Definition at line 70 of file deflate.c.

◆ config

typedef struct config_s config

Enumeration Type Documentation

◆ block_state

Enumerator
need_more 
block_done 
finish_started 
finish_done 

Definition at line 63 of file deflate.c.

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

Function Documentation

◆ deflate()

int ZEXPORT deflate ( z_streamp strm,
int flush )

Definition at line 950 of file deflate.c.

950 {
951 int old_flush; /* value of flush param for previous deflate call */
952 deflate_state *s;
953
954 if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
955 return Z_STREAM_ERROR;
956 }
957 s = strm->state;
958
959 if (strm->next_out == Z_NULL ||
960 (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
961 (s->status == FINISH_STATE && flush != Z_FINISH)) {
963 }
964 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
965
966 old_flush = s->last_flush;
967 s->last_flush = flush;
968
969 /* Flush as much pending output as possible */
970 if (s->pending != 0) {
971 flush_pending(strm);
972 if (strm->avail_out == 0) {
973 /* Since avail_out is 0, deflate will be called again with
974 * more output space, but possibly with both pending and
975 * avail_in equal to zero. There won't be anything to do,
976 * but this is not an error situation so make sure we
977 * return OK instead of BUF_ERROR at next call of deflate:
978 */
979 s->last_flush = -1;
980 return Z_OK;
981 }
982
983 /* Make sure there is something to do and avoid duplicate consecutive
984 * flushes. For repeated and useless calls with Z_FINISH, we keep
985 * returning Z_STREAM_END instead of Z_BUF_ERROR.
986 */
987 } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
988 flush != Z_FINISH) {
989 ERR_RETURN(strm, Z_BUF_ERROR);
990 }
991
992 /* User must not provide more input after the first FINISH: */
993 if (s->status == FINISH_STATE && strm->avail_in != 0) {
994 ERR_RETURN(strm, Z_BUF_ERROR);
995 }
996
997 /* Write the header */
998 if (s->status == INIT_STATE && s->wrap == 0)
999 s->status = BUSY_STATE;
1000 if (s->status == INIT_STATE) {
1001 /* zlib header */
1002 uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8;
1003 uInt level_flags;
1004
1005 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
1006 level_flags = 0;
1007 else if (s->level < 6)
1008 level_flags = 1;
1009 else if (s->level == 6)
1010 level_flags = 2;
1011 else
1012 level_flags = 3;
1013 header |= (level_flags << 6);
1014 if (s->strstart != 0) header |= PRESET_DICT;
1015 header += 31 - (header % 31);
1016
1017 putShortMSB(s, header);
1018
1019 /* Save the adler32 of the preset dictionary: */
1020 if (s->strstart != 0) {
1021 putShortMSB(s, (uInt)(strm->adler >> 16));
1022 putShortMSB(s, (uInt)(strm->adler & 0xffff));
1023 }
1024 strm->adler = adler32(0L, Z_NULL, 0);
1025 s->status = BUSY_STATE;
1026
1027 /* Compression must start with an empty pending buffer */
1028 flush_pending(strm);
1029 if (s->pending != 0) {
1030 s->last_flush = -1;
1031 return Z_OK;
1032 }
1033 }
1034#ifdef GZIP
1035 if (s->status == GZIP_STATE) {
1036 /* gzip header */
1037 strm->adler = crc32(0L, Z_NULL, 0);
1038 put_byte(s, 31);
1039 put_byte(s, 139);
1040 put_byte(s, 8);
1041 if (s->gzhead == Z_NULL) {
1042 put_byte(s, 0);
1043 put_byte(s, 0);
1044 put_byte(s, 0);
1045 put_byte(s, 0);
1046 put_byte(s, 0);
1047 put_byte(s, s->level == 9 ? 2 :
1048 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
1049 4 : 0));
1050 put_byte(s, OS_CODE);
1051 s->status = BUSY_STATE;
1052
1053 /* Compression must start with an empty pending buffer */
1054 flush_pending(strm);
1055 if (s->pending != 0) {
1056 s->last_flush = -1;
1057 return Z_OK;
1058 }
1059 }
1060 else {
1061 put_byte(s, (s->gzhead->text ? 1 : 0) +
1062 (s->gzhead->hcrc ? 2 : 0) +
1063 (s->gzhead->extra == Z_NULL ? 0 : 4) +
1064 (s->gzhead->name == Z_NULL ? 0 : 8) +
1065 (s->gzhead->comment == Z_NULL ? 0 : 16)
1066 );
1067 put_byte(s, (Byte)(s->gzhead->time & 0xff));
1068 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
1069 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
1070 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
1071 put_byte(s, s->level == 9 ? 2 :
1072 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
1073 4 : 0));
1074 put_byte(s, s->gzhead->os & 0xff);
1075 if (s->gzhead->extra != Z_NULL) {
1076 put_byte(s, s->gzhead->extra_len & 0xff);
1077 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
1078 }
1079 if (s->gzhead->hcrc)
1080 strm->adler = crc32(strm->adler, s->pending_buf,
1081 s->pending);
1082 s->gzindex = 0;
1083 s->status = EXTRA_STATE;
1084 }
1085 }
1086 if (s->status == EXTRA_STATE) {
1087 if (s->gzhead->extra != Z_NULL) {
1088 ulg beg = s->pending; /* start of bytes to update crc */
1089 uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
1090 while (s->pending + left > s->pending_buf_size) {
1091 uInt copy = s->pending_buf_size - s->pending;
1092 zmemcpy(s->pending_buf + s->pending,
1093 s->gzhead->extra + s->gzindex, copy);
1094 s->pending = s->pending_buf_size;
1095 HCRC_UPDATE(beg);
1096 s->gzindex += copy;
1097 flush_pending(strm);
1098 if (s->pending != 0) {
1099 s->last_flush = -1;
1100 return Z_OK;
1101 }
1102 beg = 0;
1103 left -= copy;
1104 }
1105 zmemcpy(s->pending_buf + s->pending,
1106 s->gzhead->extra + s->gzindex, left);
1107 s->pending += left;
1108 HCRC_UPDATE(beg);
1109 s->gzindex = 0;
1110 }
1111 s->status = NAME_STATE;
1112 }
1113 if (s->status == NAME_STATE) {
1114 if (s->gzhead->name != Z_NULL) {
1115 ulg beg = s->pending; /* start of bytes to update crc */
1116 int val;
1117 do {
1118 if (s->pending == s->pending_buf_size) {
1119 HCRC_UPDATE(beg);
1120 flush_pending(strm);
1121 if (s->pending != 0) {
1122 s->last_flush = -1;
1123 return Z_OK;
1124 }
1125 beg = 0;
1126 }
1127 val = s->gzhead->name[s->gzindex++];
1128 put_byte(s, val);
1129 } while (val != 0);
1130 HCRC_UPDATE(beg);
1131 s->gzindex = 0;
1132 }
1133 s->status = COMMENT_STATE;
1134 }
1135 if (s->status == COMMENT_STATE) {
1136 if (s->gzhead->comment != Z_NULL) {
1137 ulg beg = s->pending; /* start of bytes to update crc */
1138 int val;
1139 do {
1140 if (s->pending == s->pending_buf_size) {
1141 HCRC_UPDATE(beg);
1142 flush_pending(strm);
1143 if (s->pending != 0) {
1144 s->last_flush = -1;
1145 return Z_OK;
1146 }
1147 beg = 0;
1148 }
1149 val = s->gzhead->comment[s->gzindex++];
1150 put_byte(s, val);
1151 } while (val != 0);
1152 HCRC_UPDATE(beg);
1153 }
1154 s->status = HCRC_STATE;
1155 }
1156 if (s->status == HCRC_STATE) {
1157 if (s->gzhead->hcrc) {
1158 if (s->pending + 2 > s->pending_buf_size) {
1159 flush_pending(strm);
1160 if (s->pending != 0) {
1161 s->last_flush = -1;
1162 return Z_OK;
1163 }
1164 }
1165 put_byte(s, (Byte)(strm->adler & 0xff));
1166 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1167 strm->adler = crc32(0L, Z_NULL, 0);
1168 }
1169 s->status = BUSY_STATE;
1170
1171 /* Compression must start with an empty pending buffer */
1172 flush_pending(strm);
1173 if (s->pending != 0) {
1174 s->last_flush = -1;
1175 return Z_OK;
1176 }
1177 }
1178#endif
1179
1180 /* Start a new block or continue the current one.
1181 */
1182 if (strm->avail_in != 0 || s->lookahead != 0 ||
1183 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
1184 block_state bstate;
1185
1186 bstate = s->level == 0 ? deflate_stored(s, flush) :
1187 s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
1188 s->strategy == Z_RLE ? deflate_rle(s, flush) :
1189 (*(configuration_table[s->level].func))(s, flush);
1190
1191 if (bstate == finish_started || bstate == finish_done) {
1192 s->status = FINISH_STATE;
1193 }
1194 if (bstate == need_more || bstate == finish_started) {
1195 if (strm->avail_out == 0) {
1196 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
1197 }
1198 return Z_OK;
1199 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
1200 * of deflate should use the same flush parameter to make sure
1201 * that the flush is complete. So we don't have to output an
1202 * empty block here, this will be done at next call. This also
1203 * ensures that for a very small output buffer, we emit at most
1204 * one empty block.
1205 */
1206 }
1207 if (bstate == block_done) {
1208 if (flush == Z_PARTIAL_FLUSH) {
1209 _tr_align(s);
1210 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
1211 _tr_stored_block(s, (char*)0, 0L, 0);
1212 /* For a full flush, this empty block will be recognized
1213 * as a special marker by inflate_sync().
1214 */
1215 if (flush == Z_FULL_FLUSH) {
1216 CLEAR_HASH(s); /* forget history */
1217 if (s->lookahead == 0) {
1218 s->strstart = 0;
1219 s->block_start = 0L;
1220 s->insert = 0;
1221 }
1222 }
1223 }
1224 flush_pending(strm);
1225 if (strm->avail_out == 0) {
1226 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
1227 return Z_OK;
1228 }
1229 }
1230 }
1231
1232 if (flush != Z_FINISH) return Z_OK;
1233 if (s->wrap <= 0) return Z_STREAM_END;
1234
1235 /* Write the trailer */
1236#ifdef GZIP
1237 if (s->wrap == 2) {
1238 put_byte(s, (Byte)(strm->adler & 0xff));
1239 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
1240 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
1241 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
1242 put_byte(s, (Byte)(strm->total_in & 0xff));
1243 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
1244 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
1245 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
1246 }
1247 else
1248#endif
1249 {
1250 putShortMSB(s, (uInt)(strm->adler >> 16));
1251 putShortMSB(s, (uInt)(strm->adler & 0xffff));
1252 }
1253 flush_pending(strm);
1254 /* If avail_out is zero, the application will call deflate again
1255 * to flush the rest.
1256 */
1257 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
1258 return s->pending != 0 ? Z_OK : Z_STREAM_END;
1259}
#define HCRC_UPDATE(beg)
Definition deflate.c:942
local block_state deflate_huff(deflate_state *s, int flush)
Definition deflate.c:2113
local block_state deflate_stored(deflate_state *s, int flush)
Definition deflate.c:1631
local void putShortMSB(deflate_state *s, uInt b)
Definition deflate.c:908
local int deflateStateCheck(z_streamp strm)
Definition deflate.c:533
local const config configuration_table[10]
Definition deflate.c:112
#define RANK(f)
Definition deflate.c:133
local void flush_pending(z_streamp strm)
Definition deflate.c:919
local block_state deflate_rle(deflate_state *s, int flush)
Definition deflate.c:2042
#define CLEAR_HASH(s)
Definition deflate.c:170
#define FINISH_STATE
Definition deflate.h:67
#define COMMENT_STATE
Definition deflate.h:64
#define HCRC_STATE
Definition deflate.h:65
#define GZIP_STATE
Definition deflate.h:60
#define BUSY_STATE
Definition deflate.h:66
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
Definition trees.c:858
#define put_byte(s, c)
Definition deflate.h:287
#define INIT_STATE
Definition deflate.h:58
void ZLIB_INTERNAL _tr_align(deflate_state *s)
Definition trees.c:886
#define NAME_STATE
Definition deflate.h:63
struct internal_state deflate_state
#define EXTRA_STATE
Definition deflate.h:62
void copy(G4double dst[], const G4double src[], std::size_t size=G4FieldTrack::ncompSVEC)
#define Z_HUFFMAN_ONLY
Definition zlib.h:197
#define Z_DEFLATED
Definition zlib.h:209
ZEXTERN uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition adler32.c:128
#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
#define ERR_RETURN(strm, err)
Definition zutil.h:61
#define PRESET_DICT
Definition zutil.h:88
#define OS_CODE
Definition zutil.h:177
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition zutil.c:145

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

◆ deflate_fast()

local block_state deflate_fast ( deflate_state * s,
int flush )

Definition at line 1815 of file deflate.c.

1815 {
1816 IPos hash_head; /* head of the hash chain */
1817 int bflush; /* set if current block must be flushed */
1818
1819 for (;;) {
1820 /* Make sure that we always have enough lookahead, except
1821 * at the end of the input file. We need MAX_MATCH bytes
1822 * for the next match, plus MIN_MATCH bytes to insert the
1823 * string following the next match.
1824 */
1825 if (s->lookahead < MIN_LOOKAHEAD) {
1826 fill_window(s);
1827 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1828 return need_more;
1829 }
1830 if (s->lookahead == 0) break; /* flush the current block */
1831 }
1832
1833 /* Insert the string window[strstart .. strstart + 2] in the
1834 * dictionary, and set hash_head to the head of the hash chain:
1835 */
1836 hash_head = NIL;
1837 if (s->lookahead >= MIN_MATCH) {
1838 INSERT_STRING(s, s->strstart, hash_head);
1839 }
1840
1841 /* Find the longest match, discarding those <= prev_length.
1842 * At this point we have always match_length < MIN_MATCH
1843 */
1844 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1845 /* To simplify the code, we prevent matches with the string
1846 * of window index 0 (in particular we have to avoid a match
1847 * of the string with itself at the start of the input file).
1848 */
1849 s->match_length = longest_match (s, hash_head);
1850 /* longest_match() sets match_start */
1851 }
1852 if (s->match_length >= MIN_MATCH) {
1853 check_match(s, s->strstart, s->match_start, s->match_length);
1854
1855 _tr_tally_dist(s, s->strstart - s->match_start,
1856 s->match_length - MIN_MATCH, bflush);
1857
1858 s->lookahead -= s->match_length;
1859
1860 /* Insert new strings in the hash table only if the match length
1861 * is not too large. This saves time but degrades compression.
1862 */
1863#ifndef FASTEST
1864 if (s->match_length <= s->max_insert_length &&
1865 s->lookahead >= MIN_MATCH) {
1866 s->match_length--; /* string at strstart already in table */
1867 do {
1868 s->strstart++;
1869 INSERT_STRING(s, s->strstart, hash_head);
1870 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1871 * always MIN_MATCH bytes ahead.
1872 */
1873 } while (--s->match_length != 0);
1874 s->strstart++;
1875 } else
1876#endif
1877 {
1878 s->strstart += s->match_length;
1879 s->match_length = 0;
1880 s->ins_h = s->window[s->strstart];
1881 UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]);
1882#if MIN_MATCH != 3
1883 Call UPDATE_HASH() MIN_MATCH-3 more times
1884#endif
1885 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1886 * matter since it will be recomputed at next deflate call.
1887 */
1888 }
1889 } else {
1890 /* No match, output a literal byte */
1891 Tracevv((stderr,"%c", s->window[s->strstart]));
1892 _tr_tally_lit(s, s->window[s->strstart], bflush);
1893 s->lookahead--;
1894 s->strstart++;
1895 }
1896 if (bflush) FLUSH_BLOCK(s, 0);
1897 }
1898 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
1899 if (flush == Z_FINISH) {
1900 FLUSH_BLOCK(s, 1);
1901 return finish_done;
1902 }
1903 if (s->sym_next)
1904 FLUSH_BLOCK(s, 0);
1905 return block_done;
1906}
#define check_match(s, start, match, length)
Definition deflate.c:1586
#define INSERT_STRING(s, str, match_head)
Definition deflate.c:160
local void fill_window(deflate_state *s)
Definition deflate.c:251
local uInt longest_match(deflate_state *s, IPos cur_match)
Definition deflate.c:1352
#define FLUSH_BLOCK(s, last)
Definition deflate.c:1605
#define MAX_DIST(s)
Definition deflate.h:295
#define _tr_tally_dist(s, distance, length, flush)
Definition deflate.h:359
#define MIN_LOOKAHEAD
Definition deflate.h:290
unsigned IPos
Definition deflate.h:98
#define _tr_tally_lit(s, c, flush)
Definition deflate.h:351
#define Tracevv(x)
Definition zutil.h:230

◆ deflate_huff()

local block_state deflate_huff ( deflate_state * s,
int flush )

Definition at line 2113 of file deflate.c.

2113 {
2114 int bflush; /* set if current block must be flushed */
2115
2116 for (;;) {
2117 /* Make sure that we have a literal to write. */
2118 if (s->lookahead == 0) {
2119 fill_window(s);
2120 if (s->lookahead == 0) {
2121 if (flush == Z_NO_FLUSH)
2122 return need_more;
2123 break; /* flush the current block */
2124 }
2125 }
2126
2127 /* Output a literal byte */
2128 s->match_length = 0;
2129 Tracevv((stderr,"%c", s->window[s->strstart]));
2130 _tr_tally_lit(s, s->window[s->strstart], bflush);
2131 s->lookahead--;
2132 s->strstart++;
2133 if (bflush) FLUSH_BLOCK(s, 0);
2134 }
2135 s->insert = 0;
2136 if (flush == Z_FINISH) {
2137 FLUSH_BLOCK(s, 1);
2138 return finish_done;
2139 }
2140 if (s->sym_next)
2141 FLUSH_BLOCK(s, 0);
2142 return block_done;
2143}

Referenced by deflate().

◆ deflate_rle()

local block_state deflate_rle ( deflate_state * s,
int flush )

Definition at line 2042 of file deflate.c.

2042 {
2043 int bflush; /* set if current block must be flushed */
2044 uInt prev; /* byte at distance one to match */
2045 Bytef *scan, *strend; /* scan goes up to strend for length of run */
2046
2047 for (;;) {
2048 /* Make sure that we always have enough lookahead, except
2049 * at the end of the input file. We need MAX_MATCH bytes
2050 * for the longest run, plus one for the unrolled loop.
2051 */
2052 if (s->lookahead <= MAX_MATCH) {
2053 fill_window(s);
2054 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
2055 return need_more;
2056 }
2057 if (s->lookahead == 0) break; /* flush the current block */
2058 }
2059
2060 /* See how many times the previous byte repeats */
2061 s->match_length = 0;
2062 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
2063 scan = s->window + s->strstart - 1;
2064 prev = *scan;
2065 if (prev == *++scan && prev == *++scan && prev == *++scan) {
2066 strend = s->window + s->strstart + MAX_MATCH;
2067 do {
2068 } while (prev == *++scan && prev == *++scan &&
2069 prev == *++scan && prev == *++scan &&
2070 prev == *++scan && prev == *++scan &&
2071 prev == *++scan && prev == *++scan &&
2072 scan < strend);
2073 s->match_length = MAX_MATCH - (uInt)(strend - scan);
2074 if (s->match_length > s->lookahead)
2075 s->match_length = s->lookahead;
2076 }
2077 Assert(scan <= s->window + (uInt)(s->window_size - 1),
2078 "wild scan");
2079 }
2080
2081 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2082 if (s->match_length >= MIN_MATCH) {
2083 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2084
2085 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
2086
2087 s->lookahead -= s->match_length;
2088 s->strstart += s->match_length;
2089 s->match_length = 0;
2090 } else {
2091 /* No match, output a literal byte */
2092 Tracevv((stderr,"%c", s->window[s->strstart]));
2093 _tr_tally_lit(s, s->window[s->strstart], bflush);
2094 s->lookahead--;
2095 s->strstart++;
2096 }
2097 if (bflush) FLUSH_BLOCK(s, 0);
2098 }
2099 s->insert = 0;
2100 if (flush == Z_FINISH) {
2101 FLUSH_BLOCK(s, 1);
2102 return finish_done;
2103 }
2104 if (s->sym_next)
2105 FLUSH_BLOCK(s, 0);
2106 return block_done;
2107}
#define Assert(cond, msg)
Definition zutil.h:227
#define MAX_MATCH
Definition zutil.h:85

Referenced by deflate().

◆ deflate_slow()

local block_state deflate_slow ( deflate_state * s,
int flush )

Definition at line 1914 of file deflate.c.

1914 {
1915 IPos hash_head; /* head of hash chain */
1916 int bflush; /* set if current block must be flushed */
1917
1918 /* Process the input block. */
1919 for (;;) {
1920 /* Make sure that we always have enough lookahead, except
1921 * at the end of the input file. We need MAX_MATCH bytes
1922 * for the next match, plus MIN_MATCH bytes to insert the
1923 * string following the next match.
1924 */
1925 if (s->lookahead < MIN_LOOKAHEAD) {
1926 fill_window(s);
1927 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1928 return need_more;
1929 }
1930 if (s->lookahead == 0) break; /* flush the current block */
1931 }
1932
1933 /* Insert the string window[strstart .. strstart + 2] in the
1934 * dictionary, and set hash_head to the head of the hash chain:
1935 */
1936 hash_head = NIL;
1937 if (s->lookahead >= MIN_MATCH) {
1938 INSERT_STRING(s, s->strstart, hash_head);
1939 }
1940
1941 /* Find the longest match, discarding those <= prev_length.
1942 */
1943 s->prev_length = s->match_length, s->prev_match = s->match_start;
1944 s->match_length = MIN_MATCH-1;
1945
1946 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1947 s->strstart - hash_head <= MAX_DIST(s)) {
1948 /* To simplify the code, we prevent matches with the string
1949 * of window index 0 (in particular we have to avoid a match
1950 * of the string with itself at the start of the input file).
1951 */
1952 s->match_length = longest_match (s, hash_head);
1953 /* longest_match() sets match_start */
1954
1955 if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1956#if TOO_FAR <= 32767
1957 || (s->match_length == MIN_MATCH &&
1958 s->strstart - s->match_start > TOO_FAR)
1959#endif
1960 )) {
1961
1962 /* If prev_match is also MIN_MATCH, match_start is garbage
1963 * but we will ignore the current match anyway.
1964 */
1965 s->match_length = MIN_MATCH-1;
1966 }
1967 }
1968 /* If there was a match at the previous step and the current
1969 * match is not better, output the previous match:
1970 */
1971 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1972 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1973 /* Do not insert strings in hash table beyond this. */
1974
1975 check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
1976
1977 _tr_tally_dist(s, s->strstart - 1 - s->prev_match,
1978 s->prev_length - MIN_MATCH, bflush);
1979
1980 /* Insert in hash table all strings up to the end of the match.
1981 * strstart - 1 and strstart are already inserted. If there is not
1982 * enough lookahead, the last two strings are not inserted in
1983 * the hash table.
1984 */
1985 s->lookahead -= s->prev_length - 1;
1986 s->prev_length -= 2;
1987 do {
1988 if (++s->strstart <= max_insert) {
1989 INSERT_STRING(s, s->strstart, hash_head);
1990 }
1991 } while (--s->prev_length != 0);
1992 s->match_available = 0;
1993 s->match_length = MIN_MATCH-1;
1994 s->strstart++;
1995
1996 if (bflush) FLUSH_BLOCK(s, 0);
1997
1998 } else if (s->match_available) {
1999 /* If there was no match at the previous position, output a
2000 * single literal. If there was a match but the current match
2001 * is longer, truncate the previous match to a single literal.
2002 */
2003 Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2004 _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2005 if (bflush) {
2006 FLUSH_BLOCK_ONLY(s, 0);
2007 }
2008 s->strstart++;
2009 s->lookahead--;
2010 if (s->strm->avail_out == 0) return need_more;
2011 } else {
2012 /* There is no previous match to compare with, wait for
2013 * the next step to decide.
2014 */
2015 s->match_available = 1;
2016 s->strstart++;
2017 s->lookahead--;
2018 }
2019 }
2020 Assert (flush != Z_NO_FLUSH, "no flush?");
2021 if (s->match_available) {
2022 Tracevv((stderr,"%c", s->window[s->strstart - 1]));
2023 _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
2024 s->match_available = 0;
2025 }
2026 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
2027 if (flush == Z_FINISH) {
2028 FLUSH_BLOCK(s, 1);
2029 return finish_done;
2030 }
2031 if (s->sym_next)
2032 FLUSH_BLOCK(s, 0);
2033 return block_done;
2034}
#define FLUSH_BLOCK_ONLY(s, last)
Definition deflate.c:1593
#define TOO_FAR
Definition deflate.c:89
#define Z_FILTERED
Definition zlib.h:196

◆ deflate_stored()

local block_state deflate_stored ( deflate_state * s,
int flush )

Definition at line 1631 of file deflate.c.

1631 {
1632 /* Smallest worthy block size when not flushing or finishing. By default
1633 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1634 * large input and output buffers, the stored block size will be larger.
1635 */
1636 unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
1637
1638 /* Copy as many min_block or larger stored blocks directly to next_out as
1639 * possible. If flushing, copy the remaining available input to next_out as
1640 * stored blocks, if there is enough space.
1641 */
1642 unsigned len, left, have, last = 0;
1643 unsigned used = s->strm->avail_in;
1644 do {
1645 /* Set len to the maximum size block that we can copy directly with the
1646 * available input data and output space. Set left to how much of that
1647 * would be copied from what's left in the window.
1648 */
1649 len = MAX_STORED; /* maximum deflate stored block length */
1650 have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1651 if (s->strm->avail_out < have) /* need room for header */
1652 break;
1653 /* maximum stored block length that will fit in avail_out: */
1654 have = s->strm->avail_out - have;
1655 left = s->strstart - s->block_start; /* bytes left in window */
1656 if (len > (ulg)left + s->strm->avail_in)
1657 len = left + s->strm->avail_in; /* limit len to the input */
1658 if (len > have)
1659 len = have; /* limit len to the output */
1660
1661 /* If the stored block would be less than min_block in length, or if
1662 * unable to copy all of the available input when flushing, then try
1663 * copying to the window and the pending buffer instead. Also don't
1664 * write an empty block when flushing -- deflate() does that.
1665 */
1666 if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
1667 flush == Z_NO_FLUSH ||
1668 len != left + s->strm->avail_in))
1669 break;
1670
1671 /* Make a dummy stored block in pending to get the header bytes,
1672 * including any pending bits. This also updates the debugging counts.
1673 */
1674 last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1675 _tr_stored_block(s, (char *)0, 0L, last);
1676
1677 /* Replace the lengths in the dummy stored block with len. */
1678 s->pending_buf[s->pending - 4] = len;
1679 s->pending_buf[s->pending - 3] = len >> 8;
1680 s->pending_buf[s->pending - 2] = ~len;
1681 s->pending_buf[s->pending - 1] = ~len >> 8;
1682
1683 /* Write the stored block header bytes. */
1684 flush_pending(s->strm);
1685
1686#ifdef ZLIB_DEBUG
1687 /* Update debugging counts for the data about to be copied. */
1688 s->compressed_len += len << 3;
1689 s->bits_sent += len << 3;
1690#endif
1691
1692 /* Copy uncompressed bytes from the window to next_out. */
1693 if (left) {
1694 if (left > len)
1695 left = len;
1696 zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1697 s->strm->next_out += left;
1698 s->strm->avail_out -= left;
1699 s->strm->total_out += left;
1700 s->block_start += left;
1701 len -= left;
1702 }
1703
1704 /* Copy uncompressed bytes directly from next_in to next_out, updating
1705 * the check value.
1706 */
1707 if (len) {
1708 read_buf(s->strm, s->strm->next_out, len);
1709 s->strm->next_out += len;
1710 s->strm->avail_out -= len;
1711 s->strm->total_out += len;
1712 }
1713 } while (last == 0);
1714
1715 /* Update the sliding window with the last s->w_size bytes of the copied
1716 * data, or append all of the copied data to the existing window if less
1717 * than s->w_size bytes were copied. Also update the number of bytes to
1718 * insert in the hash tables, in the event that deflateParams() switches to
1719 * a non-zero compression level.
1720 */
1721 used -= s->strm->avail_in; /* number of input bytes directly copied */
1722 if (used) {
1723 /* If any input was used, then no unused input remains in the window,
1724 * therefore s->block_start == s->strstart.
1725 */
1726 if (used >= s->w_size) { /* supplant the previous history */
1727 s->matches = 2; /* clear hash */
1728 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1729 s->strstart = s->w_size;
1730 s->insert = s->strstart;
1731 }
1732 else {
1733 if (s->window_size - s->strstart <= used) {
1734 /* Slide the window down. */
1735 s->strstart -= s->w_size;
1736 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1737 if (s->matches < 2)
1738 s->matches++; /* add a pending slide_hash() */
1739 if (s->insert > s->strstart)
1740 s->insert = s->strstart;
1741 }
1742 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1743 s->strstart += used;
1744 s->insert += MIN(used, s->w_size - s->insert);
1745 }
1746 s->block_start = s->strstart;
1747 }
1748 if (s->high_water < s->strstart)
1749 s->high_water = s->strstart;
1750
1751 /* If the last block was written to next_out, then done. */
1752 if (last)
1753 return finish_done;
1754
1755 /* If flushing and all input has been consumed, then done. */
1756 if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
1757 s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
1758 return block_done;
1759
1760 /* Fill the window with any remaining input. */
1761 have = s->window_size - s->strstart;
1762 if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
1763 /* Slide the window down. */
1764 s->block_start -= s->w_size;
1765 s->strstart -= s->w_size;
1766 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1767 if (s->matches < 2)
1768 s->matches++; /* add a pending slide_hash() */
1769 have += s->w_size; /* more space now */
1770 if (s->insert > s->strstart)
1771 s->insert = s->strstart;
1772 }
1773 if (have > s->strm->avail_in)
1774 have = s->strm->avail_in;
1775 if (have) {
1776 read_buf(s->strm, s->window + s->strstart, have);
1777 s->strstart += have;
1778 s->insert += MIN(have, s->w_size - s->insert);
1779 }
1780 if (s->high_water < s->strstart)
1781 s->high_water = s->strstart;
1782
1783 /* There was not enough avail_out to write a complete worthy or flushed
1784 * stored block to next_out. Write a stored block to pending instead, if we
1785 * have enough input for a worthy block, or if flushing and there is enough
1786 * room for the remaining input as a stored block in the pending buffer.
1787 */
1788 have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1789 /* maximum stored block length that will fit in pending: */
1790 have = MIN(s->pending_buf_size - have, MAX_STORED);
1791 min_block = MIN(have, s->w_size);
1792 left = s->strstart - s->block_start;
1793 if (left >= min_block ||
1794 ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
1795 s->strm->avail_in == 0 && left <= have)) {
1796 len = MIN(left, have);
1797 last = flush == Z_FINISH && s->strm->avail_in == 0 &&
1798 len == left ? 1 : 0;
1799 _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
1800 s->block_start += len;
1801 flush_pending(s->strm);
1802 }
1803
1804 /* We've done all we can with the available input and output. */
1805 return last ? finish_started : need_more;
1806}
#define MIN(a, b)
Definition deflate.c:1614
local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size)
Definition deflate.c:218
#define MAX_STORED
Definition deflate.c:1611

Referenced by deflate().

◆ deflateBound()

uLong ZEXPORT deflateBound ( z_streamp strm,
uLong sourceLen )

Definition at line 838 of file deflate.c.

838 {
839 deflate_state *s;
840 uLong fixedlen, storelen, wraplen;
841
842 /* upper bound for fixed blocks with 9-bit literals and length 255
843 (memLevel == 2, which is the lowest that may not use stored blocks) --
844 ~13% overhead plus a small constant */
845 fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) +
846 (sourceLen >> 9) + 4;
847
848 /* upper bound for stored blocks with length 127 (memLevel == 1) --
849 ~4% overhead plus a small constant */
850 storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) +
851 (sourceLen >> 11) + 7;
852
853 /* if can't get parameters, return larger bound plus a zlib wrapper */
854 if (deflateStateCheck(strm))
855 return (fixedlen > storelen ? fixedlen : storelen) + 6;
856
857 /* compute wrapper length */
858 s = strm->state;
859 switch (s->wrap) {
860 case 0: /* raw deflate */
861 wraplen = 0;
862 break;
863 case 1: /* zlib wrapper */
864 wraplen = 6 + (s->strstart ? 4 : 0);
865 break;
866#ifdef GZIP
867 case 2: /* gzip wrapper */
868 wraplen = 18;
869 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
870 Bytef *str;
871 if (s->gzhead->extra != Z_NULL)
872 wraplen += 2 + s->gzhead->extra_len;
873 str = s->gzhead->name;
874 if (str != Z_NULL)
875 do {
876 wraplen++;
877 } while (*str++);
878 str = s->gzhead->comment;
879 if (str != Z_NULL)
880 do {
881 wraplen++;
882 } while (*str++);
883 if (s->gzhead->hcrc)
884 wraplen += 2;
885 }
886 break;
887#endif
888 default: /* for compiler happiness */
889 wraplen = 6;
890 }
891
892 /* if not default parameters, return one of the conservative bounds */
893 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
894 return (s->w_bits <= s->hash_bits && s->level ? fixedlen : storelen) +
895 wraplen;
896
897 /* default settings: return tight bound for that case -- ~0.03% overhead
898 plus a small constant */
899 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
900 (sourceLen >> 25) + 13 - 6 + wraplen;
901}

◆ deflateCopy()

int ZEXPORT deflateCopy ( z_streamp dest,
z_streamp source )

Definition at line 1286 of file deflate.c.

1286 {
1287#ifdef MAXSEG_64K
1288 (void)dest;
1289 (void)source;
1290 return Z_STREAM_ERROR;
1291#else
1292 deflate_state *ds;
1293 deflate_state *ss;
1294
1295
1296 if (deflateStateCheck(source) || dest == Z_NULL) {
1297 return Z_STREAM_ERROR;
1298 }
1299
1300 ss = source->state;
1301
1302 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1303
1304 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1305 if (ds == Z_NULL) return Z_MEM_ERROR;
1306 dest->state = (struct internal_state FAR *) ds;
1307 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
1308 ds->strm = dest;
1309
1310 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
1311 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
1312 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
1313 ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS);
1314
1315 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
1316 ds->pending_buf == Z_NULL) {
1317 deflateEnd (dest);
1318 return Z_MEM_ERROR;
1319 }
1320 /* following zmemcpy do not work for 16-bit MSDOS */
1321 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
1322 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
1323 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
1324 zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS);
1325
1326 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1327#ifdef LIT_MEM
1328 ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1329 ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1330#else
1331 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1332#endif
1333
1334 ds->l_desc.dyn_tree = ds->dyn_ltree;
1335 ds->d_desc.dyn_tree = ds->dyn_dtree;
1336 ds->bl_desc.dyn_tree = ds->bl_tree;
1337
1338 return Z_OK;
1339#endif /* MAXSEG_64K */
1340}
int ZEXPORT deflateEnd(z_streamp strm)
Definition deflate.c:1262
Pos FAR Posf
Definition deflate.h:97
#define LIT_BUFS
Definition deflate.h:229
struct z_stream_s z_stream
#define Z_MEM_ERROR
Definition zlib.h:183
#define ZALLOC(strm, items, size)
Definition zutil.h:241
uch FAR uchf
Definition zutil.h:40
ush FAR ushf
Definition zutil.h:42

◆ deflateEnd()

int ZEXPORT deflateEnd ( z_streamp strm)

Definition at line 1262 of file deflate.c.

1262 {
1263 int status;
1264
1265 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
1266
1267 status = strm->state->status;
1268
1269 /* Deallocate in reverse order of allocations: */
1270 TRY_FREE(strm, strm->state->pending_buf);
1271 TRY_FREE(strm, strm->state->head);
1272 TRY_FREE(strm, strm->state->prev);
1273 TRY_FREE(strm, strm->state->window);
1274
1275 ZFREE(strm, strm->state);
1276 strm->state = Z_NULL;
1277
1278 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1279}
#define Z_DATA_ERROR
Definition zlib.h:182
#define ZFREE(strm, addr)
Definition zutil.h:243
#define TRY_FREE(s, p)
Definition zutil.h:244

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

◆ deflateGetDictionary()

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

Definition at line 620 of file deflate.c.

621 {
622 deflate_state *s;
623 uInt len;
624
625 if (deflateStateCheck(strm))
626 return Z_STREAM_ERROR;
627 s = strm->state;
628 len = s->strstart + s->lookahead;
629 if (len > s->w_size)
630 len = s->w_size;
631 if (dictionary != Z_NULL && len)
632 zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
633 if (dictLength != Z_NULL)
634 *dictLength = len;
635 return Z_OK;
636}

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

381 {
382 deflate_state *s;
383 int wrap = 1;
384 static const char my_version[] = ZLIB_VERSION;
385
386 if (version == Z_NULL || version[0] != my_version[0] ||
387 stream_size != sizeof(z_stream)) {
388 return Z_VERSION_ERROR;
389 }
390 if (strm == Z_NULL) return Z_STREAM_ERROR;
391
392 strm->msg = Z_NULL;
393 if (strm->zalloc == (alloc_func)0) {
394#ifdef Z_SOLO
395 return Z_STREAM_ERROR;
396#else
397 strm->zalloc = zcalloc;
398 strm->opaque = (voidpf)0;
399#endif
400 }
401 if (strm->zfree == (free_func)0)
402#ifdef Z_SOLO
403 return Z_STREAM_ERROR;
404#else
405 strm->zfree = zcfree;
406#endif
407
408#ifdef FASTEST
409 if (level != 0) level = 1;
410#else
411 if (level == Z_DEFAULT_COMPRESSION) level = 6;
412#endif
413
414 if (windowBits < 0) { /* suppress zlib wrapper */
415 wrap = 0;
416 if (windowBits < -15)
417 return Z_STREAM_ERROR;
418 windowBits = -windowBits;
419 }
420#ifdef GZIP
421 else if (windowBits > 15) {
422 wrap = 2; /* write gzip wrapper instead */
423 windowBits -= 16;
424 }
425#endif
426 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
427 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
428 strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
429 return Z_STREAM_ERROR;
430 }
431 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
432 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
433 if (s == Z_NULL) return Z_MEM_ERROR;
434 strm->state = (struct internal_state FAR *)s;
435 s->strm = strm;
436 s->status = INIT_STATE; /* to pass state test in deflateReset() */
437
438 s->wrap = wrap;
439 s->gzhead = Z_NULL;
440 s->w_bits = (uInt)windowBits;
441 s->w_size = 1 << s->w_bits;
442 s->w_mask = s->w_size - 1;
443
444 s->hash_bits = (uInt)memLevel + 7;
445 s->hash_size = 1 << s->hash_bits;
446 s->hash_mask = s->hash_size - 1;
447 s->hash_shift = ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH);
448
449 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
450 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
451 /* Avoid use of uninitialized value, see:
452 * https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11360
453 */
454 zmemzero(s->prev, s->w_size * sizeof(Pos));
455 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
456
457 s->high_water = 0; /* nothing written to s->window yet */
458
459 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
460
461 /* We overlay pending_buf and sym_buf. This works since the average size
462 * for length/distance pairs over any compressed block is assured to be 31
463 * bits or less.
464 *
465 * Analysis: The longest fixed codes are a length code of 8 bits plus 5
466 * extra bits, for lengths 131 to 257. The longest fixed distance codes are
467 * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
468 * possible fixed-codes length/distance pair is then 31 bits total.
469 *
470 * sym_buf starts one-fourth of the way into pending_buf. So there are
471 * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
472 * in sym_buf is three bytes -- two for the distance and one for the
473 * literal/length. As each symbol is consumed, the pointer to the next
474 * sym_buf value to read moves forward three bytes. From that symbol, up to
475 * 31 bits are written to pending_buf. The closest the written pending_buf
476 * bits gets to the next sym_buf symbol to read is just before the last
477 * code is written. At that time, 31*(n - 2) bits have been written, just
478 * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at
479 * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1
480 * symbols are written.) The closest the writing gets to what is unread is
481 * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and
482 * can range from 128 to 32768.
483 *
484 * Therefore, at a minimum, there are 142 bits of space between what is
485 * written and what is read in the overlain buffers, so the symbols cannot
486 * be overwritten by the compressed data. That space is actually 139 bits,
487 * due to the three-bit fixed-code block header.
488 *
489 * That covers the case where either Z_FIXED is specified, forcing fixed
490 * codes, or when the use of fixed codes is chosen, because that choice
491 * results in a smaller compressed block than dynamic codes. That latter
492 * condition then assures that the above analysis also covers all dynamic
493 * blocks. A dynamic-code block will only be chosen to be emitted if it has
494 * fewer bits than a fixed-code block would for the same set of symbols.
495 * Therefore its average symbol length is assured to be less than 31. So
496 * the compressed data for a dynamic block also cannot overwrite the
497 * symbols from which it is being constructed.
498 */
499
500 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, LIT_BUFS);
501 s->pending_buf_size = (ulg)s->lit_bufsize * 4;
502
503 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
504 s->pending_buf == Z_NULL) {
505 s->status = FINISH_STATE;
506 strm->msg = ERR_MSG(Z_MEM_ERROR);
508 return Z_MEM_ERROR;
509 }
510#ifdef LIT_MEM
511 s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
512 s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
513 s->sym_end = s->lit_bufsize - 1;
514#else
515 s->sym_buf = s->pending_buf + s->lit_bufsize;
516 s->sym_end = (s->lit_bufsize - 1) * 3;
517#endif
518 /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
519 * on 16 bit machines and because stored blocks are restricted to
520 * 64K-1 bytes.
521 */
522
523 s->level = level;
524 s->strategy = strategy;
525 s->method = (Byte)method;
526
527 return deflateReset(strm);
528}
int ZEXPORT deflateReset(z_streamp strm)
Definition deflate.c:699
z_streamp strm
Definition deflate.h:105
void(* free_func)(voidpf opaque, voidpf address)
Definition zlib.h:82
voidpf(* alloc_func)(voidpf opaque, uInt items, uInt size)
Definition zlib.h:81
#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:292
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
Definition zutil.c:286
#define ERR_MSG(err)
Definition zutil.h:59
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
Definition zutil.c:161

Referenced by deflateInit_().

◆ deflateInit_()

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

Definition at line 371 of file deflate.c.

372 {
373 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
374 Z_DEFAULT_STRATEGY, version, stream_size);
375 /* To do: ignore strm->next_in if we use it as window */
376}
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:379
#define DEF_MEM_LEVEL
Definition gzguts.h:150
#define Z_DEFAULT_STRATEGY
Definition zlib.h:200

◆ deflateParams()

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

Definition at line 756 of file deflate.c.

756 {
757 deflate_state *s;
758 compress_func func;
759
760 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
761 s = strm->state;
762
763#ifdef FASTEST
764 if (level != 0) level = 1;
765#else
766 if (level == Z_DEFAULT_COMPRESSION) level = 6;
767#endif
768 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
769 return Z_STREAM_ERROR;
770 }
771 func = configuration_table[s->level].func;
772
773 if ((strategy != s->strategy || func != configuration_table[level].func) &&
774 s->last_flush != -2) {
775 /* Flush the last buffer: */
776 int err = deflate(strm, Z_BLOCK);
777 if (err == Z_STREAM_ERROR)
778 return err;
779 if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
780 return Z_BUF_ERROR;
781 }
782 if (s->level != level) {
783 if (s->level == 0 && s->matches != 0) {
784 if (s->matches == 1)
785 slide_hash(s);
786 else
787 CLEAR_HASH(s);
788 s->matches = 0;
789 }
790 s->level = level;
791 s->max_lazy_match = configuration_table[level].max_lazy;
792 s->good_match = configuration_table[level].good_length;
793 s->nice_match = configuration_table[level].nice_length;
794 s->max_chain_length = configuration_table[level].max_chain;
795 }
796 s->strategy = strategy;
797 return Z_OK;
798}
block_state(* compress_func)(deflate_state *s, int flush)
Definition deflate.c:70
local void slide_hash(deflate_state *s)
Definition deflate.c:187
int ZEXPORT deflate(z_streamp strm, int flush)
Definition deflate.c:950

Referenced by gzsetparams().

◆ deflatePending()

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

Definition at line 717 of file deflate.c.

717 {
718 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
719 if (pending != Z_NULL)
720 *pending = strm->state->pending;
721 if (bits != Z_NULL)
722 *bits = strm->state->bi_valid;
723 return Z_OK;
724}

◆ deflatePrime()

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

Definition at line 727 of file deflate.c.

727 {
728 deflate_state *s;
729 int put;
730
731 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
732 s = strm->state;
733#ifdef LIT_MEM
734 if (bits < 0 || bits > 16 ||
735 (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3))
736 return Z_BUF_ERROR;
737#else
738 if (bits < 0 || bits > 16 ||
739 s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
740 return Z_BUF_ERROR;
741#endif
742 do {
743 put = Buf_size - s->bi_valid;
744 if (put > bits)
745 put = bits;
746 s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
747 s->bi_valid += put;
749 value >>= put;
750 bits -= put;
751 } while (bits);
752 return Z_OK;
753}
#define Buf_size
Definition deflate.h:55
void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s)
Definition trees.c:878
unsigned short ush
Definition zutil.h:41

◆ deflateReset()

int ZEXPORT deflateReset ( z_streamp strm)

Definition at line 699 of file deflate.c.

699 {
700 int ret;
701
702 ret = deflateResetKeep(strm);
703 if (ret == Z_OK)
704 lm_init(strm->state);
705 return ret;
706}
local void lm_init(deflate_state *s)
Definition deflate.c:677
int ZEXPORT deflateResetKeep(z_streamp strm)
Definition deflate.c:639

Referenced by deflateInit2_(), and gz_comp().

◆ deflateResetKeep()

int ZEXPORT deflateResetKeep ( z_streamp strm)

Definition at line 639 of file deflate.c.

639 {
640 deflate_state *s;
641
642 if (deflateStateCheck(strm)) {
643 return Z_STREAM_ERROR;
644 }
645
646 strm->total_in = strm->total_out = 0;
647 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
648 strm->data_type = Z_UNKNOWN;
649
650 s = (deflate_state *)strm->state;
651 s->pending = 0;
652 s->pending_out = s->pending_buf;
653
654 if (s->wrap < 0) {
655 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
656 }
657 s->status =
658#ifdef GZIP
659 s->wrap == 2 ? GZIP_STATE :
660#endif
662 strm->adler =
663#ifdef GZIP
664 s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
665#endif
666 adler32(0L, Z_NULL, 0);
667 s->last_flush = -2;
668
669 _tr_init(s);
670
671 return Z_OK;
672}
void ZLIB_INTERNAL _tr_init(deflate_state *s)
Definition trees.c:455
#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 554 of file deflate.c.

555 {
556 deflate_state *s;
557 uInt str, n;
558 int wrap;
559 unsigned avail;
560 z_const unsigned char *next;
561
562 if (deflateStateCheck(strm) || dictionary == Z_NULL)
563 return Z_STREAM_ERROR;
564 s = strm->state;
565 wrap = s->wrap;
566 if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
567 return Z_STREAM_ERROR;
568
569 /* when using zlib wrappers, compute Adler-32 for provided dictionary */
570 if (wrap == 1)
571 strm->adler = adler32(strm->adler, dictionary, dictLength);
572 s->wrap = 0; /* avoid computing Adler-32 in read_buf */
573
574 /* if dictionary would fill window, just replace the history */
575 if (dictLength >= s->w_size) {
576 if (wrap == 0) { /* already empty otherwise */
577 CLEAR_HASH(s);
578 s->strstart = 0;
579 s->block_start = 0L;
580 s->insert = 0;
581 }
582 dictionary += dictLength - s->w_size; /* use the tail */
583 dictLength = s->w_size;
584 }
585
586 /* insert dictionary into window and hash */
587 avail = strm->avail_in;
588 next = strm->next_in;
589 strm->avail_in = dictLength;
590 strm->next_in = (z_const Bytef *)dictionary;
591 fill_window(s);
592 while (s->lookahead >= MIN_MATCH) {
593 str = s->strstart;
594 n = s->lookahead - (MIN_MATCH-1);
595 do {
596 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
597#ifndef FASTEST
598 s->prev[str & s->w_mask] = s->head[s->ins_h];
599#endif
600 s->head[s->ins_h] = (Pos)str;
601 str++;
602 } while (--n);
603 s->strstart = str;
604 s->lookahead = MIN_MATCH-1;
605 fill_window(s);
606 }
607 s->strstart += s->lookahead;
608 s->block_start = (long)s->strstart;
609 s->insert = s->lookahead;
610 s->lookahead = 0;
611 s->match_length = s->prev_length = MIN_MATCH-1;
612 s->match_available = 0;
613 strm->next_in = next;
614 strm->avail_in = avail;
615 s->wrap = wrap;
616 return Z_OK;
617}

◆ deflateSetHeader()

int ZEXPORT deflateSetHeader ( z_streamp strm,
gz_headerp head )

Definition at line 709 of file deflate.c.

709 {
710 if (deflateStateCheck(strm) || strm->state->wrap != 2)
711 return Z_STREAM_ERROR;
712 strm->state->gzhead = head;
713 return Z_OK;
714}

◆ deflateStateCheck()

local int deflateStateCheck ( z_streamp strm)

Definition at line 533 of file deflate.c.

533 {
534 deflate_state *s;
535 if (strm == Z_NULL ||
536 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
537 return 1;
538 s = strm->state;
539 if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
540#ifdef GZIP
541 s->status != GZIP_STATE &&
542#endif
543 s->status != EXTRA_STATE &&
544 s->status != NAME_STATE &&
545 s->status != COMMENT_STATE &&
546 s->status != HCRC_STATE &&
547 s->status != BUSY_STATE &&
548 s->status != FINISH_STATE))
549 return 1;
550 return 0;
551}
#define GZIP
Definition deflate.h:23

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

802 {
803 deflate_state *s;
804
805 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
806 s = strm->state;
807 s->good_match = (uInt)good_length;
808 s->max_lazy_match = (uInt)max_lazy;
809 s->nice_match = nice_length;
810 s->max_chain_length = (uInt)max_chain;
811 return Z_OK;
812}

◆ fill_window()

local void fill_window ( deflate_state * s)

Definition at line 251 of file deflate.c.

251 {
252 unsigned n;
253 unsigned more; /* Amount of free space at the end of the window. */
254 uInt wsize = s->w_size;
255
256 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
257
258 do {
259 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
260
261 /* Deal with !@#$% 64K limit: */
262 if (sizeof(int) <= 2) {
263 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
264 more = wsize;
265
266 } else if (more == (unsigned)(-1)) {
267 /* Very unlikely, but possible on 16 bit machine if
268 * strstart == 0 && lookahead == 1 (input done a byte at time)
269 */
270 more--;
271 }
272 }
273
274 /* If the window is almost full and there is insufficient lookahead,
275 * move the upper half to the lower one to make room in the upper half.
276 */
277 if (s->strstart >= wsize + MAX_DIST(s)) {
278
279 zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more);
280 s->match_start -= wsize;
281 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
282 s->block_start -= (long) wsize;
283 if (s->insert > s->strstart)
284 s->insert = s->strstart;
285 slide_hash(s);
286 more += wsize;
287 }
288 if (s->strm->avail_in == 0) break;
289
290 /* If there was no sliding:
291 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
292 * more == window_size - lookahead - strstart
293 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
294 * => more >= window_size - 2*WSIZE + 2
295 * In the BIG_MEM or MMAP case (not yet supported),
296 * window_size == input_size + MIN_LOOKAHEAD &&
297 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
298 * Otherwise, window_size == 2*WSIZE so more >= 2.
299 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
300 */
301 Assert(more >= 2, "more < 2");
302
303 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
304 s->lookahead += n;
305
306 /* Initialize the hash value now that we have some input: */
307 if (s->lookahead + s->insert >= MIN_MATCH) {
308 uInt str = s->strstart - s->insert;
309 s->ins_h = s->window[str];
310 UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
311#if MIN_MATCH != 3
312 Call UPDATE_HASH() MIN_MATCH-3 more times
313#endif
314 while (s->insert) {
315 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
316#ifndef FASTEST
317 s->prev[str & s->w_mask] = s->head[s->ins_h];
318#endif
319 s->head[s->ins_h] = (Pos)str;
320 str++;
321 s->insert--;
322 if (s->lookahead + s->insert < MIN_MATCH)
323 break;
324 }
325 }
326 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
327 * but this is not important since only literal bytes will be emitted.
328 */
329
330 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
331
332 /* If the WIN_INIT bytes after the end of the current data have never been
333 * written, then zero those bytes in order to avoid memory check reports of
334 * the use of uninitialized (or uninitialised as Julian writes) bytes by
335 * the longest match routines. Update the high water mark for the next
336 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
337 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
338 */
339 if (s->high_water < s->window_size) {
340 ulg curr = s->strstart + (ulg)(s->lookahead);
341 ulg init;
342
343 if (s->high_water < curr) {
344 /* Previous high water mark below current data -- zero WIN_INIT
345 * bytes or up to end of window, whichever is less.
346 */
347 init = s->window_size - curr;
348 if (init > WIN_INIT)
349 init = WIN_INIT;
350 zmemzero(s->window + curr, (unsigned)init);
351 s->high_water = curr + init;
352 }
353 else if (s->high_water < (ulg)curr + WIN_INIT) {
354 /* High water mark at or above current data, but below current data
355 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
356 * to end of window, whichever is less.
357 */
358 init = (ulg)curr + WIN_INIT - s->high_water;
359 if (init > s->window_size - s->high_water)
360 init = s->window_size - s->high_water;
361 zmemzero(s->window + s->high_water, (unsigned)init);
362 s->high_water += init;
363 }
364 }
365
366 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
367 "not enough room for search");
368}
#define WIN_INIT
Definition deflate.h:300

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

◆ flush_pending()

local void flush_pending ( z_streamp strm)

Definition at line 919 of file deflate.c.

919 {
920 unsigned len;
921 deflate_state *s = strm->state;
922
924 len = s->pending;
925 if (len > strm->avail_out) len = strm->avail_out;
926 if (len == 0) return;
927
928 zmemcpy(strm->next_out, s->pending_out, len);
929 strm->next_out += len;
930 s->pending_out += len;
931 strm->total_out += len;
932 strm->avail_out -= len;
933 s->pending -= len;
934 if (s->pending == 0) {
935 s->pending_out = s->pending_buf;
936 }
937}

Referenced by deflate(), and deflate_stored().

◆ lm_init()

local void lm_init ( deflate_state * s)

Definition at line 677 of file deflate.c.

677 {
678 s->window_size = (ulg)2L*s->w_size;
679
680 CLEAR_HASH(s);
681
682 /* Set the default configuration parameters:
683 */
684 s->max_lazy_match = configuration_table[s->level].max_lazy;
685 s->good_match = configuration_table[s->level].good_length;
686 s->nice_match = configuration_table[s->level].nice_length;
687 s->max_chain_length = configuration_table[s->level].max_chain;
688
689 s->strstart = 0;
690 s->block_start = 0L;
691 s->lookahead = 0;
692 s->insert = 0;
693 s->match_length = s->prev_length = MIN_MATCH-1;
694 s->match_available = 0;
695 s->ins_h = 0;
696}

Referenced by deflateReset().

◆ longest_match()

local uInt longest_match ( deflate_state * s,
IPos cur_match )

Definition at line 1352 of file deflate.c.

1352 {
1353 unsigned chain_length = s->max_chain_length;/* max hash chain length */
1354 register Bytef *scan = s->window + s->strstart; /* current string */
1355 register Bytef *match; /* matched string */
1356 register int len; /* length of current match */
1357 int best_len = (int)s->prev_length; /* best match length so far */
1358 int nice_match = s->nice_match; /* stop if match long enough */
1359 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1360 s->strstart - (IPos)MAX_DIST(s) : NIL;
1361 /* Stop when cur_match becomes <= limit. To simplify the code,
1362 * we prevent matches with the string of window index 0.
1363 */
1364 Posf *prev = s->prev;
1365 uInt wmask = s->w_mask;
1366
1367#ifdef UNALIGNED_OK
1368 /* Compare two bytes at a time. Note: this is not always beneficial.
1369 * Try with and without -DUNALIGNED_OK to check.
1370 */
1371 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1372 register ush scan_start = *(ushf*)scan;
1373 register ush scan_end = *(ushf*)(scan + best_len - 1);
1374#else
1375 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1376 register Byte scan_end1 = scan[best_len - 1];
1377 register Byte scan_end = scan[best_len];
1378#endif
1379
1380 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1381 * It is easy to get rid of this optimization if necessary.
1382 */
1383 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1384
1385 /* Do not waste too much time if we already have a good match: */
1386 if (s->prev_length >= s->good_match) {
1387 chain_length >>= 2;
1388 }
1389 /* Do not look for matches beyond the end of the input. This is necessary
1390 * to make deflate deterministic.
1391 */
1392 if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
1393
1394 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1395 "need lookahead");
1396
1397 do {
1398 Assert(cur_match < s->strstart, "no future");
1399 match = s->window + cur_match;
1400
1401 /* Skip to next match if the match length cannot increase
1402 * or if the match length is less than 2. Note that the checks below
1403 * for insufficient lookahead only occur occasionally for performance
1404 * reasons. Therefore uninitialized memory will be accessed, and
1405 * conditional jumps will be made that depend on those values.
1406 * However the length of the match is limited to the lookahead, so
1407 * the output of deflate is not affected by the uninitialized values.
1408 */
1409#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1410 /* This code assumes sizeof(unsigned short) == 2. Do not use
1411 * UNALIGNED_OK if your compiler uses a different size.
1412 */
1413 if (*(ushf*)(match + best_len - 1) != scan_end ||
1414 *(ushf*)match != scan_start) continue;
1415
1416 /* It is not necessary to compare scan[2] and match[2] since they are
1417 * always equal when the other bytes match, given that the hash keys
1418 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1419 * strstart + 3, + 5, up to strstart + 257. We check for insufficient
1420 * lookahead only every 4th comparison; the 128th check will be made
1421 * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is
1422 * necessary to put more guard bytes at the end of the window, or
1423 * to check more often for insufficient lookahead.
1424 */
1425 Assert(scan[2] == match[2], "scan[2]?");
1426 scan++, match++;
1427 do {
1428 } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1429 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1430 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1431 *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
1432 scan < strend);
1433 /* The funny "do {}" generates better code on most compilers */
1434
1435 /* Here, scan <= window + strstart + 257 */
1436 Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1437 "wild scan");
1438 if (*scan == *match) scan++;
1439
1440 len = (MAX_MATCH - 1) - (int)(strend - scan);
1441 scan = strend - (MAX_MATCH-1);
1442
1443#else /* UNALIGNED_OK */
1444
1445 if (match[best_len] != scan_end ||
1446 match[best_len - 1] != scan_end1 ||
1447 *match != *scan ||
1448 *++match != scan[1]) continue;
1449
1450 /* The check at best_len - 1 can be removed because it will be made
1451 * again later. (This heuristic is not always a win.)
1452 * It is not necessary to compare scan[2] and match[2] since they
1453 * are always equal when the other bytes match, given that
1454 * the hash keys are equal and that HASH_BITS >= 8.
1455 */
1456 scan += 2, match++;
1457 Assert(*scan == *match, "match[2]?");
1458
1459 /* We check for insufficient lookahead only every 8th comparison;
1460 * the 256th check will be made at strstart + 258.
1461 */
1462 do {
1463 } while (*++scan == *++match && *++scan == *++match &&
1464 *++scan == *++match && *++scan == *++match &&
1465 *++scan == *++match && *++scan == *++match &&
1466 *++scan == *++match && *++scan == *++match &&
1467 scan < strend);
1468
1469 Assert(scan <= s->window + (unsigned)(s->window_size - 1),
1470 "wild scan");
1471
1472 len = MAX_MATCH - (int)(strend - scan);
1473 scan = strend - MAX_MATCH;
1474
1475#endif /* UNALIGNED_OK */
1476
1477 if (len > best_len) {
1478 s->match_start = cur_match;
1479 best_len = len;
1480 if (len >= nice_match) break;
1481#ifdef UNALIGNED_OK
1482 scan_end = *(ushf*)(scan + best_len - 1);
1483#else
1484 scan_end1 = scan[best_len - 1];
1485 scan_end = scan[best_len];
1486#endif
1487 }
1488 } while ((cur_match = prev[cur_match & wmask]) > limit
1489 && --chain_length != 0);
1490
1491 if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1492 return s->lookahead;
1493}

Referenced by deflate_fast(), and deflate_slow().

◆ putShortMSB()

local void putShortMSB ( deflate_state * s,
uInt b )

Definition at line 908 of file deflate.c.

908 {
909 put_byte(s, (Byte)(b >> 8));
910 put_byte(s, (Byte)(b & 0xff));
911}

Referenced by deflate().

◆ read_buf()

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

Definition at line 218 of file deflate.c.

218 {
219 unsigned len = strm->avail_in;
220
221 if (len > size) len = size;
222 if (len == 0) return 0;
223
224 strm->avail_in -= len;
225
226 zmemcpy(buf, strm->next_in, len);
227 if (strm->state->wrap == 1) {
228 strm->adler = adler32(strm->adler, buf, len);
229 }
230#ifdef GZIP
231 else if (strm->state->wrap == 2) {
232 strm->adler = crc32(strm->adler, buf, len);
233 }
234#endif
235 strm->next_in += len;
236 strm->total_in += len;
237
238 return len;
239}

Referenced by deflate_stored(), and fill_window().

◆ slide_hash()

local void slide_hash ( deflate_state * s)

Definition at line 187 of file deflate.c.

187 {
188 unsigned n, m;
189 Posf *p;
190 uInt wsize = s->w_size;
191
192 n = s->hash_size;
193 p = &s->head[n];
194 do {
195 m = *--p;
196 *p = (Pos)(m >= wsize ? m - wsize : NIL);
197 } while (--n);
198 n = wsize;
199#ifndef FASTEST
200 p = &s->prev[n];
201 do {
202 m = *--p;
203 *p = (Pos)(m >= wsize ? m - wsize : NIL);
204 /* If n is not on any hash chain, prev[n] is garbage but
205 * its value will never be used.
206 */
207 } while (--n);
208#endif
209}

Referenced by deflateParams(), and fill_window().

Variable Documentation

◆ configuration_table

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_fast(deflate_state *s, int flush)
Definition deflate.c:1815
local block_state deflate_slow(deflate_state *s, int flush)
Definition deflate.c:1914

Definition at line 112 of file deflate.c.

112 {
113/* good lazy nice chain */
114/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
115/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
116/* 2 */ {4, 5, 16, 8, deflate_fast},
117/* 3 */ {4, 6, 32, 32, deflate_fast},
118
119/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
120/* 5 */ {8, 16, 32, 32, deflate_slow},
121/* 6 */ {8, 16, 128, 128, deflate_slow},
122/* 7 */ {8, 32, 128, 256, deflate_slow},
123/* 8 */ {32, 128, 258, 1024, deflate_slow},
124/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */

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

◆ deflate_copyright

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

Definition at line 54 of file deflate.c.