-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
101 lines (87 loc) · 2.63 KB
/
main.c
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
#include "ft_ssl.h"
#include "utils.h"
#include "md5.h"
#include "sha256.h"
#include "base64.h"
#include "des.h"
static uint32_t parse_arg(char **argv, ft_ssl_param *param) {
uint32_t i = -1;
while (argv[++i]) {
if (!ft_strcmp(argv[i], "-r"))
param->r = 1;
else if (!ft_strcmp(argv[i], "-p"))
param->p = 1;
else if (!ft_strcmp(argv[i], "-q"))
param->q = 1;
else if (!ft_strcmp(argv[i], "-s"))
param->s = 1;
else
break;
}
return i;
}
static int32_t wrapper(int32_t (*p)(uint8_t **, ft_ssl_param, int), uint8_t ** argv, int argc) {
ft_ssl_param param = {};
uint32_t i = parse_arg((char **)argv, ¶m);
return p(&argv[i], param, argc - i);
}
static int32_t cipher_wrapper(int32_t (*p)(uint8_t **, ft_ssl_param, int), uint8_t ** argv, int argc) {
ft_ssl_param param = {};
uint32_t i = parse_arg((char **)argv, ¶m);
return p(&argv[i], param, argc - i);
}
// int32_t base64(uint8_t **argv, ft_ssl_param param, int argc) {
// (void)argv;
// (void)param;
// return argc;
// }
// int32_t des(uint8_t **argv, ft_ssl_param param, int argc) {
// (void)argv;
// (void)param;
// return argc;
// }
int32_t des_ecb(uint8_t **argv, ft_ssl_param param, int argc) {
(void)argv;
(void)param;
return argc;
}
int32_t des_cbc(uint8_t **argv, ft_ssl_param param, int argc) {
(void)argv;
(void)param;
return argc;
}
int main(int argc, char **argv) {
if (argc < 2)
return fprint("usage: ft_ssl command [flags] [file/string]\n"), 0;
int32_t (*mdc[])(uint8_t **, ft_ssl_param, int) = {md5, sha256};
int32_t (*cm[])(uint8_t **, ft_ssl_param, int) = {base64, des, des_ecb, des_cbc};
char *name[] = {"md5", "sha256", "base64", "des", "des-ecb", "des-cbc"};
char *fmd[] = {"-p", "-q", "-r", "-s"};
int check = -1;
int nb_mdc = (sizeof(mdc) / sizeof(int32_t *));
int nb_cm = (sizeof(cm) / sizeof(int32_t *));
for (int i = 0; i < nb_mdc; ++i) {
if (!ft_strcmp(argv[1], name[i]))
check = check && wrapper(mdc[i], (uint8_t **)&argv[2], argc - 2);
}
for (int i = nb_mdc; i < nb_mdc + nb_cm; ++i) {
if (!ft_strcmp(argv[1], name[i]))
check = check && cipher_wrapper(cm[i - nb_mdc], (uint8_t **)&argv[2], argc - 2);
}
if (check == -1) {
fprint("ft_ssl: Error: '%s' is an invalid command.\n", argv[1]);
fprint("\nStandard commands:\n");
fprint("\nMessage Digest commands:\n");
for (int i = 0; i < nb_mdc; ++i)
fprint("%s\n", name[i]);
fprint("\nMessage Digest Flags:\n");
for (int i = 0; i < (int)(sizeof(fmd) / sizeof(char *)); ++i)
fprint("%s ", fmd[i]);
fprint("\n");
fprint("\nCipher commands:\n");
for (int i = nb_mdc; i < nb_cm + nb_mdc; ++i)
fprint("%s\n", name[i]);
check = -check;
}
return check;
}