* ObjectWriter.cs, ObjectReader.cs: Added suport for headers.
[mono.git] / mcs / class / corlib / System.Runtime.Serialization.Formatters.Binary / BinaryCommon.cs
1 // BinaryCommon.cs
2 //
3 // Author:
4 //   Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // (C) 2003 Lluis Sanchez Gual
7 \r
8 using System;\r
9 \r
10 namespace System.Runtime.Serialization.Formatters.Binary\r
11 {\r
12         internal class BinaryCommon\r
13         {\r
14                 // Header present in all binary serializations\r
15                 public static byte[] BinaryHeader = new Byte[] {0,1,0,0,0,255,255,255,255,1,0,0,0,0,0,0,0};\r
16 \r
17                 static Type[] _typeCodesToType;\r
18                 static byte[] _typeCodeMap;\r
19 \r
20                 static BinaryCommon()\r
21                 {\r
22                         _typeCodesToType = new Type [19];\r
23                         _typeCodesToType[(int)BinaryTypeCode.Boolean] = typeof (Boolean);\r
24                         _typeCodesToType[(int)BinaryTypeCode.Byte] = typeof (Byte);\r
25                         _typeCodesToType[(int)BinaryTypeCode.Char] = typeof (Char);\r
26                         _typeCodesToType[(int)BinaryTypeCode.DateTime] = typeof (DateTime);\r
27                         _typeCodesToType[(int)BinaryTypeCode.Decimal] = typeof (Decimal);\r
28                         _typeCodesToType[(int)BinaryTypeCode.Double] = typeof (Double);\r
29                         _typeCodesToType[(int)BinaryTypeCode.Int16] = typeof (Int16);\r
30                         _typeCodesToType[(int)BinaryTypeCode.Int32] = typeof (Int32);\r
31                         _typeCodesToType[(int)BinaryTypeCode.Int64] = typeof (Int64);\r
32                         _typeCodesToType[(int)BinaryTypeCode.SByte] = typeof (SByte);\r
33                         _typeCodesToType[(int)BinaryTypeCode.Single] = typeof (Single);\r
34                         _typeCodesToType[(int)BinaryTypeCode.UInt16] = typeof (UInt16);\r
35                         _typeCodesToType[(int)BinaryTypeCode.UInt32] = typeof (UInt32);\r
36                         _typeCodesToType[(int)BinaryTypeCode.UInt64] = typeof (UInt64);\r
37                         _typeCodesToType[(int)BinaryTypeCode.Null] = null;\r
38                         _typeCodesToType[(int)BinaryTypeCode.String] = typeof (string);\r
39 \r
40                         _typeCodeMap = new byte[30];\r
41                         _typeCodeMap[(int)TypeCode.Boolean] = (byte) BinaryTypeCode.Boolean;\r
42                         _typeCodeMap[(int)TypeCode.Byte] = (byte) BinaryTypeCode.Byte;\r
43                         _typeCodeMap[(int)TypeCode.Char] = (byte) BinaryTypeCode.Char;\r
44                         _typeCodeMap[(int)TypeCode.DateTime] = (byte) BinaryTypeCode.DateTime;\r
45                         _typeCodeMap[(int)TypeCode.Decimal] = (byte) BinaryTypeCode.Decimal;\r
46                         _typeCodeMap[(int)TypeCode.Double] = (byte) BinaryTypeCode.Double;\r
47                         _typeCodeMap[(int)TypeCode.Int16] = (byte) BinaryTypeCode.Int16;\r
48                         _typeCodeMap[(int)TypeCode.Int32] = (byte) BinaryTypeCode.Int32;\r
49                         _typeCodeMap[(int)TypeCode.Int64] = (byte) BinaryTypeCode.Int64;\r
50                         _typeCodeMap[(int)TypeCode.SByte] = (byte) BinaryTypeCode.SByte;\r
51                         _typeCodeMap[(int)TypeCode.Single] = (byte) BinaryTypeCode.Single;\r
52                         _typeCodeMap[(int)TypeCode.UInt16] = (byte) BinaryTypeCode.UInt16;\r
53                         _typeCodeMap[(int)TypeCode.UInt32] = (byte) BinaryTypeCode.UInt32;\r
54                         _typeCodeMap[(int)TypeCode.UInt64] = (byte) BinaryTypeCode.UInt64;\r
55                         _typeCodeMap[(int)TypeCode.String] = (byte) BinaryTypeCode.String;\r
56                 }\r
57 \r
58                 public static bool IsPrimitive (Type type)\r
59                 {\r
60                         return type.IsPrimitive || type == typeof (DateTime) || type == typeof (Decimal);\r
61                 }\r
62 \r
63                 public static byte GetTypeCode (Type type)\r
64                 {\r
65                         return _typeCodeMap [(int)Type.GetTypeCode(type)];\r
66                 }\r
67 \r
68                 public static Type GetTypeFromCode (int code)\r
69                 {\r
70                         return _typeCodesToType [code];\r
71                 }\r
72         }\r
73 \r
74         internal enum BinaryElement : byte\r
75         {
76                 Header = 0,
77                 RefTypeObject = 1,
78                 _Unknown1 = 2,
79                 _Unknown2 = 3,
80                 RuntimeObject = 4,
81                 ExternalObject = 5,
82                 String = 6,
83                 GenericArray = 7,
84                 BoxedPrimitiveTypeValue = 8,
85                 ObjectReference = 9,
86                 NullValue = 10,
87                 End = 11,
88                 Assembly = 12,
89                 ArrayFiller8b = 13,
90                 ArrayFiller32b = 14,
91                 ArrayOfPrimitiveType = 15,
92                 ArrayOfObject = 16,
93                 ArrayOfString = 17,
94                 Method = 18,
95                 _Unknown4 = 19,
96                 _Unknown5 = 20,
97                 MethodCall = 21,
98                 MethodResponse = 22
99         }
100
101         internal enum TypeTag : byte
102         {
103                 PrimitiveType = 0,
104                 String = 1,
105                 ObjectType = 2,
106                 RuntimeType = 3,
107                 GenericType = 4,
108                 ArrayOfObject = 5,
109                 ArrayOfString = 6,
110                 ArrayOfPrimitiveType = 7
111         }
112
113         internal enum ArrayStructure : byte
114         {
115                 SingleDimensional = 0,
116                 Jagged = 1,
117                 MultiDimensional = 2
118         }\r
119 \r
120         internal enum MethodFlags : byte
121         {
122                 NoArguments = 1,
123                 PrimitiveArguments = 2,
124                 ArgumentsInSimpleArray = 4,
125                 ArgumentsInMultiArray = 8,
126                 ExcludeLogicalCallContext = 16,
127                 IncludesLogicalCallContext = 64,
128                 IncludesSignature = 128,
129
130                 FormatMask = 15,
131                 NeedsInfoArrayMask = 4 + 8 + 64 + 128
132         }\r
133 \r
134         internal enum ReturnTypeTag : byte\r
135         {\r
136                 Null = 2,\r
137                 PrimitiveType = 8,\r
138                 ObjectType = 16,\r
139                 Exception = 32\r
140         }\r
141 \r
142         enum BinaryTypeCode : byte\r
143         {\r
144                 Boolean = 1,\r
145                 Byte = 2,\r
146                 Char = 3,\r
147                 Decimal = 5,\r
148                 Double = 6,\r
149                 Int16 = 7,\r
150                 Int32 = 8,\r
151                 Int64 = 9,\r
152                 SByte = 10,\r
153                 Single = 11,\r
154                 DateTime = 13,\r
155                 UInt16 = 14,\r
156                 UInt32 = 15,\r
157                 UInt64 = 16,\r
158                 Null = 17,\r
159                 String = 18\r
160         }\r
161 \r
162 }\r