[xbuild] Set ProjectFile and TargetName metadata on target outputs.
[mono.git] / support / zlib-helper.c
index 5e11cb341eb67de8e28164806a58a1f510beb3bb..6cfeb8c442c50f65645c9c611efb09f6149372fb 100644 (file)
@@ -42,6 +42,7 @@ 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 gint flush_internal (ZStream *stream, gboolean is_final);
 
 static void *
 z_alloc (void *opaque, gsize nitems, gsize item_size)
@@ -103,10 +104,12 @@ CloseZStream (ZStream *zstream)
 
        status = 0;
        if (zstream->compress) {
-               if (zstream->stream->total_out) {
-                       status = deflate (zstream->stream, Z_FINISH);
-                       flush_status = Flush (zstream);
-                       if (status == Z_OK || status == Z_STREAM_END)
+               if (zstream->stream->total_in > 0) {
+                       do {
+                               status = deflate (zstream->stream, Z_FINISH);
+                               flush_status = flush_internal (zstream, TRUE);
+                       } while (status == Z_OK); /* We want Z_STREAM_END or error here here */
+                       if (status == Z_STREAM_END)
                                status = flush_status;
                }
                deflateEnd (zstream->stream);
@@ -130,22 +133,35 @@ write_to_managed (ZStream *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;
+               zs->avail_out = BUFFER_SIZE;
                if (n < 0)
                        return IO_ERROR;
        }
        return 0;
 }
 
-gint
-Flush (ZStream *stream)
+static gint
+flush_internal (ZStream *stream, gboolean is_final)
 {
+       gint status;
+
        if (!stream->compress)
                return 0;
 
+       if (!is_final) {
+               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)
 {
@@ -205,7 +221,7 @@ WriteZStream (ZStream *stream, guchar *buffer, gint length)
                        zs->next_out = stream->buffer;
                        zs->avail_out = BUFFER_SIZE;
                }
-               status = deflate (stream->stream, Z_SYNC_FLUSH);
+               status = deflate (stream->stream, Z_NO_FLUSH);
                if (status != Z_OK && status != Z_STREAM_END)
                        return status;