ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.17 by root, Tue Oct 29 22:04:52 2013 UTC vs.
Revision 1.36 by root, Sat Nov 30 17:37:45 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines