Deathmax

Untitled

Apr 13th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         /// <summary>
  2.         /// Byte array to be used by FindPattern
  3.         /// </summary>
  4.         private static byte[] _file;
  5.         /// <summary>
  6.         /// Finds a specific pattern in a byte array
  7.         /// </summary>
  8.         /// <param name="btPattern">Byte array containing pattern to search</param>
  9.         /// <param name="strMask">x for exact match, ? for wildcard</param>
  10.         /// <returns>Position of pattern in _file, -1 if pattern not found</returns>
  11.         public static int FindPattern(byte[] btPattern, string strMask)
  12.         {
  13.             try
  14.             {
  15.                 if (strMask.Length != btPattern.Length)
  16.                     return -1;
  17.                 for (int x = 0; x < _file.Length; x++)
  18.                 {
  19.                     if (MaskCheck(x, btPattern, strMask))
  20.                     {
  21.                         return x;
  22.                     }
  23.                 }
  24.                 return -1;
  25.             }
  26.             catch (Exception ex)
  27.             {
  28.                 return -1;
  29.             }
  30.         }
  31.  
  32.         private static bool MaskCheck(int nOffset, byte[] btPattern, string strMask)
  33.         {
  34.             return !btPattern.Where((t, x) => strMask[x] != '?' && ((strMask[x] == 'x') && (t != _file[nOffset + x]))).Any();
  35.         }
Advertisement
Add Comment
Please, Sign In to add comment