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.69 by root, Mon Nov 30 20:38:25 2020 UTC vs.
Revision 1.70 by root, Fri Dec 4 02:57:14 2020 UTC

109 AS_BYTES = 2, 109 AS_BYTES = 2,
110 AS_TEXT = 3, 110 AS_TEXT = 3,
111 AS_FLOAT16 = 4, 111 AS_FLOAT16 = 4,
112 AS_FLOAT32 = 5, 112 AS_FLOAT32 = 5,
113 AS_FLOAT64 = 6, 113 AS_FLOAT64 = 6,
114 AS_MAP = 7,
114 // possibly future enhancements: (generic) float, (generic) string 115 // possibly future enhancements: (generic) float, (generic) string
115}; 116};
116 117
117#define F_SHRINK 0x00000001UL 118#define F_SHRINK 0x00000001UL
118#define F_ALLOW_UNKNOWN 0x00000002UL 119#define F_ALLOW_UNKNOWN 0x00000002UL
238 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 239 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
239 enc->cur = SvPVX (enc->sv) + cur; 240 enc->cur = SvPVX (enc->sv) + cur;
240 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 241 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
241 } 242 }
242} 243}
244
245static void encode_sv (enc_t *enc, SV *sv);
243 246
244ecb_inline void 247ecb_inline void
245encode_ch (enc_t *enc, char ch) 248encode_ch (enc_t *enc, char ch)
246{ 249{
247 need (enc, 1); 250 need (enc, 1);
423encode_bool (enc_t *enc, int istrue) 426encode_bool (enc_t *enc, int istrue)
424{ 427{
425 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 428 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
426} 429}
427 430
431// encodes an arrayref containing key-value pairs as CBOR map
432ecb_inline void
433encode_array_as_map (enc_t *enc, SV *sv)
434{
435 if (enc->depth >= enc->cbor.max_depth)
436 croak (ERR_NESTING_EXCEEDED);
437
438 ++enc->depth;
439
440 // as_map does error checking for us, but we re-check in case
441 // things have changed.
442
443 if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV)
444 croak ("CBOR::XS::as_map requires an array reference (did you change the array after calling as_map?)");
445
446 AV *av = (AV *)SvRV (sv);
447 int i, len = av_len (av);
448
449 if (!(len & 1))
450 croak ("CBOR::XS::as_map requires an even number of elements (did you change the array after calling as_map?)");
451
452 encode_uint (enc, MAJOR_MAP, (len + 1) >> 1);
453
454 for (i = 0; i <= len; ++i)
455 {
456 SV **svp = av_fetch (av, i, 0);
457 encode_sv (enc, svp ? *svp : &PL_sv_undef);
458 }
459
460 --enc->depth;
461}
462
428ecb_inline void 463ecb_inline void
429encode_forced (enc_t *enc, UV type, SV *sv) 464encode_forced (enc_t *enc, UV type, SV *sv)
430{ 465{
431 switch (type) 466 switch (type)
432 { 467 {
461 496
462 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; 497 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
463 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; 498 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
464 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; 499 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
465 500
501 case AS_MAP: encode_array_as_map (enc, sv); break;
502
466 default: 503 default:
467 croak ("encountered malformed CBOR::XS::Tagged object"); 504 croak ("encountered malformed CBOR::XS::Tagged object");
468 } 505 }
469} 506}
470
471static void encode_sv (enc_t *enc, SV *sv);
472 507
473static void 508static void
474encode_av (enc_t *enc, AV *av) 509encode_av (enc_t *enc, AV *av)
475{ 510{
476 int i, len = av_len (av); 511 int i, len = av_len (av);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines