PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/Renci.SshNet/Common/Extensions.cs

https://gitlab.com/timofonic/win-sshfs
C# | 203 lines | 126 code | 26 blank | 51 comment | 22 complexity | 1ee9ea8278afafdfe44655da047f977f MD5 | raw file
  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 partial 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. enumerator1.Dispose();
  59. 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. #if SILVERLIGHT
  74. #else
  75. /// <summary>
  76. /// Prints out
  77. /// </summary>
  78. /// <param name="bytes">The bytes.</param>
  79. internal static void DebugPrint(this IEnumerable<byte> bytes)
  80. {
  81. foreach (var b in bytes)
  82. {
  83. Debug.Write(string.Format(CultureInfo.CurrentCulture, "0x{0:x2}, ", b));
  84. }
  85. Debug.WriteLine(string.Empty);
  86. }
  87. #endif
  88. /// <summary>
  89. /// Trims the leading zero from bytes array.
  90. /// </summary>
  91. /// <param name="data">The data.</param>
  92. /// <returns></returns>
  93. internal static IEnumerable<byte> TrimLeadingZero(this IEnumerable<byte> data)
  94. {
  95. bool leadingZero = true;
  96. foreach (var item in data)
  97. {
  98. if (item == 0 & leadingZero)
  99. {
  100. continue;
  101. }
  102. else
  103. {
  104. leadingZero = false;
  105. }
  106. yield return item;
  107. }
  108. }
  109. /// <summary>
  110. /// Creates an instance of the specified type using that type's default constructor.
  111. /// </summary>
  112. /// <typeparam name="T">The type to create.</typeparam>
  113. /// <param name="type">Type of the instance to create.</param>
  114. /// <returns>A reference to the newly created object.</returns>
  115. internal static T CreateInstance<T>(this Type type) where T : class
  116. {
  117. if (type == null)
  118. return null;
  119. return Activator.CreateInstance(type) as T;
  120. }
  121. /// <summary>
  122. /// Returns the specified 16-bit unsigned integer value as an array of bytes.
  123. /// </summary>
  124. /// <param name="value">The number to convert.</param>
  125. /// <returns>An array of bytes with length 2.</returns>
  126. internal static byte[] GetBytes(this UInt16 value)
  127. {
  128. return new byte[] { (byte)(value >> 8), (byte)(value & 0xFF) };
  129. }
  130. /// <summary>
  131. /// Returns the specified 32-bit unsigned integer value as an array of bytes.
  132. /// </summary>
  133. /// <param name="value">The number to convert.</param>
  134. /// <returns>An array of bytes with length 4.</returns>
  135. internal static byte[] GetBytes(this UInt32 value)
  136. {
  137. return new byte[] { (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value & 0xFF) };
  138. }
  139. /// <summary>
  140. /// Returns the specified 64-bit unsigned integer value as an array of bytes.
  141. /// </summary>
  142. /// <param name="value">The number to convert.</param>
  143. /// <returns>An array of bytes with length 8.</returns>
  144. internal static byte[] GetBytes(this UInt64 value)
  145. {
  146. 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) };
  147. }
  148. /// <summary>
  149. /// Returns the specified 64-bit signed integer value as an array of bytes.
  150. /// </summary>
  151. /// <param name="value">The number to convert.</param>
  152. /// <returns>An array of bytes with length 8.</returns>
  153. internal static byte[] GetBytes(this Int64 value)
  154. {
  155. 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) };
  156. }
  157. #if SILVERLIGHT
  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);
  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);
  160. #else
  161. 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);
  162. 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);
  163. #endif
  164. internal static bool IsValidHost(this string value)
  165. {
  166. return value != null && (_rehost.Match(value).Success || _reIPv6.Match(value).Success);
  167. }
  168. internal static bool IsValidPort(this uint value)
  169. {
  170. return value >= IPEndPoint.MinPort && value <= IPEndPoint.MaxPort;
  171. }
  172. internal static bool IsValidPort(this int value)
  173. {
  174. return value >= IPEndPoint.MinPort && value <= IPEndPoint.MaxPort;
  175. }
  176. }
  177. }