torgzip.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Copyright 2004 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file torgzip.c
  6. *
  7. * \brief Simple in-memory gzip implementation.
  8. **/
  9. #include "orconfig.h"
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <assert.h>
  13. #ifdef MS_WINDOWS
  14. #include "..\..\contrib\zlib\zlib.h"
  15. #else
  16. #include <zlib.h>
  17. #endif
  18. #include "util.h"
  19. #include "log.h"
  20. #include "torgzip.h"
  21. static int gzip_is_supported = -1;
  22. int
  23. is_gzip_supported(void)
  24. {
  25. if (gzip_is_supported >= 0)
  26. return gzip_is_supported;
  27. if (!strcmpstart(ZLIB_VERSION, "0.") ||
  28. !strcmpstart(ZLIB_VERSION, "1.0") ||
  29. !strcmpstart(ZLIB_VERSION, "1.1"))
  30. gzip_is_supported = 0;
  31. else
  32. gzip_is_supported = 1;
  33. return gzip_is_supported;
  34. }
  35. static INLINE int
  36. method_bits(compress_method_t method)
  37. {
  38. /* Bits+16 means "use gzip" in zlib >= 1.2 */
  39. return method == GZIP_METHOD ? 15+16 : 15;
  40. }
  41. int
  42. tor_gzip_compress(char **out, size_t *out_len,
  43. const char *in, size_t in_len,
  44. compress_method_t method)
  45. {
  46. struct z_stream_s *stream = NULL;
  47. size_t out_size;
  48. off_t offset;
  49. tor_assert(out);
  50. tor_assert(out_len);
  51. tor_assert(in);
  52. if (method == GZIP_METHOD && !is_gzip_supported()) {
  53. /* Old zlib version don't support gzip in deflateInit2 */
  54. log_fn(LOG_WARN, "Gzip not supported with zlib %s", ZLIB_VERSION);
  55. return -1;
  56. }
  57. *out = NULL;
  58. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  59. stream->zalloc = Z_NULL;
  60. stream->zfree = Z_NULL;
  61. stream->opaque = NULL;
  62. stream->next_in = (unsigned char*) in;
  63. stream->avail_in = in_len;
  64. if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
  65. method_bits(method),
  66. 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  67. log_fn(LOG_WARN, "Error from deflateInit2: %s",
  68. stream->msg?stream->msg:"<no message>");
  69. goto err;
  70. }
  71. /* Guess 50% compression. */
  72. out_size = in_len / 2;
  73. if (out_size < 1024) out_size = 1024;
  74. *out = tor_malloc(out_size);
  75. stream->next_out = *out;
  76. stream->avail_out = out_size;
  77. while (1) {
  78. switch (deflate(stream, Z_FINISH))
  79. {
  80. case Z_STREAM_END:
  81. goto done;
  82. case Z_OK:
  83. /* In case zlib doesn't work as I think .... */
  84. if (stream->avail_out >= stream->avail_in+16)
  85. break;
  86. case Z_BUF_ERROR:
  87. offset = stream->next_out - ((unsigned char*)*out);
  88. out_size *= 2;
  89. *out = tor_realloc(*out, out_size);
  90. stream->next_out = *out + offset;
  91. stream->avail_out = out_size - offset;
  92. break;
  93. default:
  94. log_fn(LOG_WARN, "Gzip compression didn't finish: %s",
  95. stream->msg ? stream->msg : "<no message>");
  96. goto err;
  97. }
  98. }
  99. done:
  100. *out_len = stream->total_out;
  101. if (deflateEnd(stream)!=Z_OK) {
  102. log_fn(LOG_WARN, "Error freeing gzip structures");
  103. goto err;
  104. }
  105. tor_free(stream);
  106. return 0;
  107. err:
  108. if (stream) {
  109. deflateEnd(stream);
  110. tor_free(stream);
  111. }
  112. if (*out) {
  113. tor_free(*out);
  114. }
  115. return -1;
  116. }
  117. int
  118. tor_gzip_uncompress(char **out, size_t *out_len,
  119. const char *in, size_t in_len,
  120. compress_method_t method)
  121. {
  122. struct z_stream_s *stream = NULL;
  123. size_t out_size;
  124. off_t offset;
  125. tor_assert(out);
  126. tor_assert(out_len);
  127. tor_assert(in);
  128. if (method == GZIP_METHOD && !is_gzip_supported()) {
  129. /* Old zlib version don't support gzip in inflateInit2 */
  130. log_fn(LOG_WARN, "Gzip not supported with zlib %s", ZLIB_VERSION);
  131. return -1;
  132. }
  133. *out = NULL;
  134. stream = tor_malloc_zero(sizeof(struct z_stream_s));
  135. stream->zalloc = Z_NULL;
  136. stream->zfree = Z_NULL;
  137. stream->opaque = NULL;
  138. stream->next_in = (unsigned char*) in;
  139. stream->avail_in = in_len;
  140. if (inflateInit2(stream,
  141. method_bits(method)) != Z_OK) {
  142. log_fn(LOG_WARN, "Error from inflateInit2: %s",
  143. stream->msg?stream->msg:"<no message>");
  144. goto err;
  145. }
  146. out_size = in_len * 2; /* guess 50% compression. */
  147. if (out_size < 1024) out_size = 1024;
  148. *out = tor_malloc(out_size);
  149. stream->next_out = *out;
  150. stream->avail_out = out_size;
  151. while (1) {
  152. switch(inflate(stream, Z_FINISH))
  153. {
  154. case Z_STREAM_END:
  155. goto done;
  156. case Z_OK:
  157. /* In case zlib doesn't work as I think.... */
  158. if (stream->avail_out >= stream->avail_in+16)
  159. break;
  160. case Z_BUF_ERROR:
  161. offset = stream->next_out - ((unsigned char*)*out);
  162. out_size *= 2;
  163. *out = tor_realloc(*out, out_size);
  164. stream->next_out = *out + offset;
  165. stream->avail_out = out_size - offset;
  166. break;
  167. default:
  168. log_fn(LOG_WARN, "Gzip decompression returned an error: %s",
  169. stream->msg ? stream->msg : "<no message>");
  170. goto err;
  171. }
  172. }
  173. done:
  174. *out_len = stream->total_out;
  175. if (inflateEnd(stream)!=Z_OK) {
  176. log_fn(LOG_WARN, "Error freeing gzip structures");
  177. goto err;
  178. }
  179. tor_free(stream);
  180. /* NUL-terminate output. */
  181. if (out_size == *out_len)
  182. *out = tor_realloc(*out, out_size + 1);
  183. (*out)[*out_len] = '\0';
  184. return 0;
  185. err:
  186. if (stream) {
  187. inflateEnd(stream);
  188. tor_free(stream);
  189. }
  190. if (*out) {
  191. tor_free(*out);
  192. }
  193. return -1;
  194. }