public void SetAt(int index, char c) { lock (_methodLock) { if (index < 0 || index >= _decryptedLength) { throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexString); } EnsureNotDisposed(); EnsureNotReadOnly(); Debug.Assert(_buffer != null); SafeBuffer?bufferToRelease = null; try { UnprotectMemory(); Span <char> span = AcquireSpan(ref bufferToRelease); span[index] = c; } finally { ProtectMemory(); bufferToRelease?.DangerousRelease(); } } }
public void AppendChar(char c) { lock (_methodLock) { EnsureNotDisposed(); EnsureNotReadOnly(); Debug.Assert(_buffer != null); SafeBuffer?bufferToRelease = null; try { UnprotectMemory(); EnsureCapacity(_decryptedLength + 1); Span <char> span = AcquireSpan(ref bufferToRelease); span[_decryptedLength] = c; _decryptedLength++; } finally { ProtectMemory(); bufferToRelease?.DangerousRelease(); } } }
private void Initialize(ReadOnlySpan <char> value) { _buffer = UnmanagedBuffer.Allocate(GetAlignedByteSize(value.Length)); _decryptedLength = value.Length; SafeBuffer?bufferToRelease = null; try { Span <char> span = AcquireSpan(ref bufferToRelease); value.CopyTo(span); } finally { ProtectMemory(); bufferToRelease?.DangerousRelease(); } }
// clears the current contents. Only available if writable public void Clear() { lock (_methodLock) { EnsureNotDisposed(); EnsureNotReadOnly(); Debug.Assert(_buffer != null); _decryptedLength = 0; SafeBuffer?bufferToRelease = null; try { Span <char> span = AcquireSpan(ref bufferToRelease); span.Clear(); } finally { bufferToRelease?.DangerousRelease(); } } }
internal unsafe IntPtr MarshalToBSTR() { lock (_methodLock) { EnsureNotDisposed(); UnprotectMemory(); SafeBuffer?bufferToRelease = null; IntPtr ptr = IntPtr.Zero; int length = 0; try { Span <char> span = AcquireSpan(ref bufferToRelease); length = _decryptedLength; ptr = Marshal.AllocBSTR(length); span.Slice(0, length).CopyTo(new Span <char>((void *)ptr, length)); IntPtr result = ptr; ptr = IntPtr.Zero; return(result); } finally { // If we failed for any reason, free the new buffer if (ptr != IntPtr.Zero) { new Span <char>((void *)ptr, length).Clear(); Marshal.FreeBSTR(ptr); } ProtectMemory(); bufferToRelease?.DangerousRelease(); } } }
public override bool Release() { _buffer.DangerousRelease(); return(_buffer.IsClosed); }
internal unsafe IntPtr MarshalToString(bool globalAlloc, bool unicode) { lock (_methodLock) { EnsureNotDisposed(); UnprotectMemory(); SafeBuffer?bufferToRelease = null; IntPtr ptr = IntPtr.Zero; int byteLength = 0; try { Span <char> span = AcquireSpan(ref bufferToRelease).Slice(0, _decryptedLength); if (unicode) { byteLength = (span.Length + 1) * sizeof(char); } else { byteLength = Marshal.GetAnsiStringByteCount(span); } if (globalAlloc) { ptr = Marshal.AllocHGlobal(byteLength); } else { ptr = Marshal.AllocCoTaskMem(byteLength); } if (unicode) { Span <char> resultSpan = new Span <char>((void *)ptr, byteLength / sizeof(char)); span.CopyTo(resultSpan); resultSpan[resultSpan.Length - 1] = '\0'; } else { Marshal.GetAnsiStringBytes(span, new Span <byte>((void *)ptr, byteLength)); } IntPtr result = ptr; ptr = IntPtr.Zero; return(result); } finally { // If we failed for any reason, free the new buffer if (ptr != IntPtr.Zero) { new Span <byte>((void *)ptr, byteLength).Clear(); if (globalAlloc) { Marshal.FreeHGlobal(ptr); } else { Marshal.FreeCoTaskMem(ptr); } } ProtectMemory(); bufferToRelease?.DangerousRelease(); } } }