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 * U64 because both bundled Snappy and miniz can wrap their 32-bit bounds. */
49 SRL_STATIC_INLINE U64
50 0 srl_compress_bound(STRLEN source_len, U32 compress_flags)
51 {
52 0 const U64 len = (U64)source_len;
53  
54 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
55 0 return 32 + len + len / 6
56 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
57 0 ▶ } else if (!(compress_flags & SRL_F_COMPRESS_ZSTD)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!(compress_flags & SRL_F_COMPRESS_ZSTD)) { 0 0 0
58 /* Mirror miniz's two bounds in a wide type, and allow an external
59 * miniz implementation to return a larger bound. */
60 0 const U64 bound1 = 128 + (len * 110) / 100;
61 0 const U64 bound2 = 128 + len + ((len / (31 * 1024)) + 1) * 5;
62 0 const U64 miniz_bound = (U64)mz_compressBound((mz_ulong)source_len);
63 0 const U64 bound = bound1 > bound2 ? bound1 : bound2;
64 0 return (bound > miniz_bound ? bound : miniz_bound) + 2 * SRL_MAX_VARINT_LENGTH;
65 }
66 0 return 0; /* zstd uses a size_t bound and has no 32-bit output limit */
67 }
68  
69 /* Update a varint anywhere in the output stream with defined start and end
70 * positions. This can produce non-canonical varints and is useful for filling
71 * pre-allocated varints. */
72 SRL_STATIC_INLINE void
73 0 srl_update_varint_from_to(pTHX_ unsigned char *varint_start, unsigned char *varint_end, UV number)
74 {
75 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
76 0 *varint_start++ = (number & 0x7f) | 0x80; /* write out the least significant 7 bits, set the high bit */
77 0 number = number >> 7; /* shift off the 7 least significant bits */
78 }
79  
80 /* if it is the same size we can use a canonical varint */
81 0 ▶ if ( varint_start == varint_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( varint_start == varint_end ) { 0 0 0
82 0 *varint_start = number; /* encode the last 7 bits without the high bit being set */
83 } else {
84 /* if not we produce a non-canonical varint, basically we stuff
85 * 0 bits (via 0x80) into the "tail" of the varint, until we can
86 * stick in a null to terminate the sequence. This means that the
87 * varint is effectively "self-padding", and we only need special
88 * logic in the encoder - a decoder will happily process a non-canonical
89 * varint with no problem */
90 0 *varint_start++ = (number & 0x7f) | 0x80;
91 0 ▶ while ( varint_start < varint_end )
Branch: 1 branch
BranchTrueFalseTotal
while ( varint_start < varint_end ) 0 0 0
92 0 *varint_start++ = 0x80;
93 0 *varint_start= 0;
94 }
95 0 }
96  
97 /* Lazy working buffer alloc */
98 SRL_STATIC_INLINE void
99 0 srl_init_snappy_workmem(pTHX_ void **workmem)
100 {
101 /* Lazy working buffer alloc */
102 0 ▶ if (expect_false(*workmem == NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(*workmem == NULL)) { 0 0 0
103 /* Cleaned up automatically by the cleanup handler */
104 0 Newx(*workmem, CSNAPPY_WORKMEM_BYTES, char);
105 0 ▶ if (*workmem == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (*workmem == NULL) 0 0 0
106 0 croak("Out of memory!");
107 }
108 0 }
109  
110 /* Destroy working buffer */
111 SRL_STATIC_INLINE void
112 7 srl_destroy_snappy_workmem(pTHX_ void *workmem)
113 {
114 7 Safefree(workmem);
115 7 }
116  
117 SRL_STATIC_INLINE U8
118 7 srl_get_compression_header_flag(const U32 compress_flags)
119 {
120 7 ▶ if (compress_flags & SRL_F_COMPRESS_SNAPPY) {
Branch: 1 branch
BranchTrueFalseTotal
if (compress_flags & SRL_F_COMPRESS_SNAPPY) { 0 7 7
121 0 return SRL_PROTOCOL_ENCODING_SNAPPY;
122 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
123 0 return SRL_PROTOCOL_ENCODING_SNAPPY_INCREMENTAL;
124 7 ▶ } else if (compress_flags & SRL_F_COMPRESS_ZLIB) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (compress_flags & SRL_F_COMPRESS_ZLIB) { 0 7 7
125 0 return SRL_PROTOCOL_ENCODING_ZLIB;
126 7 ▶ } else if (compress_flags & SRL_F_COMPRESS_ZSTD) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (compress_flags & SRL_F_COMPRESS_ZSTD) { 0 7 7
127 0 return SRL_PROTOCOL_ENCODING_ZSTD;
128 } else {
129 7 return SRL_PROTOCOL_ENCODING_RAW;
130 }
131 }
132  
133 /* Sets the compression header flag */
134 SRL_STATIC_INLINE void
135 0 srl_set_compression_header_flag(srl_buffer_t *buf, const U32 compress_flags)
136 {
137 /* sizeof(const char *) includes a count of \0 */
138 0 srl_buffer_char *flags_and_version_byte = buf->start + sizeof(SRL_MAGIC_STRING) - 1;
139 0 *flags_and_version_byte |= srl_get_compression_header_flag(compress_flags);
140 0 }
141  
142 /* Resets the compression header flag to OFF.
143 * Obviously requires that a Sereal header was already written to the
144 * encoder's output buffer. */
145 SRL_STATIC_INLINE void
146 0 srl_reset_compression_header_flag(srl_buffer_t *buf)
147 {
148 /* sizeof(const char *) includes a count of \0 */
149 0 srl_buffer_char *flags_and_version_byte = buf->start + sizeof(SRL_MAGIC_STRING) - 1;
150  
151 /* disable snappy flag in header */
152 0 *flags_and_version_byte = SRL_PROTOCOL_ENCODING_RAW |
153 0 (*flags_and_version_byte & SRL_PROTOCOL_VERSION_MASK);
154 0 }
155  
156 /* Compress body with one of available compressors (zlib, snappy).
157 * The function sets/resets compression bits at version byte.
158 * The caller has to adjust buf->body_pos by calling SRL_UPDATE_BODY_POS
159 * right after exiting from srl_compress_body.
160 */
161  
162 SRL_STATIC_INLINE void
163 0 srl_compress_body(pTHX_ srl_buffer_t *buf, STRLEN sereal_header_length,
164 const U32 compress_flags, const int compress_level, void **workmem)
165 {
166 0 const int is_traditional_snappy = compress_flags & SRL_F_COMPRESS_SNAPPY;
167 0 const int is_incremental_snappy = compress_flags & SRL_F_COMPRESS_SNAPPY_INCREMENTAL;
168 0 const int is_zstd = compress_flags & SRL_F_COMPRESS_ZSTD;
169 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
170  
171 0 size_t uncompressed_body_length = BUF_POS_OFS(buf) - sereal_header_length;
172 size_t compressed_body_length;
173 0 srl_buffer_char *varint_start = NULL;
174 0 srl_buffer_char *varint_end = NULL;
175 srl_buffer_t old_buf;
176  
177 DEBUG_ASSERT_BUF_SANE(buf);
178  
179 0 ▶ if (uncompressed_body_length > (size_t)U32_MAX
Branch: 1 branch
BranchTrueFalseTotal
if (uncompressed_body_length > (size_t)U32_MAX 0 0 0
180 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
181 0 warn("disabling compression as buffer is too large!");
182 0 srl_reset_compression_header_flag(buf);
183 0 return;
184 }
185  
186 /* Get estimated compressed payload length */
187 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
188 0 compressed_body_length = (size_t)srl_compress_bound(uncompressed_body_length, compress_flags);
189 0 ▶ } else if (is_zstd) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zstd) { 0 0 0
190 0 compressed_body_length = ZSTD_compressBound(uncompressed_body_length);
191 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
192 0 croak("Compressed Sereal packet is too large");
193 0 compressed_body_length += SRL_MAX_VARINT_LENGTH; /* will have to embed compressed packet length as varint */
194 } else {
195 0 compressed_body_length = (size_t)srl_compress_bound(uncompressed_body_length, compress_flags);
196 }
197  
198 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
199 0 croak("Compressed Sereal packet is too large");
200  
201 /* Back up old buffer and allocate new one with correct size */
202 0 srl_buf_copy_buffer(aTHX_ buf, &old_buf);
203 0 srl_buf_init_buffer(aTHX_ buf, sereal_header_length + compressed_body_length + 1);
204  
205 /* Copy Sereal header */
206 0 Copy(old_buf.start, buf->pos, sereal_header_length, char);
207 0 buf->pos += sereal_header_length;
208  
209 /* Embed uncompressed packet length if Zlib */
210 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
211  
212 /* Embed compressed packet length if incr. Snappy, Zlib or Zstd*/
213 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
214 0 varint_start = buf->pos;
215 0 srl_buf_cat_varint_nocheck(aTHX_ buf, 0, compressed_body_length);
216 0 varint_end = buf->pos - 1;
217 }
218  
219 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
220 0 uint32_t len = (uint32_t) compressed_body_length;
221 0 srl_init_snappy_workmem(aTHX_ workmem);
222  
223 0 csnappy_compress((char*) (old_buf.start + sereal_header_length), (uint32_t) uncompressed_body_length,
224 0 (char*) buf->pos, &len, *workmem, CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
225  
226 0 compressed_body_length = (size_t) len;
227 0 ▶ } else if (is_zstd) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zstd) { 0 0 0
228 0 size_t code = ZSTD_compress((void*) buf->pos, compressed_body_length,
229 0 (void*) (old_buf.start + sereal_header_length), uncompressed_body_length,
230 compress_level);
231  
232 0 ▶ if (ZSTD_isError(code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ZSTD_isError(code)) { 0 0 0
233 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
234 0 srl_buf_free_buffer(aTHX_ &old_buf);
235 0 croak("Zstd compression failed: %s", ZSTD_getErrorName(code));
236 }
237 0 compressed_body_length = code;
238 0 ▶ } else if (is_zlib) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (is_zlib) { 0 0 0
239 0 mz_ulong dl = (mz_ulong) compressed_body_length;
240 0 int status = mz_compress2(
241 0 buf->pos,
242 &dl,
243 0 old_buf.start + sereal_header_length,
244 (mz_ulong) uncompressed_body_length,
245 compress_level
246 );
247  
248 0 ▶ if (status != Z_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != Z_OK) { 0 0 0
249 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
250 0 srl_buf_free_buffer(aTHX_ &old_buf);
251 0 croak("Zlib compression failed with error %d", status);
252 }
253 0 compressed_body_length = (size_t) dl;
254 }
255  
256 assert(compressed_body_length != 0);
257  
258 /* If compression didn't help, swap back to old, uncompressed buffer */
259 0 ▶ if (compressed_body_length >= uncompressed_body_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (compressed_body_length >= uncompressed_body_length) { 0 0 0
260 /* swap in old, uncompressed buffer */
261 0 srl_buf_swap_buffer(aTHX_ buf, &old_buf);
262 /* disable compression flag */
263 0 srl_reset_compression_header_flag(buf);
264 } else { /* go ahead with Snappy and do final fixups */
265 /* overwrite the max size varint with the real size of the compressed data */
266 0 ▶ if (varint_start)
Branch: 1 branch
BranchTrueFalseTotal
if (varint_start) 0 0 0
267 0 srl_update_varint_from_to(aTHX_ varint_start, varint_end, compressed_body_length);
268  
269 0 buf->pos += compressed_body_length;
270  
271 /* enable compression flag */
272 0 srl_set_compression_header_flag(buf, compress_flags);
273 }
274  
275 0 srl_buf_free_buffer(aTHX_ &old_buf);
276 DEBUG_ASSERT_BUF_SANE(buf);
277 }
278  
279 #endif
« srl_float.h ↑ Summary srl_encoder.c »