compress_lzma.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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_lzma.c
  7. * \brief Compression backend for LZMA.
  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_lzma.h"
  17. #ifdef HAVE_LZMA
  18. #include <lzma.h>
  19. #endif
  20. /** Total number of bytes allocated for LZMA state. */
  21. static atomic_counter_t total_lzma_allocation;
  22. #ifdef HAVE_LZMA
  23. /** Given <b>level</b> return the memory level. */
  24. static int
  25. memory_level(compression_level_t level)
  26. {
  27. switch (level) {
  28. default:
  29. case BEST_COMPRESSION:
  30. case HIGH_COMPRESSION: return 9;
  31. case MEDIUM_COMPRESSION: return 6;
  32. case LOW_COMPRESSION: return 3;
  33. }
  34. }
  35. /** Convert a given <b>error</b> to a human readable error string. */
  36. static const char *
  37. lzma_error_str(lzma_ret error)
  38. {
  39. switch (error) {
  40. case LZMA_OK:
  41. return "Operation completed successfully";
  42. case LZMA_STREAM_END:
  43. return "End of stream";
  44. case LZMA_NO_CHECK:
  45. return "Input stream lacks integrity check";
  46. case LZMA_UNSUPPORTED_CHECK:
  47. return "Unable to calculate integrity check";
  48. case LZMA_GET_CHECK:
  49. return "Integrity check available";
  50. case LZMA_MEM_ERROR:
  51. return "Unable to allocate memory";
  52. case LZMA_MEMLIMIT_ERROR:
  53. return "Memory limit reached";
  54. case LZMA_FORMAT_ERROR:
  55. return "Unknown file format";
  56. case LZMA_OPTIONS_ERROR:
  57. return "Unsupported options";
  58. case LZMA_DATA_ERROR:
  59. return "Corrupt input data";
  60. case LZMA_BUF_ERROR:
  61. return "Unable to progress";
  62. case LZMA_PROG_ERROR:
  63. return "Programming error";
  64. default:
  65. return "Unknown LZMA error";
  66. }
  67. }
  68. #endif // HAVE_LZMA.
  69. /** Return 1 if LZMA compression is supported; otherwise 0. */
  70. int
  71. tor_lzma_method_supported(void)
  72. {
  73. #ifdef HAVE_LZMA
  74. return 1;
  75. #else
  76. return 0;
  77. #endif
  78. }
  79. /** Return a string representation of the version of the currently running
  80. * version of liblzma. Returns NULL if LZMA is unsupported. */
  81. const char *
  82. tor_lzma_get_version_str(void)
  83. {
  84. #ifdef HAVE_LZMA
  85. return lzma_version_string();
  86. #else
  87. return NULL;
  88. #endif
  89. }
  90. /** Return a string representation of the version of liblzma used at
  91. * compilation time. Returns NULL if LZMA is unsupported. */
  92. const char *
  93. tor_lzma_get_header_version_str(void)
  94. {
  95. #ifdef HAVE_LZMA
  96. return LZMA_VERSION_STRING;
  97. #else
  98. return NULL;
  99. #endif
  100. }
  101. /** Internal LZMA state for incremental compression/decompression.
  102. * The body of this struct is not exposed. */
  103. struct tor_lzma_compress_state_t {
  104. #ifdef HAVE_LZMA
  105. lzma_stream stream; /**< The LZMA stream. */
  106. #endif
  107. int compress; /**< True if we are compressing; false if we are inflating */
  108. /** Number of bytes read so far. Used to detect compression bombs. */
  109. size_t input_so_far;
  110. /** Number of bytes written so far. Used to detect compression bombs. */
  111. size_t output_so_far;
  112. /** Approximate number of bytes allocated for this object. */
  113. size_t allocation;
  114. };
  115. /** Construct and return a tor_lzma_compress_state_t object using
  116. * <b>method</b>. If <b>compress</b>, it's for compression; otherwise it's for
  117. * decompression. */
  118. tor_lzma_compress_state_t *
  119. tor_lzma_compress_new(int compress,
  120. compress_method_t method,
  121. compression_level_t compression_level)
  122. {
  123. tor_assert(method == LZMA_METHOD);
  124. #ifdef HAVE_LZMA
  125. tor_lzma_compress_state_t *result;
  126. lzma_ret retval;
  127. lzma_options_lzma stream_options;
  128. // Note that we do not explicitly initialize the lzma_stream object here,
  129. // since the LZMA_STREAM_INIT "just" initializes all members to 0, which is
  130. // also what `tor_malloc_zero()` does.
  131. result = tor_malloc_zero(sizeof(tor_lzma_compress_state_t));
  132. result->compress = compress;
  133. // FIXME(ahf): We should either try to do the pre-calculation that is done
  134. // with the zlib backend or use a custom allocator here where we pass our
  135. // tor_lzma_compress_state_t as the opaque value.
  136. result->allocation = 0;
  137. if (compress) {
  138. lzma_lzma_preset(&stream_options,
  139. memory_level(compression_level));
  140. retval = lzma_alone_encoder(&result->stream, &stream_options);
  141. if (retval != LZMA_OK) {
  142. log_warn(LD_GENERAL, "Error from LZMA encoder: %s (%u).",
  143. lzma_error_str(retval), retval);
  144. goto err;
  145. }
  146. } else {
  147. // FIXME(ahf): This should be something more sensible than
  148. // UINT64_MAX: See #21665.
  149. retval = lzma_alone_decoder(&result->stream, UINT64_MAX);
  150. if (retval != LZMA_OK) {
  151. log_warn(LD_GENERAL, "Error from LZMA decoder: %s (%u).",
  152. lzma_error_str(retval), retval);
  153. goto err;
  154. }
  155. }
  156. atomic_counter_add(&total_lzma_allocation, result->allocation);
  157. return result;
  158. err:
  159. tor_free(result);
  160. return NULL;
  161. #else // HAVE_LZMA.
  162. (void)compress;
  163. (void)method;
  164. (void)compression_level;
  165. return NULL;
  166. #endif // HAVE_LZMA.
  167. }
  168. /** Compress/decompress some bytes using <b>state</b>. Read up to
  169. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  170. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  171. * we've reached the end of the input.
  172. *
  173. * Return TOR_COMPRESS_DONE if we've finished the entire
  174. * compression/decompression.
  175. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  176. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  177. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  178. */
  179. tor_compress_output_t
  180. tor_lzma_compress_process(tor_lzma_compress_state_t *state,
  181. char **out, size_t *out_len,
  182. const char **in, size_t *in_len,
  183. int finish)
  184. {
  185. #ifdef HAVE_LZMA
  186. lzma_ret retval;
  187. lzma_action action;
  188. tor_assert(state != NULL);
  189. tor_assert(*in_len <= UINT_MAX);
  190. tor_assert(*out_len <= UINT_MAX);
  191. state->stream.next_in = (unsigned char *)*in;
  192. state->stream.avail_in = *in_len;
  193. state->stream.next_out = (unsigned char *)*out;
  194. state->stream.avail_out = *out_len;
  195. action = finish ? LZMA_FINISH : LZMA_RUN;
  196. retval = lzma_code(&state->stream, action);
  197. state->input_so_far += state->stream.next_in - ((unsigned char *)*in);
  198. state->output_so_far += state->stream.next_out - ((unsigned char *)*out);
  199. *out = (char *)state->stream.next_out;
  200. *out_len = state->stream.avail_out;
  201. *in = (const char *)state->stream.next_in;
  202. *in_len = state->stream.avail_in;
  203. if (! state->compress &&
  204. tor_compress_is_compression_bomb(state->input_so_far,
  205. state->output_so_far)) {
  206. log_warn(LD_DIR, "Possible compression bomb; abandoning stream.");
  207. return TOR_COMPRESS_ERROR;
  208. }
  209. switch (retval) {
  210. case LZMA_OK:
  211. if (state->stream.avail_out == 0 || finish)
  212. return TOR_COMPRESS_BUFFER_FULL;
  213. return TOR_COMPRESS_OK;
  214. case LZMA_BUF_ERROR:
  215. if (state->stream.avail_in == 0 && !finish)
  216. return TOR_COMPRESS_OK;
  217. return TOR_COMPRESS_BUFFER_FULL;
  218. case LZMA_STREAM_END:
  219. return TOR_COMPRESS_DONE;
  220. // We list all the possible values of `lzma_ret` here to silence the
  221. // `switch-enum` warning and to detect if a new member was added.
  222. case LZMA_NO_CHECK:
  223. case LZMA_UNSUPPORTED_CHECK:
  224. case LZMA_GET_CHECK:
  225. case LZMA_MEM_ERROR:
  226. case LZMA_MEMLIMIT_ERROR:
  227. case LZMA_FORMAT_ERROR:
  228. case LZMA_OPTIONS_ERROR:
  229. case LZMA_DATA_ERROR:
  230. case LZMA_PROG_ERROR:
  231. default:
  232. log_warn(LD_GENERAL, "LZMA %s didn't finish: %s.",
  233. state->compress ? "compression" : "decompression",
  234. lzma_error_str(retval));
  235. return TOR_COMPRESS_ERROR;
  236. }
  237. #else // HAVE_LZMA.
  238. (void)state;
  239. (void)out;
  240. (void)out_len;
  241. (void)in;
  242. (void)in_len;
  243. (void)finish;
  244. return TOR_COMPRESS_ERROR;
  245. #endif // HAVE_LZMA.
  246. }
  247. /** Deallocate <b>state</b>. */
  248. void
  249. tor_lzma_compress_free(tor_lzma_compress_state_t *state)
  250. {
  251. if (state == NULL)
  252. return;
  253. atomic_counter_sub(&total_lzma_allocation, state->allocation);
  254. #ifdef HAVE_LZMA
  255. lzma_end(&state->stream);
  256. #endif
  257. tor_free(state);
  258. }
  259. /** Return the approximate number of bytes allocated for <b>state</b>. */
  260. size_t
  261. tor_lzma_compress_state_size(const tor_lzma_compress_state_t *state)
  262. {
  263. tor_assert(state != NULL);
  264. return state->allocation;
  265. }
  266. /** Return the approximate number of bytes allocated for all LZMA states. */
  267. size_t
  268. tor_lzma_get_total_allocation(void)
  269. {
  270. return atomic_counter_get(&total_lzma_allocation);
  271. }
  272. /** Initialize the lzma module */
  273. void
  274. tor_lzma_init(void)
  275. {
  276. atomic_counter_init(&total_lzma_allocation);
  277. }