--- CBOR-XS/XS.xs 2013/10/28 19:06:45 1.12 +++ CBOR-XS/XS.xs 2013/11/22 05:54:07 1.25 @@ -11,12 +11,28 @@ #include "ecb.h" +// compatibility with perl <5.18 +#ifndef HvNAMELEN_get +# define HvNAMELEN_get(hv) strlen (HvNAME (hv)) +#endif +#ifndef HvNAMELEN +# define HvNAMELEN(hv) HvNAMELEN_get (hv) +#endif +#ifndef HvNAMEUTF8 +# define HvNAMEUTF8(hv) 0 +#endif + // known tags enum cbor_tag { // inofficial extensions (pending iana registration) - CBOR_TAG_PERL_OBJECT = 256, - CBOR_TAG_GENERIC_OBJECT = 257, + CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object + CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object + CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing + CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing + CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref + CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref + CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection // rfc7049 CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 @@ -40,8 +56,10 @@ CBOR_TAG_MAGIC = 55799 // self-describe cbor }; -#define F_SHRINK 0x00000200UL -#define F_ALLOW_UNKNOWN 0x00002000UL +#define F_SHRINK 0x00000001UL +#define F_ALLOW_UNKNOWN 0x00000002UL +#define F_ALLOW_SHARING 0x00000004UL //TODO +#define F_ALLOW_STRINGREF 0x00000008UL //TODO #define INIT_SIZE 32 // initial scalar size to be allocated @@ -107,10 +125,20 @@ } } -///////////////////////////////////////////////////////////////////////////// -// fp hell - -//TODO +// minimum length of a string to be registered for stringref +ecb_inline int +minimum_string_length (UV idx) +{ + return idx > 23 + ? idx > 0xffU + ? idx > 0xffffU + ? idx > 0xffffffffU + ? 7 + : 6 + : 5 + : 4 + : 3; +} ///////////////////////////////////////////////////////////////////////////// // encoder @@ -123,6 +151,10 @@ SV *sv; // result scalar CBOR cbor; U32 depth; // recursion level + HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) + UV stringref_idx; + HV *shareable; // ptr => index, or 0 + UV shareable_idx; } enc_t; ecb_inline void @@ -184,9 +216,34 @@ } } +ecb_inline void +encode_tag (enc_t *enc, UV tag) +{ + encode_uint (enc, 0xc0, tag); +} + static void encode_str (enc_t *enc, int utf8, char *str, STRLEN len) { + if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) + { + SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); + + if (SvOK (*svp)) + { + // already registered, use stringref + encode_tag (enc, CBOR_TAG_STRINGREF); + encode_uint (enc, 0x00, SvUV (*svp)); + return; + } + else if (len >= minimum_string_length (enc->stringref_idx)) + { + // register only + sv_setuv (*svp, enc->stringref_idx); + ++enc->stringref_idx; + } + } + encode_uint (enc, utf8 ? 0x60 : 0x40, len); need (enc, len); memcpy (enc->cur, str, len); @@ -216,6 +273,11 @@ --enc->depth; } +ecb_inline void +encode_he (enc_t *enc, HE *he) +{ +} + static void encode_hv (enc_t *enc, HV *hv) { @@ -254,10 +316,31 @@ static void encode_rv (enc_t *enc, SV *sv) { - svtype svt; - SvGETMAGIC (sv); - svt = SvTYPE (sv); + + if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) + && ecb_expect_false (SvREFCNT (sv) > 1)) + { + if (!enc->shareable) + enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); + + SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); + + if (SvOK (*svp)) + { + encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); + encode_uint (enc, 0x00, SvUV (*svp)); + return; + } + else + { + sv_setuv (*svp, enc->shareable_idx); + ++enc->shareable_idx; + encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); + } + } + + svtype svt = SvTYPE (sv); if (ecb_expect_false (SvOBJECT (sv))) { @@ -327,7 +410,7 @@ if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); - encode_uint (enc, 0xc0, CBOR_TAG_PERL_OBJECT); + encode_tag (enc, CBOR_TAG_PERL_OBJECT); encode_uint (enc, 0x80, count + 1); encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); @@ -346,26 +429,11 @@ encode_hv (enc, (HV *)sv); else if (svt == SVt_PVAV) encode_av (enc, (AV *)sv); - else if (svt < SVt_PVAV) + else { - STRLEN len = 0; - char *pv = svt ? SvPV (sv, len) : 0; - - if (len == 1 && *pv == '1') - encode_ch (enc, 0xe0 | 21); - else if (len == 1 && *pv == '0') - encode_ch (enc, 0xe0 | 20); - else if (enc->cbor.flags & F_ALLOW_UNKNOWN) - encode_ch (enc, 0xe0 | 23); - else - croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", - SvPV_nolen (sv_2mortal (newRV_inc (sv)))); + encode_tag (enc, CBOR_TAG_INDIRECTION); + encode_sv (enc, sv); } - else if (enc->cbor.flags & F_ALLOW_UNKNOWN) - encode_ch (enc, 0xe0 | 23); - else - croak ("encountered %s, but CBOR can only represent references to arrays or hashes", - SvPV_nolen (sv_2mortal (newRV_inc (sv)))); } static void @@ -440,15 +508,22 @@ static SV * encode_cbor (SV *scalar, CBOR *cbor) { - enc_t enc; + enc_t enc = { }; enc.cbor = *cbor; enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); enc.cur = SvPVX (enc.sv); enc.end = SvEND (enc.sv); - enc.depth = 0; SvPOK_only (enc.sv); + + if (cbor->flags & F_ALLOW_STRINGREF) + { + encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); + enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); + enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); + } + encode_sv (&enc, scalar); SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); @@ -472,6 +547,8 @@ CBOR cbor; U32 depth; // recursion depth U32 maxdepth; // recursion depth limit + AV *shareable; + AV *stringref; } dec_t; #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE @@ -575,6 +652,49 @@ return &PL_sv_undef; } +static void +decode_he (dec_t *dec, HV *hv) +{ + // for speed reasons, we specialcase single-string + // byte or utf-8 strings as keys, but only when !stringref + + if (ecb_expect_true (!dec->stringref)) + if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) + { + I32 len = decode_uint (dec); + char *key = (char *)dec->cur; + + dec->cur += len; + + if (ecb_expect_false (dec->stringref)) + av_push (dec->stringref, newSVpvn (key, len)); + + hv_store (hv, key, len, decode_sv (dec), 0); + + return; + } + else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) + { + I32 len = decode_uint (dec); + char *key = (char *)dec->cur; + + dec->cur += len; + + if (ecb_expect_false (dec->stringref)) + av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); + + hv_store (hv, key, -len, decode_sv (dec), 0); + + return; + } + + SV *k = decode_sv (dec); + SV *v = decode_sv (dec); + + hv_store_ent (hv, k, v, 0); + SvREFCNT_dec (k); +} + static SV * decode_hv (dec_t *dec) { @@ -596,25 +716,15 @@ break; } - SV *k = decode_sv (dec); - SV *v = decode_sv (dec); - - hv_store_ent (hv, k, v, 0); - SvREFCNT_dec (k); + decode_he (dec, hv); } } else { - int len = decode_uint (dec); - - while (len--) - { - SV *k = decode_sv (dec); - SV *v = decode_sv (dec); + int pairs = decode_uint (dec); - hv_store_ent (hv, k, v, 0); - SvREFCNT_dec (k); - } + while (pairs--) + decode_he (dec, hv); } DEC_DEC_DEPTH; @@ -658,6 +768,10 @@ WANT (len); sv = newSVpvn (dec->cur, len); dec->cur += len; + + if (ecb_expect_false (dec->stringref) + && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) + av_push (dec->stringref, SvREFCNT_inc_NN (sv)); } if (utf8) @@ -673,66 +787,146 @@ static SV * decode_tagged (dec_t *dec) { + SV *sv = 0; UV tag = decode_uint (dec); - SV *sv = decode_sv (dec); - if (tag == CBOR_TAG_MAGIC) - return sv; - else if (tag == CBOR_TAG_PERL_OBJECT) + WANT (1); + + switch (tag) { - if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) - ERR ("corrupted CBOR data (non-array perl object)"); + case CBOR_TAG_MAGIC: + sv = decode_sv (dec); + break; + + case CBOR_TAG_INDIRECTION: + sv = newRV_noinc (decode_sv (dec)); + break; + + case CBOR_TAG_STRINGREF_NAMESPACE: + { + ENTER; SAVETMPS; + + SAVESPTR (dec->stringref); + dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); + + sv = decode_sv (dec); + + FREETMPS; LEAVE; + } + break; - AV *av = (AV *)SvRV (sv); - int len = av_len (av) + 1; - HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); + case CBOR_TAG_STRINGREF: + { + if ((*dec->cur >> 5) != 0) + ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); - if (!stash) - ERR ("cannot decode perl-object (package does not exist)"); + UV idx = decode_uint (dec); - GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); - - if (!method) - ERR ("cannot decode perl-object (package does not have a THAW method)"); - - dSP; + if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) + ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); - ENTER; SAVETMPS; PUSHMARK (SP); - EXTEND (SP, len + 1); - // we re-bless the reference to get overload and other niceties right - PUSHs (*av_fetch (av, 0, 1)); - PUSHs (sv_cbor); + sv = newSVsv (AvARRAY (dec->stringref)[idx]); + } + break; - int i; + case CBOR_TAG_VALUE_SHAREABLE: + { + if (ecb_expect_false (!dec->shareable)) + dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); - for (i = 1; i < len; ++i) - PUSHs (*av_fetch (av, i, 1)); + sv = newSV (0); + av_push (dec->shareable, SvREFCNT_inc_NN (sv)); - PUTBACK; - call_sv ((SV *)GvCV (method), G_SCALAR); - SPAGAIN; + SV *osv = decode_sv (dec); + sv_setsv (sv, osv); + SvREFCNT_dec_NN (osv); + } + break; - sv = SvREFCNT_inc (POPs); + case CBOR_TAG_VALUE_SHAREDREF: + { + if ((*dec->cur >> 5) != 0) + ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); - PUTBACK; + UV idx = decode_uint (dec); - FREETMPS; LEAVE; + if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) + ERR ("corrupted CBOR data (sharedref index out of bounds)"); - return sv; - } - else - { - AV *av = newAV (); - av_push (av, newSVuv (tag)); - av_push (av, sv); + sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); + } + break; - HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash - ? cbor_tagged_stash - : gv_stashpv ("CBOR::XS::Tagged" , 1); + case CBOR_TAG_PERL_OBJECT: + { + sv = decode_sv (dec); + + if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) + ERR ("corrupted CBOR data (non-array perl object)"); + + AV *av = (AV *)SvRV (sv); + int len = av_len (av) + 1; + HV *stash = gv_stashsv (*av_fetch (av, 0, 1), 0); + + if (!stash) + ERR ("cannot decode perl-object (package does not exist)"); + + GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); + + if (!method) + ERR ("cannot decode perl-object (package does not have a THAW method)"); + + dSP; + + ENTER; SAVETMPS; PUSHMARK (SP); + EXTEND (SP, len + 1); + // we re-bless the reference to get overload and other niceties right + PUSHs (*av_fetch (av, 0, 1)); + PUSHs (sv_cbor); + + int i; + + for (i = 1; i < len; ++i) + PUSHs (*av_fetch (av, i, 1)); + + PUTBACK; + call_sv ((SV *)GvCV (method), G_SCALAR | G_EVAL); + SPAGAIN; + + if (SvTRUE (ERRSV)) + { + FREETMPS; LEAVE; + ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); + } + + SvREFCNT_dec (sv); + sv = SvREFCNT_inc (POPs); + + PUTBACK; + + FREETMPS; LEAVE; + } + break; + + default: + { + sv = decode_sv (dec); + + AV *av = newAV (); + av_push (av, newSVuv (tag)); + av_push (av, sv); - return sv_bless (newRV_noinc ((SV *)av), tagged_stash); + HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash + ? cbor_tagged_stash + : gv_stashpv ("CBOR::XS::Tagged" , 1); + + sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); + } + break; } + return sv; + fail: SvREFCNT_dec (sv); return &PL_sv_undef; @@ -832,49 +1026,18 @@ static SV * decode_cbor (SV *string, CBOR *cbor, char **offset_return) { - dec_t dec; + dec_t dec = { }; SV *sv; + STRLEN len; + char *data = SvPVbyte (string, len); - /* work around bugs in 5.10 where manipulating magic values - * makes perl ignore the magic in subsequent accesses. - * also make a copy of non-PV values, to get them into a clean - * state (SvPV should do that, but it's buggy, see below). - */ - /*SvGETMAGIC (string);*/ - if (SvMAGICAL (string) || !SvPOK (string)) - string = sv_2mortal (newSVsv (string)); - - SvUPGRADE (string, SVt_PV); - - /* work around a bug in perl 5.10, which causes SvCUR to fail an - * assertion with -DDEBUGGING, although SvCUR is documented to - * return the xpv_cur field which certainly exists after upgrading. - * according to nicholas clark, calling SvPOK fixes this. - * But it doesn't fix it, so try another workaround, call SvPV_nolen - * and hope for the best. - * Damnit, SvPV_nolen still trips over yet another assertion. This - * assertion business is seriously broken, try yet another workaround - * for the broken -DDEBUGGING. - */ - { -#ifdef DEBUGGING - STRLEN offset = SvOK (string) ? sv_len (string) : 0; -#else - STRLEN offset = SvCUR (string); -#endif - - if (offset > cbor->max_size && cbor->max_size) - croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", - (unsigned long)SvCUR (string), (unsigned long)cbor->max_size); - } - - sv_utf8_downgrade (string, 0); + if (len > cbor->max_size && cbor->max_size) + croak ("attempted decode of CBOR text of %lu bytes size, but max_size is set to %lu", + (unsigned long)len, (unsigned long)cbor->max_size); dec.cbor = *cbor; - dec.cur = (U8 *)SvPVX (string); - dec.end = (U8 *)SvEND (string); - dec.err = 0; - dec.depth = 0; + dec.cur = (U8 *)data; + dec.end = (U8 *)data + len; sv = decode_sv (&dec); @@ -888,7 +1051,7 @@ if (dec.err) { SvREFCNT_dec (sv); - croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)SvPVX (string), (int)(uint8_t)*dec.cur); + croak ("%s, at offset %d (octet 0x%02x)", dec.err, dec.cur - (U8 *)data, (int)(uint8_t)*dec.cur); } sv = sv_2mortal (sv); @@ -942,6 +1105,8 @@ ALIAS: shrink = F_SHRINK allow_unknown = F_ALLOW_UNKNOWN + allow_sharing = F_ALLOW_SHARING + allow_stringref = F_ALLOW_STRINGREF PPCODE: { if (enable) @@ -956,6 +1121,8 @@ ALIAS: get_shrink = F_SHRINK get_allow_unknown = F_ALLOW_UNKNOWN + get_allow_sharing = F_ALLOW_SHARING + get_allow_stringref = F_ALLOW_STRINGREF PPCODE: XPUSHs (boolSV (self->flags & ix)); @@ -981,41 +1148,6 @@ OUTPUT: RETVAL -#if 0 //TODO - -void filter_cbor_object (CBOR *self, SV *cb = &PL_sv_undef) - PPCODE: -{ - SvREFCNT_dec (self->cb_object); - self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; - - XPUSHs (ST (0)); -} - -void filter_cbor_single_key_object (CBOR *self, SV *key, SV *cb = &PL_sv_undef) - PPCODE: -{ - if (!self->cb_sk_object) - self->cb_sk_object = newHV (); - - if (SvOK (cb)) - hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); - else - { - hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); - - if (!HvKEYS (self->cb_sk_object)) - { - SvREFCNT_dec (self->cb_sk_object); - self->cb_sk_object = 0; - } - } - - XPUSHs (ST (0)); -} - -#endif - void encode (CBOR *self, SV *scalar) PPCODE: PUTBACK; scalar = encode_cbor (scalar, self); SPAGAIN; @@ -1037,15 +1169,6 @@ PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); } -#if 0 - -void DESTROY (CBOR *self) - CODE: - SvREFCNT_dec (self->cb_sk_object); - SvREFCNT_dec (self->cb_object); - -#endif - PROTOTYPES: ENABLE void encode_cbor (SV *scalar)