繁体   English   中英

如何可靠地正则表达式Base64String

[英]How to reliably regex a Base64String

因此,基本上,我正在写一个在二进制文件中查找PNG文件的应用程序。 它通过将文件中的整个二进制文件读入字节数组,然后使用Convert.ToBase64String方法将其转换为字符串,然后使用与PNG的标头信息和端块匹配的正则表达式来查找图像,来完成此操作。 问题在于,使用ToBase64String方法会根据字节数组的长度生成截然不同的输出,并且MSDN上的文档似乎并未对此进行详细说明。 无论如何,这是我的意思的一个例子。

 byte[] somebytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };

 Console.WriteLine(Convert.ToBase64String(somebytes));

如果我跳过一个字节,在这种情况下的输出现在是“ AQIDBAUGBwg =“。

 byte[] somebytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };

 somebytes = somebytes.Skip(1).ToArray();

 Console.WriteLine(Convert.ToBase64String(somebytes));

现在的输出为“ AgMEBQYHCA ==”,因此几乎每个字符都与上一个示例有所不同。

因此,我是否无可救药地沿着错误的路径重新分配二进制文件,或者是否有方法(也许通过填充?)可以保证这些转换的一致性?

更新:根据我收集到的反馈,看来我应该离开Regex解决方案,自己手动搜索开始和结束字节序列。 不知道为什么我被低估了,因为我只是想了解为什么我的其他解决方案行得通,并且似乎没有关于该主题的其他文章。 无论如何,感谢大家的快速反馈。 完成后,我将发布用于查找图片的算法,以防其他人受益。

您在注释中确认您正在尝试从C#结构化文件(EXE或DLL)中提取资源。 您可以使用反射方法将其拉出:GetManifestResourceStream,GetManifestResourceNames,GetManifestResourceInfo是一个很好的起点。

正如我所承诺的那样,我编写了在二进制文件中查找图像的逻辑,以防它可能对其他人有所帮助。 但是,我最终可能会使用SledgeHammers方法,但对我来说,也能够使用此方法处理它也很重要。

public class BinarySearch
{
    public static IEnumerable<byte[]> Match(byte[] source, byte[] beginningSequence, byte[] endSequence)
    {
        int index = 0;

        IList<byte[]> matches = new List<byte[]>();

        while (index < source.Length)
        {
            var startIndex = FindSequence(source, beginningSequence, index);

            if (startIndex >= 0)
            {
                var endIndex = FindSequence(source, endSequence, startIndex + beginningSequence.Length);

                if (endIndex >= 0)
                {
                    var length = (endIndex - startIndex) + endSequence.Length;
                    var buffer = new byte[length];

                    Array.Copy(source, startIndex, buffer, 0, length);

                    matches.Add(buffer);

                    index = endIndex + endSequence.Length;
                }
                else
                {
                    index = source.Length;
                }
            }
            else
            {
                index = source.Length;
            }
        }

        return matches;
    }

    private static int FindSequence(byte[] bytes, byte[] sequence, int startIndex = 0)
    {
        int currentIndex = startIndex;
        int sequenceIndex = 0;
        bool found = false;

        while (!found && currentIndex < bytes.Length)
        {
            if (bytes[currentIndex] == sequence[sequenceIndex])
            {
                if (sequenceIndex == (sequence.Length - 1))
                {
                    found = true;
                }
                else
                {
                    sequenceIndex++;
                }
            }
            else
            {
                currentIndex -= sequenceIndex;
                sequenceIndex = 0;
            }

            currentIndex++;
        }

        return found ? (currentIndex - sequence.Length) : -1;
    }
}

这是PNG文件用法的一个示例。

var imageHeaderStart = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00 };

var imageEOF = new byte[] { 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 };

var matches = BinarySearch.Match(binaryData, imageHeaderStart, imageEOF);

如果有人对我的“完成”实现感兴趣,我将在Github项目完成后添加一个链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM