Rizin
unix-like reverse engineering framework and cli tools
compress_hc_fuzzer.c
Go to the documentation of this file.
1 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "fuzz_helpers.h"
13 #include "fuzz_data_producer.h"
14 #include "lz4.h"
15 #include "lz4hc.h"
16 
17 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
18 {
20  size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
21  size_t const levelSeed = FUZZ_dataProducer_retrieve32(producer);
23 
24  size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, size);
26 
27  char* const dst = (char*)malloc(dstCapacity);
28  char* const rt = (char*)malloc(size);
29 
31  FUZZ_ASSERT(rt);
32 
33  /* If compression succeeds it must round trip correctly. */
34  {
35  int const dstSize = LZ4_compress_HC((const char*)data, dst, size,
36  dstCapacity, level);
37  if (dstSize > 0) {
38  int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size);
39  FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size");
40  FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
41  }
42  }
43 
44  if (dstCapacity > 0) {
45  /* Compression succeeds and must round trip correctly. */
46  void* state = malloc(LZ4_sizeofStateHC());
48  int compressedSize = size;
49  int const dstSize = LZ4_compress_HC_destSize(state, (const char*)data,
51  dstCapacity, level);
52  FUZZ_ASSERT(dstSize > 0);
53  int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size);
54  FUZZ_ASSERT_MSG(rtSize == compressedSize, "Incorrect regenerated size");
55  FUZZ_ASSERT_MSG(!memcmp(data, rt, compressedSize), "Corruption!");
56  free(state);
57  }
58 
59  free(dst);
60  free(rt);
61  FUZZ_dataProducer_free(producer);
62 
63  return 0;
64 }
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer)
uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max)
size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer)
FUZZ_dataProducer_t * FUZZ_dataProducer_create(const uint8_t *data, size_t size)
uint32_t FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t *producer)
#define FUZZ_ASSERT(cond)
Definition: fuzz_helpers.h:46
#define FUZZ_ASSERT_MSG(cond, msg)
Definition: fuzz_helpers.h:41
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123
LZ4_FORCE_O2 int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
Definition: lz4.c:2171
char int compressedSize
Definition: lz4.h:724
char * dst
Definition: lz4.h:724
int LZ4_sizeofStateHC(void)
Definition: lz4hc.c:921
int LZ4_compress_HC_destSize(void *state, const char *source, char *dest, int *sourceSizePtr, int targetDestSize, int cLevel)
Definition: lz4hc.c:970
int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity, int compressionLevel)
Definition: lz4hc.c:954
#define LZ4HC_CLEVEL_MAX
Definition: lz4hc.h:50
#define LZ4HC_CLEVEL_MIN
Definition: lz4hc.h:47
unsigned char uint8_t
Definition: sftypes.h:31
Definition: dis.h:43
static int level
Definition: vmenus.c:2424