… | |
… | |
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, rfc7049 |
15 | # define snprintf _snprintf // C compilers have this in stdio.h |
15 | enum cbor_tag |
16 | #endif |
16 | { |
|
|
17 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
|
|
18 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
|
|
19 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
|
|
20 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
|
|
21 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
|
|
22 | CBOR_TAG_BIGFLOAT = 5, // array |
|
|
23 | |
|
|
24 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
|
|
25 | CBOR_TAG_CONV_B64 = 22, // base64, any |
|
|
26 | CBOR_TAG_CONV_HEX = 23, // base16, any |
|
|
27 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
|
|
28 | |
|
|
29 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
|
|
30 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
|
|
31 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
|
|
32 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
|
|
33 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
|
|
34 | |
|
|
35 | CBOR_TAG_MAGIC = 55799 |
|
|
36 | }; |
17 | |
37 | |
18 | #define F_SHRINK 0x00000200UL |
38 | #define F_SHRINK 0x00000200UL |
19 | #define F_ALLOW_UNKNOWN 0x00002000UL |
39 | #define F_ALLOW_UNKNOWN 0x00002000UL |
20 | |
40 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
41 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | |
42 | |
23 | #define SB do { |
43 | #define SB do { |
24 | #define SE } while (0) |
44 | #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 | |
45 | |
37 | #define IN_RANGE_INC(type,val,beg,end) \ |
46 | #define IN_RANGE_INC(type,val,beg,end) \ |
38 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
47 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
39 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
48 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
40 | |
49 | |
… | |
… | |
46 | #else |
55 | #else |
47 | # define CBOR_SLOW 0 |
56 | # define CBOR_SLOW 0 |
48 | # define CBOR_STASH cbor_stash |
57 | # define CBOR_STASH cbor_stash |
49 | #endif |
58 | #endif |
50 | |
59 | |
51 | static HV *cbor_stash, *cbor_boolean_stash; // CBOR::XS:: |
60 | static HV *cbor_stash, *cbor_boolean_stash, *cbor_tagged_stash; // CBOR::XS:: |
52 | static SV *cbor_true, *cbor_false; |
61 | static SV *cbor_true, *cbor_false; |
53 | |
62 | |
54 | typedef struct { |
63 | typedef struct { |
55 | U32 flags; |
64 | U32 flags; |
56 | U32 max_depth; |
65 | U32 max_depth; |
57 | STRLEN max_size; |
66 | STRLEN max_size; |
58 | |
|
|
59 | SV *cb_object; |
|
|
60 | HV *cb_sk_object; |
|
|
61 | } CBOR; |
67 | } CBOR; |
62 | |
68 | |
63 | INLINE void |
69 | ecb_inline void |
64 | cbor_init (CBOR *cbor) |
70 | cbor_init (CBOR *cbor) |
65 | { |
71 | { |
66 | Zero (cbor, 1, CBOR); |
72 | Zero (cbor, 1, CBOR); |
67 | cbor->max_depth = 512; |
73 | cbor->max_depth = 512; |
68 | } |
74 | } |
69 | |
75 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
76 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
77 | // utility functions |
72 | |
78 | |
73 | INLINE SV * |
79 | ecb_inline SV * |
74 | get_bool (const char *name) |
80 | get_bool (const char *name) |
75 | { |
81 | { |
76 | SV *sv = get_sv (name, 1); |
82 | SV *sv = get_sv (name, 1); |
77 | |
83 | |
78 | SvREADONLY_on (sv); |
84 | SvREADONLY_on (sv); |
79 | SvREADONLY_on (SvRV (sv)); |
85 | SvREADONLY_on (SvRV (sv)); |
80 | |
86 | |
81 | return sv; |
87 | return sv; |
82 | } |
88 | } |
83 | |
89 | |
84 | INLINE void |
90 | ecb_inline void |
85 | shrink (SV *sv) |
91 | shrink (SV *sv) |
86 | { |
92 | { |
87 | sv_utf8_downgrade (sv, 1); |
93 | sv_utf8_downgrade (sv, 1); |
88 | |
94 | |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
95 | if (SvLEN (sv) > SvCUR (sv) + 1) |
… | |
… | |
112 | SV *sv; // result scalar |
118 | SV *sv; // result scalar |
113 | CBOR cbor; |
119 | CBOR cbor; |
114 | U32 depth; // recursion level |
120 | U32 depth; // recursion level |
115 | } enc_t; |
121 | } enc_t; |
116 | |
122 | |
117 | INLINE void |
123 | ecb_inline void |
118 | need (enc_t *enc, STRLEN len) |
124 | need (enc_t *enc, STRLEN len) |
119 | { |
125 | { |
120 | if (expect_false (enc->cur + len >= enc->end)) |
126 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
121 | { |
127 | { |
122 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
128 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
123 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
129 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
124 | enc->cur = SvPVX (enc->sv) + cur; |
130 | enc->cur = SvPVX (enc->sv) + cur; |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
131 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
126 | } |
132 | } |
127 | } |
133 | } |
128 | |
134 | |
129 | INLINE void |
135 | ecb_inline void |
130 | encode_ch (enc_t *enc, char ch) |
136 | encode_ch (enc_t *enc, char ch) |
131 | { |
137 | { |
132 | need (enc, 1); |
138 | need (enc, 1); |
133 | *enc->cur++ = ch; |
139 | *enc->cur++ = ch; |
134 | } |
140 | } |
… | |
… | |
138 | { |
144 | { |
139 | need (enc, 9); |
145 | need (enc, 9); |
140 | |
146 | |
141 | if (len < 24) |
147 | if (len < 24) |
142 | *enc->cur++ = major | len; |
148 | *enc->cur++ = major | len; |
143 | else if (len < 0x100) |
149 | else if (len <= 0xff) |
144 | { |
150 | { |
145 | *enc->cur++ = major | 24; |
151 | *enc->cur++ = major | 24; |
146 | *enc->cur++ = len; |
152 | *enc->cur++ = len; |
147 | } |
153 | } |
148 | else if (len < 0x10000) |
154 | else if (len <= 0xffff) |
149 | { |
155 | { |
150 | *enc->cur++ = major | 25; |
156 | *enc->cur++ = major | 25; |
151 | *enc->cur++ = len >> 8; |
157 | *enc->cur++ = len >> 8; |
152 | *enc->cur++ = len; |
158 | *enc->cur++ = len; |
153 | } |
159 | } |
154 | else if (len < 0x100000000) |
160 | else if (len <= 0xffffffff) |
155 | { |
161 | { |
156 | *enc->cur++ = major | 26; |
162 | *enc->cur++ = major | 26; |
157 | *enc->cur++ = len >> 24; |
163 | *enc->cur++ = len >> 24; |
158 | *enc->cur++ = len >> 16; |
164 | *enc->cur++ = len >> 16; |
159 | *enc->cur++ = len >> 8; |
165 | *enc->cur++ = len >> 8; |
160 | *enc->cur++ = len; |
166 | *enc->cur++ = len; |
161 | } |
167 | } |
162 | else if (len) |
168 | else |
163 | { |
169 | { |
164 | *enc->cur++ = major | 27; |
170 | *enc->cur++ = major | 27; |
165 | *enc->cur++ = len >> 56; |
171 | *enc->cur++ = len >> 56; |
166 | *enc->cur++ = len >> 48; |
172 | *enc->cur++ = len >> 48; |
167 | *enc->cur++ = len >> 40; |
173 | *enc->cur++ = len >> 40; |
… | |
… | |
228 | if (HeKLEN (he) == HEf_SVKEY) |
234 | if (HeKLEN (he) == HEf_SVKEY) |
229 | encode_sv (enc, HeSVKEY (he)); |
235 | encode_sv (enc, HeSVKEY (he)); |
230 | else |
236 | else |
231 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
237 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
232 | |
238 | |
233 | encode_sv (enc, expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
239 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
234 | } |
240 | } |
235 | |
241 | |
236 | if (mg) |
242 | if (mg) |
237 | encode_ch (enc, 0xe0 | 31); |
243 | encode_ch (enc, 0xe0 | 31); |
238 | |
244 | |
… | |
… | |
246 | svtype svt; |
252 | svtype svt; |
247 | |
253 | |
248 | SvGETMAGIC (sv); |
254 | SvGETMAGIC (sv); |
249 | svt = SvTYPE (sv); |
255 | svt = SvTYPE (sv); |
250 | |
256 | |
251 | if (expect_false (SvOBJECT (sv))) |
257 | if (ecb_expect_false (SvOBJECT (sv))) |
252 | { |
258 | { |
253 | HV *stash = !CBOR_SLOW || cbor_boolean_stash |
259 | HV *boolean_stash = !CBOR_SLOW || cbor_boolean_stash |
254 | ? cbor_boolean_stash |
260 | ? cbor_boolean_stash |
255 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
261 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
262 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
263 | ? cbor_tagged_stash |
|
|
264 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
256 | |
265 | |
257 | if (SvSTASH (sv) == stash) |
266 | if (SvSTASH (sv) == boolean_stash) |
258 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
267 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
268 | else if (SvSTASH (sv) == tagged_stash) |
|
|
269 | { |
|
|
270 | if (svt != SVt_PVAV) |
|
|
271 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
|
|
272 | |
|
|
273 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
|
|
274 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
275 | } |
259 | else |
276 | else |
260 | { |
277 | { |
261 | #if 0 //TODO |
278 | // we re-bless the reference to get overload and other niceties right |
262 | if (enc->cbor.flags & F_CONV_BLESSED) |
279 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
280 | |
|
|
281 | if (to_cbor) |
263 | { |
282 | { |
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; |
283 | dSP; |
270 | |
284 | |
271 | ENTER; SAVETMPS; PUSHMARK (SP); |
285 | ENTER; SAVETMPS; PUSHMARK (SP); |
272 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
286 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
273 | |
287 | |
274 | // calling with G_SCALAR ensures that we always get a 1 return value |
288 | // calling with G_SCALAR ensures that we always get a 1 return value |
275 | PUTBACK; |
289 | PUTBACK; |
276 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
290 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
277 | SPAGAIN; |
291 | SPAGAIN; |
278 | |
292 | |
279 | // catch this surprisingly common error |
293 | // catch this surprisingly common error |
280 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
294 | 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))); |
295 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
282 | |
296 | |
283 | sv = POPs; |
297 | sv = POPs; |
284 | PUTBACK; |
298 | PUTBACK; |
285 | |
299 | |
286 | encode_sv (enc, sv); |
300 | encode_sv (enc, sv); |
287 | |
301 | |
288 | FREETMPS; LEAVE; |
302 | 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 | } |
303 | } |
296 | else if (enc->cbor.flags & F_ALLOW_BLESSED) |
|
|
297 | encode_str (enc, "null", 4, 0); |
|
|
298 | else |
304 | else |
299 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
305 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
300 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
306 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
301 | #endif |
|
|
302 | } |
307 | } |
303 | } |
308 | } |
304 | else if (svt == SVt_PVHV) |
309 | else if (svt == SVt_PVHV) |
305 | encode_hv (enc, (HV *)sv); |
310 | encode_hv (enc, (HV *)sv); |
306 | else if (svt == SVt_PVAV) |
311 | else if (svt == SVt_PVAV) |
… | |
… | |
332 | { |
337 | { |
333 | double nv = SvNVX (sv); |
338 | double nv = SvNVX (sv); |
334 | |
339 | |
335 | need (enc, 9); |
340 | need (enc, 9); |
336 | |
341 | |
337 | if (expect_false (nv == (U32)nv)) |
342 | if (ecb_expect_false (nv == (U32)nv)) |
338 | encode_uint (enc, 0x00, (U32)nv); |
343 | encode_uint (enc, 0x00, (U32)nv); |
339 | //TODO: maybe I32? |
344 | //TODO: maybe I32? |
340 | else if (expect_false (nv == (float)nv)) |
345 | else if (ecb_expect_false (nv == (float)nv)) |
341 | { |
346 | { |
342 | uint32_t fp = ecb_float_to_binary32 (nv); |
347 | uint32_t fp = ecb_float_to_binary32 (nv); |
343 | |
348 | |
344 | *enc->cur++ = 0xe0 | 26; |
349 | *enc->cur++ = 0xe0 | 26; |
345 | |
350 | |
… | |
… | |
433 | U32 maxdepth; // recursion depth limit |
438 | U32 maxdepth; // recursion depth limit |
434 | } dec_t; |
439 | } dec_t; |
435 | |
440 | |
436 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
441 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
437 | |
442 | |
438 | #define WANT(len) if (expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data"); |
443 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
439 | |
444 | |
440 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
445 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
441 | #define DEC_DEC_DEPTH --dec->depth |
446 | #define DEC_DEC_DEPTH --dec->depth |
442 | |
447 | |
443 | static UV |
448 | static UV |
… | |
… | |
504 | |
509 | |
505 | for (;;) |
510 | for (;;) |
506 | { |
511 | { |
507 | WANT (1); |
512 | WANT (1); |
508 | |
513 | |
509 | if (*dec->cur == 0xe0 | 31) |
514 | if (*dec->cur == (0xe0 | 31)) |
510 | { |
515 | { |
511 | ++dec->cur; |
516 | ++dec->cur; |
512 | break; |
517 | break; |
513 | } |
518 | } |
514 | |
519 | |
… | |
… | |
547 | |
552 | |
548 | for (;;) |
553 | for (;;) |
549 | { |
554 | { |
550 | WANT (1); |
555 | WANT (1); |
551 | |
556 | |
552 | if (*dec->cur == 0xe0 | 31) |
557 | if (*dec->cur == (0xe0 | 31)) |
553 | { |
558 | { |
554 | ++dec->cur; |
559 | ++dec->cur; |
555 | break; |
560 | break; |
556 | } |
561 | } |
557 | |
562 | |
… | |
… | |
575 | } |
580 | } |
576 | |
581 | |
577 | DEC_DEC_DEPTH; |
582 | DEC_DEC_DEPTH; |
578 | return newRV_noinc ((SV *)hv); |
583 | return newRV_noinc ((SV *)hv); |
579 | |
584 | |
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: |
585 | fail: |
736 | SvREFCNT_dec (hv); |
586 | SvREFCNT_dec (hv); |
737 | DEC_DEC_DEPTH; |
587 | DEC_DEC_DEPTH; |
738 | return &PL_sv_undef; |
588 | return &PL_sv_undef; |
739 | } |
589 | } |
740 | |
590 | |
741 | static SV * |
591 | static SV * |
742 | decode_str (dec_t *dec, int utf8) |
592 | decode_str (dec_t *dec, int utf8) |
743 | { |
593 | { |
744 | SV *sv; |
594 | SV *sv = 0; |
745 | |
595 | |
746 | if ((*dec->cur & 31) == 31) |
596 | if ((*dec->cur & 31) == 31) |
747 | { |
597 | { |
748 | ++dec->cur; |
598 | ++dec->cur; |
749 | |
599 | |
… | |
… | |
752 | // not very fast, and certainly not robust against illegal input |
602 | // not very fast, and certainly not robust against illegal input |
753 | for (;;) |
603 | for (;;) |
754 | { |
604 | { |
755 | WANT (1); |
605 | WANT (1); |
756 | |
606 | |
757 | if (*dec->cur == 0xe0 | 31) |
607 | if (*dec->cur == (0xe0 | 31)) |
758 | { |
608 | { |
759 | ++dec->cur; |
609 | ++dec->cur; |
760 | break; |
610 | break; |
761 | } |
611 | } |
762 | |
612 | |
763 | SV *sv2 = decode_sv (dec); |
|
|
764 | sv_catsv (sv, sv2); |
613 | sv_catsv (sv, decode_sv (dec)); |
765 | } |
614 | } |
766 | } |
615 | } |
767 | else |
616 | else |
768 | { |
617 | { |
769 | STRLEN len = decode_uint (dec); |
618 | STRLEN len = decode_uint (dec); |
… | |
… | |
777 | SvUTF8_on (sv); |
626 | SvUTF8_on (sv); |
778 | |
627 | |
779 | return sv; |
628 | return sv; |
780 | |
629 | |
781 | fail: |
630 | fail: |
|
|
631 | SvREFCNT_dec (sv); |
782 | return &PL_sv_undef; |
632 | return &PL_sv_undef; |
|
|
633 | } |
|
|
634 | |
|
|
635 | static SV * |
|
|
636 | decode_tagged (dec_t *dec) |
|
|
637 | { |
|
|
638 | UV tag = decode_uint (dec); |
|
|
639 | SV *sv = decode_sv (dec); |
|
|
640 | |
|
|
641 | if (tag == CBOR_TAG_MAGIC) // 2.4.5 Self-Describe CBOR |
|
|
642 | return sv; |
|
|
643 | |
|
|
644 | AV *av = newAV (); |
|
|
645 | av_push (av, newSVuv (tag)); |
|
|
646 | av_push (av, sv); |
|
|
647 | |
|
|
648 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
649 | ? cbor_tagged_stash |
|
|
650 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
|
|
651 | |
|
|
652 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
783 | } |
653 | } |
784 | |
654 | |
785 | static SV * |
655 | static SV * |
786 | decode_sv (dec_t *dec) |
656 | decode_sv (dec_t *dec) |
787 | { |
657 | { |
788 | WANT (1); |
658 | WANT (1); |
789 | |
659 | |
790 | switch (*dec->cur >> 5) |
660 | switch (*dec->cur >> 5) |
791 | { |
661 | { |
792 | case 0: // unsigned int |
662 | case 0: // unsigned int |
793 | //TODO: 64 bit values on 3 2bit perls |
|
|
794 | return newSVuv (decode_uint (dec)); |
663 | return newSVuv (decode_uint (dec)); |
795 | case 1: // negative int |
664 | case 1: // negative int |
796 | return newSViv (-1 - (IV)decode_uint (dec)); |
665 | return newSViv (-1 - (IV)decode_uint (dec)); |
797 | case 2: // octet string |
666 | case 2: // octet string |
798 | return decode_str (dec, 0); |
667 | return decode_str (dec, 0); |
… | |
… | |
801 | case 4: // array |
670 | case 4: // array |
802 | return decode_av (dec); |
671 | return decode_av (dec); |
803 | case 5: // map |
672 | case 5: // map |
804 | return decode_hv (dec); |
673 | return decode_hv (dec); |
805 | case 6: // tag |
674 | case 6: // tag |
806 | abort (); |
675 | return decode_tagged (dec); |
807 | break; |
|
|
808 | case 7: // misc |
676 | case 7: // misc |
809 | switch (*dec->cur++ & 31) |
677 | switch (*dec->cur++ & 31) |
810 | { |
678 | { |
811 | case 20: |
679 | case 20: |
812 | #if CBOR_SLOW |
680 | #if CBOR_SLOW |
… | |
… | |
820 | return newSVsv (cbor_true); |
688 | return newSVsv (cbor_true); |
821 | case 22: |
689 | case 22: |
822 | return newSVsv (&PL_sv_undef); |
690 | return newSVsv (&PL_sv_undef); |
823 | |
691 | |
824 | case 25: |
692 | case 25: |
825 | // half float |
|
|
826 | abort (); |
|
|
827 | break; |
693 | { |
|
|
694 | WANT (2); |
|
|
695 | |
|
|
696 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
|
|
697 | dec->cur += 2; |
|
|
698 | |
|
|
699 | return newSVnv (ecb_binary16_to_float (fp)); |
|
|
700 | } |
828 | |
701 | |
829 | case 26: |
702 | case 26: |
830 | { |
703 | { |
831 | uint32_t fp; |
704 | uint32_t fp; |
832 | WANT (4); |
705 | WANT (4); |
… | |
… | |
858 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
731 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
859 | } |
732 | } |
860 | |
733 | |
861 | break; |
734 | break; |
862 | } |
735 | } |
863 | #if 0 |
|
|
864 | switch (*dec->cur) |
|
|
865 | { |
|
|
866 | //case '"': ++dec->cur; return decode_str (dec); |
|
|
867 | case '[': ++dec->cur; return decode_av (dec); |
|
|
868 | case '{': ++dec->cur; return decode_hv (dec); |
|
|
869 | |
|
|
870 | case '-': |
|
|
871 | case '0': case '1': case '2': case '3': case '4': |
|
|
872 | case '5': case '6': case '7': case '8': case '9': |
|
|
873 | //TODO return decode_num (dec); |
|
|
874 | |
|
|
875 | case 't': |
|
|
876 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
|
|
877 | { |
|
|
878 | dec->cur += 4; |
|
|
879 | #if CBOR_SLOW |
|
|
880 | cbor_true = get_bool ("CBOR::XS::true"); |
|
|
881 | #endif |
|
|
882 | return newSVsv (cbor_true); |
|
|
883 | } |
|
|
884 | else |
|
|
885 | ERR ("'true' expected"); |
|
|
886 | |
|
|
887 | break; |
|
|
888 | |
|
|
889 | case 'f': |
|
|
890 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
|
|
891 | { |
|
|
892 | dec->cur += 5; |
|
|
893 | #if CBOR_SLOW |
|
|
894 | cbor_false = get_bool ("CBOR::XS::false"); |
|
|
895 | #endif |
|
|
896 | return newSVsv (cbor_false); |
|
|
897 | } |
|
|
898 | else |
|
|
899 | ERR ("'false' expected"); |
|
|
900 | |
|
|
901 | break; |
|
|
902 | |
|
|
903 | case 'n': |
|
|
904 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
|
|
905 | { |
|
|
906 | dec->cur += 4; |
|
|
907 | return newSVsv (&PL_sv_undef); |
|
|
908 | } |
|
|
909 | else |
|
|
910 | ERR ("'null' expected"); |
|
|
911 | |
|
|
912 | break; |
|
|
913 | |
|
|
914 | default: |
|
|
915 | ERR ("malformed CBOR string, neither array, object, number, string or atom"); |
|
|
916 | break; |
|
|
917 | } |
|
|
918 | #endif |
|
|
919 | |
736 | |
920 | fail: |
737 | fail: |
921 | return &PL_sv_undef; |
738 | return &PL_sv_undef; |
922 | } |
739 | } |
923 | |
740 | |
… | |
… | |
966 | dec.cur = (U8 *)SvPVX (string); |
783 | dec.cur = (U8 *)SvPVX (string); |
967 | dec.end = (U8 *)SvEND (string); |
784 | dec.end = (U8 *)SvEND (string); |
968 | dec.err = 0; |
785 | dec.err = 0; |
969 | dec.depth = 0; |
786 | dec.depth = 0; |
970 | |
787 | |
971 | if (dec.cbor.cb_object || dec.cbor.cb_sk_object) |
|
|
972 | ;//TODO dec.cbor.flags |= F_HOOK; |
|
|
973 | |
|
|
974 | sv = decode_sv (&dec); |
788 | sv = decode_sv (&dec); |
975 | |
789 | |
976 | if (offset_return) |
790 | if (offset_return) |
977 | *offset_return = dec.cur; |
791 | *offset_return = dec.cur; |
978 | |
792 | |
979 | if (!(offset_return || !sv)) |
793 | if (!(offset_return || !sv)) |
980 | { |
|
|
981 | if (*dec.cur && !dec.err) |
794 | if (dec.cur != dec.end && !dec.err) |
982 | { |
|
|
983 | dec.err = "garbage after CBOR object"; |
795 | dec.err = "garbage after CBOR object"; |
|
|
796 | |
|
|
797 | if (dec.err) |
|
|
798 | { |
984 | SvREFCNT_dec (sv); |
799 | SvREFCNT_dec (sv); |
985 | sv = 0; |
|
|
986 | } |
|
|
987 | } |
|
|
988 | |
|
|
989 | if (!sv) |
|
|
990 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
800 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
|
|
801 | } |
991 | |
802 | |
992 | sv = sv_2mortal (sv); |
803 | sv = sv_2mortal (sv); |
993 | |
804 | |
994 | return sv; |
805 | return sv; |
995 | } |
806 | } |
… | |
… | |
1001 | |
812 | |
1002 | BOOT: |
813 | BOOT: |
1003 | { |
814 | { |
1004 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
815 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
1005 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
816 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
817 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
1006 | |
818 | |
1007 | cbor_true = get_bool ("CBOR::XS::true"); |
819 | cbor_true = get_bool ("CBOR::XS::true"); |
1008 | cbor_false = get_bool ("CBOR::XS::false"); |
820 | cbor_false = get_bool ("CBOR::XS::false"); |
1009 | } |
821 | } |
1010 | |
822 | |
… | |
… | |
1012 | |
824 | |
1013 | void CLONE (...) |
825 | void CLONE (...) |
1014 | CODE: |
826 | CODE: |
1015 | cbor_stash = 0; |
827 | cbor_stash = 0; |
1016 | cbor_boolean_stash = 0; |
828 | cbor_boolean_stash = 0; |
|
|
829 | cbor_tagged_stash = 0; |
1017 | |
830 | |
1018 | void new (char *klass) |
831 | void new (char *klass) |
1019 | PPCODE: |
832 | PPCODE: |
1020 | { |
833 | { |
1021 | SV *pv = NEWSV (0, sizeof (CBOR)); |
834 | SV *pv = NEWSV (0, sizeof (CBOR)); |
… | |
… | |
1124 | EXTEND (SP, 2); |
937 | EXTEND (SP, 2); |
1125 | PUSHs (sv); |
938 | PUSHs (sv); |
1126 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
939 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1127 | } |
940 | } |
1128 | |
941 | |
|
|
942 | #if 0 |
|
|
943 | |
1129 | void DESTROY (CBOR *self) |
944 | void DESTROY (CBOR *self) |
1130 | CODE: |
945 | CODE: |
1131 | SvREFCNT_dec (self->cb_sk_object); |
946 | SvREFCNT_dec (self->cb_sk_object); |
1132 | SvREFCNT_dec (self->cb_object); |
947 | SvREFCNT_dec (self->cb_object); |
1133 | |
948 | |
|
|
949 | #endif |
|
|
950 | |
1134 | PROTOTYPES: ENABLE |
951 | PROTOTYPES: ENABLE |
1135 | |
952 | |
1136 | void encode_cbor (SV *scalar) |
953 | void encode_cbor (SV *scalar) |
1137 | PPCODE: |
954 | PPCODE: |
1138 | { |
955 | { |