view libutil/hexstdin.c @ 2:d5e9af482548

ota-smswrap-sjs1 program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Feb 2021 22:21:29 +0000
parents b149db92cb0e
children
line wrap: on
line source

/*
 * This module contains the function for reading hex data from stdin.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

read_hex_from_stdin(databuf, maxlen)
	u_char *databuf;
	unsigned maxlen;
{
	unsigned count;
	int c, c2;

	for (count = 0; ; count++) {
		do
			c = getchar();
		while (isspace(c));
		if (c < 0)
			break;
		if (!isxdigit(c)) {
inv_input:		fprintf(stderr, "error: invalid hex input on stdin\n");
			return(-1);
		}
		c2 = getchar();
		if (!isxdigit(c2))
			goto inv_input;
		if (count >= maxlen) {
			fprintf(stderr, "error: stdin hex data is too long\n");
			return(-1);
		}
		databuf[count] = (decode_hex_digit(c) << 4) |
				 decode_hex_digit(c2);
	}
	if (!count) {
		fprintf(stderr, "error: no hex data given on stdin\n");
		return(-1);
	}
	return(count);
}