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