-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitvector.cpp
More file actions
324 lines (269 loc) · 5.92 KB
/
Copy pathbitvector.cpp
File metadata and controls
324 lines (269 loc) · 5.92 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
322
323
#include "bitvector.h"
bitvector::bitvector()
{
int i;
for(i = 0; i< 128; i++)
byte[i] = 0;
}
int bitvector::set(int pos)
{
int index = 127 - pos/8;
int offset = 7 - pos%8;
char old;
char k = 0x01;
if (pos > 1023 || pos < 0)
return -1;
k = k << (7-offset);
old = byte[index];
byte[index] = (old | k);
return 1;
}
int bitvector::set(string kwrd) {
// Taken from Akshat's code
unsigned char str_buf[20];
unsigned char md5_out[16];
transform(kwrd.begin(), kwrd.end(), kwrd.begin(), (int(*)(int)) std::tolower);
char *p = (char *)calloc(kwrd.length(), sizeof(char));
strcpy(p, kwrd.c_str());
//fprintf(fp,"%s ", p);
unsigned int value;
SHA1((unsigned char *)p, strlen(p), str_buf);
value = str_buf[18];
value = value & 1;
value = value << 8;
value = value | str_buf[19];
set(512 + value);
MD5((unsigned char *)p, strlen(p), md5_out);
value = md5_out[14];
value = value & 1;
value = value << 8;
value = value | md5_out[15];
set(value);
return 0;
}
// To set bitvector directly from the metafile.
// CAUTION: Overwrite to the previous data!
//
int bitvector::set(char* hex)
{
char* p = hex;
unsigned char hex1;
unsigned char hex2;
unsigned char _byte;
for(int i = 0; i < 128; i++) {
hex1 = *p++;
hex2 = *p++;
if (isupper(hex1)) {
hex1 = 10 + hex1 - 'A';
}
else if(islower(hex1)) {
hex1 = 10 + hex1 - 'a';
}
else {
hex1 -= '0';
}
if (isupper(hex2)) {
hex2 = 10 + hex2 - 'A';
}
else if(islower(hex2)) {
hex2 = 10 + hex2 - 'a';
}
else {
hex2 -= '0';
}
hex1 = hex1 << 4;
_byte = (unsigned char) hex1 | (unsigned char) hex2;
byte[i] = _byte;
}
return 1;
}
int bitvector::reset(int pos)
{
int index = 127 - pos/8;
int offset = 7 - pos%8;
unsigned char k = 0x01;
unsigned char l = 0xff;
unsigned char old = byte[index];
if(pos > 1023 || pos < 0)
return -1;
k = k << (7-offset);
l = l ^ k;
byte[index] = (old & l);
return 1;
}
bitvector bitvector::bool_and(bitvector b)
{
bitvector c;
for(int i = 0; i< 128; i++)
c.byte[i] = byte[i] & b.byte[i];
return c;
}
bitvector bitvector::bool_or(bitvector b)
{
bitvector c;
for(int i = 0; i< 128; i++)
c.byte[i] = byte[i] | b.byte[i];
return c;
}
bool bitvector::is_equal(bitvector b)
{
for(int i = 0; i< 128; i++) {
if(byte[i] != b.byte[i])
return 0;
}
return 1;
}
bool bitvector::is_hit(bitvector& b)
{
bitvector match = bool_and(b);
if(match.is_equal(b)) {
return true;
}
else {
return false;
}
}
bool bitvector::operator<(const bitvector &right) const // To be used as a key in STL map.
{
//return strcmp((char*)byte, (char*)right.byte);
int i=0;
while(byte[i] == right.byte[i])
i++;
if(i<128) {
if(byte[i] < right.byte[i])
return 1;
else
return 0;
}
return 0;
}
bool bitvector::operator==(bitvector &right)
{
return is_hit(right);
}
//bool bitvector::operator!=(bitvector &right)
//{
// return not is_hit(right);
//}
void bitvector::print_hex()
{
int i;
for(i = 0; i< 128; i++)
printf("%02x", byte[i]);
printf("\n");
}
void bitvector::print_hex(FILE *fp)
{
int i;
for(i = 0; i< 128; i++)
fprintf(fp, "%02x", byte[i]);
}
void bitvector::read_hex(FILE *fp)
{
char hex[257] = "";
fscanf(fp, "%s", hex);
set(hex);
}
void bitvector::print()
{
int i;
unsigned char val, k;
int j;
for(i = 0; i<128; i++) {
k = 0x80;
for(j = 0; j<8; j++) {
val = byte[i];
val = (val & k);
val = val >> (7 - j);
if(val)
printf("1");
else
printf("0");
k = k >> 1;
}
}
printf("\n");
}
/*
// Test case for set(char*)
// Bob 11-19-2008
int main() {
bitvector vec0;
bitvector vec1;
bitvector vec2;
bitvector vec3;
char hex0[1000]=
"112233445566778899aabbccddeeffAABBCCDDEEFF0102030405060708090A0B0C0D0E0F102030485060708090A0B0C0D\
0E0F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000";
char hex1[1000]=
"0123456789abcdef000000000000000000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000";
char hex2[1000]=
"00000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000";
char hex3[1000]=
"1240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000";
vec0.set(hex0);
vec0.print();
vec0.print_hex();
vec1.set(hex1);
vec2.set(hex2);
vec3.set(hex3);
if(vec1 == vec2) {
printf("vec1 hits vec2.\n");
}
if(vec1 == vec3) {
printf("vec1 hits vec3.\n");
}
return 0;
}
*/
/*
int main()
{
bitvector obj, obj1;
int i=0;
// for(i = 0; i< 1024; i=i+1)
obj1.set(i);
/ *
for(i = 0; i<1024; i = i+3)
obj.reset(i);
obj.print();
printf("\n\n\n");
for(i = 0; i< 128; i++)
printf("%02x", obj.byte[i]);
unsigned char k = 0xf0 | 0x0f;
printf("\n%02x\n", k);
// * /
* /
string s = "categories";
unsigned char str_buf[20];
char *p;
p = (char *)calloc(s.length(), sizeof(char));
strcpy(p, s.c_str());
SHA1((unsigned char *)p, strlen(p), str_buf);
unsigned int value;
value = str_buf[18];
value = value & 1;
value = value << 8;
value = value | str_buf[19];
obj.set(512 + value);
unsigned char md5_out[16];
MD5((unsigned char *)p, strlen(p), md5_out);
value = md5_out[14];
value = value & 1;
value = value << 8;
value = value | md5_out[15];
obj.set(value);
bitvector result = obj1.bool_and(obj);
result.print_hex();
cout<<endl<<endl<<result.is_equal(obj)<<endl<<endl;
obj.print_hex();
return 1;
}
*/