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. // LCOV_EXCL_START
  42. switch (error) {
  43. case LZMA_OK:
  44. return "Operation completed successfully";
  45. case LZMA_STREAM_END:
  46. return "End of stream";
  47. case LZMA_NO_CHECK:
  48. return "Input stream lacks integrity check";
  49. case LZMA_UNSUPPORTED_CHECK:
  50. return "Unable to calculate integrity check";
  51. case LZMA_GET_CHECK:
  52. return "Integrity check available";
  53. case LZMA_MEM_ERROR:
  54. return "Unable to allocate memory";
  55. case LZMA_MEMLIMIT_ERROR:
  56. return "Memory limit reached";
  57. case LZMA_FORMAT_ERROR:
  58. return "Unknown file format";
  59. case LZMA_OPTIONS_ERROR:
  60. return "Unsupported options";
  61. case LZMA_DATA_ERROR:
  62. return "Corrupt input data";
  63. case LZMA_BUF_ERROR:
  64. return "Unable to progress";
  65. case LZMA_PROG_ERROR:
  66. return "Programming error";
  67. default:
  68. return "Unknown LZMA error";
  69. }
  70. // LCOV_EXCL_STOP
  71. }
  72. #endif // HAVE_LZMA.
  73. /** Return 1 if LZMA compression is supported; otherwise 0. */
  74. int
  75. tor_lzma_method_supported(void)
  76. {
  77. #ifdef HAVE_LZMA
  78. return 1;
  79. #else
  80. return 0;
  81. #endif
  82. }
  83. /** Return a string representation of the version of the currently running
  84. * version of liblzma. Returns NULL if LZMA is unsupported. */
  85. const char *
  86. tor_lzma_get_version_str(void)
  87. {
  88. #ifdef HAVE_LZMA
  89. return lzma_version_string();
  90. #else
  91. return NULL;
  92. #endif
  93. }
  94. /** Return a string representation of the version of liblzma used at
  95. * compilation time. Returns NULL if LZMA is unsupported. */
  96. const char *
  97. tor_lzma_get_header_version_str(void)
  98. {
  99. #ifdef HAVE_LZMA
  100. return LZMA_VERSION_STRING;
  101. #else
  102. return NULL;
  103. #endif
  104. }
  105. /** Internal LZMA state for incremental compression/decompression.
  106. * The body of this struct is not exposed. */
  107. struct tor_lzma_compress_state_t {
  108. #ifdef HAVE_LZMA
  109. lzma_stream stream; /**< The LZMA stream. */
  110. #endif
  111. int compress; /**< True if we are compressing; false if we are inflating */
  112. /** Number of bytes read so far. Used to detect compression bombs. */
  113. size_t input_so_far;
  114. /** Number of bytes written so far. Used to detect compression bombs. */
  115. size_t output_so_far;
  116. /** Approximate number of bytes allocated for this object. */
  117. size_t allocation;
  118. };
  119. #ifdef HAVE_LZMA
  120. /** Return an approximate number of bytes stored in memory to hold the LZMA
  121. * encoder/decoder state. */
  122. static size_t
  123. tor_lzma_state_size_precalc(int compress, compression_level_t level)
  124. {
  125. uint64_t memory_usage;
  126. if (compress)
  127. memory_usage = lzma_easy_encoder_memusage(memory_level(level));
  128. else
  129. memory_usage = lzma_easy_decoder_memusage(memory_level(level));
  130. if (memory_usage == UINT64_MAX) {
  131. // LCOV_EXCL_START
  132. log_warn(LD_GENERAL, "Unsupported compression level passed to LZMA %s",
  133. compress ? "encoder" : "decoder");
  134. goto err;
  135. // LCOV_EXCL_STOP
  136. }
  137. if (memory_usage + sizeof(tor_lzma_compress_state_t) > SIZE_MAX)
  138. memory_usage = SIZE_MAX;
  139. else
  140. memory_usage += sizeof(tor_lzma_compress_state_t);
  141. return (size_t)memory_usage;
  142. err:
  143. return 0; // LCOV_EXCL_LINE
  144. }
  145. #endif // 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. err:
  188. tor_free(result); // LCOV_EXCL_LINE
  189. return NULL;
  190. #else // HAVE_LZMA.
  191. (void)compress;
  192. (void)method;
  193. (void)level;
  194. return NULL;
  195. #endif // HAVE_LZMA.
  196. }
  197. /** Compress/decompress some bytes using <b>state</b>. Read up to
  198. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  199. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  200. * we've reached the end of the input.
  201. *
  202. * Return TOR_COMPRESS_DONE if we've finished the entire
  203. * compression/decompression.
  204. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  205. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  206. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  207. */
  208. tor_compress_output_t
  209. tor_lzma_compress_process(tor_lzma_compress_state_t *state,
  210. char **out, size_t *out_len,
  211. const char **in, size_t *in_len,
  212. int finish)
  213. {
  214. #ifdef HAVE_LZMA
  215. lzma_ret retval;
  216. lzma_action action;
  217. tor_assert(state != NULL);
  218. tor_assert(*in_len <= UINT_MAX);
  219. tor_assert(*out_len <= UINT_MAX);
  220. state->stream.next_in = (unsigned char *)*in;
  221. state->stream.avail_in = *in_len;
  222. state->stream.next_out = (unsigned char *)*out;
  223. state->stream.avail_out = *out_len;
  224. action = finish ? LZMA_FINISH : LZMA_RUN;
  225. retval = lzma_code(&state->stream, action);
  226. state->input_so_far += state->stream.next_in - ((unsigned char *)*in);
  227. state->output_so_far += state->stream.next_out - ((unsigned char *)*out);
  228. *out = (char *)state->stream.next_out;
  229. *out_len = state->stream.avail_out;
  230. *in = (const char *)state->stream.next_in;
  231. *in_len = state->stream.avail_in;
  232. if (! state->compress &&
  233. tor_compress_is_compression_bomb(state->input_so_far,
  234. state->output_so_far)) {
  235. log_warn(LD_DIR, "Possible compression bomb; abandoning stream.");
  236. return TOR_COMPRESS_ERROR;
  237. }
  238. switch (retval) {
  239. case LZMA_OK:
  240. if (state->stream.avail_out == 0 || finish)
  241. return TOR_COMPRESS_BUFFER_FULL;
  242. return TOR_COMPRESS_OK;
  243. case LZMA_BUF_ERROR:
  244. if (state->stream.avail_in == 0 && !finish)
  245. return TOR_COMPRESS_OK;
  246. return TOR_COMPRESS_BUFFER_FULL;
  247. case LZMA_STREAM_END:
  248. return TOR_COMPRESS_DONE;
  249. // We list all the possible values of `lzma_ret` here to silence the
  250. // `switch-enum` warning and to detect if a new member was added.
  251. case LZMA_NO_CHECK:
  252. case LZMA_UNSUPPORTED_CHECK:
  253. case LZMA_GET_CHECK:
  254. case LZMA_MEM_ERROR:
  255. case LZMA_MEMLIMIT_ERROR:
  256. case LZMA_FORMAT_ERROR:
  257. case LZMA_OPTIONS_ERROR:
  258. case LZMA_DATA_ERROR:
  259. case LZMA_PROG_ERROR:
  260. default:
  261. // LCOV_EXCL_START
  262. log_warn(LD_GENERAL, "LZMA %s didn't finish: %s.",
  263. state->compress ? "compression" : "decompression",
  264. lzma_error_str(retval));
  265. return TOR_COMPRESS_ERROR;
  266. // LCOV_EXCL_STOP
  267. }
  268. #else // 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 // 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. }