zstream.c 4.83 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "zstream.h"

10 11
#include <zlib.h>

12
#include "str.h"
13

14 15
#define ZSTREAM_BUFFER_SIZE (1024 * 1024)
#define ZSTREAM_BUFFER_MIN_EXTRA 8
16

17
GIT_INLINE(int) zstream_seterr(git_zstream *zs)
18
{
19 20 21 22
	switch (zs->zerr) {
	case Z_OK:
	case Z_STREAM_END:
	case Z_BUF_ERROR: /* not fatal; we retry with a larger buffer */
23
		return 0;
24
	case Z_MEM_ERROR:
25
		git_error_set_oom();
26 27 28
		break;
	default:
		if (zs->z.msg)
29
			git_error_set_str(GIT_ERROR_ZLIB, zs->z.msg);
30
		else
31
			git_error_set(GIT_ERROR_ZLIB, "unknown compression error");
32
	}
33 34 35 36

	return -1;
}

37
int git_zstream_init(git_zstream *zstream, git_zstream_t type)
38
{
39 40 41 42 43 44
	zstream->type = type;

	if (zstream->type == GIT_ZSTREAM_INFLATE)
		zstream->zerr = inflateInit(&zstream->z);
	else
		zstream->zerr = deflateInit(&zstream->z, Z_DEFAULT_COMPRESSION);
45
	return zstream_seterr(zstream);
46 47
}

48
void git_zstream_free(git_zstream *zstream)
49
{
50 51 52 53
	if (zstream->type == GIT_ZSTREAM_INFLATE)
		inflateEnd(&zstream->z);
	else
		deflateEnd(&zstream->z);
54
}
55

56 57
void git_zstream_reset(git_zstream *zstream)
{
58 59 60 61
	if (zstream->type == GIT_ZSTREAM_INFLATE)
		inflateReset(&zstream->z);
	else
		deflateReset(&zstream->z);
62 63 64 65
	zstream->in = NULL;
	zstream->in_len = 0;
	zstream->zerr = Z_STREAM_END;
}
66

67 68 69 70 71 72 73
int git_zstream_set_input(git_zstream *zstream, const void *in, size_t in_len)
{
	zstream->in = in;
	zstream->in_len = in_len;
	zstream->zerr = Z_OK;
	return 0;
}
74

75 76 77
bool git_zstream_done(git_zstream *zstream)
{
	return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
78 79
}

80 81 82 83 84
bool git_zstream_eos(git_zstream *zstream)
{
	return zstream->zerr == Z_STREAM_END;
}

85
size_t git_zstream_suggest_output_len(git_zstream *zstream)
86
{
87 88 89 90 91 92
	if (zstream->in_len > ZSTREAM_BUFFER_SIZE)
		return ZSTREAM_BUFFER_SIZE;
	else if (zstream->in_len > ZSTREAM_BUFFER_MIN_EXTRA)
		return zstream->in_len;
	else
		return ZSTREAM_BUFFER_MIN_EXTRA;
93 94
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
int git_zstream_get_output_chunk(
	void *out, size_t *out_len, git_zstream *zstream)
{
	size_t in_queued, in_used, out_queued;

	/* set up input data */
	zstream->z.next_in = (Bytef *)zstream->in;

	/* feed as much data to zlib as it can consume, at most UINT_MAX */
	if (zstream->in_len > UINT_MAX) {
		zstream->z.avail_in = UINT_MAX;
		zstream->flush = Z_NO_FLUSH;
	} else {
		zstream->z.avail_in = (uInt)zstream->in_len;
		zstream->flush = Z_FINISH;
	}
	in_queued = (size_t)zstream->z.avail_in;

	/* set up output data */
	zstream->z.next_out = out;
	zstream->z.avail_out = (uInt)*out_len;

	if ((size_t)zstream->z.avail_out != *out_len)
		zstream->z.avail_out = UINT_MAX;
	out_queued = (size_t)zstream->z.avail_out;

	/* compress next chunk */
	if (zstream->type == GIT_ZSTREAM_INFLATE)
		zstream->zerr = inflate(&zstream->z, zstream->flush);
	else
		zstream->zerr = deflate(&zstream->z, zstream->flush);

	if (zstream_seterr(zstream))
		return -1;

	in_used = (in_queued - zstream->z.avail_in);
	zstream->in_len -= in_used;
	zstream->in += in_used;

	*out_len = (out_queued - zstream->z.avail_out);

	return 0;
}

139
int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
140
{
141 142
	size_t out_remain = *out_len;

143
	if (zstream->in_len && zstream->zerr == Z_STREAM_END) {
144
		git_error_set(GIT_ERROR_ZLIB, "zlib input had trailing garbage");
145 146 147
		return -1;
	}

148
	while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
149
		size_t out_written = out_remain;
150

151
		if (git_zstream_get_output_chunk(out, &out_written, zstream) < 0)
152
			return -1;
153

154 155
		out_remain -= out_written;
		out = ((char *)out) + out_written;
156 157 158
	}

	/* either we finished the input or we did not flush the data */
159
	GIT_ASSERT(zstream->in_len > 0 || zstream->flush == Z_FINISH);
160 161 162 163 164

	/* set out_size to number of bytes actually written to output */
	*out_len = *out_len - out_remain;

	return 0;
165 166
}

167
static int zstream_buf(git_str *out, const void *in, size_t in_len, git_zstream_t type)
168
{
169
	git_zstream zs = GIT_ZSTREAM_INIT;
170 171
	int error = 0;

172
	if ((error = git_zstream_init(&zs, type)) < 0)
XTao committed
173
		return error;
174

175 176
	if ((error = git_zstream_set_input(&zs, in, in_len)) < 0)
		goto done;
177

178 179
	while (!git_zstream_done(&zs)) {
		size_t step = git_zstream_suggest_output_len(&zs), written;
180

181
		if ((error = git_str_grow_by(out, step)) < 0)
182
			goto done;
183

184
		written = out->asize - out->size;
185

186 187 188 189 190 191
		if ((error = git_zstream_get_output(
				out->ptr + out->size, &written, &zs)) < 0)
			goto done;

		out->size += written;
	}
192

193 194 195 196
	/* NULL terminate for consistency if possible */
	if (out->size < out->asize)
		out->ptr[out->size] = '\0';

197 198
done:
	git_zstream_free(&zs);
199 200
	return error;
}
201

202
int git_zstream_deflatebuf(git_str *out, const void *in, size_t in_len)
203 204 205 206
{
	return zstream_buf(out, in, in_len, GIT_ZSTREAM_DEFLATE);
}

207
int git_zstream_inflatebuf(git_str *out, const void *in, size_t in_len)
208 209 210
{
	return zstream_buf(out, in, in_len, GIT_ZSTREAM_INFLATE);
}