22
}
23
}
24
25
-void git_inflate_init(z_streamp strm)
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
+ */
30
+#define ZLIB_BUF_MAX ((uInt)-1)
31
+static inline uInt zlib_buf_cap(unsigned long len)
32
+{
33
+ if (ZLIB_BUF_MAX < len)
34
+ die("working buffer for zlib too large");
35
+ return len;
36
+}
37
+
38
+static 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
+
48
+static void zlib_post_call(git_zstream *s)
49
+{
50
+ s->next_in = s->z.next_in;
51
+ s->next_out = s->z.next_out;
52
+ s->total_in = s->z.total_in;
53
+ s->total_out = s->z.total_out;
54
+ s->avail_in = s->z.avail_in;
55
+ s->avail_out = s->z.avail_out;
56
+}
57
+
58
+void git_inflate_init(git_zstream *strm)
59
{
27
- int status = inflateInit(strm);
60
+ int status;
61
62
+ zlib_pre_call(strm);
63
+ status = inflateInit(&strm->z);
64
+ zlib_post_call(strm);
65
if (status == Z_OK)
66
return;
67
die("inflateInit: %s (%s)", zerr_to_string(status),
32
- strm->msg ? strm->msg : "no message");
68
+ strm->z.msg ? strm->z.msg : "no message");
69
}
70
35
-void git_inflate_init_gzip_only(z_streamp strm)
71
+void git_inflate_init_gzip_only(git_zstream *strm)
72
{
73
/*
74
* Use default 15 bits, +16 is to accept only gzip and to
75
* yield Z_DATA_ERROR when fed zlib format.
76
*/
77
const int windowBits = 15 + 16;
42
- int status = inflateInit2(strm, windowBits);
78
+ int status;
79
80
+ zlib_pre_call(strm);
81
+ status = inflateInit2(&strm->z, windowBits);
82
+ zlib_post_call(strm);
83
if (status == Z_OK)
84
return;
85
die("inflateInit2: %s (%s)", zerr_to_string(status),
47
- strm->msg ? strm->msg : "no message");
86
+ strm->z.msg ? strm->z.msg : "no message");
87
}
88
50
-void git_inflate_end(z_streamp strm)
89
+void git_inflate_end(git_zstream *strm)
90
{
52
- int status = inflateEnd(strm);
91
+ int status;
92
93
+ zlib_pre_call(strm);
94
+ status = inflateEnd(&strm->z);
95
+ zlib_post_call(strm);
96
if (status == Z_OK)
97
return;
98
error("inflateEnd: %s (%s)", zerr_to_string(status),
57
- strm->msg ? strm->msg : "no message");
99
+ strm->z.msg ? strm->z.msg : "no message");
100
}
101
60
-int git_inflate(z_streamp strm, int flush)
102
+int git_inflate(git_zstream *strm, int flush)
103
{
62
- int status = inflate(strm, flush);
104
+ int status;
105
106
+ zlib_pre_call(strm);
107
+ status = inflate(&strm->z, flush);
108
+ zlib_post_call(strm);
109
switch (status) {
110
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
111
case Z_BUF_ERROR:
119
break;
120
}
121
error("inflate: %s (%s)", zerr_to_string(status),
77
- strm->msg ? strm->msg : "no message");
122
+ strm->z.msg ? strm->z.msg : "no message");
123
return status;
124
}
125
127
#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
128
#endif
129
85
-unsigned long git_deflate_bound(z_streamp strm, unsigned long size)
130
+unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
131
{
87
- return deflateBound(strm, size);
132
+ return deflateBound(&strm->z, size);
133
}
134
90
-void git_deflate_init(z_streamp strm, int level)
135
+void git_deflate_init(git_zstream *strm, int level)
136
{
92
- int status = deflateInit(strm, level);
137
+ int status;
138
139
+ zlib_pre_call(strm);
140
+ status = deflateInit(&strm->z, level);
141
+ zlib_post_call(strm);
142
if (status == Z_OK)
143
return;
144
die("deflateInit: %s (%s)", zerr_to_string(status),
97
- strm->msg ? strm->msg : "no message");
145
+ strm->z.msg ? strm->z.msg : "no message");
146
}
147
100
-void git_deflate_init_gzip(z_streamp strm, int level)
148
+void git_deflate_init_gzip(git_zstream *strm, int level)
149
{
150
/*
151
* Use default 15 bits, +16 is to generate gzip header/trailer
152
* instead of the zlib wrapper.
153
*/
154
const int windowBits = 15 + 16;
107
- int status = deflateInit2(strm, level,
155
+ int status;
156
+
157
+ zlib_pre_call(strm);
158
+ status = deflateInit2(&strm->z, level,
159
Z_DEFLATED, windowBits,
160
8, Z_DEFAULT_STRATEGY);
161
+ zlib_post_call(strm);
162
if (status == Z_OK)
163
return;
164
die("deflateInit2: %s (%s)", zerr_to_string(status),
113
- strm->msg ? strm->msg : "no message");
165
+ strm->z.msg ? strm->z.msg : "no message");
166
}
167
116
-void git_deflate_end(z_streamp strm)
168
+void git_deflate_end(git_zstream *strm)
169
{
118
- int status = deflateEnd(strm);
170
+ int status;
171
172
+ zlib_pre_call(strm);
173
+ status = deflateEnd(&strm->z);
174
+ zlib_post_call(strm);
175
if (status == Z_OK)
176
return;
177
error("deflateEnd: %s (%s)", zerr_to_string(status),
123
- strm->msg ? strm->msg : "no message");
178
+ strm->z.msg ? strm->z.msg : "no message");
179
}
180
126
-int git_deflate_end_gently(z_streamp strm)
181
+int git_deflate_end_gently(git_zstream *strm)
182
{
128
- return deflateEnd(strm);
183
+ int status;
184
+
185
+ zlib_pre_call(strm);
186
+ status = deflateEnd(&strm->z);
187
+ zlib_post_call(strm);
188
+ return status;
189
}
190
131
-int git_deflate(z_streamp strm, int flush)
191
+int git_deflate(git_zstream *strm, int flush)
192
{
133
- int status = deflate(strm, flush);
193
+ int status;
194
195
+ zlib_pre_call(strm);
196
+ status = deflate(&strm->z, flush);
197
+ zlib_post_call(strm);
198
switch (status) {
199
/* Z_BUF_ERROR: normal, needs more space in the output buffer */
200
case Z_BUF_ERROR:
208
break;
209
}
210
error("deflate: %s (%s)", zerr_to_string(status),
148
- strm->msg ? strm->msg : "no message");
211
+ strm->z.msg ? strm->z.msg : "no message");
212
return status;
213
}