extlz4 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -4
  3. data/bin/extlz4 +1 -1
  4. data/contrib/lz4/NEWS +36 -0
  5. data/contrib/lz4/README.md +11 -12
  6. data/contrib/lz4/build/README.md +55 -0
  7. data/contrib/lz4/build/VS2010/datagen/datagen.vcxproj +169 -0
  8. data/contrib/lz4/build/VS2010/frametest/frametest.vcxproj +176 -0
  9. data/contrib/lz4/build/VS2010/fullbench-dll/fullbench-dll.vcxproj +180 -0
  10. data/contrib/lz4/build/VS2010/fullbench/fullbench.vcxproj +176 -0
  11. data/contrib/lz4/build/VS2010/fuzzer/fuzzer.vcxproj +173 -0
  12. data/contrib/lz4/build/VS2010/liblz4-dll/liblz4-dll.rc +51 -0
  13. data/contrib/lz4/build/VS2010/liblz4-dll/liblz4-dll.vcxproj +179 -0
  14. data/contrib/lz4/build/VS2010/liblz4/liblz4.vcxproj +175 -0
  15. data/contrib/lz4/build/VS2010/lz4.sln +98 -0
  16. data/contrib/lz4/build/VS2010/lz4/lz4.rc +51 -0
  17. data/contrib/lz4/build/VS2010/lz4/lz4.vcxproj +189 -0
  18. data/contrib/lz4/build/VS2017/datagen/datagen.vcxproj +173 -0
  19. data/contrib/lz4/build/VS2017/frametest/frametest.vcxproj +180 -0
  20. data/contrib/lz4/build/VS2017/fullbench-dll/fullbench-dll.vcxproj +184 -0
  21. data/contrib/lz4/build/VS2017/fullbench/fullbench.vcxproj +180 -0
  22. data/contrib/lz4/build/VS2017/fuzzer/fuzzer.vcxproj +177 -0
  23. data/contrib/lz4/build/VS2017/liblz4-dll/liblz4-dll.rc +51 -0
  24. data/contrib/lz4/build/VS2017/liblz4-dll/liblz4-dll.vcxproj +183 -0
  25. data/contrib/lz4/build/VS2017/liblz4/liblz4.vcxproj +179 -0
  26. data/contrib/lz4/build/VS2017/lz4.sln +103 -0
  27. data/contrib/lz4/build/VS2017/lz4/lz4.rc +51 -0
  28. data/contrib/lz4/build/VS2017/lz4/lz4.vcxproj +164 -0
  29. data/contrib/lz4/build/cmake/CMakeLists.txt +235 -0
  30. data/contrib/lz4/lib/README.md +27 -10
  31. data/contrib/lz4/lib/lz4.c +327 -230
  32. data/contrib/lz4/lib/lz4.h +80 -70
  33. data/contrib/lz4/lib/lz4frame.c +93 -54
  34. data/contrib/lz4/lib/lz4frame.h +22 -14
  35. data/contrib/lz4/lib/lz4hc.c +192 -115
  36. data/contrib/lz4/lib/lz4hc.h +15 -40
  37. data/contrib/lz4/ossfuzz/Makefile +12 -8
  38. data/contrib/lz4/ossfuzz/compress_frame_fuzzer.c +11 -5
  39. data/contrib/lz4/ossfuzz/compress_fuzzer.c +9 -2
  40. data/contrib/lz4/ossfuzz/compress_hc_fuzzer.c +10 -3
  41. data/contrib/lz4/ossfuzz/decompress_frame_fuzzer.c +11 -3
  42. data/contrib/lz4/ossfuzz/decompress_fuzzer.c +6 -2
  43. data/contrib/lz4/ossfuzz/fuzz_data_producer.c +77 -0
  44. data/contrib/lz4/ossfuzz/fuzz_data_producer.h +36 -0
  45. data/contrib/lz4/ossfuzz/round_trip_frame_fuzzer.c +8 -4
  46. data/contrib/lz4/ossfuzz/round_trip_fuzzer.c +9 -2
  47. data/contrib/lz4/ossfuzz/round_trip_hc_fuzzer.c +7 -2
  48. data/contrib/lz4/ossfuzz/travisoss.sh +6 -1
  49. data/contrib/lz4/tmp +0 -0
  50. data/contrib/lz4/tmpsparse +0 -0
  51. data/ext/extlz4.c +2 -0
  52. data/ext/extlz4.h +5 -0
  53. data/ext/hashargs.c +1 -1
  54. data/ext/hashargs.h +1 -1
  55. data/gemstub.rb +3 -14
  56. data/lib/extlz4.rb +0 -2
  57. data/lib/extlz4/oldstream.rb +1 -1
  58. metadata +40 -25
  59. data/lib/extlz4/version.rb +0 -3
