… | |
… | |
100 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
|
|
105 | #define F_UTF8_STRINGS 0x00000020UL |
|
|
106 | #define F_UTF8_KEYS 0x00000040UL |
105 | #define F_VALIDATE_UTF8 0x00000020UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
106 | |
108 | |
107 | #define INIT_SIZE 32 // initial scalar size to be allocated |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
108 | |
110 | |
109 | #define SB do { |
111 | #define SB do { |
110 | #define SE } while (0) |
112 | #define SE } while (0) |
… | |
… | |
276 | encode_tag (enc_t *enc, UV tag) |
278 | encode_tag (enc_t *enc, UV tag) |
277 | { |
279 | { |
278 | encode_uint (enc, MAJOR_TAG, tag); |
280 | encode_uint (enc, MAJOR_TAG, tag); |
279 | } |
281 | } |
280 | |
282 | |
|
|
283 | // exceptional (hopefully) slow path for byte strings that need to be utf8-encoded |
|
|
284 | ecb_noinline static void |
|
|
285 | encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
286 | { |
|
|
287 | STRLEN ulen = len; |
|
|
288 | U8 *p, *pend = (U8 *)str + len; |
|
|
289 | |
|
|
290 | for (p = (U8 *)str; p < pend; ++p) |
|
|
291 | ulen += *p >> 7; // count set high bits |
|
|
292 | |
|
|
293 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
294 | |
|
|
295 | need (enc, ulen); |
|
|
296 | for (p = (U8 *)str; p < pend; ++p) |
|
|
297 | if (*p < 0x80) |
|
|
298 | *enc->cur++ = *p; |
|
|
299 | else |
|
|
300 | { |
|
|
301 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
302 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
281 | ecb_inline void |
306 | ecb_inline void |
282 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
307 | encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
283 | { |
308 | { |
|
|
309 | if (ecb_expect_false (upgrade_utf8)) |
|
|
310 | if (!utf8) |
|
|
311 | { |
|
|
312 | encode_str_utf8 (enc, utf8, str, len); |
|
|
313 | return; |
|
|
314 | } |
|
|
315 | |
284 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
316 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
285 | need (enc, len); |
317 | need (enc, len); |
286 | memcpy (enc->cur, str, len); |
318 | memcpy (enc->cur, str, len); |
287 | enc->cur += len; |
319 | enc->cur += len; |
288 | } |
320 | } |
289 | |
321 | |
290 | static void |
322 | ecb_inline |
291 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
323 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
292 | { |
324 | { |
293 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
325 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
294 | { |
326 | { |
295 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
327 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
296 | |
328 | |
… | |
… | |
307 | sv_setuv (*svp, enc->stringref_idx); |
339 | sv_setuv (*svp, enc->stringref_idx); |
308 | ++enc->stringref_idx; |
340 | ++enc->stringref_idx; |
309 | } |
341 | } |
310 | } |
342 | } |
311 | |
343 | |
312 | encode_str (enc, utf8, str, len); |
344 | encode_str (enc, upgrade_utf8, utf8, str, len); |
313 | } |
345 | } |
314 | |
346 | |
315 | static void encode_sv (enc_t *enc, SV *sv); |
347 | static void encode_sv (enc_t *enc, SV *sv); |
316 | |
348 | |
317 | static void |
349 | static void |
… | |
… | |
363 | while ((he = hv_iternext (hv))) |
395 | while ((he = hv_iternext (hv))) |
364 | { |
396 | { |
365 | if (HeKLEN (he) == HEf_SVKEY) |
397 | if (HeKLEN (he) == HEf_SVKEY) |
366 | encode_sv (enc, HeSVKEY (he)); |
398 | encode_sv (enc, HeSVKEY (he)); |
367 | else |
399 | else |
368 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
400 | encode_strref (enc, enc->cbor.flags & (F_UTF8_KEYS | F_UTF8_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
369 | |
401 | |
370 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
402 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
371 | } |
403 | } |
372 | |
404 | |
373 | if (mg) |
405 | if (mg) |
… | |
… | |
491 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
523 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
492 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
524 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
493 | |
525 | |
494 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
526 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
495 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
527 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
496 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
528 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
497 | |
529 | |
498 | while (count) |
530 | while (count) |
499 | encode_sv (enc, SP[1 - count--]); |
531 | encode_sv (enc, SP[1 - count--]); |
500 | |
532 | |
501 | PUTBACK; |
533 | PUTBACK; |
… | |
… | |
560 | |
592 | |
561 | if (SvPOKp (sv)) |
593 | if (SvPOKp (sv)) |
562 | { |
594 | { |
563 | STRLEN len; |
595 | STRLEN len; |
564 | char *str = SvPV (sv, len); |
596 | char *str = SvPV (sv, len); |
565 | encode_strref (enc, SvUTF8 (sv), str, len); |
597 | encode_strref (enc, enc->cbor.flags & F_UTF8_STRINGS, SvUTF8 (sv), str, len); |
566 | } |
598 | } |
567 | else if (SvNOKp (sv)) |
599 | else if (SvNOKp (sv)) |
568 | encode_nv (enc, sv); |
600 | encode_nv (enc, sv); |
569 | else if (SvIOKp (sv)) |
601 | else if (SvIOKp (sv)) |
570 | { |
602 | { |
… | |
… | |
1390 | shrink = F_SHRINK |
1422 | shrink = F_SHRINK |
1391 | allow_unknown = F_ALLOW_UNKNOWN |
1423 | allow_unknown = F_ALLOW_UNKNOWN |
1392 | allow_sharing = F_ALLOW_SHARING |
1424 | allow_sharing = F_ALLOW_SHARING |
1393 | allow_cycles = F_ALLOW_CYCLES |
1425 | allow_cycles = F_ALLOW_CYCLES |
1394 | pack_strings = F_PACK_STRINGS |
1426 | pack_strings = F_PACK_STRINGS |
|
|
1427 | utf8_strings = F_UTF8_STRINGS |
1395 | validate_utf8 = F_VALIDATE_UTF8 |
1428 | validate_utf8 = F_VALIDATE_UTF8 |
1396 | PPCODE: |
1429 | PPCODE: |
1397 | { |
1430 | { |
1398 | if (enable) |
1431 | if (enable) |
1399 | self->flags |= ix; |
1432 | self->flags |= ix; |