Skip to content

Commit 129415f

Browse files
committed
Remove IBinaryReader and revert related code changes.
Turns out the unification of `PEBinaryReader` and `BlobReader` can be avoided for now, and refactorings can still be made without it.
1 parent 4d0cd27 commit 129415f

File tree

10 files changed

+151
-230
lines changed

10 files changed

+151
-230
lines changed

src/libraries/System.Reflection.Metadata/src/System.Reflection.Metadata.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ The System.Reflection.Metadata library is built-in as part of the shared framewo
2727
Link="Common\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
2828
Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
2929
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" Link="System\Runtime\CompilerServices\IsExternalInit.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
30-
<Compile Include="System\Reflection\Internal\Utilities\IBinaryReader.cs" />
3130
<Compile Include="System\Reflection\Internal\Utilities\PinnedObject.cs" />
3231
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.cs" />
3332
<Compile Include="System\Reflection\Internal\Utilities\ExceptionUtilities.cs" />

src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/IBinaryReader.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobReader.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace System.Reflection.Metadata
1212
{
1313
[DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
14-
public unsafe struct BlobReader : IBinaryReader
14+
public unsafe struct BlobReader
1515
{
1616
internal const int InvalidCompressedInteger = int.MaxValue;
1717

@@ -438,16 +438,6 @@ public void ReadBytes(int byteCount, byte[] buffer, int bufferOffset)
438438
Marshal.Copy((IntPtr)GetCurrentPointerAndAdvance(byteCount), buffer, bufferOffset, byteCount);
439439
}
440440

441-
string IBinaryReader.ReadNullPaddedUTF8(int byteCount)
442-
{
443-
byte* ptr = GetCurrentPointerAndAdvance(byteCount);
444-
while (byteCount > 0 && ptr[byteCount - 1] == 0)
445-
{
446-
byteCount--;
447-
}
448-
return Encoding.UTF8.GetString(ptr, byteCount);
449-
}
450-
451441
internal string ReadUtf8NullTerminated()
452442
{
453443
int bytesRead;

src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/CoffHeader.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,40 @@ public sealed class CoffHeader
88
/// <summary>
99
/// The type of target machine.
1010
/// </summary>
11-
public Machine Machine { get; private init; }
11+
public Machine Machine { get; }
1212

1313
/// <summary>
1414
/// The number of sections. This indicates the size of the section table, which immediately follows the headers.
1515
/// </summary>
16-
public short NumberOfSections { get; private init; }
16+
public short NumberOfSections { get; }
1717

1818
/// <summary>
1919
/// The low 32 bits of the number of seconds since 00:00 January 1, 1970, that indicates when the file was created.
2020
/// </summary>
21-
public int TimeDateStamp { get; private init; }
21+
public int TimeDateStamp { get; }
2222

2323
/// <summary>
2424
/// The file pointer to the COFF symbol table, or zero if no COFF symbol table is present.
2525
/// This value should be zero for a PE image.
2626
/// </summary>
27-
public int PointerToSymbolTable { get; private init; }
27+
public int PointerToSymbolTable { get; }
2828

2929
/// <summary>
3030
/// The number of entries in the symbol table. This data can be used to locate the string table,
3131
/// which immediately follows the symbol table. This value should be zero for a PE image.
3232
/// </summary>
33-
public int NumberOfSymbols { get; private init; }
33+
public int NumberOfSymbols { get; }
3434

3535
/// <summary>
3636
/// The size of the optional header, which is required for executable files but not for object files.
3737
/// This value should be zero for an object file.
3838
/// </summary>
39-
public short SizeOfOptionalHeader { get; private init; }
39+
public short SizeOfOptionalHeader { get; }
4040

4141
/// <summary>
4242
/// The flags that indicate the attributes of the file.
4343
/// </summary>
44-
public Characteristics Characteristics { get; private init; }
44+
public Characteristics Characteristics { get; }
4545

4646
internal const int Size =
4747
sizeof(short) + // Machine
@@ -52,20 +52,15 @@ public sealed class CoffHeader
5252
sizeof(short) + // SizeOfOptionalHeader:
5353
sizeof(ushort); // Characteristics
5454

55-
private CoffHeader() { }
56-
57-
internal static CoffHeader Create<TReader>(ref TReader reader) where TReader : IBinaryReader
55+
internal CoffHeader(ref PEBinaryReader reader)
5856
{
59-
return new CoffHeader()
60-
{
61-
Machine = (Machine)reader.ReadUInt16(),
62-
NumberOfSections = reader.ReadInt16(),
63-
TimeDateStamp = reader.ReadInt32(),
64-
PointerToSymbolTable = reader.ReadInt32(),
65-
NumberOfSymbols = reader.ReadInt32(),
66-
SizeOfOptionalHeader = reader.ReadInt16(),
67-
Characteristics = (Characteristics)reader.ReadUInt16(),
68-
};
57+
Machine = (Machine)reader.ReadUInt16();
58+
NumberOfSections = reader.ReadInt16();
59+
TimeDateStamp = reader.ReadInt32();
60+
PointerToSymbolTable = reader.ReadInt32();
61+
NumberOfSymbols = reader.ReadInt32();
62+
SizeOfOptionalHeader = reader.ReadInt16();
63+
Characteristics = (Characteristics)reader.ReadUInt16();
6964
}
7065
}
7166
}

src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/CorHeader.cs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,34 @@ namespace System.Reflection.PortableExecutable
55
{
66
public sealed class CorHeader
77
{
8-
public ushort MajorRuntimeVersion { get; private init; }
9-
public ushort MinorRuntimeVersion { get; private init; }
10-
public DirectoryEntry MetadataDirectory { get; private init; }
11-
public CorFlags Flags { get; private init; }
12-
public int EntryPointTokenOrRelativeVirtualAddress { get; private init; }
13-
public DirectoryEntry ResourcesDirectory { get; private init; }
14-
public DirectoryEntry StrongNameSignatureDirectory { get; private init; }
15-
public DirectoryEntry CodeManagerTableDirectory { get; private init; }
16-
public DirectoryEntry VtableFixupsDirectory { get; private init; }
17-
public DirectoryEntry ExportAddressTableJumpsDirectory { get; private init; }
18-
public DirectoryEntry ManagedNativeHeaderDirectory { get; private init; }
8+
public ushort MajorRuntimeVersion { get; }
9+
public ushort MinorRuntimeVersion { get; }
10+
public DirectoryEntry MetadataDirectory { get; }
11+
public CorFlags Flags { get; }
12+
public int EntryPointTokenOrRelativeVirtualAddress { get; }
13+
public DirectoryEntry ResourcesDirectory { get; }
14+
public DirectoryEntry StrongNameSignatureDirectory { get; }
15+
public DirectoryEntry CodeManagerTableDirectory { get; }
16+
public DirectoryEntry VtableFixupsDirectory { get; }
17+
public DirectoryEntry ExportAddressTableJumpsDirectory { get; }
18+
public DirectoryEntry ManagedNativeHeaderDirectory { get; }
1919

20-
private CorHeader() { }
21-
22-
internal static CorHeader Create<TReader>(ref TReader reader) where TReader : IBinaryReader
20+
internal CorHeader(ref PEBinaryReader reader)
2321
{
2422
// byte count
2523
reader.ReadInt32();
2624

27-
return new CorHeader()
28-
{
29-
MajorRuntimeVersion = reader.ReadUInt16(),
30-
MinorRuntimeVersion = reader.ReadUInt16(),
31-
MetadataDirectory = DirectoryEntry.Create(ref reader),
32-
Flags = (CorFlags)reader.ReadUInt32(),
33-
EntryPointTokenOrRelativeVirtualAddress = reader.ReadInt32(),
34-
ResourcesDirectory = DirectoryEntry.Create(ref reader),
35-
StrongNameSignatureDirectory = DirectoryEntry.Create(ref reader),
36-
CodeManagerTableDirectory = DirectoryEntry.Create(ref reader),
37-
VtableFixupsDirectory = DirectoryEntry.Create(ref reader),
38-
ExportAddressTableJumpsDirectory = DirectoryEntry.Create(ref reader),
39-
ManagedNativeHeaderDirectory = DirectoryEntry.Create(ref reader),
40-
};
25+
MajorRuntimeVersion = reader.ReadUInt16();
26+
MinorRuntimeVersion = reader.ReadUInt16();
27+
MetadataDirectory = new DirectoryEntry(ref reader);
28+
Flags = (CorFlags)reader.ReadUInt32();
29+
EntryPointTokenOrRelativeVirtualAddress = reader.ReadInt32();
30+
ResourcesDirectory = new DirectoryEntry(ref reader);
31+
StrongNameSignatureDirectory = new DirectoryEntry(ref reader);
32+
CodeManagerTableDirectory = new DirectoryEntry(ref reader);
33+
VtableFixupsDirectory = new DirectoryEntry(ref reader);
34+
ExportAddressTableJumpsDirectory = new DirectoryEntry(ref reader);
35+
ManagedNativeHeaderDirectory = new DirectoryEntry(ref reader);
4136
}
4237
}
4338
}

src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DirectoryEntry.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public DirectoryEntry(int relativeVirtualAddress, int size)
1414
Size = size;
1515
}
1616

17-
internal static DirectoryEntry Create<TReader>(ref TReader reader) where TReader : IBinaryReader
17+
internal DirectoryEntry(ref PEBinaryReader reader)
1818
{
19-
return new DirectoryEntry(reader.ReadInt32(), reader.ReadInt32());
19+
RelativeVirtualAddress = reader.ReadInt32();
20+
Size = reader.ReadInt32();
2021
}
2122
}
2223
}

src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEBinaryReader.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace System.Reflection.PortableExecutable
1717
///
1818
/// Only methods that are needed to read PE headers are implemented.
1919
/// </summary>
20-
internal readonly struct PEBinaryReader : IBinaryReader
20+
internal readonly struct PEBinaryReader
2121
{
2222
private readonly long _startOffset;
2323
private readonly long _maxOffset;
@@ -84,6 +84,14 @@ public ulong ReadUInt64()
8484
return _reader.ReadUInt64();
8585
}
8686

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>
8795
public string ReadNullPaddedUTF8(int byteCount)
8896
{
8997
byte[] bytes = ReadBytes(byteCount);

0 commit comments

Comments
 (0)