特征码搜索

1.Sunday字符串搜索算法

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
int String2Bytes(char* szSrc, unsigned short* szDst)
{
int len = 0;
WORD TzmLength = strlen((const char*)szSrc) / 3 + 1;

//将十六进制特征码转为十进制
for (int i = 0; i < strlen((const char*)szSrc); )
{
char num[2];
num[0] = szSrc[i++];
num[1] = szSrc[i++];
i++;
if (num[0] != '?' && num[1] != '?')
{
int sum = 0;
WORD a[2];
for (int i = 0; i < 2; i++)
{
if (num[i] >= '0' && num[i] <= '9')
{
a[i] = num[i] - '0';
}
else if (num[i] >= 'a' && num[i] <= 'z')
{
a[i] = num[i] - 87;
}
else if (num[i] >= 'A' && num[i] <= 'Z')
{
a[i] = num[i] - 55;
}

}
sum = a[0] * 16 + a[1];
szDst[len++] = sum;
}
else
{
szDst[len++] = 256;
}
}
return TzmLength;
}

void SundayStrSearch(unsigned char* szSrc, char* szDst, std::vector<int>& vRes)
{
size_t sSrc = strlen((const char*)szSrc);
unsigned short* pDst = (unsigned short*)malloc(2 * (strlen(szDst) / 3 + 1));
size_t sDst = String2Bytes(szDst, pDst);

// 构造Next数组
short szNext[260] = { 0 };
for (int i = 0; i < 260; i++)
szNext[i] = -1;
for (int i = 0; i < sDst; i++)
szNext[pDst[i]] = i;

// 字符串匹配
for (int i = 0, j, k; i < sSrc;)
{
j = i; k = 0;

for (; k < sDst && j < sSrc && (pDst[k] == szSrc[j] || pDst[k] == 256); k++, j++);

if (k == sDst)
{
vRes.push_back(i);
}

if ((size_t)(i + sDst) >= sSrc)
{
break;
}

int num = szNext[szSrc[sDst + i]];
if (num == -1)
i += (sDst - szNext[256]);
else
i += (sDst - num);
}
if (pDst)
{
free(pDst);
pDst = NULL;
}
}

int main()
{
unsigned char pSrc[] =
{
0xFF, 0x87, 0x88, 0x21, 0x67, 0x00
};

vector<int> res;
SundayStrSearch(pSrc, "88 ?? 67", res);
for (int i = 0; i < res.size(); i++)
cout << res[i] << endl;

return 0;
}

特征码搜索
http://helloymf.github.io/2022/09/23/te-zheng-ma-sou-suo/
作者
JNZ
许可协议