ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
Revision: 1.15
Committed: Tue Oct 29 18:37:31 2013 UTC (10 years, 6 months ago) by root
Branch: MAIN
Changes since 1.14: +0 -44 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 root 1.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 root 1.9 // known tags
26 root 1.8 enum cbor_tag
27     {
28 root 1.9 // 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 root 1.8
51 root 1.9 CBOR_TAG_MAGIC = 55799 // self-describe cbor
52 root 1.8 };
53    
54 root 1.1 #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 root 1.10 static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS::
77 root 1.11 static SV *types_true, *types_false, *types_error, *sv_cbor;
78 root 1.1
79     typedef struct {
80     U32 flags;
81     U32 max_depth;
82     STRLEN max_size;
83     } CBOR;
84    
85 root 1.5 ecb_inline void
86 root 1.1 cbor_init (CBOR *cbor)
87     {
88     Zero (cbor, 1, CBOR);
89     cbor->max_depth = 512;
90     }
91    
92     /////////////////////////////////////////////////////////////////////////////
93     // utility functions
94    
95 root 1.5 ecb_inline SV *
96 root 1.1 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 root 1.5 ecb_inline void
107 root 1.1 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 root 1.5 ecb_inline void
140 root 1.1 need (enc_t *enc, STRLEN len)
141     {
142 root 1.5 if (ecb_expect_false (enc->cur + len >= enc->end))
143 root 1.1 {
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 root 1.5 ecb_inline void
152 root 1.1 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 root 1.4 else if (len <= 0xff)
166 root 1.1 {
167     *enc->cur++ = major | 24;
168     *enc->cur++ = len;
169     }
170 root 1.4 else if (len <= 0xffff)
171 root 1.1 {
172     *enc->cur++ = major | 25;
173     *enc->cur++ = len >> 8;
174     *enc->cur++ = len;
175     }
176 root 1.4 else if (len <= 0xffffffff)
177 root 1.1 {
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 root 1.4 else
185 root 1.1 {
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 root 1.5 encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he));
256 root 1.1 }
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 root 1.5 if (ecb_expect_false (SvOBJECT (sv)))
274 root 1.1 {
275 root 1.10 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 root 1.6 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
282     ? cbor_tagged_stash
283     : gv_stashpv ("CBOR::XS::Tagged" , 1);
284 root 1.1
285 root 1.11 HV *stash = SvSTASH (sv);
286     GV *method;
287    
288     if (stash == boolean_stash)
289 root 1.1 encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20);
290 root 1.11 else if (stash == error_stash)
291 root 1.10 encode_ch (enc, 0xe0 | 23);
292 root 1.11 else if (stash == tagged_stash)
293 root 1.6 {
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 root 1.11 else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0)))
301 root 1.1 {
302 root 1.11 dSP;
303    
304     ENTER; SAVETMPS; PUSHMARK (SP);
305 root 1.6 // we re-bless the reference to get overload and other niceties right
306 root 1.11 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 root 1.6
327 root 1.11 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 root 1.1
333 root 1.11 PUTBACK;
334     int count = call_sv ((SV *)GvCV (method), G_ARRAY);
335     SPAGAIN;
336 root 1.6
337 root 1.11 // 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 root 1.6
341 root 1.11 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 root 1.6
345 root 1.11 while (count)
346     encode_sv (enc, SP[1 - count--]);
347 root 1.6
348 root 1.11 PUTBACK;
349 root 1.6
350 root 1.11 FREETMPS; LEAVE;
351 root 1.1 }
352 root 1.11 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 root 1.1 }
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 root 1.5 if (ecb_expect_false (nv == (U32)nv))
390 root 1.1 encode_uint (enc, 0x00, (U32)nv);
391     //TODO: maybe I32?
392 root 1.5 else if (ecb_expect_false (nv == (float)nv))
393 root 1.1 {
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 root 1.5 #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data")
491 root 1.1
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 root 1.2 if (*dec->cur == (0xe0 | 31))
562 root 1.1 {
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 root 1.2 if (*dec->cur == (0xe0 | 31))
605 root 1.1 {
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 root 1.12 SvREFCNT_dec (k);
615 root 1.1 }
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 root 1.12 SvREFCNT_dec (k);
628 root 1.1 }
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 root 1.6 SV *sv = 0;
644 root 1.1
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 root 1.2 if (*dec->cur == (0xe0 | 31))
657 root 1.1 {
658     ++dec->cur;
659     break;
660     }
661    
662 root 1.6 sv_catsv (sv, decode_sv (dec));
663 root 1.1 }
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 root 1.6 SvREFCNT_dec (sv);
681 root 1.1 return &PL_sv_undef;
682     }
683    
684     static SV *
685 root 1.3 decode_tagged (dec_t *dec)
686     {
687     UV tag = decode_uint (dec);
688     SV *sv = decode_sv (dec);
689    
690 root 1.9 if (tag == CBOR_TAG_MAGIC)
691 root 1.3 return sv;
692 root 1.11 else if (tag == CBOR_TAG_PERL_OBJECT)
693 root 1.9 {
694     if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
695     ERR ("corrupted CBOR data (non-array perl object)");
696 root 1.11
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 root 1.9
706 root 1.11 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 root 1.13 SvREFCNT_dec (sv);
727 root 1.11 sv = SvREFCNT_inc (POPs);
728    
729     PUTBACK;
730    
731     FREETMPS; LEAVE;
732    
733     return sv;
734 root 1.9 }
735 root 1.11 else
736     {
737     AV *av = newAV ();
738     av_push (av, newSVuv (tag));
739     av_push (av, sv);
740 root 1.9
741 root 1.11 HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash
742     ? cbor_tagged_stash
743     : gv_stashpv ("CBOR::XS::Tagged" , 1);
744 root 1.7
745 root 1.11 return sv_bless (newRV_noinc ((SV *)av), tagged_stash);
746     }
747 root 1.9
748     fail:
749     SvREFCNT_dec (sv);
750     return &PL_sv_undef;
751 root 1.3 }
752    
753     static SV *
754 root 1.1 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 root 1.3 return decode_tagged (dec);
774 root 1.1 case 7: // misc
775     switch (*dec->cur++ & 31)
776     {
777     case 20:
778     #if CBOR_SLOW
779 root 1.10 types_false = get_bool ("Types::Serialiser::false");
780 root 1.1 #endif
781 root 1.10 return newSVsv (types_false);
782 root 1.1 case 21:
783     #if CBOR_SLOW
784 root 1.10 types_true = get_bool ("Types::Serialiser::true");
785 root 1.1 #endif
786 root 1.10 return newSVsv (types_true);
787 root 1.1 case 22:
788     return newSVsv (&PL_sv_undef);
789 root 1.10 case 23:
790     #if CBOR_SLOW
791     types_error = get_bool ("Types::Serialiser::error");
792     #endif
793     return newSVsv (types_error);
794 root 1.1
795     case 25:
796 root 1.2 {
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 root 1.1
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 root 1.2 if (dec.cur != dec.end && !dec.err)
898     dec.err = "garbage after CBOR object";
899    
900     if (dec.err)
901 root 1.1 {
902 root 1.2 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 root 1.1 }
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 root 1.6 cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1);
920 root 1.1
921 root 1.10 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 root 1.11
928     sv_cbor = newSVpv ("CBOR", 0);
929     SvREADONLY_on (sv_cbor);
930 root 1.1 }
931    
932     PROTOTYPES: DISABLE
933    
934     void CLONE (...)
935     CODE:
936 root 1.10 cbor_stash = 0;
937     cbor_tagged_stash = 0;
938     types_error_stash = 0;
939     types_boolean_stash = 0;
940 root 1.1
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     void encode (CBOR *self, SV *scalar)
997     PPCODE:
998     PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN;
999     XPUSHs (scalar);
1000    
1001     void decode (CBOR *self, SV *cborstr)
1002     PPCODE:
1003     PUTBACK; cborstr = decode_cbor (cborstr, self, 0); SPAGAIN;
1004     XPUSHs (cborstr);
1005    
1006     void decode_prefix (CBOR *self, SV *cborstr)
1007     PPCODE:
1008     {
1009     SV *sv;
1010     char *offset;
1011     PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN;
1012     EXTEND (SP, 2);
1013     PUSHs (sv);
1014     PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr))));
1015     }
1016    
1017     PROTOTYPES: ENABLE
1018    
1019     void encode_cbor (SV *scalar)
1020     PPCODE:
1021     {
1022     CBOR cbor;
1023     cbor_init (&cbor);
1024     PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN;
1025     XPUSHs (scalar);
1026     }
1027    
1028     void decode_cbor (SV *cborstr)
1029     PPCODE:
1030     {
1031     CBOR cbor;
1032     cbor_init (&cbor);
1033     PUTBACK; cborstr = decode_cbor (cborstr, &cbor, 0); SPAGAIN;
1034     XPUSHs (cborstr);
1035     }
1036