ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
Revision: 1.16
Committed: Tue Oct 29 20:59:16 2013 UTC (10 years, 6 months ago) by root
Branch: MAIN
Changes since 1.15: +46 -51 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <assert.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <limits.h>
10 #include <float.h>
11
12 #include "ecb.h"
13
14 // compatibility with perl <5.18
15 #ifndef HvNAMELEN_get
16 # define HvNAMELEN_get(hv) strlen (HvNAME (hv))
17 #endif
18 #ifndef HvNAMELEN
19 # define HvNAMELEN(hv) HvNAMELEN_get (hv)
20 #endif
21 #ifndef HvNAMEUTF8
22 # define HvNAMEUTF8(hv) 0
23 #endif
24
25 // known tags
26 enum cbor_tag
27 {
28 // inofficial extensions (pending iana registration)
29 CBOR_TAG_PERL_OBJECT = 256,
30 CBOR_TAG_GENERIC_OBJECT = 257,
31
32 // rfc7049
33 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8
34 CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any
35 CBOR_TAG_POS_BIGNUM = 2, // byte string
36 CBOR_TAG_NEG_BIGNUM = 3, // byte string
37 CBOR_TAG_DECIMAL = 4, // decimal fraction, array
38 CBOR_TAG_BIGFLOAT = 5, // array
39
40 CBOR_TAG_CONV_B64U = 21, // base64url, any
41 CBOR_TAG_CONV_B64 = 22, // base64, any
42 CBOR_TAG_CONV_HEX = 23, // base16, any
43 CBOR_TAG_CBOR = 24, // embedded cbor, byte string
44
45 CBOR_TAG_URI = 32, // URI rfc3986, utf-8
46 CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8
47 CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8
48 CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8
49 CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8
50
51 CBOR_TAG_MAGIC = 55799 // self-describe cbor
52 };
53
54 #define F_SHRINK 0x00000200UL
55 #define F_ALLOW_UNKNOWN 0x00002000UL
56
57 #define INIT_SIZE 32 // initial scalar size to be allocated
58
59 #define SB do {
60 #define SE } while (0)
61
62 #define IN_RANGE_INC(type,val,beg,end) \
63 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
64 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
65
66 #define ERR_NESTING_EXCEEDED "cbor text or perl structure exceeds maximum nesting level (max_depth set too low?)"
67
68 #ifdef USE_ITHREADS
69 # define CBOR_SLOW 1
70 # define CBOR_STASH (cbor_stash ? cbor_stash : gv_stashpv ("CBOR::XS", 1))
71 #else
72 # define CBOR_SLOW 0
73 # define CBOR_STASH cbor_stash
74 #endif
75
76 static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS::
77 static SV *types_true, *types_false, *types_error, *sv_cbor;
78
79 typedef struct {
80 U32 flags;
81 U32 max_depth;
82 STRLEN max_size;
83 } CBOR;
84
85 ecb_inline void
86 cbor_init (CBOR *cbor)
87 {
88 Zero (cbor, 1, CBOR);
89 cbor->max_depth = 512;
90 }
91
92 /////////////////////////////////////////////////////////////////////////////
93 // utility functions
94
95 ecb_inline SV *
96 get_bool (const char *name)
97 {
98 SV *sv = get_sv (name, 1);
99
100 SvREADONLY_on (sv);
101 SvREADONLY_on (SvRV (sv));
102
103 return sv;
104 }
105
106 ecb_inline void
107 shrink (SV *sv)
108 {
109 sv_utf8_downgrade (sv, 1);
110
111 if (SvLEN (sv) > SvCUR (sv) + 1)
112 {
113 #ifdef SvPV_shrink_to_cur
114 SvPV_shrink_to_cur (sv);
115 #elif defined (SvPV_renew)
116 SvPV_renew (sv, SvCUR (sv) + 1);
117 #endif
118 }
119 }
120
121 /////////////////////////////////////////////////////////////////////////////
122 // fp hell
123
124 //TODO
125
126 /////////////////////////////////////////////////////////////////////////////
127 // encoder
128
129 // structure used for encoding CBOR
130 typedef struct
131 {
132 char *cur; // SvPVX (sv) + current output position
133 char *end; // SvEND (sv)
134 SV *sv; // result scalar
135 CBOR cbor;
136 U32 depth; // recursion level
137 } enc_t;
138
139 ecb_inline void
140 need (enc_t *enc, STRLEN len)
141 {
142 if (ecb_expect_false (enc->cur + len >= enc->end))
143 {
144 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
145 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
146 enc->cur = SvPVX (enc->sv) + cur;
147 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
148 }
149 }
150
151 ecb_inline void
152 encode_ch (enc_t *enc, char ch)
153 {
154 need (enc, 1);
155 *enc->cur++ = ch;
156 }
157
158 static void
159 encode_uint (enc_t *enc, int major, UV len)
160 {
161 need (enc, 9);
162
163 if (len < 24)
164 *enc->cur++ = major | len;
165 else if (len <= 0xff)
166 {
167 *enc->cur++ = major | 24;
168 *enc->cur++ = len;
169 }
170 else if (len <= 0xffff)
171 {
172 *enc->cur++ = major | 25;
173 *enc->cur++ = len >> 8;
174 *enc->cur++ = len;
175 }
176 else if (len <= 0xffffffff)
177 {
178 *enc->cur++ = major | 26;
179 *enc->cur++ = len >> 24;
180 *enc->cur++ = len >> 16;
181 *enc->cur++ = len >> 8;
182 *enc->cur++ = len;
183 }
184 else
185 {
186 *enc->cur++ = major | 27;
187 *enc->cur++ = len >> 56;
188 *enc->cur++ = len >> 48;
189 *enc->cur++ = len >> 40;
190 *enc->cur++ = len >> 32;
191 *enc->cur++ = len >> 24;
192 *enc->cur++ = len >> 16;
193 *enc->cur++ = len >> 8;
194 *enc->cur++ = len;
195 }
196 }
197
198 static void
199 encode_str (enc_t *enc, int utf8, char *str, STRLEN len)
200 {
201 encode_uint (enc, utf8 ? 0x60 : 0x40, len);
202 need (enc, len);
203 memcpy (enc->cur, str, len);
204 enc->cur += len;
205 }
206
207 static void encode_sv (enc_t *enc, SV *sv);
208
209 static void
210 encode_av (enc_t *enc, AV *av)
211 {
212 int i, len = av_len (av);
213
214 if (enc->depth >= enc->cbor.max_depth)
215 croak (ERR_NESTING_EXCEEDED);
216
217 ++enc->depth;
218
219 encode_uint (enc, 0x80, len + 1);
220
221 for (i = 0; i <= len; ++i)
222 {
223 SV **svp = av_fetch (av, i, 0);
224 encode_sv (enc, svp ? *svp : &PL_sv_undef);
225 }
226
227 --enc->depth;
228 }
229
230 static void
231 encode_hv (enc_t *enc, HV *hv)
232 {
233 HE *he;
234
235 if (enc->depth >= enc->cbor.max_depth)
236 croak (ERR_NESTING_EXCEEDED);
237
238 ++enc->depth;
239
240 int pairs = hv_iterinit (hv);
241 int mg = SvMAGICAL (hv);
242
243 if (mg)
244 encode_ch (enc, 0xa0 | 31);
245 else
246 encode_uint (enc, 0xa0, pairs);
247
248 while ((he = hv_iternext (hv)))
249 {
250 if (HeKLEN (he) == HEf_SVKEY)
251 encode_sv (enc, HeSVKEY (he));
252 else
253 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he));
254
255 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
256 }
257
258 if (mg)
259 encode_ch (enc, 0xe0 | 31);
260
261 --enc->depth;
262 }
263
264 // encode objects, arrays and special \0=false and \1=true values.
265 static void
266 encode_rv (enc_t *enc, SV *sv)
267 {
268 svtype svt;
269
270 SvGETMAGIC (sv);
271 svt = SvTYPE (sv);
272
273 if (ecb_expect_false (SvOBJECT (sv)))
274 {
275 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash
276 ? types_boolean_stash
277 : gv_stashpv ("Types::Serialiser::Boolean", 1);
278 HV *error_stash = !CBOR_SLOW || types_error_stash
279 ? types_error_stash
280 : gv_stashpv ("Types::Serialiser::Error", 1);
281 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
282 ? cbor_tagged_stash
283 : gv_stashpv ("CBOR::XS::Tagged" , 1);
284
285 HV *stash = SvSTASH (sv);
286 GV *method;
287
288 if (stash == boolean_stash)
289 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20);
290 else if (stash == error_stash)
291 encode_ch (enc, 0xe0 | 23);
292 else if (stash == tagged_stash)
293 {
294 if (svt != SVt_PVAV)
295 croak ("encountered CBOR::XS::Tagged object that isn't an array");
296
297 encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1)));
298 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
299 }
300 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
301 {
302 dSP;
303
304 ENTER; SAVETMPS; PUSHMARK (SP);
305 // we re-bless the reference to get overload and other niceties right
306 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
307
308 PUTBACK;
309 // G_SCALAR ensures that return value is 1
310 call_sv ((SV *)GvCV (method), G_SCALAR);
311 SPAGAIN;
312
313 // catch this surprisingly common error
314 if (SvROK (TOPs) && SvRV (TOPs) == sv)
315 croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash));
316
317 encode_sv (enc, POPs);
318
319 PUTBACK;
320
321 FREETMPS; LEAVE;
322 }
323 else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0)
324 {
325 dSP;
326
327 ENTER; SAVETMPS; PUSHMARK (SP);
328 EXTEND (SP, 2);
329 // we re-bless the reference to get overload and other niceties right
330 PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash));
331 PUSHs (sv_cbor);
332
333 PUTBACK;
334 int count = call_sv ((SV *)GvCV (method), G_ARRAY);
335 SPAGAIN;
336
337 // catch this surprisingly common error
338 if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv)
339 croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash));
340
341 encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT);
342 encode_uint (enc, 0x80, count + 1);
343 encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash));
344
345 while (count)
346 encode_sv (enc, SP[1 - count--]);
347
348 PUTBACK;
349
350 FREETMPS; LEAVE;
351 }
352 else
353 croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it",
354 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
355 }
356 else if (svt == SVt_PVHV)
357 encode_hv (enc, (HV *)sv);
358 else if (svt == SVt_PVAV)
359 encode_av (enc, (AV *)sv);
360 else if (svt < SVt_PVAV)
361 {
362 STRLEN len = 0;
363 char *pv = svt ? SvPV (sv, len) : 0;
364
365 if (len == 1 && *pv == '1')
366 encode_ch (enc, 0xe0 | 21);
367 else if (len == 1 && *pv == '0')
368 encode_ch (enc, 0xe0 | 20);
369 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
370 encode_ch (enc, 0xe0 | 23);
371 else
372 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
373 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
374 }
375 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
376 encode_ch (enc, 0xe0 | 23);
377 else
378 croak ("encountered %s, but CBOR can only represent references to arrays or hashes",
379 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
380 }
381
382 static void
383 encode_nv (enc_t *enc, SV *sv)
384 {
385 double nv = SvNVX (sv);
386
387 need (enc, 9);
388
389 if (ecb_expect_false (nv == (U32)nv))
390 encode_uint (enc, 0x00, (U32)nv);
391 //TODO: maybe I32?
392 else if (ecb_expect_false (nv == (float)nv))
393 {
394 uint32_t fp = ecb_float_to_binary32 (nv);
395
396 *enc->cur++ = 0xe0 | 26;
397
398 if (!ecb_big_endian ())
399 fp = ecb_bswap32 (fp);
400
401 memcpy (enc->cur, &fp, 4);
402 enc->cur += 4;
403 }
404 else
405 {
406 uint64_t fp = ecb_double_to_binary64 (nv);
407
408 *enc->cur++ = 0xe0 | 27;
409
410 if (!ecb_big_endian ())
411 fp = ecb_bswap64 (fp);
412
413 memcpy (enc->cur, &fp, 8);
414 enc->cur += 8;
415 }
416 }
417
418 static void
419 encode_sv (enc_t *enc, SV *sv)
420 {
421 SvGETMAGIC (sv);
422
423 if (SvPOKp (sv))
424 {
425 STRLEN len;
426 char *str = SvPV (sv, len);
427 encode_str (enc, SvUTF8 (sv), str, len);
428 }
429 else if (SvNOKp (sv))
430 encode_nv (enc, sv);
431 else if (SvIOKp (sv))
432 {
433 if (SvIsUV (sv))
434 encode_uint (enc, 0x00, SvUVX (sv));
435 else if (SvIVX (sv) >= 0)
436 encode_uint (enc, 0x00, SvIVX (sv));
437 else
438 encode_uint (enc, 0x20, -(SvIVX (sv) + 1));
439 }
440 else if (SvROK (sv))
441 encode_rv (enc, SvRV (sv));
442 else if (!SvOK (sv))
443 encode_ch (enc, 0xe0 | 22);
444 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
445 encode_ch (enc, 0xe0 | 23);
446 else
447 croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data",
448 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
449 }
450
451 static SV *
452 encode_cbor (SV *scalar, CBOR *cbor)
453 {
454 enc_t enc;
455
456 enc.cbor = *cbor;
457 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
458 enc.cur = SvPVX (enc.sv);
459 enc.end = SvEND (enc.sv);
460 enc.depth = 0;
461
462 SvPOK_only (enc.sv);
463 encode_sv (&enc, scalar);
464
465 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
466 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
467
468 if (enc.cbor.flags & F_SHRINK)
469 shrink (enc.sv);
470
471 return enc.sv;
472 }
473
474 /////////////////////////////////////////////////////////////////////////////
475 // decoder
476
477 // structure used for decoding CBOR
478 typedef struct
479 {
480 U8 *cur; // current parser pointer
481 U8 *end; // end of input string
482 const char *err; // parse error, if != 0
483 CBOR cbor;
484 U32 depth; // recursion depth
485 U32 maxdepth; // recursion depth limit
486 } dec_t;
487
488 #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
489
490 #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data")
491
492 #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED)
493 #define DEC_DEC_DEPTH --dec->depth
494
495 static UV
496 decode_uint (dec_t *dec)
497 {
498 switch (*dec->cur & 31)
499 {
500 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
501 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15:
502 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
503 return *dec->cur++ & 31;
504
505 case 24:
506 WANT (2);
507 dec->cur += 2;
508 return dec->cur[-1];
509
510 case 25:
511 WANT (3);
512 dec->cur += 3;
513 return (((UV)dec->cur[-2]) << 8)
514 | ((UV)dec->cur[-1]);
515
516 case 26:
517 WANT (5);
518 dec->cur += 5;
519 return (((UV)dec->cur[-4]) << 24)
520 | (((UV)dec->cur[-3]) << 16)
521 | (((UV)dec->cur[-2]) << 8)
522 | ((UV)dec->cur[-1]);
523
524 case 27:
525 WANT (9);
526 dec->cur += 9;
527 return (((UV)dec->cur[-8]) << 56)
528 | (((UV)dec->cur[-7]) << 48)
529 | (((UV)dec->cur[-6]) << 40)
530 | (((UV)dec->cur[-5]) << 32)
531 | (((UV)dec->cur[-4]) << 24)
532 | (((UV)dec->cur[-3]) << 16)
533 | (((UV)dec->cur[-2]) << 8)
534 | ((UV)dec->cur[-1]);
535
536 default:
537 ERR ("corrupted CBOR data (unsupported integer minor encoding)");
538 }
539
540 fail:
541 return 0;
542 }
543
544 static SV *decode_sv (dec_t *dec);
545
546 static SV *
547 decode_av (dec_t *dec)
548 {
549 AV *av = newAV ();
550
551 DEC_INC_DEPTH;
552
553 if ((*dec->cur & 31) == 31)
554 {
555 ++dec->cur;
556
557 for (;;)
558 {
559 WANT (1);
560
561 if (*dec->cur == (0xe0 | 31))
562 {
563 ++dec->cur;
564 break;
565 }
566
567 av_push (av, decode_sv (dec));
568 }
569 }
570 else
571 {
572 int i, len = decode_uint (dec);
573
574 av_fill (av, len - 1);
575
576 for (i = 0; i < len; ++i)
577 AvARRAY (av)[i] = decode_sv (dec);
578 }
579
580 DEC_DEC_DEPTH;
581 return newRV_noinc ((SV *)av);
582
583 fail:
584 SvREFCNT_dec (av);
585 DEC_DEC_DEPTH;
586 return &PL_sv_undef;
587 }
588
589 static void
590 decode_he (dec_t *dec, HV *hv)
591 {
592 // for speed reasons, we specialcase single-string
593 // byte or utf-8 strings as keys.
594
595 if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27)
596 {
597 I32 len = decode_uint (dec);
598 char *key = (char *)dec->cur;
599
600 dec->cur += len;
601
602 hv_store (hv, key, len, decode_sv (dec), 0);
603 }
604 else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27)
605 {
606 I32 len = decode_uint (dec);
607 char *key = (char *)dec->cur;
608
609 dec->cur += len;
610
611 hv_store (hv, key, -len, decode_sv (dec), 0);
612 }
613 else
614 {
615 SV *k = decode_sv (dec);
616 SV *v = decode_sv (dec);
617
618 hv_store_ent (hv, k, v, 0);
619 SvREFCNT_dec (k);
620 }
621 }
622
623 static SV *
624 decode_hv (dec_t *dec)
625 {
626 HV *hv = newHV ();
627
628 DEC_INC_DEPTH;
629
630 if ((*dec->cur & 31) == 31)
631 {
632 ++dec->cur;
633
634 for (;;)
635 {
636 WANT (1);
637
638 if (*dec->cur == (0xe0 | 31))
639 {
640 ++dec->cur;
641 break;
642 }
643
644 decode_he (dec, hv);
645 }
646 }
647 else
648 {
649 int pairs = decode_uint (dec);
650
651 while (pairs--)
652 decode_he (dec, hv);
653 }
654
655 DEC_DEC_DEPTH;
656 return newRV_noinc ((SV *)hv);
657
658 fail:
659 SvREFCNT_dec (hv);
660 DEC_DEC_DEPTH;
661 return &PL_sv_undef;
662 }
663
664 static SV *
665 decode_str (dec_t *dec, int utf8)
666 {
667 SV *sv = 0;
668
669 if ((*dec->cur & 31) == 31)
670 {
671 ++dec->cur;
672
673 sv = newSVpvn ("", 0);
674
675 // not very fast, and certainly not robust against illegal input
676 for (;;)
677 {
678 WANT (1);
679
680 if (*dec->cur == (0xe0 | 31))
681 {
682 ++dec->cur;
683 break;
684 }
685
686 sv_catsv (sv, decode_sv (dec));
687 }
688 }
689 else
690 {
691 STRLEN len = decode_uint (dec);
692
693 WANT (len);
694 sv = newSVpvn (dec->cur, len);
695 dec->cur += len;
696 }
697
698 if (utf8)
699 SvUTF8_on (sv);
700
701 return sv;
702
703 fail:
704 SvREFCNT_dec (sv);
705 return &PL_sv_undef;
706 }
707
708 static SV *
709 decode_tagged (dec_t *dec)
710 {
711 UV tag = decode_uint (dec);
712 SV *sv = decode_sv (dec);
713
714 if (tag == CBOR_TAG_MAGIC)
715 return sv;
716 else if (tag == CBOR_TAG_PERL_OBJECT)
717 {
718 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
719 ERR ("corrupted CBOR data (non-array perl object)");
720
721 AV *av = (AV *)SvRV (sv);
722 int len = av_len (av) + 1;
723 HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0);
724
725 if (!stash)
726 ERR ("cannot decode perl-object (package does not exist)");
727
728 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
729
730 if (!method)
731 ERR ("cannot decode perl-object (package does not have a THAW method)");
732
733 dSP;
734
735 ENTER; SAVETMPS; PUSHMARK (SP);
736 EXTEND (SP, len + 1);
737 // we re-bless the reference to get overload and other niceties right
738 PUSHs (*av_fetch (av, 0, 1));
739 PUSHs (sv_cbor);
740
741 int i;
742
743 for (i = 1; i < len; ++i)
744 PUSHs (*av_fetch (av, i, 1));
745
746 PUTBACK;
747 call_sv ((SV *)GvCV (method), G_SCALAR);
748 SPAGAIN;
749
750 SvREFCNT_dec (sv);
751 sv = SvREFCNT_inc (POPs);
752
753 PUTBACK;
754
755 FREETMPS; LEAVE;
756
757 return sv;
758 }
759 else
760 {
761 AV *av = newAV ();
762 av_push (av, newSVuv (tag));
763 av_push (av, sv);
764
765 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
766 ? cbor_tagged_stash
767 : gv_stashpv ("CBOR::XS::Tagged" , 1);
768
769 return sv_bless (newRV_noinc ((SV *)av), tagged_stash);
770 }
771
772 fail:
773 SvREFCNT_dec (sv);
774 return &PL_sv_undef;
775 }
776
777 static SV *
778 decode_sv (dec_t *dec)
779 {
780 WANT (1);
781
782 switch (*dec->cur >> 5)
783 {
784 case 0: // unsigned int
785 return newSVuv (decode_uint (dec));
786 case 1: // negative int
787 return newSViv (-1 - (IV)decode_uint (dec));
788 case 2: // octet string
789 return decode_str (dec, 0);
790 case 3: // utf-8 string
791 return decode_str (dec, 1);
792 case 4: // array
793 return decode_av (dec);
794 case 5: // map
795 return decode_hv (dec);
796 case 6: // tag
797 return decode_tagged (dec);
798 case 7: // misc
799 switch (*dec->cur++ & 31)
800 {
801 case 20:
802 #if CBOR_SLOW
803 types_false = get_bool ("Types::Serialiser::false");
804 #endif
805 return newSVsv (types_false);
806 case 21:
807 #if CBOR_SLOW
808 types_true = get_bool ("Types::Serialiser::true");
809 #endif
810 return newSVsv (types_true);
811 case 22:
812 return newSVsv (&PL_sv_undef);
813 case 23:
814 #if CBOR_SLOW
815 types_error = get_bool ("Types::Serialiser::error");
816 #endif
817 return newSVsv (types_error);
818
819 case 25:
820 {
821 WANT (2);
822
823 uint16_t fp = (dec->cur[0] << 8) | dec->cur[1];
824 dec->cur += 2;
825
826 return newSVnv (ecb_binary16_to_float (fp));
827 }
828
829 case 26:
830 {
831 uint32_t fp;
832 WANT (4);
833 memcpy (&fp, dec->cur, 4);
834 dec->cur += 4;
835
836 if (!ecb_big_endian ())
837 fp = ecb_bswap32 (fp);
838
839 return newSVnv (ecb_binary32_to_float (fp));
840 }
841
842 case 27:
843 {
844 uint64_t fp;
845 WANT (8);
846 memcpy (&fp, dec->cur, 8);
847 dec->cur += 8;
848
849 if (!ecb_big_endian ())
850 fp = ecb_bswap64 (fp);
851
852 return newSVnv (ecb_binary64_to_double (fp));
853 }
854
855 // 0..19 unassigned
856 // 24 reserved + unassigned (reserved values are not encodable)
857 default:
858 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)");
859 }
860
861 break;
862 }
863
864 fail:
865 return &PL_sv_undef;
866 }
867
868 static SV *
869 decode_cbor (SV *string, CBOR *cbor, char **offset_return)
870 {
871 dec_t dec;
872 SV *sv;
873 STRLEN len;
874 char *data = SvPVbyte (string, len);
875
876 if (len > cbor->max_size && cbor->max_size)
877 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
878 (unsigned long)len, (unsigned long)cbor->max_size);
879
880 dec.cbor = *cbor;
881 dec.cur = (U8 *)data;
882 dec.end = (U8 *)data + len;
883 dec.err = 0;
884 dec.depth = 0;
885
886 sv = decode_sv (&dec);
887
888 if (offset_return)
889 *offset_return = dec.cur;
890
891 if (!(offset_return || !sv))
892 if (dec.cur != dec.end && !dec.err)
893 dec.err = "garbage after CBOR object";
894
895 if (dec.err)
896 {
897 SvREFCNT_dec (sv);
898 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur);
899 }
900
901 sv = sv_2mortal (sv);
902
903 return sv;
904 }
905
906 /////////////////////////////////////////////////////////////////////////////
907 // XS interface functions
908
909 MODULE = CBOR::XS PACKAGE = CBOR::XS
910
911 BOOT:
912 {
913 cbor_stash = gv_stashpv ("CBOR::XS" , 1);
914 cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1);
915
916 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
917 types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1);
918
919 types_true = get_bool ("Types::Serialiser::true" );
920 types_false = get_bool ("Types::Serialiser::false");
921 types_error = get_bool ("Types::Serialiser::error");
922
923 sv_cbor = newSVpv ("CBOR", 0);
924 SvREADONLY_on (sv_cbor);
925 }
926
927 PROTOTYPES: DISABLE
928
929 void CLONE (...)
930 CODE:
931 cbor_stash = 0;
932 cbor_tagged_stash = 0;
933 types_error_stash = 0;
934 types_boolean_stash = 0;
935
936 void new (char *klass)
937 PPCODE:
938 {
939 SV *pv = NEWSV (0, sizeof (CBOR));
940 SvPOK_only (pv);
941 cbor_init ((CBOR *)SvPVX (pv));
942 XPUSHs (sv_2mortal (sv_bless (
943 newRV_noinc (pv),
944 strEQ (klass, "CBOR::XS") ? CBOR_STASH : gv_stashpv (klass, 1)
945 )));
946 }
947
948 void shrink (CBOR *self, int enable = 1)
949 ALIAS:
950 shrink = F_SHRINK
951 allow_unknown = F_ALLOW_UNKNOWN
952 PPCODE:
953 {
954 if (enable)
955 self->flags |= ix;
956 else
957 self->flags &= ~ix;
958
959 XPUSHs (ST (0));
960 }
961
962 void get_shrink (CBOR *self)
963 ALIAS:
964 get_shrink = F_SHRINK
965 get_allow_unknown = F_ALLOW_UNKNOWN
966 PPCODE:
967 XPUSHs (boolSV (self->flags & ix));
968
969 void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
970 PPCODE:
971 self->max_depth = max_depth;
972 XPUSHs (ST (0));
973
974 U32 get_max_depth (CBOR *self)
975 CODE:
976 RETVAL = self->max_depth;
977 OUTPUT:
978 RETVAL
979
980 void max_size (CBOR *self, U32 max_size = 0)
981 PPCODE:
982 self->max_size = max_size;
983 XPUSHs (ST (0));
984
985 int get_max_size (CBOR *self)
986 CODE:
987 RETVAL = self->max_size;
988 OUTPUT:
989 RETVAL
990
991 void encode (CBOR *self, SV *scalar)
992 PPCODE:
993 PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN;
994 XPUSHs (scalar);
995
996 void decode (CBOR *self, SV *cborstr)
997 PPCODE:
998 PUTBACK; cborstr = decode_cbor (cborstr, self, 0); SPAGAIN;
999 XPUSHs (cborstr);
1000
1001 void decode_prefix (CBOR *self, SV *cborstr)
1002 PPCODE:
1003 {
1004 SV *sv;
1005 char *offset;
1006 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1007 EXTEND (SP, 2);
1008 PUSHs (sv);
1009 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1010 }
1011
1012 PROTOTYPES: ENABLE
1013
1014 void encode_cbor (SV *scalar)
1015 PPCODE:
1016 {
1017 CBOR cbor;
1018 cbor_init (&cbor);
1019 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1020 XPUSHs (scalar);
1021 }
1022
1023 void decode_cbor (SV *cborstr)
1024 PPCODE:
1025 {
1026 CBOR cbor;
1027 cbor_init (&cbor);
1028 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1029 XPUSHs (cborstr);
1030 }
1031