compress.c 21 KB

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