2 * Used by System.IO.Compression.DeflateStream
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
7 * (c) Copyright 2009 Novell, Inc.
10 #if defined (HAVE_SYS_ZLIB)
13 #include "../mono/zlib/zlib.h"
29 #define BUFFER_SIZE 4096
30 #define ARGUMENT_ERROR -10
32 #define MONO_EXCEPTION -12
34 #define z_new0(type) ((type *) calloc (sizeof (type), 1))
36 typedef gint (*read_write_func
) (guchar
*buffer
, gint length
, void *gchandle
);
46 typedef struct _ZStream ZStream
;
48 // FIXME? Names should start "mono"?
49 MONO_API ZStream
*CreateZStream (gint compress
, guchar gzip
, read_write_func func
, void *gchandle
);
50 MONO_API gint
CloseZStream (ZStream
*zstream
);
51 MONO_API gint
Flush (ZStream
*stream
);
52 MONO_API gint
ReadZStream (ZStream
*stream
, guchar
*buffer
, gint length
);
53 MONO_API gint
WriteZStream (ZStream
*stream
, guchar
*buffer
, gint length
);
54 static gint
flush_internal (ZStream
*stream
, gboolean is_final
);
57 z_alloc (void *opaque
, unsigned int nitems
, unsigned int item_size
)
59 return calloc (nitems
, item_size
);
63 z_free (void *opaque
, void *ptr
)
69 CreateZStream (gint compress
, guchar gzip
, read_write_func func
, void *gchandle
)
78 z
= z_new0 (z_stream
);
80 retval
= deflateInit2 (z
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, gzip
? 31 : -15, 8, Z_DEFAULT_STRATEGY
);
82 retval
= inflateInit2 (z
, gzip
? 31 : -15);
91 result
= z_new0 (ZStream
);
94 result
->gchandle
= gchandle
;
95 result
->compress
= compress
;
96 result
->buffer
= (guchar
*)malloc (BUFFER_SIZE
);
97 result
->stream
->next_out
= result
->buffer
;
98 result
->stream
->avail_out
= BUFFER_SIZE
;
99 result
->stream
->total_in
= 0;
104 CloseZStream (ZStream
*zstream
)
110 return ARGUMENT_ERROR
;
113 if (zstream
->compress
) {
114 if (zstream
->stream
->total_in
> 0) {
116 status
= deflate (zstream
->stream
, Z_FINISH
);
117 flush_status
= flush_internal (zstream
, TRUE
);
118 if (flush_status
== MONO_EXCEPTION
) {
119 status
= flush_status
;
122 } while (status
== Z_OK
); /* We want Z_STREAM_END or error here here */
123 if (status
== Z_STREAM_END
)
124 status
= flush_status
;
126 deflateEnd (zstream
->stream
);
128 inflateEnd (zstream
->stream
);
130 free (zstream
->buffer
);
131 free (zstream
->stream
);
132 memset (zstream
, 0, sizeof (ZStream
));
138 write_to_managed (ZStream
*stream
)
144 if (zs
->avail_out
!= BUFFER_SIZE
) {
145 n
= stream
->func (stream
->buffer
, BUFFER_SIZE
- zs
->avail_out
, stream
->gchandle
);
146 zs
->next_out
= stream
->buffer
;
147 zs
->avail_out
= BUFFER_SIZE
;
148 if (n
== MONO_EXCEPTION
)
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
) {
211 if (zs
->avail_in
!= 0) {
215 } else if (status
!= Z_OK
) {
219 return length
- zs
->avail_out
;
223 WriteZStream (ZStream
*stream
, guchar
*buffer
, gint length
)
229 if (stream
== NULL
|| buffer
== NULL
|| length
< 0)
230 return ARGUMENT_ERROR
;
236 zs
->next_in
= buffer
;
237 zs
->avail_in
= length
;
238 while (zs
->avail_in
> 0) {
239 if (zs
->avail_out
== 0) {
240 zs
->next_out
= stream
->buffer
;
241 zs
->avail_out
= BUFFER_SIZE
;
243 status
= deflate (stream
->stream
, Z_NO_FLUSH
);
244 if (status
!= Z_OK
&& status
!= Z_STREAM_END
)
247 if (zs
->avail_out
== 0) {
248 n
= write_to_managed (stream
);