12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- namespace SMBLibrary.Win32.Security
- {
- public enum SecBufferType : uint
- {
- SECBUFFER_EMPTY = 0,
- SECBUFFER_DATA = 1,
- SECBUFFER_TOKEN = 2
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct SecBuffer : IDisposable
- {
- public uint cbBuffer;
- public uint BufferType;
- public IntPtr pvBuffer;
- public SecBuffer(int bufferSize)
- {
- cbBuffer = (uint)bufferSize;
- BufferType = (uint)SecBufferType.SECBUFFER_TOKEN;
- pvBuffer = Marshal.AllocHGlobal(bufferSize);
- }
- public SecBuffer(byte[] secBufferBytes)
- {
- cbBuffer = (uint)secBufferBytes.Length;
- BufferType = (uint)SecBufferType.SECBUFFER_TOKEN;
- pvBuffer = Marshal.AllocHGlobal(secBufferBytes.Length);
- Marshal.Copy(secBufferBytes, 0, pvBuffer, secBufferBytes.Length);
- }
- public SecBuffer(byte[] secBufferBytes, SecBufferType bufferType)
- {
- cbBuffer = (uint)secBufferBytes.Length;
- BufferType = (uint)bufferType;
- pvBuffer = Marshal.AllocHGlobal(secBufferBytes.Length);
- Marshal.Copy(secBufferBytes, 0, pvBuffer, secBufferBytes.Length);
- }
- public void Dispose()
- {
- if (pvBuffer != IntPtr.Zero)
- {
- Marshal.FreeHGlobal(pvBuffer);
- pvBuffer = IntPtr.Zero;
- }
- }
- public byte[] GetBufferBytes()
- {
- byte[] buffer = null;
- if (cbBuffer > 0)
- {
- buffer = new byte[cbBuffer];
- Marshal.Copy(pvBuffer, buffer, 0, (int)cbBuffer);
- }
- return buffer;
- }
- }
- }
|