Browse code

core: relocate str json escape function to strutils

Daniel-Constantin Mierla authored on 17/11/2021 11:49:56
Showing 3 changed files
... ...
@@ -40,6 +40,7 @@
40 40
 #include "globals.h"
41 41
 #include "dprint.h"
42 42
 #include "pvar.h"
43
+#include "strutils.h"
43 44
 
44 45
 static void log_callid_set(sip_msg_t *msg);
45 46
 
... ...
@@ -538,83 +539,6 @@ static int _ksr_slog_json_flags = 0;
538 539
 #define LOGV_CALLID_LEN (((_ksr_slog_json_flags & KSR_SLOGJSON_FL_CALLID) \
539 540
 			&& (log_callid_str.len>0))?log_callid_str.len:0)
540 541
 
541
-static void ksr_slog_json_str_escape(str *s_in, str *s_out, int *emode)
542
-{
543
-	char *p1, *p2;
544
-	int len = 0;
545
-	char token;
546
-	int i;
547
-
548
-	s_out->len = 0;
549
-	if (!s_in || !s_in->s) {
550
-		s_out->s = strdup("");
551
-		*emode = 1;
552
-		return;
553
-	}
554
-	for(i = 0; i < s_in->len; i++) {
555
-		if (strchr("\"\\\b\f\n\r\t", s_in->s[i])) {
556
-			len += 2;
557
-		} else if (s_in->s[i] < 32) {
558
-			len += 6;
559
-		} else {
560
-			len++;
561
-		}
562
-	}
563
-	if(len == s_in->len) {
564
-		s_out->s = s_in->s;
565
-		s_out->len = s_in->len;
566
-		*emode = 0;
567
-		return;
568
-	}
569
-
570
-	s_out->s = (char*)malloc(len + 2);
571
-	if (!s_out->s) {
572
-		return;
573
-	}
574
-	*emode = 1;
575
-
576
-	p2 = s_out->s;
577
-	p1 = s_in->s;
578
-	while (p1 < s_in->s + s_in->len) {
579
-		if ((unsigned char) *p1 > 31 && *p1 != '\"' && *p1 != '\\') {
580
-			*p2++ = *p1++;
581
-		} else {
582
-			*p2++ = '\\';
583
-			switch (token = *p1++) {
584
-			case '\\':
585
-				*p2++ = '\\';
586
-				break;
587
-			case '\"':
588
-				*p2++ = '\"';
589
-				break;
590
-			case '\b':
591
-				*p2++ = 'b';
592
-				break;
593
-			case '\f':
594
-				*p2++ = 'f';
595
-				break;
596
-			case '\n':
597
-				*p2++ = 'n';
598
-				break;
599
-			case '\r':
600
-				*p2++ = 'r';
601
-				break;
602
-			case '\t':
603
-				*p2++ = 't';
604
-				break;
605
-			default:
606
-				/* escape and print */
607
-				snprintf(p2, 6, "u%04x", token);
608
-				p2 += 5;
609
-				break;
610
-			}
611
-		}
612
-	}
613
-	*p2++ = 0;
614
-	s_out->len = len;
615
-	return;
616
-}
617
-
618 542
 #define KSR_SLOG_SYSLOG_JSON_FMT "{ \"level\": \"%s\", \"module\": \"%s\", \"file\": \"%s\"," \
619 543
 	" \"line\": %d, \"function\": \"%s\"%.*s%s%s%.*s%s, \"%smessage\": %s%.*s%s }%s"
620 544
 
... ...
@@ -717,7 +641,7 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
717 641
 	}
718 642
 
719 643
 	if(s_out.s == NULL) {
720
-		ksr_slog_json_str_escape(&s_in, &s_out, &emode);
644
+		ksr_str_json_escape(&s_in, &s_out, &emode);
721 645
 		if(s_out.s == NULL) {
722 646
 			goto error;
723 647
 		}
... ...
@@ -20,6 +20,7 @@
20 20
 
21 21
 #include <sys/types.h>
22 22
 #include <string.h>
23
+#include <stdlib.h>
23 24
 #include <regex.h>
24 25
 #include <ctype.h>
25 26
 
... ...
@@ -874,3 +875,83 @@ int urldecode(str *sin, str *sout)
874 875
 	LM_DBG("urldecoded string is <%s>\n", sout->s);
875 876
 	return 0;
876 877
 }
878
+
879
+/*! \brief
880
+ *  escape input string to prepare it for use as json value
881
+ */
882
+void ksr_str_json_escape(str *s_in, str *s_out, int *emode)
883
+{
884
+	char *p1, *p2;
885
+	int len = 0;
886
+	char token;
887
+	int i;
888
+
889
+	s_out->len = 0;
890
+	if (!s_in || !s_in->s) {
891
+		s_out->s = strdup("");
892
+		*emode = 1;
893
+		return;
894
+	}
895
+	for(i = 0; i < s_in->len; i++) {
896
+		if (strchr("\"\\\b\f\n\r\t", s_in->s[i])) {
897
+			len += 2;
898
+		} else if (s_in->s[i] < 32) {
899
+			len += 6;
900
+		} else {
901
+			len++;
902
+		}
903
+	}
904
+	if(len == s_in->len) {
905
+		s_out->s = s_in->s;
906
+		s_out->len = s_in->len;
907
+		*emode = 0;
908
+		return;
909
+	}
910
+
911
+	s_out->s = (char*)malloc(len + 2);
912
+	if (!s_out->s) {
913
+		return;
914
+	}
915
+	*emode = 1;
916
+
917
+	p2 = s_out->s;
918
+	p1 = s_in->s;
919
+	while (p1 < s_in->s + s_in->len) {
920
+		if ((unsigned char) *p1 > 31 && *p1 != '\"' && *p1 != '\\') {
921
+			*p2++ = *p1++;
922
+		} else {
923
+			*p2++ = '\\';
924
+			switch (token = *p1++) {
925
+			case '\\':
926
+				*p2++ = '\\';
927
+				break;
928
+			case '\"':
929
+				*p2++ = '\"';
930
+				break;
931
+			case '\b':
932
+				*p2++ = 'b';
933
+				break;
934
+			case '\f':
935
+				*p2++ = 'f';
936
+				break;
937
+			case '\n':
938
+				*p2++ = 'n';
939
+				break;
940
+			case '\r':
941
+				*p2++ = 'r';
942
+				break;
943
+			case '\t':
944
+				*p2++ = 't';
945
+				break;
946
+			default:
947
+				/* escape and print */
948
+				snprintf(p2, 6, "u%04x", token);
949
+				p2 += 5;
950
+				break;
951
+			}
952
+		}
953
+	}
954
+	*p2++ = 0;
955
+	s_out->len = len;
956
+	return;
957
+}
... ...
@@ -66,4 +66,6 @@ char hex_to_char(char hex_code);
66 66
 char char_to_hex(char char_code);
67 67
 int urlencode(str *sin, str *sout);
68 68
 int urldecode(str *sin, str *sout);
69
+
70
+void ksr_str_json_escape(str *s_in, str *s_out, int *emode);
69 71
 #endif