-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
321 lines (237 loc) · 6.52 KB
/
main.cpp
File metadata and controls
321 lines (237 loc) · 6.52 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
312
313
314
315
316
317
318
319
320
321
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <cstring>
#include <time.h>
//â äóñòðóêòîðå óäàëÿòü âîîáùå âñå, íî ñíà÷àëà èñïîðòèòü âñå äàííûå, ïîòîì ó ïåðåìåííûõ áðàòü àäðåñ è free
typedef unsigned long long ull;
class Stack {
private:
ull canary1;
int d_size_;
int number;
char* data_;
int shaf1, shaf2;
int flag;
int Hash1();
int Hash2();
int Hash3();
int Hash4();
int Hash5();
int Hash6();
int (Stack::*foo[6]) () = {&Stack::Hash1, &Stack::Hash2, &Stack::Hash3, &Stack::Hash4, &Stack::Hash5, &Stack::Hash6};
int hash1, hash2;
int Ok();
void Shafle();
ull canary2;
friend bool Check_Hash(Stack*);
friend void S_print(Stack&);
public:
Stack();
void IncData();
Stack(const Stack& object);
~Stack();
bool Push(int val);
bool Pop();
//bool Check_Canary();
};
typedef int (Stack::*pf)();
class random_int{
private:
int ran;
// friend bool Stack::Check_Canary();
public:
random_int(){
srand(time(NULL));
ran = rand() % 10000;
}
};
//const static random_int canary1;
//const static random_int canary2;
int main() { //!!!!!!!!!!!!!!!!!!
Stack obj1;
obj1.Pop();
obj1.Pop();
obj1.Push(1);
obj1.Pop();
//obj1.Push(13);
//S_print(obj1);
return 0;
} //!!!!!!!!!!!!!!!!!!
//bool Stack::Check_Canary(){
//
// if(canary1.ran != canary1 || canary2.ran != canary2)
// return false;
//
// return true;
//}
int Stack::Hash1(){
int hash_ = 0;
for(int i = 0; i <= this->number; i++){
hash_ = 111;
// hash_ = (hash_ + ((i + 1) % 131) * ((this->data_[i]) % 131)) % 131;
}
return hash_;
}
int Stack::Hash2() {
int hash_ = 0;
for (int i = 0; i <= number; i++){
hash_ = 222;
// hash_ = (hash_ + data_[i] % 131);
}
return hash_;
}
int Stack::Hash3() {
int hash_ = 2;
for (int i = 0; i <= number; i++){
hash_ = 333;
// hash_ = (hash_ * (data_[i] % 131)) % 131;
}
return hash_;
}
int Stack::Hash4() {
int hash_ = 1;
for (int i = 0; i < number; i++)
hash_ = 444;
// hash_ = (hash_ * ((i + this->data_[i]) % 131)) % 131;
return hash_;
}
int Stack::Hash5() {
int hash_ = 1;
for (int i = 0; i < number; i++)
hash_ = 555;
// hash_ = (hash_ * (data_[i] % 131)) % 131;
return hash_;
}
int Stack::Hash6() {
int hash_ = 1;
for (int i = 0; i < number; i++)
hash_ = 666;
// hash_ = (hash_ + data_[i] % 131);
return hash_;
}
bool Check_Hash(Stack* object){ //true = everything is OK; else false
pf hash1 = object->foo[object->shaf1];
pf hash2 = object->foo[object->shaf2];
int check1 = (object->*hash1)();
int check2 = (object->*hash2)();
printf("Checking: check1 = %d\tcheck2 = %d\n", check1, check2);
if (check1 == object->hash1 && check2 == object->hash2){
return true;
} else {
//printf("----ERROR----hash is not true\n");
return false;
}
}
int Stack::Ok(){ //0 åñëè âçëîìàëè äàííûå ñòåêà, -1 åñëè âçëîìàëè ìàññèâ, 1 åñëè âñå îê
if (canary1 != 88005553535 || canary2 != 53535550088)
return 0;
if ((ull)*data_ != canary1 || (ull)*(data_ + 2 + d_size_) != canary2)
return -1;
return 1;
}
//int (Stack::*Stack::Shafle())(int* shaf) {
void Stack::Shafle (){
int ran1 = rand() % 6;
shaf1 = ran1;
int ran2 = rand() % 6;
shaf2 = ran2;
}
Stack::Stack(){
flag = 0;
canary1 = 88005553535;
canary2 = 53535550088;
d_size_ = 20;
number = sizeof(ull);
printf("construct\n");
data_ = (char*) calloc (d_size_ * sizeof(int) + 2 * sizeof (ull), sizeof(char));
assert(data_ != NULL);
*data_ = canary1;
*(data_ + sizeof(ull) + d_size_ * sizeof (int)) = canary2;
srand(time(NULL));
shaf1 = 0;
shaf2 = 1;
pf p1 = foo[shaf1];
pf p2 = foo[shaf2];
hash1 = (this->*p1)();
hash2 = (this->*p2)();
for (int i = 0; i < 20; i++)
Push(i + 1);
//for(int i = 0; i < d_size_; i++)
// printf("%d ", data_[i]);
printf("\n");
}
Stack::Stack(const Stack& object){
if (flag == 1)
return;
flag++;
Stack fake_obj = object;
flag = 0;
if (!Check_Hash(&fake_obj))
return;
printf ("Copy\n d_size = ");
printf("%d", object.d_size_);
printf("\n");
canary1 = object.canary1;
d_size_ = object.d_size_;
number = object.number;
hash1 = object.hash1;
hash2 = object.hash2;
shaf1 = object.shaf1;
shaf2 = object.shaf2;
flag = object.flag;
canary2 = object.canary2;
data_ = (char*) calloc (d_size_ * sizeof(int) + 2 * sizeof (ull), sizeof(char));
assert(data_ != NULL);
*data_ = canary1;
for(unsigned i = sizeof(ull); i < sizeof (ull) + d_size_ * sizeof (int); i += sizeof (int))
data_[i] = object.data_[i];
*(data_ + sizeof (ull) + d_size_ * sizeof (int)) = canary2;
}
void Stack::IncData(){
data_ = (char*) realloc (data_, (d_size_ + sizeof (ull) / sizeof (int)) * sizeof (int));
d_size_ += 10;
data_ = (char*) realloc (data_, (d_size_ + 2 * sizeof (ull) / sizeof (int)) * sizeof (int));
*(data_ + 2 + d_size_) = canary2;
}
Stack::~Stack(){
if (!Check_Hash(this))
return;
printf("distruct\n");
free(data_);
}
bool Stack::Push(int val){
if (this->number > (int)sizeof(ull)){
if(!Check_Hash(this))
return false;
}
if (number == (int) (sizeof(ull) + d_size_ * sizeof (int)))
this->IncData();
this->data_[this->number] = val;
number += sizeof (int);
Shafle();
pf p1 = foo[shaf1];
pf p2 = foo[shaf2];
hash1 = (this->*p1)();
hash2 = (this->*p2)();
return true;
}
bool Stack::Pop(){
if(!Check_Hash(this))
return false;
int num = (number - sizeof (ull)) / sizeof (int);
printf("data[%d] = %d\n", num, this->data_[this->number - sizeof(int)]);
number -= sizeof (int);
Shafle();
pf p1 = foo[shaf1];
pf p2 = foo[shaf2];
hash1 = (this->*p1)();
hash2 = (this->*p2)();
return true;
}
void S_print(Stack& obj){
for (int i = 0; i <= obj.number; i++){
printf("%d ", obj.data_[i]);
}
printf("\n");
}