@@ -198,57 +198,32 @@ LZ4LIB_API int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, in
198
198
  #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1)
199
199
 
200
200
 
201
- #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
202
- #include <stdint.h>
203
-
204
- typedef struct LZ4HC_CCtx_internal LZ4HC_CCtx_internal;
205
- struct LZ4HC_CCtx_internal
206
- {
207
- uint32_t hashTable[LZ4HC_HASHTABLESIZE];
208
- uint16_t chainTable[LZ4HC_MAXD];
209
- const uint8_t* end; /* next block here to continue on current prefix */
210
- const uint8_t* base; /* All index relative to this position */
211
- const uint8_t* dictBase; /* alternate base for extDict */
212
- uint32_t dictLimit; /* below that point, need extDict */
213
- uint32_t lowLimit; /* below that point, no more dict */
214
- uint32_t nextToUpdate; /* index from which to continue dictionary update */
215
- short compressionLevel;
216
- int8_t favorDecSpeed; /* favor decompression speed if this flag set,
217
- otherwise, favor compression ratio */
218
- int8_t dirty; /* stream has to be fully reset if this flag is set */
219
- const LZ4HC_CCtx_internal* dictCtx;
220
- };
221
-
222
- #else
223
-
224
201
  typedef struct LZ4HC_CCtx_internal LZ4HC_CCtx_internal;
225
202
  struct LZ4HC_CCtx_internal
226
203
  {
227
- unsigned int hashTable[LZ4HC_HASHTABLESIZE];
228
- unsigned short chainTable[LZ4HC_MAXD];
229
- const unsigned char* end; /* next block here to continue on current prefix */
230
- const unsigned char* base; /* All index relative to this position */
231
- const unsigned char* dictBase; /* alternate base for extDict */
232
- unsigned int dictLimit; /* below that point, need extDict */
233
- unsigned int lowLimit; /* below that point, no more dict */
234
- unsigned int nextToUpdate; /* index from which to continue dictionary update */
235
- short compressionLevel;
236
- char favorDecSpeed; /* favor decompression speed if this flag set,
237
- otherwise, favor compression ratio */
238
- char dirty; /* stream has to be fully reset if this flag is set */
204
+ LZ4_u32 hashTable[LZ4HC_HASHTABLESIZE];
205
+ LZ4_u16 chainTable[LZ4HC_MAXD];
206
+ const LZ4_byte* end; /* next block here to continue on current prefix */
207
+ const LZ4_byte* base; /* All index relative to this position */
208
+ const LZ4_byte* dictBase; /* alternate base for extDict */
209
+ LZ4_u32 dictLimit; /* below that point, need extDict */
210
+ LZ4_u32 lowLimit; /* below that point, no more dict */
211
+ LZ4_u32 nextToUpdate; /* index from which to continue dictionary update */
212
+ short compressionLevel;
213
+ LZ4_i8 favorDecSpeed; /* favor decompression speed if this flag set,
214
+ otherwise, favor compression ratio */
215
+ LZ4_i8 dirty; /* stream has to be fully reset if this flag is set */
239
216
  const LZ4HC_CCtx_internal* dictCtx;
240
217
  };
241
218
 
242
- #endif
243
-
244
219
 
245
220
  /* Do not use these definitions directly !
246
221
  * Declare or allocate an LZ4_streamHC_t instead.
247
222
  */
