-
Notifications
You must be signed in to change notification settings - Fork 1
/
sha512_read.c
101 lines (94 loc) · 3.05 KB
/
sha512_read.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sha512_read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: itiievsk <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/09/17 16:17:27 by itiievsk #+# #+# */
/* Updated: 2018/09/17 16:17:29 by itiievsk ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ssl.h"
#include "sha512.h"
int sha512_choose_target(char **argv, t_flags *flags, int i,
t_sha512_ctx *ctx)
{
int j;
j = 0;
while (argv[i][++j])
{
check_extra_flags(flags, argv[i][j], ctx->func);
if (argv[i][j] == 's' && (flags->s = 1) && argv[i][j + 1]
&& (ctx->len = ft_strlen(&argv[i][j + 1])) < MAX &&
sha512_encrypt(&argv[i][j + 1], flags, ctx) > 0)
return (i);
else if (argv[i][j] == 's' && i + 1 >= ctx->argc)
ssl_s_error(flags, ctx->func);
else if (argv[i][j] == 's' && (ctx->len = ft_strlen(argv[i + 1])) < MAX
&& sha512_encrypt(argv[i + 1], flags, ctx) > 0)
return (i + 1);
else if (argv[i][j] == 'p' && ++(flags->p) && ++(flags->stdin))
{
sha512_encrypt(ssl_read_stdin(&ctx->len, 0, flags), flags, ctx);
if (argv[i][j + 1] == '\0')
return (i);
flags_init(flags);
}
}
return (i);
}
void sha512_parse_targets(int argc, char **argv, t_flags *flags,
t_sha512_ctx *ctx)
{
int j;
int i;
int fd;
i = 1;
while (++i < argc && ((j = 0) == 0) && !flags->nomore && flags_init(flags))
{
if (argv[i][0] == '-' && !flags->nomore)
i = sha512_choose_target(argv, flags, i, ctx);
else
flags->nomore = 1;
}
if (!flags->p)
i = ((flags->s) ? i - 1 : i - 2);
flags_init(flags);
while (++i < argc && i > 1 && flags->nomore == 1)
{
if ((((fd = open(argv[i], O_WRONLY)) < 0 && errno == EISDIR) ||
(fd = open(argv[i], O_RDONLY)) < 0) && ++(ctx->targets))
ft_printf("ft_ssl: %s: %s: %s\n", ctx->func, argv[i],
strerror(errno));
else
sha512_encrypt(ssl_read_file(
argv[i], &ctx->len, ctx->func, &ctx->file), flags, ctx);
}
}
void sha512(int argc, char **argv)
{
t_flags *flags;
t_sha512_ctx ctx;
if (!(flags = (t_flags*)malloc(sizeof(t_flags))))
return ;
flags->nomore = 0;
flags->p = 0;
flags->q = 0;
flags->r = 0;
flags->s = 0;
flags->stdin = 0;
ctx.argc = argc;
ctx.targets = 0;
ft_memcpy(ctx.func, "sha512\0\0\0\0", 10);
sha512_parse_targets(argc, argv, flags, &ctx);
if (ctx.targets == 0 || ((flags->stdin == ctx.targets) &&
(flags->q || flags->r)))
{
flags_init(flags);
(flags->p) = -2;
++(flags->stdin);
sha512_encrypt(ssl_read_stdin(&ctx.len, 0, flags), flags, &ctx);
}
free(flags);
}