-
Notifications
You must be signed in to change notification settings - Fork 2
/
blz4.c
493 lines (420 loc) · 10.8 KB
/
blz4.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* blz4 - Example of LZ4 compression with BriefLZ algorithms
*
* Copyright (c) 2018-2020 Joergen Ibsen
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must
* not claim that you wrote the original software. If you use this
* software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must
* not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifdef _MSC_VER
# define _CRT_SECURE_NO_WARNINGS
# define _CRT_DISABLE_PERFCRIT_LOCKS
#else
# define _FILE_OFFSET_BITS 64
# define _ftelli64 ftello64
#endif
#ifdef __MINGW32__
# define __USE_MINGW_ANSI_STDIO 1
#endif
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "lz4.h"
#include "parg.h"
#define LZ4_LEGACY_MAGIC (0x184C2102UL)
/*
* The default block size used to process data.
*/
#ifndef BLOCK_SIZE
# define BLOCK_SIZE (8 * 1024 * 1024UL)
#endif
/*
* Unsigned char type.
*/
typedef unsigned char byte;
/*
* Get the low-order 8 bits of a value.
*/
#if CHAR_BIT == 8
# define octet(v) ((byte) (v))
#else
# define octet(v) ((v) & 0x00FF)
#endif
/*
* Store a 32-bit unsigned value in little-endian order.
*/
static void
write_le32(byte *p, unsigned long val)
{
p[0] = octet(val);
p[1] = octet(val >> 8);
p[2] = octet(val >> 16);
p[3] = octet(val >> 24);
}
/*
* Read a 32-bit unsigned value in little-endian order.
*/
static unsigned long
read_le32(const byte *p)
{
return ((unsigned long) octet(p[0]))
| ((unsigned long) octet(p[1]) << 8)
| ((unsigned long) octet(p[2]) << 16)
| ((unsigned long) octet(p[3]) << 24);
}
static unsigned int
ratio(long long x, long long y)
{
if (x <= LLONG_MAX / 100) {
x *= 100;
}
else {
y /= 100;
}
if (y == 0) {
y = 1;
}
return (unsigned int) (x / y);
}
static void
printf_error(const char *fmt, ...)
{
va_list arg;
fputs("blz4: ", stderr);
va_start(arg, fmt);
vfprintf(stderr, fmt, arg);
va_end(arg);
fputs("\n", stderr);
}
static void
printf_usage(const char *fmt, ...)
{
va_list arg;
fputs("blz4: ", stderr);
va_start(arg, fmt);
vfprintf(stderr, fmt, arg);
va_end(arg);
fputs("\n"
"usage: blz4 [-56789 | --optimal] [-v] INFILE OUTFILE\n"
" blz4 -d [-v] INFILE OUTFILE\n"
" blz4 -V | --version\n"
" blz4 -h | --help\n", stderr);
}
static int
compress_file(const char *oldname, const char *packedname, int be_verbose,
int level)
{
const byte lz4_magic[4] = { 0x02, 0x21, 0x4C, 0x18 };
byte header[4];
FILE *oldfile = NULL;
FILE *packedfile = NULL;
byte *data = NULL;
byte *packed = NULL;
byte *workmem = NULL;
long long insize = 0, outsize = 0;
static const char rotator[] = "-\\|/";
unsigned int counter = 0;
size_t n_read;
clock_t clocks;
int res = 1;
/* Allocate memory */
if ((data = (byte *) malloc(BLOCK_SIZE)) == NULL
|| (packed = (byte *) malloc(lz4_max_packed_size(BLOCK_SIZE))) == NULL
|| (workmem = (byte *) malloc(lz4_workmem_size_level(BLOCK_SIZE, level))) == NULL) {
printf_error("not enough memory");
goto out;
}
/* Open input file */
if ((oldfile = fopen(oldname, "rb")) == NULL) {
printf_usage("unable to open input file '%s'", oldname);
goto out;
}
/* Create output file */
if ((packedfile = fopen(packedname, "wb")) == NULL) {
printf_usage("unable to open output file '%s'", packedname);
goto out;
}
clocks = clock();
/* Write LZ4 header magic */
fwrite(lz4_magic, 1, sizeof(lz4_magic), packedfile);
outsize += sizeof(lz4_magic);
/* While we are able to read data from input file .. */
while ((n_read = fread(data, 1, BLOCK_SIZE, oldfile)) > 0) {
size_t packedsize;
/* Show a little progress indicator */
if (be_verbose) {
fprintf(stderr, "%c\r", rotator[counter]);
counter = (counter + 1) & 0x03;
}
/* Compress data block */
packedsize = lz4_pack_level(data, packed, (unsigned long) n_read,
workmem, level);
/* Check for compression error */
if (packedsize == 0) {
printf_error("an error occured while compressing");
goto out;
}
/* Put block-specific values into header */
write_le32(header, (unsigned long) packedsize);
/* Write header and compressed data */
fwrite(header, 1, sizeof(header), packedfile);
fwrite(packed, 1, packedsize, packedfile);
/* Sum input and output size */
insize += n_read;
outsize += packedsize + sizeof(header);
}
clocks = clock() - clocks;
/* Show result */
if (be_verbose) {
fprintf(stderr, "in %lld out %lld ratio %u%% time %.2f\n",
insize, outsize, ratio(outsize, insize),
(double) clocks / (double) CLOCKS_PER_SEC);
}
res = 0;
out:
/* Close files */
if (packedfile != NULL) {
fclose(packedfile);
}
if (oldfile != NULL) {
fclose(oldfile);
}
/* Free memory */
if (workmem != NULL) {
free(workmem);
}
if (packed != NULL) {
free(packed);
}
if (data != NULL) {
free(data);
}
return res;
}
static int
decompress_file(const char *packedname, const char *newname, int be_verbose)
{
byte header[4];
FILE *newfile = NULL;
FILE *packedfile = NULL;
byte *data = NULL;
byte *packed = NULL;
long long insize = 0, outsize = 0;
static const char rotator[] = "-\\|/";
unsigned int counter = 0;
clock_t clocks;
size_t max_packed_size;
int res = 1;
max_packed_size = lz4_max_packed_size(BLOCK_SIZE);
/* Allocate memory */
if ((data = (byte *) malloc(BLOCK_SIZE)) == NULL
|| (packed = (byte *) malloc(max_packed_size)) == NULL) {
printf_error("not enough memory");
goto out;
}
/* Open input file */
if ((packedfile = fopen(packedname, "rb")) == NULL) {
printf_usage("unable to open input file '%s'", packedname);
goto out;
}
/* Create output file */
if ((newfile = fopen(newname, "wb")) == NULL) {
printf_usage("unable to open output file '%s'", newname);
goto out;
}
clocks = clock();
/* Read LZ4 header magic */
if (fread(header, 1, sizeof(header), packedfile) != sizeof(header)) {
printf_error("unable to read LZ4 header magic");
goto out;
}
/* Check header is LZ4 legacy magic */
if (read_le32(header) != LZ4_LEGACY_MAGIC) {
printf_error("LZ4 header magic mismatch");
goto out;
}
/* While we are able to read a header from input file .. */
while (fread(header, 1, sizeof(header), packedfile) == sizeof(header)) {
size_t hdr_packedsize, depackedsize;
/* Show a little progress indicator */
if (be_verbose) {
fprintf(stderr, "%c\r", rotator[counter]);
counter = (counter + 1) & 0x03;
}
/* Get compressed size from header */
hdr_packedsize = (size_t) read_le32(header);
/* If header is LZ4 magic value, assume new frame */
if (hdr_packedsize == LZ4_LEGACY_MAGIC) {
insize += sizeof(header);
continue;
}
/* Check buffer is sufficient */
if (hdr_packedsize > max_packed_size) {
printf_error("compressed size in header too large");
goto out;
}
/* Read compressed data */
if (fread(packed, 1, hdr_packedsize, packedfile) != hdr_packedsize) {
printf_error("error reading block from compressed file");
goto out;
}
/* Decompress data */
depackedsize = lz4_depack(packed, data,
(unsigned long) hdr_packedsize);
/* Check for decompression error */
if (depackedsize == LZ4_ERROR) {
printf_error("an error occured while decompressing");
goto out;
}
/* Write decompressed data */
fwrite(data, 1, depackedsize, newfile);
/* Sum input and output size */
insize += hdr_packedsize + sizeof(header);
outsize += depackedsize;
}
clocks = clock() - clocks;
/* Show result */
if (be_verbose) {
fprintf(stderr, "in %lld out %lld ratio %u%% time %.2f\n",
insize, outsize, ratio(insize, outsize),
(double) clocks / (double) CLOCKS_PER_SEC);
}
res = 0;
out:
/* Close files */
if (packedfile != NULL) {
fclose(packedfile);
}
if (newfile != NULL) {
fclose(newfile);
}
/* Free memory */
if (packed != NULL) {
free(packed);
}
if (data != NULL) {
free(data);
}
return res;
}
static void
print_syntax(void)
{
fputs("usage: blz4 [options] INFILE OUTFILE\n"
"\n"
"options:\n"
" -5 compress faster (default)\n"
" -9 compress better\n"
" --optimal optimal but very slow compression\n"
" -d, --decompress decompress\n"
" -h, --help print this help and exit\n"
" -v, --verbose verbose mode\n"
" -V, --version print version and exit\n"
"\n"
"PLEASE NOTE: This is an experiment, use at your own risk.\n", stdout);
}
static void
print_version(void)
{
fputs("blz4 " LZ4_VER_STRING "\n"
"\n"
"Copyright (c) 2018-2020 Joergen Ibsen\n"
"\n"
"Licensed under the zlib license (Zlib).\n"
"There is NO WARRANTY, to the extent permitted by law.\n", stdout);
}
int
main(int argc, char *argv[])
{
struct parg_state ps;
const char *infile = NULL;
const char *outfile = NULL;
int flag_decompress = 0;
int flag_verbose = 0;
int level = 5;
int c;
const struct parg_option long_options[] = {
{ "decompress", PARG_NOARG, NULL, 'd' },
{ "help", PARG_NOARG, NULL, 'h' },
{ "optimal", PARG_NOARG, NULL, 'x' },
{ "verbose", PARG_NOARG, NULL, 'v' },
{ "version", PARG_NOARG, NULL, 'V' },
{ 0, 0, 0, 0 }
};
parg_init(&ps);
while ((c = parg_getopt_long(&ps, argc, argv, "56789dhvVx", long_options, NULL)) != -1) {
switch (c) {
case 1:
if (infile == NULL) {
infile = ps.optarg;
}
else if (outfile == NULL) {
outfile = ps.optarg;
}
else {
printf_usage("too many arguments");
return EXIT_FAILURE;
}
break;
case '5':
case '6':
case '7':
case '8':
case '9':
level = c - '0';
break;
case 'x':
level = 10;
break;
case 'd':
flag_decompress = 1;
break;
case 'h':
print_syntax();
return EXIT_SUCCESS;
break;
case 'v':
flag_verbose = 1;
break;
case 'V':
print_version();
return EXIT_SUCCESS;
break;
default:
printf_usage("unknown option '%s'", argv[ps.optind - 1]);
return EXIT_FAILURE;
break;
}
}
if (outfile == NULL) {
printf_usage("too few arguments");
return EXIT_FAILURE;
}
if (flag_decompress) {
return decompress_file(infile, outfile, flag_verbose);
}
else {
return compress_file(infile, outfile, flag_verbose, level);
}
return EXIT_SUCCESS;
}