-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackfuncs.h
More file actions
186 lines (139 loc) · 4.77 KB
/
stackfuncs.h
File metadata and controls
186 lines (139 loc) · 4.77 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
#ifndef STACKFUNCS_H
#define STACKFUNCS_H
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <limits.h>
#include "userconfig.h"
typedef int ErrorCode;
typedef unsigned long long canary_t;
const uint32_t MOD_AIDEN = 64;
const int MAX_STACK_SIZE = 4000 * 8;
const int NULLPTR = 0;
const canary_t LEFT_STRUCT_CANARY = 0xDEADAAAA;
const canary_t RIGHT_STRUCT_CANARY = 0xDEADBBBB;
const canary_t LEFT_DATA_CANARY = 0xDEADEEEE;
const canary_t RIGHT_DATA_CANARY = 0xDEADFFFF;
#define LOG fprintf
#ifdef DEBUG
#define ON_DEBUG(...) \
__VA_ARGS__
#define NAME "(%s)"
#else
#define ON_DEBUG(...)
#define NAME
#endif
#ifdef INT_T
#define FORMAT "d"
typedef int elem_t;
const elem_t POISON = INT_MAX;
const int DEFAULT_CAPACITY = 2;
#endif
#ifdef FLOAT_T
#define FORMAT "f"
typedef float elem_t;
const elem_t POISON = NAN;
const int DEFAULT_CAPACITY = 2;
#endif
#ifdef DOUBLE_T
#define FORMAT "lg"
typedef double elem_t;
const elem_t POISON = NAN;
const int DEFAULT_CAPACITY = 1;
#endif
#ifdef CHAR_T
#define FORMAT "c"
typedef char elem_t;
const elem_t POISON = '!';
const int DEFAULT_CAPACITY = 8;
#endif
#ifdef STRING_T
#define FORMAT "s"
typedef char* elem_t;
const elem_t POISON = "POISON";
#endif
#define ASSERTSOFT(EXPRESSION, ERROR_CODE) \
if (!(EXPRESSION)) \
{ \
fprintf (stderr,"%s, failed at %s:%d\n", \
#ERROR_CODE, __FILE__, __LINE__); \
exit(ERROR_CODE); \
}
#define ASSERTHARD(stack) \
do \
{ \
int error = stackVerify(stack); \
if (error) \
{ \
DUMP(stack); \
exit(error); \
} \
} while(0);
#define DUMP(stack) \
do \
{ \
stackDump(stack, __FILE__, __LINE__, __func__); \
} while(0);
#define CHECK_ERROR(EXPRESSION, ERROR, errorSum) \
if (EXPRESSION) \
{ \
errorSum += ERROR; \
}
#define CreateStack(stack) \
do \
{ \
createStack(&stack ON_DEBUG(, #stack)); \
} while(0); \
enum RESIZE
{
COMPRESS = -1,
EXPAND = 1
};
enum STACK_ERRORS
{
NULLPTR_STACK = (1 << 0),
NULLPTR_DATA = (1 << 1),
SIZE_BIGGER_CAPACITY = (1 << 2),
LCANARY_DATA_CHANGED = (1 << 3),
RCANARY_DATA_CHANGED = (1 << 4),
LCANARY_STRUCT_CHANGED = (1 << 5),
RCANARY_STRUCT_CHANGED = (1 << 6),
CANARY_SIZE_CHANGED = (1 << 7),
MAX_CAPACITY_OVERFLOW = (1 << 8),
CAPACITY_LESS_DEFAULT = (1 << 9),
HASH_CHANGED = (1 << 10)
};
enum ERRORS
{
OK = 0,
NO_MEMORY = 3,
STACK_DELETED = 5,
UNABLE_TO_OPEN_FILE = 7
};
struct Stack
{
ON_DEBUG(canary_t leftCanary);
ON_DEBUG(char* name);
elem_t* data;
size_t capacity;
size_t size;
ON_DEBUG(unsigned int hash);
ON_DEBUG(canary_t rightCanary);
};
ErrorCode createStack(Stack* stk ON_DEBUG(, char* name));
elem_t* initData(void);
ErrorCode reallocStack(Stack* stk);
ErrorCode placeCanary(Stack* stk, size_t place, canary_t canary);
ErrorCode poisonFill(Stack* stk);
ErrorCode DestroyStack(Stack* stk);
ErrorCode Push(Stack* stk, elem_t value);
elem_t Pop(Stack* stk);
ErrorCode PrintStack(const Stack* stk);
const char* getTime(void);
const char* stackStrError (const int code);
ErrorCode stackVerify(const Stack* stk);
ErrorCode stackDump(const Stack* stk, const char* filename, const int lineNum, const char* functionName);
canary_t* getCanaryRightptr(const Stack* stack);
canary_t* getCanaryLeftptr(const Stack* stack);
unsigned int hashAiden32(const Stack* stack);
#endif