2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
11 This program takes a file in input,
12 performs a zstd round-trip test (compression - decompress)
13 compares the result with original
14 and generates a crash (double free) on corruption detection.
17 /*===========================================
19 *==========================================*/
20 #include <stddef.h> /* size_t */
21 #include <stdlib.h> /* malloc, free, exit */
22 #include <stdio.h> /* fprintf */
23 #include <sys/types.h> /* stat */
24 #include <sys/stat.h> /* stat */
28 /*===========================================
30 *==========================================*/
31 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
34 * Compresses `srcBuff` into `compressedBuff`,
35 * then decompresses `compressedBuff` into `resultBuff`.
36 * Compression level used is derived from first content byte.
37 * @return : result of decompression, which should be == `srcSize`
38 * or an error code if either compression or decompression fails.
39 * Note : `compressedBuffCapacity` should be `>= ZSTD_compressBound(srcSize)`
40 * for compression to be guaranteed to work */
41 static size_t roundTripTest(void* resultBuff, size_t resultBuffCapacity,
42 void* compressedBuff, size_t compressedBuffCapacity,
43 const void* srcBuff, size_t srcBuffSize)
45 static const int maxClevel = 19;
46 size_t const hashLength = MIN(128, srcBuffSize);
47 unsigned const h32 = XXH32(srcBuff, hashLength, 0);
48 int const cLevel = h32 % maxClevel;
49 size_t const cSize = ZSTD_compress(compressedBuff, compressedBuffCapacity, srcBuff, srcBuffSize, cLevel);
50 if (ZSTD_isError(cSize)) {
51 fprintf(stderr, "Compression error : %s \n", ZSTD_getErrorName(cSize));
54 return ZSTD_decompress(resultBuff, resultBuffCapacity, compressedBuff, cSize);
58 static size_t checkBuffers(const void* buff1, const void* buff2, size_t buffSize)
60 const char* ip1 = (const char*)buff1;
61 const char* ip2 = (const char*)buff2;
64 for (pos=0; pos<buffSize; pos++)
65 if (ip1[pos]!=ip2[pos])
71 static void crash(int errorCode){
72 /* abort if AFL/libfuzzer, exit otherwise */
73 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* could also use __AFL_COMPILER */
80 static void roundTripCheck(const void* srcBuff, size_t srcBuffSize)
82 size_t const cBuffSize = ZSTD_compressBound(srcBuffSize);
83 void* cBuff = malloc(cBuffSize);
84 void* rBuff = malloc(cBuffSize);
86 if (!cBuff || !rBuff) {
87 fprintf(stderr, "not enough memory ! \n");
91 { size_t const result = roundTripTest(rBuff, cBuffSize, cBuff, cBuffSize, srcBuff, srcBuffSize);
92 if (ZSTD_isError(result)) {
93 fprintf(stderr, "roundTripTest error : %s \n", ZSTD_getErrorName(result));
96 if (result != srcBuffSize) {
97 fprintf(stderr, "Incorrect regenerated size : %u != %u\n", (unsigned)result, (unsigned)srcBuffSize);
100 if (checkBuffers(srcBuff, rBuff, srcBuffSize) != srcBuffSize) {
101 fprintf(stderr, "Silent decoding corruption !!!");
111 static size_t getFileSize(const char* infilename)
114 #if defined(_MSC_VER)
115 struct _stat64 statbuf;
116 r = _stat64(infilename, &statbuf);
117 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
120 r = stat(infilename, &statbuf);
121 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
123 return (size_t)statbuf.st_size;
127 static int isDirectory(const char* infilename)
130 #if defined(_MSC_VER)
131 struct _stat64 statbuf;
132 r = _stat64(infilename, &statbuf);
133 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
136 r = stat(infilename, &statbuf);
137 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
144 * requirement : `buffer` size >= `fileSize` */
145 static void loadFile(void* buffer, const char* fileName, size_t fileSize)
147 FILE* const f = fopen(fileName, "rb");
148 if (isDirectory(fileName)) {
149 fprintf(stderr, "Ignoring %s directory \n", fileName);
153 fprintf(stderr, "Impossible to open %s \n", fileName);
156 { size_t const readSize = fread(buffer, 1, fileSize, f);
157 if (readSize != fileSize) {
158 fprintf(stderr, "Error reading %s \n", fileName);
165 static void fileCheck(const char* fileName)
167 size_t const fileSize = getFileSize(fileName);
168 void* buffer = malloc(fileSize);
170 fprintf(stderr, "not enough memory \n");
173 loadFile(buffer, fileName, fileSize);
174 roundTripCheck(buffer, fileSize);
178 int main(int argCount, const char** argv) {
180 fprintf(stderr, "Error : no argument : need input file \n");
184 fprintf(stderr, "no pb detected\n");