Merge branch 'maint-0.4.8'
[tor.git] / src / lib / compress / compress.h
blobc15434a1aea2b840a5708388e3128df412eb38f3
1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file compress.h
8 * \brief Headers for compress.c
9 **/
11 #ifndef TOR_COMPRESS_H
12 #define TOR_COMPRESS_H
14 #include <stddef.h>
15 #include "lib/testsupport/testsupport.h"
17 /** Enumeration of what kind of compression to use. Only ZLIB_METHOD and
18 * GZIP_METHOD is guaranteed to be supported by the compress/uncompress
19 * functions here. Call tor_compress_supports_method() to check if a given
20 * compression schema is supported by Tor. */
21 typedef enum compress_method_t {
22 NO_METHOD=0, // This method must be first.
23 GZIP_METHOD=1,
24 ZLIB_METHOD=2,
25 LZMA_METHOD=3,
26 ZSTD_METHOD=4,
27 UNKNOWN_METHOD=5, // This method must be last. Add new ones in the middle.
28 } compress_method_t;
30 /**
31 * Enumeration to define tradeoffs between memory usage and compression level.
32 * BEST_COMPRESSION saves the most bandwidth; LOW_COMPRESSION saves the most
33 * memory.
34 **/
35 typedef enum compression_level_t {
36 BEST_COMPRESSION, HIGH_COMPRESSION, MEDIUM_COMPRESSION, LOW_COMPRESSION
37 } compression_level_t;
39 int tor_compress(char **out, size_t *out_len,
40 const char *in, size_t in_len,
41 compress_method_t method);
43 int tor_uncompress(char **out, size_t *out_len,
44 const char *in, size_t in_len,
45 compress_method_t method,
46 int complete_only,
47 int protocol_warn_level);
49 compress_method_t detect_compression_method(const char *in, size_t in_len);
51 MOCK_DECL(int,tor_compress_is_compression_bomb,(size_t size_in,
52 size_t size_out));
54 int tor_compress_supports_method(compress_method_t method);
55 unsigned tor_compress_get_supported_method_bitmask(void);
56 const char *compression_method_get_name(compress_method_t method);
57 const char *compression_method_get_human_name(compress_method_t method);
58 compress_method_t compression_method_get_by_name(const char *name);
60 const char *tor_compress_version_str(compress_method_t method);
62 const char *tor_compress_header_version_str(compress_method_t method);
64 size_t tor_compress_get_total_allocation(void);
66 /** Return values from tor_compress_process; see that function's documentation
67 * for details. */
68 typedef enum {
69 TOR_COMPRESS_OK,
70 TOR_COMPRESS_DONE,
71 TOR_COMPRESS_BUFFER_FULL,
72 TOR_COMPRESS_ERROR
73 } tor_compress_output_t;
75 /** Internal state for an incremental compression/decompression. */
76 typedef struct tor_compress_state_t tor_compress_state_t;
78 tor_compress_state_t *tor_compress_new(int compress,
79 compress_method_t method,
80 compression_level_t level);
82 tor_compress_output_t tor_compress_process(tor_compress_state_t *state,
83 char **out, size_t *out_len,
84 const char **in, size_t *in_len,
85 int finish);
86 void tor_compress_free_(tor_compress_state_t *state);
87 #define tor_compress_free(st) \
88 FREE_AND_NULL(tor_compress_state_t, tor_compress_free_, (st))
90 size_t tor_compress_state_size(const tor_compress_state_t *state);
92 int tor_compress_init(void);
93 void tor_compress_log_init_warnings(void);
95 struct buf_t;
96 int buf_add_compress(struct buf_t *buf, struct tor_compress_state_t *state,
97 const char *data, size_t data_len, int done);
99 #endif /* !defined(TOR_COMPRESS_H) */