-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsearch.cpp
138 lines (108 loc) · 3.04 KB
/
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
//
// Created by willyterra on 1/13/22.
//
#include <iostream>
#include "search.h"
using namespace std;
string DeleteSpace(string markCode)
{
if (!markCode.empty()) {
int index = 0;
//Delete all space
while ((index = markCode.find(' ', index)) >= 0) {
markCode.erase(index, 1);
}
index = 0;
//Delete the header match code
while (true) {
index = markCode.find("??", index);
if (index == 0)
markCode.erase(index, 2);
else
break;
}
}
if (markCode.length() % 2 != 0) return 0;
return markCode;
}
int String2ByteArray(string markCode, uint8_t* pMarkCode)
{
int markCodeLength = markCode.length()/2;
int markCodePosition = 0;
for (int i = 0; i < markCodeLength; i++)
{
string tempStr = markCode.substr(i * 2,2);
if (tempStr == "??")
{
pMarkCode[i] = 0x3F;
if (markCodePosition == 0) markCodePosition = i;
}
else
{
pMarkCode[i] = strtoul(tempStr.c_str(),0,16);
}
}
return markCodePosition;
}
int PatchPatternSearch(uint8_t* pSrcBuffer,uint32_t SrcLength,uint8_t* pPatchPattern, uint8_t PatternLength, int MarkCodePos)
{
uint32_t nOffset = 0;
uint32_t i = 0, j = 0, nCount = 0;
uint8_t aSunday[0xFF + 1] = {0};
for (int i = 0; i < MarkCodePos; i++) {
aSunday[pPatchPattern[i]] = i + 1;
}
while (j < PatternLength) {
if (pSrcBuffer[i] == pPatchPattern[j] || pPatchPattern[j] == 0x3F) {
i++;
j++;
}
else {
nOffset = i - j + MarkCodePos;
if (nOffset > SrcLength - PatternLength) break;
if (aSunday[pSrcBuffer[nOffset]]) {
i = nOffset - aSunday[pSrcBuffer[nOffset]] + 1;
j = 0;
}
else {
i = nOffset + 1;
j = 0;
}
}
}
if (j == PatternLength) {
return i - PatternLength;
}
return -1;
}
int PatchPatternSearchInfo(uint8_t* pSrcBuffer,uint32_t SrcLength, stSearchPattern patData)
{
uint32_t nOffset = 0;
uint32_t i = 0, j = 0, nCount = 0;
uint8_t aSunday[0xFF + 1] = {0};
for (int i = 0; i < patData.posMarkCode; i++) {
aSunday[patData.pPatternData[i]] = i + 1;
}
while (j < patData.lenSearchPattern) {
if (pSrcBuffer[i] == patData.pPatternData[j] || patData.pPatternData[j] == 0x3F) {
i++;
j++;
}
else {
nOffset = i - j + patData.posMarkCode;
if (nOffset > SrcLength - patData.lenSearchPattern) break;
if (aSunday[pSrcBuffer[nOffset]]) {
i = nOffset - aSunday[pSrcBuffer[nOffset]] + 1;
j = 0;
}
else {
i = nOffset + 1;
j = 0;
}
}
}
if (j == patData.lenSearchPattern) {
return i - patData.lenSearchPattern;
}
return -1;
}