… | |
… | |
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_SHARABLE = 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 | |
… | |
… | |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
170 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
171 | #endif |
126 | } |
172 | } |
127 | } |
173 | } |
128 | |
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; |
|
|
188 | } |
|
|
189 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
190 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
191 | // encoder |
131 | |
192 | |
132 | // structure used for encoding CBOR |
193 | // structure used for encoding CBOR |
133 | typedef struct |
194 | typedef struct |
… | |
… | |
135 | char *cur; // SvPVX (sv) + current output position |
196 | char *cur; // SvPVX (sv) + current output position |
136 | char *end; // SvEND (sv) |
197 | char *end; // SvEND (sv) |
137 | SV *sv; // result scalar |
198 | SV *sv; // result scalar |
138 | CBOR cbor; |
199 | CBOR cbor; |
139 | U32 depth; // recursion level |
200 | U32 depth; // recursion level |
140 | HV *stringref; // string => index, or 0 |
201 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
202 | UV stringref_idx; |
141 | HV *sharable; // ptr => index, or 0 |
203 | HV *shareable; // ptr => index, or 0 |
142 | HV *sharable_idx; |
204 | UV shareable_idx; |
143 | } enc_t; |
205 | } enc_t; |
144 | |
206 | |
145 | ecb_inline void |
207 | ecb_inline void |
146 | need (enc_t *enc, STRLEN len) |
208 | need (enc_t *enc, STRLEN len) |
147 | { |
209 | { |
… | |
… | |
164 | static void |
226 | static void |
165 | encode_uint (enc_t *enc, int major, UV len) |
227 | encode_uint (enc_t *enc, int major, UV len) |
166 | { |
228 | { |
167 | need (enc, 9); |
229 | need (enc, 9); |
168 | |
230 | |
169 | if (len < 24) |
231 | if (ecb_expect_true (len < LENGTH_EXT1)) |
170 | *enc->cur++ = major | len; |
232 | *enc->cur++ = major | len; |
171 | else if (len <= 0xff) |
233 | else if (ecb_expect_true (len <= 0xff)) |
172 | { |
234 | { |
173 | *enc->cur++ = major | 24; |
235 | *enc->cur++ = major | LENGTH_EXT1; |
174 | *enc->cur++ = len; |
236 | *enc->cur++ = len; |
175 | } |
237 | } |
176 | else if (len <= 0xffff) |
238 | else if (len <= 0xffff) |
177 | { |
239 | { |
178 | *enc->cur++ = major | 25; |
240 | *enc->cur++ = major | LENGTH_EXT2; |
179 | *enc->cur++ = len >> 8; |
241 | *enc->cur++ = len >> 8; |
180 | *enc->cur++ = len; |
242 | *enc->cur++ = len; |
181 | } |
243 | } |
182 | else if (len <= 0xffffffff) |
244 | else if (len <= 0xffffffff) |
183 | { |
245 | { |
184 | *enc->cur++ = major | 26; |
246 | *enc->cur++ = major | LENGTH_EXT4; |
185 | *enc->cur++ = len >> 24; |
247 | *enc->cur++ = len >> 24; |
186 | *enc->cur++ = len >> 16; |
248 | *enc->cur++ = len >> 16; |
187 | *enc->cur++ = len >> 8; |
249 | *enc->cur++ = len >> 8; |
188 | *enc->cur++ = len; |
250 | *enc->cur++ = len; |
189 | } |
251 | } |
190 | else |
252 | else |
191 | { |
253 | { |
192 | *enc->cur++ = major | 27; |
254 | *enc->cur++ = major | LENGTH_EXT8; |
193 | *enc->cur++ = len >> 56; |
255 | *enc->cur++ = len >> 56; |
194 | *enc->cur++ = len >> 48; |
256 | *enc->cur++ = len >> 48; |
195 | *enc->cur++ = len >> 40; |
257 | *enc->cur++ = len >> 40; |
196 | *enc->cur++ = len >> 32; |
258 | *enc->cur++ = len >> 32; |
197 | *enc->cur++ = len >> 24; |
259 | *enc->cur++ = len >> 24; |
… | |
… | |
199 | *enc->cur++ = len >> 8; |
261 | *enc->cur++ = len >> 8; |
200 | *enc->cur++ = len; |
262 | *enc->cur++ = len; |
201 | } |
263 | } |
202 | } |
264 | } |
203 | |
265 | |
204 | 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 |
205 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
273 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
206 | { |
274 | { |
207 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
275 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
208 | need (enc, len); |
276 | need (enc, len); |
209 | memcpy (enc->cur, str, len); |
277 | memcpy (enc->cur, str, len); |
210 | enc->cur += len; |
278 | enc->cur += len; |
211 | } |
279 | } |
212 | |
280 | |
213 | ecb_inline void |
|
|
214 | encode_tag (enc_t *enc, UV tag) |
|
|
215 | { |
|
|
216 | encode_uint (enc, 0xc0, tag); |
|
|
217 | } |
|
|
218 | |
|
|
219 | static int |
281 | static void |
220 | encode_sharable2 (enc_t *enc, SV *sv) |
282 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
221 | { |
283 | { |
222 | if (!enc->sharable) |
284 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
223 | enc->sharable = (HV *)sv_2mortal ((SV *)newHV ()); |
285 | { |
|
|
286 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
224 | |
287 | |
225 | SV **svp = hv_fetch (enc->sharable, &sv, sizeof (sv), 1); |
|
|
226 | |
|
|
227 | if (SvOK (*svp)) |
288 | if (SvOK (*svp)) |
228 | { |
289 | { |
|
|
290 | // already registered, use stringref |
229 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
291 | encode_tag (enc, CBOR_TAG_STRINGREF); |
230 | encode_uint (enc, 0x00, SvUV (*svp)); |
292 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
231 | |
|
|
232 | return 1; |
293 | return; |
233 | } |
294 | } |
234 | else |
295 | else if (len >= minimum_string_length (enc->stringref_idx)) |
235 | { |
296 | { |
|
|
297 | // register only |
236 | sv_setuv (*svp, enc->sharable_idx++); |
298 | sv_setuv (*svp, enc->stringref_idx); |
237 | encode_tag (enc, CBOR_TAG_VALUE_SHARABLE); |
299 | ++enc->stringref_idx; |
238 | |
300 | } |
239 | return 0; |
|
|
240 | } |
301 | } |
241 | } |
|
|
242 | |
302 | |
243 | ecb_inline int |
303 | encode_str (enc, utf8, str, len); |
244 | encode_sharable (enc_t *enc, SV *sv) |
|
|
245 | { |
|
|
246 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
247 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
248 | return encode_sharable2 (enc, sv); |
|
|
249 | |
|
|
250 | return 0; |
|
|
251 | } |
304 | } |
252 | |
305 | |
253 | static void encode_sv (enc_t *enc, SV *sv); |
306 | static void encode_sv (enc_t *enc, SV *sv); |
254 | |
307 | |
255 | static void |
308 | static void |
… | |
… | |
260 | if (enc->depth >= enc->cbor.max_depth) |
313 | if (enc->depth >= enc->cbor.max_depth) |
261 | croak (ERR_NESTING_EXCEEDED); |
314 | croak (ERR_NESTING_EXCEEDED); |
262 | |
315 | |
263 | ++enc->depth; |
316 | ++enc->depth; |
264 | |
317 | |
265 | encode_uint (enc, 0x80, len + 1); |
318 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
266 | |
319 | |
267 | for (i = 0; i <= len; ++i) |
320 | for (i = 0; i <= len; ++i) |
268 | { |
321 | { |
269 | SV **svp = av_fetch (av, i, 0); |
322 | SV **svp = av_fetch (av, i, 0); |
270 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
323 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
… | |
… | |
285 | |
338 | |
286 | int pairs = hv_iterinit (hv); |
339 | int pairs = hv_iterinit (hv); |
287 | int mg = SvMAGICAL (hv); |
340 | int mg = SvMAGICAL (hv); |
288 | |
341 | |
289 | if (mg) |
342 | if (mg) |
290 | encode_ch (enc, 0xa0 | 31); |
343 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
291 | else |
344 | else |
292 | encode_uint (enc, 0xa0, pairs); |
345 | encode_uint (enc, MAJOR_MAP, pairs); |
293 | |
346 | |
294 | while ((he = hv_iternext (hv))) |
347 | while ((he = hv_iternext (hv))) |
295 | { |
348 | { |
296 | if (HeKLEN (he) == HEf_SVKEY) |
349 | if (HeKLEN (he) == HEf_SVKEY) |
297 | encode_sv (enc, HeSVKEY (he)); |
350 | encode_sv (enc, HeSVKEY (he)); |
298 | else |
351 | else |
299 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
352 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
300 | |
353 | |
301 | 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)); |
302 | } |
355 | } |
303 | |
356 | |
304 | if (mg) |
357 | if (mg) |
305 | encode_ch (enc, 0xe0 | 31); |
358 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
306 | |
359 | |
307 | --enc->depth; |
360 | --enc->depth; |
308 | } |
361 | } |
309 | |
362 | |
310 | // encode objects, arrays and special \0=false and \1=true values. |
363 | // encode objects, arrays and special \0=false and \1=true values. |
311 | static void |
364 | static void |
312 | encode_rv (enc_t *enc, SV *sv) |
365 | encode_rv (enc_t *enc, SV *sv) |
313 | { |
366 | { |
314 | svtype svt; |
|
|
315 | |
|
|
316 | SvGETMAGIC (sv); |
367 | SvGETMAGIC (sv); |
|
|
368 | |
317 | svt = SvTYPE (sv); |
369 | svtype svt = SvTYPE (sv); |
318 | |
|
|
319 | if (encode_sharable (enc, sv)) |
|
|
320 | return; |
|
|
321 | |
370 | |
322 | if (ecb_expect_false (SvOBJECT (sv))) |
371 | if (ecb_expect_false (SvOBJECT (sv))) |
323 | { |
372 | { |
324 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
373 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
325 | ? types_boolean_stash |
374 | ? types_boolean_stash |
… | |
… | |
330 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
379 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
331 | ? cbor_tagged_stash |
380 | ? cbor_tagged_stash |
332 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
381 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
333 | |
382 | |
334 | HV *stash = SvSTASH (sv); |
383 | HV *stash = SvSTASH (sv); |
335 | GV *method; |
|
|
336 | |
384 | |
337 | if (stash == boolean_stash) |
385 | if (stash == boolean_stash) |
338 | 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 | } |
339 | else if (stash == error_stash) |
390 | else if (stash == error_stash) |
340 | encode_ch (enc, 0xe0 | 23); |
391 | { |
|
|
392 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
393 | return; |
|
|
394 | } |
341 | else if (stash == tagged_stash) |
395 | else if (stash == tagged_stash) |
342 | { |
396 | { |
343 | if (svt != SVt_PVAV) |
397 | if (svt != SVt_PVAV) |
344 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
398 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
345 | |
399 | |
346 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
400 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
347 | 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)) |
348 | } |
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 | |
349 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
434 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
350 | { |
435 | { |
351 | dSP; |
436 | dSP; |
352 | |
437 | |
353 | ENTER; SAVETMPS; PUSHMARK (SP); |
438 | ENTER; SAVETMPS; PUSHMARK (SP); |
354 | // 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 |
… | |
… | |
386 | // catch this surprisingly common error |
471 | // catch this surprisingly common error |
387 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
472 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
388 | 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)); |
389 | |
474 | |
390 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
475 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
391 | encode_uint (enc, 0x80, count + 1); |
476 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
392 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
477 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
393 | |
478 | |
394 | while (count) |
479 | while (count) |
395 | encode_sv (enc, SP[1 - count--]); |
480 | encode_sv (enc, SP[1 - count--]); |
396 | |
481 | |
397 | PUTBACK; |
482 | PUTBACK; |
… | |
… | |
418 | { |
503 | { |
419 | double nv = SvNVX (sv); |
504 | double nv = SvNVX (sv); |
420 | |
505 | |
421 | need (enc, 9); |
506 | need (enc, 9); |
422 | |
507 | |
423 | if (ecb_expect_false (nv == (U32)nv)) |
508 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
424 | encode_uint (enc, 0x00, (U32)nv); |
509 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
425 | //TODO: maybe I32? |
510 | //TODO: maybe I32? |
426 | else if (ecb_expect_false (nv == (float)nv)) |
511 | else if (ecb_expect_false (nv == (float)nv)) |
427 | { |
512 | { |
428 | uint32_t fp = ecb_float_to_binary32 (nv); |
513 | uint32_t fp = ecb_float_to_binary32 (nv); |
429 | |
514 | |
430 | *enc->cur++ = 0xe0 | 26; |
515 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
431 | |
516 | |
432 | if (!ecb_big_endian ()) |
517 | if (!ecb_big_endian ()) |
433 | fp = ecb_bswap32 (fp); |
518 | fp = ecb_bswap32 (fp); |
434 | |
519 | |
435 | memcpy (enc->cur, &fp, 4); |
520 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
437 | } |
522 | } |
438 | else |
523 | else |
439 | { |
524 | { |
440 | uint64_t fp = ecb_double_to_binary64 (nv); |
525 | uint64_t fp = ecb_double_to_binary64 (nv); |
441 | |
526 | |
442 | *enc->cur++ = 0xe0 | 27; |
527 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
443 | |
528 | |
444 | if (!ecb_big_endian ()) |
529 | if (!ecb_big_endian ()) |
445 | fp = ecb_bswap64 (fp); |
530 | fp = ecb_bswap64 (fp); |
446 | |
531 | |
447 | memcpy (enc->cur, &fp, 8); |
532 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
452 | static void |
537 | static void |
453 | encode_sv (enc_t *enc, SV *sv) |
538 | encode_sv (enc_t *enc, SV *sv) |
454 | { |
539 | { |
455 | SvGETMAGIC (sv); |
540 | SvGETMAGIC (sv); |
456 | |
541 | |
457 | if (encode_sharable (enc, sv)) |
|
|
458 | return; |
|
|
459 | |
|
|
460 | if (SvPOKp (sv)) |
542 | if (SvPOKp (sv)) |
461 | { |
543 | { |
462 | STRLEN len; |
544 | STRLEN len; |
463 | char *str = SvPV (sv, len); |
545 | char *str = SvPV (sv, len); |
464 | encode_str (enc, SvUTF8 (sv), str, len); |
546 | encode_strref (enc, SvUTF8 (sv), str, len); |
465 | } |
547 | } |
466 | else if (SvNOKp (sv)) |
548 | else if (SvNOKp (sv)) |
467 | encode_nv (enc, sv); |
549 | encode_nv (enc, sv); |
468 | else if (SvIOKp (sv)) |
550 | else if (SvIOKp (sv)) |
469 | { |
551 | { |
470 | if (SvIsUV (sv)) |
552 | if (SvIsUV (sv)) |
471 | encode_uint (enc, 0x00, SvUVX (sv)); |
553 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
472 | else if (SvIVX (sv) >= 0) |
554 | else if (SvIVX (sv) >= 0) |
473 | encode_uint (enc, 0x00, SvIVX (sv)); |
555 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
474 | else |
556 | else |
475 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
557 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
476 | } |
558 | } |
477 | else if (SvROK (sv)) |
559 | else if (SvROK (sv)) |
478 | encode_rv (enc, SvRV (sv)); |
560 | encode_rv (enc, SvRV (sv)); |
479 | else if (!SvOK (sv)) |
561 | else if (!SvOK (sv)) |
480 | encode_ch (enc, 0xe0 | 22); |
562 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
481 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
563 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
482 | encode_ch (enc, 0xe0 | 23); |
564 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
483 | else |
565 | else |
484 | 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", |
485 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
567 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
486 | } |
568 | } |
487 | |
569 | |
… | |
… | |
494 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
576 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
495 | enc.cur = SvPVX (enc.sv); |
577 | enc.cur = SvPVX (enc.sv); |
496 | enc.end = SvEND (enc.sv); |
578 | enc.end = SvEND (enc.sv); |
497 | |
579 | |
498 | SvPOK_only (enc.sv); |
580 | SvPOK_only (enc.sv); |
|
|
581 | |
|
|
582 | if (cbor->flags & F_PACK_STRINGS) |
|
|
583 | { |
|
|
584 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
585 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
586 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
587 | } |
|
|
588 | |
499 | encode_sv (&enc, scalar); |
589 | encode_sv (&enc, scalar); |
500 | |
590 | |
501 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
591 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
502 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
592 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
503 | |
593 | |
… | |
… | |
517 | U8 *end; // end of input string |
607 | U8 *end; // end of input string |
518 | const char *err; // parse error, if != 0 |
608 | const char *err; // parse error, if != 0 |
519 | CBOR cbor; |
609 | CBOR cbor; |
520 | U32 depth; // recursion depth |
610 | U32 depth; // recursion depth |
521 | U32 maxdepth; // recursion depth limit |
611 | U32 maxdepth; // recursion depth limit |
522 | AV *sharable; |
612 | AV *shareable; |
|
|
613 | AV *stringref; |
|
|
614 | SV *decode_tagged; |
523 | } dec_t; |
615 | } dec_t; |
524 | |
616 | |
525 | #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 |
526 | |
618 | |
527 | #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") |
… | |
… | |
530 | #define DEC_DEC_DEPTH --dec->depth |
622 | #define DEC_DEC_DEPTH --dec->depth |
531 | |
623 | |
532 | static UV |
624 | static UV |
533 | decode_uint (dec_t *dec) |
625 | decode_uint (dec_t *dec) |
534 | { |
626 | { |
535 | 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) |
536 | { |
634 | { |
537 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
635 | case LENGTH_EXT1: |
538 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
636 | WANT (1); |
539 | case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
637 | dec->cur += 1; |
540 | return *dec->cur++ & 31; |
638 | return dec->cur[-1]; |
541 | |
639 | |
542 | case 24: |
640 | case LENGTH_EXT2: |
543 | WANT (2); |
641 | WANT (2); |
544 | dec->cur += 2; |
642 | dec->cur += 2; |
545 | return dec->cur[-1]; |
|
|
546 | |
|
|
547 | case 25: |
|
|
548 | WANT (3); |
|
|
549 | dec->cur += 3; |
|
|
550 | return (((UV)dec->cur[-2]) << 8) |
643 | return (((UV)dec->cur[-2]) << 8) |
551 | | ((UV)dec->cur[-1]); |
644 | | ((UV)dec->cur[-1]); |
552 | |
645 | |
553 | case 26: |
646 | case LENGTH_EXT4: |
554 | WANT (5); |
647 | WANT (4); |
555 | dec->cur += 5; |
648 | dec->cur += 4; |
556 | return (((UV)dec->cur[-4]) << 24) |
649 | return (((UV)dec->cur[-4]) << 24) |
557 | | (((UV)dec->cur[-3]) << 16) |
650 | | (((UV)dec->cur[-3]) << 16) |
558 | | (((UV)dec->cur[-2]) << 8) |
651 | | (((UV)dec->cur[-2]) << 8) |
559 | | ((UV)dec->cur[-1]); |
652 | | ((UV)dec->cur[-1]); |
560 | |
653 | |
561 | case 27: |
654 | case LENGTH_EXT8: |
562 | WANT (9); |
655 | WANT (8); |
563 | dec->cur += 9; |
656 | dec->cur += 8; |
|
|
657 | |
|
|
658 | return |
|
|
659 | #if UVSIZE < 8 |
|
|
660 | 0 |
|
|
661 | #else |
564 | return (((UV)dec->cur[-8]) << 56) |
662 | (((UV)dec->cur[-8]) << 56) |
565 | | (((UV)dec->cur[-7]) << 48) |
663 | | (((UV)dec->cur[-7]) << 48) |
566 | | (((UV)dec->cur[-6]) << 40) |
664 | | (((UV)dec->cur[-6]) << 40) |
567 | | (((UV)dec->cur[-5]) << 32) |
665 | | (((UV)dec->cur[-5]) << 32) |
|
|
666 | #endif |
568 | | (((UV)dec->cur[-4]) << 24) |
667 | | (((UV)dec->cur[-4]) << 24) |
569 | | (((UV)dec->cur[-3]) << 16) |
668 | | (((UV)dec->cur[-3]) << 16) |
570 | | (((UV)dec->cur[-2]) << 8) |
669 | | (((UV)dec->cur[-2]) << 8) |
571 | | ((UV)dec->cur[-1]); |
670 | | ((UV)dec->cur[-1]); |
572 | |
671 | |
… | |
… | |
585 | { |
684 | { |
586 | AV *av = newAV (); |
685 | AV *av = newAV (); |
587 | |
686 | |
588 | DEC_INC_DEPTH; |
687 | DEC_INC_DEPTH; |
589 | |
688 | |
590 | if ((*dec->cur & 31) == 31) |
689 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
591 | { |
690 | { |
592 | ++dec->cur; |
691 | ++dec->cur; |
593 | |
692 | |
594 | for (;;) |
693 | for (;;) |
595 | { |
694 | { |
596 | WANT (1); |
695 | WANT (1); |
597 | |
696 | |
598 | if (*dec->cur == (0xe0 | 31)) |
697 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
599 | { |
698 | { |
600 | ++dec->cur; |
699 | ++dec->cur; |
601 | break; |
700 | break; |
602 | } |
701 | } |
603 | |
702 | |
… | |
… | |
625 | |
724 | |
626 | static void |
725 | static void |
627 | decode_he (dec_t *dec, HV *hv) |
726 | decode_he (dec_t *dec, HV *hv) |
628 | { |
727 | { |
629 | // for speed reasons, we specialcase single-string |
728 | // for speed reasons, we specialcase single-string |
630 | // byte or utf-8 strings as keys. |
729 | // byte or utf-8 strings as keys, but only when !stringref |
631 | |
730 | |
632 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
731 | if (ecb_expect_true (!dec->stringref)) |
|
|
732 | if ((*dec->cur - MAJOR_BYTES) <= 27) |
633 | { |
733 | { |
634 | I32 len = decode_uint (dec); |
734 | I32 len = decode_uint (dec); |
635 | char *key = (char *)dec->cur; |
735 | char *key = (char *)dec->cur; |
636 | |
736 | |
637 | dec->cur += len; |
737 | dec->cur += len; |
638 | |
738 | |
|
|
739 | if (ecb_expect_false (dec->stringref)) |
|
|
740 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
741 | |
639 | hv_store (hv, key, len, decode_sv (dec), 0); |
742 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
743 | |
|
|
744 | return; |
640 | } |
745 | } |
641 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
746 | else if ((*dec->cur - MAJOR_TEXT) <= 27) |
642 | { |
747 | { |
643 | I32 len = decode_uint (dec); |
748 | I32 len = decode_uint (dec); |
644 | char *key = (char *)dec->cur; |
749 | char *key = (char *)dec->cur; |
645 | |
750 | |
646 | dec->cur += len; |
751 | dec->cur += len; |
647 | |
752 | |
|
|
753 | if (ecb_expect_false (dec->stringref)) |
|
|
754 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
|
|
755 | |
648 | hv_store (hv, key, -len, decode_sv (dec), 0); |
756 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
757 | |
|
|
758 | return; |
649 | } |
759 | } |
650 | else |
760 | |
651 | { |
|
|
652 | SV *k = decode_sv (dec); |
761 | SV *k = decode_sv (dec); |
653 | SV *v = decode_sv (dec); |
762 | SV *v = decode_sv (dec); |
654 | |
763 | |
655 | hv_store_ent (hv, k, v, 0); |
764 | hv_store_ent (hv, k, v, 0); |
656 | SvREFCNT_dec (k); |
765 | SvREFCNT_dec (k); |
657 | } |
|
|
658 | } |
766 | } |
659 | |
767 | |
660 | static SV * |
768 | static SV * |
661 | decode_hv (dec_t *dec) |
769 | decode_hv (dec_t *dec) |
662 | { |
770 | { |
663 | HV *hv = newHV (); |
771 | HV *hv = newHV (); |
664 | |
772 | |
665 | DEC_INC_DEPTH; |
773 | DEC_INC_DEPTH; |
666 | |
774 | |
667 | if ((*dec->cur & 31) == 31) |
775 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
668 | { |
776 | { |
669 | ++dec->cur; |
777 | ++dec->cur; |
670 | |
778 | |
671 | for (;;) |
779 | for (;;) |
672 | { |
780 | { |
673 | WANT (1); |
781 | WANT (1); |
674 | |
782 | |
675 | if (*dec->cur == (0xe0 | 31)) |
783 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
676 | { |
784 | { |
677 | ++dec->cur; |
785 | ++dec->cur; |
678 | break; |
786 | break; |
679 | } |
787 | } |
680 | |
788 | |
… | |
… | |
701 | static SV * |
809 | static SV * |
702 | decode_str (dec_t *dec, int utf8) |
810 | decode_str (dec_t *dec, int utf8) |
703 | { |
811 | { |
704 | SV *sv = 0; |
812 | SV *sv = 0; |
705 | |
813 | |
706 | if ((*dec->cur & 31) == 31) |
814 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
707 | { |
815 | { |
|
|
816 | // indefinite length strings |
708 | ++dec->cur; |
817 | ++dec->cur; |
709 | |
818 | |
|
|
819 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
820 | |
710 | sv = newSVpvn ("", 0); |
821 | sv = newSVpvn ("", 0); |
711 | |
822 | |
712 | // not very fast, and certainly not robust against illegal input |
|
|
713 | for (;;) |
823 | for (;;) |
714 | { |
824 | { |
715 | WANT (1); |
825 | WANT (1); |
716 | |
826 | |
717 | if (*dec->cur == (0xe0 | 31)) |
827 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
828 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
718 | { |
829 | { |
719 | ++dec->cur; |
830 | ++dec->cur; |
720 | break; |
831 | break; |
721 | } |
832 | } |
|
|
833 | else |
|
|
834 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
722 | |
835 | |
723 | 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; |
724 | } |
841 | } |
725 | } |
842 | } |
726 | else |
843 | else |
727 | { |
844 | { |
728 | STRLEN len = decode_uint (dec); |
845 | STRLEN len = decode_uint (dec); |
729 | |
846 | |
730 | WANT (len); |
847 | WANT (len); |
731 | sv = newSVpvn (dec->cur, len); |
848 | sv = newSVpvn (dec->cur, len); |
732 | 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)); |
733 | } |
854 | } |
734 | |
855 | |
735 | if (utf8) |
856 | if (utf8) |
736 | SvUTF8_on (sv); |
857 | SvUTF8_on (sv); |
737 | |
858 | |
… | |
… | |
743 | } |
864 | } |
744 | |
865 | |
745 | static SV * |
866 | static SV * |
746 | decode_tagged (dec_t *dec) |
867 | decode_tagged (dec_t *dec) |
747 | { |
868 | { |
|
|
869 | SV *sv = 0; |
748 | UV tag = decode_uint (dec); |
870 | UV tag = decode_uint (dec); |
749 | SV *sv = decode_sv (dec); |
871 | |
|
|
872 | WANT (1); |
750 | |
873 | |
751 | switch (tag) |
874 | switch (tag) |
752 | { |
875 | { |
753 | case CBOR_TAG_MAGIC: |
876 | case CBOR_TAG_MAGIC: |
754 | return sv; |
877 | sv = decode_sv (dec); |
|
|
878 | break; |
755 | |
879 | |
756 | case CBOR_TAG_INDIRECTION: |
880 | case CBOR_TAG_INDIRECTION: |
757 | return newRV_noinc (sv); |
881 | sv = newRV_noinc (decode_sv (dec)); |
|
|
882 | break; |
758 | |
883 | |
|
|
884 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
885 | { |
|
|
886 | ENTER; SAVETMPS; |
|
|
887 | |
|
|
888 | SAVESPTR (dec->stringref); |
|
|
889 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
890 | |
|
|
891 | sv = decode_sv (dec); |
|
|
892 | |
|
|
893 | FREETMPS; LEAVE; |
|
|
894 | } |
|
|
895 | break; |
|
|
896 | |
|
|
897 | case CBOR_TAG_STRINGREF: |
|
|
898 | { |
|
|
899 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
|
|
900 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
901 | |
|
|
902 | UV idx = decode_uint (dec); |
|
|
903 | |
|
|
904 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
905 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
906 | |
|
|
907 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
908 | } |
|
|
909 | break; |
|
|
910 | |
759 | case CBOR_TAG_VALUE_SHARABLE: |
911 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
912 | { |
760 | if (ecb_expect_false (!dec->sharable)) |
913 | if (ecb_expect_false (!dec->shareable)) |
761 | dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); |
914 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
762 | |
915 | |
|
|
916 | sv = newSV (0); |
763 | av_push (dec->sharable, SvREFCNT_inc_NN (sv)); |
917 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
764 | |
918 | |
765 | return sv; |
919 | SV *osv = decode_sv (dec); |
|
|
920 | sv_setsv (sv, osv); |
|
|
921 | SvREFCNT_dec_NN (osv); |
|
|
922 | } |
|
|
923 | break; |
766 | |
924 | |
767 | case CBOR_TAG_VALUE_SHAREDREF: |
925 | case CBOR_TAG_VALUE_SHAREDREF: |
768 | { |
926 | { |
769 | // TODO: should verify that the sv atcually was a CBOR unsigned integer |
927 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
770 | UV idx = SvUV (sv); |
928 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
771 | |
929 | |
|
|
930 | UV idx = decode_uint (dec); |
|
|
931 | |
772 | if (!dec->sharable || idx > AvFILLp (dec->sharable)) |
932 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
773 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
933 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
774 | |
934 | |
775 | SvREFCNT_dec (sv); |
|
|
776 | |
|
|
777 | return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); |
935 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
778 | } |
936 | } |
|
|
937 | break; |
779 | |
938 | |
780 | case CBOR_TAG_PERL_OBJECT: |
939 | case CBOR_TAG_PERL_OBJECT: |
781 | { |
940 | { |
|
|
941 | sv = decode_sv (dec); |
|
|
942 | |
782 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
943 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
783 | ERR ("corrupted CBOR data (non-array perl object)"); |
944 | ERR ("corrupted CBOR data (non-array perl object)"); |
784 | |
945 | |
785 | AV *av = (AV *)SvRV (sv); |
946 | AV *av = (AV *)SvRV (sv); |
786 | int len = av_len (av) + 1; |
947 | int len = av_len (av) + 1; |
… | |
… | |
821 | sv = SvREFCNT_inc (POPs); |
982 | sv = SvREFCNT_inc (POPs); |
822 | |
983 | |
823 | PUTBACK; |
984 | PUTBACK; |
824 | |
985 | |
825 | FREETMPS; LEAVE; |
986 | FREETMPS; LEAVE; |
826 | |
|
|
827 | return sv; |
|
|
828 | } |
987 | } |
|
|
988 | break; |
829 | |
989 | |
830 | default: |
990 | default: |
831 | { |
991 | { |
|
|
992 | sv = decode_sv (dec); |
|
|
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 | { |
832 | AV *av = newAV (); |
1017 | AV *av = newAV (); |
833 | av_push (av, newSVuv (tag)); |
1018 | av_push (av, newSVuv (tag)); |
834 | av_push (av, sv); |
1019 | av_push (av, sv); |
835 | |
1020 | |
836 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1021 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
837 | ? cbor_tagged_stash |
1022 | ? cbor_tagged_stash |
838 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1023 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
839 | |
|
|
840 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
1024 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
841 | } |
1025 | } |
|
|
1026 | |
|
|
1027 | PUTBACK; |
|
|
1028 | |
|
|
1029 | FREETMPS; LEAVE; |
|
|
1030 | } |
|
|
1031 | break; |
842 | } |
1032 | } |
|
|
1033 | |
|
|
1034 | return sv; |
843 | |
1035 | |
844 | fail: |
1036 | fail: |
845 | SvREFCNT_dec (sv); |
1037 | SvREFCNT_dec (sv); |
846 | return &PL_sv_undef; |
1038 | return &PL_sv_undef; |
847 | } |
1039 | } |
… | |
… | |
849 | static SV * |
1041 | static SV * |
850 | decode_sv (dec_t *dec) |
1042 | decode_sv (dec_t *dec) |
851 | { |
1043 | { |
852 | WANT (1); |
1044 | WANT (1); |
853 | |
1045 | |
854 | switch (*dec->cur >> 5) |
1046 | switch (*dec->cur >> MAJOR_SHIFT) |
855 | { |
1047 | { |
856 | case 0: // unsigned int |
1048 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
857 | return newSVuv (decode_uint (dec)); |
1049 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
858 | case 1: // negative int |
1050 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
859 | return newSViv (-1 - (IV)decode_uint (dec)); |
1051 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
860 | case 2: // octet string |
1052 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
861 | return decode_str (dec, 0); |
1053 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
862 | case 3: // utf-8 string |
1054 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
863 | return decode_str (dec, 1); |
1055 | |
864 | case 4: // array |
1056 | case MAJOR_MISC >> MAJOR_SHIFT: |
865 | return decode_av (dec); |
|
|
866 | case 5: // map |
|
|
867 | return decode_hv (dec); |
|
|
868 | case 6: // tag |
|
|
869 | return decode_tagged (dec); |
|
|
870 | case 7: // misc |
|
|
871 | switch (*dec->cur++ & 31) |
1057 | switch (*dec->cur++ & MINOR_MASK) |
872 | { |
1058 | { |
873 | case 20: |
1059 | case SIMPLE_FALSE: |
874 | #if CBOR_SLOW |
1060 | #if CBOR_SLOW |
875 | types_false = get_bool ("Types::Serialiser::false"); |
1061 | types_false = get_bool ("Types::Serialiser::false"); |
876 | #endif |
1062 | #endif |
877 | return newSVsv (types_false); |
1063 | return newSVsv (types_false); |
878 | case 21: |
1064 | case SIMPLE_TRUE: |
879 | #if CBOR_SLOW |
1065 | #if CBOR_SLOW |
880 | types_true = get_bool ("Types::Serialiser::true"); |
1066 | types_true = get_bool ("Types::Serialiser::true"); |
881 | #endif |
1067 | #endif |
882 | return newSVsv (types_true); |
1068 | return newSVsv (types_true); |
883 | case 22: |
1069 | case SIMPLE_NULL: |
884 | return newSVsv (&PL_sv_undef); |
1070 | return newSVsv (&PL_sv_undef); |
885 | case 23: |
1071 | case SIMPLE_UNDEF: |
886 | #if CBOR_SLOW |
1072 | #if CBOR_SLOW |
887 | types_error = get_bool ("Types::Serialiser::error"); |
1073 | types_error = get_bool ("Types::Serialiser::error"); |
888 | #endif |
1074 | #endif |
889 | return newSVsv (types_error); |
1075 | return newSVsv (types_error); |
890 | |
1076 | |
891 | case 25: |
1077 | case MISC_FLOAT16: |
892 | { |
1078 | { |
893 | WANT (2); |
1079 | WANT (2); |
894 | |
1080 | |
895 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1081 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
896 | dec->cur += 2; |
1082 | dec->cur += 2; |
897 | |
1083 | |
898 | return newSVnv (ecb_binary16_to_float (fp)); |
1084 | return newSVnv (ecb_binary16_to_float (fp)); |
899 | } |
1085 | } |
900 | |
1086 | |
901 | case 26: |
1087 | case MISC_FLOAT32: |
902 | { |
1088 | { |
903 | uint32_t fp; |
1089 | uint32_t fp; |
904 | WANT (4); |
1090 | WANT (4); |
905 | memcpy (&fp, dec->cur, 4); |
1091 | memcpy (&fp, dec->cur, 4); |
906 | dec->cur += 4; |
1092 | dec->cur += 4; |
… | |
… | |
909 | fp = ecb_bswap32 (fp); |
1095 | fp = ecb_bswap32 (fp); |
910 | |
1096 | |
911 | return newSVnv (ecb_binary32_to_float (fp)); |
1097 | return newSVnv (ecb_binary32_to_float (fp)); |
912 | } |
1098 | } |
913 | |
1099 | |
914 | case 27: |
1100 | case MISC_FLOAT64: |
915 | { |
1101 | { |
916 | uint64_t fp; |
1102 | uint64_t fp; |
917 | WANT (8); |
1103 | WANT (8); |
918 | memcpy (&fp, dec->cur, 8); |
1104 | memcpy (&fp, dec->cur, 8); |
919 | dec->cur += 8; |
1105 | dec->cur += 8; |
… | |
… | |
922 | fp = ecb_bswap64 (fp); |
1108 | fp = ecb_bswap64 (fp); |
923 | |
1109 | |
924 | return newSVnv (ecb_binary64_to_double (fp)); |
1110 | return newSVnv (ecb_binary64_to_double (fp)); |
925 | } |
1111 | } |
926 | |
1112 | |
927 | // 0..19 unassigned |
1113 | // 0..19 unassigned simple |
928 | // 24 reserved + unassigned (reserved values are not encodable) |
1114 | // 24 reserved + unassigned (reserved values are not encodable) |
929 | default: |
1115 | default: |
930 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1116 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
931 | } |
1117 | } |
932 | |
1118 | |
… | |
… | |
987 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1173 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
988 | |
1174 | |
989 | types_true = get_bool ("Types::Serialiser::true" ); |
1175 | types_true = get_bool ("Types::Serialiser::true" ); |
990 | types_false = get_bool ("Types::Serialiser::false"); |
1176 | types_false = get_bool ("Types::Serialiser::false"); |
991 | 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); |
992 | |
1180 | |
993 | sv_cbor = newSVpv ("CBOR", 0); |
1181 | sv_cbor = newSVpv ("CBOR", 0); |
994 | SvREADONLY_on (sv_cbor); |
1182 | SvREADONLY_on (sv_cbor); |
995 | } |
1183 | } |
996 | |
1184 | |
… | |
… | |
1018 | void shrink (CBOR *self, int enable = 1) |
1206 | void shrink (CBOR *self, int enable = 1) |
1019 | ALIAS: |
1207 | ALIAS: |
1020 | shrink = F_SHRINK |
1208 | shrink = F_SHRINK |
1021 | allow_unknown = F_ALLOW_UNKNOWN |
1209 | allow_unknown = F_ALLOW_UNKNOWN |
1022 | allow_sharing = F_ALLOW_SHARING |
1210 | allow_sharing = F_ALLOW_SHARING |
1023 | dedup_keys = F_DEDUP_KEYS |
|
|
1024 | dedup_strings = F_DEDUP_STRINGS |
1211 | pack_strings = F_PACK_STRINGS |
1025 | PPCODE: |
1212 | PPCODE: |
1026 | { |
1213 | { |
1027 | if (enable) |
1214 | if (enable) |
1028 | self->flags |= ix; |
1215 | self->flags |= ix; |
1029 | else |
1216 | else |
… | |
… | |
1035 | void get_shrink (CBOR *self) |
1222 | void get_shrink (CBOR *self) |
1036 | ALIAS: |
1223 | ALIAS: |
1037 | get_shrink = F_SHRINK |
1224 | get_shrink = F_SHRINK |
1038 | get_allow_unknown = F_ALLOW_UNKNOWN |
1225 | get_allow_unknown = F_ALLOW_UNKNOWN |
1039 | get_allow_sharing = F_ALLOW_SHARING |
1226 | get_allow_sharing = F_ALLOW_SHARING |
1040 | get_dedup_keys = F_DEDUP_KEYS |
|
|
1041 | get_dedup_strings = F_DEDUP_STRINGS |
1227 | get_pack_strings = F_PACK_STRINGS |
1042 | PPCODE: |
1228 | PPCODE: |
1043 | XPUSHs (boolSV (self->flags & ix)); |
1229 | XPUSHs (boolSV (self->flags & ix)); |
1044 | |
1230 | |
1045 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1231 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1046 | PPCODE: |
1232 | PPCODE: |
… | |
… | |
1059 | XPUSHs (ST (0)); |
1245 | XPUSHs (ST (0)); |
1060 | |
1246 | |
1061 | int get_max_size (CBOR *self) |
1247 | int get_max_size (CBOR *self) |
1062 | CODE: |
1248 | CODE: |
1063 | 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); |
1064 | OUTPUT: |
1262 | OUTPUT: |
1065 | RETVAL |
1263 | RETVAL |
1066 | |
1264 | |
1067 | void encode (CBOR *self, SV *scalar) |
1265 | void encode (CBOR *self, SV *scalar) |
1068 | PPCODE: |
1266 | PPCODE: |
… | |
… | |
1083 | EXTEND (SP, 2); |
1281 | EXTEND (SP, 2); |
1084 | PUSHs (sv); |
1282 | PUSHs (sv); |
1085 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1283 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1086 | } |
1284 | } |
1087 | |
1285 | |
|
|
1286 | void DESTROY (CBOR *self) |
|
|
1287 | PPCODE: |
|
|
1288 | cbor_free (self); |
|
|
1289 | |
1088 | PROTOTYPES: ENABLE |
1290 | PROTOTYPES: ENABLE |
1089 | |
1291 | |
1090 | void encode_cbor (SV *scalar) |
1292 | void encode_cbor (SV *scalar) |
1091 | PPCODE: |
1293 | PPCODE: |
1092 | { |
1294 | { |