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)
24 #define BUFFER_SIZE 4096
25 #define ARGUMENT_ERROR -10
28 typedef int (*read_write_func) (unsigned char *buffer, int length);
31 unsigned char *buffer;
33 unsigned char compress;
36 typedef struct _ZStream ZStream;
38 ZStream *CreateZStream (int compress, unsigned char gzip, read_write_func func);
39 int CloseZStream (ZStream *zstream);
40 int Flush (ZStream *stream);
41 int ReadZStream (ZStream *stream, unsigned char *buffer, int length);
42 int WriteZStream (ZStream *stream, unsigned char *buffer, int length);
46 CreateZStream (int compress, unsigned char gzip, read_write_func func)
55 #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
56 /* Older versions of zlib do not support raw deflate or gzip */
60 z = (z_stream *) malloc (sizeof (z_stream));
63 memset (z, 0, sizeof (z_stream));
65 retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
67 retval = inflateInit2 (z, gzip ? 31 : -15);
74 result = malloc (sizeof (ZStream));
75 memset (result, 0, sizeof (ZStream));
78 result->compress = compress;
79 result->buffer = (unsigned char *) malloc (BUFFER_SIZE);
80 if (result->buffer == NULL) {
90 memset (result->buffer, 0, BUFFER_SIZE);
95 CloseZStream (ZStream *zstream)
101 return ARGUMENT_ERROR;
104 if (zstream->compress) {
105 status = deflate (zstream->stream, Z_FINISH);
106 flush_status = Flush (zstream);
107 if (status == Z_OK || status == Z_STREAM_END)
108 status = flush_status;
109 deflateEnd (zstream->stream);
111 inflateEnd (zstream->stream);
113 free (zstream->stream);
114 memset (zstream, 0, sizeof (ZStream));
120 write_to_managed (ZStream *stream)
126 if (zs->avail_out != BUFFER_SIZE) {
127 n = stream->func (stream->buffer, BUFFER_SIZE - zs->avail_out);
128 zs->next_out = stream->buffer;
129 zs->avail_out = BUFFER_SIZE;
137 Flush (ZStream *stream)
139 if (!stream->compress)
142 return write_to_managed (stream);
146 ReadZStream (ZStream *stream, unsigned char *buffer, int length)
152 if (stream == NULL || buffer == NULL || length < 0)
153 return ARGUMENT_ERROR;
159 zs->next_out = buffer;
160 zs->avail_out = length;
161 while (zs->avail_out > 0) {
162 if (zs->avail_in == 0) {
163 n = stream->func (stream->buffer, BUFFER_SIZE);
168 zs->next_in = stream->buffer;
172 status = inflate (stream->stream, Z_SYNC_FLUSH);
173 if (status != Z_OK && status != Z_STREAM_END)
176 return length - zs->avail_out;
180 WriteZStream (ZStream *stream, unsigned char *buffer, int length)
186 if (stream == NULL || buffer == NULL || length < 0)
187 return ARGUMENT_ERROR;
193 zs->next_in = buffer;
194 zs->avail_in = length;
195 while (zs->avail_in > 0) {
196 if (zs->avail_out == 0) {
197 zs->next_out = stream->buffer;
198 zs->avail_out = BUFFER_SIZE;
200 status = deflate (stream->stream, Z_SYNC_FLUSH);
201 if (status != Z_OK && status != Z_STREAM_END)
204 if (zs->avail_out == 0) {
205 n = write_to_managed (stream);