-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
203 lines (176 loc) · 8.35 KB
/
Program.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using CommandLine;
using CppAst;
using PeNet;
namespace DLLHeaderParser
{
class Program
{
static void RunOptions(CommandLineOptions opts)
{
//handle options
if (opts.Verbose)
{
Console.WriteLine("parsing with the following arguments:");
/* iterate through the CommandLineOptions fields*/
foreach (var argument in typeof(CommandLineOptions).GetProperties())
{
if (argument.PropertyType.IsArray || argument.PropertyType.IsEquivalentTo(typeof(IEnumerable<string>)))
{
var array = ((IEnumerable)argument.GetValue(opts)).Cast<object>().ToArray();
foreach (object ob in array)
{
Console.WriteLine("{0} = {1}", argument.Name, ob.ToString());
}
}
else
{
Console.WriteLine("{0} = {1}", argument.Name, argument.GetValue(opts));
}
}
}
}
static void HandleParseError(IEnumerable<Error> errs)
{
//handle errors
Console.WriteLine("Error in parsing arguments!");
Environment.Exit(-1);
}
static void Main(string[] args)
{
var result = Parser.Default.ParseArguments<CommandLineOptions>(args)
.WithParsed(RunOptions)
.WithNotParsed(HandleParseError);
Parsed<CommandLineOptions> cmdoptions = (Parsed<CommandLineOptions>)result;
//redirect Console.Out to log file if instructed so
if (cmdoptions.Value.writeLog != null) {
FileStream filestream = new FileStream(cmdoptions.Value.writeLog , FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
}
/* ================== 1- PARSE INPUT ARGUMENTS ===================== */
/* ===== Forward cmdoptions to parserOptions ==== */
var parserOptions = new CppParserOptions();
/* Compiler defines */
parserOptions.Defines.AddRange(cmdoptions.Value.CompilerDefines);
/* additinal arguments */
parserOptions.AdditionalArguments.AddRange(cmdoptions.Value.AdditionalArguments);
/* DLL include folders */
parserOptions.IncludeFolders.AddRange(cmdoptions.Value.IncludeFolders);
/* Compiler include folders */
parserOptions.SystemIncludeFolders.AddRange(cmdoptions.Value.SystemIncludeFolders);
/* disable parsing system includes */
parserOptions.ParseSystemIncludes = false;
/* enable parsing macros? */
parserOptions.ParseMacros = cmdoptions.Value.ParseMacros;
/* enable parsing attributes? */
parserOptions.ParseAttributes = cmdoptions.Value.ParseAttributes;
/* parse files as c++? (usually false does not work for C)*/
parserOptions.ParseAsCpp = (bool)cmdoptions.Value.ParseAsCpp;
/* disable autosquashing typedefs as we need the typedefs intact*/
parserOptions.AutoSquashTypedef = false;
/* add all input files and folders */
List<string> mainfiles = new List<string>();
//add files from folders
foreach (var dir in cmdoptions.Value.MainFolders.ToList()) {
mainfiles.AddRange(Directory.EnumerateFiles(dir,"*.h"));
}
//add direct files
mainfiles.AddRange(cmdoptions.Value.MainFiles.ToList());
//remove files from the exclusion list if any
foreach(var fileex in cmdoptions.Value.MainfilesEx)
{
if (!mainfiles.Remove(fileex))
{
Console.WriteLine(fileex + " was not found/excluded from the compilation");
}
else {
Console.WriteLine(fileex + " excluded from the compilation!");
}
}
/* ================== 1.1- PARSE THE FILES ===================== */
CppCompilation parserResult = CppParser.ParseFiles(mainfiles, parserOptions);
/*print input text to parser in case of verbose */
if (cmdoptions.Value.Verbose) {
Console.WriteLine();
Console.WriteLine("List of input text to parser:");
Console.WriteLine(parserResult.InputText);
}
if (parserResult.HasErrors) {
Console.WriteLine("List of Errors:");
foreach ( var msg in parserResult.Diagnostics.Messages) {
if (msg.Type == CppLogMessageType.Error) {
Console.WriteLine("Error:{0}:{1}",msg.Location,msg.Text);
}
}
Console.WriteLine();
}
/*print parser warnings in case of verbose */
if (cmdoptions.Value.Verbose)
{
Console.WriteLine("List of Warnings:");
foreach (var msg in parserResult.Diagnostics.Messages)
{
if (msg.Type == CppLogMessageType.Warning)
{
Console.WriteLine("Warning:{0}:{1}", msg.Location, msg.Text);
}
}
Console.WriteLine();
}
/* ================== 2- READ INPUT DLL TO GET FUNCTIONS ===================== */
/* get dll functions */
PeFile inputdll = null;
try
{
inputdll = new PeNet.PeFile(cmdoptions.Value.InputDLL);
}
catch (Exception e) {
Console.WriteLine(e.Message);
Console.WriteLine("Error in opening the DLL file");
Environment.Exit(-1);
}
if (parserResult.HasErrors) {
Console.WriteLine("Parser reported errors. Aborting...");
Environment.Exit(-1);
}
/* Extract Dll functions from file and save them for mapping later */
List<string> dllfunctions = inputdll.ExportedFunctions.ToList().ConvertAll(x => x.Name);
if (cmdoptions.Value.Verbose) {
Console.WriteLine(Environment.NewLine + "Found following functions in DLL:");
dllfunctions.ForEach(Console.WriteLine);
}
/* ================== 2.1- Match DLL functions with parser output ===================== */
//filter , intersect the dllfunctions with parser funtions -> list of functions we need with their type
var matchingFunctions = parserResult.Functions.Where(x => dllfunctions.Contains(x.Name)).ToList();
if (cmdoptions.Value.Verbose)
{
Console.WriteLine(Environment.NewLine + "Matching items:");
matchingFunctions.ForEach(x => Console.WriteLine(x.Name));
}
/* issue a warning for the functions that are not found in header file */
Console.WriteLine(Environment.NewLine + "Warning ! Missing Items:");
var nonMatchingItems = dllfunctions.Where(function => !matchingFunctions.Exists(matchingItem => function.Equals(matchingItem.Name)));
nonMatchingItems.ToList().ForEach(Console.WriteLine);
Console.WriteLine();
/* ================== 3- Build results ===================== */
/* generate results for WinApiOverride */
if (cmdoptions.Value.genWinApiOverride) {
WinApiOverride.genWinApiOverride(parserResult, matchingFunctions, cmdoptions.Value.WinApiOverrideFolder, cmdoptions.Value.InputDLL);
}
/* generate results for ApiMonitor */
if (cmdoptions.Value.genApiMonitor)
{
/* now we need to build the xml for the matching functions */
XmlMaker.genResults(parserResult, matchingFunctions);
}
Console.WriteLine("Parsing Finished successfully!");
}
}
}