… | |
… | |
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) |
14 | // known tags |
15 | # define snprintf _snprintf // C compilers have this in stdio.h |
15 | enum cbor_tag |
16 | #endif |
16 | { |
|
|
17 | // inofficial extensions (pending iana registration) |
|
|
18 | CBOR_TAG_PERL_OBJECT = 256, |
|
|
19 | CBOR_TAG_GENERIC_OBJECT = 257, |
|
|
20 | |
|
|
21 | // rfc7049 |
|
|
22 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
|
|
23 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
|
|
24 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
|
|
25 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
|
|
26 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
|
|
27 | CBOR_TAG_BIGFLOAT = 5, // array |
|
|
28 | |
|
|
29 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
|
|
30 | CBOR_TAG_CONV_B64 = 22, // base64, any |
|
|
31 | CBOR_TAG_CONV_HEX = 23, // base16, any |
|
|
32 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
|
|
33 | |
|
|
34 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
|
|
35 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
|
|
36 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
|
|
37 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
|
|
38 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
|
|
39 | |
|
|
40 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
|
|
41 | }; |
17 | |
42 | |
18 | #define F_SHRINK 0x00000200UL |
43 | #define F_SHRINK 0x00000200UL |
19 | #define F_ALLOW_UNKNOWN 0x00002000UL |
44 | #define F_ALLOW_UNKNOWN 0x00002000UL |
20 | |
45 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
46 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | |
47 | |
23 | #define SB do { |
48 | #define SB do { |
24 | #define SE } while (0) |
49 | #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 | |
50 | |
37 | #define IN_RANGE_INC(type,val,beg,end) \ |
51 | #define IN_RANGE_INC(type,val,beg,end) \ |
38 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
52 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
39 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
53 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
40 | |
54 | |
… | |
… | |
46 | #else |
60 | #else |
47 | # define CBOR_SLOW 0 |
61 | # define CBOR_SLOW 0 |
48 | # define CBOR_STASH cbor_stash |
62 | # define CBOR_STASH cbor_stash |
49 | #endif |
63 | #endif |
50 | |
64 | |
51 | static HV *cbor_stash, *cbor_boolean_stash; // CBOR::XS:: |
65 | static HV *cbor_stash, *cbor_boolean_stash, *cbor_tagged_stash; // CBOR::XS:: |
52 | static SV *cbor_true, *cbor_false; |
66 | static SV *cbor_true, *cbor_false; |
53 | |
67 | |
54 | typedef struct { |
68 | typedef struct { |
55 | U32 flags; |
69 | U32 flags; |
56 | U32 max_depth; |
70 | U32 max_depth; |
57 | STRLEN max_size; |
71 | STRLEN max_size; |
58 | |
|
|
59 | SV *cb_object; |
|
|
60 | HV *cb_sk_object; |
|
|
61 | } CBOR; |
72 | } CBOR; |
62 | |
73 | |
63 | INLINE void |
74 | ecb_inline void |
64 | cbor_init (CBOR *cbor) |
75 | cbor_init (CBOR *cbor) |
65 | { |
76 | { |
66 | Zero (cbor, 1, CBOR); |
77 | Zero (cbor, 1, CBOR); |
67 | cbor->max_depth = 512; |
78 | cbor->max_depth = 512; |
68 | } |
79 | } |
69 | |
80 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
81 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
82 | // utility functions |
72 | |
83 | |
73 | INLINE SV * |
84 | ecb_inline SV * |
74 | get_bool (const char *name) |
85 | get_bool (const char *name) |
75 | { |
86 | { |
76 | SV *sv = get_sv (name, 1); |
87 | SV *sv = get_sv (name, 1); |
77 | |
88 | |
78 | SvREADONLY_on (sv); |
89 | SvREADONLY_on (sv); |
79 | SvREADONLY_on (SvRV (sv)); |
90 | SvREADONLY_on (SvRV (sv)); |
80 | |
91 | |
81 | return sv; |
92 | return sv; |
82 | } |
93 | } |
83 | |
94 | |
84 | INLINE void |
95 | ecb_inline void |
85 | shrink (SV *sv) |
96 | shrink (SV *sv) |
86 | { |
97 | { |
87 | sv_utf8_downgrade (sv, 1); |
98 | sv_utf8_downgrade (sv, 1); |
88 | |
99 | |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
100 | if (SvLEN (sv) > SvCUR (sv) + 1) |
… | |
… | |
112 | SV *sv; // result scalar |
123 | SV *sv; // result scalar |
113 | CBOR cbor; |
124 | CBOR cbor; |
114 | U32 depth; // recursion level |
125 | U32 depth; // recursion level |
115 | } enc_t; |
126 | } enc_t; |
116 | |
127 | |
117 | INLINE void |
128 | ecb_inline void |
118 | need (enc_t *enc, STRLEN len) |
129 | need (enc_t *enc, STRLEN len) |
119 | { |
130 | { |
120 | if (expect_false (enc->cur + len >= enc->end)) |
131 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
121 | { |
132 | { |
122 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
133 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
123 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
134 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
124 | enc->cur = SvPVX (enc->sv) + cur; |
135 | enc->cur = SvPVX (enc->sv) + cur; |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
136 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
126 | } |
137 | } |
127 | } |
138 | } |
128 | |
139 | |
129 | INLINE void |
140 | ecb_inline void |
130 | encode_ch (enc_t *enc, char ch) |
141 | encode_ch (enc_t *enc, char ch) |
131 | { |
142 | { |
132 | need (enc, 1); |
143 | need (enc, 1); |
133 | *enc->cur++ = ch; |
144 | *enc->cur++ = ch; |
134 | } |
145 | } |
… | |
… | |
138 | { |
149 | { |
139 | need (enc, 9); |
150 | need (enc, 9); |
140 | |
151 | |
141 | if (len < 24) |
152 | if (len < 24) |
142 | *enc->cur++ = major | len; |
153 | *enc->cur++ = major | len; |
143 | else if (len < 0x100) |
154 | else if (len <= 0xff) |
144 | { |
155 | { |
145 | *enc->cur++ = major | 24; |
156 | *enc->cur++ = major | 24; |
146 | *enc->cur++ = len; |
157 | *enc->cur++ = len; |
147 | } |
158 | } |
148 | else if (len < 0x10000) |
159 | else if (len <= 0xffff) |
149 | { |
160 | { |
150 | *enc->cur++ = major | 25; |
161 | *enc->cur++ = major | 25; |
151 | *enc->cur++ = len >> 8; |
162 | *enc->cur++ = len >> 8; |
152 | *enc->cur++ = len; |
163 | *enc->cur++ = len; |
153 | } |
164 | } |
154 | else if (len < 0x100000000) |
165 | else if (len <= 0xffffffff) |
155 | { |
166 | { |
156 | *enc->cur++ = major | 26; |
167 | *enc->cur++ = major | 26; |
157 | *enc->cur++ = len >> 24; |
168 | *enc->cur++ = len >> 24; |
158 | *enc->cur++ = len >> 16; |
169 | *enc->cur++ = len >> 16; |
159 | *enc->cur++ = len >> 8; |
170 | *enc->cur++ = len >> 8; |
160 | *enc->cur++ = len; |
171 | *enc->cur++ = len; |
161 | } |
172 | } |
162 | else if (len) |
173 | else |
163 | { |
174 | { |
164 | *enc->cur++ = major | 27; |
175 | *enc->cur++ = major | 27; |
165 | *enc->cur++ = len >> 56; |
176 | *enc->cur++ = len >> 56; |
166 | *enc->cur++ = len >> 48; |
177 | *enc->cur++ = len >> 48; |
167 | *enc->cur++ = len >> 40; |
178 | *enc->cur++ = len >> 40; |
… | |
… | |
228 | if (HeKLEN (he) == HEf_SVKEY) |
239 | if (HeKLEN (he) == HEf_SVKEY) |
229 | encode_sv (enc, HeSVKEY (he)); |
240 | encode_sv (enc, HeSVKEY (he)); |
230 | else |
241 | else |
231 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
242 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
232 | |
243 | |
233 | encode_sv (enc, expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
244 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
234 | } |
245 | } |
235 | |
246 | |
236 | if (mg) |
247 | if (mg) |
237 | encode_ch (enc, 0xe0 | 31); |
248 | encode_ch (enc, 0xe0 | 31); |
238 | |
249 | |
… | |
… | |
246 | svtype svt; |
257 | svtype svt; |
247 | |
258 | |
248 | SvGETMAGIC (sv); |
259 | SvGETMAGIC (sv); |
249 | svt = SvTYPE (sv); |
260 | svt = SvTYPE (sv); |
250 | |
261 | |
251 | if (expect_false (SvOBJECT (sv))) |
262 | if (ecb_expect_false (SvOBJECT (sv))) |
252 | { |
263 | { |
253 | HV *stash = !CBOR_SLOW || cbor_boolean_stash |
264 | HV *boolean_stash = !CBOR_SLOW || cbor_boolean_stash |
254 | ? cbor_boolean_stash |
265 | ? cbor_boolean_stash |
255 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
266 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
267 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
268 | ? cbor_tagged_stash |
|
|
269 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
256 | |
270 | |
257 | if (SvSTASH (sv) == stash) |
271 | if (SvSTASH (sv) == boolean_stash) |
258 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
272 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
273 | else if (SvSTASH (sv) == tagged_stash) |
|
|
274 | { |
|
|
275 | if (svt != SVt_PVAV) |
|
|
276 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
|
|
277 | |
|
|
278 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
|
|
279 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
280 | } |
259 | else |
281 | else |
260 | { |
282 | { |
261 | #if 0 //TODO |
283 | // we re-bless the reference to get overload and other niceties right |
262 | if (enc->cbor.flags & F_CONV_BLESSED) |
284 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
285 | |
|
|
286 | if (to_cbor) |
263 | { |
287 | { |
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; |
288 | dSP; |
270 | |
289 | |
271 | ENTER; SAVETMPS; PUSHMARK (SP); |
290 | ENTER; SAVETMPS; PUSHMARK (SP); |
272 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
291 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
273 | |
292 | |
274 | // calling with G_SCALAR ensures that we always get a 1 return value |
293 | // calling with G_SCALAR ensures that we always get a 1 return value |
275 | PUTBACK; |
294 | PUTBACK; |
276 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
295 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
277 | SPAGAIN; |
296 | SPAGAIN; |
278 | |
297 | |
279 | // catch this surprisingly common error |
298 | // catch this surprisingly common error |
280 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
299 | 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))); |
300 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
282 | |
301 | |
283 | sv = POPs; |
302 | sv = POPs; |
284 | PUTBACK; |
303 | PUTBACK; |
285 | |
304 | |
286 | encode_sv (enc, sv); |
305 | encode_sv (enc, sv); |
287 | |
306 | |
288 | FREETMPS; LEAVE; |
307 | 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 | } |
308 | } |
296 | else if (enc->cbor.flags & F_ALLOW_BLESSED) |
|
|
297 | encode_str (enc, "null", 4, 0); |
|
|
298 | else |
309 | else |
299 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
310 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
300 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
311 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
301 | #endif |
|
|
302 | } |
312 | } |
303 | } |
313 | } |
304 | else if (svt == SVt_PVHV) |
314 | else if (svt == SVt_PVHV) |
305 | encode_hv (enc, (HV *)sv); |
315 | encode_hv (enc, (HV *)sv); |
306 | else if (svt == SVt_PVAV) |
316 | else if (svt == SVt_PVAV) |
… | |
… | |
332 | { |
342 | { |
333 | double nv = SvNVX (sv); |
343 | double nv = SvNVX (sv); |
334 | |
344 | |
335 | need (enc, 9); |
345 | need (enc, 9); |
336 | |
346 | |
337 | if (expect_false (nv == (U32)nv)) |
347 | if (ecb_expect_false (nv == (U32)nv)) |
338 | encode_uint (enc, 0x00, (U32)nv); |
348 | encode_uint (enc, 0x00, (U32)nv); |
339 | //TODO: maybe I32? |
349 | //TODO: maybe I32? |
340 | else if (expect_false (nv == (float)nv)) |
350 | else if (ecb_expect_false (nv == (float)nv)) |
341 | { |
351 | { |
342 | uint32_t fp = ecb_float_to_binary32 (nv); |
352 | uint32_t fp = ecb_float_to_binary32 (nv); |
343 | |
353 | |
344 | *enc->cur++ = 0xe0 | 26; |
354 | *enc->cur++ = 0xe0 | 26; |
345 | |
355 | |
… | |
… | |
433 | U32 maxdepth; // recursion depth limit |
443 | U32 maxdepth; // recursion depth limit |
434 | } dec_t; |
444 | } dec_t; |
435 | |
445 | |
436 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
446 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
437 | |
447 | |
438 | #define WANT(len) if (expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data"); |
448 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
439 | |
449 | |
440 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
450 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
441 | #define DEC_DEC_DEPTH --dec->depth |
451 | #define DEC_DEC_DEPTH --dec->depth |
442 | |
452 | |
443 | static UV |
453 | static UV |
… | |
… | |
575 | } |
585 | } |
576 | |
586 | |
577 | DEC_DEC_DEPTH; |
587 | DEC_DEC_DEPTH; |
578 | return newRV_noinc ((SV *)hv); |
588 | return newRV_noinc ((SV *)hv); |
579 | |
589 | |
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: |
590 | fail: |
736 | SvREFCNT_dec (hv); |
591 | SvREFCNT_dec (hv); |
737 | DEC_DEC_DEPTH; |
592 | DEC_DEC_DEPTH; |
738 | return &PL_sv_undef; |
593 | return &PL_sv_undef; |
739 | } |
594 | } |
740 | |
595 | |
741 | static SV * |
596 | static SV * |
742 | decode_str (dec_t *dec, int utf8) |
597 | decode_str (dec_t *dec, int utf8) |
743 | { |
598 | { |
744 | SV *sv; |
599 | SV *sv = 0; |
745 | |
600 | |
746 | if ((*dec->cur & 31) == 31) |
601 | if ((*dec->cur & 31) == 31) |
747 | { |
602 | { |
748 | ++dec->cur; |
603 | ++dec->cur; |
749 | |
604 | |
… | |
… | |
758 | { |
613 | { |
759 | ++dec->cur; |
614 | ++dec->cur; |
760 | break; |
615 | break; |
761 | } |
616 | } |
762 | |
617 | |
763 | SV *sv2 = decode_sv (dec); |
|
|
764 | sv_catsv (sv, sv2); |
618 | sv_catsv (sv, decode_sv (dec)); |
765 | } |
619 | } |
766 | } |
620 | } |
767 | else |
621 | else |
768 | { |
622 | { |
769 | STRLEN len = decode_uint (dec); |
623 | STRLEN len = decode_uint (dec); |
… | |
… | |
777 | SvUTF8_on (sv); |
631 | SvUTF8_on (sv); |
778 | |
632 | |
779 | return sv; |
633 | return sv; |
780 | |
634 | |
781 | fail: |
635 | fail: |
|
|
636 | SvREFCNT_dec (sv); |
|
|
637 | return &PL_sv_undef; |
|
|
638 | } |
|
|
639 | |
|
|
640 | static SV * |
|
|
641 | decode_tagged (dec_t *dec) |
|
|
642 | { |
|
|
643 | UV tag = decode_uint (dec); |
|
|
644 | SV *sv = decode_sv (dec); |
|
|
645 | |
|
|
646 | if (tag == CBOR_TAG_MAGIC) |
|
|
647 | return sv; |
|
|
648 | |
|
|
649 | if (tag == CBOR_TAG_PERL_OBJECT) |
|
|
650 | { |
|
|
651 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
652 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
653 | |
|
|
654 | // TODO |
|
|
655 | } |
|
|
656 | |
|
|
657 | AV *av = newAV (); |
|
|
658 | av_push (av, newSVuv (tag)); |
|
|
659 | av_push (av, sv); |
|
|
660 | |
|
|
661 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
662 | ? cbor_tagged_stash |
|
|
663 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
|
|
664 | |
|
|
665 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
666 | |
|
|
667 | fail: |
|
|
668 | SvREFCNT_dec (sv); |
782 | return &PL_sv_undef; |
669 | return &PL_sv_undef; |
783 | } |
670 | } |
784 | |
671 | |
785 | static SV * |
672 | static SV * |
786 | decode_sv (dec_t *dec) |
673 | decode_sv (dec_t *dec) |
… | |
… | |
788 | WANT (1); |
675 | WANT (1); |
789 | |
676 | |
790 | switch (*dec->cur >> 5) |
677 | switch (*dec->cur >> 5) |
791 | { |
678 | { |
792 | case 0: // unsigned int |
679 | case 0: // unsigned int |
793 | //TODO: 64 bit values on 3 2bit perls |
|
|
794 | return newSVuv (decode_uint (dec)); |
680 | return newSVuv (decode_uint (dec)); |
795 | case 1: // negative int |
681 | case 1: // negative int |
796 | return newSViv (-1 - (IV)decode_uint (dec)); |
682 | return newSViv (-1 - (IV)decode_uint (dec)); |
797 | case 2: // octet string |
683 | case 2: // octet string |
798 | return decode_str (dec, 0); |
684 | return decode_str (dec, 0); |
… | |
… | |
801 | case 4: // array |
687 | case 4: // array |
802 | return decode_av (dec); |
688 | return decode_av (dec); |
803 | case 5: // map |
689 | case 5: // map |
804 | return decode_hv (dec); |
690 | return decode_hv (dec); |
805 | case 6: // tag |
691 | case 6: // tag |
806 | abort (); |
692 | return decode_tagged (dec); |
807 | break; |
|
|
808 | case 7: // misc |
693 | case 7: // misc |
809 | switch (*dec->cur++ & 31) |
694 | switch (*dec->cur++ & 31) |
810 | { |
695 | { |
811 | case 20: |
696 | case 20: |
812 | #if CBOR_SLOW |
697 | #if CBOR_SLOW |
… | |
… | |
863 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
748 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
864 | } |
749 | } |
865 | |
750 | |
866 | break; |
751 | break; |
867 | } |
752 | } |
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 | |
753 | |
925 | fail: |
754 | fail: |
926 | return &PL_sv_undef; |
755 | return &PL_sv_undef; |
927 | } |
756 | } |
928 | |
757 | |
… | |
… | |
971 | dec.cur = (U8 *)SvPVX (string); |
800 | dec.cur = (U8 *)SvPVX (string); |
972 | dec.end = (U8 *)SvEND (string); |
801 | dec.end = (U8 *)SvEND (string); |
973 | dec.err = 0; |
802 | dec.err = 0; |
974 | dec.depth = 0; |
803 | dec.depth = 0; |
975 | |
804 | |
976 | if (dec.cbor.cb_object || dec.cbor.cb_sk_object) |
|
|
977 | ;//TODO dec.cbor.flags |= F_HOOK; |
|
|
978 | |
|
|
979 | sv = decode_sv (&dec); |
805 | sv = decode_sv (&dec); |
980 | |
806 | |
981 | if (offset_return) |
807 | if (offset_return) |
982 | *offset_return = dec.cur; |
808 | *offset_return = dec.cur; |
983 | |
809 | |
… | |
… | |
1003 | |
829 | |
1004 | BOOT: |
830 | BOOT: |
1005 | { |
831 | { |
1006 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
832 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
1007 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
833 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
834 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
1008 | |
835 | |
1009 | cbor_true = get_bool ("CBOR::XS::true"); |
836 | cbor_true = get_bool ("CBOR::XS::true"); |
1010 | cbor_false = get_bool ("CBOR::XS::false"); |
837 | cbor_false = get_bool ("CBOR::XS::false"); |
1011 | } |
838 | } |
1012 | |
839 | |
… | |
… | |
1014 | |
841 | |
1015 | void CLONE (...) |
842 | void CLONE (...) |
1016 | CODE: |
843 | CODE: |
1017 | cbor_stash = 0; |
844 | cbor_stash = 0; |
1018 | cbor_boolean_stash = 0; |
845 | cbor_boolean_stash = 0; |
|
|
846 | cbor_tagged_stash = 0; |
1019 | |
847 | |
1020 | void new (char *klass) |
848 | void new (char *klass) |
1021 | PPCODE: |
849 | PPCODE: |
1022 | { |
850 | { |
1023 | SV *pv = NEWSV (0, sizeof (CBOR)); |
851 | SV *pv = NEWSV (0, sizeof (CBOR)); |
… | |
… | |
1126 | EXTEND (SP, 2); |
954 | EXTEND (SP, 2); |
1127 | PUSHs (sv); |
955 | PUSHs (sv); |
1128 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
956 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1129 | } |
957 | } |
1130 | |
958 | |
|
|
959 | #if 0 |
|
|
960 | |
1131 | void DESTROY (CBOR *self) |
961 | void DESTROY (CBOR *self) |
1132 | CODE: |
962 | CODE: |
1133 | SvREFCNT_dec (self->cb_sk_object); |
963 | SvREFCNT_dec (self->cb_sk_object); |
1134 | SvREFCNT_dec (self->cb_object); |
964 | SvREFCNT_dec (self->cb_object); |
1135 | |
965 | |
|
|
966 | #endif |
|
|
967 | |
1136 | PROTOTYPES: ENABLE |
968 | PROTOTYPES: ENABLE |
1137 | |
969 | |
1138 | void encode_cbor (SV *scalar) |
970 | void encode_cbor (SV *scalar) |
1139 | PPCODE: |
971 | PPCODE: |
1140 | { |
972 | { |