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.

266 lines
5.9 KiB

  1. //
  2. // Microsoft Windows Media Technologies
  3. // Copyright (C) Microsoft Corporation, 1999 - 2001. All rights reserved.
  4. //
  5. //
  6. // OperationHelper.cpp : Implementation of COperationHelper
  7. //
  8. #include "progPCH.h"
  9. #include "ProgHelp.h"
  10. #include "Sac.h"
  11. #include "SCClient.h"
  12. #include "OperationHelper.h"
  13. #include "StrSafe.h"
  14. //
  15. // Constructor/Destructor
  16. //
  17. COperationHelper::COperationHelper()
  18. {
  19. m_eStatus = E_OPERATION_NOTHING;
  20. m_hFile = INVALID_HANDLE_VALUE;
  21. memset( m_pszFileName, __T('\0'), MAX_PATH * sizeof( TCHAR ) ); //memset is fine; sizeof arg1 == sizeof arg3
  22. m_pSACClient = NULL;
  23. }
  24. COperationHelper::~COperationHelper()
  25. {
  26. if( m_hFile != INVALID_HANDLE_VALUE )
  27. {
  28. CloseHandle(m_hFile);
  29. }
  30. }
  31. //
  32. // IWMDMOperation
  33. //
  34. // IWMDMOperation::BeginRead
  35. //
  36. HRESULT COperationHelper::BeginRead()
  37. {
  38. // m_eStatus = E_OPERATION_RECEIVING;
  39. return E_NOTIMPL;
  40. }
  41. // IWMDMOperation::BeginWrite
  42. // Start sending data
  43. HRESULT COperationHelper::BeginWrite()
  44. {
  45. m_eStatus = E_OPERATION_SENDING;
  46. if( _tcslen( m_pszFileName ) == 0 )
  47. {
  48. return E_FAIL;
  49. }
  50. m_hFile = CreateFile( m_pszFileName, // file name
  51. GENERIC_READ, // access mode
  52. FILE_SHARE_READ, // share mode
  53. NULL, // SD
  54. OPEN_EXISTING, // how to create
  55. FILE_ATTRIBUTE_NORMAL, // file attributes
  56. NULL ); // handle to template file
  57. if( m_hFile == INVALID_HANDLE_VALUE )
  58. {
  59. return E_FAIL;
  60. }
  61. return S_OK;
  62. }
  63. // IWMDMOperation::GetObjectName
  64. //
  65. HRESULT COperationHelper::GetObjectName(LPWSTR pwszName, UINT nMaxChars)
  66. {
  67. USES_CONVERSION;
  68. TCHAR* pszNoPath;
  69. if (pwszName == NULL)
  70. {
  71. return E_POINTER;
  72. }
  73. if (nMaxChars == 0)
  74. {
  75. return E_INVALIDARG;
  76. }
  77. if( m_pszFileName == NULL || _tcslen( m_pszFileName ) == 0 )
  78. {
  79. return E_FAIL;
  80. }
  81. pszNoPath = _tcsrchr( m_pszFileName, __T('\\') );
  82. if( pszNoPath == NULL ) pszNoPath = m_pszFileName;
  83. else pszNoPath++;
  84. WCHAR* wcp = A2W(pszNoPath);
  85. return HRESULT_CODE(StringCchCopyW(pwszName, nMaxChars, wcp));
  86. }
  87. // IWMDMOperation::SetObjectName
  88. //
  89. HRESULT COperationHelper::SetObjectName(LPWSTR pwszName, UINT nMaxChars)
  90. {
  91. return E_NOTIMPL;
  92. }
  93. // IWMDMOperation::GetObjectAttributes
  94. //
  95. HRESULT COperationHelper::GetObjectAttributes(DWORD *pdwAttributes, _WAVEFORMATEX *pFormat)
  96. {
  97. DWORD dwAttrib;
  98. USES_CONVERSION;
  99. if( pdwAttributes == NULL ) return E_INVALIDARG;
  100. if( _tcslen( m_pszFileName ) == 0 ) return E_FAIL;
  101. dwAttrib = GetFileAttributes(m_pszFileName);
  102. *pdwAttributes = 0;
  103. if( dwAttrib & FILE_ATTRIBUTE_DIRECTORY )
  104. *pdwAttributes |= WMDM_FILE_ATTR_FOLDER;
  105. else
  106. *pdwAttributes |= WMDM_FILE_ATTR_FILE;
  107. *pdwAttributes |= WMDM_FILE_CREATE_OVERWRITE;
  108. return S_OK;
  109. }
  110. // IWMDMOperation::SetObjectAttributes
  111. //
  112. HRESULT COperationHelper::SetObjectAttributes(DWORD dwAttributes, _WAVEFORMATEX *pFormat)
  113. {
  114. return E_NOTIMPL;
  115. }
  116. // IWMDMOperation::GetObjectTotalSize
  117. //
  118. HRESULT COperationHelper::GetObjectTotalSize(DWORD *pdwSize, DWORD *pdwHighSize)
  119. {
  120. if( pdwSize == NULL || m_hFile == INVALID_HANDLE_VALUE )
  121. {
  122. return E_INVALIDARG;
  123. }
  124. *pdwSize = GetFileSize(m_hFile, pdwHighSize );
  125. return S_OK;
  126. }
  127. // IWMDMOperation::SetObjectTotalSize
  128. //
  129. HRESULT COperationHelper::SetObjectTotalSize(DWORD dwSize, DWORD dwHighSize)
  130. {
  131. return E_NOTIMPL;
  132. }
  133. // IWMDMOperation::TransferObjectData
  134. //
  135. HRESULT COperationHelper::TransferObjectData(BYTE *pData,DWORD *pdwSize, BYTE *pbMac)
  136. {
  137. DWORD dwReadLen;
  138. DWORD dwMacVerifyLen = WMDM_MAC_LENGTH;
  139. DWORD dwMacLen = WMDM_MAC_LENGTH;
  140. if( pData == NULL || pdwSize == NULL || pbMac == NULL )
  141. {
  142. return E_INVALIDARG;
  143. }
  144. if( m_eStatus != E_OPERATION_SENDING && m_eStatus != E_OPERATION_RECEIVING )
  145. {
  146. return E_FAIL;
  147. }
  148. // Sending
  149. if( m_eStatus == E_OPERATION_SENDING )
  150. {
  151. if( m_pSACClient == NULL )
  152. {
  153. return E_FAIL;
  154. }
  155. dwReadLen = *pdwSize;
  156. if( ReadFile(m_hFile, pData, dwReadLen, pdwSize, NULL) == FALSE )
  157. {
  158. return E_FAIL;
  159. }
  160. HMAC hMAC;
  161. // Create the MAC to return to caller
  162. m_pSACClient->MACInit(&hMAC);
  163. m_pSACClient->MACUpdate(hMAC, (BYTE*)(pData), *pdwSize);
  164. m_pSACClient->MACUpdate(hMAC, (BYTE*)(pdwSize), sizeof(DWORD));
  165. m_pSACClient->MACFinal(hMAC, (BYTE*)pbMac);
  166. m_pSACClient->EncryptParam((BYTE*)(pData), *pdwSize);
  167. }
  168. // Receiving
  169. else
  170. {
  171. return E_NOTIMPL;
  172. }
  173. return S_OK;
  174. }
  175. // IWMDMOperation::End
  176. //
  177. HRESULT COperationHelper::End(HRESULT *phCompletionCode, IUnknown *pNewObject)
  178. {
  179. m_eStatus = E_OPERATION_NOTHING;
  180. CloseHandle(m_hFile);
  181. m_hFile = INVALID_HANDLE_VALUE;
  182. return S_OK;
  183. }
  184. //
  185. // IWMDMOperation
  186. //
  187. // IWMDMOperation::SetFileName
  188. //
  189. STDMETHODIMP COperationHelper::SetFileName(LPWSTR pwszFileName)
  190. {
  191. USES_CONVERSION;
  192. if (pwszFileName == NULL)
  193. {
  194. return E_POINTER;
  195. }
  196. if (pwszFileName[0] == L'\0')
  197. {
  198. return E_INVALIDARG;
  199. }
  200. return HRESULT_CODE(StringCbCopy(m_pszFileName, sizeof(m_pszFileName), W2T(pwszFileName)));
  201. }
  202. // IWMDMOperation::SetSAC
  203. // Passing pointer to CSecureChannelClient object
  204. STDMETHODIMP COperationHelper::SetSAC( void* pSACClient)
  205. {
  206. if( pSACClient == NULL )
  207. {
  208. return E_INVALIDARG;
  209. }
  210. if( m_pSACClient != NULL )
  211. {
  212. return E_FAIL;
  213. }
  214. m_pSACClient = (CSecureChannelClient*)pSACClient;
  215. return S_OK;
  216. }