| 1 |
|
| #ifndef PSAML_CLOS_H |
| 2 |
|
| #define PSAML_CLOS_H |
| 3 |
|
| |
| 4 |
|
| /* Closures, and the small predicates the rest of this distribution reads |
| 5 |
|
| * options with. |
| 6 |
|
| * |
| 7 |
|
| * This is Punk-Feed's pfeed_closure, which is Punk-Authorisation's |
| 8 |
|
| * pau_closure, which is Punk-TOTP's pp_closure, which is a copy of |
| 9 |
|
| * Punk's private punk_closure: a CV built with newXS carrying captured |
| 10 |
|
| * SVs in PERL_MAGIC_ext. Punk does not export it - pk_abi.h is an |
| 11 |
|
| * observer interface and says so - so a plugin that wants a body with |
| 12 |
|
| * state carries its own. |
| 13 |
|
| * |
| 14 |
|
| * The capture is an AV rather than a fixed pair, because the bodies here |
| 15 |
|
| * capture [app], [app, idp] and [app, idp, opts]. */ |
| 16 |
|
| |
| 17 |
|
| #include "psaml_compat.h" |
| 18 |
|
| |
| 19 |
|
| typedef struct { AV *cap; } psaml_clos_t; |
| 20 |
|
| |
| 21 |
116 |
| static int psaml_clos_free(pTHX_ SV *sv, MAGIC *mg) { |
| 22 |
116 |
| psaml_clos_t *c = (psaml_clos_t *)mg->mg_ptr; |
| 23 |
|
| PERL_UNUSED_ARG(sv); |
| 24 |
116 |
▶ | if (c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c) { |
116 |
0 |
116 |
|
| 25 |
116 |
▶ | if (c->cap) SvREFCNT_dec((SV *)c->cap); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->cap) SvREFCNT_dec((SV *)c->cap); |
116 |
0 |
116 |
|
| 26 |
116 |
| Safefree(c); |
| 27 |
|
| } |
| 28 |
116 |
| return 0; |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
|
| static MGVTBL psaml_clos_vtbl = { NULL, NULL, NULL, NULL, psaml_clos_free, |
| 32 |
|
| NULL, NULL, NULL }; |
| 33 |
|
| |
| 34 |
|
| /* Takes ownership of cap. */ |
| 35 |
258 |
| PERL_STATIC_INLINE SV *psaml_closure(pTHX_ XSUBADDR_t body, AV *cap) { |
| 36 |
258 |
| CV *cv = (CV *)newXS(NULL, body, (char *)__FILE__); |
| 37 |
|
| psaml_clos_t *c; |
| 38 |
258 |
| Newxz(c, 1, psaml_clos_t); |
| 39 |
258 |
| c->cap = cap; |
| 40 |
258 |
| sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &psaml_clos_vtbl, (char *)c, 0); |
| 41 |
258 |
| return newRV_noinc((SV *)cv); |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
146 |
| PERL_STATIC_INLINE AV *psaml_cap_of(pTHX_ CV *cv) { |
| 45 |
146 |
| MAGIC *mg = mg_findext((SV *)cv, PERL_MAGIC_ext, &psaml_clos_vtbl); |
| 46 |
146 |
▶ | AV *cap = mg ? ((psaml_clos_t *)mg->mg_ptr)->cap : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| AV *cap = mg ? ((psaml_clos_t *)mg->mg_ptr)->cap : NULL; |
146 |
0 |
146 |
|
| 47 |
146 |
▶ | if (!cap) croak("Punk::Plugin::SAML: a closure lost its capture"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cap) croak("Punk::Plugin::SAML: a closure lost its capture"); |
0 |
146 |
146 |
|
| 48 |
146 |
| return cap; |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| /* One capture slot, borrowed. */ |
| 52 |
146 |
| PERL_STATIC_INLINE SV *psaml_cap_slot(pTHX_ CV *cv, SSize_t i) { |
| 53 |
146 |
| AV *cap = psaml_cap_of(aTHX_ cv); |
| 54 |
146 |
| SV **e = av_fetch(cap, i, 0); |
| 55 |
146 |
▶ | return (e && *e) ? *e : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (e && *e) ? *e : NULL; |
146 |
0 |
146 |
| return (e && *e) ? *e : NULL; |
146 |
0 |
146 |
|
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
|
| /* ---- option predicates ----------------------------------------------- */ |
| 59 |
|
| |
| 60 |
|
| /* A defined, non-empty string option, borrowed, or NULL. |
| 61 |
|
| * |
| 62 |
|
| * SvPV is taken once into a named variable rather than called twice, |
| 63 |
|
| * because a caller writing f(SvPV(sv, l), l) reads l before SvPV has set |
| 64 |
|
| * it: the two arguments are siblings and their evaluation order is not |
| 65 |
|
| * ordered by anything. */ |
| 66 |
|
| PERL_STATIC_INLINE const char *psaml_opt_str(pTHX_ HV *o, const char *key, STRLEN *len) { |
| 67 |
|
| SV **e = hv_fetch(o, key, (I32)strlen(key), 0); |
| 68 |
|
| const char *p; |
| 69 |
|
| STRLEN n; |
| 70 |
|
| if (!e || !*e || !SvOK(*e)) return NULL; |
| 71 |
|
| p = SvPV_const(*e, n); |
| 72 |
|
| if (!n) return NULL; |
| 73 |
|
| if (len) *len = n; |
| 74 |
|
| return p; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
|
| /* A boolean option with a stated default. |
| 78 |
|
| * |
| 79 |
|
| * SvTRUE is a multi-evaluating macro, so the fetched SV goes into a |
| 80 |
|
| * variable first. Around a POPs it corrupts the stack; here it would |
| 81 |
|
| * merely fetch twice, but the habit is the point: this family has been |
| 82 |
|
| * bitten by SvTRUE and SvIV on expressions with side effects more than |
| 83 |
|
| * once. */ |
| 84 |
1265 |
| PERL_STATIC_INLINE int psaml_opt_bool(pTHX_ HV *o, const char *key, int dflt) { |
| 85 |
1265 |
| SV **e = hv_fetch(o, key, (I32)strlen(key), 0); |
| 86 |
|
| SV *sv; |
| 87 |
1265 |
▶ | if (!e || !*e || !SvOK(*e)) return dflt; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!e || !*e || !SvOK(*e)) return dflt; |
241 |
1024 |
1265 |
| if (!e || !*e || !SvOK(*e)) return dflt; |
241 |
0 |
241 |
| if (!e || !*e || !SvOK(*e)) return dflt; |
0 |
241 |
241 |
|
| 88 |
241 |
| sv = *e; |
| 89 |
241 |
| return SvTRUE(sv) ? 1 : 0; |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
483 |
| PERL_STATIC_INLINE IV psaml_opt_iv(pTHX_ HV *o, const char *key, IV dflt) { |
| 93 |
483 |
| SV **e = hv_fetch(o, key, (I32)strlen(key), 0); |
| 94 |
|
| SV *sv; |
| 95 |
483 |
▶ | if (!e || !*e || !SvOK(*e)) return dflt; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!e || !*e || !SvOK(*e)) return dflt; |
105 |
378 |
483 |
| if (!e || !*e || !SvOK(*e)) return dflt; |
105 |
0 |
105 |
| if (!e || !*e || !SvOK(*e)) return dflt; |
0 |
105 |
105 |
|
| 96 |
105 |
| sv = *e; |
| 97 |
105 |
| return SvIV(sv); |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| /* ---- small readers ---------------------------------------------------- */ |
| 101 |
|
| |
| 102 |
4888 |
| PERL_STATIC_INLINE SV *psaml_hget(pTHX_ HV *h, const char *k) { |
| 103 |
4888 |
▶ | SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL; |
4888 |
0 |
4888 |
|
| 104 |
4888 |
▶ | return (e && *e) ? *e : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (e && *e) ? *e : NULL; |
3121 |
1767 |
4888 |
| return (e && *e) ? *e : NULL; |
3121 |
0 |
3121 |
|
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
48 |
| PERL_STATIC_INLINE int psaml_is_code(pTHX_ SV *sv) { |
| 108 |
48 |
▶ | return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; |
Branch: 3 branches
| Branch | True | False | Total |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; |
24 |
24 |
48 |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; |
22 |
2 |
24 |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; |
22 |
0 |
22 |
|
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
84 |
| PERL_STATIC_INLINE int psaml_is_hash(pTHX_ SV *sv) { |
| 112 |
84 |
▶ | return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; |
Branch: 3 branches
| Branch | True | False | Total |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; |
84 |
0 |
84 |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; |
84 |
0 |
84 |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; |
84 |
0 |
84 |
|
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
112 |
| PERL_STATIC_INLINE int psaml_is_array(pTHX_ SV *sv) { |
| 116 |
112 |
▶ | return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; |
Branch: 3 branches
| Branch | True | False | Total |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; |
112 |
0 |
112 |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; |
6 |
106 |
112 |
| return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; |
5 |
1 |
6 |
|
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| /* A non-negative integer option with a default. A negative skew or ttl is |
| 120 |
|
| * not a smaller one, it is a comparison that runs backwards. */ |
| 121 |
113 |
| PERL_STATIC_INLINE IV psaml_opt_uv(pTHX_ HV *in, const char *k, IV dflt) { |
| 122 |
113 |
| SV *v = psaml_hget(aTHX_ in, k); |
| 123 |
|
| IV n; |
| 124 |
113 |
▶ | if (!v || !SvOK(v)) return dflt; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || !SvOK(v)) return dflt; |
2 |
111 |
113 |
| if (!v || !SvOK(v)) return dflt; |
0 |
2 |
2 |
|
| 125 |
2 |
| n = SvIV(v); |
| 126 |
2 |
▶ | if (n < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) |
1 |
1 |
2 |
|
| 127 |
1 |
| croak("%s: `%s` must be zero or more, not %" IVdf, PSAML_WHO, k, n); |
| 128 |
1 |
| return n; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| #endif /* PSAML_CLOS_H */ |