compress.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.c
  7. * \brief Common compression API.
  8. **/
  9. #include "orconfig.h"
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <assert.h>
  13. #include <string.h>
  14. #include "torint.h"
  15. #ifdef HAVE_NETINET_IN_H
  16. #include <netinet/in.h>
  17. #endif
  18. #include "util.h"
  19. #include "torlog.h"
  20. #include "compress.h"
  21. #include "compress_lzma.h"
  22. #include "compress_zlib.h"
  23. #include "compress_zstd.h"
  24. /** @{ */
  25. /* These macros define the maximum allowable compression factor. Anything of
  26. * size greater than CHECK_FOR_COMPRESSION_BOMB_AFTER is not allowed to
  27. * have an uncompression factor (uncompressed size:compressed size ratio) of
  28. * any greater than MAX_UNCOMPRESSION_FACTOR.
  29. *
  30. * Picking a value for MAX_UNCOMPRESSION_FACTOR is a trade-off: we want it to
  31. * be small to limit the attack multiplier, but we also want it to be large
  32. * enough so that no legitimate document --even ones we might invent in the
  33. * future -- ever compresses by a factor of greater than
  34. * MAX_UNCOMPRESSION_FACTOR. Within those parameters, there's a reasonably
  35. * large range of possible values. IMO, anything over 8 is probably safe; IMO
  36. * anything under 50 is probably sufficient.
  37. */
  38. #define MAX_UNCOMPRESSION_FACTOR 25
  39. #define CHECK_FOR_COMPRESSION_BOMB_AFTER (1024*64)
  40. /** @} */
  41. /** Return true if uncompressing an input of size <b>in_size</b> to an input of
  42. * size at least <b>size_out</b> looks like a compression bomb. */
  43. int
  44. tor_compress_is_compression_bomb(size_t size_in, size_t size_out)
  45. {
  46. if (size_in == 0 || size_out < CHECK_FOR_COMPRESSION_BOMB_AFTER)
  47. return 0;
  48. return (size_out / size_in > MAX_UNCOMPRESSION_FACTOR);
  49. }
  50. /** Given <b>level</b> return the memory level. The memory level is needed for
  51. * the various compression backends used in Tor.
  52. */
  53. int
  54. tor_compress_memory_level(compression_level_t level)
  55. {
  56. switch (level) {
  57. default:
  58. case HIGH_COMPRESSION: return 8;
  59. case MEDIUM_COMPRESSION: return 7;
  60. case LOW_COMPRESSION: return 6;
  61. }
  62. }
  63. /** Given <b>in_len</b> bytes at <b>in</b>, compress them into a newly
  64. * allocated buffer, using the method described in <b>method</b>. Store the
  65. * compressed string in *<b>out</b>, and its length in *<b>out_len</b>.
  66. * Return 0 on success, -1 on failure.
  67. */
  68. int
  69. tor_compress(char **out, size_t *out_len,
  70. const char *in, size_t in_len,
  71. compress_method_t method)
  72. {
  73. if (method == GZIP_METHOD || method == ZLIB_METHOD)
  74. return tor_zlib_compress(out, out_len, in, in_len, method);
  75. if (method == LZMA_METHOD)
  76. return tor_lzma_compress(out, out_len, in, in_len, method);
  77. if (method == ZSTD_METHOD)
  78. return tor_zstd_compress(out, out_len, in, in_len, method);
  79. return -1;
  80. }
  81. /** Given zero or more zlib-compressed or gzip-compressed strings of
  82. * total length
  83. * <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
  84. * buffer, using the method described in <b>method</b>. Store the uncompressed
  85. * string in *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on
  86. * success, -1 on failure.
  87. *
  88. * If <b>complete_only</b> is true, we consider a truncated input as a
  89. * failure; otherwise we decompress as much as we can. Warn about truncated
  90. * or corrupt inputs at <b>protocol_warn_level</b>.
  91. */
  92. int
  93. tor_uncompress(char **out, size_t *out_len,
  94. const char *in, size_t in_len,
  95. compress_method_t method,
  96. int complete_only,
  97. int protocol_warn_level)
  98. {
  99. if (method == GZIP_METHOD || method == ZLIB_METHOD)
  100. return tor_zlib_uncompress(out, out_len, in, in_len,
  101. method,
  102. complete_only,
  103. protocol_warn_level);
  104. if (method == LZMA_METHOD)
  105. return tor_lzma_uncompress(out, out_len, in, in_len,
  106. method,
  107. complete_only,
  108. protocol_warn_level);
  109. if (method == ZSTD_METHOD)
  110. return tor_zstd_uncompress(out, out_len, in, in_len,
  111. method,
  112. complete_only,
  113. protocol_warn_level);
  114. return -1;
  115. }
  116. /** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
  117. * to be compressed or not. If it is, return the likeliest compression method.
  118. * Otherwise, return UNKNOWN_METHOD.
  119. */
  120. compress_method_t
  121. detect_compression_method(const char *in, size_t in_len)
  122. {
  123. if (in_len > 2 && fast_memeq(in, "\x1f\x8b", 2)) {
  124. return GZIP_METHOD;
  125. } else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
  126. (ntohs(get_uint16(in)) % 31) == 0) {
  127. return ZLIB_METHOD;
  128. } else if (in_len > 3 &&
  129. fast_memeq(in, "\x5d\x00\x00\x00", 4)) {
  130. return LZMA_METHOD;
  131. } else if (in_len > 3 &&
  132. fast_memeq(in, "\x28\xb5\x2f\xfd", 4)) {
  133. return ZSTD_METHOD;
  134. } else {
  135. return UNKNOWN_METHOD;
  136. }
  137. }
  138. /** Return the approximate number of bytes allocated for all
  139. * supported compression schemas. */
  140. size_t
  141. tor_compress_get_total_allocation(void)
  142. {
  143. return tor_zlib_get_total_allocation() +
  144. tor_lzma_get_total_allocation() +
  145. tor_zstd_get_total_allocation();
  146. }
  147. /** Internal state for an incremental compression/decompression. The body of
  148. * this struct is not exposed. */
  149. struct tor_compress_state_t {
  150. compress_method_t method; /**< The compression method. */
  151. union {
  152. tor_zlib_compress_state_t *zlib_state;
  153. tor_lzma_compress_state_t *lzma_state;
  154. tor_zstd_compress_state_t *zstd_state;
  155. } u; /**< Compression backend state. */
  156. };
  157. /** Construct and return a tor_compress_state_t object using <b>method</b>. If
  158. * <b>compress</b>, it's for compression; otherwise it's for decompression. */
  159. tor_compress_state_t *
  160. tor_compress_new(int compress, compress_method_t method,
  161. compression_level_t compression_level)
  162. {
  163. tor_compress_state_t *state;
  164. state = tor_malloc_zero(sizeof(tor_compress_state_t));
  165. state->method = method;
  166. switch (method) {
  167. case GZIP_METHOD:
  168. case ZLIB_METHOD: {
  169. tor_zlib_compress_state_t *zlib_state =
  170. tor_zlib_compress_new(compress, method, compression_level);
  171. if (zlib_state == NULL)
  172. goto err;
  173. state->u.zlib_state = zlib_state;
  174. break;
  175. }
  176. case LZMA_METHOD: {
  177. tor_lzma_compress_state_t *lzma_state =
  178. tor_lzma_compress_new(compress, method, compression_level);
  179. if (lzma_state == NULL)
  180. goto err;
  181. state->u.lzma_state = lzma_state;
  182. break;
  183. }
  184. case ZSTD_METHOD: {
  185. tor_zstd_compress_state_t *zstd_state =
  186. tor_zstd_compress_new(compress, method, compression_level);
  187. if (zstd_state == NULL)
  188. goto err;
  189. state->u.zstd_state = zstd_state;
  190. break;
  191. }
  192. case NO_METHOD:
  193. case UNKNOWN_METHOD:
  194. goto err;
  195. }
  196. return state;
  197. err:
  198. tor_free(state);
  199. return NULL;
  200. }
  201. /** Compress/decompress some bytes using <b>state</b>. Read up to
  202. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  203. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  204. * we've reached the end of the input.
  205. *
  206. * Return TOR_COMPRESS_DONE if we've finished the entire
  207. * compression/decompression.
  208. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  209. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  210. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  211. */
  212. tor_compress_output_t
  213. tor_compress_process(tor_compress_state_t *state,
  214. char **out, size_t *out_len,
  215. const char **in, size_t *in_len,
  216. int finish)
  217. {
  218. tor_assert(state != NULL);
  219. switch (state->method) {
  220. case GZIP_METHOD:
  221. case ZLIB_METHOD:
  222. return tor_zlib_compress_process(state->u.zlib_state,
  223. out, out_len, in, in_len,
  224. finish);
  225. case LZMA_METHOD:
  226. return tor_lzma_compress_process(state->u.lzma_state,
  227. out, out_len, in, in_len,
  228. finish);
  229. case ZSTD_METHOD:
  230. return tor_zstd_compress_process(state->u.zstd_state,
  231. out, out_len, in, in_len,
  232. finish);
  233. case NO_METHOD:
  234. case UNKNOWN_METHOD:
  235. goto err;
  236. }
  237. err:
  238. return TOR_COMPRESS_ERROR;
  239. }
  240. /** Deallocate <b>state</b>. */
  241. void
  242. tor_compress_free(tor_compress_state_t *state)
  243. {
  244. if (state == NULL)
  245. return;
  246. switch (state->method) {
  247. case GZIP_METHOD:
  248. case ZLIB_METHOD:
  249. tor_zlib_compress_free(state->u.zlib_state);
  250. break;
  251. case LZMA_METHOD:
  252. tor_lzma_compress_free(state->u.lzma_state);
  253. break;
  254. case ZSTD_METHOD:
  255. tor_zstd_compress_free(state->u.zstd_state);
  256. break;
  257. case NO_METHOD:
  258. case UNKNOWN_METHOD:
  259. break;
  260. }
  261. tor_free(state);
  262. }
  263. /** Return the approximate number of bytes allocated for <b>state</b>. */
  264. size_t
  265. tor_compress_state_size(const tor_compress_state_t *state)
  266. {
  267. tor_assert(state != NULL);
  268. switch (state->method) {
  269. case GZIP_METHOD:
  270. case ZLIB_METHOD:
  271. return tor_zlib_compress_state_size(state->u.zlib_state);
  272. case LZMA_METHOD:
  273. return tor_lzma_compress_state_size(state->u.lzma_state);
  274. case ZSTD_METHOD:
  275. return tor_zstd_compress_state_size(state->u.zstd_state);
  276. case NO_METHOD:
  277. case UNKNOWN_METHOD:
  278. goto err;
  279. }
  280. err:
  281. return 0;
  282. }