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.
17 * Support for ZSTD compression http://zstd.net
24 #include <zstd_errors.h>
26 #include "squashfs_fs.h"
27 #include "zstd_wrapper.h"
28 #include "compressor.h"
30 static int compression_level
= ZSTD_DEFAULT_COMPRESSION_LEVEL
;
33 * This function is called by the options parsing code in mksquashfs.c
34 * to parse any -X compressor option.
36 * This function returns:
37 * >=0 (number of additional args parsed) on success
38 * -1 if the option was unrecognised, or
39 * -2 if the option was recognised, but otherwise bad in
40 * some way (e.g. invalid parameter)
42 * Note: this function sets internal compressor state, but does not
43 * pass back the results of the parsing other than success/failure.
44 * The zstd_dump_options() function is called later to get the options in
45 * a format suitable for writing to the filesystem.
47 static int zstd_options(char *argv
[], int argc
)
53 * This function is called by mksquashfs to dump the parsed
54 * compressor options in a format suitable for writing to the
55 * compressor options field in the filesystem (stored immediately
56 * after the superblock).
58 * This function returns a pointer to the compression options structure
59 * to be stored (and the size), or NULL if there are no compression
62 static void *zstd_dump_options(int block_size
, int *size
)
68 * This function is a helper specifically for the append mode of
69 * mksquashfs. Its purpose is to set the internal compressor state
70 * to the stored compressor options in the passed compressor options
73 * In effect this function sets up the compressor options
74 * to the same state they were when the filesystem was originally
75 * generated, this is to ensure on appending, the compressor uses
76 * the same compression options that were used to generate the
77 * original filesystem.
79 * Note, even if there are no compressor options, this function is still
80 * called with an empty compressor structure (size == 0), to explicitly
81 * set the default options, this is to ensure any user supplied
82 * -X options on the appending mksquashfs command line are over-ridden.
84 * This function returns 0 on sucessful extraction of options, and -1 on error.
86 static int zstd_extract_options(int block_size
, void *buffer
, int size
)
88 struct zstd_comp_opts
*comp_opts
= buffer
;
91 /* Set default values */
92 compression_level
= ZSTD_DEFAULT_COMPRESSION_LEVEL
;
96 /* we expect a comp_opts structure of sufficient size to be present */
97 if (size
< sizeof(*comp_opts
))
100 SQUASHFS_INSWAP_COMP_OPTS(comp_opts
);
102 if (comp_opts
->compression_level
< 1) {
103 fprintf(stderr
, "zstd: bad compression level in compression "
104 "options structure\n");
108 compression_level
= comp_opts
->compression_level
;
113 fprintf(stderr
, "zstd: error reading stored compressor options from "
119 static void zstd_display_options(void *buffer
, int size
)
121 struct zstd_comp_opts
*comp_opts
= buffer
;
123 /* we expect a comp_opts structure of sufficient size to be present */
124 if (size
< sizeof(*comp_opts
))
127 SQUASHFS_INSWAP_COMP_OPTS(comp_opts
);
129 if (comp_opts
->compression_level
< 1) {
130 fprintf(stderr
, "zstd: bad compression level in compression "
131 "options structure\n");
135 printf("\tcompression-level %d\n", comp_opts
->compression_level
);
140 fprintf(stderr
, "zstd: error reading stored compressor options from "
145 * This function is called by mksquashfs to initialise the
146 * compressor, before compress() is called.
148 * This function returns 0 on success, and -1 on error.
150 static int zstd_init(void **strm
, int block_size
, int datablock
)
155 static int zstd_compress(void *strm
, void *dest
, void *src
, int size
,
156 int block_size
, int *error
)
167 static int zstd_uncompress(void *dest
, void *src
, int size
, int outsize
,
170 const size_t res
= ZSTD_decompress(dest
, outsize
, src
, size
);
172 if (ZSTD_isError(res
)) {
173 fprintf(stderr
, "\t%d %d\n", outsize
, size
);
175 *error
= (int)ZSTD_getErrorCode(res
);
182 static void zstd_usage(void)
184 fprintf(stderr
, "\t -Xcompression-level <compression-level>\n");
187 struct compressor zstd_comp_ops
= {
189 .compress
= zstd_compress
,
190 .uncompress
= zstd_uncompress
,
191 .options
= zstd_options
,
192 .dump_options
= zstd_dump_options
,
193 .extract_options
= zstd_extract_options
,
194 .display_options
= zstd_display_options
,
196 .id
= ZSTD_COMPRESSION
,