… | |
… | |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
20 | #endif |
20 | #endif |
21 | #ifndef HvNAMEUTF8 |
21 | #ifndef HvNAMEUTF8 |
22 | # define HvNAMEUTF8(hv) 0 |
22 | # define HvNAMEUTF8(hv) 0 |
23 | #endif |
23 | #endif |
|
|
24 | #ifndef SvREFCNT_dec_NN |
|
|
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
26 | #endif |
24 | |
27 | |
25 | // known tags |
28 | // known tags |
26 | enum cbor_tag |
29 | enum cbor_tag |
27 | { |
30 | { |
28 | // inofficial extensions (pending iana registration) |
31 | // extensions |
|
|
32 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
33 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
34 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
35 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
36 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
37 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // 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 |
38 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
39 | |
37 | // rfc7049 |
40 | // rfc7049 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
41 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
42 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
… | |
… | |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
57 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
55 | |
58 | |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
57 | }; |
60 | }; |
58 | |
61 | |
59 | #define F_SHRINK 0x00000001UL |
62 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
64 | #define F_ALLOW_SHARING 0x00000004UL |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
65 | #define F_PACK_STRINGS 0x00000008UL |
63 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
|
|
64 | |
66 | |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
66 | |
68 | |
67 | #define SB do { |
69 | #define SB do { |
68 | #define SE } while (0) |
70 | #define SE } while (0) |
… | |
… | |
80 | # define CBOR_SLOW 0 |
82 | # define CBOR_SLOW 0 |
81 | # define CBOR_STASH cbor_stash |
83 | # define CBOR_STASH cbor_stash |
82 | #endif |
84 | #endif |
83 | |
85 | |
84 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
86 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
85 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
87 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
86 | |
88 | |
87 | typedef struct { |
89 | typedef struct { |
88 | U32 flags; |
90 | U32 flags; |
89 | U32 max_depth; |
91 | U32 max_depth; |
90 | STRLEN max_size; |
92 | STRLEN max_size; |
|
|
93 | SV *filter; |
91 | } CBOR; |
94 | } CBOR; |
92 | |
95 | |
93 | ecb_inline void |
96 | ecb_inline void |
94 | cbor_init (CBOR *cbor) |
97 | cbor_init (CBOR *cbor) |
95 | { |
98 | { |
96 | Zero (cbor, 1, CBOR); |
99 | Zero (cbor, 1, CBOR); |
97 | cbor->max_depth = 512; |
100 | cbor->max_depth = 512; |
|
|
101 | } |
|
|
102 | |
|
|
103 | ecb_inline void |
|
|
104 | cbor_free (CBOR *cbor) |
|
|
105 | { |
|
|
106 | SvREFCNT_dec (cbor->filter); |
98 | } |
107 | } |
99 | |
108 | |
100 | ///////////////////////////////////////////////////////////////////////////// |
109 | ///////////////////////////////////////////////////////////////////////////// |
101 | // utility functions |
110 | // utility functions |
102 | |
111 | |
… | |
… | |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
133 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
134 | #endif |
126 | } |
135 | } |
127 | } |
136 | } |
128 | |
137 | |
|
|
138 | // minimum length of a string to be registered for stringref |
|
|
139 | ecb_inline int |
|
|
140 | minimum_string_length (UV idx) |
|
|
141 | { |
|
|
142 | return idx > 23 |
|
|
143 | ? idx > 0xffU |
|
|
144 | ? idx > 0xffffU |
|
|
145 | ? idx > 0xffffffffU |
|
|
146 | ? 11 |
|
|
147 | : 7 |
|
|
148 | : 5 |
|
|
149 | : 4 |
|
|
150 | : 3; |
|
|
151 | } |
|
|
152 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
153 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
154 | // encoder |
131 | |
155 | |
132 | // structure used for encoding CBOR |
156 | // structure used for encoding CBOR |
133 | typedef struct |
157 | typedef struct |
… | |
… | |
135 | char *cur; // SvPVX (sv) + current output position |
159 | char *cur; // SvPVX (sv) + current output position |
136 | char *end; // SvEND (sv) |
160 | char *end; // SvEND (sv) |
137 | SV *sv; // result scalar |
161 | SV *sv; // result scalar |
138 | CBOR cbor; |
162 | CBOR cbor; |
139 | U32 depth; // recursion level |
163 | U32 depth; // recursion level |
140 | HV *stringref; // string => index, or 0 |
164 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
165 | UV stringref_idx; |
141 | HV *shareable; // ptr => index, or 0 |
166 | HV *shareable; // ptr => index, or 0 |
142 | UV shareable_idx; |
167 | UV shareable_idx; |
143 | } enc_t; |
168 | } enc_t; |
144 | |
169 | |
145 | ecb_inline void |
170 | ecb_inline void |
… | |
… | |
164 | static void |
189 | static void |
165 | encode_uint (enc_t *enc, int major, UV len) |
190 | encode_uint (enc_t *enc, int major, UV len) |
166 | { |
191 | { |
167 | need (enc, 9); |
192 | need (enc, 9); |
168 | |
193 | |
169 | if (len < 24) |
194 | if (ecb_expect_true (len < 24)) |
170 | *enc->cur++ = major | len; |
195 | *enc->cur++ = major | len; |
171 | else if (len <= 0xff) |
196 | else if (ecb_expect_true (len <= 0xff)) |
172 | { |
197 | { |
173 | *enc->cur++ = major | 24; |
198 | *enc->cur++ = major | 24; |
174 | *enc->cur++ = len; |
199 | *enc->cur++ = len; |
175 | } |
200 | } |
176 | else if (len <= 0xffff) |
201 | else if (len <= 0xffff) |
… | |
… | |
199 | *enc->cur++ = len >> 8; |
224 | *enc->cur++ = len >> 8; |
200 | *enc->cur++ = len; |
225 | *enc->cur++ = len; |
201 | } |
226 | } |
202 | } |
227 | } |
203 | |
228 | |
204 | static void |
229 | ecb_inline void |
|
|
230 | encode_tag (enc_t *enc, UV tag) |
|
|
231 | { |
|
|
232 | encode_uint (enc, 0xc0, tag); |
|
|
233 | } |
|
|
234 | |
|
|
235 | ecb_inline void |
205 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
236 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
206 | { |
237 | { |
207 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
238 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
208 | need (enc, len); |
239 | need (enc, len); |
209 | memcpy (enc->cur, str, len); |
240 | memcpy (enc->cur, str, len); |
210 | enc->cur += len; |
241 | enc->cur += len; |
211 | } |
242 | } |
212 | |
243 | |
213 | ecb_inline void |
244 | static void |
214 | encode_tag (enc_t *enc, UV tag) |
245 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
215 | { |
246 | { |
216 | encode_uint (enc, 0xc0, tag); |
247 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
|
|
248 | { |
|
|
249 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
|
|
250 | |
|
|
251 | if (SvOK (*svp)) |
|
|
252 | { |
|
|
253 | // already registered, use stringref |
|
|
254 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
255 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
256 | return; |
|
|
257 | } |
|
|
258 | else if (len >= minimum_string_length (enc->stringref_idx)) |
|
|
259 | { |
|
|
260 | // register only |
|
|
261 | sv_setuv (*svp, enc->stringref_idx); |
|
|
262 | ++enc->stringref_idx; |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
|
|
266 | encode_str (enc, utf8, str, len); |
217 | } |
267 | } |
218 | |
268 | |
219 | static void encode_sv (enc_t *enc, SV *sv); |
269 | static void encode_sv (enc_t *enc, SV *sv); |
220 | |
270 | |
221 | static void |
271 | static void |
… | |
… | |
260 | while ((he = hv_iternext (hv))) |
310 | while ((he = hv_iternext (hv))) |
261 | { |
311 | { |
262 | if (HeKLEN (he) == HEf_SVKEY) |
312 | if (HeKLEN (he) == HEf_SVKEY) |
263 | encode_sv (enc, HeSVKEY (he)); |
313 | encode_sv (enc, HeSVKEY (he)); |
264 | else |
314 | else |
265 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
315 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
266 | |
316 | |
267 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
317 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
268 | } |
318 | } |
269 | |
319 | |
270 | if (mg) |
320 | if (mg) |
… | |
… | |
276 | // encode objects, arrays and special \0=false and \1=true values. |
326 | // encode objects, arrays and special \0=false and \1=true values. |
277 | static void |
327 | static void |
278 | encode_rv (enc_t *enc, SV *sv) |
328 | encode_rv (enc_t *enc, SV *sv) |
279 | { |
329 | { |
280 | SvGETMAGIC (sv); |
330 | SvGETMAGIC (sv); |
281 | |
|
|
282 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
283 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
284 | { |
|
|
285 | if (!enc->shareable) |
|
|
286 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
287 | |
|
|
288 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
289 | |
|
|
290 | if (SvOK (*svp)) |
|
|
291 | { |
|
|
292 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
293 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
294 | return; |
|
|
295 | } |
|
|
296 | else |
|
|
297 | { |
|
|
298 | sv_setuv (*svp, enc->shareable_idx); |
|
|
299 | ++enc->shareable_idx; |
|
|
300 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
301 | } |
|
|
302 | } |
|
|
303 | |
331 | |
304 | svtype svt = SvTYPE (sv); |
332 | svtype svt = SvTYPE (sv); |
305 | |
333 | |
306 | if (ecb_expect_false (SvOBJECT (sv))) |
334 | if (ecb_expect_false (SvOBJECT (sv))) |
307 | { |
335 | { |
… | |
… | |
314 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
342 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
315 | ? cbor_tagged_stash |
343 | ? cbor_tagged_stash |
316 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
344 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
317 | |
345 | |
318 | HV *stash = SvSTASH (sv); |
346 | HV *stash = SvSTASH (sv); |
319 | GV *method; |
|
|
320 | |
347 | |
321 | if (stash == boolean_stash) |
348 | if (stash == boolean_stash) |
|
|
349 | { |
322 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
350 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
351 | return; |
|
|
352 | } |
323 | else if (stash == error_stash) |
353 | else if (stash == error_stash) |
|
|
354 | { |
324 | encode_ch (enc, 0xe0 | 23); |
355 | encode_ch (enc, 0xe0 | 23); |
|
|
356 | return; |
|
|
357 | } |
325 | else if (stash == tagged_stash) |
358 | else if (stash == tagged_stash) |
326 | { |
359 | { |
327 | if (svt != SVt_PVAV) |
360 | if (svt != SVt_PVAV) |
328 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
361 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
329 | |
362 | |
330 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
363 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
331 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
364 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
365 | |
|
|
366 | return; |
|
|
367 | } |
|
|
368 | } |
|
|
369 | |
|
|
370 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
371 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
372 | { |
|
|
373 | if (!enc->shareable) |
|
|
374 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
375 | |
|
|
376 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
377 | |
|
|
378 | if (SvOK (*svp)) |
332 | } |
379 | { |
|
|
380 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
381 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
382 | return; |
|
|
383 | } |
|
|
384 | else |
|
|
385 | { |
|
|
386 | sv_setuv (*svp, enc->shareable_idx); |
|
|
387 | ++enc->shareable_idx; |
|
|
388 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
389 | } |
|
|
390 | } |
|
|
391 | |
|
|
392 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
393 | { |
|
|
394 | HV *stash = SvSTASH (sv); |
|
|
395 | GV *method; |
|
|
396 | |
333 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
397 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
334 | { |
398 | { |
335 | dSP; |
399 | dSP; |
336 | |
400 | |
337 | ENTER; SAVETMPS; PUSHMARK (SP); |
401 | ENTER; SAVETMPS; PUSHMARK (SP); |
338 | // we re-bless the reference to get overload and other niceties right |
402 | // we re-bless the reference to get overload and other niceties right |
… | |
… | |
371 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
435 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
372 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
436 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
373 | |
437 | |
374 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
438 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
375 | encode_uint (enc, 0x80, count + 1); |
439 | encode_uint (enc, 0x80, count + 1); |
376 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
440 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
377 | |
441 | |
378 | while (count) |
442 | while (count) |
379 | encode_sv (enc, SP[1 - count--]); |
443 | encode_sv (enc, SP[1 - count--]); |
380 | |
444 | |
381 | PUTBACK; |
445 | PUTBACK; |
… | |
… | |
440 | |
504 | |
441 | if (SvPOKp (sv)) |
505 | if (SvPOKp (sv)) |
442 | { |
506 | { |
443 | STRLEN len; |
507 | STRLEN len; |
444 | char *str = SvPV (sv, len); |
508 | char *str = SvPV (sv, len); |
445 | encode_str (enc, SvUTF8 (sv), str, len); |
509 | encode_strref (enc, SvUTF8 (sv), str, len); |
446 | } |
510 | } |
447 | else if (SvNOKp (sv)) |
511 | else if (SvNOKp (sv)) |
448 | encode_nv (enc, sv); |
512 | encode_nv (enc, sv); |
449 | else if (SvIOKp (sv)) |
513 | else if (SvIOKp (sv)) |
450 | { |
514 | { |
… | |
… | |
475 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
539 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
476 | enc.cur = SvPVX (enc.sv); |
540 | enc.cur = SvPVX (enc.sv); |
477 | enc.end = SvEND (enc.sv); |
541 | enc.end = SvEND (enc.sv); |
478 | |
542 | |
479 | SvPOK_only (enc.sv); |
543 | SvPOK_only (enc.sv); |
|
|
544 | |
|
|
545 | if (cbor->flags & F_PACK_STRINGS) |
|
|
546 | { |
|
|
547 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
548 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
549 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
550 | } |
|
|
551 | |
480 | encode_sv (&enc, scalar); |
552 | encode_sv (&enc, scalar); |
481 | |
553 | |
482 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
554 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
483 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
555 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
484 | |
556 | |
… | |
… | |
499 | const char *err; // parse error, if != 0 |
571 | const char *err; // parse error, if != 0 |
500 | CBOR cbor; |
572 | CBOR cbor; |
501 | U32 depth; // recursion depth |
573 | U32 depth; // recursion depth |
502 | U32 maxdepth; // recursion depth limit |
574 | U32 maxdepth; // recursion depth limit |
503 | AV *shareable; |
575 | AV *shareable; |
|
|
576 | AV *stringref; |
|
|
577 | SV *decode_tagged; |
504 | } dec_t; |
578 | } dec_t; |
505 | |
579 | |
506 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
580 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
507 | |
581 | |
508 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
582 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
540 | | ((UV)dec->cur[-1]); |
614 | | ((UV)dec->cur[-1]); |
541 | |
615 | |
542 | case 27: |
616 | case 27: |
543 | WANT (9); |
617 | WANT (9); |
544 | dec->cur += 9; |
618 | dec->cur += 9; |
|
|
619 | |
|
|
620 | return |
|
|
621 | #if UVSIZE < 8 |
|
|
622 | 0 |
|
|
623 | #else |
545 | return (((UV)dec->cur[-8]) << 56) |
624 | (((UV)dec->cur[-8]) << 56) |
546 | | (((UV)dec->cur[-7]) << 48) |
625 | | (((UV)dec->cur[-7]) << 48) |
547 | | (((UV)dec->cur[-6]) << 40) |
626 | | (((UV)dec->cur[-6]) << 40) |
548 | | (((UV)dec->cur[-5]) << 32) |
627 | | (((UV)dec->cur[-5]) << 32) |
|
|
628 | #endif |
549 | | (((UV)dec->cur[-4]) << 24) |
629 | | (((UV)dec->cur[-4]) << 24) |
550 | | (((UV)dec->cur[-3]) << 16) |
630 | | (((UV)dec->cur[-3]) << 16) |
551 | | (((UV)dec->cur[-2]) << 8) |
631 | | (((UV)dec->cur[-2]) << 8) |
552 | | ((UV)dec->cur[-1]); |
632 | | ((UV)dec->cur[-1]); |
553 | |
633 | |
… | |
… | |
606 | |
686 | |
607 | static void |
687 | static void |
608 | decode_he (dec_t *dec, HV *hv) |
688 | decode_he (dec_t *dec, HV *hv) |
609 | { |
689 | { |
610 | // for speed reasons, we specialcase single-string |
690 | // for speed reasons, we specialcase single-string |
611 | // byte or utf-8 strings as keys. |
691 | // byte or utf-8 strings as keys, but only when !stringref |
612 | |
692 | |
|
|
693 | if (ecb_expect_true (!dec->stringref)) |
613 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
694 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
614 | { |
695 | { |
615 | I32 len = decode_uint (dec); |
696 | I32 len = decode_uint (dec); |
616 | char *key = (char *)dec->cur; |
697 | char *key = (char *)dec->cur; |
617 | |
698 | |
618 | dec->cur += len; |
699 | dec->cur += len; |
619 | |
700 | |
|
|
701 | if (ecb_expect_false (dec->stringref)) |
|
|
702 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
703 | |
620 | hv_store (hv, key, len, decode_sv (dec), 0); |
704 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
705 | |
|
|
706 | return; |
621 | } |
707 | } |
622 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
708 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
623 | { |
709 | { |
624 | I32 len = decode_uint (dec); |
710 | I32 len = decode_uint (dec); |
625 | char *key = (char *)dec->cur; |
711 | char *key = (char *)dec->cur; |
626 | |
712 | |
627 | dec->cur += len; |
713 | dec->cur += len; |
628 | |
714 | |
|
|
715 | if (ecb_expect_false (dec->stringref)) |
|
|
716 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
|
|
717 | |
629 | hv_store (hv, key, -len, decode_sv (dec), 0); |
718 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
719 | |
|
|
720 | return; |
630 | } |
721 | } |
631 | else |
722 | |
632 | { |
|
|
633 | SV *k = decode_sv (dec); |
723 | SV *k = decode_sv (dec); |
634 | SV *v = decode_sv (dec); |
724 | SV *v = decode_sv (dec); |
635 | |
725 | |
636 | hv_store_ent (hv, k, v, 0); |
726 | hv_store_ent (hv, k, v, 0); |
637 | SvREFCNT_dec (k); |
727 | SvREFCNT_dec (k); |
638 | } |
|
|
639 | } |
728 | } |
640 | |
729 | |
641 | static SV * |
730 | static SV * |
642 | decode_hv (dec_t *dec) |
731 | decode_hv (dec_t *dec) |
643 | { |
732 | { |
… | |
… | |
684 | { |
773 | { |
685 | SV *sv = 0; |
774 | SV *sv = 0; |
686 | |
775 | |
687 | if ((*dec->cur & 31) == 31) |
776 | if ((*dec->cur & 31) == 31) |
688 | { |
777 | { |
|
|
778 | // indefinite length strings |
689 | ++dec->cur; |
779 | ++dec->cur; |
690 | |
780 | |
|
|
781 | unsigned char major = *dec->cur & 0xe0; |
|
|
782 | |
691 | sv = newSVpvn ("", 0); |
783 | sv = newSVpvn ("", 0); |
692 | |
784 | |
693 | // not very fast, and certainly not robust against illegal input |
|
|
694 | for (;;) |
785 | for (;;) |
695 | { |
786 | { |
696 | WANT (1); |
787 | WANT (1); |
697 | |
788 | |
|
|
789 | if ((*dec->cur ^ major) >= 31) |
698 | if (*dec->cur == (0xe0 | 31)) |
790 | if (*dec->cur == (0xe0 | 31)) |
699 | { |
791 | { |
700 | ++dec->cur; |
792 | ++dec->cur; |
701 | break; |
793 | break; |
702 | } |
794 | } |
|
|
795 | else |
|
|
796 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
703 | |
797 | |
704 | sv_catsv (sv, decode_sv (dec)); |
798 | STRLEN len = decode_uint (dec); |
|
|
799 | |
|
|
800 | WANT (len); |
|
|
801 | sv_catpvn (sv, dec->cur, len); |
|
|
802 | dec->cur += len; |
705 | } |
803 | } |
706 | } |
804 | } |
707 | else |
805 | else |
708 | { |
806 | { |
709 | STRLEN len = decode_uint (dec); |
807 | STRLEN len = decode_uint (dec); |
710 | |
808 | |
711 | WANT (len); |
809 | WANT (len); |
712 | sv = newSVpvn (dec->cur, len); |
810 | sv = newSVpvn (dec->cur, len); |
713 | dec->cur += len; |
811 | dec->cur += len; |
|
|
812 | |
|
|
813 | if (ecb_expect_false (dec->stringref) |
|
|
814 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
815 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
714 | } |
816 | } |
715 | |
817 | |
716 | if (utf8) |
818 | if (utf8) |
717 | SvUTF8_on (sv); |
819 | SvUTF8_on (sv); |
718 | |
820 | |
… | |
… | |
732 | WANT (1); |
834 | WANT (1); |
733 | |
835 | |
734 | switch (tag) |
836 | switch (tag) |
735 | { |
837 | { |
736 | case CBOR_TAG_MAGIC: |
838 | case CBOR_TAG_MAGIC: |
737 | return decode_sv (dec); |
839 | sv = decode_sv (dec); |
|
|
840 | break; |
738 | |
841 | |
739 | case CBOR_TAG_INDIRECTION: |
842 | case CBOR_TAG_INDIRECTION: |
740 | return newRV_noinc (decode_sv (dec)); |
843 | sv = newRV_noinc (decode_sv (dec)); |
|
|
844 | break; |
|
|
845 | |
|
|
846 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
847 | { |
|
|
848 | ENTER; SAVETMPS; |
|
|
849 | |
|
|
850 | SAVESPTR (dec->stringref); |
|
|
851 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
852 | |
|
|
853 | sv = decode_sv (dec); |
|
|
854 | |
|
|
855 | FREETMPS; LEAVE; |
|
|
856 | } |
|
|
857 | break; |
|
|
858 | |
|
|
859 | case CBOR_TAG_STRINGREF: |
|
|
860 | { |
|
|
861 | if ((*dec->cur >> 5) != 0) |
|
|
862 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
863 | |
|
|
864 | UV idx = decode_uint (dec); |
|
|
865 | |
|
|
866 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
867 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
868 | |
|
|
869 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
870 | } |
|
|
871 | break; |
741 | |
872 | |
742 | case CBOR_TAG_VALUE_SHAREABLE: |
873 | case CBOR_TAG_VALUE_SHAREABLE: |
743 | { |
874 | { |
744 | if (ecb_expect_false (!dec->shareable)) |
875 | if (ecb_expect_false (!dec->shareable)) |
745 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
876 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
… | |
… | |
749 | |
880 | |
750 | SV *osv = decode_sv (dec); |
881 | SV *osv = decode_sv (dec); |
751 | sv_setsv (sv, osv); |
882 | sv_setsv (sv, osv); |
752 | SvREFCNT_dec_NN (osv); |
883 | SvREFCNT_dec_NN (osv); |
753 | } |
884 | } |
754 | |
885 | break; |
755 | return sv; |
|
|
756 | |
886 | |
757 | case CBOR_TAG_VALUE_SHAREDREF: |
887 | case CBOR_TAG_VALUE_SHAREDREF: |
758 | { |
888 | { |
759 | if ((*dec->cur >> 5) != 0) |
889 | if ((*dec->cur >> 5) != 0) |
760 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
890 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
761 | |
891 | |
762 | UV idx = decode_uint (dec); |
892 | UV idx = decode_uint (dec); |
763 | |
893 | |
764 | if (!dec->shareable || idx > AvFILLp (dec->shareable)) |
894 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
765 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
895 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
766 | |
896 | |
767 | return SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
897 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
768 | } |
898 | } |
|
|
899 | break; |
769 | |
900 | |
770 | case CBOR_TAG_PERL_OBJECT: |
901 | case CBOR_TAG_PERL_OBJECT: |
771 | { |
902 | { |
772 | sv = decode_sv (dec); |
903 | sv = decode_sv (dec); |
773 | |
904 | |
… | |
… | |
813 | sv = SvREFCNT_inc (POPs); |
944 | sv = SvREFCNT_inc (POPs); |
814 | |
945 | |
815 | PUTBACK; |
946 | PUTBACK; |
816 | |
947 | |
817 | FREETMPS; LEAVE; |
948 | FREETMPS; LEAVE; |
818 | |
|
|
819 | return sv; |
|
|
820 | } |
949 | } |
|
|
950 | break; |
821 | |
951 | |
822 | default: |
952 | default: |
823 | { |
953 | { |
824 | sv = decode_sv (dec); |
954 | sv = decode_sv (dec); |
825 | |
955 | |
|
|
956 | dSP; |
|
|
957 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
958 | EXTEND (SP, 2); |
|
|
959 | PUSHs (newSVuv (tag)); |
|
|
960 | PUSHs (sv); |
|
|
961 | |
|
|
962 | PUTBACK; |
|
|
963 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
964 | SPAGAIN; |
|
|
965 | |
|
|
966 | if (SvTRUE (ERRSV)) |
|
|
967 | { |
|
|
968 | FREETMPS; LEAVE; |
|
|
969 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
970 | } |
|
|
971 | |
|
|
972 | if (count) |
|
|
973 | { |
|
|
974 | SvREFCNT_dec (sv); |
|
|
975 | sv = SvREFCNT_inc (POPs); |
|
|
976 | } |
|
|
977 | else |
|
|
978 | { |
826 | AV *av = newAV (); |
979 | AV *av = newAV (); |
827 | av_push (av, newSVuv (tag)); |
980 | av_push (av, newSVuv (tag)); |
828 | av_push (av, sv); |
981 | av_push (av, sv); |
829 | |
982 | |
830 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
983 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
831 | ? cbor_tagged_stash |
984 | ? cbor_tagged_stash |
832 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
985 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
833 | |
|
|
834 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
986 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
835 | } |
987 | } |
|
|
988 | |
|
|
989 | PUTBACK; |
|
|
990 | |
|
|
991 | FREETMPS; LEAVE; |
|
|
992 | } |
|
|
993 | break; |
836 | } |
994 | } |
|
|
995 | |
|
|
996 | return sv; |
837 | |
997 | |
838 | fail: |
998 | fail: |
839 | SvREFCNT_dec (sv); |
999 | SvREFCNT_dec (sv); |
840 | return &PL_sv_undef; |
1000 | return &PL_sv_undef; |
841 | } |
1001 | } |
… | |
… | |
982 | |
1142 | |
983 | types_true = get_bool ("Types::Serialiser::true" ); |
1143 | types_true = get_bool ("Types::Serialiser::true" ); |
984 | types_false = get_bool ("Types::Serialiser::false"); |
1144 | types_false = get_bool ("Types::Serialiser::false"); |
985 | types_error = get_bool ("Types::Serialiser::error"); |
1145 | types_error = get_bool ("Types::Serialiser::error"); |
986 | |
1146 | |
|
|
1147 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1148 | |
987 | sv_cbor = newSVpv ("CBOR", 0); |
1149 | sv_cbor = newSVpv ("CBOR", 0); |
988 | SvREADONLY_on (sv_cbor); |
1150 | SvREADONLY_on (sv_cbor); |
989 | } |
1151 | } |
990 | |
1152 | |
991 | PROTOTYPES: DISABLE |
1153 | PROTOTYPES: DISABLE |
… | |
… | |
1012 | void shrink (CBOR *self, int enable = 1) |
1174 | void shrink (CBOR *self, int enable = 1) |
1013 | ALIAS: |
1175 | ALIAS: |
1014 | shrink = F_SHRINK |
1176 | shrink = F_SHRINK |
1015 | allow_unknown = F_ALLOW_UNKNOWN |
1177 | allow_unknown = F_ALLOW_UNKNOWN |
1016 | allow_sharing = F_ALLOW_SHARING |
1178 | allow_sharing = F_ALLOW_SHARING |
1017 | dedup_keys = F_DEDUP_KEYS |
|
|
1018 | dedup_strings = F_DEDUP_STRINGS |
1179 | pack_strings = F_PACK_STRINGS |
1019 | PPCODE: |
1180 | PPCODE: |
1020 | { |
1181 | { |
1021 | if (enable) |
1182 | if (enable) |
1022 | self->flags |= ix; |
1183 | self->flags |= ix; |
1023 | else |
1184 | else |
… | |
… | |
1029 | void get_shrink (CBOR *self) |
1190 | void get_shrink (CBOR *self) |
1030 | ALIAS: |
1191 | ALIAS: |
1031 | get_shrink = F_SHRINK |
1192 | get_shrink = F_SHRINK |
1032 | get_allow_unknown = F_ALLOW_UNKNOWN |
1193 | get_allow_unknown = F_ALLOW_UNKNOWN |
1033 | get_allow_sharing = F_ALLOW_SHARING |
1194 | get_allow_sharing = F_ALLOW_SHARING |
1034 | get_dedup_keys = F_DEDUP_KEYS |
|
|
1035 | get_dedup_strings = F_DEDUP_STRINGS |
1195 | get_pack_strings = F_PACK_STRINGS |
1036 | PPCODE: |
1196 | PPCODE: |
1037 | XPUSHs (boolSV (self->flags & ix)); |
1197 | XPUSHs (boolSV (self->flags & ix)); |
1038 | |
1198 | |
1039 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1199 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1040 | PPCODE: |
1200 | PPCODE: |
… | |
… | |
1053 | XPUSHs (ST (0)); |
1213 | XPUSHs (ST (0)); |
1054 | |
1214 | |
1055 | int get_max_size (CBOR *self) |
1215 | int get_max_size (CBOR *self) |
1056 | CODE: |
1216 | CODE: |
1057 | RETVAL = self->max_size; |
1217 | RETVAL = self->max_size; |
|
|
1218 | OUTPUT: |
|
|
1219 | RETVAL |
|
|
1220 | |
|
|
1221 | void filter (CBOR *self, SV *filter = 0) |
|
|
1222 | PPCODE: |
|
|
1223 | SvREFCNT_dec (self->filter); |
|
|
1224 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1225 | XPUSHs (ST (0)); |
|
|
1226 | |
|
|
1227 | SV *get_filter (CBOR *self) |
|
|
1228 | CODE: |
|
|
1229 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
1058 | OUTPUT: |
1230 | OUTPUT: |
1059 | RETVAL |
1231 | RETVAL |
1060 | |
1232 | |
1061 | void encode (CBOR *self, SV *scalar) |
1233 | void encode (CBOR *self, SV *scalar) |
1062 | PPCODE: |
1234 | PPCODE: |
… | |
… | |
1077 | EXTEND (SP, 2); |
1249 | EXTEND (SP, 2); |
1078 | PUSHs (sv); |
1250 | PUSHs (sv); |
1079 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1251 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1080 | } |
1252 | } |
1081 | |
1253 | |
|
|
1254 | void DESTROY (CBOR *self) |
|
|
1255 | PPCODE: |
|
|
1256 | cbor_free (self); |
|
|
1257 | |
1082 | PROTOTYPES: ENABLE |
1258 | PROTOTYPES: ENABLE |
1083 | |
1259 | |
1084 | void encode_cbor (SV *scalar) |
1260 | void encode_cbor (SV *scalar) |
1085 | PPCODE: |
1261 | PPCODE: |
1086 | { |
1262 | { |