… | |
… | |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
11 | |
11 | |
|
|
12 | #define ECB_NO_THREADS 1 |
12 | #include "ecb.h" |
13 | #include "ecb.h" |
13 | |
14 | |
|
|
15 | // compatibility with perl <5.18 |
|
|
16 | #ifndef HvNAMELEN_get |
|
|
17 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
18 | #endif |
|
|
19 | #ifndef HvNAMELEN |
|
|
20 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
21 | #endif |
|
|
22 | #ifndef HvNAMEUTF8 |
|
|
23 | # define HvNAMEUTF8(hv) 0 |
|
|
24 | #endif |
|
|
25 | #ifndef SvREFCNT_dec_NN |
|
|
26 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
27 | #endif |
|
|
28 | |
|
|
29 | // known major and minor types |
|
|
30 | enum cbor_type |
|
|
31 | { |
|
|
32 | MAJOR_SHIFT = 5, |
|
|
33 | MINOR_MASK = 0x1f, |
|
|
34 | |
|
|
35 | MAJOR_POS_INT = 0 << MAJOR_SHIFT, |
|
|
36 | MAJOR_NEG_INT = 1 << MAJOR_SHIFT, |
|
|
37 | MAJOR_BYTES = 2 << MAJOR_SHIFT, |
|
|
38 | MAJOR_TEXT = 3 << MAJOR_SHIFT, |
|
|
39 | MAJOR_ARRAY = 4 << MAJOR_SHIFT, |
|
|
40 | MAJOR_MAP = 5 << MAJOR_SHIFT, |
|
|
41 | MAJOR_TAG = 6 << MAJOR_SHIFT, |
|
|
42 | MAJOR_MISC = 7 << MAJOR_SHIFT, |
|
|
43 | |
|
|
44 | // INT/STRING/ARRAY/MAP subtypes |
|
|
45 | LENGTH_EXT1 = 24, |
|
|
46 | LENGTH_EXT2 = 25, |
|
|
47 | LENGTH_EXT4 = 26, |
|
|
48 | LENGTH_EXT8 = 27, |
|
|
49 | |
|
|
50 | // SIMPLE types (effectively MISC subtypes) |
|
|
51 | SIMPLE_FALSE = 20, |
|
|
52 | SIMPLE_TRUE = 21, |
|
|
53 | SIMPLE_NULL = 22, |
|
|
54 | SIMPLE_UNDEF = 23, |
|
|
55 | |
|
|
56 | // MISC subtype (unused) |
|
|
57 | MISC_EXT1 = 24, |
|
|
58 | MISC_FLOAT16 = 25, |
|
|
59 | MISC_FLOAT32 = 26, |
|
|
60 | MISC_FLOAT64 = 27, |
|
|
61 | |
|
|
62 | // BYTES/TEXT/ARRAY/MAP |
|
|
63 | MINOR_INDEF = 31, |
|
|
64 | }; |
|
|
65 | |
|
|
66 | // known tags |
|
|
67 | enum cbor_tag |
|
|
68 | { |
|
|
69 | // extensions |
|
|
70 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
|
|
71 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
|
|
72 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
|
|
73 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
|
|
74 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
|
|
75 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref |
|
|
76 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
|
|
77 | |
|
|
78 | // rfc7049 |
|
|
79 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
|
|
80 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
|
|
81 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
|
|
82 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
|
|
83 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
|
|
84 | CBOR_TAG_BIGFLOAT = 5, // array |
|
|
85 | |
|
|
86 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
|
|
87 | CBOR_TAG_CONV_B64 = 22, // base64, any |
|
|
88 | CBOR_TAG_CONV_HEX = 23, // base16, any |
|
|
89 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
|
|
90 | |
|
|
91 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
|
|
92 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
|
|
93 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
|
|
94 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
|
|
95 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
|
|
96 | |
|
|
97 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
|
|
98 | }; |
|
|
99 | |
14 | #define F_SHRINK 0x00000200UL |
100 | #define F_SHRINK 0x00000001UL |
15 | #define F_ALLOW_UNKNOWN 0x00002000UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
|
|
102 | #define F_ALLOW_SHARING 0x00000004UL |
|
|
103 | #define F_ALLOW_CYCLES 0x00000008UL |
|
|
104 | #define F_FORBID_OBJECTS 0x00000010UL |
|
|
105 | #define F_PACK_STRINGS 0x00000020UL |
|
|
106 | #define F_TEXT_KEYS 0x00000040UL |
|
|
107 | #define F_TEXT_STRINGS 0x00000080UL |
|
|
108 | #define F_VALIDATE_UTF8 0x00000100UL |
16 | |
109 | |
17 | #define INIT_SIZE 32 // initial scalar size to be allocated |
110 | #define INIT_SIZE 32 // initial scalar size to be allocated |
18 | |
111 | |
19 | #define SB do { |
112 | #define SB do { |
20 | #define SE } while (0) |
113 | #define SE } while (0) |
… | |
… | |
31 | #else |
124 | #else |
32 | # define CBOR_SLOW 0 |
125 | # define CBOR_SLOW 0 |
33 | # define CBOR_STASH cbor_stash |
126 | # define CBOR_STASH cbor_stash |
34 | #endif |
127 | #endif |
35 | |
128 | |
36 | static HV *cbor_stash, *cbor_boolean_stash, *cbor_tagged_stash; // CBOR::XS:: |
129 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
37 | static SV *cbor_true, *cbor_false; |
130 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
38 | |
131 | |
39 | typedef struct { |
132 | typedef struct { |
40 | U32 flags; |
133 | U32 flags; |
41 | U32 max_depth; |
134 | U32 max_depth; |
42 | STRLEN max_size; |
135 | STRLEN max_size; |
|
|
136 | SV *filter; |
|
|
137 | |
|
|
138 | // for the incremental parser |
|
|
139 | STRLEN incr_pos; // the current offset into the text |
|
|
140 | STRLEN incr_need; // minimum bytes needed to decode |
|
|
141 | AV *incr_count; // for every nesting level, the number of outstanding values, or -1 for indef. |
43 | } CBOR; |
142 | } CBOR; |
44 | |
143 | |
45 | ecb_inline void |
144 | ecb_inline void |
46 | cbor_init (CBOR *cbor) |
145 | cbor_init (CBOR *cbor) |
47 | { |
146 | { |
48 | Zero (cbor, 1, CBOR); |
147 | Zero (cbor, 1, CBOR); |
49 | cbor->max_depth = 512; |
148 | cbor->max_depth = 512; |
|
|
149 | } |
|
|
150 | |
|
|
151 | ecb_inline void |
|
|
152 | cbor_free (CBOR *cbor) |
|
|
153 | { |
|
|
154 | SvREFCNT_dec (cbor->filter); |
|
|
155 | SvREFCNT_dec (cbor->incr_count); |
50 | } |
156 | } |
51 | |
157 | |
52 | ///////////////////////////////////////////////////////////////////////////// |
158 | ///////////////////////////////////////////////////////////////////////////// |
53 | // utility functions |
159 | // utility functions |
54 | |
160 | |
… | |
… | |
76 | SvPV_renew (sv, SvCUR (sv) + 1); |
182 | SvPV_renew (sv, SvCUR (sv) + 1); |
77 | #endif |
183 | #endif |
78 | } |
184 | } |
79 | } |
185 | } |
80 | |
186 | |
81 | ///////////////////////////////////////////////////////////////////////////// |
187 | // minimum length of a string to be registered for stringref |
82 | // fp hell |
188 | ecb_inline int |
83 | |
189 | minimum_string_length (UV idx) |
84 | //TODO |
190 | { |
|
|
191 | return idx > 23 |
|
|
192 | ? idx > 0xffU |
|
|
193 | ? idx > 0xffffU |
|
|
194 | ? idx > 0xffffffffU |
|
|
195 | ? 11 |
|
|
196 | : 7 |
|
|
197 | : 5 |
|
|
198 | : 4 |
|
|
199 | : 3; |
|
|
200 | } |
85 | |
201 | |
86 | ///////////////////////////////////////////////////////////////////////////// |
202 | ///////////////////////////////////////////////////////////////////////////// |
87 | // encoder |
203 | // encoder |
88 | |
204 | |
89 | // structure used for encoding CBOR |
205 | // structure used for encoding CBOR |
… | |
… | |
92 | char *cur; // SvPVX (sv) + current output position |
208 | char *cur; // SvPVX (sv) + current output position |
93 | char *end; // SvEND (sv) |
209 | char *end; // SvEND (sv) |
94 | SV *sv; // result scalar |
210 | SV *sv; // result scalar |
95 | CBOR cbor; |
211 | CBOR cbor; |
96 | U32 depth; // recursion level |
212 | U32 depth; // recursion level |
|
|
213 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
214 | UV stringref_idx; |
|
|
215 | HV *shareable; // ptr => index, or 0 |
|
|
216 | UV shareable_idx; |
97 | } enc_t; |
217 | } enc_t; |
98 | |
218 | |
99 | ecb_inline void |
219 | ecb_inline void |
100 | need (enc_t *enc, STRLEN len) |
220 | need (enc_t *enc, STRLEN len) |
101 | { |
221 | { |
… | |
… | |
118 | static void |
238 | static void |
119 | encode_uint (enc_t *enc, int major, UV len) |
239 | encode_uint (enc_t *enc, int major, UV len) |
120 | { |
240 | { |
121 | need (enc, 9); |
241 | need (enc, 9); |
122 | |
242 | |
123 | if (len < 24) |
243 | if (ecb_expect_true (len < LENGTH_EXT1)) |
124 | *enc->cur++ = major | len; |
244 | *enc->cur++ = major | len; |
125 | else if (len <= 0xff) |
245 | else if (ecb_expect_true (len <= 0xffU)) |
126 | { |
246 | { |
127 | *enc->cur++ = major | 24; |
247 | *enc->cur++ = major | LENGTH_EXT1; |
128 | *enc->cur++ = len; |
248 | *enc->cur++ = len; |
129 | } |
249 | } |
130 | else if (len <= 0xffff) |
250 | else if (len <= 0xffffU) |
131 | { |
251 | { |
132 | *enc->cur++ = major | 25; |
252 | *enc->cur++ = major | LENGTH_EXT2; |
133 | *enc->cur++ = len >> 8; |
253 | *enc->cur++ = len >> 8; |
134 | *enc->cur++ = len; |
254 | *enc->cur++ = len; |
135 | } |
255 | } |
136 | else if (len <= 0xffffffff) |
256 | else if (len <= 0xffffffffU) |
137 | { |
257 | { |
138 | *enc->cur++ = major | 26; |
258 | *enc->cur++ = major | LENGTH_EXT4; |
139 | *enc->cur++ = len >> 24; |
259 | *enc->cur++ = len >> 24; |
140 | *enc->cur++ = len >> 16; |
260 | *enc->cur++ = len >> 16; |
141 | *enc->cur++ = len >> 8; |
261 | *enc->cur++ = len >> 8; |
142 | *enc->cur++ = len; |
262 | *enc->cur++ = len; |
143 | } |
263 | } |
144 | else |
264 | else |
145 | { |
265 | { |
146 | *enc->cur++ = major | 27; |
266 | *enc->cur++ = major | LENGTH_EXT8; |
147 | *enc->cur++ = len >> 56; |
267 | *enc->cur++ = len >> 56; |
148 | *enc->cur++ = len >> 48; |
268 | *enc->cur++ = len >> 48; |
149 | *enc->cur++ = len >> 40; |
269 | *enc->cur++ = len >> 40; |
150 | *enc->cur++ = len >> 32; |
270 | *enc->cur++ = len >> 32; |
151 | *enc->cur++ = len >> 24; |
271 | *enc->cur++ = len >> 24; |
… | |
… | |
153 | *enc->cur++ = len >> 8; |
273 | *enc->cur++ = len >> 8; |
154 | *enc->cur++ = len; |
274 | *enc->cur++ = len; |
155 | } |
275 | } |
156 | } |
276 | } |
157 | |
277 | |
158 | static void |
278 | ecb_inline void |
|
|
279 | encode_tag (enc_t *enc, UV tag) |
|
|
280 | { |
|
|
281 | encode_uint (enc, MAJOR_TAG, tag); |
|
|
282 | } |
|
|
283 | |
|
|
284 | // exceptional (hopefully) slow path for byte strings that need to be utf8-encoded |
|
|
285 | ecb_noinline static void |
159 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
286 | encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len) |
160 | { |
287 | { |
161 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
288 | STRLEN ulen = len; |
|
|
289 | U8 *p, *pend = (U8 *)str + len; |
|
|
290 | |
|
|
291 | for (p = (U8 *)str; p < pend; ++p) |
|
|
292 | ulen += *p >> 7; // count set high bits |
|
|
293 | |
|
|
294 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
295 | |
|
|
296 | need (enc, ulen); |
|
|
297 | for (p = (U8 *)str; p < pend; ++p) |
|
|
298 | if (*p < 0x80) |
|
|
299 | *enc->cur++ = *p; |
|
|
300 | else |
|
|
301 | { |
|
|
302 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
303 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
304 | } |
|
|
305 | } |
|
|
306 | |
|
|
307 | ecb_inline void |
|
|
308 | encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
|
|
309 | { |
|
|
310 | if (ecb_expect_false (upgrade_utf8)) |
|
|
311 | if (!utf8) |
|
|
312 | { |
|
|
313 | encode_str_utf8 (enc, utf8, str, len); |
|
|
314 | return; |
|
|
315 | } |
|
|
316 | |
|
|
317 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
162 | need (enc, len); |
318 | need (enc, len); |
163 | memcpy (enc->cur, str, len); |
319 | memcpy (enc->cur, str, len); |
164 | enc->cur += len; |
320 | enc->cur += len; |
165 | } |
321 | } |
166 | |
322 | |
|
|
323 | ecb_inline void |
|
|
324 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
|
|
325 | { |
|
|
326 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
|
|
327 | { |
|
|
328 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
|
|
329 | |
|
|
330 | if (SvOK (*svp)) |
|
|
331 | { |
|
|
332 | // already registered, use stringref |
|
|
333 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
334 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
335 | return; |
|
|
336 | } |
|
|
337 | else if (len >= minimum_string_length (enc->stringref_idx)) |
|
|
338 | { |
|
|
339 | // register only |
|
|
340 | sv_setuv (*svp, enc->stringref_idx); |
|
|
341 | ++enc->stringref_idx; |
|
|
342 | } |
|
|
343 | } |
|
|
344 | |
|
|
345 | encode_str (enc, upgrade_utf8, utf8, str, len); |
|
|
346 | } |
|
|
347 | |
167 | static void encode_sv (enc_t *enc, SV *sv); |
348 | static void encode_sv (enc_t *enc, SV *sv); |
168 | |
349 | |
169 | static void |
350 | static void |
170 | encode_av (enc_t *enc, AV *av) |
351 | encode_av (enc_t *enc, AV *av) |
171 | { |
352 | { |
… | |
… | |
174 | if (enc->depth >= enc->cbor.max_depth) |
355 | if (enc->depth >= enc->cbor.max_depth) |
175 | croak (ERR_NESTING_EXCEEDED); |
356 | croak (ERR_NESTING_EXCEEDED); |
176 | |
357 | |
177 | ++enc->depth; |
358 | ++enc->depth; |
178 | |
359 | |
179 | encode_uint (enc, 0x80, len + 1); |
360 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
180 | |
361 | |
|
|
362 | if (SvMAGICAL (av)) |
181 | for (i = 0; i <= len; ++i) |
363 | for (i = 0; i <= len; ++i) |
182 | { |
364 | { |
183 | SV **svp = av_fetch (av, i, 0); |
365 | SV **svp = av_fetch (av, i, 0); |
184 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
366 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
185 | } |
367 | } |
|
|
368 | else |
|
|
369 | for (i = 0; i <= len; ++i) |
|
|
370 | { |
|
|
371 | SV *sv = AvARRAY (av)[i]; |
|
|
372 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
373 | } |
186 | |
374 | |
187 | --enc->depth; |
375 | --enc->depth; |
188 | } |
376 | } |
189 | |
377 | |
190 | static void |
378 | static void |
… | |
… | |
199 | |
387 | |
200 | int pairs = hv_iterinit (hv); |
388 | int pairs = hv_iterinit (hv); |
201 | int mg = SvMAGICAL (hv); |
389 | int mg = SvMAGICAL (hv); |
202 | |
390 | |
203 | if (mg) |
391 | if (mg) |
204 | encode_ch (enc, 0xa0 | 31); |
392 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
205 | else |
393 | else |
206 | encode_uint (enc, 0xa0, pairs); |
394 | encode_uint (enc, MAJOR_MAP, pairs); |
207 | |
395 | |
208 | while ((he = hv_iternext (hv))) |
396 | while ((he = hv_iternext (hv))) |
209 | { |
397 | { |
210 | if (HeKLEN (he) == HEf_SVKEY) |
398 | if (HeKLEN (he) == HEf_SVKEY) |
211 | encode_sv (enc, HeSVKEY (he)); |
399 | encode_sv (enc, HeSVKEY (he)); |
212 | else |
400 | else |
213 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
401 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
214 | |
402 | |
215 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
403 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
216 | } |
404 | } |
217 | |
405 | |
218 | if (mg) |
406 | if (mg) |
219 | encode_ch (enc, 0xe0 | 31); |
407 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
220 | |
408 | |
221 | --enc->depth; |
409 | --enc->depth; |
222 | } |
410 | } |
223 | |
411 | |
224 | // encode objects, arrays and special \0=false and \1=true values. |
412 | // encode objects, arrays and special \0=false and \1=true values. |
225 | static void |
413 | static void |
226 | encode_rv (enc_t *enc, SV *sv) |
414 | encode_rv (enc_t *enc, SV *sv) |
227 | { |
415 | { |
228 | svtype svt; |
|
|
229 | |
|
|
230 | SvGETMAGIC (sv); |
416 | SvGETMAGIC (sv); |
|
|
417 | |
231 | svt = SvTYPE (sv); |
418 | svtype svt = SvTYPE (sv); |
232 | |
419 | |
233 | if (ecb_expect_false (SvOBJECT (sv))) |
420 | if (ecb_expect_false (SvOBJECT (sv))) |
234 | { |
421 | { |
235 | HV *boolean_stash = !CBOR_SLOW || cbor_boolean_stash |
422 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
236 | ? cbor_boolean_stash |
423 | ? types_boolean_stash |
237 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
424 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
425 | HV *error_stash = !CBOR_SLOW || types_error_stash |
|
|
426 | ? types_error_stash |
|
|
427 | : gv_stashpv ("Types::Serialiser::Error", 1); |
238 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
428 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
239 | ? cbor_tagged_stash |
429 | ? cbor_tagged_stash |
240 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
430 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
241 | |
431 | |
|
|
432 | HV *stash = SvSTASH (sv); |
|
|
433 | |
242 | if (SvSTASH (sv) == boolean_stash) |
434 | if (stash == boolean_stash) |
243 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
435 | { |
|
|
436 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
437 | return; |
|
|
438 | } |
|
|
439 | else if (stash == error_stash) |
|
|
440 | { |
|
|
441 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
442 | return; |
|
|
443 | } |
244 | else if (SvSTASH (sv) == tagged_stash) |
444 | else if (stash == tagged_stash) |
245 | { |
445 | { |
246 | if (svt != SVt_PVAV) |
446 | if (svt != SVt_PVAV) |
247 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
447 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
248 | |
448 | |
249 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
449 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
250 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
450 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
451 | |
|
|
452 | return; |
|
|
453 | } |
|
|
454 | } |
|
|
455 | |
|
|
456 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
457 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
458 | { |
|
|
459 | if (!enc->shareable) |
|
|
460 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
461 | |
|
|
462 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
463 | |
|
|
464 | if (SvOK (*svp)) |
|
|
465 | { |
|
|
466 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
467 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
468 | return; |
251 | } |
469 | } |
252 | else |
470 | else |
253 | { |
471 | { |
|
|
472 | sv_setuv (*svp, enc->shareable_idx); |
|
|
473 | ++enc->shareable_idx; |
|
|
474 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
475 | } |
|
|
476 | } |
|
|
477 | |
|
|
478 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
479 | { |
|
|
480 | HV *stash = SvSTASH (sv); |
|
|
481 | GV *method; |
|
|
482 | |
|
|
483 | if (enc->cbor.flags & F_FORBID_OBJECTS) |
|
|
484 | croak ("encountered object '%s', but forbid_objects is enabled", |
|
|
485 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
486 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
|
|
487 | { |
|
|
488 | dSP; |
|
|
489 | |
|
|
490 | ENTER; SAVETMPS; |
|
|
491 | PUSHMARK (SP); |
254 | // we re-bless the reference to get overload and other niceties right |
492 | // we re-bless the reference to get overload and other niceties right |
255 | GV *to_cbor = gv_fetchmethod_autoload (SvSTASH (sv), "TO_CBOR", 0); |
|
|
256 | |
|
|
257 | if (to_cbor) |
|
|
258 | { |
|
|
259 | dSP; |
|
|
260 | |
|
|
261 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
262 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
493 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
263 | |
494 | |
264 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
265 | PUTBACK; |
495 | PUTBACK; |
|
|
496 | // G_SCALAR ensures that return value is 1 |
266 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
497 | call_sv ((SV *)GvCV (method), G_SCALAR); |
267 | SPAGAIN; |
498 | SPAGAIN; |
268 | |
499 | |
269 | // catch this surprisingly common error |
500 | // catch this surprisingly common error |
270 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
501 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
271 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
502 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash)); |
272 | |
503 | |
273 | sv = POPs; |
504 | encode_sv (enc, POPs); |
|
|
505 | |
274 | PUTBACK; |
506 | PUTBACK; |
275 | |
507 | |
276 | encode_sv (enc, sv); |
|
|
277 | |
|
|
278 | FREETMPS; LEAVE; |
508 | FREETMPS; LEAVE; |
279 | } |
509 | } |
|
|
510 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
|
|
511 | { |
|
|
512 | dSP; |
|
|
513 | |
|
|
514 | ENTER; SAVETMPS; |
|
|
515 | SAVESTACK_POS (); |
|
|
516 | PUSHMARK (SP); |
|
|
517 | EXTEND (SP, 2); |
|
|
518 | // we re-bless the reference to get overload and other niceties right |
|
|
519 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
520 | PUSHs (sv_cbor); |
|
|
521 | |
|
|
522 | PUTBACK; |
|
|
523 | int count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
524 | SPAGAIN; |
|
|
525 | |
|
|
526 | // catch this surprisingly common error |
|
|
527 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
528 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
529 | |
|
|
530 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
|
|
531 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
|
|
532 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
|
|
533 | |
|
|
534 | while (count) |
|
|
535 | encode_sv (enc, SP[1 - count--]); |
|
|
536 | |
|
|
537 | PUTBACK; |
|
|
538 | |
|
|
539 | FREETMPS; LEAVE; |
|
|
540 | } |
280 | else |
541 | else |
281 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
542 | croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it", |
282 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
543 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
283 | } |
|
|
284 | } |
544 | } |
285 | else if (svt == SVt_PVHV) |
545 | else if (svt == SVt_PVHV) |
286 | encode_hv (enc, (HV *)sv); |
546 | encode_hv (enc, (HV *)sv); |
287 | else if (svt == SVt_PVAV) |
547 | else if (svt == SVt_PVAV) |
288 | encode_av (enc, (AV *)sv); |
548 | encode_av (enc, (AV *)sv); |
289 | else if (svt < SVt_PVAV) |
|
|
290 | { |
|
|
291 | STRLEN len = 0; |
|
|
292 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
293 | |
|
|
294 | if (len == 1 && *pv == '1') |
|
|
295 | encode_ch (enc, 0xe0 | 21); |
|
|
296 | else if (len == 1 && *pv == '0') |
|
|
297 | encode_ch (enc, 0xe0 | 20); |
|
|
298 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
299 | encode_ch (enc, 0xe0 | 23); |
|
|
300 | else |
|
|
301 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
302 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
303 | } |
|
|
304 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
|
|
305 | encode_ch (enc, 0xe0 | 23); |
|
|
306 | else |
549 | else |
307 | croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
550 | { |
308 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
551 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
|
|
552 | encode_sv (enc, sv); |
|
|
553 | } |
309 | } |
554 | } |
310 | |
555 | |
311 | static void |
556 | static void |
312 | encode_nv (enc_t *enc, SV *sv) |
557 | encode_nv (enc_t *enc, SV *sv) |
313 | { |
558 | { |
314 | double nv = SvNVX (sv); |
559 | double nv = SvNVX (sv); |
315 | |
560 | |
316 | need (enc, 9); |
561 | need (enc, 9); |
317 | |
562 | |
318 | if (ecb_expect_false (nv == (U32)nv)) |
563 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
319 | encode_uint (enc, 0x00, (U32)nv); |
564 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
320 | //TODO: maybe I32? |
565 | //TODO: maybe I32? |
321 | else if (ecb_expect_false (nv == (float)nv)) |
566 | else if (ecb_expect_false (nv == (float)nv)) |
322 | { |
567 | { |
323 | uint32_t fp = ecb_float_to_binary32 (nv); |
568 | uint32_t fp = ecb_float_to_binary32 (nv); |
324 | |
569 | |
325 | *enc->cur++ = 0xe0 | 26; |
570 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
326 | |
571 | |
327 | if (!ecb_big_endian ()) |
572 | if (!ecb_big_endian ()) |
328 | fp = ecb_bswap32 (fp); |
573 | fp = ecb_bswap32 (fp); |
329 | |
574 | |
330 | memcpy (enc->cur, &fp, 4); |
575 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
332 | } |
577 | } |
333 | else |
578 | else |
334 | { |
579 | { |
335 | uint64_t fp = ecb_double_to_binary64 (nv); |
580 | uint64_t fp = ecb_double_to_binary64 (nv); |
336 | |
581 | |
337 | *enc->cur++ = 0xe0 | 27; |
582 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
338 | |
583 | |
339 | if (!ecb_big_endian ()) |
584 | if (!ecb_big_endian ()) |
340 | fp = ecb_bswap64 (fp); |
585 | fp = ecb_bswap64 (fp); |
341 | |
586 | |
342 | memcpy (enc->cur, &fp, 8); |
587 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
351 | |
596 | |
352 | if (SvPOKp (sv)) |
597 | if (SvPOKp (sv)) |
353 | { |
598 | { |
354 | STRLEN len; |
599 | STRLEN len; |
355 | char *str = SvPV (sv, len); |
600 | char *str = SvPV (sv, len); |
356 | encode_str (enc, SvUTF8 (sv), str, len); |
601 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
357 | } |
602 | } |
358 | else if (SvNOKp (sv)) |
603 | else if (SvNOKp (sv)) |
359 | encode_nv (enc, sv); |
604 | encode_nv (enc, sv); |
360 | else if (SvIOKp (sv)) |
605 | else if (SvIOKp (sv)) |
361 | { |
606 | { |
362 | if (SvIsUV (sv)) |
607 | if (SvIsUV (sv)) |
363 | encode_uint (enc, 0x00, SvUVX (sv)); |
608 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
364 | else if (SvIVX (sv) >= 0) |
609 | else if (SvIVX (sv) >= 0) |
365 | encode_uint (enc, 0x00, SvIVX (sv)); |
610 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
366 | else |
611 | else |
367 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
612 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
368 | } |
613 | } |
369 | else if (SvROK (sv)) |
614 | else if (SvROK (sv)) |
370 | encode_rv (enc, SvRV (sv)); |
615 | encode_rv (enc, SvRV (sv)); |
371 | else if (!SvOK (sv)) |
616 | else if (!SvOK (sv)) |
372 | encode_ch (enc, 0xe0 | 22); |
617 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
373 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
618 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
374 | encode_ch (enc, 0xe0 | 23); |
619 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
375 | else |
620 | else |
376 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
621 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
377 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
622 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
378 | } |
623 | } |
379 | |
624 | |
380 | static SV * |
625 | static SV * |
381 | encode_cbor (SV *scalar, CBOR *cbor) |
626 | encode_cbor (SV *scalar, CBOR *cbor) |
382 | { |
627 | { |
383 | enc_t enc; |
628 | enc_t enc = { 0 }; |
384 | |
629 | |
385 | enc.cbor = *cbor; |
630 | enc.cbor = *cbor; |
386 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
631 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
387 | enc.cur = SvPVX (enc.sv); |
632 | enc.cur = SvPVX (enc.sv); |
388 | enc.end = SvEND (enc.sv); |
633 | enc.end = SvEND (enc.sv); |
389 | enc.depth = 0; |
|
|
390 | |
634 | |
391 | SvPOK_only (enc.sv); |
635 | SvPOK_only (enc.sv); |
|
|
636 | |
|
|
637 | if (cbor->flags & F_PACK_STRINGS) |
|
|
638 | { |
|
|
639 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
640 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
641 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
642 | } |
|
|
643 | |
392 | encode_sv (&enc, scalar); |
644 | encode_sv (&enc, scalar); |
393 | |
645 | |
394 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
646 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
395 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
647 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
396 | |
648 | |
… | |
… | |
410 | U8 *end; // end of input string |
662 | U8 *end; // end of input string |
411 | const char *err; // parse error, if != 0 |
663 | const char *err; // parse error, if != 0 |
412 | CBOR cbor; |
664 | CBOR cbor; |
413 | U32 depth; // recursion depth |
665 | U32 depth; // recursion depth |
414 | U32 maxdepth; // recursion depth limit |
666 | U32 maxdepth; // recursion depth limit |
|
|
667 | AV *shareable; |
|
|
668 | AV *stringref; |
|
|
669 | SV *decode_tagged; |
|
|
670 | SV *err_sv; // optional sv for error, needs to be freed |
415 | } dec_t; |
671 | } dec_t; |
416 | |
672 | |
417 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
673 | // set dec->err to ERRSV |
|
|
674 | ecb_cold static void |
|
|
675 | err_errsv (dec_t *dec) |
|
|
676 | { |
|
|
677 | if (!dec->err) |
|
|
678 | { |
|
|
679 | dec->err_sv = newSVsv (ERRSV); |
418 | |
680 | |
419 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
681 | // chop off the trailing \n |
|
|
682 | SvCUR_set (dec->err_sv, SvCUR (dec->err_sv) - 1); |
|
|
683 | *SvEND (dec->err_sv) = 0; |
420 | |
684 | |
|
|
685 | dec->err = SvPVutf8_nolen (dec->err_sv); |
|
|
686 | } |
|
|
687 | } |
|
|
688 | |
|
|
689 | // the following functions are used to reduce code size and help the compiler to optimise |
|
|
690 | ecb_cold static void |
|
|
691 | err_set (dec_t *dec, const char *reason) |
|
|
692 | { |
|
|
693 | if (!dec->err) |
|
|
694 | dec->err = reason; |
|
|
695 | } |
|
|
696 | |
|
|
697 | ecb_cold static void |
|
|
698 | err_unexpected_end (dec_t *dec) |
|
|
699 | { |
|
|
700 | err_set (dec, "unexpected end of CBOR data"); |
|
|
701 | } |
|
|
702 | |
|
|
703 | ecb_cold static void |
|
|
704 | err_nesting_exceeded (dec_t *dec) |
|
|
705 | { |
|
|
706 | err_set (dec, ERR_NESTING_EXCEEDED); |
|
|
707 | } |
|
|
708 | |
|
|
709 | #define ERR_DO(do) SB do; goto fail; SE |
|
|
710 | #define ERR(reason) ERR_DO (err_set (dec, reason)) |
|
|
711 | #define ERR_ERRSV ERR_DO (err_errsv (dec)) |
|
|
712 | |
|
|
713 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR_DO (err_unexpected_end (dec)) |
|
|
714 | |
421 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
715 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
422 | #define DEC_DEC_DEPTH --dec->depth |
716 | #define DEC_DEC_DEPTH --dec->depth |
423 | |
717 | |
424 | static UV |
718 | static UV |
425 | decode_uint (dec_t *dec) |
719 | decode_uint (dec_t *dec) |
426 | { |
720 | { |
427 | switch (*dec->cur & 31) |
721 | U8 m = *dec->cur & MINOR_MASK; |
428 | { |
722 | ++dec->cur; |
429 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
|
|
430 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
|
|
431 | case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
|
|
432 | return *dec->cur++ & 31; |
|
|
433 | |
723 | |
434 | case 24: |
724 | if (ecb_expect_true (m < LENGTH_EXT1)) |
|
|
725 | return m; |
|
|
726 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
|
|
727 | { |
435 | WANT (2); |
728 | WANT (1); |
436 | dec->cur += 2; |
729 | dec->cur += 1; |
437 | return dec->cur[-1]; |
730 | return dec->cur[-1]; |
438 | |
731 | } |
439 | case 25: |
732 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
733 | { |
440 | WANT (3); |
734 | WANT (2); |
441 | dec->cur += 3; |
735 | dec->cur += 2; |
442 | return (((UV)dec->cur[-2]) << 8) |
736 | return (((UV)dec->cur[-2]) << 8) |
443 | | ((UV)dec->cur[-1]); |
737 | | ((UV)dec->cur[-1]); |
444 | |
738 | } |
445 | case 26: |
739 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
740 | { |
446 | WANT (5); |
741 | WANT (4); |
447 | dec->cur += 5; |
742 | dec->cur += 4; |
448 | return (((UV)dec->cur[-4]) << 24) |
743 | return (((UV)dec->cur[-4]) << 24) |
449 | | (((UV)dec->cur[-3]) << 16) |
744 | | (((UV)dec->cur[-3]) << 16) |
450 | | (((UV)dec->cur[-2]) << 8) |
745 | | (((UV)dec->cur[-2]) << 8) |
451 | | ((UV)dec->cur[-1]); |
746 | | ((UV)dec->cur[-1]); |
452 | |
747 | } |
453 | case 27: |
748 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
749 | { |
454 | WANT (9); |
750 | WANT (8); |
455 | dec->cur += 9; |
751 | dec->cur += 8; |
|
|
752 | |
|
|
753 | return |
|
|
754 | #if UVSIZE < 8 |
|
|
755 | 0 |
|
|
756 | #else |
456 | return (((UV)dec->cur[-8]) << 56) |
757 | (((UV)dec->cur[-8]) << 56) |
457 | | (((UV)dec->cur[-7]) << 48) |
758 | | (((UV)dec->cur[-7]) << 48) |
458 | | (((UV)dec->cur[-6]) << 40) |
759 | | (((UV)dec->cur[-6]) << 40) |
459 | | (((UV)dec->cur[-5]) << 32) |
760 | | (((UV)dec->cur[-5]) << 32) |
|
|
761 | #endif |
460 | | (((UV)dec->cur[-4]) << 24) |
762 | | (((UV)dec->cur[-4]) << 24) |
461 | | (((UV)dec->cur[-3]) << 16) |
763 | | (((UV)dec->cur[-3]) << 16) |
462 | | (((UV)dec->cur[-2]) << 8) |
764 | | (((UV)dec->cur[-2]) << 8) |
463 | | ((UV)dec->cur[-1]); |
765 | | ((UV)dec->cur[-1]); |
464 | |
766 | } |
465 | default: |
767 | else |
466 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
768 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
467 | } |
|
|
468 | |
769 | |
469 | fail: |
770 | fail: |
470 | return 0; |
771 | return 0; |
471 | } |
772 | } |
472 | |
773 | |
… | |
… | |
477 | { |
778 | { |
478 | AV *av = newAV (); |
779 | AV *av = newAV (); |
479 | |
780 | |
480 | DEC_INC_DEPTH; |
781 | DEC_INC_DEPTH; |
481 | |
782 | |
482 | if ((*dec->cur & 31) == 31) |
783 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
483 | { |
784 | { |
484 | ++dec->cur; |
785 | ++dec->cur; |
485 | |
786 | |
486 | for (;;) |
787 | for (;;) |
487 | { |
788 | { |
488 | WANT (1); |
789 | WANT (1); |
489 | |
790 | |
490 | if (*dec->cur == (0xe0 | 31)) |
791 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
491 | { |
792 | { |
492 | ++dec->cur; |
793 | ++dec->cur; |
493 | break; |
794 | break; |
494 | } |
795 | } |
495 | |
796 | |
496 | av_push (av, decode_sv (dec)); |
797 | av_push (av, decode_sv (dec)); |
497 | } |
798 | } |
498 | } |
799 | } |
499 | else |
800 | else |
500 | { |
801 | { |
501 | int i, len = decode_uint (dec); |
802 | UV i, len = decode_uint (dec); |
502 | |
803 | |
|
|
804 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
503 | av_fill (av, len - 1); |
805 | av_fill (av, len - 1); |
504 | |
806 | |
505 | for (i = 0; i < len; ++i) |
807 | for (i = 0; i < len; ++i) |
506 | AvARRAY (av)[i] = decode_sv (dec); |
808 | AvARRAY (av)[i] = decode_sv (dec); |
507 | } |
809 | } |
… | |
… | |
513 | SvREFCNT_dec (av); |
815 | SvREFCNT_dec (av); |
514 | DEC_DEC_DEPTH; |
816 | DEC_DEC_DEPTH; |
515 | return &PL_sv_undef; |
817 | return &PL_sv_undef; |
516 | } |
818 | } |
517 | |
819 | |
|
|
820 | static void |
|
|
821 | decode_he (dec_t *dec, HV *hv) |
|
|
822 | { |
|
|
823 | // for speed reasons, we specialcase single-string |
|
|
824 | // byte or utf-8 strings as keys, but only when !stringref |
|
|
825 | |
|
|
826 | if (ecb_expect_true (!dec->stringref)) |
|
|
827 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
|
|
828 | { |
|
|
829 | STRLEN len = decode_uint (dec); |
|
|
830 | char *key = (char *)dec->cur; |
|
|
831 | |
|
|
832 | WANT (len); |
|
|
833 | dec->cur += len; |
|
|
834 | |
|
|
835 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
836 | |
|
|
837 | return; |
|
|
838 | } |
|
|
839 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
|
|
840 | { |
|
|
841 | STRLEN len = decode_uint (dec); |
|
|
842 | char *key = (char *)dec->cur; |
|
|
843 | |
|
|
844 | WANT (len); |
|
|
845 | dec->cur += len; |
|
|
846 | |
|
|
847 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
848 | if (!is_utf8_string (key, len)) |
|
|
849 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
|
|
850 | |
|
|
851 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
852 | |
|
|
853 | return; |
|
|
854 | } |
|
|
855 | |
|
|
856 | SV *k = decode_sv (dec); |
|
|
857 | SV *v = decode_sv (dec); |
|
|
858 | |
|
|
859 | // we leak memory if uncaught exceptions are thrown by random magical |
|
|
860 | // methods, and this is hopefully the only place where it can happen, |
|
|
861 | // so if there is a chance of an exception, take the very slow path. |
|
|
862 | // since catching exceptions is "undocumented/internal/forbidden" by |
|
|
863 | // the new p5p powers, we need to call out to a perl function :/ |
|
|
864 | if (ecb_expect_false (SvAMAGIC (k))) |
|
|
865 | { |
|
|
866 | dSP; |
|
|
867 | |
|
|
868 | ENTER; SAVETMPS; |
|
|
869 | PUSHMARK (SP); |
|
|
870 | EXTEND (SP, 3); |
|
|
871 | PUSHs (sv_2mortal (newRV_inc ((SV *)hv))); |
|
|
872 | PUSHs (sv_2mortal (k)); |
|
|
873 | PUSHs (sv_2mortal (v)); |
|
|
874 | |
|
|
875 | PUTBACK; |
|
|
876 | call_pv ("CBOR::XS::_hv_store", G_VOID | G_DISCARD | G_EVAL); |
|
|
877 | SPAGAIN; |
|
|
878 | |
|
|
879 | FREETMPS; LEAVE; |
|
|
880 | |
|
|
881 | if (SvTRUE (ERRSV)) |
|
|
882 | ERR_ERRSV; |
|
|
883 | |
|
|
884 | return; |
|
|
885 | } |
|
|
886 | |
|
|
887 | hv_store_ent (hv, k, v, 0); |
|
|
888 | SvREFCNT_dec (k); |
|
|
889 | |
|
|
890 | fail: |
|
|
891 | ; |
|
|
892 | } |
|
|
893 | |
518 | static SV * |
894 | static SV * |
519 | decode_hv (dec_t *dec) |
895 | decode_hv (dec_t *dec) |
520 | { |
896 | { |
521 | HV *hv = newHV (); |
897 | HV *hv = newHV (); |
522 | |
898 | |
523 | DEC_INC_DEPTH; |
899 | DEC_INC_DEPTH; |
524 | |
900 | |
525 | if ((*dec->cur & 31) == 31) |
901 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
526 | { |
902 | { |
527 | ++dec->cur; |
903 | ++dec->cur; |
528 | |
904 | |
529 | for (;;) |
905 | for (;;) |
530 | { |
906 | { |
531 | WANT (1); |
907 | WANT (1); |
532 | |
908 | |
533 | if (*dec->cur == (0xe0 | 31)) |
909 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
534 | { |
910 | { |
535 | ++dec->cur; |
911 | ++dec->cur; |
536 | break; |
912 | break; |
537 | } |
913 | } |
538 | |
914 | |
539 | SV *k = decode_sv (dec); |
915 | decode_he (dec, hv); |
540 | SV *v = decode_sv (dec); |
|
|
541 | |
|
|
542 | hv_store_ent (hv, k, v, 0); |
|
|
543 | } |
916 | } |
544 | } |
917 | } |
545 | else |
918 | else |
546 | { |
919 | { |
547 | int len = decode_uint (dec); |
920 | UV pairs = decode_uint (dec); |
548 | |
921 | |
|
|
922 | WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes |
|
|
923 | |
549 | while (len--) |
924 | while (pairs--) |
550 | { |
925 | decode_he (dec, hv); |
551 | SV *k = decode_sv (dec); |
|
|
552 | SV *v = decode_sv (dec); |
|
|
553 | |
|
|
554 | hv_store_ent (hv, k, v, 0); |
|
|
555 | } |
|
|
556 | } |
926 | } |
557 | |
927 | |
558 | DEC_DEC_DEPTH; |
928 | DEC_DEC_DEPTH; |
559 | return newRV_noinc ((SV *)hv); |
929 | return newRV_noinc ((SV *)hv); |
560 | |
930 | |
… | |
… | |
567 | static SV * |
937 | static SV * |
568 | decode_str (dec_t *dec, int utf8) |
938 | decode_str (dec_t *dec, int utf8) |
569 | { |
939 | { |
570 | SV *sv = 0; |
940 | SV *sv = 0; |
571 | |
941 | |
572 | if ((*dec->cur & 31) == 31) |
942 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
573 | { |
943 | { |
|
|
944 | // indefinite length strings |
574 | ++dec->cur; |
945 | ++dec->cur; |
575 | |
946 | |
|
|
947 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
948 | |
576 | sv = newSVpvn ("", 0); |
949 | sv = newSVpvn ("", 0); |
577 | |
950 | |
578 | // not very fast, and certainly not robust against illegal input |
|
|
579 | for (;;) |
951 | for (;;) |
580 | { |
952 | { |
581 | WANT (1); |
953 | WANT (1); |
582 | |
954 | |
583 | if (*dec->cur == (0xe0 | 31)) |
955 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
956 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
584 | { |
957 | { |
585 | ++dec->cur; |
958 | ++dec->cur; |
586 | break; |
959 | break; |
587 | } |
960 | } |
|
|
961 | else |
|
|
962 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
588 | |
963 | |
589 | sv_catsv (sv, decode_sv (dec)); |
964 | STRLEN len = decode_uint (dec); |
|
|
965 | |
|
|
966 | WANT (len); |
|
|
967 | sv_catpvn (sv, dec->cur, len); |
|
|
968 | dec->cur += len; |
590 | } |
969 | } |
591 | } |
970 | } |
592 | else |
971 | else |
593 | { |
972 | { |
594 | STRLEN len = decode_uint (dec); |
973 | STRLEN len = decode_uint (dec); |
595 | |
974 | |
596 | WANT (len); |
975 | WANT (len); |
597 | sv = newSVpvn (dec->cur, len); |
976 | sv = newSVpvn (dec->cur, len); |
598 | dec->cur += len; |
977 | dec->cur += len; |
|
|
978 | |
|
|
979 | if (ecb_expect_false (dec->stringref) |
|
|
980 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
981 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
599 | } |
982 | } |
600 | |
983 | |
601 | if (utf8) |
984 | if (utf8) |
|
|
985 | { |
|
|
986 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
987 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
|
|
988 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
|
|
989 | |
602 | SvUTF8_on (sv); |
990 | SvUTF8_on (sv); |
|
|
991 | } |
603 | |
992 | |
604 | return sv; |
993 | return sv; |
605 | |
994 | |
606 | fail: |
995 | fail: |
607 | SvREFCNT_dec (sv); |
996 | SvREFCNT_dec (sv); |
… | |
… | |
609 | } |
998 | } |
610 | |
999 | |
611 | static SV * |
1000 | static SV * |
612 | decode_tagged (dec_t *dec) |
1001 | decode_tagged (dec_t *dec) |
613 | { |
1002 | { |
|
|
1003 | SV *sv = 0; |
614 | UV tag = decode_uint (dec); |
1004 | UV tag = decode_uint (dec); |
|
|
1005 | |
|
|
1006 | WANT (1); |
|
|
1007 | |
|
|
1008 | switch (tag) |
|
|
1009 | { |
|
|
1010 | case CBOR_TAG_MAGIC: |
615 | SV *sv = decode_sv (dec); |
1011 | sv = decode_sv (dec); |
|
|
1012 | break; |
616 | |
1013 | |
617 | if (tag == 55799) // 2.4.5 Self-Describe CBOR |
1014 | case CBOR_TAG_INDIRECTION: |
|
|
1015 | sv = newRV_noinc (decode_sv (dec)); |
|
|
1016 | break; |
|
|
1017 | |
|
|
1018 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
1019 | { |
|
|
1020 | // do nmot use SAVETMPS/FREETMPS, as these will |
|
|
1021 | // erase mortalised caches, e.g. "shareable" |
|
|
1022 | ENTER; |
|
|
1023 | |
|
|
1024 | SAVESPTR (dec->stringref); |
|
|
1025 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
1026 | |
|
|
1027 | sv = decode_sv (dec); |
|
|
1028 | |
|
|
1029 | LEAVE; |
|
|
1030 | } |
|
|
1031 | break; |
|
|
1032 | |
|
|
1033 | case CBOR_TAG_STRINGREF: |
|
|
1034 | { |
|
|
1035 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
|
|
1036 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
1037 | |
|
|
1038 | UV idx = decode_uint (dec); |
|
|
1039 | |
|
|
1040 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
1041 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
1042 | |
|
|
1043 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
1044 | } |
|
|
1045 | break; |
|
|
1046 | |
|
|
1047 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
1048 | { |
|
|
1049 | if (ecb_expect_false (!dec->shareable)) |
|
|
1050 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
1051 | |
|
|
1052 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
1053 | { |
|
|
1054 | sv = newSV (0); |
|
|
1055 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
|
|
1056 | |
|
|
1057 | SV *osv = decode_sv (dec); |
|
|
1058 | sv_setsv (sv, osv); |
|
|
1059 | SvREFCNT_dec_NN (osv); |
|
|
1060 | } |
|
|
1061 | else |
|
|
1062 | { |
|
|
1063 | av_push (dec->shareable, &PL_sv_undef); |
|
|
1064 | int idx = AvFILLp (dec->shareable); |
|
|
1065 | sv = decode_sv (dec); |
|
|
1066 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
1067 | } |
|
|
1068 | } |
|
|
1069 | break; |
|
|
1070 | |
|
|
1071 | case CBOR_TAG_VALUE_SHAREDREF: |
|
|
1072 | { |
|
|
1073 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
|
|
1074 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
|
|
1075 | |
|
|
1076 | UV idx = decode_uint (dec); |
|
|
1077 | |
|
|
1078 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
|
|
1079 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
|
|
1080 | |
|
|
1081 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
1082 | |
|
|
1083 | if (sv == &PL_sv_undef) |
|
|
1084 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
|
|
1085 | } |
|
|
1086 | break; |
|
|
1087 | |
|
|
1088 | case CBOR_TAG_PERL_OBJECT: |
|
|
1089 | { |
|
|
1090 | if (dec->cbor.flags & F_FORBID_OBJECTS) |
|
|
1091 | goto filter; |
|
|
1092 | |
|
|
1093 | sv = decode_sv (dec); |
|
|
1094 | |
|
|
1095 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
1096 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
1097 | |
|
|
1098 | AV *av = (AV *)SvRV (sv); |
|
|
1099 | int len = av_len (av) + 1; |
|
|
1100 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
|
|
1101 | |
|
|
1102 | if (!stash) |
|
|
1103 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
1104 | |
|
|
1105 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
1106 | |
|
|
1107 | if (!method) |
|
|
1108 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
1109 | |
|
|
1110 | dSP; |
|
|
1111 | |
|
|
1112 | ENTER; SAVETMPS; |
|
|
1113 | PUSHMARK (SP); |
|
|
1114 | EXTEND (SP, len + 1); |
|
|
1115 | // we re-bless the reference to get overload and other niceties right |
|
|
1116 | PUSHs (*av_fetch (av, 0, 1)); |
|
|
1117 | PUSHs (sv_cbor); |
|
|
1118 | |
|
|
1119 | int i; |
|
|
1120 | |
|
|
1121 | for (i = 1; i < len; ++i) |
|
|
1122 | PUSHs (*av_fetch (av, i, 1)); |
|
|
1123 | |
|
|
1124 | PUTBACK; |
|
|
1125 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
|
|
1126 | SPAGAIN; |
|
|
1127 | |
|
|
1128 | if (SvTRUE (ERRSV)) |
|
|
1129 | { |
|
|
1130 | FREETMPS; LEAVE; |
|
|
1131 | ERR_ERRSV; |
|
|
1132 | } |
|
|
1133 | |
|
|
1134 | SvREFCNT_dec (sv); |
|
|
1135 | sv = SvREFCNT_inc (POPs); |
|
|
1136 | |
|
|
1137 | PUTBACK; |
|
|
1138 | |
|
|
1139 | FREETMPS; LEAVE; |
|
|
1140 | } |
|
|
1141 | break; |
|
|
1142 | |
|
|
1143 | default: |
|
|
1144 | filter: |
|
|
1145 | { |
|
|
1146 | SV *tag_sv = newSVuv (tag); |
|
|
1147 | |
|
|
1148 | sv = decode_sv (dec); |
|
|
1149 | |
|
|
1150 | dSP; |
|
|
1151 | ENTER; SAVETMPS; |
|
|
1152 | SAVESTACK_POS (); |
|
|
1153 | PUSHMARK (SP); |
|
|
1154 | EXTEND (SP, 2); |
|
|
1155 | PUSHs (tag_sv); |
|
|
1156 | PUSHs (sv); |
|
|
1157 | |
|
|
1158 | PUTBACK; |
|
|
1159 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
1160 | SPAGAIN; |
|
|
1161 | |
|
|
1162 | if (SvTRUE (ERRSV)) |
|
|
1163 | { |
|
|
1164 | SvREFCNT_dec (tag_sv); |
|
|
1165 | FREETMPS; LEAVE; |
|
|
1166 | ERR_ERRSV; |
|
|
1167 | } |
|
|
1168 | |
|
|
1169 | if (count) |
|
|
1170 | { |
|
|
1171 | SvREFCNT_dec (tag_sv); |
|
|
1172 | SvREFCNT_dec (sv); |
|
|
1173 | sv = SvREFCNT_inc (POPs); |
|
|
1174 | } |
|
|
1175 | else |
|
|
1176 | { |
|
|
1177 | AV *av = newAV (); |
|
|
1178 | av_push (av, tag_sv); |
|
|
1179 | av_push (av, sv); |
|
|
1180 | |
|
|
1181 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
1182 | ? cbor_tagged_stash |
|
|
1183 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
|
|
1184 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
1185 | } |
|
|
1186 | |
|
|
1187 | PUTBACK; |
|
|
1188 | |
|
|
1189 | FREETMPS; LEAVE; |
|
|
1190 | } |
|
|
1191 | break; |
|
|
1192 | } |
|
|
1193 | |
618 | return sv; |
1194 | return sv; |
619 | |
1195 | |
620 | AV *av = newAV (); |
1196 | fail: |
621 | av_push (av, newSVuv (tag)); |
1197 | SvREFCNT_dec (sv); |
622 | av_push (av, sv); |
1198 | return &PL_sv_undef; |
623 | |
|
|
624 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
625 | ? cbor_tagged_stash |
|
|
626 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
|
|
627 | |
|
|
628 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
629 | } |
1199 | } |
630 | |
1200 | |
631 | static SV * |
1201 | static SV * |
632 | decode_sv (dec_t *dec) |
1202 | decode_sv (dec_t *dec) |
633 | { |
1203 | { |
634 | WANT (1); |
1204 | WANT (1); |
635 | |
1205 | |
636 | switch (*dec->cur >> 5) |
1206 | switch (*dec->cur >> MAJOR_SHIFT) |
637 | { |
1207 | { |
638 | case 0: // unsigned int |
1208 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
639 | return newSVuv (decode_uint (dec)); |
1209 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
640 | case 1: // negative int |
1210 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
641 | return newSViv (-1 - (IV)decode_uint (dec)); |
1211 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
642 | case 2: // octet string |
1212 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
643 | return decode_str (dec, 0); |
1213 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
644 | case 3: // utf-8 string |
1214 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
645 | return decode_str (dec, 1); |
1215 | |
646 | case 4: // array |
1216 | case MAJOR_MISC >> MAJOR_SHIFT: |
647 | return decode_av (dec); |
|
|
648 | case 5: // map |
|
|
649 | return decode_hv (dec); |
|
|
650 | case 6: // tag |
|
|
651 | return decode_tagged (dec); |
|
|
652 | case 7: // misc |
|
|
653 | switch (*dec->cur++ & 31) |
1217 | switch (*dec->cur++ & MINOR_MASK) |
654 | { |
1218 | { |
655 | case 20: |
1219 | case SIMPLE_FALSE: |
656 | #if CBOR_SLOW |
1220 | #if CBOR_SLOW |
657 | cbor_false = get_bool ("CBOR::XS::false"); |
1221 | types_false = get_bool ("Types::Serialiser::false"); |
658 | #endif |
1222 | #endif |
659 | return newSVsv (cbor_false); |
1223 | return newSVsv (types_false); |
660 | case 21: |
1224 | case SIMPLE_TRUE: |
661 | #if CBOR_SLOW |
1225 | #if CBOR_SLOW |
662 | cbor_true = get_bool ("CBOR::XS::true"); |
1226 | types_true = get_bool ("Types::Serialiser::true"); |
663 | #endif |
1227 | #endif |
664 | return newSVsv (cbor_true); |
1228 | return newSVsv (types_true); |
665 | case 22: |
1229 | case SIMPLE_NULL: |
666 | return newSVsv (&PL_sv_undef); |
1230 | return newSVsv (&PL_sv_undef); |
|
|
1231 | case SIMPLE_UNDEF: |
|
|
1232 | #if CBOR_SLOW |
|
|
1233 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
1234 | #endif |
|
|
1235 | return newSVsv (types_error); |
667 | |
1236 | |
668 | case 25: |
1237 | case MISC_FLOAT16: |
669 | { |
1238 | { |
670 | WANT (2); |
1239 | WANT (2); |
671 | |
1240 | |
672 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1241 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
673 | dec->cur += 2; |
1242 | dec->cur += 2; |
674 | |
1243 | |
675 | return newSVnv (ecb_binary16_to_float (fp)); |
1244 | return newSVnv (ecb_binary16_to_float (fp)); |
676 | } |
1245 | } |
677 | |
1246 | |
678 | case 26: |
1247 | case MISC_FLOAT32: |
679 | { |
1248 | { |
680 | uint32_t fp; |
1249 | uint32_t fp; |
681 | WANT (4); |
1250 | WANT (4); |
682 | memcpy (&fp, dec->cur, 4); |
1251 | memcpy (&fp, dec->cur, 4); |
683 | dec->cur += 4; |
1252 | dec->cur += 4; |
… | |
… | |
686 | fp = ecb_bswap32 (fp); |
1255 | fp = ecb_bswap32 (fp); |
687 | |
1256 | |
688 | return newSVnv (ecb_binary32_to_float (fp)); |
1257 | return newSVnv (ecb_binary32_to_float (fp)); |
689 | } |
1258 | } |
690 | |
1259 | |
691 | case 27: |
1260 | case MISC_FLOAT64: |
692 | { |
1261 | { |
693 | uint64_t fp; |
1262 | uint64_t fp; |
694 | WANT (8); |
1263 | WANT (8); |
695 | memcpy (&fp, dec->cur, 8); |
1264 | memcpy (&fp, dec->cur, 8); |
696 | dec->cur += 8; |
1265 | dec->cur += 8; |
… | |
… | |
699 | fp = ecb_bswap64 (fp); |
1268 | fp = ecb_bswap64 (fp); |
700 | |
1269 | |
701 | return newSVnv (ecb_binary64_to_double (fp)); |
1270 | return newSVnv (ecb_binary64_to_double (fp)); |
702 | } |
1271 | } |
703 | |
1272 | |
704 | // 0..19 unassigned |
1273 | // 0..19 unassigned simple |
705 | // 24 reserved + unassigned (reserved values are not encodable) |
1274 | // 24 reserved + unassigned simple (reserved values are not encodable) |
|
|
1275 | // 28-30 unassigned misc |
|
|
1276 | // 31 break code |
706 | default: |
1277 | default: |
707 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1278 | ERR ("corrupted CBOR data (reserved/unassigned/unexpected major 7 value)"); |
708 | } |
1279 | } |
709 | |
1280 | |
710 | break; |
1281 | break; |
711 | } |
1282 | } |
712 | |
1283 | |
… | |
… | |
715 | } |
1286 | } |
716 | |
1287 | |
717 | static SV * |
1288 | static SV * |
718 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1289 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
719 | { |
1290 | { |
720 | dec_t dec; |
1291 | dec_t dec = { 0 }; |
721 | SV *sv; |
1292 | SV *sv; |
|
|
1293 | STRLEN len; |
|
|
1294 | char *data = SvPVbyte (string, len); |
722 | |
1295 | |
723 | /* work around bugs in 5.10 where manipulating magic values |
|
|
724 | * makes perl ignore the magic in subsequent accesses. |
|
|
725 | * also make a copy of non-PV values, to get them into a clean |
|
|
726 | * state (SvPV should do that, but it's buggy, see below). |
|
|
727 | */ |
|
|
728 | /*SvGETMAGIC (string);*/ |
|
|
729 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
730 | string = sv_2mortal (newSVsv (string)); |
|
|
731 | |
|
|
732 | SvUPGRADE (string, SVt_PV); |
|
|
733 | |
|
|
734 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
735 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
736 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
737 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
738 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
739 | * and hope for the best. |
|
|
740 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
741 | * assertion business is seriously broken, try yet another workaround |
|
|
742 | * for the broken -DDEBUGGING. |
|
|
743 | */ |
|
|
744 | { |
|
|
745 | #ifdef DEBUGGING |
|
|
746 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
747 | #else |
|
|
748 | STRLEN offset = SvCUR (string); |
|
|
749 | #endif |
|
|
750 | |
|
|
751 | if (offset > cbor->max_size && cbor->max_size) |
1296 | if (len > cbor->max_size && cbor->max_size) |
752 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
1297 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
753 | (unsigned long)SvCUR (string), (unsigned long)cbor->max_size); |
1298 | (unsigned long)len, (unsigned long)cbor->max_size); |
754 | } |
|
|
755 | |
|
|
756 | sv_utf8_downgrade (string, 0); |
|
|
757 | |
1299 | |
758 | dec.cbor = *cbor; |
1300 | dec.cbor = *cbor; |
759 | dec.cur = (U8 *)SvPVX (string); |
1301 | dec.cur = (U8 *)data; |
760 | dec.end = (U8 *)SvEND (string); |
1302 | dec.end = (U8 *)data + len; |
761 | dec.err = 0; |
|
|
762 | dec.depth = 0; |
|
|
763 | |
1303 | |
764 | sv = decode_sv (&dec); |
1304 | sv = decode_sv (&dec); |
765 | |
1305 | |
766 | if (offset_return) |
1306 | if (offset_return) |
767 | *offset_return = dec.cur; |
1307 | *offset_return = dec.cur; |
… | |
… | |
770 | if (dec.cur != dec.end && !dec.err) |
1310 | if (dec.cur != dec.end && !dec.err) |
771 | dec.err = "garbage after CBOR object"; |
1311 | dec.err = "garbage after CBOR object"; |
772 | |
1312 | |
773 | if (dec.err) |
1313 | if (dec.err) |
774 | { |
1314 | { |
|
|
1315 | if (dec.shareable) |
|
|
1316 | { |
|
|
1317 | // need to break cyclic links, which would all be in shareable |
|
|
1318 | int i; |
|
|
1319 | SV **svp; |
|
|
1320 | |
|
|
1321 | for (i = av_len (dec.shareable) + 1; i--; ) |
|
|
1322 | if ((svp = av_fetch (dec.shareable, i, 0))) |
|
|
1323 | sv_setsv (*svp, &PL_sv_undef); |
|
|
1324 | } |
|
|
1325 | |
775 | SvREFCNT_dec (sv); |
1326 | SvREFCNT_dec (sv); |
|
|
1327 | |
|
|
1328 | if (dec.err_sv) |
|
|
1329 | sv_2mortal (dec.err_sv); |
|
|
1330 | |
776 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
1331 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
777 | } |
1332 | } |
778 | |
1333 | |
779 | sv = sv_2mortal (sv); |
1334 | sv = sv_2mortal (sv); |
780 | |
1335 | |
781 | return sv; |
1336 | return sv; |
782 | } |
1337 | } |
783 | |
1338 | |
|
|
1339 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1340 | // incremental parser |
|
|
1341 | |
|
|
1342 | #define INCR_DONE(cbor) (AvFILLp (cbor->incr_count) < 0) |
|
|
1343 | |
|
|
1344 | // returns 0 for notyet, 1 for success or error |
|
|
1345 | static int |
|
|
1346 | incr_parse (CBOR *self, SV *cborstr) |
|
|
1347 | { |
|
|
1348 | STRLEN cur; |
|
|
1349 | SvPV (cborstr, cur); |
|
|
1350 | |
|
|
1351 | while (ecb_expect_true (self->incr_need <= cur)) |
|
|
1352 | { |
|
|
1353 | // table of integer count bytes |
|
|
1354 | static I8 incr_len[MINOR_MASK + 1] = { |
|
|
1355 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1356 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1357 | 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
1358 | 1, 2, 4, 8,-1,-1,-1,-2 |
|
|
1359 | }; |
|
|
1360 | |
|
|
1361 | const U8 *p = SvPVX (cborstr) + self->incr_pos; |
|
|
1362 | U8 m = *p & MINOR_MASK; |
|
|
1363 | IV count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1364 | I8 ilen = incr_len[m]; |
|
|
1365 | |
|
|
1366 | self->incr_need = self->incr_pos + 1; |
|
|
1367 | |
|
|
1368 | if (ecb_expect_false (ilen < 0)) |
|
|
1369 | { |
|
|
1370 | if (m != MINOR_INDEF) |
|
|
1371 | return 1; // error |
|
|
1372 | |
|
|
1373 | if (*p == (MAJOR_MISC | MINOR_INDEF)) |
|
|
1374 | { |
|
|
1375 | if (count >= 0) |
|
|
1376 | return 1; // error |
|
|
1377 | |
|
|
1378 | count = 1; |
|
|
1379 | } |
|
|
1380 | else |
|
|
1381 | { |
|
|
1382 | av_push (self->incr_count, newSViv (-1)); //TODO: nest |
|
|
1383 | count = -1; |
|
|
1384 | } |
|
|
1385 | } |
|
|
1386 | else |
|
|
1387 | { |
|
|
1388 | self->incr_need += ilen; |
|
|
1389 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1390 | return 0; |
|
|
1391 | |
|
|
1392 | int major = *p >> MAJOR_SHIFT; |
|
|
1393 | |
|
|
1394 | switch (major) |
|
|
1395 | { |
|
|
1396 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1397 | ++count; // tags merely prefix another value |
|
|
1398 | break; |
|
|
1399 | |
|
|
1400 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1401 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1402 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1403 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1404 | { |
|
|
1405 | UV len; |
|
|
1406 | |
|
|
1407 | if (ecb_expect_false (ilen)) |
|
|
1408 | { |
|
|
1409 | len = 0; |
|
|
1410 | |
|
|
1411 | do { |
|
|
1412 | len = (len << 8) | *++p; |
|
|
1413 | } while (--ilen); |
|
|
1414 | } |
|
|
1415 | else |
|
|
1416 | len = m; |
|
|
1417 | |
|
|
1418 | switch (major) |
|
|
1419 | { |
|
|
1420 | case MAJOR_BYTES >> MAJOR_SHIFT: |
|
|
1421 | case MAJOR_TEXT >> MAJOR_SHIFT: |
|
|
1422 | self->incr_need += len; |
|
|
1423 | if (ecb_expect_false (self->incr_need > cur)) |
|
|
1424 | return 0; |
|
|
1425 | |
|
|
1426 | break; |
|
|
1427 | |
|
|
1428 | case MAJOR_MAP >> MAJOR_SHIFT: |
|
|
1429 | len <<= 1; |
|
|
1430 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
|
|
1431 | if (len) |
|
|
1432 | { |
|
|
1433 | av_push (self->incr_count, newSViv (len + 1)); //TODO: nest |
|
|
1434 | count = len + 1; |
|
|
1435 | } |
|
|
1436 | break; |
|
|
1437 | } |
|
|
1438 | } |
|
|
1439 | } |
|
|
1440 | } |
|
|
1441 | |
|
|
1442 | self->incr_pos = self->incr_need; |
|
|
1443 | |
|
|
1444 | if (count > 0) |
|
|
1445 | { |
|
|
1446 | while (!--count) |
|
|
1447 | { |
|
|
1448 | if (!AvFILLp (self->incr_count)) |
|
|
1449 | return 1; // done |
|
|
1450 | |
|
|
1451 | SvREFCNT_dec_NN (av_pop (self->incr_count)); |
|
|
1452 | count = SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]); |
|
|
1453 | } |
|
|
1454 | |
|
|
1455 | SvIVX (AvARRAY (self->incr_count)[AvFILLp (self->incr_count)]) = count; |
|
|
1456 | } |
|
|
1457 | } |
|
|
1458 | |
|
|
1459 | return 0; |
|
|
1460 | } |
|
|
1461 | |
|
|
1462 | |
784 | ///////////////////////////////////////////////////////////////////////////// |
1463 | ///////////////////////////////////////////////////////////////////////////// |
785 | // XS interface functions |
1464 | // XS interface functions |
786 | |
1465 | |
787 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1466 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
788 | |
1467 | |
789 | BOOT: |
1468 | BOOT: |
790 | { |
1469 | { |
791 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
1470 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
792 | cbor_boolean_stash = gv_stashpv ("CBOR::XS::Boolean", 1); |
|
|
793 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
1471 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
794 | |
1472 | |
795 | cbor_true = get_bool ("CBOR::XS::true"); |
1473 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
796 | cbor_false = get_bool ("CBOR::XS::false"); |
1474 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
|
|
1475 | |
|
|
1476 | types_true = get_bool ("Types::Serialiser::true" ); |
|
|
1477 | types_false = get_bool ("Types::Serialiser::false"); |
|
|
1478 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
1479 | |
|
|
1480 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1481 | |
|
|
1482 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
1483 | SvREADONLY_on (sv_cbor); |
|
|
1484 | |
|
|
1485 | assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1)); |
797 | } |
1486 | } |
798 | |
1487 | |
799 | PROTOTYPES: DISABLE |
1488 | PROTOTYPES: DISABLE |
800 | |
1489 | |
801 | void CLONE (...) |
1490 | void CLONE (...) |
802 | CODE: |
1491 | CODE: |
803 | cbor_stash = 0; |
1492 | cbor_stash = 0; |
804 | cbor_boolean_stash = 0; |
|
|
805 | cbor_tagged_stash = 0; |
1493 | cbor_tagged_stash = 0; |
|
|
1494 | types_error_stash = 0; |
|
|
1495 | types_boolean_stash = 0; |
806 | |
1496 | |
807 | void new (char *klass) |
1497 | void new (char *klass) |
808 | PPCODE: |
1498 | PPCODE: |
809 | { |
1499 | { |
810 | SV *pv = NEWSV (0, sizeof (CBOR)); |
1500 | SV *pv = NEWSV (0, sizeof (CBOR)); |
… | |
… | |
818 | |
1508 | |
819 | void shrink (CBOR *self, int enable = 1) |
1509 | void shrink (CBOR *self, int enable = 1) |
820 | ALIAS: |
1510 | ALIAS: |
821 | shrink = F_SHRINK |
1511 | shrink = F_SHRINK |
822 | allow_unknown = F_ALLOW_UNKNOWN |
1512 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1513 | allow_sharing = F_ALLOW_SHARING |
|
|
1514 | allow_cycles = F_ALLOW_CYCLES |
|
|
1515 | forbid_objects = F_FORBID_OBJECTS |
|
|
1516 | pack_strings = F_PACK_STRINGS |
|
|
1517 | text_keys = F_TEXT_KEYS |
|
|
1518 | text_strings = F_TEXT_STRINGS |
|
|
1519 | validate_utf8 = F_VALIDATE_UTF8 |
823 | PPCODE: |
1520 | PPCODE: |
824 | { |
1521 | { |
825 | if (enable) |
1522 | if (enable) |
826 | self->flags |= ix; |
1523 | self->flags |= ix; |
827 | else |
1524 | else |
… | |
… | |
832 | |
1529 | |
833 | void get_shrink (CBOR *self) |
1530 | void get_shrink (CBOR *self) |
834 | ALIAS: |
1531 | ALIAS: |
835 | get_shrink = F_SHRINK |
1532 | get_shrink = F_SHRINK |
836 | get_allow_unknown = F_ALLOW_UNKNOWN |
1533 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1534 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1535 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1536 | get_forbid_objects = F_FORBID_OBJECTS |
|
|
1537 | get_pack_strings = F_PACK_STRINGS |
|
|
1538 | get_text_keys = F_TEXT_KEYS |
|
|
1539 | get_text_strings = F_TEXT_STRINGS |
|
|
1540 | get_validate_utf8 = F_VALIDATE_UTF8 |
837 | PPCODE: |
1541 | PPCODE: |
838 | XPUSHs (boolSV (self->flags & ix)); |
1542 | XPUSHs (boolSV (self->flags & ix)); |
839 | |
1543 | |
840 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1544 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
841 | PPCODE: |
1545 | PPCODE: |
… | |
… | |
857 | CODE: |
1561 | CODE: |
858 | RETVAL = self->max_size; |
1562 | RETVAL = self->max_size; |
859 | OUTPUT: |
1563 | OUTPUT: |
860 | RETVAL |
1564 | RETVAL |
861 | |
1565 | |
862 | #if 0 //TODO |
1566 | void filter (CBOR *self, SV *filter = 0) |
863 | |
|
|
864 | void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
|
|
865 | PPCODE: |
1567 | PPCODE: |
866 | { |
|
|
867 | SvREFCNT_dec (self->cb_object); |
1568 | SvREFCNT_dec (self->filter); |
868 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1569 | self->filter = filter ? newSVsv (filter) : filter; |
869 | |
|
|
870 | XPUSHs (ST (0)); |
1570 | XPUSHs (ST (0)); |
871 | } |
|
|
872 | |
1571 | |
873 | void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
1572 | SV *get_filter (CBOR *self) |
874 | PPCODE: |
1573 | CODE: |
875 | { |
1574 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
876 | if (!self->cb_sk_object) |
1575 | OUTPUT: |
877 | self->cb_sk_object = newHV (); |
1576 | RETVAL |
878 | |
|
|
879 | if (SvOK (cb)) |
|
|
880 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
881 | else |
|
|
882 | { |
|
|
883 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
884 | |
|
|
885 | if (!HvKEYS (self->cb_sk_object)) |
|
|
886 | { |
|
|
887 | SvREFCNT_dec (self->cb_sk_object); |
|
|
888 | self->cb_sk_object = 0; |
|
|
889 | } |
|
|
890 | } |
|
|
891 | |
|
|
892 | XPUSHs (ST (0)); |
|
|
893 | } |
|
|
894 | |
|
|
895 | #endif |
|
|
896 | |
1577 | |
897 | void encode (CBOR *self, SV *scalar) |
1578 | void encode (CBOR *self, SV *scalar) |
898 | PPCODE: |
1579 | PPCODE: |
899 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1580 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
900 | XPUSHs (scalar); |
1581 | XPUSHs (scalar); |
… | |
… | |
913 | EXTEND (SP, 2); |
1594 | EXTEND (SP, 2); |
914 | PUSHs (sv); |
1595 | PUSHs (sv); |
915 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1596 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
916 | } |
1597 | } |
917 | |
1598 | |
918 | #if 0 |
1599 | void incr_parse (CBOR *self, SV *cborstr) |
|
|
1600 | ALIAS: |
|
|
1601 | incr_parse_multiple = 1 |
|
|
1602 | PPCODE: |
|
|
1603 | { |
|
|
1604 | if (SvUTF8 (cborstr)) |
|
|
1605 | sv_utf8_downgrade (cborstr, 0); |
|
|
1606 | |
|
|
1607 | if (!self->incr_count) |
|
|
1608 | { |
|
|
1609 | self->incr_count = newAV (); |
|
|
1610 | self->incr_pos = 0; |
|
|
1611 | self->incr_need = 1; |
|
|
1612 | |
|
|
1613 | av_push (self->incr_count, newSViv (1)); |
|
|
1614 | } |
|
|
1615 | |
|
|
1616 | do |
|
|
1617 | { |
|
|
1618 | if (!incr_parse (self, cborstr)) |
|
|
1619 | { |
|
|
1620 | if (self->incr_need > self->max_size && self->max_size) |
|
|
1621 | croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", |
|
|
1622 | (unsigned long)self->incr_need, (unsigned long)self->max_size); |
|
|
1623 | |
|
|
1624 | break; |
|
|
1625 | } |
|
|
1626 | |
|
|
1627 | SV *sv; |
|
|
1628 | char *offset; |
|
|
1629 | |
|
|
1630 | PUTBACK; sv = decode_cbor (cborstr, self, &offset); SPAGAIN; |
|
|
1631 | XPUSHs (sv); |
|
|
1632 | |
|
|
1633 | sv_chop (cborstr, offset); |
|
|
1634 | |
|
|
1635 | av_clear (self->incr_count); |
|
|
1636 | av_push (self->incr_count, newSViv (1)); |
|
|
1637 | |
|
|
1638 | self->incr_pos = 0; |
|
|
1639 | self->incr_need = self->incr_pos + 1; |
|
|
1640 | } |
|
|
1641 | while (ix); |
|
|
1642 | } |
|
|
1643 | |
|
|
1644 | void incr_reset (CBOR *self) |
|
|
1645 | CODE: |
|
|
1646 | { |
|
|
1647 | SvREFCNT_dec (self->incr_count); |
|
|
1648 | self->incr_count = 0; |
|
|
1649 | } |
919 | |
1650 | |
920 | void DESTROY (CBOR *self) |
1651 | void DESTROY (CBOR *self) |
921 | CODE: |
1652 | PPCODE: |
922 | SvREFCNT_dec (self->cb_sk_object); |
1653 | cbor_free (self); |
923 | SvREFCNT_dec (self->cb_object); |
|
|
924 | |
|
|
925 | #endif |
|
|
926 | |
1654 | |
927 | PROTOTYPES: ENABLE |
1655 | PROTOTYPES: ENABLE |
928 | |
1656 | |
929 | void encode_cbor (SV *scalar) |
1657 | void encode_cbor (SV *scalar) |
|
|
1658 | ALIAS: |
|
|
1659 | encode_cbor = 0 |
|
|
1660 | encode_cbor_sharing = F_ALLOW_SHARING |
930 | PPCODE: |
1661 | PPCODE: |
931 | { |
1662 | { |
932 | CBOR cbor; |
1663 | CBOR cbor; |
933 | cbor_init (&cbor); |
1664 | cbor_init (&cbor); |
|
|
1665 | cbor.flags |= ix; |
934 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1666 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
935 | XPUSHs (scalar); |
1667 | XPUSHs (scalar); |
936 | } |
1668 | } |
937 | |
1669 | |
938 | void decode_cbor (SV *cborstr) |
1670 | void decode_cbor (SV *cborstr) |