Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
I'm trying to read a binary serialized object, that I don't have the object definition too. I took a peak into the file and saw property names, so I manually recreated the object (let's call it SomeDataFormat).

I ended up with this :

public class SomeDataFormat // 16 field
{
    public string Name{ get; set; }
    public int Country{ get; set; }
    public string UserEmail{ get; set; }
    public bool IsCaptchaDisplayed{ get; set; }
    public bool IsForgotPasswordCaptchaDisplayed{ get; set; }
    public bool IsSaveChecked{ get; set; }
    public string SessionId{ get; set; }
    public int SelectedLanguage{ get; set; }
    public int SelectedUiCulture{ get; set; }
    public int SecurityImageRefId{ get; set; }
    public int LogOnId{ get; set; }
    public bool BetaLogOn{ get; set; }
    public int Amount{ get; set; }
    public int CurrencyTo{ get; set; }
    public int Delivery{ get; set; }
    public bool displaySSN{ get; set; }
}

Now I'm able to deserialize it like this :

BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Full; // original uses this
formatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded; // this reduces size
FileStream readStream = new FileStream("data.dat", FileMode.Open);
SomeDataFormat data = (SomeDataFormat) formatter.Deserialize(readStream);


First suspicious thing is that only the 2 string (SessionId&UserEmail) has value in the deserialized *data* object. The other properties are null or just 0. This might be intended, but still, I suspect that something has gone wrone during the deserialization.

The second suspicious thing is if I reserialize this object, I end up with different file sizes. Original (695bytes). Reserialized object is 698bytes. So there is 3bytes difference. I should get the same file size as the original.

Taking a look at the original, and the new (reserialized) file:
The originally serialized file.
The reserialized file.

As you can see, after the header section, the data appears to be in different order. For example, you can see that the email, and the sessionID is not at the same place.

Q1: Why are the values are in different order in the two files?
Q2: Why is there extra 3 bytes compared the 2 serialized objects?
Q3: What am I missing? How could I do this?


Any tipps/help appreaciated.
Posted
Updated 8-Aug-13 21:40pm
v2
Comments
Sergey Alexandrovich Kryukov 8-Aug-13 16:32pm    
What exactly do you mean by "object definition"? How such object could be created? :-)
—SA
Antal Dominik 9-Aug-13 3:39am    
I mean the .cs file with the class in there. The original object that got serialized, I don't have the class for it.
Sergey Alexandrovich Kryukov 9-Aug-13 10:28am    
It cannot be so. If you have an instance of a class, you always have a class.
—SA
Antal Dominik 21-Aug-13 5:40am    
I don't know what are we talking about, I might use the wrong terminilogy here.
I mean the "class some DataFormat" class. I did not have it, but I manually recreated it from the binary serialized object, because it contained the field names.
Sergey Alexandrovich Kryukov 21-Aug-13 9:45am    
You don't understand. How can I explain it to you. In .NET, there are no objects without types, no matter how you get them.
—SA

1 solution

Binary serialization is mostly suitable for use by an application internally and not really useful for data interchange. The obvious reason is that the format can be very specific between different framework versions. A better alternative would be to use xml or json formatters because these formats or more loose. The main difference is that binary format uses predefined lengths for data. When this length changes it could become incompatible with previous versions.

Good luck!
 
Share this answer
 
Comments
Antal Dominik 9-Aug-13 7:28am    
Yeah, the only thing Is that this object is not generated by me, but by another program, that I do not own. But I want to know what's in there. I'm pretty sure it's possible, I just have to figure it out how, I'm close.
E.F. Nijboer 9-Aug-13 13:22pm    
If you do have the application that serialized the object you might be able to check it out using something like reflector. Maybe the links below are also of interest to you.

http://www.diranieh.com/NETSerialization/BinarySerialization.htm
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.surrogateselector%28v=VS.100%29.aspx
Antal Dominik 21-Aug-13 5:41am    
Thank you for your help!
I solved this question on stackoverflow btw :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900