| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Reflection.PortableExecutable; |
| | | 7 | | |
| | | 8 | | namespace System.Reflection.Internal |
| | | 9 | | { |
| | | 10 | | internal abstract class MemoryBlockProvider : IDisposable |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Creates and hydrates a memory block representing all data. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <exception cref="IOException">Error while reading from the memory source.</exception> |
| | | 16 | | public AbstractMemoryBlock GetMemoryBlock() |
| | 0 | 17 | | { |
| | 0 | 18 | | return GetMemoryBlockImpl(0, Size); |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates and hydrates a memory block representing data in the specified range. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="start">Starting offset relative to the beginning of the data represented by this provider.</par |
| | | 25 | | /// <param name="size">Size of the resulting block.</param> |
| | | 26 | | /// <exception cref="IOException">Error while reading from the memory source.</exception> |
| | | 27 | | public AbstractMemoryBlock GetMemoryBlock(int start, int size) |
| | 0 | 28 | | { |
| | | 29 | | // Add cannot overflow as it is the sum of two 32-bit values done in 64 bits. |
| | | 30 | | // Negative start or size is handle by overflow to greater than maximum size = int.MaxValue. |
| | 0 | 31 | | if ((ulong)(unchecked((uint)start)) + unchecked((uint)size) > (ulong)this.Size) |
| | 0 | 32 | | { |
| | 0 | 33 | | Throw.ImageTooSmallOrContainsInvalidOffsetOrCount(); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | return GetMemoryBlockImpl(start, size); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 40 | | protected abstract AbstractMemoryBlock GetMemoryBlockImpl(int start, int size); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the <see cref="Stream"/> backing the <see cref="MemoryBlockProvider"/>, if there is one. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <remarks> |
| | | 46 | | /// It is the caller's responsibility to use <paramref name="stream"/> only |
| | | 47 | | /// while locking on <paramref name="streamGuard"/>, and not read outside the |
| | | 48 | | /// bounds defined by <paramref name="imageStart"/> and <paramref name="imageSize"/>. |
| | | 49 | | /// </remarks> |
| | | 50 | | public virtual bool TryGetUnderlyingStream([NotNullWhen(true)] out Stream? stream, out long imageStart, out int |
| | 0 | 51 | | { |
| | 0 | 52 | | stream = null; |
| | 0 | 53 | | imageStart = 0; |
| | 0 | 54 | | imageSize = 0; |
| | 0 | 55 | | streamGuard = null; |
| | 0 | 56 | | return false; |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// The size of the data. |
| | | 61 | | /// </summary> |
| | | 62 | | public abstract int Size { get; } |
| | | 63 | | |
| | | 64 | | protected abstract void Dispose(bool disposing); |
| | | 65 | | |
| | | 66 | | public void Dispose() |
| | 0 | 67 | | { |
| | 0 | 68 | | Dispose(disposing: true); |
| | 0 | 69 | | GC.SuppressFinalize(this); |
| | 0 | 70 | | } |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |