Class ExtensionMethods
Extensions method to work make the work with buffers and addresses easier.
Inheritance
Namespace: PeNet
Assembly: PeNet.dll
Syntax
public static class ExtensionMethods : object
Methods
| Improve this Doc View SourceIs32Bit(IRawFile)
Check if a given file if 32 Bit
Declaration
public static bool Is32Bit(this IRawFile peFile)
Parameters
Type | Name | Description |
---|---|---|
IRawFile | peFile | A PE file. |
Returns
Type | Description |
---|---|
System.Boolean | True, if 32 bit. |
Is64Bit(IRawFile)
Check if a given file if 64 Bit
Declaration
public static bool Is64Bit(this IRawFile peFile)
Parameters
Type | Name | Description |
---|---|---|
IRawFile | peFile | A PE file. |
Returns
Type | Description |
---|---|
System.Boolean | True, if 64 bit. |
LittleEndianBytes(UInt16)
Converts a ushort to a 2 element byte array in little endian order
Declaration
public static byte[] LittleEndianBytes(this ushort input)
Parameters
Type | Name | Description |
---|---|---|
System.UInt16 | input |
Returns
Type | Description |
---|---|
System.Byte[] | The converted byte array |
LittleEndianBytes(UInt32)
Converts a uint to a 4 element byte array in little endian order
Declaration
public static byte[] LittleEndianBytes(this uint input)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | input |
Returns
Type | Description |
---|---|
System.Byte[] | The converted byte array |
OffsetToRva(UInt32, ICollection<ImageSectionHeader>)
Map a raw offset to a relative virtual address.
Declaration
public static uint OffsetToRva(this uint offset, ICollection<ImageSectionHeader> sectionHeaders)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | offset | Raw file offset. |
ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
Type | Description |
---|---|
System.UInt32 | Relative Virtual Address |
OffsetToRva(UInt64, ICollection<ImageSectionHeader>)
Map a raw offset to a relative virtual address.
Declaration
public static ulong OffsetToRva(this ulong offset, ICollection<ImageSectionHeader> sectionHeaders)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | offset | Raw file offset. |
ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
Type | Description |
---|---|
System.UInt64 | Relative Virtual Address |
OrEmpty<T>(Nullable<IEnumerable<T>>)
Makes an IEnumerable safe to enumerate by returning an empty enumerable in case it is null.
Declaration
public static IEnumerable<T> OrEmpty<T>(this IEnumerable<T>? enumerable)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<IEnumerable<T>> | enumerable | An IEnumerable. |
Returns
Type | Description |
---|---|
IEnumerable<T> | The enumerable, it it exists. An empty Enumerable if enumerable is null. |
Type Parameters
Name | Description |
---|---|
T |
PaddingBytes(Int32, Int32)
Compute the padding to align a data structure.
Declaration
public static uint PaddingBytes(this int offset, int alignment)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | offset | Offset to start the alignment of the next member. |
System.Int32 | alignment | Bitness of the alignment, e.g. "32". |
Returns
Type | Description |
---|---|
System.UInt32 | Number of bytes needed to align the next structure. |
PaddingBytes(Int64, Int32)
Compute the padding to align a data structure.
Declaration
public static uint PaddingBytes(this long offset, int alignment)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | offset | Offset to start the alignment of the next member. |
System.Int32 | alignment | Bitness of the alignment, e.g. "32". |
Returns
Type | Description |
---|---|
System.UInt32 | Number of bytes needed to align the next structure. |
PaddingBytes(UInt32, Int32)
Compute the padding to align a data structure.
Declaration
public static uint PaddingBytes(this uint offset, int alignment)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | offset | Offset to start the alignment of the next member. |
System.Int32 | alignment | Bitness of the alignment, e.g. "32". |
Returns
Type | Description |
---|---|
System.UInt32 | Number of bytes needed to align the next structure. |
RvaToOffset(UInt32, ICollection<ImageSectionHeader>)
Map an relative virtual address to the raw file address.
Declaration
public static uint RvaToOffset(this uint rva, ICollection<ImageSectionHeader> sectionHeaders)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | rva | Relative Virtual Address |
ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
Type | Description |
---|---|
System.UInt32 | Raw file address. |
RvaToOffset(UInt64, ICollection<ImageSectionHeader>)
Map an relative virtual address to the raw file address.
Declaration
public static ulong RvaToOffset(this ulong rva, ICollection<ImageSectionHeader> sectionHeaders)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | rva | Relative Virtual Address |
ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
Type | Description |
---|---|
System.UInt64 | Raw file address. |
ToHexString(ICollection<Byte>)
Convert a sequence of bytes into a hexadecimal string.
Declaration
public static string ToHexString(this ICollection<byte> bytes)
Parameters
Type | Name | Description |
---|---|---|
ICollection<System.Byte> | bytes | Byte sequence. |
Returns
Type | Description |
---|---|
System.String | Hex-String |
ToHexString(ICollection<UInt16>)
Convert a sequence of ushort into a hexadecimal string.
Declaration
public static string ToHexString(this ICollection<ushort> values)
Parameters
Type | Name | Description |
---|---|---|
ICollection<System.UInt16> | values | Value sequence. |
Returns
Type | Description |
---|---|
System.String | Hex-String |
ToHexString(Byte)
Convert byte into a hexadecimal string.
Declaration
public static string ToHexString(this byte value)
Parameters
Type | Name | Description |
---|---|---|
System.Byte | value | Value |
Returns
Type | Description |
---|---|
System.String | Hex-String |
ToHexString(Byte[], UInt64, UInt64)
Convert a sub array of an byte array to an hex string where every byte is separated by an whitespace.
Declaration
public static List<string> ToHexString(this byte[] input, ulong from, ulong length)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | input | Byte array. |
System.UInt64 | from | Index in the byte array where the hex string starts. |
System.UInt64 | length | Length of the hex string in the byte array. |
Returns
Type | Description |
---|---|
List<System.String> |
ToHexString(UInt16)
Convert ushort into a hexadecimal string.
Declaration
public static string ToHexString(this ushort value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt16 | value | Value |
Returns
Type | Description |
---|---|
System.String | Hex-String |
ToHexString(UInt32)
Convert uint into a hexadecimal string.
Declaration
public static string ToHexString(this uint value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | value | Value |
Returns
Type | Description |
---|---|
System.String | Hex-String |
ToHexString(UInt64)
Convert ulong into a hexadecimal string.
Declaration
public static string ToHexString(this ulong value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | value | Value |
Returns
Type | Description |
---|---|
System.String | Hex-String |
ToIntFromHexString(String)
Converts a hex string of the form 0x435A4DE3 to a long value.
Declaration
public static long ToIntFromHexString(this string hexString)
Parameters
Type | Name | Description |
---|---|---|
System.String | hexString |
Returns
Type | Description |
---|---|
System.Int64 | The hex string value as a long. |
TryRvaToOffset(UInt32, Nullable<ICollection<ImageSectionHeader>>, out UInt32)
Try to map a relative virtual address to a file offset.
Declaration
public static bool TryRvaToOffset(this uint rva, ICollection<ImageSectionHeader>? sectionHeaders, out uint fileOffset)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | rva | Relative Virtual Address |
System.Nullable<ICollection<ImageSectionHeader>> | sectionHeaders | Section Headers |
System.UInt32 | fileOffset | File offset if mapping was successful. |
Returns
Type | Description |
---|---|
System.Boolean | True if mapping was successful, false if not. |
TrySelect<T, TOut>(IEnumerable<T>, Func<T, (Boolean Success, TOut Value)>)
Selects the Value component of the tuple if the Success component of the tuple is true.
Declaration
public static IEnumerable<TOut> TrySelect<T, TOut>(this IEnumerable<T> source, Func<T, (bool Success, TOut Value)> tryFunc)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | An IEnumerable. |
Func<T, System.ValueTuple<System.Boolean, TOut>> | tryFunc | A function which maps elements of source to tuples of success and a mapped value. |
Returns
Type | Description |
---|---|
IEnumerable<TOut> | An IEnumerable of all successful values. |
Type Parameters
Name | Description |
---|---|
T | |
TOut |
UStringByteLength(String)
Get the length of a unicode string in bytes.
Declaration
public static int UStringByteLength(this string s)
Parameters
Type | Name | Description |
---|---|---|
System.String | s | A unicode string. |
Returns
Type | Description |
---|---|
System.Int32 | Length in bytes. |
VaToOffset(UInt64, ICollection<ImageSectionHeader>)
Map an virtual address to the raw file address.
Declaration
public static ulong VaToOffset(this ulong va, ICollection<ImageSectionHeader> sectionHeaders)
Parameters
Type | Name | Description |
---|---|---|
System.UInt64 | va | Virtual Address |
ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
Type | Description |
---|---|
System.UInt64 | Raw file address. |
WriteBytes<T>(T[], Int32, Span<T>)
Copies the contents of a span to an array, starting at a given offset
Declaration
public static void WriteBytes<T>(this T[] destination, int offset, Span<T> source)
Parameters
Type | Name | Description |
---|---|---|
T[] | destination | The array to save the data |
System.Int32 | offset | The offset within the array from where the content should be written |
Span<T> | source | The span to copy |
Type Parameters
Name | Description |
---|---|
T | The content type of the array and span, respectively |