forked from xbarin02/brain-compressor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.c
294 lines (231 loc) · 5.38 KB
/
x.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
#define _POSIX_C_SOURCE 2
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include "libx.h"
void fload(void *ptr, size_t size, FILE *stream)
{
if (fread(ptr, 1, size, stream) < size) {
abort();
}
}
void fsave(void *ptr, size_t size, FILE *stream)
{
if (fwrite(ptr, 1, size, stream) < size) {
abort();
}
}
size_t fsize(FILE *stream)
{
long begin = ftell(stream);
if (begin == (long)-1) {
fprintf(stderr, "Stream is not seekable\n");
abort();
}
if (fseek(stream, 0, SEEK_END)) {
abort();
}
long end = ftell(stream);
if (end == (long)-1) {
abort();
}
if (fseek(stream, begin, SEEK_SET)) {
abort();
}
return (size_t)end - (size_t)begin;
}
FILE *force_fopen(const char *pathname, const char *mode, int force)
{
if (force == 0 && access(pathname, F_OK) != -1) {
fprintf(stderr, "File already exists\n");
abort();
}
return fopen(pathname, mode);
}
enum {
COMPRESS,
DECOMPRESS
};
void print_help(char *path)
{
fprintf(stderr, "Usage :\n\t%s [arguments] [input-file] [output-file]\n\n", path);
fprintf(stderr, "Arguments :\n");
fprintf(stderr, " -0 : store only\n");
fprintf(stderr, " -1 : compress faster (default)\n");
fprintf(stderr, " -9 : compress better\n");
fprintf(stderr, " -d : force decompression\n");
fprintf(stderr, " -z : force compression\n");
fprintf(stderr, " -f : overwrite existing output file\n");
fprintf(stderr, " -k : keep (don't delete) input file (default)\n");
fprintf(stderr, " -h : print this message\n");
}
static size_t min_layers = 3;
static size_t max_layers = 1;
struct layer {
void *data; /* input data */
size_t size; /* input size */
} layer[256];
void free_layers()
{
for (size_t j = 0; j < 256; ++j) {
free(layer[j].data);
}
}
size_t multi_compress(size_t j)
{
size_t J = j + 1;
if (J >= 256 || J > max_layers) {
return j;
}
layer[J].data = malloc(8 * layer[j].size);
if (layer[J].data == NULL) {
abort();
}
init();
void *end = compress(layer[j].data, layer[j].size, layer[J].data);
layer[J].size = (char *)end - (char *)layer[J].data;
if (layer[J].size < layer[j].size || J < min_layers) {
/* try next layer */
J = multi_compress(J);
}
return layer[j].size < layer[J].size ? j : J;
}
void multi_decompress(size_t j)
{
if (j == 0) {
return;
}
assert(j > 0 && j < 256);
layer[j - 1].data = malloc(8 * layer[j].size + 4096);
if (layer[j - 1].data == NULL) {
abort();
}
init();
void *end = decompress(layer[j].data, layer[j].size, layer[j - 1].data);
layer[j - 1].size = (char *)end - (char *)layer[j - 1].data;
multi_decompress(j - 1);
}
void load_layer(size_t j, FILE *stream)
{
layer[j].size = fsize(stream);
layer[j].data = malloc(layer[j].size);
if (layer[j].data == NULL) {
fprintf(stderr, "Out of memory\n");
abort();
}
fload(layer[j].data, layer[j].size, stream);
fprintf(stderr, "Input size: %lu bytes\n", (unsigned long)layer[j].size);
}
size_t load_from_container(FILE *stream)
{
int c = fgetc(stream);
if (c == EOF) {
fprintf(stderr, "Unexpected end of file\n");
abort();
}
size_t J = c;
load_layer(J, stream);
return J;
}
void save_layer(size_t j, FILE *stream)
{
fprintf(stderr, "Output size: %lu bytes\n", (unsigned long)layer[j].size);
fsave(layer[j].data, layer[j].size, stream);
}
void save_to_container(size_t J, FILE *stream)
{
if (fputc(J, stream) == EOF) {
abort();
}
save_layer(J, stream);
}
int main(int argc, char *argv[])
{
int mode = (argc > 0 && strcmp(basename(argv[0]), "unx") == 0) ? DECOMPRESS : COMPRESS;
FILE *istream = NULL, *ostream = NULL;
int force = 0;
parse: switch (getopt(argc, argv, "zdf019kh")) {
case 'z':
mode = COMPRESS;
goto parse;
case 'd':
mode = DECOMPRESS;
goto parse;
case 'f':
force = 1;
goto parse;
case '0':
max_layers = 0;
goto parse;
case '1':
max_layers = 1;
goto parse;
case '9':
max_layers = 255;
goto parse;
case 'k':
goto parse;
case 'h':
print_help(argv[0]);
return 0;
default:
abort();
case -1:
;
}
switch (argc - optind) {
case 0:
istream = stdin;
ostream = stdout;
break;
case 1:
istream = fopen(argv[optind], "r");
/* guess output file name */
if (mode == COMPRESS) {
char path[4096];
sprintf(path, "%s.x", argv[optind]); /* add .x suffix */
ostream = force_fopen(path, "w", force);
} else {
if (strrchr(argv[optind], '.') != NULL) {
*strrchr(argv[optind], '.') = 0; /* remove suffix */
}
ostream = force_fopen(argv[optind], "w", force);
}
break;
case 2:
istream = fopen(argv[optind + 0], "r");
ostream = force_fopen(argv[optind + 1], "w", force);
break;
default:
fprintf(stderr, "Unexpected argument\n");
abort();
}
fprintf(stderr, "%s\n", mode == COMPRESS ? "Compressing..." : "Decompressing...");
if (istream == NULL) {
fprintf(stderr, "Cannot open input file\n");
abort();
}
if (ostream == NULL) {
fprintf(stderr, "Cannot open output file\n");
abort();
}
if (mode == COMPRESS) {
load_layer(0, istream);
size_t J = multi_compress(0);
fprintf(stderr, "Number of layers: %lu\n", J);
save_to_container(J, ostream);
} else {
size_t J = load_from_container(istream);
fprintf(stderr, "Number of layers: %lu\n", J);
multi_decompress(J);
save_layer(0, ostream);
}
free_layers();
fclose(istream);
fclose(ostream);
return 0;
}