changeset 21:d4dc86195382

GSM7 quoted string output factored out, uses new escapes
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 04:16:35 +0000
parents 90e7020df08a
children 1b1468869ccf
files libcommon/Makefile libcommon/alpha_decode.c libcommon/gsm7_decode.c
diffstat 3 files changed, 92 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- a/libcommon/Makefile	Fri Feb 12 03:44:59 2021 +0000
+++ b/libcommon/Makefile	Fri Feb 12 04:16:35 2021 +0000
@@ -1,8 +1,8 @@
 CC=	gcc
 CFLAGS=	-O2 -I/usr/include/PCSC
 OBJS=	alpha_decode.o alpha_fromfile.o alpha_valid.o apdu.o atr.o \
-	cardconnect.o chkblank.o dumpdirfunc.o exit.o gsm7_encode.o \
-	gsm7_encode_table.o hexdump.o hexread.o hexstr.o names.o \
+	cardconnect.o chkblank.o dumpdirfunc.o exit.o gsm7_decode.o \
+	gsm7_encode.o gsm7_encode_table.o hexdump.o hexread.o hexstr.o names.o \
 	number_decode.o number_encode.o pinentry.o revnibbles.o
 LIB=	libcommon.a
 
--- a/libcommon/alpha_decode.c	Fri Feb 12 03:44:59 2021 +0000
+++ b/libcommon/alpha_decode.c	Fri Feb 12 04:16:35 2021 +0000
@@ -4,97 +4,7 @@
  */
 
 #include <sys/types.h>
-#include <string.h>
-#include <strings.h>
 #include <stdio.h>
-#include <stdlib.h>
-
-static char gsm7_decode_table[128] = {
-	'@', 0,   '$',  0,   0,   0,    0,   0,
-	0,   0,   '\n', 0,   0,   '\r', 0,   0,
-	0,   '_', 0,    0,   0,   0,    0,   0,
-	0,   0,   0,    0,   0,   0,    0,   0,
-	' ', '!', '"',  '#', 0,   '%',  '&', 0x27,
-	'(', ')', '*',  '+', ',', '-',  '.', '/',
-	'0', '1', '2',  '3', '4', '5',  '6', '7',
-	'8', '9', ':',  ';', '<', '=',  '>', '?',
-	0,   'A', 'B',  'C', 'D', 'E',  'F', 'G',
-	'H', 'I', 'J',  'K', 'L', 'M',  'N', 'O',
-	'P', 'Q', 'R',  'S', 'T', 'U',  'V', 'W',
-	'X', 'Y', 'Z',  0,   0,   0,    0,   0,
-	0,   'a', 'b',  'c', 'd', 'e',  'f', 'g',
-	'h', 'i', 'j',  'k', 'l', 'm',  'n', 'o',
-	'p', 'q', 'r',  's', 't', 'u',  'v', 'w',
-	'x', 'y', 'z',  0,   0,   0,    0,   0
-};
-
-static char gsm7ext_decode_table[128] = {
-	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
-	0,   0, 0, 0, '^', 0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
-	0,   0, 0, 0, 0,   0, 0, 0, '{', '}', 0, 0, 0,   0,   0,   '\\',
-	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, '[', '~', ']', 0,
-	'|', 0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
-	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
-	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
-	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0
-};
-
-static
-is_gsm7_string_ok(data, nbytes)
-	u_char *data;
-	unsigned nbytes;
-{
-	u_char *dp, *endp;
-	int c;
-
-	dp = data;
-	endp = data + nbytes;
-	while (dp < endp) {
-		c = *dp++;
-		if (c == 0x1B && dp < endp)
-			c = gsm7ext_decode_table[*dp++];
-		else
-			c = gsm7_decode_table[c];
-		if (!c)
-			return(0);
-	}
-	return(1);
-}
-
-static void
-print_alpha_field_gsmdecode(data, nbytes, outf)
-	u_char *data;
-	unsigned nbytes;
-	FILE *outf;
-{
-	u_char *dp, *endp;
-	int c;
-
-	dp = data;
-	endp = data + nbytes;
-	putc('"', outf);
-	while (dp < endp) {
-		c = *dp++;
-		if (c == 0x1B && dp < endp)
-			c = gsm7ext_decode_table[*dp++];
-		else
-			c = gsm7_decode_table[c];
-		if (c == '\n') {
-			putc('\\', outf);
-			putc('n', outf);
-			continue;
-		}
-		if (c == '\r') {
-			putc('\\', outf);
-			putc('r', outf);
-			continue;
-		}
-		if (c == '"' || c == '\\')
-			putc('\\', outf);
-		putc(c, outf);
-	}
-	putc('"', outf);
-}
 
 static void
 print_alpha_field_hex(data, nbytes, outf)
