ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
Revision: 1.14
Committed: Tue Oct 29 15:56:32 2013 UTC (10 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-0_06
Changes since 1.13: +11 -0 lines
Log Message:
0.06

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