torgzip.c 11 KB

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