Esempio n. 1
0
        // Reads 'count' structs of type T from unmanaged memory, into 'array' starting at 'offset'.
        // Note: this method is not safe, since it overwrites the contents of structures, it can
        // be used to modify the private members of a struct.
        public int ReadArray <T>(long position, T[] array, int offset, int count) where T : struct
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array), SR.ArgumentNull_Buffer);
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (array.Length - offset < count)
            {
                throw new ArgumentException(SR.Argument_InvalidOffLen);
            }
            if (!_isOpen)
            {
                throw new ObjectDisposedException(nameof(UnmanagedMemoryAccessor), SR.ObjectDisposed_ViewAccessorClosed);
            }
            if (!_canRead)
            {
                throw new NotSupportedException(SR.NotSupported_Reading);
            }
            if (position < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_NeedNonNegNum);
            }

            uint sizeOfT = SafeBuffer.AlignedSizeOf <T>();

            // only check position and ask for fewer Ts if count is too big
            if (position >= _capacity)
            {
                throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_PositionLessThanCapacityRequired);
            }

            int  n         = count;
            long spaceLeft = _capacity - position;

            if (spaceLeft < 0)
            {
                n = 0;
            }
            else
            {
                ulong spaceNeeded = (ulong)(sizeOfT * count);
                if ((ulong)spaceLeft < spaceNeeded)
                {
                    n = (int)(spaceLeft / sizeOfT);
                }
            }

            _buffer.ReadArray <T>((ulong)(_offset + position), array, offset, n);

            return(n);
        }
Esempio n. 2
0
        private static string[] ReadStrings(SafeBuffer buffer, int count)
        {
            IntPtr[] ptrs = new IntPtr[count];

            buffer.ReadArray(0, ptrs, 0, count);

            return(ptrs.Select(p => GetString(p)).ToArray());
        }
Esempio n. 3
0
 private static AuthZAccessCheckResult[] CreateResult(int count, SafeBuffer error, SafeBuffer access,
                                                      ObjectTypeEntry[] object_types, NtType type)
 {
     int[] error_array = new int[count];
     error.ReadArray(0, error_array, 0, count);
     AccessMask[] access_array = new AccessMask[count];
     access.ReadArray(0, access_array, 0, count);
     return(Enumerable.Range(0, count).Select(i => new AuthZAccessCheckResult(type, (Win32Error)error_array[i],
                                                                              access_array[i], object_types?[i])).ToArray());
 }
Esempio n. 4
0
        public int ReadArray <T> (long position, T [] array, int offset, int count) where T : struct
        {
            if (position < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            long left  = capacity - position;
            var  slots = (int)(left / Marshal.SizeOf(typeof(T)));

            buffer.ReadArray((ulong)position, array, offset, slots);
            return(slots);
        }
Esempio n. 5
0
        /// <summary>
        /// Extends ReadArray<T> so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// safebuffer.ReadArray<T>(byteOffset, array);
        /// </example>
        /// </summary>
        public static void ReadArray <T>(this SafeBuffer safebuffer, UInt64 byteOffset, T[] array) where T : struct
        {
            if (safebuffer == null)
            {
                throw new ArgumentNullException("safebuffer");
            }

            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            safebuffer.ReadArray(byteOffset, array, 0, array.Length);
        }
Esempio n. 6
0
        /// <summary>
        // Reads 'count' structs of type T from unmanaged memory, into 'array' starting at 'offset'.
        // Note: this method is not safe, since it overwrites the contents of structures, it can
        // be used to modify the private members of a struct.
        /// </summary>
        public int ReadArray <T>(Int64 position, T[] array, Int32 offset, Int32 count) where T : struct
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array), SR.ArgumentNull_Buffer);
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (array.Length - offset < count)
            {
                throw new ArgumentException(SR.Argument_InvalidOffLen);
            }
            Contract.EndContractBlock();

            EnsureSafeToRead(position, 0);

            // ask for fewer Ts if count is too big

            UInt32 sizeOfT = SafeBuffer.AlignedSizeOf <T>();

            int  n         = count;
            long spaceLeft = _capacity - position;

            if (spaceLeft < 0)
            {
                n = 0;
            }
            else
            {
                ulong spaceNeeded = (ulong)(sizeOfT * count);
                if ((ulong)spaceLeft < spaceNeeded)
                {
                    n = (int)(spaceLeft / sizeOfT);
                }
            }

            _buffer.ReadArray <T>((UInt64)(_offset + position), array, offset, n);

            return(n);
        }
Esempio n. 7
0
        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());
            }
        }
Esempio n. 8
0
        [System.Security.SecurityCritical]  // auto-generated_required
        public int ReadArray <T>(Int64 position, T[] array, Int32 offset, Int32 count) where T : struct
        {
            if (array == null)
            {
                throw new ArgumentNullException("array", "Buffer cannot be null.");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (array.Length - offset < count)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_OffsetAndLengthOutOfBounds"));
            }
            Contract.EndContractBlock();
            if (!CanRead)
            {
                if (!_isOpen)
                {
                    throw new ObjectDisposedException("UnmanagedMemoryAccessor", Environment.GetResourceString("ObjectDisposed_ViewAccessorClosed"));
                }
                else
                {
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_Reading"));
                }
            }
            if (position < 0)
            {
                throw new ArgumentOutOfRangeException("position", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }

            UInt32 sizeOfT = Marshal.AlignedSizeOf <T>();

            // only check position and ask for fewer Ts if count is too big
            if (position >= _capacity)
            {
                throw new ArgumentOutOfRangeException("position", Environment.GetResourceString("ArgumentOutOfRange_PositionLessThanCapacityRequired"));
            }

            int n          = count;
            long spaceLeft = _capacity - position;

            if (spaceLeft < 0)
            {
                n = 0;
            }
            else
            {
                ulong spaceNeeded = (ulong)(sizeOfT * count);
                if ((ulong)spaceLeft < spaceNeeded)
                {
                    n = (int)(spaceLeft / sizeOfT);
                }
            }

            _buffer.ReadArray <T>((UInt64)(_offset + position), array, offset, n);

            return(n);
        }
Esempio n. 9
0
 /// <summary>
 /// Read bytes from buffer.
 /// </summary>
 /// <param name="buffer">The buffer to read from.</param>
 /// <param name="byte_offset">The byte offset to read from.</param>
 /// <param name="count">The number of bytes to read.</param>
 /// <returns>The byte array.</returns>
 public static byte[] ReadBytes(SafeBuffer buffer, ulong byte_offset, int count)
 {
     byte[] ret = new byte[count];
     buffer.ReadArray(byte_offset, ret, 0, count);
     return(ret);
 }
Esempio n. 10
0
 /// <summary>
 /// Read a Unicode string string with length.
 /// </summary>
 /// <param name="buffer">The buffer to read from.</param>
 /// <param name="count">The number of characters to read.</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 ReadUnicodeString(SafeBuffer buffer, ulong byte_offset, int count)
 {
     char[] ret = new char[count];
     buffer.ReadArray(byte_offset, ret, 0, count);
     return(new string(ret));
 }
 /// <summary>
 /// Read a char array with length.
 /// </summary>
 /// <param name="buffer">The buffer to read from.</param>
 /// <param name="count">The number of characters to read.</param>
 /// <param name="byte_offset">The byte offset to read from.</param>
 /// <returns>The chars read from the buffer</returns>
 public static char[] ReadCharArray(SafeBuffer buffer, ulong byte_offset, int count)
 {
     char[] ret = new char[count];
     buffer.ReadArray(byte_offset, ret, 0, count);
     return(ret);
 }