diff libcoding/gsm7_encode.c @ 0:2d0082216916

libcoding: beginning with a subset of uptools version
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 05 Aug 2023 00:46:23 +0000
parents
children e56bb9f09ff1
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcoding/gsm7_encode.c	Sat Aug 05 00:46:23 2023 +0000
@@ -0,0 +1,36 @@
+/*
+ * This library module implements the function for encoding from ISO 8859-1
+ * into the GSM 7-bit default alphabet (03.38 or 23.038).
+ */
+
+#include <sys/types.h>
+
+extern u_char gsm7_encode_table[256];
+
+latin1_to_gsm7(inbuf, outbuf, outmax, outlenp)
+	u_char *inbuf, *outbuf;
+	unsigned outmax, *outlenp;
+{
+	u_char *ip = inbuf, *op = outbuf;
+	unsigned outcnt = 0, c, n;
+
+	while (c = *ip++) {
+		c = gsm7_encode_table[c];
+		if (c == 0xFF)
+			return(-1);
+		if (c & 0x80)
+			n = 2;
+		else
+			n = 1;
+		if (outcnt + n > outmax)
+			return(-2);
+		if (c & 0x80) {
+			*op++ = 0x1B;
+			*op++ = c & 0x7F;
+		} else
+			*op++ = c;
+		outcnt += n;
+	}
+	*outlenp = outcnt;
+	return(0);
+}