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
);
38 typedef struct _ZStream ZStream
;
40 ZStream
*CreateZStream (gint compress
, guchar gzip
, read_write_func func
, void *gchandle
);
41 gint
CloseZStream (ZStream
*zstream
);
42 gint
Flush (ZStream
*stream
);
43 gint
ReadZStream (ZStream
*stream
, guchar
*buffer
, gint length
);
44 gint
WriteZStream (ZStream
*stream
, guchar
*buffer
, gint length
);
47 z_alloc (void *opaque
, gsize nitems
, gsize item_size
)
49 return g_malloc0 (nitems
* item_size
);
53 z_free (void *opaque
, void *ptr
)
59 CreateZStream (gint compress
, guchar gzip
, read_write_func func
, void *gchandle
)
68 #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
69 /* Older versions of zlib do not support raw deflate or gzip */
73 z
= g_new0 (z_stream
, 1);
75 retval
= deflateInit2 (z
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, gzip
? 31 : -15, 8, Z_DEFAULT_STRATEGY
);
77 retval
= inflateInit2 (z
, gzip
? 31 : -15);
86 result
= g_new0 (ZStream
, 1);
89 result
->gchandle
= gchandle
;
90 result
->compress
= compress
;
91 result
->buffer
= g_new (guchar
, BUFFER_SIZE
);
96 CloseZStream (ZStream
*zstream
)
102 return ARGUMENT_ERROR
;
105 if (zstream
->compress
) {
106 if (zstream
->stream
->total_in
> 0) {
108 status
= deflate (zstream
->stream
, Z_FINISH
);
109 flush_status
= Flush (zstream
);
110 } while (status
== Z_OK
); /* We want Z_STREAM_END or error here here */
111 if (status
== Z_STREAM_END
)
112 status
= flush_status
;
114 deflateEnd (zstream
->stream
);
116 inflateEnd (zstream
->stream
);
118 g_free (zstream
->buffer
);
119 g_free (zstream
->stream
);
120 memset (zstream
, 0, sizeof (ZStream
));
126 write_to_managed (ZStream
*stream
)
132 if (zs
->avail_out
!= BUFFER_SIZE
) {
133 n
= stream
->func (stream
->buffer
, BUFFER_SIZE
- zs
->avail_out
, stream
->gchandle
);
134 zs
->next_out
= stream
->buffer
;
135 zs
->avail_out
= BUFFER_SIZE
;
143 Flush (ZStream
*stream
)
145 if (!stream
->compress
)
148 return write_to_managed (stream
);
152 ReadZStream (ZStream
*stream
, guchar
*buffer
, gint length
)
158 if (stream
== NULL
|| buffer
== NULL
|| length
< 0)
159 return ARGUMENT_ERROR
;
165 zs
->next_out
= buffer
;
166 zs
->avail_out
= length
;
167 while (zs
->avail_out
> 0) {
168 if (zs
->avail_in
== 0) {
169 n
= stream
->func (stream
->buffer
, BUFFER_SIZE
, stream
->gchandle
);
174 zs
->next_in
= stream
->buffer
;
178 status
= inflate (stream
->stream
, Z_SYNC_FLUSH
);
179 if (status
== Z_STREAM_END
) {
182 } else if (status
!= Z_OK
) {
186 return length
- zs
->avail_out
;
190 WriteZStream (ZStream
*stream
, guchar
*buffer
, gint length
)
196 if (stream
== NULL
|| buffer
== NULL
|| length
< 0)
197 return ARGUMENT_ERROR
;
203 zs
->next_in
= buffer
;
204 zs
->avail_in
= length
;
205 while (zs
->avail_in
> 0) {
206 if (zs
->avail_out
== 0) {
207 zs
->next_out
= stream
->buffer
;
208 zs
->avail_out
= BUFFER_SIZE
;
210 status
= deflate (stream
->stream
, Z_NO_FLUSH
);
211 if (status
!= Z_OK
&& status
!= Z_STREAM_END
)
214 if (zs
->avail_out
== 0) {
215 n
= write_to_managed (stream
);