aboutsummaryrefslogtreecommitdiffstats
path: root/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'command.c')
-rw-r--r--command.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/command.c b/command.c
new file mode 100644
index 0000000..d280766
--- /dev/null
+++ b/command.c
@@ -0,0 +1,101 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+enum command
+getcommand(const char **name_out, enum libhashsum_algorithm *algorithm_out)
+{
+ const char *name, *p;
+
+ name = strrchr(argv0, '/');
+ name = name ? &name[1] : argv0;
+
+ if (strstarts(name, "anysum")) {
+ name = &name[6];
+ while (isdigit(*name) || *name == '.' || *name == '_')
+ name++;
+ if (*name++ != '-')
+ return ANYSUM;
+ }
+
+ if (strcasestarts(name, "sha")) {
+ p = &name[3];
+ if (*p == '-')
+ p++;
+ if (*p++ == '3') {
+ if (!*p || !strcasecmp(p, "sum")) {
+ *algorithm_out = LIBHASHSUM_SHA3_224;
+ return SHA3SUM;
+ }
+ }
+ }
+
+ p = name;
+ if (*p != 'b' && *p != 'B')
+ goto specialised;
+ if (!strncasecmp(++p, "lake", 4))
+ p = &p[4];
+ if (*p && strcasecmp(p, "sum"))
+ goto specialised;
+ *name_out = "blake224";
+ *algorithm_out = LIBHASHSUM_BLAKE224;
+ return BSUM;
+
+specialised:
+ if (!libhashsum_get_algorithm_string(algorithm_out, name))
+ eprintf("invalid command name, is not \"anysum\" and "
+ "does not match any known hash function");
+ if (strchr(name, '['))
+ eprintf("invalid command name, cannot contain parameters");
+ *name_out = name;
+ return SPECIALISED;
+}
+
+
+enum command
+getsupercommand(enum command cmd, enum libhashsum_algorithm algo)
+{
+ if (cmd != SPECIALISED)
+ return cmd;
+ switch (algo) {
+ case LIBHASHSUM_MD2:
+ case LIBHASHSUM_MD4:
+ case LIBHASHSUM_MD5:
+ case LIBHASHSUM_RIPEMD_128:
+ case LIBHASHSUM_RIPEMD_160:
+ case LIBHASHSUM_RIPEMD_256:
+ case LIBHASHSUM_RIPEMD_320:
+ case LIBHASHSUM_SHA0:
+ case LIBHASHSUM_SHA1:
+ case LIBHASHSUM_SHA_224:
+ case LIBHASHSUM_SHA_256:
+ case LIBHASHSUM_SHA_384:
+ case LIBHASHSUM_SHA_512:
+ case LIBHASHSUM_SHA_512_224:
+ case LIBHASHSUM_SHA_512_256:
+ return SPECIALISED;
+ case LIBHASHSUM_KECCAK:
+ case LIBHASHSUM_KECCAK_224:
+ case LIBHASHSUM_KECCAK_256:
+ case LIBHASHSUM_KECCAK_384:
+ case LIBHASHSUM_KECCAK_512:
+ case LIBHASHSUM_SHA3_224:
+ case LIBHASHSUM_SHA3_256:
+ case LIBHASHSUM_SHA3_384:
+ case LIBHASHSUM_SHA3_512:
+ case LIBHASHSUM_SHAKE128:
+ case LIBHASHSUM_SHAKE256:
+ case LIBHASHSUM_SHAKE512:
+ case LIBHASHSUM_RAWSHAKE128:
+ case LIBHASHSUM_RAWSHAKE256:
+ case LIBHASHSUM_RAWSHAKE512:
+ return SHA3SUM;
+ case LIBHASHSUM_BLAKE224:
+ case LIBHASHSUM_BLAKE256:
+ case LIBHASHSUM_BLAKE384:
+ case LIBHASHSUM_BLAKE512:
+ return BSUM;
+ default:
+ eprintf("algorithm not recognised");
+ }
+}