ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
Revision: 1.11
Committed: Sun Oct 27 22:35:15 2013 UTC (10 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-0_04
Changes since 1.10: +102 -37 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 }
604 }
605 else
606 {
607 int len = decode_uint (dec);
608
609 while (len--)
610 {
611 SV *k = decode_sv (dec);
612 SV *v = decode_sv (dec);
613
614 hv_store_ent (hv, k, v, 0);
615 }
616 }
617
618 DEC_DEC_DEPTH;
619 return newRV_noinc ((SV *)hv);
620
621 fail:
622 SvREFCNT_dec (hv);
623 DEC_DEC_DEPTH;
624 return &PL_sv_undef;
625 }
626
627 static SV *
628 decode_str (dec_t *dec, int utf8)
629 {
630 SV *sv = 0;
631
632 if ((*dec->cur & 31) == 31)
633 {
634 ++dec->cur;
635
636 sv = newSVpvn ("", 0);
637
638 // not very fast, and certainly not robust against illegal input
639 for (;;)
640 {
641 WANT (1);
642
643 if (*dec->cur == (0xe0 | 31))
644 {
645 ++dec->cur;
646 break;
647 }
648
649 sv_catsv (sv, decode_sv (dec));
650 }
651 }
652 else
653 {
654 STRLEN len = decode_uint (dec);
655
656 WANT (len);
657 sv = newSVpvn (dec->cur, len);
658 dec->cur += len;
659 }
660
661 if (utf8)
662 SvUTF8_on (sv);
663
664 return sv;
665
666 fail:
667 SvREFCNT_dec (sv);
668 return &PL_sv_undef;
669 }
670
671 static SV *
672 decode_tagged (dec_t *dec)
673 {
674 UV tag = decode_uint (dec);
675 SV *sv = decode_sv (dec);
676
677 if (tag == CBOR_TAG_MAGIC)
678 return sv;
679 else if (tag == CBOR_TAG_PERL_OBJECT)
680 {
681 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
682 ERR ("corrupted CBOR data (non-array perl object)");
683
684 AV *av = (AV *)SvRV (sv);
685 int len = av_len (av) + 1;
686 HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0);
687
688 if (!stash)
689 ERR ("cannot decode perl-object (package does not exist)");
690
691 GV *method = gv_fetchmethod_autoload (stash, "THAW", 0);
692
693 if (!method)
694 ERR ("cannot decode perl-object (package does not have a THAW method)");
695
696 dSP;
697
698 ENTER; SAVETMPS; PUSHMARK (SP);
699 EXTEND (SP, len + 1);
700 // we re-bless the reference to get overload and other niceties right
701 PUSHs (*av_fetch (av, 0, 1));
702 PUSHs (sv_cbor);
703
704 int i;
705
706 for (i = 1; i < len; ++i)
707 PUSHs (*av_fetch (av, i, 1));
708
709 PUTBACK;
710 call_sv ((SV *)GvCV (method), G_SCALAR);
711 SPAGAIN;
712
713 sv = SvREFCNT_inc (POPs);
714
715 PUTBACK;
716
717 FREETMPS; LEAVE;
718
719 return sv;
720 }
721 else
722 {
723 AV *av = newAV ();
724 av_push (av, newSVuv (tag));
725 av_push (av, sv);
726
727 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
728 ? cbor_tagged_stash
729 : gv_stashpv ("CBOR::XS::Tagged" , 1);
730
731 return sv_bless (newRV_noinc ((SV *)av), tagged_stash);
732 }
733
734 fail:
735 SvREFCNT_dec (sv);
736 return &PL_sv_undef;
737 }
738
739 static SV *
740 decode_sv (dec_t *dec)
741 {
742 WANT (1);
743
744 switch (*dec->cur >> 5)
745 {
746 case 0: // unsigned int
747 return newSVuv (decode_uint (dec));
748 case 1: // negative int
749 return newSViv (-1 - (IV)decode_uint (dec));
750 case 2: // octet string
751 return decode_str (dec, 0);
752 case 3: // utf-8 string
753 return decode_str (dec, 1);
754 case 4: // array
755 return decode_av (dec);
756 case 5: // map
757 return decode_hv (dec);
758 case 6: // tag
759 return decode_tagged (dec);
760 case 7: // misc
761 switch (*dec->cur++ & 31)
762 {
763 case 20:
764 #if CBOR_SLOW
765 types_false = get_bool ("Types::Serialiser::false");
766 #endif
767 return newSVsv (types_false);
768 case 21:
769 #if CBOR_SLOW
770 types_true = get_bool ("Types::Serialiser::true");
771 #endif
772 return newSVsv (types_true);
773 case 22:
774 return newSVsv (&PL_sv_undef);
775 case 23:
776 #if CBOR_SLOW
777 types_error = get_bool ("Types::Serialiser::error");
778 #endif
779 return newSVsv (types_error);
780
781 case 25:
782 {
783 WANT (2);
784
785 uint16_t fp = (dec->cur[0] << 8) | dec->cur[1];
786 dec->cur += 2;
787
788 return newSVnv (ecb_binary16_to_float (fp));
789 }
790
791 case 26:
792 {
793 uint32_t fp;
794 WANT (4);
795 memcpy (&fp, dec->cur, 4);
796 dec->cur += 4;
797
798 if (!ecb_big_endian ())
799 fp = ecb_bswap32 (fp);
800
801 return newSVnv (ecb_binary32_to_float (fp));
802 }
803
804 case 27:
805 {
806 uint64_t fp;
807 WANT (8);
808 memcpy (&fp, dec->cur, 8);
809 dec->cur += 8;
810
811 if (!ecb_big_endian ())
812 fp = ecb_bswap64 (fp);
813
814 return newSVnv (ecb_binary64_to_double (fp));
815 }
816
817 // 0..19 unassigned
818 // 24 reserved + unassigned (reserved values are not encodable)
819 default:
820 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)");
821 }
822
823 break;
824 }
825
826 fail:
827 return &PL_sv_undef;
828 }
829
830 static SV *
831 decode_cbor (SV *string, CBOR *cbor, char **offset_return)
832 {
833 dec_t dec;
834 SV *sv;
835
836 /* work around bugs in 5.10 where manipulating magic values
837 * makes perl ignore the magic in subsequent accesses.
838 * also make a copy of non-PV values, to get them into a clean
839 * state (SvPV should do that, but it's buggy, see below).
840 */
841 /*SvGETMAGIC (string);*/
842 if (SvMAGICAL (string) || !SvPOK (string))
843 string = sv_2mortal (newSVsv (string));
844
845 SvUPGRADE (string, SVt_PV);
846
847 /* work around a bug in perl 5.10, which causes SvCUR to fail an
848 * assertion with -DDEBUGGING, although SvCUR is documented to
849 * return the xpv_cur field which certainly exists after upgrading.
850 * according to nicholas clark, calling SvPOK fixes this.
851 * But it doesn't fix it, so try another workaround, call SvPV_nolen
852 * and hope for the best.
853 * Damnit, SvPV_nolen still trips over yet another assertion. This
854 * assertion business is seriously broken, try yet another workaround
855 * for the broken -DDEBUGGING.
856 */
857 {
858 #ifdef DEBUGGING
859 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
860 #else
861 STRLEN offset = SvCUR (string);
862 #endif
863
864 if (offset > cbor->max_size && cbor->max_size)
865 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
866 (unsigned long)SvCUR (string), (unsigned long)cbor->max_size);
867 }
868
869 sv_utf8_downgrade (string, 0);
870
871 dec.cbor = *cbor;
872 dec.cur = (U8 *)SvPVX (string);
873 dec.end = (U8 *)SvEND (string);
874 dec.err = 0;
875 dec.depth = 0;
876
877 sv = decode_sv (&dec);
878
879 if (offset_return)
880 *offset_return = dec.cur;
881
882 if (!(offset_return || !sv))
883 if (dec.cur != dec.end && !dec.err)
884 dec.err = "garbage after CBOR object";
885
886 if (dec.err)
887 {
888 SvREFCNT_dec (sv);
889 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur);
890 }
891
892 sv = sv_2mortal (sv);
893
894 return sv;
895 }
896
897 /////////////////////////////////////////////////////////////////////////////
898 // XS interface functions
899
900 MODULE = CBOR::XS PACKAGE = CBOR::XS
901
902 BOOT:
903 {
904 cbor_stash = gv_stashpv ("CBOR::XS" , 1);
905 cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1);
906
907 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
908 types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1);
909
910 types_true = get_bool ("Types::Serialiser::true" );
911 types_false = get_bool ("Types::Serialiser::false");
912 types_error = get_bool ("Types::Serialiser::error");
913
914 sv_cbor = newSVpv ("CBOR", 0);
915 SvREADONLY_on (sv_cbor);
916 }
917
918 PROTOTYPES: DISABLE
919
920 void CLONE (...)
921 CODE:
922 cbor_stash = 0;
923 cbor_tagged_stash = 0;
924 types_error_stash = 0;
925 types_boolean_stash = 0;
926
927 void new (char *klass)
928 PPCODE:
929 {
930 SV *pv = NEWSV (0, sizeof (CBOR));
931 SvPOK_only (pv);
932 cbor_init ((CBOR *)SvPVX (pv));
933 XPUSHs (sv_2mortal (sv_bless (
934 newRV_noinc (pv),
935 strEQ (klass, "CBOR::XS") ? CBOR_STASH : gv_stashpv (klass, 1)
936 )));
937 }
938
939 void shrink (CBOR *self, int enable = 1)
940 ALIAS:
941 shrink = F_SHRINK
942 allow_unknown = F_ALLOW_UNKNOWN
943 PPCODE:
944 {
945 if (enable)
946 self->flags |= ix;
947 else
948 self->flags &= ~ix;
949
950 XPUSHs (ST (0));
951 }
952
953 void get_shrink (CBOR *self)
954 ALIAS:
955 get_shrink = F_SHRINK
956 get_allow_unknown = F_ALLOW_UNKNOWN
957 PPCODE:
958 XPUSHs (boolSV (self->flags & ix));
959
960 void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
961 PPCODE:
962 self->max_depth = max_depth;
963 XPUSHs (ST (0));
964
965 U32 get_max_depth (CBOR *self)
966 CODE:
967 RETVAL = self->max_depth;
968 OUTPUT:
969 RETVAL
970
971 void max_size (CBOR *self, U32 max_size = 0)
972 PPCODE:
973 self->max_size = max_size;
974 XPUSHs (ST (0));
975
976 int get_max_size (CBOR *self)
977 CODE:
978 RETVAL = self->max_size;
979 OUTPUT:
980 RETVAL
981
982 #if 0 //TODO
983
984 void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef)
985 PPCODE:
986 {
987 SvREFCNT_dec (self->cb_object);
988 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
989
990 XPUSHs (ST (0));
991 }
992
993 void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef)
994 PPCODE:
995 {
996 if (!self->cb_sk_object)
997 self->cb_sk_object = newHV ();
998
999 if (SvOK (cb))
1000 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1001 else
1002 {
1003 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
1004
1005 if (!HvKEYS (self->cb_sk_object))
1006 {
1007 SvREFCNT_dec (self->cb_sk_object);
1008 self->cb_sk_object = 0;
1009 }
1010 }
1011
1012 XPUSHs (ST (0));
1013 }
1014
1015 #endif
1016
1017 void encode (CBOR *self, SV *scalar)
1018 PPCODE:
1019 PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN;
1020 XPUSHs (scalar);
1021
1022 void decode (CBOR *self, SV *cborstr)
1023 PPCODE:
1024 PUTBACK; cborstr = decode_cbor (cborstr, self, 0); SPAGAIN;
1025 XPUSHs (cborstr);
1026
1027 void decode_prefix (CBOR *self, SV *cborstr)
1028 PPCODE:
1029 {
1030 SV *sv;
1031 char *offset;
1032 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1033 EXTEND (SP, 2);
1034 PUSHs (sv);
1035 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1036 }
1037
1038 #if 0
1039
1040 void DESTROY (CBOR *self)
1041 CODE:
1042 SvREFCNT_dec (self->cb_sk_object);
1043 SvREFCNT_dec (self->cb_object);
1044
1045 #endif
1046
1047 PROTOTYPES: ENABLE
1048
1049 void encode_cbor (SV *scalar)
1050 PPCODE:
1051 {
1052 CBOR cbor;
1053 cbor_init (&cbor);
1054 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1055 XPUSHs (scalar);
1056 }
1057
1058 void decode_cbor (SV *cborstr)
1059 PPCODE:
1060 {
1061 CBOR cbor;
1062 cbor_init (&cbor);
1063 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1064 XPUSHs (cborstr);
1065 }
1066