compress.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 1 if a given <b>method</b> is supported; otherwise 0. */
  139. int
  140. tor_compress_supports_method(compress_method_t method)
  141. {
  142. switch (method) {
  143. case GZIP_METHOD:
  144. case ZLIB_METHOD:
  145. return tor_zlib_method_supported();
  146. case LZMA_METHOD:
  147. return tor_lzma_method_supported();
  148. case ZSTD_METHOD:
  149. return tor_zstd_method_supported();
  150. case NO_METHOD:
  151. case UNKNOWN_METHOD:
  152. default:
  153. return 0;
  154. }
  155. }
  156. /** Return a string representation of the version of the library providing the
  157. * compression method given in <b>method</b>. Returns NULL if <b>method</b> is
  158. * unknown or unsupported. */
  159. const char *
  160. tor_compress_version_str(compress_method_t method)
  161. {
  162. switch (method) {
  163. case GZIP_METHOD:
  164. case ZLIB_METHOD:
  165. return tor_zlib_get_version_str();
  166. case LZMA_METHOD:
  167. return tor_lzma_get_version_str();
  168. case ZSTD_METHOD:
  169. return tor_zstd_get_version_str();
  170. case NO_METHOD:
  171. case UNKNOWN_METHOD:
  172. default:
  173. return NULL;
  174. }
  175. }
  176. /** Return a string representation of the version of the library, found at
  177. * compile time, providing the compression method given in <b>method</b>.
  178. * Returns NULL if <b>method</b> is unknown or unsupported. */
  179. const char *
  180. tor_compress_header_version_str(compress_method_t method)
  181. {
  182. switch (method) {
  183. case GZIP_METHOD:
  184. case ZLIB_METHOD:
  185. return tor_zlib_get_header_version_str();
  186. case LZMA_METHOD:
  187. return tor_lzma_get_header_version_str();
  188. case ZSTD_METHOD:
  189. return tor_zstd_get_header_version_str();
  190. case NO_METHOD:
  191. case UNKNOWN_METHOD:
  192. default:
  193. return NULL;
  194. }
  195. }
  196. /** Return the approximate number of bytes allocated for all
  197. * supported compression schemas. */
  198. size_t
  199. tor_compress_get_total_allocation(void)
  200. {
  201. return tor_zlib_get_total_allocation() +
  202. tor_lzma_get_total_allocation() +
  203. tor_zstd_get_total_allocation();
  204. }
  205. /** Internal state for an incremental compression/decompression. The body of
  206. * this struct is not exposed. */
  207. struct tor_compress_state_t {
  208. compress_method_t method; /**< The compression method. */
  209. union {
  210. tor_zlib_compress_state_t *zlib_state;
  211. tor_lzma_compress_state_t *lzma_state;
  212. tor_zstd_compress_state_t *zstd_state;
  213. } u; /**< Compression backend state. */
  214. };
  215. /** Construct and return a tor_compress_state_t object using <b>method</b>. If
  216. * <b>compress</b>, it's for compression; otherwise it's for decompression. */
  217. tor_compress_state_t *
  218. tor_compress_new(int compress, compress_method_t method,
  219. compression_level_t compression_level)
  220. {
  221. tor_compress_state_t *state;
  222. state = tor_malloc_zero(sizeof(tor_compress_state_t));
  223. state->method = method;
  224. switch (method) {
  225. case GZIP_METHOD:
  226. case ZLIB_METHOD: {
  227. tor_zlib_compress_state_t *zlib_state =
  228. tor_zlib_compress_new(compress, method, compression_level);
  229. if (zlib_state == NULL)
  230. goto err;
  231. state->u.zlib_state = zlib_state;
  232. break;
  233. }
  234. case LZMA_METHOD: {
  235. tor_lzma_compress_state_t *lzma_state =
  236. tor_lzma_compress_new(compress, method, compression_level);
  237. if (lzma_state == NULL)
  238. goto err;
  239. state->u.lzma_state = lzma_state;
  240. break;
  241. }
  242. case ZSTD_METHOD: {
  243. tor_zstd_compress_state_t *zstd_state =
  244. tor_zstd_compress_new(compress, method, compression_level);
  245. if (zstd_state == NULL)
  246. goto err;
  247. state->u.zstd_state = zstd_state;
  248. break;
  249. }
  250. case NO_METHOD:
  251. case UNKNOWN_METHOD:
  252. goto err;
  253. }
  254. return state;
  255. err:
  256. tor_free(state);
  257. return NULL;
  258. }
  259. /** Compress/decompress some bytes using <b>state</b>. Read up to
  260. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  261. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  262. * we've reached the end of the input.
  263. *
  264. * Return TOR_COMPRESS_DONE if we've finished the entire
  265. * compression/decompression.
  266. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  267. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  268. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  269. */
  270. tor_compress_output_t
  271. tor_compress_process(tor_compress_state_t *state,
  272. char **out, size_t *out_len,
  273. const char **in, size_t *in_len,
  274. int finish)
  275. {
  276. tor_assert(state != NULL);
  277. switch (state->method) {
  278. case GZIP_METHOD:
  279. case ZLIB_METHOD:
  280. return tor_zlib_compress_process(state->u.zlib_state,
  281. out, out_len, in, in_len,
  282. finish);
  283. case LZMA_METHOD:
  284. return tor_lzma_compress_process(state->u.lzma_state,
  285. out, out_len, in, in_len,
  286. finish);
  287. case ZSTD_METHOD:
  288. return tor_zstd_compress_process(state->u.zstd_state,
  289. out, out_len, in, in_len,
  290. finish);
  291. case NO_METHOD:
  292. case UNKNOWN_METHOD:
  293. goto err;
  294. }
  295. err:
  296. return TOR_COMPRESS_ERROR;
  297. }
  298. /** Deallocate <b>state</b>. */
  299. void
  300. tor_compress_free(tor_compress_state_t *state)
  301. {
  302. if (state == NULL)
  303. return;
  304. switch (state->method) {
  305. case GZIP_METHOD:
  306. case ZLIB_METHOD:
  307. tor_zlib_compress_free(state->u.zlib_state);
  308. break;
  309. case LZMA_METHOD:
  310. tor_lzma_compress_free(state->u.lzma_state);
  311. break;
  312. case ZSTD_METHOD:
  313. tor_zstd_compress_free(state->u.zstd_state);
  314. break;
  315. case NO_METHOD:
  316. case UNKNOWN_METHOD:
  317. break;
  318. }
  319. tor_free(state);
  320. }
  321. /** Return the approximate number of bytes allocated for <b>state</b>. */
  322. size_t
  323. tor_compress_state_size(const tor_compress_state_t *state)
  324. {
  325. tor_assert(state != NULL);
  326. switch (state->method) {
  327. case GZIP_METHOD:
  328. case ZLIB_METHOD:
  329. return tor_zlib_compress_state_size(state->u.zlib_state);
  330. case LZMA_METHOD:
  331. return tor_lzma_compress_state_size(state->u.lzma_state);
  332. case ZSTD_METHOD:
  333. return tor_zstd_compress_state_size(state->u.zstd_state);
  334. case NO_METHOD:
  335. case UNKNOWN_METHOD:
  336. goto err;
  337. }
  338. err:
  339. return 0;
  340. }