aboutsummaryrefslogtreecommitdiff
path: root/lib/xfrm/src/zstd.c
blob: 70666c1901b02a6ba75f6ffa7a0c2d5bf85a6237 (plain)
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * zstd.c
 *
 * Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at>
 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <zstd.h>

#include "xfrm/stream.h"
#include "xfrm/compress.h"

typedef struct {
	xfrm_stream_t base;

	ZSTD_CStream *cstrm;
	ZSTD_DStream *dstrm;
	bool compress;
} xfrm_zstd_t;

static const ZSTD_EndDirective zstd_action[] = {
	[XFRM_STREAM_FLUSH_NONE] = ZSTD_e_continue,
	[XFRM_STREAM_FLUSH_SYNC] = ZSTD_e_flush,
	[XFRM_STREAM_FLUSH_FULL] = ZSTD_e_end,
};

static int process_data(xfrm_stream_t *stream, const void *in,
			sqfs_u32 in_size, void *out, sqfs_u32 out_size,
			sqfs_u32 *in_read, sqfs_u32 *out_written,
			int flush_mode)
{
	xfrm_zstd_t *zstd = (xfrm_zstd_t *)stream;
	ZSTD_outBuffer out_desc;
	ZSTD_inBuffer in_desc;
	size_t ret;

	if (flush_mode < 0 || flush_mode >= XFRM_STREAM_FLUSH_COUNT)
		flush_mode = XFRM_STREAM_FLUSH_NONE;

	while (in_size > 0 && out_size > 0) {
		memset(&in_desc, 0, sizeof(in_desc));
		in_desc.src = in;
		in_desc.size = in_size;

		memset(&out_desc, 0, sizeof(out_desc));
		out_desc.dst = out;
		out_desc.size = out_size;

		if (zstd->compress) {
			ret = ZSTD_compressStream2(zstd->cstrm, &out_desc,
						   &in_desc,
						   zstd_action[flush_mode]);
		} else {
			ret = ZSTD_decompressStream(zstd->dstrm, &out_desc,
						    &in_desc);
		}

		if (ZSTD_isError(ret))
			return XFRM_STREAM_ERROR;

		in = (const char *)in + in_desc.pos;
		in_size -= in_desc.pos;
		*in_read += in_desc.pos;

		out = (char *)out + out_desc.pos;
		out_size -= out_desc.pos;
		*out_written += out_desc.pos;
	}

	if (flush_mode != XFRM_STREAM_FLUSH_NONE) {
		if (in_size == 0)
			return XFRM_STREAM_END;
	}

	if (in_size > 0 && out_size == 0)
		return XFRM_STREAM_BUFFER_FULL;

	return XFRM_STREAM_OK;
}

static void destroy(sqfs_object_t *obj)
{
	xfrm_zstd_t *zstd = (xfrm_zstd_t *)obj;

	if (zstd->compress) {
		ZSTD_freeCStream(zstd->cstrm);
	} else {
		ZSTD_freeDStream(zstd->dstrm);
	}

	free(zstd);
}

static xfrm_stream_t *stream_create(const compressor_config_t *cfg,
				    bool compress)
{
	xfrm_zstd_t *zstd = calloc(1, sizeof(*zstd));
	xfrm_stream_t *strm = (xfrm_stream_t *)zstd;
	(void)cfg;

	if (zstd == NULL) {
		perror("creating zstd stream compressor");
		return NULL;
	}

	if (compress) {
		zstd->cstrm = ZSTD_createCStream();
		if (zstd->cstrm == NULL)
			goto fail_strm;
	} else {
		zstd->dstrm = ZSTD_createDStream();
		if (zstd->dstrm == NULL)
			goto fail_strm;
	}

	zstd->compress = compress;
	strm->process_data = process_data;
	sqfs_object_init(zstd, destroy, NULL);
	return strm;
fail_strm:
	fputs("error initializing zstd stream.\n", stderr);
	free(zstd);
	return NULL;
}

xfrm_stream_t *compressor_stream_zstd_create(const compressor_config_t *cfg)
{
	return stream_create(cfg, true);
}

xfrm_stream_t *decompressor_stream_zstd_create(void)
{
	return stream_create(NULL, false);
}