blob: d43bbeb6daa4c195048f7012e0bccff8c5b4b5f1 [file] [log] [blame]
Jonathan Niederb0613ce2010-11-06 06:47:34 -05001/*
2 * zlib wrappers to make sure we don't silently miss errors
3 * at init time.
4 */
Elijah Newrend88dbaa2023-04-11 00:41:51 -07005#include "git-compat-util.h"
6#include "git-zlib.h"
Jonathan Niederb0613ce2010-11-06 06:47:34 -05007
Junio C Hamano1a507fc2011-06-10 10:31:34 -07008static const char *zerr_to_string(int status)
9{
10 switch (status) {
11 case Z_MEM_ERROR:
12 return "out of memory";
13 case Z_VERSION_ERROR:
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";
21 default:
22 return "unknown error";
23 }
24}
25
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070026/*
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 */
Junio C Hamanoe01503b2011-06-10 12:15:17 -070031/* #define ZLIB_BUF_MAX ((uInt)-1) */
32#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070033static inline uInt zlib_buf_cap(unsigned long len)
Jonathan Niederb0613ce2010-11-06 06:47:34 -050034{
Junio C Hamanoe01503b2011-06-10 12:15:17 -070035 return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070036}
Jonathan Niederb0613ce2010-11-06 06:47:34 -050037
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070038static 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{
Junio C Hamanoe01503b2011-06-10 12:15:17 -070050 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)
Johannes Schindelin033abf92018-05-02 11:38:39 +020056 BUG("total_out mismatch");
Junio C Hamanoe01503b2011-06-10 12:15:17 -070057 if (s->z.total_in != s->total_in + bytes_consumed)
Johannes Schindelin033abf92018-05-02 11:38:39 +020058 BUG("total_in mismatch");
Junio C Hamanoe01503b2011-06-10 12:15:17 -070059
60 s->total_out = s->z.total_out;
61 s->total_in = s->z.total_in;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070062 s->next_in = s->z.next_in;
63 s->next_out = s->z.next_out;
Junio C Hamanoe01503b2011-06-10 12:15:17 -070064 s->avail_in -= bytes_consumed;
65 s->avail_out -= bytes_produced;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070066}
67
68void git_inflate_init(git_zstream *strm)
69{
70 int status;
71
72 zlib_pre_call(strm);
73 status = inflateInit(&strm->z);
74 zlib_post_call(strm);
Junio C Hamano1a507fc2011-06-10 10:31:34 -070075 if (status == Z_OK)
Jonathan Niederb0613ce2010-11-06 06:47:34 -050076 return;
Junio C Hamano1a507fc2011-06-10 10:31:34 -070077 die("inflateInit: %s (%s)", zerr_to_string(status),
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070078 strm->z.msg ? strm->z.msg : "no message");
Jonathan Niederb0613ce2010-11-06 06:47:34 -050079}
80
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070081void git_inflate_init_gzip_only(git_zstream *strm)
Junio C Hamano5e86c1f2011-06-10 10:45:29 -070082{
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;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070088 int status;
Junio C Hamano5e86c1f2011-06-10 10:45:29 -070089
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070090 zlib_pre_call(strm);
91 status = inflateInit2(&strm->z, windowBits);
92 zlib_post_call(strm);
Junio C Hamano5e86c1f2011-06-10 10:45:29 -070093 if (status == Z_OK)
94 return;
95 die("inflateInit2: %s (%s)", zerr_to_string(status),
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070096 strm->z.msg ? strm->z.msg : "no message");
Junio C Hamano5e86c1f2011-06-10 10:45:29 -070097}
98
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070099void git_inflate_end(git_zstream *strm)
Jonathan Niederb0613ce2010-11-06 06:47:34 -0500100{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700101 int status;
Junio C Hamano1a507fc2011-06-10 10:31:34 -0700102
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700103 zlib_pre_call(strm);
104 status = inflateEnd(&strm->z);
105 zlib_post_call(strm);
Junio C Hamano1a507fc2011-06-10 10:31:34 -0700106 if (status == Z_OK)
107 return;
108 error("inflateEnd: %s (%s)", zerr_to_string(status),
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700109 strm->z.msg ? strm->z.msg : "no message");
Jonathan Niederb0613ce2010-11-06 06:47:34 -0500110}
111
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700112int git_inflate(git_zstream *strm, int flush)
Jonathan Niederb0613ce2010-11-06 06:47:34 -0500113{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700114 int status;
Jonathan Niederb0613ce2010-11-06 06:47:34 -0500115
Junio C Hamanoe01503b2011-06-10 12:15:17 -0700116 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
Junio C Hamano1a507fc2011-06-10 10:31:34 -0700136 switch (status) {
Jonathan Niederb0613ce2010-11-06 06:47:34 -0500137 /* 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:
Junio C Hamano1a507fc2011-06-10 10:31:34 -0700141 return status;
Junio C Hamano1a507fc2011-06-10 10:31:34 -0700142 default:
143 break;
Jonathan Niederb0613ce2010-11-06 06:47:34 -0500144 }
Junio C Hamano1a507fc2011-06-10 10:31:34 -0700145 error("inflate: %s (%s)", zerr_to_string(status),
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700146 strm->z.msg ? strm->z.msg : "no message");
Junio C Hamano1a507fc2011-06-10 10:31:34 -0700147 return status;
Jonathan Niederb0613ce2010-11-06 06:47:34 -0500148}
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700149
Junio C Hamano225a6f12011-06-10 11:18:17 -0700150#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
151#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
152#endif
153
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700154unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
Junio C Hamano225a6f12011-06-10 11:18:17 -0700155{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700156 return deflateBound(&strm->z, size);
Junio C Hamano225a6f12011-06-10 11:18:17 -0700157}
158
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700159void git_deflate_init(git_zstream *strm, int level)
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700160{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700161 int status;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700162
René Scharfe9a6f1282015-03-05 23:49:46 +0100163 memset(strm, 0, sizeof(*strm));
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700164 zlib_pre_call(strm);
165 status = deflateInit(&strm->z, level);
166 zlib_post_call(strm);
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700167 if (status == Z_OK)
168 return;
169 die("deflateInit: %s (%s)", zerr_to_string(status),
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700170 strm->z.msg ? strm->z.msg : "no message");
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700171}
172
René Scharfec3c2e1a2013-03-15 23:21:51 +0100173static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700174{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700175 int status;
176
René Scharfe9a6f1282015-03-05 23:49:46 +0100177 memset(strm, 0, sizeof(*strm));
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700178 zlib_pre_call(strm);
179 status = deflateInit2(&strm->z, level,
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700180 Z_DEFLATED, windowBits,
181 8, Z_DEFAULT_STRATEGY);
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700182 zlib_post_call(strm);
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700183 if (status == Z_OK)
184 return;
185 die("deflateInit2: %s (%s)", zerr_to_string(status),
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700186 strm->z.msg ? strm->z.msg : "no message");
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700187}
188
René Scharfec3c2e1a2013-03-15 23:21:51 +0100189void 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 */
Stefano Lattarini7f490362013-04-22 18:18:40 +0200195 do_git_deflate_init(strm, level, 15 + 16);
René Scharfec3c2e1a2013-03-15 23:21:51 +0100196}
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 */
Stefano Lattarini7f490362013-04-22 18:18:40 +0200204 do_git_deflate_init(strm, level, -15);
René Scharfec3c2e1a2013-03-15 23:21:51 +0100205}
206
Junio C Hamano568508e2011-10-28 14:48:40 -0700207int git_deflate_abort(git_zstream *strm)
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700208{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700209 int status;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700210
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700211 zlib_pre_call(strm);
212 status = deflateEnd(&strm->z);
213 zlib_post_call(strm);
Junio C Hamano568508e2011-10-28 14:48:40 -0700214 return status;
215}
216
217void git_deflate_end(git_zstream *strm)
218{
219 int status = git_deflate_abort(strm);
220
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700221 if (status == Z_OK)
222 return;
223 error("deflateEnd: %s (%s)", zerr_to_string(status),
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700224 strm->z.msg ? strm->z.msg : "no message");
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700225}
226
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700227int git_deflate_end_gently(git_zstream *strm)
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700228{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700229 int status;
230
231 zlib_pre_call(strm);
232 status = deflateEnd(&strm->z);
233 zlib_post_call(strm);
234 return status;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700235}
236
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700237int git_deflate(git_zstream *strm, int flush)
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700238{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700239 int status;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700240
Junio C Hamanoe01503b2011-06-10 12:15:17 -0700241 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
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700262 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;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700268 default:
269 break;
270 }
271 error("deflate: %s (%s)", zerr_to_string(status),
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700272 strm->z.msg ? strm->z.msg : "no message");
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700273 return status;
274}