ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
Revision: 1.17
Committed: Tue Oct 29 22:04:52 2013 UTC (10 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-0_07, rel-0_08
Changes since 1.16: +7 -1 lines
Log Message:
0.07

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