-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchBinary.cs
164 lines (145 loc) · 6.26 KB
/
SearchBinary.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VCodeHunt.Config;
using System.IO;
using System.Threading;
namespace VCodeHunt
{
class SearchBinary
{
EventWaitHandle m_terminate;
public SearchBinary(EventWaitHandle terminate)
{
m_terminate = terminate;
}
public static bool IsMine(string file)
{
const int bufflen = 8 * 1024;
byte[] buff = new byte[bufflen];
int chars = 0;
bool pe = false;
long bytesRead = 0;
using (BinaryReader br = new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
bytesRead = br.Read(buff, 0, bufflen);
if (bytesRead > 2 && ((buff[0] == 'M' || buff[0] == 'm') && (buff[1] == 'Z' || buff[1] == 'z')))
{
pe = true;
}
else
{
for (int idx = 0; idx < bytesRead; idx++)
{
if (buff[idx] >= 20 && buff[idx] <= 126)
{
chars++;
}
}
}
}
return (pe || (chars < ((bytesRead / 2) - (bytesRead * 0.05))));
}
public List<VCodeHunt.SearchFile.KeywordMatch> Search(SearchParams searchParams, string file)
{
List<VCodeHunt.SearchFile.KeywordMatch> matches = new List<VCodeHunt.SearchFile.KeywordMatch>();
VCodeHunt.SearchFile.KeywordMatch match = new VCodeHunt.SearchFile.KeywordMatch();
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] keywords = ascii.GetBytes(searchParams.Keywords);
byte[] keywordsLowerCase = ascii.GetBytes(searchParams.Keywords.ToLower());
byte[] keywordsUpperCase = ascii.GetBytes(searchParams.Keywords.ToUpper());
int overlap = keywords.Count() - 1;
const int bufflen = 64 * 1024;
byte[] buff = new byte[bufflen + overlap];
using (BinaryReader br = new BinaryReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
long maxlen = br.BaseStream.Length;
long idx = 0;
long bytesRead = 0;
while (idx < maxlen || m_terminate.WaitOne(0))
{
// search data must overlap
bytesRead = br.Read(buff, 0, bufflen + overlap);
if (0 == bytesRead || bytesRead <= overlap)
{
break;
}
// search
byte[] matchkeywords = keywords;
long bytesMax = bytesRead - (searchParams.Keywords.Length - 1);
bool found = false;
for (int bidx = 0; bidx < bytesMax; bidx++)
{
found = false;
matchkeywords = keywords;
if (searchParams.UseCaseSensitiveMatch)
{
if (buff[bidx] == keywords[0])
{
found = true;
for (int kidx = 1, sidx = bidx; kidx < keywords.Length && sidx < bytesMax; kidx++, sidx++)
{
if (buff[sidx] != keywords[kidx])
{
break;
}
}
}
}
else
{
int kidx = 0;
int sidx = bidx;
for (; kidx < keywords.Length && sidx < bytesMax; kidx++, sidx++)
{
if (buff[sidx] == keywordsLowerCase[kidx])
{
matchkeywords[kidx] = keywordsLowerCase[kidx];
}
else if (buff[sidx] == keywordsUpperCase[kidx])
{
matchkeywords[kidx] = keywordsUpperCase[kidx];
}
else
{
found = false;
break;
}
}
found = kidx == keywords.Length;
}
// check for match
if (found)
{
match.SetMatch(idx + bidx, matchkeywords, matchkeywords, FileContentType.Binary);
matches.Add(match);
match = new VCodeHunt.SearchFile.KeywordMatch();
}
else if (searchParams.ShowContextLines && searchParams.ContextLinesCount > 0)
{
if (matches.Count > 0 && matches.Last().ContextPost.Count < searchParams.ContextLinesCount)
{
matches.Last().ContextPost.Enqueue(String.Format("{0:X2}", buff[bidx]));
}
else
{
if (match.ContextPre.Count != 0 && match.ContextPre.Count >= searchParams.ContextLinesCount)
{
match.ContextPre.Dequeue();
}
match.ContextPre.Enqueue(String.Format("{0:X2}", buff[bidx]));
}
}
}
idx += (bytesRead - overlap);
if (m_terminate.WaitOne(0))
{
break;
}
}
}
return matches.Count() == 0 ? null : matches;
}
}
}