OpenVPN
comp-lz4.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 * Copyright (C) 2013-2026 Gert Doering <gert@greenie.muc.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include "syshead.h"
29
30#if defined(ENABLE_LZ4)
31#include <lz4.h>
32
33#include "comp.h"
34#include "comp-lz4.h"
35#include "error.h"
36
37#include "memdbg.h"
38
39
40static void
41lz4_compress_init(struct compress_context *compctx)
42{
43 msg(D_INIT_MEDIUM, "LZ4 compression initializing");
44 ASSERT(compctx->flags & COMP_F_SWAP);
45}
46
47static void
48lz4v2_compress_init(struct compress_context *compctx)
49{
50 msg(D_INIT_MEDIUM, "LZ4v2 compression initializing");
51}
52
53static void
54lz4_compress_uninit(struct compress_context *compctx)
55{
56}
57
58#if defined(__GNUC__) || defined(__clang__)
59#pragma GCC diagnostic push
60#pragma GCC diagnostic ignored "-Wsign-conversion"
61#endif
62
63static void
64do_lz4_decompress(int zlen_max, struct buffer *work, struct buffer *buf,
65 struct compress_context *compctx)
66{
67 ASSERT(buf_safe(work, zlen_max));
68 int uncomp_len = LZ4_decompress_safe((const char *)BPTR(buf), (char *)BPTR(work), BLEN(buf),
69 zlen_max);
70 if (uncomp_len <= 0)
71 {
72 dmsg(D_COMP_ERRORS, "LZ4 decompression error: %d", uncomp_len);
73 buf->len = 0;
74 return;
75 }
76
77 ASSERT(buf_safe(work, uncomp_len));
78 work->len = uncomp_len;
79
80 dmsg(D_COMP, "LZ4 decompress %d -> %d", buf->len, work->len);
81 compctx->pre_decompress += buf->len;
82 compctx->post_decompress += work->len;
83
84 *buf = *work;
85}
86
87static void
88lz4_decompress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
89 const struct frame *frame)
90{
91 int zlen_max = frame->buf.payload_size;
92 uint8_t c; /* flag indicating whether or not our peer compressed */
93
94 if (buf->len <= 0)
95 {
96 return;
97 }
98
100
101 /* do unframing/swap (assumes buf->len > 0) */
102 {
103 uint8_t *head = BPTR(buf);
104 c = *head;
105 --buf->len;
106 *head = *BEND(buf);
107 }
108
109 if (c == LZ4_COMPRESS_BYTE) /* packet was compressed */
110 {
111 do_lz4_decompress(zlen_max, &work, buf, compctx);
112 }
113 else if (c == NO_COMPRESS_BYTE_SWAP) /* packet was not compressed */
114 {
115 /* nothing to do */
116 }
117 else
118 {
119 dmsg(D_COMP_ERRORS, "Bad LZ4 decompression header byte: %d", c);
120 buf->len = 0;
121 }
122}
123
124static void
125lz4v2_decompress(struct buffer *buf, struct buffer work, struct compress_context *compctx,
126 const struct frame *frame)
127{
128 int zlen_max = frame->buf.payload_size;
129 uint8_t c; /* flag indicating whether or not our peer compressed */
130
131 if (buf->len <= 0)
132 {
133 return;
134 }
135
136 ASSERT(buf_init(&work, frame->buf.headroom));
137
138 /* do unframing/swap (assumes buf->len > 0) */
139 const uint8_t *head = BPTR(buf);
140 c = *head;
141
142 /* Not compressed */
143 if (c != COMP_ALGV2_INDICATOR_BYTE)
144 {
145 return;
146 }
147
148 /* Packet to short to make sense */
149 if (buf->len <= 1)
150 {
151 buf->len = 0;
152 return;
153 }
154
155 c = head[1];
156 if (c == COMP_ALGV2_LZ4_BYTE) /* packet was compressed */
157 {
158 buf_advance(buf, 2);
159 do_lz4_decompress(zlen_max, &work, buf, compctx);
160 }
161 else if (c == COMP_ALGV2_UNCOMPRESSED_BYTE)
162 {
163 buf_advance(buf, 2);
164 }
165 else
166 {
167 dmsg(D_COMP_ERRORS, "Bad LZ4v2 decompression header byte: %d", c);
168 buf->len = 0;
169 }
170}
171
172#if defined(__GNUC__) || defined(__clang__)
173#pragma GCC diagnostic pop
174#endif
175
176const struct compress_alg lz4_alg = { "lz4", lz4_compress_init, lz4_compress_uninit,
177 comp_stub_compress, lz4_decompress };
178
179const struct compress_alg lz4v2_alg = { "lz4v2", lz4v2_compress_init, lz4_compress_uninit,
180 compv2_stub_compress, lz4v2_decompress };
181#endif /* ENABLE_LZ4 */
#define BEND(buf)
Return a pointer one past the end of the buffer content.
Definition buffer.h:143
#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 BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#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 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