extlz4 0.2.4.2 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +25 -0
  3. data/README.md +49 -41
  4. data/bin/extlz4 +1 -1
  5. data/contrib/lz4/INSTALL +1 -0
  6. data/contrib/lz4/Makefile.inc +87 -0
  7. data/contrib/lz4/NEWS +89 -0
  8. data/contrib/lz4/README.md +42 -36
  9. data/contrib/lz4/build/README.md +55 -0
  10. data/contrib/lz4/build/VS2010/datagen/datagen.vcxproj +169 -0
  11. data/contrib/lz4/build/VS2010/frametest/frametest.vcxproj +176 -0
  12. data/contrib/lz4/build/VS2010/fullbench-dll/fullbench-dll.vcxproj +180 -0
  13. data/contrib/lz4/build/VS2010/fullbench/fullbench.vcxproj +176 -0
  14. data/contrib/lz4/build/VS2010/fuzzer/fuzzer.vcxproj +173 -0
  15. data/contrib/lz4/build/VS2010/liblz4-dll/liblz4-dll.rc +51 -0
  16. data/contrib/lz4/build/VS2010/liblz4-dll/liblz4-dll.vcxproj +179 -0
  17. data/contrib/lz4/build/VS2010/liblz4/liblz4.vcxproj +175 -0
  18. data/contrib/lz4/build/VS2010/lz4.sln +98 -0
  19. data/contrib/lz4/build/VS2010/lz4/lz4.rc +51 -0
  20. data/contrib/lz4/build/VS2010/lz4/lz4.vcxproj +189 -0
  21. data/contrib/lz4/build/VS2017/datagen/datagen.vcxproj +173 -0
  22. data/contrib/lz4/build/VS2017/frametest/frametest.vcxproj +180 -0
  23. data/contrib/lz4/build/VS2017/fullbench-dll/fullbench-dll.vcxproj +184 -0
  24. data/contrib/lz4/build/VS2017/fullbench/fullbench.vcxproj +180 -0
  25. data/contrib/lz4/build/VS2017/fuzzer/fuzzer.vcxproj +177 -0
  26. data/contrib/lz4/build/VS2017/liblz4-dll/liblz4-dll.rc +51 -0
  27. data/contrib/lz4/build/VS2017/liblz4-dll/liblz4-dll.vcxproj +183 -0
  28. data/contrib/lz4/build/VS2017/liblz4/liblz4.vcxproj +179 -0
  29. data/contrib/lz4/build/VS2017/lz4.sln +103 -0
  30. data/contrib/lz4/build/VS2017/lz4/lz4.rc +51 -0
  31. data/contrib/lz4/build/VS2017/lz4/lz4.vcxproj +164 -0
  32. data/contrib/lz4/build/cmake/CMakeLists.txt +235 -0
  33. data/contrib/lz4/lib/README.md +98 -34
  34. data/contrib/lz4/lib/liblz4-dll.rc.in +35 -0
  35. data/contrib/lz4/lib/lz4.c +1698 -681
  36. data/contrib/lz4/lib/lz4.h +546 -235
  37. data/contrib/lz4/lib/lz4frame.c +608 -378
  38. data/contrib/lz4/lib/lz4frame.h +315 -83
  39. data/contrib/lz4/lib/lz4frame_static.h +4 -100
  40. data/contrib/lz4/lib/lz4hc.c +1090 -282
  41. data/contrib/lz4/lib/lz4hc.h +276 -141
  42. data/contrib/lz4/lib/xxhash.c +371 -235
  43. data/contrib/lz4/lib/xxhash.h +128 -93
  44. data/contrib/lz4/ossfuzz/Makefile +78 -0
  45. data/contrib/lz4/ossfuzz/compress_frame_fuzzer.c +48 -0
  46. data/contrib/lz4/ossfuzz/compress_fuzzer.c +58 -0
  47. data/contrib/lz4/ossfuzz/compress_hc_fuzzer.c +64 -0
  48. data/contrib/lz4/ossfuzz/decompress_frame_fuzzer.c +75 -0
  49. data/contrib/lz4/ossfuzz/decompress_fuzzer.c +62 -0
  50. data/contrib/lz4/ossfuzz/fuzz.h +48 -0
  51. data/contrib/lz4/ossfuzz/fuzz_data_producer.c +77 -0
  52. data/contrib/lz4/ossfuzz/fuzz_data_producer.h +36 -0
  53. data/contrib/lz4/ossfuzz/fuzz_helpers.h +94 -0
  54. data/contrib/lz4/ossfuzz/lz4_helpers.c +51 -0
  55. data/contrib/lz4/ossfuzz/lz4_helpers.h +13 -0
  56. data/contrib/lz4/ossfuzz/ossfuzz.sh +23 -0
  57. data/contrib/lz4/ossfuzz/round_trip_frame_fuzzer.c +43 -0
  58. data/contrib/lz4/ossfuzz/round_trip_fuzzer.c +57 -0
  59. data/contrib/lz4/ossfuzz/round_trip_hc_fuzzer.c +44 -0
  60. data/contrib/lz4/ossfuzz/round_trip_stream_fuzzer.c +302 -0
  61. data/contrib/lz4/ossfuzz/standaloneengine.c +74 -0
  62. data/contrib/lz4/ossfuzz/travisoss.sh +26 -0
  63. data/contrib/lz4/tmp +0 -0
  64. data/contrib/lz4/tmpsparse +0 -0
  65. data/ext/blockapi.c +5 -5
  66. data/ext/extlz4.c +2 -0
  67. data/ext/extlz4.h +5 -0
  68. data/ext/frameapi.c +1 -1
  69. data/ext/hashargs.c +2 -2
  70. data/ext/hashargs.h +1 -1
  71. data/ext/lz4_amalgam.c +0 -23
  72. data/gemstub.rb +5 -16
  73. data/lib/extlz4.rb +51 -3
  74. data/lib/extlz4/oldstream.rb +1 -1
  75. data/test/common.rb +2 -2
  76. metadata +73 -16
  77. data/contrib/lz4/circle.yml +0 -39
  78. data/contrib/lz4/lib/lz4opt.h +0 -366
  79. data/lib/extlz4/version.rb +0 -3
