# HG changeset patch # User Mychaela Falconia # Date 1614373451 0 # Node ID b86b3f8890ba98983152ad430bcf95667231c859 # Parent 2557012666ea6e3a59921244702a7db5726eca01 libutil: shorthand decimal string parsing implemented diff -r 2557012666ea -r b86b3f8890ba libutil/Makefile --- a/libutil/Makefile Fri Feb 26 20:45:17 2021 +0000 +++ b/libutil/Makefile Fri Feb 26 21:04:11 2021 +0000 @@ -3,7 +3,7 @@ OBJS= alpha_decode.o alpha_fromfile.o alpha_valid.o decimal_str.o \ filesearch.o gsm7_decode.o gsm7_encode.o gsm7_encode_table.o \ gsm7_unpack.o hexdigits.o hexread.o hexstr.o number_decode.o \ - number_encode.o pinentry.o plmncodes.o revnibbles.o + number_encode.o pinentry.o plmncodes.o revnibbles.o shorthand.o LIB= libutil.a all: ${LIB} diff -r 2557012666ea -r b86b3f8890ba libutil/shorthand.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/shorthand.c Fri Feb 26 21:04:11 2021 +0000 @@ -0,0 +1,64 @@ +/* + * This module implements the function for parsing shorthand decimal strings. + */ + +#include +#include +#include +#include +#include + +parse_decimal_shorthand(arg, dest, maxdigits) + char *arg; + u_char *dest; + unsigned maxdigits; +{ + unsigned n, ntail; + + if (!*arg) { + fprintf(stderr, + "error: empty argument given for decimal string\n"); + return(-1); + } + if (!isdigit(*arg)) { + fprintf(stderr, + "error: decimal string argument begins with a non-digit\n"); + return(-1); + } + for (n = 0; isdigit(*arg); ) { + if (n >= maxdigits) { +toolong: fprintf(stderr, + "error: decimal string exceeds limit of %u digits\n", + maxdigits); + return(-1); + } + dest[n++] = *arg++ - '0'; + } + if (!*arg) { + if (n != maxdigits) { + fprintf(stderr, + "error: %u digits required, %u digits given\n", + maxdigits, n); + return(-1); + } + return(0); + } + if (*arg++ != '-') { +malformed: fprintf(stderr, + "error: malformed shorthand decimal string argument\n"); + return(-1); + } + ntail = strlen(arg); + if (n + ntail >= maxdigits) + goto toolong; + while (n < maxdigits - ntail) + dest[n++] = 0; + if (!isdigit(*arg)) + goto malformed; + while (*arg) { + if (!isdigit(*arg)) + goto malformed; + dest[n++] = *arg++ - '0'; + } + return(0); +}