//C++ source code in Scanner.cpp
#include <iostream>
using namespace std;
#include "Scanner.h"
#include "trie.h"
Scanner::Scanner()
{
shortest_sig=UINT_MAX;
longest_sig=0;
}
Scanner::Scanner(istream &in)
{
Scanner();
LoadDatabase(in);
}
Scanner::~Scanner()
{
while (m_Patterns.empty() == false)
{
delete[] m_Patterns.front().m_Pattern;
m_Patterns.pop_front();
}
}
void Scanner::LoadDatabase(istream &in)
{
string buffer;
getline(in, buffer);
while ( in.fail() == false )
{
string::size_type pos = buffer.find('=');
if ( pos != string::npos )
{
string name = buffer.substr( 0, pos );
string scan_string = buffer.substr( pos+1 );
// Add to the string search list
Add( name, scan_string );
}
getline(in, buffer);
}
clog << endl;
m_Trie.Initialise();
m_Trie.Reset();
}
inline
void Scanner::Add( const string &name, const string &sig)
{
Pattern pat;
pat.m_Name = name;
size_t pattern_len = sig.length() / 2;
size_t siglen = sig.length();
pat.m_Pattern = new unsigned char[ pattern_len ];
pat.m_Len = 0;
char d[3], *end; d[2]='\0';
for (size_t i = 0; i < siglen; )
{
d[0] = sig.at(i); d[1] = sig.at(i+1);
if ( isalnum(d[0]) && isalnum(d[1]) )
{
pat.m_Pattern[ pat.m_Len ] = (unsigned char) strtoul(d, &end, 16);
i+=2;
pat.m_Len++;
}
else
{
throw runtime_error("not a digit, while parsing database"); // Not a digit
}
}
if ( pattern_len < shortest_sig )
shortest_sig = pattern_len;
if ( pattern_len > longest_sig )
longest_sig = pattern_len;
m_Patterns.push_back( pat );
clog << "Adding " << m_Patterns.back().m_Name << "to search trie" << endl;
m_Trie.Add( &m_Patterns.back() ); // Add string to the Trie
}
bool Scanner::Find(BlockBuffer *b, list<const Pattern*> **patterns)
{
return m_Trie.Find( b, patterns );
}