compress_lzma.c 9.4 KB

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