torgzip.c 4.8 KB

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