-
Notifications
You must be signed in to change notification settings - Fork 1
/
CompressLZ4Frame.xs
255 lines (219 loc) · 7.54 KB
/
CompressLZ4Frame.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "lz4frame.h"
#include "lz4frame_static.h"
enum { CHUNK_SIZE = 65536 }; // 64 KiB
SV * decompress_single_frame(pTHX_ char * src, size_t src_len, size_t * bytes_processed)
{
size_t result, bytes_read, dest_len;
LZ4F_decompressionContext_t ctx;
LZ4F_frameInfo_t info;
SV * decompressed = NULL;
char * dest = NULL;
*bytes_processed = 0u;
result = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
if (LZ4F_isError(result)) {
warn("Could not create decompression context: %s", LZ4F_getErrorName(result));
return NULL;
}
bytes_read = src_len;
result = LZ4F_getFrameInfo(ctx, &info, src, &bytes_read);
if (LZ4F_isError(result)) {
warn("Could not read frame info: %s", LZ4F_getErrorName(result));
LZ4F_freeDecompressionContext(ctx);
return NULL;
}
*bytes_processed += bytes_read;
src_len -= bytes_read;
if (info.contentSize)
{
// content size header has a value
dest_len = (size_t)info.contentSize;
decompressed = newSV(dest_len);
dest = SvPVX(decompressed);
if (!dest) {
warn("Could not allocate enough memory (%zu Bytes)", dest_len);
LZ4F_freeDecompressionContext(ctx);
SvREFCNT_dec(decompressed);
return NULL;
}
result = LZ4F_decompress(ctx, dest, &dest_len, src + bytes_read, &src_len, NULL);
LZ4F_freeDecompressionContext(ctx);
if (LZ4F_isError(result)) {
warn("Error during decompression: %s", LZ4F_getErrorName(result));
SvREFCNT_dec(decompressed);
return NULL;
}
*bytes_processed += src_len;
SvCUR_set(decompressed, dest_len);
SvPOK_on(decompressed);
}
else
{
// content size header is 0 => decompress in chunks
size_t dest_offset = 0u, src_offset = bytes_read, current_chunk = CHUNK_SIZE;
dest_len = CHUNK_SIZE;
Newx(dest, dest_len, char);
for (;;)
{
bytes_read = src_len;
if (!dest) {
warn("Could not allocate enough memory (%zu Bytes)", dest_len);
LZ4F_freeDecompressionContext(ctx);
return NULL;
}
result = LZ4F_decompress(ctx, dest + dest_offset, ¤t_chunk, src + src_offset, &bytes_read, NULL);
if (LZ4F_isError(result) || !current_chunk) {
if (LZ4F_isError(result))
warn("Error during decompression: %s", LZ4F_getErrorName(result));
Safefree(dest);
LZ4F_freeDecompressionContext(ctx);
return NULL;
}
// bytes_processed is relevant for concatenated frames
*bytes_processed += bytes_read;
// current_chunk contains how much was read
// dest_offset is where the current chunk started
// result contains the number of bytes that LZ4F is still expecting
// in combination this should be the full new size of the destination buffer
dest_len = dest_offset + current_chunk + result;
if (!result) // 0 means no more data in this frame
break;
// where the next chunk will be read to
dest_offset += current_chunk;
// the size of the next chunk
current_chunk = result;
// how much is left to read from the source buffer
src_len -= bytes_read;
// where to read from
src_offset += bytes_read;
Renew(dest, dest_len, char);
}
// done uncompressing, now put the stuff into a scalar
decompressed = newSV(0);
sv_usepvn_flags(decompressed, dest, dest_len, SV_SMAGIC);
LZ4F_freeDecompressionContext(ctx);
}
return decompressed;
}
MODULE = Compress::LZ4Frame PACKAGE = Compress::LZ4Frame
PROTOTYPES: ENABLE
SV *
compress(sv, level = 0)
SV * sv
int level
ALIAS:
compress_checksum = 1
PREINIT:
LZ4F_preferences_t prefs = { 0 };
char * src, * dest;
size_t src_len, dest_len;
CODE:
SvGETMAGIC(sv);
if (SvROK(sv) && !SvAMAGIC(sv)) {
sv = SvRV(sv);
SvGETMAGIC(sv);
}
if (!SvOK(sv))
XSRETURN_NO;
src = SvPVbyte(sv, src_len);
if (!src_len)
XSRETURN_NO;
prefs.frameInfo.contentChecksumFlag = (ix == 1 ? LZ4F_contentChecksumEnabled : LZ4F_noContentChecksum);
prefs.frameInfo.contentSize = (unsigned long long)src_len;
prefs.compressionLevel = level;
prefs.autoFlush = 1u;
dest_len = LZ4F_compressFrameBound(src_len, &prefs);
RETVAL = newSV(dest_len);
dest = SvPVX(RETVAL);
if (!dest) {
warn("Could not allocate enough memory (%zu Bytes)", dest_len);
SvREFCNT_dec(RETVAL);
XSRETURN_UNDEF;
}
dest_len = LZ4F_compressFrame(dest, dest_len, src, src_len, &prefs);
if (LZ4F_isError(dest_len)) {
warn("Error during compression: %s", LZ4F_getErrorName(dest_len));
SvREFCNT_dec(RETVAL);
XSRETURN_UNDEF;
}
SvCUR_set(RETVAL, dest_len);
SvPOK_on(RETVAL);
OUTPUT:
RETVAL
SV *
decompress(sv)
SV * sv
PREINIT:
char * src;
size_t src_len, bytes_read;
SV * current = (SV*)1; /* simply not NULL */
CODE:
SvGETMAGIC(sv);
if (SvROK(sv) && !SvAMAGIC(sv)) {
sv = SvRV(sv);
SvGETMAGIC(sv);
}
if (!SvOK(sv))
XSRETURN_NO;
src = SvPVbyte(sv, src_len);
if (!src_len)
XSRETURN_NO;
RETVAL = decompress_single_frame(aTHX_ src, src_len, &bytes_read);
if (RETVAL == NULL)
XSRETURN_UNDEF;
src += bytes_read;
src_len = src_len >= bytes_read ? src_len - bytes_read : 0u;
while (src_len && (current = decompress_single_frame(aTHX_ src, src_len, &bytes_read)) && (bytes_read > 0))
{
sv_catsv(RETVAL, current);
SvREFCNT_dec(current);
src += bytes_read;
src_len = src_len >= bytes_read ? src_len - bytes_read : 0u;
}
if (current == NULL)
{
SvREFCNT_dec(RETVAL);
XSRETURN_UNDEF;
}
OUTPUT:
RETVAL
int
looks_like_lz4frame(sv)
SV * sv
PREINIT:
LZ4F_decompressionContext_t ctx;
LZ4F_frameInfo_t info;
char * src;
size_t src_len;
size_t result;
CODE:
SvGETMAGIC(sv);
if (SvROK(sv) && !SvAMAGIC(sv)) {
sv = SvRV(sv);
SvGETMAGIC(sv);
}
if (!SvOK(sv))
XSRETURN_NO;
src = SvPVbyte(sv, src_len);
if (!src_len)
XSRETURN_NO;
result = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
if (LZ4F_isError(result)) {
warn("Could not create decompression context: %s", LZ4F_getErrorName(result));
XSRETURN_UNDEF;
}
result = LZ4F_getFrameInfo(ctx, &info, src, &src_len);
if (LZ4F_isError(result)) {
/*
* No warning: we actually just wanted to check if this is valid LZ4 Frame data
* warn("Could not read frame info: %s", LZ4F_getErrorName(result));
*/
LZ4F_freeDecompressionContext(ctx);
XSRETURN_NO;
}
LZ4F_freeDecompressionContext(ctx);
XSRETURN_YES;