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