view libutil/hexstdin.c @ 11:b4b4a822286c default tip

add INSTALL
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 21 May 2024 01:06:04 +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);
}