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 "zstream.h"
9 
10 #include <zlib.h>
11 
12 #include "buffer.h"
13 
14 #define ZSTREAM_BUFFER_SIZE (1024 * 1024)
15 #define ZSTREAM_BUFFER_MIN_EXTRA 8
16 
zstream_seterr(git_zstream * zs)17 GIT_INLINE(int) zstream_seterr(git_zstream *zs)
18 {
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 */
23 		return 0;
24 	case Z_MEM_ERROR:
25 		git_error_set_oom();
26 		break;
27 	default:
28 		if (zs->z.msg)
29 			git_error_set_str(GIT_ERROR_ZLIB, zs->z.msg);
30 		else
31 			git_error_set(GIT_ERROR_ZLIB, "unknown compression error");
32 	}
33 
34 	return -1;
35 }
36 
git_zstream_init(git_zstream * zstream,git_zstream_t type)37 int git_zstream_init(git_zstream *zstream, git_zstream_t type)
38 {
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);
45 	return zstream_seterr(zstream);
46 }
47 
git_zstream_free(git_zstream * zstream)48 void git_zstream_free(git_zstream *zstream)
49 {
50 	if (zstream->type == GIT_ZSTREAM_INFLATE)
51 		inflateEnd(&zstream->z);
52 	else
53 		deflateEnd(&zstream->z);
54 }
55 
git_zstream_reset(git_zstream * zstream)56 void git_zstream_reset(git_zstream *zstream)
57 {
58 	if (zstream->type == GIT_ZSTREAM_INFLATE)
59 		inflateReset(&zstream->z);
60 	else
61 		deflateReset(&zstream->z);
62 	zstream->in = NULL;
63 	zstream->in_len = 0;
64 	zstream->zerr = Z_STREAM_END;
65 }
66 
git_zstream_set_input(git_zstream * zstream,const void * in,size_t in_len)67 int 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 }
74 
git_zstream_done(git_zstream * zstream)75 bool git_zstream_done(git_zstream *zstream)
76 {
77 	return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
78 }
79 
git_zstream_eos(git_zstream * zstream)80 bool git_zstream_eos(git_zstream *zstream)
81 {
82 	return zstream->zerr == Z_STREAM_END;
83 }
84 
git_zstream_suggest_output_len(git_zstream * zstream)85 size_t git_zstream_suggest_output_len(git_zstream *zstream)
86 {
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;
93 }
94 
git_zstream_get_output_chunk(void * out,size_t * out_len,git_zstream * zstream)95 int 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 
git_zstream_get_output(void * out,size_t * out_len,git_zstream * zstream)139 int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
140 {
141 	size_t out_remain = *out_len;
142 
143 	if (zstream->in_len && zstream->zerr == Z_STREAM_END) {
144 		git_error_set(GIT_ERROR_ZLIB, "zlib input had trailing garbage");
145 		return -1;
146 	}
147 
148 	while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
149 		size_t out_written = out_remain;
150 
151 		if (git_zstream_get_output_chunk(out, &out_written, zstream) < 0)
152 			return -1;
153 
154 		out_remain -= out_written;
155 		out = ((char *)out) + out_written;
156 	}
157 
158 	/* either we finished the input or we did not flush the data */
159 	GIT_ASSERT(zstream->in_len > 0 || zstream->flush == Z_FINISH);
160 
161 	/* set out_size to number of bytes actually written to output */
162 	*out_len = *out_len - out_remain;
163 
164 	return 0;
165 }
166 
zstream_buf(git_buf * out,const void * in,size_t in_len,git_zstream_t type)167 static int zstream_buf(git_buf *out, const void *in, size_t in_len, git_zstream_t type)
168 {
169 	git_zstream zs = GIT_ZSTREAM_INIT;
170 	int error = 0;
171 
172 	if ((error = git_zstream_init(&zs, type)) < 0)
173 		return error;
174 
175 	if ((error = git_zstream_set_input(&zs, in, in_len)) < 0)
176 		goto done;
177 
178 	while (!git_zstream_done(&zs)) {
179 		size_t step = git_zstream_suggest_output_len(&zs), written;
180 
181 		if ((error = git_buf_grow_by(out, step)) < 0)
182 			goto done;
183 
184 		written = out->asize - out->size;
185 
186 		if ((error = git_zstream_get_output(
187 				out->ptr + out->size, &written, &zs)) < 0)
188 			goto done;
189 
190 		out->size += written;
191 	}
192 
193 	/* NULL terminate for consistency if possible */
194 	if (out->size < out->asize)
195 		out->ptr[out->size] = '\0';
196 
197 done:
198 	git_zstream_free(&zs);
199 	return error;
200 }
201 
git_zstream_deflatebuf(git_buf * out,const void * in,size_t in_len)202 int 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 
git_zstream_inflatebuf(git_buf * out,const void * in,size_t in_len)207 int 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 }
211