cbor_free_encode.c

filter: Statementstmt 98.9% 188 / 190 Branchbran 95.4% 84 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 97.8% 272 / 278 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/CBOR/Free.pm ↑ Summary blib/lib/CBOR/Free/Tagged.pm »
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 20 int _sort_string_and_length( const void* a, const void* b ) {
78 return (
79 20 ((struct string_and_length *)a)->length < ((struct string_and_length *)b)->length ? -1
80 36 ▶ : ((struct string_and_length *)a)->length > ((struct string_and_length *)b)->length ? 1
Branch: 1 branch
BranchTrueFalseTotal
: ((struct string_and_length *)a)->length > ((struct string_and_length *)b)->length ? 1 16 4 20
81 16 ▶ : memcmp( ((struct string_and_length *)a)->buffer, ((struct string_and_length *)b)->buffer, ((struct string_and_length *)a)->length )
Branch: 1 branch
BranchTrueFalseTotal
: memcmp( ((struct string_and_length *)a)->buffer, ((struct string_and_length *)b)->buffer, ((struct string_and_length *)a)->length ) 6 10 16
82 );
83 }
84  
85 //----------------------------------------------------------------------
86  
87 113 static inline HV *_get_tagged_stash() {
88 113 ▶ if (!tagged_stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tagged_stash) { 5 108 113
89 dTHX;
90 5 tagged_stash = gv_stashpv(TAGGED_CLASS, 1);
91 }
92  
93 113 return tagged_stash;
94 }
95  
96 34373 static inline void _COPY_INTO_ENCODE( encode_ctx *encode_state, const unsigned char *hdr, STRLEN len) {
97 34373 ▶ if ( (len + encode_state->len) > encode_state->buflen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (len + encode_state->len) > encode_state->buflen ) { 0 34373 34373
98 0 Renew( encode_state->buffer, encode_state->buflen + len + ENCODE_ALLOC_CHUNK_SIZE, char );
99 0 encode_state->buflen += len + ENCODE_ALLOC_CHUNK_SIZE;
100 }
101  
102 34373 Copy( hdr, encode_state->buffer + encode_state->len, len, char );
103 34373 encode_state->len += len;
104 34373 }
105  
106 // TODO? This could be a macro … it’d just be kind of unwieldy as such.
107 17379 static inline void _init_length_buffer( pTHX_ UV num, enum CBOR_TYPE major_type, encode_ctx *encode_state ) {
108 //fprintf(stderr, "_init_length_buffer(%llu)\n", num);
109 17379 union control_byte *scratch0 = (void *) encode_state->scratch;
110 17379 scratch0->pieces.major_type = major_type;
111  
112 17379 ▶ if ( num < CBOR_LENGTH_SMALL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( num < CBOR_LENGTH_SMALL ) { 575 16804 17379
113 //fprintf(stderr, "size tiny\n");
114 575 scratch0->pieces.length_type = (uint8_t) num;
115  
116 575 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 1);
117 }
118 16804 ▶ else if ( num <= 0xff ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( num <= 0xff ) { 593 16211 16804
119 //fprintf(stderr, "size small\n");
120 593 scratch0->pieces.length_type = CBOR_LENGTH_SMALL;
121 593 encode_state->scratch[1] = (uint8_t) num;
122 //fprintf(stderr, "uint8: %02x,%02x\n", encode_state->scratch[0], encode_state->scratch[1]);
123  
124 593 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 2);
125 }
126 16211 ▶ else if ( num <= 0xffff ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( num <= 0xffff ) { 15890 321 16211
127 15890 scratch0->pieces.length_type = CBOR_LENGTH_MEDIUM;
128  
129 15890 _u16_to_buffer( num, 1 + encode_state->scratch );
130  
131 15890 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 3);
132 }
133 321 ▶ else if ( num <= 0xffffffffU ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( num <= 0xffffffffU ) { 311 10 321
134 311 scratch0->pieces.length_type = CBOR_LENGTH_LARGE;
135  
136 311 _u32_to_buffer( num, 1 + encode_state->scratch );
137  
138 311 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 5);
139 }
140 else {
141 10 scratch0->pieces.length_type = CBOR_LENGTH_HUGE;
142  
143 10 _u64_to_buffer( num, 1 + encode_state->scratch );
144  
145 10 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 9);
146 }
147 17379 }
148  
149 static inline void _encode_tag( pTHX_ IV tagnum, SV *value, encode_ctx *encode_state );
150  
151 // Return indicates to encode the actual value.
152 7 bool _check_reference( pTHX_ SV *varref, encode_ctx *encode_state ) {
153 7 ▶ if ( SvREFCNT(varref) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvREFCNT(varref) > 1 ) { 6 1 7
154 void *this_ref;
155  
156 6 IV r = 0;
157  
158 12 ▶ while ( (this_ref = encode_state->reftracker[r++]) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (this_ref = encode_state->reftracker[r++]) ) { 9 3 12
159 9 ▶ if (this_ref == varref) {
Branch: 1 branch
BranchTrueFalseTotal
if (this_ref == varref) { 3 6 9
160 3 _init_length_buffer( aTHX_ CBOR_TAG_SHAREDREF, CBOR_TYPE_TAG, encode_state );
161 3 _init_length_buffer( aTHX_ r - 1, CBOR_TYPE_UINT, encode_state );
162 3 return false;
163 }
164 }
165  
166 3 ▶ Renew( encode_state->reftracker, 1 + r, void * );
Branch: 1 branch
BranchTrueFalseTotal
Renew( encode_state->reftracker, 1 + r, void * ); 0 3 3
167 3 encode_state->reftracker[r - 1] = varref;
168 3 encode_state->reftracker[r] = NULL;
169  
170 3 _init_length_buffer( aTHX_ CBOR_TAG_SHAREABLE, CBOR_TYPE_TAG, encode_state );
171 }
172  
173 4 return true;
174 }
175  
176 17387 void _encode( pTHX_ SV *value, encode_ctx *encode_state ) {
177 17387 ++encode_state->recurse_count;
178  
179 17387 ▶ if (encode_state->recurse_count > MAX_ENCODE_RECURSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (encode_state->recurse_count > MAX_ENCODE_RECURSE) { 2 17385 17387
180  
181 // call_pv() killed the process in Win32; this seems to fix that.
182 static char * words[] = { NULL };
183 2 call_argv("CBOR::Free::_die_recursion", G_EVAL|G_DISCARD, words);
184  
185 2 _croak_encode( encode_state, NULL );
186 }
187  
188 17385 ▶ if (!SvROK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(value)) { 17000 385 17385
189  
190 17000 ▶ if (SvIOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(value)) { 16928 72 17000
191 16928 IV val = SvIVX(value);
192  
193 // In testing, Perl’s (0 + ~0) evaluated as < 0 here,
194 // but the SvUOK() check fixes that.
195 16928 ▶ if (val < 0 && !SvUOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val < 0 && !SvUOK(value)) { 42 16886 16928
if (val < 0 && !SvUOK(value)) { 39 3 42
196 39 _init_length_buffer( aTHX_ -(++val), CBOR_TYPE_NEGINT, encode_state );
197 }
198 else {
199 // NB: SvUOK doesn’t work to identify nonnegatives … ?
200 16889 _init_length_buffer( aTHX_ val, CBOR_TYPE_UINT, encode_state );
201 }
202 }
203 72 ▶ else if (SvNOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(value)) { 25 47 72
204 25 NV val_nv = SvNVX(value);
205  
206 25 ▶ if (Perl_isnan(val_nv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Perl_isnan(val_nv)) { 3 22 25
207 3 _COPY_INTO_ENCODE(encode_state, CBOR_NAN_SHORT, 3);
208 }
209 22 ▶ else if (Perl_isinf(val_nv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (Perl_isinf(val_nv)) { 6 16 22
210 6 ▶ if (val_nv > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_nv > 0) { 3 3 6
211 3 _COPY_INTO_ENCODE(encode_state, CBOR_INF_SHORT, 3);
212 }
213 else {
214 3 _COPY_INTO_ENCODE(encode_state, CBOR_NEGINF_SHORT, 3);
215 }
216 }
217 else {
218  
219 // Typecast to a double to accommodate long-double perls.
220 16 double val = (double) val_nv;
221  
222 16 char *valptr = (char *) &val;
223  
224 #if IS_LITTLE_ENDIAN
225 16 encode_state->scratch[0] = CBOR_DOUBLE;
226 16 encode_state->scratch[1] = valptr[7];
227 16 encode_state->scratch[2] = valptr[6];
228 16 encode_state->scratch[3] = valptr[5];
229 16 encode_state->scratch[4] = valptr[4];
230 16 encode_state->scratch[5] = valptr[3];
231 16 encode_state->scratch[6] = valptr[2];
232 16 encode_state->scratch[7] = valptr[1];
233 16 encode_state->scratch[8] = valptr[0];
234  
235 16 _COPY_INTO_ENCODE(encode_state, encode_state->scratch, 9);
236 #else
237 char bytes[9] = { CBOR_DOUBLE, valptr[0], valptr[1], valptr[2], valptr[3], valptr[4], valptr[5], valptr[6], valptr[7] };
238 _COPY_INTO_ENCODE(encode_state, bytes, 9);
239 #endif
240 }
241 }
242 47 ▶ else if (!SvOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!SvOK(value)) { 10 37 47
243 10 _COPY_INTO_ENCODE(encode_state, &CBOR_NULL_U8, 1);
244 }
245 else {
246 37 ▶ char *val = SvPOK(value) ? SvPVX(value) : SvPV_nolen(value);
Branch: 1 branch
BranchTrueFalseTotal
char *val = SvPOK(value) ? SvPVX(value) : SvPV_nolen(value); 37 0 37
247  
248 37 STRLEN len = SvCUR(value);
249  
250 37 bool encode_as_text = !!SvUTF8(value);
251  
252 /*
253 if (!encode_as_text) {
254 STRLEN i;
255 for (i=0; i<len; i++) {
256 if (val[i] & 0x80) break;
257 }
258  
259 // Encode as text if there were no high-bit octets.
260 encode_as_text = (i == len);
261 }
262 */
263  
264 37 ▶ _init_length_buffer( aTHX_
Branch: 1 branch
BranchTrueFalseTotal
_init_length_buffer( aTHX_ 6 31 37
265 len,
266 (encode_as_text ? CBOR_TYPE_UTF8 : CBOR_TYPE_BINARY),
267 encode_state
268 );
269  
270 37 _COPY_INTO_ENCODE( encode_state, (unsigned char *) val, len );
271 }
272 }
273 385 ▶ else if (sv_isobject(value)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sv_isobject(value)) { 113 272 385
274 113 HV *stash = SvSTASH( SvRV(value) );
275  
276 113 ▶ if (_get_tagged_stash() == stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_tagged_stash() == stash) { 104 9 113
277 104 AV *array = (AV *)SvRV(value);
278 104 SV **tag = av_fetch(array, 0, 0);
279 104 IV tagnum = SvIV(*tag);
280  
281 104 _encode_tag( aTHX_ tagnum, *(av_fetch(array, 1, 0)), encode_state );
282 }
283 9 ▶ else if (cbf_get_boolean_stash() == stash) {
Branch: 1 branch
BranchTrueFalseTotal
else if (cbf_get_boolean_stash() == stash) { 8 1 9
284 8 ▶ _COPY_INTO_ENCODE(
Branch: 1 branch
BranchTrueFalseTotal
_COPY_INTO_ENCODE( 5 3 8
285 encode_state,
286 8 SvTRUE(SvRV(value)) ? &CBOR_TRUE_U8 : &CBOR_FALSE_U8,
287 1
288 );
289 }
290  
291 // TODO: Support TO_JSON() or TO_CBOR() method?
292  
293 1 else _croak_unrecognized(aTHX_ encode_state, value);
294 }
295 272 ▶ else if (SVt_PVAV == SvTYPE(SvRV(value))) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SVt_PVAV == SvTYPE(SvRV(value))) { 242 30 272
296 242 AV *array = (AV *)SvRV(value);
297  
298 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
299 SSize_t len;
300 241 len = 1 + av_len(array);
301  
302 241 _init_length_buffer( aTHX_ len, CBOR_TYPE_ARRAY, encode_state );
303  
304 SSize_t i;
305  
306 SV **cur;
307 400 ▶ for (i=0; i<len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<len; i++) { 356 44 400
308 356 cur = av_fetch(array, i, 0);
309 356 _encode( aTHX_ *cur, encode_state );
310 }
311 }
312 }
313 30 ▶ else if (SVt_PVHV == SvTYPE(SvRV(value))) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SVt_PVHV == SvTYPE(SvRV(value))) { 19 11 30
314 19 HV *hash = (HV *)SvRV(value);
315  
316 19 ▶ 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 17 19
if (!encode_state->reftracker || _check_reference( aTHX_ (SV *)hash, encode_state)) { 1 1 2
317 SV *cur_sv;
318 char *key;
319 I32 key_length;
320  
321 18 I32 keyscount = hv_iterinit(hash);
322  
323 18 _init_length_buffer( aTHX_ keyscount, CBOR_TYPE_MAP, encode_state );
324  
325 18 ▶ if (encode_state->is_canonical) {
Branch: 1 branch
BranchTrueFalseTotal
if (encode_state->is_canonical) { 4 14 18
326  
327 // The CBOR RFC defines canonical sorting such that the
328 // *encoded* keys are what gets sorted; however, since Perl hash
329 // keys are always (binary) strings, and since a lexicographical
330 // sort of encoded CBOR uints yields the same order as encoding
331 // that same list of uints pre-sorted, we can sort the keys
332 // prior to insertion into the output buffer.
333 //
334 // It may be faster to sort encoded keys (as the RFC envisions),
335 // but this works for now.
336  
337 struct string_and_length cur;
338 4 struct string_and_length strings[keyscount];
339  
340 4 I32 curkey = 0;
341  
342 20 ▶ while (hv_iternextsv(hash, &key, &key_length)) {
Branch: 1 branch
BranchTrueFalseTotal
while (hv_iternextsv(hash, &key, &key_length)) { 16 4 20
343 16 strings[curkey].buffer = key;
344 16 strings[curkey].length = key_length;
345 16 ++curkey;
346 }
347  
348 4 qsort(strings, keyscount, sizeof(struct string_and_length), _sort_string_and_length);
349  
350 20 ▶ for (curkey=0; curkey < keyscount; ++curkey) {
Branch: 1 branch
BranchTrueFalseTotal
for (curkey=0; curkey < keyscount; ++curkey) { 16 4 20
351 16 cur = strings[curkey];
352 16 key = cur.buffer;
353 16 key_length = cur.length;
354  
355 // Store the key.
356 16 _init_length_buffer( aTHX_ key_length, CBOR_TYPE_BINARY, encode_state );
357 16 _COPY_INTO_ENCODE( encode_state, (unsigned char *) key, key_length );
358  
359 16 cur_sv = *( hv_fetch(hash, key, key_length, 0) );
360  
361 16 _encode( aTHX_ cur_sv, encode_state );
362 }
363 }
364 else {
365 31 ▶ while ((cur_sv = hv_iternextsv(hash, &key, &key_length))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((cur_sv = hv_iternextsv(hash, &key, &key_length))) { 17 14 31
366  
367 // Store the key.
368 17 _init_length_buffer( aTHX_ key_length, CBOR_TYPE_BINARY, encode_state );
369  
370 17 _COPY_INTO_ENCODE( encode_state, (unsigned char *) key, key_length );
371  
372 17 _encode( aTHX_ cur_sv, encode_state );
373 }
374 }
375 }
376 }
377 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
378 10 SV *referent = SvRV(value);
379  
380 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
381 9 _encode_tag( aTHX_ CBOR_TAG_INDIRECTION, referent, encode_state );
382 }
383 }
384 else {
385 1 _croak_unrecognized(aTHX_ encode_state, value);
386 }
387  
388 17186 --encode_state->recurse_count;
389 17186 }
390  
391 113 static inline void _encode_tag( pTHX_ IV tagnum, SV *value, encode_ctx *encode_state ) {
392 113 _init_length_buffer( aTHX_ tagnum, CBOR_TYPE_TAG, encode_state );
393 113 _encode( aTHX_ value, encode_state );
394 113 }
395  
396 //----------------------------------------------------------------------
397  
398 16885 encode_ctx cbf_encode_ctx_create(uint8_t flags) {
399 encode_ctx encode_state;
400  
401 16885 encode_state.buffer = NULL;
402 16885 Newx( encode_state.buffer, ENCODE_ALLOC_CHUNK_SIZE, char );
403  
404 16885 encode_state.buflen = ENCODE_ALLOC_CHUNK_SIZE;
405 16885 encode_state.len = 0;
406 16885 encode_state.recurse_count = 0;
407  
408 16885 encode_state.is_canonical = !!(flags & ENCODE_FLAG_CANONICAL);
409  
410 16885 encode_state.encode_scalar_refs = !!(flags & ENCODE_FLAG_SCALAR_REFS);
411  
412 16885 ▶ if (flags & ENCODE_FLAG_PRESERVE_REFS) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & ENCODE_FLAG_PRESERVE_REFS) { 1 16884 16885
413 1 Newxz( encode_state.reftracker, 1, void * );
414 }
415 else {
416 16884 encode_state.reftracker = NULL;
417 }
418  
419 16885 return encode_state;
420 }
421  
422 16885 void cbf_encode_ctx_free_reftracker(encode_ctx* encode_state) {
423 16885 Safefree( encode_state->reftracker );
424 16885 }
425  
426 4 void cbf_encode_ctx_free_all(encode_ctx* encode_state) {
427 4 cbf_encode_ctx_free_reftracker(encode_state);
428 4 Safefree( encode_state->buffer );
429 4 }
430  
431 16885 SV *cbf_encode( pTHX_ SV *value, encode_ctx *encode_state, SV *RETVAL ) {
432 16885 _encode(aTHX_ value, encode_state);
433  
434 // Ensure that there’s a trailing NUL:
435 16881 _COPY_INTO_ENCODE( encode_state, &NUL, 1 );
436  
437 16881 return RETVAL;
438 }
« blib/lib/CBOR/Free.pm ↑ Summary blib/lib/CBOR/Free/Tagged.pm »