… | |
… | |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
20 | #endif |
20 | #endif |
21 | #ifndef HvNAMEUTF8 |
21 | #ifndef HvNAMEUTF8 |
22 | # define HvNAMEUTF8(hv) 0 |
22 | # define HvNAMEUTF8(hv) 0 |
23 | #endif |
23 | #endif |
|
|
24 | #ifndef SvREFCNT_dec_NN |
|
|
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
26 | #endif |
24 | |
27 | |
25 | // known tags |
28 | // known tags |
26 | enum cbor_tag |
29 | enum cbor_tag |
27 | { |
30 | { |
28 | // inofficial extensions (pending iana registration) |
31 | // extensions |
|
|
32 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
33 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
34 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
35 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
36 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
37 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref |
34 | CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref |
|
|
35 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
38 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
39 | |
37 | // rfc7049 |
40 | // rfc7049 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
41 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
42 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
… | |
… | |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
57 | }; |
60 | }; |
58 | |
61 | |
59 | #define F_SHRINK 0x00000001UL |
62 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
64 | #define F_ALLOW_SHARING 0x00000004UL |
62 | #define F_ALLOW_STRINGREF 0x00000008UL //TODO |
65 | #define F_PACK_STRINGS 0x00000008UL |
63 | |
66 | |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
65 | |
68 | |
66 | #define SB do { |
69 | #define SB do { |
67 | #define SE } while (0) |
70 | #define SE } while (0) |
… | |
… | |
138 | { |
141 | { |
139 | return idx > 23 |
142 | return idx > 23 |
140 | ? idx > 0xffU |
143 | ? idx > 0xffU |
141 | ? idx > 0xffffU |
144 | ? idx > 0xffffU |
142 | ? idx > 0xffffffffU |
145 | ? idx > 0xffffffffU |
|
|
146 | ? 11 |
143 | ? 7 |
147 | : 7 |
144 | : 6 |
|
|
145 | : 5 |
148 | : 5 |
146 | : 4 |
149 | : 4 |
147 | : 3; |
150 | : 3; |
148 | } |
151 | } |
149 | |
152 | |
… | |
… | |
186 | static void |
189 | static void |
187 | encode_uint (enc_t *enc, int major, UV len) |
190 | encode_uint (enc_t *enc, int major, UV len) |
188 | { |
191 | { |
189 | need (enc, 9); |
192 | need (enc, 9); |
190 | |
193 | |
191 | if (len < 24) |
194 | if (ecb_expect_true (len < 24)) |
192 | *enc->cur++ = major | len; |
195 | *enc->cur++ = major | len; |
193 | else if (len <= 0xff) |
196 | else if (ecb_expect_true (len <= 0xff)) |
194 | { |
197 | { |
195 | *enc->cur++ = major | 24; |
198 | *enc->cur++ = major | 24; |
196 | *enc->cur++ = len; |
199 | *enc->cur++ = len; |
197 | } |
200 | } |
198 | else if (len <= 0xffff) |
201 | else if (len <= 0xffff) |
… | |
… | |
227 | encode_tag (enc_t *enc, UV tag) |
230 | encode_tag (enc_t *enc, UV tag) |
228 | { |
231 | { |
229 | encode_uint (enc, 0xc0, tag); |
232 | encode_uint (enc, 0xc0, tag); |
230 | } |
233 | } |
231 | |
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 | |
232 | static void |
244 | static void |
233 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
245 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
234 | { |
246 | { |
235 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
247 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
236 | { |
248 | { |
237 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
249 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
238 | |
250 | |
239 | if (SvOK (*svp)) |
251 | if (SvOK (*svp)) |
240 | { |
252 | { |
… | |
… | |
249 | sv_setuv (*svp, enc->stringref_idx); |
261 | sv_setuv (*svp, enc->stringref_idx); |
250 | ++enc->stringref_idx; |
262 | ++enc->stringref_idx; |
251 | } |
263 | } |
252 | } |
264 | } |
253 | |
265 | |
254 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
266 | encode_str (enc, utf8, str, len); |
255 | need (enc, len); |
|
|
256 | memcpy (enc->cur, str, len); |
|
|
257 | enc->cur += len; |
|
|
258 | } |
267 | } |
259 | |
268 | |
260 | static void encode_sv (enc_t *enc, SV *sv); |
269 | static void encode_sv (enc_t *enc, SV *sv); |
261 | |
270 | |
262 | static void |
271 | static void |
… | |
… | |
301 | while ((he = hv_iternext (hv))) |
310 | while ((he = hv_iternext (hv))) |
302 | { |
311 | { |
303 | if (HeKLEN (he) == HEf_SVKEY) |
312 | if (HeKLEN (he) == HEf_SVKEY) |
304 | encode_sv (enc, HeSVKEY (he)); |
313 | encode_sv (enc, HeSVKEY (he)); |
305 | else |
314 | else |
306 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
315 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
307 | |
316 | |
308 | 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)); |
309 | } |
318 | } |
310 | |
319 | |
311 | if (mg) |
320 | if (mg) |
… | |
… | |
412 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
421 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
413 | 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)); |
414 | |
423 | |
415 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
424 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
416 | encode_uint (enc, 0x80, count + 1); |
425 | encode_uint (enc, 0x80, count + 1); |
417 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
426 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
418 | |
427 | |
419 | while (count) |
428 | while (count) |
420 | encode_sv (enc, SP[1 - count--]); |
429 | encode_sv (enc, SP[1 - count--]); |
421 | |
430 | |
422 | PUTBACK; |
431 | PUTBACK; |
… | |
… | |
481 | |
490 | |
482 | if (SvPOKp (sv)) |
491 | if (SvPOKp (sv)) |
483 | { |
492 | { |
484 | STRLEN len; |
493 | STRLEN len; |
485 | char *str = SvPV (sv, len); |
494 | char *str = SvPV (sv, len); |
486 | encode_str (enc, SvUTF8 (sv), str, len); |
495 | encode_strref (enc, SvUTF8 (sv), str, len); |
487 | } |
496 | } |
488 | else if (SvNOKp (sv)) |
497 | else if (SvNOKp (sv)) |
489 | encode_nv (enc, sv); |
498 | encode_nv (enc, sv); |
490 | else if (SvIOKp (sv)) |
499 | else if (SvIOKp (sv)) |
491 | { |
500 | { |
… | |
… | |
517 | enc.cur = SvPVX (enc.sv); |
526 | enc.cur = SvPVX (enc.sv); |
518 | enc.end = SvEND (enc.sv); |
527 | enc.end = SvEND (enc.sv); |
519 | |
528 | |
520 | SvPOK_only (enc.sv); |
529 | SvPOK_only (enc.sv); |
521 | |
530 | |
522 | if (cbor->flags & F_ALLOW_STRINGREF) |
531 | if (cbor->flags & F_PACK_STRINGS) |
523 | { |
532 | { |
524 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
533 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
525 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
534 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
526 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
535 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
527 | } |
536 | } |
… | |
… | |
1136 | void shrink (CBOR *self, int enable = 1) |
1145 | void shrink (CBOR *self, int enable = 1) |
1137 | ALIAS: |
1146 | ALIAS: |
1138 | shrink = F_SHRINK |
1147 | shrink = F_SHRINK |
1139 | allow_unknown = F_ALLOW_UNKNOWN |
1148 | allow_unknown = F_ALLOW_UNKNOWN |
1140 | allow_sharing = F_ALLOW_SHARING |
1149 | allow_sharing = F_ALLOW_SHARING |
1141 | allow_stringref = F_ALLOW_STRINGREF |
1150 | pack_strings = F_PACK_STRINGS |
1142 | PPCODE: |
1151 | PPCODE: |
1143 | { |
1152 | { |
1144 | if (enable) |
1153 | if (enable) |
1145 | self->flags |= ix; |
1154 | self->flags |= ix; |
1146 | else |
1155 | else |
… | |
… | |
1152 | void get_shrink (CBOR *self) |
1161 | void get_shrink (CBOR *self) |
1153 | ALIAS: |
1162 | ALIAS: |
1154 | get_shrink = F_SHRINK |
1163 | get_shrink = F_SHRINK |
1155 | get_allow_unknown = F_ALLOW_UNKNOWN |
1164 | get_allow_unknown = F_ALLOW_UNKNOWN |
1156 | get_allow_sharing = F_ALLOW_SHARING |
1165 | get_allow_sharing = F_ALLOW_SHARING |
1157 | get_allow_stringref = F_ALLOW_STRINGREF |
1166 | get_pack_strings = F_PACK_STRINGS |
1158 | PPCODE: |
1167 | PPCODE: |
1159 | XPUSHs (boolSV (self->flags & ix)); |
1168 | XPUSHs (boolSV (self->flags & ix)); |
1160 | |
1169 | |
1161 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1170 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1162 | PPCODE: |
1171 | PPCODE: |