Browse code

pv: implement pv transformation {en,de}code.base64

Richard Fuchs authored on 09/04/2013 20:38:22
Showing 2 changed files
... ...
@@ -39,6 +39,7 @@
39 39
 #include "../../trim.h" 
40 40
 #include "../../pvapi.h"
41 41
 #include "../../dset.h"
42
+#include "../../basex.h"
42 43
 
43 44
 #include "../../parser/parse_param.h"
44 45
 #include "../../parser/parse_uri.h"
... ...
@@ -195,6 +196,32 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
195 196
 			val->rs.s = _tr_buffer;
196 197
 			val->rs.len = i;
197 198
 			break;
199
+		case TR_S_ENCODEBASE64:
200
+			if(!(val->flags&PV_VAL_STR))
201
+				val->rs.s = int2str(val->ri, &val->rs.len);
202
+			i = base64_enc((unsigned char *) val->rs.s, val->rs.len,
203
+					(unsigned char *) _tr_buffer, TR_BUFFER_SIZE-1);
204
+			if (i < 0)
205
+				return -1;
206
+			_tr_buffer[i] = '\0';
207
+			memset(val, 0, sizeof(pv_value_t));
208
+			val->flags = PV_VAL_STR;
209
+			val->rs.s = _tr_buffer;
210
+			val->rs.len = i;
211
+			break;
212
+		case TR_S_DECODEBASE64:
213
+			if(!(val->flags&PV_VAL_STR))
214
+				val->rs.s = int2str(val->ri, &val->rs.len);
215
+			i = base64_dec((unsigned char *) val->rs.s, val->rs.len,
216
+					(unsigned char *) _tr_buffer, TR_BUFFER_SIZE-1);
217
+			if (i < 0 || (i == 0 && val->rs.len > 0))
218
+				return -1;
219
+			_tr_buffer[i] = '\0';
220
+			memset(val, 0, sizeof(pv_value_t));
221
+			val->flags = PV_VAL_STR;
222
+			val->rs.s = _tr_buffer;
223
+			val->rs.len = i;
224
+			break;
198 225
 		case TR_S_ESCAPECOMMON:
199 226
 			if(!(val->flags&PV_VAL_STR))
200 227
 				val->rs.s = int2str(val->ri, &val->rs.len);
... ...
@@ -1749,6 +1776,12 @@ char* tr_parse_string(str* in, trans_t *t)
1749 1776
 	} else if(name.len==11 && strncasecmp(name.s, "decode.hexa", 11)==0) {
1750 1777
 		t->subtype = TR_S_DECODEHEXA;
1751 1778
 		goto done;
1779
+	} else if(name.len==13 && strncasecmp(name.s, "encode.base64", 13)==0) {
1780
+		t->subtype = TR_S_ENCODEBASE64;
1781
+		goto done;
1782
+	} else if(name.len==13 && strncasecmp(name.s, "decode.base64", 13)==0) {
1783
+		t->subtype = TR_S_DECODEBASE64;
1784
+		goto done;
1752 1785
 	} else if(name.len==13 && strncasecmp(name.s, "escape.common", 13)==0) {
1753 1786
 		t->subtype = TR_S_ESCAPECOMMON;
1754 1787
 		goto done;
... ...
@@ -37,6 +37,7 @@ enum _tr_type { TR_NONE=0, TR_STRING, TR_URI, TR_PARAMLIST, TR_NAMEADDR,
37 37
 enum _tr_s_subtype { 
38 38
 	TR_S_NONE=0, TR_S_LEN, TR_S_INT, TR_S_MD5, TR_S_SUBSTR,
39 39
 	TR_S_SELECT, TR_S_ENCODEHEXA, TR_S_DECODEHEXA,
40
+	TR_S_ENCODEBASE64, TR_S_DECODEBASE64,
40 41
 	TR_S_ESCAPECOMMON, TR_S_UNESCAPECOMMON, TR_S_ESCAPEUSER, TR_S_UNESCAPEUSER,
41 42
 	TR_S_ESCAPEPARAM, TR_S_UNESCAPEPARAM, TR_S_TOLOWER, TR_S_TOUPPER,
42 43
 	TR_S_STRIP, TR_S_STRIPTAIL, TR_S_PREFIXES, TR_S_PREFIXES_QUOT, TR_S_REPLACE,