Free.xs

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