Add back Path APIs needed by UWP
authorJan Kotas <jkotas@microsoft.com>
Thu, 14 Apr 2016 03:54:21 +0000 (20:54 -0700)
committerJan Kotas <jkotas@microsoft.com>
Thu, 14 Apr 2016 03:54:21 +0000 (20:54 -0700)
[tfs-changeset: 1596013]

src/mscorlib/model.xml
src/mscorlib/src/Microsoft/Win32/Win32Native.cs
src/mscorlib/src/System/Collections/Hashtable.cs
src/mscorlib/src/System/IO/Path.cs

index 2a6e05a..c56b543 100644 (file)
       <Member Name="GetPathRoot(System.String)" />
       <Member Name="HasExtension(System.String)" />
       <Member Name="IsPathRooted(System.String)" />
+      <Member Name="GetRandomFileName" />
+      <Member Name="GetTempFileName" />
+      <Member Name="GetTempPath" />
     </Type>
     <Type Name="System.IO.PathTooLongException">
       <Member Name="#ctor" />
index 54b0afe..84fdf8a 100644 (file)
@@ -2413,26 +2413,29 @@ namespace Microsoft.Win32 {
 #if FEATURE_CORECLR
 #if FEATURE_PAL
         [DllImport(KERNEL32, EntryPoint = "PAL_Random")]
-        [ResourceExposure(ResourceScope.None)]
         internal extern static bool Random(bool bStrong,
                            [Out, MarshalAs(UnmanagedType.LPArray)] byte[] buffer, int length);
 #else
-        internal const int PROV_RSA_FULL = 1;
-        internal const int CRYPT_VERIFYCONTEXT = unchecked((int)0xF0000000);
+        private const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002;
 
-        [DllImport("advapi32", SetLastError = true, CharSet = CharSet.Unicode)]
-        [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern bool CryptAcquireContext([Out] out IntPtr phProv,
-                                                        string pszContainer,
-                                                        string pszProvider,
-                                                        int dwProvType,
-                                                        int dwFlags);
+        [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
+        private static extern uint BCryptGenRandom(IntPtr hAlgorithm, [In, Out] byte[] pbBuffer, int cbBuffer, int dwFlags);
 
-        [DllImport("advapi32", SetLastError = true)]
-        [return: MarshalAs(UnmanagedType.Bool)]
-        internal static extern bool CryptGenRandom(IntPtr hProv,
-                                                   int dwLen,
-                                                   [In, Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbBuffer);
+        internal static void Random(bool bStrong, byte[] buffer, int length)
+        {
+            uint status = BCryptGenRandom(IntPtr.Zero, buffer, length, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
+            if (status != STATUS_SUCCESS)
+            {
+                if (status == STATUS_NO_MEMORY)
+                {
+                    throw new OutOfMemoryException();
+                }
+                else
+                {
+                    throw new InvalidOperationException();
+                }
+            }
+        }
 #endif
 #endif
     }
index 49344c9..fa39fc8 100644 (file)
@@ -1804,12 +1804,7 @@ namespace System.Collections {
         }
 
         private const int bufferSize = 1024;
-#if !FEATURE_PAL
-#if FEATURE_CORECLR
-        private static IntPtr hCryptProv;
-#else
-        private static RandomNumberGenerator rng;
-#endif
+#if !FEATURE_CORECLR
 #endif
         private static byte[] data;
         private static int currentIndex = bufferSize;
@@ -1826,26 +1821,18 @@ namespace System.Collections {
                     {
                         data = new byte[bufferSize];
                         Contract.Assert(bufferSize % 8 == 0, "We increment our current index by 8, so our buffer size must be a multiple of 8");
-#if !FEATURE_PAL
-#if FEATURE_CORECLR
+#if !FEATURE_CORECLR
                         Microsoft.Win32.Win32Native.CryptAcquireContext(out hCryptProv, null, null, 
                             Microsoft.Win32.Win32Native.PROV_RSA_FULL, Microsoft.Win32.Win32Native.CRYPT_VERIFYCONTEXT);
-#else
-                        rng = RandomNumberGenerator.Create();
-#endif
 #endif
 
                     }
 
-#if FEATURE_PAL
-                    Microsoft.Win32.Win32Native.Random(true, data, data.Length);
-#else
 #if FEATURE_CORECLR
-                    Microsoft.Win32.Win32Native.CryptGenRandom(hCryptProv, data.Length, data);
+                    Microsoft.Win32.Win32Native.Random(true, data, data.Length);
 #else
                     rng.GetBytes(data);
 #endif
-#endif
                     currentIndex = 0;
                 }
 
index 0b9c320..f8f19ed 100644 (file)
@@ -906,7 +906,6 @@ namespace System.IO {
         
         }
 
-#if !FEATURE_CORECLR
         // Returns a cryptographically strong random 8.3 string that can be 
         // used as either a folder name or a file name.
 #if FEATURE_PAL
@@ -918,7 +917,7 @@ namespace System.IO {
             // This gives us exactly 8 chars. We want to avoid the 8.3 short name issue
             byte[] key = new byte[10];
 
-#if FEATURE_PAL
+#if FEATURE_CORECLR
             Win32Native.Random(true, key, key.Length);
 #else
             // RNGCryptoServiceProvider is disposable in post-Orcas desktop mscorlibs, but not in CoreCLR's
@@ -982,7 +981,6 @@ namespace System.IO {
             if (r==0) __Error.WinIOError();
             return sb.ToString();
         }
-#endif // FEATURE_CORECLR
 
         // Tests if a path includes a file extension. The result is
         // true if the characters that follow the last directory