Esempio n. 1
0
        [System.Security.SecurityCritical]  // auto-generated
        private void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access, bool skipSecurityCheck)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length", SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (buffer.ByteLength < (ulong)(offset + length))
            {
                throw new ArgumentException(SR.Argument_InvalidSafeBufferOffLen);
            }
            if (access < FileAccess.Read || access > FileAccess.ReadWrite)
            {
                throw new ArgumentOutOfRangeException("access");
            }
            Contract.EndContractBlock();

            if (_isOpen)
            {
                throw new InvalidOperationException(SR.InvalidOperation_CalledTwice);
            }

            // check for wraparound
            unsafe
            {
                byte* pointer = null;

                try
                {
                    buffer.AcquirePointer(ref pointer);
                    if ((pointer + offset + length) < pointer)
                    {
                        throw new ArgumentException(SR.ArgumentOutOfRange_UnmanagedMemStreamWrapAround);
                    }
                }
                finally
                {
                    if (pointer != null)
                    {
                        buffer.ReleasePointer();
                    }
                }
            }

            _offset = offset;
            _buffer = buffer;
            _length = length;
            _capacity = length;
            _access = access;
            _isOpen = true;
        }
Esempio n. 2
0
        internal unsafe static byte* AcquirePointer(object accessor, out SafeBuffer safeBuffer)
        {
            Debug.Assert(s_lazyIsAvailable.GetValueOrDefault());

            safeBuffer = (SafeBuffer)s_lazySafeMemoryMappedViewHandle.GetValue(accessor);

            byte* ptr = null;
            safeBuffer.AcquirePointer(ref ptr);

            try
            {
                long offset;
                if (s_lazyPointerOffset != null)
                {
                    offset = (long)s_lazyPointerOffset.GetValue(accessor);
                }
                else
                {
                    object internalView = s_lazyInternalViewField.GetValue(accessor);
                    offset = (long)s_lazyInternalPointerOffset.GetValue(internalView);
                }

                return ptr + offset;
            }
            catch (MemberAccessException)
            {
                s_lazyIsAvailable = false;
                return null;
            }
            catch (InvalidOperationException)
            {
                // thrown when accessing unapproved API in a Windows Store app
                s_lazyIsAvailable = false;
                return null;
            }
            catch (TargetInvocationException ex)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                throw;
            }
        }
        [System.Security.SecuritySafeCritical]  // auto-generated
        protected void Initialize(SafeBuffer buffer, Int64 offset, Int64 capacity, FileAccess access)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(capacity), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (buffer.ByteLength < (UInt64)(offset + capacity))
            {
                throw new ArgumentException(SR.Argument_OffsetAndCapacityOutOfBounds);
            }
            if (access < FileAccess.Read || access > FileAccess.ReadWrite)
            {
                throw new ArgumentOutOfRangeException(nameof(access));
            }
            Contract.EndContractBlock();

            if (_isOpen)
            {
                throw new InvalidOperationException(SR.InvalidOperation_CalledTwice);
            }

            unsafe
            {
                byte* pointer = null;

                try
                {
                    buffer.AcquirePointer(ref pointer);
                    if (((byte*)((Int64)pointer + offset + capacity)) < pointer)
                    {
                        throw new ArgumentException(SR.Argument_UnmanagedMemAccessorWrapAround);
                    }
                }
                finally
                {
                    if (pointer != null)
                    {
                        buffer.ReleasePointer();
                    }
                }
            }

            _offset = offset;
            _buffer = buffer;
            _capacity = capacity;
            _access = access;
            _isOpen = true;
            _canRead = (_access & FileAccess.Read) != 0;
            _canWrite = (_access & FileAccess.Write) != 0;
        }
        [System.Security.SecurityCritical]  // auto-generated
        internal void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access, bool skipSecurityCheck) {
            if (buffer == null) {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0) {
                throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (length < 0) {
                throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (buffer.ByteLength < (ulong)(offset + length)) {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSafeBufferOffLen"));
            }
            if (access < FileAccess.Read || access > FileAccess.ReadWrite) {
                throw new ArgumentOutOfRangeException("access");
            }
            Contract.EndContractBlock();

            if (_isOpen) {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CalledTwice"));
            }
            if (!skipSecurityCheck) {
#pragma warning disable 618
                new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
#pragma warning restore 618
            }

            // check for wraparound
            unsafe {
                byte* pointer = null;
                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    buffer.AcquirePointer(ref pointer);
                    if ( (pointer + offset + length) < pointer) {
                        throw new ArgumentException(Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamWrapAround"));
                    }
                }
                finally {
                    if (pointer != null) {
                        buffer.ReleasePointer();
                    }
                }
            }

            _offset = offset;
            _buffer = buffer;
            _length = length;
            _capacity = length;
            _access = access;
            _isOpen = true;
        }
Esempio n. 5
0
        internal unsafe static byte* AcquirePointer(object accessor, out SafeBuffer safeBuffer)
        {
            DebugCorlib.Assert(lazyIsAvailable.GetValueOrDefault());

            safeBuffer = (SafeBuffer)lazySafeMemoryMappedViewHandle.GetValue(accessor);

            byte* ptr = null;
            safeBuffer.AcquirePointer(ref ptr);

            long offset;
            if (lazyPointerOffset != null)
            {
                offset = (long)lazyPointerOffset.GetValue(accessor);
            }
            else
            {
                object internalView = lazyInternalViewField.GetValue(accessor);
                offset = (long)lazyInternalPointerOffset.GetValue(internalView);
            }

            return ptr + offset;
        }
 internal unsafe void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access, bool skipSecurityCheck)
 {
     if (buffer == null)
     {
         throw new ArgumentNullException("buffer");
     }
     if (offset < 0L)
     {
         throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
     }
     if (length < 0L)
     {
         throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
     }
     if (buffer.ByteLength < (offset + length))
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSafeBufferOffLen"));
     }
     if ((access < FileAccess.Read) || (access > FileAccess.ReadWrite))
     {
         throw new ArgumentOutOfRangeException("access");
     }
     if (this._isOpen)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CalledTwice"));
     }
     if (!skipSecurityCheck)
     {
         new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
     }
     byte* pointer = null;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
         buffer.AcquirePointer(ref pointer);
         if (((pointer + offset) + length) < pointer)
         {
             throw new ArgumentException(Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamWrapAround"));
         }
     }
     finally
     {
         if (pointer != null)
         {
             buffer.ReleasePointer();
         }
     }
     this._offset = offset;
     this._buffer = buffer;
     this._length = length;
     this._capacity = length;
     this._access = access;
     this._isOpen = true;
 }
