… | |
… | |
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 | // compatibility with perl <5.18 |
15 | # define snprintf _snprintf // C compilers have this in stdio.h |
15 | #ifndef HvNAMELEN_get |
|
|
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
16 | #endif |
17 | #endif |
|
|
18 | #ifndef HvNAMELEN |
|
|
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
20 | #endif |
|
|
21 | #ifndef HvNAMEUTF8 |
|
|
22 | # define HvNAMEUTF8(hv) 0 |
|
|
23 | #endif |
|
|
24 | |
|
|
25 | // known tags |
|
|
26 | enum cbor_tag |
|
|
27 | { |
|
|
28 | // inofficial extensions (pending iana registration) |
|
|
29 | CBOR_TAG_PERL_OBJECT = 256, |
|
|
30 | CBOR_TAG_GENERIC_OBJECT = 257, |
|
|
31 | |
|
|
32 | // rfc7049 |
|
|
33 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
|
|
34 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
|
|
35 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
|
|
36 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
|
|
37 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
|
|
38 | CBOR_TAG_BIGFLOAT = 5, // array |
|
|
39 | |
|
|
40 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
|
|
41 | CBOR_TAG_CONV_B64 = 22, // base64, any |
|
|
42 | CBOR_TAG_CONV_HEX = 23, // base16, any |
|
|
43 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
|
|
44 | |
|
|
45 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
|
|
46 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
|
|
47 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
|
|
48 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
|
|
49 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
|
|
50 | |
|
|
51 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
|
|
52 | }; |
17 | |
53 | |
18 | #define F_SHRINK 0x00000200UL |
54 | #define F_SHRINK 0x00000200UL |
19 | #define F_ALLOW_UNKNOWN 0x00002000UL |
55 | #define F_ALLOW_UNKNOWN 0x00002000UL |
20 | |
56 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
57 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | |
58 | |
23 | #define SB do { |
59 | #define SB do { |
24 | #define SE } while (0) |
60 | #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 | |
61 | |
37 | #define IN_RANGE_INC(type,val,beg,end) \ |
62 | #define IN_RANGE_INC(type,val,beg,end) \ |
38 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
63 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
39 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
64 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
40 | |
65 | |
… | |
… | |
46 | #else |
71 | #else |
47 | # define CBOR_SLOW 0 |
72 | # define CBOR_SLOW 0 |
48 | # define CBOR_STASH cbor_stash |
73 | # define CBOR_STASH cbor_stash |
49 | #endif |
74 | #endif |
50 | |
75 | |
51 | static HV *cbor_stash, *cbor_boolean_stash; // CBOR::XS:: |
76 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
52 | static SV *cbor_true, *cbor_false; |
77 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
53 | |
78 | |
54 | typedef struct { |
79 | typedef struct { |
55 | U32 flags; |
80 | U32 flags; |
56 | U32 max_depth; |
81 | U32 max_depth; |
57 | STRLEN max_size; |
82 | STRLEN max_size; |
58 | |
|
|
59 | SV *cb_object; |
|
|
60 | HV *cb_sk_object; |
|
|
61 | } CBOR; |
83 | } CBOR; |
62 | |
84 | |
63 | INLINE void |
85 | ecb_inline void |
64 | cbor_init (CBOR *cbor) |
86 | cbor_init (CBOR *cbor) |
65 | { |
87 | { |
66 | Zero (cbor, 1, CBOR); |
88 | Zero (cbor, 1, CBOR); |
67 | cbor->max_depth = 512; |
89 | cbor->max_depth = 512; |
68 | } |
90 | } |
69 | |
91 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
92 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
93 | // utility functions |
72 | |
94 | |
73 | INLINE SV * |
95 | ecb_inline SV * |
74 | get_bool (const char *name) |
96 | get_bool (const char *name) |
75 | { |
97 | { |
76 | SV *sv = get_sv (name, 1); |
98 | SV *sv = get_sv (name, 1); |
77 | |
99 | |
78 | SvREADONLY_on (sv); |
100 | SvREADONLY_on (sv); |
79 | SvREADONLY_on (SvRV (sv)); |
101 | SvREADONLY_on (SvRV (sv)); |
80 | |
102 | |
81 | return sv; |
103 | return sv; |
82 | } |
104 | } |
83 | |
105 | |
84 | INLINE void |
106 | ecb_inline void |
85 | shrink (SV *sv) |
107 | shrink (SV *sv) |
86 | { |
108 | { |
87 | sv_utf8_downgrade (sv, 1); |
109 | sv_utf8_downgrade (sv, 1); |
88 | |
110 | |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
111 | if (SvLEN (sv) > SvCUR (sv) + 1) |
… | |
… | |
112 | SV *sv; // result scalar |
134 | SV *sv; // result scalar |
113 | CBOR cbor; |
135 | CBOR cbor; |
114 | U32 depth; // recursion level |
136 | U32 depth; // recursion level |
115 | } enc_t; |
137 | } enc_t; |
116 | |
138 | |
117 | INLINE void |
139 | ecb_inline void |
118 | need (enc_t *enc, STRLEN len) |
140 | need (enc_t *enc, STRLEN len) |
119 | { |
141 | { |
120 | if (expect_false (enc->cur + len >= enc->end)) |
142 | if (ecb_expect_false (enc->cur + len >= enc->end)) |
121 | { |
143 | { |
122 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
144 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
123 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
145 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
124 | enc->cur = SvPVX (enc->sv) + cur; |
146 | enc->cur = SvPVX (enc->sv) + cur; |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
147 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
126 | } |
148 | } |
127 | } |
149 | } |
128 | |
150 | |
129 | INLINE void |
151 | ecb_inline void |
130 | encode_ch (enc_t *enc, char ch) |
152 | encode_ch (enc_t *enc, char ch) |
131 | { |
153 | { |
132 | need (enc, 1); |
154 | need (enc, 1); |
133 | *enc->cur++ = ch; |
155 | *enc->cur++ = ch; |
134 | } |
156 | } |
… | |
… | |
138 | { |
160 | { |
139 | need (enc, 9); |
161 | need (enc, 9); |
140 | |
162 | |
141 | if (len < 24) |
163 | if (len < 24) |
142 | *enc->cur++ = major | len; |
164 | *enc->cur++ = major | len; |
143 | else if (len < 0x100) |
165 | else if (len <= 0xff) |
144 | { |
166 | { |
145 | *enc->cur++ = major | 24; |
167 | *enc->cur++ = major | 24; |
146 | *enc->cur++ = len; |
168 | *enc->cur++ = len; |
147 | } |
169 | } |
148 | else if (len < 0x10000) |
170 | else if (len <= 0xffff) |
149 | { |
171 | { |
150 | *enc->cur++ = major | 25; |
172 | *enc->cur++ = major | 25; |
151 | *enc->cur++ = len >> 8; |
173 | *enc->cur++ = len >> 8; |
152 | *enc->cur++ = len; |
174 | *enc->cur++ = len; |
153 | } |
175 | } |
154 | else if (len < 0x100000000) |
176 | else if (len <= 0xffffffff) |
155 | { |
177 | { |
156 | *enc->cur++ = major | 26; |
178 | *enc->cur++ = major | 26; |
157 | *enc->cur++ = len >> 24; |
179 | *enc->cur++ = len >> 24; |
158 | *enc->cur++ = len >> 16; |
180 | *enc->cur++ = len >> 16; |
159 | *enc->cur++ = len >> 8; |
181 | *enc->cur++ = len >> 8; |
160 | *enc->cur++ = len; |
182 | *enc->cur++ = len; |
161 | } |
183 | } |
162 | else if (len) |
184 | else |
163 | { |
185 | { |
164 | *enc->cur++ = major | 27; |
186 | *enc->cur++ = major | 27; |
165 | *enc->cur++ = len >> 56; |
187 | *enc->cur++ = len >> 56; |
166 | *enc->cur++ = len >> 48; |
188 | *enc->cur++ = len >> 48; |
167 | *enc->cur++ = len >> 40; |
189 | *enc->cur++ = len >> 40; |
… | |
… | |
228 | if (HeKLEN (he) == HEf_SVKEY) |
250 | if (HeKLEN (he) == HEf_SVKEY) |
229 | encode_sv (enc, HeSVKEY (he)); |
251 | encode_sv (enc, HeSVKEY (he)); |
230 | else |
252 | else |
231 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
253 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
232 | |
254 | |
233 | encode_sv (enc, expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
255 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
234 | } |
256 | } |
235 | |
257 | |
236 | if (mg) |
258 | if (mg) |
237 | encode_ch (enc, 0xe0 | 31); |
259 | encode_ch (enc, 0xe0 | 31); |
238 | |
260 | |
… | |
… | |
246 | svtype svt; |
268 | svtype svt; |
247 | |
269 | |
248 | SvGETMAGIC (sv); |
270 | SvGETMAGIC (sv); |
249 | svt = SvTYPE (sv); |
271 | svt = SvTYPE (sv); |
250 | |
272 | |
251 | if (expect_false (SvOBJECT (sv))) |
273 | if (ecb_expect_false (SvOBJECT (sv))) |
252 | { |
274 | { |
253 | HV *stash = !CBOR_SLOW || cbor_boolean_stash |
275 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
254 | ? cbor_boolean_stash |
276 | ? types_boolean_stash |
|
|
277 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
278 | HV *error_stash = !CBOR_SLOW || types_error_stash |
|
|
279 | ? types_error_stash |
|
|
280 | : gv_stashpv ("Types::Serialiser::Error", 1); |
|
|
281 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
282 | ? cbor_tagged_stash |
255 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
283 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
256 | |
284 | |
257 | if (SvSTASH (sv) == stash) |
285 | HV *stash = SvSTASH (sv); |
|
|
286 | GV *method; |
|
|
287 | |
|
|
288 | if (stash == boolean_stash) |
258 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
289 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
290 | else if (stash == error_stash) |
|
|
291 | encode_ch (enc, 0xe0 | 23); |
|
|
292 | else if (stash == tagged_stash) |
|
|
293 | { |
|
|
294 | if (svt != SVt_PVAV) |
|
|
295 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
|
|
296 | |
|
|
297 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
|
|
298 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
299 | } |
|
|
300 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
|
|
301 | { |
|
|
302 | dSP; |
|
|
303 | |
|
|
304 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
305 | // we re-bless the reference to get overload and other niceties right |
|
|
306 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
307 | |
|
|
308 | PUTBACK; |
|
|
309 | // G_SCALAR ensures that return value is 1 |
|
|
310 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
311 | SPAGAIN; |
|
|
312 | |
|
|
313 | // catch this surprisingly common error |
|
|
314 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
315 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
316 | |
|
|
317 | encode_sv (enc, POPs); |
|
|
318 | |
|
|
319 | PUTBACK; |
|
|
320 | |
|
|
321 | FREETMPS; LEAVE; |
|
|
322 | } |
|
|
323 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
|
|
324 | { |
|
|
325 | dSP; |
|
|
326 | |
|
|
327 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
328 | EXTEND (SP, 2); |
|
|
329 | // we re-bless the reference to get overload and other niceties right |
|
|
330 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
331 | PUSHs (sv_cbor); |
|
|
332 | |
|
|
333 | PUTBACK; |
|
|
334 | int count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
335 | SPAGAIN; |
|
|
336 | |
|
|
337 | // catch this surprisingly common error |
|
|
338 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
339 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
340 | |
|
|
341 | encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); |
|
|
342 | encode_uint (enc, 0x80, count + 1); |
|
|
343 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
|
|
344 | |
|
|
345 | while (count) |
|
|
346 | encode_sv (enc, SP[1 - count--]); |
|
|
347 | |
|
|
348 | PUTBACK; |
|
|
349 | |
|
|
350 | FREETMPS; LEAVE; |
|
|
351 | } |
259 | else |
352 | else |
260 | { |
|
|
261 | #if 0 //TODO |
|
|
262 | if (enc->cbor.flags & F_CONV_BLESSED) |
|
|
263 | { |
|
|
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; |
|
|
270 | |
|
|
271 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
272 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
273 | |
|
|
274 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
275 | PUTBACK; |
|
|
276 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
|
|
277 | SPAGAIN; |
|
|
278 | |
|
|
279 | // catch this surprisingly common error |
|
|
280 | 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))); |
|
|
282 | |
|
|
283 | sv = POPs; |
|
|
284 | PUTBACK; |
|
|
285 | |
|
|
286 | encode_sv (enc, sv); |
|
|
287 | |
|
|
288 | 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", |
353 | croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it", |
294 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
295 | } |
|
|
296 | else if (enc->cbor.flags & F_ALLOW_BLESSED) |
|
|
297 | encode_str (enc, "null", 4, 0); |
|
|
298 | else |
|
|
299 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
300 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
354 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
301 | #endif |
|
|
302 | } |
|
|
303 | } |
355 | } |
304 | else if (svt == SVt_PVHV) |
356 | else if (svt == SVt_PVHV) |
305 | encode_hv (enc, (HV *)sv); |
357 | encode_hv (enc, (HV *)sv); |
306 | else if (svt == SVt_PVAV) |
358 | else if (svt == SVt_PVAV) |
307 | encode_av (enc, (AV *)sv); |
359 | encode_av (enc, (AV *)sv); |
… | |
… | |
332 | { |
384 | { |
333 | double nv = SvNVX (sv); |
385 | double nv = SvNVX (sv); |
334 | |
386 | |
335 | need (enc, 9); |
387 | need (enc, 9); |
336 | |
388 | |
337 | if (expect_false (nv == (U32)nv)) |
389 | if (ecb_expect_false (nv == (U32)nv)) |
338 | encode_uint (enc, 0x00, (U32)nv); |
390 | encode_uint (enc, 0x00, (U32)nv); |
339 | //TODO: maybe I32? |
391 | //TODO: maybe I32? |
340 | else if (expect_false (nv == (float)nv)) |
392 | else if (ecb_expect_false (nv == (float)nv)) |
341 | { |
393 | { |
342 | uint32_t fp = ecb_float_to_binary32 (nv); |
394 | uint32_t fp = ecb_float_to_binary32 (nv); |
343 | |
395 | |
344 | *enc->cur++ = 0xe0 | 26; |
396 | *enc->cur++ = 0xe0 | 26; |
345 | |
397 | |
… | |
… | |
433 | U32 maxdepth; // recursion depth limit |
485 | U32 maxdepth; // recursion depth limit |
434 | } dec_t; |
486 | } dec_t; |
435 | |
487 | |
436 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
488 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
437 | |
489 | |
438 | #define WANT(len) if (expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data"); |
490 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
439 | |
491 | |
440 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
492 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
441 | #define DEC_DEC_DEPTH --dec->depth |
493 | #define DEC_DEC_DEPTH --dec->depth |
442 | |
494 | |
443 | static UV |
495 | static UV |
… | |
… | |
504 | |
556 | |
505 | for (;;) |
557 | for (;;) |
506 | { |
558 | { |
507 | WANT (1); |
559 | WANT (1); |
508 | |
560 | |
509 | if (*dec->cur == 0xe0 | 31) |
561 | if (*dec->cur == (0xe0 | 31)) |
510 | { |
562 | { |
511 | ++dec->cur; |
563 | ++dec->cur; |
512 | break; |
564 | break; |
513 | } |
565 | } |
514 | |
566 | |
… | |
… | |
532 | SvREFCNT_dec (av); |
584 | SvREFCNT_dec (av); |
533 | DEC_DEC_DEPTH; |
585 | DEC_DEC_DEPTH; |
534 | return &PL_sv_undef; |
586 | return &PL_sv_undef; |
535 | } |
587 | } |
536 | |
588 | |
|
|
589 | static void |
|
|
590 | decode_he (dec_t *dec, HV *hv) |
|
|
591 | { |
|
|
592 | // for speed reasons, we specialcase single-string |
|
|
593 | // byte or utf-8 strings as keys. |
|
|
594 | |
|
|
595 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
|
|
596 | { |
|
|
597 | I32 len = decode_uint (dec); |
|
|
598 | char *key = (char *)dec->cur; |
|
|
599 | |
|
|
600 | dec->cur += len; |
|
|
601 | |
|
|
602 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
603 | } |
|
|
604 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
|
|
605 | { |
|
|
606 | I32 len = decode_uint (dec); |
|
|
607 | char *key = (char *)dec->cur; |
|
|
608 | |
|
|
609 | dec->cur += len; |
|
|
610 | |
|
|
611 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
612 | } |
|
|
613 | else |
|
|
614 | { |
|
|
615 | SV *k = decode_sv (dec); |
|
|
616 | SV *v = decode_sv (dec); |
|
|
617 | |
|
|
618 | hv_store_ent (hv, k, v, 0); |
|
|
619 | SvREFCNT_dec (k); |
|
|
620 | } |
|
|
621 | } |
|
|
622 | |
537 | static SV * |
623 | static SV * |
538 | decode_hv (dec_t *dec) |
624 | decode_hv (dec_t *dec) |
539 | { |
625 | { |
540 | HV *hv = newHV (); |
626 | HV *hv = newHV (); |
541 | |
627 | |
… | |
… | |
547 | |
633 | |
548 | for (;;) |
634 | for (;;) |
549 | { |
635 | { |
550 | WANT (1); |
636 | WANT (1); |
551 | |
637 | |
552 | if (*dec->cur == 0xe0 | 31) |
638 | if (*dec->cur == (0xe0 | 31)) |
553 | { |
639 | { |
554 | ++dec->cur; |
640 | ++dec->cur; |
555 | break; |
641 | break; |
556 | } |
642 | } |
557 | |
643 | |
558 | SV *k = decode_sv (dec); |
644 | decode_he (dec, hv); |
559 | SV *v = decode_sv (dec); |
|
|
560 | |
|
|
561 | hv_store_ent (hv, k, v, 0); |
|
|
562 | } |
645 | } |
563 | } |
646 | } |
564 | else |
647 | else |
565 | { |
648 | { |
566 | int len = decode_uint (dec); |
649 | int pairs = decode_uint (dec); |
567 | |
650 | |
568 | while (len--) |
651 | while (pairs--) |
569 | { |
652 | decode_he (dec, hv); |
570 | SV *k = decode_sv (dec); |
|
|
571 | SV *v = decode_sv (dec); |
|
|
572 | |
|
|
573 | hv_store_ent (hv, k, v, 0); |
|
|
574 | } |
|
|
575 | } |
653 | } |
576 | |
654 | |
577 | DEC_DEC_DEPTH; |
655 | DEC_DEC_DEPTH; |
578 | return newRV_noinc ((SV *)hv); |
656 | return newRV_noinc ((SV *)hv); |
579 | |
|
|
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 | |
657 | |
735 | fail: |
658 | fail: |
736 | SvREFCNT_dec (hv); |
659 | SvREFCNT_dec (hv); |
737 | DEC_DEC_DEPTH; |
660 | DEC_DEC_DEPTH; |
738 | return &PL_sv_undef; |
661 | return &PL_sv_undef; |
739 | } |
662 | } |
740 | |
663 | |
741 | static SV * |
664 | static SV * |
742 | decode_str (dec_t *dec, int utf8) |
665 | decode_str (dec_t *dec, int utf8) |
743 | { |
666 | { |
744 | SV *sv; |
667 | SV *sv = 0; |
745 | |
668 | |
746 | if ((*dec->cur & 31) == 31) |
669 | if ((*dec->cur & 31) == 31) |
747 | { |
670 | { |
748 | ++dec->cur; |
671 | ++dec->cur; |
749 | |
672 | |
… | |
… | |
752 | // not very fast, and certainly not robust against illegal input |
675 | // not very fast, and certainly not robust against illegal input |
753 | for (;;) |
676 | for (;;) |
754 | { |
677 | { |
755 | WANT (1); |
678 | WANT (1); |
756 | |
679 | |
757 | if (*dec->cur == 0xe0 | 31) |
680 | if (*dec->cur == (0xe0 | 31)) |
758 | { |
681 | { |
759 | ++dec->cur; |
682 | ++dec->cur; |
760 | break; |
683 | break; |
761 | } |
684 | } |
762 | |
685 | |
763 | SV *sv2 = decode_sv (dec); |
|
|
764 | sv_catsv (sv, sv2); |
686 | sv_catsv (sv, decode_sv (dec)); |
765 | } |
687 | } |
766 | } |
688 | } |
767 | else |
689 | else |
768 | { |
690 | { |
769 | STRLEN len = decode_uint (dec); |
691 | STRLEN len = decode_uint (dec); |
… | |
… | |
777 | SvUTF8_on (sv); |
699 | SvUTF8_on (sv); |
778 | |
700 | |
779 | return sv; |
701 | return sv; |
780 | |
702 | |
781 | fail: |
703 | fail: |
|
|
704 | SvREFCNT_dec (sv); |
|
|
705 | return &PL_sv_undef; |
|
|
706 | } |
|
|
707 | |
|
|
708 | static SV * |
|
|
709 | decode_tagged (dec_t *dec) |
|
|
710 | { |
|
|
711 | UV tag = decode_uint (dec); |
|
|
712 | SV *sv = decode_sv (dec); |
|
|
713 | |
|
|
714 | if (tag == CBOR_TAG_MAGIC) |
|
|
715 | return sv; |
|
|
716 | else if (tag == CBOR_TAG_PERL_OBJECT) |
|
|
717 | { |
|
|
718 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
719 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
720 | |
|
|
721 | AV *av = (AV *)SvRV (sv); |
|
|
722 | int len = av_len (av) + 1; |
|
|
723 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
|
|
724 | |
|
|
725 | if (!stash) |
|
|
726 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
727 | |
|
|
728 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
729 | |
|
|
730 | if (!method) |
|
|
731 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
732 | |
|
|
733 | dSP; |
|
|
734 | |
|
|
735 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
736 | EXTEND (SP, len + 1); |
|
|
737 | // we re-bless the reference to get overload and other niceties right |
|
|
738 | PUSHs (*av_fetch (av, 0, 1)); |
|
|
739 | PUSHs (sv_cbor); |
|
|
740 | |
|
|
741 | int i; |
|
|
742 | |
|
|
743 | for (i = 1; i < len; ++i) |
|
|
744 | PUSHs (*av_fetch (av, i, 1)); |
|
|
745 | |
|
|
746 | PUTBACK; |
|
|
747 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
|
|
748 | SPAGAIN; |
|
|
749 | |
|
|
750 | if (SvTRUE (ERRSV)) |
|
|
751 | { |
|
|
752 | FREETMPS; LEAVE; |
|
|
753 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
754 | } |
|
|
755 | |
|
|
756 | SvREFCNT_dec (sv); |
|
|
757 | sv = SvREFCNT_inc (POPs); |
|
|
758 | |
|
|
759 | PUTBACK; |
|
|
760 | |
|
|
761 | FREETMPS; LEAVE; |
|
|
762 | |
|
|
763 | return sv; |
|
|
764 | } |
|
|
765 | else |
|
|
766 | { |
|
|
767 | AV *av = newAV (); |
|
|
768 | av_push (av, newSVuv (tag)); |
|
|
769 | av_push (av, sv); |
|
|
770 | |
|
|
771 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
772 | ? cbor_tagged_stash |
|
|
773 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
|
|
774 | |
|
|
775 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
776 | } |
|
|
777 | |
|
|
778 | fail: |
|
|
779 | SvREFCNT_dec (sv); |
782 | return &PL_sv_undef; |
780 | return &PL_sv_undef; |
783 | } |
781 | } |
784 | |
782 | |
785 | static SV * |
783 | static SV * |
786 | decode_sv (dec_t *dec) |
784 | decode_sv (dec_t *dec) |
… | |
… | |
788 | WANT (1); |
786 | WANT (1); |
789 | |
787 | |
790 | switch (*dec->cur >> 5) |
788 | switch (*dec->cur >> 5) |
791 | { |
789 | { |
792 | case 0: // unsigned int |
790 | case 0: // unsigned int |
793 | //TODO: 64 bit values on 3 2bit perls |
|
|
794 | return newSVuv (decode_uint (dec)); |
791 | return newSVuv (decode_uint (dec)); |
795 | case 1: // negative int |
792 | case 1: // negative int |
796 | return newSViv (-1 - (IV)decode_uint (dec)); |
793 | return newSViv (-1 - (IV)decode_uint (dec)); |
797 | case 2: // octet string |
794 | case 2: // octet string |
798 | return decode_str (dec, 0); |
795 | return decode_str (dec, 0); |
… | |
… | |
801 | case 4: // array |
798 | case 4: // array |
802 | return decode_av (dec); |
799 | return decode_av (dec); |
803 | case 5: // map |
800 | case 5: // map |
804 | return decode_hv (dec); |
801 | return decode_hv (dec); |
805 | case 6: // tag |
802 | case 6: // tag |
806 | abort (); |
803 | return decode_tagged (dec); |
807 | break; |
|
|
808 | case 7: // misc |
804 | case 7: // misc |
809 | switch (*dec->cur++ & 31) |
805 | switch (*dec->cur++ & 31) |
810 | { |
806 | { |
811 | case 20: |
807 | case 20: |
812 | #if CBOR_SLOW |
808 | #if CBOR_SLOW |
813 | cbor_false = get_bool ("CBOR::XS::false"); |
809 | types_false = get_bool ("Types::Serialiser::false"); |
814 | #endif |
810 | #endif |
815 | return newSVsv (cbor_false); |
811 | return newSVsv (types_false); |
816 | case 21: |
812 | case 21: |
817 | #if CBOR_SLOW |
813 | #if CBOR_SLOW |
818 | cbor_true = get_bool ("CBOR::XS::true"); |
814 | types_true = get_bool ("Types::Serialiser::true"); |
819 | #endif |
815 | #endif |
820 | return newSVsv (cbor_true); |
816 | return newSVsv (types_true); |
821 | case 22: |
817 | case 22: |
822 | return newSVsv (&PL_sv_undef); |
818 | return newSVsv (&PL_sv_undef); |
|
|
819 | case 23: |
|
|
820 | #if CBOR_SLOW |
|
|
821 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
822 | #endif |
|
|
823 | return newSVsv (types_error); |
823 | |
824 | |
824 | case 25: |
825 | case 25: |
825 | // half float |
|
|
826 | abort (); |
|
|
827 | break; |
826 | { |
|
|
827 | WANT (2); |
|
|
828 | |
|
|
829 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
|
|
830 | dec->cur += 2; |
|
|
831 | |
|
|
832 | return newSVnv (ecb_binary16_to_float (fp)); |
|
|
833 | } |
828 | |
834 | |
829 | case 26: |
835 | case 26: |
830 | { |
836 | { |
831 | uint32_t fp; |
837 | uint32_t fp; |
832 | WANT (4); |
838 | WANT (4); |
… | |
… | |
858 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
864 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
859 | } |
865 | } |
860 | |
866 | |
861 | break; |
867 | break; |
862 | } |
868 | } |
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 | |
869 | |
920 | fail: |
870 | fail: |
921 | return &PL_sv_undef; |
871 | return &PL_sv_undef; |
922 | } |
872 | } |
923 | |
873 | |
924 | static SV * |
874 | static SV * |
925 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
875 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
926 | { |
876 | { |
927 | dec_t dec; |
877 | dec_t dec; |
928 | SV *sv; |
878 | SV *sv; |
|
|
879 | STRLEN len; |
|
|
880 | char *data = SvPVbyte (string, len); |
929 | |
881 | |
930 | /* work around bugs in 5.10 where manipulating magic values |
|
|
931 | * makes perl ignore the magic in subsequent accesses. |
|
|
932 | * also make a copy of non-PV values, to get them into a clean |
|
|
933 | * state (SvPV should do that, but it's buggy, see below). |
|
|
934 | */ |
|
|
935 | /*SvGETMAGIC (string);*/ |
|
|
936 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
937 | string = sv_2mortal (newSVsv (string)); |
|
|
938 | |
|
|
939 | SvUPGRADE (string, SVt_PV); |
|
|
940 | |
|
|
941 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
942 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
943 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
944 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
945 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
946 | * and hope for the best. |
|
|
947 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
948 | * assertion business is seriously broken, try yet another workaround |
|
|
949 | * for the broken -DDEBUGGING. |
|
|
950 | */ |
|
|
951 | { |
|
|
952 | #ifdef DEBUGGING |
|
|
953 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
954 | #else |
|
|
955 | STRLEN offset = SvCUR (string); |
|
|
956 | #endif |
|
|
957 | |
|
|
958 | if (offset > cbor->max_size && cbor->max_size) |
882 | if (len > cbor->max_size && cbor->max_size) |
959 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
883 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
960 | (unsigned long)SvCUR (string), (unsigned long)cbor->max_size); |
884 | (unsigned long)len, (unsigned long)cbor->max_size); |
961 | } |
|
|
962 | |
|
|
963 | sv_utf8_downgrade (string, 0); |
|
|
964 | |
885 | |
965 | dec.cbor = *cbor; |
886 | dec.cbor = *cbor; |
966 | dec.cur = (U8 *)SvPVX (string); |
887 | dec.cur = (U8 *)data; |
967 | dec.end = (U8 *)SvEND (string); |
888 | dec.end = (U8 *)data + len; |
968 | dec.err = 0; |
889 | dec.err = 0; |
969 | dec.depth = 0; |
890 | dec.depth = 0; |
970 | |
891 | |
971 | if (dec.cbor.cb_object || dec.cbor.cb_sk_object) |
|
|
972 | ;//TODO dec.cbor.flags |= F_HOOK; |
|
|
973 | |
|
|
974 | sv = decode_sv (&dec); |
892 | sv = decode_sv (&dec); |
975 | |
893 | |
976 | if (offset_return) |
894 | if (offset_return) |
977 | *offset_return = dec.cur; |
895 | *offset_return = dec.cur; |
978 | |
896 | |
979 | if (!(offset_return || !sv)) |
897 | if (!(offset_return || !sv)) |
980 | { |
|
|
981 | if (*dec.cur && !dec.err) |
898 | if (dec.cur != dec.end && !dec.err) |
982 | { |
|
|
983 | dec.err = "garbage after CBOR object"; |
899 | dec.err = "garbage after CBOR object"; |
|
|
900 | |
|
|
901 | if (dec.err) |
|
|
902 | { |
984 | SvREFCNT_dec (sv); |
903 | 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); |
904 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
|
|
905 | } |
991 | |
906 | |
992 | sv = sv_2mortal (sv); |
907 | sv = sv_2mortal (sv); |
993 | |
908 | |
994 | return sv; |
909 | return sv; |
995 | } |
910 | } |
… | |
… | |
1000 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
915 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1001 | |
916 | |
1002 | BOOT: |
917 | BOOT: |
1003 | { |
918 | { |
1004 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
919 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
1005 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
920 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
1006 | |
921 | |
1007 | cbor_true = get_bool ("CBOR::XS::true"); |
922 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1008 | cbor_false = get_bool ("CBOR::XS::false"); |
923 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
|
|
924 | |
|
|
925 | types_true = get_bool ("Types::Serialiser::true" ); |
|
|
926 | types_false = get_bool ("Types::Serialiser::false"); |
|
|
927 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
928 | |
|
|
929 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
930 | SvREADONLY_on (sv_cbor); |
1009 | } |
931 | } |
1010 | |
932 | |
1011 | PROTOTYPES: DISABLE |
933 | PROTOTYPES: DISABLE |
1012 | |
934 | |
1013 | void CLONE (...) |
935 | void CLONE (...) |
1014 | CODE: |
936 | CODE: |
1015 | cbor_stash = 0; |
937 | cbor_stash = 0; |
|
|
938 | cbor_tagged_stash = 0; |
|
|
939 | types_error_stash = 0; |
1016 | cbor_boolean_stash = 0; |
940 | types_boolean_stash = 0; |
1017 | |
941 | |
1018 | void new (char *klass) |
942 | void new (char *klass) |
1019 | PPCODE: |
943 | PPCODE: |
1020 | { |
944 | { |
1021 | SV *pv = NEWSV (0, sizeof (CBOR)); |
945 | SV *pv = NEWSV (0, sizeof (CBOR)); |
… | |
… | |
1068 | CODE: |
992 | CODE: |
1069 | RETVAL = self->max_size; |
993 | RETVAL = self->max_size; |
1070 | OUTPUT: |
994 | OUTPUT: |
1071 | RETVAL |
995 | RETVAL |
1072 | |
996 | |
1073 | #if 0 //TODO |
|
|
1074 | |
|
|
1075 | void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
|
|
1076 | PPCODE: |
|
|
1077 | { |
|
|
1078 | SvREFCNT_dec (self->cb_object); |
|
|
1079 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1080 | |
|
|
1081 | XPUSHs (ST (0)); |
|
|
1082 | } |
|
|
1083 | |
|
|
1084 | void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1085 | PPCODE: |
|
|
1086 | { |
|
|
1087 | if (!self->cb_sk_object) |
|
|
1088 | self->cb_sk_object = newHV (); |
|
|
1089 | |
|
|
1090 | if (SvOK (cb)) |
|
|
1091 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1092 | else |
|
|
1093 | { |
|
|
1094 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1095 | |
|
|
1096 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1097 | { |
|
|
1098 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1099 | self->cb_sk_object = 0; |
|
|
1100 | } |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | XPUSHs (ST (0)); |
|
|
1104 | } |
|
|
1105 | |
|
|
1106 | #endif |
|
|
1107 | |
|
|
1108 | void encode (CBOR *self, SV *scalar) |
997 | void encode (CBOR *self, SV *scalar) |
1109 | PPCODE: |
998 | PPCODE: |
1110 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
999 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1111 | XPUSHs (scalar); |
1000 | XPUSHs (scalar); |
1112 | |
1001 | |
… | |
… | |
1124 | EXTEND (SP, 2); |
1013 | EXTEND (SP, 2); |
1125 | PUSHs (sv); |
1014 | PUSHs (sv); |
1126 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1015 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1127 | } |
1016 | } |
1128 | |
1017 | |
1129 | void DESTROY (CBOR *self) |
|
|
1130 | CODE: |
|
|
1131 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1132 | SvREFCNT_dec (self->cb_object); |
|
|
1133 | |
|
|
1134 | PROTOTYPES: ENABLE |
1018 | PROTOTYPES: ENABLE |
1135 | |
1019 | |
1136 | void encode_cbor (SV *scalar) |
1020 | void encode_cbor (SV *scalar) |
1137 | PPCODE: |
1021 | PPCODE: |
1138 | { |
1022 | { |