torgzip.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 MS_WINDOWS
  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. 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. static INLINE int
  44. method_bits(compress_method_t method)
  45. {
  46. /* Bits+16 means "use gzip" in zlib >= 1.2 */
  47. return method == GZIP_METHOD ? 15+16 : 15;
  48. }
  49. /** Given <b>in_len</b> bytes at <b>in</b>, compress them into a newly
  50. * allocated buffer, using the method described in <b>method</b>. Store the
  51. * compressed string in *<b>out</b>, and its length in *<b>out_len</b>.
  52. * Return 0 on success, -1 on failure.
  53. */
  54. int
  55. tor_gzip_compress(char **out, size_t *out_len,
  56. const char *in, size_t in_len,
  57. compress_method_t method)
  58. {
  59. struct z_stream_s *stream = NULL;
  60. size_t out_size;
  61. off_t offset;
  62. tor_assert(out);
  63. tor_assert(out_len);
  64. tor_assert(in);
  65. if (method == GZIP_METHOD && !is_gzip_supported()) {
  66. /* Old zlib version don't support gzip in deflateInit2 */
  67. log_warn(LD_GENERAL, "Gzip not supported with zlib %s", ZLIB_VERSION);
  68. return -1;
  69. }
  70. *out = NULL;
  71. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  72. stream->zalloc = Z_NULL;
  73. stream->zfree = Z_NULL;
  74. stream->opaque = NULL;
  75. stream->next_in = (unsigned char*) in;
  76. stream->avail_in = in_len;
  77. if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
  78. method_bits(method),
  79. 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  80. log_warn(LD_GENERAL, "Error from deflateInit2: %s",
  81. stream->msg?stream->msg:"<no message>");
  82. goto err;
  83. }
  84. /* Guess 50% compression. */
  85. out_size = in_len / 2;
  86. if (out_size < 1024) out_size = 1024;
  87. *out = tor_malloc(out_size);
  88. stream->next_out = (unsigned char*)*out;
  89. stream->avail_out = out_size;
  90. while (1) {
  91. switch (deflate(stream, Z_FINISH))
  92. {
  93. case Z_STREAM_END:
  94. goto done;
  95. case Z_OK:
  96. /* In case zlib doesn't work as I think .... */
  97. if (stream->avail_out >= stream->avail_in+16)
  98. break;
  99. case Z_BUF_ERROR:
  100. offset = stream->next_out - ((unsigned char*)*out);
  101. out_size *= 2;
  102. *out = tor_realloc(*out, out_size);
  103. stream->next_out = (unsigned char*)(*out + offset);
  104. stream->avail_out = out_size - offset;
  105. break;
  106. default:
  107. log_warn(LD_GENERAL, "Gzip compression didn't finish: %s",
  108. stream->msg ? stream->msg : "<no message>");
  109. goto err;
  110. }
  111. }
  112. done:
  113. *out_len = stream->total_out;
  114. if (deflateEnd(stream)!=Z_OK) {
  115. log_warn(LD_GENERAL, "Error freeing gzip structures");
  116. goto err;
  117. }
  118. tor_free(stream);
  119. return 0;
  120. err:
  121. if (stream) {
  122. deflateEnd(stream);
  123. tor_free(stream);
  124. }
  125. if (*out) {
  126. tor_free(*out);
  127. }
  128. return -1;
  129. }
  130. /** Given zero or more zlib-compressed or gzip-compressed strings of
  131. * total length
  132. * <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
  133. * buffer, using the method described in <b>method</b>. Store the uncompressed
  134. * string in *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on
  135. * success, -1 on failure.
  136. */
  137. int
  138. tor_gzip_uncompress(char **out, size_t *out_len,
  139. const char *in, size_t in_len,
  140. compress_method_t method,
  141. int complete_only,
  142. int protocol_warn_level)
  143. {
  144. struct z_stream_s *stream = NULL;
  145. size_t out_size;
  146. off_t offset;
  147. int r;
  148. tor_assert(out);
  149. tor_assert(out_len);
  150. tor_assert(in);
  151. if (method == GZIP_METHOD && !is_gzip_supported()) {
  152. /* Old zlib version don't support gzip in inflateInit2 */
  153. log_warn(LD_GENERAL, "Gzip not supported with zlib %s", ZLIB_VERSION);
  154. return -1;
  155. }
  156. *out = NULL;
  157. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  158. stream->zalloc = Z_NULL;
  159. stream->zfree = Z_NULL;
  160. stream->opaque = NULL;
  161. stream->next_in = (unsigned char*) in;
  162. stream->avail_in = in_len;
  163. if (inflateInit2(stream,
  164. method_bits(method)) != Z_OK) {
  165. log_warn(LD_GENERAL, "Error from inflateInit2: %s",
  166. stream->msg?stream->msg:"<no message>");
  167. goto err;
  168. }
  169. out_size = in_len * 2; /* guess 50% compression. */
  170. if (out_size < 1024) out_size = 1024;
  171. *out = tor_malloc(out_size);
  172. stream->next_out = (unsigned char*)*out;
  173. stream->avail_out = out_size;
  174. while (1) {
  175. switch (inflate(stream, complete_only ? Z_FINISH : Z_SYNC_FLUSH))
  176. {
  177. case Z_STREAM_END:
  178. if (stream->avail_in == 0)
  179. goto done;
  180. /* There may be more compressed data here. */
  181. if (inflateInit2(stream, 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. break;
  187. case Z_OK:
  188. if (!complete_only && stream->avail_in == 0)
  189. goto done;
  190. /* In case zlib doesn't work as I think.... */
  191. if (stream->avail_out >= stream->avail_in+16)
  192. break;
  193. case Z_BUF_ERROR:
  194. if (stream->avail_out > 0) {
  195. log_fn(protocol_warn_level, LD_PROTOCOL,
  196. "possible truncated or corrupt zlib data");
  197. goto err;
  198. }
  199. offset = stream->next_out - (unsigned char*)*out;
  200. out_size *= 2;
  201. *out = tor_realloc(*out, out_size);
  202. stream->next_out = (unsigned char*)(*out + offset);
  203. stream->avail_out = out_size - offset;
  204. break;
  205. default:
  206. log_warn(LD_GENERAL, "Gzip decompression returned an error: %s",
  207. stream->msg ? stream->msg : "<no message>");
  208. goto err;
  209. }
  210. }
  211. done:
  212. *out_len = stream->next_out - (unsigned char*)*out;
  213. r = inflateEnd(stream);
  214. tor_free(stream);
  215. if (r != Z_OK) {
  216. log_warn(LD_GENERAL, "Error freeing gzip structures");
  217. goto err;
  218. }
  219. /* NUL-terminate output. */
  220. if (out_size == *out_len)
  221. *out = tor_realloc(*out, out_size + 1);
  222. (*out)[*out_len] = '\0';
  223. return 0;
  224. err:
  225. if (stream) {
  226. inflateEnd(stream);
  227. tor_free(stream);
  228. }
  229. if (*out) {
  230. tor_free(*out);
  231. }
  232. return -1;
  233. }
  234. /** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
  235. * to be compressed or not. If it is, return the likeliest compression method.
  236. * Otherwise, return 0.
  237. */
  238. int
  239. detect_compression_method(const char *in, size_t in_len)
  240. {
  241. if (in_len > 2 && !memcmp(in, "\x1f\x8b", 2)) {
  242. return GZIP_METHOD;
  243. } else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
  244. (ntohs(get_uint16(in)) % 31) == 0) {
  245. return ZLIB_METHOD;
  246. } else {
  247. return 0;
  248. }
  249. }