-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_usages_00.c
executable file
·252 lines (186 loc) · 5.96 KB
/
test_usages_00.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
#include "main.h"
// global variables
#define DATA_SIZE 20
int display_N_values_per_line = 5;
/*
==================================================
============= || LOCAL FUNCTIONS || ==============
==================================================
*/
void f(){
printf("You called me ... \n");
}
// for test_usage_choosing_function_at_runtime()
enum test_types {
A, //0
B, //1
I, //2
E, //3
};
int numberOfTestTypes = 4; // 4 test types *_test()
void a_test() {
ANNOUNCE_FUNCTION_CALLED();
printf("a test\n");
}
void b_test() {
ANNOUNCE_FUNCTION_CALLED();
printf("b test\n");
}
void i_test() {
ANNOUNCE_FUNCTION_CALLED();
printf("i test\n");
}
void e_test() {
ANNOUNCE_FUNCTION_CALLED();
printf("e test\n");
}
static int (*test_func[]) (void) = {
[A] = a_test,
[B] = b_test,
[I] = i_test,
[E] = e_test,
};
/*
=====================================================================
============= || USAGE FUNCTIONS FOR EXTERNAL CALLS || ==============
=====================================================================
*/
int test_usage_choosing_function_at_runtime()
{
INFO(">> ");
int i;
i = 2; // I-test
if (i < numberOfTestTypes)
test_func[i](); // choose the test to run // output: i-test
for (i=0; i < numberOfTestTypes; i++){
// printf('Running test_func[%d]() \n', i);
test_func[i]();
}
printf("%s\n", DEMARCATOR_STRING);
}
int test_usage_reading_addresses()
{
INFO(">> ");
// example: reading itself
long sizeOfExe = 0;
FILE *fp;
fp = fopen("./run.exe", "rb"); // reading itself
fseek(fp, 0L, SEEK_END);
sizeOfExe = ftell(fp);
printf("The size of this executable is: %ld bytes\n", sizeOfExe);
int* addressStartOfFile = (int*) &fp;
printf("Location of this executable starts at: 0x%x\n", addressStartOfFile);
printf("Location of this executable ends at: 0x%x\n", (addressStartOfFile+sizeOfExe));
/*
The size of this executable is: 18727 bytes
Location of this executable starts at: 0x28ff30
Location of this executable ends at: 0x2a23cc
*/
// get the memory address of a function
void f();
void (*p_fun)() = & f;
p_fun();
printf("Location of p_fun() starts at: 0x%x\n", p_fun);
printf("Location of f() starts at: 0x%x\n", &f);
printf("%s\n", DEMARCATOR_STRING);
}
int test_usage_passing_pointer_as_argument()
{
INFO(">> ");
char* returnStringBuffer;
char* inputStr = "112233445566aa112233445566aa112233445566aa112233445566aa";
putString (&returnStringBuffer, inputStr);
printf("%s\n", returnStringBuffer);
free(returnStringBuffer); // free malloc'd memory
printf("%s\n", DEMARCATOR_STRING);
}
/*
*/
int test_usage_pointer_trials()
{
INFO(">> ");
// create 2 arrays
int ages[] = {23, 43, 12, 89, 2, 5, 9};
char *names[] = {
"Alan", "Frank",
"Mary", "John", "Lisa",
"French", "Tom"
};
//int count = sizeof(ages) / sizeof(int); // number of element in array
int count = sizeof(names) / sizeof(char*); // number of element in array
int i = 0;
// [1] using indexing
for(i = 0; i < count; i++) {
printf("%s has %d years.\n",
names[i], ages[i]);
}
printf("---\n");
// setup the pointers to the start of the arrays
int *ptr_age = ages;
char **ptr_name = names;
// [2] using pointers
for(i = 0; i < count; i++) {
printf("%s is %d years old.\n",
*(ptr_name+i), *(ptr_age+i));
}
printf("---\n");
// [3] pointers are just arrays
for(i = 0; i < count; i++) {
printf("%s is %d years old again.\n",
ptr_name[i], ptr_age[i]);
}
printf("---\n");
// [4] pointers in a stupid complex way
for(ptr_name = names, ptr_age = ages;
(ptr_age - ages) < count;
ptr_name++, ptr_age++) {
printf("%s lived %d years so far.\n",
*ptr_name, *ptr_age);
}
printf("%s\n", DEMARCATOR_STRING);
}
int test_usage_check_if_agnostic_function_is_designated(){
INFO(">> ");
int mode = 5;
int type = 7;
const char *file;
int line = 100;
printf("contents : %p\n", agnosticFunction);
printf("address : 0x%x\n", &agnosticFunction);
agnosticFunction = designatedFunction; // comment this out, and the agnostic function will not be activated
if (agnosticFunction) { // if the address is not pointed to NULL or base 0
printf("Mode in operation.\n");
agnosticFunction(mode, type, file, line);
}
printf("contents : %p\n", agnosticFunction);
printf("address : 0x%x\n", &agnosticFunction);
printf("%s\n", DEMARCATOR_STRING);
}
int test_usage_write_data_to_specific_memory_regions()
{
INFO(">> ");
int target = 42;
// pointing to specific memory regions, and writing data to them
int *my_pointer, *my;
int my_variable = 10, other_variable = 20;
printf("the address of my_variable is : %p\n", &my_variable);
printf("the address of other_variable is : %p\n", &other_variable);
// my_pointer = (int *)(0x28FF48);
my_pointer = ⌖
// my = (int *)(0x28FF4C);
my = ⌖
printf("\nafter \"my_pointer = &my_variable\":\n");
printf("\tthe value of my_pointer is %p\n", my_pointer);
printf("\tthe value at that address is %d\n", *my_pointer);
my_variable = my_pointer;
my_pointer = my;
my = my_variable;
// temp = *my;
// my = (int*) 0x405A00; /* point to x : 0x402000*/
my = ⌖
*my = 100;
printf("\tthe value of target is %d\n", target);
printf("\tthe robin value of x is %d\n", *my);
printf("\tthe address of x is %x\n", my);
printf("%s\n", DEMARCATOR_STRING);
}