]> git.proxmox.com Git - libgit2.git/blobdiff - src/zstream.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / zstream.c
index 141b49b27c2d8769e50a9ee3c584154af29ef8bc..cb8b125ed5babf62b393359b92bf7bb65d30c61f 100644 (file)
@@ -5,25 +5,31 @@
  * a Linking Exception. For full terms see the included COPYING file.
  */
 
+#include "zstream.h"
+
 #include <zlib.h>
 
-#include "zstream.h"
-#include "buffer.h"
+#include "str.h"
 
 #define ZSTREAM_BUFFER_SIZE (1024 * 1024)
 #define ZSTREAM_BUFFER_MIN_EXTRA 8
 
-static int zstream_seterr(git_zstream *zs)
+GIT_INLINE(int) zstream_seterr(git_zstream *zs)
 {
-       if (zs->zerr == Z_OK || zs->zerr == Z_STREAM_END)
+       switch (zs->zerr) {
+       case Z_OK:
+       case Z_STREAM_END:
+       case Z_BUF_ERROR: /* not fatal; we retry with a larger buffer */
                return 0;
-
-       if (zs->zerr == Z_MEM_ERROR)
-               giterr_set_oom();
-       else if (zs->z.msg)
-               giterr_set_str(GITERR_ZLIB, zs->z.msg);
-       else
-               giterr_set(GITERR_ZLIB, "unknown compression error");
+       case Z_MEM_ERROR:
+               git_error_set_oom();
+               break;
+       default:
+               if (zs->z.msg)
+                       git_error_set_str(GIT_ERROR_ZLIB, zs->z.msg);
+               else
+                       git_error_set(GIT_ERROR_ZLIB, "unknown compression error");
+       }
 
        return -1;
 }
@@ -71,6 +77,11 @@ bool git_zstream_done(git_zstream *zstream)
        return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
 }
 
+bool git_zstream_eos(git_zstream *zstream)
+{
+       return zstream->zerr == Z_STREAM_END;
+}
+
 size_t git_zstream_suggest_output_len(git_zstream *zstream)
 {
        if (zstream->in_len > ZSTREAM_BUFFER_SIZE)
@@ -81,57 +92,71 @@ size_t git_zstream_suggest_output_len(git_zstream *zstream)
                return ZSTREAM_BUFFER_MIN_EXTRA;
 }
 
+int git_zstream_get_output_chunk(
+       void *out, size_t *out_len, git_zstream *zstream)
+{
+       size_t in_queued, in_used, out_queued;
+
+       /* set up input data */
+       zstream->z.next_in = (Bytef *)zstream->in;
+
+       /* feed as much data to zlib as it can consume, at most UINT_MAX */
+       if (zstream->in_len > UINT_MAX) {
+               zstream->z.avail_in = UINT_MAX;
+               zstream->flush = Z_NO_FLUSH;
+       } else {
+               zstream->z.avail_in = (uInt)zstream->in_len;
+               zstream->flush = Z_FINISH;
+       }
+       in_queued = (size_t)zstream->z.avail_in;
+
+       /* set up output data */
+       zstream->z.next_out = out;
+       zstream->z.avail_out = (uInt)*out_len;
+
+       if ((size_t)zstream->z.avail_out != *out_len)
+               zstream->z.avail_out = UINT_MAX;
+       out_queued = (size_t)zstream->z.avail_out;
+
+       /* compress next chunk */
+       if (zstream->type == GIT_ZSTREAM_INFLATE)
+               zstream->zerr = inflate(&zstream->z, zstream->flush);
+       else
+               zstream->zerr = deflate(&zstream->z, zstream->flush);
+
+       if (zstream_seterr(zstream))
+               return -1;
+
+       in_used = (in_queued - zstream->z.avail_in);
+       zstream->in_len -= in_used;
+       zstream->in += in_used;
+
+       *out_len = (out_queued - zstream->z.avail_out);
+
+       return 0;
+}
+
 int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
 {
-       int zflush = Z_FINISH;
        size_t out_remain = *out_len;
 
        if (zstream->in_len && zstream->zerr == Z_STREAM_END) {
-               giterr_set(GITERR_ZLIB, "zlib input had trailing garbage");
+               git_error_set(GIT_ERROR_ZLIB, "zlib input had trailing garbage");
                return -1;
        }
 
        while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
-               size_t out_queued, in_queued, out_used, in_used;
-
-               /* set up in data */
-               zstream->z.next_in  = (Bytef *)zstream->in;
-               zstream->z.avail_in = (uInt)zstream->in_len;
-               if ((size_t)zstream->z.avail_in != zstream->in_len) {
-                       zstream->z.avail_in = INT_MAX;
-                       zflush = Z_NO_FLUSH;
-               } else {
-                       zflush = Z_FINISH;
-               }
-               in_queued = (size_t)zstream->z.avail_in;
-
-               /* set up out data */
-               zstream->z.next_out = out;
-               zstream->z.avail_out = (uInt)out_remain;
-               if ((size_t)zstream->z.avail_out != out_remain)
-                       zstream->z.avail_out = INT_MAX;
-               out_queued = (size_t)zstream->z.avail_out;
-
-               /* compress next chunk */
-               if (zstream->type == GIT_ZSTREAM_INFLATE)
-                       zstream->zerr = inflate(&zstream->z, zflush);
-               else
-                       zstream->zerr = deflate(&zstream->z, zflush);
-
-               if (zstream->zerr == Z_STREAM_ERROR)
-                       return zstream_seterr(zstream);
+               size_t out_written = out_remain;
 
-               out_used = (out_queued - zstream->z.avail_out);
-               out_remain -= out_used;
-               out = ((char *)out) + out_used;
+               if (git_zstream_get_output_chunk(out, &out_written, zstream) < 0)
+                       return -1;
 
-               in_used = (in_queued - zstream->z.avail_in);
-               zstream->in_len -= in_used;
-               zstream->in += in_used;
+               out_remain -= out_written;
+               out = ((char *)out) + out_written;
        }
 
        /* either we finished the input or we did not flush the data */
-       assert(zstream->in_len > 0 || zflush == Z_FINISH);
+       GIT_ASSERT(zstream->in_len > 0 || zstream->flush == Z_FINISH);
 
        /* set out_size to number of bytes actually written to output */
        *out_len = *out_len - out_remain;
@@ -139,7 +164,7 @@ int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
        return 0;
 }
 
-static int zstream_buf(git_buf *out, const void *in, size_t in_len, git_zstream_t type)
+static int zstream_buf(git_str *out, const void *in, size_t in_len, git_zstream_t type)
 {
        git_zstream zs = GIT_ZSTREAM_INIT;
        int error = 0;
@@ -153,7 +178,7 @@ static int zstream_buf(git_buf *out, const void *in, size_t in_len, git_zstream_
        while (!git_zstream_done(&zs)) {
                size_t step = git_zstream_suggest_output_len(&zs), written;
 
-               if ((error = git_buf_grow_by(out, step)) < 0)
+               if ((error = git_str_grow_by(out, step)) < 0)
                        goto done;
 
                written = out->asize - out->size;
@@ -174,12 +199,12 @@ done:
        return error;
 }
 
-int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
+int git_zstream_deflatebuf(git_str *out, const void *in, size_t in_len)
 {
        return zstream_buf(out, in, in_len, GIT_ZSTREAM_DEFLATE);
 }
 
-int git_zstream_inflatebuf(git_buf *out, const void *in, size_t in_len)
+int git_zstream_inflatebuf(git_str *out, const void *in, size_t in_len)
 {
        return zstream_buf(out, in, in_len, GIT_ZSTREAM_INFLATE);
 }