diff libcoding/utf8_decode2.c @ 23:e56bb9f09ff1

sms-encode-text: port over -e option from fcup-smsend
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 02 Sep 2023 19:22:05 +0000
parents 2d0082216916
children
line wrap: on
line diff
--- a/libcoding/utf8_decode2.c	Thu Aug 31 23:03:38 2023 +0000
+++ b/libcoding/utf8_decode2.c	Sat Sep 02 19:22:05 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;