forked from emilytouchingcomputers/CTFium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chall.c
253 lines (215 loc) · 5.2 KB
/
chall.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <emscripten.h>
#define maxlength 0x200
typedef size_t (*Filter)(char *dest, char *src, size_t length);
typedef struct {
size_t length;
char *buf;
Filter filter;
char to[0x20];
} Letter;
typedef struct {
size_t count;
Letter **letters;
} State;
size_t read_string(char *buf, size_t length){
size_t i;
if(length == 0){
abort();
}
for(i = 0; i < length - 1; i++){
if(fread(buf + i, 1, 1, stdin) != 1){
abort();
}
if(buf[i] == '\n'){
break;
}
}
buf[i] = '\0';
return i;
}
unsigned int read_int(){
char buf[16] = {};
read_string(buf, sizeof(buf));
return (unsigned int)atoi(buf);
}
size_t filter_lower(char *out, char *in, size_t length){
size_t i;
for(i = 0; i < length; i++){
out[i] = isupper((unsigned char)in[i]) ? (in[i] + 0x20) : in[i];
}
return i;
}
size_t filter_upper(char *out, char *in, size_t length){
size_t i;
for(i = 0; i < length; i++){
out[i] = islower((unsigned char)in[i]) ? (in[i] - 0x20) : in[i];
}
return i;
}
size_t filter_swapcase(char *out, char *in, size_t length){
size_t i;
for(i = 0; i < length; i++){
if(isupper((unsigned char)in[i])){
out[i] = in[i] + 0x20;
}else if(islower((unsigned char)in[i])){
out[i] = in[i] - 0x20;
}else{
out[i] = in[i];
}
}
return i;
}
void print_filters(){
puts(" 1. Lower");
puts(" 2. Upper");
puts(" 3. Swap case");
}
void add_letter(State *state){
Filter filters[] = {
filter_lower,
filter_upper,
filter_swapcase
};
Letter *letter;
size_t length;
char *buf;
size_t i, n;
puts("Size:");
length = read_int();
if(length == 0 || length > maxlength){
puts("Invalid size");
return;
}
buf = calloc(length, 1);
if(buf == NULL){
abort();
}
puts("Content:");
read_string(buf, length);
letter = calloc(1, sizeof(Letter));
if(letter == NULL){
abort();
}
letter->buf = buf;
letter->length = length;
puts("To:");
for(i = 0; i <= 32; i++){
if(fread(letter->to + i, 1, 1, stdin) != 1){
abort();
}
if(letter->to[i] == '\n'){
letter->to[i] = '\0';
break;
}
}
puts("Filter:");
print_filters();
puts("Select:");
n = read_int() - 1;
if(n >= sizeof(filters) / sizeof(Filter)){
abort();
}
letter->filter = filters[n];
state->letters = realloc(state->letters, sizeof(Letter *) * (state->count + 1));
if(state->letters == NULL){
abort();
}
state->letters[state->count++] = letter;
}
void show_letters(State *state){
size_t i;
if(state->count == 0){
puts("You don't have any letters");
return;
}
for(i = 0; i < state->count; i++){
if(state->letters[i] == NULL){
printf("[%lu] <deleted>\n", i + 1);
}else{
printf("[%lu] To: %s\n", i + 1, state->letters[i]->to);
}
}
}
void delete_letter(State *state){
size_t index;
puts("Index:");
index = read_int();
if(index >= state->count || state->letters[index] == NULL){
puts("Invalid index");
return;
}
free(state->letters[index]->buf);
free(state->letters[index]);
state->letters[index] = NULL;
}
void seal_letters(State *state, int post){
size_t i;
char *outbuf;
for(i = 0; i < state->count; i++){
if(state->letters[i] != NULL && state->letters[i]->filter != NULL){
outbuf = malloc(state->letters[i]->length);
if(outbuf == NULL){
abort();
}
state->letters[i]->filter(outbuf, state->letters[i]->buf, state->letters[i]->length);
state->letters[i]->buf = outbuf;
}
}
if(post){
emscripten_run_script("_do_post_letters()");
}
}
void show_banner(){
puts("*** Secret Mailer Service 2.0 ***");
puts("Welcome to Secret Mailer Service 2.0!");
puts("Post your secret letters here ;)");
}
void print_menu(){
puts("");
puts("---------- Menu ----------");
puts("1. Add a new letter");
puts("2. Check letters");
puts("3. Delete a letter");
puts("4. Seal all letters");
puts("5. Seal and post all letters");
puts("Select:");
}
int main(){
int n;
int running = 1;
State state = {
.count = 0,
.letters = NULL
};
setbuf(stdin, NULL);
setbuf(stdout, NULL);
show_banner();
while(running){
print_menu();
n = read_int();
switch(n){
case 1:
add_letter(&state);
break;
case 2:
show_letters(&state);
break;
case 3:
delete_letter(&state);
break;
case 4:
seal_letters(&state, 0);
running = 0;
break;
case 5:
seal_letters(&state, 1);
running = 0;
break;
}
}
puts("\nBye!");
return 0;
}