torgzip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* Copyright 2004 Roger Dingledine */
  2. /* Copyright 2004-2007 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. if (out_size - offset > UINT_MAX) {
  108. log_warn(LD_BUG, "Ran over unsigned int limit of zlib while "
  109. "uncompressing.");
  110. goto err;
  111. }
  112. stream->avail_out = (unsigned int)(out_size - offset);
  113. break;
  114. default:
  115. log_warn(LD_GENERAL, "Gzip compression didn't finish: %s",
  116. stream->msg ? stream->msg : "<no message>");
  117. goto err;
  118. }
  119. }
  120. done:
  121. *out_len = stream->total_out;
  122. if (stream->total_out > out_size + 4097) {
  123. /* If we're wasting more than 4k, don't. */
  124. tor_realloc(*out, stream->total_out + 1);
  125. }
  126. if (deflateEnd(stream)!=Z_OK) {
  127. log_warn(LD_BUG, "Error freeing gzip structures");
  128. goto err;
  129. }
  130. tor_free(stream);
  131. return 0;
  132. err:
  133. if (stream) {
  134. deflateEnd(stream);
  135. tor_free(stream);
  136. }
  137. if (*out) {
  138. tor_free(*out);
  139. }
  140. return -1;
  141. }
  142. /** Given zero or more zlib-compressed or gzip-compressed strings of
  143. * total length
  144. * <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
  145. * buffer, using the method described in <b>method</b>. Store the uncompressed
  146. * string in *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on
  147. * success, -1 on failure.
  148. *
  149. * If <b>complete_only</b> is true, we consider a truncated input as a
  150. * failure; otherwise we decompress as much as we can. Warn about truncated
  151. * or corrupt inputs at <b>protocol_warn_level</b>.
  152. */
  153. int
  154. tor_gzip_uncompress(char **out, size_t *out_len,
  155. const char *in, size_t in_len,
  156. compress_method_t method,
  157. int complete_only,
  158. int protocol_warn_level)
  159. {
  160. struct z_stream_s *stream = NULL;
  161. size_t out_size;
  162. off_t offset;
  163. int r;
  164. tor_assert(out);
  165. tor_assert(out_len);
  166. tor_assert(in);
  167. if (method == GZIP_METHOD && !is_gzip_supported()) {
  168. /* Old zlib version don't support gzip in inflateInit2 */
  169. log_warn(LD_BUG, "Gzip not supported with zlib %s", ZLIB_VERSION);
  170. return -1;
  171. }
  172. *out = NULL;
  173. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  174. stream->zalloc = Z_NULL;
  175. stream->zfree = Z_NULL;
  176. stream->opaque = NULL;
  177. stream->next_in = (unsigned char*) in;
  178. stream->avail_in = in_len;
  179. if (inflateInit2(stream,
  180. method_bits(method)) != Z_OK) {
  181. log_warn(LD_GENERAL, "Error from inflateInit2: %s",
  182. stream->msg?stream->msg:"<no message>");
  183. goto err;
  184. }
  185. out_size = in_len * 2; /* guess 50% compression. */
  186. if (out_size < 1024) out_size = 1024;
  187. *out = tor_malloc(out_size);
  188. stream->next_out = (unsigned char*)*out;
  189. stream->avail_out = out_size;
  190. while (1) {
  191. switch (inflate(stream, complete_only ? Z_FINISH : Z_SYNC_FLUSH))
  192. {
  193. case Z_STREAM_END:
  194. if (stream->avail_in == 0)
  195. goto done;
  196. /* There may be more compressed data here. */
  197. if ((r = inflateEnd(stream)) != Z_OK) {
  198. log_warn(LD_BUG, "Error freeing gzip structures");
  199. goto err;
  200. }
  201. if (inflateInit2(stream, method_bits(method)) != Z_OK) {
  202. log_warn(LD_GENERAL, "Error from second inflateInit2: %s",
  203. stream->msg?stream->msg:"<no message>");
  204. goto err;
  205. }
  206. break;
  207. case Z_OK:
  208. if (!complete_only && stream->avail_in == 0)
  209. goto done;
  210. /* In case zlib doesn't work as I think.... */
  211. if (stream->avail_out >= stream->avail_in+16)
  212. break;
  213. case Z_BUF_ERROR:
  214. if (stream->avail_out > 0) {
  215. log_fn(protocol_warn_level, LD_PROTOCOL,
  216. "possible truncated or corrupt zlib data");
  217. goto err;
  218. }
  219. offset = stream->next_out - (unsigned char*)*out;
  220. out_size *= 2;
  221. *out = tor_realloc(*out, out_size);
  222. stream->next_out = (unsigned char*)(*out + offset);
  223. if (out_size - offset > UINT_MAX) {
  224. log_warn(LD_BUG, "Ran over unsigned int limit of zlib while "
  225. "uncompressing.");
  226. goto err;
  227. }
  228. stream->avail_out = (unsigned int)(out_size - offset);
  229. break;
  230. default:
  231. log_warn(LD_GENERAL, "Gzip decompression returned an error: %s",
  232. stream->msg ? stream->msg : "<no message>");
  233. goto err;
  234. }
  235. }
  236. done:
  237. *out_len = stream->next_out - (unsigned char*)*out;
  238. r = inflateEnd(stream);
  239. tor_free(stream);
  240. if (r != Z_OK) {
  241. log_warn(LD_BUG, "Error freeing gzip structures");
  242. goto err;
  243. }
  244. /* NUL-terminate output. */
  245. if (out_size == *out_len)
  246. *out = tor_realloc(*out, out_size + 1);
  247. (*out)[*out_len] = '\0';
  248. return 0;
  249. err:
  250. if (stream) {
  251. inflateEnd(stream);
  252. tor_free(stream);
  253. }
  254. if (*out) {
  255. tor_free(*out);
  256. }
  257. return -1;
  258. }
  259. /** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
  260. * to be compressed or not. If it is, return the likeliest compression method.
  261. * Otherwise, return UNKNOWN_METHOD.
  262. */
  263. compress_method_t
  264. detect_compression_method(const char *in, size_t in_len)
  265. {
  266. if (in_len > 2 && !memcmp(in, "\x1f\x8b", 2)) {
  267. return GZIP_METHOD;
  268. } else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
  269. (ntohs(get_uint16(in)) % 31) == 0) {
  270. return ZLIB_METHOD;
  271. } else {
  272. return UNKNOWN_METHOD;
  273. }
  274. }
  275. /** Internal state for an incremental zlib compression/decompression. The
  276. * body of this struct is not exposed. */
  277. struct tor_zlib_state_t {
  278. struct z_stream_s stream;
  279. int compress;
  280. };
  281. /** Construct and return a tor_zlib_state_t object using <b>method</b>. If
  282. * <b>compress</b>, it's for compression; otherwise it's for
  283. * decompression. */
  284. tor_zlib_state_t *
  285. tor_zlib_new(int compress, compress_method_t method)
  286. {
  287. tor_zlib_state_t *out;
  288. if (method == GZIP_METHOD && !is_gzip_supported()) {
  289. /* Old zlib version don't support gzip in inflateInit2 */
  290. log_warn(LD_BUG, "Gzip not supported with zlib %s", ZLIB_VERSION);
  291. return NULL;
  292. }
  293. out = tor_malloc_zero(sizeof(tor_zlib_state_t));
  294. out->stream.zalloc = Z_NULL;
  295. out->stream.zfree = Z_NULL;
  296. out->stream.opaque = NULL;
  297. out->compress = compress;
  298. if (compress) {
  299. if (deflateInit2(&out->stream, Z_BEST_COMPRESSION, Z_DEFLATED,
  300. method_bits(method), 8, Z_DEFAULT_STRATEGY) != Z_OK)
  301. goto err;
  302. } else {
  303. if (inflateInit2(&out->stream, method_bits(method)) != Z_OK)
  304. goto err;
  305. }
  306. return out;
  307. err:
  308. tor_free(out);
  309. return NULL;
  310. }
  311. /** Compress/decommpress some bytes using <b>state</b>. Read up to
  312. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  313. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  314. * we've reached the end of the input.
  315. *
  316. * Return TOR_ZLIB_DONE if we've finished the entire compression/decompression.
  317. * Return TOR_ZLIB_OK if we're processed everything from the input.
  318. * Return TOR_ZLIB_BUF_FULL if we're out of space on <b>out</b>.
  319. * Return TOR_ZLIB_ERR if the stream is corrupt.
  320. */
  321. tor_zlib_output_t
  322. tor_zlib_process(tor_zlib_state_t *state,
  323. char **out, size_t *out_len,
  324. const char **in, size_t *in_len,
  325. int finish)
  326. {
  327. int err;
  328. state->stream.next_in = (unsigned char*) *in;
  329. state->stream.avail_in = *in_len;
  330. state->stream.next_out = (unsigned char*) *out;
  331. state->stream.avail_out = *out_len;
  332. if (state->compress) {
  333. err = deflate(&state->stream, finish ? Z_FINISH : Z_SYNC_FLUSH);
  334. } else {
  335. err = inflate(&state->stream, finish ? Z_FINISH : Z_SYNC_FLUSH);
  336. }
  337. *out = (char*) state->stream.next_out;
  338. *out_len = state->stream.avail_out;
  339. *in = (const char *) state->stream.next_in;
  340. *in_len = state->stream.avail_in;
  341. switch (err)
  342. {
  343. case Z_STREAM_END:
  344. return TOR_ZLIB_DONE;
  345. case Z_BUF_ERROR:
  346. if (state->stream.avail_in == 0)
  347. return TOR_ZLIB_OK;
  348. return TOR_ZLIB_BUF_FULL;
  349. case Z_OK:
  350. if (state->stream.avail_out == 0 || finish)
  351. return TOR_ZLIB_BUF_FULL;
  352. return TOR_ZLIB_OK;
  353. default:
  354. log_warn(LD_GENERAL, "Gzip returned an error: %s",
  355. state->stream.msg ? state->stream.msg : "<no message>");
  356. return TOR_ZLIB_ERR;
  357. }
  358. }
  359. /** Deallocate <b>state</b>. */
  360. void
  361. tor_zlib_free(tor_zlib_state_t *state)
  362. {
  363. tor_assert(state);
  364. if (state->compress)
  365. deflateEnd(&state->stream);
  366. else
  367. inflateEnd(&state->stream);
  368. tor_free(state);
  369. }