aboutsummaryrefslogtreecommitdiff
path: root/src/lib/compress/compress.h
blob: f36cdb82aab03066e4a3bbbb6435a150e01e3a0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Copyright (c) 2003, Roger Dingledine
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 * Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file compress.h
 * \brief Headers for compress.c
 **/

#ifndef TOR_COMPRESS_H
#define TOR_COMPRESS_H

#include <stddef.h>
#include "lib/testsupport/testsupport.h"

/** Enumeration of what kind of compression to use.  Only ZLIB_METHOD and
 * GZIP_METHOD is guaranteed to be supported by the compress/uncompress
 * functions here. Call tor_compress_supports_method() to check if a given
 * compression schema is supported by Tor. */
typedef enum compress_method_t {
  NO_METHOD=0, // This method must be first.
  GZIP_METHOD=1,
  ZLIB_METHOD=2,
  LZMA_METHOD=3,
  ZSTD_METHOD=4,
  UNKNOWN_METHOD=5, // This method must be last. Add new ones in the middle.
} compress_method_t;

/**
 * Enumeration to define tradeoffs between memory usage and compression level.
 * BEST_COMPRESSION saves the most bandwidth; LOW_COMPRESSION saves the most
 * memory.
 **/
typedef enum compression_level_t {
  BEST_COMPRESSION, HIGH_COMPRESSION, MEDIUM_COMPRESSION, LOW_COMPRESSION
} compression_level_t;

int tor_compress(char **out, size_t *out_len,
                 const char *in, size_t in_len,
                 compress_method_t method);

int tor_uncompress(char **out, size_t *out_len,
                   const char *in, size_t in_len,
                   compress_method_t method,
                   int complete_only,
                   int protocol_warn_level);

compress_method_t detect_compression_method(const char *in, size_t in_len);

MOCK_DECL(int,tor_compress_is_compression_bomb,(size_t size_in,
                                                size_t size_out));

int tor_compress_supports_method(compress_method_t method);
unsigned tor_compress_get_supported_method_bitmask(void);
const char *compression_method_get_name(compress_method_t method);
const char *compression_method_get_human_name(compress_method_t method);
compress_method_t compression_method_get_by_name(const char *name);

const char *tor_compress_version_str(compress_method_t method);

const char *tor_compress_header_version_str(compress_method_t method);

size_t tor_compress_get_total_allocation(void);

/** Return values from tor_compress_process; see that function's documentation
 * for details. */
typedef enum {
  TOR_COMPRESS_OK,
  TOR_COMPRESS_DONE,
  TOR_COMPRESS_BUFFER_FULL,
  TOR_COMPRESS_ERROR
} tor_compress_output_t;

/** Internal state for an incremental compression/decompression. */
typedef struct tor_compress_state_t tor_compress_state_t;

tor_compress_state_t *tor_compress_new(int compress,
                                       compress_method_t method,
                                       compression_level_t level);

tor_compress_output_t tor_compress_process(tor_compress_state_t *state,
                                           char **out, size_t *out_len,
                                           const char **in, size_t *in_len,
                                           int finish);
void tor_compress_free_(tor_compress_state_t *state);
#define tor_compress_free(st) \
  FREE_AND_NULL(tor_compress_state_t, tor_compress_free_, (st))

size_t tor_compress_state_size(const tor_compress_state_t *state);

int tor_compress_init(void);
void tor_compress_log_init_warnings(void);

struct buf_t;
int buf_add_compress(struct buf_t *buf, struct tor_compress_state_t *state,
                     const char *data, size_t data_len, int done);

#endif /* !defined(TOR_COMPRESS_H) */