可以在内核模式下使用的KMP算法源码

// 函数声明
BOOLEAN MatchingString( IN PCCH lpszSour, IN PCCH lpszMatch, IN BOOLEAN bMatchCase
= TRUE); BOOLEAN MultiMatching( IN PCCH lpszSour, IN PCCH lpszMatch, IN INT nMatchLogic = 0, IN BOOLEAN bRetReversed = 0, IN BOOLEAN bMatchCase = TRUE); INT FindingString( IN PCCH lpszSour, IN PCCH lpszFind, IN INT nStart = 0);
// 函数实现

INT FindingString(
IN PCCH lpszSour,
IN PCCH lpszFind,
IN INT nStart /* = 0 */)
{
if (lpszSour == NULL || lpszFind == NULL || nStart < 0)
return -1;
int m = strlen(lpszSour);
int n = strlen(lpszFind);
if (nStart + n > m)
return -1;
if (n == 0)
return nStart;
// int* next = new int[n];
int* next = (int*)ExAllocatePoolWithTag(NonPagedPool, n * sizeof(int), FK_MEM);
{
n--;
int j, k;
j = 0;
k = -1;
next[0] = -1;
while (j < n)
{
if (k == -1 || lpszFind[k] == '?' || lpszFind[j] == lpszFind[k])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
n++;
}
int i = nStart, j = 0;
while (i < m && j < n)
{
if (j == -1 || lpszFind[j] == '?' || lpszSour[i] == lpszFind[j])
{
i++;
j++;
}
else
j = next[j];
}
// delete[]next;
ExFreePoolWithTag(next, FK_MEM);
if (j >= n)
return i - n;
else
return -1;
}

BOOLEAN MatchingString(
IN PCCH lpszSour,
IN PCCH lpszMatch,
IN BOOLEAN bMatchCase /* = true */)
{
if (lpszSour == NULL || lpszMatch == NULL)
return FALSE;
if (lpszMatch[0] == 0)
{
if (lpszSour[0] == 0)
return TRUE;
else
return FALSE;
}
int i = 0, j = 0;
// char* szSource = new char[(j = strlen(lpszSour) + 1)];
char* szSource = (char *)ExAllocatePoolWithTag(NonPagedPool, (j = strlen(lpszSour) + 1)*sizeof(char), FK_MEM);
if (bMatchCase)
{
while (*(szSource + i) = *(lpszSour + i++));
}
else
{
i = 0;
while (lpszSour[i])
{
if (lpszSour[i] >= 'A' && lpszSour[i] <= 'Z')
szSource[i] = lpszSour[i] - 'A' + 'a';
else
szSource[i] = lpszSour[i];

i++;
}
szSource[i] = 0;
}
// char* szMatcher = new char[strlen(lpszMatch) + 1];
char* szMatcher = (char*)ExAllocatePoolWithTag(NonPagedPool, (strlen(lpszMatch) + 1)*sizeof(char), FK_MEM);
i = j = 0;
while (lpszMatch[i])
{
szMatcher[j++] = (!bMatchCase) ?
((lpszMatch[i] >= 'A' && lpszMatch[i] <= 'Z') ?
lpszMatch[i] - 'A' + 'a' :
lpszMatch[i]
) :
lpszMatch[i];
if (lpszMatch[i] == '*')
while (lpszMatch[++i] == '*');
else
i++;
}
szMatcher[j] = 0;
int nMatchOffset, nSourOffset;
bool bIsMatched = TRUE;
nMatchOffset = nSourOffset = 0;
while (szMatcher[nMatchOffset])
{
if (szMatcher[nMatchOffset] == '*')
{
if (szMatcher[nMatchOffset + 1] == 0)
{
bIsMatched = TRUE;
break;
}
else
{
int nSubOffset = nMatchOffset + 1;
while (szMatcher[nSubOffset])
{
if (szMatcher[nSubOffset] == '*')
break;
nSubOffset++;
}
if (strlen(szSource + nSourOffset) <
size_t(nSubOffset - nMatchOffset - 1))
{
bIsMatched = FALSE;
break;
}
if (!szMatcher[nSubOffset])
{
nSubOffset--;
int nTempSourOffset = strlen(szSource) - 1;
while (szMatcher[nSubOffset] != '*')
{
if (szMatcher[nSubOffset] == '?')
;
else
{
if (szMatcher[nSubOffset] != szSource[nTempSourOffset])
{
bIsMatched = FALSE;
break;
}
}
nSubOffset--;
nTempSourOffset--;
}
break;
}
else
{
nSubOffset -= nMatchOffset;
// char* szTempFinder = new char[nSubOffset];
char* szTempFinder = (char*)ExAllocatePoolWithTag(NonPagedPool, nSubOffset*sizeof(char), FK_MEM);
nSubOffset--;
memcpy(szTempFinder, szMatcher + nMatchOffset + 1, nSubOffset);
szTempFinder[nSubOffset] = 0;
int nPos = ::FindingString(szSource + nSourOffset, szTempFinder, 0);
// delete[]szTempFinder;
ExFreePoolWithTag(szTempFinder, FK_MEM);
if (nPos != -1)
{
nMatchOffset += nSubOffset;
nSourOffset += (nPos + nSubOffset - 1);
}
else
{
bIsMatched = FALSE;
break;
}
}
}
}
else if (szMatcher[nMatchOffset] == '?')
{
if (!szSource[nSourOffset])
{
bIsMatched = FALSE;
break;
}
if (!szMatcher[nMatchOffset + 1] && szSource[nSourOffset + 1])
{
bIsMatched = FALSE;
break;
}
nMatchOffset++;
nSourOffset++;
}
else
{
if (szSource[nSourOffset] != szMatcher[nMatchOffset])
{
bIsMatched = FALSE;
break;
}
if (!szMatcher[nMatchOffset + 1] && szSource[nSourOffset + 1])
{
bIsMatched = FALSE;
break;
}
nMatchOffset++;
nSourOffset++;
}
}
// delete[]szSource;
// delete[]szMatcher;
ExFreePoolWithTag(szSource, FK_MEM);
ExFreePoolWithTag(szMatcher, FK_MEM);
return bIsMatched;
}

BOOLEAN MultiMatching(
IN PCCH lpszSour,
IN PCCH lpszMatch,
IN INT nMatchLogic /* = 0 */,
IN BOOLEAN bRetReversed /* = 0 */,
IN BOOLEAN bMatchCase /* = true */)
{
if (lpszSour == NULL || lpszMatch == NULL)
return FALSE;
// char* szSubMatch = new char[strlen(lpszMatch) + 1];
char* szSubMatch = (char*)ExAllocatePoolWithTag(NonPagedPool, (strlen(lpszMatch) + 1) * sizeof(char), FK_MEM);
BOOLEAN bIsMatch;
if (nMatchLogic == 0)
{
bIsMatch = FALSE;
int i = 0;
int j = 0;
while (1)
{
if (lpszMatch[i] != 0 && lpszMatch[i] != ',')
szSubMatch[j++] = lpszMatch[i];
else
{
szSubMatch[j] = 0;
if (j != 0)
{
bIsMatch = MatchingString(lpszSour, szSubMatch, bMatchCase);
if (bIsMatch)
break;
}
j = 0;
}

if (lpszMatch[i] == 0)
break;
i++;
}
}
else
{
bIsMatch = TRUE;
int i = 0;
int j = 0;
while (1)
{
if (lpszMatch[i] != 0 && lpszMatch[i] != ',')
szSubMatch[j++] = lpszMatch[i];
else
{
szSubMatch[j] = 0;
bIsMatch = MatchingString(lpszSour, szSubMatch, bMatchCase);
if (!bIsMatch)
break;
j = 0;
}
if (lpszMatch[i] == 0)
break;
i++;
}
}
// delete[]szSubMatch;
ExFreePoolWithTag(szSubMatch, FK_MEM);
if (bRetReversed)
return !bIsMatch;
else
return bIsMatch;
}

 

//  如果是在应用模式下使用那就用注释的部分替换回来即可

 

posted on 2017-09-28 15:34  wongnel  阅读(175)  评论(0)    收藏  举报

导航