248
- #define LZ4_STREAMHCSIZE (4*LZ4HC_HASHTABLESIZE + 2*LZ4HC_MAXD + 56 + ((sizeof(void*)==16) ? 56 : 0) /* AS400*/ ) /* 262200 or 262256*/
249
- #define LZ4_STREAMHCSIZE_SIZET (LZ4_STREAMHCSIZE / sizeof(size_t))
223
+ #define LZ4_STREAMHCSIZE 262200 /* static size, for inter-version compatibility */
224
+ #define LZ4_STREAMHCSIZE_VOIDP (LZ4_STREAMHCSIZE / sizeof(void*))
250
225
  union LZ4_streamHC_u {
251
- size_t table[LZ4_STREAMHCSIZE_SIZET];
226
+ void* table[LZ4_STREAMHCSIZE_VOIDP];
252
227
  LZ4HC_CCtx_internal internal_donotuse;
253
228
  }; /* previously typedef'd to LZ4_streamHC_t */
254
229
 
@@ -26,7 +26,7 @@
26
26
  # ##########################################################################
27
27
 
28
28
  LZ4DIR := ../lib
29
- LIB_FUZZING_ENGINE ?= standaloneengine.o
29
+ LIB_FUZZING_ENGINE ?=
30
30
 
31
31
  DEBUGLEVEL?= 1
32
32
  DEBUGFLAGS = -g -DLZ4_DEBUG=$(DEBUGLEVEL)
@@ -47,6 +47,7 @@ FUZZERS := \
47
47
  round_trip_frame_fuzzer \
48
48
  decompress_frame_fuzzer
49
49
 
50
+ .PHONY: all
50
51
  all: $(FUZZERS)
51
52
 
52
53
  # Include a rule to build the static library if calling this target
@@ -58,17 +59,20 @@ $(LZ4DIR)/liblz4.a:
58
59
  $(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) $< -o $@
59
60
 
60
61
  # Generic rule for generating fuzzers
61
- %_fuzzer: %_fuzzer.o lz4_helpers.o $(LZ4DIR)/liblz4.a
62
- # Compile the standalone code just in case. The OSS-Fuzz code might
63
- # override the LIB_FUZZING_ENGINE value to "-fsanitize=fuzzer"
64
- $(CC) -c $(LZ4_CFLAGS) $(LZ4_CPPFLAGS) standaloneengine.c -o standaloneengine.o
65
-
66
- # Now compile the actual fuzzer.
62
+ ifeq ($(LIB_FUZZING_ENGINE),)
63
+ LIB_FUZZING_DEPS := standaloneengine.o
64
+ else
65
+ LIB_FUZZING_DEPS :=
66
+ endif
67
+ %_fuzzer: %_fuzzer.o lz4_helpers.o fuzz_data_producer.o $(LZ4DIR)/liblz4.a $(LIB_FUZZING_DEPS)
67
68
  $(CXX) $(LZ4_CXXFLAGS) $(LZ4_CPPFLAGS) $(LDFLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT)
68
69
 
69
70
  %_fuzzer_clean:
70
71
  $(RM) $*_fuzzer $*_fuzzer.o standaloneengine.o
71
72
 
72
73
  .PHONY: clean
73
- clean: compress_fuzzer_clean decompress_fuzzer_clean
74
+ clean: compress_fuzzer_clean decompress_fuzzer_clean \
75
+ compress_frame_fuzzer_clean compress_hc_fuzzer_clean \
76
+ decompress_frame_fuzzer_clean round_trip_frame_fuzzer_clean \
77
+ round_trip_fuzzer_clean round_trip_hc_fuzzer_clean round_trip_stream_fuzzer_clean
74
78
  $(MAKE) -C $(LZ4DIR) clean
@@ -13,18 +13,23 @@
13
13
  #include "lz4.h"
14
14
  #include "lz4frame.h"
15
15
  #include "lz4_helpers.h"
16
+ #include "fuzz_data_producer.h"
16
17
 
17
18
  int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
18
19
  {
19
- uint32_t seed = FUZZ_seed(&data, &size);
20
- LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed);
20
+ FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
21
+ LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer);
22
+ size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
23
+ size = FUZZ_dataProducer_remainingBytes(producer);
24
+
21
25
  size_t const compressBound = LZ4F_compressFrameBound(size, &prefs);
22
- size_t const dstCapacity = FUZZ_rand32(&seed, 0, compressBound);
26
+ size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, compressBound);
27
+
23
28
  char* const dst = (char*)malloc(dstCapacity);
24
29
  char* const rt = (char*)malloc(size);
25
30
 
