From 74563696fa064b7cbe169e9af1020ba840c8b8ab Mon Sep 17 00:00:00 2001 From: lazedo Date: Thu, 17 Aug 2017 03:04:20 +0100 Subject: [PATCH] pv: add string corehash transform optional size parameter to return the slot if supplied example $(var(myvar){s.corehash}) returns the hash $(var(myvar){s.corehash, 32}) returns the slot --- src/modules/pv/pv_trans.c | 48 +++++++++++++++++++++++++++++++++++++++ src/modules/pv/pv_trans.h | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/modules/pv/pv_trans.c b/src/modules/pv/pv_trans.c index eccf3b63776..603219bf4e9 100644 --- a/src/modules/pv/pv_trans.c +++ b/src/modules/pv/pv_trans.c @@ -39,6 +39,7 @@ #include "../../core/dset.h" #include "../../core/basex.h" #include "../../core/action.h" +#include "../../core/hashes.h" #include "../../core/parser/parse_param.h" #include "../../core/parser/parse_uri.h" @@ -265,6 +266,7 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype, str st, st2; pv_value_t v, w; time_t t; + uint sz1, sz2; if(val==NULL || val->flags&PV_VAL_NULL) return -1; @@ -1121,6 +1123,35 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype, val->rs = st; break; + case TR_S_COREHASH: + if(!(val->flags&PV_VAL_STR)) + val->rs.s = int2str(val->ri, &val->rs.len); + + sz1 = 0; + if(tp != NULL) { + if(tp->type==TR_PARAM_NUMBER) { + sz1 = (uint) tp->v.n; + } else { + if(pv_get_spec_value(msg, (pv_spec_p)tp->v.data, &v)!=0 + || (!(v.flags&PV_VAL_INT))) + { + LM_ERR("corehash cannot get size (cfg line: %d)\n", + get_cfg_crt_line()); + return -1; + } + sz1 = (uint) v.ri; + } + } + + sz2 = core_hash(&val->rs, NULL, sz1); + + j = sprintf(_tr_buffer, "%u", sz2); + val->flags = PV_VAL_STR; + val->ri = 0; + val->rs.s = _tr_buffer; + val->rs.len = j; + break; + default: LM_ERR("unknown subtype %d (cfg line: %d)\n", subtype, get_cfg_crt_line()); @@ -2395,6 +2426,23 @@ char* tr_parse_string(str* in, trans_t *t) goto error; } goto done; + } else if(name.len==8 && strncasecmp(name.s, "corehash", 8)==0) { + t->subtype = TR_S_COREHASH; + if(*p==TR_PARAM_MARKER) + { + p++; + _tr_parse_nparam(p, p0, tp, spec, n, sign, in, s); + t->params = tp; + } + tp = 0; + while(*p && (*p==' ' || *p=='\t' || *p=='\n')) p++; + if(*p!=TR_RBRACKET) + { + LM_ERR("invalid corehash transformation: %.*s!!\n", + in->len, in->s); + goto error; + } + goto done; } else if(name.len==4 && strncasecmp(name.s, "trim", 4)==0) { t->subtype = TR_S_TRIM; goto done; diff --git a/src/modules/pv/pv_trans.h b/src/modules/pv/pv_trans.h index 764653e5e35..aacb722b957 100644 --- a/src/modules/pv/pv_trans.h +++ b/src/modules/pv/pv_trans.h @@ -41,7 +41,7 @@ enum _tr_s_subtype { TR_S_STRIP, TR_S_STRIPTAIL, TR_S_PREFIXES, TR_S_PREFIXES_QUOT, TR_S_REPLACE, TR_S_TIMEFORMAT, TR_S_TRIM, TR_S_RTRIM, TR_S_LTRIM, TR_S_RM, TR_S_STRIPTO, TR_S_URLENCODEPARAM, TR_S_URLDECODEPARAM, TR_S_NUMERIC, TR_S_ESCAPECSV, - TR_S_ENCODEBASE58, TR_S_DECODEBASE58 + TR_S_ENCODEBASE58, TR_S_DECODEBASE58, TR_S_COREHASH }; enum _tr_uri_subtype { TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,