You declare that Marshal.Copy will no longer be supported, however, in the Marshal documentation for the class, I cannot find any signs for this.
Quite the contrary, the class is available for the following framework versions: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
One possible implementation of a utility function based on this method would be:
public static void CopyFromPtrToPtr(IntPtr src, uint srcLen, IntPtr dst, uint dstLen) { var buffer = new byte[srcLen]; Marshal.Copy(src, buffer, 0, buffer.Length); Marshal.Copy(buffer, 0, dst, (int)Math.Min(buffer.Length, dstLen)); }
However, it is possible that copying bytes from IntPtr to byte[] and vice versa negates any possible performance gain when fixing insecure memory and copying it in a loop.
Depending on how portable the code should be, you can also use P / Invoke to actually use the memcpy method. This should work well on Windows 2000 and later (all systems on which the Microsoft Visual C runtime library is installed).
[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] static extern IntPtr memcpy(IntPtr dst, IntPtr src, UIntPtr count);
Edit: The second approach is not suitable for portable class libraries, however the first will be.