-
Notifications
You must be signed in to change notification settings - Fork 120
/
ConstantsScraper.cs
1300 lines (1107 loc) · 60.1 KB
/
ConstantsScraper.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace MetadataUtils
{
public static class ConstantsScraper
{
public static ScraperResults ScrapeConstants(
string[] enumJsonFiles,
string defaultNamespace,
string scraperOutputDir,
string constantsHeaderText,
HashSet<string> exclusionNames,
Dictionary<string, string> traversedHeaderToNamespaceMap,
Dictionary<string, string> requiredNamespaces,
Dictionary<string, string> remaps,
Dictionary<string, string> withTypes,
Dictionary<string, string> withAttributes)
{
using ConstantsScraperImpl imp = new ConstantsScraperImpl();
return imp.ScrapeConstants(enumJsonFiles, defaultNamespace, scraperOutputDir, constantsHeaderText, exclusionNames, traversedHeaderToNamespaceMap, requiredNamespaces, remaps, withTypes, withAttributes);
}
private class ConstantsScraperImpl : IDisposable
{
private static readonly Regex DefineRegex =
new Regex(
@"^\s*#\s*define\s+([_A-Za-z][\dA-Za-z_]+)\s+(.+)");
private static readonly Regex DefineConstantRegex =
new Regex(
@"^((_HRESULT_TYPEDEF_|_NDIS_ERROR_TYPEDEF_)\(((?:0x)?[\da-f]+L?)\)|(\(HRESULT\)((?:0x)?[\da-f]+L?))|(-?\d+\.\d+(?:e\+\d+)?f?)|((?:0x[\da-f]+|\-?\d+)(?:UL|L)?)|((\d+)\s*(<<\s*\d+))|(MAKEINTRESOURCE[AW]{0,1}\(\s*(\-?\d+)\s*\))|(\(HWND\)(-?\d+|(?:0x)?[\da-f]+))|([a-z0-9_]+U?\s*[\+\-]\s*(\d+|0x[0-de-f]+)U?)|(\(NTSTATUS\)((?:0x)?[\da-f]+L?))|(\s*\(DWORD\)\s*\(?\s*-1(L|\b)\s*\)?)|(\(DWORD\)((?:0x)?[\da-f]+L?))|(\(BCRYPT_ALG_HANDLE\)\s*((?:0x)?[\da-f]+L?))|(\{\s*(?:(?:0x)?[\da-f]{4,8}L?,?\s*){3}\s*\{\s*(?:(?:0x)?[\da-f]{1,2}L?,?\s*){8}\s*\}\s*\})|(HIDP_ERROR_CODES\((.*),(.*)\))|(MAKEDIPROP\(\s*(\d+)\s*\))|(\{\s*(?:(?:0x)?[\da-f]{4,8}L?,?\s*){3}\s*(?:(?:0x)?[\da-f]{1,2}L?,?\s*){8}\s*\})|(\(UCHAR\)\s*((?:0x)?\d+))|(\(UCHAR\)\s*(-\d+))|(\(BYTE\)((?:0x)?[\da-f]+L?))|([a-z0-9_]+))$", RegexOptions.IgnoreCase);
private static readonly Regex DefineGuidConstRegex =
new Regex(
@"^\s*(DEFINE_GUID|DEFINE_DEVPROPKEY|DEFINE_PROPERTYKEY|DEFINE_KNOWN_FOLDER|OUR_GUID_ENTRY)\s*\((.*)");
private static readonly Regex DefineAviGuidConstRegex =
new Regex(
@"^\s*(DEFINE_AVIGUID)\s*\(\s*(.*),\s*(.*),\s*(.*),\s*(.*)\s*\);");
private static readonly Regex DefineMediaTypeGuidConstRegex =
new Regex(
@"^\s*(DEFINE_MEDIATYPE_GUID)\s*\(\s*(\S+),\s*(\S+)\s*\);");
private static readonly Regex DefinePciRootBusDevPkeyRegex =
new Regex(
@"^\s*(DEFINE_PCI_ROOT_BUS_DEVPKEY)\s*\(\s*(.*),\s*(.*)\s*\);");
private static readonly Regex DefinePciDeviceDevPkeyRegex =
new Regex(
@"^\s*(DEFINE_PCI_DEVICE_DEVPKEY)\s*\(\s*(.*),\s*(.*)\s*\);");
private static readonly Regex FccRegex =
new Regex(
@"FCC\(\'(.{4})\'\)");
private static readonly Regex DefineEnumFlagsRegex =
new Regex(
@"^\s*DEFINE_ENUM_FLAG_OPERATORS\(\s*(\S+)\s*\)\s*\;?\s*$");
private static readonly Regex CtlCodeRegex =
new Regex(
@"^\s*CTL_CODE\((.+)\)");
private static readonly Regex HidUsageRegex =
new Regex(
@"^\s*\(USAGE\)\s*(0x[\da-f]+)", RegexOptions.IgnoreCase);
private static readonly Regex MakeHresultRegex =
new Regex(
@"^\s*(?:MAKE_HRESULT|MAKE_SCODE)\((.+)\)");
private static readonly Regex IntCastToLpcstrRegex =
new Regex(
@"^\s*\((LPCSTR|LPCWSTR)\)\s*(\d+)");
private static readonly Regex NamePartsRegex = new Regex(@"[A-Z]+[a-z]*");
private static readonly Regex ContainsLowerCase = new Regex(@"[a-z]+");
private Dictionary<string, EnumWriter> namespacesToEnumWriters = new();
private Dictionary<string, IConstantWriter> namespacesToConstantWriters = new();
private WildcardDictionary requiredNamespaces;
private Dictionary<string, string> scannedNamesToNamespaces;
private Dictionary<string, string> writtenConstants;
private List<EnumObject> enumObjectsFromJsons = new();
private Dictionary<string, string> withTypes;
private Dictionary<string, string> withAttributes;
private Dictionary<string, List<EnumObject>> enumMemberNameToEnumObj;
private HashSet<string> exclusionNames = new();
private string scraperOutputDir;
private string constantsHeaderText;
private string enumFlagsFixupFileName;
private string defaultNamespace;
private List<string> output = new List<string>();
private List<string> suggestedEnumRenames = new List<string>();
// TODO: These could come from a file so we can edit a .json file instead
// of the code
private static readonly RegexConstMaker[] regexConstMakers = new RegexConstMaker[]
{
new RegexConstMaker() { Pattern = @"_WSAIO\((.+),(.+)\)", ConstType = "uint", OutputFormat = "(IOC_VOID|({0})|({1}))" },
new RegexConstMaker() { Pattern = @"_WSAIOR\((.+),(.+)\)", ConstType = "uint", OutputFormat = "(IOC_OUT|({0})|({1}))" },
new RegexConstMaker() { Pattern = @"_WSAIOW\((.+),(.+)\)", ConstType = "uint", OutputFormat = "(IOC_IN|({0})|({1}))" },
new RegexConstMaker() { Pattern = @"_WSAIORW\((.+),(.+)\)", ConstType = "uint", OutputFormat = "(IOC_INOUT|({0})|({1}))" },
};
private IRegexConstHelper regexConstHelper;
internal IFileSystem _fileSystem { get; set; } = new FileSystem();
public ConstantsScraperImpl()
{
}
public ScraperResults ScrapeConstants(
string[] enumJsonFiles,
string defaultNamespace,
string scraperOutputDir,
string constantsHeaderText,
HashSet<string> exclusionNames,
Dictionary<string, string> traversedHeaderToNamespaceMap,
Dictionary<string, string> requiredNamespaces,
Dictionary<string, string> remaps,
Dictionary<string, string> withTypes,
Dictionary<string, string> withAttributes)
{
this.requiredNamespaces = new WildcardDictionary(requiredNamespaces);
this.withTypes = withTypes;
this.exclusionNames = exclusionNames;
this.constantsHeaderText = constantsHeaderText;
this.withAttributes = withAttributes;
this.scraperOutputDir = scraperOutputDir;
this.defaultNamespace = defaultNamespace;
this.regexConstHelper = new RegexConstHelper(regexConstMakers, this);
this.scannedNamesToNamespaces = ScraperUtils.GetNameToNamespaceMap(scraperOutputDir);
this.writtenConstants = ScraperUtils.GetConstants(scraperOutputDir);
this.CleanExistingFiles();
this.LoadEnumObjectsFromJsonFiles(enumJsonFiles);
this.ScrapeConstantsFromTraversedFiles(traversedHeaderToNamespaceMap);
this.WriteEnumsAndRemaps(remaps);
return new ScraperResults(this.output);
}
public void Dispose()
{
foreach (EnumWriter enumWriter in this.namespacesToEnumWriters.Values)
{
enumWriter.Dispose();
}
this.namespacesToEnumWriters.Clear();
foreach (ConstantWriter constantWriter in this.namespacesToConstantWriters.Values)
{
constantWriter.Dispose();
}
this.namespacesToConstantWriters.Clear();
}
private static Dictionary<string, string> GetAutoValueReplacements()
{
Dictionary<string, string> ret = new Dictionary<string, string>();
ret["TRUE"] = "1";
ret["FALSE"] = "0";
return ret;
}
private static List<EnumObject> LoadEnumsFromSourceFiles(IEnumerable<string> fileNames)
{
List<EnumObject> enumObjects = new List<EnumObject>();
foreach (var file in fileNames)
{
enumObjects.AddRange(EnumObject.LoadFromFile(file));
}
return enumObjects;
}
private static string StripComments(string rawValue)
{
bool inQuote = false;
for (int i = 0; i <= rawValue.Length - 2; i++)
{
if (rawValue[i] == '\"')
{
inQuote = !inQuote;
}
if (!inQuote && rawValue[i] == '/')
{
if (rawValue[i + 1] == '/')
{
// Remove trailing line comments.
return rawValue.Substring(0, i).Trim();
}
else if (rawValue[i + 1] == '*')
{
if (rawValue.LastIndexOf("*/") == -1)
{
// Remove trailing block comments that aren't closed.
return rawValue.Substring(0, i).Trim();
}
else
{
// Remove inline block comments that are closed.
return rawValue.Substring(0, i).Trim() + rawValue.Substring(rawValue.LastIndexOf("*/") + 2).Trim();
}
}
}
}
return rawValue;
}
private void CleanExistingFiles()
{
foreach (string file in Directory.GetFiles(this.scraperOutputDir).Where(f => f.EndsWith(".enums.cs") || f.EndsWith(".constants.cs")))
{
this._fileSystem.File.Delete(file);
}
}
private void InitEnumFlagsFixupFile()
{
if (this.enumFlagsFixupFileName == null)
{
this.enumFlagsFixupFileName = Path.Combine(this.scraperOutputDir, "enumsMakeFlags.generated.rsp");
if (this._fileSystem.File.Exists(this.enumFlagsFixupFileName))
{
this._fileSystem.File.Delete(this.enumFlagsFixupFileName);
}
this._fileSystem.File.AppendAllText(this.enumFlagsFixupFileName, "--enumMakeFlags\r\n");
}
}
private void LoadMemberNameToEnumObjMap(List<EnumObject> enumObjects)
{
this.enumMemberNameToEnumObj = new Dictionary<string, List<EnumObject>>();
foreach (EnumObject obj in enumObjects)
{
foreach (EnumObject.Member member in obj.members)
{
if (StringComparer.OrdinalIgnoreCase.Equals(member.name, "None"))
{
continue;
}
if (!this.enumMemberNameToEnumObj.TryGetValue(member.name, out var objList))
{
objList = new List<EnumObject>();
this.enumMemberNameToEnumObj[member.name] = objList;
}
objList.Add(obj);
}
}
}
private List<EnumObject> LoadEnumsFromJsonFiles(string[] enumJsonFiles)
{
List<EnumObject> enumObjects = new List<EnumObject>();
if (enumJsonFiles != null)
{
foreach (var enumJsonFile in enumJsonFiles)
{
enumObjects.AddRange(EnumObject.LoadFromFile(enumJsonFile));
}
}
return enumObjects;
}
private string GetForcedTypeForName(string name)
{
// Make all error codes uint to match GetLastError even though they're defined as signed
// in winerror.h
if (name.StartsWith("ERROR_"))
{
return "uint";
}
this.withTypes.TryGetValue(name, out string forceType);
if (string.IsNullOrEmpty(forceType))
{
var wildCards = this.withTypes.Where(p => p.Key.EndsWith("*"));
foreach (var wildCard in wildCards)
{
if (name.StartsWith(wildCard.Key.Replace("*", "")))
{
forceType = wildCard.Value;
break;
}
}
}
return forceType;
}
private void AddMakeHresultConstant(string originalNamespace, string name, string severity, string facility, string code)
{
string valueText = $"unchecked((int)(({severity}) << 31) | (((int)({facility})) << 16) | (int)({code}))";
this.AddConstantInteger(originalNamespace, "HRESULT", name, valueText);
}
private void AddCtlCodeConstant(string originalNamespace, string name, string deviceType, string function, string method, string access)
{
if (this.writtenConstants.ContainsKey(name))
{
return;
}
var writer = this.GetConstantWriter(originalNamespace, name);
function = function.Replace("SCMBUS_FUNCTION(", "(IOCTL_SCMBUS_DEVICE_FUNCTION_BASE + ");
function = function.Replace("SCM_LOGICAL_DEVICE_FUNCTION(", "(IOCTL_SCM_LOGICAL_DEVICE_FUNCTION_BASE + ");
function = function.Replace("SCM_PHYSICAL_DEVICE_FUNCTION(", "(IOCTL_SCM_PHYSICAL_DEVICE_FUNCTION_BASE + ");
writer.AddValue("uint", name, $"(({deviceType}) << 16) | (uint)(((int)({access})) << 14) | (({function}) << 2) | ({method})");
this.writtenConstants.Add(name, "uint");
}
private void AddConstantValue(string originalNamespace, string type, string name, string valueText, string context = "")
{
if (this.writtenConstants.ContainsKey(name))
{
return;
}
var writer = this.GetConstantWriter(originalNamespace, name);
writer.AddValue(type, name, valueText, context);
this.writtenConstants.Add(name, type);
}
private void AddConstantGuid(string defineGuidKeyword, string originalNamespace, string line)
{
int firstComma = line.IndexOf(',');
string name = line.Substring(0, firstComma).Trim();
if (this.writtenConstants.ContainsKey(name))
{
return;
}
if (this.ShouldExclude(name))
{
return;
}
string args = line.Substring(firstComma + 1).Trim();
int closeParen = args.IndexOf(')');
args = args.Substring(0, closeParen);
args = this.GetCanonicalGuidConstantIntegerArgs(args);
var writer = this.GetConstantWriter(originalNamespace, name);
if (defineGuidKeyword == "DEFINE_DEVPROPKEY" || defineGuidKeyword == "DEFINE_PROPERTYKEY")
{
string structType = defineGuidKeyword == "DEFINE_DEVPROPKEY" ? "DEVPROPKEY" : "PROPERTYKEY";
var guidParts = args[..args.LastIndexOf(',')].Split(", ");
for (int i = 0; i < guidParts.Length; i++)
{
guidParts[i] = Convert.ToUInt32(guidParts[i].Trim(), 16).ToString();
}
var fmtid = $"{{{string.Join(", ", guidParts)}}}";
var pid = args[(args.LastIndexOf(',') + 1)..].Trim();
pid = pid.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase) ? Convert.ToUInt32(pid, 16).ToString() : pid;
writer.AddPropKey(structType, name, $"\"{fmtid}, {pid}\"");
}
else
{
writer.AddGuid(name, args);
}
this.writtenConstants.Add(name, "Guid");
}
private string GetCanonicalGuidConstantIntegerArgs(string args)
{
return Regex.Replace(args, "\\s*'(.*?)'\\s*", m =>
$"0x{Convert.ToHexString(Encoding.ASCII.GetBytes(m.Groups[1].Value))}", RegexOptions.IgnoreCase);
}
private void AddConstantInteger(string originalNamespace, string nativeTypeName, string name, string valueText)
{
if (this.writtenConstants.ContainsKey(name))
{
return;
}
string forcedType = nativeTypeName != null ? null : this.GetForcedTypeForName(name);
var writer = this.GetConstantWriter(originalNamespace, name);
writer.AddInt(forcedType, nativeTypeName, name, valueText, out var finalType);
this.writtenConstants.Add(name, finalType);
}
private void AddConstantShort(string originalNamespace, string nativeTypeName, string name, string valueText)
{
if (this.writtenConstants.ContainsKey(name))
{
return;
}
var writer = this.GetConstantWriter(originalNamespace, name);
writer.AddShort(nativeTypeName, name, valueText, out var finalType);
this.writtenConstants.Add(name, finalType);
}
private IConstantWriter GetConstantWriter(string originalNamespace, string name)
{
string foundNamespace = originalNamespace;
string newNamespace = this.LookupNamespaceForName(name);
if (!string.IsNullOrEmpty(newNamespace))
{
foundNamespace = newNamespace;
}
if (!this.namespacesToConstantWriters.TryGetValue(foundNamespace, out IConstantWriter constantWriter))
{
string partConstantsFile = Path.Combine(this.scraperOutputDir, $@"{foundNamespace}.constants.cs");
if (this._fileSystem.File.Exists(partConstantsFile))
{
this._fileSystem.File.Delete(partConstantsFile);
}
var fileStream = this._fileSystem.File.OpenWrite(partConstantsFile);
constantWriter = new ConstantWriter(fileStream, foundNamespace, this.constantsHeaderText, this.withAttributes);
this.namespacesToConstantWriters.Add(foundNamespace, constantWriter);
}
return constantWriter;
}
private HashSet<string> GetManualEnumMemberNames()
{
List<EnumObject> enumObjectsFromManualSources = LoadEnumsFromSourceFiles(this._fileSystem.Directory.GetFiles(this.scraperOutputDir, "*.manual.cs"));
HashSet<string> manualEnumMemberNames = new HashSet<string>();
foreach (EnumObject obj in enumObjectsFromManualSources)
{
foreach (EnumObject.Member member in obj.members)
{
manualEnumMemberNames.Add(member.name);
}
}
return manualEnumMemberNames;
}
private void LoadEnumObjectsFromJsonFiles(string[] enumJsonFiles)
{
// Load the enums scraped from the docs
this.enumObjectsFromJsons = this.LoadEnumsFromJsonFiles(enumJsonFiles);
// Load a map from member names to enum obj
this.LoadMemberNameToEnumObjMap(this.enumObjectsFromJsons);
}
private bool ShouldExclude(string constName)
{
return this.exclusionNames.Contains(constName);
}
private void ScrapeConstantsFromTraversedFiles(Dictionary<string, string> traversedFileMap)
{
Dictionary<string, string> autoReplacements = GetAutoValueReplacements();
HashSet<string> manualEnumMemberNames = this.GetManualEnumMemberNames();
// For each traversed header, scrape the constants
foreach (KeyValuePair<string, string> item in traversedFileMap)
{
var header = item.Key;
var currentNamespace = item.Value;
if (!this._fileSystem.File.Exists(header))
{
continue;
}
string currentHeaderName = Path.GetFileName(header).ToLowerInvariant();
var autoEnumObjsForCurrentHeader =
this.enumObjectsFromJsons
.Where(
e => e.autoPopulate != null &&
!string.IsNullOrEmpty(e.autoPopulate.filter) &&
e.autoPopulate.header.ToLowerInvariant().Split(';').Contains(currentHeaderName))
.ToArray();
Regex autoPopulateReg = null;
if (autoEnumObjsForCurrentHeader.Length != 0)
{
StringBuilder autoPopulateRegexPattern = new StringBuilder();
foreach (EnumObject autoEnumObj in autoEnumObjsForCurrentHeader)
{
if (autoPopulateRegexPattern.Length != 0)
{
autoPopulateRegexPattern.Append('|');
}
autoPopulateRegexPattern.Append($"(^{autoEnumObj.autoPopulate.filter})");
}
autoPopulateReg = new Regex(autoPopulateRegexPattern.ToString());
}
string continuation = null;
string defineRegexContinuation = null;
bool processingGuidMultiLine = false;
string defineGuidKeyword = null;
foreach (string currentLine in this._fileSystem.File.ReadAllLines(header))
{
string fixedCurrentLine = currentLine;
if (continuation != null && continuation.EndsWith('"') && currentLine.StartsWith('"'))
{
continuation = continuation.Substring(0, continuation.Length - 1);
fixedCurrentLine = currentLine.Substring(1);
}
string line = continuation == null ? fixedCurrentLine : continuation + fixedCurrentLine;
if (line.EndsWith("\\"))
{
continuation = line.Substring(0, line.Length - 1);
continue;
}
if (processingGuidMultiLine)
{
continuation = StripComments(line).Trim();
if (continuation.EndsWith(';') || continuation.EndsWith(')'))
{
processingGuidMultiLine = false;
this.AddConstantGuid(defineGuidKeyword, currentNamespace, continuation);
continuation = null;
}
continue;
}
continuation = null;
Match defineGuidMatch = DefineGuidConstRegex.Match(line);
if (defineGuidMatch.Success)
{
defineGuidKeyword = defineGuidMatch.Groups[1].Value;
line = defineGuidMatch.Groups[2].Value;
line = StripComments(line).Trim();
if (line.EndsWith(';'))
{
this.AddConstantGuid(defineGuidKeyword, currentNamespace, line);
}
else
{
continuation = line;
processingGuidMultiLine = true;
}
continue;
}
Match defineAviGuidMatch = DefineAviGuidConstRegex.Match(line);
if (defineAviGuidMatch.Success)
{
defineGuidKeyword = defineAviGuidMatch.Groups[1].Value;
var guidName = defineAviGuidMatch.Groups[2].Value;
var l = defineAviGuidMatch.Groups[3].Value;
var w1 = defineAviGuidMatch.Groups[4].Value;
var w2 = defineAviGuidMatch.Groups[5].Value;
var defineGuidLine = $"{guidName}, {l}, {w1}, {w2}, 0xC0,0,0,0,0,0,0,0x46)";
this.AddConstantGuid(defineGuidKeyword, currentNamespace, defineGuidLine);
continue;
}
Match defineMediaTypeGuidMatch = DefineMediaTypeGuidConstRegex.Match(line);
if (defineMediaTypeGuidMatch.Success)
{
defineGuidKeyword = defineMediaTypeGuidMatch.Groups[1].Value;
var guidName = defineMediaTypeGuidMatch.Groups[2].Value;
var value = defineMediaTypeGuidMatch.Groups[3].Value;
var fccMatch = FccRegex.Match(value);
if (fccMatch.Success)
{
var fccValue = fccMatch.Groups[1].Value.ToArray();
uint convertedValue =
(uint)(fccValue[0]) |
(uint)(fccValue[1] << 8) |
(uint)(fccValue[2] << 16) |
(uint)(fccValue[3] << 24);
value = $"0x{convertedValue:x}";
}
var defineGuidLine = $"{guidName}, {value}, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71)";
this.AddConstantGuid(defineGuidKeyword, currentNamespace, defineGuidLine);
continue;
}
Match definePciRootBusDevPkeyRegexMatch = DefinePciRootBusDevPkeyRegex.Match(line);
if (definePciRootBusDevPkeyRegexMatch.Success)
{
defineGuidKeyword = "DEFINE_DEVPROPKEY";
var guidName = definePciRootBusDevPkeyRegexMatch.Groups[2].Value;
var pid = definePciRootBusDevPkeyRegexMatch.Groups[3].Value;
var defineGuidLine = $"{guidName}, 0xd817fc28, 0x793e, 0x4b9e, 0x99, 0x70, 0x46, 0x9d, 0x8b, 0xe6, 0x30, 0x73, {pid})";
this.AddConstantGuid(defineGuidKeyword, currentNamespace, defineGuidLine);
continue;
}
Match definePciDeviceDevPkeyRegexMatch = DefinePciDeviceDevPkeyRegex.Match(line);
if (definePciDeviceDevPkeyRegexMatch.Success)
{
defineGuidKeyword = "DEFINE_DEVPROPKEY";
var guidName = definePciDeviceDevPkeyRegexMatch.Groups[2].Value;
var pid = definePciDeviceDevPkeyRegexMatch.Groups[3].Value;
var defineGuidLine = $"{guidName}, 0x3ab22e31, 0x8264, 0x4b4e, 0x9a, 0xf5, 0xa8, 0xd2, 0xd8, 0xe3, 0x3e, 0x62, {pid})";
this.AddConstantGuid(defineGuidKeyword, currentNamespace, defineGuidLine);
continue;
}
line = defineRegexContinuation == null ? line : defineRegexContinuation + line;
if (line.EndsWith("\\"))
{
defineRegexContinuation = line.Substring(0, line.Length - 1);
continue;
}
else
{
defineRegexContinuation = null;
}
Match defineMatch = DefineRegex.Match(line);
// Skip if not #define ...
if (!defineMatch.Success)
{
this.TryScrapingEnumFlags(line);
continue;
}
string name = defineMatch.Groups[1].Value;
if (this.ShouldExclude(name))
{
continue;
}
#if DEBUG
if (name == "SOME_CONST_NAME")
{
}
#endif
// Get rid of trailing comments
string rawValue = StripComments(defineMatch.Groups[2].Value.Trim());
if (autoReplacements.TryGetValue(rawValue, out var updatedRawValue))
{
rawValue = updatedRawValue;
}
string fixedRawValue = rawValue;
// Get rid of enclosing parens. Makes it easier to parse with regex
if (fixedRawValue.StartsWith('(') && fixedRawValue.EndsWith(')'))
{
fixedRawValue = fixedRawValue.Substring(1, rawValue.Length - 2).Trim();
}
Match ctlCodeMatch = CtlCodeRegex.Match(fixedRawValue);
if (ctlCodeMatch.Success)
{
var parts = ctlCodeMatch.Groups[1].Value.Split(',');
if (parts.Length == 4)
{
this.AddCtlCodeConstant(currentNamespace, name, parts[0].Trim(), parts[1].Trim(), parts[2].Trim(), parts[3].Trim());
continue;
}
}
Match usageMatch = HidUsageRegex.Match(fixedRawValue);
if (usageMatch.Success)
{
this.AddConstantValue(currentNamespace, "ushort", name, usageMatch.Groups[1].Value);
continue;
}
if (fixedRawValue.StartsWith("AUDCLNT_ERR("))
{
fixedRawValue = fixedRawValue.Replace("AUDCLNT_ERR(", "MAKE_HRESULT(SEVERITY_ERROR, FACILITY_AUDCLNT, ");
}
else if (fixedRawValue.StartsWith("AUDCLNT_SUCCESS("))
{
fixedRawValue = fixedRawValue.Replace("AUDCLNT_SUCCESS(", "MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_AUDCLNT, ");
}
Match makeHresultMatch = MakeHresultRegex.Match(fixedRawValue);
if (makeHresultMatch.Success)
{
var parts = makeHresultMatch.Groups[1].Value.Split(',');
if (parts.Length == 3)
{
this.AddMakeHresultConstant(currentNamespace, name, parts[0].Trim(), parts[1].Trim(), parts[2].Trim());
continue;
}
}
Match intCastToLpcstrMatch = IntCastToLpcstrRegex.Match(fixedRawValue);
if (intCastToLpcstrMatch.Success)
{
var nativeStrType = intCastToLpcstrMatch.Groups[1].Value;
var value = intCastToLpcstrMatch.Groups[2].Value;
this.AddConstantInteger(currentNamespace, nativeStrType, name, value);
continue;
}
if (this.regexConstHelper.TryProcessingLine(currentNamespace, name, fixedRawValue))
{
continue;
}
// See if matches one of our well known constants formats
Match match = DefineConstantRegex.Match(fixedRawValue);
string valueText;
string nativeTypeName = null;
string matchedConstantType = null;
bool matchedToOtherName = false;
if (match.Success)
{
// #define E_UNEXPECTED _HRESULT_TYPEDEF_(0x8000FFFF)
if (!string.IsNullOrEmpty(match.Groups[2].Value))
{
if (match.Groups[2].Value == "_HRESULT_TYPEDEF_")
{
nativeTypeName = "HRESULT";
}
valueText = match.Groups[3].Value;
}
// #define E_UNEXPECTED ((HRESULT)0x8000FFFF)
else if (!string.IsNullOrEmpty(match.Groups[5].Value))
{
nativeTypeName = "HRESULT";
valueText = match.Groups[5].Value;
}
// #define DXGI_RESOURCE_PRIORITY_MINIMUM ( 0x28000000 )
else if (!string.IsNullOrEmpty(match.Groups[7].Value))
{
valueText = match.Groups[7].Value;
}
// 1.0, -2.0f
else if (!string.IsNullOrEmpty(match.Groups[6].Value))
{
valueText = match.Groups[6].Value;
string type = valueText.EndsWith('f') ? "float" : "double";
this.AddConstantValue(currentNamespace, type, name, valueText);
continue;
}
// 1 << 5
else if (!string.IsNullOrEmpty(match.Groups[8].Value))
{
string part1 = match.Groups[9].Value + "u";
string part2 = match.Groups[10].Value;
valueText = part1 + part2;
}
// MAKEINTRESOURCE(-4), MAKEINTRESOURCEA(-1), MAKEINTRESOURCEW(42)
else if (!string.IsNullOrEmpty(match.Groups[11].Value))
{
if (match.Groups[11].Value.StartsWith("MAKEINTRESOURCEA"))
{
nativeTypeName = "LPCSTR";
}
else
{
nativeTypeName = "LPCWSTR";
}
valueText = match.Groups[12].Value;
this.AddConstantShort(currentNamespace, nativeTypeName, name, valueText);
continue;
}
// (HWND)-4
else if (!string.IsNullOrEmpty(match.Groups[13].Value))
{
nativeTypeName = "HWND";
valueText = match.Groups[14].Value;
this.AddConstantInteger(currentNamespace, nativeTypeName, name, valueText);
continue;
}
// (IDENT_FOO +/- 4)
else if (match.Groups[15].Success)
{
valueText = match.Groups[15].Value;
}
// (NTSTATUS)0x00000000L
else if (match.Groups[17].Success)
{
nativeTypeName = "NTSTATUS";
valueText = match.Groups[18].Value;
}
// (DWORD)-1
else if (match.Groups[20].Success)
{
nativeTypeName = "DWORD";
valueText = "0xFFFFFFFF";
}
// (DWORD)0xFFFFFFFF
else if (match.Groups[21].Success)
{
nativeTypeName = "DWORD";
valueText = match.Groups[22].Value;
}
// (BCRYPT_ALG_HANDLE) 0x000001a1
else if (match.Groups[23].Success)
{
nativeTypeName = "BCRYPT_ALG_HANDLE";
valueText = match.Groups[24].Value;
}
// {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
else if (match.Groups[25].Success)
{
valueText = match.Groups[25].Value.Replace("{", "").Replace("}", "").Replace(" ", "");
var defineGuidLine = $"{name}, {valueText})";
this.AddConstantGuid("", currentNamespace, defineGuidLine);
continue;
}
// HIDP_ERROR_CODES(0x0,0)
else if (match.Groups[26].Success)
{
nativeTypeName = "NTSTATUS";
var SEV = int.Parse(match.Groups[27].Value.Replace("0x", String.Empty), NumberStyles.HexNumber);
var CODE = int.Parse(match.Groups[28].Value.Replace("0x", String.Empty), NumberStyles.HexNumber);
valueText = $"(({SEV} << 28) | (0x11 << 16) | ({CODE}))";
this.AddConstantInteger(currentNamespace, nativeTypeName, name, valueText);
continue;
}
// MAKEDIPROP(1)
else if (match.Groups[29].Success)
{
var value = Convert.ToUInt32(match.Groups[30].Value).ToString("X2");
var defineGuidLine = $"{name}, 0x00000000L, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x{value})";
this.AddConstantGuid("MAKEDIPROP", currentNamespace, defineGuidLine);
continue;
}
// { 0x35378EAC, 0x683F, 0x11D2, 0xA8, 0x9A, 0x00, 0xC0, 0x4F, 0xBB, 0xCF, 0xA2 }
else if (match.Groups[31].Success)
{
valueText = match.Groups[31].Value.Replace("{", "").Replace("}", "").Replace(" ", "");
var defineGuidLine = $"{name}, {valueText})";
this.AddConstantGuid("", currentNamespace, defineGuidLine);
continue;
}
// (UCHAR) 123
else if (match.Groups[32].Success)
{
this.AddConstantValue(currentNamespace, "ushort", name, match.Groups[33].Value);
continue;
}
// (UCHAR) -42
else if (match.Groups[34].Success)
{
this.AddConstantValue(currentNamespace, "ushort", name, $"unchecked((ushort){match.Groups[35].Value})");
continue;
}
// (BYTE) 0x42
else if (match.Groups[36].Success)
{
this.AddConstantValue(currentNamespace, "byte", name, match.Groups[37].Value);
continue;
}
// SOME_OTHER_CONSTANT
else if (match.Groups[38].Success)
{
string otherName = match.Groups[38].Value;
matchedToOtherName = true;
// Only use a constant as the value if we have seen the constant before
// and we know its type
if (this.writtenConstants.TryGetValue(otherName, out var otherType))
{
// Skip guids for now
if (otherType != "Guid")
{
matchedConstantType = otherType;
}
}
// If we didn't match it to another constant, keep going as we may be setting an enum
valueText = otherName;
}
else
{
continue;
}
}
else
{
valueText = rawValue;
if (valueText.StartsWith("__TEXT("))
{
valueText = valueText.Substring(2);
}
bool isUtf16 = false;
if (valueText.StartsWith("TEXT("))
{
isUtf16 = true;
valueText = valueText.Substring("TEXT(".Length);
if (valueText.EndsWith(')'))
{
valueText = valueText.Substring(0, valueText.Length - 1);
}
}
else if (valueText.StartsWith("L\""))
{
isUtf16 = true;
valueText = valueText.Substring(1);
}
// Strings can't be part of enums so go ahead and add the constant directly
if (valueText.StartsWith('"'))
{
this.AddConstantValue(currentNamespace, "string", name, valueText, isUtf16 ? "utf-16" : "ansi");
continue;
}
valueText = valueText.Replace("(DWORD)", "(uint)");
valueText = valueText.Replace("(ULONG)", "(uint)");
}
bool updatedEnum = false;
// If we see the member is part of an enum, update the member value
if (this.enumMemberNameToEnumObj.TryGetValue(name, out var enumObjList))
{
foreach (EnumObject enumObj in enumObjList)
{
enumObj.AddIfNotSet(name, valueText);
updatedEnum = true;
}
}
if (autoPopulateReg != null && nativeTypeName == null)
{
Match autoPopulate = autoPopulateReg.Match(name);
if (autoPopulate.Success)
{
for (int i = 1; i < autoPopulate.Groups.Count; i++)
{
if (!string.IsNullOrEmpty(autoPopulate.Groups[i].Value))
{
EnumObject foundObjEnum = autoEnumObjsForCurrentHeader[i - 1];
foundObjEnum.AddIfNotSet(name, valueText);
updatedEnum = true;
if (!this.enumMemberNameToEnumObj.TryGetValue(name, out var list))
{
list = new List<EnumObject>();
this.enumMemberNameToEnumObj.Add(name, list);
}