SecBufferDesc.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Runtime.InteropServices;
  10. namespace SMBLibrary.Win32.Security
  11. {
  12. [StructLayout(LayoutKind.Sequential)]
  13. public struct SecBufferDesc : IDisposable
  14. {
  15. public uint ulVersion;
  16. public uint cBuffers; // Indicates the number of SecBuffer structures in the pBuffers array.
  17. public IntPtr pBuffers; // Pointer to an array of SecBuffer structures.
  18. public SecBufferDesc(SecBuffer buffer) : this(new SecBuffer[] { buffer })
  19. {
  20. }
  21. public SecBufferDesc(SecBuffer[] buffers)
  22. {
  23. int secBufferSize = Marshal.SizeOf(typeof(SecBuffer));
  24. ulVersion = (uint)SecBufferType.SECBUFFER_VERSION;
  25. cBuffers = (uint)buffers.Length;
  26. pBuffers = Marshal.AllocHGlobal(buffers.Length * secBufferSize);
  27. IntPtr currentBuffer = pBuffers;
  28. for (int index = 0; index < buffers.Length; index++)
  29. {
  30. Marshal.StructureToPtr(buffers[index], currentBuffer, false);
  31. currentBuffer = new IntPtr(currentBuffer.ToInt64() + secBufferSize);
  32. }
  33. }
  34. public void Dispose()
  35. {
  36. if (pBuffers != IntPtr.Zero)
  37. {
  38. Marshal.FreeHGlobal(pBuffers);
  39. pBuffers = IntPtr.Zero;
  40. }
  41. }
  42. }
  43. }