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)
25 #define BUFFER_SIZE 4096
26 #define ARGUMENT_ERROR -10
29 typedef gint (*read_write_func) (guchar *buffer, gint length, void *gchandle);
39 typedef struct _ZStream ZStream;
41 ZStream *CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle);
42 gint CloseZStream (ZStream *zstream);
43 gint Flush (ZStream *stream);
44 gint ReadZStream (ZStream *stream, guchar *buffer, gint length);
45 gint WriteZStream (ZStream *stream, guchar *buffer, gint length);
46 static gint flush_internal (ZStream *stream, gboolean is_final);
49 z_alloc (void *opaque, unsigned int nitems, unsigned int item_size)
51 return g_malloc0 (nitems * item_size);
55 z_free (void *opaque, void *ptr)
61 CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle)
70 #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
71 /* Older versions of zlib do not support raw deflate or gzip */
75 z = g_new0 (z_stream, 1);
77 retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
79 retval = inflateInit2 (z, gzip ? 31 : -15);
88 result = g_new0 (ZStream, 1);
91 result->gchandle = gchandle;
92 result->compress = compress;
93 result->buffer = g_new (guchar, BUFFER_SIZE);
94 result->stream->next_out = result->buffer;
95 result->stream->avail_out = BUFFER_SIZE;
96 result->stream->total_in = 0;
101 CloseZStream (ZStream *zstream)
107 return ARGUMENT_ERROR;
110 if (zstream->compress) {
111 if (zstream->stream->total_in > 0) {
113 status = deflate (zstream->stream, Z_FINISH);
114 flush_status = flush_internal (zstream, TRUE);
115 } while (status == Z_OK); /* We want Z_STREAM_END or error here here */
116 if (status == Z_STREAM_END)
117 status = flush_status;
119 deflateEnd (zstream->stream);
121 inflateEnd (zstream->stream);
123 g_free (zstream->buffer);
124 g_free (zstream->stream);
125 memset (zstream, 0, sizeof (ZStream));
131 write_to_managed (ZStream *stream)
137 if (zs->avail_out != BUFFER_SIZE) {
138 n = stream->func (stream->buffer, BUFFER_SIZE - zs->avail_out, stream->gchandle);
139 zs->next_out = stream->buffer;
140 zs->avail_out = BUFFER_SIZE;
148 flush_internal (ZStream *stream, gboolean is_final)
152 if (!stream->compress)
155 if (!is_final && stream->stream->avail_in != 0) {
156 status = deflate (stream->stream, Z_PARTIAL_FLUSH);
157 if (status != Z_OK && status != Z_STREAM_END)
160 return write_to_managed (stream);
164 Flush (ZStream *stream)
166 return flush_internal (stream, FALSE);
170 ReadZStream (ZStream *stream, guchar *buffer, gint length)
176 if (stream == NULL || buffer == NULL || length < 0)
177 return ARGUMENT_ERROR;
183 zs->next_out = buffer;
184 zs->avail_out = length;
185 while (zs->avail_out > 0) {
186 if (zs->avail_in == 0) {
187 n = stream->func (stream->buffer, BUFFER_SIZE, stream->gchandle);
188 stream->total_in += n;
192 zs->next_in = stream->buffer;
193 zs->avail_in = n < 0 ? 0 : n;
196 if (zs->avail_in == 0 && (zs->total_in == 0 || stream->total_in == zs->total_in))
199 status = inflate (stream->stream, Z_SYNC_FLUSH);
200 if (status == Z_STREAM_END) {
203 } else if (status != Z_OK) {
207 return length - zs->avail_out;
211 WriteZStream (ZStream *stream, guchar *buffer, gint length)
217 if (stream == NULL || buffer == NULL || length < 0)
218 return ARGUMENT_ERROR;
224 zs->next_in = buffer;
225 zs->avail_in = length;
226 while (zs->avail_in > 0) {
227 if (zs->avail_out == 0) {
228 zs->next_out = stream->buffer;
229 zs->avail_out = BUFFER_SIZE;
231 status = deflate (stream->stream, Z_NO_FLUSH);
232 if (status != Z_OK && status != Z_STREAM_END)
235 if (zs->avail_out == 0) {
236 n = write_to_managed (stream);