Rizin
unix-like reverse engineering framework and cli tools
roundTripTest.c File Reference
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "xxhash.h"
#include "lz4.h"
#include "lz4hc.h"

Go to the source code of this file.

Macros

#define MIN_CLEVEL   (int)(-5)
 
#define MIN(a, b)   ( (a) < (b) ? (a) : (b) )
 
#define MSG(...)   fprintf(stderr, __VA_ARGS__)
 
#define CONTROL_MSG(c, ...)
 

Typedefs

typedef int(* compressFn) (const char *src, char *dst, int srcSize, int dstSize, int cLevel)
 

Functions

static size_t checkBuffers (const void *buff1, const void *buff2, size_t buffSize)
 
static int select_clevel (const void *refBuff, size_t refBuffSize)
 
static void roundTripTest (void *resultBuff, size_t resultBuffCapacity, void *compressedBuff, size_t compressedBuffCapacity, const void *srcBuff, size_t srcSize, int clevel)
 
static void roundTripCheck (const void *srcBuff, size_t srcSize, int clevel)
 
static size_t getFileSize (const char *infilename)
 
static int isDirectory (const char *infilename)
 
static void loadFile (void *buffer, const char *fileName, size_t fileSize)
 
static void fileCheck (const char *fileName, int clevel)
 
int bad_usage (const char *exeName)
 
int main (int argCount, const char **argv)
 

Macro Definition Documentation

◆ CONTROL_MSG

#define CONTROL_MSG (   c,
  ... 
)
Value:
{ \
if ((c)) { \
MSG(__VA_ARGS__); \
MSG(" \n"); \
abort(); \
} \
}
#define c(i)
Definition: sha256.c:43

Definition at line 52 of file roundTripTest.c.

◆ MIN

#define MIN (   a,
  b 
)    ( (a) < (b) ? (a) : (b) )

Definition at line 48 of file roundTripTest.c.

◆ MIN_CLEVEL

#define MIN_CLEVEL   (int)(-5)

Definition at line 24 of file roundTripTest.c.

◆ MSG

#define MSG (   ...)    fprintf(stderr, __VA_ARGS__)

Definition at line 50 of file roundTripTest.c.

Typedef Documentation

◆ compressFn

typedef int(* compressFn) (const char *src, char *dst, int srcSize, int dstSize, int cLevel)

Definition at line 90 of file roundTripTest.c.

Function Documentation

◆ bad_usage()

int bad_usage ( const char *  exeName)

Definition at line 216 of file roundTripTest.c.

217 {
218  MSG(" \n");
219  MSG("bad usage: \n");
220  MSG(" \n");
221  MSG("%s [Options] fileName \n", exeName);
222  MSG(" \n");
223  MSG("Options: \n");
224  MSG("-# : use #=[0-9] compression level (default:0 == random) \n");
225  return 1;
226 }
#define MSG(...)
Definition: roundTripTest.c:50

References MSG.

Referenced by main().

◆ checkBuffers()

static size_t checkBuffers ( const void *  buff1,
const void *  buff2,
size_t  buffSize 
)
static

Definition at line 61 of file roundTripTest.c.

62 {
63  const char* const ip1 = (const char*)buff1;
64  const char* const ip2 = (const char*)buff2;
65  size_t pos;
66 
67  for (pos=0; pos<buffSize; pos++)
68  if (ip1[pos]!=ip2[pos])
69  break;
70 
71  return pos;
72 }
int pos
Definition: main.c:11

References pos.

Referenced by roundTripTest().

◆ fileCheck()

static void fileCheck ( const char *  fileName,
int  clevel 
)
static

Definition at line 202 of file roundTripTest.c.

