dfltcc_deflate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* dfltcc_deflate.c - IBM Z DEFLATE CONVERSION CALL compression support. */
  2. /*
  3. Use the following commands to build zlib-ng with DFLTCC compression support:
  4. $ ./configure --with-dfltcc-deflate
  5. or
  6. $ cmake -DWITH_DFLTCC_DEFLATE=1 .
  7. and then
  8. $ make
  9. */
  10. #include "zbuild.h"
  11. #include "deflate.h"
  12. #include "trees_emit.h"
  13. #include "dfltcc_deflate.h"
  14. #include "dfltcc_detail.h"
  15. void Z_INTERNAL PREFIX(dfltcc_reset_deflate_state)(PREFIX3(streamp) strm) {
  16. deflate_state *state = (deflate_state *)strm->state;
  17. arch_deflate_state *dfltcc_state = &state->arch;
  18. dfltcc_reset_state(&dfltcc_state->common);
  19. /* Initialize tuning parameters */
  20. dfltcc_state->level_mask = DFLTCC_LEVEL_MASK;
  21. dfltcc_state->block_size = DFLTCC_BLOCK_SIZE;
  22. dfltcc_state->block_threshold = DFLTCC_FIRST_FHT_BLOCK_SIZE;
  23. dfltcc_state->dht_threshold = DFLTCC_DHT_MIN_SAMPLE_SIZE;
  24. }
  25. static inline int dfltcc_can_deflate_with_params(PREFIX3(streamp) strm, int level, uInt window_bits, int strategy,
  26. int reproducible) {
  27. deflate_state *state = (deflate_state *)strm->state;
  28. arch_deflate_state *dfltcc_state = &state->arch;
  29. /* Unsupported compression settings */
  30. if ((dfltcc_state->level_mask & (1 << level)) == 0)
  31. return 0;
  32. if (window_bits != HB_BITS)
  33. return 0;
  34. if (strategy != Z_FIXED && strategy != Z_DEFAULT_STRATEGY)
  35. return 0;
  36. if (reproducible)
  37. return 0;
  38. /* Unsupported hardware */
  39. if (!is_bit_set(dfltcc_state->common.af.fns, DFLTCC_GDHT) ||
  40. !is_bit_set(dfltcc_state->common.af.fns, DFLTCC_CMPR) ||
  41. !is_bit_set(dfltcc_state->common.af.fmts, DFLTCC_FMT0))
  42. return 0;
  43. return 1;
  44. }
  45. int Z_INTERNAL PREFIX(dfltcc_can_deflate)(PREFIX3(streamp) strm) {
  46. deflate_state *state = (deflate_state *)strm->state;
  47. return dfltcc_can_deflate_with_params(strm, state->level, state->w_bits, state->strategy, state->reproducible);
  48. }
  49. static inline void dfltcc_gdht(PREFIX3(streamp) strm) {
  50. deflate_state *state = (deflate_state *)strm->state;
  51. struct dfltcc_param_v0 *param = &state->arch.common.param;
  52. size_t avail_in = strm->avail_in;
  53. dfltcc(DFLTCC_GDHT, param, NULL, NULL, &strm->next_in, &avail_in, NULL);
  54. }
  55. static inline dfltcc_cc dfltcc_cmpr(PREFIX3(streamp) strm) {
  56. deflate_state *state = (deflate_state *)strm->state;
  57. struct dfltcc_param_v0 *param = &state->arch.common.param;
  58. size_t avail_in = strm->avail_in;
  59. size_t avail_out = strm->avail_out;
  60. dfltcc_cc cc;
  61. cc = dfltcc(DFLTCC_CMPR | HBT_CIRCULAR,
  62. param, &strm->next_out, &avail_out,
  63. &strm->next_in, &avail_in, state->window);
  64. strm->total_in += (strm->avail_in - avail_in);
  65. strm->total_out += (strm->avail_out - avail_out);
  66. strm->avail_in = avail_in;
  67. strm->avail_out = avail_out;
  68. return cc;
  69. }
  70. static inline void send_eobs(PREFIX3(streamp) strm, const struct dfltcc_param_v0 *param) {
  71. deflate_state *state = (deflate_state *)strm->state;
  72. send_bits(state, PREFIX(bi_reverse)(param->eobs >> (15 - param->eobl), param->eobl), param->eobl, state->bi_buf, state->bi_valid);
  73. PREFIX(flush_pending)(strm);
  74. if (state->pending != 0) {
  75. /* The remaining data is located in pending_out[0:pending]. If someone
  76. * calls put_byte() - this might happen in deflate() - the byte will be
  77. * placed into pending_buf[pending], which is incorrect. Move the
  78. * remaining data to the beginning of pending_buf so that put_byte() is
  79. * usable again.
  80. */
  81. memmove(state->pending_buf, state->pending_out, state->pending);
  82. state->pending_out = state->pending_buf;
  83. }
  84. #ifdef ZLIB_DEBUG
  85. state->compressed_len += param->eobl;
  86. #endif
  87. }
  88. int Z_INTERNAL PREFIX(dfltcc_deflate)(PREFIX3(streamp) strm, int flush, block_state *result) {
  89. deflate_state *state = (deflate_state *)strm->state;
  90. arch_deflate_state *dfltcc_state = &state->arch;
  91. struct dfltcc_param_v0 *param = &dfltcc_state->common.param;
  92. uInt masked_avail_in;
  93. dfltcc_cc cc;
  94. int need_empty_block;
  95. int soft_bcc;
  96. int no_flush;
  97. if (!PREFIX(dfltcc_can_deflate)(strm)) {
  98. /* Clear history. */
  99. if (flush == Z_FULL_FLUSH)
  100. param->hl = 0;
  101. return 0;
  102. }
  103. again:
  104. masked_avail_in = 0;
  105. soft_bcc = 0;
  106. no_flush = flush == Z_NO_FLUSH;
  107. /* No input data. Return, except when Continuation Flag is set, which means
  108. * that DFLTCC has buffered some output in the parameter block and needs to
  109. * be called again in order to flush it.
  110. */
  111. if (strm->avail_in == 0 && !param->cf) {
  112. /* A block is still open, and the hardware does not support closing
  113. * blocks without adding data. Thus, close it manually.
  114. */
  115. if (!no_flush && param->bcf) {
  116. send_eobs(strm, param);
  117. param->bcf = 0;
  118. }
  119. /* Let one of deflate_* functions write a trailing empty block. */
  120. if (flush == Z_FINISH)
  121. return 0;
  122. /* Clear history. */
  123. if (flush == Z_FULL_FLUSH)
  124. param->hl = 0;
  125. /* Trigger block post-processing if necessary. */
  126. *result = no_flush ? need_more : block_done;
  127. return 1;
  128. }
  129. /* There is an open non-BFINAL block, we are not going to close it just
  130. * yet, we have compressed more than DFLTCC_BLOCK_SIZE bytes and we see
  131. * more than DFLTCC_DHT_MIN_SAMPLE_SIZE bytes. Open a new block with a new
  132. * DHT in order to adapt to a possibly changed input data distribution.
  133. */
  134. if (param->bcf && no_flush &&
  135. strm->total_in > dfltcc_state->block_threshold &&
  136. strm->avail_in >= dfltcc_state->dht_threshold) {
  137. if (param->cf) {
  138. /* We need to flush the DFLTCC buffer before writing the
  139. * End-of-block Symbol. Mask the input data and proceed as usual.
  140. */
  141. masked_avail_in += strm->avail_in;
  142. strm->avail_in = 0;
  143. no_flush = 0;
  144. } else {
  145. /* DFLTCC buffer is empty, so we can manually write the
  146. * End-of-block Symbol right away.
  147. */
  148. send_eobs(strm, param);
  149. param->bcf = 0;
  150. dfltcc_state->block_threshold = strm->total_in + dfltcc_state->block_size;
  151. }
  152. }
  153. /* No space for compressed data. If we proceed, dfltcc_cmpr() will return
  154. * DFLTCC_CC_OP1_TOO_SHORT without buffering header bits, but we will still
  155. * set BCF=1, which is wrong. Avoid complications and return early.
  156. */
  157. if (strm->avail_out == 0) {
  158. *result = need_more;
  159. return 1;
  160. }
  161. /* The caller gave us too much data. Pass only one block worth of
  162. * uncompressed data to DFLTCC and mask the rest, so that on the next
  163. * iteration we start a new block.
  164. */
  165. if (no_flush && strm->avail_in > dfltcc_state->block_size) {
  166. masked_avail_in += (strm->avail_in - dfltcc_state->block_size);
  167. strm->avail_in = dfltcc_state->block_size;
  168. }
  169. /* When we have an open non-BFINAL deflate block and caller indicates that
  170. * the stream is ending, we need to close an open deflate block and open a
  171. * BFINAL one.
  172. */
  173. need_empty_block = flush == Z_FINISH && param->bcf && !param->bhf;
  174. /* Translate stream to parameter block */
  175. param->cvt = state->wrap == 2 ? CVT_CRC32 : CVT_ADLER32;
  176. if (!no_flush)
  177. /* We need to close a block. Always do this in software - when there is
  178. * no input data, the hardware will not honor BCC. */
  179. soft_bcc = 1;
  180. if (flush == Z_FINISH && !param->bcf)
  181. /* We are about to open a BFINAL block, set Block Header Final bit
  182. * until the stream ends.
  183. */
  184. param->bhf = 1;
  185. /* DFLTCC-CMPR will write to next_out, so make sure that buffers with
  186. * higher precedence are empty.
  187. */
  188. Assert(state->pending == 0, "There must be no pending bytes");
  189. Assert(state->bi_valid < 8, "There must be less than 8 pending bits");
  190. param->sbb = (unsigned int)state->bi_valid;
  191. if (param->sbb > 0)
  192. *strm->next_out = (unsigned char)state->bi_buf;
  193. /* Honor history and check value */
  194. param->nt = 0;
  195. if (state->wrap == 1)
  196. param->cv = strm->adler;
  197. else if (state->wrap == 2)
  198. param->cv = ZSWAP32(state->crc_fold.value);
  199. /* When opening a block, choose a Huffman-Table Type */
  200. if (!param->bcf) {
  201. if (state->strategy == Z_FIXED || (strm->total_in == 0 && dfltcc_state->block_threshold > 0))
  202. param->htt = HTT_FIXED;
  203. else {
  204. param->htt = HTT_DYNAMIC;
  205. dfltcc_gdht(strm);
  206. }
  207. }
  208. /* Deflate */
  209. do {
  210. cc = dfltcc_cmpr(strm);
  211. if (strm->avail_in < 4096 && masked_avail_in > 0)
  212. /* We are about to call DFLTCC with a small input buffer, which is
  213. * inefficient. Since there is masked data, there will be at least
  214. * one more DFLTCC call, so skip the current one and make the next
  215. * one handle more data.
  216. */
  217. break;
  218. } while (cc == DFLTCC_CC_AGAIN);
  219. /* Translate parameter block to stream */
  220. strm->msg = oesc_msg(dfltcc_state->common.msg, param->oesc);
  221. state->bi_valid = param->sbb;
  222. if (state->bi_valid == 0)
  223. state->bi_buf = 0; /* Avoid accessing next_out */
  224. else
  225. state->bi_buf = *strm->next_out & ((1 << state->bi_valid) - 1);
  226. if (state->wrap == 1)
  227. strm->adler = param->cv;
  228. else if (state->wrap == 2)
  229. state->crc_fold.value = ZSWAP32(param->cv);
  230. /* Unmask the input data */
  231. strm->avail_in += masked_avail_in;
  232. masked_avail_in = 0;
  233. /* If we encounter an error, it means there is a bug in DFLTCC call */
  234. Assert(cc != DFLTCC_CC_OP2_CORRUPT || param->oesc == 0, "BUG");
  235. /* Update Block-Continuation Flag. It will be used to check whether to call
  236. * GDHT the next time.
  237. */
  238. if (cc == DFLTCC_CC_OK) {
  239. if (soft_bcc) {
  240. send_eobs(strm, param);
  241. param->bcf = 0;
  242. dfltcc_state->block_threshold = strm->total_in + dfltcc_state->block_size;
  243. } else
  244. param->bcf = 1;
  245. if (flush == Z_FINISH) {
  246. if (need_empty_block)
  247. /* Make the current deflate() call also close the stream */
  248. return 0;
  249. else {
  250. bi_windup(state);
  251. *result = finish_done;
  252. }
  253. } else {
  254. if (flush == Z_FULL_FLUSH)
  255. param->hl = 0; /* Clear history */
  256. *result = flush == Z_NO_FLUSH ? need_more : block_done;
  257. }
  258. } else {
  259. param->bcf = 1;
  260. *result = need_more;
  261. }
  262. if (strm->avail_in != 0 && strm->avail_out != 0)
  263. goto again; /* deflate() must use all input or all output */
  264. return 1;
  265. }
  266. /*
  267. Switching between hardware and software compression.
  268. DFLTCC does not support all zlib settings, e.g. generation of non-compressed
  269. blocks or alternative window sizes. When such settings are applied on the
  270. fly with deflateParams, we need to convert between hardware and software
  271. window formats.
  272. */
  273. static int dfltcc_was_deflate_used(PREFIX3(streamp) strm) {
  274. deflate_state *state = (deflate_state *)strm->state;
  275. struct dfltcc_param_v0 *param = &state->arch.common.param;
  276. return strm->total_in > 0 || param->nt == 0 || param->hl > 0;
  277. }
  278. int Z_INTERNAL PREFIX(dfltcc_deflate_params)(PREFIX3(streamp) strm, int level, int strategy, int *flush) {
  279. deflate_state *state = (deflate_state *)strm->state;
  280. int could_deflate = PREFIX(dfltcc_can_deflate)(strm);
  281. int can_deflate = dfltcc_can_deflate_with_params(strm, level, state->w_bits, strategy, state->reproducible);
  282. if (can_deflate == could_deflate)
  283. /* We continue to work in the same mode - no changes needed */
  284. return Z_OK;
  285. if (!dfltcc_was_deflate_used(strm))
  286. /* DFLTCC was not used yet - no changes needed */
  287. return Z_OK;
  288. /* For now, do not convert between window formats - simply get rid of the old data instead */
  289. *flush = Z_FULL_FLUSH;
  290. return Z_OK;
  291. }
  292. int Z_INTERNAL PREFIX(dfltcc_deflate_done)(PREFIX3(streamp) strm, int flush) {
  293. deflate_state *state = (deflate_state *)strm->state;
  294. struct dfltcc_param_v0 *param = &state->arch.common.param;
  295. /* When deflate(Z_FULL_FLUSH) is called with small avail_out, it might
  296. * close the block without resetting the compression state. Detect this
  297. * situation and return that deflation is not done.
  298. */
  299. if (flush == Z_FULL_FLUSH && strm->avail_out == 0)
  300. return 0;
  301. /* Return that deflation is not done if DFLTCC is used and either it
  302. * buffered some data (Continuation Flag is set), or has not written EOBS
  303. * yet (Block-Continuation Flag is set).
  304. */
  305. return !PREFIX(dfltcc_can_deflate)(strm) || (!param->cf && !param->bcf);
  306. }
  307. int Z_INTERNAL PREFIX(dfltcc_can_set_reproducible)(PREFIX3(streamp) strm, int reproducible) {
  308. deflate_state *state = (deflate_state *)strm->state;
  309. return reproducible != state->reproducible && !dfltcc_was_deflate_used(strm);
  310. }
  311. /*
  312. Preloading history.
  313. */
  314. int Z_INTERNAL PREFIX(dfltcc_deflate_set_dictionary)(PREFIX3(streamp) strm,
  315. const unsigned char *dictionary, uInt dict_length) {
  316. deflate_state *state = (deflate_state *)strm->state;
  317. struct dfltcc_param_v0 *param = &state->arch.common.param;
  318. append_history(param, state->window, dictionary, dict_length);
  319. state->strstart = 1; /* Add FDICT to zlib header */
  320. state->block_start = state->strstart; /* Make deflate_stored happy */
  321. return Z_OK;
  322. }
  323. int Z_INTERNAL PREFIX(dfltcc_deflate_get_dictionary)(PREFIX3(streamp) strm, unsigned char *dictionary, uInt *dict_length) {
  324. deflate_state *state = (deflate_state *)strm->state;
  325. struct dfltcc_param_v0 *param = &state->arch.common.param;
  326. if (dictionary)
  327. get_history(param, state->window, dictionary);
  328. if (dict_length)
  329. *dict_length = param->hl;
  330. return Z_OK;
  331. }