]> git.ipfire.org Git - thirdparty/git.git/blame - git-zlib.c
The twentieth batch
[thirdparty/git.git] / git-zlib.c
CommitLineData
b0613ce0
JN
1/*
2 * zlib wrappers to make sure we don't silently miss errors
3 * at init time.
4 */
d88dbaa7
EN
5#include "git-compat-util.h"
6#include "git-zlib.h"
b0613ce0 7
1a507fc1 8static const char *zerr_to_string(int status)
b0613ce0 9{
1a507fc1 10 switch (status) {
b0613ce0 11 case Z_MEM_ERROR:
1a507fc1 12 return "out of memory";
b0613ce0 13 case Z_VERSION_ERROR:
1a507fc1
JH
14 return "wrong version";
15 case Z_NEED_DICT:
16 return "needs dictionary";
17 case Z_DATA_ERROR:
18 return "data stream error";
19 case Z_STREAM_ERROR:
20 return "stream consistency error";
b0613ce0 21 default:
1a507fc1 22 return "unknown error";
b0613ce0 23 }
b0613ce0
JN
24}
25
ef49a7a0
JH
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 */
e01503b5
JH
31/* #define ZLIB_BUF_MAX ((uInt)-1) */
32#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
ef49a7a0
JH
33static inline uInt zlib_buf_cap(unsigned long len)
34{
e01503b5 35 return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
ef49a7a0
JH
36}
37
38static void zlib_pre_call(git_zstream *s)
39{
40 s->z.next_in = s->next_in;
41 s->z.next_out = s->next_out;
42 s->z.total_in = s->total_in;
43 s->z.total_out = s->total_out;
44 s->z.avail_in = zlib_buf_cap(s->avail_in);
45 s->z.avail_out = zlib_buf_cap(s->avail_out);
46}
47
48static void zlib_post_call(git_zstream *s)
49{
e01503b5
JH
50 unsigned long bytes_consumed;
51 unsigned long bytes_produced;
52
53 bytes_consumed = s->z.next_in - s->next_in;
54 bytes_produced = s->z.next_out - s->next_out;
55 if (s->z.total_out != s->total_out + bytes_produced)
033abf97 56 BUG("total_out mismatch");
e01503b5 57 if (s->z.total_in != s->total_in + bytes_consumed)
033abf97 58 BUG("total_in mismatch");
e01503b5
JH
59
60 s->total_out = s->z.total_out;
61 s->total_in = s->z.total_in;
ef49a7a0
JH
62 s->next_in = s->z.next_in;
63 s->next_out = s->z.next_out;
e01503b5
JH
64 s->avail_in -= bytes_consumed;
65 s->avail_out -= bytes_produced;
ef49a7a0
JH
66}
67
68void git_inflate_init(git_zstream *strm)
b0613ce0 69{
ef49a7a0 70 int status;
1a507fc1 71
ef49a7a0
JH
72 zlib_pre_call(strm);
73 status = inflateInit(&strm->z);
74 zlib_post_call(strm);
1a507fc1
JH
75 if (status == Z_OK)
76 return;
77 die("inflateInit: %s (%s)", zerr_to_string(status),
ef49a7a0 78 strm->z.msg ? strm->z.msg : "no message");
b0613ce0
JN
79}
80
ef49a7a0 81void git_inflate_init_gzip_only(git_zstream *strm)
5e86c1fb
JH
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 */
87 const int windowBits = 15 + 16;
ef49a7a0 88 int status;
5e86c1fb 89
ef49a7a0
JH
90 zlib_pre_call(strm);
91 status = inflateInit2(&strm->z, windowBits);
92 zlib_post_call(strm);
5e86c1fb
JH
93 if (status == Z_OK)
94 return;
95 die("inflateInit2: %s (%s)", zerr_to_string(status),
ef49a7a0 96 strm->z.msg ? strm->z.msg : "no message");
5e86c1fb
JH
97}
98
ef49a7a0 99void git_inflate_end(git_zstream *strm)
b0613ce0 100{
ef49a7a0 101 int status;
b0613ce0 102
ef49a7a0
JH
103 zlib_pre_call(strm);
104 status = inflateEnd(&strm->z);
105 zlib_post_call(strm);
1a507fc1
JH
106 if (status == Z_OK)
107 return;
108 error("inflateEnd: %s (%s)", zerr_to_string(status),
ef49a7a0 109 strm->z.msg ? strm->z.msg : "no message");
1a507fc1 110}
b0613ce0 111
ef49a7a0 112int git_inflate(git_zstream *strm, int flush)
1a507fc1 113{
ef49a7a0 114 int status;
b0613ce0 115
e01503b5
JH
116 for (;;) {
117 zlib_pre_call(strm);
118 /* Never say Z_FINISH unless we are feeding everything */
119 status = inflate(&strm->z,
120 (strm->z.avail_in != strm->avail_in)
121 ? 0 : flush);
122 if (status == Z_MEM_ERROR)
123 die("inflate: out of memory");
124 zlib_post_call(strm);
125
126 /*
127 * Let zlib work another round, while we can still
128 * make progress.
129 */
130 if ((strm->avail_out && !strm->z.avail_out) &&
131 (status == Z_OK || status == Z_BUF_ERROR))
132 continue;
133 break;
134 }
135
1a507fc1 136 switch (status) {
b0613ce0
JN
137 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
138 case Z_BUF_ERROR:
139 case Z_OK:
140 case Z_STREAM_END:
1a507fc1 141 return status;
1a507fc1
JH
142 default:
143 break;
b0613ce0 144 }
1a507fc1 145 error("inflate: %s (%s)", zerr_to_string(status),
ef49a7a0 146 strm->z.msg ? strm->z.msg : "no message");
1a507fc1 147 return status;
b0613ce0 148}
55bb5c91 149
225a6f10
JH
150#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
151#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
152#endif
153
ef49a7a0 154unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
225a6f10 155{
ef49a7a0 156 return deflateBound(&strm->z, size);
225a6f10
JH
157}
158
ef49a7a0 159void git_deflate_init(git_zstream *strm, int level)
55bb5c91 160{
ef49a7a0 161 int status;
55bb5c91 162
9a6f1287 163 memset(strm, 0, sizeof(*strm));
ef49a7a0
JH
164 zlib_pre_call(strm);
165 status = deflateInit(&strm->z, level);
166 zlib_post_call(strm);
55bb5c91
JH
167 if (status == Z_OK)
168 return;
169 die("deflateInit: %s (%s)", zerr_to_string(status),
ef49a7a0 170 strm->z.msg ? strm->z.msg : "no message");
55bb5c91
JH
171}
172
c3c2e1a0 173static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
55bb5c91 174{
ef49a7a0
JH
175 int status;
176
9a6f1287 177 memset(strm, 0, sizeof(*strm));
ef49a7a0
JH
178 zlib_pre_call(strm);
179 status = deflateInit2(&strm->z, level,
55bb5c91
JH
180 Z_DEFLATED, windowBits,
181 8, Z_DEFAULT_STRATEGY);
ef49a7a0 182 zlib_post_call(strm);
55bb5c91
JH
183 if (status == Z_OK)
184 return;
185 die("deflateInit2: %s (%s)", zerr_to_string(status),
ef49a7a0 186 strm->z.msg ? strm->z.msg : "no message");
55bb5c91
JH
187}
188
c3c2e1a0
RS
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 */
7f49036f 195 do_git_deflate_init(strm, level, 15 + 16);
c3c2e1a0
RS
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 */
7f49036f 204 do_git_deflate_init(strm, level, -15);
c3c2e1a0
RS
205}
206
568508e7 207int git_deflate_abort(git_zstream *strm)
55bb5c91 208{
ef49a7a0 209 int status;
55bb5c91 210
ef49a7a0
JH
211 zlib_pre_call(strm);
212 status = deflateEnd(&strm->z);
213 zlib_post_call(strm);
568508e7
JH
214 return status;
215}
216
217void git_deflate_end(git_zstream *strm)
218{
219 int status = git_deflate_abort(strm);
220
55bb5c91
JH
221 if (status == Z_OK)
222 return;
223 error("deflateEnd: %s (%s)", zerr_to_string(status),
ef49a7a0 224 strm->z.msg ? strm->z.msg : "no message");
55bb5c91
JH
225}
226
ef49a7a0 227int git_deflate_end_gently(git_zstream *strm)
55bb5c91 228{
ef49a7a0
JH
229 int status;
230
231 zlib_pre_call(strm);
232 status = deflateEnd(&strm->z);
233 zlib_post_call(strm);
234 return status;
55bb5c91
JH
235}
236
ef49a7a0 237int git_deflate(git_zstream *strm, int flush)
55bb5c91 238{
ef49a7a0 239 int status;
55bb5c91 240
e01503b5
JH
241 for (;;) {
242 zlib_pre_call(strm);
243
244 /* Never say Z_FINISH unless we are feeding everything */
245 status = deflate(&strm->z,
246 (strm->z.avail_in != strm->avail_in)
247 ? 0 : flush);
248 if (status == Z_MEM_ERROR)
249 die("deflate: out of memory");
250 zlib_post_call(strm);
251
252 /*
253 * Let zlib work another round, while we can still
254 * make progress.
255 */
256 if ((strm->avail_out && !strm->z.avail_out) &&
257 (status == Z_OK || status == Z_BUF_ERROR))
258 continue;
259 break;
260 }
261
55bb5c91
JH
262 switch (status) {
263 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
264 case Z_BUF_ERROR:
265 case Z_OK:
266 case Z_STREAM_END:
267 return status;
55bb5c91
JH
268 default:
269 break;
270 }
271 error("deflate: %s (%s)", zerr_to_string(status),
ef49a7a0 272 strm->z.msg ? strm->z.msg : "no message");
55bb5c91
JH
273 return status;
274}