diff libutil/hexread.c @ 157:f064dbcc5f41

libutil split from libcommon
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 26 Feb 2021 20:19:58 +0000
parents libcommon/hexread.c@a1aa8ee2da85
children fc1635333d81
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libutil/hexread.c	Fri Feb 26 20:19:58 2021 +0000
@@ -0,0 +1,57 @@
+/*
+ * This module contains the function for reading hex files,
+ * to be used in the implementation of manual write commands.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+extern FILE *open_script_input_file();
+
+read_hex_data_file(filename, databuf, maxlen)
+	char *filename;
+	u_char *databuf;
+	unsigned maxlen;
+{
+	FILE *inf;
+	unsigned count;
+	int c, c2;
+
+	inf = open_script_input_file(filename);
+	if (!inf) {
+		perror(filename);
+		return(-1);
+	}
+	for (count = 0; ; count++) {
+		do
+			c = getc(inf);
+		while (isspace(c));
+		if (c < 0)
+			break;
+		if (!isxdigit(c)) {
+inv_input:		fprintf(stderr, "%s: invalid hex file input\n",
+				filename);
+			fclose(inf);
+			return(-1);
+		}
+		c2 = getc(inf);
+		if (!isxdigit(c2))
+			goto inv_input;
+		if (count >= maxlen) {
+			fprintf(stderr, "%s: hex input data is too long\n",
+				filename);
+			fclose(inf);
+			return(-1);
+		}
+		databuf[count] = (decode_hex_digit(c) << 4) |
+				 decode_hex_digit(c2);
+	}
+	fclose(inf);
+	if (!count) {
+		fprintf(stderr, "%s: no hex data input found\n", filename);
+		return(-1);
+	}
+	return(count);
+}