-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackfuncs.cpp
More file actions
311 lines (219 loc) · 8.03 KB
/
stackfuncs.cpp
File metadata and controls
311 lines (219 loc) · 8.03 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "stackfuncs.h"
ErrorCode createStack(Stack* stack ON_DEBUG(, char* name))
{
ON_DEBUG(stack->leftCanary = LEFT_STRUCT_CANARY);
ON_DEBUG(stack->name = name);
stack->size = 0;
stack->capacity = DEFAULT_CAPACITY;
stack->data = (elem_t*)calloc(1 ON_DEBUG(+ 2), sizeof(canary_t));
if (stack->data == NULL)
return NO_MEMORY;
ON_DEBUG(stack->hash = hashAiden32(stack));
ON_DEBUG(stack->rightCanary = RIGHT_STRUCT_CANARY);
ON_DEBUG(placeCanary(stack, 0, LEFT_DATA_CANARY));
ON_DEBUG(stack->data = (elem_t*)((char*)stack->data + sizeof(canary_t)));
ON_DEBUG(placeCanary(stack, stack->capacity, RIGHT_DATA_CANARY));
ON_DEBUG(stack->hash = hashAiden32(stack));
ASSERTHARD(stack);
return OK;
}
ErrorCode placeCanary(Stack* stack, size_t place, canary_t canary)
{
*(canary_t*)((char*)stack->data + place * sizeof(elem_t)) = canary;
ON_DEBUG(stack->hash = hashAiden32(stack));
return OK;
}
canary_t* getCanaryRightptr(const Stack* stack)
{
return (canary_t*)(stack->data + stack->capacity);
}
canary_t* getCanaryLeftPtr(const Stack* stack)
{
return (canary_t*)((char*)stack->data - sizeof(canary_t));
}
ErrorCode reallocStack(Stack* stack)
{
elem_t* temp = (elem_t*)realloc((char*)stack->data ON_DEBUG(- sizeof(canary_t)), \
((stack->capacity) * sizeof(elem_t) ON_DEBUG(+ sizeof(canary_t) * 2)));
if (temp == NULL)
return NO_MEMORY;
stack->data = temp;
stack->data = (elem_t*)((char*)stack->data ON_DEBUG(+ sizeof(canary_t)));
ON_DEBUG(placeCanary(stack, stack->capacity, RIGHT_DATA_CANARY));
ASSERTHARD(stack);
ON_DEBUG(stack->hash = hashAiden32(stack));
ASSERTHARD(stack);
return OK;
}
ErrorCode Push(Stack* stack, elem_t value)
{
ASSERTHARD(stack);
if (stack->capacity <= stack->size + 1)
{
stack->capacity *= 2;
ON_DEBUG(stack->hash = hashAiden32(stack));
reallocStack(stack);
poisonFill(stack);
}
stack->data[stack->size++] = value;
ON_DEBUG(stack->hash = hashAiden32(stack));
ASSERTHARD(stack);
return OK;
}
elem_t Pop(Stack* stack)
{
ASSERTHARD(stack);
if (stack->capacity >= 2 * (stack->size + 1))
{
stack->capacity /= 2;
ON_DEBUG(stack->hash = hashAiden32(stack));
reallocStack(stack);
}
elem_t peek = stack->data[stack->size - 1];
stack->data[stack->size--] = POISON;
ON_DEBUG(stack->hash = hashAiden32(stack));
ASSERTHARD(stack);
return peek;
}
ErrorCode poisonFill(Stack* stack)
{
ASSERTHARD(stack);
for(size_t i = stack->size; i < stack->capacity; i++)
stack->data[i] = POISON;
ON_DEBUG(stack->hash = hashAiden32(stack));
ASSERTHARD(stack);
return OK;
}
ErrorCode DestroyStack(Stack* stack)
{
if (stack->data == NULL)
{
printf("stack is already deleted!\n");
return STACK_DELETED;
}
else
{
stack->size = 0;
ON_DEBUG(stack->hash = hashAiden32(stack));
poisonFill(stack);
stack->capacity = 0;
free((elem_t*)((char*)stack->data ON_DEBUG(- sizeof(canary_t))));
stack->data = NULL;
ON_DEBUG(stack->hash = 0);
ON_DEBUG(stack->name = "NONE");
}
return OK;
}
ErrorCode PrintStack(const Stack* stack)
{
ON_DEBUG(printf("\tleft data canary = %llx\n", *getCanaryLeftPtr(stack)));
for (size_t i = 0; i < stack->capacity; i++)
if (stack->data[i] == POISON)
printf("\t\t ! [%d] %" FORMAT " (POISON!)\n", i, stack->data[i]);
else
printf("\t\t [%d] %" FORMAT "\n", i, stack->data[i]);
ON_DEBUG(printf("\tright data canary = %llx\n", *getCanaryRightptr(stack)));
return OK;
}
const char* stackStrError (const int code) // TODO: change error code parsing in thhe future
{
#define CODE_(code) case code: return #code;
switch (code)
{
CODE_ (NULLPTR_STACK)
CODE_ (NULLPTR_DATA)
CODE_ (SIZE_BIGGER_CAPACITY)
CODE_ (LCANARY_DATA_CHANGED)
CODE_ (RCANARY_DATA_CHANGED)
CODE_ (LCANARY_STRUCT_CHANGED)
CODE_ (RCANARY_STRUCT_CHANGED)
CODE_ (MAX_CAPACITY_OVERFLOW)
CODE_ (CANARY_SIZE_CHANGED)
CODE_ (CAPACITY_LESS_DEFAULT)
CODE_ (HASH_CHANGED)
}
return "UNKNOWN";
#undef CODE_
}
ErrorCode stackDump(const Stack* stack, const char* filename, const int lineNum, const char* functionName)
{
FILE* fp = fopen(logfilename, "w+");
// TODO: if fp can't open return
// with open(); <--------------- add to some lib in the future
/*
with_open(fp, "filename.txt", "r",
fread(fp, ....);
fwrite(fp, ....);
);
*/
if (fp == NULL)
return UNABLE_TO_OPEN_FILE;
LOG(fp, "This log file was made at: %s\n", getTime());
int error = stackVerify(stack);
LOG(fp, "Stack " NAME " [%p], ERROR #%u (%s), in file %s, line %d, function: %s\n"
"{\n"
ON_DEBUG("\tleft struct canary = 0x%llx\n")
"\tsize = %llu\n"
"\tcapacity = %llu\n"
ON_DEBUG("\tright struct canary = 0x%llx\n")
"\n",
ON_DEBUG(stack->name,) stack, error, stackStrError(error), filename, lineNum, functionName
ON_DEBUG(, stack->leftCanary),
stack->size,
stack->capacity
ON_DEBUG(, stack->rightCanary));
LOG(fp, "\tdata[%p]:\n"
"\n"
ON_DEBUG( "\tleft data canary = 0x%llx\n"),
stack->data ON_DEBUG(, *getCanaryLeftPtr(stack)));
for (size_t i = 0; i < stack->capacity; i++)
if (stack->data[i] == POISON)
LOG(fp, "\t\t ! [%d] %" FORMAT " (POISON!)\n", i, stack->data[i]);
else
LOG(fp, "\t\t [%d] %" FORMAT "\n", i, stack->data[i]);
ON_DEBUG(LOG(fp, "\tright data canary = 0x%llx\n", *getCanaryRightptr(stack)));
LOG(fp, "}\n");
fclose(fp);
return OK;
}
const char* getTime(void)
{
time_t t;
time(&t);
return ctime(&t);
}
#define CHECK_ERROR(EXPRESSION, ERROR, errorSum) \
if (EXPRESSION) \
{ \
errorSum += ERROR; \
}
ErrorCode stackVerify(const Stack* stack)
{
int error = 0;
CHECK_ERROR(stack == NULL, NULLPTR_STACK, error);
CHECK_ERROR(stack->data == NULL, NULLPTR_DATA, error);
CHECK_ERROR(stack->size > stack->capacity, SIZE_BIGGER_CAPACITY, error);
ON_DEBUG(CHECK_ERROR(*getCanaryLeftPtr(stack) != LEFT_DATA_CANARY, LCANARY_DATA_CHANGED, error));
ON_DEBUG(CHECK_ERROR(*getCanaryRightptr(stack) != RIGHT_DATA_CANARY, RCANARY_DATA_CHANGED, error));
ON_DEBUG(CHECK_ERROR(stack->leftCanary != LEFT_STRUCT_CANARY, LCANARY_STRUCT_CHANGED, error));
ON_DEBUG(CHECK_ERROR(stack->rightCanary != RIGHT_STRUCT_CANARY, RCANARY_STRUCT_CHANGED, error));
CHECK_ERROR(stack->capacity > MAX_STACK_SIZE, MAX_CAPACITY_OVERFLOW, error);
CHECK_ERROR(stack->capacity < DEFAULT_CAPACITY, CAPACITY_LESS_DEFAULT, error);
ON_DEBUG(CHECK_ERROR(stack->hash != hashAiden32(stack), HASH_CHANGED, error));
return error;
}
#undef CHECK_ERROR
unsigned int hashAiden32(const Stack* stack)
{
unsigned int hashSum = 1;
for (size_t i = 0; i < stack->capacity * sizeof(elem_t); i++)
{
hashSum = (hashSum + *((char*)stack->data + i)) % MOD_AIDEN;
}
hashSum += stack->size % MOD_AIDEN;
hashSum += stack->capacity % MOD_AIDEN;
return hashSum;
}