Esempio n. 7
0
        public StreamHeaderRef(MemoryMappedViewAccessor accessor)
        {
            Accessor = accessor;
            Buffer = accessor.GetSafeBuffer();

            byte* temp = null;
            Buffer.AcquirePointer(ref temp);

            Ptr = (StreamHeader*)temp;
        }
Esempio n. 8
0
 public CacheEntry(MemoryMappedViewAccessor view, long offset, uint size)
 {
     CreatedWhen = Time.Ticks;
     View = view;
     Offset = offset;
     Size = size;
     IsDisposed = false;
     _RefCount = 1;
     Buffer = view.GetSafeBuffer();
     Pointer = null;
     Buffer.AcquirePointer(ref Pointer);
     PointerOffset = view.GetPointerOffset();
 }
Esempio n. 9
0
 public StreamRange(StreamRef stream, MemoryMappedViewAccessor view, long offset, uint size, long actualOffset, long actualSize)
 {
     Stream = stream;
     View = view;
     CacheEntry = default(ViewCache.CacheEntry);
     Offset = offset;
     Size = size;
     Buffer = view.GetSafeBuffer();
     Pointer = null;
     Buffer.AcquirePointer(ref Pointer);
     IsDisposed = false;
     unchecked {
         Pointer += view.GetPointerOffset();
         Pointer += (offset - actualOffset);
     }
 }