This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcppl_compress.cc
268 lines (223 loc) · 9.1 KB
/
cppl_compress.cc
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
// Copyright 2015-2018 RWTH Aachen University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <boost/program_options.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include "ast.hh"
#include "ast_print_visitor.hh"
#include "ast_preprocessor_visitor.hh"
#include "ast_policy_compressor_visitor.hh"
#include "binary.hh"
#include "policy_header.hh"
#include "policy_stack.hh"
#include "relation_set.hh"
#include "variable_set.hh"
#include "equation_driver.hh"
#include "debug.hh"
#include "performance.h"
namespace {
const size_t SUCCESS = 0;
const size_t ERROR_IN_COMMAND_LINE = 1;
const size_t ERROR_UNHANDLED_EXCEPTION = 2;
const size_t POLICY_ERROR = 3;
}
string getFile(string filename) {
ifstream file(filename);
if(!file.is_open())
throw runtime_error("Can't open file "+ filename);
//read the file into string
stringstream strStream;
strStream << file.rdbuf();
file.close();
return strStream.str();
}
int main (int argc, char *argv[]) {
try {
//default options that can be overwriten by the program options
bool traceParsingEnabled = false;
bool traceScanningEnabled = false;
string inputFile;
string policyDefinitionFile;
string outputFile;
//Define and parse the program options
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
("help", "Print help message")
("input-file", po::value<string>(), "input file")
("policy-definition-file", po::value<string>(), "JSON file that represents the policy definition")
("output-file,o", po::value<string>(), "Writes the compressed policy to this file (example: $(basename <input-file>).ccppl)")
("trace-parsing,p", "Print the parser trace (for debugging)")
("trace-scanning,s", "Print the scanning trace (for debugging)");
//all positional options should be translated into "input-file" options
po::positional_options_description p;
p.add("input-file", -1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
//--help option
if(vm.count("help")) {
std::cout << "Compact Privacy Policy Language Generator" << std::endl
<< desc << std::endl;
return SUCCESS;
}
//trace parsing options
if(vm.count("trace-parsing")) {
traceParsingEnabled = true;
}
//trace scanning options
if(vm.count("trace-scanning")) {
traceScanningEnabled = true;
}
//input-file
if(vm.count("input-file")) {
inputFile = vm["input-file"].as<string>();
} else {
std::cerr << "No input file!" << std::endl;
return ERROR_IN_COMMAND_LINE;
}
//policy-definition-file
if(vm.count("policy-definition-file")) {
policyDefinitionFile = vm["policy-definition-file"].as<string>();
} else {
std::cerr << "No policy definition file parameter!" << std::endl;
return ERROR_IN_COMMAND_LINE;
}
//output-file
if(vm.count("output-file")) {
outputFile = vm["output-file"].as<string>();
} else {
std::cerr << "No output file!" << std::endl;
return ERROR_IN_COMMAND_LINE;
}
po::notify(vm); // throws on error
}
catch(po::error& e) {
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
std::cerr << desc << std::endl;
return ERROR_IN_COMMAND_LINE;
}
//application code
//load the necessary files before measuring to compensate file caching
//get the policy definition from a json file
string policyDefinitionJsonString = getFile(policyDefinitionFile);
#if EVALUATION_OUTPUT
performance_start_benchmark();
#endif
//contains all information of the policy definition
PolicyDefinition policyDefinition(policyDefinitionJsonString);
#if EVALUATION_OUTPUT
performance_stop_benchmark("Gen Policy Definition time");
performance_start_benchmark();
#endif
//generate the function handler that evaluates the user defined function
FunctionHandler functionHandler(policyDefinition);
Binary genBinary(NULL);
VariableSet variableSet(policyDefinition, functionHandler, genBinary);
RelationSet relationSet(policyDefinition, variableSet, genBinary);
PolicyStack policyStack(relationSet, genBinary, 0);
#if EVALUATION_OUTPUT
performance_stop_benchmark("Gen Policy Class init time");
performance_start_benchmark();
#endif
EquationDriver eqDriver;
eqDriver.trace_parsing = traceParsingEnabled;
eqDriver.trace_scanning = traceScanningEnabled;
eqDriver.parse(inputFile);
//show an error if the policy is empty
if(eqDriver.ast == NULL) {
cerr << "Policy is empty" << endl;
return POLICY_ERROR;
}
#if EVALUATION_OUTPUT
performance_stop_benchmark("Gen Policy Parsing time");
performance_start_benchmark();
#endif
//preprocess the AST
// - check that the types inside the AST are used correctly
// - sort the children s.t. a correct PN is created by a DFS
AstPreprocessorVisitor preprocessor = AstPreprocessorVisitor(policyDefinition);
preprocessor.visit(*(eqDriver.ast));
#if EVALUATION_OUTPUT
performance_stop_benchmark("Gen AST Preprocessing time");
performance_start_benchmark();
#endif
//print the AST
#if DEBUG_AST_PRINT
AstPrintVisitor printVisitor = AstPrintVisitor();
printVisitor.visit(*(eqDriver.ast));
#endif
//generate the policy header
PolicyHeader policyHeader(genBinary, policyDefinition.getVersion());
#if EVALUATION_OUTPUT
performance_stop_benchmark("Gen Policy Header time");
performance_start_benchmark();
#endif
//compress the AST as RPN stack with equation set
AstPolicyCompressorVisitor policyCompressor = AstPolicyCompressorVisitor(policyDefinition, policyStack, relationSet);
#if DEBUG_POLICY_GENERATION
cout << "Compressing the policy: " << endl;
#endif
policyCompressor.visit(*(eqDriver.ast));
#if EVALUATION_OUTPUT
performance_stop_benchmark("Gen Policy Compressor time");
performance_start_benchmark();
#endif
#if DEBUG_POLICY_GENERATION
cout << endl;
#endif
//add the policy parts in the right order to the binary
policyHeader.store();
policyStack.store();
relationSet.store();
variableSet.store();
#if EVALUATION_OUTPUT
performance_stop_benchmark("Gen Binary Creation time");
#endif
//DEBUG output for PolicyStack sizes
#if EVALUATION_OUTPUT
policyHeader.printSize();
policyStack.printSize();
relationSet.printSize();
variableSet.printSize();
#endif
#if DEBUG_POLICY_GENERATION
//debug output for the complete policy parts
cout << "PolicyHeader: " << policyHeader.legend << endl << policyHeader << endl;
cout << "PolicyStack: " << policyStack.legend << endl << policyStack << endl;
cout << "RelationSet: " << relationSet.legend << relationSet << endl;
cout << "VariableSet: " << variableSet.legend << variableSet << endl;
#endif
#if DEBUG_POLICY_GENERATION
cout << "Policy: "<< endl;
genBinary.print();
#endif
#if EVALUATION_OUTPUT
cout << "Policy length: " << genBinary.size() << endl;
#endif
genBinary.write(outputFile);
}
catch(char const *msg) {
std::cerr << "Policy Error: " << msg << std::endl;
return POLICY_ERROR;
}
catch(std::exception& e) {
std::cerr << "Unhandled Exception reached the top of main: "
<< e.what() << ", application will now exit" << std::endl;
return ERROR_UNHANDLED_EXCEPTION;
}
return SUCCESS;
}