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 */
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
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
31 #ifndef _LFS64_LARGEFILE
32 #define _LFS64_LARGEFILE 0
34 #ifndef _FILE_OFFSET_BITS
35 #define _FILE_OFFSET_BITS 0
38 #define off64_t int64_t
43 #if defined ZLIB_VERNUM && ZLIB_VERNUM < 0x1200
44 #error "We require zlib version 1.2 or later."
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. */
55 memory_level(compression_level_t level
)
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>.*/
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;
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. */
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.
91 /** Return a string representation of the version of the currently running
94 tor_zlib_get_version_str(void)
99 /** Return a string representation of the version of the version of zlib
100 * used at compilation. */
102 tor_zlib_get_header_version_str(void)
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. */
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. */
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' */
125 tor_zlib_state_size_precalc(int inflate_
, int windowbits
, int memlevel
)
129 #define A_FEW_KILOBYTES 2048
134 "The memory requirements for inflate are (in bytes) 1 << windowBits
135 that is, 32K for windowBits=15 (default value) plus a few kilobytes
138 return sizeof(tor_zlib_compress_state_t
) + sizeof(struct z_stream_s
) +
139 (1 << 15) + A_FEW_KILOBYTES
;
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
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
;
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
);
178 if (deflateInit2(&out
->stream
, Z_BEST_COMPRESSION
, Z_DEFLATED
,
180 Z_DEFAULT_STRATEGY
) != Z_OK
)
181 goto err
; // LCOV_EXCL_LINE
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
);
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
,
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
);
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
;
250 return TOR_COMPRESS_DONE
;
252 if (state
->stream
.avail_in
== 0 && !finish
)
253 return TOR_COMPRESS_OK
;
254 return TOR_COMPRESS_BUFFER_FULL
;
256 if (state
->stream
.avail_out
== 0 || finish
)
257 return TOR_COMPRESS_BUFFER_FULL
;
258 return TOR_COMPRESS_OK
;
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>. */
268 tor_zlib_compress_free(tor_zlib_compress_state_t
*state
)
273 atomic_counter_sub(&total_zlib_allocation
, state
->allocation
);
276 deflateEnd(&state
->stream
);
278 inflateEnd(&state
->stream
);
283 /** Return the approximate number of bytes allocated for <b>state</b>. */
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. */
293 tor_zlib_get_total_allocation(void)
295 return atomic_counter_get(&total_zlib_allocation
);
298 /** Set up global state for the zlib module */
302 atomic_counter_init(&total_zlib_allocation
);