26
- FUZZ_ASSERT(dst);
27
- FUZZ_ASSERT(rt);
31
+ FUZZ_ASSERT(dst!=NULL);
32
+ FUZZ_ASSERT(rt!=NULL);
28
33
 
29
34
  /* If compression succeeds it must round trip correctly. */
30
35
  size_t const dstSize =
@@ -37,6 +42,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
37
42
 
38
43
  free(dst);
39
44
  free(rt);
45
+ FUZZ_dataProducer_free(producer);
40
46
 
41
47
  return 0;
42
48
  }
@@ -10,12 +10,18 @@
10
10
  #include <string.h>
11
11
 
12
12
  #include "fuzz_helpers.h"
13
+ #include "fuzz_data_producer.h"
13
14
  #include "lz4.h"
14
15
 
15
16
  int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
16
17
  {
17
- uint32_t seed = FUZZ_seed(&data, &size);
18
- size_t const dstCapacity = FUZZ_rand32(&seed, 0, LZ4_compressBound(size));
18
+ FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
19
+ size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
20
+ size = FUZZ_dataProducer_remainingBytes(producer);
21
+
22
+ size_t const compressBound = LZ4_compressBound(size);
23
+ size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, compressBound);
24
+
19
25
  char* const dst = (char*)malloc(dstCapacity);
20
26
  char* const rt = (char*)malloc(size);
21
27
 
@@ -46,6 +52,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
46
52
 
47
53
  free(dst);
48
54
  free(rt);
55
+ FUZZ_dataProducer_free(producer);
49
56
 
50
57
  return 0;
51
58
  }
@@ -10,16 +10,22 @@
10
10
  #include <string.h>
11
11
 
12
12
  #include "fuzz_helpers.h"
13
+ #include "fuzz_data_producer.h"
13
14
  #include "lz4.h"
14
15
  #include "lz4hc.h"
15
16
 
16
17
  int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
17
18
  {
18
- uint32_t seed = FUZZ_seed(&data, &size);
19
- size_t const dstCapacity = FUZZ_rand32(&seed, 0, LZ4_compressBound(size));
19
+ FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
20
+ size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
21
+ size_t const levelSeed = FUZZ_dataProducer_retrieve32(producer);
22
+ size = FUZZ_dataProducer_remainingBytes(producer);
23
+
24
+ size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, size);
25
+ int const level = FUZZ_getRange_from_uint32(levelSeed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX);
26
+
20
27
  char* const dst = (char*)malloc(dstCapacity);
21
28
  char* const rt = (char*)malloc(size);
22
- int const level = FUZZ_rand32(&seed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX);
23
29
 
24
30
  FUZZ_ASSERT(dst);
25
31
  FUZZ_ASSERT(rt);
@@ -52,6 +58,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
52
58
 
53
59
  free(dst);
54
60
  free(rt);
61
+ FUZZ_dataProducer_free(producer);
55
62
 
56
63
  return 0;
57
64
  }
@@ -9,6 +9,7 @@
9
9
  #include <string.h>
10
10
 
11
11
  #include "fuzz_helpers.h"
12
+ #include "fuzz_data_producer.h"
12
13
  #include "lz4.h"
13
14
  #define LZ4F_STATIC_LINKING_ONLY
14
15
  #include "lz4frame.h"
