compress_zlib.c 8.7 KB

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