Class to track data flow and detect a specific pattern

Committer:
chris215
Date:
Wed Jan 13 05:30:23 2016 +0000
Revision:
1:038d0018a542
Parent:
0:b8cca837d413
Removed templates and changed to using void* to make the class more generic.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris215 0:b8cca837d413 1 #ifndef PATTERN_FINDER_H
chris215 0:b8cca837d413 2 #define PATTERN_FINDER_H
chris215 0:b8cca837d413 3 #include <stdint.h>
chris215 0:b8cca837d413 4 #include <string.h>
chris215 0:b8cca837d413 5
chris215 0:b8cca837d413 6 class PatternFinder
chris215 0:b8cca837d413 7 {
chris215 0:b8cca837d413 8 private:
chris215 1:038d0018a542 9 uint8_t* m_RefPattern;
chris215 1:038d0018a542 10 uint8_t* m_CurDataStream;
chris215 1:038d0018a542 11 uint32_t m_PatternSize;
chris215 1:038d0018a542 12 uint32_t m_PatternElementSize;
chris215 1:038d0018a542 13 void ShiftDataInStream(void* data);
chris215 1:038d0018a542 14 bool IsPatternFound(void);
chris215 1:038d0018a542 15
chris215 0:b8cca837d413 16 public:
chris215 1:038d0018a542 17 PatternFinder(void* PatternData, uint32_t PatternLength, uint32_t PatternElementSize = 1);
chris215 0:b8cca837d413 18 ~PatternFinder();
chris215 1:038d0018a542 19 bool CheckPattern(void* data); //returns true if pattern was found in the stream
chris215 0:b8cca837d413 20 };
chris215 1:038d0018a542 21 inline void PatternFinder::ShiftDataInStream(void* data)
chris215 0:b8cca837d413 22 {
chris215 1:038d0018a542 23 uint8_t *src;
chris215 1:038d0018a542 24 uint8_t *dst;
chris215 1:038d0018a542 25 for(int element = (m_PatternSize-1);element > 0; element--)
chris215 1:038d0018a542 26 {
chris215 1:038d0018a542 27 dst = m_CurDataStream + element*m_PatternElementSize;
chris215 1:038d0018a542 28 src = m_CurDataStream + (element-1)*m_PatternElementSize;
chris215 1:038d0018a542 29 memcpy(dst,src,m_PatternElementSize);
chris215 1:038d0018a542 30 }
chris215 1:038d0018a542 31 src = (uint8_t*)data;
chris215 1:038d0018a542 32 dst = &m_CurDataStream[0];
chris215 1:038d0018a542 33 memcpy(dst,src,m_PatternElementSize);
chris215 0:b8cca837d413 34 }
chris215 1:038d0018a542 35
chris215 1:038d0018a542 36 inline bool PatternFinder::IsPatternFound(void)
chris215 0:b8cca837d413 37 {
chris215 0:b8cca837d413 38 uint32_t NumberOfIdenticalElementsFoundInPattern = 0;
chris215 1:038d0018a542 39 for(int x = 0;x < m_PatternSize*m_PatternElementSize;x++)
chris215 0:b8cca837d413 40 {
chris215 1:038d0018a542 41 if(m_CurDataStream[x] == m_RefPattern[x])
chris215 0:b8cca837d413 42 {
chris215 0:b8cca837d413 43 NumberOfIdenticalElementsFoundInPattern++;
chris215 0:b8cca837d413 44 }
chris215 0:b8cca837d413 45 }
chris215 1:038d0018a542 46 return (NumberOfIdenticalElementsFoundInPattern == (m_PatternSize*m_PatternElementSize));
chris215 0:b8cca837d413 47 }
chris215 0:b8cca837d413 48
chris215 1:038d0018a542 49 inline bool PatternFinder::CheckPattern(void* data)
chris215 0:b8cca837d413 50 {
chris215 1:038d0018a542 51 ShiftDataInStream(data);
chris215 1:038d0018a542 52 return IsPatternFound();
chris215 0:b8cca837d413 53 }
chris215 0:b8cca837d413 54 #endif