]>
Commit | Line | Data |
---|---|---|
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 | 16 | static 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 | 31 | int 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 | 42 | void 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 |
50 | void 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 |
61 | int 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 |
69 | bool git_zstream_done(git_zstream *zstream) |
70 | { | |
71 | return (!zstream->in_len && zstream->zerr == Z_STREAM_END); | |
c6f26b48 ET |
72 | } |
73 | ||
d9b04d78 | 74 | size_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 | 84 | int 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 | ||
b8dc2fdb ET |
89 | if (zstream->in_len && zstream->zerr == Z_STREAM_END) { |
90 | giterr_set(GITERR_ZLIB, "zlib input had trailing garbage"); | |
91 | return -1; | |
92 | } | |
93 | ||
d9b04d78 RB |
94 | while (out_remain > 0 && zstream->zerr != Z_STREAM_END) { |
95 | size_t out_queued, in_queued, out_used, in_used; | |
96 | ||
97 | /* set up in data */ | |
98 | zstream->z.next_in = (Bytef *)zstream->in; | |
99 | zstream->z.avail_in = (uInt)zstream->in_len; | |
100 | if ((size_t)zstream->z.avail_in != zstream->in_len) { | |
101 | zstream->z.avail_in = INT_MAX; | |
102 | zflush = Z_NO_FLUSH; | |
103 | } else { | |
104 | zflush = Z_FINISH; | |
105 | } | |
106 | in_queued = (size_t)zstream->z.avail_in; | |
107 | ||
108 | /* set up out data */ | |
109 | zstream->z.next_out = out; | |
110 | zstream->z.avail_out = (uInt)out_remain; | |
111 | if ((size_t)zstream->z.avail_out != out_remain) | |
112 | zstream->z.avail_out = INT_MAX; | |
113 | out_queued = (size_t)zstream->z.avail_out; | |
114 | ||
115 | /* compress next chunk */ | |
b88f1713 ET |
116 | if (zstream->type == GIT_ZSTREAM_INFLATE) |
117 | zstream->zerr = inflate(&zstream->z, zflush); | |
118 | else | |
119 | zstream->zerr = deflate(&zstream->z, zflush); | |
8606f33b | 120 | |
d9b04d78 RB |
121 | if (zstream->zerr == Z_STREAM_ERROR) |
122 | return zstream_seterr(zstream); | |
123 | ||
124 | out_used = (out_queued - zstream->z.avail_out); | |
125 | out_remain -= out_used; | |
126 | out = ((char *)out) + out_used; | |
127 | ||
128 | in_used = (in_queued - zstream->z.avail_in); | |
129 | zstream->in_len -= in_used; | |
130 | zstream->in += in_used; | |
131 | } | |
132 | ||
133 | /* either we finished the input or we did not flush the data */ | |
134 | assert(zstream->in_len > 0 || zflush == Z_FINISH); | |
135 | ||
136 | /* set out_size to number of bytes actually written to output */ | |
137 | *out_len = *out_len - out_remain; | |
138 | ||
139 | return 0; | |
c6f26b48 ET |
140 | } |
141 | ||
b88f1713 | 142 | static int zstream_buf(git_buf *out, const void *in, size_t in_len, git_zstream_t type) |
c6f26b48 | 143 | { |
d9b04d78 | 144 | git_zstream zs = GIT_ZSTREAM_INIT; |
c6f26b48 ET |
145 | int error = 0; |
146 | ||
b88f1713 | 147 | if ((error = git_zstream_init(&zs, type)) < 0) |
1cb5a811 | 148 | return error; |
c6f26b48 | 149 | |
d9b04d78 RB |
150 | if ((error = git_zstream_set_input(&zs, in, in_len)) < 0) |
151 | goto done; | |
c6f26b48 | 152 | |
d9b04d78 RB |
153 | while (!git_zstream_done(&zs)) { |
154 | size_t step = git_zstream_suggest_output_len(&zs), written; | |
c6f26b48 | 155 | |
4aa664ae | 156 | if ((error = git_buf_grow_by(out, step)) < 0) |
d9b04d78 | 157 | goto done; |
c6f26b48 | 158 | |
d9b04d78 | 159 | written = out->asize - out->size; |
c6f26b48 | 160 | |
d9b04d78 RB |
161 | if ((error = git_zstream_get_output( |
162 | out->ptr + out->size, &written, &zs)) < 0) | |
163 | goto done; | |
164 | ||
165 | out->size += written; | |
d9b04d78 | 166 | } |
c6f26b48 | 167 | |
19459b1e RB |
168 | /* NULL terminate for consistency if possible */ |
169 | if (out->size < out->asize) | |
170 | out->ptr[out->size] = '\0'; | |
171 | ||
d9b04d78 RB |
172 | done: |
173 | git_zstream_free(&zs); | |
c6f26b48 ET |
174 | return error; |
175 | } | |
b88f1713 ET |
176 | |
177 | int git_zstream_deflatebuf(git_buf *out, const void *in, size_t in_len) | |
178 | { | |
179 | return zstream_buf(out, in, in_len, GIT_ZSTREAM_DEFLATE); | |
180 | } | |
181 | ||
182 | int git_zstream_inflatebuf(git_buf *out, const void *in, size_t in_len) | |
183 | { | |
184 | return zstream_buf(out, in, in_len, GIT_ZSTREAM_INFLATE); | |
185 | } |