Hash :
52a8a130
Author :
Date :
2014-01-06T16:41:12
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
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include <zlib.h>
#include "zstream.h"
#include "buffer.h"
#define BUFFER_SIZE (1024 * 1024)
static int zstream_seterr(int zerr, git_zstream *zstream)
{
if (zerr == Z_MEM_ERROR)
giterr_set_oom();
else if (zstream->msg)
giterr_set(GITERR_ZLIB, zstream->msg);
else
giterr_set(GITERR_ZLIB, "Unknown compression error");
return -1;
}
int git_zstream_init(git_zstream *zstream)
{
int zerr;
if ((zerr = deflateInit(zstream, Z_DEFAULT_COMPRESSION)) != Z_OK)
return zstream_seterr(zerr, zstream);
return 0;
}
ssize_t git_zstream_deflate(void *out, size_t out_len, git_zstream *zstream, const void *in, size_t in_len)
{
int zerr;
if ((ssize_t)out_len < 0)
out_len = INT_MAX;
zstream->next_in = (Bytef *)in;
zstream->avail_in = in_len;
zstream->next_out = out;
zstream->avail_out = out_len;
if ((zerr = deflate(zstream, Z_FINISH)) == Z_STREAM_ERROR)
return zstream_seterr(zerr, zstream);
return (out_len - zstream->avail_out);
}
void git_zstream_reset(git_zstream *zstream)
{
deflateReset(zstream);
}
void git_zstream_free(git_zstream *zstream)
{
deflateEnd(zstream);
}
int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
{
git_zstream zstream = GIT_ZSTREAM_INIT;
size_t out_len;
ssize_t written;
int error = 0;
if ((error = git_zstream_init(&zstream)) < 0)
goto done;
do {
if (out->asize - out->size < BUFFER_SIZE)
git_buf_grow(out, out->asize + BUFFER_SIZE);
out_len = out->asize - out->size;
if ((written = git_zstream_deflate(out->ptr + out->size, out_len, &zstream, in, in_len)) <= 0)
break;
in = (char *)in + written;
in_len -= written;
out->size += written;
} while (written > 0);
if (written < 0)
error = written;
done:
git_zstream_free(&zstream);
return error;
}