Extensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Net;
  6. using Renci.SshNet.Messages;
  7. using Renci.SshNet.Messages.Connection;
  8. namespace Renci.SshNet.Common
  9. {
  10. /// <summary>
  11. /// Collection of different extension method
  12. /// </summary>
  13. internal static partial class Extensions
  14. {
  15. internal static byte[] ToArray(this GlobalRequestName globalRequestName)
  16. {
  17. switch (globalRequestName)
  18. {
  19. case GlobalRequestName.TcpIpForward:
  20. return SshData.Ascii.GetBytes("tcpip-forward");
  21. case GlobalRequestName.CancelTcpIpForward:
  22. return SshData.Ascii.GetBytes("cancel-tcpip-forward");
  23. default:
  24. throw new NotSupportedException(string.Format("Global request name '{0}' is not supported.", globalRequestName));
  25. }
  26. }
  27. internal static byte[] ToArray(this ServiceName serviceName)
  28. {
  29. switch (serviceName)
  30. {
  31. case ServiceName.UserAuthentication:
  32. return SshData.Ascii.GetBytes("ssh-userauth");
  33. case ServiceName.Connection:
  34. return SshData.Ascii.GetBytes("ssh-connection");
  35. default:
  36. throw new NotSupportedException(string.Format("Service name '{0}' is not supported.", serviceName));
  37. }
  38. }
  39. internal static ServiceName ToServiceName(this byte[] data)
  40. {
  41. var sshServiceName = SshData.Ascii.GetString(data);
  42. switch (sshServiceName)
  43. {
  44. case "ssh-userauth":
  45. return ServiceName.UserAuthentication;
  46. case "ssh-connection":
  47. return ServiceName.Connection;
  48. default:
  49. throw new NotSupportedException(string.Format("Service name '{0}' is not supported.", sshServiceName));
  50. }
  51. }
  52. internal static GlobalRequestName ToGlobalRequestName(this byte[] data)
  53. {
  54. var sshGlobalRequestName = SshData.Ascii.GetString(data);
  55. switch (sshGlobalRequestName)
  56. {
  57. case "tcpip-forward":
  58. return GlobalRequestName.TcpIpForward;
  59. case "cancel-tcpip-forward":
  60. return GlobalRequestName.CancelTcpIpForward;
  61. default:
  62. throw new NotSupportedException(string.Format("Global request name '{0}' is not supported.", sshGlobalRequestName));
  63. }
  64. }
  65. #if TUNING
  66. internal static BigInteger ToBigInteger(this byte[] data)
  67. {
  68. var reversed = new byte[data.Length];
  69. Buffer.BlockCopy(data, 0, reversed, 0, data.Length);
  70. return new BigInteger(reversed.Reverse());
  71. }
  72. /// <summary>
  73. /// Reverses the sequence of the elements in the entire one-dimensional <see cref="Array"/>.
  74. /// </summary>
  75. /// <param name="array">The one-dimensional <see cref="Array"/> to reverse.</param>
  76. /// <returns>
  77. /// The <see cref="Array"/> with its elements reversed.
  78. /// </returns>
  79. internal static T[] Reverse<T>(this T[] array)
  80. {
  81. Array.Reverse(array);
  82. return array;
  83. }
  84. #endif
  85. /// <summary>
  86. /// Checks whether a collection is the same as another collection
  87. /// </summary>
  88. /// <param name="value">The current instance object</param>
  89. /// <param name="compareList">The collection to compare with</param>
  90. /// <param name="comparer">The comparer object to use to compare each item in the collection. If null uses EqualityComparer(T).Default</param>
  91. /// <returns>True if the two collections contain all the same items in the same order</returns>
  92. internal static bool IsEqualTo<TSource>(this IEnumerable<TSource> value, IEnumerable<TSource> compareList, IEqualityComparer<TSource> comparer)
  93. {
  94. if (value == compareList)
  95. return true;
  96. if (value == null || compareList == null)
  97. return false;
  98. if (comparer == null)
  99. {
  100. comparer = EqualityComparer<TSource>.Default;
  101. }
  102. var enumerator1 = value.GetEnumerator();
  103. var enumerator2 = compareList.GetEnumerator();
  104. var enum1HasValue = enumerator1.MoveNext();
  105. var enum2HasValue = enumerator2.MoveNext();
  106. try
  107. {
  108. while (enum1HasValue && enum2HasValue)
  109. {
  110. if (!comparer.Equals(enumerator1.Current, enumerator2.Current))
  111. {
  112. return false;
  113. }
  114. enum1HasValue = enumerator1.MoveNext();
  115. enum2HasValue = enumerator2.MoveNext();
  116. }
  117. return !(enum1HasValue || enum2HasValue);
  118. }
  119. finally
  120. {
  121. enumerator1.Dispose();
  122. enumerator2.Dispose();
  123. }
  124. }
  125. /// <summary>
  126. /// Checks whether a collection is the same as another collection
  127. /// </summary>
  128. /// <param name="value">The current instance object</param>
  129. /// <param name="compareList">The collection to compare with</param>
  130. /// <returns>True if the two collections contain all the same items in the same order</returns>
  131. internal static bool IsEqualTo<TSource>(this IEnumerable<TSource> value, IEnumerable<TSource> compareList)
  132. {
  133. return IsEqualTo(value, compareList, null);
  134. }
  135. #if SILVERLIGHT
  136. #else
  137. /// <summary>
  138. /// Prints out
  139. /// </summary>
  140. /// <param name="bytes">The bytes.</param>
  141. internal static void DebugPrint(this IEnumerable<byte> bytes)
  142. {
  143. foreach (var b in bytes)
  144. {
  145. Debug.Write(string.Format(CultureInfo.CurrentCulture, "0x{0:x2}, ", b));
  146. }
  147. Debug.WriteLine(string.Empty);
  148. }
  149. #endif
  150. /// <summary>
  151. /// Trims the leading zero from bytes array.
  152. /// </summary>
  153. /// <param name="data">The data.</param>
  154. /// <returns>Data without leading zeros.</returns>
  155. internal static IEnumerable<byte> TrimLeadingZero(this IEnumerable<byte> data)
  156. {
  157. var leadingZero = true;
  158. foreach (var item in data)
  159. {
  160. if (item == 0 & leadingZero)
  161. {
  162. continue;
  163. }
  164. leadingZero = false;
  165. yield return item;
  166. }
  167. }
  168. /// <summary>
  169. /// Creates an instance of the specified type using that type's default constructor.
  170. /// </summary>
  171. /// <typeparam name="T">The type to create.</typeparam>
  172. /// <param name="type">Type of the instance to create.</param>
  173. /// <returns>A reference to the newly created object.</returns>
  174. internal static T CreateInstance<T>(this Type type) where T : class
  175. {
  176. if (type == null)
  177. return null;
  178. return Activator.CreateInstance(type) as T;
  179. }
  180. /// <summary>
  181. /// Returns the specified 16-bit unsigned integer value as an array of bytes.
  182. /// </summary>
  183. /// <param name="value">The number to convert.</param>
  184. /// <returns>An array of bytes with length 2.</returns>
  185. internal static byte[] GetBytes(this UInt16 value)
  186. {
  187. return new[] {(byte) (value >> 8), (byte) (value & 0xFF)};
  188. }
  189. /// <summary>
  190. /// Returns the specified 32-bit unsigned integer value as an array of bytes.
  191. /// </summary>
  192. /// <param name="value">The number to convert.</param>
  193. /// <returns>An array of bytes with length 4.</returns>
  194. internal static byte[] GetBytes(this UInt32 value)
  195. {
  196. #if TUNING
  197. var buffer = new byte[4];
  198. value.Write(buffer, 0);
  199. return buffer;
  200. #else
  201. return new[] {(byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) (value & 0xFF)};
  202. #endif
  203. }
  204. /// <summary>
  205. /// Returns the specified 32-bit unsigned integer value as an array of bytes.
  206. /// </summary>
  207. /// <param name="value">The number to convert.</param>
  208. /// <param name="buffer">The array of bytes to write <paramref name="value"/> to.</param>
  209. /// <param name="offset">The zero-based offset in <paramref name="buffer"/> at which to begin writing.</param>
  210. internal static void Write(this uint value, byte[] buffer, int offset)
  211. {
  212. buffer[offset++] = (byte) (value >> 24);
  213. buffer[offset++] = (byte) (value >> 16);
  214. buffer[offset++] = (byte)(value >> 8);
  215. buffer[offset] = (byte) (value & 0xFF);
  216. }
  217. /// <summary>
  218. /// Returns the specified 64-bit unsigned integer value as an array of bytes.
  219. /// </summary>
  220. /// <param name="value">The number to convert.</param>
  221. /// <returns>An array of bytes with length 8.</returns>
  222. internal static byte[] GetBytes(this UInt64 value)
  223. {
  224. return new[]
  225. {
  226. (byte) (value >> 56), (byte) (value >> 48), (byte) (value >> 40), (byte) (value >> 32),
  227. (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) (value & 0xFF)
  228. };
  229. }
  230. /// <summary>
  231. /// Returns the specified 64-bit signed integer value as an array of bytes.
  232. /// </summary>
  233. /// <param name="value">The number to convert.</param>
  234. /// <returns>An array of bytes with length 8.</returns>
  235. internal static byte[] GetBytes(this Int64 value)
  236. {
  237. return new[]
  238. {
  239. (byte) (value >> 56), (byte) (value >> 48), (byte) (value >> 40), (byte) (value >> 32),
  240. (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) (value & 0xFF)
  241. };
  242. }
  243. internal static void ValidatePort(this uint value, string argument)
  244. {
  245. if (value > IPEndPoint.MaxPort)
  246. throw new ArgumentOutOfRangeException(argument,
  247. string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.",
  248. IPEndPoint.MaxPort));
  249. }
  250. internal static void ValidatePort(this int value, string argument)
  251. {
  252. if (value < IPEndPoint.MinPort)
  253. throw new ArgumentOutOfRangeException(argument,
  254. string.Format(CultureInfo.InvariantCulture, "Specified value cannot be less than {0}.",
  255. IPEndPoint.MinPort));
  256. if (value > IPEndPoint.MaxPort)
  257. throw new ArgumentOutOfRangeException(argument,
  258. string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.",
  259. IPEndPoint.MaxPort));
  260. }
  261. /// <summary>
  262. /// Returns a specified number of contiguous bytes from a given offset.
  263. /// </summary>
  264. /// <param name="data">The array to return a number of bytes from.</param>
  265. /// <param name="offset">The zero-based offset in <paramref name="data"/> at which to begin taking bytes.</param>
  266. /// <param name="length">The number of bytes to take from <paramref name="data"/>.</param>
  267. /// <returns>
  268. /// A <see cref="byte"/> array that contains the specified number of bytes at the specified offset
  269. /// of the input array.
  270. /// </returns>
  271. internal static byte[] Take(this byte[] data, int offset, int length)
  272. {
  273. var taken = new byte[length];
  274. Buffer.BlockCopy(data, offset, taken, 0, length);
  275. return taken;
  276. }
  277. }
  278. }