torgzip.c 11 KB

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