203 {
204  size_t const fileSize = getFileSize(fileName);
205  void* const buffer = malloc(fileSize + !fileSize /* avoid 0 */);
206  if (!buffer) {
207  MSG("not enough memory \n");
208  exit(4);
209  }
210  loadFile(buffer, fileName, fileSize);
211  roundTripCheck(buffer, fileSize, clevel);
212  free (buffer);
213 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * malloc(size_t size)
Definition: malloc.c:123
static void roundTripCheck(const void *srcBuff, size_t srcSize, int clevel)
static size_t getFileSize(const char *infilename)
static void loadFile(void *buffer, const char *fileName, size_t fileSize)
Definition: buffer.h:15

References test-lz4-list::exit, test-lz4-speed::fileName, free(), getFileSize(), loadFile(), malloc(), MSG, and roundTripCheck().

Referenced by main().

◆ getFileSize()

static size_t getFileSize ( const char *  infilename)
static

Definition at line 148 of file roundTripTest.c.

149 {
150  int r;
151 #if defined(_MSC_VER)
152  struct _stat64 statbuf;
153  r = _stat64(infilename, &statbuf);
154  if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
155 #else
156  struct stat statbuf;
157  r = stat(infilename, &statbuf);
158  if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
159 #endif
160  return (size_t)statbuf.st_size;
161 }
#define S_ISREG(mode)
Definition: compat.h:191
#define r
Definition: crypto_rc6.c:12
static stat
Definition: sflib.h:131
Definition: sftypes.h:80

References r, S_ISREG, and stat.

Referenced by fileCheck().

◆ isDirectory()

static int isDirectory ( const char *  infilename)
static

Definition at line 164 of file roundTripTest.c.

165 {
166  int r;
167 #if defined(_MSC_VER)
168  struct _stat64 statbuf;
169  r = _stat64(infilename, &statbuf);
170  if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
171 #else
172  struct stat statbuf;
173  r = stat(infilename, &statbuf);
174  if (!r && S_ISDIR(statbuf.st_mode)) return 1;
175 #endif
176  return 0;
177 }
#define S_ISDIR(mode)
Definition: compat.h:187

References r, S_ISDIR, and stat.

Referenced by loadFile().

◆ loadFile()

static void loadFile ( void *  buffer,
const char *  fileName,
size_t  fileSize 
)
static

loadFile() : requirement : buffer size >= fileSize

Definition at line 182 of file roundTripTest.c.

183 {
184  FILE* const f = fopen(fileName, "rb");
185  if (isDirectory(fileName)) {
186  MSG("Ignoring %s directory \n", fileName);
187  exit(2);
188  }
189  if (f==NULL) {
190  MSG("Impossible to open %s \n", fileName);
191  exit(3);
192  }
193  { size_t const readSize = fread(buffer, 1, fileSize, f);
194  if (readSize != fileSize) {
195  MSG("Error reading %s \n", fileName);
196  exit(5);
197  } }
198  fclose(f);
199 }
#define NULL
Definition: cris-opc.c:27
string FILE
Definition: benchmark.py:21
static int isDirectory(const char *infilename)
#define f(i)
Definition: sha256.c:46

References test-lz4-list::exit, f, benchmark::FILE, test-lz4-speed::fileName, isDirectory(), MSG, and NULL.

Referenced by fileCheck().

◆ main()

int main ( int  argCount,
const char **  argv 
)

Definition at line 229 of file roundTripTest.c.

230 {
231  const char* const exeName = argv[0];
232  int argNb = 1;
233  int clevel = 0;
234 
235  assert(argCount >= 1);
236  if (argCount < 2) return bad_usage(exeName);
237 
238  if (argv[1][0] == '-') {
239  clevel = argv[1][1] - '0';
240  argNb = 2;
241  }
242 
243  if (argNb >= argCount) return bad_usage(exeName);
244 
245  fileCheck(argv[argNb], clevel);
246  MSG("no pb detected \n");
247  return 0;
248 }
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
assert(limit<=UINT32_MAX/2)
static void fileCheck(const char *fileName, int clevel)
int bad_usage(const char *exeName)

References argv, assert(), bad_usage(), fileCheck(), and MSG.

◆ roundTripCheck()

static void roundTripCheck ( const void *  srcBuff,
size_t  srcSize,
int  clevel 
)
static

Definition at line 127 of file roundTripTest.c.

128 {
129  size_t const cBuffSize = LZ4_compressBound((int)srcSize);
130  void* const cBuff = malloc(cBuffSize);
131  void* const rBuff = malloc(cBuffSize);
132 
133  if (!cBuff || !rBuff) {
134  fprintf(stderr, "not enough memory ! \n");
135  exit(1);
136  }
137 
138  roundTripTest(rBuff, cBuffSize,
139  cBuff, cBuffSize,
140  srcBuff, srcSize,
141  clevel);
142 
143  free(rBuff);
144  free(cBuff);
145 }
int LZ4_compressBound(int isize)
Definition: lz4.c:674
char int srcSize
Definition: lz4.h:697
static void roundTripTest(void *resultBuff, size_t resultBuffCapacity, void *compressedBuff, size_t compressedBuffCapacity, const void *srcBuff, size_t srcSize, int clevel)

References test-lz4-list::exit, free(), LZ4_compressBound(), malloc(), roundTripTest(), and srcSize.

Referenced by fileCheck().

◆ roundTripTest()

static void roundTripTest ( void *  resultBuff,
size_t  resultBuffCapacity,
void *  compressedBuff,
size_t  compressedBuffCapacity,
const void *  srcBuff,
size_t  srcSize,
int  clevel 
)
static

roundTripTest() : Compresses srcBuff into compressedBuff, then decompresses compressedBuff into resultBuff. If clevel==0, compression level is derived from srcBuff's content head bytes. This function abort() if it detects any round-trip error. Therefore, if it returns, round trip is considered successfully validated. Note : compressedBuffCapacity should be >= LZ4_compressBound(srcSize) for compression to be guaranteed to work

Definition at line 101 of file roundTripTest.c.

105 {
106  int const proposed_clevel = clevel ? clevel : select_clevel(srcBuff, srcSize);
107  int const selected_clevel = proposed_clevel < 0 ? -proposed_clevel : proposed_clevel; /* if level < 0, it becomes an accelearion value */
109  int const cSize = compress((const char*)srcBuff, (char*)compressedBuff, (int)srcSize, (int)compressedBuffCapacity, selected_clevel);
110  CONTROL_MSG(cSize == 0, "Compression error !");
111 
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 !");
115  }
116 
117  /* check potential content corruption error */
118  assert(resultBuffCapacity >= srcSize);
119  { size_t const errorPos = checkBuffers(srcBuff, resultBuff, srcSize);
120  CONTROL_MSG(errorPos != srcSize,
121  "Silent decoding corruption, at pos %u !!!",
122  (unsigned)errorPos);
123  }
124 
125 }
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
int LZ4_compress_fast(const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration)
Definition: lz4.c:1354
LZ4_FORCE_O2 int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
Definition: lz4.c:2171
int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel)
Definition: lz4hc.c:954
#define LZ4HC_CLEVEL_MIN
Definition: lz4hc.h:47
static size_t checkBuffers(const void *buff1, const void *buff2, size_t buffSize)
Definition: roundTripTest.c:61
#define CONTROL_MSG(c,...)
Definition: roundTripTest.c:52
static int select_clevel(const void *refBuff, size_t refBuffSize)
Definition: roundTripTest.c:77
int(* compressFn)(const char *src, char *dst, int srcSize, int dstSize, int cLevel)
Definition: roundTripTest.c:90

References assert(), checkBuffers(), compress(), CONTROL_MSG, LZ4_compress_fast(), LZ4_compress_HC(), LZ4_decompress_safe(), LZ4HC_CLEVEL_MIN, select_clevel(), and srcSize.

Referenced by roundTripCheck().

◆ select_clevel()

static int select_clevel ( const void *  refBuff,
size_t  refBuffSize 
)
static

Definition at line 77 of file roundTripTest.c.

78 {
79  const int minCLevel = MIN_CLEVEL;
80  const int maxClevel = LZ4HC_CLEVEL_MAX;
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);
85 
86  return minCLevel + randL;
87 }
XXH_PUBLIC_API unsigned int XXH32(const void *input, size_t len, unsigned int seed)
Definition: xxhash.c:392
#define LZ4HC_CLEVEL_MAX
Definition: lz4hc.h:50
#define MIN_CLEVEL
Definition: roundTripTest.c:24
#define MIN(a, b)
Definition: roundTripTest.c:48

References LZ4HC_CLEVEL_MAX, MIN, MIN_CLEVEL, and XXH32().

Referenced by roundTripTest().