compress_zlib.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 size_t total_zlib_allocation = 0;
  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 HIGH_COMPRESSION: return 8;
  50. case MEDIUM_COMPRESSION: return 7;
  51. case LOW_COMPRESSION: return 6;
  52. }
  53. }
  54. /** Return the 'bits' value to tell zlib to use <b>method</b>.*/
  55. static inline int
  56. method_bits(compress_method_t method, compression_level_t level)
  57. {
  58. /* Bits+16 means "use gzip" in zlib >= 1.2 */
  59. const int flag = method == GZIP_METHOD ? 16 : 0;
  60. switch (level) {
  61. default:
  62. case HIGH_COMPRESSION: return flag + 15;
  63. case MEDIUM_COMPRESSION: return flag + 13;
  64. case LOW_COMPRESSION: return flag + 11;
  65. }
  66. }
  67. /** Return 1 if zlib/gzip compression is supported; otherwise 0. */
  68. int
  69. tor_zlib_method_supported(void)
  70. {
  71. /* We currently always support zlib/gzip, but we keep this function around in
  72. * case we some day decide to deprecate zlib/gzip support.
  73. */
  74. return 1;
  75. }
  76. /** Return a string representation of the version of the currently running
  77. * version of zlib. */
  78. const char *
  79. tor_zlib_get_version_str(void)
  80. {
  81. return zlibVersion();
  82. }
  83. /** Return a string representation of the version of the version of zlib
  84. * used at compilation. */
  85. const char *
  86. tor_zlib_get_header_version_str(void)
  87. {
  88. return ZLIB_VERSION;
  89. }
  90. /** Internal zlib state for an incremental compression/decompression.
  91. * The body of this struct is not exposed. */
  92. struct tor_zlib_compress_state_t {
  93. struct z_stream_s stream; /**< The zlib stream */
  94. int compress; /**< True if we are compressing; false if we are inflating */
  95. /** Number of bytes read so far. Used to detect zlib bombs. */
  96. size_t input_so_far;
  97. /** Number of bytes written so far. Used to detect zlib bombs. */
  98. size_t output_so_far;
  99. /** Approximate number of bytes allocated for this object. */
  100. size_t allocation;
  101. };
  102. /** Return an approximate number of bytes used in RAM to hold a state with
  103. * window bits <b>windowBits</b> and compression level 'memlevel' */
  104. static size_t
  105. tor_zlib_state_size_precalc(int inflate_, int windowbits, int memlevel)
  106. {
  107. windowbits &= 15;
  108. #define A_FEW_KILOBYTES 2048
  109. if (inflate_) {
  110. /* From zconf.h:
  111. "The memory requirements for inflate are (in bytes) 1 << windowBits
  112. that is, 32K for windowBits=15 (default value) plus a few kilobytes
  113. for small objects."
  114. */
  115. return sizeof(tor_zlib_compress_state_t) + sizeof(struct z_stream_s) +
  116. (1 << 15) + A_FEW_KILOBYTES;
  117. } else {
  118. /* Also from zconf.h:
  119. "The memory requirements for deflate are (in bytes):
  120. (1 << (windowBits+2)) + (1 << (memLevel+9))
  121. ... plus a few kilobytes for small objects."
  122. */
  123. return sizeof(tor_zlib_compress_state_t) + sizeof(struct z_stream_s) +
  124. (1 << (windowbits + 2)) + (1 << (memlevel + 9)) + A_FEW_KILOBYTES;
  125. }
  126. #undef A_FEW_KILOBYTES
  127. }
  128. /** Construct and return a tor_zlib_compress_state_t object using
  129. * <b>method</b>. If <b>compress</b>, it's for compression; otherwise it's for
  130. * decompression. */
  131. tor_zlib_compress_state_t *
  132. tor_zlib_compress_new(int compress_,
  133. compress_method_t method,
  134. compression_level_t compression_level)
  135. {
  136. tor_zlib_compress_state_t *out;
  137. int bits, memlevel;
  138. if (! compress_) {
  139. /* use this setting for decompression, since we might have the
  140. * max number of window bits */
  141. compression_level = HIGH_COMPRESSION;
  142. }
  143. out = tor_malloc_zero(sizeof(tor_zlib_compress_state_t));
  144. out->stream.zalloc = Z_NULL;
  145. out->stream.zfree = Z_NULL;
  146. out->stream.opaque = NULL;
  147. out->compress = compress_;
  148. bits = method_bits(method, compression_level);
  149. memlevel = memory_level(compression_level);
  150. if (compress_) {
  151. if (deflateInit2(&out->stream, Z_BEST_COMPRESSION, Z_DEFLATED,
  152. bits, memlevel,
  153. Z_DEFAULT_STRATEGY) != Z_OK)
  154. goto err; // LCOV_EXCL_LINE
  155. } else {
  156. if (inflateInit2(&out->stream, bits) != Z_OK)
  157. goto err; // LCOV_EXCL_LINE
  158. }
  159. out->allocation = tor_zlib_state_size_precalc(!compress_, bits, memlevel);
  160. total_zlib_allocation += out->allocation;
  161. return out;
  162. err:
  163. tor_free(out);
  164. return NULL;
  165. }
  166. /** Compress/decompress some bytes using <b>state</b>. Read up to
  167. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  168. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  169. * we've reached the end of the input.
  170. *
  171. * Return TOR_COMPRESS_DONE if we've finished the entire
  172. * compression/decompression.
  173. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  174. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  175. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  176. */
  177. tor_compress_output_t
  178. tor_zlib_compress_process(tor_zlib_compress_state_t *state,
  179. char **out, size_t *out_len,
  180. const char **in, size_t *in_len,
  181. int finish)
  182. {
  183. int err;
  184. tor_assert(state != NULL);
  185. tor_assert(*in_len <= UINT_MAX);
  186. tor_assert(*out_len <= UINT_MAX);
  187. state->stream.next_in = (unsigned char*) *in;
  188. state->stream.avail_in = (unsigned int)*in_len;
  189. state->stream.next_out = (unsigned char*) *out;
  190. state->stream.avail_out = (unsigned int)*out_len;
  191. if (state->compress) {
  192. err = deflate(&state->stream, finish ? Z_FINISH : Z_NO_FLUSH);
  193. } else {
  194. err = inflate(&state->stream, finish ? Z_FINISH : Z_SYNC_FLUSH);
  195. }
  196. state->input_so_far += state->stream.next_in - ((unsigned char*)*in);
  197. state->output_so_far += state->stream.next_out - ((unsigned char*)*out);
  198. *out = (char*) state->stream.next_out;
  199. *out_len = state->stream.avail_out;
  200. *in = (const char *) state->stream.next_in;
  201. *in_len = state->stream.avail_in;
  202. if (! state->compress &&
  203. tor_compress_is_compression_bomb(state->input_so_far,
  204. state->output_so_far)) {
  205. log_warn(LD_DIR, "Possible zlib bomb; abandoning stream.");
  206. return TOR_COMPRESS_ERROR;
  207. }
  208. switch (err)
  209. {
  210. case Z_STREAM_END:
  211. return TOR_COMPRESS_DONE;
  212. case Z_BUF_ERROR:
  213. if (state->stream.avail_in == 0 && !finish)
  214. return TOR_COMPRESS_OK;
  215. return TOR_COMPRESS_BUFFER_FULL;
  216. case Z_OK:
  217. if (state->stream.avail_out == 0 || finish)
  218. return TOR_COMPRESS_BUFFER_FULL;
  219. return TOR_COMPRESS_OK;
  220. default:
  221. log_warn(LD_GENERAL, "Gzip returned an error: %s",
  222. state->stream.msg ? state->stream.msg : "<no message>");
  223. return TOR_COMPRESS_ERROR;
  224. }
  225. }
  226. /** Deallocate <b>state</b>. */
  227. void
  228. tor_zlib_compress_free(tor_zlib_compress_state_t *state)
  229. {
  230. if (state == NULL)
  231. return;
  232. total_zlib_allocation -= state->allocation;
  233. if (state->compress)
  234. deflateEnd(&state->stream);
  235. else
  236. inflateEnd(&state->stream);
  237. tor_free(state);
  238. }
  239. /** Return the approximate number of bytes allocated for <b>state</b>. */
  240. size_t
  241. tor_zlib_compress_state_size(const tor_zlib_compress_state_t *state)
  242. {
  243. tor_assert(state != NULL);
  244. return state->allocation;
  245. }
  246. /** Return the approximate number of bytes allocated for all zlib states. */
  247. size_t
  248. tor_zlib_get_total_allocation(void)
  249. {
  250. return total_zlib_allocation;
  251. }