aboutsummaryrefslogtreecommitdiff
path: root/lib/zstd/zstd.c
blob: bf9cd19cfa34265f889c091de074cd17a7ea6926 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2021 Google LLC
 */

#define LOG_CATEGORY	LOGC_BOOT

#include <common.h>
#include <abuf.h>
#include <log.h>
#include <malloc.h>
#include <linux/zstd.h>

int zstd_decompress(struct abuf *in, struct abuf *out)
{
	ZSTD_DStream *dstream;
	ZSTD_inBuffer in_buf;
	ZSTD_outBuffer out_buf;
	void *workspace;
	size_t wsize;
	int ret;

	wsize = ZSTD_DStreamWorkspaceBound(abuf_size(in));
	workspace = malloc(wsize);
	if (!workspace) {
		debug("%s: cannot allocate workspace of size %zu\n", __func__,
			wsize);
		return -ENOMEM;
	}

	dstream = ZSTD_initDStream(abuf_size(in), workspace, wsize);
	if (!dstream) {
		log_err("%s: ZSTD_initDStream failed\n", __func__);
		ret = -EPERM;
		goto do_free;
	}

	in_buf.src = abuf_data(in);
	in_buf.pos = 0;
	in_buf.size = abuf_size(in);

	out_buf.dst = abuf_data(out);
	out_buf.pos = 0;
	out_buf.size = abuf_size(out);

	while (1) {
		size_t res;

		res = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
		if (ZSTD_isError(res)) {
			ret = ZSTD_getErrorCode(res);
			log_err("ZSTD_decompressStream error %d\n", ret);
			goto do_free;
		}

		if (in_buf.pos >= abuf_size(in) || !res)
			break;
	}

	ret = out_buf.pos;
do_free:
	free(workspace);
	return ret;
}