36
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
38
#define MSG(...) fprintf(stderr, __VA_ARGS__)
40
#define CONTROL_MSG(c, ...) { \
49
static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize)
51
const char* const ip1 = (const char*)buff1;
52
const char* const ip2 = (const char*)buff2;
55
for (pos=0; pos<buffSize; pos++)
56
if (ip1[pos]!=ip2[pos])
63
LZ4_stream_t LZ4_cState;
64
LZ4_streamDecode_t LZ4_dState;
74
static void roundTripTest(void* resultBuff, size_t resultBuffCapacity,
75
void* compressedBuff, size_t compressedBuffCapacity,
76
const void* srcBuff, size_t srcSize)
78
int const acceleration = 1;
80
memset(&LZ4_cState, 0, sizeof(LZ4_cState));
81
{ int const cSize = LZ4_compress_fast_continue(&LZ4_cState, (const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, acceleration);
82
CONTROL_MSG(cSize == 0, "Compression error !");
83
{ int const dInit = LZ4_setStreamDecode(&LZ4_dState, NULL, 0);
84
CONTROL_MSG(dInit == 0, "LZ4_setStreamDecode error !");
86
{ int const dSize = LZ4_decompress_safe_continue (&LZ4_dState, (const char*)compressedBuff, (char*)resultBuff, cSize, (int)resultBuffCapacity);
87
CONTROL_MSG(dSize < 0, "Decompression detected an error !");
88
CONTROL_MSG(dSize != (int)srcSize, "Decompression corruption error : wrong decompressed size !");
92
assert(resultBuffCapacity >= srcSize);
93
{ size_t const errorPos = checkBuffers(srcBuff, resultBuff, srcSize);
94
CONTROL_MSG(errorPos != srcSize,
95
"Silent decoding corruption, at pos %u !!!",
100
static void roundTripCheck(const void* srcBuff, size_t srcSize)
102
size_t const cBuffSize = LZ4_COMPRESSBOUND(srcSize);
103
void* const cBuff = malloc(cBuffSize);
104
void* const rBuff = malloc(cBuffSize);
106
if (!cBuff || !rBuff) {
107
fprintf(stderr, "not enough memory ! \n");
111
roundTripTest(rBuff, cBuffSize,
120
static size_t getFileSize(const char* infilename)
124
struct _stat64 statbuf;
125
r = _stat64(infilename, &statbuf);
126
if (r || !(statbuf.st_mode & S_IFREG)) return 0;
129
r = stat(infilename, &statbuf);
130
if (r || !S_ISREG(statbuf.st_mode)) return 0;
132
return (size_t)statbuf.st_size;
136
static int isDirectory(const char* infilename)
140
struct _stat64 statbuf;
141
r = _stat64(infilename, &statbuf);
142
if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
145
r = stat(infilename, &statbuf);
146
if (!r && S_ISDIR(statbuf.st_mode)) return 1;
154
static void loadFile(void* buffer, const char* fileName, size_t fileSize)
156
FILE* const f = fopen(fileName, "rb");
157
if (isDirectory(fileName)) {
158
MSG("Ignoring %s directory \n", fileName);
162
MSG("Impossible to open %s \n", fileName);
165
{ size_t const readSize = fread(buffer, 1, fileSize, f);
166
if (readSize != fileSize) {
167
MSG("Error reading %s \n", fileName);
174
static void fileCheck(const char* fileName)
176
size_t const fileSize = getFileSize(fileName);
177
void* const buffer = malloc(fileSize + !fileSize );
179
MSG("not enough memory \n");
182
loadFile(buffer, fileName, fileSize);
183
roundTripCheck(buffer, fileSize);
188
int bad_usage(const char* exeName)
191
MSG("bad usage: \n");
193
MSG("%s [Options] fileName \n", exeName);
196
MSG("-# : use #=[0-9] compression level (default:0 == random) \n");
201
int main(int argCount, const char** argv)
203
const char* const exeName = argv[0];
206
MSG("abiTest, built binary based on API %s \n", LZ4_VERSION_STRING);
208
MSG("currently linked to dll %s \n", LZ4_versionString());
210
assert(argCount >= 1);
211
if (argCount < 2) return bad_usage(exeName);
213
if (argNb >= argCount) return bad_usage(exeName);
215
fileCheck(argv[argNb]);
216
MSG("no pb detected \n");