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.

263 lines
5.1 KiB

  1. #include "stdafx.h"
  2. #include "imgutil.h"
  3. #include "resource.h"
  4. #include "csnfstrm.h"
  5. CSniffStream::CSniffStream() :
  6. m_pbBuffer( NULL ),
  7. m_iOffset( 0 ),
  8. m_nBufferSize( 0 ),
  9. m_nValidBytes( 0 ),
  10. m_iNextFreeByte( 0 )
  11. {
  12. }
  13. CSniffStream::~CSniffStream()
  14. {
  15. delete m_pbBuffer;
  16. }
  17. STDMETHODIMP CSniffStream::Clone( IStream** ppStream )
  18. {
  19. if( ppStream == NULL )
  20. {
  21. return( E_POINTER );
  22. }
  23. *ppStream = NULL;
  24. return( E_NOTIMPL );
  25. }
  26. STDMETHODIMP CSniffStream::Commit( DWORD dwFlags )
  27. {
  28. (void)dwFlags;
  29. return( E_NOTIMPL );
  30. }
  31. STDMETHODIMP CSniffStream::CopyTo( IStream* pStream, ULARGE_INTEGER nBytes,
  32. ULARGE_INTEGER* pnBytesRead, ULARGE_INTEGER* pnBytesWritten )
  33. {
  34. (void)pStream;
  35. (void)nBytes;
  36. (void)pnBytesRead;
  37. (void)pnBytesWritten;
  38. return( E_NOTIMPL );
  39. }
  40. STDMETHODIMP CSniffStream::LockRegion( ULARGE_INTEGER iOffset,
  41. ULARGE_INTEGER nBytes, DWORD dwLockType )
  42. {
  43. (void)iOffset;
  44. (void)nBytes;
  45. (void)dwLockType;
  46. return( E_NOTIMPL );
  47. }
  48. STDMETHODIMP CSniffStream::Read( void* pBuffer, ULONG nBytes,
  49. ULONG* pnBytesRead )
  50. {
  51. ULONG nBytesToRead = 0;
  52. ULONG nBytesLeft;
  53. ULONG nBytesRead;
  54. BYTE* pbBuffer;
  55. HRESULT hResult;
  56. if( pnBytesRead != NULL )
  57. {
  58. *pnBytesRead = 0;
  59. }
  60. if( pBuffer == NULL )
  61. {
  62. return( E_POINTER );
  63. }
  64. if( nBytes == 0 )
  65. {
  66. return( E_INVALIDARG );
  67. }
  68. pbBuffer = LPBYTE( pBuffer );
  69. nBytesLeft = nBytes;
  70. if( m_pbBuffer != NULL )
  71. {
  72. _ASSERTE( m_nValidBytes > 0 );
  73. nBytesToRead = min( nBytesLeft, m_nValidBytes );
  74. memcpy( pbBuffer, &m_pbBuffer[m_iOffset], nBytesToRead );
  75. nBytesLeft -= nBytesToRead;
  76. if( pnBytesRead != NULL )
  77. {
  78. *pnBytesRead += nBytesToRead;
  79. }
  80. m_nValidBytes -= nBytesToRead;
  81. pbBuffer += nBytesToRead;
  82. m_iOffset += nBytesToRead;
  83. if( m_nValidBytes == 0 )
  84. {
  85. delete m_pbBuffer;
  86. m_pbBuffer = NULL;
  87. m_nValidBytes = 0;
  88. m_iNextFreeByte = 0;
  89. m_iOffset = 0;
  90. m_nBufferSize = 0;
  91. }
  92. }
  93. if( nBytesLeft == 0 )
  94. {
  95. return( S_OK );
  96. }
  97. _ASSERTE( m_pbBuffer == NULL );
  98. hResult = m_pStream->Read( pbBuffer, nBytesLeft, &nBytesRead );
  99. if( hResult == S_OK && pnBytesRead != NULL )
  100. {
  101. *pnBytesRead += nBytesRead;
  102. }
  103. return( hResult );
  104. }
  105. STDMETHODIMP CSniffStream::Revert()
  106. {
  107. return( E_NOTIMPL );
  108. }
  109. STDMETHODIMP CSniffStream::Seek( LARGE_INTEGER nDisplacement, DWORD dwOrigin,
  110. ULARGE_INTEGER* piNewPosition )
  111. {
  112. (void)nDisplacement;
  113. (void)dwOrigin;
  114. (void)piNewPosition;
  115. return( E_NOTIMPL );
  116. }
  117. STDMETHODIMP CSniffStream::SetSize( ULARGE_INTEGER nNewSize )
  118. {
  119. (void)nNewSize;
  120. return( E_NOTIMPL );
  121. }
  122. STDMETHODIMP CSniffStream::Stat( STATSTG* pStatStg, DWORD dwFlags )
  123. {
  124. (void)pStatStg;
  125. (void)dwFlags;
  126. return( E_NOTIMPL );
  127. }
  128. STDMETHODIMP CSniffStream::UnlockRegion( ULARGE_INTEGER iOffset,
  129. ULARGE_INTEGER nBytes, DWORD dwLockType )
  130. {
  131. (void)iOffset;
  132. (void)nBytes;
  133. (void)dwLockType;
  134. return( E_NOTIMPL );
  135. }
  136. STDMETHODIMP CSniffStream::Write( const void* pBuffer, ULONG nBytes,
  137. ULONG* pnBytesWritten )
  138. {
  139. (void)pBuffer;
  140. (void)nBytes;
  141. (void)pnBytesWritten;
  142. return( E_NOTIMPL );
  143. }
  144. STDMETHODIMP CSniffStream::Init( IStream* pStream )
  145. {
  146. if( pStream == NULL )
  147. {
  148. return( E_INVALIDARG );
  149. }
  150. m_pStream = pStream;
  151. return( S_OK );
  152. }
  153. STDMETHODIMP CSniffStream::Peek( void* pBuffer, ULONG nBytes,
  154. ULONG* pnBytesRead )
  155. {
  156. BYTE* pbNewBuffer;
  157. HRESULT hResult;
  158. ULONG nBytesToRead;
  159. ULONG nBytesRead;
  160. if( pnBytesRead != NULL )
  161. {
  162. *pnBytesRead = 0;
  163. }
  164. if( pBuffer == NULL )
  165. {
  166. return( E_POINTER );
  167. }
  168. if( nBytes == 0 )
  169. {
  170. return( E_INVALIDARG );
  171. }
  172. hResult = S_OK;
  173. if( nBytes > m_nValidBytes )
  174. {
  175. // We have to read from the stream
  176. if( nBytes > (m_nBufferSize-m_iOffset) )
  177. {
  178. // We need more buffer space
  179. pbNewBuffer = new BYTE[nBytes];
  180. if( pbNewBuffer == NULL )
  181. {
  182. return( E_OUTOFMEMORY );
  183. }
  184. if( m_pbBuffer != NULL )
  185. {
  186. memcpy( pbNewBuffer, &m_pbBuffer[m_iOffset], m_nValidBytes );
  187. }
  188. delete m_pbBuffer;
  189. m_pbBuffer = pbNewBuffer;
  190. m_nBufferSize = nBytes;
  191. m_iOffset = 0;
  192. m_iNextFreeByte = m_nValidBytes;
  193. }
  194. hResult = m_pStream->Read( &m_pbBuffer[m_iNextFreeByte],
  195. nBytes-m_nValidBytes, &nBytesRead );
  196. m_iNextFreeByte += nBytesRead;
  197. m_nValidBytes += nBytesRead;
  198. }
  199. // Copy as much as we can from our buffer
  200. nBytesToRead = min( nBytes, m_nValidBytes );
  201. if( nBytesToRead > 0 )
  202. {
  203. memcpy( pBuffer, &m_pbBuffer[m_iOffset], nBytesToRead );
  204. if( pnBytesRead != NULL )
  205. {
  206. *pnBytesRead += nBytesToRead;
  207. }
  208. }
  209. if( nBytesToRead == nBytes )
  210. {
  211. return( S_OK );
  212. }
  213. else
  214. {
  215. return( hResult );
  216. }
  217. }