1 // SPDX-License-Identifier: Zlib
3 #include "../zlib_inflate/inflate.h"
4 #include "dfltcc_util.h"
5 #include "dfltcc_inflate.h"
7 #include <linux/export.h>
8 #include <linux/zutil.h>
13 int dfltcc_can_inflate(
17 struct inflate_state *state = (struct inflate_state *)strm->state;
18 struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
20 /* Check for kernel dfltcc command line parameter */
21 if (zlib_dfltcc_support == ZLIB_DFLTCC_DISABLED ||
22 zlib_dfltcc_support == ZLIB_DFLTCC_DEFLATE_ONLY)
25 /* Unsupported hardware */
26 return is_bit_set(dfltcc_state->af.fns, DFLTCC_XPND) &&
27 is_bit_set(dfltcc_state->af.fmts, DFLTCC_FMT0);
29 EXPORT_SYMBOL(dfltcc_can_inflate);
31 void dfltcc_reset_inflate_state(z_streamp strm) {
32 struct inflate_state *state = (struct inflate_state *)strm->state;
33 struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
35 dfltcc_reset_state(dfltcc_state);
37 EXPORT_SYMBOL(dfltcc_reset_inflate_state);
39 static int dfltcc_was_inflate_used(
43 struct inflate_state *state = (struct inflate_state *)strm->state;
44 struct dfltcc_param_v0 *param = &GET_DFLTCC_STATE(state)->param;
49 static int dfltcc_inflate_disable(
53 struct inflate_state *state = (struct inflate_state *)strm->state;
54 struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
56 if (!dfltcc_can_inflate(strm))
58 if (dfltcc_was_inflate_used(strm))
59 /* DFLTCC has already decompressed some data. Since there is not
60 * enough information to resume decompression in software, the call
64 /* DFLTCC was not used yet - decompress in software */
65 memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af));
69 static dfltcc_cc dfltcc_xpnd(
73 struct inflate_state *state = (struct inflate_state *)strm->state;
74 struct dfltcc_param_v0 *param = &GET_DFLTCC_STATE(state)->param;
75 size_t avail_in = strm->avail_in;
76 size_t avail_out = strm->avail_out;
79 cc = dfltcc(DFLTCC_XPND | HBT_CIRCULAR,
80 param, &strm->next_out, &avail_out,
81 &strm->next_in, &avail_in, state->window);
82 strm->avail_in = avail_in;
83 strm->avail_out = avail_out;
87 dfltcc_inflate_action dfltcc_inflate(
93 struct inflate_state *state = (struct inflate_state *)strm->state;
94 struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state);
95 struct dfltcc_param_v0 *param = &dfltcc_state->param;
98 if (flush == Z_BLOCK || flush == Z_PACKET_FLUSH) {
99 /* DFLTCC does not support stopping on block boundaries (Z_BLOCK flush option)
100 * as well as the use of Z_PACKET_FLUSH option (used exclusively by PPP driver)
102 if (dfltcc_inflate_disable(strm)) {
103 *ret = Z_STREAM_ERROR;
104 return DFLTCC_INFLATE_BREAK;
106 return DFLTCC_INFLATE_SOFTWARE;
110 if (state->bits != 0) {
116 return DFLTCC_INFLATE_CONTINUE;
119 if (strm->avail_in == 0 && !param->cf)
120 return DFLTCC_INFLATE_BREAK;
122 if (!state->window || state->wsize == 0) {
124 return DFLTCC_INFLATE_CONTINUE;
127 /* Translate stream to parameter block */
128 param->cvt = CVT_ADLER32;
129 param->sbb = state->bits;
131 param->nt = 0; /* Honor history for the first block */
132 param->cv = state->check;
136 cc = dfltcc_xpnd(strm);
137 } while (cc == DFLTCC_CC_AGAIN);
139 /* Translate parameter block to stream */
140 strm->msg = oesc_msg(dfltcc_state->msg, param->oesc);
141 state->last = cc == DFLTCC_CC_OK;
142 state->bits = param->sbb;
143 state->check = param->cv;
144 if (cc == DFLTCC_CC_OP2_CORRUPT && param->oesc != 0) {
145 /* Report an error if stream is corrupted */
147 return DFLTCC_INFLATE_CONTINUE;
149 state->mode = TYPEDO;
150 /* Break if operands are exhausted, otherwise continue looping */
151 return (cc == DFLTCC_CC_OP1_TOO_SHORT || cc == DFLTCC_CC_OP2_TOO_SHORT) ?
152 DFLTCC_INFLATE_BREAK : DFLTCC_INFLATE_CONTINUE;
154 EXPORT_SYMBOL(dfltcc_inflate);