Skip to content

Commit

Permalink
Merge pull request #530 from jarrodb/transformations
Browse files Browse the repository at this point in the history
add width string transformation
  • Loading branch information
liviuchircu committed Jun 2, 2015
2 parents 5bf5479 + a9d3150 commit 5ff8ff5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
71 changes: 71 additions & 0 deletions transformations.c
Expand Up @@ -706,6 +706,53 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
else
trans_fill_right(val, st, i);

break;
case TR_S_WIDTH:
if(tp==NULL || tp->next!=NULL)
{
LM_ERR("width invalid parameters\n");
return -1;
}
if(!(val->flags&PV_VAL_STR))
val->rs.s = int2str(val->ri, &val->rs.len);
if(tp->type==TR_PARAM_NUMBER)
{
i = 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("substr cannot get p1\n");
return -1;
}
i = v.ri;
}
if (i <= 0) {
LM_ERR("width invalid (must be >= 1)\n");
return -1;
}
if (i <= val->rs.len) {
/* since the requested width is less than
the value length, just update the length */
val->rs.len = i;
break;
}

if(i>TR_BUFFER_SIZE-1)
/* width cant be greater than buffer */
return -1;

j = i - val->rs.len; /* calc extra length */
p = _tr_buffer;

/* copy existing string to buffer and append j spaces */
memcpy(p, val->rs.s, val->rs.len);
memset(p+val->rs.len, ' ', j);
memset(val, 0, sizeof(pv_value_t));

val->flags = PV_VAL_STR;
val->rs.s = _tr_buffer;
val->rs.len = i;
break;
default:
LM_ERR("unknown subtype %d\n",
Expand Down Expand Up @@ -2542,6 +2589,30 @@ char* tr_parse_string(str* in, trans_t *t)
goto error;
}
return p;
} else if(name.len==5 && strncasecmp(name.s, "width", 5)==0) {
t->subtype = TR_S_WIDTH;
if(*p!=TR_PARAM_MARKER)
{
LM_ERR("invalid substr transformation: %.*s!\n", in->len, in->s);
goto error;
}
p++;
_tr_parse_nparam(p, p0, tp, spec, n, sign, in, s);
t->params = tp;
if(tp->type==TR_PARAM_NUMBER && tp->v.n<0)
{
LM_ERR("width negative\n");
goto error;
}
tp = 0;
while(is_in_str(p, in) && (*p==' ' || *p=='\t' || *p=='\n')) p++;
if(*p!=TR_RBRACKET)
{
LM_ERR("invalid width transformation: %.*s!!\n",
in->len, in->s);
goto error;
}
return p;
}

LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s,
Expand Down
2 changes: 1 addition & 1 deletion transformations.h
Expand Up @@ -44,7 +44,7 @@ enum _tr_s_subtype {
TR_S_SELECT, TR_S_ENCODEHEXA, TR_S_DECODEHEXA, TR_S_HEX2DEC, TR_S_DEC2HEX,
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_CRC32,
TR_S_INDEX, TR_S_RINDEX, TR_S_FILL_LEFT, TR_S_FILL_RIGHT
TR_S_INDEX, TR_S_RINDEX, TR_S_FILL_LEFT, TR_S_FILL_RIGHT, TR_S_WIDTH
};
enum _tr_uri_subtype {
TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,
Expand Down

0 comments on commit 5ff8ff5

Please sign in to comment.