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