/
zreader.c
321 lines (268 loc) · 7.89 KB
/
zreader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) 2018 Alexey Tourbin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <stdbool.h>
#include <assert.h>
#include <errno.h>
#include "reada.h"
#include "zreader.h"
// Decompresson error, as opposed to a system error.
#define ZREAD_ERR (errno = 0, -1)
static size_t read_gzip(struct zreader *z, struct fda *fda, void *buf, size_t size)
{
assert(size + 1 > 1);
size_t total = 0;
z_stream *strm = &z->u.strm;
do {
// Prefill the internal buffer.
unsigned long w;
ssize_t ret = peeka(fda, &w, sizeof w);
if (ret <= 0) {
// expected vs unexpected EOF
if (ret == 0) {
if (z->eos)
return total;
// not a system failure, reset errno
errno = 0;
}
return -1;
}
// Got some data, but the last inflate call may have reported
// Z_STREAM_END. Trying to concatenate the next gzip stream.
// This is slightly different from zlib:gzread(), which tends
// to discard "trailing grabage".
if (z->eos) {
z->eos = false;
if (inflateReset(strm) != Z_OK)
return ZREAD_ERR;
}
// The inflate call is imminent.
strm->next_in = (void *) fda->cur;
strm->avail_in = fda->end - fda->cur;
strm->next_out = buf;
strm->avail_out = size;
int zret = inflate(strm, Z_NO_FLUSH);
if (zret == Z_STREAM_END)
z->eos = true;
else if (zret != Z_OK)
// XXX zlib.h says Z_BUF_ERROR is not fatal,
// but obviously we can't expand the output buffer.
// The condition probably can't happen.
return ZREAD_ERR;
// See how many bytes have been consumed.
fda->cur = fda->end - strm->avail_in;
assert(fda->cur == (void *) strm->next_in);
// See how many bytes have been recovered.
size_t n = size - strm->avail_out;
size = strm->avail_out, buf = (char *) buf + n;
total += n;
} while (size);
return total;
}
static void fini_gzip(struct zreader *z)
{
inflateEnd(&z->u.strm);
}
static bool init_gzip(struct zreader *z)
{
z_stream *strm = &z->u.strm;
// inflateInit2 demands that these fields be initialized.
strm->next_in = NULL;
strm->avail_in = 0;
strm->zalloc = NULL;
strm->zfree = NULL;
strm->opaque = NULL;
int zret = inflateInit2(strm, 15 + 16); // 32K window + gzip only
if (zret != Z_OK)
return false;
z->read = read_gzip;
z->fini = fini_gzip;
return true;
}
static size_t read_lzma(struct zreader *z, struct fda *fda, void *buf, size_t size)
{
assert(size + 1 > 1);
size_t total = 0;
lzma_stream *lzma = &z->u.lzma;
do {
unsigned long w;
ssize_t ret = peeka(fda, &w, sizeof w);
if (ret <= 0) {
if (ret == 0) {
if (z->eos)
return total;
errno = 0;
}
return -1;
}
// Still have some data? LZMA1 streams don't have magic, but they
// do have uncompressed size / EOS marker, so EOS is reported reliably.
// LZMA1 streams are not supposed to be concatenated, trailing garbage
// not supported.
if (z->eos)
return ZREAD_ERR;
lzma->next_in = (void *) fda->cur;
lzma->avail_in = fda->end - fda->cur;
lzma->next_out = buf;
lzma->avail_out = size;
lzma_ret zret = lzma_code(lzma, LZMA_RUN);
if (zret == Z_STREAM_END)
z->eos = true;
else if (zret != LZMA_OK)
return ZREAD_ERR;
fda->cur = fda->end - lzma->avail_in;
assert(fda->cur == (void *) lzma->next_in);
size_t n = size - lzma->avail_out;
size = lzma->avail_out, buf = (char *) buf + n;
total += n;
} while (size);
return total;
}
static size_t read_xz(struct zreader *z, struct fda *fda, void *buf, size_t size)
{
assert(size + 1 > 1);
size_t total = 0;
lzma_stream *lzma = &z->u.lzma;
do {
unsigned long w;
ssize_t ret = peeka(fda, &w, sizeof w);
if (ret <= 0) {
if (ret < 0)
return -1;
if (z->eos)
return total;
// XZ frames permit zero padding and may somehow be combined
// with other kinds of frames, but when LZMA_CONCATENATED is
// enabled, trailing garbage is explicitly porhibited. Thus
// underlying EOF must be signalled by LZMA_FINISH.
lzma->next_in = NULL;
lzma->avail_in = 0;
lzma->next_out = buf;
lzma->avail_out = size;
lzma_ret zret = lzma_code(lzma, LZMA_FINISH);
if (zret == Z_STREAM_END)
z->eos = true;
else
return ZREAD_ERR;
// Still may produce some data at this stage.
size_t n = size - lzma->avail_out;
return total += n;
}
// This is hardly possible, but what if they call again after
// eof+eos and there is more data on the file descriptor?
if (z->eos)
return ZREAD_ERR;
lzma->next_in = (void *) fda->cur;
lzma->avail_in = fda->end - fda->cur;
lzma->next_out = buf;
lzma->avail_out = size;
lzma_ret zret = lzma_code(lzma, LZMA_RUN);
if (zret != LZMA_OK)
return ZREAD_ERR;
fda->cur = fda->end - lzma->avail_in;
assert(fda->cur == (void *) lzma->next_in);
size_t n = size - lzma->avail_out;
size = lzma->avail_out, buf = (char *) buf + n;
total += n;
} while (size);
return total;
}
static void fini_lzma(struct zreader *z)
{
lzma_end(&z->u.lzma);
}
static bool init_lzma(struct zreader *z)
{
lzma_stream *lzma = &z->u.lzma;
*lzma = (lzma_stream) LZMA_STREAM_INIT;
// 100M is rpm's default limit, we follow suit.
lzma_ret zret = lzma_alone_decoder(lzma, 100<<20);
if (zret != LZMA_OK)
return false;
z->read = read_lzma;
z->fini = fini_lzma;
return true;
}
static bool init_xz(struct zreader *z)
{
lzma_stream *lzma = &z->u.lzma;
*lzma = (lzma_stream) LZMA_STREAM_INIT;
lzma_ret zret = lzma_stream_decoder(lzma, 100<<20, LZMA_CONCATENATED);
if (zret != LZMA_OK)
return false;
z->read = read_xz;
z->fini = fini_lzma;
return true;
}
bool zreader_init(struct zreader *z, const char *zprog)
{
z->eos = false;
switch (*zprog) {
case 'g':
if (strcmp(zprog, "gzip") == 0)
return init_gzip(z);
break;
case 'l':
if (strcmp(zprog, "lzma") == 0)
return init_lzma(z);
break;
case 'x':
if (strcmp(zprog, "xz") == 0)
return init_xz(z);
break;
}
errno = 0;
return false;
}
#ifdef ZREADER_MAIN
#include <stdio.h>
#include <unistd.h>
#define PROG "zreader"
#define warn(fmt, args...) fprintf(stderr, PROG ": " fmt "\n", ##args)
#define die(fmt, args...) return warn(fmt, ##args), 2
int main(int argc, char **argv)
{
if (argc != 2) {
usage: fprintf(stderr, "Usage: " PROG " COMPRESSION-METHOD < COMPRESSED-INPUT\n");
return 2;
}
if (isatty(0)) {
warn("refusing to read binary data from a terminal");
goto usage;
}
char fdabuf[NREADA];
struct fda fda = { 0, fdabuf };
struct zreader z;
if (!zreader_init(&z, argv[1]))
die("cannot initialize %s decoder", argv[1]);
char buf[BUFSIZ];
size_t size;
while ((size = zreader_read(&z, &fda, buf, sizeof buf)) + 1 > 1)
if (fwrite_unlocked(buf, 1, size, stdout) != size)
die("fwrite: %m");
if (size + 1 == 0) {
if (errno)
die("read: %m");
else
die("%s decompression failed", argv[1]);
}
return 0;
}
#endif
// ex:set ts=8 sts=4 sw=4 noet: