torgzip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* Copyright 2004 Roger Dingledine */
  2. /* Copyright 2004-2006 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char torgzip_c_id[] =
  6. "$Id$";
  7. /**
  8. * \file torgzip.c
  9. * \brief A simple in-memory gzip implementation.
  10. **/
  11. #include "orconfig.h"
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <assert.h>
  15. #ifdef _MSC_VER
  16. #include "..\..\contrib\zlib\zlib.h"
  17. #else
  18. #include <zlib.h>
  19. #endif
  20. #include <string.h>
  21. #ifdef HAVE_NETINET_IN_H
  22. #include <netinet/in.h>
  23. #endif
  24. #include "util.h"
  25. #include "log.h"
  26. #include "torgzip.h"
  27. /** Set to 1 if zlib is a version that supports gzip; set to 0 if it doesn't;
  28. * set to -1 if we haven't checked yet. */
  29. static int gzip_is_supported = -1;
  30. /** Return true iff we support gzip-based compression. Otherwise, we need to
  31. * use zlib. */
  32. int
  33. is_gzip_supported(void)
  34. {
  35. if (gzip_is_supported >= 0)
  36. return gzip_is_supported;
  37. if (!strcmpstart(ZLIB_VERSION, "0.") ||
  38. !strcmpstart(ZLIB_VERSION, "1.0") ||
  39. !strcmpstart(ZLIB_VERSION, "1.1"))
  40. gzip_is_supported = 0;
  41. else
  42. gzip_is_supported = 1;
  43. return gzip_is_supported;
  44. }
  45. /** Return the 'bits' value to tell zlib to use <b>method</b>.*/
  46. static INLINE int
  47. method_bits(compress_method_t method)
  48. {
  49. /* Bits+16 means "use gzip" in zlib >= 1.2 */
  50. return method == GZIP_METHOD ? 15+16 : 15;
  51. }
  52. /** Given <b>in_len</b> bytes at <b>in</b>, compress them into a newly
  53. * allocated buffer, using the method described in <b>method</b>. Store the
  54. * compressed string in *<b>out</b>, and its length in *<b>out_len</b>.
  55. * Return 0 on success, -1 on failure.
  56. */
  57. int
  58. tor_gzip_compress(char **out, size_t *out_len,
  59. const char *in, size_t in_len,
  60. compress_method_t method)
  61. {
  62. struct z_stream_s *stream = NULL;
  63. size_t out_size;
  64. off_t offset;
  65. tor_assert(out);
  66. tor_assert(out_len);
  67. tor_assert(in);
  68. if (method == GZIP_METHOD && !is_gzip_supported()) {
  69. /* Old zlib version don't support gzip in deflateInit2 */
  70. log_warn(LD_BUG, "Gzip not supported with zlib %s", ZLIB_VERSION);
  71. return -1;
  72. }
  73. *out = NULL;
  74. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  75. stream->zalloc = Z_NULL;
  76. stream->zfree = Z_NULL;
  77. stream->opaque = NULL;
  78. stream->next_in = (unsigned char*) in;
  79. stream->avail_in = in_len;
  80. if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
  81. method_bits(method),
  82. 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  83. log_warn(LD_GENERAL, "Error from deflateInit2: %s",
  84. stream->msg?stream->msg:"<no message>");
  85. goto err;
  86. }
  87. /* Guess 50% compression. */
  88. out_size = in_len / 2;
  89. if (out_size < 1024) out_size = 1024;
  90. *out = tor_malloc(out_size);
  91. stream->next_out = (unsigned char*)*out;
  92. stream->avail_out = out_size;
  93. while (1) {
  94. switch (deflate(stream, Z_FINISH))
  95. {
  96. case Z_STREAM_END:
  97. goto done;
  98. case Z_OK:
  99. /* In case zlib doesn't work as I think .... */
  100. if (stream->avail_out >= stream->avail_in+16)
  101. break;
  102. case Z_BUF_ERROR:
  103. offset = stream->next_out - ((unsigned char*)*out);
  104. out_size *= 2;
  105. *out = tor_realloc(*out, out_size);
  106. stream->next_out = (unsigned char*)(*out + offset);
  107. stream->avail_out = out_size - offset;
  108. break;
  109. default:
  110. log_warn(LD_GENERAL, "Gzip compression didn't finish: %s",
  111. stream->msg ? stream->msg : "<no message>");
  112. goto err;
  113. }
  114. }
  115. done:
  116. *out_len = stream->total_out;
  117. if (deflateEnd(stream)!=Z_OK) {
  118. log_warn(LD_BUG, "Error freeing gzip structures");
  119. goto err;
  120. }
  121. tor_free(stream);
  122. return 0;
  123. err:
  124. if (stream) {
  125. deflateEnd(stream);
  126. tor_free(stream);
  127. }
  128. if (*out) {
  129. tor_free(*out);
  130. }
  131. return -1;
  132. }
  133. /** Given zero or more zlib-compressed or gzip-compressed strings of
  134. * total length
  135. * <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
  136. * buffer, using the method described in <b>method</b>. Store the uncompressed
  137. * string in *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on
  138. * success, -1 on failure.
  139. *
  140. * If <b>complete_only</b> is true, we consider a truncated input as a
  141. * failure; otherwise we decompress as much as we can. Warn about truncated
  142. * or corrupt inputs at <b>protocol_warn_level</b>.
  143. */
  144. int
  145. tor_gzip_uncompress(char **out, size_t *out_len,
  146. const char *in, size_t in_len,
  147. compress_method_t method,
  148. int complete_only,
  149. int protocol_warn_level)
  150. {
  151. struct z_stream_s *stream = NULL;
  152. size_t out_size;
  153. off_t offset;
  154. int r;
  155. tor_assert(out);
  156. tor_assert(out_len);
  157. tor_assert(in);
  158. if (method == GZIP_METHOD && !is_gzip_supported()) {
  159. /* Old zlib version don't support gzip in inflateInit2 */
  160. log_warn(LD_BUG, "Gzip not supported with zlib %s", ZLIB_VERSION);
  161. return -1;
  162. }
  163. *out = NULL;
  164. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  165. stream->zalloc = Z_NULL;
  166. stream->zfree = Z_NULL;
  167. stream->opaque = NULL;
  168. stream->next_in = (unsigned char*) in;
  169. stream->avail_in = in_len;
  170. if (inflateInit2(stream,
  171. method_bits(method)) != Z_OK) {
  172. log_warn(LD_GENERAL, "Error from inflateInit2: %s",
  173. stream->msg?stream->msg:"<no message>");
  174. goto err;
  175. }
  176. out_size = in_len * 2; /* guess 50% compression. */
  177. if (out_size < 1024) out_size = 1024;
  178. *out = tor_malloc(out_size);
  179. stream->next_out = (unsigned char*)*out;
  180. stream->avail_out = out_size;
  181. while (1) {
  182. switch (inflate(stream, complete_only ? Z_FINISH : Z_SYNC_FLUSH))
  183. {
  184. case Z_STREAM_END:
  185. if (stream->avail_in == 0)
  186. goto done;
  187. /* There may be more compressed data here. */
  188. if ((r = inflateEnd(stream)) != Z_OK) {
  189. log_warn(LD_BUG, "Error freeing gzip structures");
  190. goto err;
  191. }
  192. if (inflateInit2(stream, method_bits(method)) != Z_OK) {
  193. log_warn(LD_GENERAL, "Error from second inflateInit2: %s",
  194. stream->msg?stream->msg:"<no message>");
  195. goto err;
  196. }
  197. break;
  198. case Z_OK:
  199. if (!complete_only && stream->avail_in == 0)
  200. goto done;
  201. /* In case zlib doesn't work as I think.... */
  202. if (stream->avail_out >= stream->avail_in+16)
  203. break;
  204. case Z_BUF_ERROR:
  205. if (stream->avail_out > 0) {
  206. log_fn(protocol_warn_level, LD_PROTOCOL,
  207. "possible truncated or corrupt zlib data");
  208. goto err;
  209. }
  210. offset = stream->next_out - (unsigned char*)*out;
  211. out_size *= 2;
  212. *out = tor_realloc(*out, out_size);
  213. stream->next_out = (unsigned char*)(*out + offset);
  214. stream->avail_out = out_size - offset;
  215. break;
  216. default:
  217. log_warn(LD_GENERAL, "Gzip decompression returned an error: %s",
  218. stream->msg ? stream->msg : "<no message>");
  219. goto err;
  220. }
  221. }
  222. done:
  223. *out_len = stream->next_out - (unsigned char*)*out;
  224. r = inflateEnd(stream);
  225. tor_free(stream);
  226. if (r != Z_OK) {
  227. log_warn(LD_BUG, "Error freeing gzip structures");
  228. goto err;
  229. }
  230. /* NUL-terminate output. */
  231. if (out_size == *out_len)
  232. *out = tor_realloc(*out, out_size + 1);
  233. (*out)[*out_len] = '\0';
  234. return 0;
  235. err:
  236. if (stream) {
  237. inflateEnd(stream);
  238. tor_free(stream);
  239. }
  240. if (*out) {
  241. tor_free(*out);
  242. }
  243. return -1;
  244. }
  245. /** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
  246. * to be compressed or not. If it is, return the likeliest compression method.
  247. * Otherwise, return 0.
  248. */
  249. int
  250. detect_compression_method(const char *in, size_t in_len)
  251. {
  252. if (in_len > 2 && !memcmp(in, "\x1f\x8b", 2)) {
  253. return GZIP_METHOD;
  254. } else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
  255. (ntohs(get_uint16(in)) % 31) == 0) {
  256. return ZLIB_METHOD;
  257. } else {
  258. return 0;
  259. }
  260. }
  261. struct tor_zlib_state_t {
  262. struct z_stream_s stream;
  263. int compress;
  264. };
  265. /** Construct and return a tor_zlib_state_t object using <b>method</b>. If
  266. * <b>compress</b>, it's for compression; otherwise it's for
  267. * decompression. */
  268. tor_zlib_state_t *
  269. tor_zlib_new(int compress, compress_method_t method)
  270. {
  271. tor_zlib_state_t *out;
  272. if (method == GZIP_METHOD && !is_gzip_supported()) {
  273. /* Old zlib version don't support gzip in inflateInit2 */
  274. log_warn(LD_BUG, "Gzip not supported with zlib %s", ZLIB_VERSION);
  275. return NULL;
  276. }
  277. out = tor_malloc_zero(sizeof(tor_zlib_state_t));
  278. out->stream.zalloc = Z_NULL;
  279. out->stream.zfree = Z_NULL;
  280. out->stream.opaque = NULL;
  281. out->compress = compress;
  282. if (compress) {
  283. if (deflateInit2(&out->stream, Z_BEST_COMPRESSION, Z_DEFLATED,
  284. method_bits(method), 8, Z_DEFAULT_STRATEGY) != Z_OK)
  285. goto err;
  286. } else {
  287. if (inflateInit2(&out->stream, method_bits(method)) != Z_OK)
  288. goto err;
  289. }
  290. return out;
  291. err:
  292. tor_free(out);
  293. return NULL;
  294. }
  295. /** Compress/decommpress some bytes using <b>state</b>. Read up to
  296. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  297. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  298. * we've reached the end of the input.
  299. *
  300. * Return TOR_ZLIB_DONE if we've finished the entire compression/decompression.
  301. * Return TOR_ZLIB_OK if we're processed everything from the input.
  302. * Return TOR_ZLIB_BUF_FULL if we're out of space on <b>out</b>.
  303. * Return TOR_ZLIB_ERR if the stream is corrupt.
  304. */
  305. tor_zlib_output_t
  306. tor_zlib_process(tor_zlib_state_t *state,
  307. char **out, size_t *out_len,
  308. const char **in, size_t *in_len,
  309. int finish)
  310. {
  311. int err;
  312. state->stream.next_in = (unsigned char*) *in;
  313. state->stream.avail_in = *in_len;
  314. state->stream.next_out = (unsigned char*) *out;
  315. state->stream.avail_out = *out_len;
  316. if (state->compress) {
  317. err = deflate(&state->stream, finish ? Z_FINISH : Z_SYNC_FLUSH);
  318. } else {
  319. err = inflate(&state->stream, finish ? Z_FINISH : Z_SYNC_FLUSH);
  320. }
  321. *out = (char*) state->stream.next_out;
  322. *out_len = state->stream.avail_out;
  323. *in = (const char *) state->stream.next_in;
  324. *in_len = state->stream.avail_in;
  325. switch (err)
  326. {
  327. case Z_STREAM_END:
  328. return TOR_ZLIB_DONE;
  329. case Z_BUF_ERROR:
  330. if (state->stream.avail_in == 0)
  331. return TOR_ZLIB_OK;
  332. return TOR_ZLIB_BUF_FULL;
  333. case Z_OK:
  334. if (state->stream.avail_out == 0 || finish)
  335. return TOR_ZLIB_BUF_FULL;
  336. return TOR_ZLIB_OK;
  337. default:
  338. log_warn(LD_GENERAL, "Gzip returned an error: %s",
  339. state->stream.msg ? state->stream.msg : "<no message>");
  340. return TOR_ZLIB_ERR;
  341. }
  342. }
  343. /** Deallocate <b>state</b>. */
  344. void
  345. tor_zlib_free(tor_zlib_state_t *state)
  346. {
  347. tor_assert(state);
  348. if (state->compress)
  349. deflateEnd(&state->stream);
  350. else
  351. inflateEnd(&state->stream);
  352. tor_free(state);
  353. }