-
Notifications
You must be signed in to change notification settings - Fork 17
/
hal_search.cpp
243 lines (205 loc) · 6.19 KB
/
hal_search.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// code provided by Andrew based on the work of David Musser and Nishanov
// http://www.team5150.com/~andrew/
// http://www.cs.rpi.edu/~musser/gp/gensearch/index.html
//
#include <windows.h>
#include <pro.h>
#include <kernwin.hpp>
#include "findcrypt3.hpp"
#define HASH_RANGE_MAX 512
#define SUFFIX_SIZE 2
static inline ssize_t Hash(PBYTE pData)
{
return ((((ssize_t) pData[-1]) + ((ssize_t) pData[0])) & (HASH_RANGE_MAX - 1));
}
static ssize_t SearchSmallpat(PBYTE pSrc, ssize_t iSrcLen, PBYTE pPattern, ssize_t iPatternLen)
{
if ((0 == iSrcLen) || (0 == iPatternLen) || (iPatternLen > iSrcLen))
return -1;
PBYTE pLimit = (pSrc + iSrcLen - iPatternLen);
for (PBYTE p = pSrc; p <= pLimit; p++)
{
if (0 == memcmp(p, pPattern, iPatternLen))
return (p - pSrc);
}
return -1;
}
static void ComputeBacktrackTable(PBYTE pPattern, ssize_t iPatternLen, ssize_t *piPatternBacktrack)
{
ssize_t j = 0, t = -1;
piPatternBacktrack[j] = -1;
while (j < iPatternLen - 1)
{
while ((t >= 0) && (pPattern[j] != pPattern[t]))
{
t = piPatternBacktrack[t];
}
++j, ++t;
piPatternBacktrack[j] = pPattern[j] == pPattern[t] ? piPatternBacktrack[t] : t;
};
}
static ssize_t SearchHashed2(PBYTE pSrc, ssize_t iSrcLen, PBYTE pPattern, ssize_t iPatternLen, ssize_t *piPatternBacktrack)
{
if ((iSrcLen <= 0) || (iPatternLen <= 0) || (iPatternLen > iSrcLen))
return -1;
if (iPatternLen < SUFFIX_SIZE)
return SearchSmallpat(pSrc, iSrcLen, pPattern, iPatternLen);
ComputeBacktrackTable(pPattern, iPatternLen, piPatternBacktrack);
ssize_t aSkip[HASH_RANGE_MAX];
for (ssize_t i = 0; i < HASH_RANGE_MAX; i++)
aSkip[i] = iPatternLen - SUFFIX_SIZE + 1;
for (ssize_t i = 0; i < iPatternLen - 1; i++)
aSkip[Hash(pPattern + i)] = iPatternLen - 1 - i;
ssize_t iLarge = iSrcLen + 1;
ssize_t iMismatchShift = aSkip[Hash(pPattern + iPatternLen - 1)];
aSkip[Hash(pPattern + iPatternLen - 1)] = iLarge;
PBYTE pSrcEnd = pSrc + iSrcLen;
ssize_t k = -iSrcLen;
ssize_t iAdjustment = iLarge + iPatternLen - 1;
while (true)
{
k += iPatternLen - 1;
if (k >= 0)
return -1;
do
{
k += aSkip[Hash(pSrcEnd + k)];
}
while (k < 0);
if (k < iPatternLen)
{
return -1;
}
k -= iAdjustment;
if (pSrcEnd[k] != pPattern[0])
{
k += iMismatchShift;
continue;
}
ssize_t i = 1;
while (true)
{
if (pSrcEnd[++k] != pPattern[i])
{
break;
}
if (++i == iPatternLen)
{
return (iSrcLen + k) - iPatternLen + 1;
}
}
if (iMismatchShift > i)
{
k += iMismatchShift - i;
continue;
}
while (true)
{
i = piPatternBacktrack[i];
if (i <= 0)
{
if (i < 0)
{
k++;
}
break;
}
while (pSrcEnd[k] == pPattern[i])
{
k++;
if (++i == iPatternLen)
{
return (iSrcLen + k) - iPatternLen;
}
if (k == 0)
{
return -1;
}
}
}
}
}
static ssize_t *piPatternBacktrack = nullptr;
static ssize_t iPatternBacktrackSize = 0;
// Clean up pattern search data
void ClearPatternSearchData()
{
if (piPatternBacktrack)
{
qfree(piPatternBacktrack);
piPatternBacktrack = nullptr;
};
iPatternBacktrackSize = 0;
}
// Search for pattern
ssize_t PatternSearch(PBYTE pSrc, ssize_t iSrcLen, PBYTE pPattern, ssize_t iPatternLen, ssize_t iAnd)
{
// Init backtrack buffer the first time
if (!piPatternBacktrack)
{
// Largest seen 7/9/2012 131072 bytes
iPatternBacktrackSize = qmax((iPatternLen * sizeof(ssize_t)), 131072);
piPatternBacktrack = qalloc_array<ssize_t>(iPatternBacktrackSize);
if (!piPatternBacktrack)
{
msg("** Failed to allocate pattern backtrace bufferr! **\n");
iPatternBacktrackSize = 0;
return -1;
}
}
// Expand buffer as needed
ssize_t iNeeded = (iPatternLen * sizeof(ssize_t));
if (iNeeded > iPatternBacktrackSize)
{
// msg("Expanding backtrace buffer from %d to %d bytes\n", iPatternBacktrackSize, iNeeded);
piPatternBacktrack = qrealloc_array<ssize_t>(piPatternBacktrack, iNeeded);
if (nullptr != piPatternBacktrack)
{
iPatternBacktrackSize = iNeeded;
}
else
{
msg("** Failed to expand pattern backtrace bufferr from %d to %d bytes! **\n",
iPatternBacktrackSize, iNeeded);
iPatternBacktrackSize = 0;
return -1;
}
}
ssize_t iGranularity = iAnd >> 3;
ssize_t iSlicesize = iGranularity ? iGranularity : iPatternLen;
ssize_t iRemaining = iSrcLen;
ssize_t iOfs = -1;
ssize_t iMaxAndDistance = iPatternLen * 16;
PBYTE pPatLimit = pPattern + iPatternLen;
PBYTE pPatStart = nullptr;
for (PBYTE pStart = pSrc, p = pPattern; p < pPatLimit;)
{
iOfs = SearchHashed2(pStart, iRemaining, p, iSlicesize, piPatternBacktrack);
if (iOfs != -1)
{
iRemaining -= (iOfs + iSlicesize);
if (!pPatStart)
{
pPatStart = pStart + iOfs;
if (iRemaining > iMaxAndDistance)
{
iRemaining = iMaxAndDistance;
}
}
pStart += (iOfs + iSlicesize);
p += iSlicesize;
}
else
{
if (!pPatStart)
{
break;
}
pStart = pPatStart + iSlicesize;
iRemaining = iSrcLen - (pStart - pSrc);
p = pPattern;
pPatStart = 0;
}
}
return (iOfs != -1) ? (pPatStart - pSrc) : (ssize_t) -1;
}