torgzip.c 12 KB

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