zlib.c 6.08 KB
Newer Older
1 2 3 4 5 6
/*
 * zlib wrappers to make sure we don't silently miss errors
 * at init time.
 */
#include "cache.h"

7
static const char *zerr_to_string(int status)
8
{
9
	switch (status) {
10
	case Z_MEM_ERROR:
11
		return "out of memory";
12
	case Z_VERSION_ERROR:
13 14 15 16 17 18 19
		return "wrong version";
	case Z_NEED_DICT:
		return "needs dictionary";
	case Z_DATA_ERROR:
		return "data stream error";
	case Z_STREAM_ERROR:
		return "stream consistency error";
20
	default:
21
		return "unknown error";
22 23 24
	}
}

25 26 27 28 29
/*
 * avail_in and avail_out in zlib are counted in uInt, which typically
 * limits the size of the buffer we can use to 4GB when interacting
 * with zlib in a single call to inflate/deflate.
 */
30 31
/* #define ZLIB_BUF_MAX ((uInt)-1) */
#define ZLIB_BUF_MAX ((uInt) 1024 * 1024 * 1024) /* 1GB */
32 33
static inline uInt zlib_buf_cap(unsigned long len)
{
34
	return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
35 36 37 38 39 40 41 42 43 44 45 46 47 48
}

static void zlib_pre_call(git_zstream *s)
{
	s->z.next_in = s->next_in;
	s->z.next_out = s->next_out;
	s->z.total_in = s->total_in;
	s->z.total_out = s->total_out;
	s->z.avail_in = zlib_buf_cap(s->avail_in);
	s->z.avail_out = zlib_buf_cap(s->avail_out);
}

static void zlib_post_call(git_zstream *s)
{
49 50 51 52 53 54 55 56 57 58 59 60
	unsigned long bytes_consumed;
	unsigned long bytes_produced;

	bytes_consumed = s->z.next_in - s->next_in;
	bytes_produced = s->z.next_out - s->next_out;
	if (s->z.total_out != s->total_out + bytes_produced)
		die("BUG: total_out mismatch");
	if (s->z.total_in != s->total_in + bytes_consumed)
		die("BUG: total_in mismatch");

	s->total_out = s->z.total_out;
	s->total_in = s->z.total_in;
61 62
	s->next_in = s->z.next_in;
	s->next_out = s->z.next_out;
63 64
	s->avail_in -= bytes_consumed;
	s->avail_out -= bytes_produced;
65 66 67
}

void git_inflate_init(git_zstream *strm)
68
{
69
	int status;
70

71 72 73
	zlib_pre_call(strm);
	status = inflateInit(&strm->z);
	zlib_post_call(strm);
74 75 76
	if (status == Z_OK)
		return;
	die("inflateInit: %s (%s)", zerr_to_string(status),
77
	    strm->z.msg ? strm->z.msg : "no message");
78 79
}

80
void git_inflate_init_gzip_only(git_zstream *strm)
81 82 83 84 85 86
{
	/*
	 * Use default 15 bits, +16 is to accept only gzip and to
	 * yield Z_DATA_ERROR when fed zlib format.
	 */
	const int windowBits = 15 + 16;
87
	int status;
88

89 90 91
	zlib_pre_call(strm);
	status = inflateInit2(&strm->z, windowBits);
	zlib_post_call(strm);
92 93 94
	if (status == Z_OK)
		return;
	die("inflateInit2: %s (%s)", zerr_to_string(status),
95
	    strm->z.msg ? strm->z.msg : "no message");
96 97
}

98
void git_inflate_end(git_zstream *strm)
99
{
100
	int status;
101

102 103 104
	zlib_pre_call(strm);
	status = inflateEnd(&strm->z);
	zlib_post_call(strm);
105 106 107
	if (status == Z_OK)
		return;
	error("inflateEnd: %s (%s)", zerr_to_string(status),
108
	      strm->z.msg ? strm->z.msg : "no message");
109
}
110

