2010-01-13 Marek Safar <marek.safar@gmail.com>
[mono.git] / support / zlib-helper.c
index d01716a5c3cee5aaecdc877deafd7f9d111ae768..1d61c3d35fbd7f04c035197c9e952f435bb3b6d0 100644 (file)
@@ -13,6 +13,7 @@
 #include "zlib.h"
 #endif
 
+#include <glib.h>
 #include <string.h>
 #include <stdlib.h>
 
 #define ARGUMENT_ERROR -10
 #define IO_ERROR -11
 
-typedef int (*read_write_func) (unsigned char *buffer, int length);
+typedef gint (*read_write_func) (guchar *buffer, gint length, void *gchandle);
 struct _ZStream {
        z_stream *stream;
-       unsigned char *buffer;
+       guchar *buffer;
        read_write_func func;
-       unsigned char compress;
-       unsigned char eof;
+       void *gchandle;
+       guchar compress;
+       guchar eof;
 };
 typedef struct _ZStream ZStream;
 
-ZStream *CreateZStream (int compress, unsigned char gzip, read_write_func func);
-int CloseZStream (ZStream *zstream);
-int Flush (ZStream *stream);
-int ReadZStream (ZStream *stream, unsigned char *buffer, int length);
-int WriteZStream (ZStream *stream, unsigned char *buffer, int length);
+ZStream *CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle);
+gint CloseZStream (ZStream *zstream);
+gint Flush (ZStream *stream);
+gint ReadZStream (ZStream *stream, guchar *buffer, gint length);
+gint WriteZStream (ZStream *stream, guchar *buffer, gint length);
 
+static void *
+z_alloc (void *opaque, gsize nitems, gsize item_size)
+{
+       return g_malloc0 (nitems * item_size);
+}
+
+static void
+z_free (void *opaque, void *ptr)
+{
+       g_free (ptr);
+}
 
 ZStream *
-CreateZStream (int compress, unsigned char gzip, read_write_func func)
+CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle)
 {
        z_stream *z;
-       int retval;
+       gint retval;
        ZStream *result;
 
        if (func == NULL)
@@ -57,10 +70,7 @@ CreateZStream (int compress, unsigned char gzip, read_write_func func)
        return NULL;
 #endif
 
-       z = (z_stream *) malloc (sizeof (z_stream));
-       if (z == NULL)
-               return NULL;
-       memset (z, 0, sizeof (z_stream));
+       z = g_new0 (z_stream, 1);
        if (compress) {
                retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
        } else {
@@ -68,63 +78,59 @@ CreateZStream (int compress, unsigned char gzip, read_write_func func)
        }
 
        if (retval != Z_OK) {
-               free (z);
+               g_free (z);
                return NULL;
        }
-       result = malloc (sizeof (ZStream));
-       memset (result, 0, sizeof (ZStream));
+       z->zalloc = z_alloc;
+       z->zfree = z_free;
+       result = g_new0 (ZStream, 1);
        result->stream = z;
        result->func = func;
+       result->gchandle = gchandle;
        result->compress = compress;
-       result->buffer = (unsigned char *) malloc (BUFFER_SIZE);
-       if (result->buffer == NULL) {
-               free (result);
-               if (compress) {
-                       deflateEnd (z);
-               } else {
-                       inflateEnd (z);
-               }
-               free (z);
-               return NULL;
-       }
-       memset (result->buffer, 0, BUFFER_SIZE);
+       result->buffer = g_new (guchar, BUFFER_SIZE);
        return result;
 }
 
-int
+gint
 CloseZStream (ZStream *zstream)
 {
-       int status;
-       int flush_status;
+       gint status;
+       gint flush_status;
 
        if (zstream == NULL)
                return ARGUMENT_ERROR;
 
        status = 0;
        if (zstream->compress) {
-               status = deflate (zstream->stream, Z_FINISH);
-               flush_status = Flush (zstream);
-               if (status == Z_OK || status == Z_STREAM_END)
-                       status = flush_status;
+               if (zstream->stream->total_out) {
+                       do {
+                               status = deflate (zstream->stream, Z_FINISH);
+                               flush_status = Flush (zstream);
+                       } while (status == Z_OK); /* We want Z_STREAM_END or error here here */
+                       if (status == Z_STREAM_END)
+                               status = flush_status;
+               }
                deflateEnd (zstream->stream);
        } else {
                inflateEnd (zstream->stream);
        }
-       free (zstream->stream);
+       g_free (zstream->buffer);
+       g_free (zstream->stream);
        memset (zstream, 0, sizeof (ZStream));
-       free (zstream);
+       g_free (zstream);
        return status;
 }
 
-static int
+static gint
 write_to_managed (ZStream *stream)
 {
-       int n;
+       gint n;
        z_stream *zs;
 
        zs = stream->stream;
        if (zs->avail_out != BUFFER_SIZE) {
-               n = stream->func (stream->buffer, BUFFER_SIZE - zs->avail_out);
+               n = stream->func (stream->buffer, BUFFER_SIZE - zs->avail_out, stream->gchandle);
                zs->next_out = stream->buffer;
                zs->avail_out =  BUFFER_SIZE;
                if (n < 0)
@@ -133,7 +139,7 @@ write_to_managed (ZStream *stream)
        return 0;
 }
 
-int
+gint
 Flush (ZStream *stream)
 {
        if (!stream->compress)
@@ -142,11 +148,11 @@ Flush (ZStream *stream)
        return write_to_managed (stream);
 }
 
-int
-ReadZStream (ZStream *stream, unsigned char *buffer, int length)
+gint
+ReadZStream (ZStream *stream, guchar *buffer, gint length)
 {
-       int n;
-       int status;
+       gint n;
+       gint status;
        z_stream *zs;
 
        if (stream == NULL || buffer == NULL || length < 0)
@@ -160,7 +166,7 @@ ReadZStream (ZStream *stream, unsigned char *buffer, int length)
        zs->avail_out = length;
        while (zs->avail_out > 0) {
                if (zs->avail_in == 0) {
-                       n = stream->func (stream->buffer, BUFFER_SIZE);
+                       n = stream->func (stream->buffer, BUFFER_SIZE, stream->gchandle);
                        if (n <= 0) {
                                stream->eof = TRUE;
                                break;
@@ -170,17 +176,21 @@ ReadZStream (ZStream *stream, unsigned char *buffer, int length)
                }
 
                status = inflate (stream->stream, Z_SYNC_FLUSH);
-               if (status != Z_OK && status != Z_STREAM_END)
+               if (status == Z_STREAM_END) {
+                       stream->eof = TRUE;
+                       break;
+               } else if (status != Z_OK) {
                        return status;
+               }
        }
        return length - zs->avail_out;
 }
 
-int
-WriteZStream (ZStream *stream, unsigned char *buffer, int length)
+gint
+WriteZStream (ZStream *stream, guchar *buffer, gint length)
 {
-       int n;
-       int status;
+       gint n;
+       gint status;
        z_stream *zs;
 
        if (stream == NULL || buffer == NULL || length < 0)