git
/
git-zlib.c
274 строки · 6.2 Кб
1/*
2* zlib wrappers to make sure we don't silently miss errors
3* at init time.
4*/
5#include "git-compat-util.h"6#include "git-zlib.h"7
8static const char *zerr_to_string(int status)9{
10switch (status) {11case Z_MEM_ERROR:12return "out of memory";13case Z_VERSION_ERROR:14return "wrong version";15case Z_NEED_DICT:16return "needs dictionary";17case Z_DATA_ERROR:18return "data stream error";19case Z_STREAM_ERROR:20return "stream consistency error";21default:22return "unknown error";23}24}
25
26/*
27* avail_in and avail_out in zlib are counted in uInt, which typically
28* limits the size of the buffer we can use to 4GB when interacting
29* with zlib in a single call to inflate/deflate.
30*/
31/* #define ZLIB_BUF_MAX ((uInt)-1) */
32#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */33static inline uInt zlib_buf_cap(unsigned long len)34{
35return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;36}
37
38static void zlib_pre_call(git_zstream *s)39{
40s->z.next_in = s->next_in;41s->z.next_out = s->next_out;42s->z.total_in = s->total_in;43s->z.total_out = s->total_out;44s->z.avail_in = zlib_buf_cap(s->avail_in);45s->z.avail_out = zlib_buf_cap(s->avail_out);46}
47
48static void zlib_post_call(git_zstream *s)49{
50unsigned long bytes_consumed;51unsigned long bytes_produced;52
53bytes_consumed = s->z.next_in - s->next_in;54bytes_produced = s->z.next_out - s->next_out;55if (s->z.total_out != s->total_out + bytes_produced)56BUG("total_out mismatch");57if (s->z.total_in != s->total_in + bytes_consumed)58BUG("total_in mismatch");59
60s->total_out = s->z.total_out;61s->total_in = s->z.total_in;62s->next_in = s->z.next_in;63s->next_out = s->z.next_out;64s->avail_in -= bytes_consumed;65s->avail_out -= bytes_produced;66}
67
68void git_inflate_init(git_zstream *strm)69{
70int status;71
72zlib_pre_call(strm);73status = inflateInit(&strm->z);74zlib_post_call(strm);75if (status == Z_OK)76return;77die("inflateInit: %s (%s)", zerr_to_string(status),78strm->z.msg ? strm->z.msg : "no message");79}
80
81void git_inflate_init_gzip_only(git_zstream *strm)82{
83/*84* Use default 15 bits, +16 is to accept only gzip and to
85* yield Z_DATA_ERROR when fed zlib format.
86*/
87const int windowBits = 15 + 16;88int status;89
90zlib_pre_call(strm);91status = inflateInit2(&strm->z, windowBits);92zlib_post_call(strm);93if (status == Z_OK)94return;95die("inflateInit2: %s (%s)", zerr_to_string(status),96strm->z.msg ? strm->z.msg : "no message");97}
98
99void git_inflate_end(git_zstream *strm)100{
101int status;102
103zlib_pre_call(strm);104status = inflateEnd(&strm->z);105zlib_post_call(strm);106if (status == Z_OK)107return;108error("inflateEnd: %s (%s)", zerr_to_string(status),109strm->z.msg ? strm->z.msg : "no message");110}
111
112int git_inflate(git_zstream *strm, int flush)113{
114int status;115
116for (;;) {117zlib_pre_call(strm);118/* Never say Z_FINISH unless we are feeding everything */119status = inflate(&strm->z,120(strm->z.avail_in != strm->avail_in)121? 0 : flush);122if (status == Z_MEM_ERROR)123die("inflate: out of memory");124zlib_post_call(strm);125
126/*127* Let zlib work another round, while we can still
128* make progress.
129*/
130if ((strm->avail_out && !strm->z.avail_out) &&131(status == Z_OK || status == Z_BUF_ERROR))132continue;133break;134}135
136switch (status) {137/* Z_BUF_ERROR: normal, needs more space in the output buffer */138case Z_BUF_ERROR:139case Z_OK:140case Z_STREAM_END:141return status;142default:143break;144}145error("inflate: %s (%s)", zerr_to_string(status),146strm->z.msg ? strm->z.msg : "no message");147return status;148}
149
150#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200151#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)152#endif153
154unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)155{
156return deflateBound(&strm->z, size);157}
158
159void git_deflate_init(git_zstream *strm, int level)160{
161int status;162
163memset(strm, 0, sizeof(*strm));164zlib_pre_call(strm);165status = deflateInit(&strm->z, level);166zlib_post_call(strm);167if (status == Z_OK)168return;169die("deflateInit: %s (%s)", zerr_to_string(status),170strm->z.msg ? strm->z.msg : "no message");171}
172
173static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)174{
175int status;176
177memset(strm, 0, sizeof(*strm));178zlib_pre_call(strm);179status = deflateInit2(&strm->z, level,180Z_DEFLATED, windowBits,1818, Z_DEFAULT_STRATEGY);182zlib_post_call(strm);183if (status == Z_OK)184return;185die("deflateInit2: %s (%s)", zerr_to_string(status),186strm->z.msg ? strm->z.msg : "no message");187}
188
189void git_deflate_init_gzip(git_zstream *strm, int level)190{
191/*192* Use default 15 bits, +16 is to generate gzip header/trailer
193* instead of the zlib wrapper.
194*/
195do_git_deflate_init(strm, level, 15 + 16);196}
197
198void git_deflate_init_raw(git_zstream *strm, int level)199{
200/*201* Use default 15 bits, negate the value to get raw compressed
202* data without zlib header and trailer.
203*/
204do_git_deflate_init(strm, level, -15);205}
206
207int git_deflate_abort(git_zstream *strm)208{
209int status;210
211zlib_pre_call(strm);212status = deflateEnd(&strm->z);213zlib_post_call(strm);214return status;215}
216
217void git_deflate_end(git_zstream *strm)218{
219int status = git_deflate_abort(strm);220
221if (status == Z_OK)222return;223error("deflateEnd: %s (%s)", zerr_to_string(status),224strm->z.msg ? strm->z.msg : "no message");225}
226
227int git_deflate_end_gently(git_zstream *strm)228{
229int status;230
231zlib_pre_call(strm);232status = deflateEnd(&strm->z);233zlib_post_call(strm);234return status;235}
236
237int git_deflate(git_zstream *strm, int flush)238{
239int status;240
241for (;;) {242zlib_pre_call(strm);243
244/* Never say Z_FINISH unless we are feeding everything */245status = deflate(&strm->z,246(strm->z.avail_in != strm->avail_in)247? 0 : flush);248if (status == Z_MEM_ERROR)249die("deflate: out of memory");250zlib_post_call(strm);251
252/*253* Let zlib work another round, while we can still
254* make progress.
255*/
256if ((strm->avail_out && !strm->z.avail_out) &&257(status == Z_OK || status == Z_BUF_ERROR))258continue;259break;260}261
262switch (status) {263/* Z_BUF_ERROR: normal, needs more space in the output buffer */264case Z_BUF_ERROR:265case Z_OK:266case Z_STREAM_END:267return status;268default:269break;270}271error("deflate: %s (%s)", zerr_to_string(status),272strm->z.msg ? strm->z.msg : "no message");273return status;274}
275