2 * Used by System.IO.Compression.DeflateStream
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
7 * (c) Copyright 2009 Novell, Inc.
10 #if defined (HAVE_ZLIB)
29 #define BUFFER_SIZE 4096
30 #define ARGUMENT_ERROR -10
33 #define z_malloc(size) ((gpointer) malloc(size))
34 #define z_malloc0(size) ((gpointer) calloc(1,size))
35 #define z_new(type,size) ((type *) z_malloc (sizeof (type) * (size)))
36 #define z_new0(type,size) ((type *) z_malloc0 (sizeof (type)* (size)))
38 typedef gint (*read_write_func) (guchar *buffer, gint length, void *gchandle);
48 typedef struct _ZStream ZStream;
50 MONO_API ZStream *CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle);
51 MONO_API gint CloseZStream (ZStream *zstream);
52 MONO_API gint Flush (ZStream *stream);
53 MONO_API gint ReadZStream (ZStream *stream, guchar *buffer, gint length);
54 MONO_API gint WriteZStream (ZStream *stream, guchar *buffer, gint length);
55 static gint flush_internal (ZStream *stream, gboolean is_final);
58 z_alloc (void *opaque, unsigned int nitems, unsigned int item_size)
60 return z_malloc0 (nitems * item_size);
64 z_free (void *opaque, void *ptr)
70 CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle)
79 #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
80 /* Older versions of zlib do not support raw deflate or gzip */
84 z = z_new0 (z_stream, 1);
86 retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
88 retval = inflateInit2 (z, gzip ? 31 : -15);
97 result = z_new0 (ZStream, 1);
100 result->gchandle = gchandle;
101 result->compress = compress;
102 result->buffer = z_new (guchar, BUFFER_SIZE);
103 result->stream->next_out = result->buffer;
104 result->stream->avail_out = BUFFER_SIZE;
105 result->stream->total_in = 0;
110 CloseZStream (ZStream *zstream)
116 return ARGUMENT_ERROR;
119 if (zstream->compress) {
120 if (zstream->stream->total_in > 0) {
122 status = deflate (zstream->stream, Z_FINISH);
123 flush_status = flush_internal (zstream, TRUE);
124 } while (status == Z_OK); /* We want Z_STREAM_END or error here here */
125 if (status == Z_STREAM_END)
126 status = flush_status;
128 deflateEnd (zstream->stream);
130 inflateEnd (zstream->stream);
132 free (zstream->buffer);
133 free (zstream->stream);
134 memset (zstream, 0, sizeof (ZStream));
140 write_to_managed (ZStream *stream)
146 if (zs->avail_out != BUFFER_SIZE) {
147 n = stream->func (stream->buffer, BUFFER_SIZE - zs->avail_out, stream->gchandle);
148 zs->next_out = stream->buffer;
149 zs->avail_out = BUFFER_SIZE;
157 flush_internal (ZStream *stream, gboolean is_final)
161 if (!stream->compress)
164 if (!is_final && stream->stream->avail_in != 0) {
165 status = deflate (stream->stream, Z_PARTIAL_FLUSH);
166 if (status != Z_OK && status != Z_STREAM_END)
169 return write_to_managed (stream);
173 Flush (ZStream *stream)
175 return flush_internal (stream, FALSE);
179 ReadZStream (ZStream *stream, guchar *buffer, gint length)
185 if (stream == NULL || buffer == NULL || length < 0)
186 return ARGUMENT_ERROR;
192 zs->next_out = buffer;
193 zs->avail_out = length;
194 while (zs->avail_out > 0) {
195 if (zs->avail_in == 0) {
196 n = stream->func (stream->buffer, BUFFER_SIZE, stream->gchandle);
198 stream->total_in += n;
199 zs->next_in = stream->buffer;
203 if (zs->avail_in == 0 && zs->total_in == 0)
206 status = inflate (stream->stream, Z_SYNC_FLUSH);
207 if (status == Z_STREAM_END) {
210 } else if (status == Z_BUF_ERROR && stream->total_in == zs->total_in) {
213 } else if (status != Z_OK) {
217 return length - zs->avail_out;
221 WriteZStream (ZStream *stream, guchar *buffer, gint length)
227 if (stream == NULL || buffer == NULL || length < 0)
228 return ARGUMENT_ERROR;
234 zs->next_in = buffer;
235 zs->avail_in = length;
236 while (zs->avail_in > 0) {
237 if (zs->avail_out == 0) {
238 zs->next_out = stream->buffer;
239 zs->avail_out = BUFFER_SIZE;
241 status = deflate (stream->stream, Z_NO_FLUSH);
242 if (status != Z_OK && status != Z_STREAM_END)
245 if (zs->avail_out == 0) {
246 n = write_to_managed (stream);