Module: kamailio
Branch: master
Commit: 5afe1e5942ba586ef3165e220a588a7176fc79e9
URL:
https://github.com/kamailio/kamailio/commit/5afe1e5942ba586ef3165e220a588a7…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2020-01-14T09:38:54+01:00
pv: new transformations s.encode.base64t and s.decode.base64t
- base64 encode/decode with optional trailing padding
---
Modified: src/modules/pv/pv_trans.c
Modified: src/modules/pv/pv_trans.h
---
Diff:
https://github.com/kamailio/kamailio/commit/5afe1e5942ba586ef3165e220a588a7…
Patch:
https://github.com/kamailio/kamailio/commit/5afe1e5942ba586ef3165e220a588a7…
---
diff --git a/src/modules/pv/pv_trans.c b/src/modules/pv/pv_trans.c
index 547ba60705..354a51795c 100644
--- a/src/modules/pv/pv_trans.c
+++ b/src/modules/pv/pv_trans.c
@@ -465,6 +465,55 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
val->rs.s = _tr_buffer;
val->rs.len = i;
break;
+ case TR_S_ENCODEBASE64T:
+ if(!(val->flags&PV_VAL_STR))
+ val->rs.s = int2str(val->ri, &val->rs.len);
+ i = base64_enc((unsigned char *) val->rs.s, val->rs.len,
+ (unsigned char *) _tr_buffer, TR_BUFFER_SIZE-1);
+ if (i < 0)
+ return -1;
+ if (i>1 && _tr_buffer[i-1] == '=') {
+ i--;
+ if (i>1 && _tr_buffer[i-1] == '=') {
+ i--;
+ }
+ }
+ _tr_buffer[i] = '\0';
+ memset(val, 0, sizeof(pv_value_t));
+ val->flags = PV_VAL_STR;
+ val->rs.s = _tr_buffer;
+ val->rs.len = i;
+ break;
+ case TR_S_DECODEBASE64T:
+ if(!(val->flags&PV_VAL_STR))
+ val->rs.s = int2str(val->ri, &val->rs.len);
+ if(val->rs.len % 4) {
+ if(val->rs.len + 4 >= TR_BUFFER_SIZE-1) {
+ LM_ERR("not enough space to insert padding\n");
+ return -1;
+ }
+ memcpy(_tr_buffer, val->rs.s, val->rs.len);
+ for(i=0; i < (4 - (val->rs.len % 4)); i++) {
+ _tr_buffer[val->rs.len + i] = '=';
+ }
+ st.s = _tr_buffer;
+ st.len = val->rs.len + (4 - (val->rs.len % 4));
+ /* move to next buffer */
+ tr_set_crt_buffer();
+ i = base64_dec((unsigned char *) st.s, st.len,
+ (unsigned char *) _tr_buffer, TR_BUFFER_SIZE-1);
+ } else {
+ i = base64_dec((unsigned char *) val->rs.s, val->rs.len,
+ (unsigned char *) _tr_buffer, TR_BUFFER_SIZE-1);
+ }
+ if (i < 0 || (i == 0 && val->rs.len > 0))
+ return -1;
+ _tr_buffer[i] = '\0';
+ memset(val, 0, sizeof(pv_value_t));
+ val->flags = PV_VAL_STR;
+ val->rs.s = _tr_buffer;
+ val->rs.len = i;
+ break;
case TR_S_ESCAPECOMMON:
if(!(val->flags&PV_VAL_STR))
val->rs.s = int2str(val->ri, &val->rs.len);
@@ -2315,6 +2364,12 @@ char* tr_parse_string(str* in, trans_t *t)
} else if(name.len==13 && strncasecmp(name.s, "decode.base64", 13)==0)
{
t->subtype = TR_S_DECODEBASE64;
goto done;
+ } else if(name.len==14 && strncasecmp(name.s, "encode.base64t",
14)==0) {
+ t->subtype = TR_S_ENCODEBASE64T;
+ goto done;
+ } else if(name.len==14 && strncasecmp(name.s, "decode.base64t",
14)==0) {
+ t->subtype = TR_S_DECODEBASE64T;
+ goto done;
} else if(name.len==13 && strncasecmp(name.s, "escape.common", 13)==0)
{
t->subtype = TR_S_ESCAPECOMMON;
goto done;
diff --git a/src/modules/pv/pv_trans.h b/src/modules/pv/pv_trans.h
index 0b579e4b27..88cb9eb321 100644
--- a/src/modules/pv/pv_trans.h
+++ b/src/modules/pv/pv_trans.h
@@ -13,8 +13,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
@@ -42,7 +42,7 @@ enum _tr_s_subtype {
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_COREHASH, TR_S_UNQUOTE,
- TR_S_UNBRACKET, TR_S_COUNT
+ TR_S_UNBRACKET, TR_S_COUNT, TR_S_ENCODEBASE64T, TR_S_DECODEBASE64T
};
enum _tr_uri_subtype {
TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,