| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Runtime.InteropServices; |
| | 4 | | using System.Security.Principal; |
| | 5 | | using Microsoft.Win32.SafeHandles; |
| | 6 | |
|
| | 7 | | namespace SharpHoundRPC.Handles |
| | 8 | | { |
| | 9 | | public abstract class BasePointer : SafeHandleZeroOrMinusOneIsInvalid |
| | 10 | | { |
| 0 | 11 | | protected BasePointer() : base(true) |
| 0 | 12 | | { |
| 0 | 13 | | } |
| | 14 | |
|
| 1 | 15 | | protected BasePointer(bool ownsHandle) : base(ownsHandle) |
| 1 | 16 | | { |
| 1 | 17 | | } |
| | 18 | |
|
| 0 | 19 | | protected BasePointer(IntPtr handle) : base(true) |
| 0 | 20 | | { |
| 0 | 21 | | SetHandle(handle); |
| 0 | 22 | | } |
| | 23 | |
|
| 0 | 24 | | protected BasePointer(IntPtr handle, bool ownsHandle) : base(ownsHandle) |
| 0 | 25 | | { |
| 0 | 26 | | SetHandle(handle); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public IEnumerable<T> GetEnumerable<T>(int count) |
| 0 | 30 | | { |
| 0 | 31 | | for (var i = 0; i < count; i++) |
| 0 | 32 | | if (typeof(T) == typeof(int)) |
| 0 | 33 | | yield return (T) (object) ReadInt32(i); |
| 0 | 34 | | else if (typeof(T) == typeof(long)) |
| 0 | 35 | | yield return (T) (object) ReadInt64(i); |
| 0 | 36 | | else if (typeof(T) == typeof(SecurityIdentifier)) |
| 0 | 37 | | yield return (T) (object) new SecurityIdentifier(ReadIntPtr(i)); |
| | 38 | | else |
| 0 | 39 | | yield return Marshal.PtrToStructure<T>(handle + Marshal.SizeOf<T>() * i); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public T GetData<T>() |
| 0 | 43 | | { |
| 0 | 44 | | if (typeof(T) == typeof(int)) return (T) (object) ReadInt32(); |
| | 45 | |
|
| 0 | 46 | | if (typeof(T) == typeof(long)) return (T) (object) ReadInt64(); |
| | 47 | |
|
| 0 | 48 | | if (typeof(T) == typeof(SecurityIdentifier)) return (T) (object) new SecurityIdentifier(handle); |
| | 49 | |
|
| 0 | 50 | | return Marshal.PtrToStructure<T>(handle); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | private int ReadInt32(int offset = 0) |
| 0 | 54 | | { |
| 0 | 55 | | return Marshal.ReadInt32(handle + offset * Marshal.SizeOf<int>()); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | private long ReadInt64(int offset = 0) |
| 0 | 59 | | { |
| 0 | 60 | | return Marshal.ReadInt64(handle + offset * Marshal.SizeOf<long>()); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | private IntPtr ReadIntPtr(int offset = 0) |
| 0 | 64 | | { |
| 0 | 65 | | return Marshal.ReadIntPtr(handle + offset * Marshal.SizeOf<IntPtr>()); |
| 0 | 66 | | } |
| | 67 | | } |
| | 68 | | } |