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 */
zstd_options(char * argv[],int argc)47 static int zstd_options(char *argv[], int argc)
48 {
49 if (strcmp(argv[0], "-Xcompression-level") == 0) {
50 if (argc < 2) {
51 fprintf(stderr, "zstd: -Xcompression-level missing "
52 "compression level\n");
53 fprintf(stderr, "zstd: -Xcompression-level it should "
54 "be 1 <= n <= %d\n", ZSTD_maxCLevel());
55 goto failed;
56 }
57
58 compression_level = atoi(argv[1]);
59 if (compression_level < 1 ||
60 compression_level > ZSTD_maxCLevel()) {
61 fprintf(stderr, "zstd: -Xcompression-level invalid, it "
62 "should be 1 <= n <= %d\n", ZSTD_maxCLevel());
63 goto failed;
64 }
65
66 return 1;
67 }
68
69 return -1;
70 failed:
71 return -2;
72 }
73
74 /*
75 * This function is called by mksquashfs to dump the parsed
76 * compressor options in a format suitable for writing to the
77 * compressor options field in the filesystem (stored immediately
78 * after the superblock).
79 *
80 * This function returns a pointer to the compression options structure
81 * to be stored (and the size), or NULL if there are no compression
82 * options.
83 */
zstd_dump_options(int block_size,int * size)84 static void *zstd_dump_options(int block_size, int *size)
85 {
86 static struct zstd_comp_opts comp_opts;
87
88 /* don't return anything if the options are all default */
89 if (compression_level == ZSTD_DEFAULT_COMPRESSION_LEVEL)
90 return NULL;
91
92 comp_opts.compression_level = compression_level;
93
94 SQUASHFS_INSWAP_COMP_OPTS(&comp_opts);
95
96 *size = sizeof(comp_opts);
97 return &comp_opts;
98 }
99
100 /*
101 * This function is a helper specifically for the append mode of
102 * mksquashfs. Its purpose is to set the internal compressor state
103 * to the stored compressor options in the passed compressor options
104 * structure.
105 *
106 * In effect this function sets up the compressor options
107 * to the same state they were when the filesystem was originally
108 * generated, this is to ensure on appending, the compressor uses
109 * the same compression options that were used to generate the
110 * original filesystem.
111 *
112 * Note, even if there are no compressor options, this function is still
113 * called with an empty compressor structure (size == 0), to explicitly
114 * set the default options, this is to ensure any user supplied
115 * -X options on the appending mksquashfs command line are over-ridden.
116 *
117 * This function returns 0 on sucessful extraction of options, and -1 on error.
118 */
zstd_extract_options(int block_size,void * buffer,int size)119 static int zstd_extract_options(int block_size, void *buffer, int size)
120 {
121 struct zstd_comp_opts *comp_opts = buffer;
122
123 if (size == 0) {
124 /* Set default values */
125 compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
126 return 0;
127 }
128
129 /* we expect a comp_opts structure of sufficient size to be present */
130 if (size < sizeof(*comp_opts))
131 goto failed;
132
133 SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
134
135 if (comp_opts->compression_level < 1 ||
136 comp_opts->compression_level > ZSTD_maxCLevel()) {
137 fprintf(stderr, "zstd: bad compression level in compression "
138 "options structure\n");
139 goto failed;
140 }
141
142 compression_level = comp_opts->compression_level;
143
144 return 0;
145
146 failed:
147 fprintf(stderr, "zstd: error reading stored compressor options from "
148 "filesystem!\n");
149
150 return -1;
151 }
152
zstd_display_options(void * buffer,int size)153 static void zstd_display_options(void *buffer, int size)
154 {
155 struct zstd_comp_opts *comp_opts = buffer;
156
157 /* we expect a comp_opts structure of sufficient size to be present */
158 if (size < sizeof(*comp_opts))
159 goto failed;
160
161 SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
162
163 if (comp_opts->compression_level < 1 ||
164 comp_opts->compression_level > ZSTD_maxCLevel()) {
165 fprintf(stderr, "zstd: bad compression level in compression "
166 "options structure\n");
167 goto failed;
168 }
169
170 printf("\tcompression-level %d\n", comp_opts->compression_level);
171
172 return;
173
174 failed:
175 fprintf(stderr, "zstd: error reading stored compressor options from "
176 "filesystem!\n");
177 }
178
179 /*
180 * This function is called by mksquashfs to initialise the
181 * compressor, before compress() is called.
182 *
183 * This function returns 0 on success, and -1 on error.
184 */
zstd_init(void ** strm,int block_size,int datablock)185 static int zstd_init(void **strm, int block_size, int datablock)
186 {
187 ZSTD_CCtx *cctx = ZSTD_createCCtx();
188
189 if (!cctx) {
190 fprintf(stderr, "zstd: failed to allocate compression "
191 "context!\n");
192 return -1;
193 }
194
195 *strm = cctx;
196 return 0;
197 }
198
zstd_compress(void * strm,void * dest,void * src,int size,int block_size,int * error)199 static int zstd_compress(void *strm, void *dest, void *src, int size,
200 int block_size, int *error)
201 {
202 const size_t res = ZSTD_compressCCtx((ZSTD_CCtx*)strm, dest, block_size,
203 src, size, compression_level);
204
205 if (ZSTD_isError(res)) {
206 /* FIXME:
207 * zstd does not expose stable error codes. The error enum may
208 * change between versions. Until upstream zstd stablizes the
209 * error codes, we have no way of knowing why the error occurs.
210 * zstd shouldn't fail to compress any input unless there isn't
211 * enough output space. We assume that is the cause and return
212 * the special error code for not enough output space.
213 */
214 return 0;
215 }
216
217 return (int)res;
218 }
219
zstd_uncompress(void * dest,void * src,int size,int outsize,int * error)220 static int zstd_uncompress(void *dest, void *src, int size, int outsize,
221 int *error)
222 {
223 const size_t res = ZSTD_decompress(dest, outsize, src, size);
224
225 if (ZSTD_isError(res)) {
226 fprintf(stderr, "\t%d %d\n", outsize, size);
227
228 *error = (int)ZSTD_getErrorCode(res);
229 return -1;
230 }
231
232 return (int)res;
233 }
234
zstd_usage(void)235 static void zstd_usage(void)
236 {
237 fprintf(stderr, "\t -Xcompression-level <compression-level>\n");
238 fprintf(stderr, "\t\t<compression-level> should be 1 .. %d (default "
239 "%d)\n", ZSTD_maxCLevel(), ZSTD_DEFAULT_COMPRESSION_LEVEL);
240 }
241
242 struct compressor zstd_comp_ops = {
243 .init = zstd_init,
244 .compress = zstd_compress,
245 .uncompress = zstd_uncompress,
246 .options = zstd_options,
247 .dump_options = zstd_dump_options,
248 .extract_options = zstd_extract_options,
249 .display_options = zstd_display_options,
250 .usage = zstd_usage,
251 .id = ZSTD_COMPRESSION,
252 .name = "zstd",
253 .supported = 1
254 };
255