compress.c 20 KB

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