extzstd 0.0.1.CONCEPT → 0.0.2.CONCEPT
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +27 -15
- data/Rakefile +19 -8
- data/contrib/zstd/Makefile +5 -4
- data/contrib/zstd/fse.c +1528 -583
- data/contrib/zstd/fse.h +167 -227
- data/contrib/zstd/fse_static.h +197 -23
- data/contrib/zstd/zstd.c +594 -627
- data/contrib/zstd/zstd.h +7 -16
- data/contrib/zstd/zstd_static.h +24 -15
- data/ext/extconf.rb +6 -1
- data/ext/extzstd-stream.c +238 -59
- data/ext/extzstd.c +77 -18
- data/ext/extzstd.h +29 -2
- data/gemstub.rb +3 -3
- data/lib/extzstd.rb +127 -1
- data/lib/extzstd/version.rb +1 -3
- metadata +13 -7
data/contrib/zstd/zstd.h
CHANGED
@@ -46,9 +46,9 @@ extern "C" {
|
|
46
46
|
* Version
|
47
47
|
**************************************/
|
48
48
|
#define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */
|
49
|
-
#define ZSTD_VERSION_MINOR
|
50
|
-
#define ZSTD_VERSION_RELEASE
|
51
|
-
#define ZSTD_VERSION_NUMBER
|
49
|
+
#define ZSTD_VERSION_MINOR 1 /* for new (non-breaking) interface capabilities */
|
50
|
+
#define ZSTD_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */
|
51
|
+
#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
|
52
52
|
unsigned ZSTD_versionNumber (void);
|
53
53
|
|
54
54
|
|
@@ -64,13 +64,13 @@ size_t ZSTD_decompress( void* dst, size_t maxOriginalSize,
|
|
64
64
|
/*
|
65
65
|
ZSTD_compress() :
|
66
66
|
Compresses 'srcSize' bytes from buffer 'src' into buffer 'dst', of maximum size 'dstSize'.
|
67
|
-
Destination buffer
|
68
|
-
|
67
|
+
Destination buffer must be already allocated.
|
68
|
+
Compression runs faster if maxDstSize >= ZSTD_compressBound(srcSize).
|
69
69
|
return : the number of bytes written into buffer 'dst'
|
70
70
|
or an error code if it fails (which can be tested using ZSTD_isError())
|
71
71
|
|
72
72
|
ZSTD_decompress() :
|
73
|
-
compressedSize : is
|
73
|
+
compressedSize : is the exact source size
|
74
74
|
maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.
|
75
75
|
It must be equal or larger than originalSize, otherwise decompression will fail.
|
76
76
|
return : the number of bytes decompressed into destination buffer (originalSize)
|
@@ -81,22 +81,13 @@ ZSTD_decompress() :
|
|
81
81
|
/**************************************
|
82
82
|
* Tool functions
|
83
83
|
**************************************/
|
84
|
-
size_t ZSTD_compressBound(size_t srcSize); /* maximum compressed size */
|
84
|
+
size_t ZSTD_compressBound(size_t srcSize); /* maximum compressed size (worst case scenario) */
|
85
85
|
|
86
86
|
/* Error Management */
|
87
87
|
unsigned ZSTD_isError(size_t code); /* tells if a return value is an error code */
|
88
88
|
const char* ZSTD_getErrorName(size_t code); /* provides error code string (useful for debugging) */
|
89
89
|
|
90
90
|
|
91
|
-
#define CHECK_OVERFLOW(statment) \
|
92
|
-
do { \
|
93
|
-
if (!(statment)) { \
|
94
|
-
fprintf(stderr, \
|
95
|
-
"%s:%d:%s: detect buffer overflow - assert for (%s)\n", \
|
96
|
-
__FILE__, __LINE__, __func__, #statment); \
|
97
|
-
} \
|
98
|
-
} while (0) \
|
99
|
-
|
100
91
|
#if defined (__cplusplus)
|
101
92
|
}
|
102
93
|
#endif
|
data/contrib/zstd/zstd_static.h
CHANGED
@@ -45,30 +45,39 @@ extern "C" {
|
|
45
45
|
/**************************************
|
46
46
|
* Streaming functions
|
47
47
|
**************************************/
|
48
|
-
typedef
|
49
|
-
|
50
|
-
size_t
|
48
|
+
typedef struct ZSTD_Cctx_s ZSTD_Cctx;
|
49
|
+
ZSTD_Cctx* ZSTD_createCCtx(void);
|
50
|
+
size_t ZSTD_freeCCtx(ZSTD_Cctx* cctx);
|
51
51
|
|
52
|
-
size_t ZSTD_compressBegin(
|
53
|
-
size_t ZSTD_compressContinue(
|
54
|
-
size_t ZSTD_compressEnd(
|
52
|
+
size_t ZSTD_compressBegin(ZSTD_Cctx* cctx, void* dst, size_t maxDstSize);
|
53
|
+
size_t ZSTD_compressContinue(ZSTD_Cctx* cctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
|
54
|
+
size_t ZSTD_compressEnd(ZSTD_Cctx* cctx, void* dst, size_t maxDstSize);
|
55
55
|
|
56
|
-
typedef void* ZSTD_dctx_t;
|
57
|
-
ZSTD_dctx_t ZSTD_createDCtx(void);
|
58
|
-
size_t ZSTD_freeDCtx(ZSTD_dctx_t dctx);
|
59
56
|
|
60
|
-
|
61
|
-
|
57
|
+
typedef struct ZSTD_Dctx_s ZSTD_Dctx;
|
58
|
+
ZSTD_Dctx* ZSTD_createDCtx(void);
|
59
|
+
size_t ZSTD_resetDCtx(ZSTD_Dctx* dctx);
|
60
|
+
size_t ZSTD_freeDCtx(ZSTD_Dctx* dctx);
|
62
61
|
|
62
|
+
size_t ZSTD_nextSrcSizeToDecompress(ZSTD_Dctx* dctx);
|
63
|
+
size_t ZSTD_decompressContinue(ZSTD_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
|
64
|
+
/*
|
65
|
+
Use above functions alternatively.
|
66
|
+
ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as input to ZSTD_decompressContinue().
|
67
|
+
This value is expected to be provided, precisely, as 'srcSize'.
|
68
|
+
Otherwise, compression will fail (result is an error code, which can be tested using ZSTD_isError() )
|
69
|
+
ZSTD_decompressContinue() result is the number of bytes regenerated within 'dst'.
|
70
|
+
It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
|
71
|
+
*/
|
63
72
|
|
64
73
|
/**************************************
|
65
74
|
* Error management
|
66
75
|
**************************************/
|
67
76
|
#define ZSTD_LIST_ERRORS(ITEM) \
|
68
77
|
ITEM(ZSTD_OK_NoError) ITEM(ZSTD_ERROR_GENERIC) \
|
69
|
-
ITEM(
|
70
|
-
ITEM(
|
71
|
-
ITEM(
|
78
|
+
ITEM(ZSTD_ERROR_MagicNumber) \
|
79
|
+
ITEM(ZSTD_ERROR_SrcSize) ITEM(ZSTD_ERROR_maxDstSize_tooSmall) \
|
80
|
+
ITEM(ZSTD_ERROR_corruption) \
|
72
81
|
ITEM(ZSTD_ERROR_maxCode)
|
73
82
|
|
74
83
|
#define ZSTD_GENERATE_ENUM(ENUM) ENUM,
|
@@ -77,4 +86,4 @@ typedef enum { ZSTD_LIST_ERRORS(ZSTD_GENERATE_ENUM) } ZSTD_errorCodes; /* expo
|
|
77
86
|
|
78
87
|
#if defined (__cplusplus)
|
79
88
|
}
|
80
|
-
#endif
|
89
|
+
#endif
|
data/ext/extconf.rb
CHANGED
@@ -7,11 +7,16 @@ dir = File.dirname(__FILE__).gsub(/[\[\{\?\*]/, "[\\0]")
|
|
7
7
|
filepattern = "{.,../contrib/zstd}/*.c"
|
8
8
|
target = File.join(dir, filepattern)
|
9
9
|
files = Dir.glob(target).map { |n| File.basename n }
|
10
|
+
## reject fse.c, because it's included in zstd.c
|
10
11
|
files.reject! { |n| "/contrib/zstd/fse.c".include?(n) }
|
11
12
|
$srcs = files
|
12
13
|
|
13
14
|
$VPATH.push "$(srcdir)/../contrib/zstd"
|
14
15
|
|
15
|
-
find_header "zstd.h", "$(srcdir)/../contrib/zstd"
|
16
|
+
find_header "zstd.h", "$(srcdir)/../contrib/zstd" or abort 1
|
17
|
+
|
18
|
+
if RbConfig::CONFIG["arch"] =~ /mingw/
|
19
|
+
$LDFLAGS << " -static-libgcc"
|
20
|
+
end
|
16
21
|
|
17
22
|
create_makefile("extzstd")
|
data/ext/extzstd-stream.c
CHANGED
@@ -1,30 +1,53 @@
|
|
1
1
|
#include "extzstd.h"
|
2
2
|
|
3
|
-
|
4
|
-
ID id_op_lshift;
|
3
|
+
static ID id_op_lshift;
|
5
4
|
|
6
|
-
|
5
|
+
#if 0
|
6
|
+
static size_t
|
7
|
+
aux_read(VALUE io, size_t size, char *buf)
|
8
|
+
{
|
9
|
+
VALUE w = SEND(io, id_read, SIZET2NUM(size));
|
10
|
+
if (NIL_P(w)) { return 0; }
|
11
|
+
rb_check_type(w, RUBY_T_STRING);
|
12
|
+
size_t s = RSTRING_LEN(w);
|
13
|
+
if (s > size) {
|
14
|
+
rb_raise(rb_eRuntimeError,
|
15
|
+
"read size is too big - #<%s:%p> (size is %"PRIuSIZE" for %"PRIuSIZE")",
|
16
|
+
rb_obj_classname(io), (void *)io, s, size);
|
17
|
+
}
|
18
|
+
memcpy(buf, RSTRING_PTR(w), s);
|
19
|
+
return s;
|
20
|
+
}
|
21
|
+
#endif
|
22
|
+
|
23
|
+
/*
|
24
|
+
* class Zstd::Encoder
|
25
|
+
*/
|
26
|
+
|
27
|
+
static VALUE cEncoder;
|
28
|
+
|
29
|
+
struct encoder
|
7
30
|
{
|
8
31
|
VALUE outport;
|
9
32
|
VALUE destbuf;
|
10
|
-
|
33
|
+
ZSTD_Cctx *encoder;
|
11
34
|
};
|
12
35
|
|
13
36
|
static void
|
14
|
-
|
37
|
+
enc_mark(void *pp)
|
15
38
|
{
|
16
39
|
if (pp) {
|
17
|
-
struct
|
40
|
+
struct encoder *p = pp;
|
18
41
|
rb_gc_mark(p->outport);
|
19
42
|
rb_gc_mark(p->destbuf);
|
20
43
|
}
|
21
44
|
}
|
22
45
|
|
23
46
|
static void
|
24
|
-
|
47
|
+
enc_free(void *pp)
|
25
48
|
{
|
26
49
|
if (pp) {
|
27
|
-
struct
|
50
|
+
struct encoder *p = pp;
|
28
51
|
if (p->encoder) {
|
29
52
|
ZSTD_freeCCtx(p->encoder);
|
30
53
|
}
|
@@ -35,39 +58,39 @@ zstdenc_free(void *pp)
|
|
35
58
|
}
|
36
59
|
}
|
37
60
|
|
38
|
-
static const rb_data_type_t
|
61
|
+
static const rb_data_type_t encoder_type = {
|
39
62
|
.wrap_struct_name = "extzstd.Zstd.Encoder",
|
40
|
-
.function.dmark =
|
41
|
-
.function.dfree =
|
42
|
-
/* .function.dsize =
|
63
|
+
.function.dmark = enc_mark,
|
64
|
+
.function.dfree = enc_free,
|
65
|
+
/* .function.dsize = enc_size, */
|
43
66
|
};
|
44
67
|
|
45
68
|
|
46
69
|
static VALUE
|
47
|
-
|
70
|
+
enc_alloc(VALUE klass)
|
48
71
|
{
|
49
|
-
struct
|
50
|
-
VALUE v = TypedData_Make_Struct(klass, struct
|
72
|
+
struct encoder *p;
|
73
|
+
VALUE v = TypedData_Make_Struct(klass, struct encoder, &encoder_type, p);
|
51
74
|
p->outport = Qnil;
|
52
75
|
p->destbuf = Qnil;
|
53
76
|
p->encoder = NULL;
|
54
77
|
return v;
|
55
78
|
}
|
56
79
|
|
57
|
-
static inline struct
|
80
|
+
static inline struct encoder *
|
58
81
|
getencoderp(VALUE enc)
|
59
82
|
{
|
60
|
-
return getrefp(enc, &
|
83
|
+
return getrefp(enc, &encoder_type);
|
61
84
|
}
|
62
85
|
|
63
|
-
static inline struct
|
86
|
+
static inline struct encoder *
|
64
87
|
getencoder(VALUE enc)
|
65
88
|
{
|
66
|
-
return getref(enc, &
|
89
|
+
return getref(enc, &encoder_type);
|
67
90
|
}
|
68
91
|
|
69
92
|
static inline void
|
70
|
-
|
93
|
+
enc_init_args(int argc, VALUE argv[], VALUE *outport)
|
71
94
|
{
|
72
95
|
rb_scan_args(argc, argv, "01", outport);
|
73
96
|
if (NIL_P(*outport)) {
|
@@ -78,30 +101,31 @@ zstdenc_init_args(int argc, VALUE argv[], VALUE *outport)
|
|
78
101
|
/*
|
79
102
|
* call-seq:
|
80
103
|
* initialize(outport) -> self
|
104
|
+
*
|
105
|
+
* +outport+ is required +.<<+ method for behavior as same as +IO#<<+ method.
|
81
106
|
*/
|
82
107
|
static VALUE
|
83
|
-
|
108
|
+
enc_init(int argc, VALUE argv[], VALUE enc)
|
84
109
|
{
|
85
|
-
struct
|
86
|
-
if (p->encoder) { reiniterror(
|
87
|
-
|
110
|
+
struct encoder *p = getencoder(enc);
|
111
|
+
if (p->encoder) { reiniterror(enc); }
|
112
|
+
enc_init_args(argc, argv, &p->outport);
|
88
113
|
p->destbuf = rb_str_buf_new(4 * 1024); /* FIXME: ``4 * 1024`` to constant value */
|
89
114
|
p->encoder = ZSTD_createCCtx();
|
90
115
|
if (!p->encoder) {
|
91
116
|
rb_raise(eError,
|
92
|
-
|
117
|
+
"failed ZSTD_createCCtx()");
|
93
118
|
}
|
94
119
|
|
95
120
|
size_t destsize = rb_str_capacity(p->destbuf);
|
96
|
-
//rb_str_resize(p->destbuf, destsize); /* FIXME: rb_str_resize は縮小もするので、できれば rb_str_modify_expand をつかう */
|
97
121
|
|
98
122
|
size_t s = ZSTD_compressBegin(p->encoder, RSTRING_PTR(p->destbuf), destsize);
|
99
123
|
if (ZSTD_isError(s)) {
|
100
124
|
rb_raise(eError,
|
101
|
-
|
102
|
-
|
125
|
+
"failed ZSTD_compressBegin() - %s (%d)",
|
126
|
+
ZSTD_getErrorName(s), (int)s);
|
103
127
|
}
|
104
|
-
|
128
|
+
|
105
129
|
if (s > destsize) {
|
106
130
|
rb_bug("detect buffer overflow from in ZSTD_compressBegin() (destbuf.bytesize=%d, returned bytesize=%d)",
|
107
131
|
(int)RSTRING_LEN(p->destbuf), (int)s);
|
@@ -110,7 +134,7 @@ zstdenc_init(int argc, VALUE argv[], VALUE senc)
|
|
110
134
|
rb_str_set_len(p->destbuf, s);
|
111
135
|
rb_funcall2(p->outport, id_op_lshift, 1, &p->destbuf);
|
112
136
|
|
113
|
-
return
|
137
|
+
return enc;
|
114
138
|
}
|
115
139
|
|
116
140
|
/*
|
@@ -118,10 +142,10 @@ zstdenc_init(int argc, VALUE argv[], VALUE senc)
|
|
118
142
|
* write(src) -> self
|
119
143
|
*/
|
120
144
|
static VALUE
|
121
|
-
|
145
|
+
enc_write(int argc, VALUE argv[], VALUE enc)
|
122
146
|
{
|
123
|
-
struct
|
124
|
-
if (!p->encoder) { referror(
|
147
|
+
struct encoder *p = getencoder(enc);
|
148
|
+
if (!p->encoder) { referror(enc); }
|
125
149
|
|
126
150
|
VALUE src;
|
127
151
|
rb_scan_args(argc, argv, "1", &src);
|
@@ -131,14 +155,15 @@ zstdenc_write(int argc, VALUE argv[], VALUE senc)
|
|
131
155
|
rb_str_modify(p->destbuf);
|
132
156
|
rb_str_set_len(p->destbuf, 0);
|
133
157
|
rb_str_modify_expand(p->destbuf, destsize);
|
134
|
-
|
158
|
+
rb_obj_infect(enc, src);
|
159
|
+
rb_obj_infect(p->destbuf, enc);
|
135
160
|
size_t s = ZSTD_compressContinue(p->encoder, RSTRING_PTR(p->destbuf), destsize, RSTRING_PTR(src), RSTRING_LEN(src));
|
136
161
|
if (ZSTD_isError(s)) {
|
137
162
|
rb_raise(eError,
|
138
|
-
|
139
|
-
|
163
|
+
"failed ZSTD_compressContinue() - %s (%d)",
|
164
|
+
ZSTD_getErrorName(s), (int)s);
|
140
165
|
}
|
141
|
-
|
166
|
+
|
142
167
|
if (s > destsize) {
|
143
168
|
rb_bug("detect buffer overflow from in ZSTD_compressContinue() (destbuf.bytesize=%d, returned bytesize=%d)",
|
144
169
|
(int)RSTRING_LEN(p->destbuf), (int)s);
|
@@ -147,7 +172,7 @@ zstdenc_write(int argc, VALUE argv[], VALUE senc)
|
|
147
172
|
rb_str_set_len(p->destbuf, s);
|
148
173
|
rb_funcall2(p->outport, id_op_lshift, 1, &p->destbuf);
|
149
174
|
|
150
|
-
return
|
175
|
+
return enc;
|
151
176
|
}
|
152
177
|
|
153
178
|
/*
|
@@ -155,10 +180,10 @@ zstdenc_write(int argc, VALUE argv[], VALUE senc)
|
|
155
180
|
* finish -> self
|
156
181
|
*/
|
157
182
|
static VALUE
|
158
|
-
|
183
|
+
enc_finish(int argc, VALUE argv[], VALUE enc)
|
159
184
|
{
|
160
|
-
struct
|
161
|
-
if (!p->encoder) { referror(
|
185
|
+
struct encoder *p = getencoder(enc);
|
186
|
+
if (!p->encoder) { referror(enc); }
|
162
187
|
|
163
188
|
rb_scan_args(argc, argv, "0");
|
164
189
|
|
@@ -170,8 +195,8 @@ zstdenc_finish(int argc, VALUE argv[], VALUE senc)
|
|
170
195
|
size_t s = ZSTD_compressEnd(p->encoder, RSTRING_PTR(p->destbuf), destsize);
|
171
196
|
if (ZSTD_isError(s)) {
|
172
197
|
rb_raise(eError,
|
173
|
-
|
174
|
-
|
198
|
+
"failed ZSTD_compressEnd() - %s (%d)",
|
199
|
+
ZSTD_getErrorName(s), (int)s);
|
175
200
|
}
|
176
201
|
|
177
202
|
if (s > destsize) {
|
@@ -182,38 +207,192 @@ zstdenc_finish(int argc, VALUE argv[], VALUE senc)
|
|
182
207
|
rb_str_set_len(p->destbuf, s);
|
183
208
|
rb_funcall2(p->outport, id_op_lshift, 1, &p->destbuf);
|
184
209
|
|
185
|
-
return
|
210
|
+
return enc;
|
186
211
|
}
|
187
212
|
|
188
213
|
static VALUE
|
189
|
-
|
214
|
+
enc_getoutport(VALUE enc)
|
190
215
|
{
|
191
216
|
return getencoder(enc)->outport;
|
192
217
|
}
|
193
218
|
|
194
219
|
static VALUE
|
195
|
-
|
220
|
+
enc_setoutport(VALUE enc, VALUE outport)
|
196
221
|
{
|
197
222
|
return getencoder(enc)->outport = outport;
|
198
223
|
}
|
199
224
|
|
225
|
+
/*
|
226
|
+
* class Zstd::LowLevelDecoder
|
227
|
+
*/
|
228
|
+
|
229
|
+
static VALUE cLLDecoder;
|
230
|
+
|
231
|
+
struct lldecoder
|
232
|
+
{
|
233
|
+
ZSTD_Dctx *decoder;
|
234
|
+
};
|
235
|
+
|
236
|
+
static void
|
237
|
+
lldec_mark(void *pp)
|
238
|
+
{
|
239
|
+
if (pp) {
|
240
|
+
struct lldecoder *p = pp;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
244
|
+
static void
|
245
|
+
lldec_free(void *pp)
|
246
|
+
{
|
247
|
+
if (pp) {
|
248
|
+
struct lldecoder *p = pp;
|
249
|
+
if (p->decoder) {
|
250
|
+
ZSTD_freeDCtx(p->decoder);
|
251
|
+
}
|
252
|
+
free(p);
|
253
|
+
}
|
254
|
+
}
|
255
|
+
|
256
|
+
static const rb_data_type_t lldecoder_type = {
|
257
|
+
.wrap_struct_name = "extzstd.Zstd.LowLevelDecoder",
|
258
|
+
.function.dmark = lldec_mark,
|
259
|
+
.function.dfree = lldec_free,
|
260
|
+
/* .function.dsize = lldec_size, */
|
261
|
+
};
|
262
|
+
|
263
|
+
|
264
|
+
static VALUE
|
265
|
+
lldec_alloc(VALUE klass)
|
266
|
+
{
|
267
|
+
struct lldecoder *p;
|
268
|
+
VALUE v = TypedData_Make_Struct(klass, struct lldecoder, &lldecoder_type, p);
|
269
|
+
p->decoder = NULL;
|
270
|
+
return v;
|
271
|
+
}
|
272
|
+
|
273
|
+
static inline struct lldecoder *
|
274
|
+
getlldecoderp(VALUE v)
|
275
|
+
{
|
276
|
+
return getrefp(v, &lldecoder_type);
|
277
|
+
}
|
278
|
+
|
279
|
+
static inline struct lldecoder *
|
280
|
+
getlldecoder(VALUE v)
|
281
|
+
{
|
282
|
+
return getref(v, &lldecoder_type);
|
283
|
+
}
|
284
|
+
|
285
|
+
/*
|
286
|
+
* call-seq:
|
287
|
+
* initialize
|
288
|
+
*/
|
289
|
+
static VALUE
|
290
|
+
lldec_init(int argc, VALUE argv[], VALUE dec)
|
291
|
+
{
|
292
|
+
struct lldecoder *p = getlldecoder(dec);
|
293
|
+
if (p->decoder) { reiniterror(dec); }
|
294
|
+
rb_check_arity(argc, 0, 0);
|
295
|
+
p->decoder = ZSTD_createDCtx();
|
296
|
+
if (!p->decoder) {
|
297
|
+
rb_raise(eError,
|
298
|
+
"failed ZSTD_createDCtx()");
|
299
|
+
}
|
300
|
+
|
301
|
+
ZSTD_resetDCtx(p->decoder);
|
302
|
+
|
303
|
+
return dec;
|
304
|
+
}
|
305
|
+
|
306
|
+
/*
|
307
|
+
* call-seq:
|
308
|
+
* reset -> self
|
309
|
+
*/
|
310
|
+
static VALUE
|
311
|
+
lldec_reset(VALUE dec)
|
312
|
+
{
|
313
|
+
struct lldecoder *p = getlldecoder(dec);
|
314
|
+
if (!p->decoder) { referror(dec); }
|
315
|
+
size_t s = ZSTD_resetDCtx(p->decoder);
|
316
|
+
if (ZSTD_isError(s)) {
|
317
|
+
rb_raise(eError,
|
318
|
+
"failed ZSTD_resetDCtx() - %s (%d)",
|
319
|
+
ZSTD_getErrorName(s), (int)s);
|
320
|
+
}
|
321
|
+
return dec;
|
322
|
+
}
|
323
|
+
|
324
|
+
/*
|
325
|
+
* call-seq:
|
326
|
+
* next_srcsize -> next size as integer
|
327
|
+
*/
|
328
|
+
static VALUE
|
329
|
+
lldec_next_srcsize(VALUE dec)
|
330
|
+
{
|
331
|
+
struct lldecoder *p = getlldecoder(dec);
|
332
|
+
if (!p->decoder) { referror(dec); }
|
333
|
+
size_t s = ZSTD_nextSrcSizeToDecompress(p->decoder);
|
334
|
+
if (ZSTD_isError(s)) {
|
335
|
+
rb_raise(eError,
|
336
|
+
"failed ZSTD_nextSrcSizeToDecompress() - %s (%d)",
|
337
|
+
ZSTD_getErrorName(s), (int)s);
|
338
|
+
}
|
339
|
+
return SIZET2NUM(s);
|
340
|
+
}
|
341
|
+
|
342
|
+
/*
|
343
|
+
* call-seq:
|
344
|
+
* decode(src, dest, maxdestsize) -> dest OR nil
|
345
|
+
*/
|
346
|
+
static VALUE
|
347
|
+
lldec_decode(VALUE dec, VALUE src, VALUE dest, VALUE maxdestsize)
|
348
|
+
{
|
349
|
+
struct lldecoder *p = getlldecoder(dec);
|
350
|
+
if (!p->decoder) { referror(dec); }
|
351
|
+
|
352
|
+
rb_obj_infect(dec, src);
|
353
|
+
rb_obj_infect(dest, dec);
|
354
|
+
const char *srcp = StringValuePtr(src);
|
355
|
+
size_t srcsize = RSTRING_LEN(src);
|
356
|
+
size_t destsize = NUM2UINT(maxdestsize); /* サイズ制限のために NUM2UINT にしている */
|
357
|
+
aux_str_modify_expand(dest, destsize);
|
358
|
+
char *destp = RSTRING_PTR(dest);
|
359
|
+
size_t s = ZSTD_decompressContinue(p->decoder, destp, destsize, srcp, srcsize);
|
360
|
+
if (ZSTD_isError(s)) {
|
361
|
+
rb_raise(eError,
|
362
|
+
"failed ZSTD_decompressContinue() - %s (%d)",
|
363
|
+
ZSTD_getErrorName(s), (int)s);
|
364
|
+
}
|
365
|
+
rb_str_set_len(dest, s);
|
366
|
+
if (s == 0) {
|
367
|
+
return Qnil;
|
368
|
+
} else {
|
369
|
+
return dest;
|
370
|
+
}
|
371
|
+
}
|
372
|
+
|
373
|
+
/*
|
374
|
+
* initializer for Zstd::Encoder and Zstd::LowLevelDecoder
|
375
|
+
*/
|
376
|
+
|
200
377
|
void
|
201
378
|
init_extzstd_stream(void)
|
202
379
|
{
|
203
380
|
id_op_lshift = rb_intern("<<");
|
204
381
|
|
382
|
+
RDOCFAKE(mZstd = rb_define_module("Zstd"));
|
383
|
+
|
205
384
|
cEncoder = rb_define_class_under(mZstd, "Encoder", rb_cObject);
|
206
|
-
rb_define_alloc_func(cEncoder,
|
207
|
-
rb_define_method(cEncoder, "initialize", RUBY_METHOD_FUNC(
|
208
|
-
rb_define_method(cEncoder, "write", RUBY_METHOD_FUNC(
|
209
|
-
rb_define_method(cEncoder, "finish", RUBY_METHOD_FUNC(
|
210
|
-
rb_define_method(cEncoder, "outport", RUBY_METHOD_FUNC(
|
211
|
-
rb_define_method(cEncoder, "outport=", RUBY_METHOD_FUNC(
|
212
|
-
|
213
|
-
|
214
|
-
rb_define_alloc_func(
|
215
|
-
rb_define_method(
|
216
|
-
rb_define_method(
|
217
|
-
rb_define_method(
|
218
|
-
|
385
|
+
rb_define_alloc_func(cEncoder, enc_alloc);
|
386
|
+
rb_define_method(cEncoder, "initialize", RUBY_METHOD_FUNC(enc_init), -1);
|
387
|
+
rb_define_method(cEncoder, "write", RUBY_METHOD_FUNC(enc_write), -1);
|
388
|
+
rb_define_method(cEncoder, "finish", RUBY_METHOD_FUNC(enc_finish), -1);
|
389
|
+
rb_define_method(cEncoder, "outport", RUBY_METHOD_FUNC(enc_getoutport), 0);
|
390
|
+
rb_define_method(cEncoder, "outport=", RUBY_METHOD_FUNC(enc_setoutport), 1);
|
391
|
+
|
392
|
+
cLLDecoder = rb_define_class_under(mZstd, "LowLevelDecoder", rb_cObject);
|
393
|
+
rb_define_alloc_func(cLLDecoder, lldec_alloc);
|
394
|
+
rb_define_method(cLLDecoder, "initialize", RUBY_METHOD_FUNC(lldec_init), -1);
|
395
|
+
rb_define_method(cLLDecoder, "reset", RUBY_METHOD_FUNC(lldec_reset), 0);
|
396
|
+
rb_define_method(cLLDecoder, "next_srcsize", RUBY_METHOD_FUNC(lldec_next_srcsize), 0);
|
397
|
+
rb_define_method(cLLDecoder, "decode", RUBY_METHOD_FUNC(lldec_decode), 3);
|
219
398
|
}
|