… | |
… | |
58 | |
58 | |
59 | #define F_SHRINK 0x00000001UL |
59 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
63 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
|
|
64 | |
63 | |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
66 | |
65 | |
67 | #define SB do { |
66 | #define SB do { |
68 | #define SE } while (0) |
67 | #define SE } while (0) |
… | |
… | |
122 | SvPV_shrink_to_cur (sv); |
121 | SvPV_shrink_to_cur (sv); |
123 | #elif defined (SvPV_renew) |
122 | #elif defined (SvPV_renew) |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
123 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
124 | #endif |
126 | } |
125 | } |
|
|
126 | } |
|
|
127 | |
|
|
128 | // minimum length of a string to be registered for stringref |
|
|
129 | ecb_inline int |
|
|
130 | minimum_string_length (UV idx) |
|
|
131 | { |
|
|
132 | return idx > 23 |
|
|
133 | ? idx > 0xffU |
|
|
134 | ? idx > 0xffffU |
|
|
135 | ? idx > 0xffffffffU |
|
|
136 | ? 7 |
|
|
137 | : 6 |
|
|
138 | : 5 |
|
|
139 | : 4 |
|
|
140 | : 3; |
127 | } |
141 | } |
128 | |
142 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
143 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
144 | // encoder |
131 | |
145 | |
… | |
… | |
200 | *enc->cur++ = len >> 8; |
214 | *enc->cur++ = len >> 8; |
201 | *enc->cur++ = len; |
215 | *enc->cur++ = len; |
202 | } |
216 | } |
203 | } |
217 | } |
204 | |
218 | |
|
|
219 | ecb_inline void |
|
|
220 | encode_tag (enc_t *enc, UV tag) |
|
|
221 | { |
|
|
222 | encode_uint (enc, 0xc0, tag); |
|
|
223 | } |
|
|
224 | |
205 | static void |
225 | static void |
206 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
226 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
207 | { |
227 | { |
|
|
228 | if (ecb_expect_false (enc->cbor.flags & F_DEDUP_STRINGS)) |
|
|
229 | { |
|
|
230 | SV **svp = hv_fetch (enc->stringref[utf8], str, len, 1); |
|
|
231 | |
|
|
232 | if (SvOK (*svp)) |
|
|
233 | { |
|
|
234 | // already registered, use stringref |
|
|
235 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
236 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
237 | return; |
|
|
238 | } |
|
|
239 | else if (len >= minimum_string_length (enc->stringref_idx)) |
|
|
240 | { |
|
|
241 | // register only |
|
|
242 | sv_setuv (*svp, enc->stringref_idx); |
|
|
243 | ++enc->stringref_idx; |
|
|
244 | } |
|
|
245 | } |
|
|
246 | |
208 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
247 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
209 | need (enc, len); |
248 | need (enc, len); |
210 | memcpy (enc->cur, str, len); |
249 | memcpy (enc->cur, str, len); |
211 | enc->cur += len; |
250 | enc->cur += len; |
212 | } |
251 | } |
213 | |
252 | |
214 | ecb_inline void |
|
|
215 | encode_tag (enc_t *enc, UV tag) |
|
|
216 | { |
|
|
217 | encode_uint (enc, 0xc0, tag); |
|
|
218 | } |
|
|
219 | |
|
|
220 | static void encode_sv (enc_t *enc, SV *sv); |
253 | static void encode_sv (enc_t *enc, SV *sv); |
221 | |
254 | |
222 | static void |
255 | static void |
223 | encode_av (enc_t *enc, AV *av) |
256 | encode_av (enc_t *enc, AV *av) |
224 | { |
257 | { |
… | |
… | |
241 | } |
274 | } |
242 | |
275 | |
243 | ecb_inline void |
276 | ecb_inline void |
244 | encode_he (enc_t *enc, HE *he) |
277 | encode_he (enc_t *enc, HE *he) |
245 | { |
278 | { |
246 | if (HeKLEN (he) == HEf_SVKEY) |
|
|
247 | encode_sv (enc, HeSVKEY (he)); |
|
|
248 | else |
|
|
249 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
|
|
250 | } |
279 | } |
251 | |
280 | |
252 | static void |
281 | static void |
253 | encode_hv (enc_t *enc, HV *hv) |
282 | encode_hv (enc_t *enc, HV *hv) |
254 | { |
283 | { |
… | |
… | |
267 | else |
296 | else |
268 | encode_uint (enc, 0xa0, pairs); |
297 | encode_uint (enc, 0xa0, pairs); |
269 | |
298 | |
270 | while ((he = hv_iternext (hv))) |
299 | while ((he = hv_iternext (hv))) |
271 | { |
300 | { |
272 | if (ecb_expect_false (enc->cbor.flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS))) |
|
|
273 | { |
|
|
274 | SV **svp; |
|
|
275 | |
|
|
276 | if (HeKLEN (he) == HEf_SVKEY) |
301 | if (HeKLEN (he) == HEf_SVKEY) |
277 | svp = hv_fetch_ent (enc->stringref[!! SvUTF8 (HeSVKEY (he))], HeSVKEY (he) , 1, 0);//TODO return HE :/ |
302 | encode_sv (enc, HeSVKEY (he)); |
278 | else |
|
|
279 | svp = hv_fetch (enc->stringref[!! HeKUTF8 (he) ], HeKEY (he), HeKLEN (he), 1); |
|
|
280 | |
|
|
281 | if (SvOK (*svp)) |
|
|
282 | { |
|
|
283 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
284 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
285 | } |
|
|
286 | else |
|
|
287 | { |
|
|
288 | sv_setuv (*svp, enc->stringref_idx); |
|
|
289 | ++enc->stringref_idx; |
|
|
290 | encode_he (enc, he); |
|
|
291 | } |
|
|
292 | } |
|
|
293 | else |
303 | else |
294 | encode_he (enc, he); |
304 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
295 | |
305 | |
296 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
306 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
297 | } |
307 | } |
298 | |
308 | |
299 | if (mg) |
309 | if (mg) |
… | |
… | |
505 | enc.cur = SvPVX (enc.sv); |
515 | enc.cur = SvPVX (enc.sv); |
506 | enc.end = SvEND (enc.sv); |
516 | enc.end = SvEND (enc.sv); |
507 | |
517 | |
508 | SvPOK_only (enc.sv); |
518 | SvPOK_only (enc.sv); |
509 | |
519 | |
510 | if (cbor->flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS)) |
520 | if (cbor->flags & F_DEDUP_STRINGS) |
511 | { |
521 | { |
512 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
522 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
513 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
523 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
514 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
524 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
515 | } |
525 | } |
… | |
… | |
644 | |
654 | |
645 | static void |
655 | static void |
646 | decode_he (dec_t *dec, HV *hv) |
656 | decode_he (dec_t *dec, HV *hv) |
647 | { |
657 | { |
648 | // for speed reasons, we specialcase single-string |
658 | // for speed reasons, we specialcase single-string |
649 | // byte or utf-8 strings as keys. |
659 | // byte or utf-8 strings as keys, but only when !stringref |
650 | |
660 | |
|
|
661 | if (expect_true (!dec->stringref)) |
651 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
662 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
652 | { |
663 | { |
653 | I32 len = decode_uint (dec); |
664 | I32 len = decode_uint (dec); |
654 | char *key = (char *)dec->cur; |
665 | char *key = (char *)dec->cur; |
655 | |
666 | |
656 | dec->cur += len; |
667 | dec->cur += len; |
657 | |
668 | |
658 | if (ecb_expect_false (dec->stringref)) |
669 | if (ecb_expect_false (dec->stringref)) |
659 | av_push (dec->stringref, newSVpvn (key, len)); |
670 | av_push (dec->stringref, newSVpvn (key, len)); |
660 | |
671 | |
661 | hv_store (hv, key, len, decode_sv (dec), 0); |
672 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
673 | |
|
|
674 | return; |
662 | } |
675 | } |
663 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
676 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
664 | { |
677 | { |
665 | I32 len = decode_uint (dec); |
678 | I32 len = decode_uint (dec); |
666 | char *key = (char *)dec->cur; |
679 | char *key = (char *)dec->cur; |
667 | |
680 | |
668 | dec->cur += len; |
681 | dec->cur += len; |
669 | |
682 | |
670 | if (ecb_expect_false (dec->stringref)) |
683 | if (ecb_expect_false (dec->stringref)) |
671 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
684 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
672 | |
685 | |
673 | hv_store (hv, key, -len, decode_sv (dec), 0); |
686 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
687 | |
|
|
688 | return; |
674 | } |
689 | } |
675 | else |
690 | |
676 | { |
|
|
677 | SV *k = decode_sv (dec); |
691 | SV *k = decode_sv (dec); |
678 | SV *v = decode_sv (dec); |
692 | SV *v = decode_sv (dec); |
679 | |
693 | |
680 | hv_store_ent (hv, k, v, 0); |
694 | hv_store_ent (hv, k, v, 0); |
681 | SvREFCNT_dec (k); |
695 | SvREFCNT_dec (k); |
682 | } |
|
|
683 | } |
696 | } |
684 | |
697 | |
685 | static SV * |
698 | static SV * |
686 | decode_hv (dec_t *dec) |
699 | decode_hv (dec_t *dec) |
687 | { |
700 | { |
… | |
… | |
758 | } |
771 | } |
759 | |
772 | |
760 | if (utf8) |
773 | if (utf8) |
761 | SvUTF8_on (sv); |
774 | SvUTF8_on (sv); |
762 | |
775 | |
763 | if (ecb_expect_false (dec->stringref)) |
776 | if (ecb_expect_false (dec->stringref) |
|
|
777 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
764 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
778 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
765 | |
779 | |
766 | return sv; |
780 | return sv; |
767 | |
781 | |
768 | fail: |
782 | fail: |
… | |
… | |
1090 | void shrink (CBOR *self, int enable = 1) |
1104 | void shrink (CBOR *self, int enable = 1) |
1091 | ALIAS: |
1105 | ALIAS: |
1092 | shrink = F_SHRINK |
1106 | shrink = F_SHRINK |
1093 | allow_unknown = F_ALLOW_UNKNOWN |
1107 | allow_unknown = F_ALLOW_UNKNOWN |
1094 | allow_sharing = F_ALLOW_SHARING |
1108 | allow_sharing = F_ALLOW_SHARING |
1095 | dedup_keys = F_DEDUP_KEYS |
|
|
1096 | dedup_strings = F_DEDUP_STRINGS |
1109 | dedup_strings = F_DEDUP_STRINGS |
1097 | PPCODE: |
1110 | PPCODE: |
1098 | { |
1111 | { |
1099 | if (enable) |
1112 | if (enable) |
1100 | self->flags |= ix; |
1113 | self->flags |= ix; |
… | |
… | |
1107 | void get_shrink (CBOR *self) |
1120 | void get_shrink (CBOR *self) |
1108 | ALIAS: |
1121 | ALIAS: |
1109 | get_shrink = F_SHRINK |
1122 | get_shrink = F_SHRINK |
1110 | get_allow_unknown = F_ALLOW_UNKNOWN |
1123 | get_allow_unknown = F_ALLOW_UNKNOWN |
1111 | get_allow_sharing = F_ALLOW_SHARING |
1124 | get_allow_sharing = F_ALLOW_SHARING |
1112 | get_dedup_keys = F_DEDUP_KEYS |
|
|
1113 | get_dedup_strings = F_DEDUP_STRINGS |
1125 | get_dedup_strings = F_DEDUP_STRINGS |
1114 | PPCODE: |
1126 | PPCODE: |
1115 | XPUSHs (boolSV (self->flags & ix)); |
1127 | XPUSHs (boolSV (self->flags & ix)); |
1116 | |
1128 | |
1117 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1129 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |