| | | 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.Collections.Immutable; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Reflection.Internal; |
| | | 7 | | using System.Reflection.Metadata; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | |
| | | 10 | | namespace System.Reflection.PortableExecutable |
| | | 11 | | { |
| | | 12 | | public readonly struct PEMemoryBlock |
| | | 13 | | { |
| | | 14 | | private readonly AbstractMemoryBlock _block; |
| | | 15 | | private readonly int _offset; |
| | | 16 | | |
| | | 17 | | internal PEMemoryBlock(AbstractMemoryBlock block, int offset = 0) |
| | 0 | 18 | | { |
| | 0 | 19 | | Debug.Assert(block != null); |
| | 0 | 20 | | Debug.Assert(offset >= 0 && offset <= block.Size); |
| | | 21 | | |
| | 0 | 22 | | _block = block; |
| | 0 | 23 | | _offset = offset; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Pointer to the first byte of the block. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public unsafe byte* Pointer => (_block != null) ? _block.Pointer + _offset : null; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Length of the block. |
| | | 33 | | /// </summary> |
| | 0 | 34 | | public int Length => _block?.Size - _offset ?? 0; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Creates <see cref="BlobReader"/> for a blob spanning the entire block. |
| | | 38 | | /// </summary> |
| | | 39 | | public unsafe BlobReader GetReader() |
| | 0 | 40 | | { |
| | 0 | 41 | | return new BlobReader(Pointer, Length); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates <see cref="BlobReader"/> for a blob spanning a part of the block. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <exception cref="ArgumentOutOfRangeException">Specified range is not contained within the block.</exception> |
| | | 48 | | public unsafe BlobReader GetReader(int start, int length) |
| | 0 | 49 | | { |
| | 0 | 50 | | BlobUtilities.ValidateRange(Length, start, length, nameof(length)); |
| | 0 | 51 | | return new BlobReader(Pointer + start, length); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Reads the content of the entire block into an array. |
| | | 56 | | /// </summary> |
| | | 57 | | public ImmutableArray<byte> GetContent() |
| | 0 | 58 | | { |
| | 0 | 59 | | return _block?.GetContentUnchecked(_offset, Length) ?? ImmutableArray<byte>.Empty; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Reads the content of a part of the block into an array. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <exception cref="ArgumentOutOfRangeException">Specified range is not contained within the block.</exception> |
| | | 66 | | public ImmutableArray<byte> GetContent(int start, int length) |
| | 0 | 67 | | { |
| | 0 | 68 | | BlobUtilities.ValidateRange(Length, start, length, nameof(length)); |
| | 0 | 69 | | return _block?.GetContentUnchecked(_offset + start, length) ?? ImmutableArray<byte>.Empty; |
| | 0 | 70 | | } |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |