Line Flow Count Block(s) Source
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 "zstream.h"
9 -
10 - #include <zlib.h>
11 -
12 - #include "buffer.h"
13 -
14 - #define ZSTREAM_BUFFER_SIZE (1024 * 1024)
15 - #define ZSTREAM_BUFFER_MIN_EXTRA 8
16 -
17 240770 2 GIT_INLINE(int) zstream_seterr(git_zstream *zs)
18 - {
19 240770 2 switch (zs->zerr) {
20 - case Z_OK:
21 - case Z_STREAM_END:
22 - case Z_BUF_ERROR: /* not fatal; we retry with a larger buffer */
23 240585 3 return 0;
24 - case Z_MEM_ERROR:
25 ##### 4 git_error_set_oom();
26 ##### 5 break;
27 - default:
28 185 6 if (zs->z.msg)
29 185 7 git_error_set_str(GIT_ERROR_ZLIB, zs->z.msg);
30 - else
31 ##### 8 git_error_set(GIT_ERROR_ZLIB, "unknown compression error");
32 - }
33 -
34 185 9 return -1;
35 - }
36 -
37 98689 2 int git_zstream_init(git_zstream *zstream, git_zstream_t type)
38 - {
39 98689 2 zstream->type = type;
40 -
41 98689 2 if (zstream->type == GIT_ZSTREAM_INFLATE)
42 98906 3,4 zstream->zerr = inflateInit(&zstream->z);
43 - else
44 297 5,6 zstream->zerr = deflateInit(&zstream->z, Z_DEFAULT_COMPRESSION);
45 99203 7 return zstream_seterr(zstream);
46 - }
47 -
48 98925 2 void git_zstream_free(git_zstream *zstream)
49 - {
50 98925 2 if (zstream->type == GIT_ZSTREAM_INFLATE)
51 98628 3 inflateEnd(&zstream->z);
52 - else
53 297 4 deflateEnd(&zstream->z);
54 99457 5 }
55 -
56 2098 2 void git_zstream_reset(git_zstream *zstream)
57 - {
58 2098 2 if (zstream->type == GIT_ZSTREAM_INFLATE)
59 ##### 3 inflateReset(&zstream->z);
60 - else
61 2098 4 deflateReset(&zstream->z);
62 2098 5 zstream->in = NULL;
63 2098 5 zstream->in_len = 0;
64 2098 5 zstream->zerr = Z_STREAM_END;
65 2098 5 }
66 -
67 101010 2 int git_zstream_set_input(git_zstream *zstream, const void *in, size_t in_len)
68 - {
69 101010 2 zstream->in = in;
70 101010 2 zstream->in_len = in_len;
71 101010 2 zstream->zerr = Z_OK;
72 101010 2 return 0;
73 - }
74 -
75 72042 2 bool git_zstream_done(git_zstream *zstream)
76 - {
77 72042 2 return (!zstream->in_len && zstream->zerr == Z_STREAM_END);
78 - }
79 -
80 40515 2 bool git_zstream_eos(git_zstream *zstream)
81 - {
82 40515 2 return zstream->zerr == Z_STREAM_END;
83 - }
84 -
85 445 2 size_t git_zstream_suggest_output_len(git_zstream *zstream)
86 - {
87 445 2 if (zstream->in_len > ZSTREAM_BUFFER_SIZE)
88 8 3 return ZSTREAM_BUFFER_SIZE;
89 437 4 else if (zstream->in_len > ZSTREAM_BUFFER_MIN_EXTRA)
90 230 5 return zstream->in_len;
91 - else
92 207 6 return ZSTREAM_BUFFER_MIN_EXTRA;
93 - }
94 -
95 141989 2 int git_zstream_get_output_chunk(
96 - void *out, size_t *out_len, git_zstream *zstream)
97 - {
98 - size_t in_queued, in_used, out_queued;
99 -
100 - /* set up input data */
101 141989 2 zstream->z.next_in = (Bytef *)zstream->in;
102 -
103 - /* feed as much data to zlib as it can consume, at most UINT_MAX */
104 141989 2 if (zstream->in_len > UINT_MAX) {
105 174 3 zstream->z.avail_in = UINT_MAX;
106 174 3 zstream->flush = Z_NO_FLUSH;
107 - } else {
108 141815 4 zstream->z.avail_in = (uInt)zstream->in_len;
109 141815 4 zstream->flush = Z_FINISH;
110 - }
111 141989 5 in_queued = (size_t)zstream->z.avail_in;
112 -
113 - /* set up output data */
114 141989 5 zstream->z.next_out = out;
115 141989 5 zstream->z.avail_out = (uInt)*out_len;
116 -
117 141989 5 if ((size_t)zstream->z.avail_out != *out_len)
118 ##### 6 zstream->z.avail_out = UINT_MAX;
119 141989 7 out_queued = (size_t)zstream->z.avail_out;
120 -
121 - /* compress next chunk */
122 141989 7 if (zstream->type == GIT_ZSTREAM_INFLATE)
123 120960 8,9 zstream->zerr = inflate(&zstream->z, zstream->flush);
124 - else
125 21301 10,11 zstream->zerr = deflate(&zstream->z, zstream->flush);
126 -
127 142261 12,13 if (zstream_seterr(zstream))
128 185 14 return -1;
129 -
130 142066 15 in_used = (in_queued - zstream->z.avail_in);
131 142066 15 zstream->in_len -= in_used;
132 142066 15 zstream->in += in_used;
133 -
134 142066 15 *out_len = (out_queued - zstream->z.avail_out);
135 -
136 142066 15 return 0;
137 - }
138 -
139 120763 2 int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
140 - {
141 120763 2 size_t out_remain = *out_len;
142 -
143 120763 2,3 if (zstream->in_len && zstream->zerr == Z_STREAM_END) {
144 1 4 git_error_set(GIT_ERROR_ZLIB, "zlib input had trailing garbage");
145 1 5 return -1;
146 - }
147 -
148 212331 6,11,12 while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
149 91453 7 size_t out_written = out_remain;
150 -
151 91754 7,8 if (git_zstream_get_output_chunk(out, &out_written, zstream) < 0)
152 185 9 return -1;
153 -
154 91569 10 out_remain -= out_written;
155 91569 10 out = ((char *)out) + out_written;
156 - }
157 -
158 - /* either we finished the input or we did not flush the data */
159 120878 13-15 assert(zstream->in_len > 0 || zstream->flush == Z_FINISH);
160 -
161 - /* set out_size to number of bytes actually written to output */
162 120878 16 *out_len = *out_len - out_remain;
163 -
164 120878 16 return 0;
165 - }
166 -
167 246 2 static int zstream_buf(git_buf *out, const void *in, size_t in_len, git_zstream_t type)
168 - {
169 246 2 git_zstream zs = GIT_ZSTREAM_INIT;
170 246 2 int error = 0;
171 -
172 246 2,3 if ((error = git_zstream_init(&zs, type)) < 0)
173 ##### 4 return error;
174 -
175 246 5,6 if ((error = git_zstream_set_input(&zs, in, in_len)) < 0)
176 ##### 7 goto done;
177 -
178 690 8,18,19 while (!git_zstream_done(&zs)) {
179 445 9 size_t step = git_zstream_suggest_output_len(&zs), written;
180 -
181 445 10,11 if ((error = git_buf_grow_by(out, step)) < 0)
182 1 12,17 goto done;
183 -
184 445 13 written = out->asize - out->size;
185 -
186 445 13,14 if ((error = git_zstream_get_output(
187 445 13 out->ptr + out->size, &written, &zs)) < 0)
188 1 15 goto done;
189 -
190 444 16 out->size += written;
191 - }
192 -
193 - /* NULL terminate for consistency if possible */
194 245 20 if (out->size < out->asize)
195 198 21 out->ptr[out->size] = '\0';
196 -
197 - done:
198 246 22 git_zstream_free(&zs);
199 246 23 return error;
200 - }
201 -
202 210 2 int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len)
203 - {
204 210 2 return zstream_buf(out, in, in_len, GIT_ZSTREAM_DEFLATE);
205 - }
206 -
207 36 2 int git_zstream_inflatebuf(git_buf *out, const void *in, size_t in_len)
208 - {
209 36 2 return zstream_buf(out, in, in_len, GIT_ZSTREAM_INFLATE);
210 - }