… | |
… | |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
60 | }; |
60 | }; |
61 | |
61 | |
62 | #define F_SHRINK 0x00000001UL |
62 | #define F_SHRINK 0x00000001UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
64 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
64 | #define F_ALLOW_SHARING 0x00000004UL |
65 | #define F_ALLOW_STRINGREF 0x00000008UL //TODO |
65 | #define F_PACK_STRINGS 0x00000008UL |
66 | |
66 | |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
68 | |
68 | |
69 | #define SB do { |
69 | #define SB do { |
70 | #define SE } while (0) |
70 | #define SE } while (0) |
… | |
… | |
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_PACK_STRINGS)) |
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 | |
242 | if (SvOK (*svp)) |
251 | if (SvOK (*svp)) |
243 | { |
252 | { |
… | |
… | |
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 | { |
… | |
… | |
520 | enc.cur = SvPVX (enc.sv); |
526 | enc.cur = SvPVX (enc.sv); |
521 | enc.end = SvEND (enc.sv); |
527 | enc.end = SvEND (enc.sv); |
522 | |
528 | |
523 | SvPOK_only (enc.sv); |
529 | SvPOK_only (enc.sv); |
524 | |
530 | |
525 | if (cbor->flags & F_ALLOW_STRINGREF) |
531 | if (cbor->flags & F_PACK_STRINGS) |
526 | { |
532 | { |
527 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
533 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
528 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
534 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
529 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
535 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
530 | } |
536 | } |
… | |
… | |
1139 | void shrink (CBOR *self, int enable = 1) |
1145 | void shrink (CBOR *self, int enable = 1) |
1140 | ALIAS: |
1146 | ALIAS: |
1141 | shrink = F_SHRINK |
1147 | shrink = F_SHRINK |
1142 | allow_unknown = F_ALLOW_UNKNOWN |
1148 | allow_unknown = F_ALLOW_UNKNOWN |
1143 | allow_sharing = F_ALLOW_SHARING |
1149 | allow_sharing = F_ALLOW_SHARING |
1144 | allow_stringref = F_ALLOW_STRINGREF |
1150 | pack_strings = F_PACK_STRINGS |
1145 | PPCODE: |
1151 | PPCODE: |
1146 | { |
1152 | { |
1147 | if (enable) |
1153 | if (enable) |
1148 | self->flags |= ix; |
1154 | self->flags |= ix; |
1149 | else |
1155 | else |
… | |
… | |
1155 | void get_shrink (CBOR *self) |
1161 | void get_shrink (CBOR *self) |
1156 | ALIAS: |
1162 | ALIAS: |
1157 | get_shrink = F_SHRINK |
1163 | get_shrink = F_SHRINK |
1158 | get_allow_unknown = F_ALLOW_UNKNOWN |
1164 | get_allow_unknown = F_ALLOW_UNKNOWN |
1159 | get_allow_sharing = F_ALLOW_SHARING |
1165 | get_allow_sharing = F_ALLOW_SHARING |
1160 | get_allow_stringref = F_ALLOW_STRINGREF |
1166 | get_pack_strings = F_PACK_STRINGS |
1161 | PPCODE: |
1167 | PPCODE: |
1162 | XPUSHs (boolSV (self->flags & ix)); |
1168 | XPUSHs (boolSV (self->flags & ix)); |
1163 | |
1169 | |
1164 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1170 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1165 | PPCODE: |
1171 | PPCODE: |