compress_lzma.c 9.2 KB

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