Question

all

I am very new to handle with binary data.

I am looking into some codes from MSDN.

Server side:

        string fileName = "AppSettings.dat";

        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write(1.250F);
            writer.Write(@"c:\Temp");
            writer.Write(10);
            writer.Write(true);
        }

And I received the binary file from server.

        string clientFile = "AppSettings.dat";

        using (BinaryReader reader = new BinaryReader(File.Open(clientFile, FileMode.Open)))
        {
            float aspectRatio = reader.ReadSingle();
            string tempDirectory = reader.ReadString();
            int autoSaveTime = reader.ReadInt32();
            bool showStatusBar = reader.ReadBoolean();
        }

It seems pretty easy to read and write when you use BinaryWriter and BinaryReader class.

But, here is my problem.

The server developer left the company, and somehow nobody is able to contact to him.

I cannot figure out how he wrote to BinaryWriter.

I mean the exact order.

In the example, I have to read reader in order as

ReadSingle

ReadString

ReadInt32

ReadBoolean

Then I can get the correct result.

But in my situation, I don't know the exact order.

So, what I can try is how to read all the characters and numbers at once.

Like......

string contents = reader.someMethod;

Then contents is like below:

1.250F
c:\Temp
10
true

The carriage return is skippable.

EDIT

I have tried File.ReadAllBytes.

But the result is not what I expected to be...

        byte[] bytes    = System.IO.File.ReadAllBytes(clientFile);
        string contents = Encoding.UTF8.GetString(bytes);

\0\0�?\ac:\Temp\n\0\0\0

Was it helpful?

Solution

The code below writes a binary file, then reads it into a byte array and outputs the hexadecimal values.

using (var writer = new BinaryWriter(File.Create("foo.bin")))
{
    writer.Write(1.250F);
    writer.Write(@"c:\Temp");
    writer.Write(10);
    writer.Write(true);
}

var bytes = File.ReadAllBytes("foo.bin");
var text = BitConverter.ToString(bytes);
Console.WriteLine(text);

Here's the output:

00-00-A0-3F-07-63-3A-5C-54-65-6D-70-0A-00-00-00-01

Decoding, we have:

0000A03f - 4 bytes = 1.250F

Next are 8 bytes that correspond to the string "c:\Temp". The first byte, 07, is the length. BinaryWriter uses a 7-bit encoding scheme for the length, so strings shorter than 128 bytes only require a single length byte. The 7 characters that make up the string follow the length byte.

Following that you have the four bytes 0A000000, which is the integer 10.

Finally, you have the value 1, which is the boolean True.

You should use BitConverter.ToString(bytes) to output the data from your file. Then you can examine the bytes directly.

If you have some idea of what's in the file, you can usually puzzle it out. Strings are easy to pick out, and you can often use them as anchors to figure out what's on either side of them. This is especially true if you have knowledge of what's in the file and you're just trying to figure out the order in which things were written.

You don't really need a decompiler to figure out the order he wrote things in. You can use ildasm.exe to examine the generated MSIL. The function calls to BinaryWriter.Write will be easy to spot, and the disassembled code will tell you whether it's writing a string, Boolean, integer, etc. For a one-time thing, that's the way I'd go. If I had a larger job where I needed to understand a lot of code, I'd go with a better decompiler.

OTHER TIPS

I worry that without knowing the correct order; then you're going to have a very hard time working this out.

What I would suggest is using a .net decompiler to read his code. I'm making the assumption that the code is owned by your company, so this probably isn't wrong to do so.

RedGates reflector and Resharper both have facilities to decompile .net assemblies into C#.

There's also a free one called 'dotPeek' from Telerik which I've been informed is quite good but I've not used it myself.

Essentially you just run the program and give it the assembly and you can browse the source code, as long as it's not been Obfuscated; but even then - there are some operations that can't be obfuscated completely, such as the binaryWriter.Write as they have to happen in a specific order.

If this isn't what you need, please try and clarify your question!

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top