aboutsummaryrefslogtreecommitdiffstats
path: root/command.c
blob: d28076685d2b87ff6c2170c61d0d7aefe32548e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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");
	}
}