OpenVPN 3 Core Library
Loading...
Searching...
No Matches
lzoasym.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_COMPRESS_LZOASYM_H
13#define OPENVPN_COMPRESS_LZOASYM_H
14
17
18// Implement asymmetrical LZO compression (only uncompress, don't compress)
19// Should only be included by lzoselect.hpp
20
21namespace openvpn {
22
24{
25 public:
26 // magic number for LZO compression
27 enum
28 {
31 };
32
33 OPENVPN_SIMPLE_EXCEPTION(lzo_init_failed);
34
37 const bool support_swap_arg,
38 const bool asym_arg) // we are always asymmetrical, regardless of setting
40 support_swap(support_swap_arg)
41 {
42 OVPN_LOG_INFO("LZO-ASYM init swap=" << support_swap_arg << " asym=" << asym_arg);
43 }
44
45 static void init_static()
46 {
47 }
48
49 const char *name() const override
50 {
51 return "lzo-asym";
52 }
53
55 {
56 // initialize work buffer
58
59 // do uncompress
60 const int err = lzo_asym_impl::lzo1x_decompress_safe(buf.c_data(), buf.size(), work.data(), &zlen);
62 {
63 error(buf);
64 return;
65 }
66 OVPN_LOG_VERBOSE("LZO-ASYM uncompress " << buf.size() << " -> " << zlen);
67 work.set_size(zlen);
68 buf.swap(work);
69 }
70
71 void compress(BufferAllocated &buf, const bool hint) override
72 {
73 // skip null packets
74 if (!buf.size())
75 return;
76
77 // indicate that we didn't compress
78 if (support_swap)
80 else
82 }
83
84 void decompress(BufferAllocated &buf) override
85 {
86 // skip null packets
87 if (!buf.size())
88 return;
89
90 const unsigned char c = buf.pop_front();
91 switch (c)
92 {
94 do_unswap(buf);
95 [[fallthrough]];
96 case NO_COMPRESS:
97 break;
99 do_unswap(buf);
100 [[fallthrough]];
101 case LZO_COMPRESS:
102 decompress_work(buf);
103 break;
104 default:
105 error(buf); // unknown op
106 }
107 }
108
109 private:
110 const bool support_swap;
112};
113
114} // namespace openvpn
115
116#endif // OPENVPN_COMPRESS_LZOASYM_H
void swap(BufferAllocatedType< T_ > &other)
Swaps the contents of this BufferAllocatedType object with another BufferAllocatedType object.
Definition buffer.hpp:1799
BufferAllocated work
Definition lzoasym.hpp:111
void decompress_work(BufferAllocated &buf)
Definition lzoasym.hpp:54
CompressLZOAsym(const Frame::Ptr &frame, const SessionStats::Ptr &stats, const bool support_swap_arg, const bool asym_arg)
Definition lzoasym.hpp:35
void decompress(BufferAllocated &buf) override
Definition lzoasym.hpp:84
void compress(BufferAllocated &buf, const bool hint) override
Definition lzoasym.hpp:71
OPENVPN_SIMPLE_EXCEPTION(lzo_init_failed)
const char * name() const override
Definition lzoasym.hpp:49
static void init_static()
Definition lzoasym.hpp:45
void do_swap(Buffer &buf, unsigned char op)
Definition compress.hpp:82
void error(BufferAllocated &buf)
Definition compress.hpp:76
void do_unswap(Buffer &buf)
Definition compress.hpp:93
Frame::Ptr frame
Definition compress.hpp:127
SessionStats::Ptr stats
Definition compress.hpp:128
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1194
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1242
T * data()
Get a mutable pointer to the start of the array.
Definition buffer.hpp:1450
T pop_front()
Removes and returns the first element from the buffer.
Definition buffer.hpp:1256
void push_front(const T &value)
Append a T object to the array, with possible resize.
Definition buffer.hpp:1490
void set_size(const size_t size)
After an external method, operating on the array as a mutable unsigned char buffer,...
Definition buffer.hpp:1384
size_t prepare(const unsigned int context, Buffer &buf) const
Definition frame.hpp:266
#define OVPN_LOG_INFO(args)
Definition logger.hpp:224
#define OVPN_LOG_VERBOSE(args)
Definition logger.hpp:225
int lzo1x_decompress_safe(const unsigned char *input, size_t input_length, unsigned char *output, size_t *output_length)