-
Notifications
You must be signed in to change notification settings - Fork 12
/
deb.c
136 lines (126 loc) · 3.66 KB
/
deb.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
/* Decompress brotli streams on the command line or from stdin using yeast.
The compressed output is written to the same name with the suffix ".bro" or
".compressed" removed and ".out" added, or to "deb.out" when reading from
stdin. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "load.h"
#include "yeast.h"
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
#define SUFFIX1 ".compressed"
#define SUFFIX2 ".bro"
#define OUT ".out"
/* Write decompressed output to a derived file name with extension ".out". */
static void deliver(char *in, void *data, size_t len)
{
char *out;
FILE *file;
size_t inlen, suf;
inlen = strlen(in);
out = malloc(inlen + strlen(OUT) + 1);
if (out == NULL) {
fprintf(stderr, "out of memory");
return;
}
suf = strlen(SUFFIX1);
if (inlen >= suf && strcmp(in + inlen - suf, SUFFIX1) == 0)
inlen -= suf;
else {
suf = strlen(SUFFIX2);
if (inlen >= suf && strcmp(in + inlen - suf, SUFFIX2) == 0)
inlen -= suf;
}
memcpy(out, in, inlen);
strcpy(out + inlen, OUT);
file = fopen(out, "wb");
if (file == NULL) {
fprintf(stderr, "could not create %s", out);
free(out);
return;
}
fwrite(data, 1, len, file);
fclose(file);
free(out);
}
/* Decompress all of the files on the command line, or from stdin if no
arguments. */
int main(int argc, char **argv)
{
FILE *in;
int ret;
void *source = NULL;
void *dest;
size_t size, len, got;
#ifdef DEBUG
/* process verbosity option */
if (argc > 1 && argv[1][0] == '-') {
char *opt;
--argc;
opt = *++argv;
while (*++opt) {
if (*opt == 'v')
yeast_verbosity++;
else {
fprintf(stderr, "deb: invalid option %s\n", opt);
return 1;
}
}
}
#endif
/* decompress each file on the remaining command line */
if (--argc) {
for (;;) {
in = fopen(*++argv, "rb");
if (in == NULL) {
fprintf(stderr, "error opening %s\n", *argv);
continue;
}
ret = load(in, 0, &source, &size, &len);
fclose(in);
if (ret < 0) {
fprintf(stderr, "error reading %s\n", *argv);
continue;
}
if (ret > 0) {
fputs("out of memory\n", stderr);
return 1;
}
fputs(*argv, stderr);
fputs(":\n", stderr);
ret = yeast(&dest, &got, source, &len, 0);
fprintf(stderr, "uncompressed length = %zu\n", got);
if (ret)
fprintf(stderr, "yeast() returned %d\n", ret);
deliver(*argv, dest, got);
free(dest);
if (--argc == 0)
break;
putc('\n', stderr);
}
}
/* or if no names on the remaining command line, decompress from stdin */
else {
SET_BINARY_MODE(stdin);
ret = load(stdin, 0, &source, &size, &len);
if (ret) {
fputs(ret > 0 ? "out of memory\n" : "error reading stdin\n",
stderr);
return 1;
}
ret = yeast(&dest, &got, source, &len, 0);
fprintf(stderr, "uncompressed length = %zu\n", got);
if (ret)
fprintf(stderr, "yeast() returned %d\n", ret);
deliver("deb", dest, got);
free(dest);
}
free(source);
return 0;
}