-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.c
266 lines (223 loc) · 6.47 KB
/
gzip.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
// @LINK SHARED: libz
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#define CHUNK_SIZE 16384
char *get_dest_filename(const char *source, int compress) {
char *dest = malloc(strlen(source) + 6);
strcpy(dest, source);
if (compress) {
strcat(dest, ".gz");
return dest;
}
char *ext = strrchr(dest, '.');
if (ext && strcmp(ext, ".gz") == 0) {
*ext = '\0';
return dest;
}
strcat(dest, ".out");
return dest;
}
int compress_fd(FILE *in, FILE *out) {
int ret;
unsigned char inbuf[CHUNK_SIZE];
unsigned char outbuf[CHUNK_SIZE];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK) {
fprintf(stderr, "gzip: deflateInit failed\n");
return ret;
}
do {
strm.avail_in = fread(inbuf, 1, CHUNK_SIZE, in);
if (ferror(in)) {
deflateEnd(&strm);
fprintf(stderr, "gzip: fread failed\n");
return Z_ERRNO;
}
strm.next_in = inbuf;
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = outbuf;
ret = deflate(&strm, feof(in) ? Z_FINISH : Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR) {
deflateEnd(&strm);
fprintf(stderr, "gzip: deflate failed\n");
return ret;
}
if (fwrite(outbuf, 1, CHUNK_SIZE - strm.avail_out, out) != CHUNK_SIZE - strm.avail_out || ferror(out)) {
deflateEnd(&strm);
fprintf(stderr, "gzip: fwrite failed\n");
return Z_ERRNO;
}
} while (strm.avail_out == 0);
} while (!feof(in));
deflateEnd(&strm);
return Z_OK;
}
int compress_file(const char *source) {
char *dest = get_dest_filename(source, 1);
FILE *source_file = fopen(source, "rb");
if (!source_file) {
free(dest);
fprintf(stderr, "gzip: %s: No such file or directory\n", source);
return 1;
}
FILE *dest_file = fopen(dest, "wb");
if (!dest_file) {
fclose(source_file);
free(dest);
fprintf(stderr, "gzip: %s: Failed to open file\n", dest);
return 1;
}
fwrite("\x1f\x8b", 1, 2, dest_file);
int ret = compress_fd(source_file, dest_file) != Z_OK;
fclose(source_file);
fclose(dest_file);
free(dest);
return ret;
}
int decompress_fd(FILE *in, FILE *out) {
int ret;
unsigned char inbuf[CHUNK_SIZE];
unsigned char outbuf[CHUNK_SIZE];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if (ret != Z_OK) {
fprintf(stderr, "gzip: inflateInit failed\n");
return ret;
}
do {
strm.avail_in = fread(inbuf, 1, CHUNK_SIZE, in);
if (ferror(in)) {
inflateEnd(&strm);
fprintf(stderr, "gzip: fread failed\n");
return Z_ERRNO;
}
if (strm.avail_in == 0) break;
strm.next_in = inbuf;
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = outbuf;
ret = inflate(&strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR) {
inflateEnd(&strm);
fprintf(stderr, "gzip: inflate failed\n");
return ret;
}
if (fwrite(outbuf, 1, CHUNK_SIZE - strm.avail_out, out) != CHUNK_SIZE - strm.avail_out || ferror(out)) {
inflateEnd(&strm);
fprintf(stderr, "gzip: fwrite failed\n");
return Z_ERRNO;
}
} while (strm.avail_out == 0);
} while (ret != Z_STREAM_END);
inflateEnd(&strm);
return Z_OK;
}
int is_fd_gzip(FILE *file) {
unsigned char header[2];
if (fread(header, 1, 2, file) != 2) return 0;
return header[0] == 0x1f && header[1] == 0x8b;
}
int decompress_file(const char *source) {
char *dest = get_dest_filename(source, 0);
int ret;
FILE *source_file = fopen(source, "rb");
if (!source_file) {
free(dest);
fprintf(stderr, "gzip: %s: No such file or directory\n", source);
return 1;
}
if (!is_fd_gzip(source_file)) {
fclose(source_file);
free(dest);
fprintf(stderr, "gzip: %s: Not in gzip format\n", source);
return 1;
}
FILE *dest_file = fopen(dest, "wb");
if (!dest_file) {
fclose(source_file);
free(dest);
fprintf(stderr, "gzip: %s: Failed to open file\n", dest);
return 1;
}
ret = decompress_fd(source_file, dest_file) != Z_OK;
fclose(source_file);
fclose(dest_file);
free(dest);
return ret;
}
typedef struct {
uint8_t compress;
uint8_t keep;
uint8_t help;
int arg_offset;
} gzip_args_t;
void print_help(int full) {
puts("Usage: gzip [options] <file1> [file2 ...]");
if (!full) {
puts("Try 'gzip -h' for more information.");
return;
}
puts(
"Options:\n"
" -d decompress files\n"
" -k keep original files\n"
" -h display this help message"
);
}
gzip_args_t *parse_args(int argc, char **argv) {
gzip_args_t *args = malloc(sizeof(gzip_args_t));
args->compress = 1;
args->keep = 0;
args->help = 0;
args->arg_offset = 1;
if (argc == 1) {
args->help = 1;
return args;
}
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] != '\0') {
if (strcmp(argv[i], "-d") == 0) {
args->compress = 0;
} else if (strcmp(argv[i], "-k") == 0) {
args->keep = 1;
} else if (strcmp(argv[i], "-h") == 0) {
args->help = 1;
} else {
fprintf(stderr, "gzip: invalid option -- '%s'\n", argv[i]);
args->help = 1;
}
} else {
args->arg_offset = i;
break;
}
}
return args;
}
int main(int argc, char **argv) {
gzip_args_t *args = parse_args(argc, argv);
int ret = 0;
if (args->help) {
ret = args->arg_offset == 1;
print_help(!ret);
free(args);
return ret;
}
for (int i = args->arg_offset; i < argc; i++) {
if ((ret = ((args->compress) ? compress_file : decompress_file)(argv[i]))) break;
if (!args->keep) unlink(argv[i]);
}
free(args);
return ret;
}