簡體   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