cbor_free_encode.c

filter: Statementstmt 99.0% 200 / 202 Branchbran 77.5% 121 / 156 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.6% 321 / 358 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Free.xs ↑ 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 <stdlib.h>
8  
9 #include "cbor_free_encode.h"
10  
11 #define TAGGED_CLASS "CBOR::Free::Tagged"
12  
13 #define IS_SCALAR_REFERENCE(value) SvTYPE(SvRV(value)) <= SVt_PVMG
14  
15 static const unsigned char NUL = 0;
16 static const unsigned char CBOR_NULL_U8 = CBOR_NULL;
17 static const unsigned char CBOR_FALSE_U8 = CBOR_FALSE;
18 static const unsigned char CBOR_TRUE_U8 = CBOR_TRUE;
19  
20 static const unsigned char CBOR_INF_SHORT[3] = { 0xf9, 0x7c, 0x00 };
21 static const unsigned char CBOR_NAN_SHORT[3] = { 0xf9, 0x7e, 0x00 };
22 static const unsigned char CBOR_NEGINF_SHORT[3] = { 0xf9, 0xfc, 0x00 };
23  
24 static HV *tagged_stash = NULL;
25  
26 //----------------------------------------------------------------------
27  
28 // These encode num as big-endian into buffer.
29 // Importantly, on big-endian systems this is just a memcpy,
30 // while on little-endian systems it’s a bswap.
31  
32 15890 static inline void _u16_to_buffer( UV num, uint8_t *buffer ) {
33 15890 *(buffer++) = num >> 8;
34 15890 *(buffer++) = num;
35 15890 }
36  
37 311 static inline void _u32_to_buffer( UV num, unsigned char *buffer ) {
38 311 *(buffer++) = num >> 24;
39 311 *(buffer++) = num >> 16;
40 311 *(buffer++) = num >> 8;
41 311 *(buffer++) = num;
42 311 }
43  
44 10 static inline void _u64_to_buffer( UV num, unsigned char *buffer ) {
45 10 *(buffer++) = num >> 56;
46 10 *(buffer++) = num >> 48;
47 10 *(buffer++) = num >> 40;
48 10 *(buffer++) = num >> 32;
49 10 *(buffer++) = num >> 24;
50 10 *(buffer++) = num >> 16;
51 10 *(buffer++) = num >> 8;
52 10 *(buffer++) = num;
53 10 }
54  
55 //----------------------------------------------------------------------
56 // Croakers
57  
58 2 void _croak_unrecognized(pTHX_ encode_ctx *encode_state, SV *value) {
59 2 char * words[3] = { "Unrecognized", SvPV_nolen(value), NULL };
60  
61 2 cbf_encode_ctx_free_all(encode_state);
62  
63 2 _die( G_DISCARD, words );
64 }
65  
66 // This has to be a macro because _croak() needs a string literal.
67 #define _croak_encode(encode_state, str) \
68 cbf_encode_ctx_free_all(encode_state); \
69 _croak(str);
70  
71 //----------------------------------------------------------------------
72  
73 // NOTE: Contrary to JSON’s “canonical” order, for canonical CBOR
74 // keys are only byte-sorted if their lengths are identical. Thus,
75 // “z” sorts EARLIER than “aa”. (cf. section 3.9 of the RFC)
76  
77 #define _SORT(x) ((struct sortable_hash_entry *)x)
78  
79 41 int _sort_map_keys( const void* a, const void* b ) {
80  
81 // The CBOR RFC defines canonical sorting such that the
82 // *encoded* keys are what gets sorted; however, it’s easier to
83 // anticipate the sort order algorithmically rather than to
84 // create the encoded keys *then* sort those. Since Perl hash keys
85 // are always strings (either with or without the UTF8 flag), we
86 // only have 2 CBOR types to deal with (text & binary strings) and
87 // can sort accordingly.
88  
89 return (
90 41 _SORT(a)->is_utf8 < _SORT(b)->is_utf8 ? -1
91 82 ▶ : _SORT(a)->is_utf8 > _SORT(b)->is_utf8 ? 1
Branch: 1 branch
BranchTrueFalseTotal
: _SORT(a)->is_utf8 > _SORT(b)->is_utf8 ? 1 41 0 41
92 72 ▶ : _SORT(a)->length < _SORT(b)->length ? -1
Branch: 1 branch
BranchTrueFalseTotal
: _SORT(a)->length < _SORT(b)->length ? -1 31 10 41
93 54 ▶ : _SORT(a)->length > _SORT(b)->length ? 1
Branch: 1 branch
BranchTrueFalseTotal
: _SORT(a)->length > _SORT(b)->length ? 1 23 8 31
94 23 ▶ : memcmp( _SORT(a)->buffer, _SORT(b)->buffer, _SORT(a)->length )
Branch: 1 branch
BranchTrueFalseTotal
: memcmp( _SORT(a)->buffer, _SORT(b)->buffer, _SORT(a)->length ) 14 9 23
95 );
96 }
97  
98 //----------------------------------------------------------------------
99  
100 113 static inline HV *_get_tagged_stash() {
101 113 ▶ if (!tagged_stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tagged_stash) { 5 108 113
102 dTHX;
103 5 tagged_stash = gv_stashpv(TAGGED_CLASS, 1);
104 }
105  
106 113 return tagged_stash;
107 }
108  
109 34473 static inline void _COPY_INTO_ENCODE( encode_ctx *encode_state, const unsigned char *hdr, STRLEN len) {
110 34473 ▶ if ( (len + encode_state->len) > encode_state->buflen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (len + encode_state->len) > encode_state->buflen ) { 0 34473 34473
111 0 Renew( encode_state->buffer, encode_state->buflen + len + ENCODE_ALLOC_CHUNK_SIZE, char );
112 0 encode_state->buflen += len + ENCODE_ALLOC_CHUNK_SIZE;
113 }
114  
115 34473 Copy( hdr, encode_state->buffer + encode_state->len, len, char );
116 34473 encode_state->len += len;
117 34473 }
118  
119 // TODO? This could be a macro … it’d just be kind of unwieldy as such.
120 17440 static inline void _init_length_buffer( pTHX_ UV num, enum CBOR_TYPE major_type, encode_ctx *encode_state ) {
121 17440 union control_byte *scratch0 = (void *) encode_state->scratch;
122 17440 scratch0->pieces.major_type = major_type;
123  
124 17440 ▶ if ( num < CBOR_LENGTH_SMALL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( num < CBOR_LENGTH_SMALL ) { 636 16804 17440
125 636 scratch0->pieces.length_type = (uint8_t) num;
126  
127 636 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 1);
128 }
129 16804 ▶ else if ( num <= 0xff ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( num <= 0xff ) { 593 16211 16804
130 593 scratch0->pieces.length_type = CBOR_LENGTH_SMALL;
131 593 encode_state->scratch[1] = (uint8_t) num;
132  
133 593 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 2);
134 }
135 16211 ▶ else if ( num <= 0xffff ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( num <= 0xffff ) { 15890 321 16211
136 15890 scratch0->pieces.length_type = CBOR_LENGTH_MEDIUM;
137  
138 15890 _u16_to_buffer( num, 1 + encode_state->scratch );
139  
140 15890 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 3);
141 }
142 321 ▶ else if ( num <= 0xffffffffU ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( num <= 0xffffffffU ) { 311 10 321
143 311 scratch0->pieces.length_type = CBOR_LENGTH_LARGE;
144  
145 311 _u32_to_buffer( num, 1 + encode_state->scratch );
146  
147 311 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 5);
148 }
149 else {
150 10 scratch0->pieces.length_type = CBOR_LENGTH_HUGE;
151  
152 10 _u64_to_buffer( num, 1 + encode_state->scratch );
153  
154 10 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 9);
155 }
156 17440 }
157  
158 static inline void _encode_tag( pTHX_ IV tagnum, SV *value, encode_ctx *encode_state );
159  
160 // Return indicates to encode the actual value.
161 7 bool _check_reference( pTHX_ SV *varref, encode_ctx *encode_state ) {
162 7 ▶ if ( SvREFCNT(varref) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvREFCNT(varref) > 1 ) { 6 1 7
163 void *this_ref;
164  
165 6 IV r = 0;
166  
167 12 ▶ while ( (this_ref = encode_state->reftracker[r++]) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (this_ref = encode_state->reftracker[r++]) ) { 9 3 12
168 9 ▶ if (this_ref == varref) {
Branch: 1 branch
BranchTrueFalseTotal
if (this_ref == varref) { 3 6 9
169 3 _init_length_buffer( aTHX_ CBOR_TAG_SHAREDREF, CBOR_TYPE_TAG, encode_state );
170 3 _init_length_buffer( aTHX_ r - 1, CBOR_TYPE_UINT, encode_state );
171 3 return false;
172 }
173 }
174  
175 3 ▶ Renew( encode_state->reftracker, 1 + r, void * );
Branch: 1 branch
BranchTrueFalseTotal
Renew( encode_state->reftracker, 1 + r, void * ); 0 3 3
176 3 encode_state->reftracker[r - 1] = varref;
177 3 encode_state->reftracker[r] = NULL;
178  
179 3 _init_length_buffer( aTHX_ CBOR_TAG_SHAREABLE, CBOR_TYPE_TAG, encode_state );
180 }
181  
182 4 return true;
183 }
184  
185 17429 void _encode( pTHX_ SV *value, encode_ctx *encode_state ) {
186 17429 ++encode_state->recurse_count;
187  
188 17429 ▶ if (encode_state->recurse_count > MAX_ENCODE_RECURSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (encode_state->recurse_count > MAX_ENCODE_RECURSE) { 2 17427 17429
189  
190 // call_pv() killed the process in Win32; this seems to fix that.
191 static char * words[] = { NULL };
192 2 call_argv("CBOR::Free::_die_recursion", G_EVAL|G_DISCARD, words);
193  
194 2 _croak_encode( encode_state, NULL );
195 }
196  
197 17427 ▶ if (!SvROK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(value)) { 17030 397 17427
198  
199 17030 ▶ if (SvIOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(value)) { 16951 79 17030
200 16951 IV val = SvIVX(value);
201  
202 // In testing, Perl’s (0 + ~0) evaluated as < 0 here,
203 // but the SvUOK() check fixes that.
204 16951 ▶ if (val < 0 && !SvUOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val < 0 && !SvUOK(value)) { 42 16909 16951
if (val < 0 && !SvUOK(value)) { 39 3 42
205 39 _init_length_buffer( aTHX_ -(++val), CBOR_TYPE_NEGINT, encode_state );
206 }
207 else {
208 // NB: SvUOK doesn’t work to identify nonnegatives … ?
209 16912 _init_length_buffer( aTHX_ val, CBOR_TYPE_UINT, encode_state );
210 }
211 }
212 79 ▶ else if (SvNOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(value)) { 25 54 79
213 25 NV val_nv = SvNVX(value);
214  
215 25 ▶ if (Perl_isnan(val_nv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Perl_isnan(val_nv)) { 3 22 25
216 3 _COPY_INTO_ENCODE(encode_state, CBOR_NAN_SHORT, 3);
217 }
218 22 ▶ else if (Perl_isinf(val_nv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (Perl_isinf(val_nv)) { 6 16 22
219 6 ▶ if (val_nv > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_nv > 0) { 3 3 6
220 3 _COPY_INTO_ENCODE(encode_state, CBOR_INF_SHORT, 3);
221 }
222 else {
223 3 _COPY_INTO_ENCODE(encode_state, CBOR_NEGINF_SHORT, 3);
224 }
225 }
226 else {
227  
228 // Typecast to a double to accommodate long-double perls.
229 16 double val = (double) val_nv;
230  
231 16 char *valptr = (char *) &val;
232  
233 #if IS_LITTLE_ENDIAN
234 16 encode_state->scratch[0] = CBOR_DOUBLE;
235 16 encode_state->scratch[1] = valptr[7];
236 16 encode_state->scratch[2] = valptr[6];
237 16 encode_state->scratch[3] = valptr[5];
238 16 encode_state->scratch[4] = valptr[4];
239 16 encode_state->scratch[5] = valptr[3];
240 16 encode_state->scratch[6] = valptr[2];
241 16 encode_state->scratch[7] = valptr[1];
242 16 encode_state->scratch[8] = valptr[0];
243  
244 16 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 9);
245 #else
246 char bytes[9] = { CBOR_DOUBLE, valptr[0], valptr[1], valptr[2], valptr[3], valptr[4], valptr[5], valptr[6], valptr[7] };
247 _COPY_INTO_ENCODE(encode_state, bytes, 9);
248 #endif
249 }
250 }
251 54 ▶ else if (!SvOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!SvOK(value)) { 10 44 54
252 10 _COPY_INTO_ENCODE(encode_state, &CBOR_NULL_U8, 1);
253 }
254 else {
255 44 ▶ char *val = SvPOK(value) ? SvPVX(value) : SvPV_nolen(value);
Branch: 1 branch
BranchTrueFalseTotal
char *val = SvPOK(value) ? SvPVX(value) : SvPV_nolen(value); 44 0 44
256  
257 44 STRLEN len = SvCUR(value);
258  
259 44 bool encode_as_text = !!SvUTF8(value);
260  
261 /*
262 if (!encode_as_text) {
263 STRLEN i;
264 for (i=0; i<len; i++) {
265 if (val[i] & 0x80) break;
266 }
267  
268 // Encode as text if there were no high-bit octets.
269 encode_as_text = (i == len);
270 }
271 */
272  
273 44 ▶ _init_length_buffer( aTHX_
Branch: 1 branch
BranchTrueFalseTotal
_init_length_buffer( aTHX_ 12 32 44
274 len,
275 (encode_as_text ? CBOR_TYPE_UTF8 : CBOR_TYPE_BINARY),
276 encode_state
277 );
278  
279 44 _COPY_INTO_ENCODE( encode_state, (unsigned char *) val, len );
280 }
281 }
282 397 ▶ else if (sv_isobject(value)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sv_isobject(value)) { 113 284 397
283 113 HV *stash = SvSTASH( SvRV(value) );
284  
285 113 ▶ if (_get_tagged_stash() == stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_tagged_stash() == stash) { 104 9 113
286 104 AV *array = (AV *)SvRV(value);
287 104 SV **tag = av_fetch(array, 0, 0);
288 104 IV tagnum = SvIV(*tag);
289  
290 104 _encode_tag( aTHX_ tagnum, *(av_fetch(array, 1, 0)), encode_state );
291 }
292 9 ▶ else if (cbf_get_boolean_stash() == stash) {
Branch: 1 branch
BranchTrueFalseTotal
else if (cbf_get_boolean_stash() == stash) { 8 1 9
293 8 ▶ _COPY_INTO_ENCODE(
Branch: 1 branch
BranchTrueFalseTotal
_COPY_INTO_ENCODE( 5 3 8
294 encode_state,
295 8 SvTRUE(SvRV(value)) ? &CBOR_TRUE_U8 : &CBOR_FALSE_U8,
296 1
297 );
298 }
299  
300 // TODO: Support TO_JSON() or TO_CBOR() method?
301  
302 1 else _croak_unrecognized(aTHX_ encode_state, value);
303 }
304 284 ▶ else if (SVt_PVAV == SvTYPE(SvRV(value))) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SVt_PVAV == SvTYPE(SvRV(value))) { 242 42 284
305 242 AV *array = (AV *)SvRV(value);
306  
307 242 ▶ if (!encode_state->reftracker || _check_reference( aTHX_ (SV *)array, encode_state )) {
Branch: 2 branches
BranchTrueFalseTotal
if (!encode_state->reftracker || _check_reference( aTHX_ (SV *)array, encode_state )) { 3 239 242
if (!encode_state->reftracker || _check_reference( aTHX_ (SV *)array, encode_state )) { 2 1 3
308 SSize_t len;
309 241 len = 1 + av_len(array);
310  
311 241 _init_length_buffer( aTHX_ len, CBOR_TYPE_ARRAY, encode_state );
312  
313 SSize_t i;
314  
315 SV **cur;
316 400 ▶ for (i=0; i<len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<len; i++) { 356 44 400
317 356 cur = av_fetch(array, i, 0);
318 356 _encode( aTHX_ *cur, encode_state );
319 }
320 }
321 }
322 42 ▶ else if (SVt_PVHV == SvTYPE(SvRV(value))) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SVt_PVHV == SvTYPE(SvRV(value))) { 31 11 42
323 31 HV *hash = (HV *)SvRV(value);
324  
325 31 ▶ if (!encode_state->reftracker || _check_reference( aTHX_ (SV *)hash, encode_state)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!encode_state->reftracker || _check_reference( aTHX_ (SV *)hash, encode_state)) { 2 29 31
if (!encode_state->reftracker || _check_reference( aTHX_ (SV *)hash, encode_state)) { 1 1 2
326 SV *cur_sv;
327 char *key;
328 STRLEN key_length;
329  
330 HE* h_entry;
331 SV* svkey;
332  
333 30 I32 keyscount = hv_iterinit(hash);
334  
335 30 _init_length_buffer( aTHX_ keyscount, CBOR_TYPE_MAP, encode_state );
336  
337 30 ▶ if (encode_state->is_canonical) {
Branch: 1 branch
BranchTrueFalseTotal
if (encode_state->is_canonical) { 9 21 30
338 9 I32 curkey = 0;
339  
340 9 struct sortable_hash_entry sortables[keyscount];
341  
342 42 ▶ while ( (h_entry = hv_iternext(hash)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (h_entry = hv_iternext(hash)) ) { 33 9 42
343 33 ▶ if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) {
Branch: 11 branches
BranchTrueFalseTotal
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 33 33
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 0 0
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 32 1 33
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 26 6 32
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 26 26
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 0 0
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 26 0 26
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 26 0 26
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 26 26
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 0 0
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 22 4 26
344 23 ▶ key = HePV(h_entry, key_length);
Branch: 1 branch
BranchTrueFalseTotal
key = HePV(h_entry, key_length); 0 23 23
345 23 ▶ sortables[curkey].is_utf8 = !!HeUTF8(h_entry);
Branch: 1 branch
BranchTrueFalseTotal
sortables[curkey].is_utf8 = !!HeUTF8(h_entry); 0 23 23
346 23 sortables[curkey].buffer = key;
347 23 sortables[curkey].length = key_length;
348 }
349 else {
350 10 ▶ SV* key_sv = HeSVKEY_force(h_entry);
Branch: 2 branches
BranchTrueFalseTotal
SV* key_sv = HeSVKEY_force(h_entry); 10 0 10
SV* key_sv = HeSVKEY_force(h_entry); 0 10 10
351 10 sv_utf8_upgrade(key_sv);
352 10 sortables[curkey].is_utf8 = true;
353  
354 20 sortables[curkey].buffer = SvPV(key_sv, sortables[curkey].length);
355 }
356  
357 33 sortables[curkey].value = HeVAL(h_entry);
358  
359 33 curkey++;
360 }
361  
362 9 qsort(sortables, keyscount, sizeof(struct sortable_hash_entry), _sort_map_keys);
363  
364 42 ▶ for (curkey=0; curkey < keyscount; ++curkey) {
Branch: 1 branch
BranchTrueFalseTotal
for (curkey=0; curkey < keyscount; ++curkey) { 33 9 42
365 33 ▶ _init_length_buffer( aTHX_ sortables[curkey].length, sortables[curkey].is_utf8 ? CBOR_TYPE_UTF8 : CBOR_TYPE_BINARY, encode_state );
Branch: 1 branch
BranchTrueFalseTotal
_init_length_buffer( aTHX_ sortables[curkey].length, sortables[curkey].is_utf8 ? CBOR_TYPE_UTF8 : CBOR_TYPE_BINARY, encode_state ); 11 22 33
366 33 _COPY_INTO_ENCODE( encode_state, (unsigned char *) sortables[curkey].buffer, sortables[curkey].length );
367  
368 33 _encode( aTHX_ sortables[curkey].value, encode_state );
369 }
370 }
371 else {
372 45 ▶ while ( (h_entry = hv_iternext(hash)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (h_entry = hv_iternext(hash)) ) { 24 21 45
373  
374 // If the key is HeUTF8, then we can use it as-is.
375 // (This will yield a CBOR text key.)
376 //
377 // If the key is !CBF_HeUTF8 and we aren’t in text-keys
378 // mode, then use the key as-is.
379 // (This will yield a CBOR binary key.)
380 //
381 // If the key is !HeUTF8 but IS CBF_HeUTF8, then we
382 // force an SV conversion and upgrade to UTF-8.
383 //
384 // Likewise, if the key is !HeUTF8 but we are in text-keys
385 // mode, we need the same conversion.
386  
387 24 ▶ if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) {
Branch: 11 branches
BranchTrueFalseTotal
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 24 24
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 0 0
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 22 2 24
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 18 4 22
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 18 18
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 0 0
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 18 0 18
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 18 0 18
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 18 18
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 0 0 0
if (HeUTF8(h_entry) || (!encode_state->text_keys && !CBF_HeUTF8(h_entry))) { 17 1 18
388 19 ▶ key = HePV(h_entry, key_length);
Branch: 1 branch
BranchTrueFalseTotal
key = HePV(h_entry, key_length); 0 19 19
389  
390 19 ▶ _init_length_buffer( aTHX_ key_length, HeUTF8(h_entry) ? CBOR_TYPE_UTF8 : CBOR_TYPE_BINARY, encode_state );
Branch: 2 branches
BranchTrueFalseTotal
_init_length_buffer( aTHX_ key_length, HeUTF8(h_entry) ? CBOR_TYPE_UTF8 : CBOR_TYPE_BINARY, encode_state ); 0 19 19
_init_length_buffer( aTHX_ key_length, HeUTF8(h_entry) ? CBOR_TYPE_UTF8 : CBOR_TYPE_BINARY, encode_state ); 2 17 19
391 19 _COPY_INTO_ENCODE( encode_state, (unsigned char *) key, key_length );
392 }
393 else {
394 5 ▶ SV* key_sv = HeSVKEY_force(h_entry);
Branch: 2 branches
BranchTrueFalseTotal
SV* key_sv = HeSVKEY_force(h_entry); 5 0 5
SV* key_sv = HeSVKEY_force(h_entry); 0 5 5
395 5 sv_utf8_upgrade(key_sv);
396 5 _encode( aTHX_ key_sv, encode_state );
397 }
398  
399 24 _encode( aTHX_ HeVAL(h_entry), encode_state );
400 }
401 }
402 }
403 }
404 21 ▶ else if (encode_state->encode_scalar_refs && IS_SCALAR_REFERENCE(value)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (encode_state->encode_scalar_refs && IS_SCALAR_REFERENCE(value)) { 10 1 11
else if (encode_state->encode_scalar_refs && IS_SCALAR_REFERENCE(value)) { 10 0 10
405 10 SV *referent = SvRV(value);
406  
407 10 ▶ if (!encode_state->reftracker || _check_reference( aTHX_ referent, encode_state)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!encode_state->reftracker || _check_reference( aTHX_ referent, encode_state)) { 2 8 10
if (!encode_state->reftracker || _check_reference( aTHX_ referent, encode_state)) { 1 1 2
408 9 _encode_tag( aTHX_ CBOR_TAG_INDIRECTION, referent, encode_state );
409 }
410 }
411 else {
412 1 _croak_unrecognized(aTHX_ encode_state, value);
413 }
414  
415 17228 --encode_state->recurse_count;
416 17228 }
417  
418 113 static inline void _encode_tag( pTHX_ IV tagnum, SV *value, encode_ctx *encode_state ) {
419 113 _init_length_buffer( aTHX_ tagnum, CBOR_TYPE_TAG, encode_state );
420 113 _encode( aTHX_ value, encode_state );
421 113 }
422  
423 //----------------------------------------------------------------------
424  
425 16898 encode_ctx cbf_encode_ctx_create(uint8_t flags) {
426 encode_ctx encode_state;
427  
428 16898 encode_state.buffer = NULL;
429 16898 Newx( encode_state.buffer, ENCODE_ALLOC_CHUNK_SIZE, char );
430  
431 16898 encode_state.buflen = ENCODE_ALLOC_CHUNK_SIZE;
432 16898 encode_state.len = 0;
433 16898 encode_state.recurse_count = 0;
434  
435 16898 encode_state.is_canonical = !!(flags & ENCODE_FLAG_CANONICAL);
436  
437 16898 encode_state.text_keys = !!(flags & ENCODE_FLAG_TEXT_KEYS);
438  
439 16898 encode_state.encode_scalar_refs = !!(flags & ENCODE_FLAG_SCALAR_REFS);
440  
441 16898 ▶ if (flags & ENCODE_FLAG_PRESERVE_REFS) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & ENCODE_FLAG_PRESERVE_REFS) { 1 16897 16898
442 1 Newxz( encode_state.reftracker, 1, void * );
443 }
444 else {
445 16897 encode_state.reftracker = NULL;
446 }
447  
448 16898 return encode_state;
449 }
450  
451 16898 void cbf_encode_ctx_free_reftracker(encode_ctx* encode_state) {
452 16898 Safefree( encode_state->reftracker );
453 16898 }
454  
455 4 void cbf_encode_ctx_free_all(encode_ctx* encode_state) {
456 4 cbf_encode_ctx_free_reftracker(encode_state);
457 4 Safefree( encode_state->buffer );
458 4 }
459  
460 16898 SV *cbf_encode( pTHX_ SV *value, encode_ctx *encode_state, SV *RETVAL ) {
461 16898 _encode(aTHX_ value, encode_state);
462  
463 // Ensure that there’s a trailing NUL:
464 16894 _COPY_INTO_ENCODE( encode_state, &NUL, 1 );
465  
466 16894 return RETVAL;
467 }
« Free.xs ↑ Summary cbor_free_decode.c »