forked from RPISEC/MBE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab8B.c
251 lines (233 loc) · 5.23 KB
/
lab8B.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
/*
* gcc -z relro -z now -fPIE -pie -fstack-protector-all -o lab8B lab8B.c
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_FAVES 10
struct vector {
void (*printFunc)(struct vector*);
char a;
short b;
unsigned short c;
int d;
unsigned int e;
long f;
unsigned long g;
long long h;
unsigned long long i;
};
struct vector v1;
struct vector v2;
struct vector v3;
struct vector* faves[MAX_FAVES];
void printVector(struct vector* v);
void printMenu()
{
printf("+------------------------------------------------------------+\n");
printf("| |\n");
printf("| 1. Enter data :> |\n");
printf("| 2. Sum vectors :] |\n");
printf("| 3. Print vector :3 |\n");
printf("| 4. Save sum to favorites 8) |\n");
printf("| 5. Print favorites :O |\n");
printf("| 6. Load favorite :$ |\n");
printf("| 9. Get help :D |\n");
printf("| |\n");
printf("+------------------------------------------------------------+\n");
printf("I COMMAND YOU TO ENTER YOUR COMMAND: ");
}
struct vector* vectorSel()
{
printf("Which vector? ");
char sel;
while((sel = getchar()) == '\n'); // I love C.
switch(sel)
{
case '1':
return &v1;
case '2':
return &v2;
case '3':
return &v3;
default:
printf("\nBAD VECTOR SELECTION\n");
exit(EXIT_FAILURE);
}
}
void enterData()
{
struct vector* v = vectorSel();
if(v == &v3)
{
printf("Please don't try to manually enter data into the sum.\n");
return;
}
printf("Data entry time!\n");
printf("char a: ");
while((v->a = getchar()) == '\n'); // Still love C.
printf("short b: ");
scanf("%hd", &(v->b));
printf("unsigned short c: ");
scanf("%hu", &(v->c));
printf("int d: ");
scanf("%d", &(v->d));
printf("unsigned int e: ");
scanf("%u", &(v->e));
printf("long f: ");
scanf("%ld", &(v->f));
printf("unsigned long g: ");
scanf("%lu", &(v->g));
printf("long long h: ");
scanf("%lld", &(v->h));
printf("unsigned long long i: ");
scanf("%llu", &(v->i));
v->printFunc = printVector;
}
void sumVectors()
{
if(v1.a==0 || v2.a==0 ||
v1.b==0 || v2.b==0 ||
v1.c==0 || v2.c==0 ||
v1.d==0 || v2.d==0 ||
v1.e==0 || v2.e==0 ||
v1.f==0 || v2.f==0 ||
v1.g==0 || v2.g==0 ||
v1.h==0 || v2.h==0 ||
v1.i==0 || v2.i==0)
{
printf("You didn't even set the addends... :(\n");
return;
}
v3.a = v1.a + v2.a;
v3.b = v1.b + v2.b;
v3.c = v1.c + v2.c;
v3.d = v1.d + v2.d;
v3.e = v1.e + v2.e;
v3.f = v1.f + v2.f;
v3.g = v1.g + v2.g;
v3.h = v1.h + v2.h;
v3.i = v1.i + v2.i;
printf("Summed.\n");
}
/*
* Bonus points if you don't use this function.
*/
void thisIsASecret()
{
system("/bin/sh");
}
void printVector(struct vector* v)
{
printf("Address: %p\n", v);
printf("void printFunc: %p\n", v->printFunc);
printf("char a: %c\n", v->a);
printf("short b: %hd\n", v->b);
printf("unsigned short c: %hu\n", v->c);
printf("int d: %d\n", v->d);
printf("unsigned int e: %u\n", v->e);
printf("long f: %ld\n", v->f);
printf("unsigned long g: %lu\n", v->g);
printf("long long h: %lld\n", v->h);
printf("unsigned long long i: %llu\n", v->i);
}
void fave()
{
unsigned int i;
for(i=0; i<MAX_FAVES; i++)
if(!faves[i])
break;
if(i == MAX_FAVES)
printf("You have too many favorites.\n");
else
{
faves[i] = malloc(sizeof(struct vector));
memcpy(faves[i], (int*)(&v3)+i, sizeof(struct vector));
printf("I see you added that vector to your favorites, \
but was it really your favorite?\n");
}
}
void printFaves()
{
unsigned int i;
for(i=0; i<MAX_FAVES; i++)
if(faves[i])
printVector(faves[i]);
else
break;
printf("Printed %u vectors.\n", i);
}
void loadFave()
{
printf("Which favorite? ");
unsigned int i;
scanf("%u", &i);
if(i >= MAX_FAVES)
{
printf("Index out of bounds\n");
return;
}
struct vector* v = vectorSel();
if(v == &v3)
{
printf("Please don't try to manually enter data into the sum.\n");
return;
}
memcpy(v, faves[i], sizeof(v));
}
void help()
{
printf("\
This program adds two vectors together and stores it in a third vector. You \
can then add the sum to your list of favorites, or load a favorite back into \
one of the addends.\n");
}
int main(int argc, char** argv)
{
char sel;
printMenu();
v1.printFunc = printf;
v2.printFunc = printf;
v3.printFunc = printf;
struct vector* v;
while((sel = getchar()) && (sel == '\n' || getchar())) // Magic ;^)
{
if(sel == '\n')
continue;
switch(sel)
{
case '0':
printf("OK, bye.\n");
return EXIT_SUCCESS;
case '1':
enterData();
break;
case '2':
sumVectors();
break;
case '3':
v = vectorSel();
//printf("Calling %p\n", v->printFunc);
v->printFunc(v);
break;
case '4':
fave();
break;
case '5':
printFaves();
break;
case '6':
loadFave();
break;
case '9':
help();
break;
default:
printf("\nThat was bad input. \
Just like your futile attempt to pwn this.\n");
return EXIT_FAILURE;
}
printMenu();
}
return EXIT_SUCCESS;
}