extzstd 0.0.2.CONCEPT-x86-mingw32 → 0.0.3.CONCEPT-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +72 -22
  3. data/Rakefile +73 -19
  4. data/contrib/zstd/README.md +68 -0
  5. data/contrib/zstd/common/bitstream.h +414 -0
  6. data/contrib/zstd/common/entropy_common.c +231 -0
  7. data/contrib/zstd/common/error_private.h +125 -0
  8. data/contrib/zstd/common/error_public.h +77 -0
  9. data/contrib/zstd/common/fse.h +628 -0
  10. data/contrib/zstd/common/fse_decompress.c +331 -0
  11. data/contrib/zstd/common/huf.h +228 -0
  12. data/contrib/zstd/common/mem.h +377 -0
  13. data/contrib/zstd/common/xxhash.c +854 -0
  14. data/contrib/zstd/common/xxhash.h +273 -0
  15. data/contrib/zstd/common/zbuff.h +197 -0
  16. data/contrib/zstd/common/zstd.h +475 -0
  17. data/contrib/zstd/common/zstd_common.c +91 -0
  18. data/contrib/zstd/common/zstd_internal.h +238 -0
  19. data/contrib/zstd/compress/fse_compress.c +807 -0
  20. data/contrib/zstd/compress/huf_compress.c +577 -0
  21. data/contrib/zstd/compress/zbuff_compress.c +327 -0
  22. data/contrib/zstd/compress/zstd_compress.c +3074 -0
  23. data/contrib/zstd/compress/zstd_opt.h +1046 -0
  24. data/contrib/zstd/decompress/huf_decompress.c +894 -0
  25. data/contrib/zstd/decompress/zbuff_decompress.c +294 -0
  26. data/contrib/zstd/decompress/zstd_decompress.c +1362 -0
  27. data/contrib/zstd/dictBuilder/divsufsort.c +1913 -0
  28. data/contrib/zstd/dictBuilder/divsufsort.h +67 -0
  29. data/contrib/zstd/dictBuilder/zdict.c +1045 -0
  30. data/contrib/zstd/dictBuilder/zdict.h +113 -0
  31. data/contrib/zstd/legacy/zstd_legacy.h +140 -0
  32. data/contrib/zstd/legacy/zstd_v01.c +2178 -0
  33. data/contrib/zstd/{zstd.h → legacy/zstd_v01.h} +46 -39
  34. data/contrib/zstd/legacy/zstd_v02.c +3748 -0
  35. data/contrib/zstd/legacy/zstd_v02.h +99 -0
  36. data/contrib/zstd/legacy/zstd_v03.c +3389 -0
  37. data/contrib/zstd/legacy/zstd_v03.h +99 -0
  38. data/contrib/zstd/legacy/zstd_v04.c +4056 -0
  39. data/contrib/zstd/legacy/zstd_v04.h +148 -0
  40. data/contrib/zstd/legacy/zstd_v05.c +4325 -0
  41. data/contrib/zstd/legacy/zstd_v05.h +171 -0
  42. data/contrib/zstd/legacy/zstd_v06.c +4581 -0
  43. data/contrib/zstd/legacy/zstd_v06.h +185 -0
  44. data/ext/extconf.rb +10 -12
  45. data/ext/extzstd.c +497 -144
  46. data/ext/extzstd.h +127 -22
  47. data/ext/extzstd_buffered.c +265 -0
  48. data/ext/extzstd_nogvls.h +174 -0
  49. data/ext/zstd_amalgam.c +18 -0
  50. data/ext/zstd_legacy_v01.c +1 -0
  51. data/ext/zstd_legacy_v02.c +1 -0
  52. data/ext/zstd_legacy_v03.c +1 -0
  53. data/ext/zstd_legacy_v04.c +1 -0
  54. data/ext/zstd_legacy_v05.c +1 -0
  55. data/ext/zstd_legacy_v06.c +1 -0
  56. data/gemstub.rb +17 -1
  57. data/lib/2.0/extzstd.so +0 -0
  58. data/lib/2.1/extzstd.so +0 -0
  59. data/lib/2.2/extzstd.so +0 -0
  60. data/lib/2.3/extzstd.so +0 -0
  61. data/lib/extzstd.rb +197 -77
  62. data/lib/extzstd/version.rb +1 -1
  63. data/test/test_basic.rb +41 -0
  64. metadata +71 -20
  65. data/contrib/zstd/Makefile +0 -115
  66. data/contrib/zstd/fse.c +0 -2466
  67. data/contrib/zstd/fse.h +0 -320
  68. data/contrib/zstd/fse_static.h +0 -282
  69. data/contrib/zstd/libzstd.pc.in +0 -14
  70. data/contrib/zstd/zstd.c +0 -1768
  71. data/contrib/zstd/zstd_static.h +0 -89
  72. data/ext/extzstd-stream.c +0 -398
