Free.xs

filter: Statementstmt 96.9% 129 / 133 Branchbran 73.4% 47 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.3% 176 / 197 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« cbor_free_common.c ↑ Summary cbor_free_encode.c »
1 #include "easyxs/init.h"
2  
3 #include <stdbool.h>
4 #include <stdlib.h>
5 #include <string.h>
6  
7 #include "cbor_free_common.h"
8  
9 #include "cbor_free_boolean.h"
10 #include "cbor_free_encode.h"
11 #include "cbor_free_decode.h"
12  
13 #define _PACKAGE "CBOR::Free"
14  
15 #define CANONICAL_OPT "canonical"
16 #define PRESERVE_REFS_OPT "preserve_references"
17 #define SCALAR_REFS_OPT "scalar_references"
18 #define STRING_ENCODE_MODE_OPT "string_encode_mode"
19  
20 #define UNUSED(x) (void)(x)
21  
22 const char* const cbf_string_encode_mode_options[] = {
23 "sv",
24 "encode_text",
25 "as_text",
26 "as_binary",
27 };
28  
29 HV *cbf_stash = NULL;
30  
31 35 SV* _seqdecode_get( pTHX_ seqdecode_ctx* seqdecode) {
32 35 decode_ctx* decode_state = seqdecode->decode_state;
33  
34 35 decode_state->curbyte = decode_state->start;
35  
36 35 ▶ if (decode_state->flags & CBF_FLAG_PRESERVE_REFERENCES) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_state->flags & CBF_FLAG_PRESERVE_REFERENCES) { 2 33 35
37 2 reset_reflist_if_needed(aTHX_ decode_state);
38 }
39  
40 35 SV *referent = cbf_decode_one( aTHX_ seqdecode->decode_state );
41  
42 34 ▶ if (seqdecode->decode_state->incomplete_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (seqdecode->decode_state->incomplete_by) { 16 18 34
43 16 seqdecode->decode_state->incomplete_by = 0;
44 16 return &PL_sv_undef;
45 }
46  
47 // TODO: Once the lead offset gets big enough,
48 // recreate this buffer.
49 18 sv_chop( seqdecode->cbor, decode_state->curbyte );
50  
51 18 advance_decode_state_buffer( aTHX_ decode_state );
52  
53 18 return newRV_noinc(referent);
54 }
55  
56 12 bool _handle_flag_call( pTHX_ decode_ctx* decode_state, SV* new_setting, U8 flagval ) {
57 12 ▶ if (new_setting == NULL || SvTRUE(new_setting)) {
Branch: 2 branches
BranchTrueFalseTotal
if (new_setting == NULL || SvTRUE(new_setting)) { 5 7 12
if (new_setting == NULL || SvTRUE(new_setting)) { 0 5 5
58 7 decode_state->flags |= flagval;
59 }
60 else {
61 5 decode_state->flags &= ~flagval;
62 }
63  
64 12 return( !!(decode_state->flags & flagval) );
65 }
66  
67 118 SV * _bless_to_sv( pTHX_ SV *class, void* ptr ) {
68 118 SV *RETVAL = newSV(0);
69 118 sv_setref_pv(RETVAL, SvPV_nolen(class), ptr);
70  
71 118 return RETVAL;
72 }
73  
74 6 static inline void * sv_to_ptr( pTHX_ SV *self) {
75 6 IV tmp = SvIV((SV*)SvRV(self));
76 6 return INT2PTR(void*, tmp);
77 }
78  
79 3 static inline SV* _set_string_decode( pTHX_ SV* self, enum cbf_string_decode_mode new_setting ) {
80 3 decode_ctx* decode_state = (decode_ctx*) sv_to_ptr(aTHX_ self);
81 3 decode_state->string_decode_mode = new_setting;
82  
83 3 ▶ return (GIMME_V == G_VOID) ? NULL : newSVsv(self);
Branch: 1 branch
BranchTrueFalseTotal
return (GIMME_V == G_VOID) ? NULL : newSVsv(self); 3 0 3
84 }
85  
86 3 static inline SV* _seq_set_string_decode( pTHX_ SV* self, enum cbf_string_decode_mode new_setting ) {
87 3 seqdecode_ctx* seqdecode = (seqdecode_ctx*) sv_to_ptr(aTHX_ self);
88 3 seqdecode->decode_state->string_decode_mode = new_setting;
89  
90 3 ▶ return (GIMME_V == G_VOID) ? NULL : newSVsv(self);
Branch: 1 branch
BranchTrueFalseTotal
return (GIMME_V == G_VOID) ? NULL : newSVsv(self); 3 0 3
91 }
92  
93 6 static inline bool _handle_preserve_references( pTHX_ decode_ctx* decode_state, SV* new_setting ) {
94 6 bool RETVAL = _handle_flag_call( aTHX_ decode_state, new_setting, CBF_FLAG_PRESERVE_REFERENCES );
95  
96 6 ▶ if (RETVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL) { 2 4 6
97 2 ensure_reflist_exists( aTHX_ decode_state );
98 }
99 4 ▶ else if (NULL != decode_state->reflist) {
Branch: 1 branch
BranchTrueFalseTotal
else if (NULL != decode_state->reflist) { 2 2 4
100 2 delete_reflist( aTHX_ decode_state );
101 }
102  
103 6 return RETVAL;
104 }
105  
106 103 static inline void _set_tag_handlers( pTHX_ decode_ctx* decode_state, U8 items_len, SV** args ) {
107 103 ▶ if (!(items_len % 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(items_len % 2)) { 0 103 103
108 0 croak("Odd key-value pair given!");
109 }
110  
111 103 ▶ if (NULL == decode_state->tag_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == decode_state->tag_handler) { 102 1 103
112 102 decode_state->tag_handler = newHV();
113 }
114  
115 U8 i;
116 206 ▶ for (i=1; i<items_len; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=1; i<items_len; i += 2) { 103 103 206
117 103 HV* tag_handler = decode_state->tag_handler;
118  
119 103 SV* tagnum_sv = args[i];
120 103 UV tagnum = SvUV(tagnum_sv);
121  
122 103 i++;
123 103 ▶ if (i<items_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i<items_len) { 103 0 103
124 103 SV* tagcb_sv = args[i];
125  
126 103 hv_store(
127 tag_handler,
128 (const char *) &tagnum,
129 sizeof(UV),
130 tagcb_sv,
131 0
132 );
133  
134 103 SvREFCNT_inc(tagcb_sv);
135 }
136 }
137 103 }
138  
139 //----------------------------------------------------------------------
140 //----------------------------------------------------------------------
141  
142 MODULE = CBOR::Free PACKAGE = CBOR::Free
143  
144 PROTOTYPES: DISABLE
145  
146 BOOT:
147 27 cbf_stash = gv_stashpv(_PACKAGE, FALSE);
148 27 newCONSTSUB(cbf_stash, "_MAX_RECURSION", newSVuv( MAX_ENCODE_RECURSE ));
149  
150  
151 SV *
152 encode( SV * value, ... )
153 CODE:
154 16967 uint8_t encode_state_flags = 0;
155 16967 enum cbf_string_encode_mode string_encode_mode = CBF_STRING_ENCODE_SV;
156  
157 U8 i;
158 char* optname;
159 SV* opt_sv;
160  
161 17118 ▶ for (i=1; i<items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=1; i<items; i++) { 151 16967 17118
162 151 ▶ if (!(i % 2)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(i % 2)) continue; 0 151 151
163  
164 151 opt_sv = ST(i);
165 151 ▶ if (!SvPOK(opt_sv)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(opt_sv)) continue; 0 151 151
166  
167 151 optname = SvPVX(opt_sv);
168  
169 151 ▶ if (strEQ(optname, STRING_ENCODE_MODE_OPT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(optname, STRING_ENCODE_MODE_OPT)) { 71 80 151
170 71 ++i;
171  
172 71 ▶ if (i<items) {
Branch: 1 branch
BranchTrueFalseTotal
if (i<items) { 71 0 71
173 71 SV* opt = ST(i);
174  
175 71 ▶ if (SvOK(opt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(opt)) { 71 0 71
176 71 char* optstr = SvPV_nolen(opt);
177  
178 U8 i;
179 204 ▶ for (i=0; i<CBF_STRING_ENCODE__LIMIT; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<CBF_STRING_ENCODE__LIMIT; i++) { 204 0 204
180 204 ▶ if (strEQ(optstr, cbf_string_encode_mode_options[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(optstr, cbf_string_encode_mode_options[i])) { 71 133 204
181 71 string_encode_mode = i;
182 71 break;
183 }
184 }
185  
186 71 ▶ if (i == CBF_STRING_ENCODE__LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == CBF_STRING_ENCODE__LIMIT) { 0 71 71
187 0 croak("Invalid " STRING_ENCODE_MODE_OPT ": %s", optstr);
188 }
189 }
190  
191 }
192 }
193  
194 80 ▶ else if (strEQ(optname, CANONICAL_OPT)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(optname, CANONICAL_OPT)) { 72 8 80
195 72 ++i;
196 72 ▶ if (i<items && SvTRUE(ST(i))) {
Branch: 2 branches
BranchTrueFalseTotal
if (i<items && SvTRUE(ST(i))) { 72 0 72
if (i<items && SvTRUE(ST(i))) { 39 33 72
197 39 encode_state_flags |= ENCODE_FLAG_CANONICAL;
198 }
199 }
200  
201 8 ▶ else if (strEQ(optname, PRESERVE_REFS_OPT)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(optname, PRESERVE_REFS_OPT)) { 2 6 8
202 2 ++i;
203 2 ▶ if (i<items && SvTRUE(ST(i))) {
Branch: 2 branches
BranchTrueFalseTotal
if (i<items && SvTRUE(ST(i))) { 2 0 2
if (i<items && SvTRUE(ST(i))) { 2 0 2
204 2 encode_state_flags |= ENCODE_FLAG_PRESERVE_REFS;
205 }
206 }
207  
208 6 ▶ else if (strEQ(optname, SCALAR_REFS_OPT)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(optname, SCALAR_REFS_OPT)) { 6 0 6
209 6 ++i;
210 6 ▶ if (i<items && SvTRUE(ST(i))) {
Branch: 2 branches
BranchTrueFalseTotal
if (i<items && SvTRUE(ST(i))) { 6 0 6
if (i<items && SvTRUE(ST(i))) { 3 3 6
211 3 encode_state_flags |= ENCODE_FLAG_SCALAR_REFS;
212 }
213 }
214  
215 else {
216 0 warn("Invalid option: %s", optname);
217 }
218 }
219  
220 16967 encode_ctx encode_state = cbf_encode_ctx_create(encode_state_flags, string_encode_mode);
221  
222 16967 RETVAL = newSV(0);
223  
224 16967 cbf_encode(aTHX_ value, &encode_state, RETVAL);
225  
226 16951 cbf_encode_ctx_free_reftracker( &encode_state );
227  
228 // Don’t use newSVpvn here because that will copy the string.
229 // Instead, create a new SV and manually assign its pieces.
230 // This follows the example from ext/POSIX/POSIX.xs:
231  
232 16951 ▶ SvUPGRADE(RETVAL, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(RETVAL, SVt_PV); 16951 0 16951
233 16951 SvPV_set(RETVAL, encode_state.buffer);
234 16951 SvPOK_on(RETVAL);
235 16951 SvCUR_set(RETVAL, encode_state.len - 1);
236 16951 SvLEN_set(RETVAL, encode_state.buflen);
237  
238 OUTPUT:
239 RETVAL
240  
241  
242 SV *
243 decode( SV *cbor )
244 CODE:
245 464 RETVAL = cbf_decode( aTHX_ cbor, NULL, false );
246  
247 OUTPUT:
248 RETVAL
249  
250 # ----------------------------------------------------------------------
251  
252 MODULE = CBOR::Free PACKAGE = CBOR::Free::Decoder
253  
254 PROTOTYPES: DISABLE
255  
256 SV*
257 new(SV *class)
258 CODE:
259 108 decode_ctx* decode_state = create_decode_state( aTHX_ NULL, NULL, CBF_FLAG_PERSIST_STATE);
260  
261 108 RETVAL = _bless_to_sv( aTHX_ class, (void*)decode_state);
262  
263 OUTPUT:
264 RETVAL
265  
266 SV*
267 decode(decode_ctx* decode_state, SV* cbor)
268 CODE:
269 137 decode_state->curbyte = 0;
270 137 renew_decode_state_buffer( aTHX_ decode_state, cbor );
271  
272 137 ▶ if (decode_state->flags & CBF_FLAG_PRESERVE_REFERENCES) {
Branch: 1 branch
BranchTrueFalseTotal
if (decode_state->flags & CBF_FLAG_PRESERVE_REFERENCES) { 2 135 137
273 2 reset_reflist_if_needed(aTHX_ decode_state);
274 }
275  
276 137 RETVAL = cbf_decode_document( aTHX_ decode_state );
277  
278 OUTPUT:
279 RETVAL
280  
281 bool
282 preserve_references(decode_ctx* decode_state, SV* new_setting = NULL)
283 CODE:
284 3 RETVAL = _handle_preserve_references( aTHX_ decode_state, new_setting );
285  
286 OUTPUT:
287 RETVAL
288  
289 bool
290 naive_utf8(decode_ctx* decode_state, SV* new_setting = NULL)
291 CODE:
292 2 RETVAL = _handle_flag_call( aTHX_ decode_state, new_setting, CBF_FLAG_NAIVE_UTF8 );
293  
294 OUTPUT:
295 RETVAL
296  
297 bool
298 reject_duplicate_keys(decode_ctx* decode_state, SV* new_setting = NULL)
299 CODE:
300 2 RETVAL = _handle_flag_call( aTHX_ decode_state, new_setting, CBF_FLAG_REJECT_DUPLICATE_KEYS );
301  
302 OUTPUT:
303 RETVAL
304  
305 SV *
306 string_decode_cbor(SV* self)
307 CODE:
308 1 RETVAL = _set_string_decode( aTHX_ self, CBF_STRING_DECODE_CBOR );
309  
310 OUTPUT:
311 RETVAL
312  
313 SV *
314 string_decode_never(SV* self)
315 CODE:
316 1 RETVAL = _set_string_decode( aTHX_ self, CBF_STRING_DECODE_NEVER );
317  
318 OUTPUT:
319 RETVAL
320  
321 SV *
322 string_decode_always(SV* self)
323 CODE:
324 1 RETVAL = _set_string_decode( aTHX_ self, CBF_STRING_DECODE_ALWAYS );
325  
326 OUTPUT:
327 RETVAL
328  
329 void
330 _set_tag_handlers_backend(decode_ctx* decode_state, ...)
331 CODE:
332 103 _set_tag_handlers( aTHX_ decode_state, items, &ST(0) );
333  
334 void
335 DESTROY(decode_ctx* decode_state)
336 CODE:
337 108 free_decode_state( aTHX_ decode_state);
338  
339  
340 # ----------------------------------------------------------------------
341  
342 MODULE = CBOR::Free PACKAGE = CBOR::Free::SequenceDecoder
343  
344 PROTOTYPES: DISABLE
345  
346 SV *
347 new(SV *class)
348 CODE:
349  
350 10 SV* cbor = newSVpvs("");
351  
352 10 decode_ctx* decode_state = create_decode_state( aTHX_ cbor, NULL, CBF_FLAG_PERSIST_STATE);
353  
354 seqdecode_ctx* seqdecode;
355  
356 10 Newx( seqdecode, 1, seqdecode_ctx );
357  
358 10 seqdecode->decode_state = decode_state;
359 10 seqdecode->cbor = cbor;
360  
361 10 RETVAL = _bless_to_sv( aTHX_ class, (void*)seqdecode);
362  
363 OUTPUT:
364 RETVAL
365  
366 SV *
367 give(seqdecode_ctx* seqdecode, SV* addend)
368 CODE:
369 32 sv_catsv( seqdecode->cbor, addend );
370  
371 32 renew_decode_state_buffer( aTHX_ seqdecode->decode_state, seqdecode->cbor );
372  
373 32 RETVAL = _seqdecode_get( aTHX_ seqdecode);
374  
375 OUTPUT:
376 RETVAL
377  
378 SV *
379 get(seqdecode_ctx* seqdecode)
380 CODE:
381 3 RETVAL = _seqdecode_get( aTHX_ seqdecode);
382  
383 OUTPUT:
384 RETVAL
385  
386 bool
387 preserve_references(seqdecode_ctx* seqdecode, SV* new_setting = NULL)
388 CODE:
389 3 RETVAL = _handle_preserve_references( aTHX_ seqdecode->decode_state, new_setting );
390  
391 OUTPUT:
392 RETVAL
393  
394 bool
395 naive_utf8(seqdecode_ctx* seqdecode, SV* new_setting = NULL)
396 CODE:
397 1 RETVAL = _handle_flag_call( aTHX_ seqdecode->decode_state, new_setting, CBF_FLAG_NAIVE_UTF8 );
398  
399 OUTPUT:
400 RETVAL
401  
402 bool
403 reject_duplicate_keys(seqdecode_ctx* seqdecode, SV* new_setting = NULL)
404 CODE:
405 1 RETVAL = _handle_flag_call( aTHX_ seqdecode->decode_state, new_setting, CBF_FLAG_REJECT_DUPLICATE_KEYS );
406  
407 OUTPUT:
408 RETVAL
409  
410  
411 SV *
412 string_decode_cbor(SV* self)
413 CODE:
414 1 RETVAL = _seq_set_string_decode( aTHX_ self, CBF_STRING_DECODE_CBOR );
415  
416 OUTPUT:
417 RETVAL
418  
419 SV *
420 string_decode_never(SV* self)
421 CODE:
422 1 RETVAL = _seq_set_string_decode( aTHX_ self, CBF_STRING_DECODE_NEVER );
423  
424 OUTPUT:
425 RETVAL
426  
427 SV *
428 string_decode_always(SV* self)
429 CODE:
430 1 RETVAL = _seq_set_string_decode( aTHX_ self, CBF_STRING_DECODE_ALWAYS );
431  
432 OUTPUT:
433 RETVAL
434  
435 void
436 _set_tag_handlers_backend(seqdecode_ctx* seqdecode, ...)
437 CODE:
438 0 _set_tag_handlers( aTHX_ seqdecode->decode_state, items, &ST(0) );
439  
440 void
441 DESTROY(seqdecode_ctx* seqdecode)
442 CODE:
443 10 free_decode_state( aTHX_ seqdecode->decode_state);
444 10 SvREFCNT_dec(seqdecode->cbor);
445  
446 10 Safefree(seqdecode);
« cbor_free_common.c ↑ Summary cbor_free_encode.c »