Skip to content
Newer
Older
100644 274 lines (238 sloc) 6.13 KB
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
1 /*
2 * zlib wrappers to make sure we don't silently miss errors
3 * at init time.
4 */
5 #include "cache.h"
6
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
7 static const char *zerr_to_string(int status)
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
8 {
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
9 switch (status) {
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
10 case Z_MEM_ERROR:
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
11 return "out of memory";
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
12 case Z_VERSION_ERROR:
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
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";
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
20 default:
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
21 return "unknown error";
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
22 }
23 }
24
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
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 */
e01503b @gitster zlib: allow feeding more than 4GB in one go
gitster authored Jun 10, 2011
30 /* #define ZLIB_BUF_MAX ((uInt)-1) */
31 #define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
32 static inline uInt zlib_buf_cap(unsigned long len)
33 {
e01503b @gitster zlib: allow feeding more than 4GB in one go
gitster authored Jun 10, 2011
34 return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
35 }
36
37 static 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
47 static void zlib_post_call(git_zstream *s)
48 {
e01503b @gitster zlib: allow feeding more than 4GB in one go
gitster authored Jun 10, 2011
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;
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
61 s->next_in = s->z.next_in;
62 s->next_out = s->z.next_out;
e01503b @gitster zlib: allow feeding more than 4GB in one go
gitster authored Jun 10, 2011
63 s->avail_in -= bytes_consumed;
64 s->avail_out -= bytes_produced;
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
65 }
66
67 void git_inflate_init(git_zstream *strm)
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
68 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
69 int status;
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
70
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
71 zlib_pre_call(strm);
72 status = inflateInit(&strm->z);
73 zlib_post_call(strm);
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
74 if (status == Z_OK)
75 return;
76 die("inflateInit: %s (%s)", zerr_to_string(status),
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
77 strm->z.msg ? strm->z.msg : "no message");
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
78 }
79
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
80 void git_inflate_init_gzip_only(git_zstream *strm)
5e86c1f @gitster zlib: wrap inflateInit2 used to accept only for gzip format
gitster authored Jun 10, 2011
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;
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
87 int status;
5e86c1f @gitster zlib: wrap inflateInit2 used to accept only for gzip format
gitster authored Jun 10, 2011
88
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
89 zlib_pre_call(strm);
90 status = inflateInit2(&strm->z, windowBits);
91 zlib_post_call(strm);
5e86c1f @gitster zlib: wrap inflateInit2 used to accept only for gzip format
gitster authored Jun 10, 2011
92 if (status == Z_OK)
93 return;
94 die("inflateInit2: %s (%s)", zerr_to_string(status),
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
95 strm->z.msg ? strm->z.msg : "no message");
5e86c1f @gitster zlib: wrap inflateInit2 used to accept only for gzip format
gitster authored Jun 10, 2011
96 }
97
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
98 void git_inflate_end(git_zstream *strm)
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
99 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
100 int status;
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
101
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
102 zlib_pre_call(strm);
103 status = inflateEnd(&strm->z);
104 zlib_post_call(strm);
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
105 if (status == Z_OK)
106 return;
107 error("inflateEnd: %s (%s)", zerr_to_string(status),
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
108 strm->z.msg ? strm->z.msg : "no message");
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
109 }
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
110
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
111 int git_inflate(git_zstream *strm, int flush)
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
112 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
113 int status;
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
114
e01503b @gitster zlib: allow feeding more than 4GB in one go
gitster authored Jun 10, 2011
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
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
135 switch (status) {
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
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:
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
140 return status;
141 default:
142 break;
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
143 }
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
144 error("inflate: %s (%s)", zerr_to_string(status),
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
145 strm->z.msg ? strm->z.msg : "no message");
1a507fc @gitster zlib wrapper: refactor error message formatter
gitster authored Jun 10, 2011
146 return status;
b0613ce @jrn wrapper: give zlib wrappers their own translation unit
jrn authored Nov 6, 2010
147 }
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
148
225a6f1 @gitster zlib: wrap deflateBound() too
gitster authored Jun 10, 2011
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
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
153 unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
225a6f1 @gitster zlib: wrap deflateBound() too
gitster authored Jun 10, 2011
154 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
155 return deflateBound(&strm->z, size);
225a6f1 @gitster zlib: wrap deflateBound() too
gitster authored Jun 10, 2011
156 }
157
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
158 void git_deflate_init(git_zstream *strm, int level)
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
159 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
160 int status;
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
161
9a6f128 zlib: initialize git_zstream in git_deflate_init{,_gzip,_raw}
René Scharfe authored Mar 5, 2015
162 memset(strm, 0, sizeof(*strm));
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
163 zlib_pre_call(strm);
164 status = deflateInit(&strm->z, level);
165 zlib_post_call(strm);
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
166 if (status == Z_OK)
167 return;
168 die("deflateInit: %s (%s)", zerr_to_string(status),
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
169 strm->z.msg ? strm->z.msg : "no message");
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
170 }
171
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored Mar 15, 2013
172 static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
173 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
174 int status;
175
9a6f128 zlib: initialize git_zstream in git_deflate_init{,_gzip,_raw}
René Scharfe authored Mar 5, 2015
176 memset(strm, 0, sizeof(*strm));
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
177 zlib_pre_call(strm);
178 status = deflateInit2(&strm->z, level,
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
179 Z_DEFLATED, windowBits,
180 8, Z_DEFAULT_STRATEGY);
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
181 zlib_post_call(strm);
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
182 if (status == Z_OK)
183 return;
184 die("deflateInit2: %s (%s)", zerr_to_string(status),
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
185 strm->z.msg ? strm->z.msg : "no message");
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
186 }
187
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored Mar 15, 2013
188 void git_deflate_init_gzip(git_zstream *strm, int level)
189 {
190 /*
191 * Use default 15 bits, +16 is to generate gzip header/trailer
192 * instead of the zlib wrapper.
193 */
7f49036 @slattarini zlib: fix compilation failures with Sun C Compilaer
slattarini authored Apr 22, 2013
194 do_git_deflate_init(strm, level, 15 + 16);
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored Mar 15, 2013
195 }
196
197 void git_deflate_init_raw(git_zstream *strm, int level)
198 {
199 /*
200 * Use default 15 bits, negate the value to get raw compressed
201 * data without zlib header and trailer.
202 */
7f49036 @slattarini zlib: fix compilation failures with Sun C Compilaer
slattarini authored Apr 22, 2013
203 do_git_deflate_init(strm, level, -15);
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored Mar 15, 2013
204 }
205
568508e @gitster bulk-checkin: replace fast-import based implementation
gitster authored Oct 28, 2011
206 int git_deflate_abort(git_zstream *strm)
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
207 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
208 int status;
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
209
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
210 zlib_pre_call(strm);
211 status = deflateEnd(&strm->z);
212 zlib_post_call(strm);
568508e @gitster bulk-checkin: replace fast-import based implementation
gitster authored Oct 28, 2011
213 return status;
214 }
215
216 void git_deflate_end(git_zstream *strm)
217 {
218 int status = git_deflate_abort(strm);
219
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
220 if (status == Z_OK)
221 return;
222 error("deflateEnd: %s (%s)", zerr_to_string(status),
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
223 strm->z.msg ? strm->z.msg : "no message");
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
224 }
225
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
226 int git_deflate_end_gently(git_zstream *strm)
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
227 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
228 int status;
229
230 zlib_pre_call(strm);
231 status = deflateEnd(&strm->z);
232 zlib_post_call(strm);
233 return status;
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
234 }
235
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
236 int git_deflate(git_zstream *strm, int flush)
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
237 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
238 int status;
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
239
e01503b @gitster zlib: allow feeding more than 4GB in one go
gitster authored Jun 10, 2011
240 for (;;) {
241 zlib_pre_call(strm);
242
243 /* Never say Z_FINISH unless we are feeding everything */
244 status = deflate(&strm->z,
245 (strm->z.avail_in != strm->avail_in)
246 ? 0 : flush);
247 if (status == Z_MEM_ERROR)
248 die("deflate: out of memory");
249 zlib_post_call(strm);
250
251 /*
252 * Let zlib work another round, while we can still
253 * make progress.
254 */
255 if ((strm->avail_out && !strm->z.avail_out) &&
256 (status == Z_OK || status == Z_BUF_ERROR))
257 continue;
258 break;
259 }
260
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
261 switch (status) {
262 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
263 case Z_BUF_ERROR:
264 case Z_OK:
265 case Z_STREAM_END:
266 return status;
267 default:
268 break;
269 }
270 error("deflate: %s (%s)", zerr_to_string(status),
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored Jun 10, 2011
271 strm->z.msg ? strm->z.msg : "no message");
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored Jun 10, 2011
272 return status;
273 }
Something went wrong with that request. Please try again.