]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - SQUASHFS/squashfs-tools-4.4/squashfs-tools/lz4_wrapper.c
1.1.07 release
[Ventoy.git] / SQUASHFS / squashfs-tools-4.4 / squashfs-tools / lz4_wrapper.c
1 /*
2 * Copyright (c) 2013, 2019
3 * Phillip Lougher <phillip@squashfs.org.uk>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2,
8 * or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * lz4_wrapper.c
20 *
21 * Support for LZ4 compression http://fastcompression.blogspot.com/p/lz4.html
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <lz4.h>
28 #include <lz4hc.h>
29
30 #include "squashfs_fs.h"
31 #include "lz4_wrapper.h"
32 #include "compressor.h"
33
34 /* LZ4 1.7.0 introduced new functions, and since r131,
35 * the older functions produce deprecated warnings.
36 *
37 * There are still too many distros using older versions
38 * to switch to the newer functions, but, the deprecated
39 * functions may completely disappear. This is a mess.
40 *
41 * Support both by checking the library version and
42 * using shadow definitions
43 */
44
45 /* Earlier (but > 1.7.0) versions don't define this */
46 #ifndef LZ4HC_CLEVEL_MAX
47 #define LZ4HC_CLEVEL_MAX 12
48 #endif
49
50 #if LZ4_VERSION_NUMBER >= 10700
51 #define COMPRESS(src, dest, size, max) LZ4_compress_default(src, dest, size, max)
52 #define COMPRESS_HC(src, dest, size, max) LZ4_compress_HC(src, dest, size, max, LZ4HC_CLEVEL_MAX)
53 #else
54 #define COMPRESS(src, dest, size, max) LZ4_compress_limitedOutput(src, dest, size, max)
55 #define COMPRESS_HC(src, dest, size, max) LZ4_compressHC_limitedOutput(src, dest, size, max)
56 #endif
57
58 static int hc = 0;
59
60 /*
61 * This function is called by the options parsing code in mksquashfs.c
62 * to parse any -X compressor option.
63 *
64 * This function returns:
65 * >=0 (number of additional args parsed) on success
66 * -1 if the option was unrecognised, or
67 * -2 if the option was recognised, but otherwise bad in
68 * some way (e.g. invalid parameter)
69 *
70 * Note: this function sets internal compressor state, but does not
71 * pass back the results of the parsing other than success/failure.
72 * The lz4_dump_options() function is called later to get the options in
73 * a format suitable for writing to the filesystem.
74 */
75 static int lz4_options(char *argv[], int argc)
76 {
77 if(strcmp(argv[0], "-Xhc") == 0) {
78 hc = 1;
79 return 0;
80 }
81
82 return -1;
83 }
84
85
86 /*
87 * This function is called by mksquashfs to dump the parsed
88 * compressor options in a format suitable for writing to the
89 * compressor options field in the filesystem (stored immediately
90 * after the superblock).
91 *
92 * This function returns a pointer to the compression options structure
93 * to be stored (and the size), or NULL if there are no compression
94 * options
95 *
96 * Currently LZ4 always returns a comp_opts structure, with
97 * the version indicating LZ4_LEGACY stream fomat. This is to
98 * easily accomodate changes in the kernel code to different
99 * stream formats
100 */
101 static void *lz4_dump_options(int block_size, int *size)
102 {
103 static struct lz4_comp_opts comp_opts;
104
105 comp_opts.version = LZ4_LEGACY;
106 comp_opts.flags = hc ? LZ4_HC : 0;
107 SQUASHFS_INSWAP_COMP_OPTS(&comp_opts);
108
109 *size = sizeof(comp_opts);
110 return &comp_opts;
111 }
112
113
114 /*
115 * This function is a helper specifically for the append mode of
116 * mksquashfs. Its purpose is to set the internal compressor state
117 * to the stored compressor options in the passed compressor options
118 * structure.
119 *
120 * In effect this function sets up the compressor options
121 * to the same state they were when the filesystem was originally
122 * generated, this is to ensure on appending, the compressor uses
123 * the same compression options that were used to generate the
124 * original filesystem.
125 *
126 * Note, even if there are no compressor options, this function is still
127 * called with an empty compressor structure (size == 0), to explicitly
128 * set the default options, this is to ensure any user supplied
129 * -X options on the appending mksquashfs command line are over-ridden
130 *
131 * This function returns 0 on sucessful extraction of options, and
132 * -1 on error
133 */
134 static int lz4_extract_options(int block_size, void *buffer, int size)
135 {
136 struct lz4_comp_opts *comp_opts = buffer;
137
138 /* we expect a comp_opts structure to be present */
139 if(size < sizeof(*comp_opts))
140 goto failed;
141
142 SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
143
144 /* we expect the stream format to be LZ4_LEGACY */
145 if(comp_opts->version != LZ4_LEGACY) {
146 fprintf(stderr, "lz4: unknown LZ4 version\n");
147 goto failed;
148 }
149
150 /*
151 * Check compression flags, currently only LZ4_HC ("high compression")
152 * can be set.
153 */
154 if(comp_opts->flags == LZ4_HC)
155 hc = 1;
156 else if(comp_opts->flags != 0) {
157 fprintf(stderr, "lz4: unknown LZ4 flags\n");
158 goto failed;
159 }
160
161 return 0;
162
163 failed:
164 fprintf(stderr, "lz4: error reading stored compressor options from "
165 "filesystem!\n");
166
167 return -1;
168 }
169
170
171 /*
172 * This function is a helper specifically for unsquashfs.
173 * Its purpose is to check that the compression options are
174 * understood by this version of LZ4.
175 *
176 * This is important for LZ4 because the format understood by the
177 * Linux kernel may change from the already obsolete legacy format
178 * currently supported.
179 *
180 * If this does happen, then this version of LZ4 will not be able to decode
181 * the newer format. So we need to check for this.
182 *
183 * This function returns 0 on sucessful checking of options, and
184 * -1 on error
185 */
186 static int lz4_check_options(int block_size, void *buffer, int size)
187 {
188 struct lz4_comp_opts *comp_opts = buffer;
189
190 /* we expect a comp_opts structure to be present */
191 if(size < sizeof(*comp_opts))
192 goto failed;
193
194 SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
195
196 /* we expect the stream format to be LZ4_LEGACY */
197 if(comp_opts->version != LZ4_LEGACY) {
198 fprintf(stderr, "lz4: unknown LZ4 version\n");
199 goto failed;
200 }
201
202 return 0;
203
204 failed:
205 fprintf(stderr, "lz4: error reading stored compressor options from "
206 "filesystem!\n");
207 return -1;
208 }
209
210
211 static void lz4_display_options(void *buffer, int size)
212 {
213 struct lz4_comp_opts *comp_opts = buffer;
214
215 /* check passed comp opts struct is of the correct length */
216 if(size < sizeof(*comp_opts))
217 goto failed;
218
219 SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
220
221 /* we expect the stream format to be LZ4_LEGACY */
222 if(comp_opts->version != LZ4_LEGACY) {
223 fprintf(stderr, "lz4: unknown LZ4 version\n");
224 goto failed;
225 }
226
227 /*
228 * Check compression flags, currently only LZ4_HC ("high compression")
229 * can be set.
230 */
231 if(comp_opts->flags & ~LZ4_FLAGS_MASK) {
232 fprintf(stderr, "lz4: unknown LZ4 flags\n");
233 goto failed;
234 }
235
236 if(comp_opts->flags & LZ4_HC)
237 printf("\tHigh Compression option specified (-Xhc)\n");
238
239 return;
240
241 failed:
242 fprintf(stderr, "lz4: error reading stored compressor options from "
243 "filesystem!\n");
244 }
245
246
247 static int lz4_compress(void *strm, void *dest, void *src, int size,
248 int block_size, int *error)
249 {
250 return 0;
251 }
252
253
254 static int lz4_uncompress(void *dest, void *src, int size, int outsize,
255 int *error)
256 {
257 int res = LZ4_decompress_safe(src, dest, size, outsize);
258 if(res < 0) {
259 *error = res;
260 return -1;
261 }
262
263 return res;
264 }
265
266
267 static void lz4_usage()
268 {
269 fprintf(stderr, "\t -Xhc\n");
270 fprintf(stderr, "\t\tCompress using LZ4 High Compression\n");
271 }
272
273
274 struct compressor lz4_comp_ops = {
275 .compress = lz4_compress,
276 .uncompress = lz4_uncompress,
277 .options = lz4_options,
278 .dump_options = lz4_dump_options,
279 .extract_options = lz4_extract_options,
280 .check_options = lz4_check_options,
281 .display_options = lz4_display_options,
282 .usage = lz4_usage,
283 .id = LZ4_COMPRESSION,
284 .name = "lz4",
285 .supported = 1
286 };