FreeCalypso > hg > themwi-ota-tools
diff libutil/hexstdin.c @ 1:b149db92cb0e
libutil started
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 21 Feb 2021 19:08:14 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/hexstdin.c Sun Feb 21 19:08:14 2021 +0000 @@ -0,0 +1,42 @@ +/* + * 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); +}