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.

387 lines
9.5 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. // mmio.c - mmio functions
  24. ////
  25. #include "winlocal.h"
  26. #include <mmsystem.h>
  27. #include "mmio.h"
  28. #include "mem.h"
  29. #include "sys.h"
  30. #include "trace.h"
  31. ////
  32. // private definitions
  33. ////
  34. // helper functions
  35. //
  36. static LRESULT MmioIOOpen(LPMMIOINFO lpmmioinfo, LPTSTR lpszFileName);
  37. static LRESULT MmioIOClose(LPMMIOINFO lpmmioinfo, UINT uFlags);
  38. static LRESULT MmioIORead(LPMMIOINFO lpmmioinfo, HPSTR pch, LONG cch);
  39. static LRESULT MmioIOWrite(LPMMIOINFO lpmmioinfo, const HPSTR pch, LONG cch, BOOL fFlush);
  40. static LRESULT MmioIOSeek(LPMMIOINFO lpmmioinfo, LONG lOffset, int iOrigin);
  41. static LRESULT MmioIORename(LPMMIOINFO lpmmioinfo, LPCTSTR lpszFileName, LPCTSTR lpszNewFileName);
  42. static LRESULT MmioIOGetInfo(LPMMIOINFO lpmmioinfo, int iInfo);
  43. static LRESULT MmioIOChSize(LPMMIOINFO lpmmioinfo, long lSize);
  44. ////
  45. // public functions
  46. ////
  47. // MmioIOProc - i/o procedure for mmio data
  48. // <lpmmioinfo> (i/o) information about open file
  49. // <uMessage> (i) message indicating the requested I/O operation
  50. // <lParam1> (i) message specific parameter
  51. // <lParam2> (i) message specific parameter
  52. // returns 0 if message not recognized, otherwise message specific value
  53. //
  54. // NOTE: the address of this function should be passed to the WavOpen()
  55. // or mmioInstallIOProc() functions for accessing mmio format file data.
  56. //
  57. LRESULT DLLEXPORT CALLBACK MmioIOProc(LPTSTR lpmmioinfo,
  58. UINT uMessage, LPARAM lParam1, LPARAM lParam2)
  59. {
  60. BOOL fSuccess = TRUE;
  61. LRESULT lResult = 0;
  62. if (lpmmioinfo == NULL)
  63. fSuccess = TraceFALSE(NULL);
  64. else switch (uMessage)
  65. {
  66. case MMIOM_OPEN:
  67. lResult = MmioIOOpen((LPMMIOINFO) lpmmioinfo,
  68. (LPTSTR) lParam1);
  69. break;
  70. case MMIOM_CLOSE:
  71. lResult = MmioIOClose((LPMMIOINFO) lpmmioinfo,
  72. (UINT) lParam1);
  73. break;
  74. case MMIOM_READ:
  75. lResult = MmioIORead((LPMMIOINFO) lpmmioinfo,
  76. (HPSTR) lParam1, (LONG) lParam2);
  77. break;
  78. case MMIOM_WRITE:
  79. lResult = MmioIOWrite((LPMMIOINFO) lpmmioinfo,
  80. (const HPSTR) lParam1, (LONG) lParam2, FALSE);
  81. break;
  82. case MMIOM_WRITEFLUSH:
  83. lResult = MmioIOWrite((LPMMIOINFO) lpmmioinfo,
  84. (const HPSTR) lParam1, (LONG) lParam2, TRUE);
  85. break;
  86. case MMIOM_SEEK:
  87. lResult = MmioIOSeek((LPMMIOINFO) lpmmioinfo,
  88. (LONG) lParam1, (int) lParam2);
  89. break;
  90. case MMIOM_RENAME:
  91. lResult = MmioIORename((LPMMIOINFO) lpmmioinfo,
  92. (LPCTSTR) lParam1, (LPCTSTR) lParam2);
  93. break;
  94. case MMIOM_GETINFO:
  95. lResult = MmioIOGetInfo((LPMMIOINFO) lpmmioinfo,
  96. (int) lParam1);
  97. break;
  98. case MMIOM_CHSIZE:
  99. lResult = MmioIOChSize((LPMMIOINFO) lpmmioinfo,
  100. (long) lParam1);
  101. break;
  102. default:
  103. lResult = 0;
  104. break;
  105. }
  106. return lResult;
  107. }
  108. ////
  109. // installable file i/o procedures
  110. ////
  111. static LRESULT MmioIOOpen(LPMMIOINFO lpmmioinfo, LPTSTR lpszFileName)
  112. {
  113. BOOL fSuccess = TRUE;
  114. HMMIO hmmio = NULL;
  115. MMIOINFO mmioinfo;
  116. TracePrintf_1(NULL, 5,
  117. TEXT("MmioIOOpen (%s)\n"),
  118. (LPTSTR) lpszFileName);
  119. MemSet(&mmioinfo, 0, sizeof(mmioinfo));
  120. // special case flags which do not actually return an open file handle
  121. //
  122. if ((lpmmioinfo->dwFlags & MMIO_EXIST) ||
  123. (lpmmioinfo->dwFlags & MMIO_DELETE) ||
  124. (lpmmioinfo->dwFlags & MMIO_GETTEMP) ||
  125. (lpmmioinfo->dwFlags & MMIO_PARSE))
  126. {
  127. hmmio = mmioOpen(lpszFileName, &mmioinfo, lpmmioinfo->dwFlags);
  128. return (LRESULT) fSuccess;
  129. }
  130. else if ((hmmio = mmioOpen(lpszFileName, &mmioinfo, lpmmioinfo->dwFlags)) == NULL)
  131. fSuccess = TraceFALSE(NULL);
  132. else
  133. {
  134. // save stuff for use in other i/o routines
  135. //
  136. lpmmioinfo->adwInfo[0] = (DWORD) (LPVOID) hmmio;
  137. }
  138. if (!fSuccess && hmmio != NULL && mmioClose(hmmio, 0) != 0)
  139. fSuccess = TraceFALSE(NULL);
  140. // return the same error code given by mmioOpen
  141. //
  142. return fSuccess ? lpmmioinfo->wErrorRet = mmioinfo.wErrorRet : MMIOERR_CANNOTOPEN;
  143. }
  144. static LRESULT MmioIOClose(LPMMIOINFO lpmmioinfo, UINT uFlags)
  145. {
  146. BOOL fSuccess = TRUE;
  147. HMMIO hmmio = (HMMIO) lpmmioinfo->adwInfo[0];
  148. UINT uRet = MMIOERR_CANNOTCLOSE;
  149. TracePrintf_0(NULL, 5,
  150. TEXT("MmioIOClose\n"));
  151. if ((uRet = mmioClose(hmmio, uFlags)) != 0)
  152. fSuccess = TraceFALSE(NULL);
  153. else
  154. {
  155. lpmmioinfo->adwInfo[0] = (DWORD) NULL;
  156. }
  157. return fSuccess ? 0 : uRet;
  158. }
  159. static LRESULT MmioIORead(LPMMIOINFO lpmmioinfo, HPSTR pch, LONG cch)
  160. {
  161. BOOL fSuccess = TRUE;
  162. HMMIO hmmio = (HMMIO) lpmmioinfo->adwInfo[0];
  163. LONG lBytesRead;
  164. TracePrintf_1(NULL, 5,
  165. TEXT("MmioIORead (%ld)\n"),
  166. (long) cch);
  167. if (cch <= 0)
  168. lBytesRead = 0; // nothing to do
  169. // read
  170. //
  171. else if ((lBytesRead = mmioRead(hmmio, pch, cch)) == -1)
  172. fSuccess = TraceFALSE(NULL);
  173. // update simulated file position
  174. //
  175. else
  176. lpmmioinfo->lDiskOffset += lBytesRead;
  177. TracePrintf_2(NULL, 5,
  178. TEXT("lpmmioinfo->lDiskOffset=%ld, lBytesRead=%ld\n"),
  179. (long) lpmmioinfo->lDiskOffset,
  180. (long) lBytesRead);
  181. // return number of bytes read
  182. //
  183. return fSuccess ? lBytesRead : -1;
  184. }
  185. static LRESULT MmioIOWrite(LPMMIOINFO lpmmioinfo, const HPSTR pch, LONG cch, BOOL fFlush)
  186. {
  187. BOOL fSuccess = TRUE;
  188. HMMIO hmmio = (HMMIO) lpmmioinfo->adwInfo[0];
  189. LONG lBytesWritten;
  190. TracePrintf_1(NULL, 5,
  191. TEXT("MmioIOWrite (%ld)\n"),
  192. (long) cch);
  193. if (cch <= 0)
  194. lBytesWritten = 0; // nothing to do
  195. // write
  196. //
  197. else if ((lBytesWritten = mmioWrite(hmmio, pch, cch)) == -1)
  198. fSuccess = TraceFALSE(NULL);
  199. // update file position
  200. //
  201. else
  202. lpmmioinfo->lDiskOffset += lBytesWritten;
  203. TracePrintf_2(NULL, 5,
  204. TEXT("lpmmioinfo->lDiskOffset=%ld, lBytesWritten=%ld\n"),
  205. (long) lpmmioinfo->lDiskOffset,
  206. (long) lBytesWritten);
  207. // return number of bytes written
  208. //
  209. return fSuccess ? lBytesWritten : -1;
  210. }
  211. static LRESULT MmioIOSeek(LPMMIOINFO lpmmioinfo, LONG lOffset, int iOrigin)
  212. {
  213. BOOL fSuccess = TRUE;
  214. HMMIO hmmio = (HMMIO) lpmmioinfo->adwInfo[0];
  215. LONG lPosNew;
  216. TracePrintf_2(NULL, 5,
  217. TEXT("MmioIOSeek (%ld, %d)\n"),
  218. (long) lOffset,
  219. (int) iOrigin);
  220. // seek
  221. //
  222. if ((lPosNew = mmioSeek(hmmio, lOffset, iOrigin)) == -1)
  223. fSuccess = TraceFALSE(NULL);
  224. // update file position
  225. //
  226. else
  227. lpmmioinfo->lDiskOffset = lPosNew;
  228. TracePrintf_1(NULL, 5,
  229. TEXT("lpmmioinfo->lDiskOffset=%ld\n"),
  230. (long) lpmmioinfo->lDiskOffset);
  231. return fSuccess ? lpmmioinfo->lDiskOffset : -1;
  232. }
  233. static LRESULT MmioIORename(LPMMIOINFO lpmmioinfo, LPCTSTR lpszFileName, LPCTSTR lpszNewFileName)
  234. {
  235. BOOL fSuccess = TRUE;
  236. UINT uRet;
  237. TracePrintf_2(NULL, 5,
  238. TEXT("MmioIORename (%s, %s)\n"),
  239. (LPTSTR) lpszFileName,
  240. (LPTSTR) lpszNewFileName);
  241. if ((uRet = mmioRename(lpszFileName, lpszNewFileName, lpmmioinfo, 0)) != 0)
  242. fSuccess = TraceFALSE(NULL);
  243. return fSuccess ? 0 : uRet;
  244. }
  245. static LRESULT MmioIOGetInfo(LPMMIOINFO lpmmioinfo, int iInfo)
  246. {
  247. BOOL fSuccess = TRUE;
  248. TracePrintf_1(NULL, 5,
  249. TEXT("MmioIOGetInfo (%d)\n"),
  250. (int) iInfo);
  251. if (iInfo < 0 || iInfo > 2)
  252. fSuccess = TraceFALSE(NULL);
  253. return fSuccess ? (LRESULT) lpmmioinfo->adwInfo[iInfo] : 0;
  254. }
  255. static LRESULT MmioIOChSize(LPMMIOINFO lpmmioinfo, long lSize)
  256. {
  257. BOOL fSuccess = TRUE;
  258. long lPosCurr;
  259. long lPosEnd;
  260. TracePrintf_1(NULL, 5,
  261. TEXT("MmioIOChSize (%ld)\n"),
  262. (long) lSize);
  263. if ((lPosCurr = MmioIOSeek(lpmmioinfo, 0, SEEK_CUR)) < 0)
  264. fSuccess = TraceFALSE(NULL);
  265. else if ((lPosEnd = MmioIOSeek(lpmmioinfo, 0, SEEK_END)) < 0)
  266. fSuccess = TraceFALSE(NULL);
  267. else if (lPosEnd == lSize)
  268. ; // nothing to do, since the file is already the specified size
  269. // make file larger by writing bytes at end
  270. //
  271. else if (lPosEnd < lSize)
  272. {
  273. void _huge *hpBuf = NULL;
  274. long sizBuf = lSize - lPosEnd;
  275. if ((hpBuf = MemAlloc(NULL, sizBuf, 0)) == NULL)
  276. fSuccess = TraceFALSE(NULL);
  277. else if (MmioIOWrite(lpmmioinfo, hpBuf, sizBuf, TRUE) < 0)
  278. fSuccess = TraceFALSE(NULL);
  279. if (hpBuf != NULL && (hpBuf = MemFree(NULL, hpBuf)) != NULL)
  280. fSuccess = TraceFALSE(NULL);
  281. }
  282. // make file smaller by truncating at specified position
  283. //
  284. else if (lPosEnd > lSize)
  285. {
  286. // seek to the specified position
  287. //
  288. if (MmioIOSeek(lpmmioinfo, lSize, SEEK_SET) != lSize)
  289. fSuccess = TraceFALSE(NULL);
  290. // truncate file
  291. //
  292. else
  293. {
  294. #ifdef _WIN32
  295. // $FIXUP - where do we get the file handle ?
  296. //
  297. #if 0
  298. if (SetEndOfFile(hfile)
  299. fSuccess = TraceFALSE(NULL);
  300. #endif
  301. #else
  302. BYTE abBuf[1];
  303. // writing zero bytes under DOS will truncate file at current position
  304. //
  305. if (MmioIOWrite(lpmmioinfo, abBuf, 0, TRUE) < 0)
  306. fSuccess = TraceFALSE(NULL);
  307. #endif
  308. }
  309. }
  310. return fSuccess ? 0 : -1;
  311. }