changeset 18:2ef261371347

alpha tag from file parsing functions factored out of pb-update
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 03:21:39 +0000
parents b8d27c72747a
children 72a24b8538eb
files libcommon/Makefile libcommon/alpha_fromfile.c simtool/pbupd_file.c
diffstat 3 files changed, 110 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/libcommon/Makefile	Fri Feb 12 02:54:17 2021 +0000
+++ b/libcommon/Makefile	Fri Feb 12 03:21:39 2021 +0000
@@ -1,9 +1,9 @@
 CC=	gcc
 CFLAGS=	-O2 -I/usr/include/PCSC
-OBJS=	alpha_decode.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 number_decode.o number_encode.o pbdumpfunc.o\
-	pinentry.o revnibbles.o
+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 \
+	number_decode.o number_encode.o pbdumpfunc.o pinentry.o revnibbles.o
 LIB=	libcommon.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcommon/alpha_fromfile.c	Fri Feb 12 03:21:39 2021 +0000
@@ -0,0 +1,99 @@
+/*
+ * This module implements functions for parsing alpha tag strings
+ * from input data files, to be used by commands like pb-update
+ * and smsp-restore.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+
+extern u_char gsm7_encode_table[256];
+
+char *
+alpha_from_file_qstring(cp, record, maxlen, filename_for_errs, lineno_for_errs)
+	char *cp, *filename_for_errs;
+	u_char *record;
+	unsigned maxlen;
+{
+	unsigned acclen, nadd;
+	int c;
+
+	for (acclen = 0; ; ) {
+		if (*cp == '\0') {
+unterm_qstring:		fprintf(stderr,
+				"%s line %d: unterminated quoted string\n",
+				filename_for_errs, lineno_for_errs);
+			return(0);
+		}
+		if (*cp == '"')
+			break;
+		c = *cp++;
+		if (c == '\\') {
+			if (*cp == '\0')
+				goto unterm_qstring;
+			c = *cp++;
+			switch (c) {
+			case 'n':
+				c = '\n';
+				break;
+			case 'r':
+				c = '\r';
+				break;
+			case '"':
+			case '\\':
+				break;
+			default:
+				fprintf(stderr,
+				"%s line %d: non-understood backslash escape\n",
+					filename_for_errs, lineno_for_errs);
+				return(0);
+			}
+		}
+		c = gsm7_encode_table[c];
+		if (c == 0xFF) {
+			fprintf(stderr,
+	"%s line %d: character in quoted string cannot be encoded in GSM7\n",
+				filename_for_errs, lineno_for_errs);
+			return(0);
+		}
+		if (c & 0x80)
+			nadd = 2;
+		else
+			nadd = 1;
+		if (acclen + nadd > maxlen) {
+			fprintf(stderr,
+		"%s line %d: alpha tag string is longer than SIM limit\n",
+				filename_for_errs, lineno_for_errs);
+			return(0);
+		}
+		if (c & 0x80)
+			record[acclen++] = 0x1B;
+		record[acclen++] = c & 0x7F;
+	}
+	return(cp + 1);
+}
+
+char *
+alpha_from_file_hex(cp, record, maxlen, filename_for_errs, lineno_for_errs)
+	char *cp, *filename_for_errs;
+	u_char *record;
+	unsigned maxlen;
+{
+	unsigned acclen;
+
+	for (acclen = 0; ; ) {
+		if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
+			break;
+		if (acclen >= maxlen) {
+			fprintf(stderr,
+		"%s line %d: alpha tag string is longer than SIM limit\n",
+				filename_for_errs, lineno_for_errs);
+			return(0);
+		}
+		record[acclen++] = (decode_hex_digit(cp[0]) << 4) |
+				    decode_hex_digit(cp[1]);
+		cp += 2;
+	}
+	return(cp);
+}
--- a/simtool/pbupd_file.c	Fri Feb 12 02:54:17 2021 +0000
+++ b/simtool/pbupd_file.c	Fri Feb 12 03:21:39 2021 +0000
@@ -10,95 +10,8 @@
 #include <stdlib.h>
 #include "curfile.h"
 
-extern u_char gsm7_encode_table[256];
-
-static char *
-decode_qstring_alpha(cp, record, maxlen, filename_for_errs, lineno_for_errs)
-	char *cp, *filename_for_errs;
-	u_char *record;
-	unsigned maxlen;
-{
-	unsigned acclen, nadd;
-	int c;
-
-	for (acclen = 0; ; ) {
-		if (*cp == '\0') {
-unterm_qstring:		fprintf(stderr,
-				"%s line %d: unterminated quoted string\n",
-				filename_for_errs, lineno_for_errs);
-			return(0);
-		}
-		if (*cp == '"')
-			break;
-		c = *cp++;
-		if (c == '\\') {
-			if (*cp == '\0')
-				goto unterm_qstring;
-			c = *cp++;
-			switch (c) {
-			case 'n':
-				c = '\n';
-				break;
-			case 'r':
-				c = '\r';
-				break;
-			case '"':
-			case '\\':
-				break;
-			default:
-				fprintf(stderr,
-				"%s line %d: non-understood backslash escape\n",
-					filename_for_errs, lineno_for_errs);
-				return(0);
-			}
-		}
-		c = gsm7_encode_table[c];
-		if (c == 0xFF) {
-			fprintf(stderr,
-	"%s line %d: character in quoted string cannot be encoded in GSM7\n",
-				filename_for_errs, lineno_for_errs);
-			return(0);
-		}
-		if (c & 0x80)
-			nadd = 2;
-		else
-			nadd = 1;
-		if (acclen + nadd > maxlen) {
-			fprintf(stderr,
-		"%s line %d: alpha tag string is longer than SIM limit\n",
-				filename_for_errs, lineno_for_errs);
-			return(0);
-		}
-		if (c & 0x80)
-			record[acclen++] = 0x1B;
-		record[acclen++] = c & 0x7F;
-	}
-	return(cp + 1);
-}
-
-static char *
-decode_hex_alpha(cp, record, maxlen, filename_for_errs, lineno_for_errs)
-	char *cp, *filename_for_errs;
-	u_char *record;
-	unsigned maxlen;
-{
-	unsigned acclen;
-
-	for (acclen = 0; ; ) {
-		if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
-			break;
-		if (acclen >= maxlen) {
-			fprintf(stderr,
-		"%s line %d: alpha tag string is longer than SIM limit\n",
-				filename_for_errs, lineno_for_errs);
-			return(0);
-		}
-		record[acclen++] = (decode_hex_digit(cp[0]) << 4) |
-				    decode_hex_digit(cp[1]);
-		cp += 2;
-	}
-	return(cp);
-}
+extern char *alpha_from_file_qstring();
+extern char *alpha_from_file_hex();
 
 static
 process_record(line, pb_record_len, pb_record_count, filename_for_errs,
@@ -177,16 +90,17 @@
 	}
 	if (*cp == '"') {
 		cp++;
-		cp = decode_qstring_alpha(cp, record, pb_record_len - 14,
-					  filename_for_errs, lineno_for_errs);
+		cp = alpha_from_file_qstring(cp, record, pb_record_len - 14,
+					     filename_for_errs,
+					     lineno_for_errs);
 		if (!cp)
 			return(-1);
 	} else if (!strncasecmp(cp, "HEX", 3)) {
 		cp += 3;
 		while (isspace(*cp))
 			cp++;
-		cp = decode_hex_alpha(cp, record, pb_record_len - 14,
-				      filename_for_errs, lineno_for_errs);
+		cp = alpha_from_file_hex(cp, record, pb_record_len - 14,
+					 filename_for_errs, lineno_for_errs);
 		if (!cp)
 			return(-1);
 	} else