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