… | |
… | |
189 | static void |
189 | static void |
190 | encode_uint (enc_t *enc, int major, UV len) |
190 | encode_uint (enc_t *enc, int major, UV len) |
191 | { |
191 | { |
192 | need (enc, 9); |
192 | need (enc, 9); |
193 | |
193 | |
194 | if (len < 24) |
194 | if (ecb_expect_true (len < 24)) |
195 | *enc->cur++ = major | len; |
195 | *enc->cur++ = major | len; |
196 | else if (len <= 0xff) |
196 | else if (ecb_expect_true (len <= 0xff)) |
197 | { |
197 | { |
198 | *enc->cur++ = major | 24; |
198 | *enc->cur++ = major | 24; |
199 | *enc->cur++ = len; |
199 | *enc->cur++ = len; |
200 | } |
200 | } |
201 | else if (len <= 0xffff) |
201 | else if (len <= 0xffff) |
… | |
… | |
230 | encode_tag (enc_t *enc, UV tag) |
230 | encode_tag (enc_t *enc, UV tag) |
231 | { |
231 | { |
232 | encode_uint (enc, 0xc0, tag); |
232 | encode_uint (enc, 0xc0, tag); |
233 | } |
233 | } |
234 | |
234 | |
|
|
235 | ecb_inline void |
|
|
236 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
237 | { |
|
|
238 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
|
|
239 | need (enc, len); |
|
|
240 | memcpy (enc->cur, str, len); |
|
|
241 | enc->cur += len; |
|
|
242 | } |
|
|
243 | |
235 | static void |
244 | static void |
236 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
245 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
237 | { |
246 | { |
238 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
247 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
239 | { |
248 | { |
240 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
249 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
241 | |
250 | |
… | |
… | |
252 | sv_setuv (*svp, enc->stringref_idx); |
261 | sv_setuv (*svp, enc->stringref_idx); |
253 | ++enc->stringref_idx; |
262 | ++enc->stringref_idx; |
254 | } |
263 | } |
255 | } |
264 | } |
256 | |
265 | |
257 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
266 | encode_str (enc, utf8, str, len); |
258 | need (enc, len); |
|
|
259 | memcpy (enc->cur, str, len); |
|
|
260 | enc->cur += len; |
|
|
261 | } |
267 | } |
262 | |
268 | |
263 | static void encode_sv (enc_t *enc, SV *sv); |
269 | static void encode_sv (enc_t *enc, SV *sv); |
264 | |
270 | |
265 | static void |
271 | static void |
… | |
… | |
304 | while ((he = hv_iternext (hv))) |
310 | while ((he = hv_iternext (hv))) |
305 | { |
311 | { |
306 | if (HeKLEN (he) == HEf_SVKEY) |
312 | if (HeKLEN (he) == HEf_SVKEY) |
307 | encode_sv (enc, HeSVKEY (he)); |
313 | encode_sv (enc, HeSVKEY (he)); |
308 | else |
314 | else |
309 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
315 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
310 | |
316 | |
311 | 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)); |
312 | } |
318 | } |
313 | |
319 | |
314 | if (mg) |
320 | if (mg) |
… | |
… | |
415 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
421 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
416 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
422 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
417 | |
423 | |
418 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
424 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
419 | encode_uint (enc, 0x80, count + 1); |
425 | encode_uint (enc, 0x80, count + 1); |
420 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
426 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
421 | |
427 | |
422 | while (count) |
428 | while (count) |
423 | encode_sv (enc, SP[1 - count--]); |
429 | encode_sv (enc, SP[1 - count--]); |
424 | |
430 | |
425 | PUTBACK; |
431 | PUTBACK; |
… | |
… | |
484 | |
490 | |
485 | if (SvPOKp (sv)) |
491 | if (SvPOKp (sv)) |
486 | { |
492 | { |
487 | STRLEN len; |
493 | STRLEN len; |
488 | char *str = SvPV (sv, len); |
494 | char *str = SvPV (sv, len); |
489 | encode_str (enc, SvUTF8 (sv), str, len); |
495 | encode_strref (enc, SvUTF8 (sv), str, len); |
490 | } |
496 | } |
491 | else if (SvNOKp (sv)) |
497 | else if (SvNOKp (sv)) |
492 | encode_nv (enc, sv); |
498 | encode_nv (enc, sv); |
493 | else if (SvIOKp (sv)) |
499 | else if (SvIOKp (sv)) |
494 | { |
500 | { |