VulnC.cs
2.73 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace VulnCrawler
{
public class VulnC : VulnAbstractCrawler
{
protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ (?<{MethodName}>(static)? [\w]+ [\w]+)\([\w \*\,\t\n]*\)";
protected override string Extension => ".c";
protected override string ReservedFileName => "CReserved.txt";
public override MatchCollection GetMatches(string patchCode) {
var regs = Regex.Matches(patchCode, RegexFuncPattern);
return regs;
}
public override string RemoveComment(string original) {
string txt = Regex.Replace(original, Environment.NewLine, "");
//StringBuilder sb = new StringBuilder();
//sb.Append("\"\"\"");
//sb.Append(@".*");
//sb.Append("\"\"\"");
string replace = txt;
//if (Regex.Match(txt, sb.ToString()).Success) {
// replace = Regex.Replace(txt, sb.ToString(), "");
//}
return replace;
}
protected override string GetOriginalFunc(Stream oldStream, string methodName) {
StringBuilder oldBuilder = new StringBuilder();
using (var reader = new StreamReader(oldStream)) {
bool found = false;
int bracketCount = -1;
while (!reader.EndOfStream) {
string line = reader.ReadLine();
if (found)
{
int openBracketCount = line.Count(c => c == '{');
int closeBracketCount = line.Count(c => c == '}');
if (bracketCount == -1)
{
}
if (line.Count(c => c == '{') > 0)
{
}
}
if (Regex.Match(line, $@"{methodName}").Success) {
found = true;
int openBracketCount = line.Count(c => c == '{');
int closeBracketCount = line.Count(c => c == '}');
int subtract = openBracketCount - closeBracketCount;
oldBuilder.AppendLine(line);
if (subtract < 0)
{
break;
}
bracketCount = subtract;
}
}
}
return oldBuilder.ToString();
}
}
}