compress_lzma.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 /* defined(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. // LCOV_EXCL_START
  141. err:
  142. return 0;
  143. // LCOV_EXCL_STOP
  144. }
  145. #endif /* defined(HAVE_LZMA) */
  146. /** Construct and return a tor_lzma_compress_state_t object using
  147. * <b>method</b>. If <b>compress</b>, it's for compression; otherwise it's for
  148. * decompression. */
  149. tor_lzma_compress_state_t *
  150. tor_lzma_compress_new(int compress,
  151. compress_method_t method,
  152. compression_level_t level)
  153. {
  154. tor_assert(method == LZMA_METHOD);
  155. #ifdef HAVE_LZMA
  156. tor_lzma_compress_state_t *result;
  157. lzma_ret retval;
  158. lzma_options_lzma stream_options;
  159. // Note that we do not explicitly initialize the lzma_stream object here,
  160. // since the LZMA_STREAM_INIT "just" initializes all members to 0, which is
  161. // also what `tor_malloc_zero()` does.
  162. result = tor_malloc_zero(sizeof(tor_lzma_compress_state_t));
  163. result->compress = compress;
  164. result->allocation = tor_lzma_state_size_precalc(compress, level);
  165. if (compress) {
  166. lzma_lzma_preset(&stream_options, memory_level(level));
  167. retval = lzma_alone_encoder(&result->stream, &stream_options);
  168. if (retval != LZMA_OK) {
  169. // LCOV_EXCL_START
  170. log_warn(LD_GENERAL, "Error from LZMA encoder: %s (%u).",
  171. lzma_error_str(retval), retval);
  172. goto err;
  173. // LCOV_EXCL_STOP
  174. }
  175. } else {
  176. retval = lzma_alone_decoder(&result->stream, MEMORY_LIMIT);
  177. if (retval != LZMA_OK) {
  178. // LCOV_EXCL_START
  179. log_warn(LD_GENERAL, "Error from LZMA decoder: %s (%u).",
  180. lzma_error_str(retval), retval);
  181. goto err;
  182. // LCOV_EXCL_STOP
  183. }
  184. }
  185. atomic_counter_add(&total_lzma_allocation, result->allocation);
  186. return result;
  187. /* LCOV_EXCL_START */
  188. err:
  189. tor_free(result);
  190. return NULL;
  191. /* LCOV_EXCL_STOP */
  192. #else /* !(defined(HAVE_LZMA)) */
  193. (void)compress;
  194. (void)method;
  195. (void)level;
  196. return NULL;
  197. #endif /* defined(HAVE_LZMA) */
  198. }
  199. /** Compress/decompress some bytes using <b>state</b>. Read up to
  200. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  201. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  202. * we've reached the end of the input.
  203. *
  204. * Return TOR_COMPRESS_DONE if we've finished the entire
  205. * compression/decompression.
  206. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  207. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  208. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  209. */
  210. tor_compress_output_t
  211. tor_lzma_compress_process(tor_lzma_compress_state_t *state,
  212. char **out, size_t *out_len,
  213. const char **in, size_t *in_len,
  214. int finish)
  215. {
  216. #ifdef HAVE_LZMA
  217. lzma_ret retval;
  218. lzma_action action;
  219. tor_assert(state != NULL);
  220. tor_assert(*in_len <= UINT_MAX);
  221. tor_assert(*out_len <= UINT_MAX);
  222. state->stream.next_in = (unsigned char *)*in;
  223. state->stream.avail_in = *in_len;
  224. state->stream.next_out = (unsigned char *)*out;
  225. state->stream.avail_out = *out_len;
  226. action = finish ? LZMA_FINISH : LZMA_RUN;
  227. retval = lzma_code(&state->stream, action);
  228. state->input_so_far += state->stream.next_in - ((unsigned char *)*in);
  229. state->output_so_far += state->stream.next_out - ((unsigned char *)*out);
  230. *out = (char *)state->stream.next_out;
  231. *out_len = state->stream.avail_out;
  232. *in = (const char *)state->stream.next_in;
  233. *in_len = state->stream.avail_in;
  234. if (! state->compress &&
  235. tor_compress_is_compression_bomb(state->input_so_far,
  236. state->output_so_far)) {
  237. log_warn(LD_DIR, "Possible compression bomb; abandoning stream.");
  238. return TOR_COMPRESS_ERROR;
  239. }
  240. switch (retval) {
  241. case LZMA_OK:
  242. if (state->stream.avail_out == 0 || finish)
  243. return TOR_COMPRESS_BUFFER_FULL;
  244. return TOR_COMPRESS_OK;
  245. case LZMA_BUF_ERROR:
  246. if (state->stream.avail_in == 0 && !finish)
  247. return TOR_COMPRESS_OK;
  248. return TOR_COMPRESS_BUFFER_FULL;
  249. case LZMA_STREAM_END:
  250. return TOR_COMPRESS_DONE;
  251. // We list all the possible values of `lzma_ret` here to silence the
  252. // `switch-enum` warning and to detect if a new member was added.
  253. case LZMA_NO_CHECK:
  254. case LZMA_UNSUPPORTED_CHECK:
  255. case LZMA_GET_CHECK:
  256. case LZMA_MEM_ERROR:
  257. case LZMA_MEMLIMIT_ERROR:
  258. case LZMA_FORMAT_ERROR:
  259. case LZMA_OPTIONS_ERROR:
  260. case LZMA_DATA_ERROR:
  261. case LZMA_PROG_ERROR:
  262. default:
  263. log_warn(LD_GENERAL, "LZMA %s didn't finish: %s.",
  264. state->compress ? "compression" : "decompression",
  265. lzma_error_str(retval));
  266. return TOR_COMPRESS_ERROR;
  267. }
  268. #else /* !(defined(HAVE_LZMA)) */
  269. (void)state;
  270. (void)out;
  271. (void)out_len;
  272. (void)in;
  273. (void)in_len;
  274. (void)finish;
  275. return TOR_COMPRESS_ERROR;
  276. #endif /* defined(HAVE_LZMA) */
  277. }
  278. /** Deallocate <b>state</b>. */
  279. void
  280. tor_lzma_compress_free_(tor_lzma_compress_state_t *state)
  281. {
  282. if (state == NULL)
  283. return;
  284. atomic_counter_sub(&total_lzma_allocation, state->allocation);
  285. #ifdef HAVE_LZMA
  286. lzma_end(&state->stream);
  287. #endif
  288. tor_free(state);
  289. }
  290. /** Return the approximate number of bytes allocated for <b>state</b>. */
  291. size_t
  292. tor_lzma_compress_state_size(const tor_lzma_compress_state_t *state)
  293. {
  294. tor_assert(state != NULL);
  295. return state->allocation;
  296. }
  297. /** Return the approximate number of bytes allocated for all LZMA states. */
  298. size_t
  299. tor_lzma_get_total_allocation(void)
  300. {
  301. return atomic_counter_get(&total_lzma_allocation);
  302. }
  303. /** Initialize the lzma module */
  304. void
  305. tor_lzma_init(void)
  306. {
  307. atomic_counter_init(&total_lzma_allocation);
  308. }