1  // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2  /*
3   * Copyright (c) Facebook, Inc.
4   * All rights reserved.
5   *
6   * This source code is licensed under both the BSD-style license (found in the
7   * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8   * in the COPYING file in the root directory of this source tree).
9   * You may select, at your option, one of the above-listed licenses.
10   */
11  
12  #include <linux/kernel.h>
13  #include <linux/module.h>
14  #include <linux/string.h>
15  #include <linux/zstd.h>
16  
17  #include "common/zstd_deps.h"
18  
19  /* Common symbols. zstd_compress must depend on zstd_decompress. */
20  
zstd_is_error(size_t code)21  unsigned int zstd_is_error(size_t code)
22  {
23  	return ZSTD_isError(code);
24  }
25  EXPORT_SYMBOL(zstd_is_error);
26  
zstd_get_error_code(size_t code)27  zstd_error_code zstd_get_error_code(size_t code)
28  {
29  	return ZSTD_getErrorCode(code);
30  }
31  EXPORT_SYMBOL(zstd_get_error_code);
32  
zstd_get_error_name(size_t code)33  const char *zstd_get_error_name(size_t code)
34  {
35  	return ZSTD_getErrorName(code);
36  }
37  EXPORT_SYMBOL(zstd_get_error_name);
38  
39  /* Decompression symbols. */
40  
zstd_dctx_workspace_bound(void)41  size_t zstd_dctx_workspace_bound(void)
42  {
43  	return ZSTD_estimateDCtxSize();
44  }
45  EXPORT_SYMBOL(zstd_dctx_workspace_bound);
46  
zstd_create_dctx_advanced(zstd_custom_mem custom_mem)47  zstd_dctx *zstd_create_dctx_advanced(zstd_custom_mem custom_mem)
48  {
49  	return ZSTD_createDCtx_advanced(custom_mem);
50  }
51  EXPORT_SYMBOL(zstd_create_dctx_advanced);
52  
zstd_free_dctx(zstd_dctx * dctx)53  size_t zstd_free_dctx(zstd_dctx *dctx)
54  {
55  	return ZSTD_freeDCtx(dctx);
56  }
57  EXPORT_SYMBOL(zstd_free_dctx);
58  
zstd_create_ddict_byreference(const void * dict,size_t dict_size,zstd_custom_mem custom_mem)59  zstd_ddict *zstd_create_ddict_byreference(const void *dict, size_t dict_size,
60  					  zstd_custom_mem custom_mem)
61  {
62  	return ZSTD_createDDict_advanced(dict, dict_size, ZSTD_dlm_byRef,
63  					 ZSTD_dct_auto, custom_mem);
64  
65  }
66  EXPORT_SYMBOL(zstd_create_ddict_byreference);
67  
zstd_free_ddict(zstd_ddict * ddict)68  size_t zstd_free_ddict(zstd_ddict *ddict)
69  {
70  	return ZSTD_freeDDict(ddict);
71  }
72  EXPORT_SYMBOL(zstd_free_ddict);
73  
zstd_init_dctx(void * workspace,size_t workspace_size)74  zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size)
75  {
76  	if (workspace == NULL)
77  		return NULL;
78  	return ZSTD_initStaticDCtx(workspace, workspace_size);
79  }
80  EXPORT_SYMBOL(zstd_init_dctx);
81  
zstd_decompress_dctx(zstd_dctx * dctx,void * dst,size_t dst_capacity,const void * src,size_t src_size)82  size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity,
83  	const void *src, size_t src_size)
84  {
85  	return ZSTD_decompressDCtx(dctx, dst, dst_capacity, src, src_size);
86  }
87  EXPORT_SYMBOL(zstd_decompress_dctx);
88  
zstd_decompress_using_ddict(zstd_dctx * dctx,void * dst,size_t dst_capacity,const void * src,size_t src_size,const zstd_ddict * ddict)89  size_t zstd_decompress_using_ddict(zstd_dctx *dctx,
90  	void *dst, size_t dst_capacity, const void* src, size_t src_size,
91  	const zstd_ddict* ddict)
92  {
93  	return ZSTD_decompress_usingDDict(dctx, dst, dst_capacity, src,
94  					  src_size, ddict);
95  }
96  EXPORT_SYMBOL(zstd_decompress_using_ddict);
97  
zstd_dstream_workspace_bound(size_t max_window_size)98  size_t zstd_dstream_workspace_bound(size_t max_window_size)
99  {
100  	return ZSTD_estimateDStreamSize(max_window_size);
101  }
102  EXPORT_SYMBOL(zstd_dstream_workspace_bound);
103  
zstd_init_dstream(size_t max_window_size,void * workspace,size_t workspace_size)104  zstd_dstream *zstd_init_dstream(size_t max_window_size, void *workspace,
105  	size_t workspace_size)
106  {
107  	if (workspace == NULL)
108  		return NULL;
109  	(void)max_window_size;
110  	return ZSTD_initStaticDStream(workspace, workspace_size);
111  }
112  EXPORT_SYMBOL(zstd_init_dstream);
113  
zstd_reset_dstream(zstd_dstream * dstream)114  size_t zstd_reset_dstream(zstd_dstream *dstream)
115  {
116  	return ZSTD_resetDStream(dstream);
117  }
118  EXPORT_SYMBOL(zstd_reset_dstream);
119  
zstd_decompress_stream(zstd_dstream * dstream,zstd_out_buffer * output,zstd_in_buffer * input)120  size_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output,
121  	zstd_in_buffer *input)
122  {
123  	return ZSTD_decompressStream(dstream, output, input);
124  }
125  EXPORT_SYMBOL(zstd_decompress_stream);
126  
zstd_find_frame_compressed_size(const void * src,size_t src_size)127  size_t zstd_find_frame_compressed_size(const void *src, size_t src_size)
128  {
129  	return ZSTD_findFrameCompressedSize(src, src_size);
130  }
131  EXPORT_SYMBOL(zstd_find_frame_compressed_size);
132  
zstd_get_frame_header(zstd_frame_header * header,const void * src,size_t src_size)133  size_t zstd_get_frame_header(zstd_frame_header *header, const void *src,
134  	size_t src_size)
135  {
136  	return ZSTD_getFrameHeader(header, src, src_size);
137  }
138  EXPORT_SYMBOL(zstd_get_frame_header);
139  
140  MODULE_LICENSE("Dual BSD/GPL");
141  MODULE_DESCRIPTION("Zstd Decompressor");
142