@@ -29,11 +30,17 @@ static void decompress(LZ4F_dctx* dctx, void* dst, size_t dstCapacity,
29
30
 
30
31
  int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
31
32
  {
33
+ FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
34
+ size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
35
+ size_t const dictSizeSeed = FUZZ_dataProducer_retrieve32(producer);
36
+ size = FUZZ_dataProducer_remainingBytes(producer);
32
37
 
33
- uint32_t seed = FUZZ_seed(&data, &size);
34
- size_t const dstCapacity = FUZZ_rand32(&seed, 0, 4 * size);
38
+ size_t const dstCapacity = FUZZ_getRange_from_uint32(
39
+ dstCapacitySeed, 0, 4 * size);
35
40
  size_t const largeDictSize = 64 * 1024;
36
- size_t const dictSize = FUZZ_rand32(&seed, 0, largeDictSize);
41
+ size_t const dictSize = FUZZ_getRange_from_uint32(
42
+ dictSizeSeed, 0, largeDictSize);
43
+
37
44
  char* const dst = (char*)malloc(dstCapacity);
38
45
  char* const dict = (char*)malloc(dictSize);
39
46
  LZ4F_decompressOptions_t opts;
@@ -62,6 +69,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
62
69
  LZ4F_freeDecompressionContext(dctx);
63
70
  free(dst);
64
71
  free(dict);
72
+ FUZZ_dataProducer_free(producer);
65
73
 
66
74
  return 0;
67
75
  }
@@ -9,13 +9,16 @@
9
9
  #include <string.h>
10
10
 
11
11
  #include "fuzz_helpers.h"
12
+ #include "fuzz_data_producer.h"
12
13
  #include "lz4.h"
13
14
 
14
15
  int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
15
16
  {
17
+ FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
18
+ size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
19
+ size = FUZZ_dataProducer_remainingBytes(producer);
16
20
 
17
- uint32_t seed = FUZZ_seed(&data, &size);
18
- size_t const dstCapacity = FUZZ_rand32(&seed, 0, 4 * size);
21
+ size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, 4 * size);
19
22
  size_t const smallDictSize = size + 1;
20
23
  size_t const largeDictSize = 64 * 1024 - 1;
21
24
  size_t const dictSize = MAX(smallDictSize, largeDictSize);
@@ -53,6 +56,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
53
56
  dstCapacity, dstCapacity);
54
57
  free(dst);
55
58
  free(dict);
59
+ FUZZ_dataProducer_free(producer);
56
60
 
57
61
  return 0;
58
62
  }
@@ -0,0 +1,77 @@
1
+ #include "fuzz_data_producer.h"
2
+
3
+ struct FUZZ_dataProducer_s{
4
+ const uint8_t *data;
5
+ size_t size;
6
+ };
7
+
8
+ FUZZ_dataProducer_t* FUZZ_dataProducer_create(const uint8_t* data, size_t size) {
9
+ FUZZ_dataProducer_t* const producer = malloc(sizeof(FUZZ_dataProducer_t));
10
+
11
+ FUZZ_ASSERT(producer != NULL);
12
+
13
+ producer->data = data;
14
+ producer->size = size;
15
+ return producer;
16
+ }
17
+
18
+ void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); }
19
+
20
+ uint32_t FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t *producer) {
21
+ const uint8_t* data = producer->data;
22
+ const size_t size = producer->size;
23
+ if (size == 0) {
24
+ return 0;
25
+ } else if (size < 4) {
26
+ producer->size -= 1;
27
+ return (uint32_t)data[size - 1];
28
+ } else {
29
+ producer->size -= 4;
30
+ return *(data + size - 4);
31
+ }
32
+ }
33
+
34
+ uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max)
35
+ {
36
+ uint32_t range = max - min;
37
+ if (range == 0xffffffff) {
38
+ return seed;
39
+ }
40
+ return min + seed % (range + 1);
41
+ }
42
+
43
+ uint32_t FUZZ_dataProducer_range32(FUZZ_dataProducer_t* producer,
44
+ uint32_t min, uint32_t max)
45
+ {
46
+ size_t const seed = FUZZ_dataProducer_retrieve32(producer);
47
+ return FUZZ_getRange_from_uint32(seed, min, max);
48
+ }
49
+
50
+ LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer)
51
+ {
52
+ LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO;
53
+ info.blockSizeID = FUZZ_dataProducer_range32(producer, LZ4F_max64KB - 1, LZ4F_max4MB);
54
+ if (info.blockSizeID < LZ4F_max64KB) {
55
+ info.blockSizeID = LZ4F_default;
56
+ }
57
+ info.blockMode = FUZZ_dataProducer_range32(producer, LZ4F_blockLinked, LZ4F_blockIndependent);
58
+ info.contentChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noContentChecksum,
59
+ LZ4F_contentChecksumEnabled);
60
+ info.blockChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noBlockChecksum,
61
+ LZ4F_blockChecksumEnabled);
62
+ return info;
63
+ }
64
+
65
+ LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer)
66
+ {
67
+ LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES;
68
+ prefs.frameInfo = FUZZ_dataProducer_frameInfo(producer);
69
+ prefs.compressionLevel = FUZZ_dataProducer_range32(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3;
70
+ prefs.autoFlush = FUZZ_dataProducer_range32(producer, 0, 1);
71
+ prefs.favorDecSpeed = FUZZ_dataProducer_range32(producer, 0, 1);
72
+ return prefs;
73
+ }
74
+
75
+ size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer){
76
+ return producer->size;
77
+ }
@@ -0,0 +1,36 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+
6
+ #include "fuzz_helpers.h"
7
+ #include "lz4frame.h"
8
+ #include "lz4hc.h"
9
+
10
+ /* Struct used for maintaining the state of the data */
11
+ typedef struct FUZZ_dataProducer_s FUZZ_dataProducer_t;
12
+
13
+ /* Returns a data producer state struct. Use for producer initialization. */
14
+ FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size);
15
+
16
+ /* Frees the data producer */
17
+ void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer);
18
+
19
+ /* Returns 32 bits from the end of data */
20
+ uint32_t FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t *producer);
21
+
22
+ /* Returns value between [min, max] */
23
+ uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max);
24
+
25
+ /* Combination of above two functions for non adaptive use cases. ie where size is not involved */
26
+ uint32_t FUZZ_dataProducer_range32(FUZZ_dataProducer_t *producer, uint32_t min,
27
+ uint32_t max);
28
+
29
+ /* Returns lz4 preferences */
30
+ LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer);
31
+
32
+ /* Returns lz4 frame info */
33
+ LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer);
34
+
35
+ /* Returns the size of the remaining bytes of data in the producer */
36
+ size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer);
@@ -12,14 +12,17 @@
12
12
  #include "lz4.h"
