| 1 |
/* dfltcc_inflate.c - IBM Z DEFLATE CONVERSION CALL decompression support. */ |
| 2 |
|
| 3 |
/* |
| 4 |
Use the following commands to build zlib-ng with DFLTCC decompression support: |
| 5 |
|
| 6 |
$ ./configure --with-dfltcc-inflate |
| 7 |
or |
| 8 |
|
| 9 |
$ cmake -DWITH_DFLTCC_INFLATE=1 . |
| 10 |
|
| 11 |
and then |
| 12 |
|
| 13 |
$ make |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "zbuild.h" |
| 17 |
#include "zutil.h" |
| 18 |
#include "inftrees.h" |
| 19 |
#include "inflate.h" |
| 20 |
#include "dfltcc_inflate.h" |
| 21 |
#include "dfltcc_detail.h" |
| 22 |
|
| 23 |
struct inflate_state Z_INTERNAL *PREFIX(dfltcc_alloc_inflate_state)(PREFIX3(streamp) strm) { |
| 24 |
return (struct inflate_state *)dfltcc_alloc_state(strm, sizeof(struct inflate_state), sizeof(struct dfltcc_state)); |
| 25 |
} |
| 26 |
|
| 27 |
void Z_INTERNAL PREFIX(dfltcc_reset_inflate_state)(PREFIX3(streamp) strm) { |
| 28 |
struct inflate_state *state = (struct inflate_state *)strm->state; |
| 29 |
struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); |
| 30 |
|
| 31 |
dfltcc_reset_state(dfltcc_state); |
| 32 |
} |
| 33 |
|
| 34 |
void Z_INTERNAL PREFIX(dfltcc_copy_inflate_state)(struct inflate_state *dst, const struct inflate_state *src) { |
| 35 |
dfltcc_copy_state(dst, src, sizeof(struct inflate_state), sizeof(struct dfltcc_state)); |
| 36 |
} |
| 37 |
|
| 38 |
int Z_INTERNAL PREFIX(dfltcc_can_inflate)(PREFIX3(streamp) strm) { |
| 39 |
struct inflate_state *state = (struct inflate_state *)strm->state; |
| 40 |
struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); |
| 41 |
|
| 42 |
/* Unsupported hardware */ |
| 43 |
return is_bit_set(dfltcc_state->af.fns, DFLTCC_XPND) && is_bit_set(dfltcc_state->af.fmts, DFLTCC_FMT0); |
| 44 |
} |
| 45 |
|
| 46 |
static inline dfltcc_cc dfltcc_xpnd(PREFIX3(streamp) strm) { |
| 47 |
struct inflate_state *state = (struct inflate_state *)strm->state; |
| 48 |
struct dfltcc_param_v0 *param = &GET_DFLTCC_STATE(state)->param; |
| 49 |
size_t avail_in = strm->avail_in; |
| 50 |
size_t avail_out = strm->avail_out; |
| 51 |
dfltcc_cc cc; |
| 52 |
|
| 53 |
cc = dfltcc(DFLTCC_XPND | HBT_CIRCULAR, |
| 54 |
param, &strm->next_out, &avail_out, |
| 55 |
&strm->next_in, &avail_in, state->window); |
| 56 |
strm->avail_in = avail_in; |
| 57 |
strm->avail_out = avail_out; |
| 58 |
return cc; |
| 59 |
} |
| 60 |
|
| 61 |
dfltcc_inflate_action Z_INTERNAL PREFIX(dfltcc_inflate)(PREFIX3(streamp) strm, int flush, int *ret) { |
| 62 |
struct inflate_state *state = (struct inflate_state *)strm->state; |
| 63 |
struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); |
| 64 |
struct dfltcc_param_v0 *param = &dfltcc_state->param; |
| 65 |
dfltcc_cc cc; |
| 66 |
|
| 67 |
if (flush == Z_BLOCK || flush == Z_TREES) { |
| 68 |
/* DFLTCC does not support stopping on block boundaries */ |
| 69 |
if (PREFIX(dfltcc_inflate_disable)(strm)) { |
| 70 |
*ret = Z_STREAM_ERROR; |
| 71 |
return DFLTCC_INFLATE_BREAK; |
| 72 |
} else |
| 73 |
return DFLTCC_INFLATE_SOFTWARE; |
| 74 |
} |
| 75 |
|
| 76 |
if (state->last) { |
| 77 |
if (state->bits != 0) { |
| 78 |
strm->next_in++; |
| 79 |
strm->avail_in--; |
| 80 |
state->bits = 0; |
| 81 |
} |
| 82 |
state->mode = CHECK; |
| 83 |
return DFLTCC_INFLATE_CONTINUE; |
| 84 |
} |
| 85 |
|
| 86 |
if (strm->avail_in == 0 && !param->cf) |
| 87 |
return DFLTCC_INFLATE_BREAK; |
| 88 |
|
| 89 |
if (PREFIX(inflate_ensure_window)(state)) { |
| 90 |
state->mode = MEM; |
| 91 |
return DFLTCC_INFLATE_CONTINUE; |
| 92 |
} |
| 93 |
|
| 94 |
/* Translate stream to parameter block */ |
| 95 |
param->cvt = ((state->wrap & 4) && state->flags) ? CVT_CRC32 : CVT_ADLER32; |
| 96 |
param->sbb = state->bits; |
| 97 |
if (param->hl) |
| 98 |
param->nt = 0; /* Honor history for the first block */ |
| 99 |
if (state->wrap & 4) |
| 100 |
param->cv = state->flags ? ZSWAP32(state->check) : state->check; |
| 101 |
|
| 102 |
/* Inflate */ |
| 103 |
do { |
| 104 |
cc = dfltcc_xpnd(strm); |
| 105 |
} while (cc == DFLTCC_CC_AGAIN); |
| 106 |
|
| 107 |
/* Translate parameter block to stream */ |
| 108 |
strm->msg = oesc_msg(dfltcc_state->msg, param->oesc); |
| 109 |
state->last = cc == DFLTCC_CC_OK; |
| 110 |
state->bits = param->sbb; |
| 111 |
if (state->wrap & 4) |
| 112 |
strm->adler = state->check = state->flags ? ZSWAP32(param->cv) : param->cv; |
| 113 |
if (cc == DFLTCC_CC_OP2_CORRUPT && param->oesc != 0) { |
| 114 |
/* Report an error if stream is corrupted */ |
| 115 |
state->mode = BAD; |
| 116 |
return DFLTCC_INFLATE_CONTINUE; |
| 117 |
} |
| 118 |
state->mode = TYPEDO; |
| 119 |
/* Break if operands are exhausted, otherwise continue looping */ |
| 120 |
return (cc == DFLTCC_CC_OP1_TOO_SHORT || cc == DFLTCC_CC_OP2_TOO_SHORT) ? |
| 121 |
DFLTCC_INFLATE_BREAK : DFLTCC_INFLATE_CONTINUE; |
| 122 |
} |
| 123 |
|
| 124 |
int Z_INTERNAL PREFIX(dfltcc_was_inflate_used)(PREFIX3(streamp) strm) { |
| 125 |
struct inflate_state *state = (struct inflate_state *)strm->state; |
| 126 |
struct dfltcc_param_v0 *param = &GET_DFLTCC_STATE(state)->param; |
| 127 |
|
| 128 |
return !param->nt; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
Rotates a circular buffer. |
| 133 |
The implementation is based on https://cplusplus.com/reference/algorithm/rotate/ |
| 134 |
*/ |
| 135 |
static void rotate(unsigned char *start, unsigned char *pivot, unsigned char *end) { |
| 136 |
unsigned char *p = pivot; |
| 137 |
unsigned char tmp; |
| 138 |
|
| 139 |
while (p != start) { |
| 140 |
tmp = *start; |
| 141 |
*start = *p; |
| 142 |
*p = tmp; |
| 143 |
|
| 144 |
start++; |
| 145 |
p++; |
| 146 |
|
| 147 |
if (p == end) |
| 148 |
p = pivot; |
| 149 |
else if (start == pivot) |
| 150 |
pivot = p; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
int Z_INTERNAL PREFIX(dfltcc_inflate_disable)(PREFIX3(streamp) strm) { |
| 155 |
struct inflate_state *state = (struct inflate_state *)strm->state; |
| 156 |
struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); |
| 157 |
struct dfltcc_param_v0 *param = &dfltcc_state->param; |
| 158 |
|
| 159 |
if (!PREFIX(dfltcc_can_inflate)(strm)) |
| 160 |
return 0; |
| 161 |
if (PREFIX(dfltcc_was_inflate_used)(strm)) |
| 162 |
/* DFLTCC has already decompressed some data. Since there is not |
| 163 |
* enough information to resume decompression in software, the call |
| 164 |
* must fail. |
| 165 |
*/ |
| 166 |
return 1; |
| 167 |
/* DFLTCC was not used yet - decompress in software */ |
| 168 |
memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af)); |
| 169 |
/* Convert the window from the hardware to the software format */ |
| 170 |
rotate(state->window, state->window + param->ho, state->window + HB_SIZE); |
| 171 |
state->whave = state->wnext = MIN(param->hl, state->wsize); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
Preloading history. |
| 177 |
*/ |
| 178 |
int Z_INTERNAL PREFIX(dfltcc_inflate_set_dictionary)(PREFIX3(streamp) strm, |
| 179 |
const unsigned char *dictionary, uInt dict_length) { |
| 180 |
struct inflate_state *state = (struct inflate_state *)strm->state; |
| 181 |
struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); |
| 182 |
struct dfltcc_param_v0 *param = &dfltcc_state->param; |
| 183 |
|
| 184 |
if (PREFIX(inflate_ensure_window)(state)) { |
| 185 |
state->mode = MEM; |
| 186 |
return Z_MEM_ERROR; |
| 187 |
} |
| 188 |
|
| 189 |
append_history(param, state->window, dictionary, dict_length); |
| 190 |
state->havedict = 1; |
| 191 |
return Z_OK; |
| 192 |
} |
| 193 |
|
| 194 |
int Z_INTERNAL PREFIX(dfltcc_inflate_get_dictionary)(PREFIX3(streamp) strm, |
| 195 |
unsigned char *dictionary, uInt *dict_length) { |
| 196 |
struct inflate_state *state = (struct inflate_state *)strm->state; |
| 197 |
struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); |
| 198 |
struct dfltcc_param_v0 *param = &dfltcc_state->param; |
| 199 |
|
| 200 |
if (dictionary && state->window) |
| 201 |
get_history(param, state->window, dictionary); |
| 202 |
if (dict_length) |
| 203 |
*dict_length = param->hl; |
| 204 |
return Z_OK; |
| 205 |
} |