zstream.c 4.41 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 12 13
#include <zlib.h>

#include "buffer.h"

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

17
static int zstream_seterr(git_zstream *zs)
18
{
19 20 21 22
	if (zs->zerr == Z_OK || zs->zerr == Z_STREAM_END)
		return 0;

	if (zs->zerr == Z_MEM_ERROR)
23
		giterr_set_oom();
24
	else if (zs->z.msg)
25
		giterr_set_str(GITERR_ZLIB, zs->z.msg);
26
	else
27
		giterr_set(GITERR_ZLIB, "unknown compression error");
28 29 30 31

	return -1;
}

32
int git_zstream_init(git_zstream *zstream, git_zstream_t type)
33
{
34 35 36 37 38 39
	zstream->type = type;

	if (zstream->type == GIT_ZSTREAM_INFLATE)
		zstream->zerr = inflateInit(&zstream->z);
	else
		zstream->zerr = deflateInit(&zstream->z, Z_DEFAULT_COMPRESSION);
40
	return zstream_seterr(zstream);
41 42
}

43
void git_zstream_free(git_zstream *zstream)
44
{
45 46 47 48
	if (zstream->type == GIT_ZSTREAM_INFLATE)
		inflateEnd(&zstream->z);
	else
		deflateEnd(&zstream->z);
49
}
50

51 52
void git_zstream_reset(git_zstream *zstream)
{
53 54 55 56
	if (zstream->type == GIT_ZSTREAM_INFLATE)
		inflateReset(&zstream->z);
	else
		deflateReset(&zstream->z);
57 58 59 60
	zstream->in = NULL;
	zstream->in_len = 0;
	zstream->zerr = Z_STREAM_END;
}
61

62 63 64 65 66 67 68
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;
}
69

70 71 72
bool git_zstream_done(git_zstream *zstream)
{
	return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
73 74
}

75
size_t git_zstream_suggest_output_len(git_zstream *zstream)
76
{
77 78 79 80 81 82
	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;
83 84
}

85
int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
86
{
87 88 89
	int zflush = Z_FINISH;
	size_t out_remain = *out_len;

90 91 92 93 94
	if (zstream->in_len && zstream->zerr == Z_STREAM_END) {
		giterr_set(GITERR_ZLIB, "zlib input had trailing garbage");
		return -1;
	}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
		size_t out_queued, in_queued, out_used, in_used;

		/* set up in data */
		zstream->z.next_in  = (Bytef *)zstream->in;
		zstream->z.avail_in = (uInt)zstream->in_len;
		if ((size_t)zstream->z.avail_in != zstream->in_len) {
			zstream->z.avail_in = INT_MAX;
			zflush = Z_NO_FLUSH;
		} else {
			zflush = Z_FINISH;
		}
		in_queued = (size_t)zstream->z.avail_in;

		/* set up out data */
		zstream->z.next_out = out;
		zstream->z.avail_out = (uInt)out_remain;
		if ((size_t)zstream->z.avail_out != out_remain)
			zstream->z.avail_out = INT_MAX;
		out_queued = (size_t)zstream->z.avail_out;

		/* compress next chunk */
117 118 119 120
		if (zstream->type == GIT_ZSTREAM_INFLATE)
			zstream->zerr = inflate(&zstream->z, zflush);
		else
			zstream->zerr = deflate(&zstream->z, zflush);
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		if (zstream->zerr == Z_STREAM_ERROR)
			return zstream_seterr(zstream);

		out_used = (out_queued - zstream->z.avail_out);
		out_remain -= out_used;
		out = ((char *)out) + out_used;

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

	/* either we finished the input or we did not flush the data */
	assert(zstream->in_len > 0 || zflush == Z_FINISH);

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

	return 0;
141 142
}

143
static int zstream_buf(git_buf *out, const void *in, size_t in_len, git_zstream_t type)
144
{
145
	git_zstream zs = GIT_ZSTREAM_INIT;
146 147
	int error = 0;

148
	if ((error = git_zstream_init(&zs, type)) < 0)
XTao committed
149
		return error;
150

151 152
	if ((error = git_zstream_set_input(&zs, in, in_len)) < 0)
		goto done;
153

154 155
	while (!git_zstream_done(&zs)) {
		size_t step = git_zstream_suggest_output_len(&zs), written;
156

157
		if ((error = git_buf_grow_by(out, step)) < 0)
158
			goto done;
159

160
		written = out->asize - out->size;
161

162 163 164 165 166 167
		if ((error = git_zstream_get_output(
				out->ptr + out->size, &written, &zs)) < 0)
			goto done;

		out->size += written;
	}
168

169 170 171 172
	/* NULL terminate for consistency if possible */
	if (out->size < out->asize)
		out->ptr[out->size] = '\0';

173 174
done:
	git_zstream_free(&zs);
175 176
	return error;
}
177 178 179 180 181 182 183 184 185 186

int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
{
	return zstream_buf(out, in, in_len, GIT_ZSTREAM_DEFLATE);
}

int git_zstream_inflatebuf(git_buf *out, const void *in, size_t in_len)
{
	return zstream_buf(out, in, in_len, GIT_ZSTREAM_INFLATE);
}