Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ MarshalToString()

unsafe IntPtr System.Security.SecureString.MarshalToString ( bool globalAlloc,
bool unicode )
inlinepackage

Definition at line 354 of file SecureString.cs.

355 {
356 lock (_methodLock)
357 {
360 SafeBuffer bufferToRelease = null;
361 IntPtr intPtr = IntPtr.Zero;
362 int num = 0;
363 try
364 {
365 Span<char> span = AcquireSpan(ref bufferToRelease).Slice(0, _decryptedLength);
366 num = ((!unicode) ? Marshal.GetAnsiStringByteCount(span) : ((span.Length + 1) * 2));
367 intPtr = ((!globalAlloc) ? Marshal.AllocCoTaskMem(num) : Marshal.AllocHGlobal(num));
368 if (unicode)
369 {
370 Span<char> destination = new Span<char>((void*)intPtr, num / 2);
371 span.CopyTo(destination);
372 destination[destination.Length - 1] = '\0';
373 }
374 else
375 {
376 Marshal.GetAnsiStringBytes(span, new Span<byte>((void*)intPtr, num));
377 }
378 IntPtr result = intPtr;
379 intPtr = IntPtr.Zero;
380 return result;
381 }
382 finally
383 {
384 if (intPtr != IntPtr.Zero)
385 {
386 new Span<byte>((void*)intPtr, num).Clear();
387 if (globalAlloc)
388 {
389 Marshal.FreeHGlobal(intPtr);
390 }
391 else
392 {
393 Marshal.FreeCoTaskMem(intPtr);
394 }
395 }
397 bufferToRelease?.DangerousRelease();
398 }
399 }
400 }
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static void FreeCoTaskMem(IntPtr ptr)
Definition Marshal.cs:1712
static unsafe int GetAnsiStringByteCount(ReadOnlySpan< char > chars)
Definition Marshal.cs:1625
static IntPtr AllocCoTaskMem(int cb)
Definition Marshal.cs:1702
static unsafe void GetAnsiStringBytes(ReadOnlySpan< char > chars, Span< byte > bytes)
Definition Marshal.cs:1646
static IntPtr AllocHGlobal(int cb)
Definition Marshal.cs:625
unsafe Span< char > AcquireSpan(ref SafeBuffer bufferToRelease)
Span< T > Slice(int start)
Definition Span.cs:271

References System.Security.SecureString._decryptedLength, System.Security.SecureString._methodLock, System.Security.SecureString.AcquireSpan(), System.Runtime.InteropServices.Marshal.AllocCoTaskMem(), System.Runtime.InteropServices.Marshal.AllocHGlobal(), System.Span< T >.Clear(), System.Span< T >.CopyTo(), System.Runtime.InteropServices.SafeHandle.DangerousRelease(), System.destination, System.Security.SecureString.EnsureNotDisposed(), System.Runtime.InteropServices.Marshal.FreeCoTaskMem(), System.Runtime.InteropServices.Marshal.FreeHGlobal(), System.Runtime.InteropServices.Marshal.GetAnsiStringByteCount(), System.Runtime.InteropServices.Marshal.GetAnsiStringBytes(), System.Security.SecureString.ProtectMemory(), System.Span< T >.Slice(), System.Security.SecureString.UnprotectMemory(), and System.IntPtr.Zero.