]> git.proxmox.com Git - libgit2.git/blame - src/zstream.c
git_buf: decode base85 inputs
[libgit2.git] / src / zstream.c
CommitLineData
c6f26b48
ET
1/*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8#include <zlib.h>
9
10#include "zstream.h"
11#include "buffer.h"
12
d9b04d78
RB
13#define ZSTREAM_BUFFER_SIZE (1024 * 1024)
14#define ZSTREAM_BUFFER_MIN_EXTRA 8
c6f26b48 15
d9b04d78 16static int zstream_seterr(git_zstream *zs)
c6f26b48 17{
d9b04d78
RB
18 if (zs->zerr == Z_OK || zs->zerr == Z_STREAM_END)
19 return 0;
20
21 if (zs->zerr == Z_MEM_ERROR)
c6f26b48 22 giterr_set_oom();
d9b04d78
RB
23 else if (zs->z.msg)
24 giterr_set(GITERR_ZLIB, zs->z.msg);
c6f26b48
ET
25 else
26 giterr_set(GITERR_ZLIB, "Unknown compression error");
27
28 return -1;
29}
30
b88f1713 31int git_zstream_init(git_zstream *zstream, git_zstream_t type)
c6f26b48 32{
b88f1713
ET
33 zstream->type = type;
34
35 if (zstream->type == GIT_ZSTREAM_INFLATE)
36 zstream->zerr = inflateInit(&zstream->z);
37 else
38 zstream->zerr = deflateInit(&zstream->z, Z_DEFAULT_COMPRESSION);
d9b04d78 39 return zstream_seterr(zstream);
c6f26b48
ET
40}
41
d9b04d78 42void git_zstream_free(git_zstream *zstream)
c6f26b48 43{
b88f1713
ET
44 if (zstream->type == GIT_ZSTREAM_INFLATE)
45 inflateEnd(&zstream->z);
46 else
47 deflateEnd(&zstream->z);
d9b04d78 48}
c6f26b48 49
d9b04d78
RB
50void git_zstream_reset(git_zstream *zstream)
51{
b88f1713
ET
52 if (zstream->type == GIT_ZSTREAM_INFLATE)
53 inflateReset(&zstream->z);
54 else
55 deflateReset(&zstream->z);
d9b04d78
RB
56 zstream->in = NULL;
57 zstream->in_len = 0;
58 zstream->zerr = Z_STREAM_END;
59}
c6f26b48 60
d9b04d78
RB
61int git_zstream_set_input(git_zstream *zstream, const void *in, size_t in_len)
62{
63 zstream->in = in;
64 zstream->in_len = in_len;
65 zstream->zerr = Z_OK;
66 return 0;
67}
c6f26b48 68
d9b04d78
RB
69bool git_zstream_done(git_zstream *zstream)
70{
71 return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
c6f26b48
ET
72}
73
d9b04d78 74size_t git_zstream_suggest_output_len(git_zstream *zstream)
52a8a130 75{
d9b04d78
RB
76 if (zstream->in_len > ZSTREAM_BUFFER_SIZE)
77 return ZSTREAM_BUFFER_SIZE;
78 else if (zstream->in_len > ZSTREAM_BUFFER_MIN_EXTRA)
79 return zstream->in_len;
80 else
81 return ZSTREAM_BUFFER_MIN_EXTRA;
52a8a130
ET
82}
83
d9b04d78 84int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
c6f26b48 85{
d9b04d78
RB
86 int zflush = Z_FINISH;
87 size_t out_remain = *out_len;
88
89 while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
90 size_t out_queued, in_queued, out_used, in_used;
91
92 /* set up in data */
93 zstream->z.next_in = (Bytef *)zstream->in;
94 zstream->z.avail_in = (uInt)zstream->in_len;
95 if ((size_t)zstream->z.avail_in != zstream->in_len) {
96 zstream->z.avail_in = INT_MAX;
97 zflush = Z_NO_FLUSH;
98 } else {
99 zflush = Z_FINISH;
100 }
101 in_queued = (size_t)zstream->z.avail_in;
102
103 /* set up out data */
104 zstream->z.next_out = out;
105 zstream->z.avail_out = (uInt)out_remain;
106 if ((size_t)zstream->z.avail_out != out_remain)
107 zstream->z.avail_out = INT_MAX;
108 out_queued = (size_t)zstream->z.avail_out;
109
110 /* compress next chunk */
b88f1713
ET
111 if (zstream->type == GIT_ZSTREAM_INFLATE)
112 zstream->zerr = inflate(&zstream->z, zflush);
113 else
114 zstream->zerr = deflate(&zstream->z, zflush);
8606f33b 115
d9b04d78
RB
116 if (zstream->zerr == Z_STREAM_ERROR)
117 return zstream_seterr(zstream);
118
119 out_used = (out_queued - zstream->z.avail_out);
120 out_remain -= out_used;
121 out = ((char *)out) + out_used;
122
123 in_used = (in_queued - zstream->z.avail_in);
124 zstream->in_len -= in_used;
125 zstream->in += in_used;
126 }
127
128 /* either we finished the input or we did not flush the data */
129 assert(zstream->in_len > 0 || zflush == Z_FINISH);
130
131 /* set out_size to number of bytes actually written to output */
132 *out_len = *out_len - out_remain;
133
134 return 0;
c6f26b48
ET
135}
136
b88f1713 137static int zstream_buf(git_buf *out, const void *in, size_t in_len, git_zstream_t type)
c6f26b48 138{
d9b04d78 139 git_zstream zs = GIT_ZSTREAM_INIT;
c6f26b48
ET
140 int error = 0;
141
b88f1713 142 if ((error = git_zstream_init(&zs, type)) < 0)
1cb5a811 143 return error;
c6f26b48 144
d9b04d78
RB
145 if ((error = git_zstream_set_input(&zs, in, in_len)) < 0)
146 goto done;
c6f26b48 147
d9b04d78
RB
148 while (!git_zstream_done(&zs)) {
149 size_t step = git_zstream_suggest_output_len(&zs), written;
c6f26b48 150
4aa664ae 151 if ((error = git_buf_grow_by(out, step)) < 0)
d9b04d78 152 goto done;
c6f26b48 153
d9b04d78 154 written = out->asize - out->size;
c6f26b48 155
d9b04d78
RB
156 if ((error = git_zstream_get_output(
157 out->ptr + out->size, &written, &zs)) < 0)
158 goto done;
159
160 out->size += written;
d9b04d78 161 }
c6f26b48 162
19459b1e
RB
163 /* NULL terminate for consistency if possible */
164 if (out->size < out->asize)
165 out->ptr[out->size] = '\0';
166
d9b04d78
RB
167done:
168 git_zstream_free(&zs);
c6f26b48
ET
169 return error;
170}
b88f1713
ET
171
172int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
173{
174 return zstream_buf(out, in, in_len, GIT_ZSTREAM_DEFLATE);
175}
176
177int git_zstream_inflatebuf(git_buf *out, const void *in, size_t in_len)
178{
179 return zstream_buf(out, in, in_len, GIT_ZSTREAM_INFLATE);
180}