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