dfltcc_deflate.c 9.1 KB

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