Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Schubert <schu@schu.io>2012-06-11 00:04:24 +0400
committerMichael Schubert <schu@schu.io>2012-10-09 23:28:31 +0400
commit4bc1a30f135db3edbcddf8a548f00353c54dacd8 (patch)
tree03f0552ca87c68601976b908df10f0d0786845c9 /src/compress.c
parent21e0d297af95e49b933c2a8d09994a32011354b1 (diff)
util: add git__compress()
Diffstat (limited to 'src/compress.c')
-rw-r--r--src/compress.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/compress.c b/src/compress.c
new file mode 100644
index 000000000..9388df1f2
--- /dev/null
+++ b/src/compress.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * 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 "compress.h"
+
+#include <zlib.h>
+
+#define BUFFER_SIZE (1024 * 1024)
+
+int git__compress(git_buf *buf, const void *buff, size_t len)
+{
+ z_stream zs;
+ char *zb;
+ size_t have;
+
+ memset(&zs, 0, sizeof(zs));
+ if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK)
+ return -1;
+
+ zb = git__malloc(BUFFER_SIZE);
+ GITERR_CHECK_ALLOC(zb);
+
+ zs.next_in = (void *)buff;
+ zs.avail_in = (uInt)len;
+
+ do {
+ zs.next_out = (unsigned char *)zb;
+ zs.avail_out = BUFFER_SIZE;
+
+ if (deflate(&zs, Z_FINISH) == Z_STREAM_ERROR) {
+ git__free(zb);
+ return -1;
+ }
+
+ have = BUFFER_SIZE - (size_t)zs.avail_out;
+
+ if (git_buf_put(buf, zb, have) < 0) {
+ git__free(zb);
+ return -1;
+ }
+
+ } while (zs.avail_out == 0);
+
+ assert(zs.avail_in == 0);
+
+ deflateEnd(&zs);
+ git__free(zb);
+ return 0;
+}