Просмотр исходного кода

cleaned up namespace in SNFMulti

master
Pete McNeil 4 лет назад
Родитель
Сommit
cb4666686f
2 измененных файлов: 59 добавлений и 57 удалений
  1. 12
    10
      snf_engine.cpp
  2. 47
    47
      snf_engine.hpp

+ 12
- 10
snf_engine.cpp Просмотреть файл

#include "../CodeDweller/mangler.hpp" #include "../CodeDweller/mangler.hpp"
#include "snf_engine.hpp" #include "snf_engine.hpp"


namespace cd = codedweller;
namespace codedweller {


/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// BEGIN IMPLEMENTATIONS ////////////////////////////////////////////////////////////////// // BEGIN IMPLEMENTATIONS //////////////////////////////////////////////////////////////////


// TokenMatrix::Load(filename) // TokenMatrix::Load(filename)


void TokenMatrix::Load(string& FileName) { // Initialize using a string for file name.
void TokenMatrix::Load(std::string& FileName) { // Initialize using a string for file name.
Load(FileName.c_str()); // Convert the string to a null terminated Load(FileName.c_str()); // Convert the string to a null terminated
} // char* and call the function below. } // char* and call the function below.


void TokenMatrix::Load(const char* FileName) { // Initializes the token matrix by file name. void TokenMatrix::Load(const char* FileName) { // Initializes the token matrix by file name.


ifstream MatrixFile(FileName,ios::binary); // Open the file.
std::ifstream MatrixFile(FileName,std::ios::binary); // Open the file.
if(MatrixFile.bad()) // If anything is wrong with the file if(MatrixFile.bad()) // If anything is wrong with the file
throw BadFile("TokenMatrix::Load() finds MatrixFile.bad()"); // then throw a bad file exception. throw BadFile("TokenMatrix::Load() finds MatrixFile.bad()"); // then throw a bad file exception.




// TokenMatrix::Load(stream) // TokenMatrix::Load(stream)


const cd::AbortCheck CompatibleIntSizeCheck("TokenMatrix::Load():CompatibleIntSizeCheck(sizeof(unsigned int)==4)");
const AbortCheck CompatibleIntSizeCheck("TokenMatrix::Load():CompatibleIntSizeCheck(sizeof(unsigned int)==4)");


void TokenMatrix::Load(ifstream& F) { // Initializes the token matrix from a file.
void TokenMatrix::Load(std::ifstream& F) { // Initializes the token matrix from a file.
CompatibleIntSizeCheck(sizeof(unsigned int)==4); // Check our assumptions. CompatibleIntSizeCheck(sizeof(unsigned int)==4); // Check our assumptions.


MatrixSize = 0; // Clear out the old Matrix Size and array. MatrixSize = 0; // Clear out the old Matrix Size and array.
if(Matrix) delete Matrix; // that is, if there is an array. if(Matrix) delete Matrix; // that is, if there is an array.


F.seekg(0,ios::end); // Find the end of the file.
F.seekg(0,std::ios::end); // Find the end of the file.
MatrixSize = F.tellg() / sizeof(Token); // Calculate how many tokens. MatrixSize = F.tellg() / sizeof(Token); // Calculate how many tokens.
F.seekg(0); // Go back to the beginning. F.seekg(0); // Go back to the beginning.




// TokenMatrix::Validate(key) // TokenMatrix::Validate(key)


void TokenMatrix::Validate(string& SecurityKey) { // Decrypts and validates the matrix.
void TokenMatrix::Validate(std::string& SecurityKey) { // Decrypts and validates the matrix.


MANGLER ValidationChecker; // Create a mangler engine for validation. MANGLER ValidationChecker; // Create a mangler engine for validation.




SecurityCheckKey[SecurityKeyBufferSize-1] = 0; // Add a safety null just in case. SecurityCheckKey[SecurityKeyBufferSize-1] = 0; // Add a safety null just in case.


string SecurityCheck((char*)SecurityCheckKey); // Make a string.
std::string SecurityCheck((char*)SecurityCheckKey); // Make a string.


// By now we should have a SecurityCheck string to compare to our SecurityKey. // By now we should have a SecurityCheck string to compare to our SecurityKey.
// If they match then we know everything worked out and that our token matrix has // If they match then we know everything worked out and that our token matrix has


// TokenMatrix::Verify(key) // TokenMatrix::Verify(key)


void TokenMatrix::Verify(string& SecurityKey) { // Builds and verifies a file digest.
void TokenMatrix::Verify(std::string& SecurityKey) { // Builds and verifies a file digest.


MANGLER DigestChecker; // Create a mangler for the digest. MANGLER DigestChecker; // Create a mangler for the digest.


return PassResult; // When we're finished, return the last known result. return PassResult; // When we're finished, return the last known result.
} }


void EvaluationMatrix::evaluateSegment(vector<unsigned char>& data, unsigned int start, unsigned int finish) {
void EvaluationMatrix::evaluateSegment(std::vector<unsigned char>& data, unsigned int start, unsigned int finish) {
restartEngineAt(start); restartEngineAt(start);
finish = (finish < data.size()) ? finish : data.size(); finish = (finish < data.size()) ? finish : data.size();
for(unsigned int a = start; a < finish; a++) EvaluateThis(data[a]); for(unsigned int a = start; a < finish; a++) EvaluateThis(data[a]);
} }

} // End namespace codedweller

+ 47
- 47
snf_engine.hpp Просмотреть файл



// 20021030 _M - Created. // 20021030 _M - Created.


#ifndef _MN_SNF_ENGINE
#define _MN_SNF_ENGINE
#pragma once


#include <cassert> #include <cassert>
#include <stdexcept> #include <stdexcept>
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string>
#include <string>
#include <vector> #include <vector>
#include <exception>
#include <exception>
#include "../CodeDweller/faults.hpp" #include "../CodeDweller/faults.hpp"
#include "../CodeDweller/mangler.hpp" #include "../CodeDweller/mangler.hpp"
//#include "../nvwa-0.6/nvwa/debug_new.h" //#include "../nvwa-0.6/nvwa/debug_new.h"


using namespace std;
namespace codedweller {


// 20030929 _M SYMBOL_RANGE moved to snf_engine.hpp as part of augmenting the // 20030929 _M SYMBOL_RANGE moved to snf_engine.hpp as part of augmenting the
// capability of a match record. Match records now can decode themselves. // capability of a match record. Match records now can decode themselves.


// Exceptions... // Exceptions...


class BadAllocation : public runtime_error { // Exception for a bad memory allocation.
public: BadAllocation(const string& w):runtime_error(w) {}
class BadAllocation : public std::runtime_error { // Exception for a bad memory allocation.
public: BadAllocation(const std::string& w):runtime_error(w) {}
}; };
class BadMatrix : public runtime_error { // Exception for invalid matrix loads.
public: BadMatrix(const string& w):runtime_error(w) {}
class BadMatrix : public std::runtime_error { // Exception for invalid matrix loads.
public: BadMatrix(const std::string& w):runtime_error(w) {}
}; };
class BadFile : public runtime_error { // Exception for missing rulebase files.
public: BadFile(const string& w):runtime_error(w) {}
class BadFile : public std::runtime_error { // Exception for missing rulebase files.
public: BadFile(const std::string& w):runtime_error(w) {}
}; };
class OutOfRange : public runtime_error { // Exception for indexes out of range.
public: OutOfRange(const string& w):runtime_error(w) {}
class OutOfRange : public std::runtime_error { // Exception for indexes out of range.
public: OutOfRange(const std::string& w):runtime_error(w) {}
}; };


// Standards... // Standards...


void Load(const char* FileName); // Loads the matrix from a file name. void Load(const char* FileName); // Loads the matrix from a file name.


void Load(string& FileName); // Loads the matrix from a file name string.
void Load(std::string& FileName); // Loads the matrix from a file name string.


void Load(ifstream& F); // Loads the token matrix from the file.
void Load(std::ifstream& F); // Loads the token matrix from the file.


void Validate(string& SecurityKey); // Validates the matrix with a key string.
void Validate(std::string& SecurityKey); // Validates the matrix with a key string.


void Verify(string& SecurityKey); // Verifies the matrix digest.
void Verify(std::string& SecurityKey); // Verifies the matrix digest.


void FlipEndian(); // Converts big/little endian tokens. void FlipEndian(); // Converts big/little endian tokens.


Matrix(NULL), Matrix(NULL),
MatrixSize(0) { } MatrixSize(0) { }


TokenMatrix(ifstream& F) :
TokenMatrix(std::ifstream& F) :
Matrix(NULL), Matrix(NULL),
MatrixSize(0) { MatrixSize(0) {
Load(F); Load(F);
unsigned int PositionLimit; // Largest CurrentPosition. unsigned int PositionLimit; // Largest CurrentPosition.


// 20030216 _M Optimization conversions // 20030216 _M Optimization conversions
// 20140119 _M Deprecated by jump table in evaluator // 20140119 _M Deprecated by jump table in evaluator
// inline int i_lower(); // { return myEvaluationMatrix->i_lower; } // inline int i_lower(); // { return myEvaluationMatrix->i_lower; }
// inline bool i_isDigit(); // { return myEvaluationMatrix->i_isDigit; } // inline bool i_isDigit(); // { return myEvaluationMatrix->i_isDigit; }
// inline bool i_isSpace(); // { return myEvaluationMatrix->i_isSpace; } // inline bool i_isSpace(); // { return myEvaluationMatrix->i_isSpace; }
// inline bool i_isAlpha(); // { return myEvaluationMatrix->i_isAphpa; }
unsigned int JumpPoint;
// inline bool i_isAlpha(); // { return myEvaluationMatrix->i_isAphpa; }
unsigned int JumpPoint;
int xLetter(); // Match Any letter. int xLetter(); // Match Any letter.
int xDigit(); // Match Any digit. int xDigit(); // Match Any digit.
int xNonWhite(); // Match Any non-whitespace. int xNonWhite(); // Match Any non-whitespace.
int xAnyInline(); // Match Any byte but new line. int xAnyInline(); // Match Any byte but new line.
int xAnything(); // Match Any character at all. int xAnything(); // Match Any character at all.
int xRunGateway(); // Match the run-loop gateway. int xRunGateway(); // Match the run-loop gateway.
void doFollowOrMakeBuddy(int keyVector); // Follow and divide algorithm.
void tryFollowingPrecisePath(unsigned short int i);
void tryFollowingNoCasePath(unsigned short int i);
void tryFollowingWildAlphaPath();
void tryFollowingWildDigitPath();
void tryFollowingWildNonWhitePath();
void tryFollowingWildWhitePath();
void tryFollowingWildInlinePath();
void tryFollowingWildAnythingPath();
void doFollowOrMakeBuddy(int keyVector); // Follow and divide algorithm.
void tryFollowingPrecisePath(unsigned short int i);
void tryFollowingNoCasePath(unsigned short int i);
void tryFollowingWildAlphaPath();
void tryFollowingWildDigitPath();
void tryFollowingWildNonWhitePath();
void tryFollowingWildWhitePath();
void tryFollowingWildInlinePath();
void tryFollowingWildAnythingPath();
void doFollowerJumpTable(unsigned short int i); void doFollowerJumpTable(unsigned short int i);


public: public:
// evaluator node if it exists. Therefore, the caller should skip it's normal // evaluator node if it exists. Therefore, the caller should skip it's normal
// list itteration code when this function has been called. // list itteration code when this function has been called.


void DropEvaluator();
void DropEvaluator();
void dropAllEvaluators(); void dropAllEvaluators();


public: public:


// Exception classes... // Exception classes...


class BadAllocation : public runtime_error { // Allocation failed exception.
public: BadAllocation(const string& w):runtime_error(w) {}
class BadAllocation : public std::runtime_error { // Allocation failed exception.
public: BadAllocation(const std::string& w):runtime_error(w) {}
}; };
class MaxEvalsExceeded : public runtime_error { // Too many evaluators exception.
public: MaxEvalsExceeded(const string& w):runtime_error(w) {}
class MaxEvalsExceeded : public std::runtime_error { // Too many evaluators exception.
public: MaxEvalsExceeded(const std::string& w):runtime_error(w) {}
}; };
class OutOfRange : public runtime_error { // Out of range exception.
public: OutOfRange(const string& w):runtime_error(w) {}
class OutOfRange : public std::runtime_error { // Out of range exception.
public: OutOfRange(const std::string& w):runtime_error(w) {}
}; };


// Attributes... // Attributes...


// 20030216 _M High Level Conversion Optimizers... // 20030216 _M High Level Conversion Optimizers...


// 20140119 _M Deprecated by jump table in evaluator
// 20140119 _M Deprecated by jump table in evaluator
// int i_lower; // Lower case version of byte under test. // int i_lower; // Lower case version of byte under test.
// bool i_isDigit; // true if i is a digit. // bool i_isDigit; // true if i is a digit.
// bool i_isSpace; // true if i is whitespace. // bool i_isSpace; // true if i is whitespace.
// EvaluateThis() Moves each evaluator with the current character and creates a new // EvaluateThis() Moves each evaluator with the current character and creates a new
// evaluator for the current spot in the input file to make all rules global. // evaluator for the current spot in the input file to make all rules global.


int EvaluateThis(unsigned short int i);
void evaluateSegment(vector<unsigned char>& data, unsigned int start, unsigned int finish);
int EvaluateThis(unsigned short int i);
void evaluateSegment(std::vector<unsigned char>& data, unsigned int start, unsigned int finish);
void restartEngineAt(int newCharacterCount); void restartEngineAt(int newCharacterCount);


EvaluationMatrix(TokenMatrix* m) { // Constructor w/ pointer to Token Matrix... EvaluationMatrix(TokenMatrix* m) { // Constructor w/ pointer to Token Matrix...
// When that first evaulator is used, the NULL pointer will return to the root // When that first evaulator is used, the NULL pointer will return to the root
// of the EvaluatorCache list. In this regard the cache acts like a stack. // of the EvaluatorCache list. In this regard the cache acts like a stack.


#endif
} // End namespace codedweller




Загрузка…
Отмена
Сохранить