*/
/* ===========================================================================
- * Function prototypes.
+ * Architecture-specific hooks.
*/
+/* Memory management for the deflate state. Useful for allocating arch-specific extension blocks. */
+#define ZALLOC_STATE(strm, items, size) ZALLOC(strm, items, size)
+#define ZFREE_STATE(strm, addr) ZFREE(strm, addr)
+#define ZCOPY_STATE(dst, src, size) memcpy(dst, src, size)
+/* Memory management for the window. Useful for allocation the aligned window. */
+#define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size)
+#define TRY_FREE_WINDOW(strm, addr) TRY_FREE(strm, addr)
+/* Invoked at the beginning of deflateSetDictionary(). Useful for checking arch-specific window data. */
+#define DEFLATE_SET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
+/* Invoked at the beginning of deflateGetDictionary(). Useful for adjusting arch-specific window data. */
+#define DEFLATE_GET_DICTIONARY_HOOK(strm, dict, dict_len) do {} while (0)
+/* Invoked at the end of deflateResetKeep(). Useful for initializing arch-specific extension blocks. */
+#define DEFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
+/* Invoked at the beginning of deflateParams(). Useful for updating arch-specific compression parameters. */
+#define DEFLATE_PARAMS_HOOK(strm, level, strategy) do {} while (0)
+/* Adjusts the upper bound on compressed data length based on compression parameters and uncompressed data length.
+ * Useful when arch-specific deflation code behaves differently than regular zlib-ng algorithms. */
+#define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen) do {} while (0)
+/* Returns whether an optimistic upper bound on compressed data length should *not* be used.
+ * Useful when arch-specific deflation code behaves differently than regular zlib-ng algorithms. */
+#define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) 0
+/* Invoked for each deflate() call. Useful for plugging arch-specific deflation code. */
+#define DEFLATE_HOOK(strm, flush, bstate) 0
+/* Returns whether zlib-ng should compute a checksum. Set to 0 if arch-specific deflation code already does that. */
+#define DEFLATE_NEED_CHECKSUM(strm) 1
+/* ===========================================================================
+ * Function prototypes.
+ */
typedef block_state (*compress_func) (deflate_state *s, int flush);
/* Compression function. Returns the block state after the call. */
windowBits = 13;
#endif
- s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
+ s = (deflate_state *) ZALLOC_STATE(strm, 1, sizeof(deflate_state));
if (s == NULL)
return Z_MEM_ERROR;
strm->state = (struct internal_state *)s;
window_padding = 8;
#endif
- s->window = (unsigned char *) ZALLOC(strm, s->w_size + window_padding, 2*sizeof(unsigned char));
+ s->window = (unsigned char *) ZALLOC_WINDOW(strm, s->w_size + window_padding, 2*sizeof(unsigned char));
s->prev = (Pos *) ZALLOC(strm, s->w_size, sizeof(Pos));
memset(s->prev, 0, s->w_size * sizeof(Pos));
s->head = (Pos *) ZALLOC(strm, s->hash_size, sizeof(Pos));
/* when using zlib wrappers, compute Adler-32 for provided dictionary */
if (wrap == 1)
strm->adler = functable.adler32(strm->adler, dictionary, dictLength);
+ DEFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength); /* hook for IBM Z DFLTCC */
s->wrap = 0; /* avoid computing Adler-32 in read_buf */
/* if dictionary would fill window, just replace the history */
if (deflateStateCheck(strm))
return Z_STREAM_ERROR;
+ DEFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength); /* hook for IBM Z DFLTCC */
s = strm->state;
len = s->strstart + s->lookahead;
if (len > s->w_size)
_tr_init(s);
+ DEFLATE_RESET_KEEP_HOOK(strm); /* hook for IBM Z DFLTCC */
+
return Z_OK;
}
if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
return Z_STREAM_ERROR;
}
+ DEFLATE_PARAMS_HOOK(strm, level, strategy); /* hook for IBM Z DFLTCC */
func = configuration_table[s->level].func;
if ((strategy != s->strategy || func != configuration_table[level].func) &&
/* conservative upper bound for compressed data */
complen = sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
+ DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, sourceLen); /* hook for IBM Z DFLTCC */
/* if can't get parameters, return conservative bound plus zlib wrapper */
if (deflateStateCheck(strm))
}
/* if not default parameters, return conservative bound */
- if (s->w_bits != 15 || s->hash_bits != 8 + 7)
+ if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) || /* hook for IBM Z DFLTCC */
+ s->w_bits != 15 || s->hash_bits != 8 + 7)
return complen + wraplen;
/* default settings: return tight bound for that case */
if (strm->avail_in != 0 || s->lookahead != 0 || (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
block_state bstate;
- bstate = s->level == 0 ? deflate_stored(s, flush) :
+ bstate = DEFLATE_HOOK(strm, flush, &bstate) ? bstate : /* hook for IBM Z DFLTCC */
+ s->level == 0 ? deflate_stored(s, flush) :
s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
s->strategy == Z_RLE ? deflate_rle(s, flush) :
#ifdef X86_QUICK_STRATEGY
TRY_FREE(strm, strm->state->pending_buf);
TRY_FREE(strm, strm->state->head);
TRY_FREE(strm, strm->state->prev);
- TRY_FREE(strm, strm->state->window);
+ TRY_FREE_WINDOW(strm, strm->state->window);
- ZFREE(strm, strm->state);
+ ZFREE_STATE(strm, strm->state);
strm->state = NULL;
return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
memcpy((void *)dest, (void *)source, sizeof(PREFIX3(stream)));
- ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
+ ds = (deflate_state *) ZALLOC_STATE(dest, 1, sizeof(deflate_state));
if (ds == NULL)
return Z_MEM_ERROR;
dest->state = (struct internal_state *) ds;
- memcpy((void *)ds, (void *)ss, sizeof(deflate_state));
+ ZCOPY_STATE((void *)ds, (void *)ss, sizeof(deflate_state));
ds->strm = dest;
- ds->window = (unsigned char *) ZALLOC(dest, ds->w_size, 2*sizeof(unsigned char));
+ ds->window = (unsigned char *) ZALLOC_WINDOW(dest, ds->w_size, 2*sizeof(unsigned char));
ds->prev = (Pos *) ZALLOC(dest, ds->w_size, sizeof(Pos));
ds->head = (Pos *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
ds->pending_buf = (unsigned char *) ZALLOC(dest, ds->lit_bufsize, 4);
strm->avail_in -= len;
+ if (!DEFLATE_NEED_CHECKSUM(strm)) {
+ memcpy(buf, strm->next_in, len);
+ } else
#ifdef GZIP
if (strm->state->wrap == 2)
copy_with_crc(strm, buf, len);
#include "memcopy.h"
#include "functable.h"
+/* Architecture-specific hooks. */
+/* Memory management for the inflate state. Useful for allocating arch-specific extension blocks. */
+#define ZALLOC_STATE(strm, items, size) ZALLOC(strm, items, size)
+#define ZFREE_STATE(strm, addr) ZFREE(strm, addr)
+#define ZCOPY_STATE(dst, src, size) memcpy(dst, src, size)
+/* Memory management for the window. Useful for allocation the aligned window. */
+#define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size)
+#define ZFREE_WINDOW(strm, addr) ZFREE(strm, addr)
+/* Invoked at the end of inflateResetKeep(). Useful for initializing arch-specific extension blocks. */
+#define INFLATE_RESET_KEEP_HOOK(strm) do {} while (0)
+/* Invoked at the beginning of inflatePrime(). Useful for updating arch-specific buffers. */
+#define INFLATE_PRIME_HOOK(strm, bits, value) do {} while (0)
+/* Invoked at the beginning of each block. Useful for plugging arch-specific inflation code. */
+#define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
+/* Returns whether zlib-ng should compute a checksum. Set to 0 if arch-specific inflation code already does that. */
+#define INFLATE_NEED_CHECKSUM(strm) 1
+/* Returns whether zlib-ng should update a window. Set to 0 if arch-specific inflation code already does that. */
+#define INFLATE_NEED_UPDATEWINDOW(strm) 1
+/* Invoked at the beginning of inflateMark(). Useful for updating arch-specific pointers and offsets. */
+#define INFLATE_MARK_HOOK(strm) do {} while (0)
+
#ifdef MAKEFIXED
# ifndef BUILDFIXED
# define BUILDFIXED
state->lencode = state->distcode = state->next = state->codes;
state->sane = 1;
state->back = -1;
+ INFLATE_RESET_KEEP_HOOK(strm); /* hook for IBM Z DFLTCC */
Tracev((stderr, "inflate: reset\n"));
return Z_OK;
}
if (windowBits && (windowBits < 8 || windowBits > 15))
return Z_STREAM_ERROR;
if (state->window != NULL && state->wbits != (unsigned)windowBits) {
- ZFREE(strm, state->window);
+ ZFREE_WINDOW(strm, state->window);
state->window = NULL;
}
}
if (strm->zfree == NULL)
strm->zfree = zcfree;
- state = (struct inflate_state *) ZALLOC(strm, 1, sizeof(struct inflate_state));
+ state = (struct inflate_state *) ZALLOC_STATE(strm, 1, sizeof(struct inflate_state));
if (state == NULL)
return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n"));
state->mode = HEAD; /* to pass state test in inflateReset2() */
ret = PREFIX(inflateReset2)(strm, windowBits);
if (ret != Z_OK) {
- ZFREE(strm, state);
+ ZFREE_STATE(strm, state);
strm->state = NULL;
}
return ret;
if (inflateStateCheck(strm))
return Z_STREAM_ERROR;
+ INFLATE_PRIME_HOOK(strm, bits, value); /* hook for IBM Z DFLTCC */
state = (struct inflate_state *)strm->state;
if (bits < 0) {
state->hold = 0;
if (state->window == NULL) {
#ifdef INFFAST_CHUNKSIZE
unsigned wsize = 1U << state->wbits;
- state->window = (unsigned char *) ZALLOC(state->strm, wsize + INFFAST_CHUNKSIZE, sizeof(unsigned char));
+ state->window = (unsigned char *) ZALLOC_WINDOW(state->strm, wsize + INFFAST_CHUNKSIZE, sizeof(unsigned char));
if (state->window == Z_NULL)
return 1;
memset(state->window + wsize, 0, INFFAST_CHUNKSIZE);
#else
- state->window = (unsigned char *) ZALLOC(state->strm, 1U << state->wbits, sizeof(unsigned char));
+ state->window = (unsigned char *) ZALLOC_WINDOW(state->strm, 1U << state->wbits, sizeof(unsigned char));
if (state->window == NULL)
return 1;
#endif
if (flush == Z_BLOCK || flush == Z_TREES)
goto inf_leave;
case TYPEDO:
+ INFLATE_TYPEDO_HOOK(strm, flush); /* hook for IBM Z DFLTCC */
if (state->last) {
BYTEBITS();
state->mode = CHECK;
out -= left;
strm->total_out += out;
state->total += out;
- if ((state->wrap & 4) && out)
+ if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
strm->adler = state->check = UPDATE(state->check, put - out, out);
out = left;
if ((state->wrap & 4) && (
*/
inf_leave:
RESTORE();
- if (state->wsize || (out != strm->avail_out && state->mode < BAD && (state->mode < CHECK || flush != Z_FINISH))) {
+ if (INFLATE_NEED_UPDATEWINDOW(strm) &&
+ (state->wsize || (out != strm->avail_out && state->mode < BAD &&
+ (state->mode < CHECK || flush != Z_FINISH)))) {
if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
state->mode = MEM;
return Z_MEM_ERROR;
strm->total_in += in;
strm->total_out += out;
state->total += out;
- if ((state->wrap & 4) && out)
+ if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
strm->adler = state->check = UPDATE(state->check, strm->next_out - out, out);
strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
(state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
return Z_STREAM_ERROR;
state = (struct inflate_state *)strm->state;
if (state->window != NULL)
- ZFREE(strm, state->window);
- ZFREE(strm, strm->state);
+ ZFREE_WINDOW(strm, state->window);
+ ZFREE_STATE(strm, strm->state);
strm->state = NULL;
Tracev((stderr, "inflate: end\n"));
return Z_OK;
/* allocate space */
copy = (struct inflate_state *)
- ZALLOC(source, 1, sizeof(struct inflate_state));
+ ZALLOC_STATE(source, 1, sizeof(struct inflate_state));
if (copy == NULL)
return Z_MEM_ERROR;
window = NULL;
if (state->window != NULL) {
- window = (unsigned char *) ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
+ window = (unsigned char *) ZALLOC_WINDOW(source, 1U << state->wbits, sizeof(unsigned char));
if (window == NULL) {
- ZFREE(source, copy);
+ ZFREE_STATE(source, copy);
return Z_MEM_ERROR;
}
}
/* copy state */
memcpy((void *)dest, (void *)source, sizeof(PREFIX3(stream)));
- memcpy((void *)copy, (void *)state, sizeof(struct inflate_state));
+ ZCOPY_STATE((void *)copy, (void *)state, sizeof(struct inflate_state));
copy->strm = dest;
if (state->lencode >= state->codes && state->lencode <= state->codes + ENOUGH - 1) {
copy->lencode = copy->codes + (state->lencode - state->codes);
if (inflateStateCheck(strm))
return -65536;
+ INFLATE_MARK_HOOK(strm); /* hook for IBM Z DFLTCC */
state = (struct inflate_state *)strm->state;
return ((long)(state->back) << 16) + (state->mode == COPY ? state->length :
(state->mode == MATCH ? state->was - state->length : 0));