Skip to content

Commit

Permalink
GenerateZw: Update to .NET 9
Browse files Browse the repository at this point in the history
  • Loading branch information
dmex committed Dec 13, 2024
1 parent 2ccd757 commit cec7798
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 46 deletions.
5 changes: 4 additions & 1 deletion tools/GenerateZw/GenerateZw.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@
<OutputType>Exe</OutputType>
<Platforms>AnyCPU</Platforms>
<PlatformTarget>AnyCPU</PlatformTarget>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<IntDir>$(ProjectDir)obj\$(Configuration)\$(TargetFramework)-$(PlatformTarget)\</IntDir>
<OutDir>$(ProjectDir)bin\$(Configuration)\$(TargetFramework)-$(PlatformTarget)\</OutDir>
<PublishAot>true</PublishAot>
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants />
<DebugType>embedded</DebugType>
<DebugSymbols>true</DebugSymbols>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>

</Project>
72 changes: 27 additions & 45 deletions tools/GenerateZw/ZwGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,7 @@ public static class ZwGen
private static readonly string OutputFile = "ntzwapi.h";
private static readonly string Header = "#ifndef _NTZWAPI_H\r\n#define _NTZWAPI_H\r\n\r\n// This file was automatically generated. Do not edit.\r\n\r\n";
private static readonly string Footer = "#endif\r\n";
public static readonly string[] Files =
{
"ntdbg.h",
"ntexapi.h",
"ntgdi.h",
"ntioapi.h",
"ntkeapi.h",
"ntldr.h",
"ntlpcapi.h",
"ntmisc.h",
"ntmmapi.h",
"ntnls.h",
"ntobapi.h",
"ntpebteb.h",
"ntpfapi.h",
"ntpnpapi.h",
"ntpoapi.h",
"ntpsapi.h",
"ntregapi.h",
"ntrtl.h",
"ntsam.h",
"ntseapi.h",
"nttmapi.h",
"nttp.h",
"ntwow64.h",
"ntxcapi.h"
};
public static readonly string[] Exclude = { "ntuser.h" };

static ZwGen()
{
Expand All @@ -70,26 +44,30 @@ static ZwGen()

public static void Execute()
{
var files = Directory.EnumerateFiles(BaseDirectory, "*.h", SearchOption.AllDirectories);

// Build up a list of definitions.

var definitions = new List<ServiceDefinition>();
var regex = RegexDefinition.Regex();

foreach (string fileName in Files)
foreach (string fileName in files)
{
var file = BaseDirectory + Path.DirectorySeparatorChar + fileName;
var text = File.ReadAllText(file);
if (Exclude.Contains(Path.GetFileName(fileName), StringComparer.OrdinalIgnoreCase))
continue;

var text = File.ReadAllText(fileName);

MatchCollection matches = regex.Matches(text);

foreach (Match match in matches)
{
definitions.Add(new ServiceDefinition
{
Name = match.Groups[1].Value,
Text = match.Value,
NameIndex = match.Groups[1].Index - match.Index
});
(
match.Groups[1].Value,
match.Value,
match.Groups[1].Index - match.Index
));
}
}

Expand All @@ -108,10 +86,10 @@ public static void Execute()

foreach (var d in _defs)
{
Console.WriteLine("System service: " + d.Name);
Console.WriteLine($"System service: {d.Name}");

// Write the original definition, replacing "Nt" with "Zw".
sw.Write(string.Concat(d.Text.AsSpan(0, d.NameIndex), "Zw", d.Text.AsSpan(d.NameIndex + 2), "\r\n\r\n"));
sw.Write($"{d.Text.AsSpan(0, d.NameIndex)}Zw{d.Text.AsSpan(d.NameIndex + 2)}\r\n\r\n");
}

// Footer
Expand All @@ -123,15 +101,22 @@ public static void Execute()

public static partial class RegexDefinition
{
[GeneratedRegex(@"NTSYSCALLAPI[\w\s_]*NTAPI\s*(Nt(\w)*)\(.*?\);", RegexOptions.Singleline)]
[GeneratedRegex(@"NTSYSCALLAPI[\w\s_]*NTAPI\s*(Nt(\w)*)\(.*?\);", RegexOptions.Singleline | RegexOptions.Compiled)]
public static partial Regex Regex();
}

public class ServiceDefinition : IEquatable<ServiceDefinition>
{
public string Name;
public string Text;
public int NameIndex;
public readonly string Name;
public readonly string Text;
public readonly int NameIndex;

public ServiceDefinition(string Name, string Text, int NameIndex)
{
this.Name = Name;
this.Text = Text;
this.NameIndex = NameIndex;
}

public override string ToString()
{
Expand All @@ -145,9 +130,6 @@ public override int GetHashCode()

public override bool Equals(object obj)
{
if (obj == null)
return false;

if (obj is not ServiceDefinition service)
return false;

Expand All @@ -156,7 +138,7 @@ public override bool Equals(object obj)

public bool Equals(ServiceDefinition other)
{
return this.Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase);
return other != null && this.Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase);
}
}

Expand Down

0 comments on commit cec7798

Please sign in to comment.