1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/string.h>
6 #include <linux/zstd.h>
7 
8 #include "zstd.h"
9 #include "common/zstd_deps.h"
10 #include "common/zstd_internal.h"
11 
zstd_check_structs(void)12 static void zstd_check_structs(void) {
13 	/* Check that the structs have the same size. */
14 	ZSTD_STATIC_ASSERT(sizeof(ZSTD_parameters) ==
15 		sizeof(struct zstd_parameters));
16 	ZSTD_STATIC_ASSERT(sizeof(ZSTD_compressionParameters) ==
17 		sizeof(struct zstd_compression_parameters));
18 	ZSTD_STATIC_ASSERT(sizeof(ZSTD_frameParameters) ==
19 		sizeof(struct zstd_frame_parameters));
20 	/* Zstd guarantees that the layout of the structs never change. Verify it. */
21 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_parameters, cParams) ==
22 		offsetof(struct zstd_parameters, cparams));
23 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_parameters, fParams) ==
24 		offsetof(struct zstd_parameters, fparams));
25 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, windowLog) ==
26 		offsetof(struct zstd_compression_parameters, window_log));
27 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, chainLog) ==
28 		offsetof(struct zstd_compression_parameters, chain_log));
29 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, hashLog) ==
30 		offsetof(struct zstd_compression_parameters, hash_log));
31 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, searchLog) ==
32 		offsetof(struct zstd_compression_parameters, search_log));
33 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, minMatch) ==
34 		offsetof(struct zstd_compression_parameters, search_length));
35 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, targetLength) ==
36 		offsetof(struct zstd_compression_parameters, target_length));
37 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, strategy) ==
38 		offsetof(struct zstd_compression_parameters, strategy));
39 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_frameParameters, contentSizeFlag) ==
40 		offsetof(struct zstd_frame_parameters, content_size_flag));
41 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_frameParameters, checksumFlag) ==
42 		offsetof(struct zstd_frame_parameters, checksum_flag));
43 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_frameParameters, noDictIDFlag) ==
44 		offsetof(struct zstd_frame_parameters, no_dict_id_flag));
45 	/* Check that the strategies are the same. This can change. */
46 	ZSTD_STATIC_ASSERT((int)ZSTD_fast == (int)zstd_fast);
47 	ZSTD_STATIC_ASSERT((int)ZSTD_dfast == (int)zstd_dfast);
48 	ZSTD_STATIC_ASSERT((int)ZSTD_greedy == (int)zstd_greedy);
49 	ZSTD_STATIC_ASSERT((int)ZSTD_lazy == (int)zstd_lazy);
50 	ZSTD_STATIC_ASSERT((int)ZSTD_lazy2 == (int)zstd_lazy2);
51 	ZSTD_STATIC_ASSERT((int)ZSTD_btlazy2 == (int)zstd_btlazy2);
52 	ZSTD_STATIC_ASSERT((int)ZSTD_btopt == (int)zstd_btopt);
53 	ZSTD_STATIC_ASSERT((int)ZSTD_btultra == (int)zstd_btultra);
54 	ZSTD_STATIC_ASSERT((int)ZSTD_btultra2 == (int)zstd_btultra2);
55 	/* Check input buffer */
56 	ZSTD_STATIC_ASSERT(sizeof(ZSTD_inBuffer) == sizeof(struct zstd_in_buffer));
57 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_inBuffer, src) ==
58 		offsetof(struct zstd_in_buffer, src));
59 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_inBuffer, size) ==
60 		offsetof(struct zstd_in_buffer, size));
61 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_inBuffer, pos) ==
62 		offsetof(struct zstd_in_buffer, pos));
63 	/* Check output buffer */
64 	ZSTD_STATIC_ASSERT(sizeof(ZSTD_outBuffer) ==
65 		sizeof(struct zstd_out_buffer));
66 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_outBuffer, dst) ==
67 		offsetof(struct zstd_out_buffer, dst));
68 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_outBuffer, size) ==
69 		offsetof(struct zstd_out_buffer, size));
70 	ZSTD_STATIC_ASSERT(offsetof(ZSTD_outBuffer, pos) ==
71 		offsetof(struct zstd_out_buffer, pos));
72 }
73 
zstd_compress_bound(size_t src_size)74 size_t zstd_compress_bound(size_t src_size)
75 {
76 	return ZSTD_compressBound(src_size);
77 }
78 EXPORT_SYMBOL(zstd_compress_bound);
79 
zstd_get_params(int level,unsigned long long estimated_src_size)80 struct zstd_parameters zstd_get_params(int level,
81 	unsigned long long estimated_src_size)
82 {
83 	const ZSTD_parameters params = ZSTD_getParams(level, estimated_src_size, 0);
84 	struct zstd_parameters out;
85 
86 	/* no-op */
87 	zstd_check_structs();
88 	ZSTD_memcpy(&out, &params, sizeof(out));
89 	return out;
90 }
91 EXPORT_SYMBOL(zstd_get_params);
92 
zstd_cctx_workspace_bound(const struct zstd_compression_parameters * cparams)93 size_t zstd_cctx_workspace_bound(
94 	const struct zstd_compression_parameters *cparams)
95 {
96 	ZSTD_compressionParameters p;
97 
98 	ZSTD_memcpy(&p, cparams, sizeof(p));
99 	return ZSTD_estimateCCtxSize_usingCParams(p);
100 }
101 EXPORT_SYMBOL(zstd_cctx_workspace_bound);
102 
zstd_init_cctx(void * workspace,size_t workspace_size)103 zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size)
104 {
105 	if (workspace == NULL)
106 		return NULL;
107 	return ZSTD_initStaticCCtx(workspace, workspace_size);
108 }
109 EXPORT_SYMBOL(zstd_init_cctx);
110 
zstd_compress_cctx(zstd_cctx * cctx,void * dst,size_t dst_capacity,const void * src,size_t src_size,const struct zstd_parameters * parameters)111 size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
112 	const void *src, size_t src_size, const struct zstd_parameters *parameters)
113 {
114 	ZSTD_parameters p;
115 
116 	ZSTD_memcpy(&p, parameters, sizeof(p));
117 	return ZSTD_compress_advanced(cctx, dst, dst_capacity, src, src_size, NULL, 0, p);
118 }
119 EXPORT_SYMBOL(zstd_compress_cctx);
120 
zstd_cstream_workspace_bound(const struct zstd_compression_parameters * cparams)121 size_t zstd_cstream_workspace_bound(
122 	const struct zstd_compression_parameters *cparams)
123 {
124 	ZSTD_compressionParameters p;
125 
126 	ZSTD_memcpy(&p, cparams, sizeof(p));
127 	return ZSTD_estimateCStreamSize_usingCParams(p);
128 }
129 EXPORT_SYMBOL(zstd_cstream_workspace_bound);
130 
zstd_init_cstream(const struct zstd_parameters * parameters,unsigned long long pledged_src_size,void * workspace,size_t workspace_size)131 zstd_cstream *zstd_init_cstream(const struct zstd_parameters *parameters,
132 	unsigned long long pledged_src_size, void *workspace, size_t workspace_size)
133 {
134 	ZSTD_parameters p;
135 	zstd_cstream *cstream;
136 	size_t ret;
137 
138 	if (workspace == NULL)
139 		return NULL;
140 
141 	cstream = ZSTD_initStaticCStream(workspace, workspace_size);
142 	if (cstream == NULL)
143 		return NULL;
144 
145 	/* 0 means unknown in linux zstd API but means 0 in new zstd API */
146 	if (pledged_src_size == 0)
147 		pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN;
148 
149 	ZSTD_memcpy(&p, parameters, sizeof(p));
150 	ret = ZSTD_initCStream_advanced(cstream, NULL, 0, p, pledged_src_size);
151 	if (ZSTD_isError(ret))
152 		return NULL;
153 
154 	return cstream;
155 }
156 EXPORT_SYMBOL(zstd_init_cstream);
157 
zstd_reset_cstream(zstd_cstream * cstream,unsigned long long pledged_src_size)158 size_t zstd_reset_cstream(zstd_cstream *cstream,
159 	unsigned long long pledged_src_size)
160 {
161 	return ZSTD_resetCStream(cstream, pledged_src_size);
162 }
163 EXPORT_SYMBOL(zstd_reset_cstream);
164 
zstd_compress_stream(zstd_cstream * cstream,struct zstd_out_buffer * output,struct zstd_in_buffer * input)165 size_t zstd_compress_stream(zstd_cstream *cstream,
166 	struct zstd_out_buffer *output, struct zstd_in_buffer *input)
167 {
168 	ZSTD_outBuffer o;
169 	ZSTD_inBuffer i;
170 	size_t ret;
171 
172 	ZSTD_memcpy(&o, output, sizeof(o));
173 	ZSTD_memcpy(&i, input, sizeof(i));
174 	ret = ZSTD_compressStream(cstream, &o, &i);
175 	ZSTD_memcpy(output, &o, sizeof(o));
176 	ZSTD_memcpy(input, &i, sizeof(i));
177 	return ret;
178 }
179 EXPORT_SYMBOL(zstd_compress_stream);
180 
zstd_flush_stream(zstd_cstream * cstream,struct zstd_out_buffer * output)181 size_t zstd_flush_stream(zstd_cstream *cstream, struct zstd_out_buffer *output)
182 {
183 	ZSTD_outBuffer o;
184 	size_t ret;
185 
186 	ZSTD_memcpy(&o, output, sizeof(o));
187 	ret = ZSTD_flushStream(cstream, &o);
188 	ZSTD_memcpy(output, &o, sizeof(o));
189 	return ret;
190 }
191 EXPORT_SYMBOL(zstd_flush_stream);
192 
zstd_end_stream(zstd_cstream * cstream,struct zstd_out_buffer * output)193 size_t zstd_end_stream(zstd_cstream *cstream, struct zstd_out_buffer *output)
194 {
195 	ZSTD_outBuffer o;
196 	size_t ret;
197 
198 	ZSTD_memcpy(&o, output, sizeof(o));
199 	ret = ZSTD_endStream(cstream, &o);
200 	ZSTD_memcpy(output, &o, sizeof(o));
201 	return ret;
202 }
203 EXPORT_SYMBOL(zstd_end_stream);
204 
205 MODULE_LICENSE("Dual BSD/GPL");
206 MODULE_DESCRIPTION("Zstd Compressor");
207