diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-28 16:42:05 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-28 16:42:05 +0200 |
commit | a24071ae913b223487df78859c8d830f9e69f580 (patch) | |
tree | e2ec712cc29461c82cfdd477e8b1ba961b50018d /cmdline_bsum.c | |
parent | First commit (diff) | |
download | anysum-a24071ae913b223487df78859c8d830f9e69f580.tar.gz anysum-a24071ae913b223487df78859c8d830f9e69f580.tar.bz2 anysum-a24071ae913b223487df78859c8d830f9e69f580.tar.xz |
Second commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'cmdline_bsum.c')
-rw-r--r-- | cmdline_bsum.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/cmdline_bsum.c b/cmdline_bsum.c new file mode 100644 index 0000000..d267314 --- /dev/null +++ b/cmdline_bsum.c @@ -0,0 +1,102 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +cmdline_bsum(int argc, char **argv, enum command command, struct config *config, + const char **algostr, enum libhashsum_algorithm *algorithm, char **algostrbuf) +{ + const char *salt = NULL; + char *arg; + + *algostrbuf = NULL; + + ARGBEGIN { + case 'b': /* binary input (compat) */ + case 't': /* text input (compat) */ + config->hexinput = 0; + break; + + case 'c': + config->verify = 1; + break; + + case 'w': + config->warn_improper_format = 1; + break; + + case 'z': + config->format &= (enum format)~WITH_LF; + config->format |= WITH_NUL; + break; + + case 'l': + if (command != BSUM) + usage(); + arg = ARG(); + if (!strcmp(arg, "224")) *algostr = "blake224", *algorithm = LIBHASHSUM_BLAKE224; + else if (!strcmp(arg, "256")) *algostr = "blake256", *algorithm = LIBHASHSUM_BLAKE256; + else if (!strcmp(arg, "384")) *algostr = "blake384", *algorithm = LIBHASHSUM_BLAKE384; + else if (!strcmp(arg, "512")) *algostr = "blake512", *algorithm = LIBHASHSUM_BLAKE512; + else + eprintf("unsupport value on -l option, expected 224, 256, 384, or 512"); + break; + + case 'S': + if (salt) + usage(); + arg = ARG(); + if (!strncasecmp(arg, "0x", 2)) + arg = &arg[2]; + if (!*arg) + usage(); + salt = arg; + while (isxdigit(*arg)) + arg++; + if (!*arg) + eprintf("expected hexadecimal value for -S option"); + break; + + case 'x': /* (compat) */ + config->hexinput = 1; + break; + + case 'L': /* (compat) */ + config->format &= (enum format)~FORMAT_MASK; + config->format |= LOWERCASE_HEX; + break; + + case 'U': /* (compat) */ + config->format &= (enum format)~FORMAT_MASK; + config->format |= UPPERCASE_HEX; + break; + + case 'B': /* (compat) */ + config->format &= (enum format)~FORMAT_MASK; + config->format |= BINARY; + break; + + case 'W': + arg = parseopts(config, ARG(), &parseopt_vendor); + if (*arg) + eprintf("unsupported -W options: %s", arg); + break; + + default: + usage(); + } ARGEND; + + if (salt) { + size_t expected_salt_len = 64; /* BLAKEb */ + if (*algorithm == LIBHASHSUM_BLAKE224 || *algorithm == LIBHASHSUM_BLAKE256) + expected_salt_len = 32; /* BLAKEs */ + if (strlen(salt) != expected_salt_len) + eprintf("salt for BLAKE%s is expected to be %zu hexadecimal characters long", + &(*algostr)[5], expected_salt_len); + *algostrbuf = malloc(strlen(*algostr) + strlen(salt) + sizeof("[salt=]")); + sprintf(*algostrbuf, "%s[salt=%s]", *algostr, salt); + *algostr = *algostrbuf; + } + + return argc; +} |