compress.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /* Copyright (c) 2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, 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 <string.h>
  13. #include "lib/cc/torint.h"
  14. #ifdef HAVE_NETINET_IN_H
  15. #include <netinet/in.h>
  16. #endif
  17. #include "lib/log/torlog.h"
  18. #include "lib/log/util_bug.h"
  19. #include "lib/arch/bytes.h"
  20. #include "lib/ctime/di_ops.h"
  21. #include "lib/compress/compress.h"
  22. #include "lib/compress/compress_lzma.h"
  23. #include "lib/compress/compress_none.h"
  24. #include "lib/compress/compress_zlib.h"
  25. #include "lib/compress/compress_zstd.h"
  26. #include "lib/intmath/cmp.h"
  27. #include "lib/malloc/util_malloc.h"
  28. #include "lib/thread/threads.h"
  29. /** Total number of bytes allocated for compression state overhead. */
  30. static atomic_counter_t total_compress_allocation;
  31. /** @{ */
  32. /* These macros define the maximum allowable compression factor. Anything of
  33. * size greater than CHECK_FOR_COMPRESSION_BOMB_AFTER is not allowed to
  34. * have an uncompression factor (uncompressed size:compressed size ratio) of
  35. * any greater than MAX_UNCOMPRESSION_FACTOR.
  36. *
  37. * Picking a value for MAX_UNCOMPRESSION_FACTOR is a trade-off: we want it to
  38. * be small to limit the attack multiplier, but we also want it to be large
  39. * enough so that no legitimate document --even ones we might invent in the
  40. * future -- ever compresses by a factor of greater than
  41. * MAX_UNCOMPRESSION_FACTOR. Within those parameters, there's a reasonably
  42. * large range of possible values. IMO, anything over 8 is probably safe; IMO
  43. * anything under 50 is probably sufficient.
  44. */
  45. #define MAX_UNCOMPRESSION_FACTOR 25
  46. #define CHECK_FOR_COMPRESSION_BOMB_AFTER (1024*64)
  47. /** @} */
  48. /** Return true if uncompressing an input of size <b>in_size</b> to an input of
  49. * size at least <b>size_out</b> looks like a compression bomb. */
  50. MOCK_IMPL(int,
  51. tor_compress_is_compression_bomb,(size_t size_in, size_t size_out))
  52. {
  53. if (size_in == 0 || size_out < CHECK_FOR_COMPRESSION_BOMB_AFTER)
  54. return 0;
  55. return (size_out / size_in > MAX_UNCOMPRESSION_FACTOR);
  56. }
  57. /** Guess the size that <b>in_len</b> will be after compression or
  58. * decompression. */
  59. static size_t
  60. guess_compress_size(int compress, compress_method_t method,
  61. compression_level_t compression_level,
  62. size_t in_len)
  63. {
  64. // ignore these for now.
  65. (void)compression_level;
  66. if (method == NO_METHOD) {
  67. /* Guess that we'll need an extra byte, to avoid a needless realloc
  68. * for nul-termination */
  69. return (in_len < SIZE_MAX) ? in_len + 1 : in_len;
  70. }
  71. /* Always guess a factor of 2. */
  72. if (compress) {
  73. in_len /= 2;
  74. } else {
  75. if (in_len < SIZE_T_CEILING/2)
  76. in_len *= 2;
  77. }
  78. return MAX(in_len, 1024);
  79. }
  80. /** Internal function to implement tor_compress/tor_uncompress, depending on
  81. * whether <b>compress</b> is set. All arguments are as for tor_compress or
  82. * tor_uncompress. */
  83. static int
  84. tor_compress_impl(int compress,
  85. char **out, size_t *out_len,
  86. const char *in, size_t in_len,
  87. compress_method_t method,
  88. compression_level_t compression_level,
  89. int complete_only,
  90. int protocol_warn_level)
  91. {
  92. tor_compress_state_t *stream;
  93. int rv;
  94. stream = tor_compress_new(compress, method, compression_level);
  95. if (stream == NULL) {
  96. log_warn(LD_GENERAL, "NULL stream while %scompressing",
  97. compress?"":"de");
  98. log_debug(LD_GENERAL, "method: %d level: %d at len: %lu",
  99. method, compression_level, (unsigned long)in_len);
  100. return -1;
  101. }
  102. size_t in_len_orig = in_len;
  103. size_t out_remaining, out_alloc;
  104. char *outptr;
  105. out_remaining = out_alloc =
  106. guess_compress_size(compress, method, compression_level, in_len);
  107. *out = outptr = tor_malloc(out_remaining);
  108. const int finish = complete_only || compress;
  109. while (1) {
  110. switch (tor_compress_process(stream,
  111. &outptr, &out_remaining,
  112. &in, &in_len, finish)) {
  113. case TOR_COMPRESS_DONE:
  114. if (in_len == 0 || compress) {
  115. goto done;
  116. } else {
  117. // More data is present, and we're decompressing. So we may need to
  118. // reinitialize the stream if we are handling multiple concatenated
  119. // inputs.
  120. tor_compress_free(stream);
  121. stream = tor_compress_new(compress, method, compression_level);
  122. if (stream == NULL) {
  123. log_warn(LD_GENERAL, "NULL stream while %scompressing",
  124. compress?"":"de");
  125. goto err;
  126. }
  127. }
  128. break;
  129. case TOR_COMPRESS_OK:
  130. if (compress || complete_only) {
  131. log_fn(protocol_warn_level, LD_PROTOCOL,
  132. "Unexpected %s while %scompressing",
  133. complete_only?"end of input":"result",
  134. compress?"":"de");
  135. log_debug(LD_GENERAL, "method: %d level: %d at len: %lu",
  136. method, compression_level, (unsigned long)in_len);
  137. goto err;
  138. } else {
  139. if (in_len == 0) {
  140. goto done;
  141. }
  142. }
  143. break;
  144. case TOR_COMPRESS_BUFFER_FULL: {
  145. if (!compress && outptr < *out+out_alloc) {
  146. // A buffer error in this case means that we have a problem
  147. // with our input.
  148. log_fn(protocol_warn_level, LD_PROTOCOL,
  149. "Possible truncated or corrupt compressed data");
  150. goto err;
  151. }
  152. if (out_alloc >= SIZE_T_CEILING / 2) {
  153. log_warn(LD_GENERAL, "While %scompressing data: ran out of space.",
  154. compress?"":"un");
  155. goto err;
  156. }
  157. if (!compress &&
  158. tor_compress_is_compression_bomb(in_len_orig, out_alloc)) {
  159. // This should already have been caught down in the backend logic.
  160. // LCOV_EXCL_START
  161. tor_assert_nonfatal_unreached();
  162. goto err;
  163. // LCOV_EXCL_STOP
  164. }
  165. const size_t offset = outptr - *out;
  166. out_alloc *= 2;
  167. *out = tor_realloc(*out, out_alloc);
  168. outptr = *out + offset;
  169. out_remaining = out_alloc - offset;
  170. break;
  171. }
  172. case TOR_COMPRESS_ERROR:
  173. log_fn(protocol_warn_level, LD_GENERAL,
  174. "Error while %scompressing data: bad input?",
  175. compress?"":"un");
  176. goto err; // bad data.
  177. // LCOV_EXCL_START
  178. default:
  179. tor_assert_nonfatal_unreached();
  180. goto err;
  181. // LCOV_EXCL_STOP
  182. }
  183. }
  184. done:
  185. *out_len = outptr - *out;
  186. if (compress && tor_compress_is_compression_bomb(*out_len, in_len_orig)) {
  187. log_warn(LD_BUG, "We compressed something and got an insanely high "
  188. "compression factor; other Tors would think this was a "
  189. "compression bomb.");
  190. goto err;
  191. }
  192. if (!compress) {
  193. // NUL-terminate our output.
  194. if (out_alloc == *out_len)
  195. *out = tor_realloc(*out, out_alloc + 1);
  196. (*out)[*out_len] = '\0';
  197. }
  198. rv = 0;
  199. goto out;
  200. err:
  201. tor_free(*out);
  202. *out_len = 0;
  203. rv = -1;
  204. goto out;
  205. out:
  206. tor_compress_free(stream);
  207. return rv;
  208. }
  209. /** Given <b>in_len</b> bytes at <b>in</b>, compress them into a newly
  210. * allocated buffer, using the method described in <b>method</b>. Store the
  211. * compressed string in *<b>out</b>, and its length in *<b>out_len</b>.
  212. * Return 0 on success, -1 on failure.
  213. */
  214. int
  215. tor_compress(char **out, size_t *out_len,
  216. const char *in, size_t in_len,
  217. compress_method_t method)
  218. {
  219. return tor_compress_impl(1, out, out_len, in, in_len, method,
  220. BEST_COMPRESSION,
  221. 1, LOG_WARN);
  222. }
  223. /** Given zero or more compressed strings of total length <b>in_len</b> bytes
  224. * at <b>in</b>, uncompress them into a newly allocated buffer, using the
  225. * method described in <b>method</b>. Store the uncompressed string in
  226. * *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on success, -1 on
  227. * failure.
  228. *
  229. * If any bytes are written to <b>out</b>, an extra byte NUL is always
  230. * written at the end, but not counted in <b>out_len</b>. This is a
  231. * safety feature to ensure that the output can be treated as a
  232. * NUL-terminated string -- though of course, callers should check
  233. * out_len anyway.
  234. *
  235. * If <b>complete_only</b> is true, we consider a truncated input as a
  236. * failure; otherwise we decompress as much as we can. Warn about truncated
  237. * or corrupt inputs at <b>protocol_warn_level</b>.
  238. */
  239. int
  240. tor_uncompress(char **out, size_t *out_len,
  241. const char *in, size_t in_len,
  242. compress_method_t method,
  243. int complete_only,
  244. int protocol_warn_level)
  245. {
  246. return tor_compress_impl(0, out, out_len, in, in_len, method,
  247. BEST_COMPRESSION,
  248. complete_only, protocol_warn_level);
  249. }
  250. /** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
  251. * to be compressed or not. If it is, return the likeliest compression method.
  252. * Otherwise, return UNKNOWN_METHOD.
  253. */
  254. compress_method_t
  255. detect_compression_method(const char *in, size_t in_len)
  256. {
  257. if (in_len > 2 && fast_memeq(in, "\x1f\x8b", 2)) {
  258. return GZIP_METHOD;
  259. } else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
  260. (tor_ntohs(get_uint16(in)) % 31) == 0) {
  261. return ZLIB_METHOD;
  262. } else if (in_len > 2 &&
  263. fast_memeq(in, "\x5d\x00\x00", 3)) {
  264. return LZMA_METHOD;
  265. } else if (in_len > 3 &&
  266. fast_memeq(in, "\x28\xb5\x2f\xfd", 4)) {
  267. return ZSTD_METHOD;
  268. } else {
  269. return UNKNOWN_METHOD;
  270. }
  271. }
  272. /** Return 1 if a given <b>method</b> is supported; otherwise 0. */
  273. int
  274. tor_compress_supports_method(compress_method_t method)
  275. {
  276. switch (method) {
  277. case GZIP_METHOD:
  278. case ZLIB_METHOD:
  279. return tor_zlib_method_supported();
  280. case LZMA_METHOD:
  281. return tor_lzma_method_supported();
  282. case ZSTD_METHOD:
  283. return tor_zstd_method_supported();
  284. case NO_METHOD:
  285. return 1;
  286. case UNKNOWN_METHOD:
  287. default:
  288. return 0;
  289. }
  290. }
  291. /**
  292. * Return a bitmask of the supported compression types, where 1&lt;&lt;m is
  293. * set in the bitmask if and only if compression with method <b>m</b> is
  294. * supported.
  295. */
  296. unsigned
  297. tor_compress_get_supported_method_bitmask(void)
  298. {
  299. static unsigned supported = 0;
  300. if (supported == 0) {
  301. compress_method_t m;
  302. for (m = NO_METHOD; m <= UNKNOWN_METHOD; ++m) {
  303. if (tor_compress_supports_method(m)) {
  304. supported |= (1u << m);
  305. }
  306. }
  307. }
  308. return supported;
  309. }
  310. /** Table of compression method names. These should have an "x-" prefix,
  311. * if they are not listed in the IANA content coding registry. */
  312. static const struct {
  313. const char *name;
  314. compress_method_t method;
  315. } compression_method_names[] = {
  316. { "gzip", GZIP_METHOD },
  317. { "deflate", ZLIB_METHOD },
  318. // We call this "x-tor-lzma" rather than "x-lzma", because we impose a
  319. // lower maximum memory usage on the decoding side.
  320. { "x-tor-lzma", LZMA_METHOD },
  321. { "x-zstd" , ZSTD_METHOD },
  322. { "identity", NO_METHOD },
  323. /* Later entries in this table are not canonical; these are recognized but
  324. * not emitted. */
  325. { "x-gzip", GZIP_METHOD },
  326. };
  327. /** Return the canonical string representation of the compression method
  328. * <b>method</b>, or NULL if the method isn't recognized. */
  329. const char *
  330. compression_method_get_name(compress_method_t method)
  331. {
  332. unsigned i;
  333. for (i = 0; i < ARRAY_LENGTH(compression_method_names); ++i) {
  334. if (method == compression_method_names[i].method)
  335. return compression_method_names[i].name;
  336. }
  337. return NULL;
  338. }
  339. /** Table of compression human readable method names. */
  340. static const struct {
  341. compress_method_t method;
  342. const char *name;
  343. } compression_method_human_names[] = {
  344. { NO_METHOD, "uncompressed" },
  345. { GZIP_METHOD, "gzipped" },
  346. { ZLIB_METHOD, "deflated" },
  347. { LZMA_METHOD, "LZMA compressed" },
  348. { ZSTD_METHOD, "Zstandard compressed" },
  349. { UNKNOWN_METHOD, "unknown encoding" },
  350. };
  351. /** Return a human readable string representation of the compression method
  352. * <b>method</b>, or NULL if the method isn't recognized. */
  353. const char *
  354. compression_method_get_human_name(compress_method_t method)
  355. {
  356. unsigned i;
  357. for (i = 0; i < ARRAY_LENGTH(compression_method_human_names); ++i) {
  358. if (method == compression_method_human_names[i].method)
  359. return compression_method_human_names[i].name;
  360. }
  361. return NULL;
  362. }
  363. /** Return the compression method represented by the string <b>name</b>, or
  364. * UNKNOWN_METHOD if the string isn't recognized. */
  365. compress_method_t
  366. compression_method_get_by_name(const char *name)
  367. {
  368. unsigned i;
  369. for (i = 0; i < ARRAY_LENGTH(compression_method_names); ++i) {
  370. if (!strcmp(compression_method_names[i].name, name))
  371. return compression_method_names[i].method;
  372. }
  373. return UNKNOWN_METHOD;
  374. }
  375. /** Return a string representation of the version of the library providing the
  376. * compression method given in <b>method</b>. Returns NULL if <b>method</b> is
  377. * unknown or unsupported. */
  378. const char *
  379. tor_compress_version_str(compress_method_t method)
  380. {
  381. switch (method) {
  382. case GZIP_METHOD:
  383. case ZLIB_METHOD:
  384. return tor_zlib_get_version_str();
  385. case LZMA_METHOD:
  386. return tor_lzma_get_version_str();
  387. case ZSTD_METHOD:
  388. return tor_zstd_get_version_str();
  389. case NO_METHOD:
  390. case UNKNOWN_METHOD:
  391. default:
  392. return NULL;
  393. }
  394. }
  395. /** Return a string representation of the version of the library, found at
  396. * compile time, providing the compression method given in <b>method</b>.
  397. * Returns NULL if <b>method</b> is unknown or unsupported. */
  398. const char *
  399. tor_compress_header_version_str(compress_method_t method)
  400. {
  401. switch (method) {
  402. case GZIP_METHOD:
  403. case ZLIB_METHOD:
  404. return tor_zlib_get_header_version_str();
  405. case LZMA_METHOD:
  406. return tor_lzma_get_header_version_str();
  407. case ZSTD_METHOD:
  408. return tor_zstd_get_header_version_str();
  409. case NO_METHOD:
  410. case UNKNOWN_METHOD:
  411. default:
  412. return NULL;
  413. }
  414. }
  415. /** Return the approximate number of bytes allocated for all
  416. * supported compression schemas. */
  417. size_t
  418. tor_compress_get_total_allocation(void)
  419. {
  420. return atomic_counter_get(&total_compress_allocation) +
  421. tor_zlib_get_total_allocation() +
  422. tor_lzma_get_total_allocation() +
  423. tor_zstd_get_total_allocation();
  424. }
  425. /** Internal state for an incremental compression/decompression. The body of
  426. * this struct is not exposed. */
  427. struct tor_compress_state_t {
  428. compress_method_t method; /**< The compression method. */
  429. union {
  430. tor_zlib_compress_state_t *zlib_state;
  431. tor_lzma_compress_state_t *lzma_state;
  432. tor_zstd_compress_state_t *zstd_state;
  433. } u; /**< Compression backend state. */
  434. };
  435. /** Construct and return a tor_compress_state_t object using <b>method</b>. If
  436. * <b>compress</b>, it's for compression; otherwise it's for decompression. */
  437. tor_compress_state_t *
  438. tor_compress_new(int compress, compress_method_t method,
  439. compression_level_t compression_level)
  440. {
  441. tor_compress_state_t *state;
  442. state = tor_malloc_zero(sizeof(tor_compress_state_t));
  443. state->method = method;
  444. switch (method) {
  445. case GZIP_METHOD:
  446. case ZLIB_METHOD: {
  447. tor_zlib_compress_state_t *zlib_state =
  448. tor_zlib_compress_new(compress, method, compression_level);
  449. if (zlib_state == NULL)
  450. goto err;
  451. state->u.zlib_state = zlib_state;
  452. break;
  453. }
  454. case LZMA_METHOD: {
  455. tor_lzma_compress_state_t *lzma_state =
  456. tor_lzma_compress_new(compress, method, compression_level);
  457. if (lzma_state == NULL)
  458. goto err;
  459. state->u.lzma_state = lzma_state;
  460. break;
  461. }
  462. case ZSTD_METHOD: {
  463. tor_zstd_compress_state_t *zstd_state =
  464. tor_zstd_compress_new(compress, method, compression_level);
  465. if (zstd_state == NULL)
  466. goto err;
  467. state->u.zstd_state = zstd_state;
  468. break;
  469. }
  470. case NO_METHOD: {
  471. break;
  472. }
  473. case UNKNOWN_METHOD:
  474. goto err;
  475. }
  476. atomic_counter_add(&total_compress_allocation,
  477. sizeof(tor_compress_state_t));
  478. return state;
  479. err:
  480. tor_free(state);
  481. return NULL;
  482. }
  483. /** Compress/decompress some bytes using <b>state</b>. Read up to
  484. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  485. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  486. * we've reached the end of the input.
  487. *
  488. * Return TOR_COMPRESS_DONE if we've finished the entire
  489. * compression/decompression.
  490. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  491. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  492. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  493. */
  494. tor_compress_output_t
  495. tor_compress_process(tor_compress_state_t *state,
  496. char **out, size_t *out_len,
  497. const char **in, size_t *in_len,
  498. int finish)
  499. {
  500. tor_assert(state != NULL);
  501. const size_t in_len_orig = *in_len;
  502. const size_t out_len_orig = *out_len;
  503. tor_compress_output_t rv;
  504. if (*out_len == 0 && (*in_len > 0 || finish)) {
  505. // If we still have input data, but no space for output data, we might as
  506. // well return early and let the caller do the reallocation of the out
  507. // variable.
  508. return TOR_COMPRESS_BUFFER_FULL;
  509. }
  510. switch (state->method) {
  511. case GZIP_METHOD:
  512. case ZLIB_METHOD:
  513. rv = tor_zlib_compress_process(state->u.zlib_state,
  514. out, out_len, in, in_len,
  515. finish);
  516. break;
  517. case LZMA_METHOD:
  518. rv = tor_lzma_compress_process(state->u.lzma_state,
  519. out, out_len, in, in_len,
  520. finish);
  521. break;
  522. case ZSTD_METHOD:
  523. rv = tor_zstd_compress_process(state->u.zstd_state,
  524. out, out_len, in, in_len,
  525. finish);
  526. break;
  527. case NO_METHOD:
  528. rv = tor_cnone_compress_process(out, out_len, in, in_len,
  529. finish);
  530. break;
  531. default:
  532. case UNKNOWN_METHOD:
  533. goto err;
  534. }
  535. if (BUG((rv == TOR_COMPRESS_OK) &&
  536. *in_len == in_len_orig &&
  537. *out_len == out_len_orig)) {
  538. log_warn(LD_GENERAL,
  539. "More info on the bug: method == %s, finish == %d, "
  540. " *in_len == in_len_orig == %lu, "
  541. "*out_len == out_len_orig == %lu",
  542. compression_method_get_human_name(state->method), finish,
  543. (unsigned long)in_len_orig, (unsigned long)out_len_orig);
  544. return TOR_COMPRESS_ERROR;
  545. }
  546. return rv;
  547. err:
  548. return TOR_COMPRESS_ERROR;
  549. }
  550. /** Deallocate <b>state</b>. */
  551. void
  552. tor_compress_free_(tor_compress_state_t *state)
  553. {
  554. if (state == NULL)
  555. return;
  556. switch (state->method) {
  557. case GZIP_METHOD:
  558. case ZLIB_METHOD:
  559. tor_zlib_compress_free(state->u.zlib_state);
  560. break;
  561. case LZMA_METHOD:
  562. tor_lzma_compress_free(state->u.lzma_state);
  563. break;
  564. case ZSTD_METHOD:
  565. tor_zstd_compress_free(state->u.zstd_state);
  566. break;
  567. case NO_METHOD:
  568. break;
  569. case UNKNOWN_METHOD:
  570. break;
  571. }
  572. atomic_counter_sub(&total_compress_allocation,
  573. sizeof(tor_compress_state_t));
  574. tor_free(state);
  575. }
  576. /** Return the approximate number of bytes allocated for <b>state</b>. */
  577. size_t
  578. tor_compress_state_size(const tor_compress_state_t *state)
  579. {
  580. tor_assert(state != NULL);
  581. size_t size = sizeof(tor_compress_state_t);
  582. switch (state->method) {
  583. case GZIP_METHOD:
  584. case ZLIB_METHOD:
  585. size += tor_zlib_compress_state_size(state->u.zlib_state);
  586. break;
  587. case LZMA_METHOD:
  588. size += tor_lzma_compress_state_size(state->u.lzma_state);
  589. break;
  590. case ZSTD_METHOD:
  591. size += tor_zstd_compress_state_size(state->u.zstd_state);
  592. break;
  593. case NO_METHOD:
  594. case UNKNOWN_METHOD:
  595. break;
  596. }
  597. return size;
  598. }
  599. /** Initialize all compression modules. */
  600. void
  601. tor_compress_init(void)
  602. {
  603. atomic_counter_init(&total_compress_allocation);
  604. tor_zlib_init();
  605. tor_lzma_init();
  606. tor_zstd_init();
  607. }
  608. /** Warn if we had any problems while setting up our compression libraries.
  609. *
  610. * (This isn't part of tor_compress_init, since the logs aren't set up yet.)
  611. */
  612. void
  613. tor_compress_log_init_warnings(void)
  614. {
  615. tor_zstd_warn_if_version_mismatched();
  616. }