zlib_macros.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Helper routines to use Zlib
  3. *
  4. * Author:
  5. * Christopher Lahey ([email protected])
  6. *
  7. * (C) 2004 Novell, Inc.
  8. */
  9. #include <config.h>
  10. #if defined (HAVE_ZLIB)
  11. #include <zlib.h>
  12. #else
  13. #include "zlib.h"
  14. #endif
  15. #include <stdlib.h>
  16. z_stream *
  17. create_z_stream(int compress, unsigned char gzip)
  18. {
  19. z_stream *z;
  20. int retval;
  21. #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
  22. /* Older versions of zlib do not support raw deflate or gzip */
  23. return NULL;
  24. #endif
  25. z = malloc (sizeof (z_stream));
  26. z->next_in = Z_NULL;
  27. z->avail_in = 0;
  28. z->next_out = Z_NULL;
  29. z->avail_out = 0;
  30. z->zalloc = Z_NULL;
  31. z->zfree = Z_NULL;
  32. z->opaque = NULL;
  33. if (compress) {
  34. retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
  35. } else {
  36. retval = inflateInit2 (z, gzip ? 31 : -15);
  37. }
  38. if (retval == Z_OK)
  39. return z;
  40. free (z);
  41. return NULL;
  42. }
  43. void
  44. free_z_stream(z_stream *z, int compress)
  45. {
  46. if (compress) {
  47. deflateEnd (z);
  48. } else {
  49. inflateEnd (z);
  50. }
  51. free (z);
  52. }
  53. void
  54. z_stream_set_next_in(z_stream *z, unsigned char *next_in)
  55. {
  56. z->next_in = next_in;
  57. }
  58. void
  59. z_stream_set_avail_in(z_stream *z, int avail_in)
  60. {
  61. z->avail_in = avail_in;
  62. }
  63. int
  64. z_stream_get_avail_in(z_stream *z)
  65. {
  66. return z->avail_in;
  67. }
  68. void
  69. z_stream_set_next_out(z_stream *z, unsigned char *next_out)
  70. {
  71. z->next_out = next_out;
  72. }
  73. void
  74. z_stream_set_avail_out(z_stream *z, int avail_out)
  75. {
  76. z->avail_out = avail_out;
  77. }
  78. int
  79. z_stream_deflate (z_stream *z, int flush, unsigned char *next_out, int *avail_out)
  80. {
  81. int ret_val;
  82. z->next_out = next_out;
  83. z->avail_out = *avail_out;
  84. ret_val = deflate (z, flush);
  85. *avail_out = z->avail_out;
  86. return ret_val;
  87. }
  88. int
  89. z_stream_inflate (z_stream *z, int *avail_out)
  90. {
  91. int ret_val;
  92. z->avail_out = *avail_out;
  93. ret_val = inflate (z, Z_NO_FLUSH);
  94. *avail_out = z->avail_out;
  95. return ret_val;
  96. }