ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
Revision: 1.4
Committed: Sat Oct 26 21:06:37 2013 UTC (10 years, 6 months ago) by root
Branch: MAIN
Changes since 1.3: +4 -4 lines
Log Message:
*** empty log message ***

File Contents

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