-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathmzcat.c
416 lines (363 loc) · 12.9 KB
/
mzcat.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2022 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// ----------------
/*
mzcat decompresses stdin to stdout. It is similar to the standard /bin/bzcat,
/bin/lzcat or /bin/zcat programs but the single program speaks multiple file
formats (listed below). On Linux, it also self-imposes a SECCOMP_MODE_STRICT
sandbox. To run:
$CC mzcat.c && ./a.out < ../../test/data/romeo.txt.bz2; rm -f a.out
for a C compiler $CC, such as clang or gcc.
Supported compression formats:
- bzip2
- gzip
- lzip
- lzma
- xz
- zlib
*/
#include <errno.h>
#include <unistd.h>
// Wuffs ships as a "single file C library" or "header file library" as per
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
//
// To use that single file as a "foo.c"-like implementation, instead of a
// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
// compiling it.
#define WUFFS_IMPLEMENTATION
// Defining the WUFFS_CONFIG__STATIC_FUNCTIONS macro is optional, but when
// combined with WUFFS_IMPLEMENTATION, it demonstrates making all of Wuffs'
// functions have static storage.
//
// This can help the compiler ignore or discard unused code, which can produce
// faster compiles and smaller binaries. Other motivations are discussed in the
// "ALLOW STATIC IMPLEMENTATION" section of
// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt
#define WUFFS_CONFIG__STATIC_FUNCTIONS
// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
// release/c/etc.c choose which parts of Wuffs to build. That file contains the
// entire Wuffs standard library, implementing a variety of codecs and file
// formats. Without this macro definition, an optimizing compiler or linker may
// very well discard Wuffs code for unused codecs, but listing the Wuffs
// modules we use makes that process explicit. Preprocessing means that such
// code simply isn't compiled.
#define WUFFS_CONFIG__MODULES
#define WUFFS_CONFIG__MODULE__ADLER32
#define WUFFS_CONFIG__MODULE__BASE
#define WUFFS_CONFIG__MODULE__BZIP2
#define WUFFS_CONFIG__MODULE__CRC32
#define WUFFS_CONFIG__MODULE__CRC64
#define WUFFS_CONFIG__MODULE__DEFLATE
#define WUFFS_CONFIG__MODULE__GZIP
#define WUFFS_CONFIG__MODULE__LZIP
#define WUFFS_CONFIG__MODULE__LZMA
#define WUFFS_CONFIG__MODULE__SHA256
#define WUFFS_CONFIG__MODULE__XZ
#define WUFFS_CONFIG__MODULE__ZLIB
// If building this program in an environment that doesn't easily accommodate
// relative includes, you can use the script/inline-c-relative-includes.go
// program to generate a stand-alone C file.
#include "../../release/c/wuffs-unsupported-snapshot.c"
#if defined(__linux__)
#include <linux/seccomp.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#define WUFFS_EXAMPLE_USE_SECCOMP
#endif
#ifndef DST_BUFFER_ARRAY_SIZE
#define DST_BUFFER_ARRAY_SIZE (64 * 1024 * 1024)
#endif
#ifndef SRC_BUFFER_ARRAY_SIZE
#define SRC_BUFFER_ARRAY_SIZE (64 * 1024 * 1024)
#endif
// 80 MiB is big enough to decode files encoded with "xz -9".
#ifndef WORKBUF_ARRAY_SIZE
#define WORKBUF_ARRAY_SIZE (80 * 1024 * 1024)
#endif
uint8_t g_dst_buffer_array[DST_BUFFER_ARRAY_SIZE];
uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE];
uint8_t g_workbuf_array[WORKBUF_ARRAY_SIZE];
// ----
static bool g_sandboxed = false;
int32_t g_fourcc = 0;
wuffs_base__io_transformer* g_io_transformer = NULL;
union {
wuffs_bzip2__decoder bzip2;
wuffs_gzip__decoder gzip;
wuffs_lzip__decoder lzip;
wuffs_lzma__decoder lzma;
wuffs_xz__decoder xz;
wuffs_zlib__decoder zlib;
} g_potential_decoders;
wuffs_crc32__ieee_hasher g_digest_hasher;
// ----
struct {
int remaining_argc;
char** remaining_argv;
bool fail_if_unsandboxed;
bool ignore_checksum;
bool output_crc32_digest;
} g_flags = {0};
const char* //
parse_flags(int argc, char** argv) {
int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
for (; c < argc; c++) {
char* arg = argv[c];
if (*arg++ != '-') {
break;
}
// A double-dash "--foo" is equivalent to a single-dash "-foo". As special
// cases, a bare "-" is not a flag (some programs may interpret it as
// stdin) and a bare "--" means to stop parsing flags.
if (*arg == '\x00') {
break;
} else if (*arg == '-') {
arg++;
if (*arg == '\x00') {
c++;
break;
}
}
if (!strcmp(arg, "fail-if-unsandboxed")) {
g_flags.fail_if_unsandboxed = true;
continue;
} else if (!strcmp(arg, "ignore-checksum")) {
g_flags.ignore_checksum = true;
continue;
} else if (!strcmp(arg, "output-crc32-digest")) {
g_flags.output_crc32_digest = true;
continue;
}
return "main: unrecognized flag argument";
}
g_flags.remaining_argc = argc - c;
g_flags.remaining_argv = argv + c;
return NULL;
}
// ----
// ignore_return_value suppresses errors from -Wall -Werror.
static void //
ignore_return_value(int ignored) {}
const char* //
initialize_io_transformer(uint8_t input_first_byte) {
wuffs_base__status status =
wuffs_base__make_status("main: unrecognized input compression format");
wuffs_base__io_transformer* io_transformer = NULL;
switch (input_first_byte) {
case 0x1F:
status = wuffs_gzip__decoder__initialize(
&g_potential_decoders.gzip, sizeof g_potential_decoders.gzip,
WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS);
io_transformer =
wuffs_gzip__decoder__upcast_as__wuffs_base__io_transformer(
&g_potential_decoders.gzip);
break;
case 0x42:
status = wuffs_bzip2__decoder__initialize(
&g_potential_decoders.bzip2, sizeof g_potential_decoders.bzip2,
WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS);
io_transformer =
wuffs_bzip2__decoder__upcast_as__wuffs_base__io_transformer(
&g_potential_decoders.bzip2);
break;
case 0x4C:
status = wuffs_lzip__decoder__initialize(
&g_potential_decoders.lzip, sizeof g_potential_decoders.lzip,
WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS);
io_transformer =
wuffs_lzip__decoder__upcast_as__wuffs_base__io_transformer(
&g_potential_decoders.lzip);
break;
case 0x5D:
status = wuffs_lzma__decoder__initialize(
&g_potential_decoders.lzma, sizeof g_potential_decoders.lzma,
WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS);
io_transformer =
wuffs_lzma__decoder__upcast_as__wuffs_base__io_transformer(
&g_potential_decoders.lzma);
break;
case 0x78:
status = wuffs_zlib__decoder__initialize(
&g_potential_decoders.zlib, sizeof g_potential_decoders.zlib,
WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS);
io_transformer =
wuffs_zlib__decoder__upcast_as__wuffs_base__io_transformer(
&g_potential_decoders.zlib);
break;
case 0xFD:
status = wuffs_xz__decoder__initialize(
&g_potential_decoders.xz, sizeof g_potential_decoders.xz,
WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS);
io_transformer = wuffs_xz__decoder__upcast_as__wuffs_base__io_transformer(
&g_potential_decoders.xz);
wuffs_base__io_transformer__set_quirk(
io_transformer,
WUFFS_XZ__QUIRK_DECODE_STANDALONE_CONCATENATED_STREAMS, 1);
break;
}
if (g_flags.ignore_checksum) {
wuffs_base__io_transformer__set_quirk(io_transformer,
WUFFS_BASE__QUIRK_IGNORE_CHECKSUM, 1);
}
if (!wuffs_base__status__is_ok(&status)) {
return wuffs_base__status__message(&status);
}
g_io_transformer = io_transformer;
return NULL;
}
const char* //
main1(int argc, char** argv) {
const char* z = parse_flags(argc, argv);
if (z) {
return z;
} else if (g_flags.remaining_argc > 0) {
return "main: bad argument: use \"program < input\", not \"program input\"";
} else if (g_flags.fail_if_unsandboxed && !g_sandboxed) {
return "main: unsandboxed";
}
wuffs_base__io_buffer dst;
dst.data.ptr = g_dst_buffer_array;
dst.data.len = DST_BUFFER_ARRAY_SIZE;
dst.meta.wi = 0;
dst.meta.ri = 0;
dst.meta.pos = 0;
dst.meta.closed = false;
wuffs_base__io_buffer src;
src.data.ptr = g_src_buffer_array;
src.data.len = SRC_BUFFER_ARRAY_SIZE;
src.meta.wi = 0;
src.meta.ri = 0;
src.meta.pos = 0;
src.meta.closed = false;
while (true) {
const int stdin_fd = 0;
ssize_t n =
read(stdin_fd, src.data.ptr + src.meta.wi, src.data.len - src.meta.wi);
if (n < 0) {
if (errno != EINTR) {
return strerror(errno);
}
continue;
}
src.meta.wi += n;
if (n == 0) {
src.meta.closed = true;
}
if (g_io_transformer) {
// No-op.
} else if (src.meta.ri == src.meta.wi) {
return "main: invalid empty input";
} else {
const char* z = initialize_io_transformer(src.data.ptr[src.meta.ri]);
if (z) {
return z;
} else if (g_flags.output_crc32_digest) {
wuffs_base__status status = wuffs_crc32__ieee_hasher__initialize(
&g_digest_hasher, sizeof g_digest_hasher, WUFFS_VERSION,
WUFFS_INITIALIZE__DEFAULT_OPTIONS);
if (status.repr) {
return wuffs_base__status__message(&status);
}
}
}
while (true) {
wuffs_base__status status = wuffs_base__io_transformer__transform_io(
g_io_transformer, &dst, &src,
wuffs_base__make_slice_u8(&g_workbuf_array[0],
sizeof(g_workbuf_array)));
if (dst.meta.ri < dst.meta.wi) {
if (g_flags.output_crc32_digest) {
wuffs_crc32__ieee_hasher__update(
&g_digest_hasher, wuffs_base__io_buffer__reader_slice(&dst));
} else {
// TODO: handle EINTR and other write errors; see "man 2 write".
const int stdout_fd = 1;
ignore_return_value(write(stdout_fd, g_dst_buffer_array + dst.meta.ri,
dst.meta.wi - dst.meta.ri));
}
dst.meta.ri = dst.meta.wi;
wuffs_base__optional_u63 hrl =
wuffs_base__io_transformer__dst_history_retain_length(
g_io_transformer);
wuffs_base__io_buffer__compact_retaining(
&dst, wuffs_base__optional_u63__value_or(&hrl, UINT64_MAX));
if (dst.meta.wi == dst.data.len) {
return "main: unsupported history length (a.k.a. dictionary size)";
}
}
if (status.repr == wuffs_base__suspension__short_read) {
break;
}
if (status.repr == wuffs_base__suspension__short_write) {
continue;
}
return wuffs_base__status__message(&status);
}
wuffs_base__io_buffer__compact(&src);
}
}
int //
compute_exit_code(const char* status_msg) {
if (!status_msg) {
return 0;
}
size_t n = strnlen(status_msg, 2047);
if (n >= 2047) {
status_msg = "main: internal error: error message is too long";
n = strlen(status_msg);
}
const int stderr_fd = 2;
ignore_return_value(write(stderr_fd, status_msg, n));
ignore_return_value(write(stderr_fd, "\n", 1));
// Return an exit code of 1 for regular (foreseen) errors, e.g. badly
// formatted or unsupported input.
//
// Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
// run-time checks found that an internal invariant did not hold.
//
// Automated testing, including badly formatted inputs, can therefore
// discriminate between expected failure (exit code 1) and unexpected failure
// (other non-zero exit codes). Specifically, exit code 2 for internal
// invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
// linux) for a segmentation fault (e.g. null pointer dereference).
return strstr(status_msg, "internal error:") ? 2 : 1;
}
void //
print_crc32_digest(bool bad) {
const char* hex = "0123456789abcdef";
uint32_t hash = wuffs_crc32__ieee_hasher__checksum_u32(&g_digest_hasher);
char buf[13];
memcpy(buf + 0, bad ? "BAD " : "OK. ", 4);
for (int i = 0; i < 8; i++) {
buf[4 + i] = hex[hash >> 28];
hash <<= 4;
}
buf[12] = '\n';
const int stdout_fd = 1;
ignore_return_value(write(stdout_fd, buf, 13));
}
int //
main(int argc, char** argv) {
#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
g_sandboxed = true;
#endif
int exit_code = compute_exit_code(main1(argc, argv));
if (g_flags.output_crc32_digest) {
print_crc32_digest(exit_code != 0);
}
#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
// Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by
// either calling _exit or returning from main. SECCOMP_MODE_STRICT allows
// only SYS_exit.
syscall(SYS_exit, exit_code);
#endif
return exit_code;
}