-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
157 lines (131 loc) · 3.4 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "aqua/aqua.h"
typedef enum { STREAM_TYPE_protect, STREAM_TYPE_purify } STREAM_TYPE;
int
no_errata_purify(Polynomial* m, int symn){
Polynomial errp = {{0}, 0};
return purify(m, &errp, symn);
}
int
stream(STREAM_TYPE type, int length, int symn){
Polynomial msg = {{0}, 0};
FILE *fpin = freopen(NULL, "rb", stdin);
FILE *fpout = freopen(NULL, "wb", stdout);
int in_size = 0;
int (*function)(Polynomial*, int) = NULL;
switch (type) {
case STREAM_TYPE_protect:
in_size = length;
function = protect;
break;
case STREAM_TYPE_purify:
in_size = length + symn;
function = no_errata_purify;
break;
}
while (1){
int bytes = fread(msg.data, 1, in_size, fpin);
if (bytes == 0){
break;
}
msg.order = bytes;
int result = function(&msg, symn);
if (result != 0){
return result;
}
fwrite(msg.data, 1, msg.order, fpout);
}
fclose(fpin);
fclose(fpout);
return 0;
}
int
usage(void){
fprintf(stderr, "Usage: aqua {MODE} [OPTIONS]...\n");
fprintf(stderr, "Protect and recover protected files ");
fprintf(stderr, "using Reed-Solomon\n");
fprintf(stderr, "Avaliable modes\n");
fprintf(stderr, " protect Encodes a stream\n");
fprintf(stderr, " purify Decodes a stream\n");
fprintf(stderr, "\nMessage size + number of symbols can't be more");
fprintf(stderr, " than 255\n");
fprintf(stderr, " -c message size, defaults to 245, or");
fprintf(stderr, " (255 - s) if -s provided\n");
fprintf(stderr, " -s number of symbols, defaults to 10, or");
fprintf(stderr, " (255 - c) if -c provided\n");
return 1;
}
int
argint(char* arg, int* out){
char *ptr = arg;
long i = strtol(arg, &ptr, 10);
if (i > 255){
return 1;
}
if (strlen(ptr) != 0){
return 1;
}
*out = i;
return 0;
}
int
main(int argc, char** argv){
init_aqua();
int c = 0;
int s = 0;
if (argc < 2){
return usage();
}
STREAM_TYPE type;
if (strcmp(argv[1], "protect") == 0) {
type = STREAM_TYPE_protect;
} else if (strcmp(argv[1], "purify") == 0){
type = STREAM_TYPE_purify;
} else {
return usage();
}
int i;
while ((i = getopt (argc, argv, "c:s:")) != -1)
switch (i){
case 'c':
if (argint(optarg, &c) != 0){
return usage();
}
break;
case 's':
if (argint(optarg, &s) != 0){
return usage();
}
break;
case '?':
return usage();
break;
default:
return usage();
}
if (c == 0){
c = (s != 0) ? (255 - s) : (245);
}
if (s == 0){
s = (c != 0) ? (255 - c) : (10);
}
if ((c + s) > 255){
return usage();
}
int result = stream(type, c, s);
switch (result){
case 3:
fprintf(stderr, "I can't fix it, too many errors\n");
break;
case 4:
fprintf(stderr, "I couldn't find all errors\n");
break;
case 5:
fprintf(stderr, "The message can't be fixed\n");
break;
}
return result;
}