]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - SQUASHFS/squashfs-tools-4.4/squashfs-tools/lz4_wrapper.c
2 * Copyright (c) 2013, 2019
3 * Phillip Lougher <phillip@squashfs.org.uk>
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.
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.
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.
21 * Support for LZ4 compression http://fastcompression.blogspot.com/p/lz4.html
30 #include "squashfs_fs.h"
31 #include "lz4_wrapper.h"
32 #include "compressor.h"
34 /* LZ4 1.7.0 introduced new functions, and since r131,
35 * the older functions produce deprecated warnings.
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.
41 * Support both by checking the library version and
42 * using shadow definitions
45 /* Earlier (but > 1.7.0) versions don't define this */
46 #ifndef LZ4HC_CLEVEL_MAX
47 #define LZ4HC_CLEVEL_MAX 12
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)
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)
61 * This function is called by the options parsing code in mksquashfs.c
62 * to parse any -X compressor option.
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)
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.
75 static int lz4_options(char *argv
[], int argc
)
77 if(strcmp(argv
[0], "-Xhc") == 0) {
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).
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
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
101 static void *lz4_dump_options(int block_size
, int *size
)
103 static struct lz4_comp_opts comp_opts
;
105 comp_opts
.version
= LZ4_LEGACY
;
106 comp_opts
.flags
= hc
? LZ4_HC
: 0;
107 SQUASHFS_INSWAP_COMP_OPTS(&comp_opts
);
109 *size
= sizeof(comp_opts
);
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
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.
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
131 * This function returns 0 on sucessful extraction of options, and
134 static int lz4_extract_options(int block_size
, void *buffer
, int size
)
136 struct lz4_comp_opts
*comp_opts
= buffer
;
138 /* we expect a comp_opts structure to be present */
139 if(size
< sizeof(*comp_opts
))
142 SQUASHFS_INSWAP_COMP_OPTS(comp_opts
);
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");
151 * Check compression flags, currently only LZ4_HC ("high compression")
154 if(comp_opts
->flags
== LZ4_HC
)
156 else if(comp_opts
->flags
!= 0) {
157 fprintf(stderr
, "lz4: unknown LZ4 flags\n");
164 fprintf(stderr
, "lz4: error reading stored compressor options from "
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.
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.
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.
183 * This function returns 0 on sucessful checking of options, and
186 static int lz4_check_options(int block_size
, void *buffer
, int size
)
188 struct lz4_comp_opts
*comp_opts
= buffer
;
190 /* we expect a comp_opts structure to be present */
191 if(size
< sizeof(*comp_opts
))
194 SQUASHFS_INSWAP_COMP_OPTS(comp_opts
);
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");
205 fprintf(stderr
, "lz4: error reading stored compressor options from "
211 static void lz4_display_options(void *buffer
, int size
)
213 struct lz4_comp_opts
*comp_opts
= buffer
;
215 /* check passed comp opts struct is of the correct length */
216 if(size
< sizeof(*comp_opts
))
219 SQUASHFS_INSWAP_COMP_OPTS(comp_opts
);
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");
228 * Check compression flags, currently only LZ4_HC ("high compression")
231 if(comp_opts
->flags
& ~LZ4_FLAGS_MASK
) {
232 fprintf(stderr
, "lz4: unknown LZ4 flags\n");
236 if(comp_opts
->flags
& LZ4_HC
)
237 printf("\tHigh Compression option specified (-Xhc)\n");
242 fprintf(stderr
, "lz4: error reading stored compressor options from "
247 static int lz4_compress(void *strm
, void *dest
, void *src
, int size
,
248 int block_size
, int *error
)
254 static int lz4_uncompress(void *dest
, void *src
, int size
, int outsize
,
257 int res
= LZ4_decompress_safe(src
, dest
, size
, outsize
);
267 static void lz4_usage()
269 fprintf(stderr
, "\t -Xhc\n");
270 fprintf(stderr
, "\t\tCompress using LZ4 High Compression\n");
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
,
283 .id
= LZ4_COMPRESSION
,