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
|
// 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_dctx *ctx;
size_t wsize, len;
void *workspace;
int ret;
wsize = zstd_dctx_workspace_bound();
workspace = malloc(wsize);
if (!workspace) {
debug("%s: cannot allocate workspace of size %zu\n", __func__,
wsize);
return -ENOMEM;
}
ctx = zstd_init_dctx(workspace, wsize);
if (!ctx) {
log_err("%s: zstd_init_dctx() failed\n", __func__);
ret = -EPERM;
goto do_free;
}
/*
* Find out how large the frame actually is, there may be junk at
* the end of the frame that zstd_decompress_dctx() can't handle.
*/
len = zstd_find_frame_compressed_size(abuf_data(in), abuf_size(in));
if (zstd_is_error(len)) {
log_err("%s: failed to detect compressed size: %d\n", __func__,
zstd_get_error_code(len));
ret = -EINVAL;
goto do_free;
}
len = zstd_decompress_dctx(ctx, abuf_data(out), abuf_size(out),
abuf_data(in), len);
if (zstd_is_error(len)) {
log_err("%s: failed to decompress: %d\n", __func__,
zstd_get_error_code(len));
ret = -EINVAL;
goto do_free;
}
ret = len;
do_free:
free(workspace);
return ret;
}
|