]> git.proxmox.com Git - libgit2.git/blame - src/zstream.c
New upstream version 1.3.0+dfsg.1
[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
eae0bfdc
PP
8#include "zstream.h"
9
c6f26b48
ET
10#include <zlib.h>
11
c6f26b48
ET
12#include "buffer.h"
13
d9b04d78
RB
14#define ZSTREAM_BUFFER_SIZE (1024 * 1024)
15#define ZSTREAM_BUFFER_MIN_EXTRA 8
c6f26b48 16
eae0bfdc 17GIT_INLINE(int) zstream_seterr(git_zstream *zs)
c6f26b48 18{
eae0bfdc
PP
19 switch (zs->zerr) {
20 case Z_OK:
21 case Z_STREAM_END:
22 case Z_BUF_ERROR: /* not fatal; we retry with a larger buffer */
d9b04d78 23 return 0;
eae0bfdc 24 case Z_MEM_ERROR:
ac3d33df 25 git_error_set_oom();
eae0bfdc
PP
26 break;
27 default:
28 if (zs->z.msg)
ac3d33df 29 git_error_set_str(GIT_ERROR_ZLIB, zs->z.msg);
eae0bfdc 30 else
ac3d33df 31 git_error_set(GIT_ERROR_ZLIB, "unknown compression error");
eae0bfdc 32 }
c6f26b48
ET
33
34 return -1;
35}
36
b88f1713 37int git_zstream_init(git_zstream *zstream, git_zstream_t type)
c6f26b48 38{
b88f1713
ET
39 zstream->type = type;
40
41 if (zstream->type == GIT_ZSTREAM_INFLATE)
42 zstream->zerr = inflateInit(&zstream->z);
43 else
44 zstream->zerr = deflateInit(&zstream->z, Z_DEFAULT_COMPRESSION);
d9b04d78 45 return zstream_seterr(zstream);
c6f26b48
ET
46}
47
d9b04d78 48void git_zstream_free(git_zstream *zstream)
c6f26b48 49{
b88f1713
ET
50 if (zstream->type == GIT_ZSTREAM_INFLATE)
51 inflateEnd(&zstream->z);
52 else
53 deflateEnd(&zstream->z);
d9b04d78 54}
c6f26b48 55
d9b04d78
RB
56void git_zstream_reset(git_zstream *zstream)
57{
b88f1713
ET
58 if (zstream->type == GIT_ZSTREAM_INFLATE)
59 inflateReset(&zstream->z);
60 else
61 deflateReset(&zstream->z);
d9b04d78
RB
62 zstream->in = NULL;
63 zstream->in_len = 0;
64 zstream->zerr = Z_STREAM_END;
65}
c6f26b48 66
d9b04d78
RB
67int git_zstream_set_input(git_zstream *zstream, const void *in, size_t in_len)
68{
69 zstream->in = in;
70 zstream->in_len = in_len;
71 zstream->zerr = Z_OK;
72 return 0;
73}
c6f26b48 74
d9b04d78
RB
75bool git_zstream_done(git_zstream *zstream)
76{
77 return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
c6f26b48
ET
78}
79
22a2d3d5
UG
80bool git_zstream_eos(git_zstream *zstream)
81{
82 return zstream->zerr == Z_STREAM_END;
83}
84
d9b04d78 85size_t git_zstream_suggest_output_len(git_zstream *zstream)
52a8a130 86{
d9b04d78
RB
87 if (zstream->in_len > ZSTREAM_BUFFER_SIZE)
88 return ZSTREAM_BUFFER_SIZE;
89 else if (zstream->in_len > ZSTREAM_BUFFER_MIN_EXTRA)
90 return zstream->in_len;
91 else
92 return ZSTREAM_BUFFER_MIN_EXTRA;
52a8a130
ET
93}
94
eae0bfdc
PP
95int git_zstream_get_output_chunk(
96 void *out, size_t *out_len, git_zstream *zstream)
97{
98 size_t in_queued, in_used, out_queued;
99
100 /* set up input data */
101 zstream->z.next_in = (Bytef *)zstream->in;
102
103 /* feed as much data to zlib as it can consume, at most UINT_MAX */
104 if (zstream->in_len > UINT_MAX) {
105 zstream->z.avail_in = UINT_MAX;
106 zstream->flush = Z_NO_FLUSH;
107 } else {
108 zstream->z.avail_in = (uInt)zstream->in_len;
109 zstream->flush = Z_FINISH;
110 }
111 in_queued = (size_t)zstream->z.avail_in;
112
113 /* set up output data */
114 zstream->z.next_out = out;
115 zstream->z.avail_out = (uInt)*out_len;
116
117 if ((size_t)zstream->z.avail_out != *out_len)
118 zstream->z.avail_out = UINT_MAX;
119 out_queued = (size_t)zstream->z.avail_out;
120
121 /* compress next chunk */
122 if (zstream->type == GIT_ZSTREAM_INFLATE)
123 zstream->zerr = inflate(&zstream->z, zstream->flush);
124 else
125 zstream->zerr = deflate(&zstream->z, zstream->flush);
126
127 if (zstream_seterr(zstream))
128 return -1;
129
130 in_used = (in_queued - zstream->z.avail_in);
131 zstream->in_len -= in_used;
132 zstream->in += in_used;
133
134 *out_len = (out_queued - zstream->z.avail_out);
135
136 return 0;
137}
138
d9b04d78 139int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
c6f26b48 140{
d9b04d78
RB
141 size_t out_remain = *out_len;
142
b8dc2fdb 143 if (zstream->in_len && zstream->zerr == Z_STREAM_END) {
ac3d33df 144 git_error_set(GIT_ERROR_ZLIB, "zlib input had trailing garbage");
b8dc2fdb
ET
145 return -1;
146 }
147
d9b04d78 148 while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
eae0bfdc 149 size_t out_written = out_remain;
d9b04d78 150
eae0bfdc
PP
151 if (git_zstream_get_output_chunk(out, &out_written, zstream) < 0)
152 return -1;
d9b04d78 153
eae0bfdc
PP
154 out_remain -= out_written;
155 out = ((char *)out) + out_written;
d9b04d78
RB
156 }
157
158 /* either we finished the input or we did not flush the data */
c25aa7cd 159 GIT_ASSERT(zstream->in_len > 0 || zstream->flush == Z_FINISH);
d9b04d78
RB
160
161 /* set out_size to number of bytes actually written to output */
162 *out_len = *out_len - out_remain;
163
164 return 0;
c6f26b48
ET
165}
166
b88f1713 167static int zstream_buf(git_buf *out, const void *in, size_t in_len, git_zstream_t type)
c6f26b48 168{
d9b04d78 169 git_zstream zs = GIT_ZSTREAM_INIT;
c6f26b48
ET
170 int error = 0;
171
b88f1713 172 if ((error = git_zstream_init(&zs, type)) < 0)
1cb5a811 173 return error;
c6f26b48 174
d9b04d78
RB
175 if ((error = git_zstream_set_input(&zs, in, in_len)) < 0)
176 goto done;
c6f26b48 177
d9b04d78
RB
178 while (!git_zstream_done(&zs)) {
179 size_t step = git_zstream_suggest_output_len(&zs), written;
c6f26b48 180
4aa664ae 181 if ((error = git_buf_grow_by(out, step)) < 0)
d9b04d78 182 goto done;
c6f26b48 183
d9b04d78 184 written = out->asize - out->size;
c6f26b48 185
d9b04d78
RB
186 if ((error = git_zstream_get_output(
187 out->ptr + out->size, &written, &zs)) < 0)
188 goto done;
189
190 out->size += written;
d9b04d78 191 }
c6f26b48 192
19459b1e
RB
193 /* NULL terminate for consistency if possible */
194 if (out->size < out->asize)
195 out->ptr[out->size] = '\0';
196
d9b04d78
RB
197done:
198 git_zstream_free(&zs);
c6f26b48
ET
199 return error;
200}
b88f1713
ET
201
202int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
203{
204 return zstream_buf(out, in, in_len, GIT_ZSTREAM_DEFLATE);
205}
206
207int git_zstream_inflatebuf(git_buf *out, const void *in, size_t in_len)
208{
209 return zstream_buf(out, in, in_len, GIT_ZSTREAM_INFLATE);
210}