13
13
  #include "lz4frame.h"
14
14
  #include "lz4_helpers.h"
15
+ #include "fuzz_data_producer.h"
15
16
 
16
17
  int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
17
18
  {
18
- uint32_t seed = FUZZ_seed(&data, &size);
19
- LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed);
20
- size_t const dstCapacity = LZ4F_compressFrameBound(size, &prefs);
19
+ FUZZ_dataProducer_t* producer = FUZZ_dataProducer_create(data, size);
20
+ LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer);
21
+ size = FUZZ_dataProducer_remainingBytes(producer);
22
+
23
+ size_t const dstCapacity = LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs);
21
24
  char* const dst = (char*)malloc(dstCapacity);
22
- char* const rt = (char*)malloc(size);
25
+ char* const rt = (char*)malloc(FUZZ_dataProducer_remainingBytes(producer));
23
26
 
24
27
  FUZZ_ASSERT(dst);
25
28
  FUZZ_ASSERT(rt);
@@ -34,6 +37,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
34
37
 
35
38
  free(dst);
36
39
  free(rt);
40
+ FUZZ_dataProducer_free(producer);
37
41
 
38
42
  return 0;
39
43
  }
@@ -10,11 +10,17 @@
10
10
 
11
11
  #include "fuzz_helpers.h"
12
12
  #include "lz4.h"
13
+ #include "fuzz_data_producer.h"
13
14
 
14
15
  int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
15
16
  {
16
- uint32_t seed = FUZZ_seed(&data, &size);
17
+ FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
18
+ size_t const partialCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
19
+ size = FUZZ_dataProducer_remainingBytes(producer);
20
+
21
+ size_t const partialCapacity = FUZZ_getRange_from_uint32(partialCapacitySeed, 0, size);
17
22
  size_t const dstCapacity = LZ4_compressBound(size);
23
+
18
24
  char* const dst = (char*)malloc(dstCapacity);
19
25
  char* const rt = (char*)malloc(size);
20
26
 
@@ -32,7 +38,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
32
38
 
33
39
  /* Partial decompression must succeed. */
34
40
  {
35
- size_t const partialCapacity = FUZZ_rand32(&seed, 0, size);
36
41
  char* const partial = (char*)malloc(partialCapacity);
37
42
  FUZZ_ASSERT(partial);
38
43
  int const partialSize = LZ4_decompress_safe_partial(
@@ -43,8 +48,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
43
48
  free(partial);
44
49
  }
45
50
 
51
+
46
52
  free(dst);
47
53
  free(rt);
54
+ FUZZ_dataProducer_free(producer);
48
55
 
49
56
  return 0;
50
57
  }