-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtection.cpp
More file actions
80 lines (49 loc) · 2.44 KB
/
Protection.cpp
File metadata and controls
80 lines (49 loc) · 2.44 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
#include <stdio.h>
#include "Types.h"
#include "Stack.h"
#include "Protection.h"
#include "Logfile.h"
hash_t data_hash_calculate(const struct Stack *stk) {
hash_t hash = 5381;
char *data_byte_ptr = (char *) stk->data;
size_t data_byte_size = (size_t) stk->size * sizeof(elem_t);
for(size_t i = 1; i <= data_byte_size; ++i) {
hash = ((hash << 5) + hash) ^ data_byte_ptr[i];
}
return hash;
}
hash_t struct_hash_calculate(const struct Stack *stk) {
hash_t hash = 5381;
hash = data_hash_calculate(stk);
hash = ((hash << 5) + hash) ^ (char)stk->size;
hash = ((hash << 5) + hash) ^ (char)stk->capacity;
hash = ((hash << 5) + hash) ^ (hash_t)&stk->name;
return hash;
}
int stack_verify (const struct Stack *stk) {
int ans = 0;
if (stk == NULL) ans = ans | StackNull;
if (stk->data == NULL) ans = ans | DataNull;
if (stk->size < 0) ans = ans | NegativeSize;
if (stk->capacity < 0) ans = ans | NegativeCapacity;
if (stk->capacity < stk->size) ans = ans | SmallCapacity;
USE_CANARIES(if (stk->canary1 != CanaryStack || stk->canary2 != CanaryStack ||
*(canary_t *)(stk->data - 1) != CanaryData ||
*(canary_t *)(stk->data + stk->capacity * sizeof(elem_t)) != CanaryData) {
ans = ans | IncorrectCanary;
})
USE_HASH(if (stk->data_hash != data_hash_calculate(stk)) ans = ans | IncorrectHash;
if (stk->struct_hash != struct_hash_calculate(stk)) ans = ans | IncorrectHash;)
return ans;
}
void print_errors(const struct Stack *stk, int err) {
if (err & StackNull) fprintf(LOG_FILE, "ERROR! Pointer to stk is NULL\n\n");
if (err & DataNull) fprintf(LOG_FILE, "ERROR! Pointer to stk.data is NULL\n\n");
if (err & NegativeSize) fprintf(LOG_FILE, "ERROR! size < 0\n\n");
if (err & NegativeCapacity) fprintf(LOG_FILE, "ERROR! capacity < 0\n\n");
if (err & SmallCapacity) fprintf(LOG_FILE, "ERROR! size > capacity \n\n");
USE_CANARIES(if (err & IncorrectCanary) fprintf(LOG_FILE,
"ERROR! Value of canary has been changed\n\n");)
USE_HASH(if (err & IncorrectHash) fprintf(LOG_FILE,
"ERROR! Value of hash has been changed\n\n");)
}