]> git.proxmox.com Git - libgit2.git/blame - src/compress.c
Merge branch 'development' into clar2
[libgit2.git] / src / compress.c
CommitLineData
4bc1a30f
MS
1/*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8#include "compress.h"
9
10#include <zlib.h>
11
12#define BUFFER_SIZE (1024 * 1024)
13
14int git__compress(git_buf *buf, const void *buff, size_t len)
15{
16 z_stream zs;
17 char *zb;
18 size_t have;
19
20 memset(&zs, 0, sizeof(zs));
21 if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK)
22 return -1;
23
24 zb = git__malloc(BUFFER_SIZE);
25 GITERR_CHECK_ALLOC(zb);
26
27 zs.next_in = (void *)buff;
28 zs.avail_in = (uInt)len;
29
30 do {
31 zs.next_out = (unsigned char *)zb;
32 zs.avail_out = BUFFER_SIZE;
33
34 if (deflate(&zs, Z_FINISH) == Z_STREAM_ERROR) {
35 git__free(zb);
36 return -1;
37 }
38
39 have = BUFFER_SIZE - (size_t)zs.avail_out;
40
41 if (git_buf_put(buf, zb, have) < 0) {
42 git__free(zb);
43 return -1;
44 }
45
46 } while (zs.avail_out == 0);
47
48 assert(zs.avail_in == 0);
49
50 deflateEnd(&zs);
51 git__free(zb);
52 return 0;
53}