Analysis of binary sequence of .NET

xiaoxiao2021-03-06  74

Author: zfive5

Email: zfive5@yahoo.com.cn

The serialization code under the corresponding C # is shown below, and the program puts the serialized data into a specified file file.bin, and analyzes the file data mainly after reading the application under non-.net. Data, which helps .NET interact with other language platforms.

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Namespace WindowsApplication2

{

[Serializable]

Public Class Object5

{

Public I1 = 0;

Public I2 = 0;

Public Float F3 = 0;

Public String Str;

}

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.Forms.Button button1;

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

InitializationComponent ();

}

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

Private vidinitiRizeComponent ()

{

This.button1 = new system.windows.Forms.Button ();

THIS.SUSPENDLAYOUT ();

This.button1.location = new system.drawing.point (72, 72);

This.button1.name = "button1";

This.button1.size = new system.drawing.size (128, 32);

this.button1.tabindex = 0;

This.Button1.Text = "Button1";

This.Button1.click = new system.eventhandler (this.button1_click);

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

THIS.CLIENTSIZE = New System.drawing.size (292, 273);

This.Controls.add (this.button1);

THIS.NAME = "Form1";

THIS.TEXT = "Form1";

This.ResumeLayout (false);

}

#ndregion

[Stathread]

Static void main ()

{

Application.run (New Form1 ());

}

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Object5 obj = new object5 ();

Obj.i1 = 128; OBJ.I2 = 24;

Obj.f3 = 1.3F;

Obj.str = "some string";

Double D1 = 1.3D;

Float F1 = 1.3F;

INT I1 = 1;

String S1 = "HelloWorld";

System.Runtime.Serialization.iformatter formatter = new system.runtime.serialization.formatters.binary.binaryformatter ();

System.io.Stream Stream = new system.io.filestream ("file.bin", system.io.filemode.create, system.io.fileAccess.write, system.io.fileshare.none;

Formatter.Serialize (Stream, Obj);

Formatter.Serialize (stream, d1);

Formatter.Serialize (stream, f1);

Formatter.Serialize (stream, i1);

Formatter.Serialize (stream, s1);

stream.close ();

Formatter = null;

}

}

}

The contents of the file are shown below:

annotation:

1) 00 01 00 00 00 FF FF FF 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

2) 0C 02 The representation of the object description information is serialized

3) 00 00 00 51 (81) length, (note is a large number of 4-bit integers), indicating the length of the subsequent object description information

4) Customize the description of the object, can be ignored during analysis, with a length of 81 characters (previously explained)

5) 05 01 Customize the representation of the object.

6) 00 00 00 1B (27) Customize the length of the object class description information

7) Customize the description of the object, the main class symbol representation ("WindowsApplication2.Object5")

8) Customize member entries in the object, such as four items in the objects defined above, Int, int, flaot, and string, respectively

9) 02 Int i1 member 2

10) Definition of the corresponding member name "I1"

11)

12) Top 10 items

13)

14) Top 10 items

15)

16) Top 10

17) Description Custom Objects The definition items are values ​​objects or other types, a total of 4 bytes, 00 is values ​​object

01 For the object object, the object defined above is the INT I1 INT I2 FLOAT F1 STRING STR to 00 00 00 01.

18) Description 17) The type of corresponding field is INT 08 INT 08 INT 0B, respectively.

19) 02 00 00 00 fixed, explain the beginning of other data

20) The value of 80 00 00 00 (128, small end format) corresponding to OBJ.I1

21) The value of the corresponding obj.i2 18 00 00 (24, small end format)

22) Value 66 66 66 A6 3F (1.3 floating point format) 23) 06 03 Object String marker, indicating that it is a String object

24, 25) 00 00 00 0b String Object Length 11 (Large end), value "Some String"

26) 0B ​​represents the end of an object sequence.

27) same 1)

28) 04 01 Double type representation symbol

29) 00 00 00 0d (13) Type length (large end)

30) Object definition marker ("system.double")

31) 01 00 00 00 (1) Contains a member

32) 07 (7) Define the length of the signature of the member

33) Members' definition symbol "m_value"

34) 00 Description is the value type 06 Description is a Double type

35) CD CC CC CC CC CC F4 3f Double Type Corresponding Numerical

36) 0B ​​End of a sequence object

Other annotation analysis is not repeated here, the principle is the same!

The type of test obtained and the partial correspondence of the representation, as shown below

BOOL 01

Byte 02

Uint 0f

Char 03

Ulong 10

Ushort 0e

Decimal 05

INT 08

Sbyte 0A

Short 07

Double 06

Float 0B

Long 09

String 06 03/06 04

Value type 00

String 01

Object 02

[] 07

Struct 04

Here is just a part! Mainly with the complexity of the object, there will be no analysis of the marker, if you find new, please send me an email, thank you very much!

At this time, everyone can also experience why XML serialization has existed. Different systems do much easier, but XML has a disadvantage that the data is large. This is not possible with binary serialization!

The XML serialization code is as follows:

System.xml.Serialization.xmlSerializer formatter = new system.xml.serialization.xmlserializer (Obj.gettype ());

The XML files after the object Object5 serialized are as follows:

128

24

1.3

Some string

Simple!

转载请注明原文地址:https://www.9cbs.com/read-87444.html

New Post(0)