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.

302 lines
7.6 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // strmio.cpp - IStream i/o functions
  24. ////
  25. #include "winlocal.h"
  26. #include <mmsystem.h>
  27. #include <mapi.h>
  28. #include <mapidefs.h>
  29. #include "strmio.h"
  30. #include "trace.h"
  31. ////
  32. // private definitions
  33. ////
  34. // helper functions
  35. //
  36. static LRESULT StreamIoOpen(LPMMIOINFO lpmmioinfo, LPTSTR lpszFileName);
  37. static LRESULT StreamIoClose(LPMMIOINFO lpmmioinfo, UINT uFlags);
  38. static LRESULT StreamIoRead(LPMMIOINFO lpmmioinfo, HPSTR pch, LONG cch);
  39. static LRESULT StreamIoWrite(LPMMIOINFO lpmmioinfo, const HPSTR pch, LONG cch, BOOL fFlush);
  40. static LRESULT StreamIoSeek(LPMMIOINFO lpmmioinfo, LONG lOffset, int iOrigin);
  41. static LRESULT StreamIoRename(LPMMIOINFO lpmmioinfo, LPCTSTR lpszFileName, LPCTSTR lpszNewFileName);
  42. ////
  43. // public functions
  44. ////
  45. // StreamIoProc - i/o procedure for IStream data
  46. // <lpmmioinfo> (i/o) information about open file
  47. // <uMessage> (i) message indicating the requested I/O operation
  48. // <lParam1> (i) message specific parameter
  49. // <lParam2> (i) message specific parameter
  50. // returns 0 if message not recognized, otherwise message specific value
  51. //
  52. // NOTE: the address of this function should be passed to the WavOpen()
  53. // or mmioInstallIOProc() functions for accessing IStream data.
  54. //
  55. LRESULT DLLEXPORT CALLBACK StreamIOProc(LPSTR lpmmioinfo,
  56. UINT uMessage, LPARAM lParam1, LPARAM lParam2)
  57. {
  58. BOOL fSuccess = TRUE;
  59. LRESULT lResult = 0;
  60. if (lpmmioinfo == NULL)
  61. fSuccess = TraceFALSE(NULL);
  62. else switch (uMessage)
  63. {
  64. case MMIOM_OPEN:
  65. lResult = StreamIoOpen((LPMMIOINFO) lpmmioinfo,
  66. (LPTSTR) lParam1);
  67. break;
  68. case MMIOM_CLOSE:
  69. lResult = StreamIoClose((LPMMIOINFO) lpmmioinfo,
  70. (UINT) lParam1);
  71. break;
  72. case MMIOM_READ:
  73. lResult = StreamIoRead((LPMMIOINFO) lpmmioinfo,
  74. (HPSTR) lParam1, (LONG) lParam2);
  75. break;
  76. case MMIOM_WRITE:
  77. lResult = StreamIoWrite((LPMMIOINFO) lpmmioinfo,
  78. (const HPSTR) lParam1, (LONG) lParam2, FALSE);
  79. break;
  80. case MMIOM_WRITEFLUSH:
  81. lResult = StreamIoWrite((LPMMIOINFO) lpmmioinfo,
  82. (const HPSTR) lParam1, (LONG) lParam2, TRUE);
  83. break;
  84. case MMIOM_SEEK:
  85. lResult = StreamIoSeek((LPMMIOINFO) lpmmioinfo,
  86. (LONG) lParam1, (int) lParam2);
  87. break;
  88. case MMIOM_RENAME:
  89. lResult = StreamIoRename((LPMMIOINFO) lpmmioinfo,
  90. (LPCTSTR) lParam1, (LPCTSTR) lParam2);
  91. break;
  92. default:
  93. lResult = 0;
  94. break;
  95. }
  96. return lResult;
  97. }
  98. ////
  99. // installable file i/o procedures
  100. ////
  101. static LRESULT StreamIoOpen(LPMMIOINFO lpmmioinfo, LPTSTR lpszFileName)
  102. {
  103. BOOL fSuccess = TRUE;
  104. LPSTREAM lpStream;
  105. TracePrintf_0(NULL, 5,
  106. TEXT("StreamIoOpen\n"));
  107. // stream pointer is within first element of info array
  108. //
  109. if ((lpStream = (LPSTREAM)(DWORD_PTR)lpmmioinfo->adwInfo[0]) == NULL)
  110. fSuccess = TraceFALSE(NULL);
  111. // seek to the beginning of the stream
  112. //
  113. else if (StreamIoSeek(lpmmioinfo, 0, 0) != 0)
  114. fSuccess = TraceFALSE(NULL);
  115. else
  116. lpStream->AddRef();
  117. // return the same error code given by mmioOpen
  118. //
  119. return fSuccess ? lpmmioinfo->wErrorRet = 0 : MMIOERR_CANNOTOPEN;
  120. }
  121. static LRESULT StreamIoClose(LPMMIOINFO lpmmioinfo, UINT uFlags)
  122. {
  123. BOOL fSuccess = TRUE;
  124. LPSTREAM lpStream = (LPSTREAM)(DWORD_PTR)lpmmioinfo->adwInfo[0];
  125. UINT uRet = MMIOERR_CANNOTCLOSE;
  126. TracePrintf_0(NULL, 5,
  127. TEXT("StreamIoClose\n"));
  128. // close the stream
  129. //
  130. lpStream->Release();
  131. lpmmioinfo->adwInfo[0] = (DWORD) NULL;
  132. return fSuccess ? 0 : uRet;
  133. }
  134. static LRESULT StreamIoRead(LPMMIOINFO lpmmioinfo, HPSTR pch, LONG cch)
  135. {
  136. BOOL fSuccess = TRUE;
  137. LPSTREAM lpStream = (LPSTREAM)(DWORD_PTR)lpmmioinfo->adwInfo[0];
  138. HRESULT hr;
  139. LONG lBytesRead = 0L;
  140. TracePrintf_1(NULL, 5,
  141. TEXT("StreamIoRead (%ld)\n"),
  142. (long) cch);
  143. if (cch <= 0)
  144. lBytesRead = 0; // nothing to do
  145. // read
  146. //
  147. else if ((hr = lpStream->Read((LPVOID) pch,
  148. (ULONG) cch, (ULONG FAR *) &lBytesRead)) != S_OK)
  149. {
  150. fSuccess = TraceFALSE(NULL);
  151. TracePrintf_2(NULL, 5,
  152. TEXT("IStream:Read failed (%ld, %ld)\n"),
  153. (long) hr,
  154. (long) lBytesRead);
  155. }
  156. // update simulated file position
  157. //
  158. else
  159. lpmmioinfo->lDiskOffset += (LONG) lBytesRead;
  160. TracePrintf_2(NULL, 5,
  161. TEXT("StreamIo: lpmmioinfo->lDiskOffset=%ld, lBytesRead=%ld\n"),
  162. (long) lpmmioinfo->lDiskOffset,
  163. (long) lBytesRead);
  164. // return number of bytes read
  165. //
  166. return fSuccess ? lBytesRead : -1;
  167. }
  168. static LRESULT StreamIoWrite(LPMMIOINFO lpmmioinfo, const HPSTR pch, LONG cch, BOOL fFlush)
  169. {
  170. BOOL fSuccess = TRUE;
  171. LPSTREAM lpStream = (LPSTREAM)(DWORD_PTR)lpmmioinfo->adwInfo[0];
  172. HRESULT hr;
  173. LONG lBytesWritten;
  174. TracePrintf_1(NULL, 5,
  175. TEXT("StreamIoWrite (%ld)\n"),
  176. (long) cch);
  177. if (cch <= 0)
  178. lBytesWritten = 0; // nothing to do
  179. // write
  180. //
  181. else if ((hr = lpStream->Write((LPVOID) pch,
  182. (ULONG) cch, (ULONG FAR *) &lBytesWritten)) != S_OK)
  183. {
  184. fSuccess = TraceFALSE(NULL);
  185. TracePrintf_2(NULL, 5,
  186. TEXT("IStream:Write failed (%ld, %ld)\n"),
  187. (long) hr,
  188. (long) lBytesWritten);
  189. }
  190. // update file position
  191. //
  192. else
  193. lpmmioinfo->lDiskOffset += lBytesWritten;
  194. TracePrintf_2(NULL, 5,
  195. TEXT("StreamIo: lpmmioinfo->lDiskOffset=%ld, lBytesWritten=%ld\n"),
  196. (long) lpmmioinfo->lDiskOffset,
  197. (long) lBytesWritten);
  198. // return number of bytes written
  199. //
  200. return fSuccess ? lBytesWritten : -1;
  201. }
  202. static LRESULT StreamIoSeek(LPMMIOINFO lpmmioinfo, LONG lOffset, int iOrigin)
  203. {
  204. BOOL fSuccess = TRUE;
  205. LPSTREAM lpStream = (LPSTREAM)(DWORD_PTR)lpmmioinfo->adwInfo[0];
  206. HRESULT hr;
  207. LARGE_INTEGER largeOffset;
  208. ULARGE_INTEGER ulargePosNew;
  209. largeOffset.LowPart = (DWORD) lOffset;
  210. largeOffset.HighPart = (DWORD) 0L;
  211. TracePrintf_2(NULL, 5,
  212. TEXT("StreamIoSeek (%ld, %d)\n"),
  213. (long) lOffset,
  214. (int) iOrigin);
  215. // seek
  216. //
  217. if ((hr = lpStream->Seek(largeOffset,
  218. (DWORD) iOrigin, &ulargePosNew)) != S_OK)
  219. {
  220. fSuccess = TraceFALSE(NULL);
  221. TracePrintf_1(NULL, 5,
  222. TEXT("IStream:Seek failed (%ld)\n"),
  223. (long) hr);
  224. }
  225. // update file position
  226. //
  227. else
  228. lpmmioinfo->lDiskOffset = (long) ulargePosNew.LowPart;
  229. TracePrintf_1(NULL, 5,
  230. TEXT("StreamIo: lpmmioinfo->lDiskOffset=%ld\n"),
  231. (long) lpmmioinfo->lDiskOffset);
  232. return fSuccess ? lpmmioinfo->lDiskOffset : -1;
  233. }
  234. static LRESULT StreamIoRename(LPMMIOINFO lpmmioinfo, LPCTSTR lpszFileName, LPCTSTR lpszNewFileName)
  235. {
  236. BOOL fSuccess = TRUE;
  237. UINT uRet = MMIOERR_FILENOTFOUND;
  238. TracePrintf_2(NULL, 5,
  239. TEXT("StreamIoRename (%s, %s)\n"),
  240. (LPTSTR) lpszFileName,
  241. (LPTSTR) lpszNewFileName);
  242. // rename is not supported by this i/o procedure
  243. //
  244. if (TRUE)
  245. fSuccess = TraceFALSE(NULL);
  246. return fSuccess ? 0 : uRet;
  247. }