GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cppl/policy_definition.cc at master · SSICLOPS/cppl
This repository was archived by the owner on Jan 26, 2021. It is now read-only.
Expand file tree/ Copy pathpolicy_definition.ccMore file actions 477 lines (423 loc) · 20.4 KB / Copy pathpolicy_definition.ccFile metadata and controls477 lines (423 loc) · 20.4 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 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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 // 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 "policy_definition.hh" //string replace from stack overflow void stringReplace(std::string& str, const std::string& oldStr, const std::string& newStr) { size_t pos = 0; while((pos = str.find(oldStr, pos)) != std::string::npos){ str.replace(pos, oldStr.length(), newStr); pos += newStr.length(); } } PolicyDefinition::PolicyDefinition(string definitionString) { //parse the policy defintion JSON string Json::Value root; Json::Reader reader; if(!reader.parse(definitionString.c_str(), root)) throw runtime_error("Parsing of policy definition failed: " + reader.getFormattedErrorMessages()); //get the policy version version = root["version"].asInt64(); //check if there is a variable list if(root.isMember("variables") == false) throw "Policy definition contains no variables object"; //fills the variable list variableCounter = 0; currentContainerPrefix = ""; //ensure that the starting container prefix is empty for(Json::ValueIterator it = root["variables"].begin() ; it != root["variables"].end() ; it++ ) parseJsonVariableList(*it); //calculate how much bits are needed to store the variable positions bitsForVariablePosition = ceil(log2(variables.size())); } void _FreeVariableVector(vector<Variable> & v){ for (vector<Variable>::iterator v_it = v.begin(); v_it != v.end(); ++v_it){ if (v_it->type == VariableSetType::STRING && (v_it->value).asString != NULL) delete (v_it->value).asString; else if (v_it->isFunction()){ _FreeVariableVector(*(v_it->funcParams)); delete (v_it->funcParams); } } } template<typename T> void _FreeVariableMap(map<T, vector<Variable>> & m){ for (typename map<T, vector<Variable>>::iterator m_it = m.begin(); m_it != m.end(); ++m_it){ _FreeVariableVector(m_it->second); } } PolicyDefinition::~PolicyDefinition(){ _FreeVariableMap<uint64_t>(enumValues); _FreeVariableMap<FunctionEnumPair>(functionEnumValues); _FreeVariableMap<uint64_t>(functionParameters); } //parses the variables object of the policy definition json //by recursion s.t. nested variable containers are supported void PolicyDefinition::parseJsonVariableList(Json::Value element) { if(element.isObject() && element.isMember("name") && element.isMember("type")) { //is variable object Variable var; if(element["type"].asString().compare("string") == 0) { var.type = VariableSetType::STRING; var.value.asString = new string("undefined"); } else if(element["type"].asString().compare("boolean") == 0) { var.type = VariableSetType::BOOLEAN; var.value.asBoolean = false; } else if(element["type"].asString().compare("function") == 0) { var.type = VariableSetType::FUNCTION; var.value.asInt32 = 0; } //signed integers else if(element["type"].asString().compare("int64") == 0) { var.type = VariableSetType::INT64; var.value.asInt64 = 0; } else if(element["type"].asString().compare("int32") == 0) { var.type = VariableSetType::INT32; var.value.asInt32 = 0; } else if(element["type"].asString().compare("int16") == 0) { var.type = VariableSetType::INT16; var.value.asInt16 = 0; } else if(element["type"].asString().compare("int8") == 0) { var.type = VariableSetType::INT8; var.value.asInt8 = 0; } //unsigned integers else if(element["type"].asString().compare("uint32") == 0) { var.type = VariableSetType::UINT32; var.value.asUInt32 = 0; } else if(element["type"].asString().compare("uint16") == 0) { var.type = VariableSetType::UINT16; var.value.asUInt16 = 0; } else if(element["type"].asString().compare("uint8") == 0) { var.type = VariableSetType::UINT8; var.value.asUInt8 = 0; } else throw "Policy Definition: Unknown variable type"; //get the parameters for functions if(var.type == VariableSetType::FUNCTION) { uint64_t functionParameterCounter = 0; //used as ids for parameters if(element.isMember("parameters")) { if(element["parameters"].isArray() == false) throw "Policy Definition: A parameters member of a function is not an array"; //get and store the types of the parameters for the function for(Json::ValueIterator it = element["parameters"].begin() ; it != element["parameters"].end() ; it++) { Variable parameter; //is the parameter an enum? //XXX currently only function parameter enums with type string are supported if((*it).isArray() == true) { parameter.isEnum = true; parameter.type = VariableSetType::ENUM_VALUE; //get enum values for(Json::ValueIterator enumIt = (*it).begin() ; enumIt != (*it).end() ; enumIt++) { if((*enumIt).isString() == false) throw "Policy Definition: A function parameter enum is only implemented for string values"; Variable enumVar; enumVar.isEnum = false; enumVar.type = VariableSetType::STRING; enumVar.value.asString = new string((*enumIt).asString()); functionEnumValues[FunctionEnumPair(variableCounter, functionParameterCounter)].push_back(enumVar); } } //non enum function parameters else if((*it).isString() == true) { parameter.isEnum = false; if((*it).asString().compare("string") == 0) parameter.type = VariableSetType::STRING; else if((*it).asString().compare("boolean") == 0) parameter.type = VariableSetType::BOOLEAN; //signed integers else if((*it).asString().compare("int64") == 0) parameter.type = VariableSetType::INT64; else if((*it).asString().compare("int32") == 0) parameter.type = VariableSetType::INT32; else if((*it).asString().compare("int16") == 0) parameter.type = VariableSetType::INT16; else if((*it).asString().compare("int8") == 0) parameter.type = VariableSetType::INT8; //unsigned integers else if((*it).asString().compare("uint32") == 0) parameter.type = VariableSetType::UINT32; else if((*it).asString().compare("uint16") == 0) parameter.type = VariableSetType::UINT16; else if((*it).asString().compare("uint8") == 0) parameter.type = VariableSetType::UINT8; //double else if((*it).asString().compare("double") == 0) parameter.type = VariableSetType::DOUBLE; else throw "Policy Definition: Unkown function parameter type"; } else throw "Policy Definition: Function parameter type must be a string or an enum array"; functionParameters[variableCounter].push_back(parameter); functionParameterCounter++; } } else throw "Policy Definition: A function has no parameters."; } //the variable is an enum, if it has an array of values and is not a function if(var.type == VariableSetType::FUNCTION && element.isMember("values")) throw "Policy Definition: A function can not have enum values."; else if(element.isMember("values")) { //check that values is an array if(element["values"].isArray() == false) throw "Policy Definition: A values member in the policy definition json file is not an array"; var.isEnum = true; //store the values for the enum in the enumValues map by its variable position //the position of the values in the array will also be used for the internal enum keys for(Json::ValueIterator it = element["values"].begin() ; it != element["values"].end() ; it++) { Variable enumVar; enumVar.isEnum = false; if(var.type == VariableSetType::STRING) { if((*it).isString() == false) throw "Policy Definition: An enum value has not the same type as the defined enum"; enumVar.type = VariableSetType::STRING; enumVar.value.asString = new string((*it).asString()); } else if(var.type == VariableSetType::INT64) { if((*it).isInt() == false) throw "Policy Definition: An enum value has not the same type as the defined enum"; enumVar.type = VariableSetType::INT64; enumVar.value.asInt64 = (*it).asInt64(); } else //else the type does not make sence in compination with an enum e.g. boolean throw "Policy Definition: A policy definition enum with this type is not supported"; enumValues[variableCounter].push_back(enumVar); } } else var.isEnum = false; variables.push_back(var); //variables will have the same order as in the policy definition //store the name for the variable in a map for a fast lookup during the eval string prefixedVariableName = currentContainerPrefix + element["name"].asString(); variableNames[variableCounter] = prefixedVariableName; //store also the position of the variable inside the variables map in a seperate map //adds the currentContainerPrefix to the variable name e.g. storage.location variablePositions[prefixedVariableName] = variableCounter++; } else if(element.isObject() && element.isMember("name") && element.isMember("variables")) { //must be a container //add the container name as prefix to the following variables string containerName = element["name"].asString(); currentContainerPrefix = currentContainerPrefix + containerName + "."; for(Json::ValueIterator it = element["variables"].begin() ; it != element["variables"].end() ; it++) parseJsonVariableList(*it); //recursion to allow nested containers //clear the current container name from the prefix (clould have more than one container e.g. storage.fde.algo) stringReplace(currentContainerPrefix, containerName + ".", ""); } else { cerr << "Current element: " << element << endl; throw "Policy Definition: Unsupported policy definition element "; } } //used for generation AstValueType PolicyDefinition::getIdType(string name) { //check that the name is a valid Id if(variablePositions.count(name) != 1) throw std::runtime_error(name + " is not defined in the policy definition"); VariableSetType type = getIdType(variablePositions.at(name)); if(type == VariableSetType::STRING) { return AstValueType::String; } else if(type == VariableSetType::BOOLEAN) { return AstValueType::Boolean; } else if(type == VariableSetType::INT64 || //unsigned integers with 64 bits are not supported type == VariableSetType::INT32 || type == VariableSetType::UINT32 || type == VariableSetType::INT16 || type == VariableSetType::UINT16 || type == VariableSetType::INT8 || type == VariableSetType::UINT8) { return AstValueType::Integer; } throw "Unknown ID type in PolicyDefinition getIdType(string)!"; } //used for evaluation VariableSetType PolicyDefinition::getIdType(uint64_t id) { return variables.at(id).type; } //uses the id enum values? bool PolicyDefinition::isEnumId(uint64_t id) { return variables.at(id).isEnum; } //used for generation uint16_t PolicyDefinition::getIdPosition(string name) { return variablePositions.at(name); } string PolicyDefinition::getIdName(uint64_t id) { return variableNames.at(id); } //used for evaluation VariableSetValue PolicyDefinition::getIdValue(uint64_t id) { Variable var = variables.at(id); return var.value; } VariableSetValue PolicyDefinition::getIdValue(string name) { Variable var = variables.at(getIdPosition(name)); return var.value; } VariableSetValue PolicyDefinition::getRuntimeValue(string name) { return runtimeVariables.at(name).value; } void PolicyDefinition::loadVariableValues(string variablesJsonString) { //parse the variables JSON string Json::Value root; Json::Reader reader; if(!reader.parse(variablesJsonString.c_str(), root)) throw runtime_error("Parsing of variables file failed: " + reader.getFormattedErrorMessages()); for(Json::ValueIterator it = root["variables"].begin() ; it != root["variables"].end() ; it++) { //check that there is a variable in the policy definition with this name string variableName = it.key().asString(); if(variablePositions.count(variableName) != 1) throw std::runtime_error("Set variable by variables file: " + variableName + " is not defined in the policy definition"); const uint32_t variablePosition = variablePositions.at(variableName); VariableSetType type = variables[variablePosition].type; VariableSetValue value; if(type == VariableSetType::STRING) { delete variables[variablePosition].value.asString; //free the old value (always present) value.asString = new string((*it).asString()); } else if(type == VariableSetType::BOOLEAN) { value.asBoolean = (*it).asBool(); } //signed integers //XXX check if there is enough place else if(type == VariableSetType::INT64) { value.asInt64 = (*it).asInt64(); } else if(type == VariableSetType::INT32) { value.asInt32 = (*it).asInt64(); } else if(type == VariableSetType::INT16) { value.asInt16 = (*it).asInt64(); } else if(type == VariableSetType::INT8) { value.asInt8 = (*it).asInt64(); } //unsigned integers //XXX check if there is enough place else if(type == VariableSetType::UINT32) { value.asUInt32 = (*it).asUInt64(); } else if(type == VariableSetType::UINT16) { value.asUInt16 = (*it).asUInt64(); } else if(type == VariableSetType::UINT8) { value.asUInt8 = (*it).asUInt64(); } else throw "Unsupported variable type in variables JSON"; variables[variablePosition].value = value; } } void PolicyDefinition::loadRuntimeVariableValues(string runtimeVariablesJsonString) { //parse the variables JSON string Json::Value root; Json::Reader reader; if(!reader.parse(runtimeVariablesJsonString.c_str(), root)) throw runtime_error("Parsing of runtime variables file failed: " + reader.getFormattedErrorMessages()); for(Json::ValueIterator it = root["variables"].begin() ; it != root["variables"].end() ; it++) { //check that the runtime variable element is valid if((*it).isObject() == false || (*it).isMember("name") == false || (*it).isMember("type") == false || (*it).isMember("value") == false) throw "Runtime variable is not valid!"; //get the runtime variable name string runtimeVariableName = (*it)["name"].asString(); //get the type and value Variable var; if((*it)["type"].asString().compare("string") == 0) { var.type = VariableSetType::STRING; var.value.asString = new string((*it)["value"].asString()); } else if((*it)["type"].asString().compare("boolean") == 0) { var.type = VariableSetType::BOOLEAN; var.value.asBoolean = (*it)["value"].asBool(); } //signed integers else if((*it)["type"].asString().compare("int64") == 0) { var.type = VariableSetType::INT64; var.value.asInt64 = (*it)["value"].asInt64(); } else if((*it)["type"].asString().compare("int32") == 0) { var.type = VariableSetType::INT32; var.value.asInt32 = (*it)["value"].asInt64(); } else if((*it)["type"].asString().compare("int16") == 0) { var.type = VariableSetType::INT16; var.value.asInt16 = (*it)["value"].asInt64(); } else if((*it)["type"].asString().compare("int8") == 0) { var.type = VariableSetType::INT8; var.value.asInt8 = (*it)["value"].asInt64(); } //unsigned integers else if((*it)["type"].asString().compare("uint32") == 0) { var.type = VariableSetType::UINT32; var.value.asUInt32 = (*it)["value"].asInt64(); } else if((*it)["type"].asString().compare("uint16") == 0) { var.type = VariableSetType::UINT16; var.value.asUInt16 = (*it)["value"].asInt64(); } else if((*it)["type"].asString().compare("uint8") == 0) { var.type = VariableSetType::UINT8; var.value.asUInt8 = (*it)["value"].asInt64(); } else throw "Policy Definition: Unknown runtime variable type in runtime variables JSON file"; //add the runtime variable to the runtime variables list runtimeVariables[runtimeVariableName] = var; } } int32_t PolicyDefinition::getEnumValuePosition(uint64_t id, Variable var) { vector<Variable> &values = enumValues.at(id); vector<Variable>::iterator enumIt = find(values.begin(), values.end(), var); if(enumIt != values.end()) //is a duplicate return distance(values.begin(), enumIt); else return -1; } uint8_t PolicyDefinition::getBitsForEnumValuePosition(uint64_t id) { return ceil(log2(enumValues.at(id).size())); } uint8_t PolicyDefinition::getBitsForFunctionEnumValuePosition(uint64_t funcId, uint64_t paramId) { return ceil(log2(functionEnumValues.at(FunctionEnumPair(funcId, paramId)).size())); } VariableSetValue PolicyDefinition::getEnumValue(uint64_t id, uint64_t enumValuePosition) { return enumValues.at(id).at(enumValuePosition).value; } VariableSetValue PolicyDefinition::getFunctionEnumValue(uint64_t funcId, uint64_t paramId, uint64_t enumValuePosition) { return functionEnumValues.at(FunctionEnumPair(funcId, paramId)).at(enumValuePosition).value; } uint64_t PolicyDefinition::getNumberOfFunctionParameters(uint64_t id) { return functionParameters.at(id).size(); } VariableSetType PolicyDefinition::getFunctionParameterType(uint64_t id, uint64_t paramPosition) { return functionParameters.at(id).at(paramPosition).type; } bool PolicyDefinition::isFunctionEnumParameter(uint64_t id, uint64_t paramPosition) { return functionParameters.at(id).at(paramPosition).isEnum; } int32_t PolicyDefinition::getFunctionEnumValuePosition(uint64_t funcId, uint64_t paramId, Variable var) { vector<Variable> &values = functionEnumValues.at(FunctionEnumPair(funcId, paramId)); vector<Variable>::iterator enumIt = find(values.begin(), values.end(), var); if(enumIt != values.end()) //is a duplicate return distance(values.begin(), enumIt); else return -1; } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |