// Reads a struct of type T from unmanaged memory, into the reference pointed to by ref value. // Note: this method is not safe, since it overwrites the contents of a structure, it can be // used to modify the private members of a struct. // This method is most performant when used with medium to large sized structs // (larger than 8 bytes -- though this is number is JIT and architecture dependent). As // such, it is best to use the ReadXXX methods for small standard types such as ints, longs, // bools, etc. public void Read <T>(long position, out T structure) where T : struct { if (position < 0) { throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_NeedNonNegNum); } if (!_isOpen) { throw new ObjectDisposedException(nameof(UnmanagedMemoryAccessor), SR.ObjectDisposed_ViewAccessorClosed); } if (!_canRead) { throw new NotSupportedException(SR.NotSupported_Reading); } uint sizeOfT = SafeBuffer.SizeOf <T>(); if (position > _capacity - sizeOfT) { if (position >= _capacity) { throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_PositionLessThanCapacityRequired); } else { throw new ArgumentException(SR.Format(SR.Argument_NotEnoughBytesToRead, typeof(T)), nameof(position)); } } structure = _buffer.Read <T>((ulong)(_offset + position)); }
[System.Security.SecurityCritical] // auto-generated_required public void Read <T>(Int64 position, out T structure) where T : struct { if (position < 0) { throw new ArgumentOutOfRangeException("position", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } Contract.EndContractBlock(); if (!_isOpen) { throw new ObjectDisposedException("UnmanagedMemoryAccessor", Environment.GetResourceString("ObjectDisposed_ViewAccessorClosed")); } if (!CanRead) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_Reading")); } UInt32 sizeOfT = Marshal.SizeOfType(typeof(T)); if (position > _capacity - sizeOfT) { if (position >= _capacity) { throw new ArgumentOutOfRangeException("position", Environment.GetResourceString("ArgumentOutOfRange_PositionLessThanCapacityRequired")); } else { throw new ArgumentException(Environment.GetResourceString("Argument_NotEnoughBytesToRead", typeof(T).FullName), "position"); } } structure = _buffer.Read <T>((UInt64)(_offset + position)); }
// Reads a struct of type T from unmanaged memory, into the reference pointed to by ref value. // Note: this method is not safe, since it overwrites the contents of a structure, it can be // used to modify the private members of a struct. Furthermore, using this with a struct that // contains reference members will most likely cause the runtime to AV. Note, that despite // various checks made by the C++ code used by Marshal.PtrToStructure, Marshal.PtrToStructure // will still overwrite privates and will also crash the runtime when used with structs // containing reference members. For this reason, I am sticking an UnmanagedCode requirement // on this method to match Marshal.PtrToStructure. // Alos note that this method is most performant when used with medium to large sized structs // (larger than 8 bytes -- though this is number is JIT and architecture dependent). As // such, it is best to use the ReadXXX methods for small standard types such as ints, longs, // bools, etc. public void Read <T>(Int64 position, out T structure) where T : struct { if (position < 0) { throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_NeedNonNegNum); } if (!_isOpen) { throw new ObjectDisposedException("UnmanagedMemoryAccessor", SR.ObjectDisposed_ViewAccessorClosed); } if (!CanRead) { throw new NotSupportedException(SR.NotSupported_Reading); } UInt32 sizeOfT = Marshal.SizeOfType(typeof(T)); if (position > _capacity - sizeOfT) { if (position >= _capacity) { throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_PositionLessThanCapacityRequired); } else { throw new ArgumentException(SR.Format(SR.Argument_NotEnoughBytesToRead, typeof(T).FullName), nameof(position)); } } structure = _buffer.Read <T>((UInt64)(_offset + position)); }
/// <summary> // Reads a struct of type T from unmanaged memory, into the reference pointed to by ref value. // Note: this method is not safe, since it overwrites the contents of a structure, it can be // used to modify the private members of a struct. // This method is most performant when used with medium to large sized structs // (larger than 8 bytes -- though this is number is JIT and architecture dependent). As // such, it is best to use the ReadXXX methods for small standard types such as ints, longs, // bools, etc. /// </summary> public void Read <T>(Int64 position, out T structure) where T : struct { int sizeOfType = Unsafe.SizeOf <T>(); EnsureSafeToRead(position, sizeOfType); structure = _buffer.Read <T>((UInt64)(_offset + position)); }
/// <summary> /// Read a NUL terminated string for the byte offset. /// </summary> /// <param name="buffer">The buffer to read from.</param> /// <param name="byte_offset">The byte offset to read from.</param> /// <returns>The string read from the buffer without the NUL terminator</returns> public static string ReadNulTerminatedUnicodeString(SafeBuffer buffer, ulong byte_offset) { List <char> chars = new List <char>(); while (byte_offset < buffer.ByteLength) { char c = buffer.Read <char>(byte_offset); if (c == 0) { break; } chars.Add(c); byte_offset += 2; } return(new string(chars.ToArray())); }
/// <summary> /// Read a NUL terminated byte string for the byte offset. /// </summary> /// <param name="buffer">The buffer to read from.</param> /// <param name="byte_offset">The byte offset to read from.</param> /// <param name="encoding">Text encoding for the string.</param> /// <returns>The string read from the buffer without the NUL terminator</returns> public static string ReadNulTerminatedAnsiString(SafeBuffer buffer, ulong byte_offset, Encoding encoding) { List <byte> chars = new List <byte>(); while (byte_offset < buffer.ByteLength) { byte b = buffer.Read <byte>(byte_offset); if (b == 0) { break; } chars.Add(b); byte_offset++; } return(encoding.GetString(chars.ToArray())); }
public static IEnumerable <GroupSecurityIdentifierInformation> GetAllGroups(this WindowsIdentity windowsIdentity) { // Get the raw group information, which is a buffer starting with a TOKEN_GROUPS structure using (SafeBuffer groupInformation = Win32Native.GetTokenInformation(windowsIdentity.GetSafeTokenHandle(), Win32Native.TokenInformationClass.TokenGroups)) { // First, read the TOKEN_GROUPS header out of the buffer Win32Native.TOKEN_GROUPS tokenGroups = groupInformation.Read <Win32Native.TOKEN_GROUPS>(0); // The TOKEN_GROUPS.Group property is an array of SID_AND_ATTRIBUTES structures. Grab that // array of data. // Iterate that array, and create managed GroupSecurityIdentifierInformation types for them. Win32Native.SID_AND_ATTRIBUTES[] sids = groupInformation.ReadArray <Win32Native.SID_AND_ATTRIBUTES>(Marshal.OffsetOf(typeof(Win32Native.TOKEN_GROUPS), "Groups").ToInt32(), tokenGroups.GroupCount); var sidInfo = from sid in sids select new GroupSecurityIdentifierInformation(new SecurityIdentifier(sid.Sid), (GroupSecurityIdentifierAttributes)sid.Attributes); // Force the conversion to GroupSecurityIdentifierInformation now, rather than lazily since // we need to read the data out of the buffer which will be disposed of after this method exits. return(sidInfo.ToArray()); } }
public byte ReadByte(long position) { if (!canread) { throw new NotSupportedException(); } if (buffer == null) { throw new ObjectDisposedException("buffer"); } if (position < 0) { throw new ArgumentOutOfRangeException(); } return(buffer.Read <byte> ((ulong)position)); }