… | |
… | |
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 | // compatibility with perl <5.18 |
|
|
15 | #ifndef HvNAMELEN_get |
|
|
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
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 | |
14 | // known tags |
25 | // known tags |
15 | enum cbor_tag |
26 | enum cbor_tag |
16 | { |
27 | { |
17 | // inofficial extensions (pending iana registration) |
28 | // inofficial extensions (pending iana registration) |
18 | CBOR_TAG_PERL_OBJECT = 256, |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
19 | CBOR_TAG_GENERIC_OBJECT = 257, |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
|
|
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
|
|
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
|
|
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
|
|
34 | CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref |
|
|
35 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
20 | |
36 | |
21 | // rfc7049 |
37 | // rfc7049 |
22 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
23 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
24 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
40 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
… | |
… | |
38 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
39 | |
55 | |
40 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
41 | }; |
57 | }; |
42 | |
58 | |
43 | #define F_SHRINK 0x00000200UL |
59 | #define F_SHRINK 0x00000001UL |
44 | #define F_ALLOW_UNKNOWN 0x00002000UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
|
|
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
|
|
62 | #define F_ALLOW_STRINGREF 0x00000008UL //TODO |
45 | |
63 | |
46 | #define INIT_SIZE 32 // initial scalar size to be allocated |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
47 | |
65 | |
48 | #define SB do { |
66 | #define SB do { |
49 | #define SE } while (0) |
67 | #define SE } while (0) |
… | |
… | |
61 | # define CBOR_SLOW 0 |
79 | # define CBOR_SLOW 0 |
62 | # define CBOR_STASH cbor_stash |
80 | # define CBOR_STASH cbor_stash |
63 | #endif |
81 | #endif |
64 | |
82 | |
65 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
83 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
66 | static SV *types_true, *types_false, *types_error; |
84 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
67 | |
85 | |
68 | typedef struct { |
86 | typedef struct { |
69 | U32 flags; |
87 | U32 flags; |
70 | U32 max_depth; |
88 | U32 max_depth; |
71 | STRLEN max_size; |
89 | STRLEN max_size; |
|
|
90 | SV *filter; |
72 | } CBOR; |
91 | } CBOR; |
73 | |
92 | |
74 | ecb_inline void |
93 | ecb_inline void |
75 | cbor_init (CBOR *cbor) |
94 | cbor_init (CBOR *cbor) |
76 | { |
95 | { |
77 | Zero (cbor, 1, CBOR); |
96 | Zero (cbor, 1, CBOR); |
78 | cbor->max_depth = 512; |
97 | cbor->max_depth = 512; |
|
|
98 | } |
|
|
99 | |
|
|
100 | ecb_inline void |
|
|
101 | cbor_free (CBOR *cbor) |
|
|
102 | { |
|
|
103 | SvREFCNT_dec (cbor->filter); |
79 | } |
104 | } |
80 | |
105 | |
81 | ///////////////////////////////////////////////////////////////////////////// |
106 | ///////////////////////////////////////////////////////////////////////////// |
82 | // utility functions |
107 | // utility functions |
83 | |
108 | |
… | |
… | |
105 | SvPV_renew (sv, SvCUR (sv) + 1); |
130 | SvPV_renew (sv, SvCUR (sv) + 1); |
106 | #endif |
131 | #endif |
107 | } |
132 | } |
108 | } |
133 | } |
109 | |
134 | |
110 | ///////////////////////////////////////////////////////////////////////////// |
135 | // minimum length of a string to be registered for stringref |
111 | // fp hell |
136 | ecb_inline int |
112 | |
137 | minimum_string_length (UV idx) |
113 | //TODO |
138 | { |
|
|
139 | return idx > 23 |
|
|
140 | ? idx > 0xffU |
|
|
141 | ? idx > 0xffffU |
|
|
142 | ? idx > 0xffffffffU |
|
|
143 | ? 7 |
|
|
144 | : 6 |
|
|
145 | : 5 |
|
|
146 | : 4 |
|
|
147 | : 3; |
|
|
148 | } |
114 | |
149 | |
115 | ///////////////////////////////////////////////////////////////////////////// |
150 | ///////////////////////////////////////////////////////////////////////////// |
116 | // encoder |
151 | // encoder |
117 | |
152 | |
118 | // structure used for encoding CBOR |
153 | // structure used for encoding CBOR |
… | |
… | |
121 | char *cur; // SvPVX (sv) + current output position |
156 | char *cur; // SvPVX (sv) + current output position |
122 | char *end; // SvEND (sv) |
157 | char *end; // SvEND (sv) |
123 | SV *sv; // result scalar |
158 | SV *sv; // result scalar |
124 | CBOR cbor; |
159 | CBOR cbor; |
125 | U32 depth; // recursion level |
160 | U32 depth; // recursion level |
|
|
161 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
162 | UV stringref_idx; |
|
|
163 | HV *shareable; // ptr => index, or 0 |
|
|
164 | UV shareable_idx; |
126 | } enc_t; |
165 | } enc_t; |
127 | |
166 | |
128 | ecb_inline void |
167 | ecb_inline void |
129 | need (enc_t *enc, STRLEN len) |
168 | need (enc_t *enc, STRLEN len) |
130 | { |
169 | { |
… | |
… | |
182 | *enc->cur++ = len >> 8; |
221 | *enc->cur++ = len >> 8; |
183 | *enc->cur++ = len; |
222 | *enc->cur++ = len; |
184 | } |
223 | } |
185 | } |
224 | } |
186 | |
225 | |
|
|
226 | ecb_inline void |
|
|
227 | encode_tag (enc_t *enc, UV tag) |
|
|
228 | { |
|
|
229 | encode_uint (enc, 0xc0, tag); |
|
|
230 | } |
|
|
231 | |
187 | static void |
232 | static void |
188 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
233 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
189 | { |
234 | { |
|
|
235 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
|
|
236 | { |
|
|
237 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
|
|
238 | |
|
|
239 | if (SvOK (*svp)) |
|
|
240 | { |
|
|
241 | // already registered, use stringref |
|
|
242 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
243 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
244 | return; |
|
|
245 | } |
|
|
246 | else if (len >= minimum_string_length (enc->stringref_idx)) |
|
|
247 | { |
|
|
248 | // register only |
|
|
249 | sv_setuv (*svp, enc->stringref_idx); |
|
|
250 | ++enc->stringref_idx; |
|
|
251 | } |
|
|
252 | } |
|
|
253 | |
190 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
254 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
191 | need (enc, len); |
255 | need (enc, len); |
192 | memcpy (enc->cur, str, len); |
256 | memcpy (enc->cur, str, len); |
193 | enc->cur += len; |
257 | enc->cur += len; |
194 | } |
258 | } |
… | |
… | |
252 | |
316 | |
253 | // encode objects, arrays and special \0=false and \1=true values. |
317 | // encode objects, arrays and special \0=false and \1=true values. |
254 | static void |
318 | static void |
255 | encode_rv (enc_t *enc, SV *sv) |
319 | encode_rv (enc_t *enc, SV *sv) |
256 | { |
320 | { |
257 | svtype svt; |
|
|
258 | |
|
|
259 | SvGETMAGIC (sv); |
321 | SvGETMAGIC (sv); |
|
|
322 | |
|
|
323 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
324 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
325 | { |
|
|
326 | if (!enc->shareable) |
|
|
327 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
328 | |
|
|
329 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
330 | |
|
|
331 | if (SvOK (*svp)) |
|
|
332 | { |
|
|
333 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
334 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
335 | return; |
|
|
336 | } |
|
|
337 | else |
|
|
338 | { |
|
|
339 | sv_setuv (*svp, enc->shareable_idx); |
|
|
340 | ++enc->shareable_idx; |
|
|
341 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
342 | } |
|
|
343 | } |
|
|
344 | |
260 | svt = SvTYPE (sv); |
345 | svtype svt = SvTYPE (sv); |
261 | |
346 | |
262 | if (ecb_expect_false (SvOBJECT (sv))) |
347 | if (ecb_expect_false (SvOBJECT (sv))) |
263 | { |
348 | { |
264 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
349 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
265 | ? types_boolean_stash |
350 | ? types_boolean_stash |
… | |
… | |
269 | : gv_stashpv ("Types::Serialiser::Error", 1); |
354 | : gv_stashpv ("Types::Serialiser::Error", 1); |
270 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
355 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
271 | ? cbor_tagged_stash |
356 | ? cbor_tagged_stash |
272 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
357 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
273 | |
358 | |
|
|
359 | HV *stash = SvSTASH (sv); |
|
|
360 | GV *method; |
|
|
361 | |
274 | if (SvSTASH (sv) == boolean_stash) |
362 | if (stash == boolean_stash) |
275 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
363 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
276 | else if (SvSTASH (sv) == error_stash) |
364 | else if (stash == error_stash) |
277 | encode_ch (enc, 0xe0 | 23); |
365 | encode_ch (enc, 0xe0 | 23); |
278 | else if (SvSTASH (sv) == tagged_stash) |
366 | else if (stash == tagged_stash) |
279 | { |
367 | { |
280 | if (svt != SVt_PVAV) |
368 | if (svt != SVt_PVAV) |
281 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
369 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
282 | |
370 | |
283 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
371 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
284 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
372 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
285 | } |
373 | } |
|
|
374 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
|
|
375 | { |
|
|
376 | dSP; |
|
|
377 | |
|
|
378 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
379 | // we re-bless the reference to get overload and other niceties right |
|
|
380 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
381 | |
|
|
382 | PUTBACK; |
|
|
383 | // G_SCALAR ensures that return value is 1 |
|
|
384 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
385 | SPAGAIN; |
|
|
386 | |
|
|
387 | // catch this surprisingly common error |
|
|
388 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
389 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
390 | |
|
|
391 | encode_sv (enc, POPs); |
|
|
392 | |
|
|
393 | PUTBACK; |
|
|
394 | |
|
|
395 | FREETMPS; LEAVE; |
|
|
396 | } |
|
|
397 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
|
|
398 | { |
|
|
399 | dSP; |
|
|
400 | |
|
|
401 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
402 | EXTEND (SP, 2); |
|
|
403 | // we re-bless the reference to get overload and other niceties right |
|
|
404 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
405 | PUSHs (sv_cbor); |
|
|
406 | |
|
|
407 | PUTBACK; |
|
|
408 | int count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
409 | SPAGAIN; |
|
|
410 | |
|
|
411 | // catch this surprisingly common error |
|
|
412 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
413 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
414 | |
|
|
415 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
|
|
416 | encode_uint (enc, 0x80, count + 1); |
|
|
417 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
|
|
418 | |
|
|
419 | while (count) |
|
|
420 | encode_sv (enc, SP[1 - count--]); |
|
|
421 | |
|
|
422 | PUTBACK; |
|
|
423 | |
|
|
424 | FREETMPS; LEAVE; |
|
|
425 | } |
286 | else |
426 | else |
287 | { |
|
|
288 | // we re-bless the reference to get overload and other niceties right |
|
|
289 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
290 | |
|
|
291 | if (to_cbor) |
|
|
292 | { |
|
|
293 | dSP; |
|
|
294 | |
|
|
295 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
296 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
297 | |
|
|
298 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
299 | PUTBACK; |
|
|
300 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
|
|
301 | SPAGAIN; |
|
|
302 | |
|
|
303 | // catch this surprisingly common error |
|
|
304 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
305 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
306 | |
|
|
307 | sv = POPs; |
|
|
308 | PUTBACK; |
|
|
309 | |
|
|
310 | encode_sv (enc, sv); |
|
|
311 | |
|
|
312 | FREETMPS; LEAVE; |
|
|
313 | } |
|
|
314 | else |
|
|
315 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
427 | croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it", |
316 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
428 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
317 | } |
|
|
318 | } |
429 | } |
319 | else if (svt == SVt_PVHV) |
430 | else if (svt == SVt_PVHV) |
320 | encode_hv (enc, (HV *)sv); |
431 | encode_hv (enc, (HV *)sv); |
321 | else if (svt == SVt_PVAV) |
432 | else if (svt == SVt_PVAV) |
322 | encode_av (enc, (AV *)sv); |
433 | encode_av (enc, (AV *)sv); |
323 | else if (svt < SVt_PVAV) |
|
|
324 | { |
|
|
325 | STRLEN len = 0; |
|
|
326 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
327 | |
|
|
328 | if (len == 1 && *pv == '1') |
|
|
329 | encode_ch (enc, 0xe0 | 21); |
|
|
330 | else if (len == 1 && *pv == '0') |
|
|
331 | encode_ch (enc, 0xe0 | 20); |
|
|
332 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
333 | encode_ch (enc, 0xe0 | 23); |
|
|
334 | else |
|
|
335 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
336 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
337 | } |
|
|
338 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
339 | encode_ch (enc, 0xe0 | 23); |
|
|
340 | else |
434 | else |
341 | croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
435 | { |
342 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
436 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
|
|
437 | encode_sv (enc, sv); |
|
|
438 | } |
343 | } |
439 | } |
344 | |
440 | |
345 | static void |
441 | static void |
346 | encode_nv (enc_t *enc, SV *sv) |
442 | encode_nv (enc_t *enc, SV *sv) |
347 | { |
443 | { |
… | |
… | |
412 | } |
508 | } |
413 | |
509 | |
414 | static SV * |
510 | static SV * |
415 | encode_cbor (SV *scalar, CBOR *cbor) |
511 | encode_cbor (SV *scalar, CBOR *cbor) |
416 | { |
512 | { |
417 | enc_t enc; |
513 | enc_t enc = { }; |
418 | |
514 | |
419 | enc.cbor = *cbor; |
515 | enc.cbor = *cbor; |
420 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
516 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
421 | enc.cur = SvPVX (enc.sv); |
517 | enc.cur = SvPVX (enc.sv); |
422 | enc.end = SvEND (enc.sv); |
518 | enc.end = SvEND (enc.sv); |
423 | enc.depth = 0; |
|
|
424 | |
519 | |
425 | SvPOK_only (enc.sv); |
520 | SvPOK_only (enc.sv); |
|
|
521 | |
|
|
522 | if (cbor->flags & F_ALLOW_STRINGREF) |
|
|
523 | { |
|
|
524 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
525 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
526 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
527 | } |
|
|
528 | |
426 | encode_sv (&enc, scalar); |
529 | encode_sv (&enc, scalar); |
427 | |
530 | |
428 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
531 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
429 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
532 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
430 | |
533 | |
… | |
… | |
444 | U8 *end; // end of input string |
547 | U8 *end; // end of input string |
445 | const char *err; // parse error, if != 0 |
548 | const char *err; // parse error, if != 0 |
446 | CBOR cbor; |
549 | CBOR cbor; |
447 | U32 depth; // recursion depth |
550 | U32 depth; // recursion depth |
448 | U32 maxdepth; // recursion depth limit |
551 | U32 maxdepth; // recursion depth limit |
|
|
552 | AV *shareable; |
|
|
553 | AV *stringref; |
|
|
554 | SV *decode_tagged; |
449 | } dec_t; |
555 | } dec_t; |
450 | |
556 | |
451 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
557 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
452 | |
558 | |
453 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
559 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
547 | SvREFCNT_dec (av); |
653 | SvREFCNT_dec (av); |
548 | DEC_DEC_DEPTH; |
654 | DEC_DEC_DEPTH; |
549 | return &PL_sv_undef; |
655 | return &PL_sv_undef; |
550 | } |
656 | } |
551 | |
657 | |
|
|
658 | static void |
|
|
659 | decode_he (dec_t *dec, HV *hv) |
|
|
660 | { |
|
|
661 | // for speed reasons, we specialcase single-string |
|
|
662 | // byte or utf-8 strings as keys, but only when !stringref |
|
|
663 | |
|
|
664 | if (ecb_expect_true (!dec->stringref)) |
|
|
665 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
|
|
666 | { |
|
|
667 | I32 len = decode_uint (dec); |
|
|
668 | char *key = (char *)dec->cur; |
|
|
669 | |
|
|
670 | dec->cur += len; |
|
|
671 | |
|
|
672 | if (ecb_expect_false (dec->stringref)) |
|
|
673 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
674 | |
|
|
675 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
676 | |
|
|
677 | return; |
|
|
678 | } |
|
|
679 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
|
|
680 | { |
|
|
681 | I32 len = decode_uint (dec); |
|
|
682 | char *key = (char *)dec->cur; |
|
|
683 | |
|
|
684 | dec->cur += len; |
|
|
685 | |
|
|
686 | if (ecb_expect_false (dec->stringref)) |
|
|
687 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
|
|
688 | |
|
|
689 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
690 | |
|
|
691 | return; |
|
|
692 | } |
|
|
693 | |
|
|
694 | SV *k = decode_sv (dec); |
|
|
695 | SV *v = decode_sv (dec); |
|
|
696 | |
|
|
697 | hv_store_ent (hv, k, v, 0); |
|
|
698 | SvREFCNT_dec (k); |
|
|
699 | } |
|
|
700 | |
552 | static SV * |
701 | static SV * |
553 | decode_hv (dec_t *dec) |
702 | decode_hv (dec_t *dec) |
554 | { |
703 | { |
555 | HV *hv = newHV (); |
704 | HV *hv = newHV (); |
556 | |
705 | |
… | |
… | |
568 | { |
717 | { |
569 | ++dec->cur; |
718 | ++dec->cur; |
570 | break; |
719 | break; |
571 | } |
720 | } |
572 | |
721 | |
573 | SV *k = decode_sv (dec); |
722 | decode_he (dec, hv); |
574 | SV *v = decode_sv (dec); |
|
|
575 | |
|
|
576 | hv_store_ent (hv, k, v, 0); |
|
|
577 | } |
723 | } |
578 | } |
724 | } |
579 | else |
725 | else |
580 | { |
726 | { |
581 | int len = decode_uint (dec); |
727 | int pairs = decode_uint (dec); |
582 | |
728 | |
583 | while (len--) |
729 | while (pairs--) |
584 | { |
730 | decode_he (dec, hv); |
585 | SV *k = decode_sv (dec); |
|
|
586 | SV *v = decode_sv (dec); |
|
|
587 | |
|
|
588 | hv_store_ent (hv, k, v, 0); |
|
|
589 | } |
|
|
590 | } |
731 | } |
591 | |
732 | |
592 | DEC_DEC_DEPTH; |
733 | DEC_DEC_DEPTH; |
593 | return newRV_noinc ((SV *)hv); |
734 | return newRV_noinc ((SV *)hv); |
594 | |
735 | |
… | |
… | |
628 | STRLEN len = decode_uint (dec); |
769 | STRLEN len = decode_uint (dec); |
629 | |
770 | |
630 | WANT (len); |
771 | WANT (len); |
631 | sv = newSVpvn (dec->cur, len); |
772 | sv = newSVpvn (dec->cur, len); |
632 | dec->cur += len; |
773 | dec->cur += len; |
|
|
774 | |
|
|
775 | if (ecb_expect_false (dec->stringref) |
|
|
776 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
777 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
633 | } |
778 | } |
634 | |
779 | |
635 | if (utf8) |
780 | if (utf8) |
636 | SvUTF8_on (sv); |
781 | SvUTF8_on (sv); |
637 | |
782 | |
… | |
… | |
643 | } |
788 | } |
644 | |
789 | |
645 | static SV * |
790 | static SV * |
646 | decode_tagged (dec_t *dec) |
791 | decode_tagged (dec_t *dec) |
647 | { |
792 | { |
|
|
793 | SV *sv = 0; |
648 | UV tag = decode_uint (dec); |
794 | UV tag = decode_uint (dec); |
|
|
795 | |
|
|
796 | WANT (1); |
|
|
797 | |
|
|
798 | switch (tag) |
|
|
799 | { |
|
|
800 | case CBOR_TAG_MAGIC: |
|
|
801 | sv = decode_sv (dec); |
|
|
802 | break; |
|
|
803 | |
|
|
804 | case CBOR_TAG_INDIRECTION: |
|
|
805 | sv = newRV_noinc (decode_sv (dec)); |
|
|
806 | break; |
|
|
807 | |
|
|
808 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
809 | { |
|
|
810 | ENTER; SAVETMPS; |
|
|
811 | |
|
|
812 | SAVESPTR (dec->stringref); |
|
|
813 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
814 | |
|
|
815 | sv = decode_sv (dec); |
|
|
816 | |
|
|
817 | FREETMPS; LEAVE; |
|
|
818 | } |
|
|
819 | break; |
|
|
820 | |
|
|
821 | case CBOR_TAG_STRINGREF: |
|
|
822 | { |
|
|
823 | if ((*dec->cur >> 5) != 0) |
|
|
824 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
825 | |
|
|
826 | UV idx = decode_uint (dec); |
|
|
827 | |
|
|
828 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
829 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
830 | |
|
|
831 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
832 | } |
|
|
833 | break; |
|
|
834 | |
|
|
835 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
836 | { |
|
|
837 | if (ecb_expect_false (!dec->shareable)) |
|
|
838 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
839 | |
|
|
840 | sv = newSV (0); |
|
|
841 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
|
|
842 | |
649 | SV *sv = decode_sv (dec); |
843 | SV *osv = decode_sv (dec); |
|
|
844 | sv_setsv (sv, osv); |
|
|
845 | SvREFCNT_dec_NN (osv); |
|
|
846 | } |
|
|
847 | break; |
650 | |
848 | |
651 | if (tag == CBOR_TAG_MAGIC) |
849 | case CBOR_TAG_VALUE_SHAREDREF: |
652 | return sv; |
850 | { |
|
|
851 | if ((*dec->cur >> 5) != 0) |
|
|
852 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
653 | |
853 | |
654 | if (tag == CBOR_TAG_PERL_OBJECT) |
854 | UV idx = decode_uint (dec); |
655 | { |
855 | |
|
|
856 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
|
|
857 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
|
|
858 | |
|
|
859 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
860 | } |
|
|
861 | break; |
|
|
862 | |
|
|
863 | case CBOR_TAG_PERL_OBJECT: |
|
|
864 | { |
|
|
865 | sv = decode_sv (dec); |
|
|
866 | |
656 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
867 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
657 | ERR ("corrupted CBOR data (non-array perl object)"); |
868 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
869 | |
|
|
870 | AV *av = (AV *)SvRV (sv); |
|
|
871 | int len = av_len (av) + 1; |
|
|
872 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
|
|
873 | |
|
|
874 | if (!stash) |
|
|
875 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
876 | |
|
|
877 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
878 | |
|
|
879 | if (!method) |
|
|
880 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
881 | |
|
|
882 | dSP; |
|
|
883 | |
|
|
884 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
885 | EXTEND (SP, len + 1); |
|
|
886 | // we re-bless the reference to get overload and other niceties right |
|
|
887 | PUSHs (*av_fetch (av, 0, 1)); |
|
|
888 | PUSHs (sv_cbor); |
|
|
889 | |
|
|
890 | int i; |
|
|
891 | |
|
|
892 | for (i = 1; i < len; ++i) |
|
|
893 | PUSHs (*av_fetch (av, i, 1)); |
|
|
894 | |
|
|
895 | PUTBACK; |
|
|
896 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
|
|
897 | SPAGAIN; |
|
|
898 | |
|
|
899 | if (SvTRUE (ERRSV)) |
|
|
900 | { |
|
|
901 | FREETMPS; LEAVE; |
|
|
902 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
903 | } |
|
|
904 | |
|
|
905 | SvREFCNT_dec (sv); |
|
|
906 | sv = SvREFCNT_inc (POPs); |
|
|
907 | |
|
|
908 | PUTBACK; |
|
|
909 | |
|
|
910 | FREETMPS; LEAVE; |
658 | |
911 | } |
659 | // TODO |
912 | break; |
660 | } |
|
|
661 | |
913 | |
|
|
914 | default: |
|
|
915 | { |
|
|
916 | sv = decode_sv (dec); |
|
|
917 | |
|
|
918 | dSP; |
|
|
919 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
920 | EXTEND (SP, 2); |
|
|
921 | PUSHs (newSVuv (tag)); |
|
|
922 | PUSHs (sv); |
|
|
923 | |
|
|
924 | PUTBACK; |
|
|
925 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
926 | SPAGAIN; |
|
|
927 | |
|
|
928 | if (SvTRUE (ERRSV)) |
|
|
929 | { |
|
|
930 | FREETMPS; LEAVE; |
|
|
931 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
932 | } |
|
|
933 | |
|
|
934 | if (count) |
|
|
935 | { |
|
|
936 | SvREFCNT_dec (sv); |
|
|
937 | sv = SvREFCNT_inc (POPs); |
|
|
938 | } |
|
|
939 | else |
|
|
940 | { |
662 | AV *av = newAV (); |
941 | AV *av = newAV (); |
663 | av_push (av, newSVuv (tag)); |
942 | av_push (av, newSVuv (tag)); |
664 | av_push (av, sv); |
943 | av_push (av, sv); |
665 | |
944 | |
666 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
945 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
667 | ? cbor_tagged_stash |
946 | ? cbor_tagged_stash |
668 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
947 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
669 | |
|
|
670 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
948 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
949 | } |
|
|
950 | |
|
|
951 | PUTBACK; |
|
|
952 | |
|
|
953 | FREETMPS; LEAVE; |
|
|
954 | } |
|
|
955 | break; |
|
|
956 | } |
|
|
957 | |
|
|
958 | return sv; |
671 | |
959 | |
672 | fail: |
960 | fail: |
673 | SvREFCNT_dec (sv); |
961 | SvREFCNT_dec (sv); |
674 | return &PL_sv_undef; |
962 | return &PL_sv_undef; |
675 | } |
963 | } |
… | |
… | |
766 | } |
1054 | } |
767 | |
1055 | |
768 | static SV * |
1056 | static SV * |
769 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1057 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
770 | { |
1058 | { |
771 | dec_t dec; |
1059 | dec_t dec = { }; |
772 | SV *sv; |
1060 | SV *sv; |
|
|
1061 | STRLEN len; |
|
|
1062 | char *data = SvPVbyte (string, len); |
773 | |
1063 | |
774 | /* work around bugs in 5.10 where manipulating magic values |
|
|
775 | * makes perl ignore the magic in subsequent accesses. |
|
|
776 | * also make a copy of non-PV values, to get them into a clean |
|
|
777 | * state (SvPV should do that, but it's buggy, see below). |
|
|
778 | */ |
|
|
779 | /*SvGETMAGIC (string);*/ |
|
|
780 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
781 | string = sv_2mortal (newSVsv (string)); |
|
|
782 | |
|
|
783 | SvUPGRADE (string, SVt_PV); |
|
|
784 | |
|
|
785 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
786 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
787 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
788 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
789 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
790 | * and hope for the best. |
|
|
791 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
792 | * assertion business is seriously broken, try yet another workaround |
|
|
793 | * for the broken -DDEBUGGING. |
|
|
794 | */ |
|
|
795 | { |
|
|
796 | #ifdef DEBUGGING |
|
|
797 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
798 | #else |
|
|
799 | STRLEN offset = SvCUR (string); |
|
|
800 | #endif |
|
|
801 | |
|
|
802 | if (offset > cbor->max_size && cbor->max_size) |
1064 | if (len > cbor->max_size && cbor->max_size) |
803 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
1065 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
804 | (unsigned long)SvCUR (string), (unsigned long)cbor->max_size); |
1066 | (unsigned long)len, (unsigned long)cbor->max_size); |
805 | } |
|
|
806 | |
|
|
807 | sv_utf8_downgrade (string, 0); |
|
|
808 | |
1067 | |
809 | dec.cbor = *cbor; |
1068 | dec.cbor = *cbor; |
810 | dec.cur = (U8 *)SvPVX (string); |
1069 | dec.cur = (U8 *)data; |
811 | dec.end = (U8 *)SvEND (string); |
1070 | dec.end = (U8 *)data + len; |
812 | dec.err = 0; |
|
|
813 | dec.depth = 0; |
|
|
814 | |
1071 | |
815 | sv = decode_sv (&dec); |
1072 | sv = decode_sv (&dec); |
816 | |
1073 | |
817 | if (offset_return) |
1074 | if (offset_return) |
818 | *offset_return = dec.cur; |
1075 | *offset_return = dec.cur; |
… | |
… | |
822 | dec.err = "garbage after CBOR object"; |
1079 | dec.err = "garbage after CBOR object"; |
823 | |
1080 | |
824 | if (dec.err) |
1081 | if (dec.err) |
825 | { |
1082 | { |
826 | SvREFCNT_dec (sv); |
1083 | SvREFCNT_dec (sv); |
827 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
1084 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
828 | } |
1085 | } |
829 | |
1086 | |
830 | sv = sv_2mortal (sv); |
1087 | sv = sv_2mortal (sv); |
831 | |
1088 | |
832 | return sv; |
1089 | return sv; |
… | |
… | |
846 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1103 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
847 | |
1104 | |
848 | types_true = get_bool ("Types::Serialiser::true" ); |
1105 | types_true = get_bool ("Types::Serialiser::true" ); |
849 | types_false = get_bool ("Types::Serialiser::false"); |
1106 | types_false = get_bool ("Types::Serialiser::false"); |
850 | types_error = get_bool ("Types::Serialiser::error"); |
1107 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
1108 | |
|
|
1109 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1110 | |
|
|
1111 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
1112 | SvREADONLY_on (sv_cbor); |
851 | } |
1113 | } |
852 | |
1114 | |
853 | PROTOTYPES: DISABLE |
1115 | PROTOTYPES: DISABLE |
854 | |
1116 | |
855 | void CLONE (...) |
1117 | void CLONE (...) |
… | |
… | |
873 | |
1135 | |
874 | void shrink (CBOR *self, int enable = 1) |
1136 | void shrink (CBOR *self, int enable = 1) |
875 | ALIAS: |
1137 | ALIAS: |
876 | shrink = F_SHRINK |
1138 | shrink = F_SHRINK |
877 | allow_unknown = F_ALLOW_UNKNOWN |
1139 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1140 | allow_sharing = F_ALLOW_SHARING |
|
|
1141 | allow_stringref = F_ALLOW_STRINGREF |
878 | PPCODE: |
1142 | PPCODE: |
879 | { |
1143 | { |
880 | if (enable) |
1144 | if (enable) |
881 | self->flags |= ix; |
1145 | self->flags |= ix; |
882 | else |
1146 | else |
… | |
… | |
887 | |
1151 | |
888 | void get_shrink (CBOR *self) |
1152 | void get_shrink (CBOR *self) |
889 | ALIAS: |
1153 | ALIAS: |
890 | get_shrink = F_SHRINK |
1154 | get_shrink = F_SHRINK |
891 | get_allow_unknown = F_ALLOW_UNKNOWN |
1155 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1156 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1157 | get_allow_stringref = F_ALLOW_STRINGREF |
892 | PPCODE: |
1158 | PPCODE: |
893 | XPUSHs (boolSV (self->flags & ix)); |
1159 | XPUSHs (boolSV (self->flags & ix)); |
894 | |
1160 | |
895 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1161 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
896 | PPCODE: |
1162 | PPCODE: |
… | |
… | |
912 | CODE: |
1178 | CODE: |
913 | RETVAL = self->max_size; |
1179 | RETVAL = self->max_size; |
914 | OUTPUT: |
1180 | OUTPUT: |
915 | RETVAL |
1181 | RETVAL |
916 | |
1182 | |
917 | #if 0 //TODO |
1183 | void filter (CBOR *self, SV *filter = 0) |
918 | |
|
|
919 | void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
|
|
920 | PPCODE: |
1184 | PPCODE: |
921 | { |
|
|
922 | SvREFCNT_dec (self->cb_object); |
1185 | SvREFCNT_dec (self->filter); |
923 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1186 | self->filter = filter ? newSVsv (filter) : filter; |
924 | |
|
|
925 | XPUSHs (ST (0)); |
1187 | XPUSHs (ST (0)); |
926 | } |
|
|
927 | |
1188 | |
928 | void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
1189 | SV *get_filter (CBOR *self) |
929 | PPCODE: |
1190 | CODE: |
930 | { |
1191 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
931 | if (!self->cb_sk_object) |
1192 | OUTPUT: |
932 | self->cb_sk_object = newHV (); |
1193 | RETVAL |
933 | |
|
|
934 | if (SvOK (cb)) |
|
|
935 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
936 | else |
|
|
937 | { |
|
|
938 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
939 | |
|
|
940 | if (!HvKEYS (self->cb_sk_object)) |
|
|
941 | { |
|
|
942 | SvREFCNT_dec (self->cb_sk_object); |
|
|
943 | self->cb_sk_object = 0; |
|
|
944 | } |
|
|
945 | } |
|
|
946 | |
|
|
947 | XPUSHs (ST (0)); |
|
|
948 | } |
|
|
949 | |
|
|
950 | #endif |
|
|
951 | |
1194 | |
952 | void encode (CBOR *self, SV *scalar) |
1195 | void encode (CBOR *self, SV *scalar) |
953 | PPCODE: |
1196 | PPCODE: |
954 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1197 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
955 | XPUSHs (scalar); |
1198 | XPUSHs (scalar); |
… | |
… | |
968 | EXTEND (SP, 2); |
1211 | EXTEND (SP, 2); |
969 | PUSHs (sv); |
1212 | PUSHs (sv); |
970 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1213 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
971 | } |
1214 | } |
972 | |
1215 | |
973 | #if 0 |
|
|
974 | |
|
|
975 | void DESTROY (CBOR *self) |
1216 | void DESTROY (CBOR *self) |
976 | CODE: |
1217 | PPCODE: |
977 | SvREFCNT_dec (self->cb_sk_object); |
1218 | cbor_free (self); |
978 | SvREFCNT_dec (self->cb_object); |
|
|
979 | |
|
|
980 | #endif |
|
|
981 | |
1219 | |
982 | PROTOTYPES: ENABLE |
1220 | PROTOTYPES: ENABLE |
983 | |
1221 | |
984 | void encode_cbor (SV *scalar) |
1222 | void encode_cbor (SV *scalar) |
985 | PPCODE: |
1223 | PPCODE: |