@@ -0,0 +1,64 @@
1
+ /**
2
+ * This fuzz target attempts to compress the fuzzed data with the simple
3
+ * compression function with an output buffer that may be too small to
4
+ * ensure that the compressor never crashes.
5
+ */
6
+
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
+ {
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
+
27
+ char* const dst = (char*)malloc(dstCapacity);
28
+ char* const rt = (char*)malloc(size);
29
+
30
+ FUZZ_ASSERT(dst);
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());
47
+ FUZZ_ASSERT(state);
48
+ int compressedSize = size;
49
+ int const dstSize = LZ4_compress_HC_destSize(state, (const char*)data,
50
+ dst, &compressedSize,
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
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * This fuzz target attempts to decompress the fuzzed data with the simple
3
+ * decompression function to ensure the decompressor never crashes.
4
+ */
5
+
6
+ #include <stddef.h>
7
+ #include <stdint.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ #include "fuzz_helpers.h"
12
+ #include "fuzz_data_producer.h"
13
+ #include "lz4.h"
14
+ #define LZ4F_STATIC_LINKING_ONLY
15
+ #include "lz4frame.h"
16
+ #include "lz4_helpers.h"
17
+
18
+ static void decompress(LZ4F_dctx* dctx, void* dst, size_t dstCapacity,
19
+ const void* src, size_t srcSize,
20
+ const void* dict, size_t dictSize,
21
+ const LZ4F_decompressOptions_t* opts)
22
+ {
23
+ LZ4F_resetDecompressionContext(dctx);
24
+ if (dictSize == 0)
25
+ LZ4F_decompress(dctx, dst, &dstCapacity, src, &srcSize, opts);
26
+ else
27
+ LZ4F_decompress_usingDict(dctx, dst, &dstCapacity, src, &srcSize,
28
+ dict, dictSize, opts);
29
+ }
30
+
31
+ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
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);
37
+
38
+ size_t const dstCapacity = FUZZ_getRange_from_uint32(
39
+ dstCapacitySeed, 0, 4 * size);
40
+ size_t const largeDictSize = 64 * 1024;
41
+ size_t const dictSize = FUZZ_getRange_from_uint32(
42
+ dictSizeSeed, 0, largeDictSize);
43
+
44
+ char* const dst = (char*)malloc(dstCapacity);
45
+ char* const dict = (char*)malloc(dictSize);
46
+ LZ4F_decompressOptions_t opts;
47
+ LZ4F_dctx* dctx;
48
+ LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION);
49
+
50
+ FUZZ_ASSERT(dctx);
51
+ FUZZ_ASSERT(dst);
52
+ FUZZ_ASSERT(dict);
53
+
54
+ /* Prepare the dictionary. The data doesn't matter for decompression. */
55
+ memset(dict, 0, dictSize);
56
+
57
+
58
+ /* Decompress using multiple configurations. */
59
+ memset(&opts, 0, sizeof(opts));
60
+ opts.stableDst = 0;
61
+ decompress(dctx, dst, dstCapacity, data, size, NULL, 0, &opts);
62
+ opts.stableDst = 1;
63
+ decompress(dctx, dst, dstCapacity, data, size, NULL, 0, &opts);
64
+ opts.stableDst = 0;
65
+ decompress(dctx, dst, dstCapacity, data, size, dict, dictSize, &opts);
66
+ opts.stableDst = 1;
67
+ decompress(dctx, dst, dstCapacity, data, size, dict, dictSize, &opts);
68
+
69
+ LZ4F_freeDecompressionContext(dctx);
70
+ free(dst);
71
+ free(dict);
72
+ FUZZ_dataProducer_free(producer);
73
+
74
+ return 0;
75
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * This fuzz target attempts to decompress the fuzzed data with the simple
3
+ * decompression function to ensure the decompressor never crashes.
4
+ */
5
+
6
+ #include <stddef.h>
7
+ #include <stdint.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ #include "fuzz_helpers.h"
12
+ #include "fuzz_data_producer.h"
13
+ #include "lz4.h"
14
+
15
+ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
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);
20
+
21
+ size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, 4 * size);
22
+ size_t const smallDictSize = size + 1;
23
+ size_t const largeDictSize = 64 * 1024 - 1;
24
+ size_t const dictSize = MAX(smallDictSize, largeDictSize);
25
+ char* const dst = (char*)malloc(dstCapacity);
26
+ char* const dict = (char*)malloc(dictSize + size);
27
+ char* const largeDict = dict;
28
+ char* const dataAfterDict = dict + dictSize;
29
+ char* const smallDict = dataAfterDict - smallDictSize;
30
+
31
+ FUZZ_ASSERT(dst);
32
+ FUZZ_ASSERT(dict);
33
+
34
+ /* Prepare the dictionary. The data doesn't matter for decompression. */
35
+ memset(dict, 0, dictSize);
36
+ memcpy(dataAfterDict, data, size);
37
+
38
+ /* Decompress using each possible dictionary configuration. */
39
+ /* No dictionary. */
40
+ LZ4_decompress_safe_usingDict((char const*)data, dst, size,
41
+ dstCapacity, NULL, 0);
42
+ /* Small external dictonary. */
43
+ LZ4_decompress_safe_usingDict((char const*)data, dst, size,
44
+ dstCapacity, smallDict, smallDictSize);
45
+ /* Large external dictionary. */
46
+ LZ4_decompress_safe_usingDict((char const*)data, dst, size,
47
+ dstCapacity, largeDict, largeDictSize);
48
+ /* Small prefix. */
49
+ LZ4_decompress_safe_usingDict((char const*)dataAfterDict, dst, size,
50
+ dstCapacity, smallDict, smallDictSize);
51
+ /* Large prefix. */
52
+ LZ4_decompress_safe_usingDict((char const*)data, dst, size,
53
+ dstCapacity, largeDict, largeDictSize);
54
+ /* Partial decompression. */
55
+ LZ4_decompress_safe_partial((char const*)data, dst, size,
56
+ dstCapacity, dstCapacity);
57
+ free(dst);
58
+ free(dict);
59
+ FUZZ_dataProducer_free(producer);
60
+
61
+ return 0;
62
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Fuzz target interface.
3
+ * Fuzz targets have some common parameters passed as macros during compilation.
4
+ * Check the documentation for each individual fuzzer for more parameters.
5
+ *
6
+ * @param FUZZ_RNG_SEED_SIZE:
7
+ * The number of bytes of the source to look at when constructing a seed
8
+ * for the deterministic RNG. These bytes are discarded before passing
9
+ * the data to lz4 functions. Every fuzzer initializes the RNG exactly
10
+ * once before doing anything else, even if it is unused.
11
+ * Default: 4.
12
+ * @param LZ4_DEBUG:
13
+ * This is a parameter for the lz4 library. Defining `LZ4_DEBUG=1`
14
+ * enables assert() statements in the lz4 library. Higher levels enable
15
+ * logging, so aren't recommended. Defining `LZ4_DEBUG=1` is
16
+ * recommended.
17
+ * @param LZ4_FORCE_MEMORY_ACCESS:
18
+ * This flag controls how the zstd library accesses unaligned memory.
19
+ * It can be undefined, or 0 through 2. If it is undefined, it selects
20
+ * the method to use based on the compiler. If testing with UBSAN set
21
+ * MEM_FORCE_MEMORY_ACCESS=0 to use the standard compliant method.
22
+ * @param FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
23
+ * This is the canonical flag to enable deterministic builds for fuzzing.
24
+ * Changes to zstd for fuzzing are gated behind this define.
25
+ * It is recommended to define this when building zstd for fuzzing.
26
+ */
27
+
28
+ #ifndef FUZZ_H
29
+ #define FUZZ_H
30
+
31
+ #ifndef FUZZ_RNG_SEED_SIZE
32
+ # define FUZZ_RNG_SEED_SIZE 4
33
+ #endif
34
+
35
+ #include <stddef.h>
36
+ #include <stdint.h>
37
+
38
+ #ifdef __cplusplus
39
+ extern "C" {
40
+ #endif
41
+
42
+ int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size);
43
+
44
+ #ifdef __cplusplus
45
+ }
46
+ #endif
47
+
48
+ #endif
@@ -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);
@@ -0,0 +1,94 @@
1
+ /*
2
+ * Copyright (c) 2016-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ */
9
+
10
+ /**
11
+ * Helper functions for fuzzing.
12
+ */
13
+
14
+ #ifndef FUZZ_HELPERS_H
15
+ #define FUZZ_HELPERS_H
16
+
17
+ #include "fuzz.h"
18
+ #include "xxhash.h"
19
+ #include <stdint.h>
20
+ #include <stdio.h>
21
+ #include <stdlib.h>
22
+
23
+ #ifdef __cplusplus
24
+ extern "C" {
25
+ #endif
26
+
27
+ #define LZ4_COMMONDEFS_ONLY
28
+ #ifndef LZ4_SRC_INCLUDED
29
+ #include "lz4.c" /* LZ4_count, constants, mem */
30
+ #endif
31
+
32
+ #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
33
+ #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
34
+
35
+ #define FUZZ_QUOTE_IMPL(str) #str
36
+ #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
37
+
38
+ /**
39
+ * Asserts for fuzzing that are always enabled.
40
+ */
41
+ #define FUZZ_ASSERT_MSG(cond, msg) \
42
+ ((cond) ? (void)0 \
43
+ : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
44
+ __LINE__, FUZZ_QUOTE(cond), (msg)), \
45
+ abort()))
46
+ #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
47
+
48
+ #if defined(__GNUC__)
49
+ #define FUZZ_STATIC static __inline __attribute__((unused))
50
+ #elif defined(__cplusplus) || \
51
+ (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
52
+ #define FUZZ_STATIC static inline
53
+ #elif defined(_MSC_VER)
54
+ #define FUZZ_STATIC static __inline
55
+ #else
56
+ #define FUZZ_STATIC static
57
+ #endif
58
+
59
+ /**
60
+ * Deterministically constructs a seed based on the fuzz input.
61
+ * Consumes up to the first FUZZ_RNG_SEED_SIZE bytes of the input.
62
+ */
63
+ FUZZ_STATIC uint32_t FUZZ_seed(uint8_t const **src, size_t* size) {
64
+ uint8_t const *data = *src;
65
+ size_t const toHash = MIN(FUZZ_RNG_SEED_SIZE, *size);
66
+ *size -= toHash;
67
+ *src += toHash;
68
+ return XXH32(data, toHash, 0);
69
+ }
70
+
71
+ #define FUZZ_rotl32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
72
+
73
+ FUZZ_STATIC uint32_t FUZZ_rand(uint32_t *state) {
74
+ static const uint32_t prime1 = 2654435761U;
75
+ static const uint32_t prime2 = 2246822519U;
76
+ uint32_t rand32 = *state;
77
+ rand32 *= prime1;
78
+ rand32 += prime2;
79
+ rand32 = FUZZ_rotl32(rand32, 13);
80
+ *state = rand32;
81
+ return rand32 >> 5;
82
+ }
83
+
84
+ /* Returns a random numer in the range [min, max]. */
85
+ FUZZ_STATIC uint32_t FUZZ_rand32(uint32_t *state, uint32_t min, uint32_t max) {
86
+ uint32_t random = FUZZ_rand(state);
87
+ return min + (random % (max - min + 1));
88
+ }
89
+
90
+ #ifdef __cplusplus
91
+ }
92
+ #endif
93
+
94
+ #endif