Extensions.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System;
  6. using System.Globalization;
  7. using System.Text.RegularExpressions;
  8. using System.Net;
  9. namespace Renci.SshNet
  10. {
  11. /// <summary>
  12. /// Collection of different extension method
  13. /// </summary>
  14. public static class Extensions
  15. {
  16. /// <summary>
  17. /// Checks whether a collection is the same as another collection
  18. /// </summary>
  19. /// <param name="value">The current instance object</param>
  20. /// <param name="compareList">The collection to compare with</param>
  21. /// <param name="comparer">The comparer object to use to compare each item in the collection. If null uses EqualityComparer(T).Default</param>
  22. /// <returns>True if the two collections contain all the same items in the same order</returns>
  23. internal static bool IsEqualTo<TSource>(this IEnumerable<TSource> value, IEnumerable<TSource> compareList, IEqualityComparer<TSource> comparer)
  24. {
  25. if (value == compareList)
  26. {
  27. return true;
  28. }
  29. else if (value == null || compareList == null)
  30. {
  31. return false;
  32. }
  33. else
  34. {
  35. if (comparer == null)
  36. {
  37. comparer = EqualityComparer<TSource>.Default;
  38. }
  39. IEnumerator<TSource> enumerator1 = value.GetEnumerator();
  40. IEnumerator<TSource> enumerator2 = compareList.GetEnumerator();
  41. bool enum1HasValue = enumerator1.MoveNext();
  42. bool enum2HasValue = enumerator2.MoveNext();
  43. try
  44. {
  45. while (enum1HasValue && enum2HasValue)
  46. {
  47. if (!comparer.Equals(enumerator1.Current, enumerator2.Current))
  48. {
  49. return false;
  50. }
  51. enum1HasValue = enumerator1.MoveNext();
  52. enum2HasValue = enumerator2.MoveNext();
  53. }
  54. return !(enum1HasValue || enum2HasValue);
  55. }
  56. finally
  57. {
  58. if (enumerator1 != null) enumerator1.Dispose();
  59. if (enumerator2 != null) enumerator2.Dispose();
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// Checks whether a collection is the same as another collection
  65. /// </summary>
  66. /// <param name="value">The current instance object</param>
  67. /// <param name="compareList">The collection to compare with</param>
  68. /// <returns>True if the two collections contain all the same items in the same order</returns>
  69. internal static bool IsEqualTo<TSource>(this IEnumerable<TSource> value, IEnumerable<TSource> compareList)
  70. {
  71. return IsEqualTo(value, compareList, null);
  72. }
  73. /// <summary>
  74. /// Prints out
  75. /// </summary>
  76. /// <param name="bytes">The bytes.</param>
  77. internal static void DebugPrint(this IEnumerable<byte> bytes)
  78. {
  79. foreach (var b in bytes)
  80. {
  81. Debug.Write(string.Format(CultureInfo.CurrentCulture, "0x{0:x2}, ", b));
  82. }
  83. Debug.WriteLine(string.Empty);
  84. }
  85. /// <summary>
  86. /// Trims the leading zero from bytes array.
  87. /// </summary>
  88. /// <param name="data">The data.</param>
  89. /// <returns></returns>
  90. internal static IEnumerable<byte> TrimLeadingZero(this IEnumerable<byte> data)
  91. {
  92. bool leadingZero = true;
  93. foreach (var item in data)
  94. {
  95. if (item == 0 & leadingZero)
  96. {
  97. continue;
  98. }
  99. else
  100. {
  101. leadingZero = false;
  102. }
  103. yield return item;
  104. }
  105. }
  106. /// <summary>
  107. /// Creates an instance of the specified type using that type's default constructor.
  108. /// </summary>
  109. /// <typeparam name="T">The type to create.</typeparam>
  110. /// <param name="type">Type of the instance to create.</param>
  111. /// <returns>A reference to the newly created object.</returns>
  112. internal static T CreateInstance<T>(this Type type) where T : class
  113. {
  114. if (type == null)
  115. return null;
  116. return Activator.CreateInstance(type) as T;
  117. }
  118. /// <summary>
  119. /// Returns the specified 16-bit unsigned integer value as an array of bytes.
  120. /// </summary>
  121. /// <param name="value">The number to convert.</param>
  122. /// <returns>An array of bytes with length 2.</returns>
  123. internal static byte[] GetBytes(this UInt16 value)
  124. {
  125. return new byte[] { (byte)(value >> 8), (byte)(value & 0xFF) };
  126. }
  127. /// <summary>
  128. /// Returns the specified 32-bit unsigned integer value as an array of bytes.
  129. /// </summary>
  130. /// <param name="value">The number to convert.</param>
  131. /// <returns>An array of bytes with length 4.</returns>
  132. internal static byte[] GetBytes(this UInt32 value)
  133. {
  134. return new byte[] { (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value & 0xFF) };
  135. }
  136. /// <summary>
  137. /// Returns the specified 64-bit unsigned integer value as an array of bytes.
  138. /// </summary>
  139. /// <param name="value">The number to convert.</param>
  140. /// <returns>An array of bytes with length 8.</returns>
  141. internal static byte[] GetBytes(this UInt64 value)
  142. {
  143. return new byte[] { (byte)(value >> 56), (byte)(value >> 48), (byte)(value >> 40), (byte)(value >> 32), (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value & 0xFF) };
  144. }
  145. /// <summary>
  146. /// Returns the specified 64-bit signed integer value as an array of bytes.
  147. /// </summary>
  148. /// <param name="value">The number to convert.</param>
  149. /// <returns>An array of bytes with length 8.</returns>
  150. internal static byte[] GetBytes(this Int64 value)
  151. {
  152. return new byte[] { (byte)(value >> 56), (byte)(value >> 48), (byte)(value >> 40), (byte)(value >> 32), (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value & 0xFF) };
  153. }
  154. #if SILVERLIGHT
  155. private static Regex _rehost = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$", RegexOptions.IgnoreCase);
  156. private static Regex _reIPv6 = new Regex(@"^(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\2([0-9A-F]{1,4}(::?|$)){0,2}|((25[0-5]|(2[0-4]|1\d|[1-9])?\d)(\.|$)){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:|\.)\z", RegexOptions.IgnoreCase);
  157. #else
  158. private static Regex _rehost = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  159. private static Regex _reIPv6 = new Regex(@"^(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\2([0-9A-F]{1,4}(::?|$)){0,2}|((25[0-5]|(2[0-4]|1\d|[1-9])?\d)(\.|$)){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:|\.)\z", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  160. #endif
  161. internal static bool IsValidHost(this string value)
  162. {
  163. if (value == null)
  164. return false;
  165. if (_rehost.Match(value).Success)
  166. return true;
  167. if (_reIPv6.Match(value).Success)
  168. return true;
  169. return false;
  170. }
  171. internal static bool IsValidPort(this uint value)
  172. {
  173. if (value < IPEndPoint.MinPort)
  174. return false;
  175. if (value > IPEndPoint.MaxPort)
  176. return false;
  177. return true;
  178. }
  179. internal static bool IsValidPort(this int value)
  180. {
  181. if (value < IPEndPoint.MinPort)
  182. return false;
  183. if (value > IPEndPoint.MaxPort)
  184. return false;
  185. return true;
  186. }
  187. }
  188. }