⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buffer.h

📁 概述:数据的纵向收集
💻 H
字号:
#pragma once
#include <assert.h>
#include <windows.h>
#include <math.h>

#define BUFFER_GROUP_LENGTH 4096

class CBuffer  
{
public:
    CBuffer() : m_nSize(BUFFER_GROUP_LENGTH) // Initial size
    {
        m_pPtr = m_pBase = (BYTE*)VirtualAlloc(NULL,m_nSize,MEM_COMMIT,PAGE_READWRITE);
    }

    virtual ~CBuffer()
    {    
        if (m_pBase)
            VirtualFree(m_pBase,0,MEM_RELEASE);
    }


public: // Methods
    void ClearBuffer()
    {
        m_pPtr = m_pBase;
        DeAllocateBuffer(BUFFER_GROUP_LENGTH);
    }

    UINT Read(BYTE* pData, UINT nSize)
    {
        // all that we have 
        if (nSize > GetBufferLen())
            nSize = GetBufferLen();

        if (nSize)
        {
            // Copy over required amount and its not up to us
            // to terminate the buffer - got that!!!
            CopyMemory(pData,m_pBase,nSize);

            // Slide the buffer back - like sinking the data
            MoveMemory(m_pBase,m_pBase+nSize,GetMemSize() - nSize);

            m_pPtr -= nSize;
        }

        DeAllocateBuffer(GetBufferLen());

        return nSize;
    }

    BOOL Write(const BYTE* pData, UINT nSize)
    {
        ReAllocateBuffer(nSize + GetBufferLen());
        CopyMemory(m_pPtr,pData,nSize);

        // Advance Pointer
        m_pPtr+=nSize;


        return nSize;
    }

    BOOL Insert(const BYTE* pData, UINT nSize)
    {
        ReAllocateBuffer(nSize + GetBufferLen());

        MoveMemory(m_pBase+nSize,m_pBase,GetMemSize() - nSize);
        CopyMemory(m_pBase,pData,nSize);

        // Advance Pointer
        m_pPtr+=nSize;

        return nSize;
    }

    UINT Delete(UINT nSize)
    {
		assert(nSize > 0);

        BYTE* nul = (BYTE*)VirtualAlloc(NULL,nSize,MEM_COMMIT,PAGE_READWRITE);
        Read(nul, nSize);
        VirtualFree(nul,0,MEM_RELEASE);
        return nSize;
    }

    BYTE* GetBuffer(UINT nPos = 0) { return (m_pBase + nPos); }
    UINT  GetBufferLen()           { return (m_pPtr - m_pBase); }
    UINT  GetMemSize()             { return (m_nSize); }

    // extern function

    // byte
    BOOL InsertByte(BYTE b){ return Insert(&b, 1);      }
    BOOL WriteByte(BYTE b) { return Write(&b, 1);       }
    BOOL ReadByte(BYTE& b) { return (1 == Read(&b, 1)); }

    // word
    BOOL InsertWord(WORD w)
    {
        w = ::htons(w);
        return Insert((BYTE*)&w, 2);
    }
    BOOL WriteWord(WORD w)
    {
        w = ::htons(w);
        return Write((BYTE*)&w, 2);
    }
    BOOL ReadWord(WORD &w)
    {
        if (2 != Read((BYTE*)&w, 2))
            return FALSE;

        w = ::ntohs(w);
        return TRUE;
    }

    // dword
    BOOL InsertDoubleWord(DWORD dw)
    {
        dw = ::htonl(dw);
        return Insert((BYTE*)&dw, 4);
    }
    BOOL WriteDoubleWord(DWORD dw)
    {
        dw = ::htons(dw);
        return Write((BYTE*)&dw, 4);
    }
    BOOL ReadDoubleWord(DWORD &dw)
    {
        if (4 != Read((BYTE*)&dw, 4))
            return FALSE;

        dw = ::ntohl(dw);
        return TRUE;
    }

