fix typo
[tor.git] / src / common / compress_zlib.c
blob284542e8854d68f40bb083a5c6bfaf04f17c376e
1 /* Copyright (c) 2004, 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 */
6 /**
7 * \file compress_zlib.c
8 * \brief Compression backend for gzip and zlib.
10 * This module should never be invoked directly. Use the compress module
11 * instead.
12 **/
14 #include "orconfig.h"
16 #include "util.h"
17 #include "torlog.h"
18 #include "compress.h"
19 #include "compress_zlib.h"
21 /* zlib 1.2.4 and 1.2.5 do some "clever" things with macros. Instead of
22 saying "(defined(FOO) ? FOO : 0)" they like to say "FOO-0", on the theory
23 that nobody will care if the compile outputs a no-such-identifier warning.
25 Sorry, but we like -Werror over here, so I guess we need to define these.
26 I hope that zlib 1.2.6 doesn't break these too.
28 #ifndef _LARGEFILE64_SOURCE
29 #define _LARGEFILE64_SOURCE 0
30 #endif
31 #ifndef _LFS64_LARGEFILE
32 #define _LFS64_LARGEFILE 0
33 #endif
34 #ifndef _FILE_OFFSET_BITS
35 #define _FILE_OFFSET_BITS 0
36 #endif
37 #ifndef off64_t
38 #define off64_t int64_t
39 #endif
41 #include <zlib.h>
43 #if defined ZLIB_VERNUM && ZLIB_VERNUM < 0x1200
44 #error "We require zlib version 1.2 or later."
45 #endif
47 static size_t tor_zlib_state_size_precalc(int inflate,
48 int windowbits, int memlevel);
50 /** Total number of bytes allocated for zlib state */
51 static atomic_counter_t total_zlib_allocation;
53 /** Given <b>level</b> return the memory level. */
54 static int
55 memory_level(compression_level_t level)
57 switch (level) {
58 default:
59 case BEST_COMPRESSION: return 9;
60 case HIGH_COMPRESSION: return 8;
61 case MEDIUM_COMPRESSION: return 7;
62 case LOW_COMPRESSION: return 6;
66 /** Return the 'bits' value to tell zlib to use <b>method</b>.*/
67 static inline int
68 method_bits(compress_method_t method, compression_level_t level)
70 /* Bits+16 means "use gzip" in zlib >= 1.2 */
71 const int flag = method == GZIP_METHOD ? 16 : 0;
72 switch (level) {
73 default:
74 case BEST_COMPRESSION:
75 case HIGH_COMPRESSION: return flag + 15;
76 case MEDIUM_COMPRESSION: return flag + 13;
77 case LOW_COMPRESSION: return flag + 11;
81 /** Return 1 if zlib/gzip compression is supported; otherwise 0. */
82 int
83 tor_zlib_method_supported(void)
85 /* We currently always support zlib/gzip, but we keep this function around in
86 * case we some day decide to deprecate zlib/gzip support.
88 return 1;
91 /** Return a string representation of the version of the currently running
92 * version of zlib. */
93 const char *
94 tor_zlib_get_version_str(void)
96 return zlibVersion();
99 /** Return a string representation of the version of the version of zlib
100 * used at compilation. */
101 const char *
102 tor_zlib_get_header_version_str(void)
104 return ZLIB_VERSION;
107 /** Internal zlib state for an incremental compression/decompression.
108 * The body of this struct is not exposed. */
109 struct tor_zlib_compress_state_t {
110 struct z_stream_s stream; /**< The zlib stream */
111 int compress; /**< True if we are compressing; false if we are inflating */
113 /** Number of bytes read so far. Used to detect zlib bombs. */
114 size_t input_so_far;
115 /** Number of bytes written so far. Used to detect zlib bombs. */
116 size_t output_so_far;
118 /** Approximate number of bytes allocated for this object. */
119 size_t allocation;
122 /** Return an approximate number of bytes used in RAM to hold a state with
123 * window bits <b>windowBits</b> and compression level 'memlevel' */
124 static size_t
125 tor_zlib_state_size_precalc(int inflate_, int windowbits, int memlevel)
127 windowbits &= 15;
129 #define A_FEW_KILOBYTES 2048
131 if (inflate_) {
132 /* From zconf.h:
134 "The memory requirements for inflate are (in bytes) 1 << windowBits
135 that is, 32K for windowBits=15 (default value) plus a few kilobytes
136 for small objects."
138 return sizeof(tor_zlib_compress_state_t) + sizeof(struct z_stream_s) +
139 (1 << 15) + A_FEW_KILOBYTES;
140 } else {
141 /* Also from zconf.h:
143 "The memory requirements for deflate are (in bytes):
144 (1 << (windowBits+2)) + (1 << (memLevel+9))
145 ... plus a few kilobytes for small objects."
147 return sizeof(tor_zlib_compress_state_t) + sizeof(struct z_stream_s) +
148 (1 << (windowbits + 2)) + (1 << (memlevel + 9)) + A_FEW_KILOBYTES;
150 #undef A_FEW_KILOBYTES
153 /** Construct and return a tor_zlib_compress_state_t object using
154 * <b>method</b>. If <b>compress</b>, it's for compression; otherwise it's for
155 * decompression. */
156 tor_zlib_compress_state_t *
157 tor_zlib_compress_new(int compress_,
158 compress_method_t method,
159 compression_level_t compression_level)
161 tor_zlib_compress_state_t *out;
162 int bits, memlevel;
164 if (! compress_) {
165 /* use this setting for decompression, since we might have the
166 * max number of window bits */
167 compression_level = BEST_COMPRESSION;
170 out = tor_malloc_zero(sizeof(tor_zlib_compress_state_t));
171 out->stream.zalloc = Z_NULL;
172 out->stream.zfree = Z_NULL;
173 out->stream.opaque = NULL;
174 out->compress = compress_;
175 bits = method_bits(method, compression_level);
176 memlevel = memory_level(compression_level);
177 if (compress_) {
178 if (deflateInit2(&out->stream, Z_BEST_COMPRESSION, Z_DEFLATED,
179 bits, memlevel,
180 Z_DEFAULT_STRATEGY) != Z_OK)
181 goto err; // LCOV_EXCL_LINE
182 } else {
183 if (inflateInit2(&out->stream, bits) != Z_OK)
184 goto err; // LCOV_EXCL_LINE
186 out->allocation = tor_zlib_state_size_precalc(!compress_, bits, memlevel);
188 atomic_counter_add(&total_zlib_allocation, out->allocation);
190 return out;
192 err:
193 tor_free(out);
194 return NULL;
197 /** Compress/decompress some bytes using <b>state</b>. Read up to
198 * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
199 * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
200 * we've reached the end of the input.
202 * Return TOR_COMPRESS_DONE if we've finished the entire
203 * compression/decompression.
204 * Return TOR_COMPRESS_OK if we're processed everything from the input.
205 * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
206 * Return TOR_COMPRESS_ERROR if the stream is corrupt.
208 tor_compress_output_t
209 tor_zlib_compress_process(tor_zlib_compress_state_t *state,
210 char **out, size_t *out_len,
211 const char **in, size_t *in_len,
212 int finish)
214 int err;
215 tor_assert(state != NULL);
216 if (*in_len > UINT_MAX ||
217 *out_len > UINT_MAX) {
218 return TOR_COMPRESS_ERROR;
221 state->stream.next_in = (unsigned char*) *in;
222 state->stream.avail_in = (unsigned int)*in_len;
223 state->stream.next_out = (unsigned char*) *out;
224 state->stream.avail_out = (unsigned int)*out_len;
226 if (state->compress) {
227 err = deflate(&state->stream, finish ? Z_FINISH : Z_NO_FLUSH);
228 } else {
229 err = inflate(&state->stream, finish ? Z_FINISH : Z_SYNC_FLUSH);
232 state->input_so_far += state->stream.next_in - ((unsigned char*)*in);
233 state->output_so_far += state->stream.next_out - ((unsigned char*)*out);
235 *out = (char*) state->stream.next_out;
236 *out_len = state->stream.avail_out;
237 *in = (const char *) state->stream.next_in;
238 *in_len = state->stream.avail_in;
240 if (! state->compress &&
241 tor_compress_is_compression_bomb(state->input_so_far,
242 state->output_so_far)) {
243 log_warn(LD_DIR, "Possible zlib bomb; abandoning stream.");
244 return TOR_COMPRESS_ERROR;
247 switch (err)
249 case Z_STREAM_END:
250 return TOR_COMPRESS_DONE;
251 case Z_BUF_ERROR:
252 if (state->stream.avail_in == 0 && !finish)
253 return TOR_COMPRESS_OK;
254 return TOR_COMPRESS_BUFFER_FULL;
255 case Z_OK:
256 if (state->stream.avail_out == 0 || finish)
257 return TOR_COMPRESS_BUFFER_FULL;
258 return TOR_COMPRESS_OK;
259 default:
260 log_warn(LD_GENERAL, "Gzip returned an error: %s",
261 state->stream.msg ? state->stream.msg : "<no message>");
262 return TOR_COMPRESS_ERROR;
266 /** Deallocate <b>state</b>. */
267 void
268 tor_zlib_compress_free(tor_zlib_compress_state_t *state)
270 if (state == NULL)
271 return;
273 atomic_counter_sub(&total_zlib_allocation, state->allocation);
275 if (state->compress)
276 deflateEnd(&state->stream);
277 else
278 inflateEnd(&state->stream);
280 tor_free(state);
283 /** Return the approximate number of bytes allocated for <b>state</b>. */
284 size_t
285 tor_zlib_compress_state_size(const tor_zlib_compress_state_t *state)
287 tor_assert(state != NULL);
288 return state->allocation;
291 /** Return the approximate number of bytes allocated for all zlib states. */
292 size_t
293 tor_zlib_get_total_allocation(void)
295 return atomic_counter_get(&total_zlib_allocation);
298 /** Set up global state for the zlib module */
299 void
300 tor_zlib_init(void)
302 atomic_counter_init(&total_zlib_allocation);