-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqfs_decompressor.c
139 lines (126 loc) · 2.94 KB
/
sqfs_decompressor.c
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
131
132
133
134
135
136
137
138
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2020 Bootlin
*
* Author: Joao Marcos Costa <[email protected]>
*
* sqfs_decompressor.c: decompress SquashFS image
*/
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <zlib.h>
#include "sqfs_decompressor.h"
#include "sqfs_utils.h"
/*
* Reference:
* https://dr-emann.github.io/squashfs/#superblock
*/
/* For now, sqfs_decompress supports only zlib compression. */
int sqfs_decompress(void *dest, size_t *dest_len, const void *source,
size_t source_len)
{
int ret;
ret = uncompress(dest, dest_len, source, source_len);
switch (ret) {
case Z_BUF_ERROR:
printd("Error: 'dest' buffer is not large enough.\n");
break;
case Z_DATA_ERROR:
printd("Error: corrupted compressed data.\n");
break;
case Z_MEM_ERROR:
printd("Error: insufficient memory.\n");
break;
case Z_OK:
printd("Decompression OK.\n");
break;
}
return ret;
}
int sqfs_fill_compression_opts(union sqfs_compression_opts *opts,
int compression, void *file_mapping)
{
/* Metadata block reading */
void *metadata;
metadata = file_mapping + SUPER_BLOCK_SIZE + HEADER_SIZE;
switch (compression) {
case ZLIB:
opts->gzip = (struct gzip_opts *)metadata;
break;
case LZMA:
break;
case LZO:
opts->lzo = (struct lzo_opts *)metadata;
break;
case XZ:
opts->xz = (struct xz_opts *)metadata;
break;
case LZ4:
opts->lz4 = (struct lz4_opts *)metadata;
break;
case ZSTD:
opts->zstd = (struct zstd_opts *)metadata;
break;
default:
return -EINVAL;
}
return 0;
}
int sqfs_dump_compression_opts(int compression,
union sqfs_compression_opts *opts)
{
if (!opts) {
printf("Error while dumping compression options\n");
return -EINVAL;
}
printf(" --- COMPRESSION OPTIONS ---\n");
printf("Compressor: ");
switch (compression) {
case ZLIB:
printf("ZLIB\n");
printf("Compression level: %u\n",
opts->gzip->compression_level);
printf("Window size: %u\n", opts->gzip->window_size);
printf("Strategies: 0x%x\n", opts->gzip->strategies);
break;
case LZMA:
printf("LZMA\n");
printf("No compression options\n");
break;
case LZO:
printf("LZO\n");
printf("Algorithm: %u\n", opts->lzo->algorithm);
printf("Level: %u\n", opts->lzo->level);
break;
case XZ:
printf("XZ\n");
printf("Dictionary size: %u kB\n",
opts->xz->dictionary_size / 1000);
printf("Executable filters: 0x%x\n",
opts->xz->executable_filters);
break;
case LZ4:
printf("LZ4\n");
printf("Version: %u\n", opts->lz4->version);
printf("Flags: 0x%x\n", opts->lz4->flags);
break;
case ZSTD:
printf("ZSTD\n");
printf("Compression level: %u\n",
opts->zstd->compression_level);
break;
default:
printf("Unknown compression type: %u\n", compression);
return -EINVAL;
}
return 0;
}