compress_zstd.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Copyright (c) 2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file compress_zstd.c
  7. * \brief Compression backend for Zstandard.
  8. *
  9. * This module should never be invoked directly. Use the compress module
  10. * instead.
  11. **/
  12. #include "orconfig.h"
  13. #include "util.h"
  14. #include "torlog.h"
  15. #include "compress.h"
  16. #include "compress_zstd.h"
  17. #ifdef HAVE_ZSTD
  18. #include <zstd.h>
  19. #include <zstd_errors.h>
  20. #endif
  21. /** Total number of bytes allocated for Zstandard state. */
  22. static size_t total_zstd_allocation = 0;
  23. #ifdef HAVE_ZSTD
  24. /** Given <b>level</b> return the memory level. */
  25. static int
  26. memory_level(compression_level_t level)
  27. {
  28. switch (level) {
  29. default:
  30. case HIGH_COMPRESSION: return 9;
  31. case MEDIUM_COMPRESSION: return 8;
  32. case LOW_COMPRESSION: return 7;
  33. }
  34. }
  35. #endif // HAVE_ZSTD.
  36. /** Return 1 if Zstandard compression is supported; otherwise 0. */
  37. int
  38. tor_zstd_method_supported(void)
  39. {
  40. #ifdef HAVE_ZSTD
  41. return 1;
  42. #else
  43. return 0;
  44. #endif
  45. }
  46. /** Return a string representation of the version of the currently running
  47. * version of libzstd. Returns NULL if Zstandard is unsupported. */
  48. const char *
  49. tor_zstd_get_version_str(void)
  50. {
  51. #ifdef HAVE_ZSTD
  52. static char version_str[16];
  53. size_t version_number;
  54. version_number = ZSTD_versionNumber();
  55. tor_snprintf(version_str, sizeof(version_str),
  56. "%lu.%lu.%lu",
  57. version_number / 10000 % 100,
  58. version_number / 100 % 100,
  59. version_number % 100);
  60. return version_str;
  61. #else
  62. return NULL;
  63. #endif
  64. }
  65. /** Return a string representation of the version of the version of libzstd
  66. * used at compilation time. Returns NULL if Zstandard is unsupported. */
  67. const char *
  68. tor_zstd_get_header_version_str(void)
  69. {
  70. #ifdef HAVE_ZSTD
  71. return ZSTD_VERSION_STRING;
  72. #else
  73. return NULL;
  74. #endif
  75. }
  76. /** Internal Zstandard state for incremental compression/decompression.
  77. * The body of this struct is not exposed. */
  78. struct tor_zstd_compress_state_t {
  79. #ifdef HAVE_ZSTD
  80. union {
  81. /** Compression stream. Used when <b>compress</b> is true. */
  82. ZSTD_CStream *compress_stream;
  83. /** Decompression stream. Used when <b>compress</b> is false. */
  84. ZSTD_DStream *decompress_stream;
  85. } u; /**< Zstandard stream objects. */
  86. #endif // HAVE_ZSTD.
  87. int compress; /**< True if we are compressing; false if we are inflating */
  88. /** Number of bytes read so far. Used to detect compression bombs. */
  89. size_t input_so_far;
  90. /** Number of bytes written so far. Used to detect compression bombs. */
  91. size_t output_so_far;
  92. /** Approximate number of bytes allocated for this object. */
  93. size_t allocation;
  94. };
  95. /** Construct and return a tor_zstd_compress_state_t object using
  96. * <b>method</b>. If <b>compress</b>, it's for compression; otherwise it's for
  97. * decompression. */
  98. tor_zstd_compress_state_t *
  99. tor_zstd_compress_new(int compress,
  100. compress_method_t method,
  101. compression_level_t compression_level)
  102. {
  103. tor_assert(method == ZSTD_METHOD);
  104. #ifdef HAVE_ZSTD
  105. tor_zstd_compress_state_t *result;
  106. size_t retval;
  107. result = tor_malloc_zero(sizeof(tor_zstd_compress_state_t));
  108. result->compress = compress;
  109. // FIXME(ahf): We should either try to do the pre-calculation that is done
  110. // with the zlib backend or use a custom allocator here where we pass our
  111. // tor_zstd_compress_state_t as the opaque value.
  112. result->allocation = 0;
  113. if (compress) {
  114. result->u.compress_stream = ZSTD_createCStream();
  115. if (result->u.compress_stream == NULL) {
  116. log_warn(LD_GENERAL, "Error while creating Zstandard stream");
  117. goto err;
  118. }
  119. retval = ZSTD_initCStream(result->u.compress_stream,
  120. memory_level(compression_level));
  121. if (ZSTD_isError(retval)) {
  122. log_warn(LD_GENERAL, "Zstandard stream initialization error: %s",
  123. ZSTD_getErrorName(retval));
  124. goto err;
  125. }
  126. } else {
  127. result->u.decompress_stream = ZSTD_createDStream();
  128. if (result->u.decompress_stream == NULL) {
  129. log_warn(LD_GENERAL, "Error while creating Zstandard stream");
  130. goto err;
  131. }
  132. retval = ZSTD_initDStream(result->u.decompress_stream);
  133. if (ZSTD_isError(retval)) {
  134. log_warn(LD_GENERAL, "Zstandard stream initialization error: %s",
  135. ZSTD_getErrorName(retval));
  136. goto err;
  137. }
  138. }
  139. return result;
  140. err:
  141. if (compress) {
  142. ZSTD_freeCStream(result->u.compress_stream);
  143. } else {
  144. ZSTD_freeDStream(result->u.decompress_stream);
  145. }
  146. tor_free(result);
  147. return NULL;
  148. #else // HAVE_ZSTD.
  149. (void)compress;
  150. (void)method;
  151. (void)compression_level;
  152. return NULL;
  153. #endif // HAVE_ZSTD.
  154. }
  155. /** Compress/decompress some bytes using <b>state</b>. Read up to
  156. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  157. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  158. * we've reached the end of the input.
  159. *
  160. * Return TOR_COMPRESS_DONE if we've finished the entire
  161. * compression/decompression.
  162. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  163. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  164. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  165. */
  166. tor_compress_output_t
  167. tor_zstd_compress_process(tor_zstd_compress_state_t *state,
  168. char **out, size_t *out_len,
  169. const char **in, size_t *in_len,
  170. int finish)
  171. {
  172. #ifdef HAVE_ZSTD
  173. size_t retval;
  174. tor_assert(state != NULL);
  175. tor_assert(*in_len <= UINT_MAX);
  176. tor_assert(*out_len <= UINT_MAX);
  177. ZSTD_inBuffer input = { *in, *in_len, 0 };
  178. ZSTD_outBuffer output = { *out, *out_len, 0 };
  179. if (state->compress) {
  180. retval = ZSTD_compressStream(state->u.compress_stream,
  181. &output, &input);
  182. } else {
  183. retval = ZSTD_decompressStream(state->u.decompress_stream,
  184. &output, &input);
  185. }
  186. state->input_so_far += input.pos;
  187. state->output_so_far += output.pos;
  188. *out = (char *)output.dst + output.pos;
  189. *out_len = output.size - output.pos;
  190. *in = (char *)input.src + input.pos;
  191. *in_len = input.size - input.pos;
  192. if (! state->compress &&
  193. tor_compress_is_compression_bomb(state->input_so_far,
  194. state->output_so_far)) {
  195. log_warn(LD_DIR, "Possible compression bomb; abandoning stream.");
  196. return TOR_COMPRESS_ERROR;
  197. }
  198. if (ZSTD_isError(retval)) {
  199. log_warn(LD_GENERAL, "Zstandard %s didn't finish: %s.",
  200. state->compress ? "compression" : "decompression",
  201. ZSTD_getErrorName(retval));
  202. return TOR_COMPRESS_ERROR;
  203. }
  204. if (state->compress && !finish) {
  205. retval = ZSTD_flushStream(state->u.compress_stream, &output);
  206. *out = (char *)output.dst + output.pos;
  207. *out_len = output.size - output.pos;
  208. if (ZSTD_isError(retval)) {
  209. log_warn(LD_GENERAL, "Zstandard compression unable to flush: %s.",
  210. ZSTD_getErrorName(retval));
  211. return TOR_COMPRESS_ERROR;
  212. }
  213. if (retval > 0)
  214. return TOR_COMPRESS_BUFFER_FULL;
  215. }
  216. if (state->compress && finish) {
  217. retval = ZSTD_endStream(state->u.compress_stream, &output);
  218. *out = (char *)output.dst + output.pos;
  219. *out_len = output.size - output.pos;
  220. if (ZSTD_isError(retval)) {
  221. log_warn(LD_GENERAL, "Zstandard compression unable to write "
  222. "epilogue: %s.",
  223. ZSTD_getErrorName(retval));
  224. return TOR_COMPRESS_ERROR;
  225. }
  226. // endStream returns the number of bytes that is needed to write the
  227. // epilogue.
  228. if (retval > 0)
  229. return TOR_COMPRESS_BUFFER_FULL;
  230. }
  231. return finish ? TOR_COMPRESS_DONE : TOR_COMPRESS_OK;
  232. #else // HAVE_ZSTD.
  233. (void)state;
  234. (void)out;
  235. (void)out_len;
  236. (void)in;
  237. (void)in_len;
  238. (void)finish;
  239. return TOR_COMPRESS_ERROR;
  240. #endif // HAVE_ZSTD.
  241. }
  242. /** Deallocate <b>state</b>. */
  243. void
  244. tor_zstd_compress_free(tor_zstd_compress_state_t *state)
  245. {
  246. if (state == NULL)
  247. return;
  248. total_zstd_allocation -= state->allocation;
  249. #ifdef HAVE_ZSTD
  250. if (state->compress) {
  251. ZSTD_freeCStream(state->u.compress_stream);
  252. } else {
  253. ZSTD_freeDStream(state->u.decompress_stream);
  254. }
  255. #endif // HAVE_ZSTD.
  256. tor_free(state);
  257. }
  258. /** Return the approximate number of bytes allocated for <b>state</b>. */
  259. size_t
  260. tor_zstd_compress_state_size(const tor_zstd_compress_state_t *state)
  261. {
  262. tor_assert(state != NULL);
  263. return state->allocation;
  264. }
  265. /** Return the approximate number of bytes allocated for all Zstandard
  266. * states. */
  267. size_t
  268. tor_zstd_get_total_allocation(void)
  269. {
  270. return total_zstd_allocation;
  271. }