[sr-dev] git:alexh/master: modules_k/pv: Add new transformations to create prefixes

Alex Hermann alex at speakup.nl
Mon Mar 7 15:19:01 CET 2011


Module: sip-router
Branch: alexh/master
Commit: 62e40ee24766f30bc2d2662d8d02476281963156
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=62e40ee24766f30bc2d2662d8d02476281963156

Author: Alex Hermann <alex at speakup.nl>
Committer: Alex Hermann <alex at speakup.nl>
Date:   Mon Dec 20 16:49:07 2010 +0100

modules_k/pv: Add new transformations to create prefixes
 s.prefixes creates a string with prefixes separated by comma's
 s.prefixes.quoted creates a string of quoted prefixes spearated by comma's
Both take a parameter to specify the max prefix length. If zero, then return upto the whole string.
Examples:
 "1234"{s.prefixes,0} => 1,12,123,1234
 "123456"{s.prefixes,4} => 1,12,123,1234
 "123456"{s.prefixes.quot,4} => '1','12','123','1234'

---

 modules_k/pv/pv_trans.c |   79 +++++++++++++++++++++++++++++++++++++++++++++-
 modules_k/pv/pv_trans.h |    2 +-
 2 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/modules_k/pv/pv_trans.c b/modules_k/pv/pv_trans.c
index e568ddb..35c7fe2 100644
--- a/modules_k/pv/pv_trans.c
+++ b/modules_k/pv/pv_trans.c
@@ -67,7 +67,7 @@ static char _tr_buffer[TR_BUFFER_SIZE];
 int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
 		pv_value_t *val)
 {
-	int i, j;
+	int i, j, max;
 	char *p, *s;
 	str st;
 	pv_value_t v;
@@ -452,6 +452,50 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
 			val->rs.len -= i;
 			break;
 
+		case TR_S_PREFIXES:
+		case TR_S_PREFIXES_QUOT:
+			if(!(val->flags&PV_VAL_STR))
+				val->rs.s = int2str(val->ri, &val->rs.len);
+
+			/* Set maximum prefix length */
+			max = val->rs.len;
+			if(tp!=NULL) {
+				if(tp->type==TR_PARAM_NUMBER) {
+					if (tp->v.n > 0 && tp->v.n < max)
+						max = 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("prefixes cannot get max\n");
+						return -1;
+					}
+					if (v.ri > 0 && v.ri < max)
+						max  = v.ri;
+				}
+			}
+
+			if(max * (max/2 + (subtype==TR_S_PREFIXES_QUOT ? 1 : 3)) > TR_BUFFER_SIZE-1) {
+				LM_ERR("prefixes buffer too short\n");
+				return -1;
+			}
+
+			j = 0;
+			for (i=1; i <= max; i++) {
+				if (subtype==TR_S_PREFIXES_QUOT)
+					_tr_buffer[j++] = '\'';
+				memcpy(&(_tr_buffer[j]), val->rs.s, i);
+				j += i;
+				if (subtype==TR_S_PREFIXES_QUOT)
+					_tr_buffer[j++] = '\'';
+				_tr_buffer[j++] = ',';
+			}
+			memset(val, 0, sizeof(pv_value_t));
+			val->flags = PV_VAL_STR;
+			val->rs.s = _tr_buffer;
+			val->rs.len = j-1;
+			break;
+
 		default:
 			LM_ERR("unknown subtype %d\n",
 					subtype);
@@ -1236,6 +1280,38 @@ char* tr_parse_string(str* in, trans_t *t)
 	} else if(name.len==14 && strncasecmp(name.s, "unescape.param", 14)==0) {
 		t->subtype = TR_S_UNESCAPEPARAM;
 		goto done;
+	} else if(name.len==8 && strncasecmp(name.s, "prefixes", 8)==0) {
+		t->subtype = TR_S_PREFIXES;
+		if(*p!=TR_PARAM_MARKER)
+			goto done;
+		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 prefixes transformation: %.*s!!\n",
+				in->len, in->s);
+			goto error;
+		}
+		goto done;
+	} else if(name.len==15 && strncasecmp(name.s, "prefixes.quoted", 15)==0) {
+		t->subtype = TR_S_PREFIXES_QUOT;
+		if(*p!=TR_PARAM_MARKER)
+			goto done;
+		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 prefixes transformation: %.*s!!\n",
+				in->len, in->s);
+			goto error;
+		}
+		goto done;
 	} else if(name.len==6 && strncasecmp(name.s, "substr", 6)==0) {
 		t->subtype = TR_S_SUBSTR;
 		if(*p!=TR_PARAM_MARKER)
@@ -1706,4 +1782,3 @@ done:
 	t->name = name;
 	return p;
 }
-
diff --git a/modules_k/pv/pv_trans.h b/modules_k/pv/pv_trans.h
index acd81db..1e601d8 100644
--- a/modules_k/pv/pv_trans.h
+++ b/modules_k/pv/pv_trans.h
@@ -39,7 +39,7 @@ enum _tr_s_subtype {
 	TR_S_SELECT, TR_S_ENCODEHEXA, TR_S_DECODEHEXA,
 	TR_S_ESCAPECOMMON, TR_S_UNESCAPECOMMON, TR_S_ESCAPEUSER, TR_S_UNESCAPEUSER,
 	TR_S_ESCAPEPARAM, TR_S_UNESCAPEPARAM, TR_S_TOLOWER, TR_S_TOUPPER,
-	TR_S_STRIP, TR_S_STRIPTAIL
+	TR_S_STRIP, TR_S_STRIPTAIL, TR_S_PREFIXES, TR_S_PREFIXES_QUOT
 };
 enum _tr_uri_subtype {
 	TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,




More information about the sr-dev mailing list