srl_compress.h

filter: Statementstmt 7.8% 9 / 115 Branchbran 5.8% 4 / 68 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 7.1% 13 / 183 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_float.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 /* Compress body with one of available compressors (zlib, snappy).
165 * The function sets/resets compression bits at version byte.
166 * The caller has to adjust buf->body_pos by calling SRL_UPDATE_BODY_POS
167 * right after exiting from srl_compress_body.
168 */
169  
170 SRL_STATIC_INLINE void
171 0 srl_compress_body(pTHX_ srl_buffer_t *buf, STRLEN sereal_header_length,
172 const U32 compress_flags, const int compress_level, void **workmem)
173 {
174 0 const int is_traditional_snappy = compress_flags & SRL_F_COMPRESS_SNAPPY;
175 0 const int is_incremental_snappy = compress_flags & SRL_F_COMPRESS_SNAPPY_INCREMENTAL;
176 0 const int is_zstd = compress_flags & SRL_F_COMPRESS_ZSTD;
177 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
178  
179 0 size_t uncompressed_body_length = BUF_POS_OFS(buf) - sereal_header_length;
180 size_t compressed_body_length;
181 0 srl_buffer_char *varint_start = NULL;
182 0 srl_buffer_char *varint_end = NULL;
183 srl_buffer_t old_buf;
184  
185 DEBUG_ASSERT_BUF_SANE(buf);
186  
187 0 ▶ if (uncompressed_body_length > (size_t)U32_MAX
Branch: 1 branch
BranchTrueFalseTotal
if (uncompressed_body_length > (size_t)U32_MAX 0 0 0
188 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
189 0 warn("disabling compression as buffer is too large!");
190 0 srl_reset_compression_header_flag(buf);
191 0 return;
192 }
193  
194 /* Get estimated compressed payload length */
195 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
196 0 compressed_body_length = (size_t)srl_compress_bound(uncompressed_body_length, compress_flags);
197 0 ▶ } else if (is_zstd) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zstd) { 0 0 0
198 0 compressed_body_length = ZSTD_compressBound(uncompressed_body_length);
199 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
200 0 croak("Compressed Sereal packet is too large");
201 0 compressed_body_length += SRL_MAX_VARINT_LENGTH; /* will have to embed compressed packet length as varint */
202 } else {
203 0 compressed_body_length = (size_t)srl_compress_bound(uncompressed_body_length, compress_flags);
204 }
205  
206 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
207 0 croak("Compressed Sereal packet is too large");
208  
209 /* Back up old buffer and allocate new one with correct size */
210 0 srl_buf_copy_buffer(aTHX_ buf, &old_buf);
211 0 srl_buf_init_buffer(aTHX_ buf, sereal_header_length + compressed_body_length + 1);
212  
213 /* Copy Sereal header */
214 0 Copy(old_buf.start, buf->pos, sereal_header_length, char);
215 0 buf->pos += sereal_header_length;
216  
217 /* Embed uncompressed packet length if Zlib */
218 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
219  
220 /* Embed compressed packet length if incr. Snappy, Zlib or Zstd*/
221 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
222 0 varint_start = buf->pos;
223 0 srl_buf_cat_varint_nocheck(aTHX_ buf, 0, compressed_body_length);
224 0 varint_end = buf->pos - 1;
225 }
226  
227 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
228 0 uint32_t len = (uint32_t) compressed_body_length;
229 0 srl_init_snappy_workmem(aTHX_ workmem);
230  
231 0 csnappy_compress((char*) (old_buf.start + sereal_header_length), (uint32_t) uncompressed_body_length,
232 0 (char*) buf->pos, &len, *workmem, CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
233  
234 0 compressed_body_length = (size_t) len;
235 0 ▶ } else if (is_zstd) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zstd) { 0 0 0
236 0 size_t code = ZSTD_compress((void*) buf->pos, compressed_body_length,
237 0 (void*) (old_buf.start + sereal_header_length), uncompressed_body_length,
238 compress_level);
239  
240 0 ▶ if (ZSTD_isError(code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ZSTD_isError(code)) { 0 0 0
241 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
242 0 srl_buf_free_buffer(aTHX_ &old_buf);
243 0 croak("Zstd compression failed: %s", ZSTD_getErrorName(code));
244 }
245 0 compressed_body_length = code;
246 0 ▶ } else if (is_zlib) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zlib) { 0 0 0
247 0 mz_ulong dl = (mz_ulong) compressed_body_length;
248 0 int status = mz_compress2(
249 0 buf->pos,
250 &dl,
251 0 old_buf.start + sereal_header_length,
252 (mz_ulong) uncompressed_body_length,
253 compress_level
254 );
255  
256 0 ▶ if (status != Z_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != Z_OK) { 0 0 0
257 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
258 0 srl_buf_free_buffer(aTHX_ &old_buf);
259 0 croak("Zlib compression failed with error %d", status);
260 }
261 0 compressed_body_length = (size_t) dl;
262 }
263  
264 assert(compressed_body_length != 0);
265  
266 /* If compression didn't help, swap back to old, uncompressed buffer */
267 0 ▶ if (compressed_body_length >= uncompressed_body_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (compressed_body_length >= uncompressed_body_length) { 0 0 0
268 /* swap in old, uncompressed buffer */
269 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
270 /* disable compression flag */
271 0 srl_reset_compression_header_flag(buf);
272 } else { /* go ahead with Snappy and do final fixups */
273 /* overwrite the max size varint with the real size of the compressed data */
274 0 ▶ if (varint_start)
Branch: 1 branch
BranchTrueFalseTotal
if (varint_start) 0 0 0
275 0 srl_update_varint_from_to(aTHX_ varint_start, varint_end, compressed_body_length);
276  
277 0 buf->pos += compressed_body_length;
278  
279 /* enable compression flag */
280 0 srl_set_compression_header_flag(buf, compress_flags);
281 }
282  
283 0 srl_buf_free_buffer(aTHX_ &old_buf);
284 DEBUG_ASSERT_BUF_SANE(buf);
285 }
286  
287 #endif
« srl_float.h ↑ Summary srl_encoder.c »