File: lz4.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (234 lines) | stat: -rw-r--r-- 5,953 bytes parent folder | download
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

#ifndef OPENVPN_COMPRESS_LZ4_H
#define OPENVPN_COMPRESS_LZ4_H

// Implement LZ4 compression.
// Should only be included by compress.hpp

#include <algorithm> // for std::max

#include <openvpn/common/numeric_util.hpp>

#include <lz4.h>

namespace openvpn {

class CompressLZ4Base : public Compress
{
  protected:
    CompressLZ4Base(const Frame::Ptr &frame, const SessionStats::Ptr &stats)
        : Compress(frame, stats)
    {
    }

    bool do_decompress(BufferAllocated &buf)
    {
        auto prepRes = frame->prepare(Frame::DECOMPRESS_WORK, work);
        if (!numeric_util::is_safe_conversion<const int>(prepRes))
            return false;

        // initialize work buffer
        auto payload_size = static_cast<const int>(prepRes);

        // do uncompress
        const int decomp_size = LZ4_decompress_safe((const char *)buf.c_data(),
                                                    (char *)work.data(),
                                                    (int)buf.size(),
                                                    payload_size);
        if (decomp_size < 0)
        {
            error(buf);
            return false;
        }
        OVPN_LOG_VERBOSE("LZ4 uncompress " << buf.size() << " -> " << decomp_size);
        work.set_size(decomp_size);
        buf.swap(work);
        return true;
    }

    bool do_compress(BufferAllocated &buf)
    {
        // initialize work buffer
        frame->prepare(Frame::COMPRESS_WORK, work);

        // verify that input data length is not too large
        if (lz4_extra_buffer(buf.size()) > work.max_size())
        {
            error(buf);
            return false;
        }

        // do compress
        const unsigned int comp_size = LZ4_compress_default((const char *)buf.c_data(),
                                                            (char *)work.data(),
                                                            (int)buf.size(),
                                                            (int)work.capacity());

        // did compression actually reduce data length?
        if (comp_size < buf.size())
        {
            if (comp_size <= 0)
            {
                error(buf);
                return false;
            }
            OVPN_LOG_VERBOSE("LZ4 compress " << buf.size() << " -> " << comp_size);
            work.set_size(comp_size);
            buf.swap(work);
            return true;
        }
        else
            return false;
    }

    // Worst case size expansion on compress.
    // Official LZ4 worst-case size expansion alg is
    // LZ4_COMPRESSBOUND macro in lz4.h.
    // However we optimize it slightly here to lose the integer division
    // when len < 65535.
    size_t lz4_extra_buffer(const size_t len)
    {
        if (likely(len < 65535))
            return len + len / 256 + 17;
        else
            return len + len / 255 + 16;
    }

    BufferAllocated work;
};

class CompressLZ4 : public CompressLZ4Base
{
    // magic number for LZ4 compression
    enum
    {
        LZ4_COMPRESS = 0x69,
    };

  public:
    CompressLZ4(const Frame::Ptr &frame, const SessionStats::Ptr &stats, const bool asym_arg)
        : CompressLZ4Base(frame, stats),
          asym(asym_arg)
    {
        OVPN_LOG_INFO("LZ4 init asym=" << asym_arg);
    }

    const char *name() const override
    {
        return "lz4";
    }

    void compress(BufferAllocated &buf, const bool hint) override
    {
        // skip null packets
        if (!buf.size())
            return;

        if (hint && !asym)
        {
            if (do_compress(buf))
            {
                do_swap(buf, LZ4_COMPRESS);
                return;
            }
        }

        // indicate that we didn't compress
        do_swap(buf, NO_COMPRESS_SWAP);
    }

    void decompress(BufferAllocated &buf) override
    {
        // skip null packets
        if (!buf.size())
            return;

        const unsigned char c = buf.pop_front();
        switch (c)
        {
        case NO_COMPRESS_SWAP:
            do_unswap(buf);
            break;
        case LZ4_COMPRESS:
            do_unswap(buf);
            do_decompress(buf);
            break;
        default:
            error(buf); // unknown op
        }
    }

  private:
    const bool asym;
};

class CompressLZ4v2 : public CompressLZ4Base
{
  public:
    CompressLZ4v2(const Frame::Ptr &frame, const SessionStats::Ptr &stats, const bool asym_arg)
        : CompressLZ4Base(frame, stats),
          asym(asym_arg)
    {
        OVPN_LOG_INFO("LZ4v2 init asym=" << asym_arg);
    }

    const char *name() const override
    {
        return "lz4v2";
    }

    void compress(BufferAllocated &buf, const bool hint) override
    {
        // skip null packets
        if (!buf.size())
            return;

        if (hint && !asym)
        {
            if (do_compress(buf))
            {
                v2_push(buf, OVPN_COMPv2_LZ4);
                return;
            }
        }

        // indicate that we didn't compress
        v2_push(buf, OVPN_COMPv2_NONE);
    }

    void decompress(BufferAllocated &buf) override
    {
        // skip null packets
        if (!buf.size())
            return;

        const int c = v2_pull(buf);
        switch (c)
        {
        case OVPN_COMPv2_NONE:
            break;
        case OVPN_COMPv2_LZ4:
            do_decompress(buf);
            break;
        default:
            error(buf); // unknown op
        }
    }

  private:
    const bool asym;
};

} // namespace openvpn

#endif