zstream.c 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * 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.
 */

#include <zlib.h>

#include "zstream.h"
#include "buffer.h"

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

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

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

	return -1;
}

int git_zstream_init(git_zstream *zstream)
{
33 34
	zstream->zerr = deflateInit(&zstream->z, Z_DEFAULT_COMPRESSION);
	return zstream_seterr(zstream);
35 36
}

37
void git_zstream_free(git_zstream *zstream)
38
{
39 40
	deflateEnd(&zstream->z);
}
41

42 43 44 45 46 47 48
void git_zstream_reset(git_zstream *zstream)
{
	deflateReset(&zstream->z);
	zstream->in = NULL;
	zstream->in_len = 0;
	zstream->zerr = Z_STREAM_END;
}
49

50 51 52 53 54 55 56
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;
}
57

58 59 60
bool git_zstream_done(git_zstream *zstream)
{
	return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
61 62
}

63
size_t git_zstream_suggest_output_len(git_zstream *zstream)
64
{
65 66 67 68 69 70
	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;
71 72
}

73
int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
74
{
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	int zflush = Z_FINISH;
	size_t out_remain = *out_len;

	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 */
		zstream->zerr = deflate(&zstream->z, zflush);
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
		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;
121 122 123 124
}

int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
{
125
	git_zstream zs = GIT_ZSTREAM_INIT;
126 127
	int error = 0;

128
	if ((error = git_zstream_init(&zs)) < 0)
XTao committed
129
		return error;
130

131 132
	if ((error = git_zstream_set_input(&zs, in, in_len)) < 0)
		goto done;
133

134 135
	while (!git_zstream_done(&zs)) {
		size_t step = git_zstream_suggest_output_len(&zs), written;
136

137
		if ((error = git_buf_grow_by(out, step)) < 0)
138
			goto done;
139

140
		written = out->asize - out->size;
141

142 143 144 145 146 147
		if ((error = git_zstream_get_output(
				out->ptr + out->size, &written, &zs)) < 0)
			goto done;

		out->size += written;
	}
148

149 150 151 152
	/* NULL terminate for consistency if possible */
	if (out->size < out->asize)
		out->ptr[out->size] = '\0';

153 154
done:
	git_zstream_free(&zs);
155 156
	return error;
}