]>
git.proxmox.com Git - libgit2.git/blob - src/zstream.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
13 #define BUFFER_SIZE (1024 * 1024)
15 static int zstream_seterr(int zerr
, git_zstream
*zstream
)
17 if (zerr
== Z_MEM_ERROR
)
19 else if (zstream
->msg
)
20 giterr_set(GITERR_ZLIB
, zstream
->msg
);
22 giterr_set(GITERR_ZLIB
, "Unknown compression error");
27 int git_zstream_init(git_zstream
*zstream
)
31 if ((zerr
= deflateInit(zstream
, Z_DEFAULT_COMPRESSION
)) != Z_OK
)
32 return zstream_seterr(zerr
, zstream
);
37 ssize_t
git_zstream_deflate(void *out
, size_t out_len
, git_zstream
*zstream
, const void *in
, size_t in_len
)
41 if ((ssize_t
)out_len
< 0)
44 zstream
->next_in
= (Bytef
*)in
;
45 zstream
->avail_in
= in_len
;
46 zstream
->next_out
= out
;
47 zstream
->avail_out
= out_len
;
49 if ((zerr
= deflate(zstream
, Z_FINISH
)) == Z_STREAM_ERROR
)
50 return zstream_seterr(zerr
, zstream
);
52 return (out_len
- zstream
->avail_out
);
55 void git_zstream_reset(git_zstream
*zstream
)
57 deflateReset(zstream
);
60 void git_zstream_free(git_zstream
*zstream
)
65 int git_zstream_deflatebuf(git_buf
*out
, const void *in
, size_t in_len
)
67 git_zstream zstream
= GIT_ZSTREAM_INIT
;
72 if ((error
= git_zstream_init(&zstream
)) < 0)
76 if (out
->asize
- out
->size
< BUFFER_SIZE
)
77 git_buf_grow(out
, out
->asize
+ BUFFER_SIZE
);
79 out_len
= out
->asize
- out
->size
;
81 if ((written
= git_zstream_deflate(out
->ptr
+ out
->size
, out_len
, &zstream
, in
, in_len
)) <= 0)
84 in
= (char *)in
+ written
;
87 } while (written
> 0);
92 git_zstream_free(&zstream
);