… | |
… | |
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 |
105 | #define F_UTF8_STRINGS 0x00000020UL |
|
|
106 | #define F_UTF8_KEYS 0x00000040UL |
106 | #define F_VALIDATE_UTF8 0x00000040UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
107 | |
108 | |
108 | #define INIT_SIZE 32 // initial scalar size to be allocated |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
109 | |
110 | |
110 | #define SB do { |
111 | #define SB do { |
111 | #define SE } while (0) |
112 | #define SE } while (0) |
… | |
… | |
277 | encode_tag (enc_t *enc, UV tag) |
278 | encode_tag (enc_t *enc, UV tag) |
278 | { |
279 | { |
279 | encode_uint (enc, MAJOR_TAG, tag); |
280 | encode_uint (enc, MAJOR_TAG, tag); |
280 | } |
281 | } |
281 | |
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 | |
282 | ecb_inline void |
306 | ecb_inline void |
283 | 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) |
284 | { |
308 | { |
285 | if (ecb_expect_false (enc->cbor.flags & F_UTF8_STRINGS)) |
309 | if (ecb_expect_false (upgrade_utf8)) |
286 | if (!utf8) |
310 | if (!utf8) |
287 | { |
311 | { |
288 | // exceptional path for bytze strings that need to be utf8-encoded |
312 | encode_str_utf8 (enc, utf8, str, len); |
289 | STRLEN ulen = len; |
|
|
290 | U8 *p, *pend = (U8 *)str + len; |
|
|
291 | |
|
|
292 | for (p = (U8 *)str; p < pend; ++p) |
|
|
293 | ulen += *p >> 7; // count set high bits |
|
|
294 | |
|
|
295 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
296 | |
|
|
297 | need (enc, ulen); |
|
|
298 | for (p = (U8 *)str; p < pend; ++p) |
|
|
299 | if (*p < 0x80) |
|
|
300 | *enc->cur++ = *p; |
|
|
301 | else |
|
|
302 | { |
|
|
303 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
304 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
305 | } |
|
|
306 | |
|
|
307 | return; |
313 | return; |
308 | } |
314 | } |
309 | |
315 | |
310 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
316 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
311 | need (enc, len); |
317 | need (enc, len); |
312 | memcpy (enc->cur, str, len); |
318 | memcpy (enc->cur, str, len); |
313 | enc->cur += len; |
319 | enc->cur += len; |
314 | } |
320 | } |
315 | |
321 | |
316 | static void |
322 | ecb_inline |
317 | 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) |
318 | { |
324 | { |
319 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
325 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
320 | { |
326 | { |
321 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
327 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
322 | |
328 | |
… | |
… | |
333 | sv_setuv (*svp, enc->stringref_idx); |
339 | sv_setuv (*svp, enc->stringref_idx); |
334 | ++enc->stringref_idx; |
340 | ++enc->stringref_idx; |
335 | } |
341 | } |
336 | } |
342 | } |
337 | |
343 | |
338 | encode_str (enc, utf8, str, len); |
344 | encode_str (enc, upgrade_utf8, utf8, str, len); |
339 | } |
345 | } |
340 | |
346 | |
341 | static void encode_sv (enc_t *enc, SV *sv); |
347 | static void encode_sv (enc_t *enc, SV *sv); |
342 | |
348 | |
343 | static void |
349 | static void |
… | |
… | |
389 | while ((he = hv_iternext (hv))) |
395 | while ((he = hv_iternext (hv))) |
390 | { |
396 | { |
391 | if (HeKLEN (he) == HEf_SVKEY) |
397 | if (HeKLEN (he) == HEf_SVKEY) |
392 | encode_sv (enc, HeSVKEY (he)); |
398 | encode_sv (enc, HeSVKEY (he)); |
393 | else |
399 | else |
394 | 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)); |
395 | |
401 | |
396 | 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)); |
397 | } |
403 | } |
398 | |
404 | |
399 | if (mg) |
405 | if (mg) |
… | |
… | |
517 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
523 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
518 | 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)); |
519 | |
525 | |
520 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
526 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
521 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
527 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
522 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
528 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
523 | |
529 | |
524 | while (count) |
530 | while (count) |
525 | encode_sv (enc, SP[1 - count--]); |
531 | encode_sv (enc, SP[1 - count--]); |
526 | |
532 | |
527 | PUTBACK; |
533 | PUTBACK; |
… | |
… | |
586 | |
592 | |
587 | if (SvPOKp (sv)) |
593 | if (SvPOKp (sv)) |
588 | { |
594 | { |
589 | STRLEN len; |
595 | STRLEN len; |
590 | char *str = SvPV (sv, len); |
596 | char *str = SvPV (sv, len); |
591 | encode_strref (enc, SvUTF8 (sv), str, len); |
597 | encode_strref (enc, enc->cbor.flags & F_UTF8_STRINGS, SvUTF8 (sv), str, len); |
592 | } |
598 | } |
593 | else if (SvNOKp (sv)) |
599 | else if (SvNOKp (sv)) |
594 | encode_nv (enc, sv); |
600 | encode_nv (enc, sv); |
595 | else if (SvIOKp (sv)) |
601 | else if (SvIOKp (sv)) |
596 | { |
602 | { |