compress.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (c) 2003, 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.h
  7. * \brief Headers for compress.c
  8. **/
  9. #ifndef TOR_COMPRESS_H
  10. #define TOR_COMPRESS_H
  11. #include <stddef.h>
  12. #include "lib/testsupport/testsupport.h"
  13. /** Enumeration of what kind of compression to use. Only ZLIB_METHOD and
  14. * GZIP_METHOD is guaranteed to be supported by the compress/uncompress
  15. * functions here. Call tor_compress_supports_method() to check if a given
  16. * compression schema is supported by Tor. */
  17. typedef enum compress_method_t {
  18. NO_METHOD=0, // This method must be first.
  19. GZIP_METHOD=1,
  20. ZLIB_METHOD=2,
  21. LZMA_METHOD=3,
  22. ZSTD_METHOD=4,
  23. UNKNOWN_METHOD=5, // This method must be last. Add new ones in the middle.
  24. } compress_method_t;
  25. /**
  26. * Enumeration to define tradeoffs between memory usage and compression level.
  27. * BEST_COMPRESSION saves the most bandwidth; LOW_COMPRESSION saves the most
  28. * memory.
  29. **/
  30. typedef enum compression_level_t {
  31. BEST_COMPRESSION, HIGH_COMPRESSION, MEDIUM_COMPRESSION, LOW_COMPRESSION
  32. } compression_level_t;
  33. int tor_compress(char **out, size_t *out_len,
  34. const char *in, size_t in_len,
  35. compress_method_t method);
  36. int tor_uncompress(char **out, size_t *out_len,
  37. const char *in, size_t in_len,
  38. compress_method_t method,
  39. int complete_only,
  40. int protocol_warn_level);
  41. compress_method_t detect_compression_method(const char *in, size_t in_len);
  42. MOCK_DECL(int,tor_compress_is_compression_bomb,(size_t size_in,
  43. size_t size_out));
  44. int tor_compress_supports_method(compress_method_t method);
  45. unsigned tor_compress_get_supported_method_bitmask(void);
  46. const char *compression_method_get_name(compress_method_t method);
  47. const char *compression_method_get_human_name(compress_method_t method);
  48. compress_method_t compression_method_get_by_name(const char *name);
  49. const char *tor_compress_version_str(compress_method_t method);
  50. const char *tor_compress_header_version_str(compress_method_t method);
  51. size_t tor_compress_get_total_allocation(void);
  52. /** Return values from tor_compress_process; see that function's documentation
  53. * for details. */
  54. typedef enum {
  55. TOR_COMPRESS_OK,
  56. TOR_COMPRESS_DONE,
  57. TOR_COMPRESS_BUFFER_FULL,
  58. TOR_COMPRESS_ERROR
  59. } tor_compress_output_t;
  60. /** Internal state for an incremental compression/decompression. */
  61. typedef struct tor_compress_state_t tor_compress_state_t;
  62. tor_compress_state_t *tor_compress_new(int compress,
  63. compress_method_t method,
  64. compression_level_t level);
  65. tor_compress_output_t tor_compress_process(tor_compress_state_t *state,
  66. char **out, size_t *out_len,
  67. const char **in, size_t *in_len,
  68. int finish);
  69. void tor_compress_free_(tor_compress_state_t *state);
  70. #define tor_compress_free(st) \
  71. FREE_AND_NULL(tor_compress_state_t, tor_compress_free_, (st))
  72. size_t tor_compress_state_size(const tor_compress_state_t *state);
  73. void tor_compress_init(void);
  74. void tor_compress_log_init_warnings(void);
  75. struct buf_t;
  76. int buf_add_compress(struct buf_t *buf, struct tor_compress_state_t *state,
  77. const char *data, size_t data_len, int done);
  78. #endif /* !defined(TOR_COMPRESS_H) */