changeset 150:54e33e9238b6

fc-simtool: harmonize hex string code with fc-uicc-tool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Feb 2021 00:06:50 +0000
parents 451ed3bbfe96
children d515cfbb3f39
files simtool/Makefile simtool/hexread.c simtool/hexstr.c simtool/writecmd.c
diffstat 4 files changed, 56 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/Makefile	Sun Feb 07 00:01:55 2021 +0000
+++ b/simtool/Makefile	Sun Feb 07 00:06:50 2021 +0000
@@ -3,9 +3,9 @@
 PROG=	fc-simtool
 OBJS=	a38.o alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o chv.o \
 	dispatch.o dumpdir.o exit.o globals.o grcard1.o hexdump.o hexread.o \
-	hlread.o main.o names.o pbcommon.o pbdump.o pberase.o pbupdate.o \
-	readcmd.o readops.o saverestore.o script.o select.o sysmo.o telsum.o \
-	writecmd.o writeops.o
+	hexstr.o hlread.o main.o names.o pbcommon.o pbdump.o pberase.o \
+	pbupdate.o readcmd.o readops.o saverestore.o script.o select.o sysmo.o \
+	telsum.o writecmd.o writeops.o
 INSTBIN=/opt/freecalypso/bin
 
 all:	${PROG}
--- a/simtool/hexread.c	Sun Feb 07 00:01:55 2021 +0000
+++ b/simtool/hexread.c	Sun Feb 07 00:06:50 2021 +0000
@@ -10,17 +10,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-decode_hex_digit(c)
-{
-	if (c >= '0' && c <= '9')
-		return(c - '0');
-	if (c >= 'A' && c <= 'F')
-		return(c - 'A' + 10);
-	if (c >= 'a' && c <= 'f')
-		return(c - 'a' + 10);
-	return(-1);
-}
-
 read_hex_data_file(filename, databuf)
 	char *filename;
 	u_char *databuf;
@@ -65,33 +54,3 @@
 	}
 	return(count);
 }
-
-decode_hex_data_from_string(arg, databuf)
-	char *arg;
-	u_char *databuf;
-{
-	unsigned count;
-
-	for (count = 0; ; count++) {
-		while (isspace(*arg))
-			arg++;
-		if (!*arg)
-			break;
-		if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
-			fprintf(stderr, "error: invalid hex string input\n");
-			return(-1);
-		}
-		if (count >= 255) {
-			fprintf(stderr, "error: hex input data is too long\n");
-			return(-1);
-		}
-		databuf[count] = (decode_hex_digit(arg[0]) << 4) |
-				 decode_hex_digit(arg[1]);
-		arg += 2;
-	}
-	if (!count) {
-		fprintf(stderr, "error: empty hex string argument\n");
-		return(-1);
-	}
-	return(count);
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/hexstr.c	Sun Feb 07 00:06:50 2021 +0000
@@ -0,0 +1,52 @@
+/*
+ * This module contains the function for decoding hex strings.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+decode_hex_digit(c)
+{
+	if (c >= '0' && c <= '9')
+		return(c - '0');
+	if (c >= 'A' && c <= 'F')
+		return(c - 'A' + 10);
+	if (c >= 'a' && c <= 'f')
+		return(c - 'a' + 10);
+	return(-1);
+}
+
+decode_hex_data_from_string(arg, databuf, maxlen)
+	char *arg;
+	u_char *databuf;
+	unsigned maxlen;
+{
+	unsigned count;
+
+	for (count = 0; ; count++) {
+		while (isspace(*arg))
+			arg++;
+		if (!*arg)
+			break;
+		if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
+			fprintf(stderr, "error: invalid hex string input\n");
+			return(-1);
+		}
+		if (count >= maxlen) {
+			fprintf(stderr, "error: hex input data is too long\n");
+			return(-1);
+		}
+		databuf[count] = (decode_hex_digit(arg[0]) << 4) |
+				 decode_hex_digit(arg[1]);
+		arg += 2;
+	}
+	if (!count) {
+		fprintf(stderr, "error: empty hex string argument\n");
+		return(-1);
+	}
+	return(count);
+}
--- a/simtool/writecmd.c	Sun Feb 07 00:01:55 2021 +0000
+++ b/simtool/writecmd.c	Sun Feb 07 00:06:50 2021 +0000
@@ -36,7 +36,7 @@
 		fprintf(stderr, "error: offset argument is out of range\n");
 		return(-1);
 	}
-	rc = decode_hex_data_from_string(argv[2], data);
+	rc = decode_hex_data_from_string(argv[2], data, 255);
 	if (rc < 0)
 		return(rc);
 	len = rc;