… | |
… | |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
11 | |
11 | |
12 | #include "ecb.h" |
12 | #include "ecb.h" |
13 | |
13 | |
14 | // known tags, rfc7049 |
14 | // compatibility with perl <5.18 |
|
|
15 | #ifndef HvNAMELEN_get |
|
|
16 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
17 | #endif |
|
|
18 | #ifndef HvNAMELEN |
|
|
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
20 | #endif |
|
|
21 | #ifndef HvNAMEUTF8 |
|
|
22 | # define HvNAMEUTF8(hv) 0 |
|
|
23 | #endif |
|
|
24 | #ifndef SvREFCNT_dec_NN |
|
|
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
26 | #endif |
|
|
27 | |
|
|
28 | // known major and minor types |
|
|
29 | enum cbor_type |
|
|
30 | { |
|
|
31 | MAJOR_SHIFT = 5, |
|
|
32 | MINOR_MASK = 0x1f, |
|
|
33 | |
|
|
34 | MAJOR_POS_INT = 0 << MAJOR_SHIFT, |
|
|
35 | MAJOR_NEG_INT = 1 << MAJOR_SHIFT, |
|
|
36 | MAJOR_BYTES = 2 << MAJOR_SHIFT, |
|
|
37 | MAJOR_TEXT = 3 << MAJOR_SHIFT, |
|
|
38 | MAJOR_ARRAY = 4 << MAJOR_SHIFT, |
|
|
39 | MAJOR_MAP = 5 << MAJOR_SHIFT, |
|
|
40 | MAJOR_TAG = 6 << MAJOR_SHIFT, |
|
|
41 | MAJOR_MISC = 7 << MAJOR_SHIFT, |
|
|
42 | |
|
|
43 | // INT/STRING/ARRAY/MAP subtypes |
|
|
44 | LENGTH_EXT1 = 24, |
|
|
45 | LENGTH_EXT2 = 25, |
|
|
46 | LENGTH_EXT4 = 26, |
|
|
47 | LENGTH_EXT8 = 27, |
|
|
48 | |
|
|
49 | // SIMPLE types (effectively MISC subtypes) |
|
|
50 | SIMPLE_FALSE = 20, |
|
|
51 | SIMPLE_TRUE = 21, |
|
|
52 | SIMPLE_NULL = 22, |
|
|
53 | SIMPLE_UNDEF = 23, |
|
|
54 | |
|
|
55 | // MISC subtype (unused) |
|
|
56 | MISC_EXT1 = 24, |
|
|
57 | MISC_FLOAT16 = 25, |
|
|
58 | MISC_FLOAT32 = 26, |
|
|
59 | MISC_FLOAT64 = 27, |
|
|
60 | |
|
|
61 | // BYTES/TEXT/ARRAY/MAP |
|
|
62 | MINOR_INDEF = 31, |
|
|
63 | }; |
|
|
64 | |
|
|
65 | // known tags |
15 | enum cbor_tag |
66 | enum cbor_tag |
16 | { |
67 | { |
|
|
68 | // extensions |
|
|
69 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
|
|
70 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
|
|
71 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
|
|
72 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
|
|
73 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
|
|
74 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref |
|
|
75 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
|
|
76 | |
|
|
77 | // rfc7049 |
17 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
78 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
18 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
79 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
19 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
80 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
20 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
81 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
21 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
82 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
22 | CBOR_TAG_BIGFLOAT = 5, // array |
83 | CBOR_TAG_BIGFLOAT = 5, // array |
23 | |
84 | |
24 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
85 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
25 | CBOR_TAG_CONV_B64 = 22, // base64, any |
86 | CBOR_TAG_CONV_B64 = 22, // base64, any |
26 | CBOR_TAG_CONV_HEX = 23, // base16, any |
87 | CBOR_TAG_CONV_HEX = 23, // base16, any |
27 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
88 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
28 | |
89 | |
29 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
90 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
30 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
91 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
31 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
92 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
32 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
93 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
33 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
94 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
34 | |
95 | |
35 | CBOR_TAG_MAGIC = 55799 |
96 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
36 | }; |
97 | }; |
37 | |
98 | |
38 | #define F_SHRINK 0x00000200UL |
99 | #define F_SHRINK 0x00000001UL |
39 | #define F_ALLOW_UNKNOWN 0x00002000UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
|
|
101 | #define F_ALLOW_SHARING 0x00000004UL |
|
|
102 | #define F_PACK_STRINGS 0x00000008UL |
40 | |
103 | |
41 | #define INIT_SIZE 32 // initial scalar size to be allocated |
104 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | |
105 | |
43 | #define SB do { |
106 | #define SB do { |
44 | #define SE } while (0) |
107 | #define SE } while (0) |
… | |
… | |
55 | #else |
118 | #else |
56 | # define CBOR_SLOW 0 |
119 | # define CBOR_SLOW 0 |
57 | # define CBOR_STASH cbor_stash |
120 | # define CBOR_STASH cbor_stash |
58 | #endif |
121 | #endif |
59 | |
122 | |
60 | static HV *cbor_stash, *cbor_boolean_stash, *cbor_tagged_stash; // CBOR::XS:: |
123 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
61 | static SV *cbor_true, *cbor_false; |
124 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
62 | |
125 | |
63 | typedef struct { |
126 | typedef struct { |
64 | U32 flags; |
127 | U32 flags; |
65 | U32 max_depth; |
128 | U32 max_depth; |
66 | STRLEN max_size; |
129 | STRLEN max_size; |
|
|
130 | SV *filter; |
67 | } CBOR; |
131 | } CBOR; |
68 | |
132 | |
69 | ecb_inline void |
133 | ecb_inline void |
70 | cbor_init (CBOR *cbor) |
134 | cbor_init (CBOR *cbor) |
71 | { |
135 | { |
72 | Zero (cbor, 1, CBOR); |
136 | Zero (cbor, 1, CBOR); |
73 | cbor->max_depth = 512; |
137 | cbor->max_depth = 512; |
|
|
138 | } |
|
|
139 | |
|
|
140 | ecb_inline void |
|
|
141 | cbor_free (CBOR *cbor) |
|
|
142 | { |
|
|
143 | SvREFCNT_dec (cbor->filter); |
74 | } |
144 | } |
75 | |
145 | |
76 | ///////////////////////////////////////////////////////////////////////////// |
146 | ///////////////////////////////////////////////////////////////////////////// |
77 | // utility functions |
147 | // utility functions |
78 | |
148 | |
… | |
… | |
100 | SvPV_renew (sv, SvCUR (sv) + 1); |
170 | SvPV_renew (sv, SvCUR (sv) + 1); |
101 | #endif |
171 | #endif |
102 | } |
172 | } |
103 | } |
173 | } |
104 | |
174 | |
105 | ///////////////////////////////////////////////////////////////////////////// |
175 | // minimum length of a string to be registered for stringref |
106 | // fp hell |
176 | ecb_inline int |
107 | |
177 | minimum_string_length (UV idx) |
108 | //TODO |
178 | { |
|
|
179 | return idx > 23 |
|
|
180 | ? idx > 0xffU |
|
|
181 | ? idx > 0xffffU |
|
|
182 | ? idx > 0xffffffffU |
|
|
183 | ? 11 |
|
|
184 | : 7 |
|
|
185 | : 5 |
|
|
186 | : 4 |
|
|
187 | : 3; |
|
|
188 | } |
109 | |
189 | |
110 | ///////////////////////////////////////////////////////////////////////////// |
190 | ///////////////////////////////////////////////////////////////////////////// |
111 | // encoder |
191 | // encoder |
112 | |
192 | |
113 | // structure used for encoding CBOR |
193 | // structure used for encoding CBOR |
… | |
… | |
116 | char *cur; // SvPVX (sv) + current output position |
196 | char *cur; // SvPVX (sv) + current output position |
117 | char *end; // SvEND (sv) |
197 | char *end; // SvEND (sv) |
118 | SV *sv; // result scalar |
198 | SV *sv; // result scalar |
119 | CBOR cbor; |
199 | CBOR cbor; |
120 | U32 depth; // recursion level |
200 | U32 depth; // recursion level |
|
|
201 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
202 | UV stringref_idx; |
|
|
203 | HV *shareable; // ptr => index, or 0 |
|
|
204 | UV shareable_idx; |
121 | } enc_t; |
205 | } enc_t; |
122 | |
206 | |
123 | ecb_inline void |
207 | ecb_inline void |
124 | need (enc_t *enc, STRLEN len) |
208 | need (enc_t *enc, STRLEN len) |
125 | { |
209 | { |
… | |
… | |
142 | static void |
226 | static void |
143 | encode_uint (enc_t *enc, int major, UV len) |
227 | encode_uint (enc_t *enc, int major, UV len) |
144 | { |
228 | { |
145 | need (enc, 9); |
229 | need (enc, 9); |
146 | |
230 | |
147 | if (len < 24) |
231 | if (ecb_expect_true (len < LENGTH_EXT1)) |
148 | *enc->cur++ = major | len; |
232 | *enc->cur++ = major | len; |
149 | else if (len <= 0xff) |
233 | else if (ecb_expect_true (len <= 0xffU)) |
150 | { |
234 | { |
151 | *enc->cur++ = major | 24; |
235 | *enc->cur++ = major | LENGTH_EXT1; |
152 | *enc->cur++ = len; |
236 | *enc->cur++ = len; |
153 | } |
237 | } |
154 | else if (len <= 0xffff) |
238 | else if (len <= 0xffffU) |
155 | { |
239 | { |
156 | *enc->cur++ = major | 25; |
240 | *enc->cur++ = major | LENGTH_EXT2; |
157 | *enc->cur++ = len >> 8; |
241 | *enc->cur++ = len >> 8; |
158 | *enc->cur++ = len; |
242 | *enc->cur++ = len; |
159 | } |
243 | } |
160 | else if (len <= 0xffffffff) |
244 | else if (len <= 0xffffffffU) |
161 | { |
245 | { |
162 | *enc->cur++ = major | 26; |
246 | *enc->cur++ = major | LENGTH_EXT4; |
163 | *enc->cur++ = len >> 24; |
247 | *enc->cur++ = len >> 24; |
164 | *enc->cur++ = len >> 16; |
248 | *enc->cur++ = len >> 16; |
165 | *enc->cur++ = len >> 8; |
249 | *enc->cur++ = len >> 8; |
166 | *enc->cur++ = len; |
250 | *enc->cur++ = len; |
167 | } |
251 | } |
168 | else |
252 | else |
169 | { |
253 | { |
170 | *enc->cur++ = major | 27; |
254 | *enc->cur++ = major | LENGTH_EXT8; |
171 | *enc->cur++ = len >> 56; |
255 | *enc->cur++ = len >> 56; |
172 | *enc->cur++ = len >> 48; |
256 | *enc->cur++ = len >> 48; |
173 | *enc->cur++ = len >> 40; |
257 | *enc->cur++ = len >> 40; |
174 | *enc->cur++ = len >> 32; |
258 | *enc->cur++ = len >> 32; |
175 | *enc->cur++ = len >> 24; |
259 | *enc->cur++ = len >> 24; |
… | |
… | |
177 | *enc->cur++ = len >> 8; |
261 | *enc->cur++ = len >> 8; |
178 | *enc->cur++ = len; |
262 | *enc->cur++ = len; |
179 | } |
263 | } |
180 | } |
264 | } |
181 | |
265 | |
182 | static void |
266 | ecb_inline void |
|
|
267 | encode_tag (enc_t *enc, UV tag) |
|
|
268 | { |
|
|
269 | encode_uint (enc, MAJOR_TAG, tag); |
|
|
270 | } |
|
|
271 | |
|
|
272 | ecb_inline void |
183 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
273 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
184 | { |
274 | { |
185 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
275 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
186 | need (enc, len); |
276 | need (enc, len); |
187 | memcpy (enc->cur, str, len); |
277 | memcpy (enc->cur, str, len); |
188 | enc->cur += len; |
278 | enc->cur += len; |
189 | } |
279 | } |
190 | |
280 | |
|
|
281 | static void |
|
|
282 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
283 | { |
|
|
284 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
|
|
285 | { |
|
|
286 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
|
|
287 | |
|
|
288 | if (SvOK (*svp)) |
|
|
289 | { |
|
|
290 | // already registered, use stringref |
|
|
291 | encode_tag (enc, CBOR_TAG_STRINGREF); |
|
|
292 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
293 | return; |
|
|
294 | } |
|
|
295 | else if (len >= minimum_string_length (enc->stringref_idx)) |
|
|
296 | { |
|
|
297 | // register only |
|
|
298 | sv_setuv (*svp, enc->stringref_idx); |
|
|
299 | ++enc->stringref_idx; |
|
|
300 | } |
|
|
301 | } |
|
|
302 | |
|
|
303 | encode_str (enc, utf8, str, len); |
|
|
304 | } |
|
|
305 | |
191 | static void encode_sv (enc_t *enc, SV *sv); |
306 | static void encode_sv (enc_t *enc, SV *sv); |
192 | |
307 | |
193 | static void |
308 | static void |
194 | encode_av (enc_t *enc, AV *av) |
309 | encode_av (enc_t *enc, AV *av) |
195 | { |
310 | { |
… | |
… | |
198 | if (enc->depth >= enc->cbor.max_depth) |
313 | if (enc->depth >= enc->cbor.max_depth) |
199 | croak (ERR_NESTING_EXCEEDED); |
314 | croak (ERR_NESTING_EXCEEDED); |
200 | |
315 | |
201 | ++enc->depth; |
316 | ++enc->depth; |
202 | |
317 | |
203 | encode_uint (enc, 0x80, len + 1); |
318 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
204 | |
319 | |
205 | for (i = 0; i <= len; ++i) |
320 | for (i = 0; i <= len; ++i) |
206 | { |
321 | { |
207 | SV **svp = av_fetch (av, i, 0); |
322 | SV **svp = av_fetch (av, i, 0); |
208 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
323 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
… | |
… | |
223 | |
338 | |
224 | int pairs = hv_iterinit (hv); |
339 | int pairs = hv_iterinit (hv); |
225 | int mg = SvMAGICAL (hv); |
340 | int mg = SvMAGICAL (hv); |
226 | |
341 | |
227 | if (mg) |
342 | if (mg) |
228 | encode_ch (enc, 0xa0 | 31); |
343 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
229 | else |
344 | else |
230 | encode_uint (enc, 0xa0, pairs); |
345 | encode_uint (enc, MAJOR_MAP, pairs); |
231 | |
346 | |
232 | while ((he = hv_iternext (hv))) |
347 | while ((he = hv_iternext (hv))) |
233 | { |
348 | { |
234 | if (HeKLEN (he) == HEf_SVKEY) |
349 | if (HeKLEN (he) == HEf_SVKEY) |
235 | encode_sv (enc, HeSVKEY (he)); |
350 | encode_sv (enc, HeSVKEY (he)); |
236 | else |
351 | else |
237 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
352 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
238 | |
353 | |
239 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
354 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
240 | } |
355 | } |
241 | |
356 | |
242 | if (mg) |
357 | if (mg) |
243 | encode_ch (enc, 0xe0 | 31); |
358 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
244 | |
359 | |
245 | --enc->depth; |
360 | --enc->depth; |
246 | } |
361 | } |
247 | |
362 | |
248 | // encode objects, arrays and special \0=false and \1=true values. |
363 | // encode objects, arrays and special \0=false and \1=true values. |
249 | static void |
364 | static void |
250 | encode_rv (enc_t *enc, SV *sv) |
365 | encode_rv (enc_t *enc, SV *sv) |
251 | { |
366 | { |
252 | svtype svt; |
|
|
253 | |
|
|
254 | SvGETMAGIC (sv); |
367 | SvGETMAGIC (sv); |
|
|
368 | |
255 | svt = SvTYPE (sv); |
369 | svtype svt = SvTYPE (sv); |
256 | |
370 | |
257 | if (ecb_expect_false (SvOBJECT (sv))) |
371 | if (ecb_expect_false (SvOBJECT (sv))) |
258 | { |
372 | { |
259 | HV *boolean_stash = !CBOR_SLOW || cbor_boolean_stash |
373 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
260 | ? cbor_boolean_stash |
374 | ? types_boolean_stash |
261 | : gv_stashpv ("CBOR::XS::Boolean", 1); |
375 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
376 | HV *error_stash = !CBOR_SLOW || types_error_stash |
|
|
377 | ? types_error_stash |
|
|
378 | : gv_stashpv ("Types::Serialiser::Error", 1); |
262 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
379 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
263 | ? cbor_tagged_stash |
380 | ? cbor_tagged_stash |
264 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
381 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
265 | |
382 | |
|
|
383 | HV *stash = SvSTASH (sv); |
|
|
384 | |
266 | if (SvSTASH (sv) == boolean_stash) |
385 | if (stash == boolean_stash) |
267 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
386 | { |
|
|
387 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
388 | return; |
|
|
389 | } |
|
|
390 | else if (stash == error_stash) |
|
|
391 | { |
|
|
392 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
393 | return; |
|
|
394 | } |
268 | else if (SvSTASH (sv) == tagged_stash) |
395 | else if (stash == tagged_stash) |
269 | { |
396 | { |
270 | if (svt != SVt_PVAV) |
397 | if (svt != SVt_PVAV) |
271 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
398 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
272 | |
399 | |
273 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
400 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
274 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
401 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
402 | |
|
|
403 | return; |
|
|
404 | } |
|
|
405 | } |
|
|
406 | |
|
|
407 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
408 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
409 | { |
|
|
410 | if (!enc->shareable) |
|
|
411 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
412 | |
|
|
413 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
414 | |
|
|
415 | if (SvOK (*svp)) |
|
|
416 | { |
|
|
417 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
418 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
419 | return; |
275 | } |
420 | } |
276 | else |
421 | else |
277 | { |
422 | { |
|
|
423 | sv_setuv (*svp, enc->shareable_idx); |
|
|
424 | ++enc->shareable_idx; |
|
|
425 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
430 | { |
|
|
431 | HV *stash = SvSTASH (sv); |
|
|
432 | GV *method; |
|
|
433 | |
|
|
434 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
|
|
435 | { |
|
|
436 | dSP; |
|
|
437 | |
|
|
438 | ENTER; SAVETMPS; PUSHMARK (SP); |
278 | // we re-bless the reference to get overload and other niceties right |
439 | // we re-bless the reference to get overload and other niceties right |
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))); |
440 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
287 | |
441 | |
288 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
289 | PUTBACK; |
442 | PUTBACK; |
|
|
443 | // G_SCALAR ensures that return value is 1 |
290 | call_sv ((SV *)GvCV (to_cbor), G_SCALAR); |
444 | call_sv ((SV *)GvCV (method), G_SCALAR); |
291 | SPAGAIN; |
445 | SPAGAIN; |
292 | |
446 | |
293 | // catch this surprisingly common error |
447 | // catch this surprisingly common error |
294 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
448 | 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))); |
449 | croak ("%s::TO_CBOR method returned same object as was passed instead of a new one", HvNAME (stash)); |
296 | |
450 | |
297 | sv = POPs; |
451 | encode_sv (enc, POPs); |
|
|
452 | |
298 | PUTBACK; |
453 | PUTBACK; |
299 | |
454 | |
300 | encode_sv (enc, sv); |
|
|
301 | |
|
|
302 | FREETMPS; LEAVE; |
455 | FREETMPS; LEAVE; |
303 | } |
456 | } |
|
|
457 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
|
|
458 | { |
|
|
459 | dSP; |
|
|
460 | |
|
|
461 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
462 | EXTEND (SP, 2); |
|
|
463 | // we re-bless the reference to get overload and other niceties right |
|
|
464 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
465 | PUSHs (sv_cbor); |
|
|
466 | |
|
|
467 | PUTBACK; |
|
|
468 | int count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
469 | SPAGAIN; |
|
|
470 | |
|
|
471 | // catch this surprisingly common error |
|
|
472 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
473 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
|
|
474 | |
|
|
475 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
|
|
476 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
|
|
477 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
|
|
478 | |
|
|
479 | while (count) |
|
|
480 | encode_sv (enc, SP[1 - count--]); |
|
|
481 | |
|
|
482 | PUTBACK; |
|
|
483 | |
|
|
484 | FREETMPS; LEAVE; |
|
|
485 | } |
304 | else |
486 | else |
305 | croak ("encountered object '%s', but no TO_CBOR method available on it", |
487 | croak ("encountered object '%s', but no TO_CBOR or FREEZE methods available on it", |
306 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
488 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
307 | } |
|
|
308 | } |
489 | } |
309 | else if (svt == SVt_PVHV) |
490 | else if (svt == SVt_PVHV) |
310 | encode_hv (enc, (HV *)sv); |
491 | encode_hv (enc, (HV *)sv); |
311 | else if (svt == SVt_PVAV) |
492 | else if (svt == SVt_PVAV) |
312 | encode_av (enc, (AV *)sv); |
493 | 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 |
494 | else |
331 | croak ("encountered %s, but CBOR can only represent references to arrays or hashes", |
495 | { |
332 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
496 | encode_tag (enc, CBOR_TAG_INDIRECTION); |
|
|
497 | encode_sv (enc, sv); |
|
|
498 | } |
333 | } |
499 | } |
334 | |
500 | |
335 | static void |
501 | static void |
336 | encode_nv (enc_t *enc, SV *sv) |
502 | encode_nv (enc_t *enc, SV *sv) |
337 | { |
503 | { |
338 | double nv = SvNVX (sv); |
504 | double nv = SvNVX (sv); |
339 | |
505 | |
340 | need (enc, 9); |
506 | need (enc, 9); |
341 | |
507 | |
342 | if (ecb_expect_false (nv == (U32)nv)) |
508 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
343 | encode_uint (enc, 0x00, (U32)nv); |
509 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
344 | //TODO: maybe I32? |
510 | //TODO: maybe I32? |
345 | else if (ecb_expect_false (nv == (float)nv)) |
511 | else if (ecb_expect_false (nv == (float)nv)) |
346 | { |
512 | { |
347 | uint32_t fp = ecb_float_to_binary32 (nv); |
513 | uint32_t fp = ecb_float_to_binary32 (nv); |
348 | |
514 | |
349 | *enc->cur++ = 0xe0 | 26; |
515 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
350 | |
516 | |
351 | if (!ecb_big_endian ()) |
517 | if (!ecb_big_endian ()) |
352 | fp = ecb_bswap32 (fp); |
518 | fp = ecb_bswap32 (fp); |
353 | |
519 | |
354 | memcpy (enc->cur, &fp, 4); |
520 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
356 | } |
522 | } |
357 | else |
523 | else |
358 | { |
524 | { |
359 | uint64_t fp = ecb_double_to_binary64 (nv); |
525 | uint64_t fp = ecb_double_to_binary64 (nv); |
360 | |
526 | |
361 | *enc->cur++ = 0xe0 | 27; |
527 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
362 | |
528 | |
363 | if (!ecb_big_endian ()) |
529 | if (!ecb_big_endian ()) |
364 | fp = ecb_bswap64 (fp); |
530 | fp = ecb_bswap64 (fp); |
365 | |
531 | |
366 | memcpy (enc->cur, &fp, 8); |
532 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
375 | |
541 | |
376 | if (SvPOKp (sv)) |
542 | if (SvPOKp (sv)) |
377 | { |
543 | { |
378 | STRLEN len; |
544 | STRLEN len; |
379 | char *str = SvPV (sv, len); |
545 | char *str = SvPV (sv, len); |
380 | encode_str (enc, SvUTF8 (sv), str, len); |
546 | encode_strref (enc, SvUTF8 (sv), str, len); |
381 | } |
547 | } |
382 | else if (SvNOKp (sv)) |
548 | else if (SvNOKp (sv)) |
383 | encode_nv (enc, sv); |
549 | encode_nv (enc, sv); |
384 | else if (SvIOKp (sv)) |
550 | else if (SvIOKp (sv)) |
385 | { |
551 | { |
386 | if (SvIsUV (sv)) |
552 | if (SvIsUV (sv)) |
387 | encode_uint (enc, 0x00, SvUVX (sv)); |
553 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
388 | else if (SvIVX (sv) >= 0) |
554 | else if (SvIVX (sv) >= 0) |
389 | encode_uint (enc, 0x00, SvIVX (sv)); |
555 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
390 | else |
556 | else |
391 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
557 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
392 | } |
558 | } |
393 | else if (SvROK (sv)) |
559 | else if (SvROK (sv)) |
394 | encode_rv (enc, SvRV (sv)); |
560 | encode_rv (enc, SvRV (sv)); |
395 | else if (!SvOK (sv)) |
561 | else if (!SvOK (sv)) |
396 | encode_ch (enc, 0xe0 | 22); |
562 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
397 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
563 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
398 | encode_ch (enc, 0xe0 | 23); |
564 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
399 | else |
565 | else |
400 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
566 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
401 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
567 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
402 | } |
568 | } |
403 | |
569 | |
404 | static SV * |
570 | static SV * |
405 | encode_cbor (SV *scalar, CBOR *cbor) |
571 | encode_cbor (SV *scalar, CBOR *cbor) |
406 | { |
572 | { |
407 | enc_t enc; |
573 | enc_t enc = { }; |
408 | |
574 | |
409 | enc.cbor = *cbor; |
575 | enc.cbor = *cbor; |
410 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
576 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
411 | enc.cur = SvPVX (enc.sv); |
577 | enc.cur = SvPVX (enc.sv); |
412 | enc.end = SvEND (enc.sv); |
578 | enc.end = SvEND (enc.sv); |
413 | enc.depth = 0; |
|
|
414 | |
579 | |
415 | SvPOK_only (enc.sv); |
580 | SvPOK_only (enc.sv); |
|
|
581 | |
|
|
582 | if (cbor->flags & F_PACK_STRINGS) |
|
|
583 | { |
|
|
584 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
585 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
586 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
587 | } |
|
|
588 | |
416 | encode_sv (&enc, scalar); |
589 | encode_sv (&enc, scalar); |
417 | |
590 | |
418 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
591 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
419 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
592 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
420 | |
593 | |
… | |
… | |
434 | U8 *end; // end of input string |
607 | U8 *end; // end of input string |
435 | const char *err; // parse error, if != 0 |
608 | const char *err; // parse error, if != 0 |
436 | CBOR cbor; |
609 | CBOR cbor; |
437 | U32 depth; // recursion depth |
610 | U32 depth; // recursion depth |
438 | U32 maxdepth; // recursion depth limit |
611 | U32 maxdepth; // recursion depth limit |
|
|
612 | AV *shareable; |
|
|
613 | AV *stringref; |
|
|
614 | SV *decode_tagged; |
439 | } dec_t; |
615 | } dec_t; |
440 | |
616 | |
441 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
617 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
442 | |
618 | |
443 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
619 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
446 | #define DEC_DEC_DEPTH --dec->depth |
622 | #define DEC_DEC_DEPTH --dec->depth |
447 | |
623 | |
448 | static UV |
624 | static UV |
449 | decode_uint (dec_t *dec) |
625 | decode_uint (dec_t *dec) |
450 | { |
626 | { |
451 | switch (*dec->cur & 31) |
627 | U8 m = *dec->cur & MINOR_MASK; |
452 | { |
628 | ++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 | |
629 | |
458 | case 24: |
630 | if (ecb_expect_true (m < LENGTH_EXT1)) |
|
|
631 | return m; |
|
|
632 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
|
|
633 | { |
459 | WANT (2); |
634 | WANT (1); |
460 | dec->cur += 2; |
635 | dec->cur += 1; |
461 | return dec->cur[-1]; |
636 | return dec->cur[-1]; |
462 | |
637 | } |
463 | case 25: |
638 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
639 | { |
464 | WANT (3); |
640 | WANT (2); |
465 | dec->cur += 3; |
641 | dec->cur += 2; |
466 | return (((UV)dec->cur[-2]) << 8) |
642 | return (((UV)dec->cur[-2]) << 8) |
467 | | ((UV)dec->cur[-1]); |
643 | | ((UV)dec->cur[-1]); |
468 | |
644 | } |
469 | case 26: |
645 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
646 | { |
470 | WANT (5); |
647 | WANT (4); |
471 | dec->cur += 5; |
648 | dec->cur += 4; |
472 | return (((UV)dec->cur[-4]) << 24) |
649 | return (((UV)dec->cur[-4]) << 24) |
473 | | (((UV)dec->cur[-3]) << 16) |
650 | | (((UV)dec->cur[-3]) << 16) |
474 | | (((UV)dec->cur[-2]) << 8) |
651 | | (((UV)dec->cur[-2]) << 8) |
475 | | ((UV)dec->cur[-1]); |
652 | | ((UV)dec->cur[-1]); |
476 | |
653 | } |
477 | case 27: |
654 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
655 | { |
478 | WANT (9); |
656 | WANT (8); |
479 | dec->cur += 9; |
657 | dec->cur += 8; |
|
|
658 | |
|
|
659 | return |
|
|
660 | #if UVSIZE < 8 |
|
|
661 | 0 |
|
|
662 | #else |
480 | return (((UV)dec->cur[-8]) << 56) |
663 | (((UV)dec->cur[-8]) << 56) |
481 | | (((UV)dec->cur[-7]) << 48) |
664 | | (((UV)dec->cur[-7]) << 48) |
482 | | (((UV)dec->cur[-6]) << 40) |
665 | | (((UV)dec->cur[-6]) << 40) |
483 | | (((UV)dec->cur[-5]) << 32) |
666 | | (((UV)dec->cur[-5]) << 32) |
|
|
667 | #endif |
484 | | (((UV)dec->cur[-4]) << 24) |
668 | | (((UV)dec->cur[-4]) << 24) |
485 | | (((UV)dec->cur[-3]) << 16) |
669 | | (((UV)dec->cur[-3]) << 16) |
486 | | (((UV)dec->cur[-2]) << 8) |
670 | | (((UV)dec->cur[-2]) << 8) |
487 | | ((UV)dec->cur[-1]); |
671 | | ((UV)dec->cur[-1]); |
488 | |
672 | } |
489 | default: |
673 | else |
490 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
674 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
491 | } |
|
|
492 | |
675 | |
493 | fail: |
676 | fail: |
494 | return 0; |
677 | return 0; |
495 | } |
678 | } |
496 | |
679 | |
… | |
… | |
501 | { |
684 | { |
502 | AV *av = newAV (); |
685 | AV *av = newAV (); |
503 | |
686 | |
504 | DEC_INC_DEPTH; |
687 | DEC_INC_DEPTH; |
505 | |
688 | |
506 | if ((*dec->cur & 31) == 31) |
689 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
507 | { |
690 | { |
508 | ++dec->cur; |
691 | ++dec->cur; |
509 | |
692 | |
510 | for (;;) |
693 | for (;;) |
511 | { |
694 | { |
512 | WANT (1); |
695 | WANT (1); |
513 | |
696 | |
514 | if (*dec->cur == (0xe0 | 31)) |
697 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
515 | { |
698 | { |
516 | ++dec->cur; |
699 | ++dec->cur; |
517 | break; |
700 | break; |
518 | } |
701 | } |
519 | |
702 | |
… | |
… | |
522 | } |
705 | } |
523 | else |
706 | else |
524 | { |
707 | { |
525 | int i, len = decode_uint (dec); |
708 | int i, len = decode_uint (dec); |
526 | |
709 | |
|
|
710 | 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); |
711 | av_fill (av, len - 1); |
528 | |
712 | |
529 | for (i = 0; i < len; ++i) |
713 | for (i = 0; i < len; ++i) |
530 | AvARRAY (av)[i] = decode_sv (dec); |
714 | AvARRAY (av)[i] = decode_sv (dec); |
531 | } |
715 | } |
… | |
… | |
537 | SvREFCNT_dec (av); |
721 | SvREFCNT_dec (av); |
538 | DEC_DEC_DEPTH; |
722 | DEC_DEC_DEPTH; |
539 | return &PL_sv_undef; |
723 | return &PL_sv_undef; |
540 | } |
724 | } |
541 | |
725 | |
|
|
726 | static void |
|
|
727 | decode_he (dec_t *dec, HV *hv) |
|
|
728 | { |
|
|
729 | // for speed reasons, we specialcase single-string |
|
|
730 | // byte or utf-8 strings as keys, but only when !stringref |
|
|
731 | |
|
|
732 | if (ecb_expect_true (!dec->stringref)) |
|
|
733 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
|
|
734 | { |
|
|
735 | I32 len = decode_uint (dec); |
|
|
736 | char *key = (char *)dec->cur; |
|
|
737 | |
|
|
738 | dec->cur += len; |
|
|
739 | |
|
|
740 | if (ecb_expect_false (dec->stringref)) |
|
|
741 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
742 | |
|
|
743 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
744 | |
|
|
745 | return; |
|
|
746 | } |
|
|
747 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
|
|
748 | { |
|
|
749 | I32 len = decode_uint (dec); |
|
|
750 | char *key = (char *)dec->cur; |
|
|
751 | |
|
|
752 | dec->cur += len; |
|
|
753 | |
|
|
754 | if (ecb_expect_false (dec->stringref)) |
|
|
755 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
|
|
756 | |
|
|
757 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
758 | |
|
|
759 | return; |
|
|
760 | } |
|
|
761 | |
|
|
762 | SV *k = decode_sv (dec); |
|
|
763 | SV *v = decode_sv (dec); |
|
|
764 | |
|
|
765 | hv_store_ent (hv, k, v, 0); |
|
|
766 | SvREFCNT_dec (k); |
|
|
767 | } |
|
|
768 | |
542 | static SV * |
769 | static SV * |
543 | decode_hv (dec_t *dec) |
770 | decode_hv (dec_t *dec) |
544 | { |
771 | { |
545 | HV *hv = newHV (); |
772 | HV *hv = newHV (); |
546 | |
773 | |
547 | DEC_INC_DEPTH; |
774 | DEC_INC_DEPTH; |
548 | |
775 | |
549 | if ((*dec->cur & 31) == 31) |
776 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
550 | { |
777 | { |
551 | ++dec->cur; |
778 | ++dec->cur; |
552 | |
779 | |
553 | for (;;) |
780 | for (;;) |
554 | { |
781 | { |
555 | WANT (1); |
782 | WANT (1); |
556 | |
783 | |
557 | if (*dec->cur == (0xe0 | 31)) |
784 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
558 | { |
785 | { |
559 | ++dec->cur; |
786 | ++dec->cur; |
560 | break; |
787 | break; |
561 | } |
788 | } |
562 | |
789 | |
563 | SV *k = decode_sv (dec); |
790 | decode_he (dec, hv); |
564 | SV *v = decode_sv (dec); |
|
|
565 | |
|
|
566 | hv_store_ent (hv, k, v, 0); |
|
|
567 | } |
791 | } |
568 | } |
792 | } |
569 | else |
793 | else |
570 | { |
794 | { |
571 | int len = decode_uint (dec); |
795 | int pairs = decode_uint (dec); |
572 | |
796 | |
573 | while (len--) |
797 | while (pairs--) |
574 | { |
798 | 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 | } |
799 | } |
581 | |
800 | |
582 | DEC_DEC_DEPTH; |
801 | DEC_DEC_DEPTH; |
583 | return newRV_noinc ((SV *)hv); |
802 | return newRV_noinc ((SV *)hv); |
584 | |
803 | |
… | |
… | |
591 | static SV * |
810 | static SV * |
592 | decode_str (dec_t *dec, int utf8) |
811 | decode_str (dec_t *dec, int utf8) |
593 | { |
812 | { |
594 | SV *sv = 0; |
813 | SV *sv = 0; |
595 | |
814 | |
596 | if ((*dec->cur & 31) == 31) |
815 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
597 | { |
816 | { |
|
|
817 | // indefinite length strings |
598 | ++dec->cur; |
818 | ++dec->cur; |
599 | |
819 | |
|
|
820 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
821 | |
600 | sv = newSVpvn ("", 0); |
822 | sv = newSVpvn ("", 0); |
601 | |
823 | |
602 | // not very fast, and certainly not robust against illegal input |
|
|
603 | for (;;) |
824 | for (;;) |
604 | { |
825 | { |
605 | WANT (1); |
826 | WANT (1); |
606 | |
827 | |
607 | if (*dec->cur == (0xe0 | 31)) |
828 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
829 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
608 | { |
830 | { |
609 | ++dec->cur; |
831 | ++dec->cur; |
610 | break; |
832 | break; |
611 | } |
833 | } |
|
|
834 | else |
|
|
835 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
612 | |
836 | |
613 | sv_catsv (sv, decode_sv (dec)); |
837 | STRLEN len = decode_uint (dec); |
|
|
838 | |
|
|
839 | WANT (len); |
|
|
840 | sv_catpvn (sv, dec->cur, len); |
|
|
841 | dec->cur += len; |
614 | } |
842 | } |
615 | } |
843 | } |
616 | else |
844 | else |
617 | { |
845 | { |
618 | STRLEN len = decode_uint (dec); |
846 | STRLEN len = decode_uint (dec); |
619 | |
847 | |
620 | WANT (len); |
848 | WANT (len); |
621 | sv = newSVpvn (dec->cur, len); |
849 | sv = newSVpvn (dec->cur, len); |
622 | dec->cur += len; |
850 | dec->cur += len; |
|
|
851 | |
|
|
852 | if (ecb_expect_false (dec->stringref) |
|
|
853 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
854 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
623 | } |
855 | } |
624 | |
856 | |
625 | if (utf8) |
857 | if (utf8) |
626 | SvUTF8_on (sv); |
858 | SvUTF8_on (sv); |
627 | |
859 | |
… | |
… | |
633 | } |
865 | } |
634 | |
866 | |
635 | static SV * |
867 | static SV * |
636 | decode_tagged (dec_t *dec) |
868 | decode_tagged (dec_t *dec) |
637 | { |
869 | { |
|
|
870 | SV *sv = 0; |
638 | UV tag = decode_uint (dec); |
871 | UV tag = decode_uint (dec); |
|
|
872 | |
|
|
873 | WANT (1); |
|
|
874 | |
|
|
875 | switch (tag) |
|
|
876 | { |
|
|
877 | case CBOR_TAG_MAGIC: |
|
|
878 | sv = decode_sv (dec); |
|
|
879 | break; |
|
|
880 | |
|
|
881 | case CBOR_TAG_INDIRECTION: |
|
|
882 | sv = newRV_noinc (decode_sv (dec)); |
|
|
883 | break; |
|
|
884 | |
|
|
885 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
886 | { |
|
|
887 | ENTER; SAVETMPS; |
|
|
888 | |
|
|
889 | SAVESPTR (dec->stringref); |
|
|
890 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
891 | |
|
|
892 | sv = decode_sv (dec); |
|
|
893 | |
|
|
894 | FREETMPS; LEAVE; |
|
|
895 | } |
|
|
896 | break; |
|
|
897 | |
|
|
898 | case CBOR_TAG_STRINGREF: |
|
|
899 | { |
|
|
900 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
|
|
901 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
902 | |
|
|
903 | UV idx = decode_uint (dec); |
|
|
904 | |
|
|
905 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
906 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
907 | |
|
|
908 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
909 | } |
|
|
910 | break; |
|
|
911 | |
|
|
912 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
913 | { |
|
|
914 | if (ecb_expect_false (!dec->shareable)) |
|
|
915 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
916 | |
|
|
917 | sv = newSV (0); |
|
|
918 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
|
|
919 | |
639 | SV *sv = decode_sv (dec); |
920 | SV *osv = decode_sv (dec); |
|
|
921 | sv_setsv (sv, osv); |
|
|
922 | SvREFCNT_dec_NN (osv); |
|
|
923 | } |
|
|
924 | break; |
640 | |
925 | |
641 | if (tag == CBOR_TAG_MAGIC) // 2.4.5 Self-Describe CBOR |
926 | case CBOR_TAG_VALUE_SHAREDREF: |
|
|
927 | { |
|
|
928 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
|
|
929 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
|
|
930 | |
|
|
931 | UV idx = decode_uint (dec); |
|
|
932 | |
|
|
933 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
|
|
934 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
|
|
935 | |
|
|
936 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
937 | } |
|
|
938 | break; |
|
|
939 | |
|
|
940 | case CBOR_TAG_PERL_OBJECT: |
|
|
941 | { |
|
|
942 | sv = decode_sv (dec); |
|
|
943 | |
|
|
944 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
|
|
945 | ERR ("corrupted CBOR data (non-array perl object)"); |
|
|
946 | |
|
|
947 | AV *av = (AV *)SvRV (sv); |
|
|
948 | int len = av_len (av) + 1; |
|
|
949 | HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); |
|
|
950 | |
|
|
951 | if (!stash) |
|
|
952 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
953 | |
|
|
954 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
955 | |
|
|
956 | if (!method) |
|
|
957 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
958 | |
|
|
959 | dSP; |
|
|
960 | |
|
|
961 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
962 | EXTEND (SP, len + 1); |
|
|
963 | // we re-bless the reference to get overload and other niceties right |
|
|
964 | PUSHs (*av_fetch (av, 0, 1)); |
|
|
965 | PUSHs (sv_cbor); |
|
|
966 | |
|
|
967 | int i; |
|
|
968 | |
|
|
969 | for (i = 1; i < len; ++i) |
|
|
970 | PUSHs (*av_fetch (av, i, 1)); |
|
|
971 | |
|
|
972 | PUTBACK; |
|
|
973 | call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); |
|
|
974 | SPAGAIN; |
|
|
975 | |
|
|
976 | if (SvTRUE (ERRSV)) |
|
|
977 | { |
|
|
978 | FREETMPS; LEAVE; |
|
|
979 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
980 | } |
|
|
981 | |
|
|
982 | SvREFCNT_dec (sv); |
|
|
983 | sv = SvREFCNT_inc (POPs); |
|
|
984 | |
|
|
985 | PUTBACK; |
|
|
986 | |
|
|
987 | FREETMPS; LEAVE; |
|
|
988 | } |
|
|
989 | break; |
|
|
990 | |
|
|
991 | default: |
|
|
992 | { |
|
|
993 | sv = decode_sv (dec); |
|
|
994 | |
|
|
995 | dSP; |
|
|
996 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
997 | EXTEND (SP, 2); |
|
|
998 | PUSHs (newSVuv (tag)); |
|
|
999 | PUSHs (sv); |
|
|
1000 | |
|
|
1001 | PUTBACK; |
|
|
1002 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
1003 | SPAGAIN; |
|
|
1004 | |
|
|
1005 | if (SvTRUE (ERRSV)) |
|
|
1006 | { |
|
|
1007 | FREETMPS; LEAVE; |
|
|
1008 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
1009 | } |
|
|
1010 | |
|
|
1011 | if (count) |
|
|
1012 | { |
|
|
1013 | SvREFCNT_dec (sv); |
|
|
1014 | sv = SvREFCNT_inc (POPs); |
|
|
1015 | } |
|
|
1016 | else |
|
|
1017 | { |
|
|
1018 | AV *av = newAV (); |
|
|
1019 | av_push (av, newSVuv (tag)); |
|
|
1020 | av_push (av, sv); |
|
|
1021 | |
|
|
1022 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
|
|
1023 | ? cbor_tagged_stash |
|
|
1024 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
|
|
1025 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
1026 | } |
|
|
1027 | |
|
|
1028 | PUTBACK; |
|
|
1029 | |
|
|
1030 | FREETMPS; LEAVE; |
|
|
1031 | } |
|
|
1032 | break; |
|
|
1033 | } |
|
|
1034 | |
642 | return sv; |
1035 | return sv; |
643 | |
1036 | |
644 | AV *av = newAV (); |
1037 | fail: |
645 | av_push (av, newSVuv (tag)); |
1038 | SvREFCNT_dec (sv); |
646 | av_push (av, sv); |
1039 | 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 | } |
1040 | } |
654 | |
1041 | |
655 | static SV * |
1042 | static SV * |
656 | decode_sv (dec_t *dec) |
1043 | decode_sv (dec_t *dec) |
657 | { |
1044 | { |
658 | WANT (1); |
1045 | WANT (1); |
659 | |
1046 | |
660 | switch (*dec->cur >> 5) |
1047 | switch (*dec->cur >> MAJOR_SHIFT) |
661 | { |
1048 | { |
662 | case 0: // unsigned int |
1049 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
663 | return newSVuv (decode_uint (dec)); |
1050 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
664 | case 1: // negative int |
1051 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
665 | return newSViv (-1 - (IV)decode_uint (dec)); |
1052 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
666 | case 2: // octet string |
1053 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
667 | return decode_str (dec, 0); |
1054 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
668 | case 3: // utf-8 string |
1055 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
669 | return decode_str (dec, 1); |
1056 | |
670 | case 4: // array |
1057 | 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) |
1058 | switch (*dec->cur++ & MINOR_MASK) |
678 | { |
1059 | { |
679 | case 20: |
1060 | case SIMPLE_FALSE: |
680 | #if CBOR_SLOW |
1061 | #if CBOR_SLOW |
681 | cbor_false = get_bool ("CBOR::XS::false"); |
1062 | types_false = get_bool ("Types::Serialiser::false"); |
682 | #endif |
1063 | #endif |
683 | return newSVsv (cbor_false); |
1064 | return newSVsv (types_false); |
684 | case 21: |
1065 | case SIMPLE_TRUE: |
685 | #if CBOR_SLOW |
1066 | #if CBOR_SLOW |
686 | cbor_true = get_bool ("CBOR::XS::true"); |
1067 | types_true = get_bool ("Types::Serialiser::true"); |
687 | #endif |
1068 | #endif |
688 | return newSVsv (cbor_true); |
1069 | return newSVsv (types_true); |
689 | case 22: |
1070 | case SIMPLE_NULL: |
690 | return newSVsv (&PL_sv_undef); |
1071 | return newSVsv (&PL_sv_undef); |
|
|
1072 | case SIMPLE_UNDEF: |
|
|
1073 | #if CBOR_SLOW |
|
|
1074 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
1075 | #endif |
|
|
1076 | return newSVsv (types_error); |
691 | |
1077 | |
692 | case 25: |
1078 | case MISC_FLOAT16: |
693 | { |
1079 | { |
694 | WANT (2); |
1080 | WANT (2); |
695 | |
1081 | |
696 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1082 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
697 | dec->cur += 2; |
1083 | dec->cur += 2; |
698 | |
1084 | |
699 | return newSVnv (ecb_binary16_to_float (fp)); |
1085 | return newSVnv (ecb_binary16_to_float (fp)); |
700 | } |
1086 | } |
701 | |
1087 | |
702 | case 26: |
1088 | case MISC_FLOAT32: |
703 | { |
1089 | { |
704 | uint32_t fp; |
1090 | uint32_t fp; |
705 | WANT (4); |
1091 | WANT (4); |
706 | memcpy (&fp, dec->cur, 4); |
1092 | memcpy (&fp, dec->cur, 4); |
707 | dec->cur += 4; |
1093 | dec->cur += 4; |
… | |
… | |
710 | fp = ecb_bswap32 (fp); |
1096 | fp = ecb_bswap32 (fp); |
711 | |
1097 | |
712 | return newSVnv (ecb_binary32_to_float (fp)); |
1098 | return newSVnv (ecb_binary32_to_float (fp)); |
713 | } |
1099 | } |
714 | |
1100 | |
715 | case 27: |
1101 | case MISC_FLOAT64: |
716 | { |
1102 | { |
717 | uint64_t fp; |
1103 | uint64_t fp; |
718 | WANT (8); |
1104 | WANT (8); |
719 | memcpy (&fp, dec->cur, 8); |
1105 | memcpy (&fp, dec->cur, 8); |
720 | dec->cur += 8; |
1106 | dec->cur += 8; |
… | |
… | |
723 | fp = ecb_bswap64 (fp); |
1109 | fp = ecb_bswap64 (fp); |
724 | |
1110 | |
725 | return newSVnv (ecb_binary64_to_double (fp)); |
1111 | return newSVnv (ecb_binary64_to_double (fp)); |
726 | } |
1112 | } |
727 | |
1113 | |
728 | // 0..19 unassigned |
1114 | // 0..19 unassigned simple |
729 | // 24 reserved + unassigned (reserved values are not encodable) |
1115 | // 24 reserved + unassigned (reserved values are not encodable) |
730 | default: |
1116 | default: |
731 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1117 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
732 | } |
1118 | } |
733 | |
1119 | |
… | |
… | |
739 | } |
1125 | } |
740 | |
1126 | |
741 | static SV * |
1127 | static SV * |
742 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1128 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
743 | { |
1129 | { |
744 | dec_t dec; |
1130 | dec_t dec = { }; |
745 | SV *sv; |
1131 | SV *sv; |
|
|
1132 | STRLEN len; |
|
|
1133 | char *data = SvPVbyte (string, len); |
746 | |
1134 | |
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) |
1135 | 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", |
1136 | 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); |
1137 | (unsigned long)len, (unsigned long)cbor->max_size); |
778 | } |
|
|
779 | |
|
|
780 | sv_utf8_downgrade (string, 0); |
|
|
781 | |
1138 | |
782 | dec.cbor = *cbor; |
1139 | dec.cbor = *cbor; |
783 | dec.cur = (U8 *)SvPVX (string); |
1140 | dec.cur = (U8 *)data; |
784 | dec.end = (U8 *)SvEND (string); |
1141 | dec.end = (U8 *)data + len; |
785 | dec.err = 0; |
|
|
786 | dec.depth = 0; |
|
|
787 | |
1142 | |
788 | sv = decode_sv (&dec); |
1143 | sv = decode_sv (&dec); |
789 | |
1144 | |
790 | if (offset_return) |
1145 | if (offset_return) |
791 | *offset_return = dec.cur; |
1146 | *offset_return = dec.cur; |
… | |
… | |
795 | dec.err = "garbage after CBOR object"; |
1150 | dec.err = "garbage after CBOR object"; |
796 | |
1151 | |
797 | if (dec.err) |
1152 | if (dec.err) |
798 | { |
1153 | { |
799 | SvREFCNT_dec (sv); |
1154 | SvREFCNT_dec (sv); |
800 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); |
1155 | croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); |
801 | } |
1156 | } |
802 | |
1157 | |
803 | sv = sv_2mortal (sv); |
1158 | sv = sv_2mortal (sv); |
804 | |
1159 | |
805 | return sv; |
1160 | return sv; |
… | |
… | |
811 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
1166 | MODULE = CBOR::XS PACKAGE = CBOR::XS |
812 | |
1167 | |
813 | BOOT: |
1168 | BOOT: |
814 | { |
1169 | { |
815 | cbor_stash = gv_stashpv ("CBOR::XS" , 1); |
1170 | 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); |
1171 | cbor_tagged_stash = gv_stashpv ("CBOR::XS::Tagged" , 1); |
818 | |
1172 | |
819 | cbor_true = get_bool ("CBOR::XS::true"); |
1173 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
820 | cbor_false = get_bool ("CBOR::XS::false"); |
1174 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
|
|
1175 | |
|
|
1176 | types_true = get_bool ("Types::Serialiser::true" ); |
|
|
1177 | types_false = get_bool ("Types::Serialiser::false"); |
|
|
1178 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
1179 | |
|
|
1180 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
|
|
1181 | |
|
|
1182 | sv_cbor = newSVpv ("CBOR", 0); |
|
|
1183 | SvREADONLY_on (sv_cbor); |
821 | } |
1184 | } |
822 | |
1185 | |
823 | PROTOTYPES: DISABLE |
1186 | PROTOTYPES: DISABLE |
824 | |
1187 | |
825 | void CLONE (...) |
1188 | void CLONE (...) |
826 | CODE: |
1189 | CODE: |
827 | cbor_stash = 0; |
1190 | cbor_stash = 0; |
828 | cbor_boolean_stash = 0; |
|
|
829 | cbor_tagged_stash = 0; |
1191 | cbor_tagged_stash = 0; |
|
|
1192 | types_error_stash = 0; |
|
|
1193 | types_boolean_stash = 0; |
830 | |
1194 | |
831 | void new (char *klass) |
1195 | void new (char *klass) |
832 | PPCODE: |
1196 | PPCODE: |
833 | { |
1197 | { |
834 | SV *pv = NEWSV (0, sizeof (CBOR)); |
1198 | SV *pv = NEWSV (0, sizeof (CBOR)); |
… | |
… | |
842 | |
1206 | |
843 | void shrink (CBOR *self, int enable = 1) |
1207 | void shrink (CBOR *self, int enable = 1) |
844 | ALIAS: |
1208 | ALIAS: |
845 | shrink = F_SHRINK |
1209 | shrink = F_SHRINK |
846 | allow_unknown = F_ALLOW_UNKNOWN |
1210 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1211 | allow_sharing = F_ALLOW_SHARING |
|
|
1212 | pack_strings = F_PACK_STRINGS |
847 | PPCODE: |
1213 | PPCODE: |
848 | { |
1214 | { |
849 | if (enable) |
1215 | if (enable) |
850 | self->flags |= ix; |
1216 | self->flags |= ix; |
851 | else |
1217 | else |
… | |
… | |
856 | |
1222 | |
857 | void get_shrink (CBOR *self) |
1223 | void get_shrink (CBOR *self) |
858 | ALIAS: |
1224 | ALIAS: |
859 | get_shrink = F_SHRINK |
1225 | get_shrink = F_SHRINK |
860 | get_allow_unknown = F_ALLOW_UNKNOWN |
1226 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1227 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1228 | get_pack_strings = F_PACK_STRINGS |
861 | PPCODE: |
1229 | PPCODE: |
862 | XPUSHs (boolSV (self->flags & ix)); |
1230 | XPUSHs (boolSV (self->flags & ix)); |
863 | |
1231 | |
864 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1232 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
865 | PPCODE: |
1233 | PPCODE: |
… | |
… | |
881 | CODE: |
1249 | CODE: |
882 | RETVAL = self->max_size; |
1250 | RETVAL = self->max_size; |
883 | OUTPUT: |
1251 | OUTPUT: |
884 | RETVAL |
1252 | RETVAL |
885 | |
1253 | |
886 | #if 0 //TODO |
1254 | void filter (CBOR *self, SV *filter = 0) |
887 | |
|
|
888 | void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) |
|
|
889 | PPCODE: |
1255 | PPCODE: |
890 | { |
|
|
891 | SvREFCNT_dec (self->cb_object); |
1256 | SvREFCNT_dec (self->filter); |
892 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1257 | self->filter = filter ? newSVsv (filter) : filter; |
893 | |
|
|
894 | XPUSHs (ST (0)); |
1258 | XPUSHs (ST (0)); |
895 | } |
|
|
896 | |
1259 | |
897 | void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) |
1260 | SV *get_filter (CBOR *self) |
898 | PPCODE: |
1261 | CODE: |
899 | { |
1262 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
900 | if (!self->cb_sk_object) |
1263 | OUTPUT: |
901 | self->cb_sk_object = newHV (); |
1264 | 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 | |
1265 | |
921 | void encode (CBOR *self, SV *scalar) |
1266 | void encode (CBOR *self, SV *scalar) |
922 | PPCODE: |
1267 | PPCODE: |
923 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
1268 | PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; |
924 | XPUSHs (scalar); |
1269 | XPUSHs (scalar); |
… | |
… | |
937 | EXTEND (SP, 2); |
1282 | EXTEND (SP, 2); |
938 | PUSHs (sv); |
1283 | PUSHs (sv); |
939 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1284 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
940 | } |
1285 | } |
941 | |
1286 | |
942 | #if 0 |
|
|
943 | |
|
|
944 | void DESTROY (CBOR *self) |
1287 | void DESTROY (CBOR *self) |
945 | CODE: |
1288 | PPCODE: |
946 | SvREFCNT_dec (self->cb_sk_object); |
1289 | cbor_free (self); |
947 | SvREFCNT_dec (self->cb_object); |
|
|
948 | |
|
|
949 | #endif |
|
|
950 | |
1290 | |
951 | PROTOTYPES: ENABLE |
1291 | PROTOTYPES: ENABLE |
952 | |
1292 | |
953 | void encode_cbor (SV *scalar) |
1293 | void encode_cbor (SV *scalar) |
|
|
1294 | ALIAS: |
|
|
1295 | encode_cbor = 0 |
|
|
1296 | encode_cbor_sharing = F_ALLOW_SHARING |
954 | PPCODE: |
1297 | PPCODE: |
955 | { |
1298 | { |
956 | CBOR cbor; |
1299 | CBOR cbor; |
957 | cbor_init (&cbor); |
1300 | cbor_init (&cbor); |
|
|
1301 | cbor.flags |= ix; |
958 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1302 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
959 | XPUSHs (scalar); |
1303 | XPUSHs (scalar); |
960 | } |
1304 | } |
961 | |
1305 | |
962 | void decode_cbor (SV *cborstr) |
1306 | void decode_cbor (SV *cborstr) |