srl_compress.h

filter: Statementstmt 6.9% 9 / 130 Branchbran 5.1% 4 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 6.2% 13 / 208 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_str_dedupe.h ↑ Summary srl_encoder.c »
1 #ifndef SRL_COMPRESS_H_
2 #define SRL_COMPRESS_H_
3  
4 #include "srl_buffer.h"
5 #include "srl_inline.h"
6 #include "srl_protocol.h"
7 #include "srl_buffer_types.h"
8  
9 /* WARNING: This is different from the protocol bit SRL_PROTOCOL_ENCODING_SNAPPY
10 * and SRL_PROTOCOL_ENCODING_ZLIB in that it's a flag indicating that
11 * we want to use Snappy or Zlib.
12 *
13 * DO NOT CHANGE THIS WITHOUT REVIEWING THE BITS IN srl_encoder.h and etc.
14 */
15  
16 #define SRL_F_COMPRESS_SNAPPY 0x00040UL
17 #define SRL_F_COMPRESS_SNAPPY_INCREMENTAL 0x00080UL
18 #define SRL_F_COMPRESS_ZLIB 0x00100UL
19 #define SRL_F_COMPRESS_ZSTD 0x40000UL
20 /* WARNING: IF ADDING NEW COMPRESSION MAKE SURE THAT NEW CONSTANT DOES NOT
21 * COLLIDE WITH CONSTANTS IN srl_encoder.h!
22 */
23  
24 #define SRL_F_COMPRESS_FLAGS_MASK (SRL_F_COMPRESS_SNAPPY | \
25 SRL_F_COMPRESS_SNAPPY_INCREMENTAL | \
26 SRL_F_COMPRESS_ZLIB | \
27 SRL_F_COMPRESS_ZSTD)
28  
29 #if defined(HAVE_CSNAPPY)
30 #include <csnappy.h>
31 #else
32 #include "snappy/csnappy_compress.c"
33 #endif
34  
35 #if defined(HAVE_MINIZ)
36 #include <miniz.h>
37 #else
38 #include "miniz.h"
39 #endif
40  
41 #if defined(HAVE_ZSTD)
42 #include <zstd.h>
43 #else
44 #include "zstd/zstd.h"
45 #endif
46  
47 /* Include the space reserved for the compressed-length varints. Compute in
48 * a wide type because both bundled Snappy and miniz can wrap their 32-bit
49 * bounds. U64 is absent on some 32-bit Perls even when C has long long. */
50 #ifdef HAS_LONG_LONG
51 typedef unsigned long long srl_compress_bound_t;
52 #else
53 typedef U64 srl_compress_bound_t;
54 #endif
55  
56 SRL_STATIC_INLINE srl_compress_bound_t
57 0 srl_compress_bound(STRLEN source_len, U32 compress_flags)
58 {
59 0 const srl_compress_bound_t len = (srl_compress_bound_t)source_len;
60  
61 0 ▶ if (compress_flags & (SRL_F_COMPRESS_SNAPPY | SRL_F_COMPRESS_SNAPPY_INCREMENTAL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (compress_flags & (SRL_F_COMPRESS_SNAPPY | SRL_F_COMPRESS_SNAPPY_INCREMENTAL)) { 0 0 0
62 0 return 32 + len + len / 6
63 0 ▶ + ((compress_flags & SRL_F_COMPRESS_SNAPPY_INCREMENTAL) ? SRL_MAX_VARINT_LENGTH : 0);
Branch: 1 branch
BranchTrueFalseTotal
+ ((compress_flags & SRL_F_COMPRESS_SNAPPY_INCREMENTAL) ? SRL_MAX_VARINT_LENGTH : 0); 0 0 0
64 0 ▶ } else if (!(compress_flags & SRL_F_COMPRESS_ZSTD)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!(compress_flags & SRL_F_COMPRESS_ZSTD)) { 0 0 0
65 /* Mirror miniz's two bounds in a wide type, and allow an external
66 * miniz implementation to return a larger bound. */
67 0 const srl_compress_bound_t bound1 = 128 + (len * 110) / 100;
68 0 const srl_compress_bound_t bound2 = 128 + len + ((len / (31 * 1024)) + 1) * 5;
69 const srl_compress_bound_t miniz_bound =
70 0 (srl_compress_bound_t)mz_compressBound((mz_ulong)source_len);
71 0 const srl_compress_bound_t bound = bound1 > bound2 ? bound1 : bound2;
72 0 return (bound > miniz_bound ? bound : miniz_bound) + 2 * SRL_MAX_VARINT_LENGTH;
73 }
74 0 return 0; /* zstd uses a size_t bound and has no 32-bit output limit */
75 }
76  
77 /* Update a varint anywhere in the output stream with defined start and end
78 * positions. This can produce non-canonical varints and is useful for filling
79 * pre-allocated varints. */
80 SRL_STATIC_INLINE void
81 0 srl_update_varint_from_to(pTHX_ unsigned char *varint_start, unsigned char *varint_end, UV number)
82 {
83 0 ▶ while (number >= 0x80) { /* while we are larger than 7 bits long */
Branch: 1 branch
BranchTrueFalseTotal
while (number >= 0x80) { /* while we are larger than 7 bits long */ 0 0 0
84 0 *varint_start++ = (number & 0x7f) | 0x80; /* write out the least significant 7 bits, set the high bit */
85 0 number = number >> 7; /* shift off the 7 least significant bits */
86 }
87  
88 /* if it is the same size we can use a canonical varint */
89 0 ▶ if ( varint_start == varint_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( varint_start == varint_end ) { 0 0 0
90 0 *varint_start = number; /* encode the last 7 bits without the high bit being set */
91 } else {
92 /* if not we produce a non-canonical varint, basically we stuff
93 * 0 bits (via 0x80) into the "tail" of the varint, until we can
94 * stick in a null to terminate the sequence. This means that the
95 * varint is effectively "self-padding", and we only need special
96 * logic in the encoder - a decoder will happily process a non-canonical
97 * varint with no problem */
98 0 *varint_start++ = (number & 0x7f) | 0x80;
99 0 ▶ while ( varint_start < varint_end )
Branch: 1 branch
BranchTrueFalseTotal
while ( varint_start < varint_end ) 0 0 0
100 0 *varint_start++ = 0x80;
101 0 *varint_start= 0;
102 }
103 0 }
104  
105 /* Lazy working buffer alloc */
106 SRL_STATIC_INLINE void
107 0 srl_init_snappy_workmem(pTHX_ void **workmem)
108 {
109 /* Lazy working buffer alloc */
110 0 ▶ if (expect_false(*workmem == NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(*workmem == NULL)) { 0 0 0
111 /* Cleaned up automatically by the cleanup handler */
112 0 Newx(*workmem, CSNAPPY_WORKMEM_BYTES, char);
113 0 ▶ if (*workmem == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (*workmem == NULL) 0 0 0
114 0 croak("Out of memory!");
115 }
116 0 }
117  
118 /* Destroy working buffer */
119 SRL_STATIC_INLINE void
120 7 srl_destroy_snappy_workmem(pTHX_ void *workmem)
121 {
122 7 Safefree(workmem);
123 7 }
124  
125 SRL_STATIC_INLINE U8
126 7 srl_get_compression_header_flag(const U32 compress_flags)
127 {
128 7 ▶ if (compress_flags & SRL_F_COMPRESS_SNAPPY) {
Branch: 1 branch
BranchTrueFalseTotal
if (compress_flags & SRL_F_COMPRESS_SNAPPY) { 0 7 7
129 0 return SRL_PROTOCOL_ENCODING_SNAPPY;
130 7 ▶ } else if (compress_flags & SRL_F_COMPRESS_SNAPPY_INCREMENTAL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (compress_flags & SRL_F_COMPRESS_SNAPPY_INCREMENTAL) { 0 7 7
131 0 return SRL_PROTOCOL_ENCODING_SNAPPY_INCREMENTAL;
132 7 ▶ } else if (compress_flags & SRL_F_COMPRESS_ZLIB) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (compress_flags & SRL_F_COMPRESS_ZLIB) { 0 7 7
133 0 return SRL_PROTOCOL_ENCODING_ZLIB;
134 7 ▶ } else if (compress_flags & SRL_F_COMPRESS_ZSTD) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (compress_flags & SRL_F_COMPRESS_ZSTD) { 0 7 7
135 0 return SRL_PROTOCOL_ENCODING_ZSTD;
136 } else {
137 7 return SRL_PROTOCOL_ENCODING_RAW;
138 }
139 }
140  
141 /* Sets the compression header flag */
142 SRL_STATIC_INLINE void
143 0 srl_set_compression_header_flag(srl_buffer_t *buf, const U32 compress_flags)
144 {
145 /* sizeof(const char *) includes a count of \0 */
146 0 srl_buffer_char *flags_and_version_byte = buf->start + sizeof(SRL_MAGIC_STRING) - 1;
147 0 *flags_and_version_byte |= srl_get_compression_header_flag(compress_flags);
148 0 }
149  
150 /* Resets the compression header flag to OFF.
151 * Obviously requires that a Sereal header was already written to the
152 * encoder's output buffer. */
153 SRL_STATIC_INLINE void
154 0 srl_reset_compression_header_flag(srl_buffer_t *buf)
155 {
156 /* sizeof(const char *) includes a count of \0 */
157 0 srl_buffer_char *flags_and_version_byte = buf->start + sizeof(SRL_MAGIC_STRING) - 1;
158  
159 /* disable snappy flag in header */
160 0 *flags_and_version_byte = SRL_PROTOCOL_ENCODING_RAW |
161 0 (*flags_and_version_byte & SRL_PROTOCOL_VERSION_MASK);
162 0 }
163  
164 /* Cap payload size for retaining a ZSTD_CCtx so an occasional huge packet
165 * does not pin multi-MB compression tables on a reusable encoder. */
166 #define SRL_ZSTD_CCTX_REUSE_MAX_LEN (512 * 1024)
167  
168 /* Compress body with one of available compressors (zlib, zstd, snappy).
169 * The function sets/resets compression bits at version byte.
170 * The caller has to adjust buf->body_pos by calling SRL_UPDATE_BODY_POS
171 * right after exiting from srl_compress_body.
172 */
173  
174 SRL_STATIC_INLINE void
175 0 srl_compress_body_ctx(pTHX_ srl_buffer_t *buf, STRLEN sereal_header_length,
176 const U32 compress_flags, const int compress_level,
177 void **workmem, void **zstd_cctx)
178 {
179 0 const int is_traditional_snappy = compress_flags & SRL_F_COMPRESS_SNAPPY;
180 0 const int is_incremental_snappy = compress_flags & SRL_F_COMPRESS_SNAPPY_INCREMENTAL;
181 0 const int is_zstd = compress_flags & SRL_F_COMPRESS_ZSTD;
182 0 ▶ const int is_zlib = !is_traditional_snappy && !is_incremental_snappy && !is_zstd;
Branch: 3 branches
BranchTrueFalseTotal
const int is_zlib = !is_traditional_snappy && !is_incremental_snappy && !is_zstd; 0 0 0
const int is_zlib = !is_traditional_snappy && !is_incremental_snappy && !is_zstd; 0 0 0
const int is_zlib = !is_traditional_snappy && !is_incremental_snappy && !is_zstd; 0 0 0
183  
184 0 size_t uncompressed_body_length = BUF_POS_OFS(buf) - sereal_header_length;
185 size_t compressed_body_length;
186 size_t dst_capacity;
187 0 srl_buffer_char *varint_start = NULL;
188 0 srl_buffer_char *varint_end = NULL;
189 srl_buffer_t old_buf;
190  
191 DEBUG_ASSERT_BUF_SANE(buf);
192  
193 0 ▶ if (uncompressed_body_length > (size_t)U32_MAX
Branch: 1 branch
BranchTrueFalseTotal
if (uncompressed_body_length > (size_t)U32_MAX 0 0 0
194 0 ▶ || srl_compress_bound(uncompressed_body_length, compress_flags) > U32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
|| srl_compress_bound(uncompressed_body_length, compress_flags) > U32_MAX) { 0 0 0
195 0 warn("disabling compression as buffer is too large!");
196 0 srl_reset_compression_header_flag(buf);
197 0 return;
198 }
199  
200 /* Get estimated compressed payload length */
201 0 ▶ if (is_incremental_snappy || is_traditional_snappy) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_incremental_snappy || is_traditional_snappy) { 0 0 0
if (is_incremental_snappy || is_traditional_snappy) { 0 0 0
202 0 compressed_body_length = (size_t)srl_compress_bound(uncompressed_body_length, compress_flags);
203 0 ▶ } else if (is_zstd) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zstd) { 0 0 0
204 0 compressed_body_length = ZSTD_compressBound(uncompressed_body_length);
205 0 ▶ if (compressed_body_length > (size_t)~(size_t)0 - SRL_MAX_VARINT_LENGTH)
Branch: 1 branch
BranchTrueFalseTotal
if (compressed_body_length > (size_t)~(size_t)0 - SRL_MAX_VARINT_LENGTH) 0 0 0
206 0 croak("Compressed Sereal packet is too large");
207 0 compressed_body_length += SRL_MAX_VARINT_LENGTH; /* will have to embed compressed packet length as varint */
208 } else {
209 0 compressed_body_length = (size_t)srl_compress_bound(uncompressed_body_length, compress_flags);
210 }
211  
212 0 ▶ if (sereal_header_length >= (STRLEN)~(STRLEN)0 - compressed_body_length)
Branch: 1 branch
BranchTrueFalseTotal
if (sereal_header_length >= (STRLEN)~(STRLEN)0 - compressed_body_length) 0 0 0
213 0 croak("Compressed Sereal packet is too large");
214  
215 /* Back up old buffer and allocate new one with correct size */
216 0 srl_buf_copy_buffer(aTHX_ buf, &old_buf);
217 0 ▶ if (expect_false(srl_buf_init_buffer(aTHX_ buf, sereal_header_length + compressed_body_length + 1) != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(srl_buf_init_buffer(aTHX_ buf, sereal_header_length + compressed_body_length + 1) != 0)) { 0 0 0
218 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
219 0 srl_buf_free_buffer(aTHX_ &old_buf);
220 0 croak("Out of memory");
221 }
222  
223 /* Copy Sereal header */
224 0 Copy(old_buf.start, buf->pos, sereal_header_length, char);
225 0 buf->pos += sereal_header_length;
226  
227 /* Embed uncompressed packet length if Zlib */
228 0 ▶ if (is_zlib) srl_buf_cat_varint_nocheck(aTHX_ buf, 0, uncompressed_body_length);
Branch: 1 branch
BranchTrueFalseTotal
if (is_zlib) srl_buf_cat_varint_nocheck(aTHX_ buf, 0, uncompressed_body_length); 0 0 0
229  
230 /* Embed compressed packet length if incr. Snappy, Zlib or Zstd*/
231 0 ▶ if (is_incremental_snappy || is_zlib || is_zstd) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_incremental_snappy || is_zlib || is_zstd) { 0 0 0
if (is_incremental_snappy || is_zlib || is_zstd) { 0 0 0
if (is_incremental_snappy || is_zlib || is_zstd) { 0 0 0
232 0 varint_start = buf->pos;
233 0 srl_buf_cat_varint_nocheck(aTHX_ buf, 0, compressed_body_length);
234 0 varint_end = buf->pos - 1;
235 }
236  
237 assert(BUF_SPACE(buf) >= 0);
238 0 dst_capacity = (size_t)BUF_SPACE(buf);
239  
240 0 ▶ if (is_incremental_snappy || is_traditional_snappy) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_incremental_snappy || is_traditional_snappy) { 0 0 0
if (is_incremental_snappy || is_traditional_snappy) { 0 0 0
241 0 uint32_t len = (uint32_t) dst_capacity;
242 0 srl_init_snappy_workmem(aTHX_ workmem);
243  
244 0 csnappy_compress((char*) (old_buf.start + sereal_header_length), (uint32_t) uncompressed_body_length,
245 0 (char*) buf->pos, &len, *workmem, CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
246  
247 0 compressed_body_length = (size_t) len;
248 0 ▶ } else if (is_zstd) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zstd) { 0 0 0
249 size_t code;
250  
251 0 ▶ if (zstd_cctx != NULL
Branch: 1 branch
BranchTrueFalseTotal
if (zstd_cctx != NULL 0 0 0
252 0 ▶ && uncompressed_body_length <= (size_t)SRL_ZSTD_CCTX_REUSE_MAX_LEN) {
Branch: 1 branch
BranchTrueFalseTotal
&& uncompressed_body_length <= (size_t)SRL_ZSTD_CCTX_REUSE_MAX_LEN) { 0 0 0
253 0 ▶ if (expect_false(*zstd_cctx == NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(*zstd_cctx == NULL)) { 0 0 0
254 0 *zstd_cctx = (void *)ZSTD_createCCtx();
255 0 ▶ if (expect_false(*zstd_cctx == NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(*zstd_cctx == NULL)) { 0 0 0
256 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
257 0 srl_buf_free_buffer(aTHX_ &old_buf);
258 0 croak("Out of memory allocating Zstd compression context");
259 }
260 }
261 0 code = ZSTD_compressCCtx((ZSTD_CCtx *)*zstd_cctx,
262 0 (void *)buf->pos, dst_capacity,
263 0 (const void *)(old_buf.start + sereal_header_length),
264 uncompressed_body_length, compress_level);
265 } else {
266 0 code = ZSTD_compress((void *)buf->pos, dst_capacity,
267 0 (const void *)(old_buf.start + sereal_header_length),
268 uncompressed_body_length, compress_level);
269 }
270  
271 0 ▶ if (ZSTD_isError(code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ZSTD_isError(code)) { 0 0 0
272 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
273 0 srl_buf_free_buffer(aTHX_ &old_buf);
274 0 croak("Zstd compression failed: %s", ZSTD_getErrorName(code));
275 }
276 0 compressed_body_length = code;
277 0 ▶ } else if (is_zlib) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zlib) { 0 0 0
278 0 mz_ulong dl = (mz_ulong) dst_capacity;
279 0 int status = mz_compress2(
280 0 buf->pos,
281 &dl,
282 0 old_buf.start + sereal_header_length,
283 (mz_ulong) uncompressed_body_length,
284 compress_level
285 );
286  
287 0 ▶ if (status != Z_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != Z_OK) { 0 0 0
288 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
289 0 srl_buf_free_buffer(aTHX_ &old_buf);
290 0 croak("Zlib compression failed with error %d", status);
291 }
292 0 compressed_body_length = (size_t) dl;
293 }
294  
295 assert(compressed_body_length != 0);
296  
297 /* If compression didn't help, swap back to old, uncompressed buffer */
298 0 ▶ if (compressed_body_length >= uncompressed_body_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (compressed_body_length >= uncompressed_body_length) { 0 0 0
299 /* swap in old, uncompressed buffer */
300 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
301 /* disable compression flag */
302 0 srl_reset_compression_header_flag(buf);
303 } else { /* go ahead with Snappy and do final fixups */
304 /* overwrite the max size varint with the real size of the compressed data */
305 0 ▶ if (varint_start)
Branch: 1 branch
BranchTrueFalseTotal
if (varint_start) 0 0 0
306 0 srl_update_varint_from_to(aTHX_ varint_start, varint_end, compressed_body_length);
307  
308 0 buf->pos += compressed_body_length;
309  
310 /* enable compression flag */
311 0 srl_set_compression_header_flag(buf, compress_flags);
312 }
313  
314 0 srl_buf_free_buffer(aTHX_ &old_buf);
315 DEBUG_ASSERT_BUF_SANE(buf);
316 }
317  
318 SRL_STATIC_INLINE void
319 srl_compress_body(pTHX_ srl_buffer_t *buf, STRLEN sereal_header_length,
320 const U32 compress_flags, const int compress_level, void **workmem)
321 {
322 srl_compress_body_ctx(aTHX_ buf, sereal_header_length, compress_flags, compress_level, workmem, NULL);
323 }
324  
325 #endif
« srl_str_dedupe.h ↑ Summary srl_encoder.c »