# HG changeset patch # User Mychaela Falconia # Date 1613934494 0 # Node ID b149db92cb0e2d192f095a3419fab7365d834d32 # Parent 0ac4c3314bf2651beb07c56c2ba2bf11fb8f98b9 libutil started diff -r 0ac4c3314bf2 -r b149db92cb0e .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Sun Feb 21 19:08:14 2021 +0000 @@ -0,0 +1,3 @@ +syntax: regexp + +\.[oa]$ diff -r 0ac4c3314bf2 -r b149db92cb0e libutil/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/Makefile Sun Feb 21 19:08:14 2021 +0000 @@ -0,0 +1,13 @@ +CC= gcc +CFLAGS= -O2 +OBJS= hexstdin.o hexstr.o +LIB= libutil.a + +all: ${LIB} + +${LIB}: ${OBJS} + ar rcu $@ ${OBJS} + ranlib $@ + +clean: + rm -f *.[oa] errs diff -r 0ac4c3314bf2 -r b149db92cb0e libutil/hexstdin.c --- /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 +#include +#include +#include + +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); +} diff -r 0ac4c3314bf2 -r b149db92cb0e libutil/hexstr.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/hexstr.c Sun Feb 21 19:08:14 2021 +0000 @@ -0,0 +1,52 @@ +/* + * This module contains the function for decoding hex strings. + */ + +#include +#include +#include +#include +#include +#include + +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, minlen, maxlen) + char *arg; + u_char *databuf; + unsigned minlen, 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 string is too long\n"); + return(-1); + } + databuf[count] = (decode_hex_digit(arg[0]) << 4) | + decode_hex_digit(arg[1]); + arg += 2; + } + if (count < minlen) { + fprintf(stderr, "error: hex string is too short\n"); + return(-1); + } + return(count); +}