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.

2341 lines
83 KiB

  1. /*-------------------------------------------------------------------*\
  2. *
  3. * mmio.c
  4. *
  5. * Basic MMIO functions.
  6. *
  7. \*-------------------------------------------------------------------*/
  8. /*--------------------------------------------------------------------*/
  9. /* Revision history:
  10. * a-davlip: Jan 00 Ported to APC.
  11. * LaurieGr: Jan 92 Ported from win16. Source tree fork, not common code.
  12. * StephenE: Apr 92 Enabled UNICODE.
  13. */
  14. /*--------------------------------------------------------------------*/
  15. /*--------------------------------------------------------------------*/
  16. /* Implementation notes:
  17. *
  18. * An HMMIO is in fact a PMMIO i.e. a pointer to a MMIOINFO.
  19. * This causes the code to be littered with casts.
  20. * Whoever exported MMIOINFO should learn about encapsulation and
  21. * all that stuff. sigh.
  22. *
  23. * The "current disk offset" is the disk offset (i.e. the location
  24. * in the disk file) that the next MMIOM_READ or MMIOM_WRITE will
  25. * read from or write to. The I/O procedure maintains the
  26. * <lDiskOffset> field of the file's MMIO structure so that
  27. * <lDiskOffset> is equal to the current disk offset.
  28. *
  29. * The "current buffered offset" is the disk offset that the next
  30. * mmioRead() or mmioWrite() call would read from or write to.
  31. * The current buffered offset is defined as
  32. *
  33. * <lBufOffset> + (<pchNext> - <pchBuffer>)
  34. *
  35. * since <lBufOffset> is the disk offset of the start of the buffer
  36. * and <pchNext> corresponds to the current buffered offset.
  37. *
  38. * If the file is unbuffered, then <pchBuffer>, <pchNext>,
  39. * <pchEndRead> and <pchEndWrite> will always be NULL, and
  40. * <lBufOffset> will always be considered the "current buffered
  41. * offset", i.e. mmioRead() and mmioWrite() will read/write
  42. * at this offset.
  43. *
  44. *
  45. * Except right at the beginning of mmioOpen(), the MMIO_ALLOCBUF
  46. * flag is set if and only if the pchBuffer field points to a block
  47. * of global memory that MMIO has allocated.
  48. */
  49. /*--------------------------------------------------------------------*/
  50. #include "winmmi.h"
  51. #include "mmioi.h"
  52. #pragma warning (disable: 4715)
  53. HANDLE hHeap;
  54. PHNDL pHandleList;
  55. CRITICAL_SECTION HandleListCritSec;
  56. /*--------------------------------------------------------------------*\
  57. * Local function prototypes
  58. \*--------------------------------------------------------------------*/
  59. static void NEAR PASCAL SetIOProc( LPCWSTR szFileName, LPMMIOINFO lpmmio);
  60. static LPMMIOPROC NEAR PASCAL RemoveIOProc(FOURCC fccIOProc, HANDLE htask);
  61. static LONG NEAR PASCAL mmioDiskIO(PMMIO pmmio, UINT uMsg, LPSTR pch, LONG cch);
  62. static UINT NEAR PASCAL mmioExpandMemFile(PMMIO pmmio, LONG lExpand);
  63. static LPMMIOPROC mmioInternalInstallIOProc( FOURCC fccIOProc,
  64. LPMMIOPROC pIOProc,
  65. DWORD dwFlags);
  66. FOURCC APIENTRY mmioStringToFOURCCW( LPCWSTR sz, UINT uFlags );
  67. /*--------------------------------------------------------------------*/
  68. /* The I/O procedure map is a linked list of IOProcMapEntry structures.
  69. * The head of the list, <gIOProcMapHead> is a pointer node to the last
  70. * entry registered. The first few elements of the list are the predefined
  71. * global IO procedures below -- these all have <hTask> equal to NULL so
  72. * that no task can unregister them.
  73. *
  74. */
  75. typedef struct IOProcMapEntryTag
  76. {
  77. FOURCC fccIOProc; // ID of installed I/O procedure
  78. LPMMIOPROC pIOProc; // I/O procedure address
  79. HANDLE hTask; // task that called mmioRegisterIOProc()
  80. struct IOProcMapEntryTag *pNext; // pointer to next IOProc entry
  81. } IOProcMapEntry, *pIOProcMapEntry;
  82. // MMIOPROC is defined in the public MMSYSTEM.H
  83. // typedef LONG (APIENTRY MMIOPROC)(LPSTR lpmmioinfo, UINT uMsg, LONG lParam1, LONG lParam2);
  84. MMIOPROC mmioDOSIOProc, mmioMEMIOProc; // standard I/O procedures
  85. static IOProcMapEntry gIOProcMaps[] = {
  86. { FOURCC_DOS, mmioDOSIOProc, NULL, &gIOProcMaps[1] },
  87. { FOURCC_MEM, mmioMEMIOProc, NULL, NULL }
  88. };
  89. //
  90. // Global head of list
  91. //
  92. static pIOProcMapEntry gIOProcMapHead = gIOProcMaps;
  93. /* Call the IOProc in the info structure and return the result.
  94. Take due account of whether it is a 16 or 32 bit IOProc.
  95. */
  96. static LRESULT IOProc(LPMMIOINFO lpmmioinfo, UINT uMsg, LONG lParam1, LONG lParam2)
  97. {
  98. /* just pass the call on */
  99. return ((LPMMIOPROC)(lpmmioinfo->pIOProc)) ((LPSTR)lpmmioinfo, uMsg, lParam1, lParam2);
  100. } /* IOProc */
  101. /*--------------------------------------------------------------------*/
  102. /* @doc INTERNAL
  103. @func LPMMIOPROC | FindIOProc | This function locates the IOProcMapEntry
  104. for a previously installed IO procedure .
  105. */
  106. /*--------------------------------------------------------------------*/
  107. static pIOProcMapEntry
  108. FindIOProc(FOURCC fccIOProc, HANDLE htask)
  109. {
  110. IOProcMapEntry *pEnt; // an entry in linked list
  111. /* walk through the linked list, first looking for an entry with
  112. * identifier <fccIOProc> that was added by the current task, then
  113. * looking for global entries.
  114. */
  115. for (pEnt = gIOProcMapHead; pEnt; pEnt = pEnt->pNext)
  116. if ((pEnt->fccIOProc == fccIOProc) && (pEnt->hTask == htask))
  117. return pEnt;
  118. for (pEnt = gIOProcMapHead; pEnt; pEnt = pEnt->pNext)
  119. if ( (pEnt->fccIOProc == fccIOProc)
  120. // ?? && (pEnt->hTask ==NULL) ??
  121. )
  122. return pEnt;
  123. return NULL;
  124. }
  125. /*--------------------------------------------------------------------*/
  126. /* @doc INTERNAL
  127. @func LPMMIOPROC | RemoveIOProc | This function removes previously installed
  128. IO procedure.
  129. */
  130. /*--------------------------------------------------------------------*/
  131. static LPMMIOPROC PASCAL NEAR
  132. RemoveIOProc(FOURCC fccIOProc, HANDLE htask)
  133. {
  134. IOProcMapEntry *pEnt; // an entry in linked list
  135. IOProcMapEntry *pEntPrev; // the entry before <pEnt>
  136. /* walk through the linked list, looking for an entry with
  137. * identifier <fccIOProc> that was added by the current task
  138. */
  139. for ( pEntPrev = NULL, pEnt = gIOProcMapHead
  140. ; pEnt
  141. ; pEntPrev = pEnt, pEnt = pEnt->pNext
  142. )
  143. if ((pEnt->fccIOProc == fccIOProc) && (pEnt->hTask == htask)) {
  144. LPMMIOPROC pIOProc;
  145. pIOProc = pEnt->pIOProc;
  146. if (pEntPrev)
  147. pEntPrev->pNext = pEnt->pNext;
  148. else
  149. gIOProcMapHead = pEnt->pNext;
  150. FreeHandle((HMMIO) pEnt);
  151. return pIOProc;
  152. }
  153. return NULL;
  154. }
  155. /*--------------------------------------------------------------------*/
  156. /* @doc INTERNAL
  157. @func void | SetIOProc | This function sets the physical IO procedure
  158. based on either the file name or the parameters within the
  159. <p lpmmioinfo> structure passed.
  160. @parm LPCWSTR | szFilename | Specifies a pointer to a string
  161. containing the filename of the file to open. If no I/O procedure is
  162. @parm LPMMIOINFO | lpmmioinfo | Specifies a pointer to an
  163. <t MMIOINFO> structure containing extra parameters used by
  164. <f SetIOProc> in determining the IO procedure to use. The
  165. <e MMIOINFO.pIOProc> element is set to the procedure found.
  166. @rdesc Nothing.
  167. */
  168. /*--------------------------------------------------------------------*/
  169. static void NEAR PASCAL
  170. SetIOProc( LPCWSTR szFileName, LPMMIOINFO lpmmio)
  171. {
  172. IOProcMapEntry *pEnt; // the entry in linked list
  173. /* If the IOProc is not given, see if the file name implies that
  174. * <szFileName> is either a RIFF compound file or some kind of
  175. * other registered storage system -- look for the last CFSEPCHAR in
  176. * the name, e.g. '+' in "foo.bnd+bar.hlp+blorg.dib", and figure
  177. * that the IOProc ID is the extension of the compound file name,
  178. * e.g. the extension of "foo.bnd+bar.hlp", i.e. 'HLP '.
  179. *
  180. * Alternatively, if <szFileName> is NULL, then assume that
  181. * <lpmmio->adwInfo[0]> is a DOS file handle.
  182. */
  183. if (lpmmio->pIOProc == NULL)
  184. {
  185. if (lpmmio->fccIOProc == 0)
  186. {
  187. if (szFileName != NULL)
  188. {
  189. LPWSTR pch;
  190. /* see if <szFileName> contains CFSEPCHAR */
  191. if ((pch = wcsrchr(szFileName, CFSEPCHAR)) != 0)
  192. {
  193. /* find the extension that precedes CFSEPCHAR,
  194. * e.g. "hlp" in "foo.bnd+bar.hlp+blorg.dib"
  195. */
  196. while ( (pch > szFileName)
  197. && (*pch != L'.')
  198. && (*pch != L':')
  199. && (*pch != L'\\')
  200. )
  201. pch--;
  202. if (*pch == L'.')
  203. {
  204. WCHAR aszFour[sizeof(FOURCC)+1];
  205. int i;
  206. for (i = 0, pch++; i < sizeof(FOURCC); i++)
  207. if (*pch == CFSEPCHAR)
  208. aszFour[i] = L'\0';
  209. else
  210. aszFour[i] = *pch++;
  211. aszFour[sizeof(FOURCC)] = L'\0';
  212. lpmmio->fccIOProc = mmioStringToFOURCCW(aszFour, MMIO_TOUPPER);
  213. }
  214. }
  215. }
  216. /* if the caller didn't specify an IOProc, and the code above
  217. * didn't determine an IOProc ID, then the default is the DOS
  218. * IOProc.
  219. */
  220. if (lpmmio->fccIOProc == 0)
  221. lpmmio->fccIOProc = FOURCC_DOS;
  222. }
  223. /* unless an IOProc address is specified explicitly, look up the
  224. * IOProc in the global IOProc ID-to-address table -- the default
  225. * is 'DOS' since we'll assume that custom storage system I/O
  226. * procedures would have been installed
  227. */
  228. pEnt = FindIOProc( lpmmio->fccIOProc
  229. , lpmmio->htask
  230. ? lpmmio->htask
  231. : (HANDLE)GetCurrentThreadId()
  232. );
  233. if (pEnt && pEnt->pIOProc) {
  234. lpmmio->pIOProc = pEnt -> pIOProc;
  235. }
  236. else {
  237. lpmmio->pIOProc = mmioDOSIOProc;
  238. lpmmio->dwReserved1 = 0;
  239. }
  240. }
  241. }
  242. /*--------------------------------------------------------------------*/
  243. /* @doc INTERNAL
  244. @func void | mmioCleanupIOProcs | removes from the linked list entries
  245. installed with the given task handle
  246. @parm HANDLE | hTask | Specifies the task to clean up for
  247. @rdesc Nothing.
  248. @comm This will only be called to clean up a WOW task.
  249. */
  250. /*--------------------------------------------------------------------*/
  251. void mmioCleanupIOProcs(HANDLE hTask)
  252. {
  253. IOProcMapEntry *pEnt;
  254. IOProcMapEntry *pEntPrev;
  255. for (pEntPrev = NULL, pEnt = gIOProcMapHead; pEnt;) {
  256. if (pEnt->hTask == hTask) {
  257. DEBUGMSG(1, (TEXT("MMIOPROC handle (%04X) not closed."), pEnt));
  258. if (pEntPrev) {
  259. pEntPrev->pNext = pEnt->pNext;
  260. FreeHandle((HMMIO)pEnt);
  261. pEnt = pEntPrev->pNext;
  262. } else {
  263. gIOProcMapHead = pEnt->pNext;
  264. FreeHandle((HMMIO)pEnt);
  265. pEnt = gIOProcMapHead;
  266. }
  267. } else {
  268. pEntPrev = pEnt;
  269. pEnt = pEnt->pNext;
  270. }
  271. }
  272. }
  273. /*--------------------------------------------------------------------*/
  274. /* @doc EXTERNAL
  275. @api UINT | mmioRename | This function renames the specified file.
  276. @parm LPCTSTR | szFilename | Specifies a pointer to a string
  277. containing the filename of the file to rename.
  278. @parm LPCTSTR | szNewFileName | Specifies a pointer to a string
  279. containing the new filename.
  280. @parm LPMMIOINFO | lpmmioinfo | Specifies a pointer to an
  281. <t MMIOINFO> structure containing extra parameters used by
  282. <f mmioRename>.
  283. If <p lpmmioinfo> is not NULL, all unused fields of the
  284. <t MMIOINFO> structure it references must be set to zero, including the
  285. reserved fields.
  286. @parm DWORD | dwRenameFlags | Specifies option flags for the rename
  287. operation. This should be set to zero.
  288. @rdesc The return value is zero if the file was renamed. Otherwise, the
  289. return value is an error code returned from <f mmioRename> or from the I/O
  290. procedure.
  291. */
  292. /*--------------------------------------------------------------------*/
  293. UINT APIENTRY
  294. mmioRenameW( LPCWSTR szFileName
  295. , LPCWSTR szNewFileName
  296. , LPCMMIOINFO lpmmioinfo
  297. , DWORD fdwRename
  298. )
  299. {
  300. MMIOINFO mmioinfo;
  301. memset(&mmioinfo, 0, sizeof(MMIOINFO));
  302. V_RPOINTER0(lpmmioinfo, sizeof(MMIOINFO), MMSYSERR_INVALPARAM);
  303. if (lpmmioinfo) {
  304. V_CALLBACK0((FARPROC)lpmmioinfo->pIOProc, MMSYSERR_INVALPARAM);
  305. mmioinfo = *lpmmioinfo;
  306. }
  307. SetIOProc(szFileName, &mmioinfo);
  308. if ( (mmioinfo.dwFlags & MMIO_UNICODEPROC )
  309. || (mmioinfo.pIOProc == mmioDOSIOProc ) // or the DOS file IO Proc
  310. || (mmioinfo.pIOProc == mmioMEMIOProc ) ) { // or a memory file IO Proc
  311. /*------------------------------------------------------------*\
  312. * We have an unicode IO Proc so use the given file names
  313. * without any conversion.
  314. \*------------------------------------------------------------*/
  315. return IOProc( &mmioinfo, MMIOM_RENAME,
  316. (LONG)szFileName, (LONG)szNewFileName );
  317. }
  318. else
  319. DEBUGMSG(1, (TEXT("mmio: ERROR -- ASCII IO proc desired")));
  320. }
  321. /*--------------------------------------------------------------------*/
  322. /* @doc EXTERNAL
  323. @api HMMIO | mmioOpen | This function opens a file for unbuffered
  324. or buffered I/O. The file can be a DOS file, a memory file, or an
  325. element of a custom storage system.
  326. @parm LPTSTR | szFilename | Specifies a pointer to a string
  327. containing the filename of the file to open. If no I/O procedure is
  328. specified to open the file, then the filename determines how the file
  329. is opened, as follows:
  330. -- If the filename does not contain "+", then it is assumed
  331. to be the name of a DOS file.
  332. -- If the filename is of the form "foo.ext+bar", then the
  333. extension "EXT " is assumed to identify an installed I/O procedure
  334. which is called to perform I/O on the file (see <f mmioInstallIOProc>).
  335. -- If the filename is NULL and no I/O procedure is given, then
  336. <e MMIOINFO.adwInfo[0]> is assumed to be the DOS file handle
  337. of a currently open file.
  338. The filename should not be longer than 128 bytes, including the
  339. terminating NULL.
  340. When opening a memory file, set <p szFilename> to NULL.
  341. @parm LPMMIOINFO | lpmmioinfo | Specifies a pointer to an
  342. <t MMIOINFO> structure containing extra parameters used by
  343. <f mmioOpen>. Unless you are opening a memory file, specifying the
  344. size of a buffer for buffered I/O, or specifying an uninstalled I/O
  345. procedure to open a file, this parameter should be NULL.
  346. If <p lpmmioinfo> is not NULL, all unused fields of the
  347. <t MMIOINFO> structure it references must be set to zero, including the
  348. reserved fields.
  349. @parm DWORD | dwOpenFlags | Specifies option flags for the open
  350. operation. The MMIO_READ, MMIO_WRITE, and MMIO_READWRITE flags are
  351. mutually exclusive--only one should be specified. The MMIO_COMPAT,
  352. MMIO_EXCLUSIVE, MMIO_DENYWRITE, MMIO_DENYREAD, and MMIO_DENYNONE flags
  353. are DOS file-sharing flags, and can only be used after the DOS
  354. command SHARE has been executed.
  355. @flag MMIO_READ | Opens the file for reading only. This is the
  356. default, if MMIO_WRITE and MMIO_READWRITE are not specified.
  357. @flag MMIO_WRITE | Opens the file for writing. You should not
  358. read from a file opened in this mode.
  359. @flag MMIO_READWRITE | Opens the file for both reading and writing.
  360. @flag MMIO_CREATE | Creates a new file.
  361. If the file already exists, it is truncated to zero length.
  362. For memory files, MMIO_CREATE indicates the end of the file
  363. is initially at the start of the buffer.
  364. @flag MMIO_DELETE | Deletes a file. If this flag is specified,
  365. <p szFilename> should not be NULL. The return
  366. value will be TRUE (cast to HMMIO) if the file was deleted
  367. successfully, FALSE otherwise. Do not call <f mmioClose>
  368. for a file that has been deleted. If this flag is specified,
  369. all other file opening flags are ignored.
  370. @flag MMIO_PARSE | Creates a fully qualified filename from the path
  371. specified in <p szFileName>. The fully qualified filename is
  372. placed back into <p szFileName>. The return value
  373. will be TRUE (cast to HMMIO) if the qualification was
  374. successful, FALSE otherwise. The file is not opened, and the function
  375. does not return a valid MMIO file handle, so do not attempt to
  376. close the file. If this flag is specified, all other file
  377. opening flags are ignored.
  378. @flag MMIO_EXIST | Determines whether the specified file exists
  379. and creates a fully qualified filename from the path
  380. specified in <p szFileName>. The fully qualified filename is
  381. placed back into <p szFileName>. The return value
  382. will be TRUE (cast to HMMIO) if the qualification was
  383. successful and the file exists, FALSE otherwise. The file is
  384. not opened, and the function does not return a valid MMIO file
  385. handle, so do not attempt to close the file.
  386. @flag MMIO_ALLOCBUF | Opens a file for buffered I/O.
  387. To allocate a buffer larger or smaller than the default
  388. buffer size (8K), set the <e MMIOINFO.cchBuffer> field of the
  389. <t MMIOINFO> structure to the desired buffer size. If
  390. <e MMIOINFO.cchBuffer> is zero, then the default buffer size
  391. is used. If you are providing your own I/O buffer, then the
  392. MMIO_ALLOCBUF flag should not be used.
  393. @flag MMIO_COMPAT | Opens the file with compatibility mode,
  394. allowing any process on a given machine to open the file
  395. any number of times. <f mmioOpen> fails if the file has
  396. been opened with any of the other sharing modes.
  397. @flag MMIO_EXCLUSIVE | Opens the file with exclusive mode,
  398. denying other processes both read and write access to the file.
  399. <f mmioOpen> fails if the file has been opened in any other
  400. mode for read or write access, even by the current process.
  401. @flag MMIO_DENYWRITE | Opens the file and denies other
  402. processes write access to the file. <f mmioOpen> fails
  403. if the file has been opened in compatibility or for write
  404. access by any other process.
  405. @flag MMIO_DENYREAD | Opens the file and denies other
  406. processes read access to the file. <f mmioOpen> fails if the
  407. file has been opened in compatibility mode or for read access
  408. by any other process.
  409. @flag MMIO_DENYNONE | Opens the file without denying other
  410. processes read or write access to the file. <f mmioOpen>
  411. fails if the file has been opened in compatibility mode
  412. by any other process.
  413. @flag MMIO_GETTEMP | Creates a temporary filename, optionally
  414. using the parameters passed in <p szFileName> to determine
  415. the temporary name. For example, you can specify "C:F" to
  416. create a temporary file residing on drive C, starting with
  417. letter "F". The resulting filename is placed in the buffer
  418. pointed to by <p szFileName>. The return value will be TRUE
  419. (cast to HMMIO) if the temporary filename was created successfully,
  420. FALSE otherwise. The file is
  421. not opened, and the function does not return a valid MMIO file
  422. handle, so do not attempt to close the file.
  423. This flag overrides all other flags.
  424. @rdesc The return value is a handle to the opened file. This handle
  425. is not a DOS file handle--do not use it with any file I/O functions
  426. other than MMIO functions.
  427. If the file cannot be opened, the return value is NULL. If
  428. <p lpmmioinfo> is not NULL, then its <e MMIOINFO.wErrorRet> field
  429. will contain extended error information returned by the I/O
  430. procedure.
  431. @comm If <p lpmmioinfo> references an <t MMIOINFO> structure, set
  432. up the fields as described below. All unused fields must be set to
  433. zero, including reserved fields.
  434. -- To request that a file be opened with an installed I/O
  435. procedure, set the <e MMIOINFO.fccIOProc> field
  436. to the four-character code of the I/O procedure,
  437. and set the <e MMIOINFO.pIOProc> field to NULL.
  438. -- To request that a file be opened with an uninstalled I/O procedure,
  439. set the <e MMIOINFO.pIOProc> field to
  440. point to the I/O procedure, and set <e MMIOINFO.fccIOProc> to NULL.
  441. -- To request that <f mmioOpen> determine which I/O procedure to use
  442. to open the file based on the filename contained in <p szFilename>,
  443. set both <e MMIOINFO.fccIOProc> and <e MMIOINFO.pIOProc> to NULL.
  444. This is the default behavior if no <t MMIOINFO> structure is specified.
  445. -- To open a memory file using an internally allocated and managed
  446. buffer, set the <e MMIOINFO.pchBuffer> field to NULL,
  447. <e MMIOINFO.fccIOProc> to FOURCC_MEM,
  448. <e MMIOINFO.cchBuffer> to the initial size of the buffer, and
  449. <e MMIOINFO.adwInfo[0]> to the incremental expansion size of the
  450. buffer. This memory file will automatically be expanded in increments of
  451. <e MMIOINFO.adwInfo[0]> bytes when necessary. Specify the MMIO_CREATE
  452. flag for the <p dwOpenFlags> parameter to initially set the end of
  453. the file to be the beginning of the buffer.
  454. -- To open a memory file using a caller-supplied buffer, set
  455. the <e MMIOINFO.pchBuffer> field to point to the memory buffer,
  456. <e MMIOINFO.fccIOProc> to FOURCC_MEM,
  457. <e MMIOINFO.cchBuffer> to the size of the buffer, and
  458. <e MMIOINFO.adwInfo[0]> to the incremental expansion size of the
  459. buffer. The expansion size in <e MMIOINFO.adwInfo[0]> should only
  460. be non-zero if <e MMIOINFO.pchBuffer> is a pointer obtained by calling
  461. <f GlobalAlloc> and <f GlobalLock>, since <f GlobalReAlloc> will be called to
  462. expand the buffer. In particular, if <e MMIOINFO.pchBuffer> points to a
  463. local or global array, a block of memory in the local heap, or a block
  464. of memory allocated by <f GlobalDosAlloc>, <e MMIOINFO.adwInfo[0]> must
  465. be zero.
  466. Specify the MMIO_CREATE flag for the <p dwOpenFlags> parameter to
  467. initially set the end of the file to be the beginning of the buffer;
  468. otherwise, the entire block of memory will be considered readable.
  469. -- To use a currently open DOS file handle with MMIO, set the
  470. <e MMIOINFO.fccIOProc> field to FOURCC_DOS,
  471. <e MMIOINFO.pchBuffer> to NULL, and <e MMIOINFO.adwInfo[0]> to the
  472. DOS file handle. Note that offsets within the file will be relative to
  473. the beginning of the file, and will not depend on the DOS file position
  474. at the time <f mmioOpen> is called; the initial MMIO offset will be the same
  475. as the DOS offset when <f mmioOpen> is called.
  476. Later, to close the MMIO file handle without closing the DOS
  477. file handle, pass the MMIO_FHOPEN flag to <f mmioClose>.
  478. You must call <f mmioClose> to close a file opened with <f mmioOpen>.
  479. Open files are not automatically closed when an application exits.
  480. @xref mmioClose
  481. */
  482. /* these are the changes to mmioOpen() to support compound files... */
  483. /* @doc CFDOC
  484. @api HMMIO | mmioOpen | ...The file can be a DOS file, a memory file,
  485. an element of a RIFF compound file...
  486. @parm LPTSTR | szFilename | ...
  487. -- If <p szFilename> is of the form "foo+bar", then <f mmioOpen>
  488. opens the compound file element named "bar" that is stored inside
  489. the RIFF compound file named "foo".
  490. -- If <p szFilename> is of the form "foo.ext+bar", then the
  491. extension "ext" is assumed to identify the installed I/O procedure
  492. (see <f mmioInstallIOProc>). The extension "bnd", and any extensions
  493. that have not been installed, are assumed to refer to a RIFF compound
  494. file.
  495. @parm LPMMIOINFO | lpmmioinfo | ...
  496. @parm DWORD | dwOpenFlags | ...
  497. @rdesc ...
  498. @comm ...
  499. The following I/O procedure identifiers (type FOURCC) are predefined:
  500. ...
  501. FOURCC_BND: <p szFilename> is assumed to be the name of
  502. a RIFF compound file element, and <p adwInfo[0]> should
  503. contain the HMMCF of the compound file. Alternatively,
  504. <p szFilename> can include the name of the compound file
  505. (e.g. "foo.bnd+bar.dib" as described above), and <p adwInfo[0]>
  506. should be NULL, to automatically open the compound file.
  507. ...
  508. The easy way to open an element of a RIFF compound file: just
  509. include the name of the compound file in <p szFilename> preceded
  510. by a "+" as described above. For example, opening
  511. "c:\data\bar.bnd+blorg.dib" opens the compound file element
  512. named "blorg.dib" in the compound file "c:\data\bar.bnd".
  513. <p lpmmioinfo> can be null in this case -- set <p dwOpenFlags>
  514. as described above. You can use this same method to open an
  515. element of a custom storage system, if the file extension of the
  516. compound file ("bnd" in the above example) corresponds to an
  517. installed I/O procedure -- see <f mmioInstallIOProc> for details.
  518. To open an element of a RIFF compound file that was opened using
  519. <f mmioCFAccess> or <f mmioCFOpen>: set <p szFilename>
  520. to be the name of the compound file element; set <p fccIOProc>
  521. to FOURCC_BND; set <p adwInfo[0]> to the HMMCF of the open compound
  522. file; set <p dwOpenFlags> and <p cchBuffer> as described above;
  523. set all other fields of <p lpmmioinfo> to zero.
  524. ...
  525. */
  526. /*--------------------------------------------------------------------*/
  527. HMMIO APIENTRY
  528. mmioOpen( LPWSTR szFileName, LPMMIOINFO lpmmioinfo, DWORD dwOpenFlags )
  529. {
  530. PMMIO pmmio; // MMIO status block
  531. LPSTR hpBuffer;
  532. UINT w; // an MMRESULT or a LRESULT from an IOPROC
  533. hHeap = GetProcessHeap();
  534. V_FLAGS(dwOpenFlags, MMIO_OPEN_VALID, mmioOpen, NULL);
  535. V_WPOINTER0(lpmmioinfo, sizeof(MMIOINFO), NULL);
  536. if (lpmmioinfo) {
  537. lpmmioinfo->wErrorRet = 0;
  538. V_CALLBACK0((FARPROC)lpmmioinfo->pIOProc, NULL);
  539. }
  540. /* allocate MMIO status information block */
  541. if ( (pmmio = (PMMIO)(NewHandle(TYPE_MMIO, sizeof(MMIOINFO)))) == NULL)
  542. {
  543. if (lpmmioinfo) {
  544. lpmmioinfo->wErrorRet = MMIOERR_OUTOFMEMORY;
  545. }
  546. return NULL;
  547. }
  548. /*----------------------------------------------------------------*\
  549. * NewHandle does not zero the allocated storage so we had better do
  550. * it now.
  551. \*----------------------------------------------------------------*/
  552. memset(pmmio, 0, sizeof(MMIOINFO));
  553. /* if user supplied <lpmmioinfo>, copy it to <pmmio> */
  554. if (lpmmioinfo != NULL) {
  555. *pmmio = *lpmmioinfo;
  556. }
  557. /* <dwOpenFlags> always takes precedence over contents of <pmmio> */
  558. pmmio->dwFlags = dwOpenFlags;
  559. pmmio->hmmio = ((HMMIO)pmmio);
  560. /* MMIO_ALLOCBUF in the flags means that the user wants a buffer
  561. * allocated for buffered I/O, but after this point it means that
  562. * a buffer *was* allocated, so turn off the flag until the buffer
  563. * is actually allocated (which is done by mmioSetBuffer() below)
  564. */
  565. if (pmmio->dwFlags & MMIO_ALLOCBUF)
  566. {
  567. /* if a buffer size is not specified, use the default */
  568. if (pmmio->cchBuffer == 0) {
  569. pmmio->cchBuffer = MMIO_DEFAULTBUFFER;
  570. }
  571. pmmio->dwFlags &= ~MMIO_ALLOCBUF;
  572. }
  573. /* Set the pIOProc function as determined by the file name or the
  574. * parameters in the pmmio structure.
  575. */
  576. SetIOProc(szFileName, pmmio);
  577. /* The pmmio structure hasn't been set up for buffering, so we must
  578. * explicitly make sure that pchBuffer is NULL.
  579. */
  580. hpBuffer = pmmio->pchBuffer;
  581. pmmio->pchBuffer = NULL;
  582. /* set up buffered I/O however the user requested it */
  583. w = mmioSetBuffer(((HMMIO)pmmio), hpBuffer, pmmio->cchBuffer, 0);
  584. if (w)
  585. {
  586. if (lpmmioinfo) {
  587. lpmmioinfo->wErrorRet = w;
  588. }
  589. FreeHandle(((HMMIO)pmmio));
  590. return NULL;
  591. }
  592. if ( (pmmio->dwFlags & MMIO_UNICODEPROC) // a Unicode IO Proc
  593. || (pmmio->pIOProc == mmioDOSIOProc ) // or the DOS file IO Proc
  594. || (pmmio->pIOProc == mmioMEMIOProc ) ) { // or a memory file IO Proc
  595. /* let the I/O procedure open/delete/qualify the file */
  596. w = IOProc( pmmio, MMIOM_OPEN, (LONG)szFileName, 0L );
  597. } else
  598. DEBUGMSG(1, (TEXT("mmio: ERROR -- cleaning up IO proc w/ ASCII name")));
  599. /* If this is non-zero, return it to the user */
  600. if (w != 0)
  601. {
  602. if (lpmmioinfo != NULL) {
  603. lpmmioinfo->wErrorRet = w;
  604. }
  605. FreeHandle(((HMMIO)pmmio));
  606. return NULL;
  607. }
  608. if (pmmio->dwFlags & (MMIO_DELETE| MMIO_PARSE| MMIO_EXIST| MMIO_GETTEMP))
  609. {
  610. /* if the file is being deleted/parsed/name gotten, exit
  611. * QUICKLY because the file handle (or whatever) in <pmmio>
  612. * is not valid.
  613. */
  614. mmioSetBuffer(((HMMIO)pmmio), NULL, 0L, 0);
  615. FreeHandle(((HMMIO)pmmio));
  616. return (HMMIO) TRUE;
  617. }
  618. /* the initial "current buffered offset" will be equal to the initial
  619. * "current disk offset"
  620. */
  621. pmmio->lBufOffset = pmmio->lDiskOffset;
  622. return ((HMMIO)pmmio);
  623. }
  624. /*--------------------------------------------------------------------*/
  625. /* @doc EXTERNAL
  626. @api MMRESULT | mmioClose | This function closes a file opened with
  627. <f mmioOpen>.
  628. @parm HMMIO | hmmio | Specifies the file handle of the file to
  629. close.
  630. @parm UINT | uFlags | Specifies options for the close operation.
  631. @flag MMIO_FHOPEN | If the file was opened by passing the DOS
  632. file handle of an already-opened file to <f mmioOpen>, then
  633. using this flag tells <f mmioClose> to close the MMIO file
  634. handle, but not the DOS file handle. (This is done by the
  635. I/O Proc).
  636. @rdesc The return value is zero if the function is successful.
  637. Otherwise, the return value is an error code, either from
  638. <f mmioFlush> or from the I/O procedure. The error code can be
  639. one of the following codes:
  640. @flag MMIOERR_CANNOTWRITE | The contents of the buffer could
  641. not be written to disk.
  642. @flag MMIOERR_CANNOTCLOSE | There was a DOS file system error when
  643. the I/O Proc attempted to close the DOS file.
  644. @xref mmioOpen mmioFlush
  645. */
  646. /*--------------------------------------------------------------------*/
  647. MMRESULT APIENTRY
  648. mmioClose(HMMIO hmmio, UINT uFlags)
  649. {
  650. UINT w; /* either an LRESULT from an IOProc or an MMRESULT */
  651. V_HANDLE(hmmio, TYPE_MMIO, MMSYSERR_INVALHANDLE);
  652. if ((w = mmioFlush(hmmio, 0)) != 0)
  653. return w;
  654. w = IOProc( (PMMIO)hmmio, MMIOM_CLOSE, (LONG)(DWORD) uFlags, (LONG) 0);
  655. if (w != 0) return w;
  656. /* free the buffer if necessary */
  657. mmioSetBuffer(hmmio, NULL, 0L, 0);
  658. FreeHandle(hmmio);
  659. return 0;
  660. }
  661. /*--------------------------------------------------------------------*/
  662. /* @doc EXTERNAL
  663. @api LRESULT | mmioRead | This function reads a specified number of
  664. bytes from a file opened with <f mmioOpen>.
  665. @parm HMMIO | hmmio | Specifies the file handle of the file to be
  666. read.
  667. @parm LPSTR | pch | Specifies a pointer to a buffer to contain
  668. the data read from the file.
  669. @parm LONG | cch | Specifies the number of bytes to read from the
  670. file.
  671. @rdesc The return value is the number of bytes actually read. If the
  672. end of the file has been reached and no more bytes can be read, the
  673. return value is zero. If there is an error reading from the file, the
  674. return value is -1.
  675. @comm On 16 bit windows pch is a huge pointer. On 32 bit windows there is no
  676. distinction between huge pointers and long pointers.
  677. @xref mmioWrite
  678. */
  679. /*--------------------------------------------------------------------*/
  680. LRESULT APIENTRY
  681. mmioRead(HMMIO hmmio, LPSTR pch, LONG cch)
  682. {
  683. LONG lTotalBytesRead = 0L; // total no. bytes read
  684. LONG lBytes; // no. bytes that can be read
  685. PMMIO pmmio=(PMMIO)hmmio; //local copy hmmio - avoid casting, simplify debug
  686. V_HANDLE(hmmio, TYPE_MMIO, -1);
  687. V_WPOINTER(pch, cch, -1);
  688. for(;;)
  689. {
  690. /* calculate the number of bytes that can be read */
  691. lBytes = pmmio->pchEndRead - pmmio->pchNext;
  692. /* can only read at most <cch> bytes from buffer */
  693. if (lBytes > cch)
  694. lBytes = cch;
  695. if (lBytes > 0)
  696. {
  697. /* this is where some performance improvements can
  698. * be made, especially for small reads...?
  699. */
  700. memcpy(pch, pmmio->pchNext, lBytes);
  701. pmmio->pchNext += lBytes;
  702. pch += lBytes;
  703. cch -= lBytes;
  704. lTotalBytesRead += lBytes;
  705. }
  706. /* cannot do MMIOM_READ from memory files */
  707. if (pmmio->fccIOProc == FOURCC_MEM)
  708. return lTotalBytesRead;
  709. if (cch == 0) // no more to read?
  710. return lTotalBytesRead;
  711. /* we need to read beyond this buffer; if we have at least
  712. * another bufferful to read, just call the I/O procedure
  713. */
  714. if (cch > pmmio->cchBuffer)
  715. break;
  716. /* read the next bufferful and loop around */
  717. if (mmioAdvance(hmmio, NULL, MMIO_READ) != 0)
  718. return -1;
  719. /* if mmioAdvance() couldn't read any more data, we must be
  720. * at the end of the file
  721. */
  722. if (pmmio->pchNext == pmmio->pchEndRead)
  723. return lTotalBytesRead;
  724. }
  725. /* flush and empty the I/O buffer and manipulate <lBufOffset>
  726. * directly to change the current file position
  727. */
  728. if (mmioFlush(hmmio, MMIO_EMPTYBUF) != 0)
  729. return -1;
  730. /* call the I/O procedure to do the rest of the reading */
  731. lBytes = mmioDiskIO(pmmio, MMIOM_READ, pch, cch);
  732. pmmio->lBufOffset = pmmio->lDiskOffset;
  733. return (lBytes == -1L) ? -1L : lTotalBytesRead + lBytes;
  734. }
  735. /*--------------------------------------------------------------------*/
  736. /* @doc EXTERNAL
  737. @api LRESULT | mmioWrite | This function writes a specified number of
  738. bytes to a file opened with <f mmioOpen>.
  739. @parm HMMIO | hmmio | Specifies the file handle of the file.
  740. @parm LPSTR | pch | Specifies a pointer to the buffer to be
  741. written to the file.
  742. @parm LONG | cch | Specifies the number of bytes to write to the
  743. file.
  744. @rdesc The return value is the number of bytes actually written. If
  745. there is an error writing to the file, the return value is -1.
  746. @comm The current file position is incremented by the number of
  747. bytes written. On 16 bit windows pch is a huge pointer.
  748. On 32 bit windows there is no distinction between huge pointers
  749. and long pointers.
  750. @xref mmioRead
  751. */
  752. /*--------------------------------------------------------------------*/
  753. LRESULT APIENTRY
  754. mmioWrite(HMMIO hmmio, LPCSTR pch, LONG cch)
  755. {
  756. LONG lTotalBytesWritten = 0L; // total no. bytes written
  757. LONG lBytes; // no. bytes that can be written
  758. // "pch" is LPCSTR which is correct, but
  759. // we pass it to a polymorphic routine
  760. // which needs LPSTR.
  761. V_HANDLE(hmmio, TYPE_MMIO, -1);
  762. V_RPOINTER(pch, cch, -1);
  763. for(;;)
  764. {
  765. /* calculate the number of bytes that can be written */
  766. lBytes = ((PMMIO)hmmio)->pchEndWrite - ((PMMIO)hmmio)->pchNext;
  767. if ((cch > lBytes) && (((PMMIO)hmmio)->fccIOProc == FOURCC_MEM))
  768. {
  769. /* this is a memory file -- expand it */
  770. if (mmioExpandMemFile(((PMMIO)hmmio), cch - lBytes) != 0)
  771. return -1; // cannot expand
  772. lBytes = ((PMMIO)hmmio)->pchEndWrite - ((PMMIO)hmmio)->pchNext;
  773. }
  774. /* can only write at most <cch> bytes into the buffer */
  775. if (lBytes > cch)
  776. lBytes = cch;
  777. /* this is where some performance improvements can
  778. * be made, especially for small writes... should
  779. * special-case cases when segment boundaries are
  780. * not crossed (or maybe hmemcpy() should do that)
  781. */
  782. if (lBytes > 0)
  783. {
  784. memcpy(((PMMIO)hmmio)->pchNext, pch, lBytes);
  785. ((PMMIO)hmmio)->dwFlags |= MMIO_DIRTY;
  786. ((PMMIO)hmmio)->pchNext += lBytes;
  787. pch += lBytes;
  788. cch -= lBytes;
  789. lTotalBytesWritten += lBytes;
  790. }
  791. /* validate <pchEndRead>, i.e. re-enforce the invariant that
  792. * <pchEndRead> points past the last valid byte in the buffer
  793. */
  794. if (((PMMIO)hmmio)->pchEndRead < ((PMMIO)hmmio)->pchNext)
  795. ((PMMIO)hmmio)->pchEndRead = ((PMMIO)hmmio)->pchNext;
  796. if (cch == 0) // no more to write?
  797. return lTotalBytesWritten;
  798. /* we need to read beyond this buffer; if we have at least
  799. * another bufferful to read, just call the I/O procedure
  800. */
  801. if (cch > ((PMMIO)hmmio)->cchBuffer)
  802. break;
  803. /* write this buffer (if needed) and read the next
  804. * bufferful (if needed)
  805. */
  806. if (mmioAdvance(hmmio, NULL, MMIO_WRITE) != 0)
  807. return -1;
  808. }
  809. /* we should never need to do MMIOM_WRITE with memory files */
  810. /* flush and empty the I/O buffer and manipulate <lBufOffset>
  811. * directly to change the current file position
  812. */
  813. if (mmioFlush(hmmio, MMIO_EMPTYBUF) != 0)
  814. return -1;
  815. /* call the I/O procedure to do the rest of the writing
  816. * mmioDiskIO is a polymorphic routine, hence we need to cast
  817. * our LPCSTR input pointer to LPTSTR.
  818. */
  819. lBytes = mmioDiskIO(((PMMIO)hmmio), MMIOM_WRITE, (LPSTR)pch, cch);
  820. ((PMMIO)hmmio)->lBufOffset = ((PMMIO)hmmio)->lDiskOffset;
  821. return (lBytes == -1L) ? -1L : lTotalBytesWritten + lBytes;
  822. }
  823. /*--------------------------------------------------------------------*/
  824. /* @doc EXTERNAL
  825. @api LRESULT | mmioSeek | This function changes the current file
  826. position in a file opened with <f mmioOpen>. The current file
  827. position is the location in the file where data is read or written.
  828. @parm HMMIO | hmmio | Specifies the file handle of the file to seek
  829. in.
  830. @parm LONG | lOffset | Specifies an offset to change the file position.
  831. @parm int | iOrigin | Specifies how the offset specified by
  832. <p lOffset> is interpreted. Contains one of the following flags:
  833. @flag SEEK_SET | Seeks to <p lOffset> bytes from the beginning
  834. of the file.
  835. @flag SEEK_CUR | Seeks to <p lOffset> bytes from the current
  836. file position.
  837. @flag SEEK_END | Seeks to <p lOffset> bytes from the end
  838. of the file.
  839. @rdesc The return value is the new file position in bytes, relative
  840. to the beginning of the file. If there is an error, the return value
  841. is -1.
  842. @comm Seeking to an invalid location in the file, such as past the
  843. end of the file, may cause <f mmioSeek> to not return an error,
  844. but may cause subsequent I/O operations on the file to fail.
  845. To locate the end of a file, call <f mmioSeek> with <p lOffset>
  846. set to zero and <p iOrigin> set to SEEK_END.
  847. */
  848. /*--------------------------------------------------------------------*/
  849. LRESULT APIENTRY
  850. mmioSeek(HMMIO hmmio, LONG lOffset, int iOrigin)
  851. {
  852. LONG lCurOffset; // disk offset of <pchNext>
  853. LONG lEndBufOffset; // disk offset of end of buffer
  854. LONG lNewOffset; // new disk offset
  855. V_HANDLE(hmmio, TYPE_MMIO, -1);
  856. /* careful! all this buffer pointer manipulation is fine, but keep
  857. * in mind that buffering may be disabled (in which case <pchEndRead>
  858. * and <pchBuffer> will both be NULL, so the buffer will appear to
  859. * be zero bytes in size)
  860. */
  861. /* <((PMMIO)hmmio)->lBufOffset> is the disk offset of the start of the
  862. * start of the buffer; determine <lCurOffset>, the offset of <pchNext>,
  863. * and <lEndBufOffset>, the offset of the end of the valid part
  864. * of the buffer
  865. */
  866. lCurOffset = ((PMMIO)hmmio)->lBufOffset +
  867. (((PMMIO)hmmio)->pchNext - ((PMMIO)hmmio)->pchBuffer);
  868. lEndBufOffset = ((PMMIO)hmmio)->lBufOffset +
  869. (((PMMIO)hmmio)->pchEndRead - ((PMMIO)hmmio)->pchBuffer);
  870. /* determine <lNewOffset>, the offset to seek to */
  871. switch (iOrigin)
  872. {
  873. case SEEK_SET: // seek relative to start of file
  874. lNewOffset = lOffset;
  875. break;
  876. case SEEK_CUR: // seek relative to current location
  877. lNewOffset = lCurOffset + lOffset;
  878. break;
  879. case SEEK_END: // seek relative to end of file
  880. if (((PMMIO)hmmio)->fccIOProc == FOURCC_MEM)
  881. lNewOffset = lEndBufOffset - lOffset;
  882. else
  883. {
  884. LONG lEndFileOffset;
  885. /* find out where the end of the file is */
  886. lEndFileOffset
  887. = IOProc( (PMMIO)hmmio, MMIOM_SEEK, (LONG) 0, (LONG) SEEK_END);
  888. if (lEndFileOffset == -1)
  889. return -1;
  890. /* Check that we don't have buffered data not yet written */
  891. if (lEndBufOffset > lEndFileOffset) {
  892. lEndFileOffset = lEndBufOffset;
  893. }
  894. lNewOffset = lEndFileOffset - lOffset;
  895. }
  896. break;
  897. default: lNewOffset = 0;
  898. {
  899. DEBUGMSG(1, (TEXT("Invalid seek type %d\n"),iOrigin));
  900. DEBUGCHK(FALSE);
  901. }
  902. }
  903. if ( (lNewOffset >= ((PMMIO)hmmio)->lBufOffset)
  904. && (lNewOffset <= lEndBufOffset)
  905. )
  906. {
  907. /* seeking within the valid part of the buffer
  908. * (possibly including seeking to <lEndBufOffset>)
  909. */
  910. ((PMMIO)hmmio)->pchNext = ((PMMIO)hmmio)->pchBuffer +
  911. (lNewOffset - ((PMMIO)hmmio)->lBufOffset);
  912. }
  913. else
  914. {
  915. /* seeking outside the buffer */
  916. if (((PMMIO)hmmio)->fccIOProc == FOURCC_MEM)
  917. return -1; // can't seek outside mem. file buffer
  918. if (mmioFlush(hmmio, 0) != 0)
  919. return -1;
  920. /* the current "buffered file position" (same as <lDiskOffset>
  921. * for unbuffered files) equals <lBufOffset> +
  922. * (<pchNext> - <pchBuffer>); we'll move the current buffered
  923. * file position (and empty the buffer, since it becomes
  924. * invalid when <lBufOffset> changes) as follows...
  925. */
  926. ((PMMIO)hmmio)->lBufOffset = lNewOffset;
  927. ((PMMIO)hmmio)->pchNext
  928. = ((PMMIO)hmmio)->pchEndRead
  929. = ((PMMIO)hmmio)->pchBuffer;
  930. /* don't need to actually seek right now, since the next
  931. * MMIOM_READ or MMIOM_WRITE will have to seek anyway
  932. */
  933. }
  934. return lNewOffset;
  935. }
  936. /*--------------------------------------------------------------------*/
  937. /* @doc EXTERNAL
  938. @api MMRESULT | mmioGetInfo | This function retrieves information
  939. about a file opened with <f mmioOpen>. This information allows the
  940. caller to directly access the I/O buffer, if the file is opened
  941. for buffered I/O.
  942. @parm HMMIO | hmmio | Specifies the file handle of the file.
  943. @parm LPMMIOINFO | lpmmioinfo | Specifies a pointer to a
  944. caller-allocated <t MMIOINFO> structure that <f mmioGetInfo>
  945. fills with information about the file. See the <t MMIOINFO> structure
  946. and the <f mmioOpen> function for information about the fields in
  947. this structure.
  948. @parm UINT | uFlags | Is not used and should be set to zero.
  949. @rdesc The return value is zero if the function is successful.
  950. @comm To directly access the I/O buffer of a file opened for
  951. buffered I/O, use the following fields of the <t MMIOINFO> structure
  952. filled by <f mmioGetInfo>:
  953. -- The <e MMIOINFO.pchNext> field points to the next byte in the
  954. buffer that can be read or written. When you read or write, increment
  955. <e MMIOINFO.pchNext> by the number of bytes read or written.
  956. -- The <e MMIOINFO.pchEndRead> field points to one byte past the
  957. last valid byte in the buffer that can be read.
  958. -- The <e MMIOINFO.pchEndWrite> field points to one byte past the
  959. last location in the buffer that can be written.
  960. Once you read or write to the buffer and modify
  961. <e MMIOINFO.pchNext>, do not call any MMIO function except
  962. <f mmioAdvance> until you call <f mmioSetInfo>. Call <f mmioSetInfo>
  963. when you are finished directly accessing the buffer.
  964. When you reach the end of the buffer specified by
  965. <e MMIOINFO.pchEndRead> or <e MMIOINFO.pchEndWrite>, call
  966. <f mmioAdvance> to fill the buffer from the disk, or write
  967. the buffer to the disk. The <f mmioAdvance> function
  968. will update the <e MMIOINFO.pchNext>, <e MMIOINFO.pchEndRead>, and
  969. <e MMIOINFO.pchEndWrite> fields in the <t MMIOINFO> structure for the
  970. file.
  971. Before calling <f mmioAdvance> or <f mmioSetInfo> to flush a
  972. buffer to disk, set the MMIO_DIRTY flag in the <e MMIOINFO.dwFlags>
  973. field of the <t MMIOINFO> structure for the file. Otherwise, the
  974. buffer will not get written to disk.
  975. Do not decrement <e MMIOINFO.pchNext> or modify any fields in the
  976. <t MMIOINFO> structure other than <e MMIOINFO.pchNext> and
  977. <e MMIOINFO.dwFlags>. Do not set any flags in <e MMIOINFO.dwFlags>
  978. except MMIO_DIRTY.
  979. @xref mmioSetInfo MMIOINFO
  980. */
  981. /*--------------------------------------------------------------------*/
  982. MMRESULT APIENTRY
  983. mmioGetInfo(HMMIO hmmio, LPMMIOINFO lpmmioinfo, UINT uFlags)
  984. {
  985. V_HANDLE(hmmio, TYPE_MMIO, MMSYSERR_INVALHANDLE);
  986. V_WPOINTER(lpmmioinfo, sizeof(MMIOINFO), MMSYSERR_INVALPARAM);
  987. *lpmmioinfo = *((PMMIO)hmmio);
  988. return 0;
  989. }
  990. /*--------------------------------------------------------------------*/
  991. /* @doc EXTERNAL
  992. @api MMRESULT | mmioSetInfo | This function updates the information
  993. retrieved by <f mmioGetInfo> about a file opened with <f mmioOpen>.
  994. Use this function to terminate direct buffer access of a file opened
  995. for buffered I/O.
  996. @parm HMMIO | hmmio | Specifies the file handle of the file.
  997. @parm LPMMIOINFO | lpmmioinfo | Specifies a pointer to an
  998. <t MMIOINFO> structure filled with information with
  999. <f mmioGetInfo>.
  1000. @parm UINT | uFlags | Is not used and should be set to zero.
  1001. @rdesc The return value is zero if the function is successful.
  1002. @comm If you have written to the file I/O buffer, set the
  1003. MMIO_DIRTY flag in the <e MMIOINFO.dwFlags> field of the <t MMIOINFO>
  1004. structure before calling <f mmioSetInfo> to terminate direct buffer
  1005. access. Otherwise, the buffer will not get flushed to disk.
  1006. @xref mmioGetInfo MMIOINFO
  1007. */
  1008. /*--------------------------------------------------------------------*/
  1009. MMRESULT APIENTRY
  1010. mmioSetInfo(HMMIO hmmio, LPCMMIOINFO lpmmioinfo, UINT fuInfo)
  1011. {
  1012. V_HANDLE(hmmio, TYPE_MMIO, MMSYSERR_INVALHANDLE);
  1013. V_RPOINTER(lpmmioinfo, sizeof(MMIOINFO), MMSYSERR_INVALPARAM);
  1014. V_WPOINTER0( lpmmioinfo->pchBuffer
  1015. , lpmmioinfo->cchBuffer
  1016. , MMSYSERR_INVALPARAM
  1017. );
  1018. V_CALLBACK((FARPROC)lpmmioinfo->pIOProc, MMSYSERR_INVALPARAM);
  1019. /* copy the relevant information from <lpmmioinfo> back into <hmmio> */
  1020. *((PMMIO)hmmio) = *lpmmioinfo;
  1021. /* validate <pchEndRead>, i.e. re-enforce the invariant that
  1022. * <pchEndRead> points past the last valid byte in the buffer
  1023. */
  1024. if (((PMMIO)hmmio)->pchEndRead < ((PMMIO)hmmio)->pchNext)
  1025. ((PMMIO)hmmio)->pchEndRead = ((PMMIO)hmmio)->pchNext;
  1026. return 0;
  1027. }
  1028. /*--------------------------------------------------------------------*/
  1029. /* @doc EXTERNAL
  1030. @api MMRESULT | mmioSetBuffer | This function enables or disables
  1031. buffered I/O, or changes the buffer or buffer size for a file opened
  1032. with <f mmioOpen>.
  1033. @parm HMMIO | hmmio | Specifies the file handle of the file.
  1034. @parm LPSTR | pchBuffer | Specifies a pointer to a
  1035. caller-supplied buffer to use for buffered I/O. If NULL,
  1036. <f mmioSetBuffer> allocates an internal buffer for buffered I/O.
  1037. @parm LONG | cchBuffer | Specifies the size of the caller-supplied
  1038. buffer, or the size of the buffer for <f mmioSetBuffer> to allocate.
  1039. @parm UINT | fuInfo | Is not used and should be set to zero.
  1040. @rdesc The return value is zero if the function is successful.
  1041. Otherwise, the return value specifies an error code. If an error
  1042. occurs, the file handle remains valid. The error code can be one
  1043. of the following codes:
  1044. @flag MMIOERR_CANNOTWRITE | The contents of the old buffer could
  1045. not be written to disk, so the operation was aborted.
  1046. @flag MMIOERR_OUTOFMEMORY | The new buffer could not be allocated,
  1047. probably due to a lack of available memory.
  1048. @comm To enable buffering using an internal buffer, set
  1049. <p pchBuffer> to NULL and <p cchBuffer> to the desired buffer size.
  1050. To supply your own buffer, set <p pchBuffer> to point to the buffer,
  1051. and set <p cchBuffer> to the size of the buffer.
  1052. To disable buffered I/O, set <p pchBuffer> to NULL and
  1053. <p cchBuffer> to zero.
  1054. If buffered I/O is already enabled using an internal buffer, you
  1055. can reallocate the buffer to a different size by setting
  1056. <p pchBuffer> to NULL and <p cchBuffer> to the new buffer size. The
  1057. contents of the buffer may be changed after resizing.
  1058. */
  1059. /*--------------------------------------------------------------------*/
  1060. MMRESULT APIENTRY
  1061. mmioSetBuffer( HMMIO hmmio
  1062. , LPSTR pchBuffer
  1063. , LONG cchBuffer
  1064. , UINT uFlags
  1065. )
  1066. {
  1067. MMRESULT mmr;
  1068. HANDLE hMem;
  1069. V_HANDLE(hmmio, TYPE_MMIO, MMSYSERR_INVALHANDLE);
  1070. // Validate the buffer - for READ/WRITE as appropriate
  1071. if (((PMMIO)hmmio)->dwFlags & (MMIO_WRITE | MMIO_READWRITE)) {
  1072. V_WPOINTER0(pchBuffer, cchBuffer, MMSYSERR_INVALPARAM);
  1073. } else {
  1074. V_RPOINTER0(pchBuffer, cchBuffer, MMSYSERR_INVALPARAM);
  1075. }
  1076. if ((((PMMIO)hmmio)->dwFlags & MMIO_ALLOCBUF) &&
  1077. (pchBuffer == NULL) && (cchBuffer > 0))
  1078. {
  1079. /* grow or shrink buffer in-place */
  1080. LPSTR pch;
  1081. LONG lDeltaNext;
  1082. LONG lDeltaEndRead;
  1083. /* Since the ALLOCBUF flag is set, we must have a buffer */
  1084. /* write the buffer to disk, but don't empty it */
  1085. if ((mmr = mmioFlush(hmmio, 0)) != 0)
  1086. return mmr;
  1087. for(;;)
  1088. {
  1089. /* remember where <pchNext> and <pchEndRead> are
  1090. * in the buffer
  1091. */
  1092. lDeltaNext = ((PMMIO)hmmio)->pchNext - ((PMMIO)hmmio)->pchBuffer;
  1093. lDeltaEndRead
  1094. = ((PMMIO)hmmio)->pchEndRead - ((PMMIO)hmmio)->pchBuffer;
  1095. if (cchBuffer >= lDeltaNext)
  1096. break;
  1097. /* caller wants to truncate the part of the buffer
  1098. * that contains <pchNext> -- handle this by
  1099. * emptying the buffer, recalculating <lDeltaNext>
  1100. * and <lDeltaEndRead>, and continuing below
  1101. */
  1102. if ((mmr = mmioFlush(hmmio, MMIO_EMPTYBUF)) != 0)
  1103. return mmr;
  1104. }
  1105. /* reallocate buffer */
  1106. {
  1107. HANDLE hTemp;
  1108. hTemp = LocalHandle( ((PMMIO)hmmio)->pchBuffer );
  1109. LocalUnlock( hTemp );
  1110. hMem = LocalReAlloc( hTemp
  1111. , cchBuffer
  1112. , LMEM_MOVEABLE
  1113. );
  1114. pch = LocalLock(hMem);
  1115. DEBUGMSG(1, (TEXT("mmioSetBuffer reallocated ptr %8x, handle %8x, to ptr %8x (handle %8x)\n"),
  1116. ((PMMIO)hmmio)->pchBuffer, hTemp, pch, hMem));
  1117. }
  1118. /* If we cannot allocate the new buffer, exit with no
  1119. * harm done.
  1120. */
  1121. if (pch == NULL)
  1122. return MMIOERR_OUTOFMEMORY; // out of memory
  1123. /* transfer pointers to new buffer */
  1124. ((PMMIO)hmmio)->cchBuffer = cchBuffer;
  1125. ((PMMIO)hmmio)->pchBuffer = pch;
  1126. ((PMMIO)hmmio)->pchNext = pch + lDeltaNext;
  1127. ((PMMIO)hmmio)->pchEndRead = pch + lDeltaEndRead;
  1128. /* <pchEndWrite> always points to the end of the buf. */
  1129. ((PMMIO)hmmio)->pchEndWrite = ((PMMIO)hmmio)->pchBuffer + cchBuffer;
  1130. /* check if the reallocation truncated valid data */
  1131. if (lDeltaEndRead > cchBuffer)
  1132. ((PMMIO)hmmio)->pchEndRead = ((PMMIO)hmmio)->pchEndWrite;
  1133. return 0;
  1134. }
  1135. /* write the buffer to disk and stop using the buffer */
  1136. if ((mmr = mmioFlush(hmmio, MMIO_EMPTYBUF)) != 0)
  1137. return mmr;
  1138. if (((PMMIO)hmmio)->dwFlags & MMIO_ALLOCBUF)
  1139. {
  1140. hMem = LocalHandle( ((PMMIO)hmmio)->pchBuffer);
  1141. LocalUnlock( hMem );
  1142. LocalFree( hMem );
  1143. ((PMMIO)hmmio)->dwFlags &= ~MMIO_ALLOCBUF;
  1144. }
  1145. /* Initially, no error. */
  1146. mmr = 0;
  1147. if ((pchBuffer == NULL) && (cchBuffer > 0))
  1148. {
  1149. hMem = LocalAlloc(LMEM_MOVEABLE, cchBuffer);
  1150. if (hMem)
  1151. pchBuffer = LocalLock(hMem);
  1152. //else pchBuffer = NULL;
  1153. /* If there is an error, change the file to be un-buffered
  1154. * and return an error code. The file is still valid.
  1155. * (Just for a little extra security.)
  1156. */
  1157. if (pchBuffer == NULL)
  1158. { mmr = MMIOERR_OUTOFMEMORY;
  1159. cchBuffer = 0L;
  1160. }
  1161. else
  1162. ((PMMIO)hmmio)->dwFlags |= MMIO_ALLOCBUF;
  1163. }
  1164. /* invariant: <pchEndRead> points past the end of the "valid" portion
  1165. * of the buffer, and <pchEndWrite> points past the last byte that
  1166. * can be written into; <pchNext> points to the next byte to read
  1167. * or write; <lBufOffset> is the current disk offset of the start
  1168. * of the buffer, and it will not change
  1169. */
  1170. ((PMMIO)hmmio)->pchBuffer = pchBuffer;
  1171. ((PMMIO)hmmio)->cchBuffer = cchBuffer;
  1172. ((PMMIO)hmmio)->pchNext
  1173. = ((PMMIO)hmmio)->pchEndRead = ((PMMIO)hmmio)->pchBuffer;
  1174. ((PMMIO)hmmio)->pchEndWrite = ((PMMIO)hmmio)->pchBuffer + cchBuffer;
  1175. return mmr;
  1176. }
  1177. /*--------------------------------------------------------------------*/
  1178. /* @doc EXTERNAL
  1179. @api MMRESULT | mmioFlush | This function writes the I/O buffer of a
  1180. file to disk, if the I/O buffer has been written to.
  1181. @parm HMMIO | hmmio | Specifies the file handle of a file opened
  1182. with <f mmioOpen>.
  1183. @parm UINT | uFlags | Is not used and should be set to zero.
  1184. @rdesc The return value is zero if the function is successful.
  1185. Otherwise, the return value specifies an error code. The error
  1186. code can be one of the following codes:
  1187. @flag MMIOERR_CANNOTWRITE | The contents of the buffer could
  1188. not be written to disk.
  1189. @comm Closing a file with <f mmioClose> will automatically flush
  1190. its buffer.
  1191. If there is insufficient disk space to write the
  1192. buffer, <f mmioFlush> will fail, even if the preceding <f mmioWrite>
  1193. calls were successful.
  1194. */
  1195. /*--------------------------------------------------------------------*/
  1196. MMRESULT APIENTRY
  1197. mmioFlush(HMMIO hmmio, UINT uFlags)
  1198. {
  1199. LONG lBytesAsk; // no. bytes to write
  1200. LONG lBytesWritten; // no. bytes actually written
  1201. V_HANDLE(hmmio, TYPE_MMIO, MMSYSERR_INVALHANDLE);
  1202. if ( ( ((PMMIO)hmmio)->fccIOProc
  1203. == FOURCC_MEM
  1204. )
  1205. || ( ((PMMIO)hmmio)->pchBuffer == NULL )
  1206. )
  1207. return 0; // cannot flush memory files
  1208. /* if the file is unbuffered then the dirty flag should not be set */
  1209. if (((PMMIO)hmmio)->dwFlags & MMIO_DIRTY)
  1210. {
  1211. /* figure out how many bytes need to be flushed */
  1212. lBytesAsk = ((PMMIO)hmmio)->pchEndRead - ((PMMIO)hmmio)->pchBuffer;
  1213. /* write the buffer to disk */
  1214. lBytesWritten = mmioDiskIO(((PMMIO)hmmio), MMIOM_WRITEFLUSH,
  1215. ((PMMIO)hmmio)->pchBuffer, lBytesAsk);
  1216. if (lBytesWritten != lBytesAsk)
  1217. return MMIOERR_CANNOTWRITE;
  1218. ((PMMIO)hmmio)->dwFlags &= ~MMIO_DIRTY; // buffer is clean now
  1219. }
  1220. if (uFlags & MMIO_EMPTYBUF)
  1221. {
  1222. /* empty the I/O buffer, and update <lBufOffset> to reflect
  1223. * what the current file position is
  1224. */
  1225. ((PMMIO)hmmio)->lBufOffset
  1226. += (((PMMIO)hmmio)->pchNext - ((PMMIO)hmmio)->pchBuffer);
  1227. ((PMMIO)hmmio)->pchNext
  1228. = ((PMMIO)hmmio)->pchEndRead = ((PMMIO)hmmio)->pchBuffer;
  1229. }
  1230. return 0;
  1231. }
  1232. /*--------------------------------------------------------------------*/
  1233. /* @doc EXTERNAL
  1234. @api MMRESULT | mmioAdvance | This function advances the I/O buffer of
  1235. a file set up for direct I/O buffer access with <f mmioGetInfo>. If
  1236. the file is opened for reading, the I/O buffer is filled from the
  1237. disk. If the file is opened for writing and the MMIO_DIRTY flag is
  1238. set in the <e MMIOINFO.dwFlags> field of the <t MMIOINFO> structure,
  1239. the buffer is written to disk. The <e MMIOINFO.pchNext>,
  1240. <e MMIOINFO.pchEndRead>, and <e MMIOINFO.pchEndWrite> fields of the
  1241. <t MMIOINFO> structure are updated to reflect the new state of
  1242. the I/O buffer.
  1243. @parm HMMIO | hmmio | Specifies the file handle for a file opened
  1244. with <f mmioOpen>.
  1245. @parm LPMMIOINFO | lpmmioinfo | Optionally specifies a pointer to the
  1246. <t MMIOINFO> structure obtained with <f mmioGetInfo>, which is used to
  1247. set the current file information, then updated after the buffer is
  1248. advanced.
  1249. @parm UINT | uFlags | Specifies options for the operation.
  1250. Contains exactly one of the following two flags:
  1251. @flag MMIO_READ | The buffer is filled from the file.
  1252. @flag MMIO_WRITE | The buffer is written to the file.
  1253. @rdesc The return value is zero if the operation is successful.
  1254. Otherwise, the return value specifies an error code. The error
  1255. code can be one of the following codes:
  1256. @flag MMIOERR_CANNOTWRITE | The contents of the buffer could
  1257. not be written to disk.
  1258. @flag MMIOERR_CANNOTREAD | An error occurred while re-filling
  1259. the buffer.
  1260. @flag MMIOERR_UNBUFFERED | The specified file is not opened
  1261. for buffered I/O.
  1262. @flag MMIOERR_CANNOTEXPAND | The specified memory file cannot
  1263. be expanded, probably because the <e MMIOINFO.adwInfo[0]> field
  1264. was set to zero in the initial call to <f mmioOpen>.
  1265. @flag MMIOERR_OUTOFMEMORY | There was not enough memory to expand
  1266. a memory file for further writing.
  1267. @comm If the specified file is opened for writing or for both
  1268. reading and writing, the I/O buffer will be flushed to disk before
  1269. the next buffer is read. If the I/O buffer cannot be written to disk
  1270. because the disk is full, then <f mmioAdvance> will return
  1271. MMIOERR_CANNOTWRITE.
  1272. If the specified file is only open for writing, the MMIO_WRITE
  1273. flag must be specified.
  1274. If you have written to the I/O buffer, you must set the MMIO_DIRTY
  1275. flag in the <e MMIOINFO.dwFlags> field of the <t MMIOINFO> structure
  1276. before calling <f mmioAdvance>. Otherwise, the buffer will not be
  1277. written to disk.
  1278. If the end of file is reached, <f mmioAdvance> will still return
  1279. success, even though no more data can be read. Thus, to check for
  1280. the end of the file, it is necessary to see if the
  1281. <e MMIOINFO.pchNext> and <e MMIOINFO.pchEndRead> fields of the
  1282. <t MMIOINFO> structure are equal after calling <f mmioAdvance>.
  1283. @xref mmioGetInfo MMIOINFO
  1284. */
  1285. /*--------------------------------------------------------------------*/
  1286. MMRESULT APIENTRY
  1287. mmioAdvance(HMMIO hmmio, LPMMIOINFO lpmmioinfo, UINT uFlags)
  1288. {
  1289. LONG lBytesRead; // bytes actually read
  1290. UINT w;
  1291. V_HANDLE(hmmio, TYPE_MMIO, MMSYSERR_INVALHANDLE);
  1292. if (((PMMIO)hmmio)->pchBuffer == NULL)
  1293. return MMIOERR_UNBUFFERED;
  1294. if (lpmmioinfo != NULL) {
  1295. V_WPOINTER(lpmmioinfo, sizeof(MMIOINFO), MMSYSERR_INVALPARAM);
  1296. mmioSetInfo(hmmio, lpmmioinfo, 0);
  1297. }
  1298. if (((PMMIO)hmmio)->fccIOProc == FOURCC_MEM)
  1299. {
  1300. /* this is a memory file:
  1301. * -- if the caller is reading, cannot advance
  1302. * -- if the caller is writing, then advance by expanding
  1303. * the buffer (if possible) if the there is less than
  1304. * <adwInfo[0]> bytes left in the buffer
  1305. */
  1306. if (!(uFlags & MMIO_WRITE))
  1307. return MMIOERR_CANNOTREAD;
  1308. if ( (DWORD)(((PMMIO)hmmio)->pchEndWrite - ((PMMIO)hmmio)->pchNext)
  1309. >= ((PMMIO)hmmio)->adwInfo[0]
  1310. )
  1311. return MMIOERR_CANNOTEXPAND;
  1312. if ((w = mmioExpandMemFile(((PMMIO)hmmio), 1L)) != 0)
  1313. return w; // out of memory, or whatever
  1314. goto GETINFO_AND_EXIT;
  1315. }
  1316. /* empty the I/O buffer, which will effectively advance the
  1317. * buffer by (<pchNext> - <pchBuffer>) bytes
  1318. */
  1319. if ((w = mmioFlush(hmmio, MMIO_EMPTYBUF)) != 0)
  1320. return w;
  1321. /* if MMIO_WRITE bit is not set in uFlags, fill the buffer */
  1322. if (!(uFlags & MMIO_WRITE))
  1323. {
  1324. /* read the next bufferful from the file */
  1325. lBytesRead = mmioDiskIO(((PMMIO)hmmio), MMIOM_READ,
  1326. ((PMMIO)hmmio)->pchBuffer, ((PMMIO)hmmio)->cchBuffer);
  1327. if (lBytesRead == -1)
  1328. return MMIOERR_CANNOTREAD;
  1329. /* reading zero bytes should not be treated as an error
  1330. * condition -- e.g. open a new file R+W and call
  1331. * mmioAdvance(), and MMIOM_READ will return zero bytes
  1332. * because the file started off empty
  1333. */
  1334. ((PMMIO)hmmio)->pchEndRead += lBytesRead;
  1335. }
  1336. GETINFO_AND_EXIT:
  1337. /* copy <hmmio> back to <lpmmioinfo> if <lpmmioinfo> is provided */
  1338. if (lpmmioinfo != NULL)
  1339. mmioGetInfo(hmmio, lpmmioinfo, 0);
  1340. return 0;
  1341. }
  1342. /*--------------------------------------------------------------------*/
  1343. /* @doc EXTERNAL
  1344. @api FOURCC | mmioStringToFOURCC | This function converts a
  1345. null-terminated string to a four-character code.
  1346. @parm LPCTSTR | sz | Specifies a pointer to a null-terminated
  1347. string to a four-character code.
  1348. @parm UINT | uFlags | Specifies options for the conversion:
  1349. @flag MMIO_TOUPPER | Converts all characters to uppercase.
  1350. @rdesc The return value is the four character code created from the
  1351. given string.
  1352. @comm This function does not check to see if the string referenced
  1353. by <p sz> follows any conventions regarding which characters to
  1354. include in a four-character code. The string is
  1355. simply copied to a four-character code and padded with blanks or
  1356. truncated to four characters if required.
  1357. @xref mmioFOURCC
  1358. */
  1359. /*--------------------------------------------------------------------*/
  1360. FOURCC APIENTRY mmioStringToFOURCCW( LPCWSTR sz, UINT uFlags )
  1361. {
  1362. FOURCC fcc;
  1363. PBYTE pByte; // ascii version of szFileName
  1364. ULONG cbDst; // character count of szFileName
  1365. int cchSrc;
  1366. // V_STRING(sz, -1, 0);
  1367. /*------------------------------------------------------------*\
  1368. * Convert the given unicode string into ascii and then call
  1369. * the ascii version of mmioStringToFOURCCW
  1370. \*------------------------------------------------------------*/
  1371. cbDst = ((cchSrc = wcslen( sz )) * sizeof(WCHAR)) + sizeof(WCHAR);
  1372. pByte = HeapAlloc( hHeap, 0, cbDst );
  1373. if ( pByte == (PBYTE)NULL ) {
  1374. return (FOURCC)NULL;
  1375. }
  1376. cbDst = WideCharToMultiByte(GetACP(), 0, sz, cchSrc, pByte, cbDst, NULL, NULL);
  1377. /* Do it ourselves...it's not that hard */
  1378. if (cbDst > 0) {
  1379. int i = sizeof(FOURCC);
  1380. char *pchSrc = pByte, *pchDst = (char *)&fcc;
  1381. while (i-- > 0)
  1382. {
  1383. if (*pchSrc)
  1384. {
  1385. if (uFlags & MMIO_TOUPPER)
  1386. *pchDst++ = (char)CharUpper((LPTSTR)*pchSrc++);
  1387. else
  1388. *pchDst++ = *pchSrc++;
  1389. }
  1390. else
  1391. *pchDst++ = ' ';
  1392. }
  1393. } else {
  1394. fcc = (FOURCC)NULL;
  1395. }
  1396. HeapFree( hHeap, 0, pByte );
  1397. return (FOURCC)fcc;
  1398. }
  1399. /*--------------------------------------------------------------------*/
  1400. /* @doc EXTERNAL
  1401. @api LPMMIOPROC | mmioInstallIOProc | This function installs or
  1402. removes a custom I/O procedure. It will also locate an installed I/O
  1403. procedure, given its corresponding four-character code.
  1404. @parm FOURCC | fccIOProc | Specifies a four-character code
  1405. identifying the I/O procedure to install, remove, or locate. All
  1406. characters in this four-character code should be uppercase characters.
  1407. @parm LPMMIOPROC | pIOProc | Specifies the address of the I/O
  1408. procedure to install. To remove or locate an I/O procedure, set this
  1409. parameter to NULL.
  1410. @parm DWORD | dwFlags | Specifies one of the following flags
  1411. indicating whether the I/O procedure is being installed, removed, or
  1412. located:
  1413. @flag MMIO_INSTALLPROC | Installs the specified I/O procedure.
  1414. @flag MMIO_GLOBALPROC | This flag is a modifier to the install flag,
  1415. and indicates the I/O procedure should be installed for global
  1416. use. This flag is ignored on removal or find.
  1417. @flag MMIO_REMOVEPROC | Removes the specified I/O procedure.
  1418. @flag MMIO_FINDPROC | Searches for the specified I/O procedure.
  1419. @rdesc The return value is the address of the I/O procedure
  1420. installed, removed, or located. If there is an error, the return value
  1421. is NULL.
  1422. @comm If the I/O procedure resides in the application, use
  1423. <f MakeProcInstance> for compatibility with 16 bit windows
  1424. to get a procedure-instance address and specify
  1425. this address for <p pIOProc>. You don't need to get a procedure-instance
  1426. address if the I/O procedure resides in a DLL.
  1427. @cb LONG FAR PASCAL | IOProc | <f IOProc> is a placeholder for the
  1428. application-supplied function name. The actual name must be exported
  1429. by including it in a EXPORTS statement in the application's
  1430. module-definitions file.
  1431. @parm LPSTR | lpmmioinfo | Specifies a pointer to an
  1432. <t MMIOINFO> structure containing information about the open
  1433. file. The I/O procedure must maintain the <e MMIOINFO.lDiskOffset>
  1434. field in this structure to indicate the file offset to the
  1435. next read or write location. The I/O procedure can use the
  1436. <e MMIOINFO.adwInfo[]> field to store state information. The
  1437. I/O procedure should not modify any other fields of the
  1438. <t MMIOINFO> structure.
  1439. @parm UINT | wMsg | Specifies a message indicating the
  1440. requested I/O operation. Messages that can be received include
  1441. <m MMIOM_OPEN>, <m MMIOM_CLOSE>, <m MMIOM_READ>, <m MMIOM_WRITE>,
  1442. and <m MMIOM_SEEK>.
  1443. @parm LONG | lParam1 | Specifies a parameter for the message.
  1444. @parm LONG | lParam2 | Specifies a parameter for the message.
  1445. @rdesc The return value depends on the message specified by
  1446. <p wMsg>. If the I/O procedure does not recognize a message, it should
  1447. return zero.
  1448. @comm The four-character code specified by the
  1449. <e MMIOINFO.fccIOProc> field in the <t MMIOINFO> structure
  1450. associated with a file identifies a filename extension for a custom
  1451. storage system. When an application calls <f mmioOpen> with a
  1452. filename such as "foo.xyz!bar", the I/O procedure associated with the
  1453. four-character code "XYZ " is called to open the "bar" element of the
  1454. file "foo.xyz".
  1455. The <f mmioInstallIOProc> function maintains a separate list of
  1456. installed I/O procedures for each Windows application. Therefore,
  1457. different applications can use the same I/O procedure identifier for
  1458. different I/O procedures without conflict. Installing an I/O procedure
  1459. globally however enables any process to use the procedure.
  1460. If an application calls <f mmioInstallIOProc> more than once to
  1461. register the same I/O procedure, then it must call
  1462. <f mmioInstallIOProc> to remove the procedure once for each time it
  1463. installed the procedure.
  1464. <f mmioInstallIOProc> will not prevent an application from
  1465. installing two different I/O procedures with the same identifier, or
  1466. installing an I/O procedure with one of the predefined identifiers
  1467. ("DOS ", "MEM "). The most recently installed procedure
  1468. takes precedence, and the most recently installed procedure is the
  1469. first one to get removed.
  1470. When searching for a specified I/O procedure, local procedures are
  1471. searched first, then global procedures.
  1472. @xref mmioOpen
  1473. */
  1474. /*--------------------------------------------------------------------*/
  1475. LPMMIOPROC APIENTRY
  1476. mmioInstallIOProcW(FOURCC fccIOProc, LPMMIOPROC pIOProc, DWORD dwFlags)
  1477. {
  1478. V_FLAGS(dwFlags, MMIO_VALIDPROC, mmioInstallIOProc, NULL);
  1479. dwFlags |= MMIO_UNICODEPROC;
  1480. return mmioInternalInstallIOProc( fccIOProc, pIOProc, dwFlags);
  1481. }
  1482. static LPMMIOPROC mmioInternalInstallIOProc(
  1483. FOURCC fccIOProc, // I/O Proc 4 char id
  1484. LPMMIOPROC pIOProc, // pointer to any I/O proc to install
  1485. DWORD dwFlags // flags from caller
  1486. )
  1487. {
  1488. IOProcMapEntry *pEnt; // an entry in linked list
  1489. HANDLE hTaskCurrent; // current Windows task handle
  1490. if (fccIOProc == 0L)
  1491. return NULL;
  1492. hTaskCurrent = (HANDLE)GetCurrentThreadId();
  1493. if (dwFlags & MMIO_INSTALLPROC)
  1494. {
  1495. /* install I/O procedure -- always add at the beginning of
  1496. * the list, so it overrides any other I/O procedures
  1497. * with the same identifier installed by the same task
  1498. */
  1499. V_CALLBACK((FARPROC)pIOProc, NULL);
  1500. if ((pEnt = (IOProcMapEntry NEAR *)
  1501. NewHandle(TYPE_MMIO, sizeof(IOProcMapEntry))) == NULL)
  1502. return NULL; // out of memory
  1503. pEnt->fccIOProc = fccIOProc;
  1504. pEnt->pIOProc = pIOProc;
  1505. pEnt->hTask = hTaskCurrent;
  1506. pEnt->pNext = gIOProcMapHead;
  1507. gIOProcMapHead = pEnt;
  1508. return pIOProc;
  1509. }
  1510. if (!pIOProc)
  1511. if (dwFlags & MMIO_REMOVEPROC)
  1512. return RemoveIOProc(fccIOProc, hTaskCurrent);
  1513. else if (dwFlags & MMIO_FINDPROC)
  1514. { pIOProcMapEntry pEnt;
  1515. pEnt = FindIOProc(fccIOProc, hTaskCurrent);
  1516. return ( pEnt==NULL
  1517. ? NULL
  1518. : pEnt->pIOProc
  1519. );
  1520. }
  1521. return NULL; // couldn't find requested I/O procedure
  1522. }
  1523. /*--------------------------------------------------------------------*/
  1524. /* @doc EXTERNAL
  1525. @api LRESULT | mmioSendMessage | This function sends a message to the
  1526. I/O procedure associated with the specified file.
  1527. @parm HMMIO | hmmio | Specifies the file handle for a file opened
  1528. with <f mmioOpen>.
  1529. @parm UINT | wMsg | Specifies the message to send to the I/O procedure.
  1530. @parm LONG | lParam1 | Specifies a parameter for the message.
  1531. @parm LONG | lParam2 | Specifies a parameter for the message.
  1532. @rdesc The return value depends on the message. If the I/O procedure
  1533. does not recognize the message, the return value is zero.
  1534. @comm Use this function to send custom user-defined messages. Do
  1535. not use it to send the <m MMIOM_OPEN>, <m MMIOM_CLOSE>,
  1536. <m MMIOM_READ>, <m MMIOM_WRITE>, <m MMIOM_WRITEFLUSH>, or
  1537. <m MMIOM_SEEK> messages. Define
  1538. custom messages to be greater than or equal to the MMIOM_USER constant.
  1539. @xref mmioInstallIOProc
  1540. */
  1541. /*--------------------------------------------------------------------*/
  1542. LRESULT APIENTRY
  1543. mmioSendMessage(HMMIO hmmio, UINT uMsg, LONG lParam1, LONG lParam2)
  1544. {
  1545. V_HANDLE(hmmio, TYPE_MMIO, (LRESULT)0);
  1546. return IOProc( (PMMIO)hmmio, uMsg, lParam1, lParam2);
  1547. }
  1548. /*--------------------------------------------------------------------*/
  1549. /* @doc INTERNAL
  1550. @api LONG | mmioDiskIO | Perform an unbuffered read or write.
  1551. Do not assume where the current disk offset <p lDiskOffset> will be.
  1552. @parm PMMIO | pmmio | The open file handle returned by <f mmioOpen>.
  1553. @parm UINT | wMsg | MMIOM_READ if <f mmioDiskIO> should read from the disk,
  1554. or MMIOM_WRITE if <f mmioDiskIO> should write to the disk,
  1555. or MMIOM_WRITEFLUSH if <f mmioDiskIO> should flush all pending I/O.
  1556. @parm LPSTR | pch | The buffer to read into or write from.
  1557. @parm LONG | cch | The number of bytes to read or write.
  1558. <f mmioDiskIO> changes the disk offset to be <p lBufOffset>
  1559. and then performs an MMIOM_READ or MMIOM_WRITE operation as
  1560. specified by <p wMsg>, <p pch>, and <p cch>.
  1561. Note that if the I/O buffer is not empty at this point, this
  1562. function may not do what you expect.
  1563. Do not call this function for memory files.
  1564. */
  1565. /*--------------------------------------------------------------------*/
  1566. static LONG NEAR PASCAL
  1567. mmioDiskIO(PMMIO pmmio, UINT uMsg, LPSTR pch, LONG cch)
  1568. {
  1569. if (pmmio->lDiskOffset != pmmio->lBufOffset)
  1570. {
  1571. if (IOProc( pmmio
  1572. , MMIOM_SEEK
  1573. , (LONG) pmmio->lBufOffset
  1574. , (LONG) SEEK_SET
  1575. )
  1576. == -1
  1577. )
  1578. return -1;
  1579. }
  1580. return IOProc( pmmio, uMsg, (LONG) pch, (LONG) cch);
  1581. }
  1582. /*--------------------------------------------------------------------*/
  1583. /* @doc INTERNAL
  1584. @api UINT | mmioExpandMemFile | Assuming that <p pmmio> is a memory file,
  1585. expand it by <p lExpand> bytes or <p adwInfo[0]> bytes, whichever
  1586. is larger. Do not disturb the contents of the buffer or change
  1587. the current file position.
  1588. @parm PMMIO | pmmio | The open file handle returned by <f mmioOpen>.
  1589. @parm LONG | lExpand | The minimum number of bytes to expand the buffer by.
  1590. @rdesc If the function succeeds, zero is returned. If the function fails,
  1591. an error code is returned. In particular, MMIOERR_OUTOFMEMORY is
  1592. returned if memory reallocation failed.
  1593. @comm Only call this function for memory files.
  1594. */
  1595. /*--------------------------------------------------------------------*/
  1596. static UINT NEAR PASCAL
  1597. mmioExpandMemFile(PMMIO pmmio, LONG lExpand)
  1598. {
  1599. MMIOMEMINFO * pInfo = (MMIOMEMINFO *) pmmio->adwInfo;
  1600. DWORD dwFlagsTemp;
  1601. UINT w;
  1602. /* make sure buffer can be expanded */
  1603. /* Note: we used to check ALLOC_BUF here, we don't now. */
  1604. if (pInfo->lExpand == 0)
  1605. return MMIOERR_CANNOTEXPAND; // cannot grow file
  1606. /* how much should the buffer be expanded by? */
  1607. if (lExpand < pInfo->lExpand)
  1608. lExpand = pInfo->lExpand;
  1609. dwFlagsTemp = pmmio->dwFlags;
  1610. pmmio->dwFlags |= MMIO_ALLOCBUF;
  1611. w = mmioSetBuffer(((HMMIO)pmmio), NULL,
  1612. pmmio->cchBuffer + lExpand, 0);
  1613. pmmio->dwFlags = dwFlagsTemp;
  1614. return w;
  1615. }
  1616. /*--------------------------------------------------------------------*/
  1617. /* @doc INTERNAL
  1618. @api LRESULT | mmioDOSIOProc | The 'DOS' I/O procedure, which handles I/O
  1619. on ordinary DOS files.
  1620. @parm LPSTR | lpmmioinfo | A pointer to an MMIOINFO block that
  1621. contains information about the open file.
  1622. @parm UINT | uMsg | The message that the I/O procedure is being
  1623. asked to execute.
  1624. @parm LONG | lParam1 | Specifies additional message information.
  1625. @parm LONG | lParam2 | Specifies additional message information.
  1626. @rdesc Return value depends on <p wMsg>.
  1627. */
  1628. /*--------------------------------------------------------------------*/
  1629. LRESULT
  1630. mmioDOSIOProc(LPSTR lpmmioStr, UINT uMsg, LONG lParam1, LONG lParam2)
  1631. {
  1632. PMMIO pmmio = (PMMIO)lpmmioStr; // only in DLL!
  1633. MMIODOSINFO *pInfo = (MMIODOSINFO *)pmmio->adwInfo;
  1634. LONG lResult;
  1635. WCHAR szPath[ MAX_PATH ];
  1636. switch (uMsg) {
  1637. case MMIOM_OPEN:
  1638. /*
  1639. * The extra info parameter optionally contains a
  1640. * sequence number to pass.
  1641. */
  1642. if ( pmmio->dwFlags & MMIO_GETTEMP )
  1643. {
  1644. DEBUGMSG(1, (TEXT("Temp files not supported under UNDER_CE!")));
  1645. return (LRESULT)MMIOERR_FILENOTFOUND;
  1646. }
  1647. /*------------------------------------------------------------*\
  1648. * <lParam1> is either a file name or NULL; if it is
  1649. * NULL, then <adwInfo[0]>, which is actually <pInfo->fh>,
  1650. * should already contain an open DOS file handle.
  1651. *
  1652. * Does lParam1 point to a file name ?
  1653. *
  1654. * if so then either:
  1655. *
  1656. * delete the file,
  1657. * check the existance of the file,
  1658. * parse the file name, or
  1659. * open the file name
  1660. *
  1661. \*------------------------------------------------------------*/
  1662. if ( lParam1 != 0 ) {
  1663. if ( pmmio->dwFlags & MMIO_DELETE ) {
  1664. return DeleteFileW( (LPWSTR)lParam1 )
  1665. ? (LRESULT)0
  1666. : (LRESULT)MMIOERR_FILENOTFOUND;
  1667. }
  1668. if ( pmmio->dwFlags & MMIO_EXIST ) {
  1669. if ( !(pmmio->dwFlags & MMIO_CREATE) ) {
  1670. #ifdef LATER
  1671. I think this should be using SearchPath (with lpszPath==lParam1)
  1672. as the definition of MMIO_EXIST states that a fully qualified
  1673. filename is returned. OR tweak the flags to turn MMIO_PARSE ON
  1674. and execute the next section.
  1675. #endif
  1676. if ( GetFileAttributesW( (LPWSTR)lParam1 ) == -1 ) {
  1677. return (LRESULT)MMIOERR_FILENOTFOUND;
  1678. }
  1679. return (LRESULT)0;
  1680. }
  1681. }
  1682. if ( pmmio->dwFlags & MMIO_PARSE ) {
  1683. DEBUGMSG(1, (TEXT("Filename parsing not supported under UNDER_CE!")));
  1684. return (LRESULT)MMIOERR_FILENOTFOUND;
  1685. }
  1686. {
  1687. DWORD dwAccess = 0;
  1688. DWORD dwSharedMode = 0;
  1689. DWORD dwCreate = 0;
  1690. DWORD dwFlags = FILE_ATTRIBUTE_NORMAL;
  1691. /*----------------------------------------------------*\
  1692. * Look at the access flags
  1693. \*----------------------------------------------------*/
  1694. if ( pmmio->dwFlags & MMIO_WRITE ) {
  1695. dwAccess = GENERIC_WRITE;
  1696. } else {
  1697. dwAccess = GENERIC_READ;
  1698. }
  1699. if ( pmmio->dwFlags & MMIO_READWRITE ) {
  1700. dwAccess |= (GENERIC_WRITE | GENERIC_READ);
  1701. }
  1702. /*----------------------------------------------------*\
  1703. * Set dwSharedMode from the share flags
  1704. \*----------------------------------------------------*/
  1705. { /* owing to some crappy design in WIN3.1, the share flags are
  1706. * exclusive = 10
  1707. * deny write = 20
  1708. * deny read = 30
  1709. * deny none = 40
  1710. * so deny read looks like exclusive + deny write. Sigh.
  1711. * 00 is taken as being DENYNONE (probably correct)
  1712. * So is 50, 60 and 70 (which is probably bogus).
  1713. * As we need to support the DOS flags for WOW, we need this
  1714. * code somewhere, so might as well leave the flag definitions
  1715. * as they are. First pull out all the share mode bits.
  1716. */
  1717. DWORD dwShare = MMIO_DENYWRITE | MMIO_DENYREAD
  1718. | MMIO_DENYNONE | MMIO_EXCLUSIVE;
  1719. dwShare &= pmmio->dwFlags;
  1720. switch (dwShare)
  1721. { case MMIO_DENYWRITE:
  1722. dwSharedMode = FILE_SHARE_READ;
  1723. break;
  1724. case MMIO_DENYREAD:
  1725. dwSharedMode = FILE_SHARE_WRITE;
  1726. break;
  1727. case MMIO_EXCLUSIVE:
  1728. dwSharedMode = 0;
  1729. break;
  1730. case MMIO_DENYNONE:
  1731. default:
  1732. dwSharedMode = FILE_SHARE_WRITE | FILE_SHARE_READ;
  1733. break;
  1734. #ifdef later
  1735. Generate an error for invalid flags?
  1736. #endif
  1737. }
  1738. }
  1739. /*----------------------------------------------------*\
  1740. * Look at the create flags
  1741. \*----------------------------------------------------*/
  1742. if ( (pmmio->dwFlags) & MMIO_CREATE) {
  1743. dwCreate = CREATE_ALWAYS;
  1744. wcscpy( szPath, (LPWSTR)lParam1 );
  1745. } else {
  1746. dwCreate = OPEN_EXISTING;
  1747. /* Under UNDER_CE, better specify the correct filename */
  1748. wcscpy(szPath, (LPWSTR)lParam1);
  1749. }
  1750. pInfo->fh = CreateFileW( szPath,
  1751. dwAccess,
  1752. dwSharedMode,
  1753. NULL,
  1754. dwCreate,
  1755. dwFlags /*| FILE_FLAG_SEQUENTIAL_SCAN*/,
  1756. NULL );
  1757. if ( pInfo->fh == INVALID_HANDLE_VALUE ) {
  1758. return (LRESULT)MMIOERR_FILENOTFOUND;
  1759. }
  1760. if ( pmmio->dwFlags & MMIO_EXIST ) {
  1761. CloseHandle( (HANDLE)pInfo->fh );
  1762. return (LRESULT)0;
  1763. }
  1764. }
  1765. }
  1766. /* check the current file offset */
  1767. pmmio->lDiskOffset = SetFilePointer(pInfo->fh, 0L, NULL, FILE_CURRENT);
  1768. return (LRESULT)0;
  1769. case MMIOM_CLOSE:
  1770. /* MMIO_FHOPEN flag means keep the DOS file handle open */
  1771. if ( !((DWORD)lParam1 & MMIO_FHOPEN)
  1772. && (CloseHandle(pInfo->fh) == HFILE_ERROR) ) {
  1773. return (LRESULT) MMIOERR_CANNOTCLOSE;
  1774. }
  1775. return (LRESULT) 0;
  1776. case MMIOM_READ:
  1777. if (ReadFile(pInfo->fh, (LPVOID)lParam1, (DWORD)lParam2, &lResult, NULL) && lResult > 0) {
  1778. pmmio->lDiskOffset += lResult;
  1779. return (LRESULT) lResult;
  1780. } else
  1781. return (LRESULT) HFILE_ERROR;
  1782. case MMIOM_WRITE:
  1783. case MMIOM_WRITEFLUSH:
  1784. if (WriteFile(pInfo->fh, (LPVOID)lParam1, (DWORD)lParam2, &lResult, NULL) && lResult > 0) {
  1785. pmmio->lDiskOffset += lResult;
  1786. return (LRESULT) lResult;
  1787. }
  1788. #ifdef DOSCANFLUSH
  1789. if (uMsg == MMIOM_WRITEFLUSH)
  1790. {
  1791. /* Issue hardware flush command */
  1792. }
  1793. #endif
  1794. return (LRESULT) HFILE_ERROR;
  1795. case MMIOM_SEEK:
  1796. lResult = SetFilePointer(pInfo->fh, (LONG)lParam1, NULL, (int)(LONG)lParam2);
  1797. if (lResult != -1L) {
  1798. pmmio->lDiskOffset = lResult;
  1799. }
  1800. return (LRESULT) lResult;
  1801. case MMIOM_RENAME:
  1802. if (!MoveFileW((LPWSTR)lParam1, (LPWSTR)lParam2)) {
  1803. return (LRESULT) MMIOERR_FILENOTFOUND;
  1804. /* ??? There are other errors too? e.g. target exists? */
  1805. }
  1806. break;
  1807. }
  1808. return (LRESULT) 0;
  1809. }
  1810. /*--------------------------------------------------------------------*/
  1811. /* @doc INTERNAL
  1812. @api LRESULT | mmioMEMIOProc | The 'MEM' I/O procedure, which handles I/O
  1813. on memory files.
  1814. @parm LPSTR | lpmmioinfo | A pointer to an MMIOINFO block that
  1815. contains information about the open file.
  1816. @parm UINT | uMsg | The message that the I/O procedure is being
  1817. asked to execute.
  1818. @parm LONG | lParam1 | Specifies additional message information.
  1819. @parm LONG | lParam2 | Specifies additional message information.
  1820. @rdesc Return value depends on <p uMsg>.
  1821. */
  1822. /*--------------------------------------------------------------------*/
  1823. LRESULT
  1824. mmioMEMIOProc(LPSTR lpmmioStr, UINT uMsg, LONG lParam1, LONG lParam2)
  1825. {
  1826. PMMIO pmmio = (PMMIO) lpmmioStr; // only in DLL!
  1827. switch (uMsg)
  1828. {
  1829. case MMIOM_OPEN:
  1830. if ( pmmio->dwFlags
  1831. & ~(MMIO_CREATE
  1832. | MMIO_READWRITE
  1833. | MMIO_WRITE
  1834. | MMIO_EXCLUSIVE
  1835. | MMIO_DENYWRITE
  1836. | MMIO_DENYREAD
  1837. | MMIO_DENYNONE
  1838. | MMIO_ALLOCBUF
  1839. )
  1840. )
  1841. return (LRESULT) MMSYSERR_INVALFLAG;
  1842. /* all the data in the buffer is valid */
  1843. if (!(pmmio->dwFlags & MMIO_CREATE))
  1844. pmmio->pchEndRead = pmmio->pchEndWrite;
  1845. return (LRESULT) 0;
  1846. case MMIOM_CLOSE:
  1847. /* nothing special to do on close */
  1848. return (LRESULT) 0;
  1849. case MMIOM_READ:
  1850. case MMIOM_WRITE:
  1851. case MMIOM_WRITEFLUSH:
  1852. case MMIOM_SEEK:
  1853. return (LRESULT) -1;
  1854. }
  1855. return (LRESULT) 0;
  1856. }