1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Bootlin
5 * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
13 #if IS_ENABLED(CONFIG_LZO)
14 #include <linux/lzo.h>
17 #if IS_ENABLED(CONFIG_ZLIB)
18 #include <u-boot/zlib.h>
21 #if IS_ENABLED(CONFIG_ZSTD)
22 #include <linux/zstd.h>
25 #include "sqfs_decompressor.h"
26 #include "sqfs_utils.h"
28 int sqfs_decompressor_init(struct squashfs_ctxt *ctxt)
30 u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
33 #if IS_ENABLED(CONFIG_LZO)
37 #if IS_ENABLED(CONFIG_ZLIB)
41 #if IS_ENABLED(CONFIG_ZSTD)
43 ctxt->zstd_workspace = malloc(ZSTD_DCtxWorkspaceBound());
44 if (!ctxt->zstd_workspace)
49 printf("Error: unknown compression type.\n");
56 void sqfs_decompressor_cleanup(struct squashfs_ctxt *ctxt)
58 u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
61 #if IS_ENABLED(CONFIG_LZO)
65 #if IS_ENABLED(CONFIG_ZLIB)
69 #if IS_ENABLED(CONFIG_ZSTD)
71 free(ctxt->zstd_workspace);
77 #if IS_ENABLED(CONFIG_ZLIB)
78 static void zlib_decompression_status(int ret)
82 printf("Error: 'dest' buffer is not large enough.\n");
85 printf("Error: corrupted compressed data.\n");
88 printf("Error: insufficient memory.\n");
94 #if IS_ENABLED(CONFIG_ZSTD)
95 static int sqfs_zstd_decompress(struct squashfs_ctxt *ctxt, void *dest,
96 unsigned long dest_len, void *source, u32 src_len)
102 wsize = ZSTD_DCtxWorkspaceBound();
103 ctx = ZSTD_initDCtx(ctxt->zstd_workspace, wsize);
104 ret = ZSTD_decompressDCtx(ctx, dest, dest_len, source, src_len);
106 return ZSTD_isError(ret);
108 #endif /* CONFIG_ZSTD */
110 int sqfs_decompress(struct squashfs_ctxt *ctxt, void *dest,
111 unsigned long *dest_len, void *source, u32 src_len)
113 u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
117 #if IS_ENABLED(CONFIG_LZO)
118 case SQFS_COMP_LZO: {
119 size_t lzo_dest_len = *dest_len;
120 ret = lzo1x_decompress_safe(source, src_len, dest, &lzo_dest_len);
122 printf("LZO decompression failed. Error code: %d\n", ret);
129 #if IS_ENABLED(CONFIG_ZLIB)
131 ret = uncompress(dest, dest_len, source, src_len);
133 zlib_decompression_status(ret);
139 #if IS_ENABLED(CONFIG_ZSTD)
141 ret = sqfs_zstd_decompress(ctxt, dest, *dest_len, source, src_len);
143 printf("ZSTD Error code: %d\n", ZSTD_getErrorCode(ret));
150 printf("Error: unknown compression type.\n");