blob: 09ca6cf6d0c5aa25ae6b364d760d586c689a60dc [file] [log] [blame]
Joao Marcos Costac5100612020-07-30 15:33:47 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Bootlin
4 *
5 * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
6 */
7
8#include <errno.h>
9#include <stdint.h>
10#include <stdio.h>
11#include <stdlib.h>
Joao Marcos Costa3634b352020-07-30 15:33:50 +020012#if IS_ENABLED(CONFIG_ZLIB)
13#include <u-boot/zlib.h>
14#endif
Joao Marcos Costac5100612020-07-30 15:33:47 +020015
16#include "sqfs_decompressor.h"
17#include "sqfs_filesystem.h"
18#include "sqfs_utils.h"
19
Joao Marcos Costa3634b352020-07-30 15:33:50 +020020#if IS_ENABLED(CONFIG_ZLIB)
21static void zlib_decompression_status(int ret)
22{
23 switch (ret) {
24 case Z_BUF_ERROR:
25 printf("Error: 'dest' buffer is not large enough.\n");
26 break;
27 case Z_DATA_ERROR:
28 printf("Error: corrupted compressed data.\n");
29 break;
30 case Z_MEM_ERROR:
31 printf("Error: insufficient memory.\n");
32 break;
33 }
34}
35#endif
36
Joao Marcos Costac5100612020-07-30 15:33:47 +020037int sqfs_decompress(u16 comp_type, void *dest, unsigned long *dest_len,
38 void *source, u32 lenp)
39{
40 int ret = 0;
41
42 switch (comp_type) {
Joao Marcos Costa3634b352020-07-30 15:33:50 +020043#if IS_ENABLED(CONFIG_ZLIB)
44 case SQFS_COMP_ZLIB:
45 ret = uncompress(dest, dest_len, source, lenp);
46 if (ret) {
47 zlib_decompression_status(ret);
48 return -EINVAL;
49 }
50
51 break;
52#endif
Joao Marcos Costac5100612020-07-30 15:33:47 +020053 default:
54 printf("Error: unknown compression type.\n");
55 return -EINVAL;
56 }
57
58 return ret;
59}