BinaryCommon.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // BinaryCommon.cs
  2. //
  3. // Author:
  4. // Lluis Sanchez Gual ([email protected])
  5. //
  6. // (C) 2003 Lluis Sanchez Gual
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. namespace System.Runtime.Serialization.Formatters.Binary
  31. {
  32. internal class BinaryCommon
  33. {
  34. // Header present in all binary serializations
  35. public static byte[] BinaryHeader = new Byte[] {0,1,0,0,0,255,255,255,255,1,0,0,0,0,0,0,0};
  36. static Type[] _typeCodesToType;
  37. static byte[] _typeCodeMap;
  38. public static bool UseReflectionSerialization = false;
  39. static BinaryCommon()
  40. {
  41. _typeCodesToType = new Type [19];
  42. _typeCodesToType[(int)BinaryTypeCode.Boolean] = typeof (Boolean);
  43. _typeCodesToType[(int)BinaryTypeCode.Byte] = typeof (Byte);
  44. _typeCodesToType[(int)BinaryTypeCode.Char] = typeof (Char);
  45. _typeCodesToType[(int)BinaryTypeCode.TimeSpan] = typeof (TimeSpan);
  46. _typeCodesToType[(int)BinaryTypeCode.DateTime] = typeof (DateTime);
  47. _typeCodesToType[(int)BinaryTypeCode.Decimal] = typeof (Decimal);
  48. _typeCodesToType[(int)BinaryTypeCode.Double] = typeof (Double);
  49. _typeCodesToType[(int)BinaryTypeCode.Int16] = typeof (Int16);
  50. _typeCodesToType[(int)BinaryTypeCode.Int32] = typeof (Int32);
  51. _typeCodesToType[(int)BinaryTypeCode.Int64] = typeof (Int64);
  52. _typeCodesToType[(int)BinaryTypeCode.SByte] = typeof (SByte);
  53. _typeCodesToType[(int)BinaryTypeCode.Single] = typeof (Single);
  54. _typeCodesToType[(int)BinaryTypeCode.UInt16] = typeof (UInt16);
  55. _typeCodesToType[(int)BinaryTypeCode.UInt32] = typeof (UInt32);
  56. _typeCodesToType[(int)BinaryTypeCode.UInt64] = typeof (UInt64);
  57. _typeCodesToType[(int)BinaryTypeCode.Null] = null;
  58. _typeCodesToType[(int)BinaryTypeCode.String] = typeof (string);
  59. _typeCodeMap = new byte[30];
  60. _typeCodeMap[(int)TypeCode.Boolean] = (byte) BinaryTypeCode.Boolean;
  61. _typeCodeMap[(int)TypeCode.Byte] = (byte) BinaryTypeCode.Byte;
  62. _typeCodeMap[(int)TypeCode.Char] = (byte) BinaryTypeCode.Char;
  63. _typeCodeMap[(int)TypeCode.DateTime] = (byte) BinaryTypeCode.DateTime;
  64. _typeCodeMap[(int)TypeCode.Decimal] = (byte) BinaryTypeCode.Decimal;
  65. _typeCodeMap[(int)TypeCode.Double] = (byte) BinaryTypeCode.Double;
  66. _typeCodeMap[(int)TypeCode.Int16] = (byte) BinaryTypeCode.Int16;
  67. _typeCodeMap[(int)TypeCode.Int32] = (byte) BinaryTypeCode.Int32;
  68. _typeCodeMap[(int)TypeCode.Int64] = (byte) BinaryTypeCode.Int64;
  69. _typeCodeMap[(int)TypeCode.SByte] = (byte) BinaryTypeCode.SByte;
  70. _typeCodeMap[(int)TypeCode.Single] = (byte) BinaryTypeCode.Single;
  71. _typeCodeMap[(int)TypeCode.UInt16] = (byte) BinaryTypeCode.UInt16;
  72. _typeCodeMap[(int)TypeCode.UInt32] = (byte) BinaryTypeCode.UInt32;
  73. _typeCodeMap[(int)TypeCode.UInt64] = (byte) BinaryTypeCode.UInt64;
  74. _typeCodeMap[(int)TypeCode.String] = (byte) BinaryTypeCode.String;
  75. // TimeStamp does not have a TypeCode, so it is managed as a special
  76. // case in GetTypeCode()
  77. // This environment variable is only for test and benchmarking pourposes.
  78. // By default, mono will always use IL generated class serializers.
  79. string s = Environment.GetEnvironmentVariable("MONO_REFLECTION_SERIALIZER");
  80. if (s == null) s = "no";
  81. UseReflectionSerialization = (s != "no");
  82. }
  83. public static bool IsPrimitive (Type type)
  84. {
  85. return type.IsPrimitive ||
  86. type == typeof (DateTime) ||
  87. type == typeof (TimeSpan) ||
  88. type == typeof (Decimal);
  89. }
  90. public static byte GetTypeCode (Type type)
  91. {
  92. if (type == typeof(TimeSpan)) return (byte) BinaryTypeCode.TimeSpan;
  93. else return _typeCodeMap [(int)Type.GetTypeCode(type)];
  94. }
  95. public static Type GetTypeFromCode (int code)
  96. {
  97. return _typeCodesToType [code];
  98. }
  99. public static void CheckSerializable (Type type, ISurrogateSelector selector, StreamingContext context)
  100. {
  101. if (!type.IsSerializable && !type.IsInterface)
  102. {
  103. if (selector != null && selector.GetSurrogate (type, context, out selector) != null)
  104. return;
  105. throw new SerializationException ("Type " + type + " is not marked as Serializable.");
  106. }
  107. }
  108. }
  109. internal enum BinaryElement : byte
  110. {
  111. Header = 0,
  112. RefTypeObject = 1,
  113. _Unknown1 = 2,
  114. _Unknown2 = 3,
  115. RuntimeObject = 4,
  116. ExternalObject = 5,
  117. String = 6,
  118. GenericArray = 7,
  119. BoxedPrimitiveTypeValue = 8,
  120. ObjectReference = 9,
  121. NullValue = 10,
  122. End = 11,
  123. Assembly = 12,
  124. ArrayFiller8b = 13,
  125. ArrayFiller32b = 14,
  126. ArrayOfPrimitiveType = 15,
  127. ArrayOfObject = 16,
  128. ArrayOfString = 17,
  129. Method = 18,
  130. _Unknown4 = 19,
  131. _Unknown5 = 20,
  132. MethodCall = 21,
  133. MethodResponse = 22
  134. }
  135. internal enum TypeTag : byte
  136. {
  137. PrimitiveType = 0,
  138. String = 1,
  139. ObjectType = 2,
  140. RuntimeType = 3,
  141. GenericType = 4,
  142. ArrayOfObject = 5,
  143. ArrayOfString = 6,
  144. ArrayOfPrimitiveType = 7
  145. }
  146. internal enum ArrayStructure : byte
  147. {
  148. SingleDimensional = 0,
  149. Jagged = 1,
  150. MultiDimensional = 2
  151. }
  152. internal enum MethodFlags : byte
  153. {
  154. NoArguments = 1,
  155. PrimitiveArguments = 2,
  156. ArgumentsInSimpleArray = 4,
  157. ArgumentsInMultiArray = 8,
  158. ExcludeLogicalCallContext = 16,
  159. IncludesLogicalCallContext = 64,
  160. IncludesSignature = 128,
  161. FormatMask = 15,
  162. NeedsInfoArrayMask = 4 + 8 + 64 + 128
  163. }
  164. internal enum ReturnTypeTag : byte
  165. {
  166. Null = 2,
  167. PrimitiveType = 8,
  168. ObjectType = 16,
  169. Exception = 32
  170. }
  171. enum BinaryTypeCode : byte
  172. {
  173. Boolean = 1,
  174. Byte = 2,
  175. Char = 3,
  176. Decimal = 5,
  177. Double = 6,
  178. Int16 = 7,
  179. Int32 = 8,
  180. Int64 = 9,
  181. SByte = 10,
  182. Single = 11,
  183. TimeSpan = 12,
  184. DateTime = 13,
  185. UInt16 = 14,
  186. UInt32 = 15,
  187. UInt64 = 16,
  188. Null = 17,
  189. String = 18
  190. }
  191. }