Merge pull request #3213 from henricm/fix-for-win-securestring-to-bstr
[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 MONO_API
21 #define MONO_API
22 #endif
23
24 #ifndef TRUE
25 #define FALSE 0
26 #define TRUE 1
27 #endif
28
29 #define BUFFER_SIZE 4096
30 #define ARGUMENT_ERROR -10
31 #define IO_ERROR -11
32
33 #define z_malloc(size)          ((gpointer) malloc(size))
34 #define z_malloc0(size)         ((gpointer) calloc(1,size))
35 #define z_new(type,size)        ((type *) z_malloc (sizeof (type) * (size)))
36 #define z_new0(type,size)       ((type *) z_malloc0 (sizeof (type)* (size)))
37
38 typedef gint (*read_write_func) (guchar *buffer, gint length, void *gchandle);
39 struct _ZStream {
40         z_stream *stream;
41         guchar *buffer;
42         read_write_func func;
43         void *gchandle;
44         guchar compress;
45         guchar eof;
46         guint32 total_in;
47 };
48 typedef struct _ZStream ZStream;
49
50 MONO_API ZStream *CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle);
51 MONO_API gint CloseZStream (ZStream *zstream);
52 MONO_API gint Flush (ZStream *stream);
53 MONO_API gint ReadZStream (ZStream *stream, guchar *buffer, gint length);
54 MONO_API gint WriteZStream (ZStream *stream, guchar *buffer, gint length);
55 static gint flush_internal (ZStream *stream, gboolean is_final);
56
57 static void *
58 z_alloc (void *opaque, unsigned int nitems, unsigned int item_size)
59 {
60         return z_malloc0 (nitems * item_size);
61 }
62
63 static void
64 z_free (void *opaque, void *ptr)
65 {
66         free (ptr);
67 }
68
69 ZStream *
70 CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle)
71 {
72         z_stream *z;
73         gint retval;
74         ZStream *result;
75
76         if (func == NULL)
77                 return NULL;
78
79 #if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1204)
80         /* Older versions of zlib do not support raw deflate or gzip */
81         return NULL;
82 #endif
83
84         z = z_new0 (z_stream, 1);
85         if (compress) {
86                 retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
87         } else {
88                 retval = inflateInit2 (z, gzip ? 31 : -15);
89         }
90
91         if (retval != Z_OK) {
92                 free (z);
93                 return NULL;
94         }
95         z->zalloc = z_alloc;
96         z->zfree = z_free;
97         result = z_new0 (ZStream, 1);
98         result->stream = z;
99         result->func = func;
100         result->gchandle = gchandle;
101         result->compress = compress;
102         result->buffer = z_new (guchar, BUFFER_SIZE);
103         result->stream->next_out = result->buffer;
104         result->stream->avail_out = BUFFER_SIZE;
105         result->stream->total_in = 0;
106         return result;
107 }
108
109 gint
110 CloseZStream (ZStream *zstream)
111 {
112         gint status;
113         gint flush_status;
114
115         if (zstream == NULL)
116                 return ARGUMENT_ERROR;
117
118         status = 0;
119         if (zstream->compress) {
120                 if (zstream->stream->total_in > 0) {
121                         do {
122                                 status = deflate (zstream->stream, Z_FINISH);
123                                 flush_status = flush_internal (zstream, TRUE);
124                         } while (status == Z_OK); /* We want Z_STREAM_END or error here here */
125                         if (status == Z_STREAM_END)
126                                 status = flush_status;
127                 }
128                 deflateEnd (zstream->stream);
129         } else {
130                 inflateEnd (zstream->stream);
131         }
132         free (zstream->buffer);
133         free (zstream->stream);
134         memset (zstream, 0, sizeof (ZStream));
135         free (zstream);
136         return status;
137 }
138
139 static gint
140 write_to_managed (ZStream *stream)
141 {
142         gint n;
143         z_stream *zs;
144
145         zs = stream->stream;
146         if (zs->avail_out != BUFFER_SIZE) {
147                 n = stream->func (stream->buffer, BUFFER_SIZE - zs->avail_out, stream->gchandle);
148                 zs->next_out = stream->buffer;
149                 zs->avail_out = BUFFER_SIZE;
150                 if (n < 0)
151                         return IO_ERROR;
152         }
153         return 0;
154 }
155
156 static gint
157 flush_internal (ZStream *stream, gboolean is_final)
158 {
159         gint status;
160
161         if (!stream->compress)
162                 return 0;
163
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)
167                         return status;
168         }
169         return write_to_managed (stream);
170 }
171
172 gint
173 Flush (ZStream *stream)
174 {
175         return flush_internal (stream, FALSE);
176 }
177
178 gint
179 ReadZStream (ZStream *stream, guchar *buffer, gint length)
180 {
181         gint n;
182         gint status;
183         z_stream *zs;
184
185         if (stream == NULL || buffer == NULL || length < 0)
186                 return ARGUMENT_ERROR;
187
188         if (stream->eof)
189                 return 0;
190
191         zs = stream->stream;
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);
197                         stream->total_in += n;
198                         if (n <= 0) {
199                                 stream->eof = TRUE;
200                         }
201                         zs->next_in = stream->buffer;
202                         zs->avail_in = n < 0 ? 0 : n;
203                 }
204
205                 if (zs->avail_in == 0 && (zs->total_in == 0 || stream->total_in == zs->total_in))
206                         return Z_STREAM_END;
207
208                 status = inflate (stream->stream, Z_SYNC_FLUSH);
209                 if (status == Z_STREAM_END) {
210                         stream->eof = TRUE;
211                         break;
212                 } else if (status != Z_OK) {
213                         return status;
214                 }
215         }
216         return length - zs->avail_out;
217 }
218
219 gint
220 WriteZStream (ZStream *stream, guchar *buffer, gint length)
221 {
222         gint n;
223         gint status;
224         z_stream *zs;
225
226         if (stream == NULL || buffer == NULL || length < 0)
227                 return ARGUMENT_ERROR;
228
229         if (stream->eof)
230                 return IO_ERROR;
231
232         zs = stream->stream;
233         zs->next_in = buffer;
234         zs->avail_in = length;
235         while (zs->avail_in > 0) {
236                 if (zs->avail_out == 0) {
237                         zs->next_out = stream->buffer;
238                         zs->avail_out = BUFFER_SIZE;
239                 }
240                 status = deflate (stream->stream, Z_NO_FLUSH);
241                 if (status != Z_OK && status != Z_STREAM_END)
242                         return status;
243
244                 if (zs->avail_out == 0) {
245                         n = write_to_managed (stream);
246                         if (n < 0)
247                                 return n;
248                 }
249         }
250         return length;
251 }
252