-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfjk.c
69 lines (59 loc) · 1.42 KB
/
fjk.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
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include "include/fjk.h"
void print_usage(char *selfname);
void run(FILE *from, FILE *to, fjk_algo method);
int main(int argc, char **argv)
{
int opt;
extern int errno;
fjk_algo method = &fjk_encrypt;
FILE *input = stdin, *output = stdout;
while((opt = getopt(argc, argv, "hdi:o:")) != -1) {
switch (opt) {
case 'd':
method = &fjk_decrypt;
break;
case 'i':
SET_IO(input, optarg, "rb");
break;
case 'o':
SET_IO(output, optarg, "wb");
break;
case 'h':
default:
print_usage(argv[0]);
exit(1);
}
}
run(input, output, method);
return 0;
}
void run(FILE *from, FILE *to, fjk_algo method)
{
char *indat;
char *outdat;
size_t buffsize = BUFF_SZ;
size_t size = 0;
assert((indat = malloc(buffsize)) != NULL);
size += fread(indat, sizeof(char), buffsize, from);
while (size == buffsize) {
buffsize = buffsize << 1;
assert((indat = realloc(indat, buffsize)) != NULL);
size += fread((indat + size), sizeof(char), buffsize >> 1, from);
}
fclose(from);
fwrite((outdat = method(indat, size)), sizeof(char), size, to);
fclose(to);
free(indat);
free(outdat);
}
void print_usage(char *selfname)
{
printf("Usage: %s [-d] [-i infile] [-o outfile] [-h]\n", selfname);
printf(" -d: decrypt\n");
printf(" -i FILE: input file, or STDIN\n");
printf(" -o FILE: output file, or STDOUT\n");
printf(" -h: display this help and exit\n");
}