torgzip.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright 2004 Roger Dingledine */
  2. /* Copyright 2004-2005 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. 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. 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. 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. 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 or more zlib-compressed or gzip-compressed strings of total length
  131. * <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
  132. * buffer, using the method described in <b>method</b>. Store the uncompressed
  133. * string in *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on
  134. * success, -1 on failure.
  135. */
  136. int
  137. tor_gzip_uncompress(char **out, size_t *out_len,
  138. const char *in, size_t in_len,
  139. compress_method_t method,
  140. int complete_only)
  141. {
  142. struct z_stream_s *stream = NULL;
  143. size_t out_size;
  144. off_t offset;
  145. int r;
  146. tor_assert(out);
  147. tor_assert(out_len);
  148. tor_assert(in);
  149. if (method == GZIP_METHOD && !is_gzip_supported()) {
  150. /* Old zlib version don't support gzip in inflateInit2 */
  151. warn(LD_GENERAL, "Gzip not supported with zlib %s", ZLIB_VERSION);
  152. return -1;
  153. }
  154. *out = NULL;
  155. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  156. stream->zalloc = Z_NULL;
  157. stream->zfree = Z_NULL;
  158. stream->opaque = NULL;
  159. stream->next_in = (unsigned char*) in;
  160. stream->avail_in = in_len;
  161. if (inflateInit2(stream,
  162. method_bits(method)) != Z_OK) {
  163. warn(LD_GENERAL, "Error from inflateInit2: %s",
  164. stream->msg?stream->msg:"<no message>");
  165. goto err;
  166. }
  167. out_size = in_len * 2; /* guess 50% compression. */
  168. if (out_size < 1024) out_size = 1024;
  169. *out = tor_malloc(out_size);
  170. stream->next_out = (unsigned char*)*out;
  171. stream->avail_out = out_size;
  172. while (1) {
  173. switch (inflate(stream, complete_only ? Z_FINISH : Z_SYNC_FLUSH))
  174. {
  175. case Z_STREAM_END:
  176. if (stream->avail_in == 0)
  177. goto done;
  178. /* There may be more compressed data here. */
  179. if (inflateInit2(stream, method_bits(method)) != Z_OK) {
  180. warn(LD_GENERAL, "Error from inflateInit2: %s",
  181. stream->msg?stream->msg:"<no message>");
  182. goto err;
  183. }
  184. break;
  185. case Z_OK:
  186. if (!complete_only && stream->avail_in == 0)
  187. goto done;
  188. /* In case zlib doesn't work as I think.... */
  189. if (stream->avail_out >= stream->avail_in+16)
  190. break;
  191. case Z_BUF_ERROR:
  192. if (stream->avail_out > 0) {
  193. warn(LD_PROTOCOL, "possible truncated or corrupt zlib data");
  194. goto err;
  195. }
  196. offset = stream->next_out - (unsigned char*)*out;
  197. out_size *= 2;
  198. *out = tor_realloc(*out, out_size);
  199. stream->next_out = (unsigned char*)(*out + offset);
  200. stream->avail_out = out_size - offset;
  201. break;
  202. default:
  203. warn(LD_GENERAL, "Gzip decompression returned an error: %s",
  204. stream->msg ? stream->msg : "<no message>");
  205. goto err;
  206. }
  207. }
  208. done:
  209. *out_len = stream->next_out - (unsigned char*)*out;
  210. r = inflateEnd(stream);
  211. tor_free(stream);
  212. if (r != Z_OK) {
  213. warn(LD_GENERAL, "Error freeing gzip structures");
  214. goto err;
  215. }
  216. /* NUL-terminate output. */
  217. if (out_size == *out_len)
  218. *out = tor_realloc(*out, out_size + 1);
  219. (*out)[*out_len] = '\0';
  220. return 0;
  221. err:
  222. if (stream) {
  223. inflateEnd(stream);
  224. tor_free(stream);
  225. }
  226. if (*out) {
  227. tor_free(*out);
  228. }
  229. return -1;
  230. }
  231. /** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
  232. * to be compressed or not. If it is, return the likeliest compression method.
  233. * Otherwise, return 0.
  234. */
  235. int
  236. detect_compression_method(const char *in, size_t in_len)
  237. {
  238. if (in_len > 2 && !memcmp(in, "\x1f\x8b", 2)) {
  239. return GZIP_METHOD;
  240. } else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
  241. (ntohs(get_uint16(in)) % 31) == 0) {
  242. return ZLIB_METHOD;
  243. } else {
  244. return 0;
  245. }
  246. }