diff uptools/libcoding/utf8_decode2.c @ 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 83c755829e31
children
line wrap: on
line diff
--- 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;