-
Notifications
You must be signed in to change notification settings - Fork 536
/
ByteArrayUtils.cs
106 lines (98 loc) · 3.68 KB
/
ByteArrayUtils.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// AForge Video Library
// AForge.NET framework
//
// Copyright © Andrew Kirillov, 2007-2008
//
namespace iSpyApplication
{
using System;
/// <summary>
/// Some internal utilities for handling arrays.
/// </summary>
///
internal static class ByteArrayUtils
{
public static unsafe bool UnsafeCompare(byte[] a1, byte[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
return false;
fixed (byte* p1 = a1, p2 = a2)
{
byte* x1 = p1, x2 = p2;
int l = a1.Length;
for (int i = 0; i < l / 8; i++, x1 += 8, x2 += 8)
if (*((long*)x1) != *((long*)x2)) return false;
if ((l & 4) != 0) { if (*((int*)x1) != *((int*)x2)) return false; x1 += 4; x2 += 4; }
if ((l & 2) != 0) { if (*((short*)x1) != *((short*)x2)) return false; x1 += 2; x2 += 2; }
if ((l & 1) != 0) if (*(x1) != *(x2)) return false;
return true;
}
}
/// <summary>
/// Check if the array contains needle at specified position.
/// </summary>
///
/// <param name="array">Source array to check for needle.</param>
/// <param name="needle">Needle we are searching for.</param>
/// <param name="startIndex">Start index in source array.</param>
///
/// <returns>Returns <b>true</b> if the source array contains the needle at
/// the specified index. Otherwise it returns <b>false</b>.</returns>
///
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;
}
/// <summary>
/// Find subarray in the source array.
/// </summary>
///
/// <param name="array">Source array to search for needle.</param>
/// <param name="needle">Needle we are searching for.</param>
/// <param name="startIndex">Start index in source array.</param>
/// <param name="sourceLength">Number of bytes in source array, where the needle is searched for.</param>
///
/// <returns>Returns starting position of the needle if it was found or <b>-1</b> otherwise.</returns>
///
public static int Find(byte[] array, byte[] needle, int startIndex, int sourceLength)
{
int needleLen = needle.Length;
while (sourceLength >= needleLen)
{
// find needle's starting element
var index = Array.IndexOf(array, needle[0], startIndex, sourceLength - needleLen + 1);
// if we did not find even the first element of the needls, then the search is failed
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)
{
// needle was found
return index;
}
// continue to search for needle
sourceLength -= (index - startIndex + 1);
startIndex = index + 1;
}
return -1;
}
}
}