dfltcc_deflate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: Zlib
  2. #include "../zlib_deflate/defutil.h"
  3. #include "dfltcc_util.h"
  4. #include "dfltcc_deflate.h"
  5. #include <asm/setup.h>
  6. #include <linux/export.h>
  7. #include <linux/zutil.h>
  8. #define GET_DFLTCC_DEFLATE_STATE(state) ((struct dfltcc_deflate_state *)GET_DFLTCC_STATE(state))
  9. /*
  10. * Compress.
  11. */
  12. int dfltcc_can_deflate(
  13. z_streamp strm
  14. )
  15. {
  16. deflate_state *state = (deflate_state *)strm->state;
  17. struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state);
  18. /* Check for kernel dfltcc command line parameter */
  19. if (zlib_dfltcc_support == ZLIB_DFLTCC_DISABLED ||
  20. zlib_dfltcc_support == ZLIB_DFLTCC_INFLATE_ONLY)
  21. return 0;
  22. /* Unsupported compression settings */
  23. if (!dfltcc_are_params_ok(state->level, state->w_bits, state->strategy,
  24. dfltcc_state->level_mask))
  25. return 0;
  26. /* Unsupported hardware */
  27. if (!is_bit_set(dfltcc_state->common.af.fns, DFLTCC_GDHT) ||
  28. !is_bit_set(dfltcc_state->common.af.fns, DFLTCC_CMPR) ||
  29. !is_bit_set(dfltcc_state->common.af.fmts, DFLTCC_FMT0))
  30. return 0;
  31. return 1;
  32. }
  33. EXPORT_SYMBOL(dfltcc_can_deflate);
  34. void dfltcc_reset_deflate_state(z_streamp strm) {
  35. deflate_state *state = (deflate_state *)strm->state;
  36. struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state);
  37. dfltcc_reset_state(&dfltcc_state->common);
  38. /* Initialize tuning parameters */
  39. if (zlib_dfltcc_support == ZLIB_DFLTCC_FULL_DEBUG)
  40. dfltcc_state->level_mask = DFLTCC_LEVEL_MASK_DEBUG;
  41. else
  42. dfltcc_state->level_mask = DFLTCC_LEVEL_MASK;
  43. dfltcc_state->block_size = DFLTCC_BLOCK_SIZE;
  44. dfltcc_state->block_threshold = DFLTCC_FIRST_FHT_BLOCK_SIZE;
  45. dfltcc_state->dht_threshold = DFLTCC_DHT_MIN_SAMPLE_SIZE;
  46. }
  47. EXPORT_SYMBOL(dfltcc_reset_deflate_state);
  48. static void dfltcc_gdht(
  49. z_streamp strm
  50. )
  51. {
  52. deflate_state *state = (deflate_state *)strm->state;
  53. struct dfltcc_param_v0 *param = &GET_DFLTCC_STATE(state)->param;
  54. size_t avail_in = strm->avail_in;
  55. dfltcc(DFLTCC_GDHT,
  56. param, NULL, NULL,
  57. &strm->next_in, &avail_in, NULL);
  58. }
  59. static dfltcc_cc dfltcc_cmpr(
  60. z_streamp strm
  61. )
  62. {
  63. deflate_state *state = (deflate_state *)strm->state;
  64. struct dfltcc_param_v0 *param = &GET_DFLTCC_STATE(state)->param;
  65. size_t avail_in = strm->avail_in;
  66. size_t avail_out = strm->avail_out;
  67. dfltcc_cc cc;
  68. cc = dfltcc(DFLTCC_CMPR | HBT_CIRCULAR,
  69. param, &strm->next_out, &avail_out,
  70. &strm->next_in, &avail_in, state->window);
  71. strm->total_in += (strm->avail_in - avail_in);
  72. strm->total_out += (strm->avail_out - avail_out);
  73. strm->avail_in = avail_in;
  74. strm->avail_out = avail_out;
  75. return cc;
  76. }
  77. static void send_eobs(
  78. z_streamp strm,
  79. const struct dfltcc_param_v0 *param
  80. )
  81. {
  82. deflate_state *state = (deflate_state *)strm->state;
  83. zlib_tr_send_bits(
  84. state,
  85. bi_reverse(param->eobs >> (15 - param->eobl), param->eobl),
  86. param->eobl);
  87. flush_pending(strm);
  88. if (state->pending != 0) {
  89. /* The remaining data is located in pending_out[0:pending]. If someone
  90. * calls put_byte() - this might happen in deflate() - the byte will be
  91. * placed into pending_buf[pending], which is incorrect. Move the
  92. * remaining data to the beginning of pending_buf so that put_byte() is
  93. * usable again.
  94. */
  95. memmove(state->pending_buf, state->pending_out, state->pending);
  96. state->pending_out = state->pending_buf;
  97. }
  98. #ifdef ZLIB_DEBUG
  99. state->compressed_len += param->eobl;
  100. #endif
  101. }
  102. int dfltcc_deflate(
  103. z_streamp strm,
  104. int flush,
  105. block_state *result
  106. )
  107. {
  108. deflate_state *state = (deflate_state *)strm->state;
  109. struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state);
  110. struct dfltcc_param_v0 *param = &dfltcc_state->common.param;
  111. uInt masked_avail_in;
  112. dfltcc_cc cc;
  113. int need_empty_block;
  114. int soft_bcc;
  115. int no_flush;
  116. if (!dfltcc_can_deflate(strm)) {
  117. /* Clear history. */
  118. if (flush == Z_FULL_FLUSH)
  119. param->hl = 0;
  120. return 0;
  121. }
  122. again:
  123. masked_avail_in = 0;
  124. soft_bcc = 0;
  125. no_flush = flush == Z_NO_FLUSH;
  126. /* No input data. Return, except when Continuation Flag is set, which means
  127. * that DFLTCC has buffered some output in the parameter block and needs to
  128. * be called again in order to flush it.
  129. */
  130. if (strm->avail_in == 0 && !param->cf) {
  131. /* A block is still open, and the hardware does not support closing
  132. * blocks without adding data. Thus, close it manually.
  133. */
  134. if (!no_flush && param->bcf) {
  135. send_eobs(strm, param);
  136. param->bcf = 0;
  137. }
  138. /* Let one of deflate_* functions write a trailing empty block. */
  139. if (flush == Z_FINISH)
  140. return 0;
  141. /* Clear history. */
  142. if (flush == Z_FULL_FLUSH)
  143. param->hl = 0;
  144. /* Trigger block post-processing if necessary. */
  145. *result = no_flush ? need_more : block_done;
  146. return 1;
  147. }
  148. /* There is an open non-BFINAL block, we are not going to close it just
  149. * yet, we have compressed more than DFLTCC_BLOCK_SIZE bytes and we see
  150. * more than DFLTCC_DHT_MIN_SAMPLE_SIZE bytes. Open a new block with a new
  151. * DHT in order to adapt to a possibly changed input data distribution.
  152. */
  153. if (param->bcf && no_flush &&
  154. strm->total_in > dfltcc_state->block_threshold &&
  155. strm->avail_in >= dfltcc_state->dht_threshold) {
  156. if (param->cf) {
  157. /* We need to flush the DFLTCC buffer before writing the
  158. * End-of-block Symbol. Mask the input data and proceed as usual.
  159. */
  160. masked_avail_in += strm->avail_in;
  161. strm->avail_in = 0;
  162. no_flush = 0;
  163. } else {
  164. /* DFLTCC buffer is empty, so we can manually write the
  165. * End-of-block Symbol right away.
  166. */
  167. send_eobs(strm, param);
  168. param->bcf = 0;
  169. dfltcc_state->block_threshold =
  170. strm->total_in + dfltcc_state->block_size;
  171. }
  172. }
  173. /* No space for compressed data. If we proceed, dfltcc_cmpr() will return
  174. * DFLTCC_CC_OP1_TOO_SHORT without buffering header bits, but we will still
  175. * set BCF=1, which is wrong. Avoid complications and return early.
  176. */
  177. if (strm->avail_out == 0) {
  178. *result = need_more;
  179. return 1;
  180. }
  181. /* The caller gave us too much data. Pass only one block worth of
  182. * uncompressed data to DFLTCC and mask the rest, so that on the next
  183. * iteration we start a new block.
  184. */
  185. if (no_flush && strm->avail_in > dfltcc_state->block_size) {
  186. masked_avail_in += (strm->avail_in - dfltcc_state->block_size);
  187. strm->avail_in = dfltcc_state->block_size;
  188. }
  189. /* When we have an open non-BFINAL deflate block and caller indicates that
  190. * the stream is ending, we need to close an open deflate block and open a
  191. * BFINAL one.
  192. */
  193. need_empty_block = flush == Z_FINISH && param->bcf && !param->bhf;
  194. /* Translate stream to parameter block */
  195. param->cvt = CVT_ADLER32;
  196. if (!no_flush)
  197. /* We need to close a block. Always do this in software - when there is
  198. * no input data, the hardware will not hohor BCC. */
  199. soft_bcc = 1;
  200. if (flush == Z_FINISH && !param->bcf)
  201. /* We are about to open a BFINAL block, set Block Header Final bit
  202. * until the stream ends.
  203. */
  204. param->bhf = 1;
  205. /* DFLTCC-CMPR will write to next_out, so make sure that buffers with
  206. * higher precedence are empty.
  207. */
  208. Assert(state->pending == 0, "There must be no pending bytes");
  209. Assert(state->bi_valid < 8, "There must be less than 8 pending bits");
  210. param->sbb = (unsigned int)state->bi_valid;
  211. if (param->sbb > 0)
  212. *strm->next_out = (Byte)state->bi_buf;
  213. /* Honor history and check value */
  214. param->nt = 0;
  215. param->cv = strm->adler;
  216. /* When opening a block, choose a Huffman-Table Type */
  217. if (!param->bcf) {
  218. if (strm->total_in == 0 && dfltcc_state->block_threshold > 0) {
  219. param->htt = HTT_FIXED;
  220. }
  221. else {
  222. param->htt = HTT_DYNAMIC;
  223. dfltcc_gdht(strm);
  224. }
  225. }
  226. /* Deflate */
  227. do {
  228. cc = dfltcc_cmpr(strm);
  229. if (strm->avail_in < 4096 && masked_avail_in > 0)
  230. /* We are about to call DFLTCC with a small input buffer, which is
  231. * inefficient. Since there is masked data, there will be at least
  232. * one more DFLTCC call, so skip the current one and make the next
  233. * one handle more data.
  234. */
  235. break;
  236. } while (cc == DFLTCC_CC_AGAIN);
  237. /* Translate parameter block to stream */
  238. strm->msg = oesc_msg(dfltcc_state->common.msg, param->oesc);
  239. state->bi_valid = param->sbb;
  240. if (state->bi_valid == 0)
  241. state->bi_buf = 0; /* Avoid accessing next_out */
  242. else
  243. state->bi_buf = *strm->next_out & ((1 << state->bi_valid) - 1);
  244. strm->adler = param->cv;
  245. /* Unmask the input data */
  246. strm->avail_in += masked_avail_in;
  247. masked_avail_in = 0;
  248. /* If we encounter an error, it means there is a bug in DFLTCC call */
  249. Assert(cc != DFLTCC_CC_OP2_CORRUPT || param->oesc == 0, "BUG");
  250. /* Update Block-Continuation Flag. It will be used to check whether to call
  251. * GDHT the next time.
  252. */
  253. if (cc == DFLTCC_CC_OK) {
  254. if (soft_bcc) {
  255. send_eobs(strm, param);
  256. param->bcf = 0;
  257. dfltcc_state->block_threshold =
  258. strm->total_in + dfltcc_state->block_size;
  259. } else
  260. param->bcf = 1;
  261. if (flush == Z_FINISH) {
  262. if (need_empty_block)
  263. /* Make the current deflate() call also close the stream */
  264. return 0;
  265. else {
  266. bi_windup(state);
  267. *result = finish_done;
  268. }
  269. } else {
  270. if (flush == Z_FULL_FLUSH)
  271. param->hl = 0; /* Clear history */
  272. *result = flush == Z_NO_FLUSH ? need_more : block_done;
  273. }
  274. } else {
  275. param->bcf = 1;
  276. *result = need_more;
  277. }
  278. if (strm->avail_in != 0 && strm->avail_out != 0)
  279. goto again; /* deflate() must use all input or all output */
  280. return 1;
  281. }
  282. EXPORT_SYMBOL(dfltcc_deflate);