ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.2 by root, Sat Oct 26 10:41:12 2013 UTC vs.
Revision 1.11 by root, Sun Oct 27 22:35:15 2013 UTC

9#include <limits.h> 9#include <limits.h>
10#include <float.h> 10#include <float.h>
11 11
12#include "ecb.h" 12#include "ecb.h"
13 13
14#if defined(__BORLANDC__) || defined(_MSC_VER) 14// known tags
15# define snprintf _snprintf // C compilers have this in stdio.h 15enum cbor_tag
16#endif 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};
17 42
18#define F_SHRINK 0x00000200UL 43#define F_SHRINK 0x00000200UL
19#define F_ALLOW_UNKNOWN 0x00002000UL 44#define F_ALLOW_UNKNOWN 0x00002000UL
20 45
21#define INIT_SIZE 32 // initial scalar size to be allocated 46#define INIT_SIZE 32 // initial scalar size to be allocated
22 47
23#define SB do { 48#define SB do {
24#define SE } while (0) 49#define SE } while (0)
25
26#if __GNUC__ >= 3
27# define expect(expr,value) __builtin_expect ((expr), (value))
28# define INLINE static inline
29#else
30# define expect(expr,value) (expr)
31# define INLINE static
32#endif
33
34#define expect_false(expr) expect ((expr) != 0, 0)
35#define expect_true(expr) expect ((expr) != 0, 1)
36 50
37#define IN_RANGE_INC(type,val,beg,end) \ 51#define IN_RANGE_INC(type,val,beg,end) \
38 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ 52 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
39 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) 53 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
40 54
46#else 60#else
47# define CBOR_SLOW 0 61# define CBOR_SLOW 0
48# define CBOR_STASH cbor_stash 62# define CBOR_STASH cbor_stash
49#endif 63#endif
50 64
51static HV *cbor_stash, *cbor_boolean_stash; // CBOR::XS:: 65static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS::
52static SV *cbor_true, *cbor_false; 66static SV *types_true, *types_false, *types_error, *sv_cbor;
53 67
54typedef struct { 68typedef struct {
55 U32 flags; 69 U32 flags;
56 U32 max_depth; 70 U32 max_depth;
57 STRLEN max_size; 71 STRLEN max_size;
58
59 SV *cb_object;
60 HV *cb_sk_object;
61} CBOR; 72} CBOR;
62 73
63INLINE void 74ecb_inline void
64cbor_init (CBOR *cbor) 75cbor_init (CBOR *cbor)
65{ 76{
66 Zero (cbor, 1, CBOR); 77 Zero (cbor, 1, CBOR);
67 cbor->max_depth = 512; 78 cbor->max_depth = 512;
68} 79}
69 80
70///////////////////////////////////////////////////////////////////////////// 81/////////////////////////////////////////////////////////////////////////////
71// utility functions 82// utility functions
72 83
73INLINE SV * 84ecb_inline SV *
74get_bool (const char *name) 85get_bool (const char *name)
75{ 86{
76 SV *sv = get_sv (name, 1); 87 SV *sv = get_sv (name, 1);
77 88
78 SvREADONLY_on (sv); 89 SvREADONLY_on (sv);
79 SvREADONLY_on (SvRV (sv)); 90 SvREADONLY_on (SvRV (sv));
80 91
81 return sv; 92 return sv;
82} 93}
83 94
84INLINE void 95ecb_inline void
85shrink (SV *sv) 96shrink (SV *sv)
86{ 97{
87 sv_utf8_downgrade (sv, 1); 98 sv_utf8_downgrade (sv, 1);
88 99
89 if (SvLEN (sv) > SvCUR (sv) + 1) 100 if (SvLEN (sv) > SvCUR (sv) + 1)
112 SV *sv; // result scalar 123 SV *sv; // result scalar
113 CBOR cbor; 124 CBOR cbor;
114 U32 depth; // recursion level 125 U32 depth; // recursion level
115} enc_t; 126} enc_t;
116 127
117INLINE void 128ecb_inline void
118need (enc_t *enc, STRLEN len) 129need (enc_t *enc, STRLEN len)
119{ 130{
120 if (expect_false (enc->cur + len >= enc->end)) 131 if (ecb_expect_false (enc->cur + len >= enc->end))
121 { 132 {
122 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 133 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
123 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 134 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
124 enc->cur = SvPVX (enc->sv) + cur; 135 enc->cur = SvPVX (enc->sv) + cur;
125 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 136 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
126 } 137 }
127} 138}
128 139
129INLINE void 140ecb_inline void
130encode_ch (enc_t *enc, char ch) 141encode_ch (enc_t *enc, char ch)
131{ 142{
132 need (enc, 1); 143 need (enc, 1);
133 *enc->cur++ = ch; 144 *enc->cur++ = ch;
134} 145}
138{ 149{
139 need (enc, 9); 150 need (enc, 9);
140 151
141 if (len < 24) 152 if (len < 24)
142 *enc->cur++ = major | len; 153 *enc->cur++ = major | len;
143 else if (len < 0x100) 154 else if (len <= 0xff)
144 { 155 {
145 *enc->cur++ = major | 24; 156 *enc->cur++ = major | 24;
146 *enc->cur++ = len; 157 *enc->cur++ = len;
147 } 158 }
148 else if (len < 0x10000) 159 else if (len <= 0xffff)
149 { 160 {
150 *enc->cur++ = major | 25; 161 *enc->cur++ = major | 25;
151 *enc->cur++ = len >> 8; 162 *enc->cur++ = len >> 8;
152 *enc->cur++ = len; 163 *enc->cur++ = len;
153 } 164 }
154 else if (len < 0x100000000) 165 else if (len <= 0xffffffff)
155 { 166 {
156 *enc->cur++ = major | 26; 167 *enc->cur++ = major | 26;
157 *enc->cur++ = len >> 24; 168 *enc->cur++ = len >> 24;
158 *enc->cur++ = len >> 16; 169 *enc->cur++ = len >> 16;
159 *enc->cur++ = len >> 8; 170 *enc->cur++ = len >> 8;
160 *enc->cur++ = len; 171 *enc->cur++ = len;
161 } 172 }
162 else if (len) 173 else
163 { 174 {
164 *enc->cur++ = major | 27; 175 *enc->cur++ = major | 27;
165 *enc->cur++ = len >> 56; 176 *enc->cur++ = len >> 56;
166 *enc->cur++ = len >> 48; 177 *enc->cur++ = len >> 48;
167 *enc->cur++ = len >> 40; 178 *enc->cur++ = len >> 40;
228 if (HeKLEN (he) == HEf_SVKEY) 239 if (HeKLEN (he) == HEf_SVKEY)
229 encode_sv (enc, HeSVKEY (he)); 240 encode_sv (enc, HeSVKEY (he));
230 else 241 else
231 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); 242 encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he));
232 243
233 encode_sv (enc, expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); 244 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
234 } 245 }
235 246
236 if (mg) 247 if (mg)
237 encode_ch (enc, 0xe0 | 31); 248 encode_ch (enc, 0xe0 | 31);
238 249
246 svtype svt; 257 svtype svt;
247 258
248 SvGETMAGIC (sv); 259 SvGETMAGIC (sv);
249 svt = SvTYPE (sv); 260 svt = SvTYPE (sv);
250 261
251 if (expect_false (SvOBJECT (sv))) 262 if (ecb_expect_false (SvOBJECT (sv)))
252 { 263 {
253 HV *stash = !CBOR_SLOW || cbor_boolean_stash 264 HV *boolean_stash = !CBOR_SLOW || types_boolean_stash
254 ? cbor_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
255 : gv_stashpv ("CBOR::XS::Boolean", 1); 272 : gv_stashpv ("CBOR::XS::Tagged" , 1);
256 273
257 if (SvSTASH (sv) == stash) 274 HV *stash = SvSTASH (sv);
275 GV *method;
276
277 if (stash == boolean_stash)
258 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); 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 }
259 else 341 else
260 {
261#if 0 //TODO
262 if (enc->cbor.flags & F_CONV_BLESSED)
263 {
264 // we re-bless the reference to get overload and other niceties right
265 GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0);
266
267 if (to_cbor)
268 {
269 dSP;
270
271 ENTER; SAVETMPS; PUSHMARK (SP);
272 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
273
274 // calling with G_SCALAR ensures that we always get a 1 return value
275 PUTBACK;
276 call_sv ((SV *)GvCV (to_cbor), G_SCALAR);
277 SPAGAIN;
278
279 // catch this surprisingly common error
280 if (SvROK (TOPs) && SvRV (TOPs) == sv)
281 croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
282
283 sv = POPs;
284 PUTBACK;
285
286 encode_sv (enc, sv);
287
288 FREETMPS; LEAVE;
289 }
290 else if (enc->cbor.flags & F_ALLOW_BLESSED)
291 encode_str (enc, "null", 4, 0);
292 else
293 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_CBOR method available on it", 342 croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it",
294 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
295 }
296 else if (enc->cbor.flags & F_ALLOW_BLESSED)
297 encode_str (enc, "null", 4, 0);
298 else
299 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
300 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 343 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
301#endif
302 }
303 } 344 }
304 else if (svt == SVt_PVHV) 345 else if (svt == SVt_PVHV)
305 encode_hv (enc, (HV *)sv); 346 encode_hv (enc, (HV *)sv);
306 else if (svt == SVt_PVAV) 347 else if (svt == SVt_PVAV)
307 encode_av (enc, (AV *)sv); 348 encode_av (enc, (AV *)sv);
332{ 373{
333 double nv = SvNVX (sv); 374 double nv = SvNVX (sv);
334 375
335 need (enc, 9); 376 need (enc, 9);
336 377
337 if (expect_false (nv == (U32)nv)) 378 if (ecb_expect_false (nv == (U32)nv))
338 encode_uint (enc, 0x00, (U32)nv); 379 encode_uint (enc, 0x00, (U32)nv);
339 //TODO: maybe I32? 380 //TODO: maybe I32?
340 else if (expect_false (nv == (float)nv)) 381 else if (ecb_expect_false (nv == (float)nv))
341 { 382 {
342 uint32_t fp = ecb_float_to_binary32 (nv); 383 uint32_t fp = ecb_float_to_binary32 (nv);
343 384
344 *enc->cur++ = 0xe0 | 26; 385 *enc->cur++ = 0xe0 | 26;
345 386
433 U32 maxdepth; // recursion depth limit 474 U32 maxdepth; // recursion depth limit
434} dec_t; 475} dec_t;
435 476
436#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE 477#define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
437 478
438#define WANT(len) if (expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data"); 479#define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data")
439 480
440#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) 481#define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED)
441#define DEC_DEC_DEPTH --dec->depth 482#define DEC_DEC_DEPTH --dec->depth
442 483
443static UV 484static UV
575 } 616 }
576 617
577 DEC_DEC_DEPTH; 618 DEC_DEC_DEPTH;
578 return newRV_noinc ((SV *)hv); 619 return newRV_noinc ((SV *)hv);
579 620
580#if 0
581 SV *sv;
582 HV *hv = newHV ();
583
584 DEC_INC_DEPTH;
585 decode_ws (dec);
586
587 for (;;)
588 {
589 // heuristic: assume that
590 // a) decode_str + hv_store_ent are abysmally slow.
591 // b) most hash keys are short, simple ascii text.
592 // => try to "fast-match" such strings to avoid
593 // the overhead of decode_str + hv_store_ent.
594 {
595 SV *value;
596 char *p = dec->cur;
597 char *e = p + 24; // only try up to 24 bytes
598
599 for (;;)
600 {
601 // the >= 0x80 is false on most architectures
602 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\')
603 {
604 // slow path, back up and use decode_str
605 SV *key = decode_str (dec);
606 if (!key)
607 goto fail;
608
609 decode_ws (dec); EXPECT_CH (':');
610
611 decode_ws (dec);
612 value = decode_sv (dec);
613 if (!value)
614 {
615 SvREFCNT_dec (key);
616 goto fail;
617 }
618
619 hv_store_ent (hv, key, value, 0);
620 SvREFCNT_dec (key);
621
622 break;
623 }
624 else if (*p == '"')
625 {
626 // fast path, got a simple key
627 char *key = dec->cur;
628 int len = p - key;
629 dec->cur = p + 1;
630
631 decode_ws (dec); EXPECT_CH (':');
632
633 decode_ws (dec);
634 value = decode_sv (dec);
635 if (!value)
636 goto fail;
637
638 hv_store (hv, key, len, value, 0);
639
640 break;
641 }
642
643 ++p;
644 }
645 }
646
647 decode_ws (dec);
648
649 if (*dec->cur == '}')
650 {
651 ++dec->cur;
652 break;
653 }
654
655 if (*dec->cur != ',')
656 ERR (", or } expected while parsing object/hash");
657
658 ++dec->cur;
659
660 decode_ws (dec);
661
662 if (*dec->cur == '}' && dec->cbor.flags & F_RELAXED)
663 {
664 ++dec->cur;
665 break;
666 }
667 }
668
669 DEC_DEC_DEPTH;
670 sv = newRV_noinc ((SV *)hv);
671
672 // check filter callbacks
673 if (dec->cbor.flags & F_HOOK)
674 {
675 if (dec->cbor.cb_sk_object && HvKEYS (hv) == 1)
676 {
677 HE *cb, *he;
678
679 hv_iterinit (hv);
680 he = hv_iternext (hv);
681 hv_iterinit (hv);
682
683 // the next line creates a mortal sv each time its called.
684 // might want to optimise this for common cases.
685 cb = hv_fetch_ent (dec->cbor.cb_sk_object, hv_iterkeysv (he), 0, 0);
686
687 if (cb)
688 {
689 dSP;
690 int count;
691
692 ENTER; SAVETMPS; PUSHMARK (SP);
693 XPUSHs (HeVAL (he));
694 sv_2mortal (sv);
695
696 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
697
698 if (count == 1)
699 {
700 sv = newSVsv (POPs);
701 FREETMPS; LEAVE;
702 return sv;
703 }
704
705 SvREFCNT_inc (sv);
706 FREETMPS; LEAVE;
707 }
708 }
709
710 if (dec->cbor.cb_object)
711 {
712 dSP;
713 int count;
714
715 ENTER; SAVETMPS; PUSHMARK (SP);
716 XPUSHs (sv_2mortal (sv));
717
718 PUTBACK; count = call_sv (dec->cbor.cb_object, G_ARRAY); SPAGAIN;
719
720 if (count == 1)
721 {
722 sv = newSVsv (POPs);
723 FREETMPS; LEAVE;
724 return sv;
725 }
726
727 SvREFCNT_inc (sv);
728 FREETMPS; LEAVE;
729 }
730 }
731
732 return sv;
733#endif
734
735fail: 621fail:
736 SvREFCNT_dec (hv); 622 SvREFCNT_dec (hv);
737 DEC_DEC_DEPTH; 623 DEC_DEC_DEPTH;
738 return &PL_sv_undef; 624 return &PL_sv_undef;
739} 625}
740 626
741static SV * 627static SV *
742decode_str (dec_t *dec, int utf8) 628decode_str (dec_t *dec, int utf8)
743{ 629{
744 SV *sv; 630 SV *sv = 0;
745 631
746 if ((*dec->cur & 31) == 31) 632 if ((*dec->cur & 31) == 31)
747 { 633 {
748 ++dec->cur; 634 ++dec->cur;
749 635
758 { 644 {
759 ++dec->cur; 645 ++dec->cur;
760 break; 646 break;
761 } 647 }
762 648
763 SV *sv2 = decode_sv (dec);
764 sv_catsv (sv, sv2); 649 sv_catsv (sv, decode_sv (dec));
765 } 650 }
766 } 651 }
767 else 652 else
768 { 653 {
769 STRLEN len = decode_uint (dec); 654 STRLEN len = decode_uint (dec);
777 SvUTF8_on (sv); 662 SvUTF8_on (sv);
778 663
779 return sv; 664 return sv;
780 665
781fail: 666fail:
667 SvREFCNT_dec (sv);
668 return &PL_sv_undef;
669}
670
671static SV *
672decode_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
734fail:
735 SvREFCNT_dec (sv);
782 return &PL_sv_undef; 736 return &PL_sv_undef;
783} 737}
784 738
785static SV * 739static SV *
786decode_sv (dec_t *dec) 740decode_sv (dec_t *dec)
788 WANT (1); 742 WANT (1);
789 743
790 switch (*dec->cur >> 5) 744 switch (*dec->cur >> 5)
791 { 745 {
792 case 0: // unsigned int 746 case 0: // unsigned int
793 //TODO: 64 bit values on 3 2bit perls
794 return newSVuv (decode_uint (dec)); 747 return newSVuv (decode_uint (dec));
795 case 1: // negative int 748 case 1: // negative int
796 return newSViv (-1 - (IV)decode_uint (dec)); 749 return newSViv (-1 - (IV)decode_uint (dec));
797 case 2: // octet string 750 case 2: // octet string
798 return decode_str (dec, 0); 751 return decode_str (dec, 0);
801 case 4: // array 754 case 4: // array
802 return decode_av (dec); 755 return decode_av (dec);
803 case 5: // map 756 case 5: // map
804 return decode_hv (dec); 757 return decode_hv (dec);
805 case 6: // tag 758 case 6: // tag
806 abort (); 759 return decode_tagged (dec);
807 break;
808 case 7: // misc 760 case 7: // misc
809 switch (*dec->cur++ & 31) 761 switch (*dec->cur++ & 31)
810 { 762 {
811 case 20: 763 case 20:
812#if CBOR_SLOW 764#if CBOR_SLOW
813 cbor_false = get_bool ("CBOR::XS::false"); 765 types_false = get_bool ("Types::Serialiser::false");
814#endif 766#endif
815 return newSVsv (cbor_false); 767 return newSVsv (types_false);
816 case 21: 768 case 21:
817#if CBOR_SLOW 769#if CBOR_SLOW
818 cbor_true = get_bool ("CBOR::XS::true"); 770 types_true = get_bool ("Types::Serialiser::true");
819#endif 771#endif
820 return newSVsv (cbor_true); 772 return newSVsv (types_true);
821 case 22: 773 case 22:
822 return newSVsv (&PL_sv_undef); 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);
823 780
824 case 25: 781 case 25:
825 { 782 {
826 WANT (2); 783 WANT (2);
827 784
863 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); 820 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)");
864 } 821 }
865 822
866 break; 823 break;
867 } 824 }
868#if 0
869 switch (*dec->cur)
870 {
871 //case '"': ++dec->cur; return decode_str (dec);
872 case '[': ++dec->cur; return decode_av (dec);
873 case '{': ++dec->cur; return decode_hv (dec);
874
875 case '-':
876 case '0': case '1': case '2': case '3': case '4':
877 case '5': case '6': case '7': case '8': case '9':
878 //TODO return decode_num (dec);
879
880 case 't':
881 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
882 {
883 dec->cur += 4;
884#if CBOR_SLOW
885 cbor_true = get_bool ("CBOR::XS::true");
886#endif
887 return newSVsv (cbor_true);
888 }
889 else
890 ERR ("'true' expected");
891
892 break;
893
894 case 'f':
895 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
896 {
897 dec->cur += 5;
898#if CBOR_SLOW
899 cbor_false = get_bool ("CBOR::XS::false");
900#endif
901 return newSVsv (cbor_false);
902 }
903 else
904 ERR ("'false' expected");
905
906 break;
907
908 case 'n':
909 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4))
910 {
911 dec->cur += 4;
912 return newSVsv (&PL_sv_undef);
913 }
914 else
915 ERR ("'null' expected");
916
917 break;
918
919 default:
920 ERR ("malformed CBOR string, neither array, object, number, string or atom");
921 break;
922 }
923#endif
924 825
925fail: 826fail:
926 return &PL_sv_undef; 827 return &PL_sv_undef;
927} 828}
928 829
971 dec.cur = (U8 *)SvPVX (string); 872 dec.cur = (U8 *)SvPVX (string);
972 dec.end = (U8 *)SvEND (string); 873 dec.end = (U8 *)SvEND (string);
973 dec.err = 0; 874 dec.err = 0;
974 dec.depth = 0; 875 dec.depth = 0;
975 876
976 if (dec.cbor.cb_object || dec.cbor.cb_sk_object)
977 ;//TODO dec.cbor.flags |= F_HOOK;
978
979 sv = decode_sv (&dec); 877 sv = decode_sv (&dec);
980 878
981 if (offset_return) 879 if (offset_return)
982 *offset_return = dec.cur; 880 *offset_return = dec.cur;
983 881
1002MODULE = CBOR::XS PACKAGE = CBOR::XS 900MODULE = CBOR::XS PACKAGE = CBOR::XS
1003 901
1004BOOT: 902BOOT:
1005{ 903{
1006 cbor_stash = gv_stashpv ("CBOR::XS" , 1); 904 cbor_stash = gv_stashpv ("CBOR::XS" , 1);
1007 cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); 905 cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1);
1008 906
1009 cbor_true = get_bool ("CBOR::XS::true"); 907 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1010 cbor_false = get_bool ("CBOR::XS::false"); 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);
1011} 916}
1012 917
1013PROTOTYPES: DISABLE 918PROTOTYPES: DISABLE
1014 919
1015void CLONE (...) 920void CLONE (...)
1016 CODE: 921 CODE:
1017 cbor_stash = 0; 922 cbor_stash = 0;
923 cbor_tagged_stash = 0;
924 types_error_stash = 0;
1018 cbor_boolean_stash = 0; 925 types_boolean_stash = 0;
1019 926
1020void new (char *klass) 927void new (char *klass)
1021 PPCODE: 928 PPCODE:
1022{ 929{
1023 SV *pv = NEWSV (0, sizeof (CBOR)); 930 SV *pv = NEWSV (0, sizeof (CBOR));
1126 EXTEND (SP, 2); 1033 EXTEND (SP, 2);
1127 PUSHs (sv); 1034 PUSHs (sv);
1128 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); 1035 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1129} 1036}
1130 1037
1038#if 0
1039
1131void DESTROY (CBOR *self) 1040void DESTROY (CBOR *self)
1132 CODE: 1041 CODE:
1133 SvREFCNT_dec (self->cb_sk_object); 1042 SvREFCNT_dec (self->cb_sk_object);
1134 SvREFCNT_dec (self->cb_object); 1043 SvREFCNT_dec (self->cb_object);
1135 1044
1045#endif
1046
1136PROTOTYPES: ENABLE 1047PROTOTYPES: ENABLE
1137 1048
1138void encode_cbor (SV *scalar) 1049void encode_cbor (SV *scalar)
1139 PPCODE: 1050 PPCODE:
1140{ 1051{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines