PdbFind/PdbFind.Lib/Locator.cs

48 lines
1.6 KiB
C#
Raw Normal View History

2022-09-16 12:32:25 +02:00
using PeNet;
namespace PdbFind.Lib
{
public class Locator
{
public static (string? pdbName, string? checksum) GetPdbChecksum(string path)
{
if (!File.Exists(path))
{
throw new FileNotFoundException("PE File not found", path);
}
var peFile = new PeFile(path);
var pdb70 = peFile.ImageDebugDirectory?.Where(d => d.CvInfoPdb70 != null).Select(d => d.CvInfoPdb70).FirstOrDefault();
var pdbName = (pdb70?.PdbFileName != null) ? Path.GetFileName(pdb70.PdbFileName) : null;
var checksum = pdb70?.Signature != null ? $"{pdb70.Signature:N}{pdb70.Age}" : null;
return (pdbName, checksum);
}
public static string LocatePdbInStore(string path, string store)
{
if (!Directory.Exists(store))
{
throw new DirectoryNotFoundException($"Directory \"{store}\" not found.");
}
var (pdbName, checksum) = GetPdbChecksum(path);
_ = pdbName ?? throw new InvalidOperationException("PDB name not found in PE header.");
_ = checksum ?? throw new InvalidOperationException("Checksum not found in PE header.");
var isIndex2 = File.Exists(Path.Combine(store, "index2.txt"));
var pdbPath = Path.Combine(pdbName, checksum, pdbName);
if (isIndex2)
{
pdbPath = Path.Combine(pdbName.Substring(0, 2), pdbName, checksum, pdbName);
}
return Path.Combine(store, pdbPath);
}
}
}