@@ -121,12 +31,8 @@
 		fputs("\"\"", outf);
 		return;
 	}
-	if (data[0] & 0x80) {
+	if (data[0] & 0x80)
 		print_alpha_field_hex(data, nbytes, outf);
-		return;
-	}
-	if (is_gsm7_string_ok(data, nbytes))
-		print_alpha_field_gsmdecode(data, nbytes, outf);
 	else
-		print_alpha_field_hex(data, nbytes, outf);
+		print_gsm7_string_to_file(data, nbytes, outf);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcommon/gsm7_decode.c	Fri Feb 12 04:16:35 2021 +0000
@@ -0,0 +1,88 @@
+/*
+ * This module contains functions for decoding GSM7 strings
+ * that exist in various SIM files.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+
+static char gsm7_decode_table[128] = {
+	'@', 0,   '$',  0,   0,   0,    0,   0,
+	0,   0,   '\n', 0,   0,   '\r', 0,   0,
+	0,   '_', 0,    0,   0,   0,    0,   0,
+	0,   0,   0,    0,   0,   0,    0,   0,
+	' ', '!', '"',  '#', 0,   '%',  '&', 0x27,
+	'(', ')', '*',  '+', ',', '-',  '.', '/',
+	'0', '1', '2',  '3', '4', '5',  '6', '7',
+	'8', '9', ':',  ';', '<', '=',  '>', '?',
+	0,   'A', 'B',  'C', 'D', 'E',  'F', 'G',
+	'H', 'I', 'J',  'K', 'L', 'M',  'N', 'O',
+	'P', 'Q', 'R',  'S', 'T', 'U',  'V', 'W',
+	'X', 'Y', 'Z',  0,   0,   0,    0,   0,
+	0,   'a', 'b',  'c', 'd', 'e',  'f', 'g',
+	'h', 'i', 'j',  'k', 'l', 'm',  'n', 'o',
+	'p', 'q', 'r',  's', 't', 'u',  'v', 'w',
+	'x', 'y', 'z',  0,   0,   0,    0,   0
+};
+
+static char gsm7ext_decode_table[128] = {
+	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
+	0,   0, 0, 0, '^', 0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
+	0,   0, 0, 0, 0,   0, 0, 0, '{', '}', 0, 0, 0,   0,   0,   '\\',
+	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, '[', '~', ']', 0,
+	'|', 0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
+	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
+	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
+	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0
+};
+
+void
+print_gsm7_string_to_file(data, nbytes, outf)
+	u_char *data;
+	unsigned nbytes;
+	FILE *outf;
+{
+	u_char *dp, *endp;
+	int b, c;
+
+	dp = data;
+	endp = data + nbytes;
+	putc('"', outf);
+	while (dp < endp) {
+		b = *dp++;
+		if (b == 0x1B) {
+			if (dp >= endp || *dp == 0x1B || *dp == '\n' ||
+			    *dp == '\r') {
+				putc('\\', outf);
+				putc('e', outf);
+				continue;
+			}
+			b = *dp++;
+			c = gsm7ext_decode_table[b];
+			if (!c) {
+				fprintf(outf, "\\e\\%02X", b);
+				continue;
+			}
+		} else {
+			c = gsm7_decode_table[b];
+			if (!c) {
+				fprintf(outf, "\\%02X", b);
+				continue;
+			}
+		}
+		if (c == '\n') {
+			putc('\\', outf);
+			putc('n', outf);
+			continue;
+		}
+		if (c == '\r') {
+			putc('\\', outf);
+			putc('r', outf);
+			continue;
+		}
+		if (c == '"' || c == '\\')
+			putc('\\', outf);
+		putc(c, outf);
+	}
+	putc('"', outf);
+}