OpenVPN 3 Core Library
Loading...
Searching...
No Matches
snappy.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_SNAPPY_H
13#define OPENVPN_COMPRESS_SNAPPY_H
14
15// Implement Snappy compression.
16// Should only be included by compress.hpp
17
18#include <snappy.h>
19
20namespace openvpn {
21
23{
24 // magic number for Snappy compression
25 enum
26 {
28 };
29
30 public:
31 CompressSnappy(const Frame::Ptr &frame, const SessionStats::Ptr &stats, const bool asym_arg)
33 asym(asym_arg)
34 {
35 OVPN_LOG_INFO("SNAPPY init asym=" << asym_arg);
36 }
37
38 const char *name() const override
39 {
40 return "snappy";
41 }
42
43 void compress(BufferAllocated &buf, const bool hint) override
44 {
45 // skip null packets
46 if (!buf.size())
47 return;
48
49 if (hint && !asym)
50 {
51 // initialize work buffer
53
54 // verify that input data length is not too large
55 if (snappy::MaxCompressedLength(buf.size()) > work.max_size())
56 {
57 error(buf);
58 return;
59 }
60
61 // do compress
62 size_t comp_size;
63 snappy::RawCompress((const char *)buf.c_data(), buf.size(), (char *)work.data(), &comp_size);
64
65 // did compression actually reduce data length?
66 if (comp_size < buf.size())
67 {
68 OVPN_LOG_VERBOSE("SNAPPY compress " << buf.size() << " -> " << comp_size);
69 work.set_size(comp_size);
71 buf.swap(work);
72 return;
73 }
74 }
75
76 // indicate that we didn't compress
78 }
79
80 void decompress(BufferAllocated &buf) override
81 {
82 // skip null packets
83 if (!buf.size())
84 return;
85
86 const unsigned char c = buf.pop_front();
87 switch (c)
88 {
90 do_unswap(buf);
91 break;
92 case SNAPPY_COMPRESS:
93 {
94 do_unswap(buf);
95
96 // initialize work buffer
97 const size_t payload_size = frame->prepare(Frame::DECOMPRESS_WORK, work);
98
99 // do uncompress
100 size_t decomp_size;
101 if (!snappy::GetUncompressedLength((const char *)buf.c_data(), buf.size(), &decomp_size)
102 || decomp_size > payload_size)
103 {
104 error(buf);
105 break;
106 }
107 if (!snappy::RawUncompress((const char *)buf.c_data(), buf.size(), (char *)work.data()))
108 {
109 error(buf);
110 break;
111 }
112 OVPN_LOG_VERBOSE("SNAPPY uncompress " << buf.size() << " -> " << decomp_size);
113 work.set_size(decomp_size);
114 buf.swap(work);
115 }
116 break;
117 default:
118 error(buf); // unknown op
119 break;
120 }
121 }
122
123 private:
124 const bool asym;
126};
127
128} // namespace openvpn
129
130#endif // OPENVPN_COMPRESS_SNAPPY_H
void swap(BufferAllocatedType< T_ > &other)
Swaps the contents of this BufferAllocatedType object with another BufferAllocatedType object.
Definition buffer.hpp:1799
const char * name() const override
Definition snappy.hpp:38
void decompress(BufferAllocated &buf) override
Definition snappy.hpp:80
BufferAllocated work
Definition snappy.hpp:125
CompressSnappy(const Frame::Ptr &frame, const SessionStats::Ptr &stats, const bool asym_arg)
Definition snappy.hpp:31
void compress(BufferAllocated &buf, const bool hint) override
Definition snappy.hpp:43
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 max_size() const
Return the maximum allowable size value in T objects given the current offset (without considering re...
Definition buffer.hpp:1377
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 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