| | | 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; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Reflection.Metadata; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace System.Reflection.PortableExecutable |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Simple BinaryReader wrapper to: |
| | | 14 | | /// |
| | | 15 | | /// 1) throw BadImageFormat instead of EndOfStream or ArgumentOutOfRange. |
| | | 16 | | /// 2) limit reads to a subset of the base stream. |
| | | 17 | | /// |
| | | 18 | | /// Only methods that are needed to read PE headers are implemented. |
| | | 19 | | /// </summary> |
| | | 20 | | internal readonly struct PEBinaryReader |
| | | 21 | | { |
| | | 22 | | private readonly long _startOffset; |
| | | 23 | | private readonly long _maxOffset; |
| | | 24 | | private readonly BinaryReader _reader; |
| | | 25 | | |
| | | 26 | | public PEBinaryReader(Stream stream, int size) |
| | 0 | 27 | | { |
| | 0 | 28 | | Debug.Assert(size >= 0 && size <= (stream.Length - stream.Position)); |
| | | 29 | | |
| | 0 | 30 | | _startOffset = stream.Position; |
| | 0 | 31 | | _maxOffset = _startOffset + size; |
| | 0 | 32 | | _reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public int Offset |
| | | 36 | | { |
| | 0 | 37 | | get { return (int)(_reader.BaseStream.Position - _startOffset); } |
| | | 38 | | set |
| | 0 | 39 | | { |
| | 0 | 40 | | CheckBounds(_startOffset, value); |
| | 0 | 41 | | _reader.BaseStream.Seek(_startOffset + value, SeekOrigin.Begin); |
| | 0 | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | private byte[] ReadBytes(int count) |
| | 0 | 46 | | { |
| | 0 | 47 | | CheckBounds(_reader.BaseStream.Position, count); |
| | 0 | 48 | | return _reader.ReadBytes(count); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | public byte ReadByte() |
| | 0 | 52 | | { |
| | 0 | 53 | | CheckBounds(sizeof(byte)); |
| | 0 | 54 | | return _reader.ReadByte(); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public short ReadInt16() |
| | 0 | 58 | | { |
| | 0 | 59 | | CheckBounds(sizeof(short)); |
| | 0 | 60 | | return _reader.ReadInt16(); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | public ushort ReadUInt16() |
| | 0 | 64 | | { |
| | 0 | 65 | | CheckBounds(sizeof(ushort)); |
| | 0 | 66 | | return _reader.ReadUInt16(); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public int ReadInt32() |
| | 0 | 70 | | { |
| | 0 | 71 | | CheckBounds(sizeof(int)); |
| | 0 | 72 | | return _reader.ReadInt32(); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public uint ReadUInt32() |
| | 0 | 76 | | { |
| | 0 | 77 | | CheckBounds(sizeof(uint)); |
| | 0 | 78 | | return _reader.ReadUInt32(); |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public ulong ReadUInt64() |
| | 0 | 82 | | { |
| | 0 | 83 | | CheckBounds(sizeof(ulong)); |
| | 0 | 84 | | return _reader.ReadUInt64(); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Reads a fixed-length byte block as a null-padded UTF-8 encoded string. |
| | | 89 | | /// The padding is not included in the returned string. |
| | | 90 | | /// |
| | | 91 | | /// Note that it is legal for UTF-8 strings to contain NUL; if NUL occurs |
| | | 92 | | /// between non-NUL codepoints, it is not considered to be padding and |
| | | 93 | | /// is included in the result. |
| | | 94 | | /// </summary> |
| | | 95 | | public string ReadNullPaddedUTF8(int byteCount) |
| | 0 | 96 | | { |
| | 0 | 97 | | byte[] bytes = ReadBytes(byteCount); |
| | 0 | 98 | | int nonPaddedLength = 0; |
| | 0 | 99 | | for (int i = bytes.Length; i > 0; --i) |
| | 0 | 100 | | { |
| | 0 | 101 | | if (bytes[i - 1] != 0) |
| | 0 | 102 | | { |
| | 0 | 103 | | nonPaddedLength = i; |
| | 0 | 104 | | break; |
| | | 105 | | } |
| | 0 | 106 | | } |
| | 0 | 107 | | return Encoding.UTF8.GetString(bytes, 0, nonPaddedLength); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | private void CheckBounds(uint count) |
| | 0 | 111 | | { |
| | 0 | 112 | | Debug.Assert(count <= sizeof(long)); // Error message assumes we're trying to read constant small number of |
| | 0 | 113 | | Debug.Assert(_reader.BaseStream.Position >= 0 && _maxOffset >= 0); |
| | | 114 | | |
| | | 115 | | // Add cannot overflow because the worst case is (ulong)long.MaxValue + uint.MaxValue < ulong.MaxValue. |
| | 0 | 116 | | if ((ulong)_reader.BaseStream.Position + count > (ulong)_maxOffset) |
| | 0 | 117 | | { |
| | 0 | 118 | | Throw.ImageTooSmall(); |
| | | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | private void CheckBounds(long startPosition, int count) |
| | 0 | 123 | | { |
| | 0 | 124 | | Debug.Assert(startPosition >= 0 && _maxOffset >= 0); |
| | | 125 | | |
| | | 126 | | // Add cannot overflow because the worst case is (ulong)long.MaxValue + uint.MaxValue < ulong.MaxValue. |
| | | 127 | | // Negative count is handled by overflow to greater than maximum size = int.MaxValue. |
| | 0 | 128 | | if ((ulong)startPosition + unchecked((uint)count) > (ulong)_maxOffset) |
| | 0 | 129 | | { |
| | 0 | 130 | | Throw.ImageTooSmallOrContainsInvalidOffsetOrCount(); |
| | | 131 | | } |
| | 0 | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |