Free.xs

filter: Statementstmt 100% 35 / 35 Branchbran 66.6% 28 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.8% 63 / 77 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/CBOR/Free/Decoder.pm ↑ Summary cbor_free_decode.c »
1 #define PERL_NO_GET_CONTEXT
2  
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6  
7 #include "ppport.h"
8  
9 //#include <stdio.h>
10 #include <stdbool.h>
11 #include <stdlib.h>
12 #include <string.h>
13  
14 #include "cbor_free_common.h"
15  
16 #include "cbor_free_boolean.h"
17 #include "cbor_free_encode.h"
18 #include "cbor_free_decode.h"
19  
20 #define _PACKAGE "CBOR::Free"
21  
22 #define CANONICAL_OPT "canonical"
23 #define CANONICAL_OPT_LEN (sizeof(CANONICAL_OPT) - 1)
24  
25 #define PRESERVE_REFS_OPT "preserve_references"
26 #define PRESERVE_REFS_OPT_LEN (sizeof(PRESERVE_REFS_OPT) - 1)
27  
28 #define SCALAR_REFS_OPT "scalar_references"
29 #define SCALAR_REFS_OPT_LEN (sizeof(SCALAR_REFS_OPT) - 1)
30  
31 HV *cbf_stash = NULL;
32  
33 //----------------------------------------------------------------------
34  
35 MODULE = CBOR::Free PACKAGE = CBOR::Free
36  
37 PROTOTYPES: DISABLE
38  
39 BOOT:
40 21 cbf_stash = gv_stashpv(_PACKAGE, FALSE);
41 21 newCONSTSUB(cbf_stash, "_MAX_RECURSION", newSVuv( MAX_ENCODE_RECURSE ));
42  
43  
44 SV *
45 encode( SV * value, ... )
46 CODE:
47 16885 uint8_t encode_state_flags = 0;
48  
49 U8 i;
50 16896 ▶ for (i=1; i<items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=1; i<items; i++) { 11 16885 16896
51 11 ▶ if (!(i % 2)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(i % 2)) continue; 2 9 11
52  
53 13 ▶ if ((SvCUR(ST(i)) == CANONICAL_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), CANONICAL_OPT, CANONICAL_OPT_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((SvCUR(ST(i)) == CANONICAL_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), CANONICAL_OPT, CANONICAL_OPT_LEN)) { 4 5 9
if ((SvCUR(ST(i)) == CANONICAL_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), CANONICAL_OPT, CANONICAL_OPT_LEN)) { 4 0 4
54 4 ++i;
55 4 ▶ if (i<items && SvTRUE(ST(i))) {
Branch: 2 branches
BranchTrueFalseTotal
if (i<items && SvTRUE(ST(i))) { 4 0 4
if (i<items && SvTRUE(ST(i))) { 4 0 4
56 4 encode_state_flags |= ENCODE_FLAG_CANONICAL;
57 }
58 }
59  
60 6 ▶ else if ((SvCUR(ST(i)) == PRESERVE_REFS_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), PRESERVE_REFS_OPT, PRESERVE_REFS_OPT_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
else if ((SvCUR(ST(i)) == PRESERVE_REFS_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), PRESERVE_REFS_OPT, PRESERVE_REFS_OPT_LEN)) { 1 4 5
else if ((SvCUR(ST(i)) == PRESERVE_REFS_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), PRESERVE_REFS_OPT, PRESERVE_REFS_OPT_LEN)) { 1 0 1
61 1 ++i;
62 1 ▶ if (i<items && SvTRUE(ST(i))) {
Branch: 2 branches
BranchTrueFalseTotal
if (i<items && SvTRUE(ST(i))) { 1 0 1
if (i<items && SvTRUE(ST(i))) { 1 0 1
63 1 encode_state_flags |= ENCODE_FLAG_PRESERVE_REFS;
64 }
65 }
66  
67 6 ▶ else if ((SvCUR(ST(i)) == SCALAR_REFS_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), SCALAR_REFS_OPT, SCALAR_REFS_OPT_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
else if ((SvCUR(ST(i)) == SCALAR_REFS_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), SCALAR_REFS_OPT, SCALAR_REFS_OPT_LEN)) { 2 2 4
else if ((SvCUR(ST(i)) == SCALAR_REFS_OPT_LEN) && memEQ( SvPV_nolen(ST(i)), SCALAR_REFS_OPT, SCALAR_REFS_OPT_LEN)) { 2 0 2
68 2 ++i;
69 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
70 2 encode_state_flags |= ENCODE_FLAG_SCALAR_REFS;
71 }
72 }
73 }
74  
75 16885 encode_ctx encode_state = cbf_encode_ctx_create(encode_state_flags);
76  
77 16885 RETVAL = newSV(0);
78  
79 16885 cbf_encode(aTHX_ value, &encode_state, RETVAL);
80  
81 16881 cbf_encode_ctx_free_reftracker( &encode_state );
82  
83 // Don’t use newSVpvn here because that will copy the string.
84 // Instead, create a new SV and manually assign its pieces.
85 // This follows the example from ext/POSIX/POSIX.xs:
86  
87 16881 ▶ SvUPGRADE(RETVAL, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(RETVAL, SVt_PV); 16881 0 16881
88 16881 SvPV_set(RETVAL, encode_state.buffer);
89 16881 SvPOK_on(RETVAL);
90 16881 SvCUR_set(RETVAL, encode_state.len - 1);
91 16881 SvLEN_set(RETVAL, encode_state.buflen);
92  
93 OUTPUT:
94 RETVAL
95  
96  
97 SV *
98 decode( SV *cbor )
99 CODE:
100 444 RETVAL = cbf_decode( aTHX_ cbor, NULL, false );
101  
102 OUTPUT:
103 RETVAL
104  
105 # ----------------------------------------------------------------------
106  
107 MODULE = CBOR::Free PACKAGE = CBOR::Free::Decoder
108  
109 PROTOTYPES: DISABLE
110  
111 SV *
112 decode( SV *selfref, SV *cbor )
113 CODE:
114 102 HV *self = (HV *)SvRV(selfref);
115  
116 102 HV *tag_handler = NULL;
117  
118 102 SV **tag_handler_hr = hv_fetchs(self, "_tag_decode_callback", 0);
119  
120 102 ▶ if (tag_handler_hr && *tag_handler_hr && SvOK(*tag_handler_hr)) {
Branch: 3 branches
BranchTrueFalseTotal
if (tag_handler_hr && *tag_handler_hr && SvOK(*tag_handler_hr)) { 101 1 102
if (tag_handler_hr && *tag_handler_hr && SvOK(*tag_handler_hr)) { 101 0 101
if (tag_handler_hr && *tag_handler_hr && SvOK(*tag_handler_hr)) { 101 0 101
121 101 tag_handler = (HV *)SvRV(*tag_handler_hr);
122 }
123  
124 102 SV **preserve_refs = hv_fetchs(self, "_preserve_references", 0);
125  
126 102 RETVAL = cbf_decode( aTHX_
127 cbor,
128 tag_handler,
129 102 ▶ preserve_refs && *preserve_refs && SvTRUE(*preserve_refs)
Branch: 3 branches
BranchTrueFalseTotal
preserve_refs && *preserve_refs && SvTRUE(*preserve_refs) 1 101 102
preserve_refs && *preserve_refs && SvTRUE(*preserve_refs) 1 0 1
preserve_refs && *preserve_refs && SvTRUE(*preserve_refs) 1 0 1
130 );
131  
132 OUTPUT:
133 RETVAL
« blib/lib/CBOR/Free/Decoder.pm ↑ Summary cbor_free_decode.c »