… | |
… | |
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, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
52 | static SV *cbor_true, *cbor_false; |
66 | static SV *types_true, *types_false, *types_error; |
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 || types_boolean_stash |
254 | ? cbor_boolean_stash |
265 | ? types_boolean_stash |
|
|
266 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
267 | HV *error_stash = !CBOR_SLOW || types_error_stash |
|
|
268 | ? types_error_stash |
|
|
269 | : gv_stashpv ("Types::Serialiser::Error", 1); |
|
|
270 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
271 | ? cbor_tagged_stash |
255 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
272 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
256 | |
273 | |
257 | if (SvSTASH (sv) == stash) |
274 | if (SvSTASH (sv) == boolean_stash) |
258 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
275 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
276 | else if (SvSTASH (sv) == error_stash) |
|
|
277 | encode_ch (enc, 0xe0 | 23); |
|
|
278 | else if (SvSTASH (sv) == tagged_stash) |
|
|
279 | { |
|
|
280 | if (svt != SVt_PVAV) |
|
|
281 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
|
|
282 | |
|
|
283 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
|
|
284 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
285 | } |
259 | else |
286 | else |
260 | { |
287 | { |
261 | #if 0 //TODO |
288 | // we re-bless the reference to get overload and other niceties right |
262 | if (enc->cbor.flags & F_CONV_BLESSED) |
289 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
290 | |
|
|
291 | if (to_cbor) |
263 | { |
292 | { |
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; |
293 | dSP; |
270 | |
294 | |
271 | ENTER; SAVETMPS; PUSHMARK (SP); |
295 | ENTER; SAVETMPS; PUSHMARK (SP); |
272 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
296 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
273 | |
297 | |
274 | // calling with G_SCALAR ensures that we always get a 1 return value |
298 | // calling with G_SCALAR ensures that we always get a 1 return value |
275 | PUTBACK; |
299 | PUTBACK; |
276 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
300 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
277 | SPAGAIN; |
301 | SPAGAIN; |
278 | |
302 | |
279 | // catch this surprisingly common error |
303 | // catch this surprisingly common error |
280 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
304 | 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))); |
305 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
282 | |
306 | |
283 | sv = POPs; |
307 | sv = POPs; |
284 | PUTBACK; |
308 | PUTBACK; |
285 | |
309 | |
286 | encode_sv (enc, sv); |
310 | encode_sv (enc, sv); |
287 | |
311 | |
288 | FREETMPS; LEAVE; |
312 | 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 | } |
313 | } |
296 | else if (enc->cbor.flags & F_ALLOW_BLESSED) |
|
|
297 | encode_str (enc, "null", 4, 0); |
|
|
298 | else |
314 | else |
299 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
315 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
300 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
316 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
301 | #endif |
|
|
302 | } |
317 | } |
303 | } |
318 | } |
304 | else if (svt == SVt_PVHV) |
319 | else if (svt == SVt_PVHV) |
305 | encode_hv (enc, (HV *)sv); |
320 | encode_hv (enc, (HV *)sv); |
306 | else if (svt == SVt_PVAV) |
321 | else if (svt == SVt_PVAV) |
… | |
… | |
332 | { |
347 | { |
333 | double nv = SvNVX (sv); |
348 | double nv = SvNVX (sv); |
334 | |
349 | |
335 | need (enc, 9); |
350 | need (enc, 9); |
336 | |
351 | |
337 | if (expect_false (nv == (U32)nv)) |
352 | if (ecb_expect_false (nv == (U32)nv)) |
338 | encode_uint (enc, 0x00, (U32)nv); |
353 | encode_uint (enc, 0x00, (U32)nv); |
339 | //TODO: maybe I32? |
354 | //TODO: maybe I32? |
340 | else if (expect_false (nv == (float)nv)) |
355 | else if (ecb_expect_false (nv == (float)nv)) |
341 | { |
356 | { |
342 | uint32_t fp = ecb_float_to_binary32 (nv); |
357 | uint32_t fp = ecb_float_to_binary32 (nv); |
343 | |
358 | |
344 | *enc->cur++ = 0xe0 | 26; |
359 | *enc->cur++ = 0xe0 | 26; |
345 | |
360 | |
… | |
… | |
433 | U32 maxdepth; // recursion depth limit |
448 | U32 maxdepth; // recursion depth limit |
434 | } dec_t; |
449 | } dec_t; |
435 | |
450 | |
436 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
451 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
437 | |
452 | |
438 | #define WANT(len) if (expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data"); |
453 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
439 | |
454 | |
440 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
455 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
441 | #define DEC_DEC_DEPTH --dec->depth |
456 | #define DEC_DEC_DEPTH --dec->depth |
442 | |
457 | |
443 | static UV |
458 | static UV |
… | |
… | |
504 | |
519 | |
505 | for (;;) |
520 | for (;;) |
506 | { |
521 | { |
507 | WANT (1); |
522 | WANT (1); |
508 | |
523 | |
509 | if (*dec->cur == 0xe0 | 31) |
524 | if (*dec->cur == (0xe0 | 31)) |
510 | { |
525 | { |
511 | ++dec->cur; |
526 | ++dec->cur; |
512 | break; |
527 | break; |
513 | } |
528 | } |
514 | |
529 | |
… | |
… | |
547 | |
562 | |
548 | for (;;) |
563 | for (;;) |
549 | { |
564 | { |
550 | WANT (1); |
565 | WANT (1); |
551 | |
566 | |
552 | if (*dec->cur == 0xe0 | 31) |
567 | if (*dec->cur == (0xe0 | 31)) |
553 | { |
568 | { |
554 | ++dec->cur; |
569 | ++dec->cur; |
555 | break; |
570 | break; |
556 | } |
571 | } |
557 | |
572 | |
… | |
… | |
575 | } |
590 | } |
576 | |
591 | |
577 | DEC_DEC_DEPTH; |
592 | DEC_DEC_DEPTH; |
578 | return newRV_noinc ((SV *)hv); |
593 | return newRV_noinc ((SV *)hv); |
579 | |
594 | |
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: |
595 | fail: |
736 | SvREFCNT_dec (hv); |
596 | SvREFCNT_dec (hv); |
737 | DEC_DEC_DEPTH; |
597 | DEC_DEC_DEPTH; |
738 | return &PL_sv_undef; |
598 | return &PL_sv_undef; |
739 | } |
599 | } |
740 | |
600 | |
741 | static SV * |
601 | static SV * |
742 | decode_str (dec_t *dec, int utf8) |
602 | decode_str (dec_t *dec, int utf8) |
743 | { |
603 | { |
744 | SV *sv; |
604 | SV *sv = 0; |
745 | |
605 | |
746 | if ((*dec->cur & 31) == 31) |
606 | if ((*dec->cur & 31) == 31) |
747 | { |
607 | { |
748 | ++dec->cur; |
608 | ++dec->cur; |
749 | |
609 | |
… | |
… | |
752 | // not very fast, and certainly not robust against illegal input |
612 | // not very fast, and certainly not robust against illegal input |
753 | for (;;) |
613 | for (;;) |
754 | { |
614 | { |
755 | WANT (1); |
615 | WANT (1); |
756 | |
616 | |
757 | if (*dec->cur == 0xe0 | 31) |
617 | if (*dec->cur == (0xe0 | 31)) |
758 | { |
618 | { |
759 | ++dec->cur; |
619 | ++dec->cur; |
760 | break; |
620 | break; |
761 | } |
621 | } |
762 | |
622 | |
763 | SV *sv2 = decode_sv (dec); |
|
|
764 | sv_catsv (sv, sv2); |
623 | sv_catsv (sv, decode_sv (dec)); |
765 | } |
624 | } |
766 | } |
625 | } |
767 | else |
626 | else |
768 | { |
627 | { |
769 | STRLEN len = decode_uint (dec); |
628 | STRLEN len = decode_uint (dec); |
… | |
… | |
777 | SvUTF8_on (sv); |
636 | SvUTF8_on (sv); |
778 | |
637 | |
779 | return sv; |
638 | return sv; |
780 | |
639 | |
781 | fail: |
640 | fail: |
|
|
641 | SvREFCNT_dec (sv); |
|
|
642 | return &PL_sv_undef; |
|
|
643 | } |
|
|
644 | |
|
|
645 | static SV * |
|
|
646 | decode_tagged (dec_t *dec) |
|
|
647 | { |
|
|
648 | UV tag = decode_uint (dec); |
|
|
649 | SV *sv = decode_sv (dec); |
|
|
650 | |
|
|
651 | if (tag == CBOR_TAG_MAGIC) |
|
|
652 | return sv; |
|
|
653 | |
|
|
654 | if (tag == CBOR_TAG_PERL_OBJECT) |
|
|
655 | { |
|
|
656 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
657 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
658 | |
|
|
659 | // TODO |
|
|
660 | } |
|
|
661 | |
|
|
662 | AV *av = newAV (); |
|
|
663 | av_push (av, newSVuv (tag)); |
|
|
664 | av_push (av, sv); |
|
|
665 | |
|
|
666 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
667 | ? cbor_tagged_stash |
|
|
668 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
|
|
669 | |
|
|
670 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
671 | |
|
|
672 | fail: |
|
|
673 | SvREFCNT_dec (sv); |
782 | return &PL_sv_undef; |
674 | return &PL_sv_undef; |
783 | } |
675 | } |
784 | |
676 | |
785 | static SV * |
677 | static SV * |
786 | decode_sv (dec_t *dec) |
678 | decode_sv (dec_t *dec) |
… | |
… | |
788 | WANT (1); |
680 | WANT (1); |
789 | |
681 | |
790 | switch (*dec->cur >> 5) |
682 | switch (*dec->cur >> 5) |
791 | { |
683 | { |
792 | case 0: // unsigned int |
684 | case 0: // unsigned int |
793 | //TODO: 64 bit values on 3 2bit perls |
|
|
794 | return newSVuv (decode_uint (dec)); |
685 | return newSVuv (decode_uint (dec)); |
795 | case 1: // negative int |
686 | case 1: // negative int |
796 | return newSViv (-1 - (IV)decode_uint (dec)); |
687 | return newSViv (-1 - (IV)decode_uint (dec)); |
797 | case 2: // octet string |
688 | case 2: // octet string |
798 | return decode_str (dec, 0); |
689 | return decode_str (dec, 0); |
… | |
… | |
801 | case 4: // array |
692 | case 4: // array |
802 | return decode_av (dec); |
693 | return decode_av (dec); |
803 | case 5: // map |
694 | case 5: // map |
804 | return decode_hv (dec); |
695 | return decode_hv (dec); |
805 | case 6: // tag |
696 | case 6: // tag |
806 | abort (); |
697 | return decode_tagged (dec); |
807 | break; |
|
|
808 | case 7: // misc |
698 | case 7: // misc |
809 | switch (*dec->cur++ & 31) |
699 | switch (*dec->cur++ & 31) |
810 | { |
700 | { |
811 | case 20: |
701 | case 20: |
812 | #if CBOR_SLOW |
702 | #if CBOR_SLOW |
813 | cbor_false = get_bool ("CBOR::XS::false"); |
703 | types_false = get_bool ("Types::Serialiser::false"); |
814 | #endif |
704 | #endif |
815 | return newSVsv (cbor_false); |
705 | return newSVsv (types_false); |
816 | case 21: |
706 | case 21: |
817 | #if CBOR_SLOW |
707 | #if CBOR_SLOW |
818 | cbor_true = get_bool ("CBOR::XS::true"); |
708 | types_true = get_bool ("Types::Serialiser::true"); |
819 | #endif |
709 | #endif |
820 | return newSVsv (cbor_true); |
710 | return newSVsv (types_true); |
821 | case 22: |
711 | case 22: |
822 | return newSVsv (&PL_sv_undef); |
712 | return newSVsv (&PL_sv_undef); |
|
|
713 | case 23: |
|
|
714 | #if CBOR_SLOW |
|
|
715 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
716 | #endif |
|
|
717 | return newSVsv (types_error); |
823 | |
718 | |
824 | case 25: |
719 | case 25: |
825 | // half float |
|
|
826 | abort (); |
|
|
827 | break; |
720 | { |
|
|
721 | WANT (2); |
|
|
722 | |
|
|
723 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
|
|
724 | dec->cur += 2; |
|
|
725 | |
|
|
726 | return newSVnv (ecb_binary16_to_float (fp)); |
|
|
727 | } |
828 | |
728 | |
829 | case 26: |
729 | case 26: |
830 | { |
730 | { |
831 | uint32_t fp; |
731 | uint32_t fp; |
832 | WANT (4); |
732 | WANT (4); |
… | |
… | |
858 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
758 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
859 | } |
759 | } |
860 | |
760 | |
861 | break; |
761 | break; |
862 | } |
762 | } |
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 | |
763 | |
920 | fail: |
764 | fail: |
921 | return &PL_sv_undef; |
765 | return &PL_sv_undef; |
922 | } |
766 | } |
923 | |
767 | |
… | |
… | |
966 | dec.cur = (U8 *)SvPVX (string); |
810 | dec.cur = (U8 *)SvPVX (string); |
967 | dec.end = (U8 *)SvEND (string); |
811 | dec.end = (U8 *)SvEND (string); |
968 | dec.err = 0; |
812 | dec.err = 0; |
969 | dec.depth = 0; |
813 | dec.depth = 0; |
970 | |
814 | |
971 | if (dec.cbor.cb_object || dec.cbor.cb_sk_object) |
|
|
972 | ;//TODO dec.cbor.flags |= F_HOOK; |
|
|
973 | |
|
|
974 | sv = decode_sv (&dec); |
815 | sv = decode_sv (&dec); |
975 | |
816 | |
976 | if (offset_return) |
817 | if (offset_return) |
977 | *offset_return = dec.cur; |
818 | *offset_return = dec.cur; |
978 | |
819 | |
979 | if (!(offset_return || !sv)) |
820 | if (!(offset_return || !sv)) |
980 | { |
|
|
981 | if (*dec.cur && !dec.err) |
821 | if (dec.cur != dec.end && !dec.err) |
982 | { |
|
|
983 | dec.err = "garbage after CBOR object"; |
822 | dec.err = "garbage after CBOR object"; |
|
|
823 | |
|
|
824 | if (dec.err) |
|
|
825 | { |
984 | SvREFCNT_dec (sv); |
826 | 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); |
827 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
|
|
828 | } |
991 | |
829 | |
992 | sv = sv_2mortal (sv); |
830 | sv = sv_2mortal (sv); |
993 | |
831 | |
994 | return sv; |
832 | return sv; |
995 | } |
833 | } |
… | |
… | |
1000 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
838 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1001 | |
839 | |
1002 | BOOT: |
840 | BOOT: |
1003 | { |
841 | { |
1004 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
842 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
1005 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
843 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
1006 | |
844 | |
1007 | cbor_true = get_bool ("CBOR::XS::true"); |
845 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1008 | cbor_false = get_bool ("CBOR::XS::false"); |
846 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
|
|
847 | |
|
|
848 | types_true = get_bool ("Types::Serialiser::true" ); |
|
|
849 | types_false = get_bool ("Types::Serialiser::false"); |
|
|
850 | types_error = get_bool ("Types::Serialiser::error"); |
1009 | } |
851 | } |
1010 | |
852 | |
1011 | PROTOTYPES: DISABLE |
853 | PROTOTYPES: DISABLE |
1012 | |
854 | |
1013 | void CLONE (...) |
855 | void CLONE (...) |
1014 | CODE: |
856 | CODE: |
1015 | cbor_stash = 0; |
857 | cbor_stash = 0; |
|
|
858 | cbor_tagged_stash = 0; |
|
|
859 | types_error_stash = 0; |
1016 | cbor_boolean_stash = 0; |
860 | types_boolean_stash = 0; |
1017 | |
861 | |
1018 | void new (char *klass) |
862 | void new (char *klass) |
1019 | PPCODE: |
863 | PPCODE: |
1020 | { |
864 | { |
1021 | SV *pv = NEWSV (0, sizeof (CBOR)); |
865 | SV *pv = NEWSV (0, sizeof (CBOR)); |
… | |
… | |
1124 | EXTEND (SP, 2); |
968 | EXTEND (SP, 2); |
1125 | PUSHs (sv); |
969 | PUSHs (sv); |
1126 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
970 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1127 | } |
971 | } |
1128 | |
972 | |
|
|
973 | #if 0 |
|
|
974 | |
1129 | void DESTROY (CBOR *self) |
975 | void DESTROY (CBOR *self) |
1130 | CODE: |
976 | CODE: |
1131 | SvREFCNT_dec (self->cb_sk_object); |
977 | SvREFCNT_dec (self->cb_sk_object); |
1132 | SvREFCNT_dec (self->cb_object); |
978 | SvREFCNT_dec (self->cb_object); |
1133 | |
979 | |
|
|
980 | #endif |
|
|
981 | |
1134 | PROTOTYPES: ENABLE |
982 | PROTOTYPES: ENABLE |
1135 | |
983 | |
1136 | void encode_cbor (SV *scalar) |
984 | void encode_cbor (SV *scalar) |
1137 | PPCODE: |
985 | PPCODE: |
1138 | { |
986 | { |