summaryrefslogtreecommitdiffstats
path: root/src/streaming/compression_gzip.c
blob: c4ef3af05ee2d384ca0fb87f347c0cf4f0ebeb5c (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: GPL-3.0-or-later

#include "compression_gzip.h"
#include <zlib.h>

void rrdpush_compressor_init_gzip(struct compressor_state *state) {
    if (!state->initialized) {
        state->initialized = true;

        // Initialize deflate stream
        z_stream *strm = state->stream = (z_stream *) mallocz(sizeof(z_stream));
        strm->zalloc = Z_NULL;
        strm->zfree = Z_NULL;
        strm->opaque = Z_NULL;

        if(state->level < Z_BEST_SPEED)
            state->level = Z_BEST_SPEED;

        if(state->level > Z_BEST_COMPRESSION)
            state->level = Z_BEST_COMPRESSION;

        // int r = deflateInit2(strm, Z_BEST_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
        int r = deflateInit2(strm, state->level, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
        if (r != Z_OK) {
            netdata_log_error("Failed to initialize deflate with error: %d", r);
            freez(state->stream);
            state->initialized = false;
            return;
        }

    }
}

void rrdpush_compressor_destroy_gzip(struct compressor_state *state) {
    if (state->stream) {
        deflateEnd(state->stream);
        freez(state->stream);
        state->stream = NULL;
    }
}

size_t rrdpush_compress_gzip(struct compressor_state *state, const char *data, size_t size, const char **out) {
    if (unlikely(!state || !size || !out))
        return 0;

    simple_ring_buffer_make_room(&state->output, deflateBound(state->stream, size));

    z_stream *strm = state->stream;
    strm->avail_in = (uInt)size;
    strm->next_in = (Bytef *)data;
    strm->avail_out = (uInt)state->output.size;
    strm->next_out = (Bytef *)state->output.data;

    int ret = deflate(strm, Z_SYNC_FLUSH);
    if (ret != Z_OK && ret != Z_STREAM_END) {
        netdata_log_error("STREAM: deflate() failed with error %d", ret);
        return 0;
    }

    if(strm->avail_in != 0) {
        netdata_log_error("STREAM: deflate() did not use all the input buffer, %u bytes out of %zu remain",
                          strm->avail_in, size);
        return 0;
    }

    if(strm->avail_out == 0) {
        netdata_log_error("STREAM: deflate() needs a bigger output buffer than the one we provided "
                          "(output buffer %zu bytes, compressed payload %zu bytes)",
                          state->output.size, size);
        return 0;
    }

    size_t compressed_data_size = state->output.size - strm->avail_out;

    if(compressed_data_size == 0) {
        netdata_log_error("STREAM: deflate() did not produce any output "
                          "(output buffer %zu bytes, compressed payload %zu bytes)",
                          state->output.size, size);
        return 0;
    }

    state->sender_locked.total_compressions++;
    state->sender_locked.total_uncompressed += size;
    state->sender_locked.total_compressed += compressed_data_size;

    *out = state->output.data;
    return compressed_data_size;
}

void rrdpush_decompressor_init_gzip(struct decompressor_state *state) {
    if (!state->initialized) {
        state->initialized = true;

        // Initialize inflate stream
        z_stream *strm = state->stream = (z_stream *)mallocz(sizeof(z_stream));
        strm->zalloc = Z_NULL;
        strm->zfree = Z_NULL;
        strm->opaque = Z_NULL;

        int r = inflateInit2(strm, 15 + 16);
        if (r != Z_OK) {
            netdata_log_error("Failed to initialize inflateInit2() with error: %d", r);
            freez(state->stream);
            state->initialized = false;
            return;
        }

        simple_ring_buffer_make_room(&state->output, COMPRESSION_MAX_CHUNK);
    }
}

void rrdpush_decompressor_destroy_gzip(struct decompressor_state *state) {
    if (state->stream) {
        inflateEnd(state->stream);
        freez(state->stream);
        state->stream = NULL;
    }
}

size_t rrdpush_decompress_gzip(struct decompressor_state *state, const char *compressed_data, size_t compressed_size) {
    if (unlikely(!state || !compressed_data || !compressed_size))
        return 0;

    // The state.output ring buffer is always EMPTY at this point,
    // meaning that (state->output.read_pos == state->output.write_pos)
    // However, THEY ARE NOT ZERO.

    z_stream *strm = state->stream;
    strm->avail_in = (uInt)compressed_size;
    strm->next_in = (Bytef *)compressed_data;
    strm->avail_out = (uInt)state->output.size;
    strm->next_out = (Bytef *)state->output.data;

    int ret = inflate(strm, Z_SYNC_FLUSH);
    if (ret != Z_STREAM_END && ret != Z_OK) {
        netdata_log_error("RRDPUSH DECOMPRESS: inflate() failed with error %d", ret);
        return 0;
    }

    if(strm->avail_in != 0) {
        netdata_log_error("RRDPUSH DECOMPRESS: inflate() did not use all compressed data we provided "
                          "(compressed payload %zu bytes, remaining to be uncompressed %u)"
                          , compressed_size, strm->avail_in);
        return 0;
    }

    if(strm->avail_out == 0) {
        netdata_log_error("RRDPUSH DECOMPRESS: inflate() needs a bigger output buffer than the one we provided "
                          "(compressed payload %zu bytes, output buffer size %zu bytes)"
                          , compressed_size, state->output.size);
        return 0;
    }

    size_t decompressed_size = state->output.size - strm->avail_out;

    state->output.read_pos = 0;
    state->output.write_pos = decompressed_size;

    state->total_compressed += compressed_size;
    state->total_uncompressed += decompressed_size;
    state->total_compressions++;

    return decompressed_size;
}