-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparse.c
More file actions
67 lines (42 loc) · 2.15 KB
/
parse.c
File metadata and controls
67 lines (42 loc) · 2.15 KB
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
// Parser for cipher types
#include "polyalphabetic.h"
// Helper for case-insensitive comparison
int str_eq(const char *a, const char *b) {
return strcasecmp(a, b) == 0;
}
int parse_cipher_type(const char *arg) {
// Check if the argument is a pure integer.
char *endptr;
long val = strtol(arg, &endptr, 10);
// If endptr points to the null terminator, the whole string was a number.
if (*arg != '\0' && *endptr == '\0') {
return (int)val;
}
// Check string aliases (case insensitive.)
// Vigenere
if (str_eq(arg, "vig") || str_eq(arg, "vigenere")) return VIGENERE;
// Quagmire I
if (str_eq(arg, "q1") || str_eq(arg, "quag1") || str_eq(arg, "quagmire1")) return QUAGMIRE_1;
// Quagmire II
if (str_eq(arg, "q2") || str_eq(arg, "quag2") || str_eq(arg, "quagmire2")) return QUAGMIRE_2;
// Quagmire III
if (str_eq(arg, "q3") || str_eq(arg, "quag3") || str_eq(arg, "quagmire3")) return QUAGMIRE_3;
// Quagmire IV
if (str_eq(arg, "q4") || str_eq(arg, "quag4") || str_eq(arg, "quagmire4")) return QUAGMIRE_4;
// Beaufort
if (str_eq(arg, "beau") || str_eq(arg, "beaufort")) return BEAUFORT;
// Porta
if (str_eq(arg, "porta")) return PORTA;
// Autokey (Vigenere Tableau)
if (str_eq(arg, "auto") || str_eq(arg, "autokey") || str_eq(arg, "auto0") || str_eq(arg, "autovig")) return AUTOKEY_0;
// Autokey Variants
if (str_eq(arg, "auto1") || str_eq(arg, "autokey1") || str_eq(arg, "autoquag1")) return AUTOKEY_1;
if (str_eq(arg, "auto2") || str_eq(arg, "autokey2") || str_eq(arg, "autoquag2")) return AUTOKEY_2;
if (str_eq(arg, "auto3") || str_eq(arg, "autokey3") || str_eq(arg, "autoquag3")) return AUTOKEY_3;
if (str_eq(arg, "auto4") || str_eq(arg, "autokey4") || str_eq(arg, "autoquag4")) return AUTOKEY_4;
// Autokey with Beaufort and Porta tableau
if (str_eq(arg, "auto5") || str_eq(arg, "abeau") || str_eq(arg, "autobeau") || str_eq(arg, "autobeaufort")) return AUTOKEY_BEAU;
if (str_eq(arg, "auto6") || str_eq(arg, "aporta") || str_eq(arg, "autoporta")) return AUTOKEY_PORTA;
// Return -1 to indicate invalid/unknown type.
return -1;
}