PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Renci.SshNet/Common/Extensions.cs

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