]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - SQUASHFS/squashfs-tools-4.4/squashfs-tools/zstd_wrapper.c
Support TrueNAS Scale (Linux) distro. (#3069 #3137)
[Ventoy.git] / SQUASHFS / squashfs-tools-4.4 / squashfs-tools / zstd_wrapper.c
1 /*
2 * Copyright (c) 2017
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 * zstd_wrapper.c
16 *
17 * Support for ZSTD compression http://zstd.net
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <zstd.h>
24 #include <zstd_errors.h>
25
26 #include "squashfs_fs.h"
27 #include "zstd_wrapper.h"
28 #include "compressor.h"
29
30 static int compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
31
32 /*
33 * This function is called by the options parsing code in mksquashfs.c
34 * to parse any -X compressor option.
35 *
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)
41 *
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.
46 */
47 static int zstd_options(char *argv[], int argc)
48 {
49 return 1;
50 }
51
52 /*
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).
57 *
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
60 * options.
61 */
62 static void *zstd_dump_options(int block_size, int *size)
63 {
64 return NULL;
65 }
66
67 /*
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
71 * structure.
72 *
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.
78 *
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.
83 *
84 * This function returns 0 on sucessful extraction of options, and -1 on error.
85 */
86 static int zstd_extract_options(int block_size, void *buffer, int size)
87 {
88 struct zstd_comp_opts *comp_opts = buffer;
89
90 if (size == 0) {
91 /* Set default values */
92 compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
93 return 0;
94 }
95
96 /* we expect a comp_opts structure of sufficient size to be present */
97 if (size < sizeof(*comp_opts))
98 goto failed;
99
100 SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
101
102 if (comp_opts->compression_level < 1) {
103 fprintf(stderr, "zstd: bad compression level in compression "
104 "options structure\n");
105 goto failed;
106 }
107
108 compression_level = comp_opts->compression_level;
109
110 return 0;
111
112 failed:
113 fprintf(stderr, "zstd: error reading stored compressor options from "
114 "filesystem!\n");
115
116 return -1;
117 }
118
119 static void zstd_display_options(void *buffer, int size)
120 {
121 struct zstd_comp_opts *comp_opts = buffer;
122
123 /* we expect a comp_opts structure of sufficient size to be present */
124 if (size < sizeof(*comp_opts))
125 goto failed;
126
127 SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
128
129 if (comp_opts->compression_level < 1) {
130 fprintf(stderr, "zstd: bad compression level in compression "
131 "options structure\n");
132 goto failed;
133 }
134
135 printf("\tcompression-level %d\n", comp_opts->compression_level);
136
137 return;
138
139 failed:
140 fprintf(stderr, "zstd: error reading stored compressor options from "
141 "filesystem!\n");
142 }
143
144 /*
145 * This function is called by mksquashfs to initialise the
146 * compressor, before compress() is called.
147 *
148 * This function returns 0 on success, and -1 on error.
149 */
150 static int zstd_init(void **strm, int block_size, int datablock)
151 {
152 return 0;
153 }
154
155 static int zstd_compress(void *strm, void *dest, void *src, int size,
156 int block_size, int *error)
157 {
158 (void)strm;
159 (void)dest;
160 (void)src;
161 (void)size;
162 (void)block_size;
163 (void)error;
164 return 0;
165 }
166
167 static int zstd_uncompress(void *dest, void *src, int size, int outsize,
168 int *error)
169 {
170 const size_t res = ZSTD_decompress(dest, outsize, src, size);
171
172 if (ZSTD_isError(res)) {
173 fprintf(stderr, "\t%d %d\n", outsize, size);
174
175 *error = (int)ZSTD_getErrorCode(res);
176 return -1;
177 }
178
179 return (int)res;
180 }
181
182 static void zstd_usage(void)
183 {
184 fprintf(stderr, "\t -Xcompression-level <compression-level>\n");
185 }
186
187 struct compressor zstd_comp_ops = {
188 .init = zstd_init,
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,
195 .usage = zstd_usage,
196 .id = ZSTD_COMPRESSION,
197 .name = "zstd",
198 .supported = 1
199 };