    // string
    BOOL InsertString(LPCSTR pStr)
    {
        assert(pStr != NULL);
        const DWORD l = strlen(pStr);
        return Insert((BYTE*)pStr, l+1);
    }
    BOOL WriteString(LPCSTR pStr)
    {
        assert(pStr != NULL);
        const DWORD l = strlen(pStr);
        return Write((BYTE*)pStr, l+1);
    }
    BOOL ReadString(LPSTR pStr, DWORD& size)
    {
        const DWORD l = strlen((LPCSTR)m_pBase);

        if (l >= size)
        {
            size = l+1;
            return FALSE;
        }

        return Read((BYTE*)pStr, l+1);
    }

    // Block
    BOOL InsertBlock(BYTE* pBuffer, DWORD size)
    {
        assert(pBuffer != NULL);
        BOOL bRet = FALSE;

        if (Insert(pBuffer, size))
        {
            if (InsertDoubleWord(size))
            {
                bRet = TRUE;
            }
            else
            {
                Delete(size);
            }
        }

        return bRet;
    }
    BOOL WriteBlock(BYTE* pBuffer, DWORD size)
    {
        assert(pBuffer != NULL);
        BOOL bRet = FALSE;

        if (InsertDoubleWord(size))
        {
            if (Insert(pBuffer, size))
            {
                bRet = TRUE;
            }
            else
            {
                Delete(sizeof(DWORD));
            }
        }

        return bRet;
    }
    BOOL ReadBlock(BYTE* pBuffer, DWORD& size)
    {
        assert(pBuffer != NULL);
        BOOL bRet = FALSE;
        DWORD nSize = 0;

        if (ReadDoubleWord(nSize))
        {
            if (size >= nSize)
            {
                size = nSize;
                if (Read(pBuffer, nSize))
                {
                    bRet = TRUE;
                }
                else
                {
                    Delete(nSize);
                }
            }
        }

        return bRet;
    }
protected:
    UINT ReAllocateBuffer(UINT nRequestedSize)
    {
        if (nRequestedSize < GetMemSize())
            return 0;

        // Allocate new size
        UINT nNewSize = (UINT)ceil(((float)(nRequestedSize + 1)) / ((float)BUFFER_GROUP_LENGTH)) * BUFFER_GROUP_LENGTH;

        // New Copy Data Over
        BYTE* pNewBuffer = (BYTE*) VirtualAlloc(NULL,nNewSize,MEM_COMMIT,PAGE_READWRITE);

        UINT nBufferLen = GetBufferLen();
        CopyMemory(pNewBuffer,m_pBase,nBufferLen);

        if (m_pBase)
            VirtualFree(m_pBase,0,MEM_RELEASE);

        // Hand over the pointer
        m_pBase = pNewBuffer;

        // Realign position pointer
        m_pPtr = m_pBase + nBufferLen;

        m_nSize = nNewSize;

        return m_nSize;
    }

    UINT DeAllocateBuffer(UINT nRequestedSize)
    {
        if (nRequestedSize < GetBufferLen())
            return 0;

        // Allocate new size
        UINT nNewSize = (UINT)ceil(((float)(nRequestedSize + 1)) / ((float)BUFFER_GROUP_LENGTH)) * BUFFER_GROUP_LENGTH;

        // New Copy Data Over
        BYTE* pNewBuffer = (BYTE*) VirtualAlloc(NULL,nNewSize,MEM_COMMIT,PAGE_READWRITE);

        UINT nBufferLen = GetBufferLen();
        CopyMemory(pNewBuffer, m_pBase, nBufferLen);

        if (m_pBase)
            VirtualFree(m_pBase,0,MEM_RELEASE);

        // Hand over the pointer
        m_pBase = pNewBuffer;

        // Realign position pointer
        m_pPtr = m_pBase + nBufferLen;

        m_nSize = nNewSize;

        return m_nSize;
    }

protected:  // Attributes
    BYTE*  m_pBase;    // 内存块基地址
    BYTE*  m_pPtr;     // 当前内存操作地址
    UINT   m_nSize;    // 内存块大小

};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -