diff libcommon/gsm7_encode.c @ 17:b8d27c72747a

GSM7 encoding function factored out of pb-update-imm
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 02:54:17 +0000
parents simtool/pbupd_imm.c@d4f8c511affe
children 90e7020df08a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcommon/gsm7_encode.c	Fri Feb 12 02:54:17 2021 +0000
@@ -0,0 +1,68 @@
+/*
+ * This module implements functions for parsing quoted string
+ * arguments intended for GSM7 string encoding, and actually
+ * encoding them into GSM7 binary strings.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+
+extern u_char gsm7_encode_table[256];
+
+qstring_arg_to_gsm7(arg, record, maxlen)
+	char *arg;
+	u_char *record;
+	unsigned maxlen;
+{
+	unsigned acclen, nadd;
+	char *cp;
+	int c;
+
+	cp = arg;
+	for (acclen = 0; *cp; ) {
+		c = *cp++;
+		if (c == '\\') {
+			if (*cp == '\0') {
+				fprintf(stderr,
+					"error: dangling backslash escape\n");
+				return(-1);
+			}
+			c = *cp++;
+			switch (c) {
+			case 'n':
+				c = '\n';
+				break;
+			case 'r':
+				c = '\r';
+				break;
+			case '"':
+			case '\\':
+				break;
+			default:
+				fprintf(stderr,
+				"error: non-understood backslash escape\n");
+				return(-1);
+			}
+		}
+		c = gsm7_encode_table[c];
+		if (c == 0xFF) {
+			fprintf(stderr,
+	"error: character in alpha tag string cannot be encoded in GSM7\n");
+			return(-1);
+		}
+		if (c & 0x80)
+			nadd = 2;
+		else
+			nadd = 1;
+		if (acclen + nadd > maxlen) {
+			fprintf(stderr,
+			"error: alpha tag string is longer than SIM limit\n");
+			return(-1);
+		}
+		if (c & 0x80)
+			record[acclen++] = 0x1B;
+		record[acclen++] = c & 0x7F;
+	}
+	return(acclen);
+}