2014-07-21 05:22:06 +02:00
|
|
|
// 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.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2020-03-04 20:48:54 +01:00
|
|
|
// Copyright (C) 2012-2020 OpenVPN Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-21 21:42:20 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
2014-07-21 05:22:06 +02:00
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-12-21 21:42:20 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2014-07-21 05:22:06 +02:00
|
|
|
//
|
2017-12-21 21:42:20 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2014-07-21 05:22:06 +02:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Base class and factory for compression/decompression objects.
|
|
|
|
// Currently we support LZO, Snappy, and LZ4 implementations.
|
|
|
|
|
2011-12-14 12:34:33 +01:00
|
|
|
#ifndef OPENVPN_COMPRESS_COMPRESS_H
|
|
|
|
#define OPENVPN_COMPRESS_COMPRESS_H
|
|
|
|
|
2015-05-17 21:27:34 +02:00
|
|
|
#include <openvpn/common/size.hpp>
|
2011-12-14 12:34:33 +01:00
|
|
|
#include <openvpn/common/exception.hpp>
|
|
|
|
#include <openvpn/common/rc.hpp>
|
2014-12-21 18:32:37 +01:00
|
|
|
#include <openvpn/common/likely.hpp>
|
2011-12-14 12:34:33 +01:00
|
|
|
#include <openvpn/buffer/buffer.hpp>
|
|
|
|
#include <openvpn/frame/frame.hpp>
|
2012-02-04 11:24:54 +01:00
|
|
|
#include <openvpn/log/sessionstats.hpp>
|
2011-12-14 12:34:33 +01:00
|
|
|
|
2012-09-09 01:02:09 +02:00
|
|
|
#define OPENVPN_LOG_COMPRESS(x)
|
|
|
|
#define OPENVPN_LOG_COMPRESS_VERBOSE(x)
|
|
|
|
|
2012-01-31 12:15:21 +01:00
|
|
|
#if defined(OPENVPN_DEBUG_COMPRESS)
|
2012-09-09 01:02:09 +02:00
|
|
|
#if OPENVPN_DEBUG_COMPRESS >= 1
|
|
|
|
#undef OPENVPN_LOG_COMPRESS
|
2012-01-31 12:15:21 +01:00
|
|
|
#define OPENVPN_LOG_COMPRESS(x) OPENVPN_LOG(x)
|
2012-09-09 01:02:09 +02:00
|
|
|
#endif
|
|
|
|
#if OPENVPN_DEBUG_COMPRESS >= 2
|
|
|
|
#undef OPENVPN_LOG_COMPRESS_VERBOSE
|
|
|
|
#define OPENVPN_LOG_COMPRESS_VERBOSE(x) OPENVPN_LOG(x)
|
|
|
|
#endif
|
2012-01-31 12:15:21 +01:00
|
|
|
#endif
|
|
|
|
|
2011-12-14 12:34:33 +01:00
|
|
|
namespace openvpn {
|
|
|
|
class Compress : public RC<thread_unsafe_refcount>
|
|
|
|
{
|
|
|
|
public:
|
2015-05-18 05:26:53 +02:00
|
|
|
typedef RCPtr<Compress> Ptr;
|
2011-12-14 12:34:33 +01:00
|
|
|
|
2012-09-10 02:01:49 +02:00
|
|
|
// Compressor name
|
|
|
|
virtual const char *name() const = 0;
|
|
|
|
|
2012-01-31 12:15:21 +01:00
|
|
|
// Compression method implemented by underlying compression class.
|
2011-12-15 09:48:14 +01:00
|
|
|
// hint should normally be true to compress the data. If hint is
|
2012-01-31 12:15:21 +01:00
|
|
|
// false, the data may be uncompressible or already compressed,
|
|
|
|
// so method shouldn't attempt compression.
|
2011-12-15 09:48:14 +01:00
|
|
|
virtual void compress(BufferAllocated& buf, const bool hint) = 0;
|
|
|
|
|
|
|
|
// Decompression method implemented by underlying compression class.
|
2011-12-14 12:34:33 +01:00
|
|
|
virtual void decompress(BufferAllocated& buf) = 0;
|
|
|
|
|
|
|
|
protected:
|
2012-01-31 12:15:21 +01:00
|
|
|
// magic numbers to indicate no compression
|
|
|
|
enum {
|
|
|
|
NO_COMPRESS = 0xFA,
|
|
|
|
NO_COMPRESS_SWAP = 0xFB, // for better alignment handling, replace this byte with last byte of packet
|
|
|
|
};
|
|
|
|
|
2014-12-21 18:32:37 +01:00
|
|
|
// Compress V2 constants
|
|
|
|
enum {
|
|
|
|
COMPRESS_V2_ESCAPE=0x50,
|
|
|
|
|
|
|
|
// Compression algs
|
|
|
|
OVPN_COMPv2_NONE=0,
|
|
|
|
OVPN_COMPv2_LZ4=1,
|
|
|
|
};
|
|
|
|
|
2011-12-14 12:34:33 +01:00
|
|
|
Compress(const Frame::Ptr& frame_arg,
|
2012-02-04 11:24:54 +01:00
|
|
|
const SessionStats::Ptr& stats_arg)
|
2011-12-14 12:34:33 +01:00
|
|
|
: frame(frame_arg), stats(stats_arg) {}
|
|
|
|
|
2012-01-31 12:15:21 +01:00
|
|
|
void error(BufferAllocated& buf)
|
|
|
|
{
|
2012-02-04 11:24:54 +01:00
|
|
|
stats->error(Error::COMPRESS_ERROR);
|
2012-01-31 12:15:21 +01:00
|
|
|
buf.reset_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_swap(Buffer& buf, unsigned char op)
|
|
|
|
{
|
|
|
|
if (buf.size())
|
|
|
|
{
|
|
|
|
buf.push_back(buf[0]);
|
|
|
|
buf[0] = op;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
buf.push_back(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_unswap(Buffer& buf)
|
|
|
|
{
|
|
|
|
if (buf.size() >= 2)
|
|
|
|
{
|
|
|
|
const unsigned char first = buf.pop_back();
|
|
|
|
buf.push_front(first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 18:32:37 +01:00
|
|
|
// Push a COMPRESS_V2 header byte (value).
|
|
|
|
// Pass value == 0 to omit push.
|
|
|
|
void v2_push(Buffer& buf, int value)
|
|
|
|
{
|
|
|
|
unsigned char uc = buf[0];
|
|
|
|
if (value == 0 && uc != COMPRESS_V2_ESCAPE)
|
|
|
|
return;
|
|
|
|
unsigned char *esc = buf.prepend_alloc(2);
|
|
|
|
esc[0] = COMPRESS_V2_ESCAPE;
|
|
|
|
esc[1] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull a COMPRESS_V2 header byte.
|
|
|
|
// Returns the compress op (> 0) on success.
|
|
|
|
// Returns 0 if no compress op.
|
|
|
|
int v2_pull(Buffer& buf)
|
|
|
|
{
|
|
|
|
unsigned char uc = buf[0];
|
|
|
|
if (uc != COMPRESS_V2_ESCAPE)
|
|
|
|
return 0;
|
|
|
|
uc = buf[1];
|
|
|
|
buf.advance(2);
|
|
|
|
return uc;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:34:33 +01:00
|
|
|
Frame::Ptr frame;
|
2012-02-04 11:24:54 +01:00
|
|
|
SessionStats::Ptr stats;
|
2011-12-14 12:34:33 +01:00
|
|
|
};
|
2014-12-21 18:32:37 +01:00
|
|
|
}
|
2011-12-14 12:34:33 +01:00
|
|
|
|
|
|
|
// include compressor implementations here
|
2012-01-31 12:15:21 +01:00
|
|
|
#include <openvpn/compress/compnull.hpp>
|
|
|
|
#include <openvpn/compress/compstub.hpp>
|
|
|
|
|
2012-09-11 10:45:27 +02:00
|
|
|
#ifndef NO_LZO
|
2012-09-08 03:36:54 +02:00
|
|
|
#include <openvpn/compress/lzoselect.hpp>
|
2012-09-11 10:45:27 +02:00
|
|
|
#endif
|
2012-01-31 12:15:21 +01:00
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
#include <openvpn/compress/lz4.hpp>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SNAPPY
|
|
|
|
#include <openvpn/compress/snappy.hpp>
|
|
|
|
#endif
|
2011-12-14 12:34:33 +01:00
|
|
|
|
|
|
|
namespace openvpn {
|
|
|
|
class CompressContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
NONE,
|
2012-01-31 12:15:21 +01:00
|
|
|
COMP_STUB, // generic compression stub
|
2014-12-21 18:32:37 +01:00
|
|
|
COMP_STUBv2, // generic compression stub using v2 protocol
|
2012-01-31 12:15:21 +01:00
|
|
|
ANY, // placeholder for any method on client, before server assigns it
|
|
|
|
ANY_LZO, // placeholder for LZO or LZO_STUB methods on client, before server assigns it
|
2011-12-14 12:34:33 +01:00
|
|
|
LZO,
|
2012-01-31 12:15:21 +01:00
|
|
|
LZO_SWAP,
|
|
|
|
LZO_STUB,
|
|
|
|
LZ4,
|
2014-12-21 18:32:37 +01:00
|
|
|
LZ4v2,
|
2012-01-31 12:15:21 +01:00
|
|
|
SNAPPY,
|
2011-12-14 12:34:33 +01:00
|
|
|
};
|
|
|
|
|
2012-01-31 12:15:21 +01:00
|
|
|
OPENVPN_SIMPLE_EXCEPTION(compressor_unavailable);
|
2011-12-14 12:34:33 +01:00
|
|
|
|
2017-08-19 08:07:29 +02:00
|
|
|
CompressContext() {}
|
2011-12-14 12:34:33 +01:00
|
|
|
|
2017-08-19 08:07:29 +02:00
|
|
|
CompressContext(const Type t, const bool asym)
|
2012-11-23 07:18:43 +01:00
|
|
|
: asym_(asym) // asym indicates asymmetrical compression where only downlink is compressed
|
2012-01-31 12:15:21 +01:00
|
|
|
{
|
|
|
|
if (!compressor_available(t))
|
|
|
|
throw compressor_unavailable();
|
|
|
|
type_ = t;
|
|
|
|
}
|
|
|
|
|
2014-12-21 18:32:37 +01:00
|
|
|
Type type() const { return type_; }
|
|
|
|
bool asym() const { return asym_; }
|
|
|
|
|
|
|
|
unsigned int extra_payload_bytes() const
|
|
|
|
{
|
|
|
|
switch (type_)
|
|
|
|
{
|
|
|
|
case NONE:
|
|
|
|
return 0;
|
|
|
|
case COMP_STUBv2:
|
|
|
|
case LZ4v2:
|
|
|
|
return 2; // worst case
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-14 12:34:33 +01:00
|
|
|
|
2012-02-04 11:24:54 +01:00
|
|
|
Compress::Ptr new_compressor(const Frame::Ptr& frame, const SessionStats::Ptr& stats)
|
2011-12-14 12:34:33 +01:00
|
|
|
{
|
|
|
|
switch (type_)
|
|
|
|
{
|
2012-01-31 12:15:21 +01:00
|
|
|
case NONE:
|
|
|
|
return new CompressNull(frame, stats);
|
|
|
|
case ANY:
|
|
|
|
case ANY_LZO:
|
|
|
|
case LZO_STUB:
|
|
|
|
return new CompressStub(frame, stats, false);
|
|
|
|
case COMP_STUB:
|
|
|
|
return new CompressStub(frame, stats, true);
|
2014-12-21 18:32:37 +01:00
|
|
|
case COMP_STUBv2:
|
|
|
|
return new CompressStubV2(frame, stats);
|
2012-09-11 10:45:27 +02:00
|
|
|
#ifndef NO_LZO
|
2012-01-31 12:15:21 +01:00
|
|
|
case LZO:
|
2012-09-08 03:36:54 +02:00
|
|
|
return new CompressLZO(frame, stats, false, asym_);
|
2012-01-31 12:15:21 +01:00
|
|
|
case LZO_SWAP:
|
2012-09-08 03:36:54 +02:00
|
|
|
return new CompressLZO(frame, stats, true, asym_);
|
2012-09-11 10:45:27 +02:00
|
|
|
#endif
|
2012-01-31 12:15:21 +01:00
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
case LZ4:
|
2012-09-08 03:36:54 +02:00
|
|
|
return new CompressLZ4(frame, stats, asym_);
|
2014-12-21 18:32:37 +01:00
|
|
|
case LZ4v2:
|
|
|
|
return new CompressLZ4v2(frame, stats, asym_);
|
2012-01-31 12:15:21 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SNAPPY
|
|
|
|
case SNAPPY:
|
2012-09-08 03:36:54 +02:00
|
|
|
return new CompressSnappy(frame, stats, asym_);
|
2012-01-31 12:15:21 +01:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
throw compressor_unavailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool compressor_available(const Type t)
|
|
|
|
{
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case NONE:
|
|
|
|
case ANY:
|
|
|
|
case ANY_LZO:
|
2011-12-14 12:34:33 +01:00
|
|
|
case LZO_STUB:
|
2012-01-31 12:15:21 +01:00
|
|
|
case COMP_STUB:
|
2014-12-21 18:32:37 +01:00
|
|
|
case COMP_STUBv2:
|
2012-01-31 12:15:21 +01:00
|
|
|
return true;
|
|
|
|
case LZO:
|
|
|
|
case LZO_SWAP:
|
2012-09-11 10:45:27 +02:00
|
|
|
#ifndef NO_LZO
|
2012-01-31 12:15:21 +01:00
|
|
|
return true;
|
2012-09-11 10:45:27 +02:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2012-01-31 12:15:21 +01:00
|
|
|
case LZ4:
|
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
2014-12-21 18:32:37 +01:00
|
|
|
#endif
|
|
|
|
case LZ4v2:
|
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
2012-01-31 12:15:21 +01:00
|
|
|
#endif
|
|
|
|
case SNAPPY:
|
|
|
|
#ifdef HAVE_SNAPPY
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2011-12-14 12:34:33 +01:00
|
|
|
default:
|
2012-01-31 12:15:21 +01:00
|
|
|
return false;
|
2011-12-14 12:34:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 03:36:54 +02:00
|
|
|
// On the client, used to tell server which compression methods we support.
|
2014-12-30 06:20:50 +01:00
|
|
|
// Includes compression V1 and V2 methods.
|
2011-12-14 12:34:33 +01:00
|
|
|
const char *peer_info_string() const
|
|
|
|
{
|
|
|
|
switch (type_)
|
|
|
|
{
|
2012-09-11 10:45:27 +02:00
|
|
|
#ifndef NO_LZO
|
2011-12-14 12:34:33 +01:00
|
|
|
case LZO:
|
2012-01-31 12:15:21 +01:00
|
|
|
return "IV_LZO=1\n";
|
|
|
|
case LZO_SWAP:
|
|
|
|
return "IV_LZO_SWAP=1\n";
|
2012-09-11 10:45:27 +02:00
|
|
|
#endif
|
2012-01-31 12:15:21 +01:00
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
case LZ4:
|
|
|
|
return "IV_LZ4=1\n";
|
|
|
|
#endif
|
2014-12-21 18:32:37 +01:00
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
case LZ4v2:
|
|
|
|
return "IV_LZ4v2=1\n";
|
|
|
|
#endif
|
2012-01-31 12:15:21 +01:00
|
|
|
#ifdef HAVE_SNAPPY
|
|
|
|
case SNAPPY:
|
|
|
|
return "IV_SNAPPY=1\n";
|
|
|
|
#endif
|
2011-12-14 12:34:33 +01:00
|
|
|
case LZO_STUB:
|
2012-01-31 12:15:21 +01:00
|
|
|
case COMP_STUB:
|
2014-12-21 18:32:37 +01:00
|
|
|
case COMP_STUBv2:
|
2012-09-17 23:41:41 +02:00
|
|
|
return
|
|
|
|
"IV_LZO_STUB=1\n"
|
|
|
|
"IV_COMP_STUB=1\n"
|
2014-12-21 18:32:37 +01:00
|
|
|
"IV_COMP_STUBv2=1\n"
|
2012-09-17 23:41:41 +02:00
|
|
|
;
|
2012-01-31 12:15:21 +01:00
|
|
|
case ANY:
|
|
|
|
return
|
2012-09-08 03:36:54 +02:00
|
|
|
#ifdef HAVE_SNAPPY
|
|
|
|
"IV_SNAPPY=1\n"
|
|
|
|
#endif
|
2012-09-11 10:45:27 +02:00
|
|
|
#ifndef NO_LZO
|
2012-01-31 12:15:21 +01:00
|
|
|
"IV_LZO=1\n"
|
|
|
|
"IV_LZO_SWAP=1\n"
|
2012-09-11 10:45:27 +02:00
|
|
|
#else
|
|
|
|
"IV_LZO_STUB=1\n"
|
|
|
|
#endif
|
2012-01-31 12:15:21 +01:00
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
"IV_LZ4=1\n"
|
2014-12-21 18:32:37 +01:00
|
|
|
"IV_LZ4v2=1\n"
|
2012-01-31 12:15:21 +01:00
|
|
|
#endif
|
|
|
|
"IV_COMP_STUB=1\n"
|
2014-12-21 18:32:37 +01:00
|
|
|
"IV_COMP_STUBv2=1\n"
|
2012-01-31 12:15:21 +01:00
|
|
|
;
|
|
|
|
case ANY_LZO:
|
|
|
|
return
|
2012-09-11 10:45:27 +02:00
|
|
|
#ifndef NO_LZO
|
2012-01-31 12:15:21 +01:00
|
|
|
"IV_LZO=1\n"
|
|
|
|
"IV_LZO_SWAP=1\n"
|
2012-09-11 10:45:27 +02:00
|
|
|
#else
|
|
|
|
"IV_LZO_STUB=1\n"
|
|
|
|
#endif
|
2012-09-17 23:41:41 +02:00
|
|
|
"IV_COMP_STUB=1\n"
|
2014-12-21 18:32:37 +01:00
|
|
|
"IV_COMP_STUBv2=1\n"
|
2012-01-31 12:15:21 +01:00
|
|
|
;
|
2011-12-14 12:34:33 +01:00
|
|
|
default:
|
2015-05-17 10:53:37 +02:00
|
|
|
return nullptr;
|
2011-12-14 12:34:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 06:20:50 +01:00
|
|
|
// On the client, used to tell server which compression methods we support.
|
|
|
|
// Limited only to compression V1 methods.
|
|
|
|
const char *peer_info_string_v1() const
|
|
|
|
{
|
|
|
|
switch (type_)
|
|
|
|
{
|
|
|
|
#ifndef NO_LZO
|
|
|
|
case LZO:
|
|
|
|
return "IV_LZO=1\n";
|
|
|
|
case LZO_SWAP:
|
|
|
|
return "IV_LZO_SWAP=1\n";
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
case LZ4:
|
|
|
|
return "IV_LZ4=1\n";
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SNAPPY
|
|
|
|
case SNAPPY:
|
|
|
|
return "IV_SNAPPY=1\n";
|
|
|
|
#endif
|
|
|
|
case LZO_STUB:
|
|
|
|
case COMP_STUB:
|
|
|
|
return
|
|
|
|
"IV_LZO_STUB=1\n"
|
|
|
|
"IV_COMP_STUB=1\n"
|
|
|
|
;
|
|
|
|
case ANY:
|
|
|
|
return
|
|
|
|
#ifdef HAVE_SNAPPY
|
|
|
|
"IV_SNAPPY=1\n"
|
|
|
|
#endif
|
|
|
|
#ifndef NO_LZO
|
|
|
|
"IV_LZO=1\n"
|
|
|
|
"IV_LZO_SWAP=1\n"
|
|
|
|
#else
|
|
|
|
"IV_LZO_STUB=1\n"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LZ4
|
|
|
|
"IV_LZ4=1\n"
|
|
|
|
#endif
|
|
|
|
"IV_COMP_STUB=1\n"
|
|
|
|
;
|
|
|
|
case ANY_LZO:
|
|
|
|
return
|
|
|
|
#ifndef NO_LZO
|
|
|
|
"IV_LZO=1\n"
|
|
|
|
"IV_LZO_SWAP=1\n"
|
|
|
|
#else
|
|
|
|
"IV_LZO_STUB=1\n"
|
|
|
|
#endif
|
|
|
|
"IV_COMP_STUB=1\n"
|
|
|
|
;
|
|
|
|
default:
|
2015-05-17 10:53:37 +02:00
|
|
|
return nullptr;
|
2014-12-30 06:20:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:34:33 +01:00
|
|
|
const char *options_string() const
|
|
|
|
{
|
|
|
|
switch (type_)
|
|
|
|
{
|
|
|
|
case LZO:
|
|
|
|
case LZO_STUB:
|
2012-01-31 12:15:21 +01:00
|
|
|
case SNAPPY:
|
|
|
|
case LZ4:
|
2014-12-21 18:32:37 +01:00
|
|
|
case LZ4v2:
|
2012-01-31 12:15:21 +01:00
|
|
|
case LZO_SWAP:
|
|
|
|
case COMP_STUB:
|
2014-12-21 18:32:37 +01:00
|
|
|
case COMP_STUBv2:
|
2012-01-31 12:15:21 +01:00
|
|
|
case ANY:
|
|
|
|
case ANY_LZO:
|
2011-12-14 12:34:33 +01:00
|
|
|
return "comp-lzo";
|
|
|
|
default:
|
2015-05-17 10:53:37 +02:00
|
|
|
return nullptr;
|
2011-12-14 12:34:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *str() const
|
|
|
|
{
|
|
|
|
switch (type_)
|
|
|
|
{
|
|
|
|
case LZO:
|
|
|
|
return "LZO";
|
2012-01-31 12:15:21 +01:00
|
|
|
case LZO_SWAP:
|
|
|
|
return "LZO_SWAP";
|
|
|
|
case LZ4:
|
|
|
|
return "LZ4";
|
2014-12-21 18:32:37 +01:00
|
|
|
case LZ4v2:
|
|
|
|
return "LZ4v2";
|
2012-01-31 12:15:21 +01:00
|
|
|
case SNAPPY:
|
|
|
|
return "SNAPPY";
|
2011-12-14 12:34:33 +01:00
|
|
|
case LZO_STUB:
|
|
|
|
return "LZO_STUB";
|
2012-01-31 12:15:21 +01:00
|
|
|
case COMP_STUB:
|
|
|
|
return "COMP_STUB";
|
2014-12-21 18:32:37 +01:00
|
|
|
case COMP_STUBv2:
|
|
|
|
return "COMP_STUBv2";
|
2012-01-31 12:15:21 +01:00
|
|
|
case ANY:
|
|
|
|
return "ANY";
|
|
|
|
case ANY_LZO:
|
|
|
|
return "ANY_LZO";
|
2011-12-14 12:34:33 +01:00
|
|
|
default:
|
|
|
|
return "NONE";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 05:47:43 +02:00
|
|
|
/* This function returns a parseable string representation of the compress
|
|
|
|
* method. NOTE: returns nullptr if no mapping is possible */
|
|
|
|
const char *method_to_string() const
|
|
|
|
{
|
|
|
|
switch (type_)
|
|
|
|
{
|
|
|
|
case LZO:
|
|
|
|
return "lzo";
|
|
|
|
case LZO_SWAP:
|
|
|
|
return "lzo-swap";
|
|
|
|
case LZO_STUB:
|
|
|
|
return "lzo-stub";
|
|
|
|
case LZ4:
|
|
|
|
return "lz4";
|
|
|
|
case LZ4v2:
|
|
|
|
return "lz4v2";
|
|
|
|
case SNAPPY:
|
|
|
|
return "snappy";
|
|
|
|
case COMP_STUB:
|
|
|
|
return "stub";
|
|
|
|
case COMP_STUBv2:
|
|
|
|
return "stub-v2";
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 12:15:21 +01:00
|
|
|
static Type parse_method(const std::string& method)
|
|
|
|
{
|
2017-09-23 21:37:31 +02:00
|
|
|
if (method == "stub-v2")
|
|
|
|
return COMP_STUBv2;
|
|
|
|
else if (method == "lz4-v2")
|
|
|
|
return LZ4v2;
|
|
|
|
else if (method == "lz4")
|
|
|
|
return LZ4;
|
|
|
|
else if (method == "lzo")
|
2012-01-31 12:15:21 +01:00
|
|
|
return LZO;
|
|
|
|
else if (method == "lzo-swap")
|
|
|
|
return LZO_SWAP;
|
|
|
|
else if (method == "lzo-stub")
|
|
|
|
return LZO_STUB;
|
|
|
|
else if (method == "snappy")
|
|
|
|
return SNAPPY;
|
|
|
|
else if (method == "stub")
|
|
|
|
return COMP_STUB;
|
|
|
|
else
|
|
|
|
return NONE;
|
|
|
|
}
|
|
|
|
|
2014-12-21 18:32:37 +01:00
|
|
|
static Type stub(const Type t)
|
|
|
|
{
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case COMP_STUBv2:
|
|
|
|
case LZ4v2:
|
|
|
|
return COMP_STUBv2;
|
|
|
|
default:
|
|
|
|
return COMP_STUB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:11:47 +02:00
|
|
|
/**
|
|
|
|
* Checks if the compression type is one of the available stub modes
|
|
|
|
*
|
|
|
|
* @param t The CompressContext::Type value
|
|
|
|
* @return Returns true if the type is one of the *_STUB{,v2} types,
|
|
|
|
* otherwise false.
|
|
|
|
*/
|
|
|
|
static bool is_any_stub(const Type t)
|
|
|
|
{
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case LZO_STUB:
|
|
|
|
case COMP_STUB:
|
|
|
|
case COMP_STUBv2:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 12:15:21 +01:00
|
|
|
static void init_static()
|
|
|
|
{
|
2012-09-11 10:45:27 +02:00
|
|
|
#ifndef NO_LZO
|
2012-01-31 12:15:21 +01:00
|
|
|
CompressLZO::init_static();
|
2012-09-11 10:45:27 +02:00
|
|
|
#endif
|
2012-01-31 12:15:21 +01:00
|
|
|
}
|
|
|
|
|
2011-12-14 12:34:33 +01:00
|
|
|
private:
|
2017-08-19 08:07:29 +02:00
|
|
|
Type type_ = NONE;
|
|
|
|
bool asym_ = false;
|
2012-09-08 03:36:54 +02:00
|
|
|
};
|
2011-12-14 12:34:33 +01:00
|
|
|
|
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif // OPENVPN_COMPRESS_COMPRESS_H
|