29 #ifndef _LARGEFILE64_SOURCE
30 #define _LARGEFILE64_SOURCE 0
32 #ifndef _LFS64_LARGEFILE
33 #define _LFS64_LARGEFILE 0
35 #ifndef _FILE_OFFSET_BITS
36 #define _FILE_OFFSET_BITS 0
39 #define off64_t int64_t
44 #if defined ZLIB_VERNUM && ZLIB_VERNUM < 0x1200
45 #error "We require zlib version 1.2 or later."
49 int windowbits,
int memlevel);
60 case BEST_COMPRESSION:
return 9;
61 case HIGH_COMPRESSION:
return 8;
62 case MEDIUM_COMPRESSION:
return 7;
63 case LOW_COMPRESSION:
return 6;
72 const int flag = method == GZIP_METHOD ? 16 : 0;
75 case BEST_COMPRESSION:
76 case HIGH_COMPRESSION:
return flag + 15;
77 case MEDIUM_COMPRESSION:
return flag + 13;
78 case LOW_COMPRESSION:
return flag + 11;
130 #define A_FEW_KILOBYTES 2048
140 (1 << 15) + A_FEW_KILOBYTES;
149 (1 << (windowbits + 2)) + (1 << (memlevel + 9)) + A_FEW_KILOBYTES;
151 #undef A_FEW_KILOBYTES
168 compression_level = BEST_COMPRESSION;
172 out->
stream.zalloc = Z_NULL;
173 out->
stream.zfree = Z_NULL;
174 out->
stream.opaque = NULL;
179 if (deflateInit2(&out->
stream, Z_BEST_COMPRESSION, Z_DEFLATED,
181 Z_DEFAULT_STRATEGY) != Z_OK)
184 if (inflateInit2(&out->
stream, bits) != Z_OK)
211 char **out,
size_t *out_len,
212 const char **in,
size_t *in_len,
217 if (*in_len > UINT_MAX ||
218 *out_len > UINT_MAX) {
219 return TOR_COMPRESS_ERROR;
222 state->
stream.next_in = (
unsigned char*) *in;
223 state->
stream.avail_in = (
unsigned int)*in_len;
224 state->
stream.next_out = (
unsigned char*) *out;
225 state->
stream.avail_out = (
unsigned int)*out_len;
228 err = deflate(&state->
stream, finish ? Z_FINISH : Z_NO_FLUSH);
230 err = inflate(&state->
stream, finish ? Z_FINISH : Z_SYNC_FLUSH);
236 *out = (
char*) state->
stream.next_out;
237 *out_len = state->
stream.avail_out;
238 *in = (
const char *) state->
stream.next_in;
239 *in_len = state->
stream.avail_in;
244 log_warn(
LD_DIR,
"Possible zlib bomb; abandoning stream.");
245 return TOR_COMPRESS_ERROR;
251 return TOR_COMPRESS_DONE;
253 if (state->
stream.avail_in == 0 && !finish)
254 return TOR_COMPRESS_OK;
255 return TOR_COMPRESS_BUFFER_FULL;
257 if (state->
stream.avail_out == 0 || finish)
258 return TOR_COMPRESS_BUFFER_FULL;
259 return TOR_COMPRESS_OK;
261 log_warn(
LD_GENERAL,
"Gzip returned an error: %s",
262 state->
stream.msg ? state->
stream.msg :
"<no message>");
263 return TOR_COMPRESS_ERROR;
277 deflateEnd(&state->
stream);
279 inflateEnd(&state->
stream);
void atomic_counter_init(atomic_counter_t *counter)
void atomic_counter_sub(atomic_counter_t *counter, size_t sub)
void atomic_counter_add(atomic_counter_t *counter, size_t add)
size_t atomic_counter_get(atomic_counter_t *counter)
int tor_compress_is_compression_bomb(size_t size_in, size_t size_out)
void tor_zlib_compress_free_(tor_zlib_compress_state_t *state)
static int memory_level(compression_level_t level)
const char * tor_zlib_get_header_version_str(void)
static size_t tor_zlib_state_size_precalc(int inflate, int windowbits, int memlevel)
int tor_zlib_method_supported(void)
static int method_bits(compress_method_t method, compression_level_t level)
static atomic_counter_t total_zlib_allocation
size_t tor_zlib_get_total_allocation(void)
tor_zlib_compress_state_t * tor_zlib_compress_new(int compress_, compress_method_t method, compression_level_t compression_level)
size_t tor_zlib_compress_state_size(const tor_zlib_compress_state_t *state)
const char * tor_zlib_get_version_str(void)
tor_compress_output_t tor_zlib_compress_process(tor_zlib_compress_state_t *state, char **out, size_t *out_len, const char **in, size_t *in_len, int finish)
Header for compress_zlib.c.
Macros to manage assertions, fatal and non-fatal.