compress.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, 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. int tor_compress_is_compression_bomb(size_t size_in, size_t size_out);
  41. int tor_compress_supports_method(compress_method_t method);
  42. unsigned tor_compress_get_supported_method_bitmask(void);
  43. const char *compression_method_get_name(compress_method_t method);
  44. const char *compression_method_get_human_name(compress_method_t method);
  45. compress_method_t compression_method_get_by_name(const char *name);
  46. const char *tor_compress_version_str(compress_method_t method);
  47. const char *tor_compress_header_version_str(compress_method_t method);
  48. size_t tor_compress_get_total_allocation(void);
  49. /** Return values from tor_compress_process; see that function's documentation
  50. * for details. */
  51. typedef enum {
  52. TOR_COMPRESS_OK,
  53. TOR_COMPRESS_DONE,
  54. TOR_COMPRESS_BUFFER_FULL,
  55. TOR_COMPRESS_ERROR
  56. } tor_compress_output_t;
  57. /** Internal state for an incremental compression/decompression. */
  58. typedef struct tor_compress_state_t tor_compress_state_t;
  59. tor_compress_state_t *tor_compress_new(int compress,
  60. compress_method_t method,
  61. compression_level_t level);
  62. tor_compress_output_t tor_compress_process(tor_compress_state_t *state,
  63. char **out, size_t *out_len,
  64. const char **in, size_t *in_len,
  65. int finish);
  66. void tor_compress_free(tor_compress_state_t *state);
  67. size_t tor_compress_state_size(const tor_compress_state_t *state);
  68. void tor_compress_init(void);
  69. #endif // TOR_COMPRESS_H.