ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
Revision: 1.9
Committed: Sun Oct 27 10:17:12 2013 UTC (10 years, 6 months ago) by root
Branch: MAIN
Changes since 1.8: +37 -20 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, *cbor_boolean_stash, *cbor_tagged_stash; // CBOR::XS::
66 static SV *cbor_true, *cbor_false;
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 || cbor_boolean_stash
265 ? cbor_boolean_stash
266 : gv_stashpv ("CBOR::XS::Boolean", 1);
267 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
268 ? cbor_tagged_stash
269 : gv_stashpv ("CBOR::XS::Tagged" , 1);
270
271 if (SvSTASH (sv) == boolean_stash)
272 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20);
273 else if (SvSTASH (sv) == tagged_stash)
274 {
275 if (svt != SVt_PVAV)
276 croak ("encountered CBOR::XS::Tagged object that isn't an array");
277
278 encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1)));
279 encode_sv (enc, *av_fetch ((AV *)sv, 1, 1));
280 }
281 else
282 {
283 // we re-bless the reference to get overload and other niceties right
284 GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0);
285
286 if (to_cbor)
287 {
288 dSP;
289
290 ENTER; SAVETMPS; PUSHMARK (SP);
291 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
292
293 // calling with G_SCALAR ensures that we always get a 1 return value
294 PUTBACK;
295 call_sv ((SV *)GvCV (to_cbor), G_SCALAR);
296 SPAGAIN;
297
298 // catch this surprisingly common error
299 if (SvROK (TOPs) && SvRV (TOPs) == sv)
300 croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
301
302 sv = POPs;
303 PUTBACK;
304
305 encode_sv (enc, sv);
306
307 FREETMPS; LEAVE;
308 }
309 else
310 croak ("encountered object '%s', but no TO_CBOR method available on it",
311 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
312 }
313 }
314 else if (svt == SVt_PVHV)
315 encode_hv (enc, (HV *)sv);
316 else if (svt == SVt_PVAV)
317 encode_av (enc, (AV *)sv);
318 else if (svt < SVt_PVAV)
319 {
320 STRLEN len = 0;
321 char *pv = svt ? SvPV (sv, len) : 0;
322
323 if (len == 1 && *pv == '1')
324 encode_ch (enc, 0xe0 | 21);
325 else if (len == 1 && *pv == '0')
326 encode_ch (enc, 0xe0 | 20);
327 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
328 encode_ch (enc, 0xe0 | 23);
329 else
330 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
331 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
332 }
333 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
334 encode_ch (enc, 0xe0 | 23);
335 else
336 croak ("encountered %s, but CBOR can only represent references to arrays or hashes",
337 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
338 }
339
340 static void
341 encode_nv (enc_t *enc, SV *sv)
342 {
343 double nv = SvNVX (sv);
344
345 need (enc, 9);
346
347 if (ecb_expect_false (nv == (U32)nv))
348 encode_uint (enc, 0x00, (U32)nv);
349 //TODO: maybe I32?
350 else if (ecb_expect_false (nv == (float)nv))
351 {
352 uint32_t fp = ecb_float_to_binary32 (nv);
353
354 *enc->cur++ = 0xe0 | 26;
355
356 if (!ecb_big_endian ())
357 fp = ecb_bswap32 (fp);
358
359 memcpy (enc->cur, &fp, 4);
360 enc->cur += 4;
361 }
362 else
363 {
364 uint64_t fp = ecb_double_to_binary64 (nv);
365
366 *enc->cur++ = 0xe0 | 27;
367
368 if (!ecb_big_endian ())
369 fp = ecb_bswap64 (fp);
370
371 memcpy (enc->cur, &fp, 8);
372 enc->cur += 8;
373 }
374 }
375
376 static void
377 encode_sv (enc_t *enc, SV *sv)
378 {
379 SvGETMAGIC (sv);
380
381 if (SvPOKp (sv))
382 {
383 STRLEN len;
384 char *str = SvPV (sv, len);
385 encode_str (enc, SvUTF8 (sv), str, len);
386 }
387 else if (SvNOKp (sv))
388 encode_nv (enc, sv);
389 else if (SvIOKp (sv))
390 {
391 if (SvIsUV (sv))
392 encode_uint (enc, 0x00, SvUVX (sv));
393 else if (SvIVX (sv) >= 0)
394 encode_uint (enc, 0x00, SvIVX (sv));
395 else
396 encode_uint (enc, 0x20, -(SvIVX (sv) + 1));
397 }
398 else if (SvROK (sv))
399 encode_rv (enc, SvRV (sv));
400 else if (!SvOK (sv))
401 encode_ch (enc, 0xe0 | 22);
402 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)
403 encode_ch (enc, 0xe0 | 23);
404 else
405 croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data",
406 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
407 }
408
409 static SV *
410 encode_cbor (SV *scalar, CBOR *cbor)
411 {
412 enc_t enc;
413
414 enc.cbor = *cbor;
415 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
416 enc.cur = SvPVX (enc.sv);
417 enc.end = SvEND (enc.sv);
418 enc.depth = 0;
419
420 SvPOK_only (enc.sv);
421 encode_sv (&enc, scalar);
422
423 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
424 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
425
426 if (enc.cbor.flags & F_SHRINK)
427 shrink (enc.sv);
428
429 return enc.sv;
430 }
431
432 /////////////////////////////////////////////////////////////////////////////
433 // decoder
434
435 // structure used for decoding CBOR
436 typedef struct
437 {
438 U8 *cur; // current parser pointer
439 U8 *end; // end of input string
440 const char *err; // parse error, if != 0
441 CBOR cbor;
442 U32 depth; // recursion depth
443 U32 maxdepth; // recursion depth limit
444 } dec_t;
445
446 #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE
447
448 #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data")
449
450 #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED)
451 #define DEC_DEC_DEPTH --dec->depth
452
453 static UV
454 decode_uint (dec_t *dec)
455 {
456 switch (*dec->cur & 31)
457 {
458 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
459 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15:
460 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
461 return *dec->cur++ & 31;
462
463 case 24:
464 WANT (2);
465 dec->cur += 2;
466 return dec->cur[-1];
467
468 case 25:
469 WANT (3);
470 dec->cur += 3;
471 return (((UV)dec->cur[-2]) << 8)
472 | ((UV)dec->cur[-1]);
473
474 case 26:
475 WANT (5);
476 dec->cur += 5;
477 return (((UV)dec->cur[-4]) << 24)
478 | (((UV)dec->cur[-3]) << 16)
479 | (((UV)dec->cur[-2]) << 8)
480 | ((UV)dec->cur[-1]);
481
482 case 27:
483 WANT (9);
484 dec->cur += 9;
485 return (((UV)dec->cur[-8]) << 56)
486 | (((UV)dec->cur[-7]) << 48)
487 | (((UV)dec->cur[-6]) << 40)
488 | (((UV)dec->cur[-5]) << 32)
489 | (((UV)dec->cur[-4]) << 24)
490 | (((UV)dec->cur[-3]) << 16)
491 | (((UV)dec->cur[-2]) << 8)
492 | ((UV)dec->cur[-1]);
493
494 default:
495 ERR ("corrupted CBOR data (unsupported integer minor encoding)");
496 }
497
498 fail:
499 return 0;
500 }
501
502 static SV *decode_sv (dec_t *dec);
503
504 static SV *
505 decode_av (dec_t *dec)
506 {
507 AV *av = newAV ();
508
509 DEC_INC_DEPTH;
510
511 if ((*dec->cur & 31) == 31)
512 {
513 ++dec->cur;
514
515 for (;;)
516 {
517 WANT (1);
518
519 if (*dec->cur == (0xe0 | 31))
520 {
521 ++dec->cur;
522 break;
523 }
524
525 av_push (av, decode_sv (dec));
526 }
527 }
528 else
529 {
530 int i, len = decode_uint (dec);
531
532 av_fill (av, len - 1);
533
534 for (i = 0; i < len; ++i)
535 AvARRAY (av)[i] = decode_sv (dec);
536 }
537
538 DEC_DEC_DEPTH;
539 return newRV_noinc ((SV *)av);
540
541 fail:
542 SvREFCNT_dec (av);
543 DEC_DEC_DEPTH;
544 return &PL_sv_undef;
545 }
546
547 static SV *
548 decode_hv (dec_t *dec)
549 {
550 HV *hv = newHV ();
551
552 DEC_INC_DEPTH;
553
554 if ((*dec->cur & 31) == 31)
555 {
556 ++dec->cur;
557
558 for (;;)
559 {
560 WANT (1);
561
562 if (*dec->cur == (0xe0 | 31))
563 {
564 ++dec->cur;
565 break;
566 }
567
568 SV *k = decode_sv (dec);
569 SV *v = decode_sv (dec);
570
571 hv_store_ent (hv, k, v, 0);
572 }
573 }
574 else
575 {
576 int len = decode_uint (dec);
577
578 while (len--)
579 {
580 SV *k = decode_sv (dec);
581 SV *v = decode_sv (dec);
582
583 hv_store_ent (hv, k, v, 0);
584 }
585 }
586
587 DEC_DEC_DEPTH;
588 return newRV_noinc ((SV *)hv);
589
590 fail:
591 SvREFCNT_dec (hv);
592 DEC_DEC_DEPTH;
593 return &PL_sv_undef;
594 }
595
596 static SV *
597 decode_str (dec_t *dec, int utf8)
598 {
599 SV *sv = 0;
600
601 if ((*dec->cur & 31) == 31)
602 {
603 ++dec->cur;
604
605 sv = newSVpvn ("", 0);
606
607 // not very fast, and certainly not robust against illegal input
608 for (;;)
609 {
610 WANT (1);
611
612 if (*dec->cur == (0xe0 | 31))
613 {
614 ++dec->cur;
615 break;
616 }
617
618 sv_catsv (sv, decode_sv (dec));
619 }
620 }
621 else
622 {
623 STRLEN len = decode_uint (dec);
624
625 WANT (len);
626 sv = newSVpvn (dec->cur, len);
627 dec->cur += len;
628 }
629
630 if (utf8)
631 SvUTF8_on (sv);
632
633 return sv;
634
635 fail:
636 SvREFCNT_dec (sv);
637 return &PL_sv_undef;
638 }
639
640 static SV *
641 decode_tagged (dec_t *dec)
642 {
643 UV tag = decode_uint (dec);
644 SV *sv = decode_sv (dec);
645
646 if (tag == CBOR_TAG_MAGIC)
647 return sv;
648
649 if (tag == CBOR_TAG_PERL_OBJECT)
650 {
651 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
652 ERR ("corrupted CBOR data (non-array perl object)");
653
654 // TODO
655 }
656
657 AV *av = newAV ();
658 av_push (av, newSVuv (tag));
659 av_push (av, sv);
660
661 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
662 ? cbor_tagged_stash
663 : gv_stashpv ("CBOR::XS::Tagged" , 1);
664
665 return sv_bless (newRV_noinc ((SV *)av), tagged_stash);
666
667 fail:
668 SvREFCNT_dec (sv);
669 return &PL_sv_undef;
670 }
671
672 static SV *
673 decode_sv (dec_t *dec)
674 {
675 WANT (1);
676
677 switch (*dec->cur >> 5)
678 {
679 case 0: // unsigned int
680 return newSVuv (decode_uint (dec));
681 case 1: // negative int
682 return newSViv (-1 - (IV)decode_uint (dec));
683 case 2: // octet string
684 return decode_str (dec, 0);
685 case 3: // utf-8 string
686 return decode_str (dec, 1);
687 case 4: // array
688 return decode_av (dec);
689 case 5: // map
690 return decode_hv (dec);
691 case 6: // tag
692 return decode_tagged (dec);
693 case 7: // misc
694 switch (*dec->cur++ & 31)
695 {
696 case 20:
697 #if CBOR_SLOW
698 cbor_false = get_bool ("CBOR::XS::false");
699 #endif
700 return newSVsv (cbor_false);
701 case 21:
702 #if CBOR_SLOW
703 cbor_true = get_bool ("CBOR::XS::true");
704 #endif
705 return newSVsv (cbor_true);
706 case 22:
707 return newSVsv (&PL_sv_undef);
708
709 case 25:
710 {
711 WANT (2);
712
713 uint16_t fp = (dec->cur[0] << 8) | dec->cur[1];
714 dec->cur += 2;
715
716 return newSVnv (ecb_binary16_to_float (fp));
717 }
718
719 case 26:
720 {
721 uint32_t fp;
722 WANT (4);
723 memcpy (&fp, dec->cur, 4);
724 dec->cur += 4;
725
726 if (!ecb_big_endian ())
727 fp = ecb_bswap32 (fp);
728
729 return newSVnv (ecb_binary32_to_float (fp));
730 }
731
732 case 27:
733 {
734 uint64_t fp;
735 WANT (8);
736 memcpy (&fp, dec->cur, 8);
737 dec->cur += 8;
738
739 if (!ecb_big_endian ())
740 fp = ecb_bswap64 (fp);
741
742 return newSVnv (ecb_binary64_to_double (fp));
743 }
744
745 // 0..19 unassigned
746 // 24 reserved + unassigned (reserved values are not encodable)
747 default:
748 ERR ("corrupted CBOR data (reserved/unassigned major 7 value)");
749 }
750
751 break;
752 }
753
754 fail:
755 return &PL_sv_undef;
756 }
757
758 static SV *
759 decode_cbor (SV *string, CBOR *cbor, char **offset_return)
760 {
761 dec_t dec;
762 SV *sv;
763
764 /* work around bugs in 5.10 where manipulating magic values
765 * makes perl ignore the magic in subsequent accesses.
766 * also make a copy of non-PV values, to get them into a clean
767 * state (SvPV should do that, but it's buggy, see below).
768 */
769 /*SvGETMAGIC (string);*/
770 if (SvMAGICAL (string) || !SvPOK (string))
771 string = sv_2mortal (newSVsv (string));
772
773 SvUPGRADE (string, SVt_PV);
774
775 /* work around a bug in perl 5.10, which causes SvCUR to fail an
776 * assertion with -DDEBUGGING, although SvCUR is documented to
777 * return the xpv_cur field which certainly exists after upgrading.
778 * according to nicholas clark, calling SvPOK fixes this.
779 * But it doesn't fix it, so try another workaround, call SvPV_nolen
780 * and hope for the best.
781 * Damnit, SvPV_nolen still trips over yet another assertion. This
782 * assertion business is seriously broken, try yet another workaround
783 * for the broken -DDEBUGGING.
784 */
785 {
786 #ifdef DEBUGGING
787 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
788 #else
789 STRLEN offset = SvCUR (string);
790 #endif
791
792 if (offset > cbor->max_size && cbor->max_size)
793 croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu",
794 (unsigned long)SvCUR (string), (unsigned long)cbor->max_size);
795 }
796
797 sv_utf8_downgrade (string, 0);
798
799 dec.cbor = *cbor;
800 dec.cur = (U8 *)SvPVX (string);
801 dec.end = (U8 *)SvEND (string);
802 dec.err = 0;
803 dec.depth = 0;
804
805 sv = decode_sv (&dec);
806
807 if (offset_return)
808 *offset_return = dec.cur;
809
810 if (!(offset_return || !sv))
811 if (dec.cur != dec.end && !dec.err)
812 dec.err = "garbage after CBOR object";
813
814 if (dec.err)
815 {
816 SvREFCNT_dec (sv);
817 croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur);
818 }
819
820 sv = sv_2mortal (sv);
821
822 return sv;
823 }
824
825 /////////////////////////////////////////////////////////////////////////////
826 // XS interface functions
827
828 MODULE = CBOR::XS PACKAGE = CBOR::XS
829
830 BOOT:
831 {
832 cbor_stash = gv_stashpv ("CBOR::XS" , 1);
833 cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1);
834 cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1);
835
836 cbor_true = get_bool ("CBOR::XS::true");
837 cbor_false = get_bool ("CBOR::XS::false");
838 }
839
840 PROTOTYPES: DISABLE
841
842 void CLONE (...)
843 CODE:
844 cbor_stash = 0;
845 cbor_boolean_stash = 0;
846 cbor_tagged_stash = 0;
847
848 void new (char *klass)
849 PPCODE:
850 {
851 SV *pv = NEWSV (0, sizeof (CBOR));
852 SvPOK_only (pv);
853 cbor_init ((CBOR *)SvPVX (pv));
854 XPUSHs (sv_2mortal (sv_bless (
855 newRV_noinc (pv),
856 strEQ (klass, "CBOR::XS") ? CBOR_STASH : gv_stashpv (klass, 1)
857 )));
858 }
859
860 void shrink (CBOR *self, int enable = 1)
861 ALIAS:
862 shrink = F_SHRINK
863 allow_unknown = F_ALLOW_UNKNOWN
864 PPCODE:
865 {
866 if (enable)
867 self->flags |= ix;
868 else
869 self->flags &= ~ix;
870
871 XPUSHs (ST (0));
872 }
873
874 void get_shrink (CBOR *self)
875 ALIAS:
876 get_shrink = F_SHRINK
877 get_allow_unknown = F_ALLOW_UNKNOWN
878 PPCODE:
879 XPUSHs (boolSV (self->flags & ix));
880
881 void max_depth (CBOR *self, U32 max_depth = 0x80000000UL)
882 PPCODE:
883 self->max_depth = max_depth;
884 XPUSHs (ST (0));
885
886 U32 get_max_depth (CBOR *self)
887 CODE:
888 RETVAL = self->max_depth;
889 OUTPUT:
890 RETVAL
891
892 void max_size (CBOR *self, U32 max_size = 0)
893 PPCODE:
894 self->max_size = max_size;
895 XPUSHs (ST (0));
896
897 int get_max_size (CBOR *self)
898 CODE:
899 RETVAL = self->max_size;
900 OUTPUT:
901 RETVAL
902
903 #if 0 //TODO
904
905 void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef)
906 PPCODE:
907 {
908 SvREFCNT_dec (self->cb_object);
909 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
910
911 XPUSHs (ST (0));
912 }
913
914 void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef)
915 PPCODE:
916 {
917 if (!self->cb_sk_object)
918 self->cb_sk_object = newHV ();
919
920 if (SvOK (cb))
921 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
922 else
923 {
924 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
925
926 if (!HvKEYS (self->cb_sk_object))
927 {
928 SvREFCNT_dec (self->cb_sk_object);
929 self->cb_sk_object = 0;
930 }
931 }
932
933 XPUSHs (ST (0));
934 }
935
936 #endif
937
938 void encode (CBOR *self, SV *scalar)
939 PPCODE:
940 PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN;
941 XPUSHs (scalar);
942
943 void decode (CBOR *self, SV *cborstr)
944 PPCODE:
945 PUTBACK; cborstr = decode_cbor (cborstr, self, 0); SPAGAIN;
946 XPUSHs (cborstr);
947
948 void decode_prefix (CBOR *self, SV *cborstr)
949 PPCODE:
950 {
951 SV *sv;
952 char *offset;
953 PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
954 EXTEND (SP, 2);
955 PUSHs (sv);
956 PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
957 }
958
959 #if 0
960
961 void DESTROY (CBOR *self)
962 CODE:
963 SvREFCNT_dec (self->cb_sk_object);
964 SvREFCNT_dec (self->cb_object);
965
966 #endif
967
968 PROTOTYPES: ENABLE
969
970 void encode_cbor (SV *scalar)
971 PPCODE:
972 {
973 CBOR cbor;
974 cbor_init (&cbor);
975 PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
976 XPUSHs (scalar);
977 }
978
979 void decode_cbor (SV *cborstr)
980 PPCODE:
981 {
982 CBOR cbor;
983 cbor_init (&cbor);
984 PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
985 XPUSHs (cborstr);
986 }
987