Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.2 KiB

  1. #ifndef _BUFFER_H
  2. #define _BUFFER_H
  3. class CBuffer
  4. {
  5. public:
  6. LPBYTE m_pBuffer,
  7. m_pCurrent;
  8. DWORD_PTR m_dwSize;
  9. enum ALIGN_TYPE
  10. {
  11. ALIGN_NONE,
  12. ALIGN_DWORD,
  13. ALIGN_QWORD,
  14. ALIGN_DWORD_PTR
  15. };
  16. CBuffer(LPCVOID pBuffer, DWORD_PTR dwSize, ALIGN_TYPE type = ALIGN_NONE);
  17. CBuffer(DWORD_PTR dwSize = 0);
  18. CBuffer(const CBuffer &other);
  19. virtual ~CBuffer();
  20. BOOL Write(LPCSTR szVal);
  21. BOOL Write(LPCWSTR szVal);
  22. BOOL Write(DWORD dwVal);
  23. BOOL Write(DWORD64 dwVal);
  24. BOOL Write(BYTE cVal);
  25. BOOL Write(WORD wVal);
  26. BOOL Write(LPCVOID pBuffer, DWORD dwSize);
  27. BOOL WriteAlignedLenString(LPCWSTR szVal);
  28. DWORD ReadDWORD();
  29. BYTE ReadBYTE();
  30. WORD ReadWORD();
  31. LPWSTR ReadAlignedLenString(DWORD *pdwBytes);
  32. short int ReadShortInt();
  33. BOOL Read(LPVOID pBuffer, DWORD dwToRead);
  34. void Reset() {m_pCurrent = m_pBuffer;}
  35. void Reset(LPCVOID pBuffer, DWORD_PTR dwSize);
  36. void Reset(DWORD_PTR dwSize) { Reset(NULL, dwSize); }
  37. BOOL Resize(DWORD_PTR dwNewSize);
  38. BOOL Grow(DWORD_PTR dwGrowBy)
  39. {
  40. return Resize(m_dwSize + dwGrowBy);
  41. }
  42. // Will make sure dwSize bytes are available. If not, it will call
  43. // Resize. If that fails, it throws a CBufferException.
  44. void AssureSizeRemains(DWORD_PTR dwSize);
  45. BOOL IsEOF() {return m_pCurrent >= m_pBuffer + m_dwSize;}
  46. DWORD_PTR GetUsedSize() {return m_pCurrent - m_pBuffer;}
  47. DWORD_PTR GetUnusedSize() {return m_dwSize - (m_pCurrent - m_pBuffer);}
  48. void SetUsedSize(DWORD_PTR dwSize) {m_pCurrent = m_pBuffer + dwSize;}
  49. void SetEOF() { m_pCurrent = m_pBuffer + m_dwSize; }
  50. // A safe way to move m_pCurrent.
  51. void MoveCurrent(int iOffset)
  52. {
  53. if (iOffset > 0)
  54. AssureSizeRemains(iOffset);
  55. m_pCurrent += iOffset;
  56. }
  57. BOOL operator ==(const CBuffer &other);
  58. const CBuffer& operator =(const CBuffer &other);
  59. // This gets called when the buffer is realloced. Override with your own
  60. // behavior if necessary.
  61. virtual void OnResize()
  62. {
  63. }
  64. protected:
  65. BOOL m_bAllocated;
  66. void Free();
  67. };
  68. #endif