Files
Chronos/.gitea/probe/pe.cs
T
Alby96andClaude Opus 5.5 3f48f7612b
probe.yml (push) Invalid workflow file
ci: temporary probe of the Gitea runner
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 09:49:03 +02:00

52 lines
2.3 KiB
C#

// Lists the Win32 resources of PE files: icon group, version and manifest must be present in a published apphost.
using System.Reflection.PortableExecutable;
using System.Text;
var failures = 0;
foreach (var path in args) failures += Check(path);
return failures;
static int Check(string path)
{
using var fs = File.OpenRead(path);
using var pe = new PEReader(fs);
var header = pe.PEHeaders.PEHeader!;
var dir = header.ResourceTableDirectory;
Console.WriteLine($"{Path.GetFileName(path)}: {fs.Length:N0} bytes, subsystem {header.Subsystem}, resources {dir.Size:N0} bytes");
if (dir.Size == 0) { Console.WriteLine(" MISSING: no resource section"); return 1; }
var root = pe.GetSectionData(dir.RelativeVirtualAddress);
var types = new List<int>();
foreach (var (id, offset) in Entries(root, 0))
{
types.Add(id);
var label = id switch { 3 => "ICON", 14 => "GROUP_ICON", 16 => "VERSION", 24 => "MANIFEST", _ => "type " + id };
var count = 0;
foreach (var (_, nameOffset) in Entries(root, offset & 0x7FFFFFFF))
foreach (var (_, dataOffset) in Entries(root, nameOffset & 0x7FFFFFFF))
{
count++;
if (id != 24) continue;
var entry = root.GetReader();
entry.Offset = (int)dataOffset;
int rva = entry.ReadInt32(), size = entry.ReadInt32();
var block = pe.GetSectionData(rva).GetReader();
var text = Encoding.UTF8.GetString(block.ReadBytes(Math.Min(size, block.Length)));
Console.WriteLine($" manifest {size} bytes, dpiAwareness: {text.Contains("PerMonitorV2", StringComparison.Ordinal)}");
}
Console.WriteLine($" {label}: {count}");
}
var ok = types.Contains(14) && types.Contains(16) && types.Contains(24);
Console.WriteLine(ok ? " OK: icon, version and manifest present" : " MISSING: icon, version or manifest");
return ok ? 0 : 1;
}
static List<(int Id, uint Offset)> Entries(PEMemoryBlock root, uint directoryOffset)
{
var r = root.GetReader();
r.Offset = (int)directoryOffset + 12;
var count = r.ReadUInt16() + r.ReadUInt16();
var list = new List<(int, uint)>();
for (var i = 0; i < count; i++) list.Add(((int)(r.ReadUInt32() & 0x7FFFFFFF), r.ReadUInt32()));
return list;
}