-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryFileSearch.cs
69 lines (64 loc) · 1.83 KB
/
BinaryFileSearch.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Edge.Patcher
{
internal class BinaryFileSearch
{
private string _fileName;
private const int BUFFER_SIZE = 128 * 1024 * 1024;
public BinaryFileSearch(string fileName)
{
_fileName = fileName;
}
public List<KeyValuePair<string, long>> FindReferences(Dictionary<string, BinaryDescriptor> items)
{
var results = new List<KeyValuePair<string, long>>();
if (items == null || items.Count == 0)
{
return results.ToList();
}
using (FileStream fs = new FileStream(_fileName, FileMode.Open, FileAccess.Read))
{
using (BinaryReader reader = new BinaryReader(fs))
{
byte[] buffer = new byte[BUFFER_SIZE];
int pos = 0;
int maxLenth = items.Max(x => Math.Max(x.Value.Key.Length, x.Value.Key.Length));
while (fs.Position < fs.Length)
{
int bufferSize = reader.Read(buffer, 0, BUFFER_SIZE);
foreach (var item in items)
{
int index = 0;
do
{
index = buffer.SearchBytes(index, item.Value.Key, bufferSize);
if (index >= 0)
{
results.Add(new KeyValuePair<string, long>(item.Key, fs.Position - bufferSize + index));
index += item.Value.Key.Length;
}
} while (index > 0);
index = 0;
do
{
index = buffer.SearchBytes(index, item.Value.Value, bufferSize);
if (index >= 0)
{
results.Add(new KeyValuePair<string, long>(item.Key + " (Value)", fs.Position - bufferSize + index));
index += item.Value.Value.Length;
}
} while (index > 0);
}
pos += BUFFER_SIZE - maxLenth;
fs.Position = pos;
}
}
}
return results.ToList();
}
}
}