]> git.proxmox.com Git - libgit2.git/blame - src/zstream.c
Merge pull request #3303 from libgit2/cmn/index-add-submodule
[libgit2.git] / src / zstream.c
CommitLineData
c6f26b48
ET
1/*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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 <zlib.h>
9
10#include "zstream.h"
11#include "buffer.h"
12
d9b04d78
RB
13#define ZSTREAM_BUFFER_SIZE (1024 * 1024)
14#define ZSTREAM_BUFFER_MIN_EXTRA 8
c6f26b48 15
d9b04d78 16static int zstream_seterr(git_zstream *zs)
c6f26b48 17{
d9b04d78
RB
18 if (zs->zerr == Z_OK || zs->zerr == Z_STREAM_END)
19 return 0;
20
21 if (zs->zerr == Z_MEM_ERROR)
c6f26b48 22 giterr_set_oom();
d9b04d78
RB
23 else if (zs->z.msg)
24 giterr_set(GITERR_ZLIB, zs->z.msg);
c6f26b48
ET
25 else
26 giterr_set(GITERR_ZLIB, "Unknown compression error");
27
28 return -1;
29}
30
31int git_zstream_init(git_zstream *zstream)
32{
d9b04d78
RB
33 zstream->zerr = deflateInit(&zstream->z, Z_DEFAULT_COMPRESSION);
34 return zstream_seterr(zstream);
c6f26b48
ET
35}
36
d9b04d78 37void git_zstream_free(git_zstream *zstream)
c6f26b48 38{
d9b04d78
RB
39 deflateEnd(&zstream->z);
40}
c6f26b48 41
d9b04d78
RB
42void git_zstream_reset(git_zstream *zstream)
43{
44 deflateReset(&zstream->z);
45 zstream->in = NULL;
46 zstream->in_len = 0;
47 zstream->zerr = Z_STREAM_END;
48}
c6f26b48 49
d9b04d78
RB
50int git_zstream_set_input(git_zstream *zstream, const void *in, size_t in_len)
51{
52 zstream->in = in;
53 zstream->in_len = in_len;
54 zstream->zerr = Z_OK;
55 return 0;
56}
c6f26b48 57
d9b04d78
RB
58bool git_zstream_done(git_zstream *zstream)
59{
60 return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
c6f26b48
ET
61}
62
d9b04d78 63size_t git_zstream_suggest_output_len(git_zstream *zstream)
52a8a130 64{
d9b04d78
RB
65 if (zstream->in_len > ZSTREAM_BUFFER_SIZE)
66 return ZSTREAM_BUFFER_SIZE;
67 else if (zstream->in_len > ZSTREAM_BUFFER_MIN_EXTRA)
68 return zstream->in_len;
69 else
70 return ZSTREAM_BUFFER_MIN_EXTRA;
52a8a130
ET
71}
72
d9b04d78 73int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
c6f26b48 74{
d9b04d78
RB
75 int zflush = Z_FINISH;
76 size_t out_remain = *out_len;
77
78 while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
79 size_t out_queued, in_queued, out_used, in_used;
80
81 /* set up in data */
82 zstream->z.next_in = (Bytef *)zstream->in;
83 zstream->z.avail_in = (uInt)zstream->in_len;
84 if ((size_t)zstream->z.avail_in != zstream->in_len) {
85 zstream->z.avail_in = INT_MAX;
86 zflush = Z_NO_FLUSH;
87 } else {
88 zflush = Z_FINISH;
89 }
90 in_queued = (size_t)zstream->z.avail_in;
91
92 /* set up out data */
93 zstream->z.next_out = out;
94 zstream->z.avail_out = (uInt)out_remain;
95 if ((size_t)zstream->z.avail_out != out_remain)
96 zstream->z.avail_out = INT_MAX;
97 out_queued = (size_t)zstream->z.avail_out;
98
99 /* compress next chunk */
100 zstream->zerr = deflate(&zstream->z, zflush);
8606f33b 101
d9b04d78
RB
102 if (zstream->zerr == Z_STREAM_ERROR)
103 return zstream_seterr(zstream);
104
105 out_used = (out_queued - zstream->z.avail_out);
106 out_remain -= out_used;
107 out = ((char *)out) + out_used;
108
109 in_used = (in_queued - zstream->z.avail_in);
110 zstream->in_len -= in_used;
111 zstream->in += in_used;
112 }
113
114 /* either we finished the input or we did not flush the data */
115 assert(zstream->in_len > 0 || zflush == Z_FINISH);
116
117 /* set out_size to number of bytes actually written to output */
118 *out_len = *out_len - out_remain;
119
120 return 0;
c6f26b48
ET
121}
122
123int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
124{
d9b04d78 125 git_zstream zs = GIT_ZSTREAM_INIT;
c6f26b48
ET
126 int error = 0;
127
d9b04d78 128 if ((error = git_zstream_init(&zs)) < 0)
1cb5a811 129 return error;
c6f26b48 130
d9b04d78
RB
131 if ((error = git_zstream_set_input(&zs, in, in_len)) < 0)
132 goto done;
c6f26b48 133
d9b04d78
RB
134 while (!git_zstream_done(&zs)) {
135 size_t step = git_zstream_suggest_output_len(&zs), written;
c6f26b48 136
4aa664ae 137 if ((error = git_buf_grow_by(out, step)) < 0)
d9b04d78 138 goto done;
c6f26b48 139
d9b04d78 140 written = out->asize - out->size;
c6f26b48 141
d9b04d78
RB
142 if ((error = git_zstream_get_output(
143 out->ptr + out->size, &written, &zs)) < 0)
144 goto done;
145
146 out->size += written;
d9b04d78 147 }
c6f26b48 148
19459b1e
RB
149 /* NULL terminate for consistency if possible */
150 if (out->size < out->asize)
151 out->ptr[out->size] = '\0';
152
d9b04d78
RB
153done:
154 git_zstream_free(&zs);
c6f26b48
ET
155 return error;
156}