line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#ifdef __cplusplus |
2
|
|
|
|
|
|
|
extern "C" { |
3
|
|
|
|
|
|
|
#endif |
4
|
|
|
|
|
|
|
#define PERL_NO_GET_CONTEXT |
5
|
|
|
|
|
|
|
#include "EXTERN.h" |
6
|
|
|
|
|
|
|
#include "perl.h" |
7
|
|
|
|
|
|
|
#include "XSUB.h" |
8
|
|
|
|
|
|
|
#ifdef __cplusplus |
9
|
|
|
|
|
|
|
} |
10
|
|
|
|
|
|
|
#endif |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
#define NEED_sv_2pvbyte |
13
|
|
|
|
|
|
|
#define NEED_newCONSTSUB |
14
|
|
|
|
|
|
|
#include "ppport.h" |
15
|
|
|
|
|
|
|
#include "zstd.h" |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
typedef struct Compress__Stream__Zstd__Compressor_s { |
18
|
|
|
|
|
|
|
ZSTD_CStream* stream; |
19
|
|
|
|
|
|
|
char* buf; |
20
|
|
|
|
|
|
|
size_t bufsize; |
21
|
|
|
|
|
|
|
size_t status ; |
22
|
|
|
|
|
|
|
}* Compress__Stream__Zstd__Compressor; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
typedef struct Compress__Stream__Zstd__Decompressor_s { |
25
|
|
|
|
|
|
|
ZSTD_DStream* stream; |
26
|
|
|
|
|
|
|
char* buf; |
27
|
|
|
|
|
|
|
size_t bufsize; |
28
|
|
|
|
|
|
|
size_t status ; |
29
|
|
|
|
|
|
|
}* Compress__Stream__Zstd__Decompressor; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
typedef ZSTD_CCtx* Compress__Stream__Zstd__CompressionContext; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
typedef ZSTD_DCtx* Compress__Stream__Zstd__DecompressionContext; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
typedef ZSTD_CDict* Compress__Stream__Zstd__CompressionDictionary; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
typedef ZSTD_DDict* Compress__Stream__Zstd__DecompressionDictionary; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
static SV* |
40
|
1
|
|
|
|
|
|
decompress_using_streaming(pTHX_ const char* src, size_t srcSize) |
41
|
|
|
|
|
|
|
{ |
42
|
|
|
|
|
|
|
char* buf; |
43
|
|
|
|
|
|
|
size_t bufsize; |
44
|
|
|
|
|
|
|
SV* output; |
45
|
1
|
|
|
|
|
|
ZSTD_inBuffer inbuf = { src, srcSize, 0 }; |
46
|
|
|
|
|
|
|
int iserror = 0; |
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
|
|
|
ZSTD_DStream* stream = ZSTD_createDStream(); |
49
|
1
|
50
|
|
|
|
|
if (stream == NULL) { |
50
|
0
|
|
|
|
|
|
croak("Failed to call ZSTD_createDStream()"); |
51
|
|
|
|
|
|
|
} |
52
|
1
|
|
|
|
|
|
ZSTD_initDStream(stream); |
53
|
|
|
|
|
|
|
|
54
|
1
|
|
|
|
|
|
bufsize = ZSTD_DStreamOutSize(); |
55
|
1
|
|
|
|
|
|
Newx(buf, bufsize, char); |
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
|
output = newSVpv("", 0); |
58
|
2
|
100
|
|
|
|
|
while (inbuf.pos < inbuf.size) { |
59
|
1
|
|
|
|
|
|
ZSTD_outBuffer outbuf = { buf, bufsize, 0 }; |
60
|
1
|
|
|
|
|
|
size_t ret = ZSTD_decompressStream(stream, &outbuf, &inbuf); |
61
|
1
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
62
|
|
|
|
|
|
|
iserror = 1; |
63
|
0
|
|
|
|
|
|
break; |
64
|
|
|
|
|
|
|
} |
65
|
1
|
|
|
|
|
|
sv_catpvn(output, outbuf.dst, outbuf.pos); |
66
|
|
|
|
|
|
|
} |
67
|
1
|
|
|
|
|
|
Safefree(buf); |
68
|
1
|
|
|
|
|
|
ZSTD_freeDStream(stream); |
69
|
1
|
50
|
|
|
|
|
if (iserror != 0) { |
70
|
|
|
|
|
|
|
SvREFCNT_dec(output); |
71
|
|
|
|
|
|
|
return NULL; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
return output; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
MODULE = Compress::Stream::Zstd PACKAGE = Compress::Stream::Zstd |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
BOOT: |
79
|
|
|
|
|
|
|
{ |
80
|
6
|
|
|
|
|
|
HV* stash = gv_stashpv("Compress::Stream::Zstd", 1); |
81
|
6
|
|
|
|
|
|
newCONSTSUB(stash, "ZSTD_VERSION_NUMBER", newSViv(ZSTD_VERSION_NUMBER)); |
82
|
6
|
|
|
|
|
|
newCONSTSUB(stash, "ZSTD_VERSION_STRING", newSVpvs(ZSTD_VERSION_STRING)); |
83
|
6
|
|
|
|
|
|
newCONSTSUB(stash, "ZSTD_MIN_CLEVEL", newSViv(ZSTD_minCLevel())); |
84
|
6
|
|
|
|
|
|
newCONSTSUB(stash, "ZSTD_MAX_CLEVEL", newSViv(ZSTD_maxCLevel())); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
void |
90
|
|
|
|
|
|
|
compress(source, level = 1) |
91
|
|
|
|
|
|
|
SV* source; |
92
|
|
|
|
|
|
|
int level; |
93
|
|
|
|
|
|
|
PREINIT: |
94
|
|
|
|
|
|
|
const char* src; |
95
|
|
|
|
|
|
|
STRLEN src_len; |
96
|
|
|
|
|
|
|
SV* dest; |
97
|
|
|
|
|
|
|
char* dst; |
98
|
|
|
|
|
|
|
size_t bound, ret; |
99
|
|
|
|
|
|
|
PPCODE: |
100
|
3
|
100
|
|
|
|
|
if (SvROK(source)) { |
101
|
1
|
|
|
|
|
|
source = SvRV(source); |
102
|
|
|
|
|
|
|
} |
103
|
3
|
50
|
|
|
|
|
if (!SvOK(source)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
105
|
|
|
|
|
|
|
} |
106
|
3
|
50
|
|
|
|
|
src = SvPVbyte(source, src_len); |
107
|
3
|
|
|
|
|
|
bound = ZSTD_compressBound(src_len); |
108
|
3
|
|
|
|
|
|
dest = sv_2mortal(newSV(bound + 1)); |
109
|
3
|
|
|
|
|
|
dst = SvPVX(dest); |
110
|
3
|
|
|
|
|
|
ret = ZSTD_compress(dst, bound + 1, src, src_len, level); |
111
|
3
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
112
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
113
|
|
|
|
|
|
|
} |
114
|
3
|
|
|
|
|
|
dst[ret] = '\0'; |
115
|
3
|
|
|
|
|
|
SvCUR_set(dest, ret); |
116
|
3
|
|
|
|
|
|
SvPOK_on(dest); |
117
|
3
|
50
|
|
|
|
|
EXTEND(SP, 1); |
118
|
3
|
|
|
|
|
|
PUSHs(dest); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
void |
123
|
|
|
|
|
|
|
decompress(source) |
124
|
|
|
|
|
|
|
SV* source; |
125
|
|
|
|
|
|
|
ALIAS: |
126
|
|
|
|
|
|
|
uncompress = 1 |
127
|
|
|
|
|
|
|
PREINIT: |
128
|
|
|
|
|
|
|
const char* src; |
129
|
|
|
|
|
|
|
STRLEN src_len; |
130
|
|
|
|
|
|
|
unsigned long long dest_len; |
131
|
|
|
|
|
|
|
SV* dest; |
132
|
|
|
|
|
|
|
char* dst; |
133
|
|
|
|
|
|
|
size_t ret; |
134
|
|
|
|
|
|
|
PPCODE: |
135
|
8
|
100
|
|
|
|
|
if (SvROK(source)) { |
136
|
1
|
|
|
|
|
|
source = SvRV(source); |
137
|
|
|
|
|
|
|
} |
138
|
8
|
50
|
|
|
|
|
if (!SvOK(source)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
140
|
|
|
|
|
|
|
} |
141
|
8
|
50
|
|
|
|
|
src = SvPVbyte(source, src_len); |
142
|
8
|
|
|
|
|
|
dest_len = ZSTD_getFrameContentSize(src, src_len); |
143
|
8
|
100
|
|
|
|
|
if (dest_len == ZSTD_CONTENTSIZE_UNKNOWN) { |
144
|
1
|
|
|
|
|
|
SV* output = decompress_using_streaming(aTHX_ src, src_len); |
145
|
1
|
50
|
|
|
|
|
if (output == NULL) { |
146
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
147
|
|
|
|
|
|
|
} |
148
|
1
|
50
|
|
|
|
|
EXTEND(SP, 1); |
149
|
1
|
|
|
|
|
|
mPUSHs(output); |
150
|
1
|
|
|
|
|
|
XSRETURN(1); |
151
|
|
|
|
|
|
|
} |
152
|
7
|
50
|
|
|
|
|
if (dest_len == ULLONG_MAX || ZSTD_isError(dest_len)) { |
|
|
100
|
|
|
|
|
|
153
|
1
|
|
|
|
|
|
XSRETURN_UNDEF; |
154
|
|
|
|
|
|
|
} |
155
|
6
|
|
|
|
|
|
dest = sv_2mortal(newSV(dest_len + 1)); |
156
|
6
|
|
|
|
|
|
dst = SvPVX(dest); |
157
|
6
|
|
|
|
|
|
ret = ZSTD_decompress(dst, dest_len + 1, src, src_len); |
158
|
6
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
159
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
160
|
|
|
|
|
|
|
} |
161
|
6
|
|
|
|
|
|
dst[ret] = '\0'; |
162
|
6
|
|
|
|
|
|
SvCUR_set(dest, ret); |
163
|
6
|
|
|
|
|
|
SvPOK_on(dest); |
164
|
6
|
50
|
|
|
|
|
EXTEND(SP, 1); |
165
|
6
|
|
|
|
|
|
PUSHs(dest); |
166
|
6
|
|
|
|
|
|
XSRETURN(1); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
MODULE = Compress::Stream::Zstd PACKAGE = Compress::Stream::Zstd::Compressor |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
BOOT: |
173
|
|
|
|
|
|
|
{ |
174
|
6
|
|
|
|
|
|
HV* stash = gv_stashpv("Compress::Stream::Zstd::Compressor", 1); |
175
|
6
|
|
|
|
|
|
newCONSTSUB(stash, "ZSTD_CSTREAM_IN_SIZE", newSViv(ZSTD_CStreamInSize())); |
176
|
6
|
|
|
|
|
|
newCONSTSUB(stash, "ZSTD_c_windowLog", newSVuv(ZSTD_c_windowLog)); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor |
180
|
|
|
|
|
|
|
new(klass, level = 1) |
181
|
|
|
|
|
|
|
const char* klass; |
182
|
|
|
|
|
|
|
int level; |
183
|
|
|
|
|
|
|
PREINIT: |
184
|
|
|
|
|
|
|
Compress__Stream__Zstd__Compressor self; |
185
|
|
|
|
|
|
|
char* buf; |
186
|
|
|
|
|
|
|
size_t bufsize; |
187
|
|
|
|
|
|
|
CODE: |
188
|
2
|
|
|
|
|
|
ZSTD_CStream* stream = ZSTD_createCStream(); |
189
|
2
|
50
|
|
|
|
|
if (stream == NULL) { |
190
|
0
|
|
|
|
|
|
croak("Failed to call ZSTD_createCStream()"); |
191
|
|
|
|
|
|
|
} |
192
|
2
|
|
|
|
|
|
ZSTD_initCStream(stream, level); |
193
|
|
|
|
|
|
|
|
194
|
2
|
|
|
|
|
|
Newx(self, sizeof(struct Compress__Stream__Zstd__Compressor_s), struct Compress__Stream__Zstd__Compressor_s); |
195
|
2
|
|
|
|
|
|
self->stream = stream; |
196
|
2
|
|
|
|
|
|
bufsize = ZSTD_CStreamOutSize(); |
197
|
2
|
|
|
|
|
|
Newx(buf, bufsize, char); |
198
|
2
|
|
|
|
|
|
self->buf = buf; |
199
|
2
|
|
|
|
|
|
self->bufsize = bufsize; |
200
|
|
|
|
|
|
|
RETVAL = self; |
201
|
|
|
|
|
|
|
OUTPUT: |
202
|
|
|
|
|
|
|
RETVAL |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
void |
205
|
|
|
|
|
|
|
init(self, level = 1) |
206
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
207
|
|
|
|
|
|
|
int level; |
208
|
|
|
|
|
|
|
CODE: |
209
|
0
|
|
|
|
|
|
ZSTD_initCStream(self->stream, level); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
void |
212
|
|
|
|
|
|
|
set_parameter(self, cParam, value) |
213
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
214
|
|
|
|
|
|
|
unsigned long cParam; |
215
|
|
|
|
|
|
|
int value; |
216
|
|
|
|
|
|
|
CODE: |
217
|
1
|
|
|
|
|
|
size_t ret = ZSTD_CCtx_setParameter( self->stream, (ZSTD_cParameter)cParam, value ); |
218
|
1
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
219
|
0
|
|
|
|
|
|
croak("%s", ZSTD_getErrorName(ret)); |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
SV* |
223
|
|
|
|
|
|
|
compress(self, input) |
224
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
225
|
|
|
|
|
|
|
SV* input; |
226
|
|
|
|
|
|
|
PREINIT: |
227
|
|
|
|
|
|
|
STRLEN len; |
228
|
|
|
|
|
|
|
SV* output; |
229
|
|
|
|
|
|
|
CODE: |
230
|
6
|
50
|
|
|
|
|
const char* in = SvPVbyte(input, len); |
231
|
6
|
|
|
|
|
|
ZSTD_inBuffer inbuf = { in, len, 0 }; |
232
|
6
|
|
|
|
|
|
output = newSVpv("", 0); |
233
|
12
|
100
|
|
|
|
|
while (inbuf.pos < inbuf.size) { |
234
|
6
|
|
|
|
|
|
ZSTD_outBuffer outbuf = { self->buf, self->bufsize, 0 }; |
235
|
6
|
|
|
|
|
|
size_t toread = self->status = ZSTD_compressStream(self->stream, &outbuf, &inbuf); |
236
|
6
|
50
|
|
|
|
|
if (ZSTD_isError(toread)) { |
237
|
0
|
|
|
|
|
|
croak("%s", ZSTD_getErrorName(toread)); |
238
|
|
|
|
|
|
|
} |
239
|
6
|
|
|
|
|
|
sv_catpvn(output, outbuf.dst, outbuf.pos); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
RETVAL = output; |
242
|
|
|
|
|
|
|
OUTPUT: |
243
|
|
|
|
|
|
|
RETVAL |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
SV* |
246
|
|
|
|
|
|
|
flush(self) |
247
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
248
|
|
|
|
|
|
|
PREINIT: |
249
|
|
|
|
|
|
|
SV* output; |
250
|
|
|
|
|
|
|
size_t ret; |
251
|
|
|
|
|
|
|
CODE: |
252
|
2
|
|
|
|
|
|
output = newSVpv("", 0); |
253
|
|
|
|
|
|
|
do { |
254
|
2
|
|
|
|
|
|
ZSTD_outBuffer outbuf = { self->buf, self->bufsize, 0 }; |
255
|
2
|
|
|
|
|
|
ret = self->status = ZSTD_flushStream(self->stream, &outbuf); |
256
|
2
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
257
|
0
|
|
|
|
|
|
croak("%s", ZSTD_getErrorName(ret)); |
258
|
|
|
|
|
|
|
} |
259
|
2
|
|
|
|
|
|
sv_catpvn(output, outbuf.dst, outbuf.pos); |
260
|
2
|
50
|
|
|
|
|
} while (ret > 0); |
261
|
|
|
|
|
|
|
RETVAL = output; |
262
|
|
|
|
|
|
|
OUTPUT: |
263
|
|
|
|
|
|
|
RETVAL |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
SV* |
266
|
|
|
|
|
|
|
end(self) |
267
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
268
|
|
|
|
|
|
|
PREINIT: |
269
|
|
|
|
|
|
|
SV* output; |
270
|
|
|
|
|
|
|
size_t ret; |
271
|
|
|
|
|
|
|
CODE: |
272
|
2
|
|
|
|
|
|
output = newSVpv("", 0); |
273
|
|
|
|
|
|
|
do { |
274
|
2
|
|
|
|
|
|
ZSTD_outBuffer outbuf = { self->buf, self->bufsize, 0 }; |
275
|
2
|
|
|
|
|
|
ret = self->status = ZSTD_endStream(self->stream, &outbuf); |
276
|
2
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
277
|
0
|
|
|
|
|
|
croak("%s", ZSTD_getErrorName(ret)); |
278
|
|
|
|
|
|
|
} |
279
|
2
|
|
|
|
|
|
sv_catpvn(output, outbuf.dst, outbuf.pos); |
280
|
2
|
50
|
|
|
|
|
} while (ret > 0); |
281
|
|
|
|
|
|
|
RETVAL = output; |
282
|
|
|
|
|
|
|
OUTPUT: |
283
|
|
|
|
|
|
|
RETVAL |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
size_t |
287
|
|
|
|
|
|
|
status(self) |
288
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
289
|
|
|
|
|
|
|
CODE: |
290
|
0
|
|
|
|
|
|
RETVAL = self->status; |
291
|
|
|
|
|
|
|
OUTPUT: |
292
|
|
|
|
|
|
|
RETVAL |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
unsigned |
295
|
|
|
|
|
|
|
isError(self) |
296
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
297
|
|
|
|
|
|
|
CODE: |
298
|
2
|
|
|
|
|
|
RETVAL = ZSTD_isError(self->status); |
299
|
|
|
|
|
|
|
OUTPUT: |
300
|
|
|
|
|
|
|
RETVAL |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
const char * |
304
|
|
|
|
|
|
|
getErrorName(self) |
305
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
306
|
|
|
|
|
|
|
CODE: |
307
|
0
|
|
|
|
|
|
RETVAL = ZSTD_getErrorName(self->status); |
308
|
|
|
|
|
|
|
OUTPUT: |
309
|
|
|
|
|
|
|
RETVAL |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
void |
312
|
|
|
|
|
|
|
DESTROY(self) |
313
|
|
|
|
|
|
|
Compress::Stream::Zstd::Compressor self; |
314
|
|
|
|
|
|
|
CODE: |
315
|
2
|
|
|
|
|
|
ZSTD_freeCStream(self->stream); |
316
|
2
|
|
|
|
|
|
Safefree(self->buf); |
317
|
2
|
|
|
|
|
|
Safefree(self); |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
MODULE = Compress::Stream::Zstd PACKAGE = Compress::Stream::Zstd::Decompressor |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
BOOT: |
324
|
|
|
|
|
|
|
{ |
325
|
6
|
|
|
|
|
|
HV* stash = gv_stashpv("Compress::Stream::Zstd::Decompressor", 1); |
326
|
6
|
|
|
|
|
|
newCONSTSUB(stash, "ZSTD_DSTREAM_IN_SIZE", newSViv(ZSTD_DStreamInSize())); |
327
|
6
|
|
|
|
|
|
newCONSTSUB(stash, "ZSTD_d_windowLogMax", newSVuv(ZSTD_d_windowLogMax)); |
328
|
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor |
331
|
|
|
|
|
|
|
new(klass) |
332
|
|
|
|
|
|
|
const char* klass; |
333
|
|
|
|
|
|
|
PREINIT: |
334
|
|
|
|
|
|
|
Compress__Stream__Zstd__Decompressor self; |
335
|
|
|
|
|
|
|
char* buf; |
336
|
|
|
|
|
|
|
size_t bufsize; |
337
|
|
|
|
|
|
|
CODE: |
338
|
3
|
|
|
|
|
|
ZSTD_DStream* stream = ZSTD_createDStream(); |
339
|
3
|
50
|
|
|
|
|
if (stream == NULL) { |
340
|
0
|
|
|
|
|
|
croak("Failed to call ZSTD_createDStream()"); |
341
|
|
|
|
|
|
|
} |
342
|
3
|
|
|
|
|
|
ZSTD_initDStream(stream); |
343
|
|
|
|
|
|
|
|
344
|
3
|
|
|
|
|
|
Newx(self, sizeof(struct Compress__Stream__Zstd__Decompressor_s), struct Compress__Stream__Zstd__Decompressor_s); |
345
|
3
|
|
|
|
|
|
self->stream = stream; |
346
|
3
|
|
|
|
|
|
bufsize = ZSTD_DStreamOutSize(); |
347
|
3
|
|
|
|
|
|
Newx(buf, bufsize, char); |
348
|
3
|
|
|
|
|
|
self->buf = buf; |
349
|
3
|
|
|
|
|
|
self->bufsize = bufsize; |
350
|
|
|
|
|
|
|
RETVAL = self; |
351
|
|
|
|
|
|
|
OUTPUT: |
352
|
|
|
|
|
|
|
RETVAL |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
void |
355
|
|
|
|
|
|
|
init(self) |
356
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor self; |
357
|
|
|
|
|
|
|
CODE: |
358
|
0
|
|
|
|
|
|
ZSTD_initDStream(self->stream); |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
void |
361
|
|
|
|
|
|
|
set_parameter(self, dParam, value) |
362
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor self; |
363
|
|
|
|
|
|
|
unsigned long dParam; |
364
|
|
|
|
|
|
|
int value; |
365
|
|
|
|
|
|
|
CODE: |
366
|
1
|
|
|
|
|
|
size_t ret = ZSTD_DCtx_setParameter( self->stream, (ZSTD_dParameter)dParam, value ); |
367
|
1
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
368
|
0
|
|
|
|
|
|
croak("%s", ZSTD_getErrorName(ret)); |
369
|
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
SV* |
372
|
|
|
|
|
|
|
decompress(self, input) |
373
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor self; |
374
|
|
|
|
|
|
|
SV* input; |
375
|
|
|
|
|
|
|
PREINIT: |
376
|
|
|
|
|
|
|
STRLEN len; |
377
|
|
|
|
|
|
|
SV* output; |
378
|
|
|
|
|
|
|
CODE: |
379
|
6
|
50
|
|
|
|
|
const char* in = SvPVbyte(input, len); |
380
|
6
|
|
|
|
|
|
ZSTD_inBuffer inbuf = { in, len, 0 }; |
381
|
6
|
|
|
|
|
|
output = newSVpv("", 0); |
382
|
12
|
100
|
|
|
|
|
while (inbuf.pos < inbuf.size) { |
383
|
6
|
|
|
|
|
|
ZSTD_outBuffer outbuf = { self->buf, self->bufsize, 0 }; |
384
|
6
|
|
|
|
|
|
size_t ret = self->status = ZSTD_decompressStream(self->stream, &outbuf, &inbuf); |
385
|
6
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
386
|
0
|
|
|
|
|
|
croak("%s", ZSTD_getErrorName(ret)); |
387
|
|
|
|
|
|
|
} |
388
|
6
|
|
|
|
|
|
sv_catpvn(output, outbuf.dst, outbuf.pos); |
389
|
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
RETVAL = output; |
391
|
|
|
|
|
|
|
OUTPUT: |
392
|
|
|
|
|
|
|
RETVAL |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
size_t |
395
|
|
|
|
|
|
|
status(self) |
396
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor self; |
397
|
|
|
|
|
|
|
CODE: |
398
|
0
|
|
|
|
|
|
RETVAL = self->status; |
399
|
|
|
|
|
|
|
OUTPUT: |
400
|
|
|
|
|
|
|
RETVAL |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
unsigned |
403
|
|
|
|
|
|
|
isError(self) |
404
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor self; |
405
|
|
|
|
|
|
|
CODE: |
406
|
2
|
|
|
|
|
|
RETVAL = ZSTD_isError(self->status); |
407
|
|
|
|
|
|
|
OUTPUT: |
408
|
|
|
|
|
|
|
RETVAL |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
unsigned |
411
|
|
|
|
|
|
|
isEndFrame(self) |
412
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor self; |
413
|
|
|
|
|
|
|
CODE: |
414
|
2
|
|
|
|
|
|
RETVAL = self->status == 0; |
415
|
|
|
|
|
|
|
OUTPUT: |
416
|
|
|
|
|
|
|
RETVAL |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
const char * |
419
|
|
|
|
|
|
|
getErrorName(self) |
420
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor self; |
421
|
|
|
|
|
|
|
CODE: |
422
|
0
|
|
|
|
|
|
RETVAL = ZSTD_getErrorName(self->status); |
423
|
|
|
|
|
|
|
OUTPUT: |
424
|
|
|
|
|
|
|
RETVAL |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
void |
427
|
|
|
|
|
|
|
DESTROY(self) |
428
|
|
|
|
|
|
|
Compress::Stream::Zstd::Decompressor self; |
429
|
|
|
|
|
|
|
CODE: |
430
|
3
|
|
|
|
|
|
ZSTD_freeDStream(self->stream); |
431
|
3
|
|
|
|
|
|
Safefree(self->buf); |
432
|
3
|
|
|
|
|
|
Safefree(self); |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
MODULE = Compress::Stream::Zstd PACKAGE = Compress::Stream::Zstd::CompressionContext |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
Compress::Stream::Zstd::CompressionContext |
440
|
|
|
|
|
|
|
new(klass) |
441
|
|
|
|
|
|
|
const char* klass; |
442
|
|
|
|
|
|
|
CODE: |
443
|
2
|
|
|
|
|
|
ZSTD_CCtx* cctx = ZSTD_createCCtx(); |
444
|
2
|
50
|
|
|
|
|
if (cctx == NULL) { |
445
|
0
|
|
|
|
|
|
croak("Failed to call ZSTD_createCCtx()"); |
446
|
|
|
|
|
|
|
} |
447
|
|
|
|
|
|
|
RETVAL = (Compress__Stream__Zstd__CompressionContext) cctx; |
448
|
|
|
|
|
|
|
OUTPUT: |
449
|
|
|
|
|
|
|
RETVAL |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
SV* |
452
|
|
|
|
|
|
|
compress(self, source, level = 1) |
453
|
|
|
|
|
|
|
Compress::Stream::Zstd::CompressionContext self; |
454
|
|
|
|
|
|
|
SV* source; |
455
|
|
|
|
|
|
|
int level; |
456
|
|
|
|
|
|
|
PREINIT: |
457
|
|
|
|
|
|
|
const char* src; |
458
|
|
|
|
|
|
|
STRLEN src_len; |
459
|
|
|
|
|
|
|
SV* dest; |
460
|
|
|
|
|
|
|
char* dst; |
461
|
|
|
|
|
|
|
size_t bound, ret; |
462
|
|
|
|
|
|
|
PPCODE: |
463
|
1
|
50
|
|
|
|
|
if (!SvOK(source)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
464
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
465
|
|
|
|
|
|
|
} |
466
|
1
|
50
|
|
|
|
|
src = SvPVbyte(source, src_len); |
467
|
1
|
|
|
|
|
|
bound = ZSTD_compressBound(src_len); |
468
|
1
|
|
|
|
|
|
dest = sv_2mortal(newSV(bound + 1)); |
469
|
1
|
|
|
|
|
|
dst = SvPVX(dest); |
470
|
1
|
|
|
|
|
|
ret = ZSTD_compressCCtx((ZSTD_CCtx*) self, dst, bound + 1, src, src_len, level); |
471
|
1
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
472
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
473
|
|
|
|
|
|
|
} |
474
|
1
|
|
|
|
|
|
dst[ret] = '\0'; |
475
|
1
|
|
|
|
|
|
SvCUR_set(dest, ret); |
476
|
1
|
|
|
|
|
|
SvPOK_on(dest); |
477
|
1
|
50
|
|
|
|
|
EXTEND(SP, 1); |
478
|
1
|
|
|
|
|
|
PUSHs(dest); |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
SV* |
481
|
|
|
|
|
|
|
compress_using_dict(self, source, dict) |
482
|
|
|
|
|
|
|
Compress::Stream::Zstd::CompressionContext self; |
483
|
|
|
|
|
|
|
SV* source; |
484
|
|
|
|
|
|
|
Compress::Stream::Zstd::CompressionDictionary dict; |
485
|
|
|
|
|
|
|
PREINIT: |
486
|
|
|
|
|
|
|
const char* src; |
487
|
|
|
|
|
|
|
STRLEN src_len; |
488
|
|
|
|
|
|
|
SV* dest; |
489
|
|
|
|
|
|
|
char* dst; |
490
|
|
|
|
|
|
|
size_t bound, ret; |
491
|
|
|
|
|
|
|
PPCODE: |
492
|
1
|
50
|
|
|
|
|
if (!SvOK(source)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
493
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
494
|
|
|
|
|
|
|
} |
495
|
1
|
50
|
|
|
|
|
src = SvPVbyte(source, src_len); |
496
|
1
|
|
|
|
|
|
bound = ZSTD_compressBound(src_len); |
497
|
1
|
|
|
|
|
|
dest = sv_2mortal(newSV(bound + 1)); |
498
|
1
|
|
|
|
|
|
dst = SvPVX(dest); |
499
|
1
|
|
|
|
|
|
ret = ZSTD_compress_usingCDict((ZSTD_CCtx*) self, dst, bound + 1, src, src_len, (ZSTD_CDict*) dict); |
500
|
1
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
501
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
502
|
|
|
|
|
|
|
} |
503
|
1
|
|
|
|
|
|
dst[ret] = '\0'; |
504
|
1
|
|
|
|
|
|
SvCUR_set(dest, ret); |
505
|
1
|
|
|
|
|
|
SvPOK_on(dest); |
506
|
1
|
50
|
|
|
|
|
EXTEND(SP, 1); |
507
|
1
|
|
|
|
|
|
PUSHs(dest); |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
void |
510
|
|
|
|
|
|
|
DESTROY(self) |
511
|
|
|
|
|
|
|
Compress::Stream::Zstd::CompressionContext self; |
512
|
|
|
|
|
|
|
CODE: |
513
|
2
|
|
|
|
|
|
ZSTD_freeCCtx((ZSTD_CCtx*) self); |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
MODULE = Compress::Stream::Zstd PACKAGE = Compress::Stream::Zstd::DecompressionContext |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
Compress::Stream::Zstd::DecompressionContext |
521
|
|
|
|
|
|
|
new(klass) |
522
|
|
|
|
|
|
|
const char* klass; |
523
|
|
|
|
|
|
|
CODE: |
524
|
2
|
|
|
|
|
|
ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
525
|
2
|
50
|
|
|
|
|
if (dctx == NULL) { |
526
|
0
|
|
|
|
|
|
croak("Failed to call ZSTD_createDCtx()"); |
527
|
|
|
|
|
|
|
} |
528
|
|
|
|
|
|
|
RETVAL = (Compress__Stream__Zstd__DecompressionContext) dctx; |
529
|
|
|
|
|
|
|
OUTPUT: |
530
|
|
|
|
|
|
|
RETVAL |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
SV* |
533
|
|
|
|
|
|
|
decompress(self, source) |
534
|
|
|
|
|
|
|
Compress::Stream::Zstd::DecompressionContext self; |
535
|
|
|
|
|
|
|
SV* source; |
536
|
|
|
|
|
|
|
ALIAS: |
537
|
|
|
|
|
|
|
uncompress = 1 |
538
|
|
|
|
|
|
|
PREINIT: |
539
|
|
|
|
|
|
|
const char* src; |
540
|
|
|
|
|
|
|
STRLEN src_len; |
541
|
|
|
|
|
|
|
unsigned long long dest_len; |
542
|
|
|
|
|
|
|
SV* dest; |
543
|
|
|
|
|
|
|
char* dst; |
544
|
|
|
|
|
|
|
size_t ret; |
545
|
|
|
|
|
|
|
PPCODE: |
546
|
1
|
50
|
|
|
|
|
if (!SvOK(source)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
547
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
548
|
|
|
|
|
|
|
} |
549
|
1
|
50
|
|
|
|
|
src = SvPVbyte(source, src_len); |
550
|
1
|
|
|
|
|
|
dest_len = ZSTD_getFrameContentSize(src, src_len); |
551
|
1
|
50
|
|
|
|
|
if (dest_len == ZSTD_CONTENTSIZE_UNKNOWN || dest_len == ULLONG_MAX || ZSTD_isError(dest_len)) { |
|
|
50
|
|
|
|
|
|
552
|
|
|
|
|
|
|
/* TODO: Support ZSTD_CONTENTSIZE_UNKNOWN */ |
553
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
554
|
|
|
|
|
|
|
} |
555
|
1
|
|
|
|
|
|
dest = sv_2mortal(newSV(dest_len + 1)); |
556
|
1
|
|
|
|
|
|
dst = SvPVX(dest); |
557
|
1
|
|
|
|
|
|
ret = ZSTD_decompressDCtx((ZSTD_DCtx*) self, dst, dest_len + 1, src, src_len); |
558
|
1
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
559
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
560
|
|
|
|
|
|
|
} |
561
|
1
|
|
|
|
|
|
dst[ret] = '\0'; |
562
|
1
|
|
|
|
|
|
SvCUR_set(dest, ret); |
563
|
1
|
|
|
|
|
|
SvPOK_on(dest); |
564
|
1
|
50
|
|
|
|
|
EXTEND(SP, 1); |
565
|
1
|
|
|
|
|
|
PUSHs(dest); |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
SV* |
568
|
|
|
|
|
|
|
decompress_using_dict(self, source, dict) |
569
|
|
|
|
|
|
|
Compress::Stream::Zstd::DecompressionContext self; |
570
|
|
|
|
|
|
|
SV* source; |
571
|
|
|
|
|
|
|
Compress::Stream::Zstd::DecompressionDictionary dict; |
572
|
|
|
|
|
|
|
PREINIT: |
573
|
|
|
|
|
|
|
const char* src; |
574
|
|
|
|
|
|
|
STRLEN src_len; |
575
|
|
|
|
|
|
|
unsigned long long dest_len; |
576
|
|
|
|
|
|
|
SV* dest; |
577
|
|
|
|
|
|
|
char* dst; |
578
|
|
|
|
|
|
|
size_t ret; |
579
|
|
|
|
|
|
|
PPCODE: |
580
|
1
|
50
|
|
|
|
|
if (!SvOK(source)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
581
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
582
|
|
|
|
|
|
|
} |
583
|
1
|
50
|
|
|
|
|
src = SvPVbyte(source, src_len); |
584
|
1
|
|
|
|
|
|
dest_len = ZSTD_getFrameContentSize(src, src_len); |
585
|
1
|
50
|
|
|
|
|
if (dest_len == ZSTD_CONTENTSIZE_UNKNOWN || dest_len == ULLONG_MAX || ZSTD_isError(dest_len)) { |
|
|
50
|
|
|
|
|
|
586
|
|
|
|
|
|
|
/* TODO: Support ZSTD_CONTENTSIZE_UNKNOWN */ |
587
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
588
|
|
|
|
|
|
|
} |
589
|
1
|
|
|
|
|
|
dest = sv_2mortal(newSV(dest_len + 1)); |
590
|
1
|
|
|
|
|
|
dst = SvPVX(dest); |
591
|
1
|
|
|
|
|
|
ret = ZSTD_decompress_usingDDict((ZSTD_DCtx*) self, dst, dest_len + 1, src, src_len, (ZSTD_DDict*) dict); |
592
|
1
|
50
|
|
|
|
|
if (ZSTD_isError(ret)) { |
593
|
0
|
|
|
|
|
|
XSRETURN_UNDEF; |
594
|
|
|
|
|
|
|
} |
595
|
1
|
|
|
|
|
|
dst[ret] = '\0'; |
596
|
1
|
|
|
|
|
|
SvCUR_set(dest, ret); |
597
|
1
|
|
|
|
|
|
SvPOK_on(dest); |
598
|
1
|
50
|
|
|
|
|
EXTEND(SP, 1); |
599
|
1
|
|
|
|
|
|
PUSHs(dest); |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
void |
602
|
|
|
|
|
|
|
DESTROY(self) |
603
|
|
|
|
|
|
|
Compress::Stream::Zstd::DecompressionContext self; |
604
|
|
|
|
|
|
|
CODE: |
605
|
2
|
|
|
|
|
|
ZSTD_freeDCtx((ZSTD_DCtx*) self); |
606
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
MODULE = Compress::Stream::Zstd PACKAGE = Compress::Stream::Zstd::CompressionDictionary |
608
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
Compress::Stream::Zstd::CompressionDictionary |
612
|
|
|
|
|
|
|
new(klass, dict, level = 1) |
613
|
|
|
|
|
|
|
const char* klass; |
614
|
|
|
|
|
|
|
SV* dict; |
615
|
|
|
|
|
|
|
int level; |
616
|
|
|
|
|
|
|
PREINIT: |
617
|
|
|
|
|
|
|
ZSTD_CDict* cdict; |
618
|
|
|
|
|
|
|
const char* dct; |
619
|
|
|
|
|
|
|
size_t dct_len; |
620
|
|
|
|
|
|
|
CODE: |
621
|
1
|
50
|
|
|
|
|
dct = SvPVbyte(dict, dct_len); |
622
|
1
|
|
|
|
|
|
cdict = ZSTD_createCDict(dct, dct_len, level); |
623
|
1
|
50
|
|
|
|
|
if (cdict == NULL) { |
624
|
0
|
|
|
|
|
|
croak("Failed to call ZSTD_createCDict()"); |
625
|
|
|
|
|
|
|
} |
626
|
|
|
|
|
|
|
RETVAL = (Compress__Stream__Zstd__CompressionDictionary) cdict; |
627
|
|
|
|
|
|
|
OUTPUT: |
628
|
|
|
|
|
|
|
RETVAL |
629
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
void |
631
|
|
|
|
|
|
|
DESTROY(self) |
632
|
|
|
|
|
|
|
Compress::Stream::Zstd::CompressionDictionary self; |
633
|
|
|
|
|
|
|
CODE: |
634
|
1
|
|
|
|
|
|
ZSTD_freeCDict((ZSTD_CDict*) self); |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
MODULE = Compress::Stream::Zstd PACKAGE = Compress::Stream::Zstd::DecompressionDictionary |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
Compress::Stream::Zstd::DecompressionDictionary |
641
|
|
|
|
|
|
|
new(klass, dict) |
642
|
|
|
|
|
|
|
const char* klass; |
643
|
|
|
|
|
|
|
SV* dict; |
644
|
|
|
|
|
|
|
PREINIT: |
645
|
|
|
|
|
|
|
ZSTD_DDict* ddict; |
646
|
|
|
|
|
|
|
const char* dct; |
647
|
|
|
|
|
|
|
size_t dct_len; |
648
|
|
|
|
|
|
|
CODE: |
649
|
1
|
50
|
|
|
|
|
dct = SvPVbyte(dict, dct_len); |
650
|
1
|
|
|
|
|
|
ddict = ZSTD_createDDict(dct, dct_len); |
651
|
1
|
50
|
|
|
|
|
if (ddict == NULL) { |
652
|
0
|
|
|
|
|
|
croak("Failed to call ZSTD_createDDict()"); |
653
|
|
|
|
|
|
|
} |
654
|
|
|
|
|
|
|
RETVAL = (Compress__Stream__Zstd__DecompressionDictionary) ddict; |
655
|
|
|
|
|
|
|
OUTPUT: |
656
|
|
|
|
|
|
|
RETVAL |
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
void |
659
|
|
|
|
|
|
|
DESTROY(self) |
660
|
|
|
|
|
|
|
Compress::Stream::Zstd::DecompressionDictionary self; |
661
|
|
|
|
|
|
|
CODE: |
662
|
1
|
|
|
|
|
|
ZSTD_freeDDict((ZSTD_DDict*) self); |