changeset 967:6bf473f77fc4

fcup-smsend: support backslash escapes in UCS-2 mode too
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 01 Sep 2023 16:43:35 +0000
parents ec7e23d5151f
children ec736c59845c
files uptools/atcmd/smsend_main.c uptools/atcmd/smsend_multmain.c uptools/libcoding/utf8_decode2.c
diffstat 3 files changed, 52 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/uptools/atcmd/smsend_main.c	Fri Sep 01 15:44:52 2023 +0000
+++ b/uptools/atcmd/smsend_main.c	Fri Sep 01 16:43:35 2023 +0000
@@ -82,11 +82,6 @@
 			argv[0]);
 		exit(ERROR_USAGE);
 	}
-	if (ucs2_mode && allow_escape) {
-		fprintf(stderr, "%s error: UCS-2 escapes not supported yet\n",
-			argv[0]);
-		exit(ERROR_USAGE);
-	}
 	if (argc > optind + 2) {
 		fprintf(stderr, "usage: %s [options] dest-addr [message]\n",
 			argv[0]);
@@ -257,7 +252,8 @@
 	u_char udh[5];
 	unsigned pos, remain, chunk;
 
-	rc = utf8_to_ucs2(msgtext, msgtext_uni, MAX_MSG_UNI, &msgtext_unilen);
+	rc = utf8_to_ucs2(msgtext, msgtext_uni, MAX_MSG_UNI, &msgtext_unilen,
+			  allow_escape);
 	if (rc == -1) {
 		fprintf(stderr, "error: invalid UTF-8 message\n");
 		exit(ERROR_USAGE);
@@ -266,6 +262,11 @@
 		fprintf(stderr, "error: message too long for max concat SMS\n");
 		exit(ERROR_USAGE);
 	}
+	if (rc == -3) {
+		fprintf(stderr,
+			"error: message contains invalid backslash escape\n");
+		exit(ERROR_USAGE);
+	}
 	if (msgtext_unilen <= 70) {
 		common_init();
 		prep_for_pdu_mode();
--- a/uptools/atcmd/smsend_multmain.c	Fri Sep 01 15:44:52 2023 +0000
+++ b/uptools/atcmd/smsend_multmain.c	Fri Sep 01 16:43:35 2023 +0000
@@ -162,7 +162,7 @@
 	unsigned msgtext_unilen;
 	int rc;
 
-	rc = utf8_to_ucs2(msgtext, msgtext_uni, 70, &msgtext_unilen);
+	rc = utf8_to_ucs2(msgtext, msgtext_uni, 70, &msgtext_unilen, 0);
 	if (rc == -1) {
 		fprintf(stderr, "input line %d: invalid UTF-8 message\n",
 			lineno);
--- a/uptools/libcoding/utf8_decode2.c	Fri Sep 01 15:44:52 2023 +0000
+++ b/uptools/libcoding/utf8_decode2.c	Fri Sep 01 16:43:35 2023 +0000
@@ -4,8 +4,44 @@
  */
 
 #include <sys/types.h>
+#include <ctype.h>
 
-utf8_to_ucs2(inbuf, outbuf, outmax, outlenp)
+static int
+handle_escape(ipp, outp)
+	u_char **ipp;
+	unsigned *outp;
+{
+	unsigned c, n, acc;
+
+	c = *(*ipp)++;
+	switch (c) {
+	case '"':
+	case '\\':
+		*outp = c;
+		return(0);
+	case 'n':
+		*outp = '\n';
+		return(0);
+	case 'r':
+		*outp = '\r';
+		return(0);
+	case 'u':
+		acc = 0;
+		for (n = 0; n < 4; n++) {
+			c = *(*ipp)++;
+			if (!isxdigit(c))
+				return(-3);
+			acc <<= 4;
+			acc |= decode_hex_digit(c);
+		}
+		*outp = acc;
+		return(0);
+	default:
+		return(-3);
+	}
+}
+
+utf8_to_ucs2(inbuf, outbuf, outmax, outlenp, allow_escape)
 	u_char *inbuf;
 	u_short *outbuf;
 	unsigned outmax, *outlenp;
@@ -13,8 +49,15 @@
 	u_char *ip = inbuf;
 	u_short *op = outbuf;
 	unsigned outcnt = 0, c, n, uni;
+	int rc;
 
 	while (c = *ip++) {
+		if (c == '\\' && allow_escape) {
+			rc = handle_escape(&ip, &uni);
+			if (rc < 0)
+				return(rc);
+			goto gotuni;
+		}
 		if (c < 0x80) {
 			uni = c;
 			goto gotuni;