cryptodeflate.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
  2. /*-
  3. * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*
  29. * This file contains a wrapper around the deflate algo compression
  30. * functions using the zlib library (see sys/contrib/zlib)
  31. */
  32. #include <sys/cdefs.h>
  33. __FBSDID("$FreeBSD$");
  34. #include <sys/types.h>
  35. #include <sys/param.h>
  36. #include <sys/malloc.h>
  37. #include <sys/param.h>
  38. #include <sys/kernel.h>
  39. #include <sys/sdt.h>
  40. #include <sys/systm.h>
  41. #include <contrib/zlib/zlib.h>
  42. #include <opencrypto/cryptodev.h>
  43. #include <opencrypto/deflate.h>
  44. SDT_PROVIDER_DECLARE(opencrypto);
  45. SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, entry,
  46. "int", "uint32_t");
  47. SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, bad,
  48. "int", "int", "int", "int", "int", "int");
  49. SDT_PROBE_DEFINE6(opencrypto, deflate, deflate_global, iter,
  50. "int", "int", "int", "int", "int", "int");
  51. SDT_PROBE_DEFINE2(opencrypto, deflate, deflate_global, return,
  52. "int", "uint32_t");
  53. int window_inflate = -1 * MAX_WBITS;
  54. int window_deflate = -12;
  55. static void *
  56. crypto_zalloc(void *nil, u_int type, u_int size)
  57. {
  58. void *ptr;
  59. ptr = malloc(type *size, M_CRYPTO_DATA, M_NOWAIT);
  60. return ptr;
  61. }
  62. static void
  63. crypto_zfree(void *nil, void *ptr)
  64. {
  65. free(ptr, M_CRYPTO_DATA);
  66. }
  67. /*
  68. * This function takes a block of data and (de)compress it using the deflate
  69. * algorithm
  70. */
  71. uint32_t
  72. deflate_global(uint8_t *data, uint32_t size, int decomp, uint8_t **out)
  73. {
  74. /* decomp indicates whether we compress (0) or decompress (1) */
  75. z_stream zbuf;
  76. uint8_t *output;
  77. uint32_t count, result;
  78. int error, i;
  79. struct deflate_buf *bufh, *bufp;
  80. SDT_PROBE2(opencrypto, deflate, deflate_global, entry, decomp, size);
  81. bufh = bufp = NULL;
  82. if (!decomp) {
  83. i = 1;
  84. } else {
  85. /*
  86. * Choose a buffer with 4x the size of the input buffer
  87. * for the size of the output buffer in the case of
  88. * decompression. If it's not sufficient, it will need to be
  89. * updated while the decompression is going on.
  90. */
  91. i = 4;
  92. }
  93. /*
  94. * Make sure we do have enough output space. Repeated calls to
  95. * deflate need at least 6 bytes of output buffer space to avoid
  96. * repeated markers. We will always provide at least 16 bytes.
  97. */
  98. while ((size * i) < 16)
  99. i++;
  100. bufh = bufp = malloc(sizeof(*bufp) + (size_t)(size * i),
  101. M_CRYPTO_DATA, M_NOWAIT);
  102. if (bufp == NULL) {
  103. SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
  104. decomp, 0, __LINE__, 0, 0, 0);
  105. goto bad2;
  106. }
  107. bufp->next = NULL;
  108. bufp->size = size * i;
  109. bzero(&zbuf, sizeof(z_stream));
  110. zbuf.zalloc = crypto_zalloc;
  111. zbuf.zfree = crypto_zfree;
  112. zbuf.opaque = Z_NULL;
  113. zbuf.next_in = data; /* Data that is going to be processed. */
  114. zbuf.avail_in = size; /* Total length of data to be processed. */
  115. zbuf.next_out = bufp->data;
  116. zbuf.avail_out = bufp->size;
  117. error = decomp ? inflateInit2(&zbuf, window_inflate) :
  118. deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
  119. window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
  120. if (error != Z_OK) {
  121. SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
  122. decomp, error, __LINE__, 0, 0, 0);
  123. goto bad;
  124. }
  125. for (;;) {
  126. error = decomp ? inflate(&zbuf, Z_SYNC_FLUSH) :
  127. deflate(&zbuf, Z_FINISH);
  128. if (error != Z_OK && error != Z_STREAM_END) {
  129. SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
  130. decomp, error, __LINE__,
  131. zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
  132. goto bad;
  133. }
  134. SDT_PROBE6(opencrypto, deflate, deflate_global, iter,
  135. decomp, error, __LINE__,
  136. zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
  137. if (decomp && zbuf.avail_in == 0 && error == Z_STREAM_END) {
  138. /* Done. */
  139. break;
  140. } else if (!decomp && error == Z_STREAM_END) {
  141. /* Done. */
  142. break;
  143. } else if (zbuf.avail_out == 0) {
  144. struct deflate_buf *p;
  145. /* We need more output space for another iteration. */
  146. p = malloc(sizeof(*p) + (size_t)(size * i),
  147. M_CRYPTO_DATA, M_NOWAIT);
  148. if (p == NULL) {
  149. SDT_PROBE6(opencrypto, deflate, deflate_global,
  150. bad, decomp, 0, __LINE__, 0, 0, 0);
  151. goto bad;
  152. }
  153. p->next = NULL;
  154. p->size = size * i;
  155. bufp->next = p;
  156. bufp = p;
  157. zbuf.next_out = bufp->data;
  158. zbuf.avail_out = bufp->size;
  159. } else {
  160. /* Unexpect result. */
  161. SDT_PROBE6(opencrypto, deflate, deflate_global,
  162. bad, decomp, error, __LINE__,
  163. zbuf.avail_in, zbuf.avail_out, zbuf.total_out);
  164. goto bad;
  165. }
  166. }
  167. result = count = zbuf.total_out;
  168. *out = malloc(result, M_CRYPTO_DATA, M_NOWAIT);
  169. if (*out == NULL) {
  170. SDT_PROBE6(opencrypto, deflate, deflate_global, bad,
  171. decomp, 0, __LINE__, 0, 0, 0);
  172. goto bad;
  173. }
  174. if (decomp)
  175. inflateEnd(&zbuf);
  176. else
  177. deflateEnd(&zbuf);
  178. output = *out;
  179. for (bufp = bufh; bufp != NULL; ) {
  180. if (count > bufp->size) {
  181. struct deflate_buf *p;
  182. bcopy(bufp->data, *out, bufp->size);
  183. *out += bufp->size;
  184. count -= bufp->size;
  185. p = bufp;
  186. bufp = bufp->next;
  187. free(p, M_CRYPTO_DATA);
  188. } else {
  189. /* It should be the last buffer. */
  190. bcopy(bufp->data, *out, count);
  191. *out += count;
  192. free(bufp, M_CRYPTO_DATA);
  193. bufp = NULL;
  194. count = 0;
  195. }
  196. }
  197. *out = output;
  198. SDT_PROBE2(opencrypto, deflate, deflate_global, return, decomp, result);
  199. return result;
  200. bad:
  201. if (decomp)
  202. inflateEnd(&zbuf);
  203. else
  204. deflateEnd(&zbuf);
  205. for (bufp = bufh; bufp != NULL; ) {
  206. struct deflate_buf *p;
  207. p = bufp;
  208. bufp = bufp->next;
  209. free(p, M_CRYPTO_DATA);
  210. }
  211. bad2:
  212. *out = NULL;
  213. return 0;
  214. }