2018-01-31 22:33:16 +00:00
|
|
|
/* LZ4frame API example : compress a file
|
2018-02-01 10:48:20 +00:00
|
|
|
* Modified from an example code by Zbigniew Jędrzejewski-Szmek
|
2018-01-31 22:33:16 +00:00
|
|
|
*
|
|
|
|
* This example streams an input file into an output file
|
|
|
|
* using a bounded memory budget.
|
|
|
|
* Input is read in chunks of IN_CHUNK_SIZE */
|
2015-06-28 10:34:12 +00:00
|
|
|
|
2015-05-21 13:17:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2018-01-31 22:33:16 +00:00
|
|
|
#include <assert.h>
|
2015-05-21 13:17:21 +00:00
|
|
|
|
|
|
|
#include <lz4frame.h>
|
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
|
|
|
|
#define IN_CHUNK_SIZE (16*1024)
|
2015-05-21 13:17:21 +00:00
|
|
|
|
2018-02-01 00:39:37 +00:00
|
|
|
static const LZ4F_preferences_t kPrefs = {
|
2017-08-09 19:29:38 +00:00
|
|
|
{ LZ4F_max256KB, LZ4F_blockLinked, LZ4F_noContentChecksum, LZ4F_frame,
|
2018-01-31 22:33:16 +00:00
|
|
|
0 /* unknown content size */, 0 /* no dictID */ , LZ4F_noBlockChecksum },
|
|
|
|
0, /* compression level; 0 == default */
|
2017-08-09 19:29:38 +00:00
|
|
|
0, /* autoflush */
|
2018-04-27 00:02:20 +00:00
|
|
|
0, /* favor decompression speed */
|
|
|
|
{ 0, 0, 0 }, /* reserved, must be set to 0 */
|
2015-05-21 13:17:21 +00:00
|
|
|
};
|
|
|
|
|
2017-08-09 19:29:38 +00:00
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
/* safe_fwrite() :
|
|
|
|
* performs fwrite(), ensure operation success, or immediately exit() */
|
|
|
|
static void safe_fwrite(void* buf, size_t eltSize, size_t nbElt, FILE* f)
|
|
|
|
{
|
|
|
|
size_t const writtenSize = fwrite(buf, eltSize, nbElt, f);
|
2018-09-05 23:04:05 +00:00
|
|
|
size_t const expectedSize = eltSize * nbElt;
|
2019-08-21 11:44:24 +00:00
|
|
|
if (nbElt>0) assert(expectedSize / nbElt == eltSize); /* check overflow */
|
2018-01-31 22:33:16 +00:00
|
|
|
if (writtenSize < expectedSize) {
|
|
|
|
if (ferror(f)) /* note : ferror() must follow fwrite */
|
2018-09-05 23:04:05 +00:00
|
|
|
fprintf(stderr, "Write failed \n");
|
2018-01-31 22:33:16 +00:00
|
|
|
else
|
2019-08-21 11:44:24 +00:00
|
|
|
fprintf(stderr, "Write too short \n");
|
2018-01-31 22:33:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-01 00:39:37 +00:00
|
|
|
/* ================================================= */
|
|
|
|
/* Streaming Compression example */
|
|
|
|
/* ================================================= */
|
2018-01-31 22:33:16 +00:00
|
|
|
|
2018-02-01 00:39:37 +00:00
|
|
|
typedef struct {
|
|
|
|
int error;
|
|
|
|
unsigned long long size_in;
|
|
|
|
unsigned long long size_out;
|
|
|
|
} compressResult_t;
|
2017-08-09 19:29:38 +00:00
|
|
|
|
2018-02-01 00:39:37 +00:00
|
|
|
static compressResult_t
|
2018-02-01 10:48:20 +00:00
|
|
|
compress_file_internal(FILE* f_in, FILE* f_out,
|
2018-09-05 23:04:05 +00:00
|
|
|
LZ4F_compressionContext_t ctx,
|
|
|
|
void* inBuff, size_t inChunkSize,
|
|
|
|
void* outBuff, size_t outCapacity)
|
2018-02-01 00:39:37 +00:00
|
|
|
{
|
|
|
|
compressResult_t result = { 1, 0, 0 }; /* result for an error */
|
|
|
|
unsigned long long count_in = 0, count_out;
|
2017-08-09 19:29:38 +00:00
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
assert(f_in != NULL); assert(f_out != NULL);
|
2018-02-01 00:39:37 +00:00
|
|
|
assert(ctx != NULL);
|
|
|
|
assert(outCapacity >= LZ4F_HEADER_SIZE_MAX);
|
|
|
|
assert(outCapacity >= LZ4F_compressBound(inChunkSize, &kPrefs));
|
2017-08-09 19:29:38 +00:00
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
/* write frame header */
|
2018-02-01 00:39:37 +00:00
|
|
|
{ size_t const headerSize = LZ4F_compressBegin(ctx, outBuff, outCapacity, &kPrefs);
|
2017-11-25 01:18:46 +00:00
|
|
|
if (LZ4F_isError(headerSize)) {
|
2018-09-19 01:08:59 +00:00
|
|
|
printf("Failed to start compression: error %u \n", (unsigned)headerSize);
|
2018-02-01 00:39:37 +00:00
|
|
|
return result;
|
2017-11-25 01:18:46 +00:00
|
|
|
}
|
2018-01-31 22:33:16 +00:00
|
|
|
count_out = headerSize;
|
2018-09-19 01:08:59 +00:00
|
|
|
printf("Buffer size is %u bytes, header size %u bytes \n",
|
|
|
|
(unsigned)outCapacity, (unsigned)headerSize);
|
2018-02-01 10:48:20 +00:00
|
|
|
safe_fwrite(outBuff, 1, headerSize, f_out);
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
/* stream file */
|
2017-08-09 19:29:38 +00:00
|
|
|
for (;;) {
|
2018-02-01 10:48:20 +00:00
|
|
|
size_t const readSize = fread(inBuff, 1, IN_CHUNK_SIZE, f_in);
|
|
|
|
if (readSize == 0) break; /* nothing left to read from input file */
|
2017-11-25 01:18:46 +00:00
|
|
|
count_in += readSize;
|
2017-08-09 19:29:38 +00:00
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
size_t const compressedSize = LZ4F_compressUpdate(ctx,
|
2018-02-01 00:39:37 +00:00
|
|
|
outBuff, outCapacity,
|
|
|
|
inBuff, readSize,
|
2018-01-31 22:33:16 +00:00
|
|
|
NULL);
|
|
|
|
if (LZ4F_isError(compressedSize)) {
|
2018-09-19 01:08:59 +00:00
|
|
|
printf("Compression failed: error %u \n", (unsigned)compressedSize);
|
2018-02-01 00:39:37 +00:00
|
|
|
return result;
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 01:08:59 +00:00
|
|
|
printf("Writing %u bytes\n", (unsigned)compressedSize);
|
2018-02-01 10:48:20 +00:00
|
|
|
safe_fwrite(outBuff, 1, compressedSize, f_out);
|
2018-01-31 22:33:16 +00:00
|
|
|
count_out += compressedSize;
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
/* flush whatever remains within internal buffers */
|
|
|
|
{ size_t const compressedSize = LZ4F_compressEnd(ctx,
|
2018-02-01 10:48:20 +00:00
|
|
|
outBuff, outCapacity,
|
|
|
|
NULL);
|
2017-11-25 01:18:46 +00:00
|
|
|
if (LZ4F_isError(compressedSize)) {
|
2018-09-19 01:08:59 +00:00
|
|
|
printf("Failed to end compression: error %u \n", (unsigned)compressedSize);
|
2018-02-01 00:39:37 +00:00
|
|
|
return result;
|
2017-11-25 01:18:46 +00:00
|
|
|
}
|
2018-01-31 22:33:16 +00:00
|
|
|
|
2018-09-19 01:08:59 +00:00
|
|
|
printf("Writing %u bytes \n", (unsigned)compressedSize);
|
2018-02-01 10:48:20 +00:00
|
|
|
safe_fwrite(outBuff, 1, compressedSize, f_out);
|
2017-11-25 01:18:46 +00:00
|
|
|
count_out += compressedSize;
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 00:39:37 +00:00
|
|
|
result.size_in = count_in;
|
|
|
|
result.size_out = count_out;
|
|
|
|
result.error = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static compressResult_t
|
2018-02-01 09:36:38 +00:00
|
|
|
compress_file(FILE* f_in, FILE* f_out)
|
2018-02-01 00:39:37 +00:00
|
|
|
{
|
2018-02-01 09:36:38 +00:00
|
|
|
assert(f_in != NULL);
|
|
|
|
assert(f_out != NULL);
|
2018-02-01 00:39:37 +00:00
|
|
|
|
2018-02-01 09:36:38 +00:00
|
|
|
/* ressource allocation */
|
2018-02-01 00:39:37 +00:00
|
|
|
LZ4F_compressionContext_t ctx;
|
2018-02-01 09:36:38 +00:00
|
|
|
size_t const ctxCreation = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
|
2018-02-01 00:39:37 +00:00
|
|
|
void* const src = malloc(IN_CHUNK_SIZE);
|
|
|
|
size_t const outbufCapacity = LZ4F_compressBound(IN_CHUNK_SIZE, &kPrefs); /* large enough for any input <= IN_CHUNK_SIZE */
|
2018-02-01 09:36:38 +00:00
|
|
|
void* const outbuff = malloc(outbufCapacity);
|
2018-02-01 00:39:37 +00:00
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
compressResult_t result = { 1, 0, 0 }; /* == error (default) */
|
2018-02-01 09:36:38 +00:00
|
|
|
if (!LZ4F_isError(ctxCreation) && src && outbuff) {
|
|
|
|
result = compress_file_internal(f_in, f_out,
|
2018-02-01 00:39:37 +00:00
|
|
|
ctx,
|
|
|
|
src, IN_CHUNK_SIZE,
|
|
|
|
outbuff, outbufCapacity);
|
2018-02-01 09:36:38 +00:00
|
|
|
} else {
|
|
|
|
printf("error : ressource allocation failed \n");
|
|
|
|
}
|
2017-11-25 01:18:46 +00:00
|
|
|
|
|
|
|
LZ4F_freeCompressionContext(ctx); /* supports free on NULL */
|
2017-08-09 19:29:38 +00:00
|
|
|
free(src);
|
2018-01-31 22:33:16 +00:00
|
|
|
free(outbuff);
|
|
|
|
return result;
|
2015-05-21 13:17:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
|
2018-02-01 00:39:37 +00:00
|
|
|
/* ================================================= */
|
|
|
|
/* Streaming decompression example */
|
|
|
|
/* ================================================= */
|
|
|
|
|
2016-11-11 19:31:52 +00:00
|
|
|
static size_t get_block_size(const LZ4F_frameInfo_t* info) {
|
2017-08-09 19:29:38 +00:00
|
|
|
switch (info->blockSizeID) {
|
2016-11-21 23:44:25 +00:00
|
|
|
case LZ4F_default:
|
2017-08-09 19:29:38 +00:00
|
|
|
case LZ4F_max64KB: return 1 << 16;
|
|
|
|
case LZ4F_max256KB: return 1 << 18;
|
|
|
|
case LZ4F_max1MB: return 1 << 20;
|
|
|
|
case LZ4F_max4MB: return 1 << 22;
|
|
|
|
default:
|
2018-02-01 10:48:20 +00:00
|
|
|
printf("Impossible with expected frame specification (<=v1.6.1)\n");
|
2017-08-09 19:29:38 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2016-11-11 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
/* @return : 1==error, 0==success */
|
|
|
|
static int
|
|
|
|
decompress_file_internal(FILE* f_in, FILE* f_out,
|
2018-09-05 23:04:05 +00:00
|
|
|
LZ4F_dctx* dctx,
|
|
|
|
void* src, size_t srcCapacity, size_t filled, size_t alreadyConsumed,
|
|
|
|
void* dst, size_t dstCapacity)
|
2018-02-01 10:48:20 +00:00
|
|
|
{
|
|
|
|
int firstChunk = 1;
|
2017-11-25 01:18:46 +00:00
|
|
|
size_t ret = 1;
|
2015-05-21 13:17:21 +00:00
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
assert(f_in != NULL); assert(f_out != NULL);
|
|
|
|
assert(dctx != NULL);
|
|
|
|
assert(src != NULL); assert(srcCapacity > 0); assert(filled <= srcCapacity); assert(alreadyConsumed <= filled);
|
|
|
|
assert(dst != NULL); assert(dstCapacity > 0);
|
2017-08-09 19:29:38 +00:00
|
|
|
|
|
|
|
/* Decompression */
|
|
|
|
while (ret != 0) {
|
|
|
|
/* Load more input */
|
2018-02-01 10:48:20 +00:00
|
|
|
size_t readSize = firstChunk ? filled : fread(src, 1, srcCapacity, f_in); firstChunk=0;
|
2018-09-13 21:29:41 +00:00
|
|
|
const void* srcPtr = (const char*)src + alreadyConsumed; alreadyConsumed=0;
|
|
|
|
const void* const srcEnd = (const char*)srcPtr + readSize;
|
2018-02-01 10:48:20 +00:00
|
|
|
if (readSize == 0 || ferror(f_in)) {
|
2017-08-09 19:29:38 +00:00
|
|
|
printf("Decompress: not enough input or error reading file\n");
|
2018-02-01 10:48:20 +00:00
|
|
|
return 1;
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
2018-02-01 10:48:20 +00:00
|
|
|
|
2017-08-09 19:29:38 +00:00
|
|
|
/* Decompress:
|
2018-02-01 10:48:20 +00:00
|
|
|
* Continue while there is more input to read (srcPtr != srcEnd)
|
|
|
|
* and the frame isn't over (ret != 0)
|
2017-08-09 19:29:38 +00:00
|
|
|
*/
|
2018-09-05 23:45:46 +00:00
|
|
|
while (srcPtr < srcEnd && ret != 0) {
|
2018-02-01 10:48:20 +00:00
|
|
|
/* Any data within dst has been flushed at this stage */
|
2017-08-09 19:29:38 +00:00
|
|
|
size_t dstSize = dstCapacity;
|
2018-09-13 21:29:41 +00:00
|
|
|
size_t srcSize = (const char*)srcEnd - (const char*)srcPtr;
|
2017-08-09 19:29:38 +00:00
|
|
|
ret = LZ4F_decompress(dctx, dst, &dstSize, srcPtr, &srcSize, /* LZ4F_decompressOptions_t */ NULL);
|
|
|
|
if (LZ4F_isError(ret)) {
|
|
|
|
printf("Decompression error: %s\n", LZ4F_getErrorName(ret));
|
2018-02-01 10:48:20 +00:00
|
|
|
return 1;
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
|
|
|
/* Flush output */
|
2018-02-01 10:48:20 +00:00
|
|
|
if (dstSize != 0) safe_fwrite(dst, 1, dstSize, f_out);
|
2017-08-09 19:29:38 +00:00
|
|
|
/* Update input */
|
2018-09-13 21:29:41 +00:00
|
|
|
srcPtr = (const char*)srcPtr + srcSize;
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
2018-09-05 23:45:46 +00:00
|
|
|
|
|
|
|
assert(srcPtr <= srcEnd);
|
|
|
|
|
|
|
|
/* Ensure all input data has been consumed.
|
|
|
|
* It is valid to have multiple frames in the same file,
|
|
|
|
* but this example only supports one frame.
|
|
|
|
*/
|
|
|
|
if (srcPtr < srcEnd) {
|
|
|
|
printf("Decompress: Trailing data left in file after frame\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
2018-02-01 10:48:20 +00:00
|
|
|
|
2018-09-05 23:45:46 +00:00
|
|
|
/* Check that there isn't trailing data in the file after the frame.
|
2018-02-01 10:48:20 +00:00
|
|
|
* It is valid to have multiple frames in the same file,
|
|
|
|
* but this example only supports one frame.
|
2017-08-09 19:29:38 +00:00
|
|
|
*/
|
2018-02-01 10:48:20 +00:00
|
|
|
{ size_t const readSize = fread(src, 1, 1, f_in);
|
|
|
|
if (readSize != 0 || !feof(f_in)) {
|
|
|
|
printf("Decompress: Trailing data left in file after frame\n");
|
|
|
|
return 1;
|
|
|
|
} }
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* @return : 1==error, 0==completed */
|
|
|
|
static int
|
|
|
|
decompress_file_allocDst(FILE* f_in, FILE* f_out,
|
|
|
|
LZ4F_dctx* dctx,
|
|
|
|
void* src, size_t srcCapacity)
|
|
|
|
{
|
|
|
|
assert(f_in != NULL); assert(f_out != NULL);
|
|
|
|
assert(dctx != NULL);
|
|
|
|
assert(src != NULL);
|
|
|
|
assert(srcCapacity >= LZ4F_HEADER_SIZE_MAX); /* ensure LZ4F_getFrameInfo() can read enough data */
|
|
|
|
|
|
|
|
/* Read Frame header */
|
|
|
|
size_t const readSize = fread(src, 1, srcCapacity, f_in);
|
|
|
|
if (readSize == 0 || ferror(f_in)) {
|
|
|
|
printf("Decompress: not enough input or error reading file\n");
|
|
|
|
return 1;
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
2015-05-21 13:17:21 +00:00
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
LZ4F_frameInfo_t info;
|
|
|
|
size_t consumedSize = readSize;
|
|
|
|
{ size_t const fires = LZ4F_getFrameInfo(dctx, &info, src, &consumedSize);
|
|
|
|
if (LZ4F_isError(fires)) {
|
|
|
|
printf("LZ4F_getFrameInfo error: %s\n", LZ4F_getErrorName(fires));
|
|
|
|
return 1;
|
|
|
|
} }
|
|
|
|
|
|
|
|
/* Allocating enough space for an entire block isn't necessary for
|
|
|
|
* correctness, but it allows some memcpy's to be elided.
|
|
|
|
*/
|
|
|
|
size_t const dstCapacity = get_block_size(&info);
|
|
|
|
void* const dst = malloc(dstCapacity);
|
|
|
|
if (!dst) { perror("decompress_file(dst)"); return 1; }
|
|
|
|
|
|
|
|
int const decompressionResult = decompress_file_internal(
|
|
|
|
f_in, f_out,
|
|
|
|
dctx,
|
2018-07-02 12:35:04 +00:00
|
|
|
src, srcCapacity, readSize-consumedSize, consumedSize,
|
2018-02-01 10:48:20 +00:00
|
|
|
dst, dstCapacity);
|
|
|
|
|
2017-08-09 19:29:38 +00:00
|
|
|
free(dst);
|
2018-02-01 10:48:20 +00:00
|
|
|
return decompressionResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* @result : 1==error, 0==success */
|
|
|
|
static int decompress_file(FILE* f_in, FILE* f_out)
|
|
|
|
{
|
|
|
|
assert(f_in != NULL); assert(f_out != NULL);
|
|
|
|
|
|
|
|
/* Ressource allocation */
|
|
|
|
void* const src = malloc(IN_CHUNK_SIZE);
|
|
|
|
if (!src) { perror("decompress_file(src)"); return 1; }
|
|
|
|
|
|
|
|
LZ4F_dctx* dctx;
|
2018-09-05 23:04:05 +00:00
|
|
|
{ size_t const dctxStatus = LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION);
|
2018-02-01 10:48:20 +00:00
|
|
|
if (LZ4F_isError(dctxStatus)) {
|
|
|
|
printf("LZ4F_dctx creation error: %s\n", LZ4F_getErrorName(dctxStatus));
|
|
|
|
} }
|
|
|
|
|
|
|
|
int const result = !dctx ? 1 /* error */ :
|
|
|
|
decompress_file_allocDst(f_in, f_out, dctx, src, IN_CHUNK_SIZE);
|
|
|
|
|
|
|
|
free(src);
|
|
|
|
LZ4F_freeDecompressionContext(dctx); /* note : free works on NULL */
|
|
|
|
return result;
|
2015-05-21 13:17:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
int compareFiles(FILE* fp0, FILE* fp1)
|
2016-11-11 19:31:52 +00:00
|
|
|
{
|
2017-08-09 19:29:38 +00:00
|
|
|
int result = 0;
|
2016-11-11 19:31:52 +00:00
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
while (result==0) {
|
2017-08-09 19:29:38 +00:00
|
|
|
char b0[1024];
|
|
|
|
char b1[1024];
|
2018-02-01 10:48:20 +00:00
|
|
|
size_t const r0 = fread(b0, 1, sizeof(b0), fp0);
|
|
|
|
size_t const r1 = fread(b1, 1, sizeof(b1), fp1);
|
2016-11-11 19:31:52 +00:00
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
result = (r0 != r1);
|
|
|
|
if (!r0 || !r1) break;
|
|
|
|
if (!result) result = memcmp(b0, b1, r0);
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
2016-11-11 19:31:52 +00:00
|
|
|
|
2017-08-09 19:29:38 +00:00
|
|
|
return result;
|
2016-11-11 19:31:52 +00:00
|
|
|
}
|
2015-06-28 10:34:12 +00:00
|
|
|
|
2018-01-31 22:33:16 +00:00
|
|
|
|
2016-11-22 00:42:23 +00:00
|
|
|
int main(int argc, const char **argv) {
|
2017-08-09 19:29:38 +00:00
|
|
|
char inpFilename[256] = { 0 };
|
|
|
|
char lz4Filename[256] = { 0 };
|
|
|
|
char decFilename[256] = { 0 };
|
|
|
|
|
2018-02-01 10:48:20 +00:00
|
|
|
if (argc < 2) {
|
2017-08-09 19:29:38 +00:00
|
|
|
printf("Please specify input filename\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(inpFilename, 256, "%s", argv[1]);
|
|
|
|
snprintf(lz4Filename, 256, "%s.lz4", argv[1]);
|
|
|
|
snprintf(decFilename, 256, "%s.lz4.dec", argv[1]);
|
|
|
|
|
|
|
|
printf("inp = [%s]\n", inpFilename);
|
|
|
|
printf("lz4 = [%s]\n", lz4Filename);
|
|
|
|
printf("dec = [%s]\n", decFilename);
|
|
|
|
|
|
|
|
/* compress */
|
|
|
|
{ FILE* const inpFp = fopen(inpFilename, "rb");
|
|
|
|
FILE* const outFp = fopen(lz4Filename, "wb");
|
|
|
|
|
|
|
|
printf("compress : %s -> %s\n", inpFilename, lz4Filename);
|
2018-02-01 00:39:37 +00:00
|
|
|
compressResult_t const ret = compress_file(inpFp, outFp);
|
2018-02-01 10:48:20 +00:00
|
|
|
|
|
|
|
fclose(outFp);
|
|
|
|
fclose(inpFp);
|
|
|
|
|
2018-02-01 00:39:37 +00:00
|
|
|
if (ret.error) {
|
|
|
|
printf("compress : failed with code %i\n", ret.error);
|
|
|
|
return ret.error;
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
|
|
|
printf("%s: %zu → %zu bytes, %.1f%%\n",
|
2018-01-31 22:33:16 +00:00
|
|
|
inpFilename,
|
2018-02-01 10:48:20 +00:00
|
|
|
(size_t)ret.size_in, (size_t)ret.size_out, /* might overflow is size_t is 32 bits and size_{in,out} > 4 GB */
|
2018-02-01 00:39:37 +00:00
|
|
|
(double)ret.size_out / ret.size_in * 100);
|
2017-08-09 19:29:38 +00:00
|
|
|
printf("compress : done\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decompress */
|
|
|
|
{ FILE* const inpFp = fopen(lz4Filename, "rb");
|
|
|
|
FILE* const outFp = fopen(decFilename, "wb");
|
|
|
|
|
|
|
|
printf("decompress : %s -> %s\n", lz4Filename, decFilename);
|
2018-02-01 10:48:20 +00:00
|
|
|
int const ret = decompress_file(inpFp, outFp);
|
2017-08-09 19:29:38 +00:00
|
|
|
|
|
|
|
fclose(outFp);
|
|
|
|
fclose(inpFp);
|
2018-02-01 10:48:20 +00:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
printf("decompress : failed with code %i\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
printf("decompress : done\n");
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
{ FILE* const inpFp = fopen(inpFilename, "rb");
|
|
|
|
FILE* const decFp = fopen(decFilename, "rb");
|
|
|
|
|
|
|
|
printf("verify : %s <-> %s\n", inpFilename, decFilename);
|
2018-02-01 10:48:20 +00:00
|
|
|
int const cmp = compareFiles(inpFp, decFp);
|
2017-08-09 19:29:38 +00:00
|
|
|
|
|
|
|
fclose(decFp);
|
|
|
|
fclose(inpFp);
|
2018-02-01 10:48:20 +00:00
|
|
|
|
|
|
|
if (cmp) {
|
|
|
|
printf("corruption detected : decompressed file differs from original\n");
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
printf("verify : OK\n");
|
2017-08-09 19:29:38 +00:00
|
|
|
}
|
2017-08-24 12:31:45 +00:00
|
|
|
|
|
|
|
return 0;
|
2015-05-21 13:17:21 +00:00
|
|
|
}
|