File: compress_zstd.c

package info (click to toggle)
kronosnet 1.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: ansic: 25,419; sh: 5,295; makefile: 664
file content (172 lines) | stat: -rw-r--r-- 3,655 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2019-2025 Red Hat, Inc.  All rights reserved.
 *
 * Author: Fabio M. Di Nitto <fabbione@kronosnet.org>
 *
 * This software licensed under LGPL-2.0+
 */
#define KNET_MODULE

#include "config.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <zstd.h>

#include "logging.h"
#include "compress_model.h"

#ifdef ZSTD_CLEVEL_DEFAULT
#define KNET_COMPRESS_DEFAULT ZSTD_CLEVEL_DEFAULT /* zstd default compression level from zstd.h */
#else
#define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT
#endif

struct zstd_ctx {
	ZSTD_CCtx* cctx;
	ZSTD_DCtx* dctx;
};

static int zstd_is_init(
	knet_handle_t knet_h,
	int method_idx)
{
	if (knet_h->compress_int_data[method_idx]) {
		return 1;
	}
	return 0;
}

static void zstd_fini(
	knet_handle_t knet_h,
	int method_idx)
{
	struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model];

	if (zstd_ctx) {
		if (zstd_ctx->cctx) {
			ZSTD_freeCCtx(zstd_ctx->cctx);
		}
		if (zstd_ctx->dctx) {
			ZSTD_freeDCtx(zstd_ctx->dctx);
		}
		free(knet_h->compress_int_data[method_idx]);
		knet_h->compress_int_data[method_idx] = NULL;
	}
	return;
}

static int zstd_init(
	knet_handle_t knet_h,
	int method_idx)
{
	struct zstd_ctx *zstd_ctx;
	int err = 0;

	if (!knet_h->compress_int_data[method_idx]) {
		zstd_ctx = malloc(sizeof(struct zstd_ctx));
		if (!zstd_ctx) {
			errno = ENOMEM;
			return -1;
		}
		memset(zstd_ctx, 0, sizeof(struct zstd_ctx));

		knet_h->compress_int_data[method_idx] = zstd_ctx;

		zstd_ctx->cctx = ZSTD_createCCtx();
		if (!zstd_ctx->cctx) {
			log_err(knet_h, KNET_SUB_ZSTDCOMP, "Unable to create compression context");
			err = -1;
			goto out_err;
		}

		zstd_ctx->dctx = ZSTD_createDCtx();
		if (!zstd_ctx->dctx) {
			log_err(knet_h, KNET_SUB_ZSTDCOMP, "Unable to create decompression context");
			err = -1;
			goto out_err;
		}
	}

out_err:
	if (err) {
		zstd_fini(knet_h, method_idx);
	}
	return err;
}

static int zstd_compress(
	knet_handle_t knet_h,
	const unsigned char *buf_in,
	const ssize_t buf_in_len,
	unsigned char *buf_out,
	ssize_t *buf_out_len)
{
	struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model];
	size_t compress_size;

	compress_size = ZSTD_compressCCtx(zstd_ctx->cctx,
					  buf_out, *buf_out_len,
					  buf_in, buf_in_len,
					  knet_h->compress_level);

	if (ZSTD_isError(compress_size)) {
		log_err(knet_h, KNET_SUB_ZSTDCOMP, "error compressing packet: %s", ZSTD_getErrorName(compress_size));
		/*
		 * ZSTD has lots of internal errors that are not easy to map
		 * to standard errnos. Use a generic one for now
		 */
		errno = EINVAL;
		return -1;
	}

	*buf_out_len = compress_size;

	return 0;
}

static int zstd_decompress(
	knet_handle_t knet_h,
	const unsigned char *buf_in,
	const ssize_t buf_in_len,
	unsigned char *buf_out,
	ssize_t *buf_out_len)
{
	struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model];
	size_t decompress_size;

	decompress_size = ZSTD_decompressDCtx(zstd_ctx->dctx,
					      buf_out, *buf_out_len,
					      buf_in, buf_in_len);

	if (ZSTD_isError(decompress_size)) {
		log_err(knet_h, KNET_SUB_ZSTDCOMP, "error decompressing packet: %s", ZSTD_getErrorName(decompress_size));
		/*
		 * ZSTD has lots of internal errors that are not easy to map
		 * to standard errnos. Use a generic one for now
		 */
		errno = EINVAL;
		return -1;
	}

	*buf_out_len = decompress_size;

	return 0;
}

static int zstd_get_default_level()
{
	return KNET_COMPRESS_DEFAULT;
}

compress_ops_t compress_model = {
	KNET_COMPRESS_MODEL_ABI,
	zstd_is_init,
	zstd_init,
	zstd_fini,
	NULL,
	zstd_compress,
	zstd_decompress,
	zstd_get_default_level
};