Platform.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Text;
  7. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  8. using System.Collections.Generic;
  9. #else
  10. using System.Collections;
  11. #endif
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities
  13. {
  14. internal abstract class Platform
  15. {
  16. private static readonly CompareInfo InvariantCompareInfo = CultureInfo.InvariantCulture.CompareInfo;
  17. #if NETCF_1_0 || NETCF_2_0
  18. private static string GetNewLine()
  19. {
  20. MemoryStream buf = new MemoryStream();
  21. StreamWriter w = new StreamWriter(buf, Encoding.UTF8);
  22. w.WriteLine();
  23. Dispose(w);
  24. byte[] bs = buf.ToArray();
  25. return Encoding.UTF8.GetString(bs, 0, bs.Length);
  26. }
  27. #else
  28. private static string GetNewLine()
  29. {
  30. return Environment.NewLine;
  31. }
  32. #endif
  33. internal static bool EqualsIgnoreCase(string a, string b)
  34. {
  35. #if PORTABLE || NETFX_CORE
  36. return String.Equals(a, b, StringComparison.OrdinalIgnoreCase);
  37. #else
  38. return ToUpperInvariant(a) == ToUpperInvariant(b);
  39. #endif
  40. }
  41. #if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT || (PORTABLE || NETFX_CORE && !DOTNET)
  42. internal static string GetEnvironmentVariable(
  43. string variable)
  44. {
  45. return null;
  46. }
  47. #else
  48. internal static string GetEnvironmentVariable(
  49. string variable)
  50. {
  51. try
  52. {
  53. return Environment.GetEnvironmentVariable(variable);
  54. }
  55. catch (System.Security.SecurityException)
  56. {
  57. // We don't have the required permission to read this environment variable,
  58. // which is fine, just act as if it's not set
  59. return null;
  60. }
  61. }
  62. #endif
  63. #if NETCF_1_0
  64. internal static Exception CreateNotImplementedException(
  65. string message)
  66. {
  67. return new Exception("Not implemented: " + message);
  68. }
  69. internal static bool Equals(
  70. object a,
  71. object b)
  72. {
  73. return a == b || (a != null && b != null && a.Equals(b));
  74. }
  75. #else
  76. internal static Exception CreateNotImplementedException(
  77. string message)
  78. {
  79. return new NotImplementedException(message);
  80. }
  81. #endif
  82. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  83. internal static System.Collections.IList CreateArrayList()
  84. {
  85. return new List<object>();
  86. }
  87. internal static System.Collections.IList CreateArrayList(int capacity)
  88. {
  89. return new List<object>(capacity);
  90. }
  91. internal static System.Collections.IList CreateArrayList(System.Collections.ICollection collection)
  92. {
  93. System.Collections.IList result = new List<object>(collection.Count);
  94. foreach (object o in collection)
  95. {
  96. result.Add(o);
  97. }
  98. return result;
  99. }
  100. internal static System.Collections.IList CreateArrayList(System.Collections.IEnumerable collection)
  101. {
  102. System.Collections.IList result = new List<object>();
  103. foreach (object o in collection)
  104. {
  105. result.Add(o);
  106. }
  107. return result;
  108. }
  109. internal static System.Collections.IDictionary CreateHashtable()
  110. {
  111. return new Dictionary<object, object>();
  112. }
  113. internal static System.Collections.IDictionary CreateHashtable(int capacity)
  114. {
  115. return new Dictionary<object, object>(capacity);
  116. }
  117. internal static System.Collections.IDictionary CreateHashtable(System.Collections.IDictionary dictionary)
  118. {
  119. System.Collections.IDictionary result = new Dictionary<object, object>(dictionary.Count);
  120. foreach (System.Collections.DictionaryEntry entry in dictionary)
  121. {
  122. result.Add(entry.Key, entry.Value);
  123. }
  124. return result;
  125. }
  126. #else
  127. internal static System.Collections.IList CreateArrayList()
  128. {
  129. return new ArrayList();
  130. }
  131. internal static System.Collections.IList CreateArrayList(int capacity)
  132. {
  133. return new ArrayList(capacity);
  134. }
  135. internal static System.Collections.IList CreateArrayList(System.Collections.ICollection collection)
  136. {
  137. return new ArrayList(collection);
  138. }
  139. internal static System.Collections.IList CreateArrayList(System.Collections.IEnumerable collection)
  140. {
  141. ArrayList result = new ArrayList();
  142. foreach (object o in collection)
  143. {
  144. result.Add(o);
  145. }
  146. return result;
  147. }
  148. internal static System.Collections.IDictionary CreateHashtable()
  149. {
  150. return new Hashtable();
  151. }
  152. internal static System.Collections.IDictionary CreateHashtable(int capacity)
  153. {
  154. return new Hashtable(capacity);
  155. }
  156. internal static System.Collections.IDictionary CreateHashtable(System.Collections.IDictionary dictionary)
  157. {
  158. return new Hashtable(dictionary);
  159. }
  160. #endif
  161. internal static string ToLowerInvariant(string s)
  162. {
  163. #if PORTABLE || NETFX_CORE
  164. return s.ToLowerInvariant();
  165. #else
  166. return s.ToLower(CultureInfo.InvariantCulture);
  167. #endif
  168. }
  169. internal static string ToUpperInvariant(string s)
  170. {
  171. #if PORTABLE || NETFX_CORE
  172. return s.ToUpperInvariant();
  173. #else
  174. return s.ToUpper(CultureInfo.InvariantCulture);
  175. #endif
  176. }
  177. internal static readonly string NewLine = GetNewLine();
  178. #if PORTABLE || NETFX_CORE
  179. internal static void Dispose(IDisposable d)
  180. {
  181. d.Dispose();
  182. }
  183. #else
  184. internal static void Dispose(Stream s)
  185. {
  186. s.Close();
  187. }
  188. internal static void Dispose(TextWriter t)
  189. {
  190. t.Close();
  191. }
  192. #endif
  193. internal static int IndexOf(string source, string value)
  194. {
  195. return InvariantCompareInfo.IndexOf(source, value, CompareOptions.Ordinal);
  196. }
  197. internal static int LastIndexOf(string source, string value)
  198. {
  199. return InvariantCompareInfo.LastIndexOf(source, value, CompareOptions.Ordinal);
  200. }
  201. internal static bool StartsWith(string source, string prefix)
  202. {
  203. return InvariantCompareInfo.IsPrefix(source, prefix, CompareOptions.Ordinal);
  204. }
  205. internal static bool EndsWith(string source, string suffix)
  206. {
  207. return InvariantCompareInfo.IsSuffix(source, suffix, CompareOptions.Ordinal);
  208. }
  209. internal static string GetTypeName(object obj)
  210. {
  211. return obj.GetType().FullName;
  212. }
  213. }
  214. }
  215. #pragma warning restore
  216. #endif