compress_lzma.c 8.4 KB

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