Struct BinaryStreamReader
- Namespace
- AsmResolver.IO
- Assembly
- AsmResolver.dll
Provides methods for reading binary data from a data source.
public struct BinaryStreamReader
- Inherited Members
Constructors
BinaryStreamReader(IDataSource)
Creates a new binary stream reader on the provided data source.
public BinaryStreamReader(IDataSource dataSource)
Parameters
dataSource
IDataSourceThe object to get the data from.
BinaryStreamReader(IDataSource, ulong, uint, uint)
Creates a new binary stream reader on the provided data source.
public BinaryStreamReader(IDataSource dataSource, ulong offset, uint rva, uint length)
Parameters
dataSource
IDataSourceThe object to get the data from.
offset
ulongThe raw offset to start at.
rva
uintThe relative virtual address associated to the offset.
length
uintThe maximum number of bytes to read.
Exceptions
- ArgumentNullException
Occurs when
dataSource
isnull
.- ArgumentOutOfRangeException
Occurs when
offset
is not a valid offset.- EndOfStreamException
Occurs when too many bytes are specified by
length
.
BinaryStreamReader(byte[])
Creates a new binary stream reader on the provided data source.
public BinaryStreamReader(byte[] data)
Parameters
data
byte[]The data to read from.
Properties
DataSource
Gets the data source the reader is reading from.
public readonly IDataSource DataSource { get; }
Property Value
EndOffset
Gets the raw address indicating the end of the stream.
public ulong EndOffset { get; }
Property Value
EndRva
Gets the relative virtual address indicating the end of the stream.
public ulong EndRva { get; }
Property Value
IsValid
Gets a value indicating whether the reader is in a valid state.
public bool IsValid { get; }
Property Value
Length
Gets the number of bytes that can be read by the reader.
public readonly uint Length { get; }
Property Value
Offset
Gets or sets the current raw offset to read from.
public ulong Offset { readonly get; set; }
Property Value
RelativeOffset
Gets or sets the current offset relative to the beginning of StartOffset to read from.
public uint RelativeOffset { get; set; }
Property Value
RemainingLength
Gets the remaining number of bytes that can be read from the stream.
public uint RemainingLength { get; }
Property Value
Rva
Gets or sets the current virtual address (relative to the image base) to read from.
public uint Rva { get; set; }
Property Value
StartOffset
Gets the raw offset this reader started from.
public readonly ulong StartOffset { get; }
Property Value
StartRva
Gets the relative virtual address this reader started from.
public readonly uint StartRva { get; }
Property Value
Methods
AdvanceUntil(byte, bool)
Advances the reader until the provided delimiter byte is reached.
public bool AdvanceUntil(byte delimiter, bool consumeDelimiter)
Parameters
delimiter
byteThe delimiter byte to stop at.
consumeDelimiter
booltrue
if the final delimiter should be consumed if available,false
otherwise.
Returns
- bool
true
if the delimiter byte was found and consumed,false
otherwise.
Align(uint)
Aligns the reader to a specified boundary.
public void Align(uint alignment)
Parameters
alignment
uintThe boundary to use.
AlignRelative(uint)
Aligns the reader to a specified boundary.
public void AlignRelative(uint alignment)
Parameters
alignment
uintThe boundary to use.
CanRead(uint)
Determines whether the provided number of bytes can be read from the current position.
public bool CanRead(uint count)
Parameters
count
uintThe number of bytes to read.
Returns
- bool
true
if the provided number of byte can be read,false
otherwise.
ChangeSize(uint)
Resizes the current reader to a new number of bytes.
public void ChangeSize(uint newSize)
Parameters
newSize
uintThe new number of bytes.
Exceptions
- EndOfStreamException
Occurs when the provided size reaches outside of the input stream's length.
Fork()
Creates an exact copy of the reader.
public readonly BinaryStreamReader Fork()
Returns
- BinaryStreamReader
The copied reader.
Remarks
This method does not copy the underlying buffer.
ForkAbsolute(ulong)
Creates a copy of the reader, and moves the offset of the copied reader to the provided file offset.
public readonly BinaryStreamReader ForkAbsolute(ulong offset)
Parameters
offset
ulongThe file offset.
Returns
- BinaryStreamReader
The new reader.
Remarks
This method does not copy the underlying buffer.
ForkAbsolute(ulong, uint)
Creates a copy of the reader, moves the offset of the copied reader to the provided file offset, and resizes the copied reader to the provided number of bytes.
public readonly BinaryStreamReader ForkAbsolute(ulong offset, uint size)
Parameters
Returns
- BinaryStreamReader
The new reader.
Remarks
This method does not copy the underlying buffer.
ForkRelative(uint)
Creates a copy of the reader, and moves to the provided relative offset of the copied reader.
public readonly BinaryStreamReader ForkRelative(uint relativeOffset)
Parameters
relativeOffset
uintThe displacement.
Returns
- BinaryStreamReader
The new reader.
Remarks
This method does not copy the underlying buffer.
ForkRelative(uint, uint)
Creates a copy of the reader, moves the copied reader to the provided relative offset, and resizes the copied reader to the provided number of bytes.
public readonly BinaryStreamReader ForkRelative(uint relativeOffset, uint size)
Parameters
Returns
- BinaryStreamReader
The new reader.
Remarks
This method does not copy the underlying buffer.
PeekByte()
Peeks a single byte from the input stream.
public int PeekByte()
Returns
- int
The read byte, or
-1
if no byte could be read.
Read7BitEncodedInt32()
Reads a 7-bit encoded 32-bit integer from the stream.
public int Read7BitEncodedInt32()
Returns
- int
The integer.
Exceptions
- FormatException
Occurs when an invalid 7-bit encoding was encountered.
ReadAsciiString()
Reads a null-terminated ASCII string from the input stream.
public string ReadAsciiString()
Returns
- string
The read ASCII string, excluding the null terminator.
ReadBinaryFormatterString()
Reads a serialized UTF-8 string that is prefixed by a 7-bit encoded length header.
public string ReadBinaryFormatterString()
Returns
- string
The string.
Exceptions
- FormatException
Occurs when the 7-bit encoded header is invalid.
ReadBinaryFormatterString(Encoding)
Reads a serialized string that is prefixed by a 7-bit encoded length header.
public string ReadBinaryFormatterString(Encoding encoding)
Parameters
encoding
EncodingThe encoding to use for decoding the bytes into a string.
Returns
- string
The string.
Exceptions
- FormatException
Occurs when the 7-bit encoded header is invalid.
ReadByte()
Reads a single byte from the input stream, and advances the current offset by one.
public byte ReadByte()
Returns
- byte
The consumed value.
ReadBytes(byte[], int, int)
Attempts to read the provided amount of bytes from the input stream.
public int ReadBytes(byte[] buffer, int index, int count)
Parameters
buffer
byte[]The buffer that receives the read bytes.
index
intThe index into the buffer to start writing into.
count
intThe number of bytes to read.
Returns
- int
The number of bytes that were read.
ReadBytes(Span<byte>)
Attempts to read the provided amount of bytes from the input stream.
public int ReadBytes(Span<byte> buffer)
Parameters
Returns
- int
The number of bytes that were read.
ReadBytesUntil(byte)
Reads bytes from the input stream until the provided delimiter byte is reached.
public byte[] ReadBytesUntil(byte delimiter)
Parameters
delimiter
byteThe delimiter byte to stop at.
Returns
- byte[]
The read bytes, including the delimiter if it was found.
ReadBytesUntil(byte, bool)
Reads bytes from the input stream until the provided delimiter byte is reached.
public byte[] ReadBytesUntil(byte delimiter, bool includeDelimiterInReturn)
Parameters
delimiter
byteThe delimiter byte to stop at.
includeDelimiterInReturn
booltrue
if the final delimiter should be included in the return value,false
otherwise.
Returns
- byte[]
The read bytes.
Remarks
This function always consumes the delimiter from the input stream if it is present, regardless of the value
of includeDelimiterInReturn
.
ReadCompressedInt32()
Reads a compressed unsigned integer from the stream.
public int ReadCompressedInt32()
Returns
- int
The signed integer that was read from the stream.
ReadCompressedUInt32()
Reads a compressed unsigned integer from the stream.
public uint ReadCompressedUInt32()
Returns
- uint
The unsigned integer that was read from the stream.
ReadDecimal()
Reads a single 128-bit decimal value from the input stream, and advances the current offset by 16.
public decimal ReadDecimal()
Returns
- decimal
The consumed value.
ReadDouble()
Reads a single signed 64-bit double precision floating point number from the input stream, and advances the current offset by four.
public double ReadDouble()
Returns
- double
The consumed value.
ReadIndex(IndexSize)
Reads a short or a long index from the stream.
public uint ReadIndex(IndexSize size)
Parameters
size
IndexSizeThe size of the index to read.
Returns
- uint
The index, zero extended to 32 bits if necessary.
Exceptions
ReadInt16()
Reads a single signed 16-bit integer from the input stream, and advances the current offset by two.
public short ReadInt16()
Returns
- short
The consumed value.
ReadInt32()
Reads a single signed 32-bit integer from the input stream, and advances the current offset by four.
public int ReadInt32()
Returns
- int
The consumed value.
ReadInt64()
Reads a single signed 64-bit integer from the input stream, and advances the current offset by eight.
public long ReadInt64()
Returns
- long
The consumed value.
ReadNativeInt(bool)
Reads either a 32-bit or a 64-bit number from the input stream.
public ulong ReadNativeInt(bool is32Bit)
Parameters
is32Bit
boolIndicates the integer to be read is 32-bit or 64-bit.
Returns
- ulong
The read number, zero extended if necessary.
ReadSByte()
Reads a single signed byte from the input stream, and advances the current offset by one.
public sbyte ReadSByte()
Returns
- sbyte
The consumed value.
ReadSegment(uint)
Creates a segment containing data from the input data source, starting at the current position of the input stream. The reader then advances the current offset by the provided number of bytes.
public IReadableSegment ReadSegment(uint count)
Parameters
count
uintThe number of bytes the segment should contain.
Returns
- IReadableSegment
The read segment.
ReadSerString()
Reads a serialized UTF8 string from the stream.
public Utf8String? ReadSerString()
Returns
- Utf8String
The string that was read from the stream.
ReadSingle()
Reads a single signed 32-bit single precision floating point number from the input stream, and advances the current offset by four.
public float ReadSingle()
Returns
- float
The consumed value.
ReadToEnd()
Consumes the remainder of the input stream.
public byte[] ReadToEnd()
Returns
- byte[]
The remaining bytes.
ReadUInt16()
Reads a single unsigned 16-bit integer from the input stream, and advances the current offset by two.
public ushort ReadUInt16()
Returns
- ushort
The consumed value.
ReadUInt32()
Reads a single unsigned 32-bit integer from the input stream, and advances the current offset by four.
public uint ReadUInt32()
Returns
- uint
The consumed value.
ReadUInt64()
Reads a single unsigned 64-bit integer from the input stream, and advances the current offset by eight.
public ulong ReadUInt64()
Returns
- ulong
The consumed value.
ReadUnicodeString()
Reads a zero-terminated Unicode string from the stream.
public string ReadUnicodeString()
Returns
- string
The string that was read from the stream.
ReadUtf8String()
Reads a null-terminated UTF-8 string from the input stream.
public Utf8String ReadUtf8String()
Returns
- Utf8String
The read UTF-8 string, excluding the null terminator.
TryReadCompressedInt32(out int)
Tries to reads a compressed signed integer from the stream.
public bool TryReadCompressedInt32(out int value)
Parameters
value
intThe signed integer that was read from the stream.
Returns
- bool
True
if the method succeeded, false otherwise.
TryReadCompressedUInt32(out uint)
Tries to reads a compressed unsigned integer from the stream.
public bool TryReadCompressedUInt32(out uint value)
Parameters
value
uintThe unsigned integer that was read from the stream.
Returns
- bool
True
if the method succeeded, false otherwise.
WriteToOutput(BinaryStreamWriter)
Consumes and copies the remainder of the contents to the provided output stream.
public void WriteToOutput(BinaryStreamWriter writer)
Parameters
writer
BinaryStreamWriterThe output stream.