-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_definitions.h
442 lines (396 loc) · 14.7 KB
/
class_definitions.h
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// C header files
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <utility>
// Include guard
#ifndef CLASS_DEFINITIONS_H_
#define CLASS_DEFINITIONS_H_
unordered_map<string,int> OpType = {
{"OR",0},
{"AND",1},
{"ATTR",2},
{"THRESHOLD",3},
{"CONDITIONAL",4},
{"NONE",5}
};
class BinNode{
private:
string node_type;
string attribute;
int index;
bool negated;
BinNode* left;
BinNode* right;
public:
// string value;
BinNode(string value, BinNode *leftn = NULL, BinNode *rightn = NULL){
if(value=="and"||value=="AND")
node_type = "AND";
else if(value=="or"||value=="OR")
node_type = "OR";
else{
negated = false;
index = -1; //None
// int index= NULL;
if(value[0]=='!'){
value = value.substr(1, value.size());
negated = true;
}
size_t pos = value.find('_');
if(pos !=string::npos)
{
// cout<<value<<endl;
string rest = value.substr(pos+1, value.size());
value = value.substr(0, pos);
pos = rest.find('_');
index = pos != string::npos ? stoi(rest.substr(0,pos)) : stoi(rest);
}
node_type = "ATTR";
transform(value.begin(), value.end(), value.begin(), ::toupper);
attribute = value;
}
left = leftn;
right = rightn;
}
string getAttribute(){
string return_val = "";
string attr = "ATTR";
if(node_type==attr){
if(negated)
return_val += '!';
return_val += attribute;
}
return return_val;
}
string getAttributeAndIndex(){
// cout<<endl<<index<<attribute<<endl;
string return_val = "";
string attr = "ATTR";
if(node_type==attr){
if(negated)
return_val += '!';
return_val += attribute;
if(index != -1)
return_val += '_'+to_string(index);
}
return return_val;
}
string getNodeType(){
return node_type;
}
BinNode* getLeft(){
return left;
}
BinNode* getRight(){
return right;
}
int getIndex(){
return index;
}
void setIndex(int value){
index = value;
}
void addSubNode(BinNode *leftn, BinNode *rightn){
left = leftn;
right = rightn;
}
};
class PolicyParser{
public:
bool verbose;
PolicyParser(bool v = false){
verbose = v;
}
vector<string> get_tokens(string line){
bool negated = false;
char ch;
int i= 0, level= 0;
string attr;
vector<string> tokens;
do{
ch = line[i];
if(ch=='(')
tokens.push_back("(");
else if(ch==')')
tokens.push_back(")");
else if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9') || ch=='!'){
if((ch=='a' || ch=='A') && (line[i+1]=='n' || line[i+1]=='N') && (line[i+2]=='d' || line[i+2]=='D')){
tokens.push_back("AND");
i+=2;
level++;
}
else if((ch=='o' || ch=='O') && (line[i+1]=='r' || line[i+1]=='R')){
tokens.push_back("OR");
i+=1;
level++;
}
else{
attr = ch;
i++;
bool parenEnd = false;
while( ((ch = line[i])!=')') && ch!=' ' && ch!= '\n' ){
attr.append(1,ch);
i++;
if(line[i]==')'){
parenEnd = true;
}
}
tokens.push_back(attr);
if(parenEnd)
tokens.push_back(")");
negated = false;
}
}
else if(ch==' ') ;
i++;
}while(line[i]);
return tokens;
}
BinNode* S(int* index, vector<string> tokens){
// cout<<"S "<<*index<<" "<<tokens[*index]<<endl;
BinNode *left = T(index, tokens);
if(*index < tokens.size() && tokens[*index]=="OR"){
(*index)++;
BinNode *right = S(index, tokens);
return new BinNode("OR", left, right);
}
return left;
}
BinNode* T(int* index, vector<string> tokens){
// cout<<"T "<<*index<<" "<<tokens[*index]<<endl;
BinNode *left = F(index, tokens);
if(*index < tokens.size() && tokens[*index]=="AND"){
(*index)++;
BinNode *right = T(index, tokens);
return new BinNode("AND", left, right);
}
return left;
}
BinNode* F(int* index, vector<string> tokens){
// cout<<"F "<<*index<<" "<<tokens[*index]<<endl;
string token = tokens[*index];
(*index)++;
BinNode *node;
if(token=="("){
node = S(index, tokens);
(*index)++;
}
else
node = new BinNode(token);
return node;
}
BinNode* parse(string line){
/*
S → T '+' S | T
T → F '.' T | F
F → A | '('S')'
A → 'a' | 'b' | 'c' | ... | ɛ
*/
vector<string> tokens = get_tokens(line);
int index = 0;
return S(&index, tokens);
}
void findDuplicates(BinNode* tree, unordered_map<string,int> &dict){
if(tree->getLeft())
findDuplicates(tree->getLeft(), dict);
if(tree->getRight())
findDuplicates(tree->getRight(), dict);
string attr = "ATTR";
if(tree->getNodeType() == attr){
string key = tree->getAttribute();
dict[key] = dict.find(key) == dict.end() ? 1 : dict[key]+1;
}
}
void labelDuplicates(BinNode* tree, unordered_map<string,int> &dictLabel){
if(tree->getLeft())
labelDuplicates(tree->getLeft(), dictLabel);
if(tree->getRight())
labelDuplicates(tree->getRight(), dictLabel);
string attr = "ATTR";
if(tree->getNodeType() == attr){
string key = tree->getAttribute();
if(dictLabel.find(key) != dictLabel.end()){
tree->setIndex(dictLabel[key]);
dictLabel[key]++;
}
}
}
pair<bool, vector<BinNode*>> requiredAttributes(BinNode *tree, vector<string> &attrList){
vector<BinNode*> sendThis;
if(!tree)
return make_pair(false, sendThis);
// cout<<tree->getAttributeAndIndex();
// for(auto i: attrList)
// cout<<i<<endl;
// cout<<endl;
BinNode *left = tree->getLeft();
BinNode *right = tree-> getRight();
pair<bool, vector<BinNode*>> return_pair_left, return_pair_right;
if(left)
return_pair_left = requiredAttributes(left, attrList);
if(right)
return_pair_right = requiredAttributes(right, attrList);
string type = tree->getNodeType();
string attr = "ATTR";
string or_node = "OR";
string and_node = "AND";
if(type == or_node){
if(return_pair_left.first)
sendThis = return_pair_left.second;
else if(return_pair_right.first)
sendThis = return_pair_right.second;
return make_pair(return_pair_left.first||return_pair_right.first, sendThis);
}
else if(type == and_node){
if(return_pair_left.first&&return_pair_right.first){
sendThis.reserve(return_pair_left.second.size() + return_pair_right.second.size());
sendThis.insert(sendThis.end(), return_pair_left.second.begin(), return_pair_left.second.end());
sendThis.insert(sendThis.end(), return_pair_right.second.begin(), return_pair_right.second.end());
}
else if(return_pair_left.first){
sendThis.reserve(return_pair_left.second.size());
sendThis.insert(sendThis.end(), return_pair_left.second.begin(), return_pair_left.second.end());
}
else if(return_pair_right.first){
sendThis.reserve(return_pair_right.second.size());
sendThis.insert(sendThis.end(), return_pair_right.second.begin(), return_pair_right.second.end());
}
return make_pair(return_pair_left.first&&return_pair_right.first, sendThis);
}
else if(type == attr){
if(find(attrList.begin(), attrList.end(), tree->getAttribute())!=attrList.end()){
sendThis.push_back(tree);
return make_pair(true, sendThis);
}
else
return make_pair(false, sendThis);
}
return make_pair(false, sendThis);
}
vector<BinNode*> prune(BinNode *tree, vector<string> attributes){
pair<bool, vector<BinNode*>> return_pair = requiredAttributes(tree, attributes);
vector<BinNode*> emptyPrunedList;
if(!return_pair.first)
return emptyPrunedList;
return return_pair.second;
}
};
class Schemebase{
public:
unordered_map<string, string> properties;
protected:
bool setProperty(string scheme = ""){
if(scheme.compare("")){
properties["scheme"] = scheme;
}
return true;
}
unordered_map<string, string> getProperty(){
return properties;
}
};
class ABEnc: public Schemebase{
unordered_map<string, int> baseSecDefs;
ABEnc(){
setProperty("ABEnc");
baseSecDefs = {
{"IND_AB_CPA", 0},
{"IND_AB_CCA", 1},
{"sIND_AB_CPA", 2},
{"sIND_AB_CCA", 3}
};
}
};
class MSP{
public:
int len_longest_row;
MSP(){
len_longest_row = 1;
}
BinNode* createPolicy(string policy_string){
PolicyParser parser = PolicyParser();
BinNode *policy_obj = parser.parse(policy_string);
unordered_map<string,int> dictCount, dictLabel;
parser.findDuplicates(policy_obj, dictCount);
for(auto i:dictCount)
if(i.second>1)
dictLabel[i.first] = 0;
parser.labelDuplicates(policy_obj, dictLabel);
return policy_obj;
}
unordered_map<string, vector<int>> convertPolicyToMSP(BinNode *tree){
vector<int> root_vector(1,1);
len_longest_row = 1;
return convertPolicyToMSP_Helper(tree, root_vector);
}
vector<BinNode*> prune(BinNode *policy, vector<string> attributes){
PolicyParser parser = PolicyParser();
return parser.prune(policy, attributes);
}
vector<string> getAttributeList(BinNode *tree){
vector<string> return_vector;
getAttributeListHelper(tree, return_vector);
return return_vector;
}
protected:
unordered_map<string, vector<int>> convertPolicyToMSP_Helper(BinNode *subtree, vector<int> &curr_vector){
unordered_map<string, vector<int>> um;
if(!subtree)
return um;
string type = subtree->getNodeType();
string attr = "ATTR";
string or_node = "OR";
string and_node = "AND";
if(type == attr){
um[subtree->getAttributeAndIndex()] = curr_vector;
return um;
}
else if(type == or_node){
unordered_map<string, vector<int>> left_dict, right_dict;
left_dict = convertPolicyToMSP_Helper(subtree->getLeft(), curr_vector);
right_dict = convertPolicyToMSP_Helper(subtree->getRight(), curr_vector);
left_dict.insert(right_dict.begin(), right_dict.end());
return left_dict;
}
else if(type == and_node){
int length = curr_vector.size();
vector<int> left_vector = curr_vector;
for(int i=0; i<len_longest_row-length; i++)
left_vector.push_back(0);
left_vector.push_back(1);
vector<int> right_vector(len_longest_row,0);
right_vector.push_back(-1);
len_longest_row++;
unordered_map<string, vector<int>> left_dict, right_dict;
left_dict = convertPolicyToMSP_Helper(subtree->getLeft(), left_vector);
right_dict = convertPolicyToMSP_Helper(subtree->getRight(), right_vector);
left_dict.insert(right_dict.begin(), right_dict.end());
return left_dict;
}
return um;
}
void getAttributeListHelper(BinNode *tree, vector<string> &list){
if(tree){
string type = tree->getNodeType();
string attr = "ATTR";
if(type == attr){
list.push_back(tree->getAttributeAndIndex());
}
else{
getAttributeListHelper(tree->getLeft(), list);
getAttributeListHelper(tree->getRight(), list);
}
}
}
string strip_index(string node_str){
size_t pos = node_str.find('_');
if(pos !=string::npos){
return node_str.substr(0,pos);
}
return node_str;
}
};
#endif