1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
/*
* Used by System.IO.Compression.DeflateStream
*
* Author:
* Gonzalo Paniagua Javier (gonzalo@novell.com)
*
* (c) Copyright 2009 Novell, Inc.
*/
#include <config.h>
#if defined (HAVE_SYS_ZLIB)
#include <zlib.h>
#else
#include "../mono/zlib/zlib.h"
#endif
#include <glib.h>
#include <string.h>
#include <stdlib.h>
#ifndef MONO_API
#define MONO_API
#endif
#ifndef TRUE
#define FALSE 0
#define TRUE 1
#endif
#define BUFFER_SIZE 4096
#define ARGUMENT_ERROR -10
#define IO_ERROR -11
#define MONO_EXCEPTION -12
#define z_new0(type) ((type *) calloc (sizeof (type), 1))
typedef gint (*read_write_func) (guchar *buffer, gint length, void *gchandle);
struct _ZStream {
z_stream *stream;
guchar *buffer;
read_write_func func;
void *gchandle;
guchar compress;
guchar eof;
guint32 total_in;
};
typedef struct _ZStream ZStream;
// FIXME? Names should start "mono"?
MONO_API ZStream *CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle);
MONO_API gint CloseZStream (ZStream *zstream);
MONO_API gint Flush (ZStream *stream);
MONO_API gint ReadZStream (ZStream *stream, guchar *buffer, gint length);
MONO_API gint WriteZStream (ZStream *stream, guchar *buffer, gint length);
static gint flush_internal (ZStream *stream, gboolean is_final);
static void *
z_alloc (void *opaque, unsigned int nitems, unsigned int item_size)
{
return calloc (nitems, item_size);
}
static void
z_free (void *opaque, void *ptr)
{
free (ptr);
}
ZStream *
CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle)
{
z_stream *z;
gint retval;
ZStream *result;
if (func == NULL)
return NULL;
z = z_new0 (z_stream);
if (compress) {
retval = deflateInit2 (z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, gzip ? 31 : -15, 8, Z_DEFAULT_STRATEGY);
} else {
retval = inflateInit2 (z, gzip ? 31 : -15);
}
if (retval != Z_OK) {
free (z);
return NULL;
}
z->zalloc = z_alloc;
z->zfree = z_free;
result = z_new0 (ZStream);
result->stream = z;
result->func = func;
result->gchandle = gchandle;
result->compress = compress;
result->buffer = (guchar*)malloc (BUFFER_SIZE);
result->stream->next_out = result->buffer;
result->stream->avail_out = BUFFER_SIZE;
result->stream->total_in = 0;
return result;
}
gint
CloseZStream (ZStream *zstream)
{
gint status;
gint flush_status;
if (zstream == NULL)
return ARGUMENT_ERROR;
status = 0;
if (zstream->compress) {
if (zstream->stream->total_in > 0) {
do {
status = deflate (zstream->stream, Z_FINISH);
flush_status = flush_internal (zstream, TRUE);
if (flush_status == MONO_EXCEPTION) {
status = flush_status;
break;
}
} 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->buffer);
free (zstream->stream);
memset (zstream, 0, sizeof (ZStream));
free (zstream);
return status;
}
static gint
write_to_managed (ZStream *stream)
{
gint n;
z_stream *zs;
zs = stream->stream;
if (zs->avail_out != BUFFER_SIZE) {
n = stream->func (stream->buffer, BUFFER_SIZE - zs->avail_out, stream->gchandle);
zs->next_out = stream->buffer;
zs->avail_out = BUFFER_SIZE;
if (n == MONO_EXCEPTION)
return n;
if (n < 0)
return IO_ERROR;
}
return 0;
}
static gint
flush_internal (ZStream *stream, gboolean is_final)
{
gint status;
if (!stream->compress)
return 0;
if (!is_final && stream->stream->avail_in != 0) {
status = deflate (stream->stream, Z_PARTIAL_FLUSH);
if (status != Z_OK && status != Z_STREAM_END)
return status;
}
return write_to_managed (stream);
}
gint
Flush (ZStream *stream)
{
return flush_internal (stream, FALSE);
}
gint
ReadZStream (ZStream *stream, guchar *buffer, gint length)
{
gint n;
gint status;
z_stream *zs;
if (stream == NULL || buffer == NULL || length < 0)
return ARGUMENT_ERROR;
if (stream->eof)
return 0;
zs = stream->stream;
zs->next_out = buffer;
zs->avail_out = length;
while (zs->avail_out > 0) {
if (zs->avail_in == 0) {
n = stream->func (stream->buffer, BUFFER_SIZE, stream->gchandle);
if (n == MONO_EXCEPTION)
return n;
n = n < 0 ? 0 : n;
stream->total_in += n;
zs->next_in = stream->buffer;
zs->avail_in = n;
}
if (zs->avail_in == 0 && zs->total_in == 0)
return 0;
status = inflate (stream->stream, Z_SYNC_FLUSH);
if (status == Z_STREAM_END) {
stream->eof = TRUE;
break;
} else if (status == Z_BUF_ERROR && stream->total_in == zs->total_in) {
if (zs->avail_in != 0) {
stream->eof = TRUE;
}
break;
} else if (status != Z_OK) {
return status;
}
}
return length - zs->avail_out;
}
gint
WriteZStream (ZStream *stream, guchar *buffer, gint length)
{
gint n;
gint status;
z_stream *zs;
if (stream == NULL || buffer == NULL || length < 0)
return ARGUMENT_ERROR;
if (stream->eof)
return IO_ERROR;
zs = stream->stream;
zs->next_in = buffer;
zs->avail_in = length;
while (zs->avail_in > 0) {
if (zs->avail_out == 0) {
zs->next_out = stream->buffer;
zs->avail_out = BUFFER_SIZE;
}
status = deflate (stream->stream, Z_NO_FLUSH);
if (status != Z_OK && status != Z_STREAM_END)
return status;
if (zs->avail_out == 0) {
n = write_to_managed (stream);
if (n < 0)
return n;
}
}
return length;
}
|