| 1 |
|
| /* Must be defined before including Perl header files or we slow down by 2x! */ |
| 2 |
|
| #define PERL_NO_GET_CONTEXT |
| 3 |
|
| |
| 4 |
|
| #define NEED_newSV_type |
| 5 |
|
| #include "EXTERN.h" |
| 6 |
|
| #include "perl.h" |
| 7 |
|
| #include "XSUB.h" |
| 8 |
|
| |
| 9 |
|
| #include "ppport.h" |
| 10 |
|
| |
| 11 |
|
| #include "srl_common.h" |
| 12 |
|
| #include "srl_decoder.h" |
| 13 |
|
| #include "srl_protocol.h" |
| 14 |
|
| #include "srl_siphash.h" |
| 15 |
|
| |
| 16 |
|
| /* new() marks its handle; a blessed integer from elsewhere is not one. */ |
| 17 |
|
| static MGVTBL srl_handle_vtbl; |
| 18 |
|
| static int |
| 19 |
293 |
| srl_is_handle(SV *sv) |
| 20 |
|
| { |
| 21 |
|
| MAGIC *mg; |
| 22 |
293 |
▶ | if (SvTYPE(sv) < SVt_PVMG) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(sv) < SVt_PVMG) |
0 |
293 |
293 |
|
| 23 |
0 |
| return 0; |
| 24 |
293 |
▶ | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) |
293 |
0 |
293 |
|
| 25 |
293 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl) |
293 |
0 |
293 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl) |
293 |
0 |
293 |
|
| 26 |
293 |
| return 1; |
| 27 |
0 |
| return 0; |
| 28 |
|
| } |
| 29 |
|
| #define SRL_IS_HANDLE(sv) srl_is_handle(sv) |
| 30 |
|
| |
| 31 |
|
| #ifndef GvCV_set |
| 32 |
|
| # define GvCV_set(gv, cv) (GvCV(gv) = (cv)) |
| 33 |
|
| #endif |
| 34 |
|
| |
| 35 |
|
| #ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE |
| 36 |
|
| #define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params) |
| 37 |
|
| |
| 38 |
|
| /* prototype to pass -Wmissing-prototypes */ |
| 39 |
|
| STATIC void |
| 40 |
|
| S_croak_xs_usage(pTHX_ const CV *const cv, const char *const params); |
| 41 |
|
| |
| 42 |
|
| STATIC void |
| 43 |
|
| S_croak_xs_usage(pTHX_ const CV *const cv, const char *const params) |
| 44 |
|
| { |
| 45 |
|
| const GV *const gv = CvGV(cv); |
| 46 |
|
| |
| 47 |
|
| PERL_ARGS_ASSERT_CROAK_XS_USAGE; |
| 48 |
|
| |
| 49 |
|
| if (gv) { |
| 50 |
|
| const char *const gvname = GvNAME(gv); |
| 51 |
|
| const HV *const stash = GvSTASH(gv); |
| 52 |
|
| const char *const hvname = stash ? HvNAME(stash) : NULL; |
| 53 |
|
| |
| 54 |
|
| if (hvname) |
| 55 |
|
| Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params); |
| 56 |
|
| else |
| 57 |
|
| Perl_croak_nocontext("Usage: %s(%s)", gvname, params); |
| 58 |
|
| } else { |
| 59 |
|
| /* Pants. I don't think that it should be possible to get here. */ |
| 60 |
|
| Perl_croak_nocontext("Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params); |
| 61 |
|
| } |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| #ifdef PERL_IMPLICIT_CONTEXT |
| 65 |
|
| #define croak_xs_usage(a,b) S_croak_xs_usage(aTHX_ a,b) |
| 66 |
|
| #else |
| 67 |
|
| #define croak_xs_usage S_croak_xs_usage |
| 68 |
|
| #endif |
| 69 |
|
| |
| 70 |
|
| #endif |
| 71 |
|
| |
| 72 |
|
| |
| 73 |
|
| #if defined(cv_set_call_checker) && defined(XopENTRY_set) |
| 74 |
|
| # define USE_CUSTOM_OPS 1 |
| 75 |
|
| #else |
| 76 |
|
| # define USE_CUSTOM_OPS 0 |
| 77 |
|
| #endif |
| 78 |
|
| |
| 79 |
|
| #define OPOPT_DO_BODY (1<<0) |
| 80 |
|
| #define OPOPT_DO_HEADER (1<<1) |
| 81 |
|
| #define OPOPT_OFFSET (1<<2) |
| 82 |
|
| #define OPOPT_OUTARG_BODY (1<<3) |
| 83 |
|
| #define OPOPT_OUTARG_HEADER (1<<4) |
| 84 |
|
| #define OPOPT_LOOKS_LIKE (1<<5) |
| 85 |
|
| #define OPOPT_CHECKSUM (1<<6) |
| 86 |
|
| #define OPOPT_VALIDATE_CHECKSUM (1<<7) |
| 87 |
|
| |
| 88 |
|
| static void |
| 89 |
0 |
| THX_pp1_checksum(pTHX_ U8 opopt, U8 arity) |
| 90 |
|
| { |
| 91 |
0 |
| const int validate = (opopt & OPOPT_VALIDATE_CHECKSUM) != 0; |
| 92 |
0 |
| const int want_result = GIMME_V != G_VOID; |
| 93 |
0 |
| SV *src, *key = NULL, *offset_sv = NULL, *result; |
| 94 |
|
| UV offset; |
| 95 |
0 |
| dSP; |
| 96 |
0 |
▶ | if (arity == 3 || (!validate && arity == 2)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (arity == 3 || (!validate && arity == 2)) |
0 |
0 |
0 |
| if (arity == 3 || (!validate && arity == 2)) |
0 |
0 |
0 |
| if (arity == 3 || (!validate && arity == 2)) |
0 |
0 |
0 |
|
| 97 |
0 |
| offset_sv = POPs; |
| 98 |
0 |
▶ | if (validate && arity >= 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (validate && arity >= 2) |
0 |
0 |
0 |
| if (validate && arity >= 2) |
0 |
0 |
0 |
|
| 99 |
0 |
| key = POPs; |
| 100 |
0 |
| src = POPs; |
| 101 |
0 |
| PUTBACK; |
| 102 |
0 |
▶ | offset = offset_sv ? SvUV(offset_sv) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| offset = offset_sv ? SvUV(offset_sv) : 0; |
0 |
0 |
0 |
|
| 103 |
0 |
| result = validate |
| 104 |
0 |
▶ | ? boolSV(srl_validate_sereal_checksum(aTHX_ src, key, offset)) |
Branch: 1 branch
| Branch | True | False | Total |
| ? boolSV(srl_validate_sereal_checksum(aTHX_ src, key, offset)) |
0 |
0 |
0 |
|
| 105 |
0 |
▶ | : sv_2mortal(srl_get_sereal_checksum(aTHX_ src, offset)); |
Branch: 1 branch
| Branch | True | False | Total |
| : sv_2mortal(srl_get_sereal_checksum(aTHX_ src, offset)); |
0 |
0 |
0 |
|
| 106 |
0 |
| SPAGAIN; |
| 107 |
0 |
▶ | if (want_result) |
Branch: 1 branch
| Branch | True | False | Total |
| if (want_result) |
0 |
0 |
0 |
|
| 108 |
0 |
▶ | XPUSHs(result); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(result); |
0 |
0 |
0 |
|
| 109 |
0 |
| PUTBACK; |
| 110 |
0 |
| } |
| 111 |
|
| |
| 112 |
|
| #define pp1_sereal_decode(opopt) THX_pp1_sereal_decode(aTHX_ opopt) |
| 113 |
|
| static void |
| 114 |
249 |
| THX_pp1_sereal_decode(pTHX_ U8 opopt) |
| 115 |
|
| { |
| 116 |
249 |
| bool need_retvalue = GIMME_V != G_VOID; |
| 117 |
|
| SV *decoder_ref_sv, *decoder_sv, *src_sv; |
| 118 |
|
| UV offset; |
| 119 |
|
| SV *body_into, *header_into; |
| 120 |
|
| srl_decoder_t *decoder; |
| 121 |
|
| char *stash_name; |
| 122 |
249 |
| dSP; |
| 123 |
|
| |
| 124 |
498 |
| header_into = expect_false(opopt & OPOPT_OUTARG_HEADER) |
| 125 |
10 |
| ? POPs |
| 126 |
249 |
▶ | : expect_false(opopt & OPOPT_DO_HEADER) ? sv_newmortal() : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| : expect_false(opopt & OPOPT_DO_HEADER) ? sv_newmortal() : NULL; |
10 |
239 |
249 |
| : expect_false(opopt & OPOPT_DO_HEADER) ? sv_newmortal() : NULL; |
0 |
239 |
239 |
|
| 127 |
498 |
| body_into = expect_false(opopt & OPOPT_OUTARG_BODY) |
| 128 |
237 |
| ? POPs |
| 129 |
249 |
▶ | : expect_true(opopt & OPOPT_DO_BODY) ? sv_newmortal() : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| : expect_true(opopt & OPOPT_DO_BODY) ? sv_newmortal() : NULL; |
237 |
12 |
249 |
| : expect_true(opopt & OPOPT_DO_BODY) ? sv_newmortal() : NULL; |
8 |
4 |
12 |
|
| 130 |
|
| |
| 131 |
249 |
▶ | offset = expect_false(opopt & OPOPT_OFFSET) ? SvUVx(POPs) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| offset = expect_false(opopt & OPOPT_OFFSET) ? SvUVx(POPs) : 0; |
0 |
249 |
249 |
|
| 132 |
249 |
| src_sv = POPs; |
| 133 |
249 |
| decoder_ref_sv = POPs; |
| 134 |
249 |
| PUTBACK; |
| 135 |
|
| |
| 136 |
249 |
▶ | if (!expect_true( |
Branch: 14 branches
| Branch | True | False | Total |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
0 |
249 |
249 |
| if (!expect_true( |
0 |
0 |
0 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
0 |
249 |
249 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
249 |
0 |
249 |
| if (!expect_true( |
0 |
249 |
249 |
|
| 137 |
|
| decoder_ref_sv && |
| 138 |
|
| SvROK(decoder_ref_sv) && |
| 139 |
|
| (decoder_sv = SvRV(decoder_ref_sv)) && |
| 140 |
|
| SvOBJECT(decoder_sv) && |
| 141 |
|
| (stash_name = HvNAME(SvSTASH(decoder_sv))) && |
| 142 |
|
| !strcmp(stash_name, "Sereal::Decoder") && |
| 143 |
|
| SRL_IS_HANDLE(decoder_sv) |
| 144 |
|
| )) |
| 145 |
|
| { |
| 146 |
0 |
| croak("handle is not a Sereal::Decoder handle"); |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
249 |
| decoder = (srl_decoder_t *)SvIV(decoder_sv); |
| 150 |
|
| /* A callback may clear the variable holding this handle. Retain its |
| 151 |
|
| * referent, which owns the C decoder, until decode cleanup has run. */ |
| 152 |
249 |
| SAVEFREESV(SvREFCNT_inc(decoder_sv)); |
| 153 |
249 |
▶ | if (expect_true(opopt & OPOPT_DO_BODY)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_true(opopt & OPOPT_DO_BODY)) { |
245 |
4 |
249 |
|
| 154 |
245 |
▶ | if (opopt & OPOPT_DO_HEADER) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opopt & OPOPT_DO_HEADER) { |
6 |
239 |
245 |
|
| 155 |
6 |
| srl_decode_all_into(aTHX_ decoder, src_sv, header_into, |
| 156 |
|
| body_into, offset); |
| 157 |
|
| } else { |
| 158 |
239 |
| srl_decode_into(aTHX_ decoder, src_sv, body_into, offset); |
| 159 |
|
| } |
| 160 |
|
| } else { |
| 161 |
4 |
| srl_decode_header_into(aTHX_ decoder, src_sv, header_into, offset); |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
247 |
▶ | if (expect_true(need_retvalue)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_true(need_retvalue)) { |
6 |
241 |
247 |
|
| 165 |
|
| SV *retvalue; |
| 166 |
6 |
▶ | if (expect_true(opopt & OPOPT_DO_BODY)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (expect_true(opopt & OPOPT_DO_BODY)) { |
6 |
0 |
6 |
|
| 167 |
6 |
▶ | if (opopt & OPOPT_DO_HEADER) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opopt & OPOPT_DO_HEADER) { |
0 |
6 |
6 |
|
| 168 |
0 |
| AV *retav = newAV(); |
| 169 |
0 |
| retvalue = newRV_noinc((SV*)retav); |
| 170 |
0 |
| sv_2mortal(retvalue); |
| 171 |
0 |
| av_extend(retav, 1); |
| 172 |
0 |
| av_store(retav, 0, SvREFCNT_inc(header_into)); |
| 173 |
0 |
| av_store(retav, 1, SvREFCNT_inc(body_into)); |
| 174 |
|
| } else { |
| 175 |
6 |
| retvalue = body_into; |
| 176 |
|
| } |
| 177 |
|
| } else { |
| 178 |
0 |
| retvalue = header_into; |
| 179 |
|
| } |
| 180 |
6 |
| SPAGAIN; |
| 181 |
6 |
▶ | XPUSHs(retvalue); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(retvalue); |
0 |
6 |
6 |
|
| 182 |
6 |
| PUTBACK; |
| 183 |
|
| } |
| 184 |
247 |
| } |
| 185 |
|
| |
| 186 |
|
| #define pp1_looks_like_sereal() THX_pp1_looks_like_sereal(aTHX) |
| 187 |
|
| static void |
| 188 |
0 |
| THX_pp1_looks_like_sereal(pTHX) |
| 189 |
|
| { |
| 190 |
0 |
| dSP; |
| 191 |
0 |
| SV *data= TOPs; |
| 192 |
|
| /* Should this be SvPOK()? Or better yet, check if it's *really* a string pointer: SvPOKp(data). After all |
| 193 |
|
| the serialization format is a string and anything otherwise would not look sereal. */ |
| 194 |
0 |
▶ | if ( SvOK(data) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvOK(data) ) { |
0 |
0 |
0 |
|
| 195 |
|
| STRLEN len; |
| 196 |
0 |
| char *strdata= SvPV(data, len); |
| 197 |
0 |
| IV ret= srl_validate_header_version_pv_len(aTHX_ strdata, len); |
| 198 |
0 |
▶ | if ( ret < 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ret < 0 ) { |
0 |
0 |
0 |
|
| 199 |
0 |
| SETs(&PL_sv_no); |
| 200 |
|
| } else { |
| 201 |
0 |
| SETs(sv_2mortal(newSViv(ret & SRL_PROTOCOL_VERSION_MASK))); |
| 202 |
|
| } |
| 203 |
|
| } else { |
| 204 |
0 |
| SETs(&PL_sv_no); |
| 205 |
|
| } |
| 206 |
0 |
| } |
| 207 |
|
| |
| 208 |
|
| #if USE_CUSTOM_OPS |
| 209 |
|
| |
| 210 |
|
| /* Custom ops bypass the XSUB compatibility wrapper. Keep their arguments |
| 211 |
|
| * alive and give their results stack references on RC-stack Perls. */ |
| 212 |
|
| #ifdef PERL_RC_STACK |
| 213 |
|
| # define SRL_PP(name, nargs) XSPP_wrapped(name, nargs, 0) |
| 214 |
|
| #else |
| 215 |
|
| # define SRL_PP(name, nargs) static OP *name(pTHX) |
| 216 |
|
| #endif |
| 217 |
|
| |
| 218 |
3 |
| SRL_PP(THX_pp_sereal_decode, |
| 219 |
|
| 2 + !!(PL_op->op_private & OPOPT_OFFSET) |
| 220 |
|
| + !!(PL_op->op_private & OPOPT_OUTARG_BODY) |
| 221 |
|
| + !!(PL_op->op_private & OPOPT_OUTARG_HEADER)) |
| 222 |
|
| { |
| 223 |
3 |
| pp1_sereal_decode(PL_op->op_private); |
| 224 |
3 |
| return NORMAL; |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
0 |
| SRL_PP(THX_pp_looks_like_sereal, 1) |
| 228 |
|
| { |
| 229 |
0 |
| pp1_looks_like_sereal(); |
| 230 |
0 |
| return NORMAL; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
0 |
| SRL_PP(THX_pp_get_sereal_checksum, PL_op->op_private & 3) |
| 234 |
|
| { |
| 235 |
0 |
| THX_pp1_checksum(aTHX_ PL_op->op_private, PL_op->op_private & 3); |
| 236 |
0 |
| return NORMAL; |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
0 |
| SRL_PP(THX_pp_validate_sereal_checksum, PL_op->op_private & 3) |
| 240 |
|
| { |
| 241 |
0 |
| THX_pp1_checksum(aTHX_ PL_op->op_private, PL_op->op_private & 3); |
| 242 |
0 |
| return NORMAL; |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| static OP * |
| 246 |
45 |
| THX_ck_entersub_args_sereal_decoder(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) |
| 247 |
|
| { |
| 248 |
|
| |
| 249 |
|
| /* pull apart a standard entersub op tree */ |
| 250 |
|
| |
| 251 |
45 |
| CV *cv = (CV*)ckobj; |
| 252 |
45 |
| I32 cv_private = CvXSUBANY(cv).any_i32; |
| 253 |
45 |
| U8 opopt = cv_private & 0xff; |
| 254 |
45 |
| U8 min_arity = (cv_private >> 8) & 0xff; |
| 255 |
45 |
| U8 max_arity = (cv_private >> 16) & 0xff; |
| 256 |
|
| OP *pushop, *firstargop, *cvop, *lastargop, *argop, *newop; |
| 257 |
|
| int arity; |
| 258 |
|
| |
| 259 |
|
| /* Walk the OP structure under the "entersub" to validate that we |
| 260 |
|
| * can use the custom OP implementation. */ |
| 261 |
|
| |
| 262 |
45 |
| entersubop = ck_entersub_args_proto(entersubop, namegv, (SV*)cv); |
| 263 |
45 |
| pushop = cUNOPx(entersubop)->op_first; |
| 264 |
45 |
▶ | if ( ! OpHAS_SIBLING(pushop) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! OpHAS_SIBLING(pushop) ) |
45 |
0 |
45 |
|
| 265 |
45 |
| pushop = cUNOPx(pushop)->op_first; |
| 266 |
45 |
▶ | firstargop = OpSIBLING(pushop); |
Branch: 1 branch
| Branch | True | False | Total |
| firstargop = OpSIBLING(pushop); |
45 |
0 |
45 |
|
| 267 |
|
| |
| 268 |
139 |
▶ | for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; |
Branch: 2 branches
| Branch | True | False | Total |
| for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; |
94 |
0 |
94 |
| for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; |
94 |
45 |
139 |
|
| 269 |
|
| |
| 270 |
45 |
| lastargop = pushop; |
| 271 |
45 |
| for ( |
| 272 |
45 |
| arity = 0, lastargop = pushop, argop = firstargop; |
| 273 |
139 |
▶ | argop != cvop; |
Branch: 1 branch
| Branch | True | False | Total |
| argop != cvop; |
94 |
45 |
139 |
|
| 274 |
94 |
| lastargop = argop, argop = OpSIBLING(argop) |
| 275 |
|
| ){ |
| 276 |
94 |
▶ | arity++; |
Branch: 1 branch
| Branch | True | False | Total |
| arity++; |
94 |
0 |
94 |
|
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
45 |
▶ | if (expect_false(arity < min_arity || arity > max_arity)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (expect_false(arity < min_arity || arity > max_arity)) |
45 |
0 |
45 |
| if (expect_false(arity < min_arity || arity > max_arity)) |
0 |
45 |
45 |
| if (expect_false(arity < min_arity || arity > max_arity)) |
0 |
45 |
45 |
|
| 280 |
0 |
| return entersubop; |
| 281 |
|
| |
| 282 |
|
| /* If we get here, we can replace the entersub with a suitable |
| 283 |
|
| * custom OP. */ |
| 284 |
|
| |
| 285 |
45 |
▶ | if (opopt & OPOPT_CHECKSUM) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opopt & OPOPT_CHECKSUM) { |
0 |
45 |
45 |
|
| 286 |
0 |
| opopt |= arity; |
| 287 |
|
| } else { |
| 288 |
45 |
▶ | if (arity > min_arity && (opopt & OPOPT_DO_BODY)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (arity > min_arity && (opopt & OPOPT_DO_BODY)) { |
3 |
42 |
45 |
| if (arity > min_arity && (opopt & OPOPT_DO_BODY)) { |
2 |
1 |
3 |
|
| 289 |
2 |
| opopt |= OPOPT_OUTARG_BODY; |
| 290 |
2 |
| min_arity++; |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
45 |
▶ | if (arity > min_arity) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arity > min_arity) |
2 |
43 |
45 |
|
| 294 |
2 |
| opopt |= OPOPT_OUTARG_HEADER; |
| 295 |
|
| } |
| 296 |
|
| |
| 297 |
|
| #ifdef op_sibling_splice |
| 298 |
|
| /* op_sibling_splice is new in 5.31 and we have to do things differenly */ |
| 299 |
|
| |
| 300 |
|
| /* cut out all ops between the pushmark and the RV2CV */ |
| 301 |
45 |
| op_sibling_splice(NULL, pushop, arity, NULL); |
| 302 |
|
| /* then throw everything else out */ |
| 303 |
45 |
| op_free(entersubop); |
| 304 |
45 |
| newop = newUNOP(OP_NULL, 0, NULL); |
| 305 |
|
| |
| 306 |
|
| #else |
| 307 |
|
| |
| 308 |
|
| OpMORESIB_set(pushop, cvop); |
| 309 |
|
| OpLASTSIB_set(lastargop, op_parent(lastargop)); |
| 310 |
|
| op_free(entersubop); |
| 311 |
|
| newop = newUNOP(OP_NULL, 0, firstargop); |
| 312 |
|
| |
| 313 |
|
| #endif |
| 314 |
|
| |
| 315 |
45 |
| newop->op_type = OP_CUSTOM; |
| 316 |
45 |
| newop->op_private = opopt; |
| 317 |
90 |
| newop->op_ppaddr = opopt & OPOPT_CHECKSUM |
| 318 |
0 |
▶ | ? (opopt & OPOPT_VALIDATE_CHECKSUM ? THX_pp_validate_sereal_checksum : THX_pp_get_sereal_checksum) |
Branch: 1 branch
| Branch | True | False | Total |
| ? (opopt & OPOPT_VALIDATE_CHECKSUM ? THX_pp_validate_sereal_checksum : THX_pp_get_sereal_checksum) |
0 |
0 |
0 |
|
| 319 |
45 |
▶ | : (opopt & OPOPT_LOOKS_LIKE ? THX_pp_looks_like_sereal : THX_pp_sereal_decode); |
Branch: 2 branches
| Branch | True | False | Total |
| : (opopt & OPOPT_LOOKS_LIKE ? THX_pp_looks_like_sereal : THX_pp_sereal_decode); |
0 |
45 |
45 |
| : (opopt & OPOPT_LOOKS_LIKE ? THX_pp_looks_like_sereal : THX_pp_sereal_decode); |
0 |
45 |
45 |
|
| 320 |
|
| |
| 321 |
|
| #ifdef op_sibling_splice |
| 322 |
|
| |
| 323 |
|
| /* attach the spliced-out args as children of the custom op, while |
| 324 |
|
| * deleting the stub op created by newUNOP() */ |
| 325 |
45 |
| op_free(op_sibling_splice(newop, NULL, 1, firstargop)); |
| 326 |
|
| |
| 327 |
|
| #endif |
| 328 |
|
| |
| 329 |
45 |
| return newop; |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
|
| #endif /* USE_CUSTOM_OPS */ |
| 333 |
|
| |
| 334 |
|
| static void |
| 335 |
0 |
| THX_xsfunc_checksum(pTHX_ CV *cv) |
| 336 |
|
| { |
| 337 |
0 |
| dMARK; |
| 338 |
0 |
| dSP; |
| 339 |
0 |
| SSize_t arity = SP - MARK; |
| 340 |
0 |
| I32 cv_private = CvXSUBANY(cv).any_i32; |
| 341 |
0 |
| U8 opopt = cv_private & 0xff; |
| 342 |
0 |
| U8 max_arity = (cv_private >> 16) & 0xff; |
| 343 |
0 |
▶ | if (arity < 1 || arity > max_arity) |
Branch: 2 branches
| Branch | True | False | Total |
| if (arity < 1 || arity > max_arity) |
0 |
0 |
0 |
| if (arity < 1 || arity > max_arity) |
0 |
0 |
0 |
|
| 344 |
0 |
▶ | croak_xs_usage(cv, opopt & OPOPT_VALIDATE_CHECKSUM ? "packet [, key [, offset]]" : "packet [, offset]"); |
Branch: 1 branch
| Branch | True | False | Total |
| croak_xs_usage(cv, opopt & OPOPT_VALIDATE_CHECKSUM ? "packet [, key [, offset]]" : "packet [, offset]"); |
0 |
0 |
0 |
|
| 345 |
0 |
| THX_pp1_checksum(aTHX_ opopt, (U8)arity); |
| 346 |
0 |
| } |
| 347 |
|
| |
| 348 |
|
| static void |
| 349 |
246 |
| THX_xsfunc_sereal_decode(pTHX_ CV *cv) |
| 350 |
|
| { |
| 351 |
246 |
| dMARK; |
| 352 |
246 |
| dSP; |
| 353 |
246 |
| SSize_t arity = SP - MARK; |
| 354 |
246 |
| I32 cv_private = CvXSUBANY(cv).any_i32; |
| 355 |
246 |
| U8 opopt = cv_private & 0xff; |
| 356 |
246 |
| U8 min_arity = (cv_private >> 8) & 0xff; |
| 357 |
246 |
| U8 max_arity = (cv_private >> 16) & 0xff; |
| 358 |
|
| |
| 359 |
246 |
▶ | if (arity < min_arity || arity > max_arity) |
Branch: 2 branches
| Branch | True | False | Total |
| if (arity < min_arity || arity > max_arity) |
246 |
0 |
246 |
| if (arity < min_arity || arity > max_arity) |
0 |
246 |
246 |
|
| 360 |
0 |
| croak("bad Sereal decoder usage"); |
| 361 |
246 |
▶ | if (arity > min_arity && (opopt & OPOPT_DO_BODY)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (arity > min_arity && (opopt & OPOPT_DO_BODY)) { |
238 |
8 |
246 |
| if (arity > min_arity && (opopt & OPOPT_DO_BODY)) { |
235 |
3 |
238 |
|
| 362 |
235 |
| opopt |= OPOPT_OUTARG_BODY; |
| 363 |
235 |
| min_arity++; |
| 364 |
|
| } |
| 365 |
246 |
▶ | if (arity > min_arity) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arity > min_arity) |
8 |
238 |
246 |
|
| 366 |
8 |
| opopt |= OPOPT_OUTARG_HEADER; |
| 367 |
|
| |
| 368 |
246 |
| pp1_sereal_decode(opopt); |
| 369 |
244 |
| } |
| 370 |
|
| |
| 371 |
|
| static void |
| 372 |
0 |
| THX_xsfunc_looks_like_sereal(pTHX_ CV *cv) |
| 373 |
|
| { |
| 374 |
0 |
| dMARK; |
| 375 |
0 |
| dSP; |
| 376 |
0 |
| SSize_t arity = SP - MARK; |
| 377 |
0 |
| I32 cv_private = CvXSUBANY(cv).any_i32; |
| 378 |
0 |
| U8 max_arity = (cv_private >> 16) & 0xff; |
| 379 |
|
| |
| 380 |
0 |
▶ | if (arity < 1 || arity > max_arity) |
Branch: 2 branches
| Branch | True | False | Total |
| if (arity < 1 || arity > max_arity) |
0 |
0 |
0 |
| if (arity < 1 || arity > max_arity) |
0 |
0 |
0 |
|
| 381 |
0 |
▶ | croak_xs_usage(cv, max_arity == 1 ? "data" : "[invocant,] data"); |
Branch: 1 branch
| Branch | True | False | Total |
| croak_xs_usage(cv, max_arity == 1 ? "data" : "[invocant,] data"); |
0 |
0 |
0 |
|
| 382 |
0 |
▶ | if(arity == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(arity == 2) { |
0 |
0 |
0 |
|
| 383 |
0 |
| SV *data = POPs; |
| 384 |
0 |
| SETs(data); |
| 385 |
0 |
| PUTBACK; |
| 386 |
|
| } |
| 387 |
0 |
| pp1_looks_like_sereal(); |
| 388 |
0 |
| } |
| 389 |
|
| |
| 390 |
|
| #define MY_CXT_KEY "Sereal::Decoder::_stash" XS_VERSION |
| 391 |
|
| |
| 392 |
|
| typedef struct { |
| 393 |
|
| sv_with_hash options[SRL_DEC_OPT_COUNT]; |
| 394 |
|
| U8 known_option_heads[SRL_OPTION_INDEX_BUCKETS]; |
| 395 |
|
| U8 known_option_next[SRL_DEC_OPT_COUNT]; |
| 396 |
|
| } my_cxt_t; |
| 397 |
|
| |
| 398 |
|
| START_MY_CXT |
| 399 |
|
| |
| 400 |
|
| |
| 401 |
|
| MODULE = Sereal::Decoder PACKAGE = Sereal::Decoder |
| 402 |
|
| PROTOTYPES: DISABLE |
| 403 |
|
| |
| 404 |
|
| SV * |
| 405 |
|
| prepare_checksum_key(key = NULL, opt_key = NULL) |
| 406 |
|
| SV *key; |
| 407 |
|
| SV *opt_key; |
| 408 |
|
| PREINIT: |
| 409 |
0 |
| STRLEN key_len = 0; |
| 410 |
0 |
| STRLEN opt_key_len = 0; |
| 411 |
0 |
| const U8 *key_bytes = (const U8 *)""; |
| 412 |
0 |
▶ | const U8 *opt_key_bytes = (const U8 *)""; |
Branch: 1 branch
| Branch | True | False | Total |
| const U8 *opt_key_bytes = (const U8 *)""; |
0 |
0 |
0 |
|
| 413 |
|
| U8 digest[16]; |
| 414 |
|
| CODE: |
| 415 |
0 |
▶ | if (key && SvOK(key)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (key && SvOK(key)) |
0 |
0 |
0 |
| if (key && SvOK(key)) |
0 |
0 |
0 |
|
| 416 |
0 |
| key_bytes = (const U8 *)SvPVbyte(key, key_len); |
| 417 |
0 |
▶ | if (opt_key && SvOK(opt_key)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (opt_key && SvOK(opt_key)) { |
0 |
0 |
0 |
| if (opt_key && SvOK(opt_key)) { |
0 |
0 |
0 |
|
| 418 |
0 |
| opt_key_bytes = (const U8 *)SvPVbyte(opt_key, opt_key_len); |
| 419 |
|
| } |
| 420 |
0 |
| srl_siphash_prepare_key(key_bytes, (size_t)key_len, |
| 421 |
|
| opt_key_bytes, (size_t)opt_key_len, digest); |
| 422 |
0 |
| RETVAL = newSVpvn((const char *)digest, sizeof(digest)); |
| 423 |
|
| OUTPUT: RETVAL |
| 424 |
|
| |
| 425 |
|
| SV * |
| 426 |
|
| siphash24(data, key = NULL) |
| 427 |
|
| SV *data; |
| 428 |
|
| SV *key; |
| 429 |
|
| PREINIT: |
| 430 |
|
| STRLEN data_len; |
| 431 |
|
| STRLEN key_len; |
| 432 |
|
| const U8 *data_bytes; |
| 433 |
|
| const U8 *key_bytes; |
| 434 |
|
| srl_siphash_ctx sip; |
| 435 |
|
| srl_u64 digest; |
| 436 |
|
| U8 digest_bytes[8]; |
| 437 |
|
| unsigned int i; |
| 438 |
|
| static const U8 zero_key[16] = { 0 }; |
| 439 |
|
| CODE: |
| 440 |
1 |
| data_bytes = (const U8 *)SvPVbyte(data, data_len); |
| 441 |
1 |
▶ | if (key == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key == NULL) { |
0 |
1 |
1 |
|
| 442 |
0 |
| key_bytes = zero_key; |
| 443 |
|
| } |
| 444 |
|
| else { |
| 445 |
1 |
| key_bytes = (const U8 *)SvPVbyte(key, key_len); |
| 446 |
1 |
▶ | if (key_len != 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != 16) |
0 |
1 |
1 |
|
| 447 |
0 |
| croak("siphash24 key must be exactly 16 bytes"); |
| 448 |
|
| } |
| 449 |
1 |
| srl_siphash_init_rounds(&sip, key_bytes, 2, 4, 0); |
| 450 |
1 |
| srl_siphash_update(&sip, data_bytes, (size_t)data_len); |
| 451 |
1 |
| digest = srl_siphash_final64(&sip); |
| 452 |
9 |
▶ | for (i = 0; i < 8; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) |
8 |
1 |
9 |
|
| 453 |
8 |
| digest_bytes[i] = (U8)(digest >> (8 * i)); |
| 454 |
1 |
| RETVAL = newSVpvn((const char *)digest_bytes, sizeof(digest_bytes)); |
| 455 |
|
| OUTPUT: RETVAL |
| 456 |
|
| |
| 457 |
|
| SV * |
| 458 |
|
| siphash24_128(data, key = NULL) |
| 459 |
|
| SV *data; |
| 460 |
|
| SV *key; |
| 461 |
|
| PREINIT: |
| 462 |
|
| STRLEN data_len; |
| 463 |
|
| STRLEN key_len; |
| 464 |
|
| const U8 *data_bytes; |
| 465 |
|
| const U8 *key_bytes; |
| 466 |
|
| srl_siphash_ctx sip; |
| 467 |
|
| U8 digest[16]; |
| 468 |
|
| static const U8 zero_key[16] = { 0 }; |
| 469 |
|
| CODE: |
| 470 |
3 |
| data_bytes = (const U8 *)SvPVbyte(data, data_len); |
| 471 |
3 |
▶ | if (key == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key == NULL) { |
0 |
3 |
3 |
|
| 472 |
0 |
| key_bytes = zero_key; |
| 473 |
|
| } |
| 474 |
|
| else { |
| 475 |
3 |
| key_bytes = (const U8 *)SvPVbyte(key, key_len); |
| 476 |
3 |
▶ | if (key_len != 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != 16) |
0 |
3 |
3 |
|
| 477 |
0 |
| croak("siphash24_128 key must be exactly 16 bytes"); |
| 478 |
|
| } |
| 479 |
3 |
| srl_siphash_init_rounds(&sip, key_bytes, 2, 4, 1); |
| 480 |
3 |
| srl_siphash_update(&sip, data_bytes, (size_t)data_len); |
| 481 |
3 |
| srl_siphash_final128(&sip, digest); |
| 482 |
3 |
| RETVAL = newSVpvn((const char *)digest, sizeof(digest)); |
| 483 |
|
| OUTPUT: RETVAL |
| 484 |
|
| |
| 485 |
|
| SV * |
| 486 |
|
| siphash36_128(data, key = NULL) |
| 487 |
|
| SV *data; |
| 488 |
|
| SV *key; |
| 489 |
|
| PREINIT: |
| 490 |
|
| STRLEN data_len; |
| 491 |
|
| STRLEN key_len; |
| 492 |
|
| const U8 *data_bytes; |
| 493 |
|
| const U8 *key_bytes; |
| 494 |
|
| srl_siphash_ctx sip; |
| 495 |
|
| U8 digest[16]; |
| 496 |
|
| static const U8 zero_key[16] = { 0 }; |
| 497 |
|
| CODE: |
| 498 |
1 |
| data_bytes = (const U8 *)SvPVbyte(data, data_len); |
| 499 |
1 |
▶ | if (key == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key == NULL) { |
0 |
1 |
1 |
|
| 500 |
0 |
| key_bytes = zero_key; |
| 501 |
|
| } |
| 502 |
|
| else { |
| 503 |
1 |
| key_bytes = (const U8 *)SvPVbyte(key, key_len); |
| 504 |
1 |
▶ | if (key_len != 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != 16) |
0 |
1 |
1 |
|
| 505 |
0 |
| croak("siphash36_128 key must be exactly 16 bytes"); |
| 506 |
|
| } |
| 507 |
1 |
| srl_siphash_init_rounds(&sip, key_bytes, 3, 6, 1); |
| 508 |
1 |
| srl_siphash_update(&sip, data_bytes, (size_t)data_len); |
| 509 |
1 |
| srl_siphash_final128(&sip, digest); |
| 510 |
1 |
| RETVAL = newSVpvn((const char *)digest, sizeof(digest)); |
| 511 |
|
| OUTPUT: RETVAL |
| 512 |
|
| |
| 513 |
|
| BOOT: |
| 514 |
|
| { |
| 515 |
|
| struct { |
| 516 |
|
| char const *name_suffix; |
| 517 |
|
| U8 opopt; |
| 518 |
50 |
| } const funcs_to_install[] = { |
| 519 |
|
| { "", OPOPT_DO_BODY }, |
| 520 |
|
| { "_only_header", OPOPT_DO_HEADER }, |
| 521 |
|
| { "_with_header", (OPOPT_DO_BODY|OPOPT_DO_HEADER) }, |
| 522 |
|
| { "_with_offset", (OPOPT_DO_BODY|OPOPT_OFFSET) }, |
| 523 |
|
| { "_only_header_with_offset", (OPOPT_DO_HEADER|OPOPT_OFFSET) }, |
| 524 |
|
| { "_with_header_and_offset", (OPOPT_DO_BODY|OPOPT_DO_HEADER|OPOPT_OFFSET) }, |
| 525 |
|
| /*012345678901234567890123*/ |
| 526 |
|
| }, *fti; |
| 527 |
|
| int i; |
| 528 |
|
| { |
| 529 |
|
| MY_CXT_INIT; |
| 530 |
50 |
| Zero(MY_CXT.known_option_heads, SRL_OPTION_INDEX_BUCKETS, U8); |
| 531 |
50 |
| Zero(MY_CXT.known_option_next, SRL_DEC_OPT_COUNT, U8); |
| 532 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_ALIAS_SMALLINT, SRL_DEC_OPT_STR_ALIAS_SMALLINT ); |
| 533 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_ALIAS_VARINT_UNDER, SRL_DEC_OPT_STR_ALIAS_VARINT_UNDER ); |
| 534 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_DESTRUCTIVE_INCREMENTAL, SRL_DEC_OPT_STR_DESTRUCTIVE_INCREMENTAL ); |
| 535 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_MAX_NUM_HASH_ENTRIES, SRL_DEC_OPT_STR_MAX_NUM_HASH_ENTRIES ); |
| 536 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_MAX_RECURSION_DEPTH, SRL_DEC_OPT_STR_MAX_RECURSION_DEPTH ); |
| 537 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_NO_BLESS_OBJECTS, SRL_DEC_OPT_STR_NO_BLESS_OBJECTS ); |
| 538 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_REFUSE_OBJECTS, SRL_DEC_OPT_STR_REFUSE_OBJECTS ); |
| 539 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_REFUSE_SNAPPY, SRL_DEC_OPT_STR_REFUSE_SNAPPY ); |
| 540 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_REFUSE_ZLIB, SRL_DEC_OPT_STR_REFUSE_ZLIB ); |
| 541 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_SET_READONLY, SRL_DEC_OPT_STR_SET_READONLY ); |
| 542 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_SET_READONLY_SCALARS, SRL_DEC_OPT_STR_SET_READONLY_SCALARS ); |
| 543 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_USE_UNDEF, SRL_DEC_OPT_STR_USE_UNDEF ); |
| 544 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_VALIDATE_UTF8, SRL_DEC_OPT_STR_VALIDATE_UTF8 ); |
| 545 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_REFUSE_ZSTD, SRL_DEC_OPT_STR_REFUSE_ZSTD ); |
| 546 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_MAX_NUM_ARRAY_ENTRIES, SRL_DEC_OPT_STR_MAX_NUM_ARRAY_ENTRIES ); |
| 547 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_MAX_STRING_LENGTH, SRL_DEC_OPT_STR_MAX_STRING_LENGTH ); |
| 548 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_MAX_UNCOMPRESSED_SIZE, SRL_DEC_OPT_STR_MAX_UNCOMPRESSED_SIZE ); |
| 549 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_NO_THAW_OBJECTS, SRL_DEC_OPT_STR_NO_THAW_OBJECTS ); |
| 550 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_THAW_ALLOW_CLASSES, SRL_DEC_OPT_STR_THAW_ALLOW_CLASSES ); |
| 551 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_ALLOWED_CLASSES, SRL_DEC_OPT_STR_ALLOWED_CLASSES ); |
| 552 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_PARANOID, SRL_DEC_OPT_STR_PARANOID ); |
| 553 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_ALLOW_CLASSES, SRL_DEC_OPT_STR_ALLOW_CLASSES ); |
| 554 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_TRACK_CLASSES, SRL_DEC_OPT_STR_TRACK_CLASSES ); |
| 555 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_THAW_DENY_ACTION, SRL_DEC_OPT_STR_THAW_DENY_ACTION ); |
| 556 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_CHECKSUM_KEY_V1, SRL_DEC_OPT_STR_CHECKSUM_KEY_V1 ); |
| 557 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_CHECKSUM_KEY_V2, SRL_DEC_OPT_STR_CHECKSUM_KEY_V2 ); |
| 558 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_CHECKSUM_KEY_V3, SRL_DEC_OPT_STR_CHECKSUM_KEY_V3 ); |
| 559 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_CHECKSUM_POLICY, SRL_DEC_OPT_STR_CHECKSUM_POLICY ); |
| 560 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_UNDEF_DEADLY_POLICY, SRL_DEC_OPT_STR_UNDEF_DEADLY_POLICY ); |
| 561 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_UNKNOWN_OPTION_POLICY, SRL_DEC_OPT_STR_UNKNOWN_OPTION_POLICY ); |
| 562 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_CHECKSUM_KEY, SRL_DEC_OPT_STR_CHECKSUM_KEY ); |
| 563 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_CHECKSUM_MODE, SRL_DEC_OPT_STR_CHECKSUM_MODE ); |
| 564 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_LEGACY_FLOAT_BYTEORDER, SRL_DEC_OPT_STR_LEGACY_FLOAT_BYTEORDER ); |
| 565 |
50 |
| SRL_INIT_OPTION( SRL_DEC_OPT_IDX_ALLOW_UNKNOWN_LONG_DOUBLE, SRL_DEC_OPT_STR_ALLOW_UNKNOWN_LONG_DOUBLE ); |
| 566 |
|
| } |
| 567 |
|
| #if USE_CUSTOM_OPS |
| 568 |
|
| { |
| 569 |
|
| /* Perl borrows this descriptor; it must outlive every interpreter. */ |
| 570 |
|
| static const XOP xop = { |
| 571 |
|
| XOPf_xop_name | XOPf_xop_desc | XOPf_xop_class, |
| 572 |
|
| "sereal_decode_with_object", "sereal_decode_with_object", OA_UNOP |
| 573 |
|
| }; |
| 574 |
50 |
| Perl_custom_op_register(aTHX_ THX_pp_sereal_decode, &xop); |
| 575 |
|
| } |
| 576 |
|
| #endif /* USE_CUSTOM_OPS */ |
| 577 |
350 |
▶ | for (i = sizeof(funcs_to_install)/sizeof(*fti); i--; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = sizeof(funcs_to_install)/sizeof(*fti); i--; ) { |
300 |
50 |
350 |
|
| 578 |
|
| # define LONG_CLASS_FMT "Sereal::Decoder::sereal_decode%s_with_object" |
| 579 |
|
| char name[sizeof(LONG_CLASS_FMT)+24]; |
| 580 |
300 |
| char proto[7], *p = proto; |
| 581 |
|
| U8 opopt; |
| 582 |
|
| I32 cv_private; |
| 583 |
|
| GV *gv; |
| 584 |
|
| CV *cv; |
| 585 |
|
| |
| 586 |
300 |
| fti = &funcs_to_install[i]; |
| 587 |
300 |
| opopt = fti->opopt; |
| 588 |
|
| /* |
| 589 |
|
| * The cv_private value incorporates flags describing the operation to be |
| 590 |
|
| * performed by the sub and precomputed arity limits. 0x020200 corresponds |
| 591 |
|
| * to min_arity=2 and max_arity=2. The various additions to cv_private |
| 592 |
|
| * increment one or both of these sub-values. |
| 593 |
|
| |
| 594 |
|
| * The six subs created there share a single C body function, and are |
| 595 |
|
| * differentiated only by the option flags in cv_private. The custom ops |
| 596 |
|
| * likewise share one op_ppaddr function, and the operations they perform |
| 597 |
|
| * are differentiated by the same flags, stored in op_private. |
| 598 |
|
| */ |
| 599 |
300 |
| cv_private = opopt | 0x020200; |
| 600 |
|
| |
| 601 |
|
| /* Yes, the subs have prototypes. The protoypes have no effect when the |
| 602 |
|
| * subs are used as methods, so there's no break of compatibility for those |
| 603 |
|
| * using the documented API. There is a change that could be detected by |
| 604 |
|
| * code such as "Sereal::Decoder::decode($dec, @v)", that uses the methods |
| 605 |
|
| * directly in an undocumented way. |
| 606 |
|
| * |
| 607 |
|
| * The prototype, specifically the putting of argument expressions into |
| 608 |
|
| * scalar context, is required in order to be able to resolve arity at |
| 609 |
|
| * compile time. If this wasn't done, there would have to be a pushmark |
| 610 |
|
| * op preceding the argument ops, and pp_sereal_decode() would need the |
| 611 |
|
| * same code as xsfunc_sereal_decode() to check arity and resolve the |
| 612 |
|
| * optional-parameter flags. |
| 613 |
|
| */ |
| 614 |
300 |
| *p++ = '$'; |
| 615 |
300 |
| *p++ = '$'; |
| 616 |
|
| |
| 617 |
300 |
▶ | if (opopt & OPOPT_OFFSET) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opopt & OPOPT_OFFSET) { |
150 |
150 |
300 |
|
| 618 |
150 |
| *p++ = '$'; |
| 619 |
150 |
| cv_private += 0x010100; |
| 620 |
|
| } |
| 621 |
300 |
| *p++ = ';'; |
| 622 |
300 |
▶ | if (opopt & OPOPT_DO_BODY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opopt & OPOPT_DO_BODY) { |
200 |
100 |
300 |
|
| 623 |
200 |
| *p++ = '$'; |
| 624 |
200 |
| cv_private += 0x010000; |
| 625 |
|
| } |
| 626 |
300 |
▶ | if (opopt & OPOPT_DO_HEADER) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opopt & OPOPT_DO_HEADER) { |
200 |
100 |
300 |
|
| 627 |
200 |
| *p++ = '$'; |
| 628 |
200 |
| cv_private += 0x010000; |
| 629 |
|
| } |
| 630 |
300 |
| *p = 0; |
| 631 |
|
| /* setup the name of the sub */ |
| 632 |
300 |
| sprintf(name, LONG_CLASS_FMT, fti->name_suffix); |
| 633 |
300 |
| cv = newXSproto_portable(name, THX_xsfunc_sereal_decode, __FILE__, |
| 634 |
|
| proto); |
| 635 |
300 |
| CvXSUBANY(cv).any_i32 = cv_private; |
| 636 |
|
| #if USE_CUSTOM_OPS |
| 637 |
300 |
| cv_set_call_checker(cv, THX_ck_entersub_args_sereal_decoder, (SV*)cv); |
| 638 |
|
| #endif /* USE_CUSTOM_OPS */ |
| 639 |
300 |
| sprintf(name, "Sereal::Decoder::decode%s", fti->name_suffix); |
| 640 |
300 |
| gv = gv_fetchpv(name, GV_ADDMULTI, SVt_PVCV); |
| 641 |
300 |
| GvCV_set(gv, cv); |
| 642 |
|
| } |
| 643 |
|
| } |
| 644 |
|
| |
| 645 |
|
| BOOT: |
| 646 |
|
| { |
| 647 |
|
| #if USE_CUSTOM_OPS |
| 648 |
|
| { |
| 649 |
|
| static const XOP xop = { |
| 650 |
|
| XOPf_xop_name | XOPf_xop_desc | XOPf_xop_class, |
| 651 |
|
| "scalar_looks_like_sereal", "scalar_looks_like_sereal", OA_UNOP |
| 652 |
|
| }; |
| 653 |
50 |
| Perl_custom_op_register(aTHX_ THX_pp_looks_like_sereal, &xop); |
| 654 |
|
| } |
| 655 |
|
| #endif /* USE_CUSTOM_OPS */ |
| 656 |
|
| { |
| 657 |
|
| CV *cv; |
| 658 |
50 |
| cv = newXSproto_portable("Sereal::Decoder::scalar_looks_like_sereal", THX_xsfunc_looks_like_sereal, __FILE__, "$"); |
| 659 |
50 |
| CvXSUBANY(cv).any_i32 = 0x010100 | OPOPT_LOOKS_LIKE; |
| 660 |
|
| #if USE_CUSTOM_OPS |
| 661 |
50 |
| cv_set_call_checker(cv, THX_ck_entersub_args_sereal_decoder, (SV*)cv); |
| 662 |
|
| #endif /* USE_CUSTOM_OPS */ |
| 663 |
50 |
| cv = newXS("Sereal::Decoder::looks_like_sereal", THX_xsfunc_looks_like_sereal, __FILE__); |
| 664 |
50 |
| CvXSUBANY(cv).any_i32 = 0x020100 | OPOPT_LOOKS_LIKE; |
| 665 |
|
| } |
| 666 |
|
| } |
| 667 |
|
| |
| 668 |
|
| BOOT: |
| 669 |
|
| { |
| 670 |
|
| int checksum_i; |
| 671 |
150 |
▶ | for (checksum_i = 0; checksum_i < 2; checksum_i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (checksum_i = 0; checksum_i < 2; checksum_i++) { |
100 |
50 |
150 |
|
| 672 |
100 |
| const char *name = checksum_i ? "Sereal::Decoder::validate_sereal_checksum" |
| 673 |
100 |
▶ | : "Sereal::Decoder::get_sereal_checksum"; |
Branch: 1 branch
| Branch | True | False | Total |
| : "Sereal::Decoder::get_sereal_checksum"; |
50 |
50 |
100 |
|
| 674 |
100 |
▶ | CV *cv = newXSproto_portable(name, THX_xsfunc_checksum, __FILE__, checksum_i ? "$;$$" : "$;$"); |
Branch: 1 branch
| Branch | True | False | Total |
| CV *cv = newXSproto_portable(name, THX_xsfunc_checksum, __FILE__, checksum_i ? "$;$$" : "$;$"); |
50 |
50 |
100 |
|
| 675 |
100 |
| CvXSUBANY(cv).any_i32 = OPOPT_CHECKSUM |
| 676 |
100 |
▶ | | (checksum_i ? OPOPT_VALIDATE_CHECKSUM | 0x030100 : 0x020100); |
Branch: 1 branch
| Branch | True | False | Total |
| | (checksum_i ? OPOPT_VALIDATE_CHECKSUM | 0x030100 : 0x020100); |
50 |
50 |
100 |
|
| 677 |
|
| #if USE_CUSTOM_OPS |
| 678 |
|
| { |
| 679 |
|
| static const XOP xops[2] = { |
| 680 |
|
| { XOPf_xop_name | XOPf_xop_desc | XOPf_xop_class, |
| 681 |
|
| "get_sereal_checksum", "get_sereal_checksum", OA_UNOP }, |
| 682 |
|
| { XOPf_xop_name | XOPf_xop_desc | XOPf_xop_class, |
| 683 |
|
| "validate_sereal_checksum", "validate_sereal_checksum", OA_UNOP } |
| 684 |
|
| }; |
| 685 |
100 |
▶ | Perl_custom_op_register(aTHX_ checksum_i ? THX_pp_validate_sereal_checksum : THX_pp_get_sereal_checksum, &xops[checksum_i]); |
Branch: 1 branch
| Branch | True | False | Total |
| Perl_custom_op_register(aTHX_ checksum_i ? THX_pp_validate_sereal_checksum : THX_pp_get_sereal_checksum, &xops[checksum_i]); |
50 |
50 |
100 |
|
| 686 |
|
| } |
| 687 |
100 |
| cv_set_call_checker(cv, THX_ck_entersub_args_sereal_decoder, (SV*)cv); |
| 688 |
|
| #endif |
| 689 |
|
| } |
| 690 |
|
| } |
| 691 |
|
| |
| 692 |
|
| srl_decoder_t * |
| 693 |
|
| new(CLASS, opt = NULL) |
| 694 |
|
| char *CLASS; |
| 695 |
|
| HV *opt; |
| 696 |
|
| PREINIT: |
| 697 |
|
| dMY_CXT; |
| 698 |
|
| CODE: |
| 699 |
44 |
| RETVAL = srl_build_decoder_struct(aTHX_ opt, MY_CXT.options, |
| 700 |
|
| MY_CXT.known_option_heads, MY_CXT.known_option_next); |
| 701 |
44 |
| RETVAL->flags |= SRL_F_DECODER_REUSE; |
| 702 |
|
| OUTPUT: RETVAL |
| 703 |
|
| |
| 704 |
|
| void |
| 705 |
|
| DESTROY(dec) |
| 706 |
|
| srl_decoder_t *dec; |
| 707 |
|
| CODE: |
| 708 |
44 |
| srl_destroy_decoder(aTHX_ dec); |
| 709 |
|
| |
| 710 |
|
| void |
| 711 |
|
| decode_sereal(src, opt = NULL, into = NULL) |
| 712 |
|
| SV *src; |
| 713 |
|
| SV *opt; |
| 714 |
|
| SV *into; |
| 715 |
|
| PREINIT: |
| 716 |
|
| dMY_CXT; |
| 717 |
0 |
▶ | srl_decoder_t *dec= NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| srl_decoder_t *dec= NULL; |
0 |
0 |
0 |
|
| 718 |
|
| PPCODE: |
| 719 |
0 |
▶ | if (SvROK(src)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(src)) |
0 |
0 |
0 |
|
| 720 |
0 |
| croak("We can't decode a reference as Sereal!"); |
| 721 |
|
| /* Support no opt at all, undef, hashref */ |
| 722 |
0 |
▶ | if (opt != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt != NULL) { |
0 |
0 |
0 |
|
| 723 |
0 |
| SvGETMAGIC(opt); |
| 724 |
0 |
▶ | if (!SvOK(opt)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(opt)) |
0 |
0 |
0 |
|
| 725 |
0 |
| opt = NULL; |
| 726 |
0 |
▶ | else if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVHV) |
0 |
0 |
0 |
| else if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVHV) |
0 |
0 |
0 |
|
| 727 |
0 |
| opt = (SV *)SvRV(opt); |
| 728 |
|
| else |
| 729 |
0 |
| croak("Options are neither undef nor hash reference"); |
| 730 |
|
| } |
| 731 |
0 |
| dec = srl_build_decoder_struct(aTHX_ (HV *)opt, MY_CXT.options, |
| 732 |
|
| MY_CXT.known_option_heads, MY_CXT.known_option_next); |
| 733 |
0 |
| ST(0)= srl_decode_into(aTHX_ dec, src, into, 0); |
| 734 |
0 |
| XSRETURN(1); |
| 735 |
|
| |
| 736 |
|
| AV * |
| 737 |
|
| decode_sereal_with_header_data(src, opt = NULL, body_into = NULL, header_into = NULL) |
| 738 |
|
| SV *src; |
| 739 |
|
| SV *opt; |
| 740 |
|
| SV *body_into; |
| 741 |
|
| SV *header_into; |
| 742 |
|
| PREINIT: |
| 743 |
|
| dMY_CXT; |
| 744 |
0 |
▶ | srl_decoder_t *dec= NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| srl_decoder_t *dec= NULL; |
0 |
0 |
0 |
|
| 745 |
|
| CODE: |
| 746 |
|
| /* Support no opt at all, undef, hashref */ |
| 747 |
0 |
▶ | if (opt != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt != NULL) { |
0 |
0 |
0 |
|
| 748 |
0 |
| SvGETMAGIC(opt); |
| 749 |
0 |
▶ | if (!SvOK(opt)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(opt)) |
0 |
0 |
0 |
|
| 750 |
0 |
| opt = NULL; |
| 751 |
0 |
▶ | else if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVHV) |
0 |
0 |
0 |
| else if (SvROK(opt) && SvTYPE(SvRV(opt)) == SVt_PVHV) |
0 |
0 |
0 |
|
| 752 |
0 |
| opt = (SV *)SvRV(opt); |
| 753 |
|
| else |
| 754 |
0 |
| croak("Options are neither undef nor hash reference"); |
| 755 |
|
| } |
| 756 |
0 |
| dec = srl_build_decoder_struct(aTHX_ (HV *)opt, MY_CXT.options, |
| 757 |
|
| MY_CXT.known_option_heads, MY_CXT.known_option_next); |
| 758 |
0 |
▶ | if (body_into == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (body_into == NULL) |
0 |
0 |
0 |
|
| 759 |
0 |
| body_into = sv_newmortal(); |
| 760 |
0 |
▶ | if (header_into == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (header_into == NULL) |
0 |
0 |
0 |
|
| 761 |
0 |
| header_into = sv_newmortal(); |
| 762 |
0 |
| srl_decode_all_into(aTHX_ dec, src, header_into, body_into, 0); |
| 763 |
0 |
| RETVAL = newAV(); |
| 764 |
0 |
| sv_2mortal((SV *)RETVAL); |
| 765 |
0 |
| av_extend(RETVAL, 1); |
| 766 |
0 |
| av_store(RETVAL, 0, SvREFCNT_inc(header_into)); |
| 767 |
0 |
| av_store(RETVAL, 1, SvREFCNT_inc(body_into)); |
| 768 |
|
| OUTPUT: RETVAL |
| 769 |
|
| |
| 770 |
|
| UV |
| 771 |
|
| bytes_consumed(dec) |
| 772 |
|
| srl_decoder_t *dec; |
| 773 |
|
| CODE: |
| 774 |
0 |
▶ | RETVAL = dec->bytes_consumed; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = dec->bytes_consumed; |
0 |
0 |
0 |
|
| 775 |
|
| OUTPUT: RETVAL |
| 776 |
|
| |
| 777 |
|
| U32 |
| 778 |
|
| flags(dec) |
| 779 |
|
| srl_decoder_t *dec; |
| 780 |
|
| CODE: |
| 781 |
0 |
▶ | RETVAL = dec->flags; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = dec->flags; |
0 |
0 |
0 |
|
| 782 |
|
| OUTPUT: RETVAL |
| 783 |
|
| |
| 784 |
|
| SV* |
| 785 |
|
| regexp_internals_type() |
| 786 |
|
| CODE: |
| 787 |
0 |
| RETVAL = newSVpvs(REGEXP_TYPE); |
| 788 |
|
| OUTPUT: RETVAL |