Platform.cs 6.8 KB

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