BinaryCommon.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 && type != typeof (IntPtr)) ||
  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. public static void SwapBytes (byte[] byteArray, int size, int dataSize)
  109. {
  110. byte b;
  111. if (dataSize == 8) {
  112. for (int n=0; n<size; n+=8) {
  113. b = byteArray [n]; byteArray [n] = byteArray [n + 7]; byteArray [n + 7] = b;
  114. b = byteArray [n+1]; byteArray [n+1] = byteArray [n + 6]; byteArray [n + 6] = b;
  115. b = byteArray [n+2]; byteArray [n+2] = byteArray [n + 5]; byteArray [n + 5] = b;
  116. b = byteArray [n+3]; byteArray [n+3] = byteArray [n + 4]; byteArray [n + 4] = b;
  117. }
  118. } else if (dataSize == 4) {
  119. for (int n=0; n<size; n+=4) {
  120. b = byteArray [n]; byteArray [n] = byteArray [n + 3]; byteArray [n + 3] = b;
  121. b = byteArray [n+1]; byteArray [n+1] = byteArray [n + 2]; byteArray [n + 2] = b;
  122. }
  123. } else if (dataSize == 2) {
  124. for (int n=0; n<size; n+=2) {
  125. b = byteArray [n]; byteArray [n] = byteArray [n + 1]; byteArray [n + 1] = b;
  126. }
  127. }
  128. }
  129. }
  130. internal enum BinaryElement : byte
  131. {
  132. Header = 0,
  133. RefTypeObject = 1,
  134. UntypedRuntimeObject = 2,
  135. UntypedExternalObject = 3,
  136. RuntimeObject = 4,
  137. ExternalObject = 5,
  138. String = 6,
  139. GenericArray = 7,
  140. BoxedPrimitiveTypeValue = 8,
  141. ObjectReference = 9,
  142. NullValue = 10,
  143. End = 11,
  144. Assembly = 12,
  145. ArrayFiller8b = 13,
  146. ArrayFiller32b = 14,
  147. ArrayOfPrimitiveType = 15,
  148. ArrayOfObject = 16,
  149. ArrayOfString = 17,
  150. Method = 18,
  151. _Unknown4 = 19,
  152. _Unknown5 = 20,
  153. MethodCall = 21,
  154. MethodResponse = 22
  155. }
  156. internal enum TypeTag : byte
  157. {
  158. PrimitiveType = 0,
  159. String = 1,
  160. ObjectType = 2,
  161. RuntimeType = 3,
  162. GenericType = 4,
  163. ArrayOfObject = 5,
  164. ArrayOfString = 6,
  165. ArrayOfPrimitiveType = 7
  166. }
  167. internal enum ArrayStructure : byte
  168. {
  169. SingleDimensional = 0,
  170. Jagged = 1,
  171. MultiDimensional = 2
  172. }
  173. internal enum MethodFlags : byte
  174. {
  175. NoArguments = 1,
  176. PrimitiveArguments = 2,
  177. ArgumentsInSimpleArray = 4,
  178. ArgumentsInMultiArray = 8,
  179. ExcludeLogicalCallContext = 16,
  180. IncludesLogicalCallContext = 64,
  181. IncludesSignature = 128,
  182. FormatMask = 15,
  183. NeedsInfoArrayMask = 4 + 8 + 64 + 128
  184. }
  185. internal enum ReturnTypeTag : byte
  186. {
  187. Null = 2,
  188. PrimitiveType = 8,
  189. ObjectType = 16,
  190. Exception = 32
  191. }
  192. enum BinaryTypeCode : byte
  193. {
  194. Boolean = 1,
  195. Byte = 2,
  196. Char = 3,
  197. Decimal = 5,
  198. Double = 6,
  199. Int16 = 7,
  200. Int32 = 8,
  201. Int64 = 9,
  202. SByte = 10,
  203. Single = 11,
  204. TimeSpan = 12,
  205. DateTime = 13,
  206. UInt16 = 14,
  207. UInt32 = 15,
  208. UInt64 = 16,
  209. Null = 17,
  210. String = 18
  211. }
  212. }