… | |
… | |
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 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
|
|
64 | |
104 | |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
105 | #define INIT_SIZE 32 // initial scalar size to be allocated |
66 | |
106 | |
67 | #define SB do { |
107 | #define SB do { |
68 | #define SE } while (0) |
108 | #define SE } while (0) |
… | |
… | |
80 | # define CBOR_SLOW 0 |
120 | # define CBOR_SLOW 0 |
81 | # define CBOR_STASH cbor_stash |
121 | # define CBOR_STASH cbor_stash |
82 | #endif |
122 | #endif |
83 | |
123 | |
84 | 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:: |
85 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
125 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
86 | |
126 | |
87 | typedef struct { |
127 | typedef struct { |
88 | U32 flags; |
128 | U32 flags; |
89 | U32 max_depth; |
129 | U32 max_depth; |
90 | STRLEN max_size; |
130 | STRLEN max_size; |
|
|
131 | SV *filter; |
91 | } CBOR; |
132 | } CBOR; |
92 | |
133 | |
93 | ecb_inline void |
134 | ecb_inline void |
94 | cbor_init (CBOR *cbor) |
135 | cbor_init (CBOR *cbor) |
95 | { |
136 | { |
96 | Zero (cbor, 1, CBOR); |
137 | Zero (cbor, 1, CBOR); |
97 | 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); |
98 | } |
145 | } |
99 | |
146 | |
100 | ///////////////////////////////////////////////////////////////////////////// |
147 | ///////////////////////////////////////////////////////////////////////////// |
101 | // utility functions |
148 | // utility functions |
102 | |
149 | |
… | |
… | |
122 | SvPV_shrink_to_cur (sv); |
169 | SvPV_shrink_to_cur (sv); |
123 | #elif defined (SvPV_renew) |
170 | #elif defined (SvPV_renew) |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
171 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
172 | #endif |
126 | } |
173 | } |
|
|
174 | } |
|
|
175 | |
|
|
176 | // minimum length of a string to be registered for stringref |
|
|
177 | ecb_inline int |
|
|
178 | minimum_string_length (UV idx) |
|
|
179 | { |
|
|
180 | return idx > 23 |
|
|
181 | ? idx > 0xffU |
|
|
182 | ? idx > 0xffffU |
|
|
183 | ? idx > 0xffffffffU |
|
|
184 | ? 11 |
|
|
185 | : 7 |
|
|
186 | : 5 |
|
|
187 | : 4 |
|
|
188 | : 3; |
127 | } |
189 | } |
128 | |
190 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
191 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
192 | // encoder |
131 | |
193 | |
… | |
… | |
165 | static void |
227 | static void |
166 | encode_uint (enc_t *enc, int major, UV len) |
228 | encode_uint (enc_t *enc, int major, UV len) |
167 | { |
229 | { |
168 | need (enc, 9); |
230 | need (enc, 9); |
169 | |
231 | |
170 | if (len < 24) |
232 | if (ecb_expect_true (len < LENGTH_EXT1)) |
171 | *enc->cur++ = major | len; |
233 | *enc->cur++ = major | len; |
172 | else if (len <= 0xff) |
234 | else if (ecb_expect_true (len <= 0xffU)) |
173 | { |
235 | { |
174 | *enc->cur++ = major | 24; |
236 | *enc->cur++ = major | LENGTH_EXT1; |
175 | *enc->cur++ = len; |
237 | *enc->cur++ = len; |
176 | } |
238 | } |
177 | else if (len <= 0xffff) |
239 | else if (len <= 0xffffU) |
178 | { |
240 | { |
179 | *enc->cur++ = major | 25; |
241 | *enc->cur++ = major | LENGTH_EXT2; |
180 | *enc->cur++ = len >> 8; |
242 | *enc->cur++ = len >> 8; |
181 | *enc->cur++ = len; |
243 | *enc->cur++ = len; |
182 | } |
244 | } |
183 | else if (len <= 0xffffffff) |
245 | else if (len <= 0xffffffffU) |
184 | { |
246 | { |
185 | *enc->cur++ = major | 26; |
247 | *enc->cur++ = major | LENGTH_EXT4; |
186 | *enc->cur++ = len >> 24; |
248 | *enc->cur++ = len >> 24; |
187 | *enc->cur++ = len >> 16; |
249 | *enc->cur++ = len >> 16; |
188 | *enc->cur++ = len >> 8; |
250 | *enc->cur++ = len >> 8; |
189 | *enc->cur++ = len; |
251 | *enc->cur++ = len; |
190 | } |
252 | } |
191 | else |
253 | else |
192 | { |
254 | { |
193 | *enc->cur++ = major | 27; |
255 | *enc->cur++ = major | LENGTH_EXT8; |
194 | *enc->cur++ = len >> 56; |
256 | *enc->cur++ = len >> 56; |
195 | *enc->cur++ = len >> 48; |
257 | *enc->cur++ = len >> 48; |
196 | *enc->cur++ = len >> 40; |
258 | *enc->cur++ = len >> 40; |
197 | *enc->cur++ = len >> 32; |
259 | *enc->cur++ = len >> 32; |
198 | *enc->cur++ = len >> 24; |
260 | *enc->cur++ = len >> 24; |
… | |
… | |
200 | *enc->cur++ = len >> 8; |
262 | *enc->cur++ = len >> 8; |
201 | *enc->cur++ = len; |
263 | *enc->cur++ = len; |
202 | } |
264 | } |
203 | } |
265 | } |
204 | |
266 | |
205 | static void |
267 | ecb_inline void |
|
|
268 | encode_tag (enc_t *enc, UV tag) |
|
|
269 | { |
|
|
270 | encode_uint (enc, MAJOR_TAG, tag); |
|
|
271 | } |
|
|
272 | |
|
|
273 | ecb_inline void |
206 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
274 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
207 | { |
275 | { |
208 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
276 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
209 | need (enc, len); |
277 | need (enc, len); |
210 | memcpy (enc->cur, str, len); |
278 | memcpy (enc->cur, str, len); |
211 | enc->cur += len; |
279 | enc->cur += len; |
212 | } |
280 | } |
213 | |
281 | |
214 | ecb_inline void |
282 | static void |
215 | encode_tag (enc_t *enc, UV tag) |
283 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
216 | { |
284 | { |
217 | encode_uint (enc, 0xc0, tag); |
285 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
|
|
286 | { |
|
|
287 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
|
|
288 | |
|
|
289 | if (SvOK (*svp)) |
|
|
290 | { |
|
|
291 | // already registered, use stringref |
|
|
292 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
293 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
294 | return; |
|
|
295 | } |
|
|
296 | else if (len >= minimum_string_length (enc->stringref_idx)) |
|
|
297 | { |
|
|
298 | // register only |
|
|
299 | sv_setuv (*svp, enc->stringref_idx); |
|
|
300 | ++enc->stringref_idx; |
|
|
301 | } |
|
|
302 | } |
|
|
303 | |
|
|
304 | encode_str (enc, utf8, str, len); |
218 | } |
305 | } |
219 | |
306 | |
220 | static void encode_sv (enc_t *enc, SV *sv); |
307 | static void encode_sv (enc_t *enc, SV *sv); |
221 | |
308 | |
222 | static void |
309 | static void |
… | |
… | |
227 | if (enc->depth >= enc->cbor.max_depth) |
314 | if (enc->depth >= enc->cbor.max_depth) |
228 | croak (ERR_NESTING_EXCEEDED); |
315 | croak (ERR_NESTING_EXCEEDED); |
229 | |
316 | |
230 | ++enc->depth; |
317 | ++enc->depth; |
231 | |
318 | |
232 | encode_uint (enc, 0x80, len + 1); |
319 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
233 | |
320 | |
234 | for (i = 0; i <= len; ++i) |
321 | for (i = 0; i <= len; ++i) |
235 | { |
322 | { |
236 | SV **svp = av_fetch (av, i, 0); |
323 | SV **svp = av_fetch (av, i, 0); |
237 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
324 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
238 | } |
325 | } |
239 | |
326 | |
240 | --enc->depth; |
327 | --enc->depth; |
241 | } |
328 | } |
242 | |
329 | |
243 | ecb_inline void |
|
|
244 | encode_he (enc_t *enc, HE *he) |
|
|
245 | { |
|
|
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 | } |
|
|
251 | |
|
|
252 | static void |
330 | static void |
253 | encode_hv (enc_t *enc, HV *hv) |
331 | encode_hv (enc_t *enc, HV *hv) |
254 | { |
332 | { |
255 | HE *he; |
333 | HE *he; |
256 | |
334 | |
… | |
… | |
261 | |
339 | |
262 | int pairs = hv_iterinit (hv); |
340 | int pairs = hv_iterinit (hv); |
263 | int mg = SvMAGICAL (hv); |
341 | int mg = SvMAGICAL (hv); |
264 | |
342 | |
265 | if (mg) |
343 | if (mg) |
266 | encode_ch (enc, 0xa0 | 31); |
344 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
267 | else |
345 | else |
268 | encode_uint (enc, 0xa0, pairs); |
346 | encode_uint (enc, MAJOR_MAP, pairs); |
269 | |
347 | |
270 | while ((he = hv_iternext (hv))) |
348 | while ((he = hv_iternext (hv))) |
271 | { |
349 | { |
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) |
350 | if (HeKLEN (he) == HEf_SVKEY) |
277 | svp = hv_fetch_ent (enc->stringref[!! SvUTF8 (HeSVKEY (he))], HeSVKEY (he) , 1, 0);//TODO return HE :/ |
351 | 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 |
352 | else |
294 | encode_he (enc, he); |
353 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
295 | |
354 | |
296 | 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)); |
297 | } |
356 | } |
298 | |
357 | |
299 | if (mg) |
358 | if (mg) |
300 | encode_ch (enc, 0xe0 | 31); |
359 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
301 | |
360 | |
302 | --enc->depth; |
361 | --enc->depth; |
303 | } |
362 | } |
304 | |
363 | |
305 | // encode objects, arrays and special \0=false and \1=true values. |
364 | // encode objects, arrays and special \0=false and \1=true values. |
306 | static void |
365 | static void |
307 | encode_rv (enc_t *enc, SV *sv) |
366 | encode_rv (enc_t *enc, SV *sv) |
308 | { |
367 | { |
309 | SvGETMAGIC (sv); |
368 | SvGETMAGIC (sv); |
310 | |
|
|
311 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
312 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
313 | { |
|
|
314 | if (!enc->shareable) |
|
|
315 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
316 | |
|
|
317 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
318 | |
|
|
319 | if (SvOK (*svp)) |
|
|
320 | { |
|
|
321 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
322 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
323 | return; |
|
|
324 | } |
|
|
325 | else |
|
|
326 | { |
|
|
327 | sv_setuv (*svp, enc->shareable_idx); |
|
|
328 | ++enc->shareable_idx; |
|
|
329 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
330 | } |
|
|
331 | } |
|
|
332 | |
369 | |
333 | svtype svt = SvTYPE (sv); |
370 | svtype svt = SvTYPE (sv); |
334 | |
371 | |
335 | if (ecb_expect_false (SvOBJECT (sv))) |
372 | if (ecb_expect_false (SvOBJECT (sv))) |
336 | { |
373 | { |
… | |
… | |
343 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
380 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
344 | ? cbor_tagged_stash |
381 | ? cbor_tagged_stash |
345 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
382 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
346 | |
383 | |
347 | HV *stash = SvSTASH (sv); |
384 | HV *stash = SvSTASH (sv); |
348 | GV *method; |
|
|
349 | |
385 | |
350 | if (stash == boolean_stash) |
386 | if (stash == boolean_stash) |
351 | 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 | } |
352 | else if (stash == error_stash) |
391 | else if (stash == error_stash) |
353 | encode_ch (enc, 0xe0 | 23); |
392 | { |
|
|
393 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
394 | return; |
|
|
395 | } |
354 | else if (stash == tagged_stash) |
396 | else if (stash == tagged_stash) |
355 | { |
397 | { |
356 | if (svt != SVt_PVAV) |
398 | if (svt != SVt_PVAV) |
357 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
399 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
358 | |
400 | |
359 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
401 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
360 | 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)) |
361 | } |
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 | |
362 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
435 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
363 | { |
436 | { |
364 | dSP; |
437 | dSP; |
365 | |
438 | |
366 | ENTER; SAVETMPS; PUSHMARK (SP); |
439 | ENTER; SAVETMPS; PUSHMARK (SP); |
367 | // 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 |
… | |
… | |
399 | // catch this surprisingly common error |
472 | // catch this surprisingly common error |
400 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
473 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
401 | 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)); |
402 | |
475 | |
403 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
476 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
404 | encode_uint (enc, 0x80, count + 1); |
477 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
405 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
478 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
406 | |
479 | |
407 | while (count) |
480 | while (count) |
408 | encode_sv (enc, SP[1 - count--]); |
481 | encode_sv (enc, SP[1 - count--]); |
409 | |
482 | |
410 | PUTBACK; |
483 | PUTBACK; |
… | |
… | |
431 | { |
504 | { |
432 | double nv = SvNVX (sv); |
505 | double nv = SvNVX (sv); |
433 | |
506 | |
434 | need (enc, 9); |
507 | need (enc, 9); |
435 | |
508 | |
436 | if (ecb_expect_false (nv == (U32)nv)) |
509 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
437 | encode_uint (enc, 0x00, (U32)nv); |
510 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
438 | //TODO: maybe I32? |
511 | //TODO: maybe I32? |
439 | else if (ecb_expect_false (nv == (float)nv)) |
512 | else if (ecb_expect_false (nv == (float)nv)) |
440 | { |
513 | { |
441 | uint32_t fp = ecb_float_to_binary32 (nv); |
514 | uint32_t fp = ecb_float_to_binary32 (nv); |
442 | |
515 | |
443 | *enc->cur++ = 0xe0 | 26; |
516 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
444 | |
517 | |
445 | if (!ecb_big_endian ()) |
518 | if (!ecb_big_endian ()) |
446 | fp = ecb_bswap32 (fp); |
519 | fp = ecb_bswap32 (fp); |
447 | |
520 | |
448 | memcpy (enc->cur, &fp, 4); |
521 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
450 | } |
523 | } |
451 | else |
524 | else |
452 | { |
525 | { |
453 | uint64_t fp = ecb_double_to_binary64 (nv); |
526 | uint64_t fp = ecb_double_to_binary64 (nv); |
454 | |
527 | |
455 | *enc->cur++ = 0xe0 | 27; |
528 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
456 | |
529 | |
457 | if (!ecb_big_endian ()) |
530 | if (!ecb_big_endian ()) |
458 | fp = ecb_bswap64 (fp); |
531 | fp = ecb_bswap64 (fp); |
459 | |
532 | |
460 | memcpy (enc->cur, &fp, 8); |
533 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
469 | |
542 | |
470 | if (SvPOKp (sv)) |
543 | if (SvPOKp (sv)) |
471 | { |
544 | { |
472 | STRLEN len; |
545 | STRLEN len; |
473 | char *str = SvPV (sv, len); |
546 | char *str = SvPV (sv, len); |
474 | encode_str (enc, SvUTF8 (sv), str, len); |
547 | encode_strref (enc, SvUTF8 (sv), str, len); |
475 | } |
548 | } |
476 | else if (SvNOKp (sv)) |
549 | else if (SvNOKp (sv)) |
477 | encode_nv (enc, sv); |
550 | encode_nv (enc, sv); |
478 | else if (SvIOKp (sv)) |
551 | else if (SvIOKp (sv)) |
479 | { |
552 | { |
480 | if (SvIsUV (sv)) |
553 | if (SvIsUV (sv)) |
481 | encode_uint (enc, 0x00, SvUVX (sv)); |
554 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
482 | else if (SvIVX (sv) >= 0) |
555 | else if (SvIVX (sv) >= 0) |
483 | encode_uint (enc, 0x00, SvIVX (sv)); |
556 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
484 | else |
557 | else |
485 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
558 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
486 | } |
559 | } |
487 | else if (SvROK (sv)) |
560 | else if (SvROK (sv)) |
488 | encode_rv (enc, SvRV (sv)); |
561 | encode_rv (enc, SvRV (sv)); |
489 | else if (!SvOK (sv)) |
562 | else if (!SvOK (sv)) |
490 | encode_ch (enc, 0xe0 | 22); |
563 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
491 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
564 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
492 | encode_ch (enc, 0xe0 | 23); |
565 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
493 | else |
566 | else |
494 | 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", |
495 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
568 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
496 | } |
569 | } |
497 | |
570 | |
… | |
… | |
505 | enc.cur = SvPVX (enc.sv); |
578 | enc.cur = SvPVX (enc.sv); |
506 | enc.end = SvEND (enc.sv); |
579 | enc.end = SvEND (enc.sv); |
507 | |
580 | |
508 | SvPOK_only (enc.sv); |
581 | SvPOK_only (enc.sv); |
509 | |
582 | |
510 | if (cbor->flags & (F_DEDUP_STRINGS | F_DEDUP_KEYS)) |
583 | if (cbor->flags & F_PACK_STRINGS) |
511 | { |
584 | { |
512 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
585 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
513 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
586 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
514 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
587 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
515 | } |
588 | } |
… | |
… | |
537 | CBOR cbor; |
610 | CBOR cbor; |
538 | U32 depth; // recursion depth |
611 | U32 depth; // recursion depth |
539 | U32 maxdepth; // recursion depth limit |
612 | U32 maxdepth; // recursion depth limit |
540 | AV *shareable; |
613 | AV *shareable; |
541 | AV *stringref; |
614 | AV *stringref; |
|
|
615 | SV *decode_tagged; |
542 | } dec_t; |
616 | } dec_t; |
543 | |
617 | |
544 | #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 |
545 | |
619 | |
546 | #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") |
… | |
… | |
549 | #define DEC_DEC_DEPTH --dec->depth |
623 | #define DEC_DEC_DEPTH --dec->depth |
550 | |
624 | |
551 | static UV |
625 | static UV |
552 | decode_uint (dec_t *dec) |
626 | decode_uint (dec_t *dec) |
553 | { |
627 | { |
554 | switch (*dec->cur & 31) |
628 | U8 m = *dec->cur & MINOR_MASK; |
555 | { |
629 | ++dec->cur; |
556 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
|
|
557 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
|
|
558 | case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
|
|
559 | return *dec->cur++ & 31; |
|
|
560 | |
630 | |
561 | case 24: |
631 | if (ecb_expect_true (m < LENGTH_EXT1)) |
|
|
632 | return m; |
|
|
633 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
|
|
634 | { |
562 | WANT (2); |
635 | WANT (1); |
563 | dec->cur += 2; |
636 | dec->cur += 1; |
564 | return dec->cur[-1]; |
637 | return dec->cur[-1]; |
565 | |
638 | } |
566 | case 25: |
639 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
640 | { |
567 | WANT (3); |
641 | WANT (2); |
568 | dec->cur += 3; |
642 | dec->cur += 2; |
569 | return (((UV)dec->cur[-2]) << 8) |
643 | return (((UV)dec->cur[-2]) << 8) |
570 | | ((UV)dec->cur[-1]); |
644 | | ((UV)dec->cur[-1]); |
571 | |
645 | } |
572 | case 26: |
646 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
647 | { |
573 | WANT (5); |
648 | WANT (4); |
574 | dec->cur += 5; |
649 | dec->cur += 4; |
575 | return (((UV)dec->cur[-4]) << 24) |
650 | return (((UV)dec->cur[-4]) << 24) |
576 | | (((UV)dec->cur[-3]) << 16) |
651 | | (((UV)dec->cur[-3]) << 16) |
577 | | (((UV)dec->cur[-2]) << 8) |
652 | | (((UV)dec->cur[-2]) << 8) |
578 | | ((UV)dec->cur[-1]); |
653 | | ((UV)dec->cur[-1]); |
579 | |
654 | } |
580 | case 27: |
655 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
656 | { |
581 | WANT (9); |
657 | WANT (8); |
582 | dec->cur += 9; |
658 | dec->cur += 8; |
|
|
659 | |
|
|
660 | return |
|
|
661 | #if UVSIZE < 8 |
|
|
662 | 0 |
|
|
663 | #else |
583 | return (((UV)dec->cur[-8]) << 56) |
664 | (((UV)dec->cur[-8]) << 56) |
584 | | (((UV)dec->cur[-7]) << 48) |
665 | | (((UV)dec->cur[-7]) << 48) |
585 | | (((UV)dec->cur[-6]) << 40) |
666 | | (((UV)dec->cur[-6]) << 40) |
586 | | (((UV)dec->cur[-5]) << 32) |
667 | | (((UV)dec->cur[-5]) << 32) |
|
|
668 | #endif |
587 | | (((UV)dec->cur[-4]) << 24) |
669 | | (((UV)dec->cur[-4]) << 24) |
588 | | (((UV)dec->cur[-3]) << 16) |
670 | | (((UV)dec->cur[-3]) << 16) |
589 | | (((UV)dec->cur[-2]) << 8) |
671 | | (((UV)dec->cur[-2]) << 8) |
590 | | ((UV)dec->cur[-1]); |
672 | | ((UV)dec->cur[-1]); |
591 | |
673 | } |
592 | default: |
674 | else |
593 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
675 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
594 | } |
|
|
595 | |
676 | |
596 | fail: |
677 | fail: |
597 | return 0; |
678 | return 0; |
598 | } |
679 | } |
599 | |
680 | |
… | |
… | |
604 | { |
685 | { |
605 | AV *av = newAV (); |
686 | AV *av = newAV (); |
606 | |
687 | |
607 | DEC_INC_DEPTH; |
688 | DEC_INC_DEPTH; |
608 | |
689 | |
609 | if ((*dec->cur & 31) == 31) |
690 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
610 | { |
691 | { |
611 | ++dec->cur; |
692 | ++dec->cur; |
612 | |
693 | |
613 | for (;;) |
694 | for (;;) |
614 | { |
695 | { |
615 | WANT (1); |
696 | WANT (1); |
616 | |
697 | |
617 | if (*dec->cur == (0xe0 | 31)) |
698 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
618 | { |
699 | { |
619 | ++dec->cur; |
700 | ++dec->cur; |
620 | break; |
701 | break; |
621 | } |
702 | } |
622 | |
703 | |
… | |
… | |
625 | } |
706 | } |
626 | else |
707 | else |
627 | { |
708 | { |
628 | int i, len = decode_uint (dec); |
709 | int i, len = decode_uint (dec); |
629 | |
710 | |
|
|
711 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
630 | av_fill (av, len - 1); |
712 | av_fill (av, len - 1); |
631 | |
713 | |
632 | for (i = 0; i < len; ++i) |
714 | for (i = 0; i < len; ++i) |
633 | AvARRAY (av)[i] = decode_sv (dec); |
715 | AvARRAY (av)[i] = decode_sv (dec); |
634 | } |
716 | } |
… | |
… | |
644 | |
726 | |
645 | static void |
727 | static void |
646 | decode_he (dec_t *dec, HV *hv) |
728 | decode_he (dec_t *dec, HV *hv) |
647 | { |
729 | { |
648 | // for speed reasons, we specialcase single-string |
730 | // for speed reasons, we specialcase single-string |
649 | // byte or utf-8 strings as keys. |
731 | // byte or utf-8 strings as keys, but only when !stringref |
650 | |
732 | |
651 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
733 | if (ecb_expect_true (!dec->stringref)) |
|
|
734 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
652 | { |
735 | { |
653 | I32 len = decode_uint (dec); |
736 | I32 len = decode_uint (dec); |
654 | char *key = (char *)dec->cur; |
737 | char *key = (char *)dec->cur; |
655 | |
738 | |
656 | dec->cur += len; |
739 | dec->cur += len; |
657 | |
740 | |
658 | if (ecb_expect_false (dec->stringref)) |
741 | if (ecb_expect_false (dec->stringref)) |
659 | av_push (dec->stringref, newSVpvn (key, len)); |
742 | av_push (dec->stringref, newSVpvn (key, len)); |
660 | |
743 | |
661 | hv_store (hv, key, len, decode_sv (dec), 0); |
744 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
745 | |
|
|
746 | return; |
662 | } |
747 | } |
663 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
748 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
664 | { |
749 | { |
665 | I32 len = decode_uint (dec); |
750 | I32 len = decode_uint (dec); |
666 | char *key = (char *)dec->cur; |
751 | char *key = (char *)dec->cur; |
667 | |
752 | |
668 | dec->cur += len; |
753 | dec->cur += len; |
669 | |
754 | |
670 | if (ecb_expect_false (dec->stringref)) |
755 | if (ecb_expect_false (dec->stringref)) |
671 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
756 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
672 | |
757 | |
673 | hv_store (hv, key, -len, decode_sv (dec), 0); |
758 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
759 | |
|
|
760 | return; |
674 | } |
761 | } |
675 | else |
762 | |
676 | { |
|
|
677 | SV *k = decode_sv (dec); |
763 | SV *k = decode_sv (dec); |
678 | SV *v = decode_sv (dec); |
764 | SV *v = decode_sv (dec); |
679 | |
765 | |
680 | hv_store_ent (hv, k, v, 0); |
766 | hv_store_ent (hv, k, v, 0); |
681 | SvREFCNT_dec (k); |
767 | SvREFCNT_dec (k); |
682 | } |
|
|
683 | } |
768 | } |
684 | |
769 | |
685 | static SV * |
770 | static SV * |
686 | decode_hv (dec_t *dec) |
771 | decode_hv (dec_t *dec) |
687 | { |
772 | { |
688 | HV *hv = newHV (); |
773 | HV *hv = newHV (); |
689 | |
774 | |
690 | DEC_INC_DEPTH; |
775 | DEC_INC_DEPTH; |
691 | |
776 | |
692 | if ((*dec->cur & 31) == 31) |
777 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
693 | { |
778 | { |
694 | ++dec->cur; |
779 | ++dec->cur; |
695 | |
780 | |
696 | for (;;) |
781 | for (;;) |
697 | { |
782 | { |
698 | WANT (1); |
783 | WANT (1); |
699 | |
784 | |
700 | if (*dec->cur == (0xe0 | 31)) |
785 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
701 | { |
786 | { |
702 | ++dec->cur; |
787 | ++dec->cur; |
703 | break; |
788 | break; |
704 | } |
789 | } |
705 | |
790 | |
… | |
… | |
726 | static SV * |
811 | static SV * |
727 | decode_str (dec_t *dec, int utf8) |
812 | decode_str (dec_t *dec, int utf8) |
728 | { |
813 | { |
729 | SV *sv = 0; |
814 | SV *sv = 0; |
730 | |
815 | |
731 | if ((*dec->cur & 31) == 31) |
816 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
732 | { |
817 | { |
|
|
818 | // indefinite length strings |
733 | ++dec->cur; |
819 | ++dec->cur; |
734 | |
820 | |
|
|
821 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
822 | |
735 | sv = newSVpvn ("", 0); |
823 | sv = newSVpvn ("", 0); |
736 | |
824 | |
737 | // not very fast, and certainly not robust against illegal input |
|
|
738 | for (;;) |
825 | for (;;) |
739 | { |
826 | { |
740 | WANT (1); |
827 | WANT (1); |
741 | |
828 | |
742 | if (*dec->cur == (0xe0 | 31)) |
829 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
830 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
743 | { |
831 | { |
744 | ++dec->cur; |
832 | ++dec->cur; |
745 | break; |
833 | break; |
746 | } |
834 | } |
|
|
835 | else |
|
|
836 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
747 | |
837 | |
748 | 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; |
749 | } |
843 | } |
750 | } |
844 | } |
751 | else |
845 | else |
752 | { |
846 | { |
753 | STRLEN len = decode_uint (dec); |
847 | STRLEN len = decode_uint (dec); |
754 | |
848 | |
755 | WANT (len); |
849 | WANT (len); |
756 | sv = newSVpvn (dec->cur, len); |
850 | sv = newSVpvn (dec->cur, len); |
757 | 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)); |
758 | } |
856 | } |
759 | |
857 | |
760 | if (utf8) |
858 | if (utf8) |
761 | SvUTF8_on (sv); |
859 | SvUTF8_on (sv); |
762 | |
|
|
763 | if (ecb_expect_false (dec->stringref)) |
|
|
764 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
|
|
765 | |
860 | |
766 | return sv; |
861 | return sv; |
767 | |
862 | |
768 | fail: |
863 | fail: |
769 | SvREFCNT_dec (sv); |
864 | SvREFCNT_dec (sv); |
… | |
… | |
801 | } |
896 | } |
802 | break; |
897 | break; |
803 | |
898 | |
804 | case CBOR_TAG_STRINGREF: |
899 | case CBOR_TAG_STRINGREF: |
805 | { |
900 | { |
806 | if ((*dec->cur >> 5) != 0) |
901 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
807 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
902 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
808 | |
903 | |
809 | UV idx = decode_uint (dec); |
904 | UV idx = decode_uint (dec); |
810 | |
905 | |
811 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
906 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
… | |
… | |
818 | case CBOR_TAG_VALUE_SHAREABLE: |
913 | case CBOR_TAG_VALUE_SHAREABLE: |
819 | { |
914 | { |
820 | if (ecb_expect_false (!dec->shareable)) |
915 | if (ecb_expect_false (!dec->shareable)) |
821 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
916 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
822 | |
917 | |
|
|
918 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
919 | { |
823 | sv = newSV (0); |
920 | sv = newSV (0); |
824 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
921 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
825 | |
922 | |
826 | SV *osv = decode_sv (dec); |
923 | SV *osv = decode_sv (dec); |
827 | sv_setsv (sv, osv); |
924 | sv_setsv (sv, osv); |
828 | 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 | } |
829 | } |
934 | } |
830 | break; |
935 | break; |
831 | |
936 | |
832 | case CBOR_TAG_VALUE_SHAREDREF: |
937 | case CBOR_TAG_VALUE_SHAREDREF: |
833 | { |
938 | { |
834 | if ((*dec->cur >> 5) != 0) |
939 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
835 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
940 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
836 | |
941 | |
837 | UV idx = decode_uint (dec); |
942 | UV idx = decode_uint (dec); |
838 | |
943 | |
839 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
944 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
840 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
945 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
841 | |
946 | |
842 | 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"); |
843 | } |
951 | } |
844 | break; |
952 | break; |
845 | |
953 | |
846 | case CBOR_TAG_PERL_OBJECT: |
954 | case CBOR_TAG_PERL_OBJECT: |
847 | { |
955 | { |
… | |
… | |
896 | |
1004 | |
897 | default: |
1005 | default: |
898 | { |
1006 | { |
899 | sv = decode_sv (dec); |
1007 | sv = decode_sv (dec); |
900 | |
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 | { |
901 | AV *av = newAV (); |
1032 | AV *av = newAV (); |
902 | av_push (av, newSVuv (tag)); |
1033 | av_push (av, newSVuv (tag)); |
903 | av_push (av, sv); |
1034 | av_push (av, sv); |
904 | |
1035 | |
905 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1036 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
906 | ? cbor_tagged_stash |
1037 | ? cbor_tagged_stash |
907 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1038 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
908 | |
|
|
909 | 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; |
910 | } |
1045 | } |
911 | break; |
1046 | break; |
912 | } |
1047 | } |
913 | |
1048 | |
914 | return sv; |
1049 | return sv; |
… | |
… | |
921 | static SV * |
1056 | static SV * |
922 | decode_sv (dec_t *dec) |
1057 | decode_sv (dec_t *dec) |
923 | { |
1058 | { |
924 | WANT (1); |
1059 | WANT (1); |
925 | |
1060 | |
926 | switch (*dec->cur >> 5) |
1061 | switch (*dec->cur >> MAJOR_SHIFT) |
927 | { |
1062 | { |
928 | case 0: // unsigned int |
1063 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
929 | return newSVuv (decode_uint (dec)); |
1064 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
930 | case 1: // negative int |
1065 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
931 | return newSViv (-1 - (IV)decode_uint (dec)); |
1066 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
932 | case 2: // octet string |
1067 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
933 | return decode_str (dec, 0); |
1068 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
934 | case 3: // utf-8 string |
1069 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
935 | return decode_str (dec, 1); |
1070 | |
936 | case 4: // array |
1071 | case MAJOR_MISC >> MAJOR_SHIFT: |
937 | return decode_av (dec); |
|
|
938 | case 5: // map |
|
|
939 | return decode_hv (dec); |
|
|
940 | case 6: // tag |
|
|
941 | return decode_tagged (dec); |
|
|
942 | case 7: // misc |
|
|
943 | switch (*dec->cur++ & 31) |
1072 | switch (*dec->cur++ & MINOR_MASK) |
944 | { |
1073 | { |
945 | case 20: |
1074 | case SIMPLE_FALSE: |
946 | #if CBOR_SLOW |
1075 | #if CBOR_SLOW |
947 | types_false = get_bool ("Types::Serialiser::false"); |
1076 | types_false = get_bool ("Types::Serialiser::false"); |
948 | #endif |
1077 | #endif |
949 | return newSVsv (types_false); |
1078 | return newSVsv (types_false); |
950 | case 21: |
1079 | case SIMPLE_TRUE: |
951 | #if CBOR_SLOW |
1080 | #if CBOR_SLOW |
952 | types_true = get_bool ("Types::Serialiser::true"); |
1081 | types_true = get_bool ("Types::Serialiser::true"); |
953 | #endif |
1082 | #endif |
954 | return newSVsv (types_true); |
1083 | return newSVsv (types_true); |
955 | case 22: |
1084 | case SIMPLE_NULL: |
956 | return newSVsv (&PL_sv_undef); |
1085 | return newSVsv (&PL_sv_undef); |
957 | case 23: |
1086 | case SIMPLE_UNDEF: |
958 | #if CBOR_SLOW |
1087 | #if CBOR_SLOW |
959 | types_error = get_bool ("Types::Serialiser::error"); |
1088 | types_error = get_bool ("Types::Serialiser::error"); |
960 | #endif |
1089 | #endif |
961 | return newSVsv (types_error); |
1090 | return newSVsv (types_error); |
962 | |
1091 | |
963 | case 25: |
1092 | case MISC_FLOAT16: |
964 | { |
1093 | { |
965 | WANT (2); |
1094 | WANT (2); |
966 | |
1095 | |
967 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1096 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
968 | dec->cur += 2; |
1097 | dec->cur += 2; |
969 | |
1098 | |
970 | return newSVnv (ecb_binary16_to_float (fp)); |
1099 | return newSVnv (ecb_binary16_to_float (fp)); |
971 | } |
1100 | } |
972 | |
1101 | |
973 | case 26: |
1102 | case MISC_FLOAT32: |
974 | { |
1103 | { |
975 | uint32_t fp; |
1104 | uint32_t fp; |
976 | WANT (4); |
1105 | WANT (4); |
977 | memcpy (&fp, dec->cur, 4); |
1106 | memcpy (&fp, dec->cur, 4); |
978 | dec->cur += 4; |
1107 | dec->cur += 4; |
… | |
… | |
981 | fp = ecb_bswap32 (fp); |
1110 | fp = ecb_bswap32 (fp); |
982 | |
1111 | |
983 | return newSVnv (ecb_binary32_to_float (fp)); |
1112 | return newSVnv (ecb_binary32_to_float (fp)); |
984 | } |
1113 | } |
985 | |
1114 | |
986 | case 27: |
1115 | case MISC_FLOAT64: |
987 | { |
1116 | { |
988 | uint64_t fp; |
1117 | uint64_t fp; |
989 | WANT (8); |
1118 | WANT (8); |
990 | memcpy (&fp, dec->cur, 8); |
1119 | memcpy (&fp, dec->cur, 8); |
991 | dec->cur += 8; |
1120 | dec->cur += 8; |
… | |
… | |
994 | fp = ecb_bswap64 (fp); |
1123 | fp = ecb_bswap64 (fp); |
995 | |
1124 | |
996 | return newSVnv (ecb_binary64_to_double (fp)); |
1125 | return newSVnv (ecb_binary64_to_double (fp)); |
997 | } |
1126 | } |
998 | |
1127 | |
999 | // 0..19 unassigned |
1128 | // 0..19 unassigned simple |
1000 | // 24 reserved + unassigned (reserved values are not encodable) |
1129 | // 24 reserved + unassigned (reserved values are not encodable) |
1001 | default: |
1130 | default: |
1002 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1131 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1003 | } |
1132 | } |
1004 | |
1133 | |
… | |
… | |
1059 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1188 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1060 | |
1189 | |
1061 | types_true = get_bool ("Types::Serialiser::true" ); |
1190 | types_true = get_bool ("Types::Serialiser::true" ); |
1062 | types_false = get_bool ("Types::Serialiser::false"); |
1191 | types_false = get_bool ("Types::Serialiser::false"); |
1063 | 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); |
1064 | |
1195 | |
1065 | sv_cbor = newSVpv ("CBOR", 0); |
1196 | sv_cbor = newSVpv ("CBOR", 0); |
1066 | SvREADONLY_on (sv_cbor); |
1197 | SvREADONLY_on (sv_cbor); |
1067 | } |
1198 | } |
1068 | |
1199 | |
… | |
… | |
1090 | void shrink (CBOR *self, int enable = 1) |
1221 | void shrink (CBOR *self, int enable = 1) |
1091 | ALIAS: |
1222 | ALIAS: |
1092 | shrink = F_SHRINK |
1223 | shrink = F_SHRINK |
1093 | allow_unknown = F_ALLOW_UNKNOWN |
1224 | allow_unknown = F_ALLOW_UNKNOWN |
1094 | allow_sharing = F_ALLOW_SHARING |
1225 | allow_sharing = F_ALLOW_SHARING |
1095 | dedup_keys = F_DEDUP_KEYS |
1226 | allow_cycles = F_ALLOW_CYCLES |
1096 | dedup_strings = F_DEDUP_STRINGS |
1227 | pack_strings = F_PACK_STRINGS |
1097 | PPCODE: |
1228 | PPCODE: |
1098 | { |
1229 | { |
1099 | if (enable) |
1230 | if (enable) |
1100 | self->flags |= ix; |
1231 | self->flags |= ix; |
1101 | else |
1232 | else |
… | |
… | |
1107 | void get_shrink (CBOR *self) |
1238 | void get_shrink (CBOR *self) |
1108 | ALIAS: |
1239 | ALIAS: |
1109 | get_shrink = F_SHRINK |
1240 | get_shrink = F_SHRINK |
1110 | get_allow_unknown = F_ALLOW_UNKNOWN |
1241 | get_allow_unknown = F_ALLOW_UNKNOWN |
1111 | get_allow_sharing = F_ALLOW_SHARING |
1242 | get_allow_sharing = F_ALLOW_SHARING |
1112 | get_dedup_keys = F_DEDUP_KEYS |
1243 | get_allow_cycles = F_ALLOW_CYCLES |
1113 | get_dedup_strings = F_DEDUP_STRINGS |
1244 | get_pack_strings = F_PACK_STRINGS |
1114 | PPCODE: |
1245 | PPCODE: |
1115 | XPUSHs (boolSV (self->flags & ix)); |
1246 | XPUSHs (boolSV (self->flags & ix)); |
1116 | |
1247 | |
1117 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1248 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1118 | PPCODE: |
1249 | PPCODE: |
… | |
… | |
1131 | XPUSHs (ST (0)); |
1262 | XPUSHs (ST (0)); |
1132 | |
1263 | |
1133 | int get_max_size (CBOR *self) |
1264 | int get_max_size (CBOR *self) |
1134 | CODE: |
1265 | CODE: |
1135 | 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); |
1136 | OUTPUT: |
1279 | OUTPUT: |
1137 | RETVAL |
1280 | RETVAL |
1138 | |
1281 | |
1139 | void encode (CBOR *self, SV *scalar) |
1282 | void encode (CBOR *self, SV *scalar) |
1140 | PPCODE: |
1283 | PPCODE: |
… | |
… | |
1155 | EXTEND (SP, 2); |
1298 | EXTEND (SP, 2); |
1156 | PUSHs (sv); |
1299 | PUSHs (sv); |
1157 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1300 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1158 | } |
1301 | } |
1159 | |
1302 | |
|
|
1303 | void DESTROY (CBOR *self) |
|
|
1304 | PPCODE: |
|
|
1305 | cbor_free (self); |
|
|
1306 | |
1160 | PROTOTYPES: ENABLE |
1307 | PROTOTYPES: ENABLE |
1161 | |
1308 | |
1162 | void encode_cbor (SV *scalar) |
1309 | void encode_cbor (SV *scalar) |
|
|
1310 | ALIAS: |
|
|
1311 | encode_cbor = 0 |
|
|
1312 | encode_cbor_sharing = F_ALLOW_SHARING |
1163 | PPCODE: |
1313 | PPCODE: |
1164 | { |
1314 | { |
1165 | CBOR cbor; |
1315 | CBOR cbor; |
1166 | cbor_init (&cbor); |
1316 | cbor_init (&cbor); |
|
|
1317 | cbor.flags |= ix; |
1167 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1318 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1168 | XPUSHs (scalar); |
1319 | XPUSHs (scalar); |
1169 | } |
1320 | } |
1170 | |
1321 | |
1171 | void decode_cbor (SV *cborstr) |
1322 | void decode_cbor (SV *cborstr) |