diff serial/hexinput.c @ 43:be27d1c85861

serial: main function implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 Mar 2021 21:49:59 +0000
parents pcsc/main.c@60fd23186e2e
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serial/hexinput.c	Sat Mar 20 21:49:59 2021 +0000
@@ -0,0 +1,42 @@
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+
+static
+decode_hex_digit(c)
+{
+	if (isdigit(c))
+		return c - '0';
+	else if (islower(c))
+		return c - 'a' + 10;
+	else
+		return c - 'A' + 10;
+}
+
+parse_hex_input(inbuf, outbuf)
+	char *inbuf;
+	u_char *outbuf;
+{
+	char *cp;
+	unsigned count;
+
+	count = 0;
+	for (cp = inbuf; ; ) {
+		while (isspace(*cp))
+			cp++;
+		if (!*cp)
+			break;
+		if (!isxdigit(cp[0]) || !isxdigit(cp[1])) {
+			printf("error: invalid hex APDU input\n");
+			return(-1);
+		}
+		if (count >= 260) {
+			printf("error: command APDU is too long\n");
+			return(-1);
+		}
+		outbuf[count++] = (decode_hex_digit(cp[0]) << 4) |
+				  decode_hex_digit(cp[1]);
+		cp += 2;
+	}
+	return count;
+}