From a24071ae913b223487df78859c8d830f9e69f580 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 28 Aug 2024 16:42:05 +0200 Subject: Second commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- cmdline.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 cmdline.c (limited to 'cmdline.c') diff --git a/cmdline.c b/cmdline.c new file mode 100644 index 0000000..182a29d --- /dev/null +++ b/cmdline.c @@ -0,0 +1,84 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +static enum command command = ANYSUM; +static enum libhashsum_algorithm algorithm; + + +#define USAGE_COMMON "[-c [-w]] [-W options] ... [-z] [file] ..." +#define USAGE_BSUM "[-l bits] [-S salt] "USAGE_COMMON +#define USAGE_SHA3SUM "[-a bits] "USAGE_COMMON +#define USAGE_KECCAK "[-R rate] [-C capacity] [-N output-size] [-S state-size] [-W word-size] [-Z squeeze-count] "USAGE_COMMON +#define USAGE_SHAKE "[-N output-bits] "USAGE_COMMON +#define USAGE_RAWSHAKE "[-N output-bits] "USAGE_COMMON +#define USAGE_ANYSUM "(-c [-w] | [-a algoritms] ... ) [-W options] ... [-z] [file] ..." + +#define static +NUSAGE(libsimple_default_failure_exit, + (command == ANYSUM ? USAGE_ANYSUM : + command == BSUM ? USAGE_BSUM : + command == SHA3SUM ? USAGE_SHA3SUM : + (algorithm == LIBHASHSUM_BLAKE224 || + algorithm == LIBHASHSUM_BLAKE256 || + algorithm == LIBHASHSUM_BLAKE384 || + algorithm == LIBHASHSUM_BLAKE512) ? USAGE_BSUM : + (algorithm == LIBHASHSUM_SHAKE128 || + algorithm == LIBHASHSUM_SHAKE256 || + algorithm == LIBHASHSUM_SHAKE512) ? USAGE_SHAKE : + (algorithm == LIBHASHSUM_RAWSHAKE128 || + algorithm == LIBHASHSUM_RAWSHAKE256 || + algorithm == LIBHASHSUM_RAWSHAKE512) ? USAGE_RAWSHAKE : + algorithm == LIBHASHSUM_KECCAK ? USAGE_KECCAK : USAGE_COMMON)); +#undef static + + +void +cmdline(int *argcp, char ***argvp, struct config *config) +{ + int old_argc = *argcp; + int argc = *argcp; + char **argv = *argvp; + const char *algostr = NULL; + char *algostrbuf = NULL; + + argv0 = argv[0]; + command = getcommand(&algostr, &algorithm); + switch (getsupercommand(command, algorithm)) { + case BSUM: + argc = cmdline_bsum(argc, argv, command, config, &algostr, &algorithm, &algostrbuf); + break; + case SHA3SUM: + argc = cmdline_sha3sum(argc, argv, command, config, &algostr, &algorithm, &algostrbuf); + break; + case ANYSUM: + case SPECIALISED: + default: + argc = cmdline_other(argc, argv, command, config); + break; + } + + if (config->warn_improper_format > config->verify) + usage(); + + if (command == ANYSUM) { + if (config->verify && config->nalgorithms) + usage(); + config->format |= WITH_ALGOSTR; + } + if (!config->verify) { + if ((config->format & FORMAT_MASK) == BINARY) + config->format &= FORMAT_MASK; + } + + if (algostr) { + config->algorithms = erealloc(config->algorithms, sizeof(*config->algorithms)); + (config->algorithms)[0].algostr = algostr; + config->nalgorithms = 1U; + } + + *argcp = argc; + *argvp = &argv[old_argc - argc]; + + free(algostrbuf); +} -- cgit v1.2.3-70-g09d2