Class to track data flow and detect a specific pattern
Revision 1:038d0018a542, committed 2016-01-13
- Comitter:
- chris215
- Date:
- Wed Jan 13 05:30:23 2016 +0000
- Parent:
- 0:b8cca837d413
- Commit message:
- Removed templates and changed to using void* to make the class more generic.
Changed in this revision
PatternFinder.cpp | Show annotated file Show diff for this revision Revisions of this file |
PatternFinder.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r b8cca837d413 -r 038d0018a542 PatternFinder.cpp --- a/PatternFinder.cpp Mon Jan 11 04:48:08 2016 +0000 +++ b/PatternFinder.cpp Wed Jan 13 05:30:23 2016 +0000 @@ -1,39 +1,27 @@ #include "PatternFinder.h" -// make the linker aware of some possible types -template class PatternFinder<uint8_t>; -template class PatternFinder<int8_t>; -template class PatternFinder<uint16_t>; -template class PatternFinder<int16_t>; -template class PatternFinder<uint32_t>; -template class PatternFinder<int32_t>; -template class PatternFinder<uint64_t>; -template class PatternFinder<int64_t>; -template class PatternFinder<char>; -template class PatternFinder<wchar_t>; - -template <class T> -PatternFinder<T>::PatternFinder(T* Pattern,uint32_t PatternSize) +PatternFinder::PatternFinder(void* PatternData, uint32_t PatternLength, uint32_t PatternElementSize) { - m_PatternSize = PatternSize; - m_PatternReference = new T[PatternSize]; + m_PatternSize = PatternLength; + m_PatternElementSize = PatternElementSize; + m_RefPattern = new uint8_t[m_PatternSize*m_PatternElementSize]; - T *src = Pattern; - T *dst = &m_PatternReference[PatternSize-1]; + uint8_t *src = (uint8_t*)PatternData; + uint8_t *dst = (uint8_t*)m_RefPattern + (m_PatternSize-1)*m_PatternElementSize; //We must invert the given pattern so that it better fits the matching algorithm - for(int x = 0; x < PatternSize ;x++) + for(int x = 0; x < m_PatternSize ;x++) { - *dst-- = *src++; - + memcpy(dst,src,m_PatternElementSize); + dst-=m_PatternElementSize; + src+=m_PatternElementSize; } - m_CurrentPattern = new T[PatternSize]; + m_CurDataStream = new uint8_t[m_PatternSize*m_PatternElementSize]; return; } -template <class T> -PatternFinder<T>::~PatternFinder() +PatternFinder::~PatternFinder() { - delete [] m_PatternReference; - delete [] m_CurrentPattern; + delete [] m_RefPattern; + delete [] m_CurDataStream; return; }
diff -r b8cca837d413 -r 038d0018a542 PatternFinder.h --- a/PatternFinder.h Mon Jan 11 04:48:08 2016 +0000 +++ b/PatternFinder.h Wed Jan 13 05:30:23 2016 +0000 @@ -3,47 +3,52 @@ #include <stdint.h> #include <string.h> - -template <typename T> class PatternFinder { private: - T *m_PatternReference; - T *m_CurrentPattern; - uint32_t m_PatternSize; - void ShiftDataInCurrentPattern(T data); - bool CheckForPattern(void); + uint8_t* m_RefPattern; + uint8_t* m_CurDataStream; + uint32_t m_PatternSize; + uint32_t m_PatternElementSize; + void ShiftDataInStream(void* data); + bool IsPatternFound(void); + public: - PatternFinder(T* Pattern,uint32_t PatternSize = 1); + PatternFinder(void* PatternData, uint32_t PatternLength, uint32_t PatternElementSize = 1); ~PatternFinder(); - bool CheckPattern(T data); //returns true if pattern was found in the stream + bool CheckPattern(void* data); //returns true if pattern was found in the stream }; - -template <class T> -inline void PatternFinder<T>::ShiftDataInCurrentPattern(T data) +inline void PatternFinder::ShiftDataInStream(void* data) { - for(int x = (m_PatternSize-1);x > 0;x--) - m_CurrentPattern[x] = m_CurrentPattern[x-1]; - m_CurrentPattern[0] = data; + uint8_t *src; + uint8_t *dst; + for(int element = (m_PatternSize-1);element > 0; element--) + { + dst = m_CurDataStream + element*m_PatternElementSize; + src = m_CurDataStream + (element-1)*m_PatternElementSize; + memcpy(dst,src,m_PatternElementSize); + } + src = (uint8_t*)data; + dst = &m_CurDataStream[0]; + memcpy(dst,src,m_PatternElementSize); } -template <class T> -inline bool PatternFinder<T>::CheckForPattern(void) + +inline bool PatternFinder::IsPatternFound(void) { uint32_t NumberOfIdenticalElementsFoundInPattern = 0; - for(int x = 0;x < m_PatternSize;x++) + for(int x = 0;x < m_PatternSize*m_PatternElementSize;x++) { - if(m_PatternReference[x] == m_CurrentPattern[x]) + if(m_CurDataStream[x] == m_RefPattern[x]) { NumberOfIdenticalElementsFoundInPattern++; } } - return NumberOfIdenticalElementsFoundInPattern == m_PatternSize; + return (NumberOfIdenticalElementsFoundInPattern == (m_PatternSize*m_PatternElementSize)); } -template <class T> -inline bool PatternFinder<T>::CheckPattern(T data) +inline bool PatternFinder::CheckPattern(void* data) { - ShiftDataInCurrentPattern(data); - return CheckForPattern(); + ShiftDataInStream(data); + return IsPatternFound(); } #endif \ No newline at end of file