Merge pull request #2386 from BrzVlad/fix-jit
[mono.git] / support / zlib-helper.c
index d0911fc07647cd6fb03dc61c10f5c8b783ad0998..dbfd1c7e24a27e7dd67283e6c1df7401b17d6100 100644 (file)
@@ -90,6 +90,8 @@ CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle)
        result->gchandle = gchandle;
        result->compress = compress;
        result->buffer = g_new (guchar, BUFFER_SIZE);
+       result->stream->next_out = result->buffer;
+       result->stream->avail_out = BUFFER_SIZE;
        return result;
 }
 
@@ -148,7 +150,7 @@ flush_internal (ZStream *stream, gboolean is_final)
        if (!stream->compress)
                return 0;
 
-       if (!is_final) {
+       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;
@@ -185,9 +187,12 @@ ReadZStream (ZStream *stream, guchar *buffer, gint length)
                                stream->eof = TRUE;
                        }
                        zs->next_in = stream->buffer;
-                       zs->avail_in = n;
+                       zs->avail_in = n < 0 ? 0 : n;
                }
 
+               if (zs->avail_in == 0 && zs->total_in == 0)
+                       return Z_STREAM_END;
+
                status = inflate (stream->stream, Z_SYNC_FLUSH);
                if (status == Z_STREAM_END) {
                        stream->eof = TRUE;