@@ -0,0 +1,185 @@
1
+ /*
2
+ zstd_v06 - decoder for 0.6 format
3
+ Header File
4
+ Copyright (C) 2014-2016, Yann Collet.
5
+
6
+ BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
+
8
+ Redistribution and use in source and binary forms, with or without
9
+ modification, are permitted provided that the following conditions are
10
+ met:
11
+ * Redistributions of source code must retain the above copyright
12
+ notice, this list of conditions and the following disclaimer.
13
+ * Redistributions in binary form must reproduce the above
14
+ copyright notice, this list of conditions and the following disclaimer
15
+ in the documentation and/or other materials provided with the
16
+ distribution.
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ You can contact the author at :
30
+ - zstd source repository : https://github.com/Cyan4973/zstd
31
+ */
32
+ #ifndef ZSTDv06_H
33
+ #define ZSTDv06_H
34
+
35
+ #if defined (__cplusplus)
36
+ extern "C" {
37
+ #endif
38
+
39
+ /*-*************************************
40
+ * Dependencies
41
+ ***************************************/
42
+ #include <stddef.h> /* size_t */
43
+
44
+
45
+ /*-***************************************************************
46
+ * Export parameters
47
+ *****************************************************************/
48
+ /*!
49
+ * ZSTDv06_DLL_EXPORT :
50
+ * Enable exporting of functions when building a Windows DLL
51
+ */
52
+ #if defined(_WIN32) && defined(ZSTDv06_DLL_EXPORT) && (ZSTDv06_DLL_EXPORT==1)
53
+ # define ZSTDLIB_API __declspec(dllexport)
54
+ #else
55
+ # define ZSTDLIB_API
56
+ #endif
57
+
58
+
59
+ /* *************************************
60
+ * Simple functions
61
+ ***************************************/
62
+ /*! ZSTDv06_decompress() :
63
+ `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.
64
+ `dstCapacity` must be large enough, equal or larger than originalSize.
65
+ @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
66
+ or an errorCode if it fails (which can be tested using ZSTDv06_isError()) */
67
+ ZSTDLIB_API size_t ZSTDv06_decompress( void* dst, size_t dstCapacity,
68
+ const void* src, size_t compressedSize);
69
+
70
+
71
+ /* *************************************
72
+ * Helper functions
73
+ ***************************************/
74
+ ZSTDLIB_API size_t ZSTDv06_compressBound(size_t srcSize); /*!< maximum compressed size (worst case scenario) */
75
+
76
+ /* Error Management */
77
+ ZSTDLIB_API unsigned ZSTDv06_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
78
+ ZSTDLIB_API const char* ZSTDv06_getErrorName(size_t code); /*!< provides readable string for an error code */
79
+
80
+
81
+ /* *************************************
82
+ * Explicit memory management
83
+ ***************************************/
84
+ /** Decompression context */
85
+ typedef struct ZSTDv06_DCtx_s ZSTDv06_DCtx;
86
+ ZSTDLIB_API ZSTDv06_DCtx* ZSTDv06_createDCtx(void);
87
+ ZSTDLIB_API size_t ZSTDv06_freeDCtx(ZSTDv06_DCtx* dctx); /*!< @return : errorCode */
88
+
89
+ /** ZSTDv06_decompressDCtx() :
90
+ * Same as ZSTDv06_decompress(), but requires an already allocated ZSTDv06_DCtx (see ZSTDv06_createDCtx()) */
91
+ ZSTDLIB_API size_t ZSTDv06_decompressDCtx(ZSTDv06_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
92
+
93
+
94
+ /*-***********************
95
+ * Dictionary API
96
+ *************************/
97
+ /*! ZSTDv06_decompress_usingDict() :
98
+ * Decompression using a pre-defined Dictionary content (see dictBuilder).
99
+ * Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.
100
+ * Note : dict can be NULL, in which case, it's equivalent to ZSTDv06_decompressDCtx() */
101
+ ZSTDLIB_API size_t ZSTDv06_decompress_usingDict(ZSTDv06_DCtx* dctx,
102
+ void* dst, size_t dstCapacity,
103
+ const void* src, size_t srcSize,
104
+ const void* dict,size_t dictSize);
105
+
106
+
107
+ /*-************************
108
+ * Advanced Streaming API
109
+ ***************************/
110
+ struct ZSTDv06_frameParams_s { unsigned long long frameContentSize; unsigned windowLog; };
111
+ typedef struct ZSTDv06_frameParams_s ZSTDv06_frameParams;
112
+
113
+ ZSTDLIB_API size_t ZSTDv06_getFrameParams(ZSTDv06_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */
114
+ ZSTDLIB_API size_t ZSTDv06_decompressBegin_usingDict(ZSTDv06_DCtx* dctx, const void* dict, size_t dictSize);
115
+ ZSTDLIB_API void ZSTDv06_copyDCtx(ZSTDv06_DCtx* dctx, const ZSTDv06_DCtx* preparedDCtx);
116
+
117
+ ZSTDLIB_API size_t ZSTDv06_nextSrcSizeToDecompress(ZSTDv06_DCtx* dctx);
118
+ ZSTDLIB_API size_t ZSTDv06_decompressContinue(ZSTDv06_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
119
+
120
+
121
+
122
+ /* *************************************
123
+ * ZBUFF API
124
+ ***************************************/
125
+
126
+ typedef struct ZBUFFv06_DCtx_s ZBUFFv06_DCtx;
127
+ ZSTDLIB_API ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void);
128
+ ZSTDLIB_API size_t ZBUFFv06_freeDCtx(ZBUFFv06_DCtx* dctx);
129
+
130
+ ZSTDLIB_API size_t ZBUFFv06_decompressInit(ZBUFFv06_DCtx* dctx);
131
+ ZSTDLIB_API size_t ZBUFFv06_decompressInitDictionary(ZBUFFv06_DCtx* dctx, const void* dict, size_t dictSize);
132
+
133
+ ZSTDLIB_API size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* dctx,
134
+ void* dst, size_t* dstCapacityPtr,
135
+ const void* src, size_t* srcSizePtr);
136
+
137
+ /*-***************************************************************************
138
+ * Streaming decompression howto
139
+ *
140
+ * A ZBUFFv06_DCtx object is required to track streaming operations.
141
+ * Use ZBUFFv06_createDCtx() and ZBUFFv06_freeDCtx() to create/release resources.
142
+ * Use ZBUFFv06_decompressInit() to start a new decompression operation,
143
+ * or ZBUFFv06_decompressInitDictionary() if decompression requires a dictionary.
144
+ * Note that ZBUFFv06_DCtx objects can be re-init multiple times.
145
+ *
146
+ * Use ZBUFFv06_decompressContinue() repetitively to consume your input.
147
+ * *srcSizePtr and *dstCapacityPtr can be any size.
148
+ * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
149
+ * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
150
+ * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
151
+ * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
152
+ * or 0 when a frame is completely decoded,
153
+ * or an error code, which can be tested using ZBUFFv06_isError().
154
+ *
155
+ * Hint : recommended buffer sizes (not compulsory) : ZBUFFv06_recommendedDInSize() and ZBUFFv06_recommendedDOutSize()
156
+ * output : ZBUFFv06_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
157
+ * input : ZBUFFv06_recommendedDInSize == 128KB + 3;
158
+ * just follow indications from ZBUFFv06_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
159
+ * *******************************************************************************/
160
+
161
+
162
+ /* *************************************
163
+ * Tool functions
164
+ ***************************************/
165
+ ZSTDLIB_API unsigned ZBUFFv06_isError(size_t errorCode);
166
+ ZSTDLIB_API const char* ZBUFFv06_getErrorName(size_t errorCode);
167
+
168
+ /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
169
+ * These sizes are just hints, they tend to offer better latency */
170
+ ZSTDLIB_API size_t ZBUFFv06_recommendedDInSize(void);
171
+ ZSTDLIB_API size_t ZBUFFv06_recommendedDOutSize(void);
172
+
173
+
174
+ /*-*************************************
175
+ * Constants
176
+ ***************************************/
177
+ #define ZSTDv06_MAGICNUMBER 0xFD2FB526 /* v0.6 */
178
+
179
+
180
+
181
+ #if defined (__cplusplus)
182
+ }
183
+ #endif
184
+
185
+ #endif /* ZSTDv06_BUFFERED_H */
@@ -1,22 +1,20 @@
1
1
  #!ruby
2
- #vim: set fileencoding:utf-8
3
2
 
4
3
  require "mkmf"
5
4
 
6
- dir = File.dirname(__FILE__).gsub(/[\[\{\?\*]/, "[\\0]")
7
- filepattern = "{.,../contrib/zstd}/*.c"
8
- target = File.join(dir, filepattern)
9
- files = Dir.glob(target).map { |n| File.basename n }
10
- ## reject fse.c, because it's included in zstd.c
11
- files.reject! { |n| "/contrib/zstd/fse.c".include?(n) }
12
- $srcs = files
5
+ find_header "zstd.h", "$(srcdir)/../contrib/zstd/common" or abort "can't find ``zstd.h''"
6
+ find_header "zdict.h", "$(srcdir)/../contrib/zstd/dictBuilder" or abort "can't find ``zdict.h''"
7
+ find_header "zstd_legacy.h", "$(srcdir)/../contrib/zstd/legacy" or abort "can't find ``zstd_legacy.h''"
13
8
 
14
- $VPATH.push "$(srcdir)/../contrib/zstd"
15
-
16
- find_header "zstd.h", "$(srcdir)/../contrib/zstd" or abort 1
9
+ #dir = File.dirname(__FILE__).gsub(/[\[\{\?\*]/, "[\\0]")
10
+ #filepattern = "{.,../contrib/zstd}/**/*.c"
11
+ #target = File.join(dir, filepattern)
12
+ #files = Dir.glob(target).sort.map { |n| File.basename n }
13
+ #$srcs = files
14
+ #$VPATH.push "$(srcdir)/../contrib/zstd", "$(srcdir)/../contrib/zstd/legacy"
17
15
 
18
16
  if RbConfig::CONFIG["arch"] =~ /mingw/
19
17
  $LDFLAGS << " -static-libgcc"
20
18
  end
21
19
 
22
- create_makefile("extzstd")
20
+ create_makefile File.join(RUBY_VERSION.slice(/\d+\.\d+/), "extzstd")
@@ -1,193 +1,546 @@
1
1
  #include "extzstd.h"
2
+ #include <mem.h>
3
+ #include <error_public.h>
4
+ #include <zdict.h>
2
5
 
3
- VALUE mZstd;
4
- VALUE eError;
6
+ VALUE extzstd_mZstd;
5
7
 
6
- static size_t
7
- aux_ZSTD_compress_nogvl(va_list *vp)
8
+ /*
9
+ * constant Zstd::LIBRARY_VERSION
10
+ */
11
+
12
+ static VALUE
13
+ libver_s_to_i(VALUE ver)
8
14
  {
9
- char *dest = va_arg(*vp, char *);
10
- size_t destsize = va_arg(*vp, size_t);
11
- const char *src = va_arg(*vp, const char *);
12
- size_t srcsize = va_arg(*vp, size_t);
13
- return ZSTD_compress(dest, destsize, src, srcsize);
15
+ return UINT2NUM(ZSTD_VERSION_NUMBER);
14
16
  }
15
17
 
16
- static inline size_t
17
- aux_ZSTD_compress(char *dest, size_t destsize, const char *src, size_t srcsize)
18
+ static VALUE
19
+ libver_s_to_s(VALUE ver)
18
20
  {
19
- return (size_t)aux_thread_call_without_gvl(
20
- (void *(*)(void *))aux_ZSTD_compress_nogvl, NULL,
21
- dest, destsize, src, srcsize);
21
+ static VALUE str;
22
+ if (!str) {
23
+ str = rb_sprintf("%d.%d.%d",
24
+ ZSTD_VERSION_MAJOR,
25
+ ZSTD_VERSION_MINOR,
26
+ ZSTD_VERSION_RELEASE);
27
+ rb_obj_freeze(str);
28
+ }
29
+ return str;
22
30
  }
23
31
 
24
- static inline void
25
- zstd_s_encode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsize)
32
+ static void
33
+ init_libver(void)
26
34
  {
27
- switch (argc) {
28
- case 1:
29
- *src = argv[0];
30
- rb_check_type(*src, RUBY_T_STRING);
31
- *maxsize = ZSTD_compressBound(RSTRING_LEN(*src));
32
- *dest = rb_str_buf_new(*maxsize);
33
- return;
34
- case 2:
35
- *src = argv[0];
36
- rb_check_type(*src, RUBY_T_STRING);
37
- *dest = argv[1];
38
- if (rb_type_p(*dest, RUBY_T_STRING)) {
39
- *maxsize = ZSTD_compressBound(RSTRING_LEN(*src));
40
- rb_str_resize(*dest, *maxsize);
41
- } else {
42
- *maxsize = NUM2SIZET(*dest);
43
- *dest = rb_str_buf_new(*maxsize);
44
- }
45
- return;
46
- case 3:
47
- *src = argv[0];
48
- rb_check_type(*src, RUBY_T_STRING);
49
- *maxsize = NUM2SIZET(argv[1]);
50
- *dest = argv[2];
51
- rb_check_type(*dest, RUBY_T_STRING);
52
- rb_str_resize(*dest, *maxsize);
53
- return;
35
+ VALUE libver = AUX_TUPLE(
36
+ INT2FIX(ZSTD_VERSION_MAJOR),
37
+ INT2FIX(ZSTD_VERSION_MINOR),
38
+ INT2FIX(ZSTD_VERSION_RELEASE));
39
+ rb_define_singleton_method(libver, "to_i", RUBY_METHOD_FUNC(libver_s_to_i), 0);
40
+ rb_define_singleton_method(libver, "to_s", RUBY_METHOD_FUNC(libver_s_to_s), 0);
41
+ rb_define_singleton_method(libver, "to_str", RUBY_METHOD_FUNC(libver_s_to_s), 0);
42
+ rb_obj_freeze(libver);
43
+ rb_define_const(extzstd_mZstd, "LIBRARY_VERSION", libver);
44
+ }
45
+
46
+ /*
47
+ * error classes
48
+ */
49
+
50
+ /*
51
+ * Document-class: Zstd::Exceptions
52
+ *
53
+ * Catch extzstd errors for rescue statment.
54
+ */
55
+ VALUE extzstd_mExceptions;
56
+
57
+ VALUE extzstd_eError;
58
+ VALUE extzstd_eError;
59
+ VALUE extzstd_eGenericError;
60
+ VALUE extzstd_ePrefixUnknownError;
61
+ VALUE extzstd_eFrameParameterUnsupportedError;
62
+ VALUE extzstd_eFrameParameterUnsupportedBy32bitsError;
63
+ VALUE extzstd_eCompressionParameterUnsupportedError;
64
+ VALUE extzstd_eInitMissingError;
65
+ VALUE extzstd_eMemoryAllocationError;
66
+ VALUE extzstd_eStageWrongError;
67
+ VALUE extzstd_eDstSizeTooSmallError;
68
+ VALUE extzstd_eSrcSizeWrongError;
69
+ VALUE extzstd_eCorruptionDetectedError;
70
+ VALUE extzstd_eChecksumWrongError;
71
+ VALUE extzstd_eTableLogTooLargeError;
72
+ VALUE extzstd_eMaxSymbolValueTooLargeError;
73
+ VALUE extzstd_eMaxSymbolValueTooSmallError;
74
+ VALUE extzstd_eDictionaryCorruptedError;
75
+ VALUE extzstd_eDictionaryWrongError;
76
+
77
+
78
+ void
79
+ extzstd_check_error(ssize_t errcode)
80
+ {
81
+ if (ZSTD_isError(errcode)) {
82
+ extzstd_error(errcode);
83
+ }
84
+ }
85
+
86
+ void
87
+ extzstd_error(ssize_t errcode)
88
+ {
89
+ rb_exc_raise(extzstd_make_error(errcode));
90
+ }
91
+
92
+ VALUE
93
+ extzstd_make_errorf(VALUE exc, const char *fmt, ...)
94
+ {
95
+ if (fmt && strlen(fmt) > 0) {
96
+ va_list va;
97
+ va_start(va, fmt);
98
+ VALUE mesg = rb_vsprintf(fmt, va);
99
+ va_end(va);
100
+ return rb_exc_new3(exc, mesg);
101
+ } else {
102
+ return rb_exc_new2(exc, "");
103
+ }
104
+ }
105
+
106
+ VALUE
107
+ extzstd_make_error(ssize_t errcode)
108
+ {
109
+ if (!ZSTD_isError(errcode)) { return Qnil; }
110
+
111
+ #define CASE_ERROR(zstderr, extzstderr, ...) \
112
+ case zstderr: return extzstd_make_errorf(extzstderr, __VA_ARGS__); \
113
+
114
+ switch (-errcode) {
115
+ CASE_ERROR(ZSTD_error_GENERIC, extzstd_eGenericError, NULL);
116
+ CASE_ERROR(ZSTD_error_prefix_unknown, extzstd_ePrefixUnknownError, NULL);
117
+ CASE_ERROR(ZSTD_error_frameParameter_unsupported, extzstd_eFrameParameterUnsupportedError, NULL);
118
+ CASE_ERROR(ZSTD_error_frameParameter_unsupportedBy32bits, extzstd_eFrameParameterUnsupportedBy32bitsError, NULL);
119
+ CASE_ERROR(ZSTD_error_compressionParameter_unsupported, extzstd_eCompressionParameterUnsupportedError, NULL);
120
+ CASE_ERROR(ZSTD_error_init_missing, extzstd_eInitMissingError, NULL);
121
+ CASE_ERROR(ZSTD_error_memory_allocation, extzstd_eMemoryAllocationError, NULL);
122
+ CASE_ERROR(ZSTD_error_stage_wrong, extzstd_eStageWrongError, NULL);
123
+ CASE_ERROR(ZSTD_error_dstSize_tooSmall, extzstd_eDstSizeTooSmallError, NULL);
124
+ CASE_ERROR(ZSTD_error_srcSize_wrong, extzstd_eSrcSizeWrongError, NULL);
125
+ CASE_ERROR(ZSTD_error_corruption_detected, extzstd_eCorruptionDetectedError, NULL);
126
+ CASE_ERROR(ZSTD_error_checksum_wrong, extzstd_eChecksumWrongError, NULL);
127
+ CASE_ERROR(ZSTD_error_tableLog_tooLarge, extzstd_eTableLogTooLargeError, NULL);
128
+ CASE_ERROR(ZSTD_error_maxSymbolValue_tooLarge, extzstd_eMaxSymbolValueTooLargeError, NULL);
129
+ CASE_ERROR(ZSTD_error_maxSymbolValue_tooSmall, extzstd_eMaxSymbolValueTooSmallError, NULL);
130
+ CASE_ERROR(ZSTD_error_dictionary_corrupted, extzstd_eDictionaryCorruptedError, NULL);
131
+ CASE_ERROR(ZSTD_error_dictionary_wrong, extzstd_eDictionaryWrongError, NULL);
54
132
  default:
55
- rb_error_arity(argc, 1, 3);
133
+ return extzstd_make_errorf(extzstd_eError,
134
+ "unknown zstd error code (%d)", errcode);
56
135
  }
136
+
137
+ #undef CASE_ERROR
138
+ }
139
+
140
+ static void
141
+ init_error(void)
142
+ {
143
+ extzstd_mExceptions = rb_define_module_under(extzstd_mZstd, "Exceptions");
144
+
145
+ extzstd_eError = rb_define_class_under(extzstd_mZstd, "Error", rb_eRuntimeError);
146
+ rb_include_module(extzstd_eError, extzstd_mExceptions);
147
+
148
+ extzstd_eGenericError = rb_define_class_under(extzstd_mZstd, "GenericError", rb_eRuntimeError);
149
+ rb_include_module(extzstd_eGenericError, extzstd_mExceptions);
150
+
151
+ extzstd_ePrefixUnknownError = rb_define_class_under(extzstd_mZstd, "PrefixUnknownError", rb_eArgError);
152
+ rb_include_module(extzstd_ePrefixUnknownError, extzstd_mExceptions);
153
+
154
+ extzstd_eFrameParameterUnsupportedError = rb_define_class_under(extzstd_mZstd, "FrameParameterUnsupportedError", rb_eRuntimeError);
155
+ rb_include_module(extzstd_eFrameParameterUnsupportedError, extzstd_mExceptions);
156
+
157
+ extzstd_eFrameParameterUnsupportedBy32bitsError = rb_define_class_under(extzstd_mZstd, "FrameParameterUnsupportedBy32bitsImplementationError", rb_eRuntimeError);
158
+ rb_include_module(extzstd_eFrameParameterUnsupportedBy32bitsError, extzstd_mExceptions);
159
+
160
+ extzstd_eCompressionParameterUnsupportedError = rb_define_class_under(extzstd_mZstd, "CompressionParameterUnsupportedError", rb_eRuntimeError);
161
+ rb_include_module(extzstd_eCompressionParameterUnsupportedError, extzstd_mExceptions);
162
+
163
+ extzstd_eInitMissingError = rb_define_class_under(extzstd_mZstd, "InitMissingError", rb_eRuntimeError);
164
+ rb_include_module(extzstd_eInitMissingError, extzstd_mExceptions);
165
+
166
+ extzstd_eMemoryAllocationError = rb_define_class_under(extzstd_mZstd, "MemoryAllocationError", aux_const_dig_str(rb_cObject, "Errno", "ENOMEM"));
167
+ rb_include_module(extzstd_eMemoryAllocationError, extzstd_mExceptions);
168
+
169
+ extzstd_eStageWrongError = rb_define_class_under(extzstd_mZstd, "StageWrongError", rb_eRuntimeError);
170
+ rb_include_module(extzstd_eStageWrongError, extzstd_mExceptions);
171
+
172
+ extzstd_eDstSizeTooSmallError = rb_define_class_under(extzstd_mZstd, "DstSizeTooSmallError", rb_eArgError);
173
+ rb_include_module(extzstd_eDstSizeTooSmallError, extzstd_mExceptions);
174
+
175
+ extzstd_eSrcSizeWrongError = rb_define_class_under(extzstd_mZstd, "SrcSizeWrongError", rb_eArgError);
176
+ rb_include_module(extzstd_eSrcSizeWrongError, extzstd_mExceptions);
177
+
178
+ extzstd_eCorruptionDetectedError = rb_define_class_under(extzstd_mZstd, "CorruptionDetectedError", rb_eRuntimeError);
179
+ rb_include_module(extzstd_eCorruptionDetectedError, extzstd_mExceptions);
180
+
181
+ extzstd_eChecksumWrongError = rb_define_class_under(extzstd_mZstd, "ChecksumWrongError", rb_eRuntimeError);
182
+ rb_include_module(extzstd_eChecksumWrongError, extzstd_mExceptions);
183
+
184
+ extzstd_eTableLogTooLargeError = rb_define_class_under(extzstd_mZstd, "TableLogTooLargeError", rb_eArgError);
185
+ rb_include_module(extzstd_eTableLogTooLargeError, extzstd_mExceptions);
186
+
187
+ extzstd_eMaxSymbolValueTooLargeError = rb_define_class_under(extzstd_mZstd, "MaxSymbolValueTooLargeError", rb_eArgError);
188
+ rb_include_module(extzstd_eMaxSymbolValueTooLargeError, extzstd_mExceptions);
189
+
190
+ extzstd_eMaxSymbolValueTooSmallError = rb_define_class_under(extzstd_mZstd, "MaxSymbolValueTooSmallError", rb_eArgError);
191
+ rb_include_module(extzstd_eMaxSymbolValueTooSmallError, extzstd_mExceptions);
192
+
193
+ extzstd_eDictionaryCorruptedError = rb_define_class_under(extzstd_mZstd, "DictionaryCorruptedError", rb_eRuntimeError);
194
+ rb_include_module(extzstd_eDictionaryCorruptedError, extzstd_mExceptions);
195
+
196
+ extzstd_eDictionaryWrongError = rb_define_class_under(extzstd_mZstd, "DictionaryWrongError", rb_eRuntimeError);
197
+ rb_include_module(extzstd_eDictionaryWrongError, extzstd_mExceptions);
198
+ }
199
+
200
+ /*
201
+ * module Zstd::Constants
202
+ */
203
+
204
+ static void
205
+ init_constants(void)
206
+ {
207
+ VALUE mConstants = rb_define_module_under(extzstd_mZstd, "Constants");
208
+ rb_include_module(extzstd_mZstd, mConstants);
209
+
210
+ rb_define_const(mConstants, "ZSTD_FAST", INT2NUM(ZSTD_fast));
211
+ rb_define_const(mConstants, "ZSTD_DFAST", INT2NUM(ZSTD_dfast));
212
+ rb_define_const(mConstants, "ZSTD_GREEDY", INT2NUM(ZSTD_greedy));
213
+ rb_define_const(mConstants, "ZSTD_LAZY", INT2NUM(ZSTD_lazy));
214
+ rb_define_const(mConstants, "ZSTD_LAZY2", INT2NUM(ZSTD_lazy2));
215
+ rb_define_const(mConstants, "ZSTD_BTLAZY2", INT2NUM(ZSTD_btlazy2));
216
+ rb_define_const(mConstants, "ZSTD_WINDOWLOG_MAX", INT2NUM(ZSTD_WINDOWLOG_MAX));
217
+ rb_define_const(mConstants, "ZSTD_WINDOWLOG_MIN", INT2NUM(ZSTD_WINDOWLOG_MIN));
218
+ rb_define_const(mConstants, "ZSTD_CHAINLOG_MAX", INT2NUM(ZSTD_CHAINLOG_MAX));
219
+ rb_define_const(mConstants, "ZSTD_CHAINLOG_MIN", INT2NUM(ZSTD_CHAINLOG_MIN));
220
+ rb_define_const(mConstants, "ZSTD_HASHLOG_MAX", INT2NUM(ZSTD_HASHLOG_MAX));
221
+ rb_define_const(mConstants, "ZSTD_HASHLOG_MIN", INT2NUM(ZSTD_HASHLOG_MIN));
222
+ rb_define_const(mConstants, "ZSTD_SEARCHLOG_MAX", INT2NUM(ZSTD_SEARCHLOG_MAX));
223
+ rb_define_const(mConstants, "ZSTD_SEARCHLOG_MIN", INT2NUM(ZSTD_SEARCHLOG_MIN));
224
+ rb_define_const(mConstants, "ZSTD_SEARCHLENGTH_MAX", INT2NUM(ZSTD_SEARCHLENGTH_MAX));
225
+ rb_define_const(mConstants, "ZSTD_SEARCHLENGTH_MIN", INT2NUM(ZSTD_SEARCHLENGTH_MIN));
226
+
227
+ rb_define_const(mConstants, "FAST", INT2NUM(ZSTD_fast));
228
+ rb_define_const(mConstants, "DFAST", INT2NUM(ZSTD_dfast));
229
+ rb_define_const(mConstants, "GREEDY", INT2NUM(ZSTD_greedy));
230
+ rb_define_const(mConstants, "LAZY", INT2NUM(ZSTD_lazy));
231
+ rb_define_const(mConstants, "LAZY2", INT2NUM(ZSTD_lazy2));
232
+ rb_define_const(mConstants, "BTLAZY2", INT2NUM(ZSTD_btlazy2));
233
+ rb_define_const(mConstants, "WINDOWLOG_MAX", INT2NUM(ZSTD_WINDOWLOG_MAX));
234
+ rb_define_const(mConstants, "WINDOWLOG_MIN", INT2NUM(ZSTD_WINDOWLOG_MIN));
235
+ rb_define_const(mConstants, "CHAINLOG_MAX", INT2NUM(ZSTD_CHAINLOG_MAX));
236
+ rb_define_const(mConstants, "CHAINLOG_MIN", INT2NUM(ZSTD_CHAINLOG_MIN));
237
+ rb_define_const(mConstants, "HASHLOG_MAX", INT2NUM(ZSTD_HASHLOG_MAX));
238
+ rb_define_const(mConstants, "HASHLOG_MIN", INT2NUM(ZSTD_HASHLOG_MIN));
239
+ rb_define_const(mConstants, "SEARCHLOG_MAX", INT2NUM(ZSTD_SEARCHLOG_MAX));
240
+ rb_define_const(mConstants, "SEARCHLOG_MIN", INT2NUM(ZSTD_SEARCHLOG_MIN));
241
+ rb_define_const(mConstants, "SEARCHLENGTH_MAX", INT2NUM(ZSTD_SEARCHLENGTH_MAX));
242
+ rb_define_const(mConstants, "SEARCHLENGTH_MIN", INT2NUM(ZSTD_SEARCHLENGTH_MIN));
243
+ }
244
+
245
+ /*
246
+ * class Zstd::EncodeParameter
247
+ */
248
+
249
+ VALUE extzstd_cParams;
250
+
251
+ AUX_IMPLEMENT_CONTEXT(
252
+ ZSTD_parameters, encparams_type, "extzstd.EncodeParameters",
253
+ encparams_alloc_dummy, NULL, free, NULL,
254
+ getencparamsp, getencparams, encparams_p);
255
+
256
+ ZSTD_parameters *
257
+ extzstd_getencparams(VALUE v)
258
+ {
259
+ return getencparams(v);
260
+ }
261
+
262
+ int
263
+ extzstd_encparams_p(VALUE v)
264
+ {
265
+ return encparams_p(v);
266
+ }
267
+
268
+ static VALUE
269
+ encparams_alloc(VALUE mod)
270
+ {
271
+ ZSTD_parameters *p;
272
+ return TypedData_Make_Struct(mod, ZSTD_parameters, &encparams_type, p);
273
+ }
274
+
275
+ VALUE
276
+ extzstd_params_alloc(ZSTD_parameters **p)
277
+ {
278
+ return TypedData_Make_Struct(extzstd_cParams, ZSTD_parameters, &encparams_type, *p);
57
279
  }
58
280
 
59
281
  /*
60
282
  * call-seq:
61
- * encode(src) -> encoded string
62
- * encode(src, size) -> encoded string
63
- * encode(src, dest) -> dest with encoded string
64
- * encode(src, size, dest) -> dest with encoded string
283
+ * initialize(preset_level = 1, srcsize_hint = 0, dictsize = 0, opts = {})
284
+ *
285
+ * Initialize struct ZSTD_parameters of C layer.
286
+ *
287
+ * [preset_level = 1]
288
+ * [srcsize_hint = 0]
289
+ * [opts windowlog: nil]
290
+ * [opts contentlog: nil]
291
+ * [opts hashlog: nil]
292
+ * [opts searchlog: nil]
293
+ * [opts searchlength: nil]
294
+ * [opts strategy: nil]
65
295
  */
66
296
  static VALUE
67
- zstd_s_encode(int argc, VALUE argv[], VALUE mod)
68
- {
69
- VALUE src, dest;
70
- size_t maxsize;
71
- zstd_s_encode_args(argc, argv, &src, &dest, &maxsize);
72
- const char *srcp;
73
- size_t srcsize;
74
- RSTRING_GETMEM(src, srcp, srcsize);
75
- rb_obj_infect(dest, src);
76
- size_t s = aux_ZSTD_compress(RSTRING_PTR(dest), maxsize, srcp, srcsize);
77
- if (ZSTD_isError(s)) {
78
- rb_raise(eError,
79
- "failed ZSTD_compress - %s(%d) in %s:%d:%s",
80
- ZSTD_getErrorName(s), (int)s,
81
- __FILE__, __LINE__, __func__);
297
+ encparams_init(int argc, VALUE argv[], VALUE v)
298
+ {
299
+ ZSTD_parameters *p = getencparams(v);
300
+ uint64_t sizehint;
301
+ size_t dictsize;
302
+ int level;
303
+ VALUE opts = Qnil;
304
+ int argc0 = argc;
305
+ if (argc > 0) {
306
+ if (rb_type_p(argv[argc - 1], RUBY_T_HASH)) {
307
+ opts = argv[argc - 1];
308
+ argc --;
309
+ }
82
310
  }
83
- if (s > maxsize) {
84
- rb_bug("%s:%d:%s: detect buffer overflow in ZSTD_compress - maxsize is %zd, but returned size is %zd",
85
- __FILE__, __LINE__, __func__, maxsize, s);
311
+ if (argc == 0) {
312
+ level = 0;
313
+ sizehint = 0;
314
+ dictsize = 0;
315
+ } else if (argc == 1) {
316
+ level = aux_num2int(argv[0], 0);
317
+ sizehint = 0;
318
+ dictsize = 0;
319
+ } else if (argc == 2) {
320
+ level = aux_num2int(argv[0], 0);
321
+ sizehint = aux_num2int_u64(argv[1], 0);
322
+ dictsize = 0;
323
+ } else if (argc == 3) {
324
+ level = aux_num2int(argv[0], 0);
325
+ sizehint = aux_num2int_u64(argv[1], 0);
326
+ dictsize = aux_num2int_u64(argv[2], 0);
327
+ } else {
328
+ rb_raise(rb_eArgError, "wrong number of argument (%d for 0..3 with keywords)", argc0);
86
329
  }
87
- rb_str_set_len(dest, s);
88
- return dest;
89
- }
90
330
 
91
- static size_t
92
- aux_ZSTD_decompress_nogvl(va_list *vp)
93
- {
94
- char *dest = va_arg(*vp, char *);
95
- size_t destsize = va_arg(*vp, size_t);
96
- const char *src = va_arg(*vp, const char *);
97
- size_t srcsize = va_arg(*vp, size_t);
98
- return ZSTD_decompress(dest, destsize, src, srcsize);
331
+ *p = ZSTD_getParams(level, sizehint, dictsize);
332
+
333
+ if (!NIL_P(opts)) {
334
+ #define SETUP_PARAM(var, opts, key, converter) \
335
+ do { \
336
+ VALUE tmp = rb_hash_lookup(opts, ID2SYM(rb_intern(key))); \
337
+ if (!NIL_P(tmp)) { \
338
+ var = converter(tmp); \
339
+ } \
340
+ } while (0) \
341
+
342
+ SETUP_PARAM(p->cParams.windowLog, opts, "windowlog", NUM2UINT);
343
+ SETUP_PARAM(p->cParams.chainLog, opts, "chainlog", NUM2UINT);
344
+ SETUP_PARAM(p->cParams.hashLog, opts, "hashlog", NUM2UINT);
345
+ SETUP_PARAM(p->cParams.searchLog, opts, "searchlog", NUM2UINT);
346
+ SETUP_PARAM(p->cParams.searchLength, opts, "searchlength", NUM2UINT);
347
+ SETUP_PARAM(p->cParams.strategy, opts, "strategy", NUM2UINT);
348
+ #undef SETUP_PARAM
349
+ }
350
+
351
+ return v;
99
352
  }
100
353
 
101
- static inline size_t
102
- aux_ZSTD_decompress(char *dest, size_t destsize, const char *src, size_t srcsize)
354
+ static VALUE
355
+ encparams_init_copy(VALUE params, VALUE src)
103
356
  {
104
- return (size_t)aux_thread_call_without_gvl(
105
- (void *(*)(void *))aux_ZSTD_decompress_nogvl, NULL,
106
- dest, destsize, src, srcsize);
357
+ ZSTD_parameters *a = getencparams(params);
358
+ ZSTD_parameters *b = getencparams(src);
359
+ rb_check_frozen(params);
360
+ rb_obj_infect(params, src);
361
+ memcpy(a, b, sizeof(*a));
362
+ return params;
107
363
  }
108
364
 
109
- static inline void
110
- zstd_s_decode_args(int argc, VALUE argv[], VALUE *src, VALUE *dest, size_t *maxsize)
365
+ //static VALUE
366
+ //encparams_validate(VALUE v)
367
+ //{
368
+ // ZSTD_validateParams(getencparams(v));
369
+ // return v;
370
+ //}
371
+
372
+ #define IMP_PARAMS(GETTER, SETTER, FIELD) \
373
+ static VALUE \
374
+ GETTER(VALUE v) \
375
+ { \
376
+ return UINT2NUM(getencparams(v)->cParams.FIELD); \
377
+ } \
378
+ \
379
+ static VALUE \
380
+ SETTER(VALUE v, VALUE n) \
381
+ { \
382
+ getencparams(v)->cParams.FIELD = NUM2UINT(n); \
383
+ return n; \
384
+ } \
385
+
386
+ //IMP_PARAMS(encparams_srcsize, encparams_set_srcsize, srcSize);
387
+ IMP_PARAMS(encparams_windowlog, encparams_set_windowlog, windowLog);
388
+ IMP_PARAMS(encparams_chainlog, encparams_set_chainlog, chainLog);
389
+ IMP_PARAMS(encparams_hashlog, encparams_set_hashlog, hashLog);
390
+ IMP_PARAMS(encparams_searchlog, encparams_set_searchlog, searchLog);
391
+ IMP_PARAMS(encparams_searchlength, encparams_set_searchlength, searchLength);
392
+ IMP_PARAMS(encparams_strategy, encparams_set_strategy, strategy);
393
+
394
+ #undef IMP_PARAMS
395
+
396
+ static VALUE
397
+ encparams_s_get_preset(int argc, VALUE argv[], VALUE mod)
111
398
  {
399
+ int level;
400
+ uint64_t sizehint;
401
+ size_t dictsize;
402
+
112
403
  switch (argc) {
404
+ case 0:
405
+ level = 0;
406
+ sizehint = 0;
407
+ dictsize = 0;
408
+ break;
409
+ case 1:
410
+ level = NUM2INT(argv[0]);
411
+ sizehint = 0;
412
+ dictsize = 0;
413
+ break;
113
414
  case 2:
114
- *src = argv[0];
115
- rb_check_type(*src, RUBY_T_STRING);
116
- *maxsize = NUM2SIZET(argv[1]);
117
- *dest = rb_str_buf_new(*maxsize);
118
- return;
415
+ level = NUM2INT(argv[0]);
416
+ sizehint = NUM2ULL(argv[1]);
417
+ dictsize = 0;
418
+ break;
119
419
  case 3:
120
- *src = argv[0];
121
- rb_check_type(*src, RUBY_T_STRING);
122
- *maxsize = NUM2SIZET(argv[1]);
123
- *dest = argv[2];
124
- rb_check_type(*dest, RUBY_T_STRING);
125
- rb_str_resize(*dest, *maxsize);
126
- return;
420
+ level = NUM2INT(argv[0]);
421
+ sizehint = NUM2ULL(argv[1]);
422
+ dictsize = NUM2SIZET(argv[2]);
423
+ break;
127
424
  default:
128
- rb_error_arity(argc, 2, 3);
425
+ rb_error_arity(argc, 0, 3);
129
426
  }
427
+
428
+ ZSTD_parameters *p;
429
+ VALUE v = TypedData_Make_Struct(mod, ZSTD_parameters, &encparams_type, p);
430
+ *p = ZSTD_getParams(level, sizehint, dictsize);
431
+ return v;
432
+ }
433
+
434
+ /*
435
+ * Document-method: Zstd::EncodeParameters#windowlog
436
+ * Document-method: Zstd::EncodeParameters#windowlog=
437
+ * Document-method: Zstd::EncodeParameters#chainlog
438
+ * Document-method: Zstd::EncodeParameters#chainlog=
439
+ * Document-method: Zstd::EncodeParameters#hashlog
440
+ * Document-method: Zstd::EncodeParameters#hashlog=
441
+ * Document-method: Zstd::EncodeParameters#searchlog
442
+ * Document-method: Zstd::EncodeParameters#searchlog=
443
+ * Document-method: Zstd::EncodeParameters#searchlength
444
+ * Document-method: Zstd::EncodeParameters#searchlength=
445
+ * Document-method: Zstd::EncodeParameters#strategy
446
+ * Document-method: Zstd::EncodeParameters#strategy=
447
+ *
448
+ * Get/Set any field from/to struct ZSTD_parameters of C layer.
449
+ */
450
+
451
+ static void
452
+ init_encparams(void)
453
+ {
454
+ extzstd_cParams = rb_define_class_under(extzstd_mZstd, "EncodeParameters", rb_cObject);
455
+ rb_define_alloc_func(extzstd_cParams, encparams_alloc);
456
+ rb_define_method(extzstd_cParams, "initialize", RUBY_METHOD_FUNC(encparams_init), -1);
457
+ rb_define_method(extzstd_cParams, "initialize_copy", RUBY_METHOD_FUNC(encparams_init_copy), 1);
458
+ //rb_define_method(extzstd_cParams, "validate", RUBY_METHOD_FUNC(encparams_validate), 0);
459
+ //rb_define_method(extzstd_cParams, "srcsize", RUBY_METHOD_FUNC(encparams_srcsize), 0);
460
+ //rb_define_method(extzstd_cParams, "srcsize=", RUBY_METHOD_FUNC(encparams_set_srcsize), 1);
461
+ rb_define_method(extzstd_cParams, "windowlog", RUBY_METHOD_FUNC(encparams_windowlog), 0);
462
+ rb_define_method(extzstd_cParams, "windowlog=", RUBY_METHOD_FUNC(encparams_set_windowlog), 1);
463
+ rb_define_method(extzstd_cParams, "chainlog", RUBY_METHOD_FUNC(encparams_chainlog), 0);
464
+ rb_define_method(extzstd_cParams, "chainlog=", RUBY_METHOD_FUNC(encparams_set_chainlog), 1);
465
+ rb_define_method(extzstd_cParams, "hashlog", RUBY_METHOD_FUNC(encparams_hashlog), 0);
466
+ rb_define_method(extzstd_cParams, "hashlog=", RUBY_METHOD_FUNC(encparams_set_hashlog), 1);
467
+ rb_define_method(extzstd_cParams, "searchlog", RUBY_METHOD_FUNC(encparams_searchlog), 0);
468
+ rb_define_method(extzstd_cParams, "searchlog=", RUBY_METHOD_FUNC(encparams_set_searchlog), 1);
469
+ rb_define_method(extzstd_cParams, "searchlength", RUBY_METHOD_FUNC(encparams_searchlength), 0);
470
+ rb_define_method(extzstd_cParams, "searchlength=", RUBY_METHOD_FUNC(encparams_set_searchlength), 1);
471
+ rb_define_method(extzstd_cParams, "strategy", RUBY_METHOD_FUNC(encparams_strategy), 0);
472
+ rb_define_method(extzstd_cParams, "strategy=", RUBY_METHOD_FUNC(encparams_set_strategy), 1);
473
+
474
+ rb_define_singleton_method(extzstd_cParams, "preset", RUBY_METHOD_FUNC(encparams_s_get_preset), -1);
475
+ rb_define_alias(rb_singleton_class(extzstd_cParams), "[]", "preset");
130
476
  }
131
477
 
478
+
479
+ /*
480
+ * zstd dictionary utilities
481
+ */
482
+
132
483
  /*
133
484
  * call-seq:
134
- * decode(src, size) -> decoded string
135
- * decode(src, size, dest) -> dest with decoded string
485
+ * dict_train_from_buffer(src, dict_capacity) -> dictionary'd string
136
486
  */
137
487
  static VALUE
138
- zstd_s_decode(int argc, VALUE argv[], VALUE mod)
139
- {
140
- VALUE src, dest;
141
- size_t maxsize;
142
- zstd_s_decode_args(argc, argv, &src, &dest, &maxsize);
143
- const char *srcp;
144
- size_t srcsize;
145
- RSTRING_GETMEM(src, srcp, srcsize);
146
- rb_obj_infect(dest, src);
147
- size_t s = aux_ZSTD_decompress(RSTRING_PTR(dest), maxsize, srcp, srcsize);
148
- if (ZSTD_isError(s)) {
149
- rb_raise(eError,
150
- "failed ZSTD_decompress - %s(%d) in %s:%d:%s",
151
- ZSTD_getErrorName(s), (int)s,
152
- __FILE__, __LINE__, __func__);
153
- }
154
- if (s > maxsize) {
155
- rb_bug("%s:%d:%s: detect buffer overflow in ZSTD_compress - maxsize is %zd, but returned size is %zd",
156
- __FILE__, __LINE__, __func__, maxsize, s);
157
- }
158
- rb_str_set_len(dest, s);
159
- return dest;
488
+ ext_s_dict_train_from_buffer(VALUE mod, VALUE src, VALUE dict_capacity)
489
+ {
490
+ rb_check_type(src, RUBY_T_STRING);
491
+ size_t capa = NUM2SIZET(dict_capacity);
492
+ VALUE dict = rb_str_buf_new(capa);
493
+ size_t srcsize = RSTRING_LEN(src);
494
+ size_t s = ZDICT_trainFromBuffer(RSTRING_PTR(dict), capa, RSTRING_PTR(src), &srcsize, 1);
495
+ extzstd_check_error(s);
496
+ rb_str_set_len(dict, s);
497
+ return dict;
160
498
  }
161
499
 
500
+ /*
501
+ * call-seq:
502
+ * dict_add_entropy_tables_from_buffer(dict, dict_capacity, sample) -> dict
503
+ */
162
504
  static VALUE
163
- libver_s_to_s(VALUE ver)
505
+ ext_s_dict_add_entropy_tables_from_buffer(VALUE mod, VALUE dict, VALUE dict_capacity, VALUE sample)
164
506
  {
165
- static VALUE str;
166
- if (!str) {
167
- str = rb_sprintf("%d.%d.%d",
168
- ZSTD_VERSION_MAJOR,
169
- ZSTD_VERSION_MINOR,
170
- ZSTD_VERSION_RELEASE);
171
- }
172
- return str;
507
+ /*
508
+ * size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
509
+ * const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
510
+ */
511
+
512
+ rb_check_type(dict, RUBY_T_STRING);
513
+ rb_check_type(sample, RUBY_T_STRING);
514
+ size_t capa = NUM2SIZET(dict_capacity);
515
+ aux_str_modify_expand(dict, capa);
516
+ size_t samplesize = RSTRING_LEN(sample);
517
+ size_t s = ZDICT_addEntropyTablesFromBuffer(RSTRING_PTR(dict), RSTRING_LEN(dict), capa, RSTRING_PTR(dict), &samplesize, 1);
518
+ extzstd_check_error(s);
519
+ rb_str_set_len(dict, s);
520
+ return dict;
173
521
  }
174
522
 
175
- void
176
- Init_extzstd(void)
523
+ static void
524
+ init_dictionary(void)
177
525
  {
178
- mZstd = rb_define_module("Zstd");
179
- rb_define_singleton_method(mZstd, "encode", RUBY_METHOD_FUNC(zstd_s_encode), -1);
180
- rb_define_singleton_method(mZstd, "decode", RUBY_METHOD_FUNC(zstd_s_decode), -1);
526
+ rb_define_singleton_method(extzstd_mZstd, "dict_train_from_buffer", ext_s_dict_train_from_buffer, 2);
527
+ rb_define_singleton_method(extzstd_mZstd, "dict_add_entropy_tables_from_buffer", ext_s_dict_add_entropy_tables_from_buffer, 3);
528
+ }
181
529
 
182
- VALUE libver = rb_ary_new3(3,
183
- INT2FIX(ZSTD_VERSION_MAJOR),
184
- INT2FIX(ZSTD_VERSION_MINOR),
185
- INT2FIX(ZSTD_VERSION_RELEASE));
186
- rb_define_singleton_method(libver, "to_s", RUBY_METHOD_FUNC(libver_s_to_s), 0);
187
- rb_obj_freeze(libver);
188
- rb_define_const(mZstd, "LIBRARY_VERSION", libver);
189
530
 
190
- eError = rb_define_class_under(mZstd, "Error", rb_eRuntimeError);
531
+ /*
532
+ * library initializer
533
+ */
534
+
535
+ void
536
+ Init_extzstd(void)
537
+ {
538
+ extzstd_mZstd = rb_define_module("Zstd");
191
539
 
192
- init_extzstd_stream();
540
+ init_libver();
541
+ init_error();
542
+ init_constants();
543
+ init_encparams();
544
+ init_dictionary();
545
+ extzstd_init_buffered();
193
546
  }