Coverage Report

Created: 2025-08-24 06:45

/src/lz4/ossfuzz/fuzz_data_producer.c
Line
Count
Source (jump to first uncovered line)
1
#include "fuzz_data_producer.h"
2
3
struct FUZZ_dataProducer_s{
4
  const uint8_t *data;
5
  size_t size;
6
};
7
8
2.09k
FUZZ_dataProducer_t* FUZZ_dataProducer_create(const uint8_t* data, size_t size) {
9
2.09k
  FUZZ_dataProducer_t* const producer = malloc(sizeof(FUZZ_dataProducer_t));
10
11
2.09k
  FUZZ_ASSERT(producer != NULL);
12
13
2.09k
  producer->data = data;
14
2.09k
  producer->size = size;
15
2.09k
  return producer;
16
2.09k
}
17
18
2.09k
void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); }
19
20
2.09k
uint32_t FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t *producer) {
21
2.09k
    const uint8_t* data = producer->data;
22
2.09k
    const size_t size = producer->size;
23
2.09k
    if (size == 0) {
24
0
        return 0;
25
2.09k
    } else if (size < 4) {
26
16
        producer->size -= 1;
27
16
        return (uint32_t)data[size - 1];
28
2.07k
    } else {
29
2.07k
        producer->size -= 4;
30
2.07k
        return *(data + size - 4);
31
2.07k
    }
32
2.09k
}
33
34
uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max)
35
2.09k
{
36
2.09k
    uint32_t range = max - min;
37
2.09k
    if (range == 0xffffffff) {
38
0
      return seed;
39
0
    }
40
2.09k
    return min + seed % (range + 1);
41
2.09k
}
42
43
uint32_t FUZZ_dataProducer_range32(FUZZ_dataProducer_t* producer,
44
    uint32_t min, uint32_t max)
45
0
{
46
0
    size_t const seed = FUZZ_dataProducer_retrieve32(producer);
47
0
    return FUZZ_getRange_from_uint32(seed, min, max);
48
0
}
49
50
LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer)
51
0
{
52
0
    LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO;
53
0
    info.blockSizeID = FUZZ_dataProducer_range32(producer, LZ4F_max64KB - 1, LZ4F_max4MB);
54
0
    if (info.blockSizeID < LZ4F_max64KB) {
55
0
        info.blockSizeID = LZ4F_default;
56
0
    }
57
0
    info.blockMode = FUZZ_dataProducer_range32(producer, LZ4F_blockLinked, LZ4F_blockIndependent);
58
0
    info.contentChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noContentChecksum,
59
0
                                           LZ4F_contentChecksumEnabled);
60
0
    info.blockChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noBlockChecksum,
61
0
                                         LZ4F_blockChecksumEnabled);
62
0
    return info;
63
0
}
64
65
LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer)
66
0
{
67
0
    LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES;
68
0
    prefs.frameInfo = FUZZ_dataProducer_frameInfo(producer);
69
0
    prefs.compressionLevel = FUZZ_dataProducer_range32(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3;
70
0
    prefs.autoFlush = FUZZ_dataProducer_range32(producer, 0, 1);
71
0
    prefs.favorDecSpeed = FUZZ_dataProducer_range32(producer, 0, 1);
72
0
    return prefs;
73
0
}
74
75
2.09k
size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer){
76
2.09k
  return producer->size;
77
2.09k
}