0

I'm writing a program that is loading and extracting files from an archive. Currently with my code it is successfully extracting one of the archives I need, and with the problem archive, it reads and extracts 6 files before giving an exception.

The exception is because it is reading the wrong integer, instead it is reading an integer 12 bytes before the integer it is supposed to read.

Here is my code:

if (versionNumber.Equals("AULB"))
{
    fileCount = binReader1.ReadInt32();

    for (int i = 0; i < fileCount; i++)
    {
        int size = binReader1.ReadInt32();
        String midValues = "" + binReader1.ReadInt64();
        int nameLength = binReader1.ReadInt32();

        String name = 
           System.Text.Encoding.Default.GetString(binReader1.ReadBytes(nameLength));

        byte[] blueprint = binReader1.ReadBytes((size) - 12 - nameLength);

        //Export blueprint for testing
        MemoryStream blueprintStream = new MemoryStream(blueprint);
        FileStream fileStream1 = new FileStream(
          "C:\\Users\\Dan\\Desktop\\blueprints\\" + i + ".blueprint", 
          FileMode.Create, FileAccess.Write);
        blueprintStream.WriteTo(fileStream1);
    }

As a quick explanation, it is reading the 4 bytes to get the size of the file, 8 bytes of the header that is unused (but I have set to a local for potential validity checks because this value should always be the same), then 4 bytes to get the length of the string name, then reads that many bytes to get the name.

After that, it grabs the remaining data of the file, so I subtract the length of the name, string length, and the extra 8 bytes because it has already read them.

To illustrate what I'm trying to load, here is the binary data. I have highlighted in light blue the file size, in light green is the extra 8 bytes, in bright green is the string length, then in pink is the string.

I have also highlighted in read the byte that my program is trying to read thinking it is the file size.

I'm not quite sure what could cause this problem, as it reads the previous 6 files and another small archive with no issues.

The file I'm loading.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Dan McCarthy
  • 23
  • 1
  • 5
  • I'd recommend reading/finding file format specification. There could be padding bytes or some other extra data in some/all files. – Alexei Levenkov Dec 29 '15 at 06:50
  • This is a proprietary format, any format specification would be private, if it still exists. The 12 bytes that aren't being read are part of the data. The 08 00 00 00 signifies 8 bytes to read, and the remaining 8 are the data. I really appreciate the suggestion, though, it was a good idea. – Dan McCarthy Dec 29 '15 at 06:57

1 Answers1

0

Oddly, after a string of tests that gave me very odd values that did not make sense to the file it was reading, I attempted to force the program to read 12 extra bytes when it reached the 6th file, it is now reading the bytes correctly.

This is very strange since I tried reading the bytes ahead and they values it gave me back did not exist in the file.

Either way, this was the code that fixed it for me. It was a very simple workaround.

if (i != 6)
{
    blueprint = binReader1.ReadBytes((size) - 12 - nameLength);
} else {
    blueprint = binReader1.ReadBytes((size) - 12 - nameLength + 12);
}
Dan McCarthy
  • 23
  • 1
  • 5