111
int git_inflate(git_zstream *strm, int flush)
112
{
113
	int status;
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	for (;;) {
		zlib_pre_call(strm);
		/* Never say Z_FINISH unless we are feeding everything */
		status = inflate(&strm->z,
				 (strm->z.avail_in != strm->avail_in)
				 ? 0 : flush);
		if (status == Z_MEM_ERROR)
			die("inflate: out of memory");
		zlib_post_call(strm);

		/*
		 * Let zlib work another round, while we can still
		 * make progress.
		 */
		if ((strm->avail_out && !strm->z.avail_out) &&
		    (status == Z_OK || status == Z_BUF_ERROR))
			continue;
		break;
	}

135
	switch (status) {
136 137 138 139
	/* Z_BUF_ERROR: normal, needs more space in the output buffer */
	case Z_BUF_ERROR:
	case Z_OK:
	case Z_STREAM_END:
140 141 142
		return status;
	default:
		break;
143
	}
144
	error("inflate: %s (%s)", zerr_to_string(status),
145
	      strm->z.msg ? strm->z.msg : "no message");
146
	return status;
147
}
148

149 150 151 152
#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
#define deflateBound(c,s)  ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
#endif

153
unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
154
{
155
	return deflateBound(&strm->z, size);
156 157
}

158
void git_deflate_init(git_zstream *strm, int level)
159
{
160
	int status;
161

162 163 164
	zlib_pre_call(strm);
	status = deflateInit(&strm->z, level);
	zlib_post_call(strm);
165 166 167
	if (status == Z_OK)
		return;
	die("deflateInit: %s (%s)", zerr_to_string(status),
168
	    strm->z.msg ? strm->z.msg : "no message");
169 170
}

171
static void do_git_deflate_init(git_zstream *strm, int level, int windowBits)
172
{
173 174 175 176
	int status;

	zlib_pre_call(strm);
	status = deflateInit2(&strm->z, level,
177 178
				  Z_DEFLATED, windowBits,
				  8, Z_DEFAULT_STRATEGY);
179
	zlib_post_call(strm);
180 181 182
	if (status == Z_OK)
		return;
	die("deflateInit2: %s (%s)", zerr_to_string(status),
183
	    strm->z.msg ? strm->z.msg : "no message");
184 185
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
void git_deflate_init_gzip(git_zstream *strm, int level)
{
	/*
	 * Use default 15 bits, +16 is to generate gzip header/trailer
	 * instead of the zlib wrapper.
	 */
	return do_git_deflate_init(strm, level, 15 + 16);
}

void git_deflate_init_raw(git_zstream *strm, int level)
{
	/*
	 * Use default 15 bits, negate the value to get raw compressed
	 * data without zlib header and trailer.
	 */
	return do_git_deflate_init(strm, level, -15);
}

204
int git_deflate_abort(git_zstream *strm)
205
{
206
	int status;
207

208 209 210
	zlib_pre_call(strm);
	status = deflateEnd(&strm->z);
	zlib_post_call(strm);
211 212 213 214 215 216 217
	return status;
}

void git_deflate_end(git_zstream *strm)
{
	int status = git_deflate_abort(strm);

218 219 220
	if (status == Z_OK)
		return;
	error("deflateEnd: %s (%s)", zerr_to_string(status),
221
	      strm->z.msg ? strm->z.msg : "no message");
222 223
}

224
int git_deflate_end_gently(git_zstream *strm)
225
{
226 227 228 229 230 231
	int status;

	zlib_pre_call(strm);
	status = deflateEnd(&strm->z);
	zlib_post_call(strm);
	return status;
232 233
}

234
int git_deflate(git_zstream *strm, int flush)
235
{
236
	int status;
237

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	for (;;) {
		zlib_pre_call(strm);

		/* Never say Z_FINISH unless we are feeding everything */
		status = deflate(&strm->z,
				 (strm->z.avail_in != strm->avail_in)
				 ? 0 : flush);
		if (status == Z_MEM_ERROR)
			die("deflate: out of memory");
		zlib_post_call(strm);

		/*
		 * Let zlib work another round, while we can still
		 * make progress.
		 */
		if ((strm->avail_out && !strm->z.avail_out) &&
		    (status == Z_OK || status == Z_BUF_ERROR))
			continue;
		break;
	}

259 260 261 262 263 264 265 266 267 268
	switch (status) {
	/* Z_BUF_ERROR: normal, needs more space in the output buffer */
	case Z_BUF_ERROR:
	case Z_OK:
	case Z_STREAM_END:
		return status;
	default:
		break;
	}
	error("deflate: %s (%s)", zerr_to_string(status),
269
	      strm->z.msg ? strm->z.msg : "no message");
270 271
	return status;
}