Revert "UTF8 Marshaling APIs (#3736)"
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
index f1d0829ff99fcfd89b8a6cc7fb9d4503d163276c..2def9ddba50828744ab6ae26d6e37d1870df033b 100644 (file)
@@ -38,7 +38,6 @@ using System.Security;
 using System.Reflection;
 using System.Threading;
 using System.Runtime.InteropServices.ComTypes;
-using System.Text;
 
 using System.Runtime.ConstrainedExecution;
 #if !FULL_AOT_RUNTIME
@@ -77,9 +76,6 @@ namespace System.Runtime.InteropServices
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr AllocCoTaskMem (int cb);
-               
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public extern static IntPtr AllocCoTaskMemSize (UIntPtr sizet);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
@@ -282,12 +278,6 @@ namespace System.Runtime.InteropServices
                        FreeCoTaskMem (s);
                }
 
-               public static void ZeroFreeCoTaskMemUTF8 (IntPtr s)
-               {
-                       ClearAnsi (s);
-                       FreeCoTaskMem (s);
-               }
-               
                public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
                {
                        ClearAnsi (s);
@@ -761,16 +751,6 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static string PtrToStringAnsi (IntPtr ptr, int len);
 
-               public static string PtrToStringUTF8 (IntPtr ptr)
-               {
-                       return PtrToStringAnsi (ptr);
-               }
-               
-               public static string PtrToStringUTF8 (IntPtr ptr, int byteLen)
-               {
-                       return PtrToStringAnsi (ptr, byteLen);
-               }
-               
                public static string PtrToStringAuto (IntPtr ptr)
                {
                        return SystemDefaultCharSize == 2
@@ -1073,9 +1053,23 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToBSTR (string s);
 
+               //
+               // I believe this is wrong, because in Mono and in P/Invoke
+               // we treat "Ansi" conversions as UTF-8 conversions, while
+               // this one does not do this
+               //
                public static IntPtr StringToCoTaskMemAnsi (string s)
                {
-                       return StringToAllocatedMemoryUTF8 (s);
+                       int length = s.Length + 1;
+                       IntPtr ctm = AllocCoTaskMem (length);
+
+                       byte[] asBytes = new byte[length];
+                       for (int i = 0; i < s.Length; i++)
+                               asBytes[i] = (byte)s[i];
+                       asBytes[s.Length] = 0;
+
+                       copy_to_unmanaged (asBytes, 0, ctm, length);
+                       return ctm;
                }
 
                public static IntPtr StringToCoTaskMemAuto (string s)
@@ -1100,29 +1094,6 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToHGlobalAnsi (string s);
 
-               unsafe public static IntPtr StringToAllocatedMemoryUTF8(String s)
-               {
-                       const int MAX_UTF8_CHAR_SIZE = 3;
-                       if (s == null)
-                               return IntPtr.Zero;
-
-                       int nb = (s.Length + 1) * MAX_UTF8_CHAR_SIZE;
-
-                       // Overflow checking
-                       if (nb < s.Length)
-                               throw new ArgumentOutOfRangeException("s");
-                       
-                       IntPtr pMem = AllocCoTaskMemSize(new UIntPtr((uint)nb +1));
-                       
-                       if (pMem == IntPtr.Zero)
-                               throw new OutOfMemoryException();
-
-                       byte* pbMem = (byte*)pMem;
-                       int nbWritten = s.GetBytesFromEncoding(pbMem, nb, Encoding.UTF8);
-                       pbMem[nbWritten] = 0;
-                       return pMem;
-               }
-               
                public static IntPtr StringToHGlobalAuto (string s)
                {
                        return SystemDefaultCharSize == 2