//
// Now its support native and managed [both of them]
// i added credits in source :P
//
Code
class Eof
{
// here a sample [ Copy & Uncomment it ]
// |-->
// byte[] data = Eof.GetEOF(@"file location");
// if (data != null)
// {
// // to do
// }
// <--|
// look here for more explain
// http://stackoverflow.com/questions/855756/difference-between-native-and-managed-code
// source :: https://hackforums.net/showthread.php?tid=1193297
// auther :: Zer0Fact0
public static byte[] GetManagedEof(string path)
{
try
{
byte[] buffer = null;
using (FileStream fs = new FileStream(path, FileMode.Open))
{
buffer = new byte[4];
fs.Position = 0x3C;
fs.Read(buffer, 0, 4);
fs.Position = BitConverter.ToInt32(buffer, 0) + 0x6;
buffer = new byte[2];
fs.Read(buffer, 0, 2);
fs.Position += 0x100 + ((BitConverter.ToInt16(buffer, 0) - 1) * 0x28);
buffer = new byte[8];
fs.Read(buffer, 0, 8);
fs.Position = BitConverter.ToInt32(buffer, 0) + BitConverter.ToInt32(buffer, 4);
buffer = new byte[fs.Length - fs.Position];
fs.Read(buffer, 0, buffer.Length);
}
return buffer;
}
catch { }
return null;
}
// source :: https://hackforums.net/showthread.php?tid=717991
// auther :: pr0t0typ3
// ported to c# by DragQueen
public static byte[] GetNativeEof(string path)
{
try
{
byte[] buffer = null;
byte[] data = File.ReadAllBytes(path);
long size = CalculateSize(data);
if (size != data.Length)
{
buffer = new byte[data.Length - size - 3]; ;
Array.Copy(data, size + 2, buffer, 0, buffer.Length);
}
if (buffer.Length > 0)
return buffer;
}
catch { }
return null;
}
static long CalculateSize(byte[] data)
{
long size = 0L;
GCHandle GC = GCHandle.Alloc(data, GCHandleType.Pinned);
int buffer = GC.AddrOfPinnedObject().ToInt32();
DOS_Header DH = (DOS_Header)Marshal.PtrToStructure(GC.AddrOfPinnedObject(), typeof(DOS_Header));
GC.Free();
NT_Headers NH = (NT_Headers)Marshal.PtrToStructure(new IntPtr(buffer + DH.Address), typeof(NT_Headers));
long offset = DH.Address + 248;
int length = NH.File.Sections - 1;
for (int i = 0; i <= length; i++)
{
Section_Header header2 = (Section_Header)Marshal.PtrToStructure(new IntPtr((buffer + offset) + (i * 40)), typeof(Section_Header));
size += header2.Size;
}
return (size + 4096);
}
[StructLayout(LayoutKind.Sequential)]
struct Data_Directory
{
public uint Address;
public uint Size;
}
[StructLayout(LayoutKind.Sequential)]
struct DOS_Header
{
public ushort Magic;
public ushort Last;
public ushort Pages;
public ushort Relocations;
public ushort Size;
public ushort Minimum;
public ushort Maximum;
public ushort SS;
public ushort SP;
public ushort Checksum;
public ushort IP;
public ushort CS;
public ushort Table;
public ushort Overlay;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public ushort[] ReservedA;
public ushort ID;
public ushort Info;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public ushort[] ReservedB;
public int Address;
}
[StructLayout(LayoutKind.Sequential)]
struct File_Header
{
public ushort Machine;
public ushort Sections;
public uint Stamp;
public uint Table;
public uint Symbols;
public ushort Size;
public ushort Flags;
}
[StructLayout(LayoutKind.Sequential)]
struct Misc
{
public uint Address;
public uint Size;
}
[StructLayout(LayoutKind.Sequential)]
struct NT_Headers
{
public uint Signature;
public File_Header File;
public Optional_Headers Optional;
}
[StructLayout(LayoutKind.Sequential)]
struct Optional_Headers
{
public ushort Magic;
public byte Major;
public byte Minor;
public uint SCode;
public uint IData;
public uint UData;
public uint Address;
public uint Code;
public uint Data;
public uint Image;
public uint SectionA;
public uint FileA;
public ushort MajorO;
public ushort MinorO;
public ushort MajorI;
public ushort MinorI;
public ushort MajorS;
public ushort MinorS;
public uint Version;
public uint SImage;
public uint SHeaders;
public uint Checksum;
public ushort Subsystem;
public ushort Flags;
public uint SSReserve;
public uint SSCommit;
public uint SHReserve;
public uint SHCommit;
public uint LFlags;
public uint Count;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)]
public Data_Directory[] DataDirectory;
}
[StructLayout(LayoutKind.Sequential)]
struct Section_Header
{
public byte Name;
public Misc Misc;
public uint Address;
public uint Size;
public uint Pointer;
public uint PRelocations;
public uint PLines;
public uint NRelocations;
public uint NLines;
public uint Flags;
}
}