compress.c 20 KB

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