OpenVPN
lzo.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "syshead.h"
33
34#if defined(ENABLE_LZO)
35
36#include "comp.h"
37#include "error.h"
38#include "otime.h"
39
40#include "memdbg.h"
41
42
43#if defined(__GNUC__) || defined(__clang__)
44#pragma GCC diagnostic push
45#pragma GCC diagnostic ignored "-Wsign-conversion"
46#endif
47
48static void
49lzo_compress_init(struct compress_context *compctx)
50{
51 msg(D_INIT_MEDIUM, "LZO compression initializing");
52 ASSERT(!(compctx->flags & COMP_F_SWAP));
53 compctx->wu.lzo.wmem_size = LZO_WORKSPACE;
54
55 int lzo_status = lzo_init();
56 if (lzo_status != LZO_E_OK)
57 {
58 msg(M_FATAL, "Cannot initialize LZO compression library (lzo_init() returns %d)",
59 lzo_status);
60 }
61 compctx->wu.lzo.wmem = (lzo_voidp)malloc(compctx->wu.lzo.wmem_size);
62 check_malloc_return(compctx->wu.lzo.wmem);
63}
64
65static void
66lzo_compress_uninit(struct compress_context *compctx)
67{
68 free(compctx->wu.lzo.wmem);
69 compctx->wu.lzo.wmem = NULL;
70}
71
72static void
73lzo_decompress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
74 const struct frame *frame)
75{
76 lzo_uint zlen = frame->buf.payload_size;
77 uint8_t c; /* flag indicating whether or not our peer compressed */
78
79 if (buf->len <= 0)
80 {
81 return;
82 }
83
85
86 c = *BPTR(buf);
87 ASSERT(buf_advance(buf, 1));
88
89 if (c == LZO_COMPRESS_BYTE) /* packet was compressed */
90 {
91 ASSERT(buf_safe(&work, zlen));
92 int err = LZO_DECOMPRESS(BPTR(buf), BLENZ(buf), BPTR(&work), &zlen, compctx->wu.lzo.wmem);
93 if (err != LZO_E_OK)
94 {
95 dmsg(D_COMP_ERRORS, "LZO decompression error: %d", err);
96 buf->len = 0;
97 return;
98 }
99
100 ASSERT(buf_safe(&work, zlen));
101 work.len = (int)zlen;
102
103 dmsg(D_COMP, "LZO decompress %d -> %d", buf->len, work.len);
104 compctx->pre_decompress += buf->len;
105 compctx->post_decompress += work.len;
106
107 *buf = work;
108 }
109 else if (c == NO_COMPRESS_BYTE) /* packet was not compressed */
110 {
111 /* nothing to do */
112 }
113 else
114 {
115 dmsg(D_COMP_ERRORS, "Bad LZO decompression header byte: %d", c);
116 buf->len = 0;
117 }
118}
119
120#if defined(__GNUC__) || defined(__clang__)
121#pragma GCC diagnostic pop
122#endif
123
124const struct compress_alg lzo_alg = { "lzo", lzo_compress_init, lzo_compress_uninit,
125 comp_stub_compress, lzo_decompress };
126#endif /* ENABLE_LZO */
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_safe(const struct buffer *buf, size_t len)
Check whether len bytes can be appended to a buffer.
Definition buffer.h:994
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
Definition buffer.h:1165
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2144
#define buf_init(buf, offset)
Definition buffer.h:364
#define COMP_F_SWAP
initial command byte is swapped with last byte in buffer to preserve payload alignment
Definition comp.h:40
#define D_COMP_ERRORS
Definition errlevel.h:60
#define D_COMP
Definition errlevel.h:162
#define D_INIT_MEDIUM
Definition errlevel.h:101
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Packet geometry parameters.
Definition mtu.h:113
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:118
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:124
struct frame::@8 buf