-
Notifications
You must be signed in to change notification settings - Fork 24
/
Library.cs
50 lines (48 loc) · 2.11 KB
/
Library.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ffxivlib
{
internal class Library
{
/// <summary>
/// Retrieves a byte array (needle) in another byte array (haystack)
/// </summary>
/// <param name="haystack">Byte array to search in</param>
/// <param name="needle">Byte array to search for</param>
/// <param name="start">Optionnal : Position to start from</param>
/// <returns>Position of the needle in the haystack or -1</returns>
internal static int ByteSearch(byte[] haystack, byte[] needle, int start = 0)
{
int found = -1;
if (haystack.Length > 0 && needle.Length > 0 && start <= (haystack.Length - needle.Length) && haystack.Length >= needle.Length)
{
for (int i = start; i <= haystack.Length - needle.Length; i++)
{
if (haystack[i] == needle[0])
{
if (haystack.Length > 1)
{
bool matched = true;
for (int y = 1; y <= needle.Length - 1; y++)
{
if (haystack[i + y] != needle[y])
{
matched = false;
break;
}
}
if (!matched) continue;
found = i;
break;
}
found = i;
break;
}
}
}
return found;
}
}
}