Files
SimpleRemoter/client/Buffer.cpp
2026-04-19 22:55:21 +02:00

167 lines
3.1 KiB
C++

#ifdef _WIN32
#include "StdAfx.h"
#endif
#include "Buffer.h"
#include <math.h>
#define U_PAGE_ALIGNMENT 3
#define F_PAGE_ALIGNMENT 3.0
CBuffer::CBuffer()
{
m_ulMaxLength = 0;
m_Ptr = m_Base = NULL;
}
CBuffer::~CBuffer(void)
{
if (m_Base) {
MVirtualFree(m_Base, 0, MEM_RELEASE);
m_Base = NULL;
}
m_Base = m_Ptr = NULL;
m_ulMaxLength = 0;
}
ULONG CBuffer::ReadBuffer(PBYTE Buffer, ULONG ulLength)
{
ULONG dataLen = (ULONG)(m_Ptr - m_Base);
if (ulLength > dataLen) {
ulLength = dataLen;
}
if (ulLength) {
CopyMemory(Buffer, m_Base, ulLength);
// 只移动有效数据,而非整个缓冲区
ULONG remaining = dataLen - ulLength;
if (remaining > 0) {
MoveMemory(m_Base, m_Base + ulLength, remaining);
}
m_Ptr = m_Base + remaining;
}
DeAllocateBuffer(m_Ptr - m_Base);
return ulLength;
}
// 重新分配内存大小
VOID CBuffer::DeAllocateBuffer(ULONG ulLength)
{
int len = m_Ptr - m_Base;
if (ulLength < len)
return;
ULONG ulNewMaxLength = ceil(ulLength / F_PAGE_ALIGNMENT) * U_PAGE_ALIGNMENT;
if (m_ulMaxLength <= ulNewMaxLength) {
return;
}
PBYTE NewBase = (PBYTE) MVirtualAlloc(NULL,ulNewMaxLength,MEM_COMMIT,PAGE_READWRITE);
if (NewBase == NULL)
return;
CopyMemory(NewBase,m_Base,len);
MVirtualFree(m_Base,0,MEM_RELEASE);
m_Base = NewBase;
m_Ptr = m_Base + len;
m_ulMaxLength = ulNewMaxLength;
}
BOOL CBuffer::WriteBuffer(PBYTE Buffer, ULONG ulLength)
{
if (ReAllocateBuffer(ulLength + (m_Ptr - m_Base)) == FALSE) {
return FALSE;
}
CopyMemory(m_Ptr, Buffer, ulLength);
m_Ptr+=ulLength;
return TRUE;
}
// 当缓存长度不足时重新分配
BOOL CBuffer::ReAllocateBuffer(ULONG ulLength)
{
if (ulLength < m_ulMaxLength)
return TRUE;
ULONG ulNewMaxLength = ceil(ulLength / F_PAGE_ALIGNMENT) * U_PAGE_ALIGNMENT;
PBYTE NewBase = (PBYTE) MVirtualAlloc(NULL,ulNewMaxLength,MEM_COMMIT,PAGE_READWRITE);
if (NewBase == NULL) {
return FALSE;
}
ULONG len = m_Ptr - m_Base;
CopyMemory(NewBase, m_Base, len);
if (m_Base) {
MVirtualFree(m_Base,0,MEM_RELEASE);
}
m_Base = NewBase;
m_Ptr = m_Base + len;
m_ulMaxLength = ulNewMaxLength;
return TRUE;
}
VOID CBuffer::ClearBuffer()
{
m_Ptr = m_Base;
DeAllocateBuffer(1024);
}
ULONG CBuffer::GetBufferLength() const
{
return m_Ptr - m_Base;
}
void CBuffer::Skip(ULONG ulPos)
{
if (ulPos == 0)
return;
// 边界检查:确保不会越界
ULONG dataLen = (ULONG)(m_Ptr - m_Base);
if (ulPos > dataLen) {
ulPos = dataLen;
}
if (ulPos > 0) {
ULONG remaining = dataLen - ulPos;
if (remaining > 0) {
MoveMemory(m_Base, m_Base + ulPos, remaining);
}
m_Ptr = m_Base + remaining;
}
}
PBYTE CBuffer::GetBuffer(ULONG ulPos) const
{
if (m_Base==NULL || ulPos>=(m_Ptr - m_Base)) {
return NULL;
}
return m_Base+ulPos;
}