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.

148 lines
4.1 KiB

  1. // --------------------------------------------------------------------------------
  2. // ByteBuff.cpp
  3. // Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  4. // Steven J. Bailey
  5. // --------------------------------------------------------------------------------
  6. #include "pch.hxx"
  7. #include "bytebuff.h"
  8. // --------------------------------------------------------------------------------
  9. // CByteBuffer::CByteBuffer
  10. // --------------------------------------------------------------------------------
  11. CByteBuffer::CByteBuffer(LPBYTE pb /* =NULL */, ULONG cbAlloc /* =0 */, ULONG cb /* =0 */, ULONG i /* =0 */)
  12. {
  13. m_cRef = 1;
  14. m_dwState = 0;
  15. m_cbGrow = BYTEBUFF_GROW;
  16. m_buffer.pb = pb;
  17. m_buffer.pbStatic = pb;
  18. m_buffer.cbAlloc = cbAlloc;
  19. m_buffer.cb = cb;
  20. m_buffer.i = i;
  21. }
  22. // --------------------------------------------------------------------------------
  23. // CByteBuffer::CByteBuffer
  24. // --------------------------------------------------------------------------------
  25. void CByteBuffer::Init(LPBYTE pb, ULONG cbAlloc, ULONG cb, ULONG i)
  26. {
  27. m_buffer.pb = pb;
  28. m_buffer.cb = cb;
  29. m_buffer.i = i;
  30. m_buffer.cbAlloc = cbAlloc;
  31. m_buffer.pbStatic = pb;
  32. }
  33. // --------------------------------------------------------------------------------
  34. // CByteBuffer::CByteBuffer
  35. // --------------------------------------------------------------------------------
  36. CByteBuffer::~CByteBuffer(void)
  37. {
  38. // Free memory if not equal to static
  39. if (m_buffer.pb != m_buffer.pbStatic)
  40. g_pMalloc->Free(m_buffer.pb);
  41. }
  42. // --------------------------------------------------------------------------------
  43. // CByteBuffer::Release
  44. // --------------------------------------------------------------------------------
  45. STDMETHODIMP_(ULONG) CByteBuffer::Release(void)
  46. {
  47. if (0 != --m_cRef)
  48. return m_cRef;
  49. delete this;
  50. return 0;
  51. }
  52. // --------------------------------------------------------------------------------
  53. // CByteBuffer::_HrRealloc
  54. // --------------------------------------------------------------------------------
  55. HRESULT CByteBuffer::_HrRealloc(DWORD cbAlloc)
  56. {
  57. // Locals
  58. HRESULT hr=S_OK;
  59. LPBYTE pbAlloc=NULL;
  60. // This should have been checked
  61. Assert(cbAlloc > m_buffer.cbAlloc);
  62. // Currently using static ?
  63. if (m_buffer.pb == m_buffer.pbStatic)
  64. {
  65. // Allocate
  66. CHECKALLOC(pbAlloc = (LPBYTE)g_pMalloc->Alloc(cbAlloc));
  67. // Copy Data into pbAlloc
  68. CopyMemory(pbAlloc, m_buffer.pb, min(cbAlloc, m_buffer.cb));
  69. }
  70. // Otherwise, realloc
  71. else
  72. {
  73. // Reallocate
  74. CHECKALLOC(pbAlloc = (LPBYTE)g_pMalloc->Realloc(m_buffer.pb, cbAlloc));
  75. }
  76. // Save pbAlloc
  77. m_buffer.pb = pbAlloc;
  78. // Save cbAlloc
  79. m_buffer.cbAlloc = cbAlloc;
  80. exit:
  81. // Done
  82. return hr;
  83. }
  84. // --------------------------------------------------------------------------------
  85. // CByteBuffer::Append
  86. // --------------------------------------------------------------------------------
  87. HRESULT CByteBuffer::Append(LPBYTE pbData, ULONG cbData)
  88. {
  89. // Locals
  90. HRESULT hr=S_OK;
  91. // Get Bigger and need to allocate
  92. if (m_buffer.cb + cbData > m_buffer.cbAlloc)
  93. {
  94. // Realloc
  95. CHECKHR(hr = _HrRealloc(m_buffer.cb + cbData + m_cbGrow));
  96. }
  97. // Append the data
  98. CopyMemory(m_buffer.pb + m_buffer.cb, pbData, cbData);
  99. // Save Size
  100. m_buffer.cb += cbData;
  101. exit:
  102. // Done
  103. return hr;
  104. }
  105. // --------------------------------------------------------------------------------
  106. // CByteBuffer::SetSize
  107. // --------------------------------------------------------------------------------
  108. HRESULT CByteBuffer::SetSize(DWORD cb)
  109. {
  110. // Locals
  111. HRESULT hr=S_OK;
  112. // Get Bigger and need to allocate
  113. if (cb > m_buffer.cb && cb > m_buffer.cbAlloc)
  114. {
  115. // Realloc
  116. CHECKHR(hr = _HrRealloc(cb + m_cbGrow));
  117. }
  118. // Save Size
  119. m_buffer.cb = cb;
  120. // Adjust Index
  121. if (m_buffer.i > cb)
  122. m_buffer.i = cb;
  123. exit:
  124. // Done
  125. return hr;
  126. }