torgzip.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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[] = "$Id$";
  6. /**
  7. * \file torgzip.c
  8. * \brief A simple in-memory gzip implementation.
  9. **/
  10. #include "orconfig.h"
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <assert.h>
  14. #ifdef MS_WINDOWS
  15. #include "..\..\contrib\zlib\zlib.h"
  16. #else
  17. #include <zlib.h>
  18. #endif
  19. #include <string.h>
  20. #ifdef HAVE_NETINET_IN_H
  21. #include <netinet/in.h>
  22. #endif
  23. #include "util.h"
  24. #include "log.h"
  25. #include "torgzip.h"
  26. static int gzip_is_supported = -1;
  27. /** Return true iff we support gzip-based compression. Otherwise, we need to
  28. * use zlib. */
  29. int
  30. is_gzip_supported(void)
  31. {
  32. if (gzip_is_supported >= 0)
  33. return gzip_is_supported;
  34. if (!strcmpstart(ZLIB_VERSION, "0.") ||
  35. !strcmpstart(ZLIB_VERSION, "1.0") ||
  36. !strcmpstart(ZLIB_VERSION, "1.1"))
  37. gzip_is_supported = 0;
  38. else
  39. gzip_is_supported = 1;
  40. return gzip_is_supported;
  41. }
  42. static INLINE int
  43. method_bits(compress_method_t method)
  44. {
  45. /* Bits+16 means "use gzip" in zlib >= 1.2 */
  46. return method == GZIP_METHOD ? 15+16 : 15;
  47. }
  48. /** Given <b>in_len</b> bytes at <b>in</b>, compress them into a newly
  49. * allocated buffer, using the method described in <b>method</b>. Store the
  50. * compressed string in *<b>out</b>, and its length in *<b>out_len</b>.
  51. * Return 0 on success, -1 on failure.
  52. */
  53. int
  54. tor_gzip_compress(char **out, size_t *out_len,
  55. const char *in, size_t in_len,
  56. compress_method_t method)
  57. {
  58. struct z_stream_s *stream = NULL;
  59. size_t out_size;
  60. off_t offset;
  61. tor_assert(out);
  62. tor_assert(out_len);
  63. tor_assert(in);
  64. if (method == GZIP_METHOD && !is_gzip_supported()) {
  65. /* Old zlib version don't support gzip in deflateInit2 */
  66. log_fn(LOG_WARN, "Gzip not supported with zlib %s", ZLIB_VERSION);
  67. return -1;
  68. }
  69. *out = NULL;
  70. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  71. stream->zalloc = Z_NULL;
  72. stream->zfree = Z_NULL;
  73. stream->opaque = NULL;
  74. stream->next_in = (unsigned char*) in;
  75. stream->avail_in = in_len;
  76. if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
  77. method_bits(method),
  78. 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  79. log_fn(LOG_WARN, "Error from deflateInit2: %s",
  80. stream->msg?stream->msg:"<no message>");
  81. goto err;
  82. }
  83. /* Guess 50% compression. */
  84. out_size = in_len / 2;
  85. if (out_size < 1024) out_size = 1024;
  86. *out = tor_malloc(out_size);
  87. stream->next_out = (unsigned char*)*out;
  88. stream->avail_out = out_size;
  89. while (1) {
  90. switch (deflate(stream, Z_FINISH))
  91. {
  92. case Z_STREAM_END:
  93. goto done;
  94. case Z_OK:
  95. /* In case zlib doesn't work as I think .... */
  96. if (stream->avail_out >= stream->avail_in+16)
  97. break;
  98. case Z_BUF_ERROR:
  99. offset = stream->next_out - ((unsigned char*)*out);
  100. out_size *= 2;
  101. *out = tor_realloc(*out, out_size);
  102. stream->next_out = (unsigned char*)(*out + offset);
  103. stream->avail_out = out_size - offset;
  104. break;
  105. default:
  106. log_fn(LOG_WARN, "Gzip compression didn't finish: %s",
  107. stream->msg ? stream->msg : "<no message>");
  108. goto err;
  109. }
  110. }
  111. done:
  112. *out_len = stream->total_out;
  113. if (deflateEnd(stream)!=Z_OK) {
  114. log_fn(LOG_WARN, "Error freeing gzip structures");
  115. goto err;
  116. }
  117. tor_free(stream);
  118. return 0;
  119. err:
  120. if (stream) {
  121. deflateEnd(stream);
  122. tor_free(stream);
  123. }
  124. if (*out) {
  125. tor_free(*out);
  126. }
  127. return -1;
  128. }
  129. /** Given or more zlib-compressed or gzip-compressed strings of total length
  130. * <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
  131. * buffer, using the method described in <b>method</b>. Store the uncompressed
  132. * string in *<b>out</b>, and its length in *<b>out_len</b>. Return 0 on
  133. * success, -1 on failure.
  134. */
  135. int
  136. tor_gzip_uncompress(char **out, size_t *out_len,
  137. const char *in, size_t in_len,
  138. compress_method_t method)
  139. {
  140. struct z_stream_s *stream = NULL;
  141. size_t out_size;
  142. off_t offset;
  143. int r;
  144. tor_assert(out);
  145. tor_assert(out_len);
  146. tor_assert(in);
  147. if (method == GZIP_METHOD && !is_gzip_supported()) {
  148. /* Old zlib version don't support gzip in inflateInit2 */
  149. log_fn(LOG_WARN, "Gzip not supported with zlib %s", ZLIB_VERSION);
  150. return -1;
  151. }
  152. *out = NULL;
  153. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  154. stream->zalloc = Z_NULL;
  155. stream->zfree = Z_NULL;
  156. stream->opaque = NULL;
  157. stream->next_in = (unsigned char*) in;
  158. stream->avail_in = in_len;
  159. if (inflateInit2(stream,
  160. method_bits(method)) != Z_OK) {
  161. log_fn(LOG_WARN, "Error from inflateInit2: %s",
  162. stream->msg?stream->msg:"<no message>");
  163. goto err;
  164. }
  165. out_size = in_len * 2; /* guess 50% compression. */
  166. if (out_size < 1024) out_size = 1024;
  167. *out = tor_malloc(out_size);
  168. stream->next_out = (unsigned char*)*out;
  169. stream->avail_out = out_size;
  170. while (1) {
  171. switch (inflate(stream, Z_FINISH))
  172. {
  173. case Z_STREAM_END:
  174. if (stream->avail_in == 0)
  175. goto done;
  176. if (inflateInit2(stream, method_bits(method)) != Z_OK) {
  177. log_fn(LOG_WARN, "Error from inflateInit2: %s",
  178. stream->msg?stream->msg:"<no message>");
  179. goto err;
  180. }
  181. break;
  182. case Z_OK:
  183. /* In case zlib doesn't work as I think.... */
  184. if (stream->avail_out >= stream->avail_in+16)
  185. break;
  186. case Z_BUF_ERROR:
  187. offset = stream->next_out - (unsigned char*)*out;
  188. out_size *= 2;
  189. *out = tor_realloc(*out, out_size);
  190. stream->next_out = (unsigned char*)(*out + offset);
  191. stream->avail_out = out_size - offset;
  192. break;
  193. default:
  194. log_fn(LOG_WARN, "Gzip decompression returned an error: %s",
  195. stream->msg ? stream->msg : "<no message>");
  196. goto err;
  197. }
  198. }
  199. done:
  200. *out_len = stream->next_out - (unsigned char*)*out;
  201. r = inflateEnd(stream);
  202. tor_free(stream);
  203. if (r != Z_OK) {
  204. log_fn(LOG_WARN, "Error freeing gzip structures");
  205. goto err;
  206. }
  207. /* NUL-terminate output. */
  208. if (out_size == *out_len)
  209. *out = tor_realloc(*out, out_size + 1);
  210. (*out)[*out_len] = '\0';
  211. return 0;
  212. err:
  213. if (stream) {
  214. inflateEnd(stream);
  215. tor_free(stream);
  216. }
  217. if (*out) {
  218. tor_free(*out);
  219. }
  220. return -1;
  221. }
  222. /** Try to tell whether the <b>in_len</b>-byte string in <b>in</b> is likely
  223. * to be compressed or not. If it is, return the likeliest compression method.
  224. * Otherwise, return 0.
  225. */
  226. int detect_compression_method(const char *in, size_t in_len)
  227. {
  228. if (in_len > 2 && !memcmp(in, "\x1f\x8b", 2)) {
  229. return GZIP_METHOD;
  230. } else if (in_len > 2 && (in[0] & 0x0f) == 8 &&
  231. (ntohs(get_uint16(in)) % 31) == 0) {
  232. return ZLIB_METHOD;
  233. } else {
  234. return 0;
  235. }
  236. }