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.

417 lines
9.3 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. xstream.cpp
  7. FILE HISTORY:
  8. */
  9. #include "stdafx.h"
  10. #include "tfschar.h"
  11. #include "xstream.h"
  12. /*!--------------------------------------------------------------------------
  13. XferStream::XferStream
  14. -
  15. Author: KennT
  16. ---------------------------------------------------------------------------*/
  17. XferStream::XferStream(IStream *pstm, Mode mode)
  18. {
  19. m_spstm.Set(pstm);
  20. m_mode = mode;
  21. m_dwSize = 0;
  22. }
  23. /*!--------------------------------------------------------------------------
  24. XferStream::XferDWORD
  25. -
  26. Author: KennT
  27. ---------------------------------------------------------------------------*/
  28. HRESULT XferStream::XferDWORD(ULONG ulId, DWORD *pdwData)
  29. {
  30. ULONG id;
  31. HRESULT hr = hrOK;
  32. CORg( _XferObjectId(&ulId) );
  33. Assert( XFER_TYPE_FROM_TAG(ulId) == XFER_DWORD );
  34. if (XFER_TYPE_FROM_TAG(ulId) != XFER_DWORD)
  35. return E_INVALIDARG;
  36. CORg( _XferDWORD(pdwData) );
  37. Error:
  38. return hr;
  39. }
  40. /*!--------------------------------------------------------------------------
  41. XferStream::XferCString
  42. -
  43. Author: KennT
  44. ---------------------------------------------------------------------------*/
  45. HRESULT XferStream::XferCString(ULONG ulId, CString *pstData)
  46. {
  47. HRESULT hr = hrOK;
  48. ULONG cbLength;
  49. LPWSTR pswzData;
  50. CORg( _XferObjectId(&ulId) );
  51. Assert( XFER_TYPE_FROM_TAG(ulId) == XFER_STRING );
  52. if (XFER_TYPE_FROM_TAG(ulId) != XFER_STRING)
  53. return E_INVALIDARG;
  54. CORg( _XferCString(pstData) );
  55. Error:
  56. return hr;
  57. }
  58. /*!--------------------------------------------------------------------------
  59. XferStream::XferLARGEINTEGER
  60. -
  61. Author: EricDav
  62. ---------------------------------------------------------------------------*/
  63. HRESULT XferStream::XferLARGEINTEGER(ULONG ulId, LARGE_INTEGER *pliData)
  64. {
  65. ULONG id;
  66. HRESULT hr = hrOK;
  67. CORg( _XferObjectId(&ulId) );
  68. Assert( XFER_TYPE_FROM_TAG(ulId) == XFER_LARGEINTEGER );
  69. if (XFER_TYPE_FROM_TAG(ulId) != XFER_LARGEINTEGER)
  70. return E_INVALIDARG;
  71. CORg( _XferDWORD(&pliData->LowPart) );
  72. CORg( _XferLONG(&pliData->HighPart) );
  73. Error:
  74. return hr;
  75. }
  76. /*!--------------------------------------------------------------------------
  77. XferStream::XferDWORDArray
  78. -
  79. Author: KennT
  80. ---------------------------------------------------------------------------*/
  81. HRESULT XferStream::XferDWORDArray(ULONG ulId, ULONG *pcArray, DWORD *pdwArray)
  82. {
  83. HRESULT hr = hrOK;
  84. ULONG i;
  85. CORg( _XferObjectId(&ulId) );
  86. Assert( XFER_TYPE_FROM_TAG(ulId) == XFER_DWORD_ARRAY );
  87. if (XFER_TYPE_FROM_TAG(ulId) != XFER_DWORD_ARRAY)
  88. return E_INVALIDARG;
  89. CORg( _XferDWORD(pcArray) );
  90. for (i=0; i<*pcArray; i++)
  91. {
  92. // Xfer each dword
  93. hr = _XferDWORD(pdwArray+i);
  94. if (!FHrSucceeded(hr))
  95. break;
  96. }
  97. Error:
  98. return hr;
  99. }
  100. /*!--------------------------------------------------------------------------
  101. XferStream::XferCStringArray
  102. -
  103. Author: EricDav
  104. ---------------------------------------------------------------------------*/
  105. HRESULT XferStream::XferCStringArray(ULONG ulId, CStringArray *pstrArray)
  106. {
  107. HRESULT hr = hrOK;
  108. ULONG i, cArray;
  109. LPWSTR pswzData;
  110. CORg( _XferObjectId(&ulId) );
  111. Assert( XFER_TYPE_FROM_TAG(ulId) == XFER_STRING_ARRAY );
  112. if (XFER_TYPE_FROM_TAG(ulId) != XFER_STRING_ARRAY)
  113. return E_INVALIDARG;
  114. if (m_mode == XferStream::MODE_WRITE)
  115. {
  116. cArray = (ULONG)pstrArray->GetSize();
  117. }
  118. CORg( _XferDWORD(&cArray) );
  119. if (m_mode == XferStream::MODE_READ)
  120. {
  121. pstrArray->SetSize(cArray);
  122. }
  123. for (i = 0; i < cArray; i++)
  124. {
  125. _XferCString( &((*pstrArray)[i]) );
  126. if (!FHrSucceeded(hr))
  127. break;
  128. }
  129. Error:
  130. return hr;
  131. }
  132. /*!--------------------------------------------------------------------------
  133. XferStream::XferDWORDArray
  134. -
  135. Author: EricDav
  136. ---------------------------------------------------------------------------*/
  137. HRESULT XferStream::XferDWORDArray(ULONG ulId, CDWordArray * pdwArray)
  138. {
  139. HRESULT hr = hrOK;
  140. ULONG i, cArray;
  141. CORg( _XferObjectId(&ulId) );
  142. Assert( XFER_TYPE_FROM_TAG(ulId) == XFER_DWORD_ARRAY );
  143. if (XFER_TYPE_FROM_TAG(ulId) != XFER_DWORD_ARRAY)
  144. return E_INVALIDARG;
  145. if (m_mode == XferStream::MODE_WRITE)
  146. {
  147. cArray = (ULONG)pdwArray->GetSize();
  148. }
  149. CORg( _XferDWORD(&cArray) );
  150. if (m_mode == XferStream::MODE_READ)
  151. {
  152. pdwArray->SetSize(cArray);
  153. }
  154. for (i = 0; i < cArray; i++)
  155. {
  156. // Xfer each dword
  157. hr = _XferDWORD( &((*pdwArray)[i]) );
  158. if (!FHrSucceeded(hr))
  159. break;
  160. }
  161. Error:
  162. return hr;
  163. }
  164. /*!--------------------------------------------------------------------------
  165. XferStream::_XferObjectId
  166. -
  167. Author: KennT
  168. ---------------------------------------------------------------------------*/
  169. HRESULT XferStream::_XferObjectId(ULONG *pulId)
  170. {
  171. ULONG id = 0;
  172. HRESULT hr = hrOK;
  173. if (m_mode == XferStream::MODE_WRITE)
  174. id = *pulId;
  175. hr = _XferDWORD(&id);
  176. *pulId = id;
  177. return hr;
  178. }
  179. /*!--------------------------------------------------------------------------
  180. XferStream::_XferDWORD
  181. -
  182. Author: KennT
  183. ---------------------------------------------------------------------------*/
  184. HRESULT XferStream::_XferDWORD(DWORD *pdw)
  185. {
  186. ULONG cbBytes;
  187. HRESULT hr = hrOK;
  188. switch (m_mode)
  189. {
  190. case XferStream::MODE_READ:
  191. hr = m_spstm->Read(pdw, sizeof(DWORD), &cbBytes);
  192. Assert(cbBytes == sizeof(DWORD));
  193. if (cbBytes != sizeof(DWORD))
  194. hr = E_FAIL;
  195. break;
  196. case XferStream::MODE_WRITE:
  197. hr = m_spstm->Write(pdw, sizeof(DWORD), &cbBytes);
  198. Assert(cbBytes == sizeof(DWORD));
  199. break;
  200. case XferStream::MODE_SIZE:
  201. m_dwSize += sizeof(DWORD);
  202. break;
  203. default:
  204. hr = E_INVALIDARG;
  205. Panic1("Unknown XferStream mode! %d\n", m_mode);
  206. break;
  207. }
  208. return hr;
  209. }
  210. /*!--------------------------------------------------------------------------
  211. XferStream::_XferLONG
  212. -
  213. Author: KennT
  214. ---------------------------------------------------------------------------*/
  215. HRESULT XferStream::_XferLONG(LONG * pdl)
  216. {
  217. return _XferDWORD((DWORD *) pdl);
  218. }
  219. /*!--------------------------------------------------------------------------
  220. XferStream::_XferCString
  221. -
  222. Author: EricDav
  223. ---------------------------------------------------------------------------*/
  224. HRESULT XferStream::_XferCString(CString * pstData)
  225. {
  226. HRESULT hr = hrOK;
  227. ULONG cbLength;
  228. LPWSTR pswzData;
  229. Assert(pstData);
  230. switch (m_mode)
  231. {
  232. case XferStream::MODE_READ:
  233. hr = _XferDWORD(&cbLength);
  234. if (FHrSucceeded(hr))
  235. {
  236. pswzData = (LPWSTR) alloca(cbLength);
  237. hr = _XferBytes((LPBYTE) pswzData, cbLength);
  238. if (FHrSucceeded(hr))
  239. *pstData = W2CT(pswzData);
  240. }
  241. break;
  242. case XferStream::MODE_SIZE:
  243. case XferStream::MODE_WRITE:
  244. cbLength = (pstData->GetLength()+1) * sizeof(WCHAR);
  245. // round the length up to a multiple of 4
  246. cbLength = (cbLength + 3) & 0xFFFFFFFC;
  247. hr = _XferDWORD(&cbLength);
  248. if (FHrSucceeded(hr))
  249. {
  250. pswzData = (LPWSTR) alloca(cbLength);
  251. StrCpyWFromT(pswzData, (LPCTSTR) *pstData);
  252. hr = _XferBytes((LPBYTE) pswzData, cbLength);
  253. }
  254. break;
  255. default:
  256. hr = E_INVALIDARG;
  257. Panic1("Unknown XferStream mode! %d\n", m_mode);
  258. break;
  259. }
  260. return hr;
  261. }
  262. /*!--------------------------------------------------------------------------
  263. XferStream::_XferBytes
  264. -
  265. Author: KennT
  266. ---------------------------------------------------------------------------*/
  267. HRESULT XferStream::_XferBytes(LPBYTE pData, ULONG cbLength)
  268. {
  269. ULONG cbBytes;
  270. HRESULT hr = hrOK;
  271. switch (m_mode)
  272. {
  273. case XferStream::MODE_READ:
  274. hr = m_spstm->Read(pData, cbLength, &cbBytes);
  275. Assert(cbBytes == cbLength);
  276. break;
  277. case XferStream::MODE_WRITE:
  278. hr = m_spstm->Write(pData, cbLength, &cbBytes);
  279. Assert(cbBytes == cbLength);
  280. break;
  281. case XferStream::MODE_SIZE:
  282. m_dwSize += cbLength;
  283. break;
  284. default:
  285. hr = E_INVALIDARG;
  286. Panic1("Unknown XferStream mode! %d\n", m_mode);
  287. break;
  288. }
  289. return hr;
  290. }
  291. /*!--------------------------------------------------------------------------
  292. XferStream::XferColumnData
  293. -
  294. Author: KennT
  295. ---------------------------------------------------------------------------*/
  296. HRESULT XferStream::XferColumnData(ULONG ulId, ULONG *pcData, ColumnData *pData)
  297. {
  298. HRESULT hr = hrOK;
  299. ULONG i;
  300. CORg( _XferObjectId(&ulId) );
  301. Assert( XFER_TYPE_FROM_TAG(ulId) == XFER_COLUMNDATA_ARRAY );
  302. if (XFER_TYPE_FROM_TAG(ulId) != XFER_COLUMNDATA_ARRAY)
  303. return E_INVALIDARG;
  304. CORg( _XferDWORD(pcData) );
  305. for (i=0; i<*pcData; i++)
  306. {
  307. // Xfer each dword
  308. hr = _XferLONG(&(pData[i].m_nPosition));
  309. Assert(pData[i].m_nPosition != 0);
  310. if (FHrSucceeded(hr))
  311. hr = _XferDWORD(&(pData[i].m_dwWidth));
  312. if (!FHrSucceeded(hr))
  313. break;
  314. }
  315. Error:
  316. return hr;
  317. }
  318. /*!--------------------------------------------------------------------------
  319. XferStream::XferRect
  320. -
  321. Author: KennT
  322. ---------------------------------------------------------------------------*/
  323. HRESULT XferStream::XferRect(ULONG ulId, RECT *prc)
  324. {
  325. HRESULT hr = hrOK;
  326. CORg( _XferObjectId(&ulId) );
  327. Assert( XFER_TYPE_FROM_TAG(ulId) == XFER_RECT );
  328. if (XFER_TYPE_FROM_TAG(ulId) != XFER_RECT)
  329. return E_INVALIDARG;
  330. CORg( _XferDWORD((DWORD *) &(prc->top)) );
  331. CORg( _XferDWORD((DWORD *) &(prc->left)) );
  332. CORg( _XferDWORD((DWORD *) &(prc->bottom)) );
  333. CORg( _XferDWORD((DWORD *) &(prc->right)) );
  334. Error:
  335. return hr;
  336. }