internal static class DirectoryParserHelpers
{
    public static int? TryMap(uint rva, NtHeaders nt, IReadOnlyList<SectionInfo> sections)
    {
        ArgumentNullException.ThrowIfNull(nt);
        ArgumentNullException.ThrowIfNull(sections);

        return RvaMapper.RvaToFileOffset(rva, sections, nt.OptionalHeader.SizeOfHeaders);
    }

    public static string ReadAsciiAtRva(
        byte[] rawBytes,
        uint rva,
        NtHeaders nt,
        IReadOnlyList<SectionInfo> sections,
        int max = 1024)
    {
        ArgumentNullException.ThrowIfNull(rawBytes);
        ArgumentNullException.ThrowIfNull(nt);
        ArgumentNullException.ThrowIfNull(sections);

        if (max <= 0)
        {
            return string.Empty;
        }

        var offset = TryMap(rva, nt, sections);
        if (offset is null)
        {
            return string.Empty;
        }

        return BinaryReaderExtensions.ReadAsciiStringAt(rawBytes, offset.Value, max);
    }
}