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_errors.h"
11
12 /* Common symbols. zstd_compress must depend on zstd_decompress. */
13
zstd_is_error(size_t code)14 unsigned int zstd_is_error(size_t code)
15 {
16 return ZSTD_isError(code);
17 }
18 EXPORT_SYMBOL(zstd_is_error);
19
zstd_get_error_code(size_t code)20 int zstd_get_error_code(size_t code)
21 {
22 return ZSTD_getErrorCode(code);
23 }
24 EXPORT_SYMBOL(zstd_get_error_code);
25
zstd_get_error_name(size_t code)26 const char *zstd_get_error_name(size_t code)
27 {
28 return ZSTD_getErrorName(code);
29 }
30 EXPORT_SYMBOL(zstd_get_error_name);
31
32 /* Decompression symbols. */
33
zstd_dctx_workspace_bound(void)34 size_t zstd_dctx_workspace_bound(void)
35 {
36 return ZSTD_estimateDCtxSize();
37 }
38 EXPORT_SYMBOL(zstd_dctx_workspace_bound);
39
zstd_init_dctx(void * workspace,size_t workspace_size)40 zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size)
41 {
42 if (workspace == NULL)
43 return NULL;
44 return ZSTD_initStaticDCtx(workspace, workspace_size);
45 }
46 EXPORT_SYMBOL(zstd_init_dctx);
47
zstd_decompress_dctx(zstd_dctx * dctx,void * dst,size_t dst_capacity,const void * src,size_t src_size)48 size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity,
49 const void *src, size_t src_size)
50 {
51 return ZSTD_decompressDCtx(dctx, dst, dst_capacity, src, src_size);
52 }
53 EXPORT_SYMBOL(zstd_decompress_dctx);
54
zstd_dstream_workspace_bound(size_t max_window_size)55 size_t zstd_dstream_workspace_bound(size_t max_window_size)
56 {
57 return ZSTD_estimateDStreamSize(max_window_size);
58 }
59 EXPORT_SYMBOL(zstd_dstream_workspace_bound);
60
zstd_init_dstream(size_t max_window_size,void * workspace,size_t workspace_size)61 zstd_dstream *zstd_init_dstream(size_t max_window_size, void *workspace,
62 size_t workspace_size)
63 {
64 if (workspace == NULL)
65 return NULL;
66 (void)max_window_size;
67 return ZSTD_initStaticDStream(workspace, workspace_size);
68 }
69 EXPORT_SYMBOL(zstd_init_dstream);
70
zstd_reset_dstream(zstd_dstream * dstream)71 size_t zstd_reset_dstream(zstd_dstream *dstream)
72 {
73 return ZSTD_resetDStream(dstream);
74 }
75 EXPORT_SYMBOL(zstd_reset_dstream);
76
zstd_decompress_stream(zstd_dstream * dstream,struct zstd_out_buffer * output,struct zstd_in_buffer * input)77 size_t zstd_decompress_stream(zstd_dstream *dstream,
78 struct zstd_out_buffer *output, struct zstd_in_buffer *input)
79 {
80 ZSTD_outBuffer o;
81 ZSTD_inBuffer i;
82 size_t ret;
83
84 ZSTD_memcpy(&o, output, sizeof(o));
85 ZSTD_memcpy(&i, input, sizeof(i));
86 ret = ZSTD_decompressStream(dstream, &o, &i);
87 ZSTD_memcpy(output, &o, sizeof(o));
88 ZSTD_memcpy(input, &i, sizeof(i));
89 return ret;
90 }
91 EXPORT_SYMBOL(zstd_decompress_stream);
92
zstd_find_frame_compressed_size(const void * src,size_t src_size)93 size_t zstd_find_frame_compressed_size(const void *src, size_t src_size)
94 {
95 return ZSTD_findFrameCompressedSize(src, src_size);
96 }
97 EXPORT_SYMBOL(zstd_find_frame_compressed_size);
98
zstd_get_frame_params(struct zstd_frame_params * params,const void * src,size_t src_size)99 size_t zstd_get_frame_params(struct zstd_frame_params *params, const void *src,
100 size_t src_size)
101 {
102 ZSTD_frameHeader h;
103 const size_t ret = ZSTD_getFrameHeader(&h, src, src_size);
104
105 if (ret != 0)
106 return ret;
107
108 if (h.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN)
109 params->frame_content_size = h.frameContentSize;
110 else
111 params->frame_content_size = 0;
112
113 params->window_size = h.windowSize;
114 params->dict_id = h.dictID;
115 params->checksum_flag = h.checksumFlag;
116
117 return ret;
118 }
119 EXPORT_SYMBOL(zstd_get_frame_params);
120
121 MODULE_LICENSE("Dual BSD/GPL");
122 MODULE_DESCRIPTION("Zstd Decompressor");
123