-3

I try to be more specific.

I have a binary file which has some portions of text inside. I want to search for some byte sequence in the binary file, if the sequences are found take the byte arrays and build a text file with them.

So the step has to be repeated till the end of the binary file. I used BinaryReader to search for a byte sequence, in order to validate the binary file, but now I am stuck on how to proceed with this combination.

My other issue is that I have to skip certain portions of the binary file until the next sequence is found.

So for example, I find the first sequence at 0x10 and it lasts for 10 bytes. Then I have to skip 32 bytes where another byte sequence then starts for x bytes till a tail byte that marks the end of the sequence.

Each time a byte sequence is found I have to save it in a text file, finally writing it to disk.

Any help?

2 Answers2

1

Something like this, then:

class Program
{
    const string filename = "some file";

    static void Main(string[] args)
    {
        byte[] bytes = System.IO.File.ReadAllBytes(filename);

        string[] find = new string[] { "me", "you" };

        int offsetAfterFind = 32;

        int pos = 0;

        while (pos < bytes.Length)
        {
            bool isFound = false;
            int index = 0;
            while (!isFound && index < find.Length)
            {
                bool isMatch = true;
                for (int n = 0; n < find[index].Length; n++)
                {
                    if (pos + n >= bytes.Length)
                    {
                        isMatch = false;
                    }
                    else
                    {
                        if (bytes[pos + n] != find[index][n]) isMatch = false;
                    }
                }
                if (isMatch)
                {
                    isFound = true;
                    break;
                }
                index++;
            }
            if (isFound)
            {
                Console.WriteLine(String.Format("Found {0} at {1}", find[index], pos));
                pos += find[index].Length + offsetAfterFind;
            }
            else
            {
                pos++;
            }
        }

    }
}
Dustin_00
  • 345
  • 2
  • 8
1

All right. I managed to do it and maybe this will be useful to someone else:

public static void ConvertToSRTSubs()
    {
        byte [] openingTimeWindow = Encoding.ASCII.GetBytes("["); \\Timespan in the binary is wrapped around square brackets
        byte [] nextOpening = Encoding.ASCII.GetBytes("[00"); \\ I need this as a point to get the end of the sentence, because there is a fixed size between sentences and next timespan.
        byte [] closingTimeWindow = Encoding.ASCII.GetBytes("]"); \\End of the timespan
        int found = 0;  \\This will iterate through every timespan match
        int backPos = 0; \\Pointer to the first occurrence
        int nextPos = 0;
        int sentenceStartPos = 0;
        int newStartFound = 0;
        string srtTime = String.Empty;
        string srtSentence = String.Empty;

        byte[] array = File.ReadAllBytes(Path.Combine(coursePath, hashedSubFileName));
        try
        {
            using (StreamWriter s = new StreamWriter(Video.outPath + ext, false))
            {
                for (int i = 0; i < array.Length; i++)
                {
                    if (openingTimeWindow[0] == array[i] && closingTimeWindow[0] == array[i + 12])
                    {
                        found++;
                        s.WriteLine(found);                            
                        try
                        {
                            backPos = i;
                            for (i = backPos + 12; i < array.Length; i++ )
                                {
                                    if (newStartFound == 1) 
                                        break;
                                    if (nextOpening[0] == array[i] && nextOpening[1] == array[i + 1] && nextOpening[2] == array[i + 2])
                                    {
                                        nextPos = i - 19;
                                        newStartFound++;
                                    }
                                }
                            i = backPos;
                            newStartFound = 0;
                            sentenceStartPos = backPos + 27;
                            sentenceSize = nextPos - sentenceStartPos;
                            if (sentenceSize < 0) sentenceSize = 1;
                            byte[] startTime = new byte[11];
                            byte[] sentence = new byte[sentenceSize];
                            Array.Copy(array, backPos + 1, startTime, 0, 11);
                            Array.Copy(array, sentenceStartPos, sentence, 0, sentenceSize);
                            srtTimeRaw = srtTime = Encoding.UTF8.GetString(startTime);
                            srtTime = srtTimeRaw.Replace('.', ',') + "0" + " --> " + span;
                            s.WriteLine(srtTime);
                            srtSentence = Encoding.UTF8.GetString(sentence);
                            s.WriteLine(srtSentence);
                            s.WriteLine();
                        }
                        catch (ArgumentException argex)
                        {
                            MessageBox.Show(argex.ToString());
                        }

                    }
                }
            }
        }
        catch (DirectoryNotFoundException dex)
        {
            MessageBox.Show(dex.ToString());
        }
    }

Maybe not the cleanest code, but it works :)