Browse code

pv: new transformations s.encode.base64t and s.decode.base64t

- base64 encode/decode with optional trailing padding

Daniel-Constantin Mierla authored on 14/01/2020 08:38:54
Showing 2 changed files
... ...
@@ -465,6 +465,55 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
465 465
 			val->rs.s = _tr_buffer;
466 466
 			val->rs.len = i;
467 467
 			break;
468
+		case TR_S_ENCODEBASE64T:
469
+			if(!(val->flags&PV_VAL_STR))
470
+				val->rs.s = int2str(val->ri, &val->rs.len);
471
+			i = base64_enc((unsigned char *) val->rs.s, val->rs.len,
472
+					(unsigned char *) _tr_buffer, TR_BUFFER_SIZE-1);
473
+			if (i < 0)
474
+				return -1;
475
+			if (i>1 && _tr_buffer[i-1] == '=') {
476
+				i--;
477
+				if (i>1 && _tr_buffer[i-1] == '=') {
478
+					i--;
479
+				}
480
+			}
481
+			_tr_buffer[i] = '\0';
482
+			memset(val, 0, sizeof(pv_value_t));
483
+			val->flags = PV_VAL_STR;
484
+			val->rs.s = _tr_buffer;
485
+			val->rs.len = i;
486
+			break;
487
+		case TR_S_DECODEBASE64T:
488
+			if(!(val->flags&PV_VAL_STR))
489
+				val->rs.s = int2str(val->ri, &val->rs.len);
490
+			if(val->rs.len % 4) {
491
+				if(val->rs.len + 4 >= TR_BUFFER_SIZE-1) {
492
+					LM_ERR("not enough space to insert padding\n");
493
+					return -1;
494
+				}
495
+				memcpy(_tr_buffer, val->rs.s, val->rs.len);
496
+				for(i=0; i < (4 - (val->rs.len % 4)); i++) {
497
+					_tr_buffer[val->rs.len + i] = '=';
498
+				}
499
+				st.s = _tr_buffer;
500
+				st.len = val->rs.len + (4 - (val->rs.len % 4));
501
+				/* move to next buffer */
502
+				tr_set_crt_buffer();
503
+				i = base64_dec((unsigned char *) st.s, st.len,
504
+						(unsigned char *) _tr_buffer, TR_BUFFER_SIZE-1);
505
+			} else {
506
+				i = base64_dec((unsigned char *) val->rs.s, val->rs.len,
507
+						(unsigned char *) _tr_buffer, TR_BUFFER_SIZE-1);
508
+			}
509
+			if (i < 0 || (i == 0 && val->rs.len > 0))
510
+				return -1;
511
+			_tr_buffer[i] = '\0';
512
+			memset(val, 0, sizeof(pv_value_t));
513
+			val->flags = PV_VAL_STR;
514
+			val->rs.s = _tr_buffer;
515
+			val->rs.len = i;
516
+			break;
468 517
 		case TR_S_ESCAPECOMMON:
469 518
 			if(!(val->flags&PV_VAL_STR))
470 519
 				val->rs.s = int2str(val->ri, &val->rs.len);
... ...
@@ -2315,6 +2364,12 @@ char* tr_parse_string(str* in, trans_t *t)
2315 2364
 	} else if(name.len==13 && strncasecmp(name.s, "decode.base64", 13)==0) {
2316 2365
 		t->subtype = TR_S_DECODEBASE64;
2317 2366
 		goto done;
2367
+	} else if(name.len==14 && strncasecmp(name.s, "encode.base64t", 14)==0) {
2368
+		t->subtype = TR_S_ENCODEBASE64T;
2369
+		goto done;
2370
+	} else if(name.len==14 && strncasecmp(name.s, "decode.base64t", 14)==0) {
2371
+		t->subtype = TR_S_DECODEBASE64T;
2372
+		goto done;
2318 2373
 	} else if(name.len==13 && strncasecmp(name.s, "escape.common", 13)==0) {
2319 2374
 		t->subtype = TR_S_ESCAPECOMMON;
2320 2375
 		goto done;
... ...
@@ -13,8 +13,8 @@
13 13
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
  * GNU General Public License for more details.
15 15
  *
16
- * You should have received a copy of the GNU General Public License 
17
- * along with this program; if not, write to the Free Software 
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program; if not, write to the Free Software
18 18
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 19
  *
20 20
  */
... ...
@@ -42,7 +42,7 @@ enum _tr_s_subtype {
42 42
 	TR_S_TIMEFORMAT, TR_S_TRIM, TR_S_RTRIM, TR_S_LTRIM, TR_S_RM, TR_S_STRIPTO,
43 43
 	TR_S_URLENCODEPARAM, TR_S_URLDECODEPARAM, TR_S_NUMERIC, TR_S_ESCAPECSV,
44 44
 	TR_S_ENCODEBASE58, TR_S_DECODEBASE58, TR_S_COREHASH, TR_S_UNQUOTE,
45
-	TR_S_UNBRACKET, TR_S_COUNT
45
+	TR_S_UNBRACKET, TR_S_COUNT, TR_S_ENCODEBASE64T, TR_S_DECODEBASE64T
46 46
 };
47 47
 enum _tr_uri_subtype {
48 48
 	TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,