… | |
… | |
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 |
|
|
27 | |
|
|
28 | // known major and minor types |
|
|
29 | enum cbor_type |
|
|
30 | { |
|
|
31 | MAJOR_SHIFT = 5, |
|
|
32 | MINOR_MASK = 0x1f, |
|
|
33 | |
|
|
34 | MAJOR_POS_INT = 0 << MAJOR_SHIFT, |
|
|
35 | MAJOR_NEG_INT = 1 << MAJOR_SHIFT, |
|
|
36 | MAJOR_BYTES = 2 << MAJOR_SHIFT, |
|
|
37 | MAJOR_TEXT = 3 << MAJOR_SHIFT, |
|
|
38 | MAJOR_ARRAY = 4 << MAJOR_SHIFT, |
|
|
39 | MAJOR_MAP = 5 << MAJOR_SHIFT, |
|
|
40 | MAJOR_TAG = 6 << MAJOR_SHIFT, |
|
|
41 | MAJOR_MISC = 7 << MAJOR_SHIFT, |
|
|
42 | |
|
|
43 | // INT/STRING/ARRAY/MAP subtypes |
|
|
44 | LENGTH_EXT1 = 24, |
|
|
45 | LENGTH_EXT2 = 25, |
|
|
46 | LENGTH_EXT4 = 26, |
|
|
47 | LENGTH_EXT8 = 27, |
|
|
48 | |
|
|
49 | // SIMPLE types (effectively MISC subtypes) |
|
|
50 | SIMPLE_FALSE = 20, |
|
|
51 | SIMPLE_TRUE = 21, |
|
|
52 | SIMPLE_NULL = 22, |
|
|
53 | SIMPLE_UNDEF = 23, |
|
|
54 | |
|
|
55 | // MISC subtype (unused) |
|
|
56 | MISC_EXT1 = 24, |
|
|
57 | MISC_FLOAT16 = 25, |
|
|
58 | MISC_FLOAT32 = 26, |
|
|
59 | MISC_FLOAT64 = 27, |
|
|
60 | |
|
|
61 | // BYTES/TEXT/ARRAY/MAP |
|
|
62 | MINOR_INDEF = 31, |
|
|
63 | }; |
24 | |
64 | |
25 | // known tags |
65 | // known tags |
26 | enum cbor_tag |
66 | enum cbor_tag |
27 | { |
67 | { |
28 | // inofficial extensions (pending iana registration) |
68 | // extensions |
|
|
69 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
70 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
71 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
72 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
73 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
74 | 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 |
75 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
76 | |
37 | // rfc7049 |
77 | // rfc7049 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
78 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
79 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
40 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
80 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
41 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
81 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
42 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
82 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
43 | CBOR_TAG_BIGFLOAT = 5, // array |
83 | CBOR_TAG_BIGFLOAT = 5, // array |
44 | |
84 | |
45 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
85 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
46 | CBOR_TAG_CONV_B64 = 22, // base64, any |
86 | CBOR_TAG_CONV_B64 = 22, // base64, any |
47 | CBOR_TAG_CONV_HEX = 23, // base16, any |
87 | CBOR_TAG_CONV_HEX = 23, // base16, any |
48 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
88 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
49 | |
89 | |
50 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
90 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
51 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
91 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
52 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
92 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
53 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
93 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
94 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
55 | |
95 | |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
96 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
57 | }; |
97 | }; |
58 | |
98 | |
59 | #define F_SHRINK 0x00000001UL |
99 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
101 | #define F_ALLOW_SHARING 0x00000004UL |
|
|
102 | #define F_ALLOW_CYCLES 0x00000008UL |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
103 | #define F_PACK_STRINGS 0x00000010UL |
63 | |
104 | |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
105 | #define INIT_SIZE 32 // initial scalar size to be allocated |
65 | |
106 | |
66 | #define SB do { |
107 | #define SB do { |
67 | #define SE } while (0) |
108 | #define SE } while (0) |
… | |
… | |
79 | # define CBOR_SLOW 0 |
120 | # define CBOR_SLOW 0 |
80 | # define CBOR_STASH cbor_stash |
121 | # define CBOR_STASH cbor_stash |
81 | #endif |
122 | #endif |
82 | |
123 | |
83 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
124 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
84 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
125 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
85 | |
126 | |
86 | typedef struct { |
127 | typedef struct { |
87 | U32 flags; |
128 | U32 flags; |
88 | U32 max_depth; |
129 | U32 max_depth; |
89 | STRLEN max_size; |
130 | STRLEN max_size; |
|
|
131 | SV *filter; |
90 | } CBOR; |
132 | } CBOR; |
91 | |
133 | |
92 | ecb_inline void |
134 | ecb_inline void |
93 | cbor_init (CBOR *cbor) |
135 | cbor_init (CBOR *cbor) |
94 | { |
136 | { |
95 | Zero (cbor, 1, CBOR); |
137 | Zero (cbor, 1, CBOR); |
96 | cbor->max_depth = 512; |
138 | cbor->max_depth = 512; |
|
|
139 | } |
|
|
140 | |
|
|
141 | ecb_inline void |
|
|
142 | cbor_free (CBOR *cbor) |
|
|
143 | { |
|
|
144 | SvREFCNT_dec (cbor->filter); |
97 | } |
145 | } |
98 | |
146 | |
99 | ///////////////////////////////////////////////////////////////////////////// |
147 | ///////////////////////////////////////////////////////////////////////////// |
100 | // utility functions |
148 | // utility functions |
101 | |
149 | |
… | |
… | |
131 | { |
179 | { |
132 | return idx > 23 |
180 | return idx > 23 |
133 | ? idx > 0xffU |
181 | ? idx > 0xffU |
134 | ? idx > 0xffffU |
182 | ? idx > 0xffffU |
135 | ? idx > 0xffffffffU |
183 | ? idx > 0xffffffffU |
|
|
184 | ? 11 |
136 | ? 7 |
185 | : 7 |
137 | : 6 |
|
|
138 | : 5 |
186 | : 5 |
139 | : 4 |
187 | : 4 |
140 | : 3; |
188 | : 3; |
141 | } |
189 | } |
142 | |
190 | |
… | |
… | |
179 | static void |
227 | static void |
180 | encode_uint (enc_t *enc, int major, UV len) |
228 | encode_uint (enc_t *enc, int major, UV len) |
181 | { |
229 | { |
182 | need (enc, 9); |
230 | need (enc, 9); |
183 | |
231 | |
184 | if (len < 24) |
232 | if (ecb_expect_true (len < LENGTH_EXT1)) |
185 | *enc->cur++ = major | len; |
233 | *enc->cur++ = major | len; |
186 | else if (len <= 0xff) |
234 | else if (ecb_expect_true (len <= 0xffU)) |
187 | { |
235 | { |
188 | *enc->cur++ = major | 24; |
236 | *enc->cur++ = major | LENGTH_EXT1; |
189 | *enc->cur++ = len; |
237 | *enc->cur++ = len; |
190 | } |
238 | } |
191 | else if (len <= 0xffff) |
239 | else if (len <= 0xffffU) |
192 | { |
240 | { |
193 | *enc->cur++ = major | 25; |
241 | *enc->cur++ = major | LENGTH_EXT2; |
194 | *enc->cur++ = len >> 8; |
242 | *enc->cur++ = len >> 8; |
195 | *enc->cur++ = len; |
243 | *enc->cur++ = len; |
196 | } |
244 | } |
197 | else if (len <= 0xffffffff) |
245 | else if (len <= 0xffffffffU) |
198 | { |
246 | { |
199 | *enc->cur++ = major | 26; |
247 | *enc->cur++ = major | LENGTH_EXT4; |
200 | *enc->cur++ = len >> 24; |
248 | *enc->cur++ = len >> 24; |
201 | *enc->cur++ = len >> 16; |
249 | *enc->cur++ = len >> 16; |
202 | *enc->cur++ = len >> 8; |
250 | *enc->cur++ = len >> 8; |
203 | *enc->cur++ = len; |
251 | *enc->cur++ = len; |
204 | } |
252 | } |
205 | else |
253 | else |
206 | { |
254 | { |
207 | *enc->cur++ = major | 27; |
255 | *enc->cur++ = major | LENGTH_EXT8; |
208 | *enc->cur++ = len >> 56; |
256 | *enc->cur++ = len >> 56; |
209 | *enc->cur++ = len >> 48; |
257 | *enc->cur++ = len >> 48; |
210 | *enc->cur++ = len >> 40; |
258 | *enc->cur++ = len >> 40; |
211 | *enc->cur++ = len >> 32; |
259 | *enc->cur++ = len >> 32; |
212 | *enc->cur++ = len >> 24; |
260 | *enc->cur++ = len >> 24; |
… | |
… | |
217 | } |
265 | } |
218 | |
266 | |
219 | ecb_inline void |
267 | ecb_inline void |
220 | encode_tag (enc_t *enc, UV tag) |
268 | encode_tag (enc_t *enc, UV tag) |
221 | { |
269 | { |
222 | encode_uint (enc, 0xc0, tag); |
270 | encode_uint (enc, MAJOR_TAG, tag); |
|
|
271 | } |
|
|
272 | |
|
|
273 | ecb_inline void |
|
|
274 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
275 | { |
|
|
276 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
|
|
277 | need (enc, len); |
|
|
278 | memcpy (enc->cur, str, len); |
|
|
279 | enc->cur += len; |
223 | } |
280 | } |
224 | |
281 | |
225 | static void |
282 | static void |
226 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
283 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
227 | { |
284 | { |
228 | if (ecb_expect_false (enc->cbor.flags & F_DEDUP_STRINGS)) |
285 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
229 | { |
286 | { |
230 | SV **svp = hv_fetch (enc->stringref[utf8], str, len, 1); |
287 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
231 | |
288 | |
232 | if (SvOK (*svp)) |
289 | if (SvOK (*svp)) |
233 | { |
290 | { |
234 | // already registered, use stringref |
291 | // already registered, use stringref |
235 | encode_tag (enc, CBOR_TAG_STRINGREF); |
292 | encode_tag (enc, CBOR_TAG_STRINGREF); |
236 | encode_uint (enc, 0x00, SvUV (*svp)); |
293 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
237 | return; |
294 | return; |
238 | } |
295 | } |
239 | else if (len >= minimum_string_length (enc->stringref_idx)) |
296 | else if (len >= minimum_string_length (enc->stringref_idx)) |
240 | { |
297 | { |
241 | // register only |
298 | // register only |
242 | sv_setuv (*svp, enc->stringref_idx); |
299 | sv_setuv (*svp, enc->stringref_idx); |
243 | ++enc->stringref_idx; |
300 | ++enc->stringref_idx; |
244 | } |
301 | } |
245 | } |
302 | } |
246 | |
303 | |
247 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
304 | encode_str (enc, utf8, str, len); |
248 | need (enc, len); |
|
|
249 | memcpy (enc->cur, str, len); |
|
|
250 | enc->cur += len; |
|
|
251 | } |
305 | } |
252 | |
306 | |
253 | static void encode_sv (enc_t *enc, SV *sv); |
307 | static void encode_sv (enc_t *enc, SV *sv); |
254 | |
308 | |
255 | static void |
309 | static void |
… | |
… | |
260 | if (enc->depth >= enc->cbor.max_depth) |
314 | if (enc->depth >= enc->cbor.max_depth) |
261 | croak (ERR_NESTING_EXCEEDED); |
315 | croak (ERR_NESTING_EXCEEDED); |
262 | |
316 | |
263 | ++enc->depth; |
317 | ++enc->depth; |
264 | |
318 | |
265 | encode_uint (enc, 0x80, len + 1); |
319 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
266 | |
320 | |
267 | for (i = 0; i <= len; ++i) |
321 | for (i = 0; i <= len; ++i) |
268 | { |
322 | { |
269 | SV **svp = av_fetch (av, i, 0); |
323 | SV **svp = av_fetch (av, i, 0); |
270 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
324 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
271 | } |
325 | } |
272 | |
326 | |
273 | --enc->depth; |
327 | --enc->depth; |
274 | } |
328 | } |
275 | |
329 | |
276 | ecb_inline void |
|
|
277 | encode_he (enc_t *enc, HE *he) |
|
|
278 | { |
|
|
279 | } |
|
|
280 | |
|
|
281 | static void |
330 | static void |
282 | encode_hv (enc_t *enc, HV *hv) |
331 | encode_hv (enc_t *enc, HV *hv) |
283 | { |
332 | { |
284 | HE *he; |
333 | HE *he; |
285 | |
334 | |
… | |
… | |
290 | |
339 | |
291 | int pairs = hv_iterinit (hv); |
340 | int pairs = hv_iterinit (hv); |
292 | int mg = SvMAGICAL (hv); |
341 | int mg = SvMAGICAL (hv); |
293 | |
342 | |
294 | if (mg) |
343 | if (mg) |
295 | encode_ch (enc, 0xa0 | 31); |
344 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
296 | else |
345 | else |
297 | encode_uint (enc, 0xa0, pairs); |
346 | encode_uint (enc, MAJOR_MAP, pairs); |
298 | |
347 | |
299 | while ((he = hv_iternext (hv))) |
348 | while ((he = hv_iternext (hv))) |
300 | { |
349 | { |
301 | if (HeKLEN (he) == HEf_SVKEY) |
350 | if (HeKLEN (he) == HEf_SVKEY) |
302 | encode_sv (enc, HeSVKEY (he)); |
351 | encode_sv (enc, HeSVKEY (he)); |
303 | else |
352 | else |
304 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
353 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
305 | |
354 | |
306 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
355 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
307 | } |
356 | } |
308 | |
357 | |
309 | if (mg) |
358 | if (mg) |
310 | encode_ch (enc, 0xe0 | 31); |
359 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
311 | |
360 | |
312 | --enc->depth; |
361 | --enc->depth; |
313 | } |
362 | } |
314 | |
363 | |
315 | // encode objects, arrays and special \0=false and \1=true values. |
364 | // encode objects, arrays and special \0=false and \1=true values. |
316 | static void |
365 | static void |
317 | encode_rv (enc_t *enc, SV *sv) |
366 | encode_rv (enc_t *enc, SV *sv) |
318 | { |
367 | { |
319 | SvGETMAGIC (sv); |
368 | SvGETMAGIC (sv); |
320 | |
|
|
321 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
322 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
323 | { |
|
|
324 | if (!enc->shareable) |
|
|
325 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
326 | |
|
|
327 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
328 | |
|
|
329 | if (SvOK (*svp)) |
|
|
330 | { |
|
|
331 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
332 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
333 | return; |
|
|
334 | } |
|
|
335 | else |
|
|
336 | { |
|
|
337 | sv_setuv (*svp, enc->shareable_idx); |
|
|
338 | ++enc->shareable_idx; |
|
|
339 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
340 | } |
|
|
341 | } |
|
|
342 | |
369 | |
343 | svtype svt = SvTYPE (sv); |
370 | svtype svt = SvTYPE (sv); |
344 | |
371 | |
345 | if (ecb_expect_false (SvOBJECT (sv))) |
372 | if (ecb_expect_false (SvOBJECT (sv))) |
346 | { |
373 | { |
… | |
… | |
353 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
380 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
354 | ? cbor_tagged_stash |
381 | ? cbor_tagged_stash |
355 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
382 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
356 | |
383 | |
357 | HV *stash = SvSTASH (sv); |
384 | HV *stash = SvSTASH (sv); |
358 | GV *method; |
|
|
359 | |
385 | |
360 | if (stash == boolean_stash) |
386 | if (stash == boolean_stash) |
361 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
387 | { |
|
|
388 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
389 | return; |
|
|
390 | } |
362 | else if (stash == error_stash) |
391 | else if (stash == error_stash) |
363 | encode_ch (enc, 0xe0 | 23); |
392 | { |
|
|
393 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
394 | return; |
|
|
395 | } |
364 | else if (stash == tagged_stash) |
396 | else if (stash == tagged_stash) |
365 | { |
397 | { |
366 | if (svt != SVt_PVAV) |
398 | if (svt != SVt_PVAV) |
367 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
399 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
368 | |
400 | |
369 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
401 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
370 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
402 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
403 | |
|
|
404 | return; |
|
|
405 | } |
|
|
406 | } |
|
|
407 | |
|
|
408 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
409 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
410 | { |
|
|
411 | if (!enc->shareable) |
|
|
412 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
413 | |
|
|
414 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
415 | |
|
|
416 | if (SvOK (*svp)) |
371 | } |
417 | { |
|
|
418 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
419 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
420 | return; |
|
|
421 | } |
|
|
422 | else |
|
|
423 | { |
|
|
424 | sv_setuv (*svp, enc->shareable_idx); |
|
|
425 | ++enc->shareable_idx; |
|
|
426 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
427 | } |
|
|
428 | } |
|
|
429 | |
|
|
430 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
431 | { |
|
|
432 | HV *stash = SvSTASH (sv); |
|
|
433 | GV *method; |
|
|
434 | |
372 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
435 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
373 | { |
436 | { |
374 | dSP; |
437 | dSP; |
375 | |
438 | |
376 | ENTER; SAVETMPS; PUSHMARK (SP); |
439 | ENTER; SAVETMPS; PUSHMARK (SP); |
377 | // we re-bless the reference to get overload and other niceties right |
440 | // we re-bless the reference to get overload and other niceties right |
… | |
… | |
409 | // catch this surprisingly common error |
472 | // catch this surprisingly common error |
410 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
473 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
411 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
474 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
412 | |
475 | |
413 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
476 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
414 | encode_uint (enc, 0x80, count + 1); |
477 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
415 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
478 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
416 | |
479 | |
417 | while (count) |
480 | while (count) |
418 | encode_sv (enc, SP[1 - count--]); |
481 | encode_sv (enc, SP[1 - count--]); |
419 | |
482 | |
420 | PUTBACK; |
483 | PUTBACK; |
… | |
… | |
441 | { |
504 | { |
442 | double nv = SvNVX (sv); |
505 | double nv = SvNVX (sv); |
443 | |
506 | |
444 | need (enc, 9); |
507 | need (enc, 9); |
445 | |
508 | |
446 | if (ecb_expect_false (nv == (U32)nv)) |
509 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
447 | encode_uint (enc, 0x00, (U32)nv); |
510 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
448 | //TODO: maybe I32? |
511 | //TODO: maybe I32? |
449 | else if (ecb_expect_false (nv == (float)nv)) |
512 | else if (ecb_expect_false (nv == (float)nv)) |
450 | { |
513 | { |
451 | uint32_t fp = ecb_float_to_binary32 (nv); |
514 | uint32_t fp = ecb_float_to_binary32 (nv); |
452 | |
515 | |
453 | *enc->cur++ = 0xe0 | 26; |
516 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
454 | |
517 | |
455 | if (!ecb_big_endian ()) |
518 | if (!ecb_big_endian ()) |
456 | fp = ecb_bswap32 (fp); |
519 | fp = ecb_bswap32 (fp); |
457 | |
520 | |
458 | memcpy (enc->cur, &fp, 4); |
521 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
460 | } |
523 | } |
461 | else |
524 | else |
462 | { |
525 | { |
463 | uint64_t fp = ecb_double_to_binary64 (nv); |
526 | uint64_t fp = ecb_double_to_binary64 (nv); |
464 | |
527 | |
465 | *enc->cur++ = 0xe0 | 27; |
528 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
466 | |
529 | |
467 | if (!ecb_big_endian ()) |
530 | if (!ecb_big_endian ()) |
468 | fp = ecb_bswap64 (fp); |
531 | fp = ecb_bswap64 (fp); |
469 | |
532 | |
470 | memcpy (enc->cur, &fp, 8); |
533 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
479 | |
542 | |
480 | if (SvPOKp (sv)) |
543 | if (SvPOKp (sv)) |
481 | { |
544 | { |
482 | STRLEN len; |
545 | STRLEN len; |
483 | char *str = SvPV (sv, len); |
546 | char *str = SvPV (sv, len); |
484 | encode_str (enc, SvUTF8 (sv), str, len); |
547 | encode_strref (enc, SvUTF8 (sv), str, len); |
485 | } |
548 | } |
486 | else if (SvNOKp (sv)) |
549 | else if (SvNOKp (sv)) |
487 | encode_nv (enc, sv); |
550 | encode_nv (enc, sv); |
488 | else if (SvIOKp (sv)) |
551 | else if (SvIOKp (sv)) |
489 | { |
552 | { |
490 | if (SvIsUV (sv)) |
553 | if (SvIsUV (sv)) |
491 | encode_uint (enc, 0x00, SvUVX (sv)); |
554 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
492 | else if (SvIVX (sv) >= 0) |
555 | else if (SvIVX (sv) >= 0) |
493 | encode_uint (enc, 0x00, SvIVX (sv)); |
556 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
494 | else |
557 | else |
495 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
558 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
496 | } |
559 | } |
497 | else if (SvROK (sv)) |
560 | else if (SvROK (sv)) |
498 | encode_rv (enc, SvRV (sv)); |
561 | encode_rv (enc, SvRV (sv)); |
499 | else if (!SvOK (sv)) |
562 | else if (!SvOK (sv)) |
500 | encode_ch (enc, 0xe0 | 22); |
563 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
501 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
564 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
502 | encode_ch (enc, 0xe0 | 23); |
565 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
503 | else |
566 | else |
504 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
567 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
505 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
568 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
506 | } |
569 | } |
507 | |
570 | |
… | |
… | |
515 | enc.cur = SvPVX (enc.sv); |
578 | enc.cur = SvPVX (enc.sv); |
516 | enc.end = SvEND (enc.sv); |
579 | enc.end = SvEND (enc.sv); |
517 | |
580 | |
518 | SvPOK_only (enc.sv); |
581 | SvPOK_only (enc.sv); |
519 | |
582 | |
520 | if (cbor->flags & F_DEDUP_STRINGS) |
583 | if (cbor->flags & F_PACK_STRINGS) |
521 | { |
584 | { |
522 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
585 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
523 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
586 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
524 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
587 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
525 | } |
588 | } |
… | |
… | |
547 | CBOR cbor; |
610 | CBOR cbor; |
548 | U32 depth; // recursion depth |
611 | U32 depth; // recursion depth |
549 | U32 maxdepth; // recursion depth limit |
612 | U32 maxdepth; // recursion depth limit |
550 | AV *shareable; |
613 | AV *shareable; |
551 | AV *stringref; |
614 | AV *stringref; |
|
|
615 | SV *decode_tagged; |
552 | } dec_t; |
616 | } dec_t; |
553 | |
617 | |
554 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
618 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
555 | |
619 | |
556 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
620 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
559 | #define DEC_DEC_DEPTH --dec->depth |
623 | #define DEC_DEC_DEPTH --dec->depth |
560 | |
624 | |
561 | static UV |
625 | static UV |
562 | decode_uint (dec_t *dec) |
626 | decode_uint (dec_t *dec) |
563 | { |
627 | { |
564 | switch (*dec->cur & 31) |
628 | U8 m = *dec->cur & MINOR_MASK; |
565 | { |
629 | ++dec->cur; |
566 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
|
|
567 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
|
|
568 | case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
|
|
569 | return *dec->cur++ & 31; |
|
|
570 | |
630 | |
571 | case 24: |
631 | if (ecb_expect_true (m < LENGTH_EXT1)) |
|
|
632 | return m; |
|
|
633 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
|
|
634 | { |
572 | WANT (2); |
635 | WANT (1); |
573 | dec->cur += 2; |
636 | dec->cur += 1; |
574 | return dec->cur[-1]; |
637 | return dec->cur[-1]; |
575 | |
638 | } |
576 | case 25: |
639 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
640 | { |
577 | WANT (3); |
641 | WANT (2); |
578 | dec->cur += 3; |
642 | dec->cur += 2; |
579 | return (((UV)dec->cur[-2]) << 8) |
643 | return (((UV)dec->cur[-2]) << 8) |
580 | | ((UV)dec->cur[-1]); |
644 | | ((UV)dec->cur[-1]); |
581 | |
645 | } |
582 | case 26: |
646 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
647 | { |
583 | WANT (5); |
648 | WANT (4); |
584 | dec->cur += 5; |
649 | dec->cur += 4; |
585 | return (((UV)dec->cur[-4]) << 24) |
650 | return (((UV)dec->cur[-4]) << 24) |
586 | | (((UV)dec->cur[-3]) << 16) |
651 | | (((UV)dec->cur[-3]) << 16) |
587 | | (((UV)dec->cur[-2]) << 8) |
652 | | (((UV)dec->cur[-2]) << 8) |
588 | | ((UV)dec->cur[-1]); |
653 | | ((UV)dec->cur[-1]); |
589 | |
654 | } |
590 | case 27: |
655 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
656 | { |
591 | WANT (9); |
657 | WANT (8); |
592 | dec->cur += 9; |
658 | dec->cur += 8; |
|
|
659 | |
|
|
660 | return |
|
|
661 | #if UVSIZE < 8 |
|
|
662 | 0 |
|
|
663 | #else |
593 | return (((UV)dec->cur[-8]) << 56) |
664 | (((UV)dec->cur[-8]) << 56) |
594 | | (((UV)dec->cur[-7]) << 48) |
665 | | (((UV)dec->cur[-7]) << 48) |
595 | | (((UV)dec->cur[-6]) << 40) |
666 | | (((UV)dec->cur[-6]) << 40) |
596 | | (((UV)dec->cur[-5]) << 32) |
667 | | (((UV)dec->cur[-5]) << 32) |
|
|
668 | #endif |
597 | | (((UV)dec->cur[-4]) << 24) |
669 | | (((UV)dec->cur[-4]) << 24) |
598 | | (((UV)dec->cur[-3]) << 16) |
670 | | (((UV)dec->cur[-3]) << 16) |
599 | | (((UV)dec->cur[-2]) << 8) |
671 | | (((UV)dec->cur[-2]) << 8) |
600 | | ((UV)dec->cur[-1]); |
672 | | ((UV)dec->cur[-1]); |
601 | |
673 | } |
602 | default: |
674 | else |
603 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
675 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
604 | } |
|
|
605 | |
676 | |
606 | fail: |
677 | fail: |
607 | return 0; |
678 | return 0; |
608 | } |
679 | } |
609 | |
680 | |
… | |
… | |
614 | { |
685 | { |
615 | AV *av = newAV (); |
686 | AV *av = newAV (); |
616 | |
687 | |
617 | DEC_INC_DEPTH; |
688 | DEC_INC_DEPTH; |
618 | |
689 | |
619 | if ((*dec->cur & 31) == 31) |
690 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
620 | { |
691 | { |
621 | ++dec->cur; |
692 | ++dec->cur; |
622 | |
693 | |
623 | for (;;) |
694 | for (;;) |
624 | { |
695 | { |
625 | WANT (1); |
696 | WANT (1); |
626 | |
697 | |
627 | if (*dec->cur == (0xe0 | 31)) |
698 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
628 | { |
699 | { |
629 | ++dec->cur; |
700 | ++dec->cur; |
630 | break; |
701 | break; |
631 | } |
702 | } |
632 | |
703 | |
… | |
… | |
635 | } |
706 | } |
636 | else |
707 | else |
637 | { |
708 | { |
638 | int i, len = decode_uint (dec); |
709 | int i, len = decode_uint (dec); |
639 | |
710 | |
|
|
711 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
640 | av_fill (av, len - 1); |
712 | av_fill (av, len - 1); |
641 | |
713 | |
642 | for (i = 0; i < len; ++i) |
714 | for (i = 0; i < len; ++i) |
643 | AvARRAY (av)[i] = decode_sv (dec); |
715 | AvARRAY (av)[i] = decode_sv (dec); |
644 | } |
716 | } |
… | |
… | |
656 | decode_he (dec_t *dec, HV *hv) |
728 | decode_he (dec_t *dec, HV *hv) |
657 | { |
729 | { |
658 | // for speed reasons, we specialcase single-string |
730 | // for speed reasons, we specialcase single-string |
659 | // byte or utf-8 strings as keys, but only when !stringref |
731 | // byte or utf-8 strings as keys, but only when !stringref |
660 | |
732 | |
661 | if (expect_true (!dec->stringref)) |
733 | if (ecb_expect_true (!dec->stringref)) |
662 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
734 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
663 | { |
735 | { |
664 | I32 len = decode_uint (dec); |
736 | I32 len = decode_uint (dec); |
665 | char *key = (char *)dec->cur; |
737 | char *key = (char *)dec->cur; |
666 | |
738 | |
667 | dec->cur += len; |
739 | dec->cur += len; |
… | |
… | |
671 | |
743 | |
672 | hv_store (hv, key, len, decode_sv (dec), 0); |
744 | hv_store (hv, key, len, decode_sv (dec), 0); |
673 | |
745 | |
674 | return; |
746 | return; |
675 | } |
747 | } |
676 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
748 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
677 | { |
749 | { |
678 | I32 len = decode_uint (dec); |
750 | I32 len = decode_uint (dec); |
679 | char *key = (char *)dec->cur; |
751 | char *key = (char *)dec->cur; |
680 | |
752 | |
681 | dec->cur += len; |
753 | dec->cur += len; |
… | |
… | |
700 | { |
772 | { |
701 | HV *hv = newHV (); |
773 | HV *hv = newHV (); |
702 | |
774 | |
703 | DEC_INC_DEPTH; |
775 | DEC_INC_DEPTH; |
704 | |
776 | |
705 | if ((*dec->cur & 31) == 31) |
777 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
706 | { |
778 | { |
707 | ++dec->cur; |
779 | ++dec->cur; |
708 | |
780 | |
709 | for (;;) |
781 | for (;;) |
710 | { |
782 | { |
711 | WANT (1); |
783 | WANT (1); |
712 | |
784 | |
713 | if (*dec->cur == (0xe0 | 31)) |
785 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
714 | { |
786 | { |
715 | ++dec->cur; |
787 | ++dec->cur; |
716 | break; |
788 | break; |
717 | } |
789 | } |
718 | |
790 | |
… | |
… | |
739 | static SV * |
811 | static SV * |
740 | decode_str (dec_t *dec, int utf8) |
812 | decode_str (dec_t *dec, int utf8) |
741 | { |
813 | { |
742 | SV *sv = 0; |
814 | SV *sv = 0; |
743 | |
815 | |
744 | if ((*dec->cur & 31) == 31) |
816 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
745 | { |
817 | { |
|
|
818 | // indefinite length strings |
746 | ++dec->cur; |
819 | ++dec->cur; |
747 | |
820 | |
|
|
821 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
822 | |
748 | sv = newSVpvn ("", 0); |
823 | sv = newSVpvn ("", 0); |
749 | |
824 | |
750 | // not very fast, and certainly not robust against illegal input |
|
|
751 | for (;;) |
825 | for (;;) |
752 | { |
826 | { |
753 | WANT (1); |
827 | WANT (1); |
754 | |
828 | |
755 | if (*dec->cur == (0xe0 | 31)) |
829 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
830 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
756 | { |
831 | { |
757 | ++dec->cur; |
832 | ++dec->cur; |
758 | break; |
833 | break; |
759 | } |
834 | } |
|
|
835 | else |
|
|
836 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
760 | |
837 | |
761 | sv_catsv (sv, decode_sv (dec)); |
838 | STRLEN len = decode_uint (dec); |
|
|
839 | |
|
|
840 | WANT (len); |
|
|
841 | sv_catpvn (sv, dec->cur, len); |
|
|
842 | dec->cur += len; |
762 | } |
843 | } |
763 | } |
844 | } |
764 | else |
845 | else |
765 | { |
846 | { |
766 | STRLEN len = decode_uint (dec); |
847 | STRLEN len = decode_uint (dec); |
767 | |
848 | |
768 | WANT (len); |
849 | WANT (len); |
769 | sv = newSVpvn (dec->cur, len); |
850 | sv = newSVpvn (dec->cur, len); |
770 | dec->cur += len; |
851 | dec->cur += len; |
|
|
852 | |
|
|
853 | if (ecb_expect_false (dec->stringref) |
|
|
854 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
855 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
771 | } |
856 | } |
772 | |
857 | |
773 | if (utf8) |
858 | if (utf8) |
774 | SvUTF8_on (sv); |
859 | SvUTF8_on (sv); |
775 | |
|
|
776 | if (ecb_expect_false (dec->stringref) |
|
|
777 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
778 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
|
|
779 | |
860 | |
780 | return sv; |
861 | return sv; |
781 | |
862 | |
782 | fail: |
863 | fail: |
783 | SvREFCNT_dec (sv); |
864 | SvREFCNT_dec (sv); |
… | |
… | |
815 | } |
896 | } |
816 | break; |
897 | break; |
817 | |
898 | |
818 | case CBOR_TAG_STRINGREF: |
899 | case CBOR_TAG_STRINGREF: |
819 | { |
900 | { |
820 | if ((*dec->cur >> 5) != 0) |
901 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
821 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
902 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
822 | |
903 | |
823 | UV idx = decode_uint (dec); |
904 | UV idx = decode_uint (dec); |
824 | |
905 | |
825 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
906 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
… | |
… | |
832 | case CBOR_TAG_VALUE_SHAREABLE: |
913 | case CBOR_TAG_VALUE_SHAREABLE: |
833 | { |
914 | { |
834 | if (ecb_expect_false (!dec->shareable)) |
915 | if (ecb_expect_false (!dec->shareable)) |
835 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
916 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
836 | |
917 | |
|
|
918 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
919 | { |
837 | sv = newSV (0); |
920 | sv = newSV (0); |
838 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
921 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
839 | |
922 | |
840 | SV *osv = decode_sv (dec); |
923 | SV *osv = decode_sv (dec); |
841 | sv_setsv (sv, osv); |
924 | sv_setsv (sv, osv); |
842 | SvREFCNT_dec_NN (osv); |
925 | SvREFCNT_dec_NN (osv); |
|
|
926 | } |
|
|
927 | else |
|
|
928 | { |
|
|
929 | av_push (dec->shareable, &PL_sv_undef); |
|
|
930 | int idx = AvFILLp (dec->shareable); |
|
|
931 | sv = decode_sv (dec); |
|
|
932 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
933 | } |
843 | } |
934 | } |
844 | break; |
935 | break; |
845 | |
936 | |
846 | case CBOR_TAG_VALUE_SHAREDREF: |
937 | case CBOR_TAG_VALUE_SHAREDREF: |
847 | { |
938 | { |
848 | if ((*dec->cur >> 5) != 0) |
939 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
849 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
940 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
850 | |
941 | |
851 | UV idx = decode_uint (dec); |
942 | UV idx = decode_uint (dec); |
852 | |
943 | |
853 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
944 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
854 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
945 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
855 | |
946 | |
856 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
947 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
948 | |
|
|
949 | if (sv == &PL_sv_undef) |
|
|
950 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
857 | } |
951 | } |
858 | break; |
952 | break; |
859 | |
953 | |
860 | case CBOR_TAG_PERL_OBJECT: |
954 | case CBOR_TAG_PERL_OBJECT: |
861 | { |
955 | { |
… | |
… | |
910 | |
1004 | |
911 | default: |
1005 | default: |
912 | { |
1006 | { |
913 | sv = decode_sv (dec); |
1007 | sv = decode_sv (dec); |
914 | |
1008 | |
|
|
1009 | dSP; |
|
|
1010 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1011 | EXTEND (SP, 2); |
|
|
1012 | PUSHs (newSVuv (tag)); |
|
|
1013 | PUSHs (sv); |
|
|
1014 | |
|
|
1015 | PUTBACK; |
|
|
1016 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
1017 | SPAGAIN; |
|
|
1018 | |
|
|
1019 | if (SvTRUE (ERRSV)) |
|
|
1020 | { |
|
|
1021 | FREETMPS; LEAVE; |
|
|
1022 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
1023 | } |
|
|
1024 | |
|
|
1025 | if (count) |
|
|
1026 | { |
|
|
1027 | SvREFCNT_dec (sv); |
|
|
1028 | sv = SvREFCNT_inc (POPs); |
|
|
1029 | } |
|
|
1030 | else |
|
|
1031 | { |
915 | AV *av = newAV (); |
1032 | AV *av = newAV (); |
916 | av_push (av, newSVuv (tag)); |
1033 | av_push (av, newSVuv (tag)); |
917 | av_push (av, sv); |
1034 | av_push (av, sv); |
918 | |
1035 | |
919 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1036 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
920 | ? cbor_tagged_stash |
1037 | ? cbor_tagged_stash |
921 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1038 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
922 | |
|
|
923 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
1039 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
1040 | } |
|
|
1041 | |
|
|
1042 | PUTBACK; |
|
|
1043 | |
|
|
1044 | FREETMPS; LEAVE; |
924 | } |
1045 | } |
925 | break; |
1046 | break; |
926 | } |
1047 | } |
927 | |
1048 | |
928 | return sv; |
1049 | return sv; |
… | |
… | |
935 | static SV * |
1056 | static SV * |
936 | decode_sv (dec_t *dec) |
1057 | decode_sv (dec_t *dec) |
937 | { |
1058 | { |
938 | WANT (1); |
1059 | WANT (1); |
939 | |
1060 | |
940 | switch (*dec->cur >> 5) |
1061 | switch (*dec->cur >> MAJOR_SHIFT) |
941 | { |
1062 | { |
942 | case 0: // unsigned int |
1063 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
943 | return newSVuv (decode_uint (dec)); |
1064 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
944 | case 1: // negative int |
1065 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
945 | return newSViv (-1 - (IV)decode_uint (dec)); |
1066 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
946 | case 2: // octet string |
1067 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
947 | return decode_str (dec, 0); |
1068 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
948 | case 3: // utf-8 string |
1069 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
949 | return decode_str (dec, 1); |
1070 | |
950 | case 4: // array |
1071 | case MAJOR_MISC >> MAJOR_SHIFT: |
951 | return decode_av (dec); |
|
|
952 | case 5: // map |
|
|
953 | return decode_hv (dec); |
|
|
954 | case 6: // tag |
|
|
955 | return decode_tagged (dec); |
|
|
956 | case 7: // misc |
|
|
957 | switch (*dec->cur++ & 31) |
1072 | switch (*dec->cur++ & MINOR_MASK) |
958 | { |
1073 | { |
959 | case 20: |
1074 | case SIMPLE_FALSE: |
960 | #if CBOR_SLOW |
1075 | #if CBOR_SLOW |
961 | types_false = get_bool ("Types::Serialiser::false"); |
1076 | types_false = get_bool ("Types::Serialiser::false"); |
962 | #endif |
1077 | #endif |
963 | return newSVsv (types_false); |
1078 | return newSVsv (types_false); |
964 | case 21: |
1079 | case SIMPLE_TRUE: |
965 | #if CBOR_SLOW |
1080 | #if CBOR_SLOW |
966 | types_true = get_bool ("Types::Serialiser::true"); |
1081 | types_true = get_bool ("Types::Serialiser::true"); |
967 | #endif |
1082 | #endif |
968 | return newSVsv (types_true); |
1083 | return newSVsv (types_true); |
969 | case 22: |
1084 | case SIMPLE_NULL: |
970 | return newSVsv (&PL_sv_undef); |
1085 | return newSVsv (&PL_sv_undef); |
971 | case 23: |
1086 | case SIMPLE_UNDEF: |
972 | #if CBOR_SLOW |
1087 | #if CBOR_SLOW |
973 | types_error = get_bool ("Types::Serialiser::error"); |
1088 | types_error = get_bool ("Types::Serialiser::error"); |
974 | #endif |
1089 | #endif |
975 | return newSVsv (types_error); |
1090 | return newSVsv (types_error); |
976 | |
1091 | |
977 | case 25: |
1092 | case MISC_FLOAT16: |
978 | { |
1093 | { |
979 | WANT (2); |
1094 | WANT (2); |
980 | |
1095 | |
981 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1096 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
982 | dec->cur += 2; |
1097 | dec->cur += 2; |
983 | |
1098 | |
984 | return newSVnv (ecb_binary16_to_float (fp)); |
1099 | return newSVnv (ecb_binary16_to_float (fp)); |
985 | } |
1100 | } |
986 | |
1101 | |
987 | case 26: |
1102 | case MISC_FLOAT32: |
988 | { |
1103 | { |
989 | uint32_t fp; |
1104 | uint32_t fp; |
990 | WANT (4); |
1105 | WANT (4); |
991 | memcpy (&fp, dec->cur, 4); |
1106 | memcpy (&fp, dec->cur, 4); |
992 | dec->cur += 4; |
1107 | dec->cur += 4; |
… | |
… | |
995 | fp = ecb_bswap32 (fp); |
1110 | fp = ecb_bswap32 (fp); |
996 | |
1111 | |
997 | return newSVnv (ecb_binary32_to_float (fp)); |
1112 | return newSVnv (ecb_binary32_to_float (fp)); |
998 | } |
1113 | } |
999 | |
1114 | |
1000 | case 27: |
1115 | case MISC_FLOAT64: |
1001 | { |
1116 | { |
1002 | uint64_t fp; |
1117 | uint64_t fp; |
1003 | WANT (8); |
1118 | WANT (8); |
1004 | memcpy (&fp, dec->cur, 8); |
1119 | memcpy (&fp, dec->cur, 8); |
1005 | dec->cur += 8; |
1120 | dec->cur += 8; |
… | |
… | |
1008 | fp = ecb_bswap64 (fp); |
1123 | fp = ecb_bswap64 (fp); |
1009 | |
1124 | |
1010 | return newSVnv (ecb_binary64_to_double (fp)); |
1125 | return newSVnv (ecb_binary64_to_double (fp)); |
1011 | } |
1126 | } |
1012 | |
1127 | |
1013 | // 0..19 unassigned |
1128 | // 0..19 unassigned simple |
1014 | // 24 reserved + unassigned (reserved values are not encodable) |
1129 | // 24 reserved + unassigned (reserved values are not encodable) |
1015 | default: |
1130 | default: |
1016 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1131 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1017 | } |
1132 | } |
1018 | |
1133 | |
… | |
… | |
1073 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1188 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1074 | |
1189 | |
1075 | types_true = get_bool ("Types::Serialiser::true" ); |
1190 | types_true = get_bool ("Types::Serialiser::true" ); |
1076 | types_false = get_bool ("Types::Serialiser::false"); |
1191 | types_false = get_bool ("Types::Serialiser::false"); |
1077 | types_error = get_bool ("Types::Serialiser::error"); |
1192 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
1193 | |
|
|
1194 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1078 | |
1195 | |
1079 | sv_cbor = newSVpv ("CBOR", 0); |
1196 | sv_cbor = newSVpv ("CBOR", 0); |
1080 | SvREADONLY_on (sv_cbor); |
1197 | SvREADONLY_on (sv_cbor); |
1081 | } |
1198 | } |
1082 | |
1199 | |
… | |
… | |
1104 | void shrink (CBOR *self, int enable = 1) |
1221 | void shrink (CBOR *self, int enable = 1) |
1105 | ALIAS: |
1222 | ALIAS: |
1106 | shrink = F_SHRINK |
1223 | shrink = F_SHRINK |
1107 | allow_unknown = F_ALLOW_UNKNOWN |
1224 | allow_unknown = F_ALLOW_UNKNOWN |
1108 | allow_sharing = F_ALLOW_SHARING |
1225 | allow_sharing = F_ALLOW_SHARING |
|
|
1226 | allow_cycles = F_ALLOW_CYCLES |
1109 | dedup_strings = F_DEDUP_STRINGS |
1227 | pack_strings = F_PACK_STRINGS |
1110 | PPCODE: |
1228 | PPCODE: |
1111 | { |
1229 | { |
1112 | if (enable) |
1230 | if (enable) |
1113 | self->flags |= ix; |
1231 | self->flags |= ix; |
1114 | else |
1232 | else |
… | |
… | |
1120 | void get_shrink (CBOR *self) |
1238 | void get_shrink (CBOR *self) |
1121 | ALIAS: |
1239 | ALIAS: |
1122 | get_shrink = F_SHRINK |
1240 | get_shrink = F_SHRINK |
1123 | get_allow_unknown = F_ALLOW_UNKNOWN |
1241 | get_allow_unknown = F_ALLOW_UNKNOWN |
1124 | get_allow_sharing = F_ALLOW_SHARING |
1242 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1243 | get_allow_cycles = F_ALLOW_CYCLES |
1125 | get_dedup_strings = F_DEDUP_STRINGS |
1244 | get_pack_strings = F_PACK_STRINGS |
1126 | PPCODE: |
1245 | PPCODE: |
1127 | XPUSHs (boolSV (self->flags & ix)); |
1246 | XPUSHs (boolSV (self->flags & ix)); |
1128 | |
1247 | |
1129 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1248 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1130 | PPCODE: |
1249 | PPCODE: |
… | |
… | |
1143 | XPUSHs (ST (0)); |
1262 | XPUSHs (ST (0)); |
1144 | |
1263 | |
1145 | int get_max_size (CBOR *self) |
1264 | int get_max_size (CBOR *self) |
1146 | CODE: |
1265 | CODE: |
1147 | RETVAL = self->max_size; |
1266 | RETVAL = self->max_size; |
|
|
1267 | OUTPUT: |
|
|
1268 | RETVAL |
|
|
1269 | |
|
|
1270 | void filter (CBOR *self, SV *filter = 0) |
|
|
1271 | PPCODE: |
|
|
1272 | SvREFCNT_dec (self->filter); |
|
|
1273 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1274 | XPUSHs (ST (0)); |
|
|
1275 | |
|
|
1276 | SV *get_filter (CBOR *self) |
|
|
1277 | CODE: |
|
|
1278 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
1148 | OUTPUT: |
1279 | OUTPUT: |
1149 | RETVAL |
1280 | RETVAL |
1150 | |
1281 | |
1151 | void encode (CBOR *self, SV *scalar) |
1282 | void encode (CBOR *self, SV *scalar) |
1152 | PPCODE: |
1283 | PPCODE: |
… | |
… | |
1167 | EXTEND (SP, 2); |
1298 | EXTEND (SP, 2); |
1168 | PUSHs (sv); |
1299 | PUSHs (sv); |
1169 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1300 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1170 | } |
1301 | } |
1171 | |
1302 | |
|
|
1303 | void DESTROY (CBOR *self) |
|
|
1304 | PPCODE: |
|
|
1305 | cbor_free (self); |
|
|
1306 | |
1172 | PROTOTYPES: ENABLE |
1307 | PROTOTYPES: ENABLE |
1173 | |
1308 | |
1174 | void encode_cbor (SV *scalar) |
1309 | void encode_cbor (SV *scalar) |
|
|
1310 | ALIAS: |
|
|
1311 | encode_cbor = 0 |
|
|
1312 | encode_cbor_sharing = F_ALLOW_SHARING |
1175 | PPCODE: |
1313 | PPCODE: |
1176 | { |
1314 | { |
1177 | CBOR cbor; |
1315 | CBOR cbor; |
1178 | cbor_init (&cbor); |
1316 | cbor_init (&cbor); |
|
|
1317 | cbor.flags |= ix; |
1179 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1318 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1180 | XPUSHs (scalar); |
1319 | XPUSHs (scalar); |
1181 | } |
1320 | } |
1182 | |
1321 | |
1183 | void decode_cbor (SV *cborstr) |
1322 | void decode_cbor (SV *cborstr) |