summaryrefslogtreecommitdiff
path: root/neozip/test/compressible_data_p.h
blob: e345bb9c9185b90e05cd0d28c274efe7820add86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* compressible_data_p.h -- generate compressible data
 * Copyright (C) 2025 Hans Kristian Rosbach
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

#ifndef COMPRESSIBLE_DATA_P_H
#define COMPRESSIBLE_DATA_P_H


static inline size_t append_raw(uint8_t *dest, size_t size, const void *src, size_t len) {
    if (len > size) len = size;
    if (len == 0) return 0;
    memcpy(dest, src, len);
    return len;
}
static inline size_t append_str(uint8_t *dest, size_t size, const char *src) {
    return append_raw(dest, size, src, strlen(src));
}
static inline size_t append_uint8_t(uint8_t *dest, size_t size, uint8_t src) {
    return append_raw(dest, size, &src, 1);
}

// Alloc and initialize buffer with highly compressible data,
// interspersed with small amounts of random data and 3-byte matches.
static uint8_t *gen_compressible_data(size_t bufsize) {
    const char teststr1[42] = "Hello hello World broken Test tast mello.";
    const char teststr2[32] = "llollollollollo He Te me orld";
    const char teststr3[4] = "bro";
    int loops = 0;

    uint8_t *buffer = (uint8_t *)malloc(bufsize);
    if (buffer == NULL) {
        return NULL;
    }

    for (size_t pos = 0; pos < bufsize; ) {
        pos += append_str(buffer+pos, bufsize-pos, teststr1);
        pos += append_uint8_t(buffer+pos, bufsize-pos, (uint8_t)(rand() & 0xFF));
        // Every so often, add a few other little bits to break the pattern
        if (loops % 13 == 0) {
            pos += append_str(buffer+pos, bufsize-pos, teststr3);
            pos += append_uint8_t(buffer+pos, bufsize-pos, (uint8_t)(rand() & 0xFF));
        }
        if (loops % 300 == 0) { // Only found once or twice per window
            pos += append_str(buffer+pos, bufsize-pos, teststr2);
        }
        loops++;
    }
    if (bufsize > 0) {
        buffer[bufsize-1] = 0;
    }
    return buffer;
}
#endif