compress.h 3.3 KB

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