compress.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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>in_len</b> bytes at <b>in</b>, compress them into a newly
  51. * allocated buffer, using the method described in <b>method</b>. Store the
  52. * compressed string in *<b>out</b>, and its length in *<b>out_len</b>.
  53. * Return 0 on success, -1 on failure.
  54. */
  55. int
  56. tor_compress(char **out, size_t *out_len,
  57. const char *in, size_t in_len,
  58. compress_method_t method)
  59. {
  60. switch (method) {
  61. case GZIP_METHOD:
  62. case ZLIB_METHOD:
  63. return tor_zlib_compress(out, out_len, in, in_len, method);
  64. case LZMA_METHOD:
  65. return tor_lzma_compress(out, out_len, in, in_len, method);
  66. case ZSTD_METHOD:
  67. return tor_zstd_compress(out, out_len, in, in_len, method);
  68. case NO_METHOD:
  69. case UNKNOWN_METHOD:
  70. default:
  71. return -1;
  72. }
  73. }
  74. /** Given zero or more zlib-compressed or gzip-compressed strings of
  75. * total length
  76. * <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
  77. * buffer, using the method described in <b>method</b>. Store the uncompressed
  78. * string in *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on
  79. * success, -1 on failure.
  80. *
  81. * If <b>complete_only</b> is true, we consider a truncated input as a
  82. * failure; otherwise we decompress as much as we can. Warn about truncated
  83. * or corrupt inputs at <b>protocol_warn_level</b>.
  84. */
  85. int
  86. tor_uncompress(char **out, size_t *out_len,
  87. const char *in, size_t in_len,
  88. compress_method_t method,
  89. int complete_only,
  90. int protocol_warn_level)
  91. {
  92. switch (method) {
  93. case GZIP_METHOD:
  94. case ZLIB_METHOD:
  95. return tor_zlib_uncompress(out, out_len, in, in_len,
  96. method,
  97. complete_only,
  98. protocol_warn_level);
  99. case LZMA_METHOD:
  100. return tor_lzma_uncompress(out, out_len, in, in_len,
  101. method,
  102. complete_only,
  103. protocol_warn_level);
  104. case ZSTD_METHOD:
  105. return tor_zstd_uncompress(out, out_len, in, in_len,
  106. method,
  107. complete_only,
  108. protocol_warn_level);
  109. case NO_METHOD:
  110. case UNKNOWN_METHOD:
  111. default:
  112. return -1;
  113. }
  114. }
  115. /** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
  116. * to be compressed or not. If it is, return the likeliest compression method.
  117. * Otherwise, return UNKNOWN_METHOD.
  118. */
  119. compress_method_t
  120. detect_compression_method(const char *in, size_t in_len)
  121. {
  122. if (in_len > 2 && fast_memeq(in, "\x1f\x8b", 2)) {
  123. return GZIP_METHOD;
  124. } else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
  125. (ntohs(get_uint16(in)) % 31) == 0) {
  126. return ZLIB_METHOD;
  127. } else if (in_len > 3 &&
  128. fast_memeq(in, "\x5d\x00\x00\x00", 4)) {
  129. return LZMA_METHOD;
  130. } else if (in_len > 3 &&
  131. fast_memeq(in, "\x28\xb5\x2f\xfd", 4)) {
  132. return ZSTD_METHOD;
  133. } else {
  134. return UNKNOWN_METHOD;
  135. }
  136. }
  137. /** Return 1 if a given <b>method</b> is supported; otherwise 0. */
  138. int
  139. tor_compress_supports_method(compress_method_t method)
  140. {
  141. switch (method) {
  142. case GZIP_METHOD:
  143. case ZLIB_METHOD:
  144. return tor_zlib_method_supported();
  145. case LZMA_METHOD:
  146. return tor_lzma_method_supported();
  147. case ZSTD_METHOD:
  148. return tor_zstd_method_supported();
  149. case NO_METHOD:
  150. case UNKNOWN_METHOD:
  151. default:
  152. return 0;
  153. }
  154. }
  155. /** Return a string representation of the version of the library providing the
  156. * compression method given in <b>method</b>. Returns NULL if <b>method</b> is
  157. * unknown or unsupported. */
  158. const char *
  159. tor_compress_version_str(compress_method_t method)
  160. {
  161. switch (method) {
  162. case GZIP_METHOD:
  163. case ZLIB_METHOD:
  164. return tor_zlib_get_version_str();
  165. case LZMA_METHOD:
  166. return tor_lzma_get_version_str();
  167. case ZSTD_METHOD:
  168. return tor_zstd_get_version_str();
  169. case NO_METHOD:
  170. case UNKNOWN_METHOD:
  171. default:
  172. return NULL;
  173. }
  174. }
  175. /** Return a string representation of the version of the library, found at
  176. * compile time, providing the compression method given in <b>method</b>.
  177. * Returns NULL if <b>method</b> is unknown or unsupported. */
  178. const char *
  179. tor_compress_header_version_str(compress_method_t method)
  180. {
  181. switch (method) {
  182. case GZIP_METHOD:
  183. case ZLIB_METHOD:
  184. return tor_zlib_get_header_version_str();
  185. case LZMA_METHOD:
  186. return tor_lzma_get_header_version_str();
  187. case ZSTD_METHOD:
  188. return tor_zstd_get_header_version_str();
  189. case NO_METHOD:
  190. case UNKNOWN_METHOD:
  191. default:
  192. return NULL;
  193. }
  194. }
  195. /** Return the approximate number of bytes allocated for all
  196. * supported compression schemas. */
  197. size_t
  198. tor_compress_get_total_allocation(void)
  199. {
  200. return tor_zlib_get_total_allocation() +
  201. tor_lzma_get_total_allocation() +
  202. tor_zstd_get_total_allocation();
  203. }
  204. /** Internal state for an incremental compression/decompression. The body of
  205. * this struct is not exposed. */
  206. struct tor_compress_state_t {
  207. compress_method_t method; /**< The compression method. */
  208. union {
  209. tor_zlib_compress_state_t *zlib_state;
  210. tor_lzma_compress_state_t *lzma_state;
  211. tor_zstd_compress_state_t *zstd_state;
  212. } u; /**< Compression backend state. */
  213. };
  214. /** Construct and return a tor_compress_state_t object using <b>method</b>. If
  215. * <b>compress</b>, it's for compression; otherwise it's for decompression. */
  216. tor_compress_state_t *
  217. tor_compress_new(int compress, compress_method_t method,
  218. compression_level_t compression_level)
  219. {
  220. tor_compress_state_t *state;
  221. state = tor_malloc_zero(sizeof(tor_compress_state_t));
  222. state->method = method;
  223. switch (method) {
  224. case GZIP_METHOD:
  225. case ZLIB_METHOD: {
  226. tor_zlib_compress_state_t *zlib_state =
  227. tor_zlib_compress_new(compress, method, compression_level);
  228. if (zlib_state == NULL)
  229. goto err;
  230. state->u.zlib_state = zlib_state;
  231. break;
  232. }
  233. case LZMA_METHOD: {
  234. tor_lzma_compress_state_t *lzma_state =
  235. tor_lzma_compress_new(compress, method, compression_level);
  236. if (lzma_state == NULL)
  237. goto err;
  238. state->u.lzma_state = lzma_state;
  239. break;
  240. }
  241. case ZSTD_METHOD: {
  242. tor_zstd_compress_state_t *zstd_state =
  243. tor_zstd_compress_new(compress, method, compression_level);
  244. if (zstd_state == NULL)
  245. goto err;
  246. state->u.zstd_state = zstd_state;
  247. break;
  248. }
  249. case NO_METHOD:
  250. case UNKNOWN_METHOD:
  251. goto err;
  252. }
  253. return state;
  254. err:
  255. tor_free(state);
  256. return NULL;
  257. }
  258. /** Compress/decompress some bytes using <b>state</b>. Read up to
  259. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  260. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  261. * we've reached the end of the input.
  262. *
  263. * Return TOR_COMPRESS_DONE if we've finished the entire
  264. * compression/decompression.
  265. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  266. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  267. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  268. */
  269. tor_compress_output_t
  270. tor_compress_process(tor_compress_state_t *state,
  271. char **out, size_t *out_len,
  272. const char **in, size_t *in_len,
  273. int finish)
  274. {
  275. tor_assert(state != NULL);
  276. switch (state->method) {
  277. case GZIP_METHOD:
  278. case ZLIB_METHOD:
  279. return tor_zlib_compress_process(state->u.zlib_state,
  280. out, out_len, in, in_len,
  281. finish);
  282. case LZMA_METHOD:
  283. return tor_lzma_compress_process(state->u.lzma_state,
  284. out, out_len, in, in_len,
  285. finish);
  286. case ZSTD_METHOD:
  287. return tor_zstd_compress_process(state->u.zstd_state,
  288. out, out_len, in, in_len,
  289. finish);
  290. case NO_METHOD:
  291. case UNKNOWN_METHOD:
  292. goto err;
  293. }
  294. err:
  295. return TOR_COMPRESS_ERROR;
  296. }
  297. /** Deallocate <b>state</b>. */
  298. void
  299. tor_compress_free(tor_compress_state_t *state)
  300. {
  301. if (state == NULL)
  302. return;
  303. switch (state->method) {
  304. case GZIP_METHOD:
  305. case ZLIB_METHOD:
  306. tor_zlib_compress_free(state->u.zlib_state);
  307. break;
  308. case LZMA_METHOD:
  309. tor_lzma_compress_free(state->u.lzma_state);
  310. break;
  311. case ZSTD_METHOD:
  312. tor_zstd_compress_free(state->u.zstd_state);
  313. break;
  314. case NO_METHOD:
  315. case UNKNOWN_METHOD:
  316. break;
  317. }
  318. tor_free(state);
  319. }
  320. /** Return the approximate number of bytes allocated for <b>state</b>. */
  321. size_t
  322. tor_compress_state_size(const tor_compress_state_t *state)
  323. {
  324. tor_assert(state != NULL);
  325. switch (state->method) {
  326. case GZIP_METHOD:
  327. case ZLIB_METHOD:
  328. return tor_zlib_compress_state_size(state->u.zlib_state);
  329. case LZMA_METHOD:
  330. return tor_lzma_compress_state_size(state->u.lzma_state);
  331. case ZSTD_METHOD:
  332. return tor_zstd_compress_state_size(state->u.zstd_state);
  333. case NO_METHOD:
  334. case UNKNOWN_METHOD:
  335. goto err;
  336. }
  337. err:
  338. return 0;
  339. }