24 # define MIN_CLEVEL (int)(-5)
37 #include <sys/types.h>
48 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
50 #define MSG(...) fprintf(stderr, __VA_ARGS__)
52 #define CONTROL_MSG(c, ...) { \
61 static size_t checkBuffers(
const void* buff1,
const void* buff2,
size_t buffSize)
63 const char*
const ip1 = (
const char*)buff1;
64 const char*
const ip2 = (
const char*)buff2;
81 const int cLevelSpan = maxClevel - minCLevel;
82 size_t const hashLength =
MIN(16, refBuffSize);
83 unsigned const h32 =
XXH32(refBuff, hashLength, 0);
84 int const randL = h32 % (cLevelSpan+1);
86 return minCLevel + randL;
102 void* compressedBuff,
size_t compressedBuffCapacity,
103 const void* srcBuff,
size_t srcSize,
107 int const selected_clevel = proposed_clevel < 0 ? -proposed_clevel : proposed_clevel;
109 int const cSize =
compress((
const char*)srcBuff, (
char*)compressedBuff, (
int)
srcSize, (
int)compressedBuffCapacity, selected_clevel);
112 {
int const dSize =
LZ4_decompress_safe((
const char*)compressedBuff, (
char*)resultBuff, cSize, (
int)resultBuffCapacity);
113 CONTROL_MSG(dSize < 0,
"Decompression detected an error !");
114 CONTROL_MSG(dSize != (
int)
srcSize,
"Decompression corruption error : wrong decompressed size !");
121 "Silent decoding corruption, at pos %u !!!",
130 void*
const cBuff =
malloc(cBuffSize);
131 void*
const rBuff =
malloc(cBuffSize);
133 if (!cBuff || !rBuff) {
134 fprintf(stderr,
"not enough memory ! \n");
151 #if defined(_MSC_VER)
152 struct _stat64 statbuf;
153 r = _stat64(infilename, &statbuf);
154 if (
r || !(statbuf.st_mode & S_IFREG))
return 0;
157 r =
stat(infilename, &statbuf);
158 if (
r || !
S_ISREG(statbuf.st_mode))
return 0;
160 return (
size_t)statbuf.st_size;
167 #if defined(_MSC_VER)
168 struct _stat64 statbuf;
169 r = _stat64(infilename, &statbuf);
170 if (!
r && (statbuf.st_mode & _S_IFDIR))
return 1;
173 r =
stat(infilename, &statbuf);
174 if (!
r &&
S_ISDIR(statbuf.st_mode))
return 1;
193 {
size_t const readSize = fread(
buffer, 1, fileSize,
f);
194 if (readSize != fileSize) {
207 MSG(
"not enough memory \n");
219 MSG(
"bad usage: \n");
221 MSG(
"%s [Options] fileName \n", exeName);
224 MSG(
"-# : use #=[0-9] compression level (default:0 == random) \n");
231 const char*
const exeName =
argv[0];
236 if (argCount < 2)
return bad_usage(exeName);
238 if (
argv[1][0] ==
'-') {
239 clevel =
argv[1][1] -
'0';
243 if (argNb >= argCount)
return bad_usage(exeName);
246 MSG(
"no pb detected \n");
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
RZ_API void Ht_() free(HtName_(Ht) *ht)
void * malloc(size_t size)
static static fork const void static count static fd const char const char static newpath char char argv
XXH_PUBLIC_API unsigned int XXH32(const void *input, size_t len, unsigned int seed)
int LZ4_compressBound(int isize)
int LZ4_compress_fast(const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
LZ4_FORCE_O2 int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel)
assert(limit<=UINT32_MAX/2)
static void roundTripCheck(const void *srcBuff, size_t srcSize, int clevel)
static void fileCheck(const char *fileName, int clevel)
static size_t checkBuffers(const void *buff1, const void *buff2, size_t buffSize)
int main(int argCount, const char **argv)
int bad_usage(const char *exeName)
static size_t getFileSize(const char *infilename)
static void roundTripTest(void *resultBuff, size_t resultBuffCapacity, void *compressedBuff, size_t compressedBuffCapacity, const void *srcBuff, size_t srcSize, int clevel)
#define CONTROL_MSG(c,...)
static void loadFile(void *buffer, const char *fileName, size_t fileSize)
static int select_clevel(const void *refBuff, size_t refBuffSize)
int(* compressFn)(const char *src, char *dst, int srcSize, int dstSize, int cLevel)
static int isDirectory(const char *infilename)