summaryrefslogtreecommitdiff
path: root/inflate.c
diff options
context:
space:
mode:
authorIlya Leoshkevich <iii@linux.ibm.com>2019-04-10 13:52:07 +0200
committerHans Kristian Rosbach <hk-github@circlestorm.org>2019-05-23 12:44:59 +0200
commitedaf8cd770dcc1d5df99da6b1d81cf2c5818aa32 (patch)
tree610dc5a2a30cc1222b28ad255b1a01a2e773e44f /inflate.c
parentb7f659f2faa899a73f3456c29554769261c1721f (diff)
Introduce inflate and deflate hooks
Diffstat (limited to 'inflate.c')
-rw-r--r--inflate.c55
1 files changed, 41 insertions, 14 deletions
diff --git a/inflate.c b/inflate.c
index 002dca8..4776b20 100644
--- a/inflate.c
+++ b/inflate.c
@@ -88,6 +88,27 @@
#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
@@ -134,6 +155,7 @@ int ZEXPORT PREFIX(inflateResetKeep)(PREFIX3(stream) *strm) {
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;
}
@@ -175,7 +197,7 @@ int ZEXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int windowBits) {
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;
}
@@ -206,7 +228,7 @@ int ZEXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int windowBits, const c
}
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"));
@@ -216,7 +238,7 @@ int ZEXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int windowBits, const c
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;
@@ -231,6 +253,7 @@ int ZEXPORT PREFIX(inflatePrime)(PREFIX3(stream) *strm, int bits, int value) {
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;
@@ -365,12 +388,12 @@ int ZLIB_INTERNAL inflate_ensure_window(struct inflate_state *state)
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
@@ -840,6 +863,7 @@ int ZEXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int flush) {
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;
@@ -1210,7 +1234,7 @@ int ZEXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int flush) {
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) && (
@@ -1261,7 +1285,9 @@ int ZEXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int flush) {
*/
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;
@@ -1272,7 +1298,7 @@ int ZEXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int flush) {
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);
@@ -1287,8 +1313,8 @@ int ZEXPORT PREFIX(inflateEnd)(PREFIX3(stream) *strm) {
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;
@@ -1468,21 +1494,21 @@ int ZEXPORT PREFIX(inflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *source)
/* 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);
@@ -1532,6 +1558,7 @@ long ZEXPORT PREFIX(inflateMark)(PREFIX3(stream) *strm) {
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));