2011-04-24 14:16:00 +00:00
|
|
|
/*
|
2011-09-13 13:30:45 +00:00
|
|
|
LZ4Demo - Demo CLI program using LZ4 compression
|
2012-05-13 17:59:10 +00:00
|
|
|
Copyright (C) Yann Collet 2011-2012
|
|
|
|
GPL v2 License
|
2011-09-13 13:30:45 +00:00
|
|
|
|
|
|
|
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 LZ4 is BSD.
|
2012-03-09 21:46:59 +00:00
|
|
|
The license of the demo program is GPL.
|
2011-05-21 07:18:49 +00:00
|
|
|
*/
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2012-08-13 07:30:58 +00:00
|
|
|
//**************************************
|
|
|
|
// Compiler Options
|
|
|
|
//**************************************
|
|
|
|
// Disable some Visual warning messages
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
|
|
#define _CRT_SECURE_NO_DEPRECATE // VS2005
|
2012-05-09 23:49:41 +00:00
|
|
|
|
|
|
|
|
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
|
2012-04-08 15:18:12 +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-05-13 17:59:10 +00:00
|
|
|
#include "lz4hc.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-03-16 19:50:58 +00:00
|
|
|
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
|
|
|
|
|
2012-04-08 15:18:12 +00:00
|
|
|
#if defined(_MSC_VER) // Visual Studio
|
2012-02-01 04:19:38 +00:00
|
|
|
#define swap32 _byteswap_ulong
|
2012-06-16 00:04:09 +00:00
|
|
|
#elif GCC_VERSION >= 430
|
2012-02-01 04:19:38 +00:00
|
|
|
#define swap32 __builtin_bswap32
|
2012-03-16 19:50:58 +00:00
|
|
|
#else
|
|
|
|
static inline unsigned int swap32(unsigned int x) {
|
|
|
|
return ((x << 24) & 0xff000000 ) |
|
|
|
|
((x << 8) & 0x00ff0000 ) |
|
|
|
|
((x >> 8) & 0x0000ff00 ) |
|
|
|
|
((x >> 24) & 0x000000ff );
|
|
|
|
}
|
2012-02-01 04:19:38 +00:00
|
|
|
#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"
|
|
|
|
#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
|
|
|
|
//****************************
|
2012-05-12 19:14:43 +00:00
|
|
|
int usage(char* exename)
|
2011-04-24 14:16:00 +00:00
|
|
|
{
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( "Usage :\n");
|
2012-05-12 19:14:43 +00:00
|
|
|
DISPLAY( " %s [arg] input output\n", exename);
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( "Arguments :\n");
|
2012-05-13 17:59:10 +00:00
|
|
|
DISPLAY( " -c0: Fast compression (default) \n");
|
|
|
|
DISPLAY( " -c1: High compression \n");
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY( " -d : decompression \n");
|
2012-05-13 17:59:10 +00:00
|
|
|
DISPLAY( " -b#: Benchmark files, using # compression level\n");
|
2012-01-08 02:45:32 +00:00
|
|
|
DISPLAY( " -t : check compressed file \n");
|
2012-04-08 15:18:12 +00:00
|
|
|
DISPLAY( " -h : help (this text)\n");
|
2011-09-27 11:59:58 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-12 19:14:43 +00:00
|
|
|
int badusage(char* exename)
|
2011-04-24 14:16:00 +00:00
|
|
|
{
|
2011-09-27 11:59:58 +00:00
|
|
|
DISPLAY("Wrong parameters\n");
|
2012-05-12 19:14:43 +00:00
|
|
|
usage(exename);
|
2011-04-24 14:16:00 +00:00
|
|
|
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
|
|
|
}
|
2012-04-08 15:18:12 +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
|
|
|
|
|
|
|
|
2012-05-13 17:59:10 +00:00
|
|
|
int compress_file(char* input_filename, char* output_filename, int compressionlevel)
|
2011-09-27 11:59:58 +00:00
|
|
|
{
|
2012-05-13 17:59:10 +00:00
|
|
|
int (*compressionFunction)(const char*, char*, int);
|
2011-09-27 11:59:58 +00:00
|
|
|
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;
|
2012-05-13 17:59:10 +00:00
|
|
|
int displayLevel = (compressionlevel>0);
|
2011-09-27 11:59:58 +00:00
|
|
|
clock_t start, end;
|
|
|
|
|
|
|
|
|
|
|
|
// Init
|
2012-05-13 17:59:10 +00:00
|
|
|
switch (compressionlevel)
|
|
|
|
{
|
|
|
|
case 0 : compressionFunction = LZ4_compress; break;
|
|
|
|
case 1 : compressionFunction = LZ4_compressHC; break;
|
|
|
|
default : compressionFunction = LZ4_compress;
|
|
|
|
}
|
2011-09-27 11:59:58 +00:00
|
|
|
start = clock();
|
|
|
|
r = get_fileHandle(input_filename, output_filename, &finput, &foutput);
|
|
|
|
if (r) return r;
|
2012-04-08 15:18:12 +00:00
|
|
|
|
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; }
|
2012-04-08 15:18:12 +00:00
|
|
|
|
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
|
2012-04-08 15:18:12 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2011-04-24 14:16:00 +00:00
|
|
|
int outSize;
|
|
|
|
// Read Block
|
2012-08-13 07:30:58 +00:00
|
|
|
int inSize = (int) fread(in_buff, (size_t)1, (size_t)CHUNKSIZE, finput);
|
2011-04-26 23:49:24 +00:00
|
|
|
if( inSize<=0 ) break;
|
|
|
|
filesize += inSize;
|
2012-05-13 17:59:10 +00:00
|
|
|
if (displayLevel) DISPLAY("Read : %i MB \r", (int)(filesize>>20));
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Compress Block
|
2012-05-13 17:59:10 +00:00
|
|
|
outSize = compressionFunction(in_buff, out_buff+4, inSize);
|
2011-05-21 07:18:49 +00:00
|
|
|
compressedfilesize += outSize+4;
|
2012-05-13 17:59:10 +00:00
|
|
|
if (displayLevel) DISPLAY("Read : %i MB ==> %.2f%%\r", (int)(filesize>>20), (double)compressedfilesize/filesize*100);
|
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();
|
2012-04-08 15:18:12 +00:00
|
|
|
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;
|
2012-05-12 19:14:43 +00:00
|
|
|
unsigned int chunkSize;
|
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; }
|
2012-04-08 15:18:12 +00:00
|
|
|
|
2011-04-24 14:16:00 +00:00
|
|
|
// Check Archive Header
|
2012-05-12 19:14:43 +00:00
|
|
|
chunkSize = 0;
|
|
|
|
uselessRet = fread(&chunkSize, 1, ARCHIVE_MAGICNUMBER_SIZE, finput);
|
|
|
|
LITTLE_ENDIAN32(chunkSize);
|
|
|
|
if (chunkSize != ARCHIVE_MAGICNUMBER) { DISPLAY("Unrecognized header : file cannot be decoded\n"); return 6; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Main Loop
|
2012-04-08 15:18:12 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2012-05-12 19:14:43 +00:00
|
|
|
// Block Size
|
|
|
|
uselessRet = fread(&chunkSize, 1, 4, finput);
|
|
|
|
if( uselessRet==0 ) break; // Nothing to read : file read is completed
|
|
|
|
LITTLE_ENDIAN32(chunkSize);
|
|
|
|
if (chunkSize == ARCHIVE_MAGICNUMBER)
|
|
|
|
continue; // appended compressed stream
|
|
|
|
|
2011-04-24 14:16:00 +00:00
|
|
|
// Read Block
|
2012-05-12 19:14:43 +00:00
|
|
|
uselessRet = fread(in_buff, 1, chunkSize, finput);
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Decode Block
|
2012-05-12 19:14:43 +00:00
|
|
|
sinkint = LZ4_uncompress_unknownOutputSize(in_buff, out_buff, chunkSize, CHUNKSIZE);
|
2012-02-01 04:19:38 +00:00
|
|
|
if (sinkint < 0) { DISPLAY("Decoding Failed ! Corrupted input !\n"); return 9; }
|
2012-05-12 19:14:43 +00:00
|
|
|
filesize += sinkint;
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// Write Block
|
2012-05-12 19:14:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-08 15:18:12 +00:00
|
|
|
int main(int argc, char** argv)
|
2011-04-24 14:16:00 +00:00
|
|
|
{
|
|
|
|
int i,
|
2012-05-13 17:59:10 +00:00
|
|
|
cLevel=0,
|
2012-01-08 02:45:32 +00:00
|
|
|
decode=0,
|
2012-02-20 20:53:49 +00:00
|
|
|
bench=0,
|
|
|
|
filenamesStart=2;
|
2012-05-12 19:14:43 +00:00
|
|
|
char* exename=argv[0];
|
2011-09-25 21:34:35 +00:00
|
|
|
char* input_filename=0;
|
|
|
|
char* output_filename=0;
|
2012-04-08 15:18:12 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-25 21:34:35 +00:00
|
|
|
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
|
|
|
|
2012-05-12 19:14:43 +00:00
|
|
|
if (argc<2) { badusage(exename); return 1; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
for(i=1; i<argc; i++)
|
|
|
|
{
|
|
|
|
char* argument = argv[i];
|
|
|
|
|
|
|
|
if(!argument) continue; // Protection if argument empty
|
|
|
|
|
|
|
|
// Select command
|
2012-02-20 20:53:49 +00:00
|
|
|
if (argument[0]=='-')
|
2011-04-24 14:16:00 +00:00
|
|
|
{
|
2012-02-20 20:53:49 +00:00
|
|
|
argument ++;
|
2012-04-08 15:18:12 +00:00
|
|
|
|
2011-09-25 21:34:35 +00:00
|
|
|
// Display help on usage
|
2012-05-12 19:14:43 +00:00
|
|
|
if ( argument[0] =='h' ) { usage(exename); return 0; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2011-09-25 21:34:35 +00:00
|
|
|
// Compression (default)
|
2012-05-13 17:59:10 +00:00
|
|
|
if ( argument[0] =='c' ) { if (argument[1] >='0') cLevel=argument[1] - '0'; 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
|
2012-05-13 17:59:10 +00:00
|
|
|
if ( argument[0] =='b' ) { bench=1; if (argument[1] >= '0') cLevel=argument[1] - '0'; continue; }
|
2012-01-08 02:45:32 +00:00
|
|
|
|
2012-02-20 20:53:49 +00:00
|
|
|
// Modify Block Size (benchmark only)
|
|
|
|
if ( argument[0] =='B' ) { int B = argument[1] - '0'; int S = 1 << (10 + 2*B); BMK_SetBlocksize(S); continue; }
|
|
|
|
|
2012-03-09 21:46:59 +00:00
|
|
|
// Modify Nb Iterations (benchmark only)
|
|
|
|
if ( argument[0] =='i' ) { int iters = argument[1] - '0'; BMK_SetNbIterations(iters); 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
|
2012-02-20 20:53:49 +00:00
|
|
|
if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
// second provided filename is output
|
2012-04-08 15:18:12 +00:00
|
|
|
if (!output_filename)
|
|
|
|
{
|
|
|
|
output_filename=argument;
|
2011-09-25 21:34:35 +00:00
|
|
|
if (!strcmp (output_filename, nullinput)) output_filename = nulmark;
|
2012-04-08 15:18:12 +00:00
|
|
|
continue;
|
2011-09-25 21:34:35 +00:00
|
|
|
}
|
2011-04-24 14:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No input filename ==> Error
|
2012-05-12 19:14:43 +00:00
|
|
|
if(!input_filename) { badusage(exename); return 1; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
2012-05-13 17:59:10 +00:00
|
|
|
if (bench) return BMK_benchFile(argv+filenamesStart, argc-filenamesStart, cLevel);
|
2012-01-08 02:45:32 +00:00
|
|
|
|
2012-05-13 17:59:10 +00:00
|
|
|
// No output filename ==> Error
|
2012-05-12 19:14:43 +00:00
|
|
|
if (!output_filename) { badusage(exename); return 1; }
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
if (decode) return decode_file(input_filename, output_filename);
|
|
|
|
|
2012-05-13 17:59:10 +00:00
|
|
|
return compress_file(input_filename, output_filename, cLevel); // Compression is 'default' action
|
2011-04-24 14:16:00 +00:00
|
|
|
|
|
|
|
}
|