summaryrefslogtreecommitdiff
path: root/fs/squashfs/sqfs_decompressor.c
blob: 9457ee5f0f9894ea135e4a56c6de78417765199e (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020 Bootlin
 *
 * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
 */

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#if IS_ENABLED(CONFIG_ZLIB)
#include <u-boot/zlib.h>
#endif

#if IS_ENABLED(CONFIG_ZSTD)
#include <linux/zstd.h>
#endif

#include "sqfs_decompressor.h"
#include "sqfs_utils.h"

int sqfs_decompressor_init(struct squashfs_ctxt *ctxt)
{
	u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);

	switch (comp_type) {
#if IS_ENABLED(CONFIG_ZLIB)
	case SQFS_COMP_ZLIB:
		break;
#endif
#if IS_ENABLED(CONFIG_ZSTD)
	case SQFS_COMP_ZSTD:
		ctxt->zstd_workspace = malloc(ZSTD_DCtxWorkspaceBound());
		if (!ctxt->zstd_workspace)
			return -ENOMEM;
		break;
#endif
	default:
		printf("Error: unknown compression type.\n");
		return -EINVAL;
	}

	return 0;
}

void sqfs_decompressor_cleanup(struct squashfs_ctxt *ctxt)
{
	u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);

	switch (comp_type) {
#if IS_ENABLED(CONFIG_ZLIB)
	case SQFS_COMP_ZLIB:
		break;
#endif
#if IS_ENABLED(CONFIG_ZSTD)
	case SQFS_COMP_ZSTD:
		free(ctxt->zstd_workspace);
		break;
#endif
	}
}

#if IS_ENABLED(CONFIG_ZLIB)
static void zlib_decompression_status(int ret)
{
	switch (ret) {
	case Z_BUF_ERROR:
		printf("Error: 'dest' buffer is not large enough.\n");
		break;
	case Z_DATA_ERROR:
		printf("Error: corrupted compressed data.\n");
		break;
	case Z_MEM_ERROR:
		printf("Error: insufficient memory.\n");
		break;
	}
}
#endif

#if IS_ENABLED(CONFIG_ZSTD)
static int sqfs_zstd_decompress(struct squashfs_ctxt *ctxt, void *dest,
				unsigned long dest_len, void *source, u32 src_len)
{
	ZSTD_DCtx *ctx;
	size_t wsize;
	int ret;

	wsize = ZSTD_DCtxWorkspaceBound();
	ctx = ZSTD_initDCtx(ctxt->zstd_workspace, wsize);
	ret = ZSTD_decompressDCtx(ctx, dest, dest_len, source, src_len);

	return ZSTD_isError(ret);
}
#endif /* CONFIG_ZSTD */

int sqfs_decompress(struct squashfs_ctxt *ctxt, void *dest,
		    unsigned long *dest_len, void *source, u32 src_len)
{
	u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
	int ret = 0;

	switch (comp_type) {
#if IS_ENABLED(CONFIG_ZLIB)
	case SQFS_COMP_ZLIB:
		ret = uncompress(dest, dest_len, source, src_len);
		if (ret) {
			zlib_decompression_status(ret);
			return -EINVAL;
		}

		break;
#endif
#if IS_ENABLED(CONFIG_ZSTD)
	case SQFS_COMP_ZSTD:
		ret = sqfs_zstd_decompress(ctxt, dest, *dest_len, source, src_len);
		if (ret) {
			printf("ZSTD Error code: %d\n", ZSTD_getErrorCode(ret));
			return -EINVAL;
		}

		break;
#endif
	default:
		printf("Error: unknown compression type.\n");
		return -EINVAL;
	}

	return ret;
}