… | |
… | |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
11 | |
11 | |
12 | #include "ecb.h" |
12 | #include "ecb.h" |
13 | |
13 | |
14 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
15 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
16 | #endif |
|
|
17 | |
|
|
18 | #define F_SHRINK 0x00000200UL |
14 | #define F_SHRINK 0x00000200UL |
19 | #define F_ALLOW_UNKNOWN 0x00002000UL |
15 | #define F_ALLOW_UNKNOWN 0x00002000UL |
20 | |
16 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
17 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | |
18 | |
23 | #define SB do { |
19 | #define SB do { |
24 | #define SE } while (0) |
20 | #define SE } while (0) |
25 | |
|
|
26 | #if __GNUC__ >= 3 |
|
|
27 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
|
|
28 | # define INLINE static inline |
|
|
29 | #else |
|
|
30 | # define expect(expr,value) (expr) |
|
|
31 | # define INLINE static |
|
|
32 | #endif |
|
|
33 | |
|
|
34 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
35 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
36 | |
21 | |
37 | #define IN_RANGE_INC(type,val,beg,end) \ |
22 | #define IN_RANGE_INC(type,val,beg,end) \ |
38 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
23 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
39 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
24 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
40 | |
25 | |
… | |
… | |
46 | #else |
31 | #else |
47 | # define CBOR_SLOW 0 |
32 | # define CBOR_SLOW 0 |
48 | # define CBOR_STASH cbor_stash |
33 | # define CBOR_STASH cbor_stash |
49 | #endif |
34 | #endif |
50 | |
35 | |
51 | static HV *cbor_stash, *cbor_boolean_stash; // CBOR::XS:: |
36 | static HV *cbor_stash, *cbor_boolean_stash, *cbor_tagged_stash; // CBOR::XS:: |
52 | static SV *cbor_true, *cbor_false; |
37 | static SV *cbor_true, *cbor_false; |
53 | |
38 | |
54 | typedef struct { |
39 | typedef struct { |
55 | U32 flags; |
40 | U32 flags; |
56 | U32 max_depth; |
41 | U32 max_depth; |
57 | STRLEN max_size; |
42 | STRLEN max_size; |
58 | |
|
|
59 | SV *cb_object; |
|
|
60 | HV *cb_sk_object; |
|
|
61 | } CBOR; |
43 | } CBOR; |
62 | |
44 | |
63 | INLINE void |
45 | ecb_inline void |
64 | cbor_init (CBOR *cbor) |
46 | cbor_init (CBOR *cbor) |
65 | { |
47 | { |
66 | Zero (cbor, 1, CBOR); |
48 | Zero (cbor, 1, CBOR); |
67 | cbor->max_depth = 512; |
49 | cbor->max_depth = 512; |
68 | } |
50 | } |
69 | |
51 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
52 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
53 | // utility functions |
72 | |
54 | |
73 | INLINE SV * |
55 | ecb_inline SV * |
74 | get_bool (const char *name) |
56 | get_bool (const char *name) |
75 | { |
57 | { |
76 | SV *sv = get_sv (name, 1); |
58 | SV *sv = get_sv (name, 1); |
77 | |
59 | |
78 | SvREADONLY_on (sv); |
60 | SvREADONLY_on (sv); |
79 | SvREADONLY_on (SvRV (sv)); |
61 | SvREADONLY_on (SvRV (sv)); |
80 | |
62 | |
81 | return sv; |
63 | return sv; |
82 | } |
64 | } |
83 | |
65 | |
84 | INLINE void |
66 | ecb_inline void |
85 | shrink (SV *sv) |
67 | shrink (SV *sv) |
86 | { |
68 | { |
87 | sv_utf8_downgrade (sv, 1); |
69 | sv_utf8_downgrade (sv, 1); |
88 | |
70 | |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
71 | if (SvLEN (sv) > SvCUR (sv) + 1) |
… | |
… | |
112 | SV *sv; // result scalar |
94 | SV *sv; // result scalar |
113 | CBOR cbor; |
95 | CBOR cbor; |
114 | U32 depth; // recursion level |
96 | U32 depth; // recursion level |
115 | } enc_t; |
97 | } enc_t; |
116 | |
98 | |
117 | INLINE void |
99 | ecb_inline void |
118 | need (enc_t *enc, STRLEN len) |
100 | need (enc_t *enc, STRLEN len) |
119 | { |
101 | { |
120 | if (expect_false (enc->cur + len >= enc->end)) |
102 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
121 | { |
103 | { |
122 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
104 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
123 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
105 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
124 | enc->cur = SvPVX (enc->sv) + cur; |
106 | enc->cur = SvPVX (enc->sv) + cur; |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
107 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
126 | } |
108 | } |
127 | } |
109 | } |
128 | |
110 | |
129 | INLINE void |
111 | ecb_inline void |
130 | encode_ch (enc_t *enc, char ch) |
112 | encode_ch (enc_t *enc, char ch) |
131 | { |
113 | { |
132 | need (enc, 1); |
114 | need (enc, 1); |
133 | *enc->cur++ = ch; |
115 | *enc->cur++ = ch; |
134 | } |
116 | } |
… | |
… | |
138 | { |
120 | { |
139 | need (enc, 9); |
121 | need (enc, 9); |
140 | |
122 | |
141 | if (len < 24) |
123 | if (len < 24) |
142 | *enc->cur++ = major | len; |
124 | *enc->cur++ = major | len; |
143 | else if (len < 0x100) |
125 | else if (len <= 0xff) |
144 | { |
126 | { |
145 | *enc->cur++ = major | 24; |
127 | *enc->cur++ = major | 24; |
146 | *enc->cur++ = len; |
128 | *enc->cur++ = len; |
147 | } |
129 | } |
148 | else if (len < 0x10000) |
130 | else if (len <= 0xffff) |
149 | { |
131 | { |
150 | *enc->cur++ = major | 25; |
132 | *enc->cur++ = major | 25; |
151 | *enc->cur++ = len >> 8; |
133 | *enc->cur++ = len >> 8; |
152 | *enc->cur++ = len; |
134 | *enc->cur++ = len; |
153 | } |
135 | } |
154 | else if (len < 0x100000000) |
136 | else if (len <= 0xffffffff) |
155 | { |
137 | { |
156 | *enc->cur++ = major | 26; |
138 | *enc->cur++ = major | 26; |
157 | *enc->cur++ = len >> 24; |
139 | *enc->cur++ = len >> 24; |
158 | *enc->cur++ = len >> 16; |
140 | *enc->cur++ = len >> 16; |
159 | *enc->cur++ = len >> 8; |
141 | *enc->cur++ = len >> 8; |
160 | *enc->cur++ = len; |
142 | *enc->cur++ = len; |
161 | } |
143 | } |
162 | else if (len) |
144 | else |
163 | { |
145 | { |
164 | *enc->cur++ = major | 27; |
146 | *enc->cur++ = major | 27; |
165 | *enc->cur++ = len >> 56; |
147 | *enc->cur++ = len >> 56; |
166 | *enc->cur++ = len >> 48; |
148 | *enc->cur++ = len >> 48; |
167 | *enc->cur++ = len >> 40; |
149 | *enc->cur++ = len >> 40; |
… | |
… | |
228 | if (HeKLEN (he) == HEf_SVKEY) |
210 | if (HeKLEN (he) == HEf_SVKEY) |
229 | encode_sv (enc, HeSVKEY (he)); |
211 | encode_sv (enc, HeSVKEY (he)); |
230 | else |
212 | else |
231 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
213 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
232 | |
214 | |
233 | encode_sv (enc, expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
215 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
234 | } |
216 | } |
235 | |
217 | |
236 | if (mg) |
218 | if (mg) |
237 | encode_ch (enc, 0xe0 | 31); |
219 | encode_ch (enc, 0xe0 | 31); |
238 | |
220 | |
… | |
… | |
246 | svtype svt; |
228 | svtype svt; |
247 | |
229 | |
248 | SvGETMAGIC (sv); |
230 | SvGETMAGIC (sv); |
249 | svt = SvTYPE (sv); |
231 | svt = SvTYPE (sv); |
250 | |
232 | |
251 | if (expect_false (SvOBJECT (sv))) |
233 | if (ecb_expect_false (SvOBJECT (sv))) |
252 | { |
234 | { |
253 | HV *stash = !CBOR_SLOW || cbor_boolean_stash |
235 | HV *boolean_stash = !CBOR_SLOW || cbor_boolean_stash |
254 | ? cbor_boolean_stash |
236 | ? cbor_boolean_stash |
255 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
237 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
238 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
239 | ? cbor_tagged_stash |
|
|
240 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
256 | |
241 | |
257 | if (SvSTASH (sv) == stash) |
242 | if (SvSTASH (sv) == boolean_stash) |
258 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
243 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
244 | else if (SvSTASH (sv) == tagged_stash) |
|
|
245 | { |
|
|
246 | if (svt != SVt_PVAV) |
|
|
247 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
|
|
248 | |
|
|
249 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
|
|
250 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
251 | } |
259 | else |
252 | else |
260 | { |
253 | { |
261 | #if 0 //TODO |
254 | // we re-bless the reference to get overload and other niceties right |
262 | if (enc->cbor.flags & F_CONV_BLESSED) |
255 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
256 | |
|
|
257 | if (to_cbor) |
263 | { |
258 | { |
264 | // we re-bless the reference to get overload and other niceties right |
|
|
265 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
266 | |
|
|
267 | if (to_cbor) |
|
|
268 | { |
|
|
269 | dSP; |
259 | dSP; |
270 | |
260 | |
271 | ENTER; SAVETMPS; PUSHMARK (SP); |
261 | ENTER; SAVETMPS; PUSHMARK (SP); |
272 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
262 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
273 | |
263 | |
274 | // calling with G_SCALAR ensures that we always get a 1 return value |
264 | // calling with G_SCALAR ensures that we always get a 1 return value |
275 | PUTBACK; |
265 | PUTBACK; |
276 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
266 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
277 | SPAGAIN; |
267 | SPAGAIN; |
278 | |
268 | |
279 | // catch this surprisingly common error |
269 | // catch this surprisingly common error |
280 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
270 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
281 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
271 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
282 | |
272 | |
283 | sv = POPs; |
273 | sv = POPs; |
284 | PUTBACK; |
274 | PUTBACK; |
285 | |
275 | |
286 | encode_sv (enc, sv); |
276 | encode_sv (enc, sv); |
287 | |
277 | |
288 | FREETMPS; LEAVE; |
278 | FREETMPS; LEAVE; |
289 | } |
|
|
290 | else if (enc->cbor.flags & F_ALLOW_BLESSED) |
|
|
291 | encode_str (enc, "null", 4, 0); |
|
|
292 | else |
|
|
293 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_CBOR method available on it", |
|
|
294 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
295 | } |
279 | } |
296 | else if (enc->cbor.flags & F_ALLOW_BLESSED) |
|
|
297 | encode_str (enc, "null", 4, 0); |
|
|
298 | else |
280 | else |
299 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
281 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
300 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
282 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
301 | #endif |
|
|
302 | } |
283 | } |
303 | } |
284 | } |
304 | else if (svt == SVt_PVHV) |
285 | else if (svt == SVt_PVHV) |
305 | encode_hv (enc, (HV *)sv); |
286 | encode_hv (enc, (HV *)sv); |
306 | else if (svt == SVt_PVAV) |
287 | else if (svt == SVt_PVAV) |
… | |
… | |
332 | { |
313 | { |
333 | double nv = SvNVX (sv); |
314 | double nv = SvNVX (sv); |
334 | |
315 | |
335 | need (enc, 9); |
316 | need (enc, 9); |
336 | |
317 | |
337 | if (expect_false (nv == (U32)nv)) |
318 | if (ecb_expect_false (nv == (U32)nv)) |
338 | encode_uint (enc, 0x00, (U32)nv); |
319 | encode_uint (enc, 0x00, (U32)nv); |
339 | //TODO: maybe I32? |
320 | //TODO: maybe I32? |
340 | else if (expect_false (nv == (float)nv)) |
321 | else if (ecb_expect_false (nv == (float)nv)) |
341 | { |
322 | { |
342 | uint32_t fp = ecb_float_to_binary32 (nv); |
323 | uint32_t fp = ecb_float_to_binary32 (nv); |
343 | |
324 | |
344 | *enc->cur++ = 0xe0 | 26; |
325 | *enc->cur++ = 0xe0 | 26; |
345 | |
326 | |
… | |
… | |
433 | U32 maxdepth; // recursion depth limit |
414 | U32 maxdepth; // recursion depth limit |
434 | } dec_t; |
415 | } dec_t; |
435 | |
416 | |
436 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
417 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
437 | |
418 | |
438 | #define WANT(len) if (expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data"); |
419 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
439 | |
420 | |
440 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
421 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
441 | #define DEC_DEC_DEPTH --dec->depth |
422 | #define DEC_DEC_DEPTH --dec->depth |
442 | |
423 | |
443 | static UV |
424 | static UV |
… | |
… | |
575 | } |
556 | } |
576 | |
557 | |
577 | DEC_DEC_DEPTH; |
558 | DEC_DEC_DEPTH; |
578 | return newRV_noinc ((SV *)hv); |
559 | return newRV_noinc ((SV *)hv); |
579 | |
560 | |
580 | #if 0 |
|
|
581 | SV *sv; |
|
|
582 | HV *hv = newHV (); |
|
|
583 | |
|
|
584 | DEC_INC_DEPTH; |
|
|
585 | decode_ws (dec); |
|
|
586 | |
|
|
587 | for (;;) |
|
|
588 | { |
|
|
589 | // heuristic: assume that |
|
|
590 | // a) decode_str + hv_store_ent are abysmally slow. |
|
|
591 | // b) most hash keys are short, simple ascii text. |
|
|
592 | // => try to "fast-match" such strings to avoid |
|
|
593 | // the overhead of decode_str + hv_store_ent. |
|
|
594 | { |
|
|
595 | SV *value; |
|
|
596 | char *p = dec->cur; |
|
|
597 | char *e = p + 24; // only try up to 24 bytes |
|
|
598 | |
|
|
599 | for (;;) |
|
|
600 | { |
|
|
601 | // the >= 0x80 is false on most architectures |
|
|
602 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
603 | { |
|
|
604 | // slow path, back up and use decode_str |
|
|
605 | SV *key = decode_str (dec); |
|
|
606 | if (!key) |
|
|
607 | goto fail; |
|
|
608 | |
|
|
609 | decode_ws (dec); EXPECT_CH (':'); |
|
|
610 | |
|
|
611 | decode_ws (dec); |
|
|
612 | value = decode_sv (dec); |
|
|
613 | if (!value) |
|
|
614 | { |
|
|
615 | SvREFCNT_dec (key); |
|
|
616 | goto fail; |
|
|
617 | } |
|
|
618 | |
|
|
619 | hv_store_ent (hv, key, value, 0); |
|
|
620 | SvREFCNT_dec (key); |
|
|
621 | |
|
|
622 | break; |
|
|
623 | } |
|
|
624 | else if (*p == '"') |
|
|
625 | { |
|
|
626 | // fast path, got a simple key |
|
|
627 | char *key = dec->cur; |
|
|
628 | int len = p - key; |
|
|
629 | dec->cur = p + 1; |
|
|
630 | |
|
|
631 | decode_ws (dec); EXPECT_CH (':'); |
|
|
632 | |
|
|
633 | decode_ws (dec); |
|
|
634 | value = decode_sv (dec); |
|
|
635 | if (!value) |
|
|
636 | goto fail; |
|
|
637 | |
|
|
638 | hv_store (hv, key, len, value, 0); |
|
|
639 | |
|
|
640 | break; |
|
|
641 | } |
|
|
642 | |
|
|
643 | ++p; |
|
|
644 | } |
|
|
645 | } |
|
|
646 | |
|
|
647 | decode_ws (dec); |
|
|
648 | |
|
|
649 | if (*dec->cur == '}') |
|
|
650 | { |
|
|
651 | ++dec->cur; |
|
|
652 | break; |
|
|
653 | } |
|
|
654 | |
|
|
655 | if (*dec->cur != ',') |
|
|
656 | ERR (", or } expected while parsing object/hash"); |
|
|
657 | |
|
|
658 | ++dec->cur; |
|
|
659 | |
|
|
660 | decode_ws (dec); |
|
|
661 | |
|
|
662 | if (*dec->cur == '}' && dec->cbor.flags & F_RELAXED) |
|
|
663 | { |
|
|
664 | ++dec->cur; |
|
|
665 | break; |
|
|
666 | } |
|
|
667 | } |
|
|
668 | |
|
|
669 | DEC_DEC_DEPTH; |
|
|
670 | sv = newRV_noinc ((SV *)hv); |
|
|
671 | |
|
|
672 | // check filter callbacks |
|
|
673 | if (dec->cbor.flags & F_HOOK) |
|
|
674 | { |
|
|
675 | if (dec->cbor.cb_sk_object && HvKEYS (hv) == 1) |
|
|
676 | { |
|
|
677 | HE *cb, *he; |
|
|
678 | |
|
|
679 | hv_iterinit (hv); |
|
|
680 | he = hv_iternext (hv); |
|
|
681 | hv_iterinit (hv); |
|
|
682 | |
|
|
683 | // the next line creates a mortal sv each time its called. |
|
|
684 | // might want to optimise this for common cases. |
|
|
685 | cb = hv_fetch_ent (dec->cbor.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
686 | |
|
|
687 | if (cb) |
|
|
688 | { |
|
|
689 | dSP; |
|
|
690 | int count; |
|
|
691 | |
|
|
692 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
693 | XPUSHs (HeVAL (he)); |
|
|
694 | sv_2mortal (sv); |
|
|
695 | |
|
|
696 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
697 | |
|
|
698 | if (count == 1) |
|
|
699 | { |
|
|
700 | sv = newSVsv (POPs); |
|
|
701 | FREETMPS; LEAVE; |
|
|
702 | return sv; |
|
|
703 | } |
|
|
704 | |
|
|
705 | SvREFCNT_inc (sv); |
|
|
706 | FREETMPS; LEAVE; |
|
|
707 | } |
|
|
708 | } |
|
|
709 | |
|
|
710 | if (dec->cbor.cb_object) |
|
|
711 | { |
|
|
712 | dSP; |
|
|
713 | int count; |
|
|
714 | |
|
|
715 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
716 | XPUSHs (sv_2mortal (sv)); |
|
|
717 | |
|
|
718 | PUTBACK; count = call_sv (dec->cbor.cb_object, G_ARRAY); SPAGAIN; |
|
|
719 | |
|
|
720 | if (count == 1) |
|
|
721 | { |
|
|
722 | sv = newSVsv (POPs); |
|
|
723 | FREETMPS; LEAVE; |
|
|
724 | return sv; |
|
|
725 | } |
|
|
726 | |
|
|
727 | SvREFCNT_inc (sv); |
|
|
728 | FREETMPS; LEAVE; |
|
|
729 | } |
|
|
730 | } |
|
|
731 | |
|
|
732 | return sv; |
|
|
733 | #endif |
|
|
734 | |
|
|
735 | fail: |
561 | fail: |
736 | SvREFCNT_dec (hv); |
562 | SvREFCNT_dec (hv); |
737 | DEC_DEC_DEPTH; |
563 | DEC_DEC_DEPTH; |
738 | return &PL_sv_undef; |
564 | return &PL_sv_undef; |
739 | } |
565 | } |
740 | |
566 | |
741 | static SV * |
567 | static SV * |
742 | decode_str (dec_t *dec, int utf8) |
568 | decode_str (dec_t *dec, int utf8) |
743 | { |
569 | { |
744 | SV *sv; |
570 | SV *sv = 0; |
745 | |
571 | |
746 | if ((*dec->cur & 31) == 31) |
572 | if ((*dec->cur & 31) == 31) |
747 | { |
573 | { |
748 | ++dec->cur; |
574 | ++dec->cur; |
749 | |
575 | |
… | |
… | |
758 | { |
584 | { |
759 | ++dec->cur; |
585 | ++dec->cur; |
760 | break; |
586 | break; |
761 | } |
587 | } |
762 | |
588 | |
763 | SV *sv2 = decode_sv (dec); |
|
|
764 | sv_catsv (sv, sv2); |
589 | sv_catsv (sv, decode_sv (dec)); |
765 | } |
590 | } |
766 | } |
591 | } |
767 | else |
592 | else |
768 | { |
593 | { |
769 | STRLEN len = decode_uint (dec); |
594 | STRLEN len = decode_uint (dec); |
… | |
… | |
777 | SvUTF8_on (sv); |
602 | SvUTF8_on (sv); |
778 | |
603 | |
779 | return sv; |
604 | return sv; |
780 | |
605 | |
781 | fail: |
606 | fail: |
|
|
607 | SvREFCNT_dec (sv); |
782 | return &PL_sv_undef; |
608 | return &PL_sv_undef; |
|
|
609 | } |
|
|
610 | |
|
|
611 | static SV * |
|
|
612 | decode_tagged (dec_t *dec) |
|
|
613 | { |
|
|
614 | UV tag = decode_uint (dec); |
|
|
615 | SV *sv = decode_sv (dec); |
|
|
616 | |
|
|
617 | if (tag == 55799) // 2.4.5 Self-Describe CBOR |
|
|
618 | return sv; |
|
|
619 | |
|
|
620 | AV *av = newAV (); |
|
|
621 | av_push (av, newSVuv (tag)); |
|
|
622 | av_push (av, sv); |
|
|
623 | return newRV_noinc ((SV *)av); |
783 | } |
624 | } |
784 | |
625 | |
785 | static SV * |
626 | static SV * |
786 | decode_sv (dec_t *dec) |
627 | decode_sv (dec_t *dec) |
787 | { |
628 | { |
788 | WANT (1); |
629 | WANT (1); |
789 | |
630 | |
790 | switch (*dec->cur >> 5) |
631 | switch (*dec->cur >> 5) |
791 | { |
632 | { |
792 | case 0: // unsigned int |
633 | case 0: // unsigned int |
793 | //TODO: 64 bit values on 3 2bit perls |
|
|
794 | return newSVuv (decode_uint (dec)); |
634 | return newSVuv (decode_uint (dec)); |
795 | case 1: // negative int |
635 | case 1: // negative int |
796 | return newSViv (-1 - (IV)decode_uint (dec)); |
636 | return newSViv (-1 - (IV)decode_uint (dec)); |
797 | case 2: // octet string |
637 | case 2: // octet string |
798 | return decode_str (dec, 0); |
638 | return decode_str (dec, 0); |
… | |
… | |
801 | case 4: // array |
641 | case 4: // array |
802 | return decode_av (dec); |
642 | return decode_av (dec); |
803 | case 5: // map |
643 | case 5: // map |
804 | return decode_hv (dec); |
644 | return decode_hv (dec); |
805 | case 6: // tag |
645 | case 6: // tag |
806 | abort (); |
646 | return decode_tagged (dec); |
807 | break; |
|
|
808 | case 7: // misc |
647 | case 7: // misc |
809 | switch (*dec->cur++ & 31) |
648 | switch (*dec->cur++ & 31) |
810 | { |
649 | { |
811 | case 20: |
650 | case 20: |
812 | #if CBOR_SLOW |
651 | #if CBOR_SLOW |
… | |
… | |
863 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
702 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
864 | } |
703 | } |
865 | |
704 | |
866 | break; |
705 | break; |
867 | } |
706 | } |
868 | #if 0 |
|
|
869 | switch (*dec->cur) |
|
|
870 | { |
|
|
871 | //case '"': ++dec->cur; return decode_str (dec); |
|
|
872 | case '[': ++dec->cur; return decode_av (dec); |
|
|
873 | case '{': ++dec->cur; return decode_hv (dec); |
|
|
874 | |
|
|
875 | case '-': |
|
|
876 | case '0': case '1': case '2': case '3': case '4': |
|
|
877 | case '5': case '6': case '7': case '8': case '9': |
|
|
878 | //TODO return decode_num (dec); |
|
|
879 | |
|
|
880 | case 't': |
|
|
881 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
|
|
882 | { |
|
|
883 | dec->cur += 4; |
|
|
884 | #if CBOR_SLOW |
|
|
885 | cbor_true = get_bool ("CBOR::XS::true"); |
|
|
886 | #endif |
|
|
887 | return newSVsv (cbor_true); |
|
|
888 | } |
|
|
889 | else |
|
|
890 | ERR ("'true' expected"); |
|
|
891 | |
|
|
892 | break; |
|
|
893 | |
|
|
894 | case 'f': |
|
|
895 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
|
|
896 | { |
|
|
897 | dec->cur += 5; |
|
|
898 | #if CBOR_SLOW |
|
|
899 | cbor_false = get_bool ("CBOR::XS::false"); |
|
|
900 | #endif |
|
|
901 | return newSVsv (cbor_false); |
|
|
902 | } |
|
|
903 | else |
|
|
904 | ERR ("'false' expected"); |
|
|
905 | |
|
|
906 | break; |
|
|
907 | |
|
|
908 | case 'n': |
|
|
909 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
|
|
910 | { |
|
|
911 | dec->cur += 4; |
|
|
912 | return newSVsv (&PL_sv_undef); |
|
|
913 | } |
|
|
914 | else |
|
|
915 | ERR ("'null' expected"); |
|
|
916 | |
|
|
917 | break; |
|
|
918 | |
|
|
919 | default: |
|
|
920 | ERR ("malformed CBOR string, neither array, object, number, string or atom"); |
|
|
921 | break; |
|
|
922 | } |
|
|
923 | #endif |
|
|
924 | |
707 | |
925 | fail: |
708 | fail: |
926 | return &PL_sv_undef; |
709 | return &PL_sv_undef; |
927 | } |
710 | } |
928 | |
711 | |
… | |
… | |
971 | dec.cur = (U8 *)SvPVX (string); |
754 | dec.cur = (U8 *)SvPVX (string); |
972 | dec.end = (U8 *)SvEND (string); |
755 | dec.end = (U8 *)SvEND (string); |
973 | dec.err = 0; |
756 | dec.err = 0; |
974 | dec.depth = 0; |
757 | dec.depth = 0; |
975 | |
758 | |
976 | if (dec.cbor.cb_object || dec.cbor.cb_sk_object) |
|
|
977 | ;//TODO dec.cbor.flags |= F_HOOK; |
|
|
978 | |
|
|
979 | sv = decode_sv (&dec); |
759 | sv = decode_sv (&dec); |
980 | |
760 | |
981 | if (offset_return) |
761 | if (offset_return) |
982 | *offset_return = dec.cur; |
762 | *offset_return = dec.cur; |
983 | |
763 | |
… | |
… | |
1003 | |
783 | |
1004 | BOOT: |
784 | BOOT: |
1005 | { |
785 | { |
1006 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
786 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
1007 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
787 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
788 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
1008 | |
789 | |
1009 | cbor_true = get_bool ("CBOR::XS::true"); |
790 | cbor_true = get_bool ("CBOR::XS::true"); |
1010 | cbor_false = get_bool ("CBOR::XS::false"); |
791 | cbor_false = get_bool ("CBOR::XS::false"); |
1011 | } |
792 | } |
1012 | |
793 | |
… | |
… | |
1014 | |
795 | |
1015 | void CLONE (...) |
796 | void CLONE (...) |
1016 | CODE: |
797 | CODE: |
1017 | cbor_stash = 0; |
798 | cbor_stash = 0; |
1018 | cbor_boolean_stash = 0; |
799 | cbor_boolean_stash = 0; |
|
|
800 | cbor_tagged_stash = 0; |
1019 | |
801 | |
1020 | void new (char *klass) |
802 | void new (char *klass) |
1021 | PPCODE: |
803 | PPCODE: |
1022 | { |
804 | { |
1023 | SV *pv = NEWSV (0, sizeof (CBOR)); |
805 | SV *pv = NEWSV (0, sizeof (CBOR)); |
… | |
… | |
1126 | EXTEND (SP, 2); |
908 | EXTEND (SP, 2); |
1127 | PUSHs (sv); |
909 | PUSHs (sv); |
1128 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
910 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1129 | } |
911 | } |
1130 | |
912 | |
|
|
913 | #if 0 |
|
|
914 | |
1131 | void DESTROY (CBOR *self) |
915 | void DESTROY (CBOR *self) |
1132 | CODE: |
916 | CODE: |
1133 | SvREFCNT_dec (self->cb_sk_object); |
917 | SvREFCNT_dec (self->cb_sk_object); |
1134 | SvREFCNT_dec (self->cb_object); |
918 | SvREFCNT_dec (self->cb_object); |
1135 | |
919 | |
|
|
920 | #endif |
|
|
921 | |
1136 | PROTOTYPES: ENABLE |
922 | PROTOTYPES: ENABLE |
1137 | |
923 | |
1138 | void encode_cbor (SV *scalar) |
924 | void encode_cbor (SV *scalar) |
1139 | PPCODE: |
925 | PPCODE: |
1140 | { |
926 | { |