Class ExtensionMethods
Extensions method to work make the work with buffers and addresses easier.
Inherited Members
Namespace: PeNet
Assembly: PeNet.dll
Syntax
public static class ExtensionMethods
Methods
| Edit this page 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 |
|---|---|
| bool | 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 |
|---|---|
| bool | True, if 64 bit. |
LittleEndianBytes(ushort)
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 |
|---|---|---|
| ushort | input |
Returns
| Type | Description |
|---|---|
| byte[] | The converted byte array |
LittleEndianBytes(uint)
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 |
|---|---|---|
| uint | input |
Returns
| Type | Description |
|---|---|
| byte[] | The converted byte array |
OffsetToRva(uint, 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 |
|---|---|---|
| uint | offset | Raw file offset. |
| ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
| Type | Description |
|---|---|
| uint | Relative Virtual Address |
OffsetToRva(ulong, 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 |
|---|---|---|
| ulong | offset | Raw file offset. |
| ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
| Type | Description |
|---|---|
| ulong | Relative Virtual Address |
OrEmpty<T>(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 |
|---|---|---|
| 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(int, int)
Compute the padding to align a data structure.
Declaration
public static uint PaddingBytes(this int offset, int alignment)
Parameters
| Type | Name | Description |
|---|---|---|
| int | offset | Offset to start the alignment of the next member. |
| int | alignment | Bitness of the alignment, e.g. "32". |
Returns
| Type | Description |
|---|---|
| uint | Number of bytes needed to align the next structure. |
PaddingBytes(long, int)
Compute the padding to align a data structure.
Declaration
public static uint PaddingBytes(this long offset, int alignment)
Parameters
| Type | Name | Description |
|---|---|---|
| long | offset | Offset to start the alignment of the next member. |
| int | alignment | Bitness of the alignment, e.g. "32". |
Returns
| Type | Description |
|---|---|
| uint | Number of bytes needed to align the next structure. |
PaddingBytes(uint, int)
Compute the padding to align a data structure.
Declaration
public static uint PaddingBytes(this uint offset, int alignment)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | offset | Offset to start the alignment of the next member. |
| int | alignment | Bitness of the alignment, e.g. "32". |
Returns
| Type | Description |
|---|---|
| uint | Number of bytes needed to align the next structure. |
RvaToOffset(uint, 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 |
|---|---|---|
| uint | rva | Relative Virtual Address |
| ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
| Type | Description |
|---|---|
| uint | Raw file address. |
RvaToOffset(ulong, 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 |
|---|---|---|
| ulong | rva | Relative Virtual Address |
| ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
| Type | Description |
|---|---|
| ulong | Raw file address. |
ToHexString(byte)
Convert byte into a hexadecimal string.
Declaration
public static string ToHexString(this byte value)
Parameters
| Type | Name | Description |
|---|---|---|
| byte | value | Value |
Returns
| Type | Description |
|---|---|
| string | Hex-String |
ToHexString(byte[], ulong, ulong)
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 |
|---|---|---|
| byte[] | input | Byte array. |
| ulong | from | Index in the byte array where the hex string starts. |
| ulong | length | Length of the hex string in the byte array. |
Returns
| Type | Description |
|---|---|
| List<string> |
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<byte> | bytes | Byte sequence. |
Returns
| Type | Description |
|---|---|
| string | Hex-String |
ToHexString(ICollection<ushort>)
Convert a sequence of ushort into a hexadecimal string.
Declaration
public static string ToHexString(this ICollection<ushort> values)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<ushort> | values | Value sequence. |
Returns
| Type | Description |
|---|---|
| string | Hex-String |
ToHexString(ushort)
Convert ushort into a hexadecimal string.
Declaration
public static string ToHexString(this ushort value)
Parameters
| Type | Name | Description |
|---|---|---|
| ushort | value | Value |
Returns
| Type | Description |
|---|---|
| string | Hex-String |
ToHexString(uint)
Convert uint into a hexadecimal string.
Declaration
public static string ToHexString(this uint value)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | value | Value |
Returns
| Type | Description |
|---|---|
| string | Hex-String |
ToHexString(ulong)
Convert ulong into a hexadecimal string.
Declaration
public static string ToHexString(this ulong value)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | value | Value |
Returns
| Type | Description |
|---|---|
| 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 |
|---|---|---|
| string | hexString |
Returns
| Type | Description |
|---|---|
| long | The hex string value as a long. |
TryRvaToOffset(uint, ICollection<ImageSectionHeader>?, out uint)
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 |
|---|---|---|
| uint | rva | Relative Virtual Address |
| ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
| uint | fileOffset | File offset if mapping was successful. |
Returns
| Type | Description |
|---|---|
| bool | True if mapping was successful, false if not. |
TrySelect<T, TOut>(IEnumerable<T>, Func<T, (bool 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, (bool Success, TOut Value)> | 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 |
|---|---|---|
| string | s | A unicode string. |
Returns
| Type | Description |
|---|---|
| int | Length in bytes. |
VaToOffset(ulong, 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 |
|---|---|---|
| ulong | va | Virtual Address |
| ICollection<ImageSectionHeader> | sectionHeaders | Section Headers |
Returns
| Type | Description |
|---|---|
| ulong | Raw file address. |
WriteBytes<T>(T[], int, 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 |
| int | 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 |