Raw
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
8 static 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
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 */
33
34 /* uLong is 32-bit on Windows, even on 64-bit systems */
35 #define ULONG_MAX_VALUE maximum_unsigned_value_of_type(uLong)
36 static inline uInt zlib_buf_cap(unsigned long len)
37 {
38 return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
39 }
40
41 static inline uLong zlib_uLong_cap(size_t s)
42 {
43 return s < ULONG_MAX_VALUE ? (uLong)s : ULONG_MAX_VALUE;
44 }
45
46 static void zlib_pre_call(git_zstream *s)
47 {
48 s->z.next_in = s->next_in;
49 s->z.next_out = s->next_out;
50 s->z.total_in = zlib_uLong_cap(s->total_in);
51 s->z.total_out = zlib_uLong_cap(s->total_out);
52 s->z.avail_in = zlib_buf_cap(s->avail_in);
53 s->z.avail_out = zlib_buf_cap(s->avail_out);
54 }
55
56 static void zlib_post_call(git_zstream *s, int status)
57 {
58 size_t bytes_consumed;
59 size_t bytes_produced;
60
61 bytes_consumed = s->z.next_in - s->next_in;
62 bytes_produced = s->z.next_out - s->next_out;
63 /*
64 * zlib's total_out/total_in are uLong which may wrap for >4GB.
65 * We track our own totals and verify only the low bits match.
66 */
67 if ((s->z.total_out & ULONG_MAX_VALUE) !=
68 ((zlib_uLong_cap(s->total_out) + bytes_produced) & ULONG_MAX_VALUE))
69 BUG("total_out mismatch");
70 /*
71 * zlib does not update total_in when it returns Z_NEED_DICT,
72 * causing a mismatch here. Skip the sanity check in that case.
73 */
74 if (status != Z_NEED_DICT &&
75 (s->z.total_in & ULONG_MAX_VALUE) !=
76 ((zlib_uLong_cap(s->total_in) + bytes_consumed) & ULONG_MAX_VALUE))
77 BUG("total_in mismatch");
78
79 s->total_out += bytes_produced;
80 s->total_in += bytes_consumed;
81 /* zlib-ng marks `next_in` as `const`, so we have to cast it away. */
82 s->next_in = (unsigned char *) s->z.next_in;
83 s->next_out = s->z.next_out;
84 s->avail_in -= bytes_consumed;
85 s->avail_out -= bytes_produced;
86 }
87
88 void git_inflate_init(git_zstream *strm)
89 {
90 int status;
91
92 zlib_pre_call(strm);
93 status = inflateInit(&strm->z);
94 zlib_post_call(strm, status);
95 if (status == Z_OK)
96 return;
97 die("inflateInit: %s (%s)", zerr_to_string(status),
98 strm->z.msg ? strm->z.msg : "no message");
99 }
100
101 void git_inflate_init_gzip_only(git_zstream *strm)
102 {
103 /*
104 * Use default 15 bits, +16 is to accept only gzip and to
105 * yield Z_DATA_ERROR when fed zlib format.
106 */
107 const int windowBits = 15 + 16;
108 int status;
109
110 zlib_pre_call(strm);
111 status = inflateInit2(&strm->z, windowBits);
112 zlib_post_call(strm, status);
113 if (status == Z_OK)
114 return;
115 die("inflateInit2: %s (%s)", zerr_to_string(status),
116 strm->z.msg ? strm->z.msg : "no message");
117 }
118
119 void git_inflate_end(git_zstream *strm)
120 {
121 int status;
122
123 zlib_pre_call(strm);
124 status = inflateEnd(&strm->z);
125 zlib_post_call(strm, status);
126 if (status == Z_OK)
127 return;
128 error("inflateEnd: %s (%s)", zerr_to_string(status),
129 strm->z.msg ? strm->z.msg : "no message");
130 }
131
132 int git_inflate(git_zstream *strm, int flush)
133 {
134 int status;
135
136 for (;;) {
137 zlib_pre_call(strm);
138 /* Never say Z_FINISH unless we are feeding everything */
139 status = inflate(&strm->z,
140 (strm->z.avail_in != strm->avail_in)
141 ? 0 : flush);
142 if (status == Z_MEM_ERROR)
143 die("inflate: out of memory");
144 zlib_post_call(strm, status);
145
146 /*
147 * Let zlib work another round, while we can still
148 * make progress.
149 */
150 if ((strm->avail_out && !strm->z.avail_out) &&
151 (status == Z_OK || status == Z_BUF_ERROR))
152 continue;
153 break;
154 }
155
156 switch (status) {
157 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
158 case Z_BUF_ERROR:
159 case Z_OK:
160 case Z_STREAM_END:
161 return status;
162 default:
163 break;
164 }
165 error("inflate: %s (%s)", zerr_to_string(status),
166 strm->z.msg ? strm->z.msg : "no message");
167 return status;
168 }
169
170 unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
171 {
172 return deflateBound(&strm->z, size);
173 }
174
175 void git_deflate_init(git_zstream *strm, int level)
176 {
177 int status;
178
179 memset(strm, 0, sizeof(*strm));
180 zlib_pre_call(strm);
181 status = deflateInit(&strm->z, level);
182 zlib_post_call(strm, status);
183 if (status == Z_OK)
184 return;
185 die("deflateInit: %s (%s)", zerr_to_string(status),
186 strm->z.msg ? strm->z.msg : "no message");
187 }
188
189 static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
190 {
191 int status;
192
193 memset(strm, 0, sizeof(*strm));
194 zlib_pre_call(strm);
195 status = deflateInit2(&strm->z, level,
196 Z_DEFLATED, windowBits,
197 8, Z_DEFAULT_STRATEGY);
198 zlib_post_call(strm, status);
199 if (status == Z_OK)
200 return;
201 die("deflateInit2: %s (%s)", zerr_to_string(status),
202 strm->z.msg ? strm->z.msg : "no message");
203 }
204
205 void git_deflate_init_gzip(git_zstream *strm, int level)
206 {
207 /*
208 * Use default 15 bits, +16 is to generate gzip header/trailer
209 * instead of the zlib wrapper.
210 */
211 do_git_deflate_init(strm, level, 15 + 16);
212 }
213
214 void git_deflate_init_raw(git_zstream *strm, int level)
215 {
216 /*
217 * Use default 15 bits, negate the value to get raw compressed
218 * data without zlib header and trailer.
219 */
220 do_git_deflate_init(strm, level, -15);
221 }
222
223 int git_deflate_abort(git_zstream *strm)
224 {
225 int status;
226
227 zlib_pre_call(strm);
228 status = deflateEnd(&strm->z);
229 zlib_post_call(strm, status);
230 return status;
231 }
232
233 void git_deflate_end(git_zstream *strm)
234 {
235 int status = git_deflate_abort(strm);
236
237 if (status == Z_OK)
238 return;
239 error("deflateEnd: %s (%s)", zerr_to_string(status),
240 strm->z.msg ? strm->z.msg : "no message");
241 }
242
243 int git_deflate_end_gently(git_zstream *strm)
244 {
245 int status;
246
247 zlib_pre_call(strm);
248 status = deflateEnd(&strm->z);
249 zlib_post_call(strm, status);
250 return status;
251 }
252
253 int git_deflate(git_zstream *strm, int flush)
254 {
255 int status;
256
257 for (;;) {
258 zlib_pre_call(strm);
259
260 /* Never say Z_FINISH unless we are feeding everything */
261 status = deflate(&strm->z,
262 (strm->z.avail_in != strm->avail_in)
263 ? 0 : flush);
264 if (status == Z_MEM_ERROR)
265 die("deflate: out of memory");
266 zlib_post_call(strm, status);
267
268 /*
269 * Let zlib work another round, while we can still
270 * make progress.
271 */
272 if ((strm->avail_out && !strm->z.avail_out) &&
273 (status == Z_OK || status == Z_BUF_ERROR))
274 continue;
275 break;
276 }
277
278 switch (status) {
279 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
280 case Z_BUF_ERROR:
281 case Z_OK:
282 case Z_STREAM_END:
283 return status;
284 default:
285 break;
286 }
287 error("deflate: %s (%s)", zerr_to_string(status),
288 strm->z.msg ? strm->z.msg : "no message");
289 return status;
290 }