diff uicc/hexstr.c @ 138:baf5bd698764

fc-uicc-tool: select-aid command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Feb 2021 04:51:08 +0000
parents
children 65a2a96386cd
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/hexstr.c	Thu Feb 04 04:51:08 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);
+}