ByteArrayUtils.cs
1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Camara Vision
//
// Copyright Andrew Kirillov, 2005-2006
// andrew.kirillov@gmail.com
//
namespace mjpeg
{
using System;
/// <summary>
/// Some array utilities
/// </summary>
internal class ByteArrayUtils
{
// Check if the array contains needle on specified position
public static bool Compare(byte[] array, byte[] needle, int startIndex)
{
int needleLen = needle.Length;
// compare
for (int i = 0, p = startIndex; i < needleLen; i++, p++)
{
if (array[p] != needle[i])
{
return false;
}
}
return true;
}
// Find subarray in array
public static int Find(byte[] array, byte[] needle, int startIndex, int count)
{
int needleLen = needle.Length;
int index;
while (count >= needleLen)
{
index = Array.IndexOf(array, needle[0], startIndex, count - needleLen + 1);
if (index == -1)
return -1;
int i, p;
// check for needle
for (i = 0, p = index; i < needleLen; i++, p++)
{
if (array[p] != needle[i])
{
break;
}
}
if (i == needleLen)
{
// found needle
return index;
}
count -= (index - startIndex + 1);
startIndex = index + 1;
}
return -1;
}
}
}