2009-10-24 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / support / zlib-helper.c
1 /*
2  * Used by System.IO.Compression.DeflateStream
3  *
4  * Author:
5  *   Gonzalo Paniagua Javier (gonzalo@novell.com)
6  *
7  * (c) Copyright 2009 Novell, Inc.
8  */
9 #include <config.h>
10 #if defined (HAVE_ZLIB)
11 #include <zlib.h>
12 #else
13 #include "zlib.h"
14 #endif
15
16 #include <glib.h>
17 #include <string.h>
18 #include <stdlib.h>
19
20 #ifndef TRUE
21 #define FALSE 0
22 #define TRUE 1
23 #endif
24
25 #define BUFFER_SIZE 4096
26 #define ARGUMENT_ERROR -10
27 #define IO_ERROR -11
28
29 typedef gint (*read_write_func) (guchar *buffer, gint length, void *gchandle);
30 struct _ZStream {
31         z_stream *stream;
32         guchar *buffer;
33         read_write_func func;
34         void *gchandle;
35         guchar compress;
36         guchar eof;
37 };
38 typedef struct _ZStream ZStream;
39
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);
45
46 static void *
47 z_alloc (void *opaque, gsize nitems, gsize item_size)
48 {
49         return g_malloc0 (nitems * item_size);
50 }
51
52 static void
53 z_free (void *opaque, void *ptr)
54 {
55         g_free (ptr);
56 }
57
58 ZStream *
59 CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle)
60 {
61         z_stream *z;
62         gint retval;
63         ZStream *result;
64
65         if (func == NULL)
66                 return NULL;
67
68 #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
69         /* Older versions of zlib do not support raw deflate or gzip */
70         return NULL;
71 #endif
72
73         z = g_new0 (z_stream, 1);
74         if (compress) {
75                 retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
76         } else {
77                 retval = inflateInit2 (z, gzip ? 31 : -15);
78         }
79
80         if (retval != Z_OK) {
81                 g_free (z);
82                 return NULL;
83         }
84         z->zalloc = z_alloc;
85         z->zfree = z_free;
86         result = g_new0 (ZStream, 1);
87         result->stream = z;
88         result->func = func;
89         result->gchandle = gchandle;
90         result->compress = compress;
91         result->buffer = g_new (guchar, BUFFER_SIZE);
92         return result;
93 }
94
95 gint
96 CloseZStream (ZStream *zstream)
97 {
98         gint status;
99         gint flush_status;
100
101         if (zstream == NULL)
102                 return ARGUMENT_ERROR;
103
104         status = 0;
105         if (zstream->compress) {
106                 if (zstream->stream->total_out) {
107                         status = deflate (zstream->stream, Z_FINISH);
108                         flush_status = Flush (zstream);
109                         if (status == Z_OK || status == Z_STREAM_END)
110                                 status = flush_status;
111                 }
112                 deflateEnd (zstream->stream);
113         } else {
114                 inflateEnd (zstream->stream);
115         }
116         g_free (zstream->buffer);
117         g_free (zstream->stream);
118         memset (zstream, 0, sizeof (ZStream));
119         g_free (zstream);
120         return status;
121 }
122
123 static gint
124 write_to_managed (ZStream *stream)
125 {
126         gint n;
127         z_stream *zs;
128
129         zs = stream->stream;
130         if (zs->avail_out != BUFFER_SIZE) {
131                 n = stream->func (stream->buffer, BUFFER_SIZE - zs->avail_out, stream->gchandle);
132                 zs->next_out = stream->buffer;
133                 zs->avail_out =  BUFFER_SIZE;
134                 if (n < 0)
135                         return IO_ERROR;
136         }
137         return 0;
138 }
139
140 gint
141 Flush (ZStream *stream)
142 {
143         if (!stream->compress)
144                 return 0;
145
146         return write_to_managed (stream);
147 }
148
149 gint
150 ReadZStream (ZStream *stream, guchar *buffer, gint length)
151 {
152         gint n;
153         gint status;
154         z_stream *zs;
155
156         if (stream == NULL || buffer == NULL || length < 0)
157                 return ARGUMENT_ERROR;
158
159         if (stream->eof)
160                 return 0;
161
162         zs = stream->stream;
163         zs->next_out = buffer;
164         zs->avail_out = length;
165         while (zs->avail_out > 0) {
166                 if (zs->avail_in == 0) {
167                         n = stream->func (stream->buffer, BUFFER_SIZE, stream->gchandle);
168                         if (n <= 0) {
169                                 stream->eof = TRUE;
170                                 break;
171                         }
172                         zs->next_in = stream->buffer;
173                         zs->avail_in = n;
174                 }
175
176                 status = inflate (stream->stream, Z_SYNC_FLUSH);
177                 if (status == Z_STREAM_END) {
178                         stream->eof = TRUE;
179                         break;
180                 } else if (status != Z_OK) {
181                         return status;
182                 }
183         }
184         return length - zs->avail_out;
185 }
186
187 gint
188 WriteZStream (ZStream *stream, guchar *buffer, gint length)
189 {
190         gint n;
191         gint status;
192         z_stream *zs;
193
194         if (stream == NULL || buffer == NULL || length < 0)
195                 return ARGUMENT_ERROR;
196
197         if (stream->eof)
198                 return IO_ERROR;
199
200         zs = stream->stream;
201         zs->next_in = buffer;
202         zs->avail_in = length;
203         while (zs->avail_in > 0) {
204                 if (zs->avail_out == 0) {
205                         zs->next_out = stream->buffer;
206                         zs->avail_out = BUFFER_SIZE;
207                 }
208                 status = deflate (stream->stream, Z_SYNC_FLUSH);
209                 if (status != Z_OK && status != Z_STREAM_END)
210                         return status;
211
212                 if (zs->avail_out == 0) {
213                         n = write_to_managed (stream);
214                         if (n < 0)
215                                 return n;
216                 }
217         }
218         return length;
219 }
220