mirror of https://github.com/x64dbg/GleeBug
added Static.BufferFile to read/write buffers
This commit is contained in:
parent
c56f2daebd
commit
399503b268
|
|
@ -163,6 +163,7 @@
|
||||||
<ClCompile Include="Debugger.Thread.HardwareBreakpoint.cpp" />
|
<ClCompile Include="Debugger.Thread.HardwareBreakpoint.cpp" />
|
||||||
<ClCompile Include="Debugger.Thread.Registers.cpp" />
|
<ClCompile Include="Debugger.Thread.Registers.cpp" />
|
||||||
<ClCompile Include="Debugger.Thread.Registers.GetSet.cpp" />
|
<ClCompile Include="Debugger.Thread.Registers.GetSet.cpp" />
|
||||||
|
<ClCompile Include="Static.BufferFile.cpp" />
|
||||||
<ClCompile Include="Static.File.cpp" />
|
<ClCompile Include="Static.File.cpp" />
|
||||||
<ClCompile Include="Static.Pe.cpp" />
|
<ClCompile Include="Static.Pe.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
@ -177,6 +178,7 @@
|
||||||
<ClInclude Include="Debugger.Thread.Registers.h" />
|
<ClInclude Include="Debugger.Thread.Registers.h" />
|
||||||
<ClInclude Include="Debugger.Thread.Registers.Register.h" />
|
<ClInclude Include="Debugger.Thread.Registers.Register.h" />
|
||||||
<ClInclude Include="GleeBug.h" />
|
<ClInclude Include="GleeBug.h" />
|
||||||
|
<ClInclude Include="Static.BufferFile.h" />
|
||||||
<ClInclude Include="Static.File.h" />
|
<ClInclude Include="Static.File.h" />
|
||||||
<ClInclude Include="Static.Global.h" />
|
<ClInclude Include="Static.Global.h" />
|
||||||
<ClInclude Include="Static.Pe.h" />
|
<ClInclude Include="Static.Pe.h" />
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,9 @@
|
||||||
<ClCompile Include="Static.Pe.cpp">
|
<ClCompile Include="Static.Pe.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Static.BufferFile.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Debugger.h">
|
<ClInclude Include="Debugger.h">
|
||||||
|
|
@ -112,5 +115,8 @@
|
||||||
<ClInclude Include="Static.Global.h">
|
<ClInclude Include="Static.Global.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Static.BufferFile.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
#include "Static.BufferFile.h"
|
||||||
|
|
||||||
|
namespace GleeBug
|
||||||
|
{
|
||||||
|
BufferFile::BufferFile(void* data, uint32 size)
|
||||||
|
: File(nullptr),
|
||||||
|
_data(data),
|
||||||
|
_size(size)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BufferFile::Open()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BufferFile::Create(bool)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BufferFile::IsOpen() const
|
||||||
|
{
|
||||||
|
return !!_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BufferFile::Close()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32 BufferFile::GetSize() const
|
||||||
|
{
|
||||||
|
return _size;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BufferFile::Read(uint32 offset, void* data, uint32 size, uint32* bytesRead) const
|
||||||
|
{
|
||||||
|
if (offset >= _size)
|
||||||
|
return false;
|
||||||
|
auto readSize = size;
|
||||||
|
auto result = true;
|
||||||
|
if (offset + size > _size)
|
||||||
|
{
|
||||||
|
readSize = _size - offset;
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
memcpy(data, (uint8*)_data + offset, readSize);
|
||||||
|
if (bytesRead)
|
||||||
|
*bytesRead = readSize;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BufferFile::Write(uint32 offset, const void* data, uint32 size, uint32* bytesWritten)
|
||||||
|
{
|
||||||
|
if (offset >= _size)
|
||||||
|
return false;
|
||||||
|
auto writeSize = size;
|
||||||
|
auto result = true;
|
||||||
|
if (offset + size > _size)
|
||||||
|
{
|
||||||
|
writeSize = _size - offset;
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
memcpy((uint8*)_data + offset, data, writeSize);
|
||||||
|
if (bytesWritten)
|
||||||
|
*bytesWritten = writeSize;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
#ifndef _STATIC_BUFFERFILE_H
|
||||||
|
#define _STATIC_BUFFERFILE_H
|
||||||
|
|
||||||
|
#include "Static.File.h"
|
||||||
|
|
||||||
|
namespace GleeBug
|
||||||
|
{
|
||||||
|
class BufferFile : public File
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BufferFile(void* data, uint32 size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Opens an existing file.
|
||||||
|
\return true if the file was opened successfully, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual bool Open() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Creates a new file.
|
||||||
|
\param overwrite (Optional) true to overwrite, false to preserve fail if the file already exists.
|
||||||
|
\return true if the file was created, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual bool Create(bool overwrite = true) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Check if there is an open/created file.
|
||||||
|
*/
|
||||||
|
virtual bool IsOpen() const override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Closes the file.
|
||||||
|
*/
|
||||||
|
virtual void Close() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Gets the size of the file.
|
||||||
|
*/
|
||||||
|
virtual uint32 GetSize() const override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Reads from the open file.
|
||||||
|
\param offset The offset to start reading from.
|
||||||
|
\param [out] data Destination buffer.
|
||||||
|
\param size The size to read.
|
||||||
|
\param [out] bytesRead (Optional) If set, returns the number of bytes read (even on failure).
|
||||||
|
\return true if the read was fully successful, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual bool Read(uint32 offset, void* data, uint32 size, uint32* bytesRead = nullptr) const override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Writes to the open file.
|
||||||
|
\param offset The offset to start writing to. Everything after this offset will be truncated!
|
||||||
|
\param data The data to write.
|
||||||
|
\param size The size to write.
|
||||||
|
\param [out] bytesWritten (Optional) If set, returns the number of bytes written (even on failure)
|
||||||
|
\return true if the write was fully successful, false otherwise.
|
||||||
|
*/
|
||||||
|
virtual bool Write(uint32 offset, const void* data, uint32 size, uint32* bytesWritten = nullptr) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void* _data;
|
||||||
|
uint32 _size;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //_STATIC_BUFFERFILE_H
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#include "MyDebugger.h"
|
#include "MyDebugger.h"
|
||||||
#include "GleeBug/Static.File.h"
|
#include "GleeBug/Static.File.h"
|
||||||
#include "GleeBug/Static.Pe.h"
|
#include "GleeBug/Static.Pe.h"
|
||||||
|
#include "GleeBug/Static.BufferFile.h"
|
||||||
|
|
||||||
static void testDebugger()
|
static void testDebugger()
|
||||||
{
|
{
|
||||||
|
|
@ -44,11 +45,17 @@ static void testStatic()
|
||||||
wchar_t szFilePath[256] = L"c:\\!exclude\\pe\\mini.exe";
|
wchar_t szFilePath[256] = L"c:\\!exclude\\pe\\mini.exe";
|
||||||
#endif //_WIN64
|
#endif //_WIN64
|
||||||
using namespace GleeBug;
|
using namespace GleeBug;
|
||||||
File file(szFilePath, File::ReadOnly);
|
File diskFile(szFilePath, File::ReadOnly);
|
||||||
if (file.Open())
|
if (diskFile.Open())
|
||||||
{
|
{
|
||||||
|
auto diskSize = diskFile.GetSize();
|
||||||
|
std::vector<uint8> diskData(diskSize);
|
||||||
|
if (diskFile.Read(0, diskData.data(), diskSize))
|
||||||
|
{
|
||||||
|
BufferFile file(diskData.data(), diskSize);
|
||||||
Pe pe(file);
|
Pe pe(file);
|
||||||
if (pe.ParseHeaders() == Pe::ErrorOk)
|
auto parseError = pe.ParseHeaders();
|
||||||
|
if (parseError == Pe::ErrorOk)
|
||||||
{
|
{
|
||||||
auto idh = pe.GetDosHeader();
|
auto idh = pe.GetDosHeader();
|
||||||
printRegion("DOS Header:", idh);
|
printRegion("DOS Header:", idh);
|
||||||
|
|
@ -100,17 +107,19 @@ static void testStatic()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
puts("Pe::ParseHeaders failed!");
|
printf("Pe::ParseHeaders failed (%d)!\n", parseError);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
puts("File::Read failed!");
|
||||||
puts("File::Open failed!");
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
puts("File::Open failed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
testStatic();
|
testStatic();
|
||||||
|
puts("");
|
||||||
system("pause");
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue