* ObjectReader.cs, ObjectWriter.cs, BinaryCommon.cs: Fixed bug #45970.
[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.TimeSpan] = typeof (TimeSpan);\r
27                         _typeCodesToType[(int)BinaryTypeCode.DateTime] = typeof (DateTime);\r
28                         _typeCodesToType[(int)BinaryTypeCode.Decimal] = typeof (Decimal);\r
29                         _typeCodesToType[(int)BinaryTypeCode.Double] = typeof (Double);\r
30                         _typeCodesToType[(int)BinaryTypeCode.Int16] = typeof (Int16);\r
31                         _typeCodesToType[(int)BinaryTypeCode.Int32] = typeof (Int32);\r
32                         _typeCodesToType[(int)BinaryTypeCode.Int64] = typeof (Int64);\r
33                         _typeCodesToType[(int)BinaryTypeCode.SByte] = typeof (SByte);\r
34                         _typeCodesToType[(int)BinaryTypeCode.Single] = typeof (Single);\r
35                         _typeCodesToType[(int)BinaryTypeCode.UInt16] = typeof (UInt16);\r
36                         _typeCodesToType[(int)BinaryTypeCode.UInt32] = typeof (UInt32);\r
37                         _typeCodesToType[(int)BinaryTypeCode.UInt64] = typeof (UInt64);\r
38                         _typeCodesToType[(int)BinaryTypeCode.Null] = null;\r
39                         _typeCodesToType[(int)BinaryTypeCode.String] = typeof (string);\r
40 \r
41                         _typeCodeMap = new byte[30];\r
42                         _typeCodeMap[(int)TypeCode.Boolean] = (byte) BinaryTypeCode.Boolean;\r
43                         _typeCodeMap[(int)TypeCode.Byte] = (byte) BinaryTypeCode.Byte;\r
44                         _typeCodeMap[(int)TypeCode.Char] = (byte) BinaryTypeCode.Char;\r
45                         _typeCodeMap[(int)TypeCode.DateTime] = (byte) BinaryTypeCode.DateTime;\r
46                         _typeCodeMap[(int)TypeCode.Decimal] = (byte) BinaryTypeCode.Decimal;\r
47                         _typeCodeMap[(int)TypeCode.Double] = (byte) BinaryTypeCode.Double;\r
48                         _typeCodeMap[(int)TypeCode.Int16] = (byte) BinaryTypeCode.Int16;\r
49                         _typeCodeMap[(int)TypeCode.Int32] = (byte) BinaryTypeCode.Int32;\r
50                         _typeCodeMap[(int)TypeCode.Int64] = (byte) BinaryTypeCode.Int64;\r
51                         _typeCodeMap[(int)TypeCode.SByte] = (byte) BinaryTypeCode.SByte;\r
52                         _typeCodeMap[(int)TypeCode.Single] = (byte) BinaryTypeCode.Single;\r
53                         _typeCodeMap[(int)TypeCode.UInt16] = (byte) BinaryTypeCode.UInt16;\r
54                         _typeCodeMap[(int)TypeCode.UInt32] = (byte) BinaryTypeCode.UInt32;\r
55                         _typeCodeMap[(int)TypeCode.UInt64] = (byte) BinaryTypeCode.UInt64;\r
56                         _typeCodeMap[(int)TypeCode.String] = (byte) BinaryTypeCode.String;\r
57 \r
58                         // TimeStamp does not have a TypeCode, so it is managed as a special\r
59                         // case in GetTypeCode()\r
60                 }\r
61 \r
62                 public static bool IsPrimitive (Type type)\r
63                 {\r
64                         return type.IsPrimitive || \r
65                                 type == typeof (DateTime) || \r
66                                 type == typeof (TimeSpan) || \r
67                                 type == typeof (Decimal);\r
68                 }\r
69 \r
70                 public static byte GetTypeCode (Type type)\r
71                 {\r
72                         if (type == typeof(TimeSpan)) return (byte) BinaryTypeCode.TimeSpan;\r
73                         else return _typeCodeMap [(int)Type.GetTypeCode(type)];\r
74                 }\r
75 \r
76                 public static Type GetTypeFromCode (int code)\r
77                 {\r
78                         return _typeCodesToType [code];\r
79                 }\r
80         }\r
81 \r
82         internal enum BinaryElement : byte\r
83         {
84                 Header = 0,
85                 RefTypeObject = 1,
86                 _Unknown1 = 2,
87                 _Unknown2 = 3,
88                 RuntimeObject = 4,
89                 ExternalObject = 5,
90                 String = 6,
91                 GenericArray = 7,
92                 BoxedPrimitiveTypeValue = 8,
93                 ObjectReference = 9,
94                 NullValue = 10,
95                 End = 11,
96                 Assembly = 12,
97                 ArrayFiller8b = 13,
98                 ArrayFiller32b = 14,
99                 ArrayOfPrimitiveType = 15,
100                 ArrayOfObject = 16,
101                 ArrayOfString = 17,
102                 Method = 18,
103                 _Unknown4 = 19,
104                 _Unknown5 = 20,
105                 MethodCall = 21,
106                 MethodResponse = 22
107         }
108
109         internal enum TypeTag : byte
110         {
111                 PrimitiveType = 0,
112                 String = 1,
113                 ObjectType = 2,
114                 RuntimeType = 3,
115                 GenericType = 4,
116                 ArrayOfObject = 5,
117                 ArrayOfString = 6,
118                 ArrayOfPrimitiveType = 7
119         }
120
121         internal enum ArrayStructure : byte
122         {
123                 SingleDimensional = 0,
124                 Jagged = 1,
125                 MultiDimensional = 2
126         }\r
127 \r
128         internal enum MethodFlags : byte
129         {
130                 NoArguments = 1,
131                 PrimitiveArguments = 2,
132                 ArgumentsInSimpleArray = 4,
133                 ArgumentsInMultiArray = 8,
134                 ExcludeLogicalCallContext = 16,
135                 IncludesLogicalCallContext = 64,
136                 IncludesSignature = 128,
137
138                 FormatMask = 15,
139                 NeedsInfoArrayMask = 4 + 8 + 64 + 128
140         }\r
141 \r
142         internal enum ReturnTypeTag : byte\r
143         {\r
144                 Null = 2,\r
145                 PrimitiveType = 8,\r
146                 ObjectType = 16,\r
147                 Exception = 32\r
148         }\r
149 \r
150         enum BinaryTypeCode : byte\r
151         {\r
152                 Boolean = 1,\r
153                 Byte = 2,\r
154                 Char = 3,\r
155                 Decimal = 5,\r
156                 Double = 6,\r
157                 Int16 = 7,\r
158                 Int32 = 8,\r
159                 Int64 = 9,\r
160                 SByte = 10,\r
161                 Single = 11,\r
162                 TimeSpan = 12,\r
163                 DateTime = 13,\r
164                 UInt16 = 14,\r
165                 UInt32 = 15,\r
166                 UInt64 = 16,\r
167                 Null = 17,\r
168                 String = 18\r
169         }\r
170 \r
171 }\r