2011-04-24 14:16:00 +00:00
|
|
|
/*
|
2011-09-13 13:30:45 +00:00
|
|
|
LZ4Demo - Demo CLI program using LZ4 compression
|
|
|
|
Copyright (C) Yann Collet 2011,
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
You can contact the author at :
|
|
|
|
- LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
|
|
|
|
- LZ4 source repository : http://code.google.com/p/lz4/
|
2011-04-24 14:16:00 +00:00
|
|
|
*/
|
2011-05-21 07:18:49 +00:00
|
|
|
/*
|
|
|
|
Note : this is *only* a demo program, an example to show how LZ4 can be used.
|
|
|
|
It is not considered part of LZ4 compression library.
|
|
|
|
The license of the demo program is GPL.
|
|
|
|
The license of LZ4 is BSD.
|
|
|
|
*/
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
//****************************
|
|
|
|
// Includes
|
|
|
|
//****************************
|
2011-11-19 21:38:27 +00:00
|
|
|
#include <stdio.h> // fprintf, fopen, fread, _fileno(?)
|
2011-09-25 21:34:35 +00:00
|
|
|
#include <stdlib.h> // malloc
|
|
|
|
#include <string.h> // strcmp
|
2011-09-27 11:59:58 +00:00
|
|
|
#include <time.h> // clock
|
2011-09-03 16:06:26 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-25 21:34:35 +00:00
|
|
|
#include <io.h> // _setmode
|
|
|
|
#include <fcntl.h> // _O_BINARY
|
2011-09-03 16:06:26 +00:00
|
|
|
#endif
|
2011-04-24 14:16:00 +00:00
|
|
|
#include "lz4.h"
|
2012-01-08 02:45:32 +00:00
|
|
|
#include "bench.h"
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
|
2011-05-25 22:25:57 +00:00
|
|
|
//**************************************
|
2012-02-01 04:19:38 +00:00
|
|
|
// Compiler functions
|
2011-05-25 22:25:57 +00:00
|
|
|
//**************************************
|
2012-02-01 04:19:38 +00:00
|
|
|
#if defined(_MSC_VER) // Visual Studio
|
|
|
|
#define swap32 _byteswap_ulong
|
|
|
|
#else // GCC assumed
|
|
|
|
#define swap32 __builtin_bswap32
|
|
|
|
#endif
|
2011-05-25 22:25:57 +00:00
|
|
|
|
|
|
|
|
2011-04-24 14:16:00 +00:00
|
|
|
//****************************
|
|
|
|
// Constants
|
|
|
|
//****************************
|
2011-09-03 16:06:26 +00:00
|
|
|
#define COMPRESSOR_NAME "Compression CLI using LZ4 algorithm"
|
2011-08-21 11:42:08 +00:00
|
|
|
#define COMPRESSOR_VERSION ""
|
2011-04-24 14:16:00 +00:00
|
|
|
#define COMPILED __DATE__
|
|
|
|
#define AUTHOR "Yann Collet"
|
2011-09-27 11:59:58 +00:00
|
|
|
#define BINARY_NAME "lz4demo.exe"
|
2011-04-24 14:16:00 +00:00
|
|
|
#define EXTENSION ".lz4"
|
|
|
|
#define WELCOME_MESSAGE "*** %s %s, by %s (%s) ***\n", COMPRESSOR_NAME, COMPRESSOR_VERSION, AUTHOR, COMPILED
|
|
|
|
|
|
|
|
#define CHUNKSIZE (8<<20) // 8 MB
|
|
|
|
#define CACHELINE 64
|
|
|
|
#define ARCHIVE_MAGICNUMBER 0x184C2102
|
|
|
|
#define ARCHIVE_MAGICNUMBER_SIZE 4
|
|
|
|
|
|
|
|
|
2011-09-27 11:59:58 +00:00
|
|
|
//**************************************
|
2012-02-01 04:19:38 +00:00
|
|
|
// Architecture Macros
|
2011-09-27 11:59:58 +00:00
|
|
|
//**************************************
|
2012-02-01 04:19:38 +00:00
|
|
|
static const int one = 1;
|
|
|
|
#define CPU_LITTLE_ENDIAN (*(char*)(&one))
|
|
|
|
#define CPU_BIG_ENDIAN (!CPU_LITTLE_ENDIAN)
|
|
|
|
#define LITTLE_ENDIAN32(i) if (CPU_BIG_ENDIAN) { i = swap32(i); }
|
2011-09-27 11:59:58 +00:00
|
|
|
|
|
|
|
|
2012-02-01 04:19:38 +00:00
|
|
|
//**************************************
|
|
|
|
// Macros
|
|
|
|
//**************************************
|
|
|
|
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
//****************************
|
|
|
|
// Functions
|
|
|
|
//****************************
|
|
|
|
int usage()
|
|
|
|
{
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( "Usage :\n");
|
2012-02-01 04:19:38 +00:00
|
|
|
DISPLAY( " %s [arg] input output\n", BINARY_NAME);
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( "Arguments :\n");
|
|
|
|
DISPLAY( " -c : compression (default)\n");
|
|
|
|
DISPLAY( " -d : decompression \n");
|
2012-01-08 02:45:32 +00:00
|
|
|
DISPLAY( " -b : benchmark with files\n");
|
|
|
|
DISPLAY( " -t : check compressed file \n");
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( " -h : help (this text)\n");
|
|
|
|
DISPLAY( "input : can be 'stdin' (pipe) or a filename\n");
|
|
|
|
DISPLAY( "output : can be 'stdout'(pipe) or a filename or 'null'\n");
|
2011-04-24 14:16:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int badusage()
|
|
|
|
{
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY("Wrong parameters\n");
|
2011-04-24 14:16:00 +00:00
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-27 11:59:58 +00:00
|
|
|
|
|
|
|
int get_fileHandle(char* input_filename, char* output_filename, FILE** pfinput, FILE** pfoutput)
|
2011-04-24 14:16:00 +00:00
|
|
|
{
|
2011-08-30 22:35:09 +00:00
|
|
|
char stdinmark[] = "stdin";
|
|
|
|
char stdoutmark[] = "stdout";
|
|
|
|
|
|
|
|
if (!strcmp (input_filename, stdinmark)) {
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( "Using stdin for input\n");
|
|
|
|
*pfinput = stdin;
|
2011-09-25 20:11:05 +00:00
|
|
|
#ifdef _WIN32 // Need to set stdin/stdout to binary mode specifically for windows
|
2011-09-03 16:06:26 +00:00
|
|
|
_setmode( _fileno( stdin ), _O_BINARY );
|
|
|
|
#endif
|
2011-08-30 22:35:09 +00:00
|
|
|
} else {
|
2011-09-27 11:59:58 +00:00
|
|
|
*pfinput = fopen( input_filename, "rb" );
|
2011-08-30 22:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp (output_filename, stdoutmark)) {
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( "Using stdout for output\n");
|
|
|
|
*pfoutput = stdout;
|
2011-09-25 20:11:05 +00:00
|
|
|
#ifdef _WIN32 // Need to set stdin/stdout to binary mode specifically for windows
|
2011-09-03 16:06:26 +00:00
|
|
|
_setmode( _fileno( stdout ), _O_BINARY );
|
|
|
|
#endif
|
2011-08-30 22:35:09 +00:00
|
|
|
} else {
|
2011-09-27 11:59:58 +00:00
|
|
|
*pfoutput = fopen( output_filename, "wb" );
|
2011-08-30 22:35:09 +00:00
|
|
|
}
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2011-09-27 11:59:58 +00:00
|
|
|
if ( *pfinput==0 ) { DISPLAY( "Pb opening %s\n", input_filename); return 2; }
|
|
|
|
if ( *pfoutput==0) { DISPLAY( "Pb opening %s\n", output_filename); return 3; }
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2011-09-27 11:59:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
int compress_file(char* input_filename, char* output_filename)
|
|
|
|
{
|
|
|
|
unsigned long long filesize = 0;
|
|
|
|
unsigned long long compressedfilesize = ARCHIVE_MAGICNUMBER_SIZE;
|
2012-02-01 04:19:38 +00:00
|
|
|
unsigned int u32var;
|
2011-09-27 11:59:58 +00:00
|
|
|
char* in_buff;
|
|
|
|
char* out_buff;
|
|
|
|
FILE* finput;
|
|
|
|
FILE* foutput;
|
|
|
|
int r;
|
|
|
|
clock_t start, end;
|
|
|
|
|
|
|
|
|
|
|
|
// Init
|
|
|
|
start = clock();
|
|
|
|
r = get_fileHandle(input_filename, output_filename, &finput, &foutput);
|
|
|
|
if (r) return r;
|
|
|
|
|
2011-04-24 14:16:00 +00:00
|
|
|
// Allocate Memory
|
2011-09-27 11:59:58 +00:00
|
|
|
in_buff = (char*)malloc(CHUNKSIZE);
|
2012-02-16 18:39:50 +00:00
|
|
|
out_buff = (char*)malloc(LZ4_compressBound(CHUNKSIZE));
|
2011-09-27 11:59:58 +00:00
|
|
|
if (!in_buff || !out_buff) { DISPLAY("Allocation error : not enough memory\n"); return 8; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Write Archive Header
|
2012-02-01 04:19:38 +00:00
|
|
|
u32var = ARCHIVE_MAGICNUMBER;
|
|
|
|
LITTLE_ENDIAN32(u32var);
|
|
|
|
*(unsigned int*)out_buff = u32var;
|
2011-04-24 14:16:00 +00:00
|
|
|
fwrite(out_buff, 1, ARCHIVE_MAGICNUMBER_SIZE, foutput);
|
|
|
|
|
|
|
|
// Main Loop
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int outSize;
|
|
|
|
// Read Block
|
2011-05-25 22:25:57 +00:00
|
|
|
int inSize = fread(in_buff, 1, CHUNKSIZE, finput);
|
2011-04-26 23:49:24 +00:00
|
|
|
if( inSize<=0 ) break;
|
|
|
|
filesize += inSize;
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Compress Block
|
|
|
|
outSize = LZ4_compress(in_buff, out_buff+4, inSize);
|
2011-05-21 07:18:49 +00:00
|
|
|
compressedfilesize += outSize+4;
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Write Block
|
2012-02-01 04:19:38 +00:00
|
|
|
LITTLE_ENDIAN32(outSize);
|
|
|
|
* (unsigned int*) out_buff = outSize;
|
|
|
|
LITTLE_ENDIAN32(outSize);
|
2011-04-24 14:16:00 +00:00
|
|
|
fwrite(out_buff, 1, outSize+4, foutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status
|
2011-09-27 11:59:58 +00:00
|
|
|
end = clock();
|
|
|
|
DISPLAY( "Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
|
2011-05-25 22:25:57 +00:00
|
|
|
(unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100);
|
2011-09-27 11:59:58 +00:00
|
|
|
{
|
|
|
|
double seconds = (double)(end - start)/CLOCKS_PER_SEC;
|
|
|
|
DISPLAY( "Done in %.2f s ==> %.2f MB/s\n", seconds, (double)filesize / seconds / 1024 / 1024);
|
|
|
|
}
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2011-09-27 11:59:58 +00:00
|
|
|
// Close & Free
|
|
|
|
free(in_buff);
|
|
|
|
free(out_buff);
|
2011-04-24 14:16:00 +00:00
|
|
|
fclose(finput);
|
|
|
|
fclose(foutput);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-25 22:25:57 +00:00
|
|
|
int decode_file(char* input_filename, char* output_filename)
|
2011-04-24 14:16:00 +00:00
|
|
|
{
|
2011-09-27 11:59:58 +00:00
|
|
|
unsigned long long filesize = 0;
|
2011-04-24 14:16:00 +00:00
|
|
|
char* in_buff;
|
|
|
|
char* out_buff;
|
2011-05-25 22:25:57 +00:00
|
|
|
size_t uselessRet;
|
2011-06-04 17:15:43 +00:00
|
|
|
int sinkint;
|
2011-09-27 20:47:20 +00:00
|
|
|
unsigned int nextSize;
|
2011-08-30 22:35:09 +00:00
|
|
|
FILE* finput;
|
|
|
|
FILE* foutput;
|
2011-09-27 11:59:58 +00:00
|
|
|
clock_t start, end;
|
|
|
|
int r;
|
2011-08-30 22:35:09 +00:00
|
|
|
|
|
|
|
|
2011-09-27 11:59:58 +00:00
|
|
|
// Init
|
|
|
|
start = clock();
|
|
|
|
r = get_fileHandle(input_filename, output_filename, &finput, &foutput);
|
|
|
|
if (r) return r;
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Allocate Memory
|
2012-02-16 18:39:50 +00:00
|
|
|
in_buff = (char*)malloc(LZ4_compressBound(CHUNKSIZE));
|
2011-09-27 11:59:58 +00:00
|
|
|
out_buff = (char*)malloc(CHUNKSIZE);
|
|
|
|
if (!in_buff || !out_buff) { DISPLAY("Allocation error : not enough memory\n"); return 7; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Check Archive Header
|
2011-05-25 22:25:57 +00:00
|
|
|
uselessRet = fread(out_buff, 1, ARCHIVE_MAGICNUMBER_SIZE, finput);
|
2012-02-01 04:19:38 +00:00
|
|
|
nextSize = *(unsigned int*)out_buff;
|
|
|
|
LITTLE_ENDIAN32(nextSize);
|
|
|
|
if (nextSize != ARCHIVE_MAGICNUMBER) { DISPLAY("Unrecognized header : file cannot be decoded\n"); return 6; }
|
|
|
|
|
|
|
|
// First Block
|
|
|
|
*(unsigned int*)in_buff = 0;
|
2011-06-04 17:15:43 +00:00
|
|
|
uselessRet = fread(in_buff, 1, 4, finput);
|
2012-02-01 04:19:38 +00:00
|
|
|
nextSize = *(unsigned int*)in_buff;
|
|
|
|
LITTLE_ENDIAN32(nextSize);
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Main Loop
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
// Read Block
|
2011-06-04 17:15:43 +00:00
|
|
|
uselessRet = fread(in_buff, 1, nextSize, finput);
|
|
|
|
|
|
|
|
// Check Next Block
|
2012-02-01 04:19:38 +00:00
|
|
|
uselessRet = (size_t) fread(&nextSize, 1, 4, finput);
|
|
|
|
if( uselessRet==0 ) break; // Nothing read : file read is completed
|
|
|
|
LITTLE_ENDIAN32(nextSize);
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Decode Block
|
2011-06-04 17:15:43 +00:00
|
|
|
sinkint = LZ4_uncompress(in_buff, out_buff, CHUNKSIZE);
|
2012-02-01 04:19:38 +00:00
|
|
|
if (sinkint < 0) { DISPLAY("Decoding Failed ! Corrupted input !\n"); return 9; }
|
2011-06-04 17:15:43 +00:00
|
|
|
filesize += CHUNKSIZE;
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Write Block
|
2011-06-04 17:15:43 +00:00
|
|
|
fwrite(out_buff, 1, CHUNKSIZE, foutput);
|
2011-04-24 14:16:00 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 04:19:38 +00:00
|
|
|
// Last Block (which size is <= CHUNKSIZE, but let LZ4 figure that out)
|
2011-06-04 17:15:43 +00:00
|
|
|
uselessRet = fread(in_buff, 1, nextSize, finput);
|
|
|
|
sinkint = LZ4_uncompress_unknownOutputSize(in_buff, out_buff, nextSize, CHUNKSIZE);
|
|
|
|
filesize += sinkint;
|
|
|
|
fwrite(out_buff, 1, sinkint, foutput);
|
|
|
|
|
2011-04-24 14:16:00 +00:00
|
|
|
// Status
|
2011-09-27 11:59:58 +00:00
|
|
|
end = clock();
|
|
|
|
DISPLAY( "Successfully decoded %llu bytes \n", (unsigned long long)filesize);
|
|
|
|
{
|
|
|
|
double seconds = (double)(end - start)/CLOCKS_PER_SEC;
|
|
|
|
DISPLAY( "Done in %.2f s ==> %.2f MB/s\n", seconds, (double)filesize / seconds / 1024 / 1024);
|
|
|
|
}
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2011-09-27 11:59:58 +00:00
|
|
|
// Close & Free
|
|
|
|
free(in_buff);
|
|
|
|
free(out_buff);
|
2011-04-24 14:16:00 +00:00
|
|
|
fclose(finput);
|
|
|
|
fclose(foutput);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
compression=1, // default action if no argument
|
2012-01-08 02:45:32 +00:00
|
|
|
decode=0,
|
|
|
|
bench=0;
|
2011-09-25 21:34:35 +00:00
|
|
|
char* input_filename=0;
|
|
|
|
char* output_filename=0;
|
|
|
|
#ifdef _WIN32
|
|
|
|
char nulmark[] = "nul";
|
|
|
|
#else
|
|
|
|
char nulmark[] = "/dev/null";
|
|
|
|
#endif
|
|
|
|
char nullinput[] = "null";
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Welcome message
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( WELCOME_MESSAGE);
|
2011-05-25 22:25:57 +00:00
|
|
|
|
2011-04-24 14:16:00 +00:00
|
|
|
if (argc<2) { badusage(); return 1; }
|
|
|
|
|
|
|
|
for(i=1; i<argc; i++)
|
|
|
|
{
|
|
|
|
char* argument = argv[i];
|
|
|
|
char command = 0;
|
|
|
|
|
|
|
|
if(!argument) continue; // Protection if argument empty
|
|
|
|
|
|
|
|
if (argument[0]=='-') command++; // valid command trigger
|
|
|
|
|
|
|
|
// Select command
|
|
|
|
if (command)
|
|
|
|
{
|
|
|
|
argument += command;
|
|
|
|
|
2011-09-25 21:34:35 +00:00
|
|
|
// Display help on usage
|
2011-08-21 11:42:08 +00:00
|
|
|
if ( argument[0] =='h' ) { usage(); return 0; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2011-09-25 21:34:35 +00:00
|
|
|
// Compression (default)
|
2011-08-21 11:42:08 +00:00
|
|
|
if ( argument[0] =='c' ) { compression=1; continue; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2011-09-25 21:34:35 +00:00
|
|
|
// Decoding
|
2011-08-21 11:42:08 +00:00
|
|
|
if ( argument[0] =='d' ) { decode=1; continue; }
|
2011-09-25 21:34:35 +00:00
|
|
|
|
2012-01-08 02:45:32 +00:00
|
|
|
// Bench
|
|
|
|
if ( argument[0] =='b' ) { bench=1; continue; }
|
|
|
|
|
2011-09-25 21:34:35 +00:00
|
|
|
// Test
|
|
|
|
if ( argument[0] =='t' ) { decode=1; output_filename=nulmark; continue; }
|
2011-04-24 14:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// first provided filename is input
|
|
|
|
if (!input_filename) { input_filename=argument; continue; }
|
|
|
|
|
|
|
|
// second provided filename is output
|
2011-09-25 21:34:35 +00:00
|
|
|
if (!output_filename)
|
|
|
|
{
|
|
|
|
output_filename=argument;
|
|
|
|
if (!strcmp (output_filename, nullinput)) output_filename = nulmark;
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-24 14:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No input filename ==> Error
|
|
|
|
if(!input_filename) { badusage(); return 1; }
|
|
|
|
|
2012-01-08 02:45:32 +00:00
|
|
|
if (bench) return BMK_benchFile(argv+2, argc-2);
|
|
|
|
|
2011-04-24 14:16:00 +00:00
|
|
|
// No output filename
|
|
|
|
if (!output_filename) { badusage(); return 1; }
|
|
|
|
|
|
|
|
if (decode) return decode_file(input_filename, output_filename);
|
|
|
|
|
|
|
|
if (compression) return compress_file(input_filename, output_filename);
|
|
|
|
|
2011-05-25 22:25:57 +00:00
|
|
|
badusage();
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|