-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
260 lines (207 loc) · 5.32 KB
/
Copy pathStack.c
File metadata and controls
260 lines (207 loc) · 5.32 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
#ifndef STACK
#define STACK
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
HASH_SUM_TYPE HashSum (const DATA_TYPE x);
int CheckData (DATA_TYPE *data);
stack *CreateStack (DATA_TYPE x)
{
int check_stack = 0;
char *canary = NULL;
HASH_SUM_TYPE *hash_sum = NULL;
stack *new_stk = (stack *) calloc (1, sizeof (*new_stk));
if (new_stk == NULL)
{
fprintf (stderr, "Can't allocate memory\n");
exit (EXIT_FAILURE);
}
canary = (char *) calloc (1, sizeof (x) + sizeof (*hash_sum) + 2 * sizeof (*canary));
if (canary == NULL)
{
fprintf (stderr, "Can't allocate memory\n");
exit (EXIT_FAILURE);
}
*canary = CANARY_SYMB;
new_stk->data = (DATA_TYPE *) (canary + sizeof (*canary));
memcpy (new_stk->data, &x, sizeof (x));
hash_sum = (HASH_SUM_TYPE *) (canary + sizeof (x) + sizeof (*canary));
*hash_sum = HashSum (x);
canary = (char *) hash_sum + sizeof (*hash_sum);
*canary = CANARY_SYMB;
new_stk->next = NULL;
check_stack = Verify (new_stk);
if (check_stack)
{
fprintf (stderr, "ERROR: Verification failed, \"CreateStack\" function, code %d\n", check_stack);
DumpElem (new_stk);
exit (EXIT_FAILURE);
}
return new_stk;
}
stack *Push (DATA_TYPE x, stack *stk)
{
int check_stack = 0;
char *canary = NULL;
HASH_SUM_TYPE *hash_sum = NULL;
stack *new_elem = NULL;
check_stack = Verify (stk);
if (check_stack)
{
fprintf (stderr, "ERROR: Verification failed\n\"Push\" function: incorrect stack as argument, code %d\n", check_stack);
DumpElem (stk);
exit (EXIT_FAILURE);
}
new_elem = (stack *) calloc (1, sizeof (*new_elem));
if (new_elem == NULL)
{
fprintf (stderr, "Can't allocate memory\n");
exit (EXIT_FAILURE);
}
canary = (char *) calloc (1, sizeof (x) + sizeof (*hash_sum) + 2 * sizeof (*canary));
if (canary == NULL)
{
fprintf (stderr, "Can't allocate memory\n");
exit (EXIT_FAILURE);
}
*canary = CANARY_SYMB;
new_elem->data = (DATA_TYPE *) (canary + sizeof (*canary));
memcpy (new_elem->data, &x, sizeof (x));
hash_sum = (HASH_SUM_TYPE *) (canary + sizeof (x) + sizeof (*canary));
*hash_sum = HashSum (x);
canary = (char *) hash_sum + sizeof (*hash_sum);
*canary = CANARY_SYMB;
new_elem->next = stk;
check_stack = Verify (new_elem);
if (check_stack)
{
fprintf (stderr, "ERROR: Verification failed\n\"Push\" function: error occured during pushing new element, code %d\n", check_stack);
DumpElem (new_elem);
exit (EXIT_FAILURE);
}
return new_elem;
}
DATA_TYPE Pop (stack **stk)
{
ASSERT (stk);
int check_stack = 0;
DATA_TYPE x = 0;
stack *cur_stk = *stk;
check_stack = Verify (cur_stk);
if (check_stack)
{
fprintf (stderr, "ERROR: Verification failed\n\"Pop\" function: incorrect stack as argument, code %d\n", check_stack);
DumpElem (cur_stk);
exit (EXIT_FAILURE);
}
if (cur_stk == NULL)
{
fprintf (stderr, "Attempt to read from empty stack\n");
exit (EXIT_FAILURE);
}
x = *cur_stk->data;
*stk = cur_stk->next;
free ((char *) cur_stk->data - 1);
free (cur_stk);
if (*stk)
{
check_stack = Verify (*stk);
if (check_stack)
{
fprintf (stderr, "ERROR: Verification failed\n\"Pop\" function: incorrect stack as argument, code %d\n", check_stack);
DumpElem (*stk);
exit (EXIT_FAILURE);
}
}
return x;
}
void DeleteStack (stack **stk)
{
ASSERT (stk);
int check_stack = 0;
check_stack = Verify (*stk);
if (check_stack)
{
fprintf (stderr, "ERROR: Verification failed\n\"DeleteStack\" function: incorrect stack as argument, code %d\n", check_stack);
DumpElem (*stk);
exit (EXIT_FAILURE);
}
if (*stk)
{
if ((*stk)->next)
DeleteStack (&((*stk)->next));
free ((char *) (*stk)->data - 1);
free (*stk);
}
}
int Verify (stack *stk)
{
ASSERT (stk);
int res = 0;
if (stk->data)
{
res = CheckData (stk->data);
}
else
{
fprintf (stderr, "ERROR:\nVerify function: NULL ptr instead of data\n");
res = 1;
}
return (stk->next) ? Verify (stk->next) || res : res;
}
HASH_SUM_TYPE HashSum (const DATA_TYPE x)
{
int i = 0;
HASH_SUM_TYPE hash = 0;
char cur_symb = 0;
int positive = 0;
for (i = 0; i < sizeof (DATA_TYPE); i++)
{
cur_symb = *(((char *) &x) + i);
positive = ((int) cur_symb >= 0) ? (int) cur_symb : 128 - (int) cur_symb ;
hash += (i + 1) * (HASH_SUM_TYPE) positive;
}
return hash;
}
int DumpElem (stack *elem)
{
ASSERT (elem);
if (elem->data)
{
printf ("------------------------\n\
Stack element dump:\n\
canary 1: %c\n\
canary 2: %c\n\
specified hash sum: %ld\n\
real hash sum: %ld\n\
-------------------------\n", *((char *) elem->data - 1), *((char *) elem->data + sizeof (DATA_TYPE) + sizeof (HASH_SUM_TYPE)), * (HASH_SUM_TYPE *) ((char *) elem->data + sizeof (DATA_TYPE)), HashSum (*(elem->data)));
return 0;
}
else
return 1;
}
int CheckData (DATA_TYPE *data)
{
int res = 0;
char *canary = NULL;
HASH_SUM_TYPE hash_sum = 0;
canary = (char *) data - 1;
if (*canary != CANARY_SYMB)
{
fprintf (stderr, "ERROR:\nCheckData function: wrong value of first canary\n");
res = 1;
}
if (HashSum (*data) != * (HASH_SUM_TYPE *) ((char *) data + sizeof (DATA_TYPE)))
{
fprintf (stderr, "ERROR:\nCheckData function: wrong hash sum\n");
res = 1;
}
canary = (char *) data + sizeof (DATA_TYPE) + sizeof (HASH_SUM_TYPE);
if (*canary != CANARY_SYMB)
{
fprintf (stderr, "ERROR:\nCheckData function: wrong value of second canary\n");
res = 1;
}
return res;
}
#endif