score:3

create an array (or list...or other enumerable) of byte[] and a 'matched' boolean and then do a while loop around your pattern search like this:

List<byte[]> patterns = new List<byte[]>()
{ 
    new byte[] { 00, 00, 00, 08, 00 }, 
    new byte[] { 00, 00, 00, 08, 01 }, 
    new byte[] { 00, 00, 00, 08, 02 }, 
    new byte[] { 00, 00, 00, 08, 03 } 
};

//bool matched = false;
foreach (byte[] pattern in patterns)
{
    //while (!matched)
    //{
    byte[] file = File.ReadAllBytes("C:\\123.cfg");

    var result = Enumerable.Range(0, file.Length - pattern.Length + 1)
                    .Where(i => pattern.Select((b, j) => new { j, b })
                                       .All(p => file[i + p.j] == p.b))
                    .Select(i => i + pattern.Length - 1);

    int[] PatternArray = result.ToArray();
    //    if (result != null)
    //        matched = true;
    //}
}

[edit] this is a function that returns all of the valid matches as a list:

public List<int[]> doPatternSearch()
        {

            List<byte[]> patterns = new List<byte[]>() { 
                new byte[] { 00, 00, 00, 08, 00 }, 
                new byte[] { 00, 00, 00, 08, 01 }, 
                new byte[] { 00, 00, 00, 08, 02 }, 
                new byte[] { 00, 00, 00, 08, 03 } 
                };
            //this becomes a container to hold all of the valid results
            List<int[]> response = new List<int[]>();

            foreach (byte[] pattern in patterns)
            {
                byte[] file = File.ReadAllBytes("C:\\123.cfg");

                var result = Enumerable.Range(0, file.Length - pattern.Length + 1)
                      .Where(i => pattern.Select((b, j) => new { j, b })
                                         .All(p => file[i + p.j] == p.b))
                      .Select(i => i + pattern.Length - 1);

                if (result != null)
                {
                    //if the result is not null then add it to the list.
                    response.Add(result.ToArray<int>());
                }
            }

            return response;

        }

score:0

ORegex can pretty much do this for you. A little example with pattern search in byte sequence:

var oregex = new ORegex<byte>("{0}{1}{2}", x=> x==12, x=> x==3, x=> x==5);
var toSearch = new byte[]{1,1,12,3,5,1,12,3,5,5,5,5};
                              ^^^^^^   ^^^^^^
var foundTwoMatches = oregex.Matches(toSearch);

You also can define IComparable object and pass it directly, without using labmda functions.

More examples here.


Related Articles