Source code of Windows XP (NT5)
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.

161 lines
2.9 KiB

  1. /*++
  2. Copyright (C) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. //***************************************************************************
  8. //
  9. // CSeqStream - bogus class for manipulating blobs.
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include <windows.h>
  14. #include <comutil.h>
  15. #include <arena.h>
  16. #include <seqstream.h>
  17. CSeqStream::CSeqStream()
  18. {
  19. m_iPos = 0;
  20. m_cRef = 0;
  21. m_pBuffer = NULL;
  22. m_cBufSize = 0;
  23. //The constructor AddRef's
  24. AddRef();
  25. }
  26. CSeqStream::~CSeqStream()
  27. {
  28. //Shouldn't have any references left
  29. CWin32DefaultArena::WbemMemFree(m_pBuffer);
  30. }
  31. ULONG CSeqStream::AddRef(void)
  32. {
  33. return ++m_cRef;
  34. }
  35. ULONG CSeqStream::Release(void)
  36. {
  37. if(--m_cRef)
  38. return m_cRef;
  39. delete this;
  40. return 0;
  41. }
  42. HRESULT CSeqStream::QueryInterface(REFIID riid, void** ppv)
  43. {
  44. *ppv = NULL;
  45. if (riid == IID_IUnknown)
  46. *ppv = this;
  47. if (riid == IID_ISequentialStream)
  48. *ppv = this;
  49. if(*ppv)
  50. {
  51. ((IUnknown*)*ppv)->AddRef();
  52. return S_OK;
  53. }
  54. return E_NOINTERFACE;
  55. }
  56. BOOL CSeqStream::Seek(ULONG iPos)
  57. {
  58. //Reset the current buffer position
  59. m_iPos = iPos;
  60. return TRUE;
  61. }
  62. BOOL CSeqStream::Clear()
  63. {
  64. m_iPos = 0;
  65. m_cBufSize = 0;
  66. CWin32DefaultArena::WbemMemFree(m_pBuffer);
  67. m_pBuffer = NULL;
  68. return TRUE;
  69. }
  70. BOOL CSeqStream::CompareData(void* pBuffer)
  71. {
  72. return memcmp(pBuffer, m_pBuffer, m_cBufSize)==0;
  73. }
  74. HRESULT CSeqStream::Read(void *pv, ULONG cb, ULONG* pcbRead)
  75. {
  76. //Parameter checking
  77. if(pcbRead)
  78. *pcbRead = 0;
  79. if(!pv)
  80. return STG_E_INVALIDPOINTER;
  81. if(cb == 0)
  82. return S_OK;
  83. //Actual code
  84. ULONG cBytesLeft = m_cBufSize - m_iPos;
  85. ULONG cBytesRead = cb > cBytesLeft ? cBytesLeft : cb;
  86. //if no more bytes to retrieve return
  87. if(cBytesLeft == 0)
  88. return S_FALSE;
  89. //Copy to users buffer the number of bytes requested or remaining
  90. memcpy(pv, (void*)((BYTE*)m_pBuffer + m_iPos), cBytesRead);
  91. m_iPos += cBytesRead;
  92. if(pcbRead)
  93. *pcbRead = cBytesRead;
  94. if(cb != cBytesRead)
  95. return S_FALSE;
  96. return S_OK;
  97. }
  98. HRESULT CSeqStream::Write(const void *pv, ULONG cb, ULONG* pcbWritten)
  99. {
  100. if(!pv)
  101. return STG_E_INVALIDPOINTER;
  102. if(pcbWritten)
  103. *pcbWritten = 0;
  104. if(cb == 0)
  105. return S_OK;
  106. //Enlarge the current buffer
  107. m_cBufSize += cb;
  108. //Need to append to the end of the stream
  109. // m_pBuffer = CWin32DefaultArena::WbemMemReAlloc(m_pBuffer, m_cBufSize);
  110. if (m_pBuffer)
  111. m_pBuffer = CWin32DefaultArena::WbemMemReAlloc(m_pBuffer, m_cBufSize);
  112. else
  113. m_pBuffer = CWin32DefaultArena::WbemMemAlloc(m_cBufSize);
  114. memcpy((void*)((BYTE*)m_pBuffer + m_iPos), pv, cb);
  115. if(pcbWritten)
  116. *pcbWritten = cb;
  117. return S_OK;
  118. }