1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
5 * Copyright 1998 Andrew Taylor
6 * Copyright 1998 Ove Kåven
7 * Copyright 2000,2002 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * + correct handling of global/local IOProcs (and temporary IOProcs)
26 * + mode of mmio objects is not used (read vs write vs readwrite)
28 * + 32 A <=> W message mapping
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
49 LRESULT (*pFnMmioCallback16)(DWORD,LPMMIOINFO,UINT,LPARAM,LPARAM) /* = NULL */;
51 /**************************************************************************
52 * mmioDosIOProc [internal]
54 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
55 LPARAM lParam1, LPARAM lParam2)
57 LRESULT ret = MMSYSERR_NOERROR;
59 TRACE("(%p, %X, 0x%lx, 0x%lx);\n", lpmmioinfo, uMessage, lParam1, lParam2);
65 * lParam1 = szFileName parameter from mmioOpen
67 * Returns: zero on success, error code on error
68 * NOTE: lDiskOffset automatically set to zero
70 LPCSTR szFileName = (LPCSTR)lParam1;
72 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
73 FIXME("MMIO_GETTEMP not implemented\n");
74 return MMIOERR_CANNOTOPEN;
77 /* if filename NULL, assume open file handle in adwInfo[0] */
80 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
82 if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
83 ret = MMIOERR_CANNOTOPEN;
89 * lParam1 = wFlags parameter from mmioClose
91 * Returns: zero on success, error code on error
93 if (!(lParam1 & MMIO_FHOPEN))
94 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
99 * lParam1 = huge pointer to read buffer
100 * lParam2 = number of bytes to read
101 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
104 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
106 lpmmioinfo->lDiskOffset += ret;
111 case MMIOM_WRITEFLUSH:
112 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
115 * lParam1 = huge pointer to write buffer
116 * lParam2 = number of bytes to write
117 * Returns: number of bytes written, -1 for error (error code in
120 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
122 lpmmioinfo->lDiskOffset += ret;
127 * lParam1 = new position
128 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
129 * Returns: new file postion, -1 on error
131 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
133 lpmmioinfo->lDiskOffset = ret;
140 * Returns: zero on success, non-zero on failure
142 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
143 ret = MMIOERR_FILENOTFOUND;
147 FIXME("unexpected message %u\n", uMessage);
154 /**************************************************************************
155 * mmioMemIOProc [internal]
157 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
158 LPARAM lParam1, LPARAM lParam2)
160 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
166 * lParam1 = filename (must be NULL)
168 * Returns: zero on success, error code on error
169 * NOTE: lDiskOffset automatically set to zero
171 /* FIXME: io proc shouldn't change it */
172 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
173 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
174 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
179 * lParam1 = wFlags parameter from mmioClose
181 * Returns: zero on success, error code on error
187 * lParam1 = huge pointer to read buffer
188 * lParam2 = number of bytes to read
189 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
191 * NOTE: lDiskOffset should be updated
193 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
197 case MMIOM_WRITEFLUSH:
198 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
201 * lParam1 = huge pointer to write buffer
202 * lParam2 = number of bytes to write
203 * Returns: number of bytes written, -1 for error (error code in
205 * NOTE: lDiskOffset should be updated
207 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
212 * lParam1 = new position
213 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
214 * Returns: new file postion, -1 on error
215 * NOTE: lDiskOffset should be updated
217 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
221 FIXME("unexpected message %u\n", uMessage);
228 /* This array will be the entire list for most apps
229 * Note that temporary ioProcs will be stored with a 0 fourCC code
232 static struct IOProcList defaultProcs[] = {
233 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
234 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
237 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
239 /****************************************************************
240 * MMIO_FindProcNode [INTERNAL]
242 * Finds the ProcList node associated with a given FOURCC code.
244 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
246 struct IOProcList* pListNode;
248 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
249 if (pListNode->fourCC == fccIOProc) {
256 /****************************************************************
257 * MMIO_InstallIOProc [INTERNAL]
259 LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
260 DWORD dwFlags, enum mmioProcType type)
262 LPMMIOPROC lpProc = NULL;
263 struct IOProcList* pListNode;
264 struct IOProcList** ppListNode;
266 TRACE("(%08lx, %p, %08lX, %i)\n", fccIOProc, pIOProc, dwFlags, type);
268 if (dwFlags & MMIO_GLOBALPROC)
269 FIXME("Global procedures not implemented\n");
271 /* just handle the known procedures for now */
272 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
273 case MMIO_INSTALLPROC:
274 /* Create new entry for the IOProc list */
275 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
277 /* Fill in this node */
278 pListNode->fourCC = fccIOProc;
279 pListNode->pIOProc = pIOProc;
280 pListNode->type = type;
281 pListNode->count = 0;
283 /* Stick it on the end of the list */
284 pListNode->pNext = pIOProcListAnchor;
285 pIOProcListAnchor = pListNode;
287 /* Return this IOProc - that's how the caller knows we succeeded */
292 case MMIO_REMOVEPROC:
294 * Search for the node that we're trying to remove
295 * We search for a matching fourCC code if it's non null, or the proc
297 * note that this method won't find the first item on the list, but
298 * since the first two items on this list are ones we won't
299 * let the user delete anyway, that's okay
301 ppListNode = &pIOProcListAnchor;
302 while ((*ppListNode) &&
304 (*ppListNode)->fourCC != fccIOProc :
305 (*ppListNode)->pIOProc != pIOProc))
306 ppListNode = &((*ppListNode)->pNext);
308 if (*ppListNode) { /* found it */
309 /* FIXME: what should be done if an open mmio object uses this proc ?
310 * shall we return an error, nuke the mmio object ?
312 if ((*ppListNode)->count) {
313 ERR("Cannot remove a mmIOProc while in use\n");
316 /* remove it, but only if it isn't builtin */
317 if ((*ppListNode) >= defaultProcs &&
318 (*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
319 WARN("Tried to remove built-in mmio proc. Skipping\n");
322 struct IOProcList* ptmpNode = *ppListNode;
323 lpProc = (*ppListNode)->pIOProc;
324 *ppListNode = (*ppListNode)->pNext;
325 HeapFree(GetProcessHeap(), 0, ptmpNode);
331 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
332 lpProc = pListNode->pIOProc;
340 /****************************************************************
341 * send_message [INTERNAL]
343 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
344 DWORD wMsg, LPARAM lParam1,
345 LPARAM lParam2, enum mmioProcType type)
347 LRESULT result = MMSYSERR_ERROR;
348 LPARAM lp1 = lParam1, lp2 = lParam2;
352 result = MMSYSERR_INVALPARAM;
355 switch (ioProc->type) {
357 if (pFnMmioCallback16)
358 result = pFnMmioCallback16((DWORD)ioProc->pIOProc,
359 mmioinfo, wMsg, lp1, lp2);
363 if (ioProc->type != type) {
364 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
365 FIXME("NIY 32 A<=>W mapping\n");
367 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
370 if (ioProc->type != type) {
371 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
376 FIXME("Internal error\n");
382 /**************************************************************************
383 * MMIO_ParseExtA [internal]
385 * Parses a filename for the extension.
388 * The FOURCC code for the extension if found, else 0.
390 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
392 /* Filenames are of the form file.ext+ABC
393 FIXME: What if a '+' is part of the file name?
394 For now, we take the last '+' present */
398 /* Note that ext{Start,End} point to the . and + respectively */
401 TRACE("(%s)\n", debugstr_a(szFileName));
405 extEnd = strrchr(szFileName,'+');
407 /* Need to parse to find the extension */
411 while (extStart >= szFileName && extStart[0] != '.') {
415 if (extStart < szFileName) {
416 ERR("+ but no . in szFileName: %s\n", debugstr_a(szFileName));
420 if (extEnd - extStart - 1 > 4)
421 WARN("Extension length > 4\n");
422 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
423 TRACE("Got extension: %s\n", debugstr_a(ext));
424 /* FOURCC codes identifying file-extensions must be uppercase */
425 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
431 /**************************************************************************
432 * MMIO_Get [internal]
434 * Retrieves the mmio object from current process
436 LPWINE_MMIO MMIO_Get(HMMIO h)
438 LPWINE_MMIO wm = NULL;
440 EnterCriticalSection(&WINMM_IData->cs);
441 for (wm = WINMM_IData->lpMMIO; wm; wm = wm->lpNext) {
442 if (wm->info.hmmio == h)
445 LeaveCriticalSection(&WINMM_IData->cs);
449 /**************************************************************************
450 * MMIO_Create [internal]
452 * Creates an internal representation for a mmio instance
454 static LPWINE_MMIO MMIO_Create(void)
456 static WORD MMIO_counter = 0;
459 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
461 EnterCriticalSection(&WINMM_IData->cs);
462 /* lookup next unallocated WORD handle, with a non NULL value */
463 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
464 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
465 wm->lpNext = WINMM_IData->lpMMIO;
466 WINMM_IData->lpMMIO = wm;
467 LeaveCriticalSection(&WINMM_IData->cs);
472 /**************************************************************************
473 * MMIO_Destroy [internal]
475 * Destroys an internal representation for a mmio instance
477 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
481 EnterCriticalSection(&WINMM_IData->cs);
482 /* search for the matching one... */
483 m = &(WINMM_IData->lpMMIO);
484 while (*m && *m != wm) m = &(*m)->lpNext;
488 HeapFree(GetProcessHeap(), 0, wm);
491 LeaveCriticalSection(&WINMM_IData->cs);
492 return wm ? FALSE : TRUE;
495 /****************************************************************
496 * MMIO_Flush [INTERNAL]
498 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
500 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
501 /* not quite sure what to do here, but I'll guess */
502 if (wm->info.dwFlags & MMIO_DIRTY) {
503 /* FIXME: error handling */
504 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
505 wm->info.lBufOffset, SEEK_SET, MMIO_PROC_32A);
506 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
507 (LPARAM)wm->info.pchBuffer,
508 wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
510 if (uFlags & MMIO_EMPTYBUF)
511 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
513 wm->info.dwFlags &= ~MMIO_DIRTY;
515 return MMSYSERR_NOERROR;
518 /***************************************************************************
519 * MMIO_GrabNextBuffer [INTERNAL]
521 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
523 LONG size = wm->info.cchBuffer;
525 TRACE("bo=%lx do=%lx of=%lx\n",
526 wm->info.lBufOffset, wm->info.lDiskOffset,
527 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
529 wm->info.lBufOffset = wm->info.lDiskOffset;
530 wm->info.pchNext = wm->info.pchBuffer;
531 wm->info.pchEndRead = wm->info.pchBuffer;
532 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
534 wm->bBufferLoaded = TRUE;
536 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
537 (LPARAM)wm->info.pchBuffer, size, MMIO_PROC_32A);
539 wm->info.pchEndRead += size;
541 wm->bBufferLoaded = FALSE;
547 /***************************************************************************
548 * MMIO_SetBuffer [INTERNAL]
550 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
553 TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
555 if (uFlags) return MMSYSERR_INVALPARAM;
556 if (cchBuffer > 0xFFFF)
557 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
559 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
560 return MMIOERR_CANNOTWRITE;
562 /* free previous buffer if allocated */
563 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
564 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
565 wm->info.pchBuffer = NULL;
566 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
570 wm->info.pchBuffer = pchBuffer;
571 } else if (cchBuffer) {
572 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
573 return MMIOERR_OUTOFMEMORY;
574 wm->info.dwFlags |= MMIO_ALLOCBUF;
576 wm->info.pchBuffer = NULL;
579 wm->info.cchBuffer = cchBuffer;
580 wm->info.pchNext = wm->info.pchBuffer;
581 wm->info.pchEndRead = wm->info.pchBuffer;
582 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
583 wm->info.lBufOffset = 0;
584 wm->bBufferLoaded = FALSE;
586 return MMSYSERR_NOERROR;
589 /**************************************************************************
590 * MMIO_Open [internal]
592 HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags,
593 enum mmioProcType type)
598 TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
601 refmminfo = &mmioinfo;
603 mmioinfo.fccIOProc = 0;
604 mmioinfo.pIOProc = NULL;
605 mmioinfo.pchBuffer = NULL;
606 mmioinfo.cchBuffer = 0;
607 type = MMIO_PROC_32A;
610 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
611 char buffer[MAX_PATH];
613 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
615 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
617 strcpy(szFileName, buffer);
621 if ((wm = MMIO_Create()) == NULL)
624 /* If both params are NULL, then parse the file name if available */
625 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
626 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
627 /* Handle any unhandled/error case. Assume DOS file */
628 if (wm->info.fccIOProc == 0)
629 wm->info.fccIOProc = FOURCC_DOS;
630 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
631 wm->bTmpIOProc = FALSE;
633 /* if just the four character code is present, look up IO proc */
634 else if (refmminfo->pIOProc == NULL) {
635 wm->info.fccIOProc = refmminfo->fccIOProc;
636 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
637 wm->bTmpIOProc = FALSE;
639 /* if IO proc specified, use it and specified four character code */
641 wm->info.fccIOProc = refmminfo->fccIOProc;
642 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
643 MMIO_INSTALLPROC, type);
644 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
645 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
646 wm->bTmpIOProc = TRUE;
649 wm->bBufferLoaded = FALSE;
652 if (dwOpenFlags & MMIO_ALLOCBUF) {
653 if ((refmminfo->wErrorRet = MMIO_SetBuffer(wm, NULL, MMIO_DEFAULTBUFFER, 0)))
655 } else if (wm->info.fccIOProc == FOURCC_MEM) {
656 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
657 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
659 wm->bBufferLoaded = TRUE;
660 } /* else => unbuffered, wm->info.pchBuffer == NULL */
662 /* see mmioDosIOProc for that one */
663 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
664 wm->info.dwFlags = dwOpenFlags;
666 /* call IO proc to actually open file */
667 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
668 (LPARAM)szFileName, 0, MMIO_PROC_32A);
670 /* grab file size, when possible */
671 wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
673 if (refmminfo->wErrorRet == 0)
674 return wm->info.hmmio;
676 if (wm->ioProc) wm->ioProc->count--;
682 /**************************************************************************
683 * mmioOpenW [WINMM.@]
685 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
693 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
694 szFn = HeapAlloc( GetProcessHeap(), 0, len );
695 if (!szFn) return (HMMIO)NULL;
696 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
699 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
701 HeapFree(GetProcessHeap(), 0, szFn);
705 /**************************************************************************
706 * mmioOpenA [WINMM.@]
708 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
711 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
714 /**************************************************************************
715 * mmioClose [WINMM.@]
717 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
722 TRACE("(%p, %04X);\n", hmmio, uFlags);
724 if ((wm = MMIO_Get(hmmio)) == NULL)
725 return MMSYSERR_INVALHANDLE;
727 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
730 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE,
731 uFlags, 0, MMIO_PROC_32A);
733 MMIO_SetBuffer(wm, NULL, 0, 0);
738 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
739 MMIO_REMOVEPROC, wm->ioProc->type);
746 /**************************************************************************
749 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
754 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
756 if ((wm = MMIO_Get(hmmio)) == NULL)
759 /* unbuffered case first */
760 if (!wm->info.pchBuffer)
761 return send_message(wm->ioProc, &wm->info, MMIOM_READ,
762 (LPARAM)pch, cch, MMIO_PROC_32A);
764 /* first try from current buffer */
765 if (wm->info.pchNext != wm->info.pchEndRead) {
766 count = wm->info.pchEndRead - wm->info.pchNext;
767 if (count > cch || count < 0) count = cch;
768 memcpy(pch, wm->info.pchNext, count);
769 wm->info.pchNext += count;
775 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
776 assert(wm->info.cchBuffer);
781 size = MMIO_GrabNextBuffer(wm, TRUE);
782 if (size <= 0) break;
783 if (size > cch) size = cch;
784 memcpy(pch, wm->info.pchBuffer, size);
785 wm->info.pchNext += size;
792 TRACE("count=%ld\n", count);
796 /**************************************************************************
797 * mmioWrite [WINMM.@]
799 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
804 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
806 if ((wm = MMIO_Get(hmmio)) == NULL)
809 if (wm->info.cchBuffer) {
814 if (wm->info.pchNext != wm->info.pchEndWrite) {
815 count = wm->info.pchEndWrite - wm->info.pchNext;
816 if (count > cch || count < 0) count = cch;
817 memcpy(wm->info.pchNext, pch, count);
818 wm->info.pchNext += count;
822 wm->info.dwFlags |= MMIO_DIRTY;
824 if (wm->info.fccIOProc == FOURCC_MEM) {
825 if (wm->info.adwInfo[0]) {
826 /* from where would we get the memory handle? */
827 FIXME("memory file expansion not implemented!\n");
833 if (wm->info.pchNext == wm->info.pchEndWrite)
835 MMIO_Flush(wm, MMIO_EMPTYBUF);
836 MMIO_GrabNextBuffer(wm, FALSE);
842 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
843 (LPARAM)pch, cch, MMIO_PROC_32A);
844 wm->info.lBufOffset = wm->info.lDiskOffset;
847 TRACE("bytes written=%ld\n", count);
851 /**************************************************************************
854 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
859 TRACE("(%p, %08lX, %d);\n", hmmio, lOffset, iOrigin);
861 if ((wm = MMIO_Get(hmmio)) == NULL)
862 return MMSYSERR_INVALHANDLE;
864 /* not buffered, direct seek on file */
865 if (!wm->info.pchBuffer)
866 return send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
867 lOffset, iOrigin, MMIO_PROC_32A);
874 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
877 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
883 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
884 /* should check that write mode exists */
885 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
887 wm->info.lBufOffset = offset;
888 wm->info.pchEndRead = wm->info.pchBuffer;
889 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
890 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
891 wm->info.lDiskOffset = wm->dwFileSize;
893 } else if ((wm->info.cchBuffer > 0) &&
894 ((offset < wm->info.lBufOffset) ||
895 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
896 !wm->bBufferLoaded)) {
897 /* stay in same buffer ? */
898 /* some memory mapped buffers are defined with -1 as a size */
900 /* condition to change buffer */
901 if ((wm->info.fccIOProc == FOURCC_MEM) ||
902 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
903 /* this also sets the wm->info.lDiskOffset field */
904 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
905 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
906 SEEK_SET, MMIO_PROC_32A) == -1)
908 MMIO_GrabNextBuffer(wm, TRUE);
911 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
913 TRACE("=> %ld\n", offset);
917 /**************************************************************************
918 * mmioGetInfo [WINMM.@]
920 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
924 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
926 if ((wm = MMIO_Get(hmmio)) == NULL)
927 return MMSYSERR_INVALHANDLE;
929 memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
930 /* don't expose 16 bit ioproc:s */
931 if (wm->ioProc->type != MMIO_PROC_16)
932 lpmmioinfo->pIOProc = wm->ioProc->pIOProc;
934 return MMSYSERR_NOERROR;
937 /**************************************************************************
938 * mmioSetInfo [WINMM.@]
940 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
944 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
946 if ((wm = MMIO_Get(hmmio)) == NULL)
947 return MMSYSERR_INVALHANDLE;
949 /* check pointers coherence */
950 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
951 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
952 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
953 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
954 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
955 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
956 return MMSYSERR_INVALPARAM;
958 wm->info.pchNext = lpmmioinfo->pchNext;
959 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
961 return MMSYSERR_NOERROR;
964 /**************************************************************************
965 * mmioSetBuffer [WINMM.@]
967 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
971 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
972 hmmio, pchBuffer, cchBuffer, uFlags);
974 if ((wm = MMIO_Get(hmmio)) == NULL)
975 return MMSYSERR_INVALHANDLE;
977 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
980 /**************************************************************************
981 * mmioFlush [WINMM.@]
983 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
987 TRACE("(%p, %04X)\n", hmmio, uFlags);
989 if ((wm = MMIO_Get(hmmio)) == NULL)
990 return MMSYSERR_INVALHANDLE;
992 return MMIO_Flush(wm, uFlags);
995 /**************************************************************************
996 * mmioAdvance [WINMM.@]
998 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1002 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1004 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1005 * here. be sure if you change something here to check mmioAdvance16 as well
1007 if ((wm = MMIO_Get(hmmio)) == NULL)
1008 return MMSYSERR_INVALHANDLE;
1010 if (!wm->info.cchBuffer)
1011 return MMIOERR_UNBUFFERED;
1013 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1014 return MMSYSERR_INVALPARAM;
1016 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1018 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
1019 lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1020 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
1021 (LPARAM)lpmmioinfo->pchBuffer,
1022 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1023 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1025 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1026 return MMIOERR_CANNOTWRITE;
1029 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1030 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1032 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1035 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1036 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1037 (wm->info.pchEndRead - wm->info.pchBuffer);
1038 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1039 (wm->info.pchEndWrite - wm->info.pchBuffer);
1040 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1041 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1043 return MMSYSERR_NOERROR;
1046 /**************************************************************************
1047 * mmioStringToFOURCCA [WINMM.@]
1049 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1054 for (i = 0; i < 4 && sz[i]; i++) {
1055 if (uFlags & MMIO_TOUPPER) {
1056 cc[i] = toupper(sz[i]);
1062 /* Pad with spaces */
1063 while (i < 4) cc[i++] = ' ';
1065 TRACE("Got '%.4s'\n",cc);
1066 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1069 /**************************************************************************
1070 * mmioStringToFOURCCW [WINMM.@]
1072 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1076 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1077 return mmioStringToFOURCCA(szA,uFlags);
1080 /**************************************************************************
1081 * mmioInstallIOProcA [WINMM.@]
1083 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1084 LPMMIOPROC pIOProc, DWORD dwFlags)
1086 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1089 /**************************************************************************
1090 * mmioInstallIOProcW [WINMM.@]
1092 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1093 LPMMIOPROC pIOProc, DWORD dwFlags)
1095 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1098 /******************************************************************
1103 LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1104 LPARAM lParam2, enum mmioProcType type)
1108 TRACE("(%p, %u, %ld, %ld, %d)\n", hmmio, uMessage, lParam1, lParam2, type);
1110 if (uMessage < MMIOM_USER)
1111 return MMSYSERR_INVALPARAM;
1113 if ((wm = MMIO_Get(hmmio)) == NULL)
1114 return MMSYSERR_INVALHANDLE;
1116 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, type);
1119 /**************************************************************************
1120 * mmioSendMessage [WINMM.@]
1122 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1123 LPARAM lParam1, LPARAM lParam2)
1125 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1128 /**************************************************************************
1129 * mmioDescend [WINMM.@]
1131 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1132 const MMCKINFO* lpckParent, UINT uFlags)
1139 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1142 return MMSYSERR_INVALPARAM;
1144 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1145 TRACE("dwOldPos=%ld\n", dwOldPos);
1147 if (lpckParent != NULL) {
1148 TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1149 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1150 if (dwOldPos < lpckParent->dwDataOffset ||
1151 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1152 WARN("outside parent chunk\n");
1153 return MMIOERR_CHUNKNOTFOUND;
1157 /* The SDK docu says 'ckid' is used for all cases. Real World
1158 * examples disagree -Marcus,990216.
1162 /* find_chunk looks for 'ckid' */
1163 if (uFlags & MMIO_FINDCHUNK)
1164 srchCkId = lpck->ckid;
1165 /* find_riff and find_list look for 'fccType' */
1166 if (uFlags & MMIO_FINDLIST) {
1167 srchCkId = FOURCC_LIST;
1168 srchType = lpck->fccType;
1170 if (uFlags & MMIO_FINDRIFF) {
1171 srchCkId = FOURCC_RIFF;
1172 srchType = lpck->fccType;
1175 if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1176 TRACE("searching for %.4s.%.4s\n",
1178 srchType?(LPSTR)&srchType:"any ");
1183 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1184 if (ix < 2*sizeof(DWORD)) {
1185 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1186 WARN("return ChunkNotFound\n");
1187 return MMIOERR_CHUNKNOTFOUND;
1189 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1190 if (ix < lpck->dwDataOffset - dwOldPos) {
1191 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1192 WARN("return ChunkNotFound\n");
1193 return MMIOERR_CHUNKNOTFOUND;
1195 TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1197 srchType?(LPSTR)&lpck->fccType:"<na>",
1199 if ((srchCkId == lpck->ckid) &&
1200 (!srchType || (srchType == lpck->fccType))
1204 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1205 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1208 /* FIXME: unverified, does it do this? */
1209 /* NB: This part is used by WAVE_mciOpen, among others */
1210 if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1211 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1212 WARN("return ChunkNotFound 2nd\n");
1213 return MMIOERR_CHUNKNOTFOUND;
1215 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1218 /* If we were looking for RIFF/LIST chunks, the final file position
1219 * is after the chunkid. If we were just looking for the chunk
1220 * it is after the cksize. So add 4 in RIFF/LIST case.
1222 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1223 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1225 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1226 TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
1227 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1228 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1229 return MMSYSERR_NOERROR;
1232 /**************************************************************************
1233 * mmioAscend [WINMM.@]
1235 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1237 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1239 if (lpck->dwFlags & MMIO_DIRTY) {
1240 DWORD dwOldPos, dwNewSize;
1242 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1243 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1244 TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1245 dwNewSize = dwOldPos - lpck->dwDataOffset;
1246 if (dwNewSize != lpck->cksize) {
1247 TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1248 lpck->cksize = dwNewSize;
1250 /* pad odd size with 0 */
1251 if (dwNewSize & 1) {
1253 mmioWrite(hmmio, &ch, 1);
1255 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1256 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1261 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1263 return MMSYSERR_NOERROR;
1266 /**************************************************************************
1267 * mmioCreateChunk [WINMM.@]
1269 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1275 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1277 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1278 TRACE("dwOldPos=%ld\n", dwOldPos);
1280 if (uFlags == MMIO_CREATELIST)
1281 lpck->ckid = FOURCC_LIST;
1282 else if (uFlags == MMIO_CREATERIFF)
1283 lpck->ckid = FOURCC_RIFF;
1285 TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1287 size = 2 * sizeof(DWORD);
1288 lpck->dwDataOffset = dwOldPos + size;
1290 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1291 size += sizeof(DWORD);
1292 lpck->dwFlags = MMIO_DIRTY;
1294 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1295 TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
1297 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1298 WARN("return CannotWrite\n");
1299 return MMIOERR_CANNOTWRITE;
1302 return MMSYSERR_NOERROR;
1305 /**************************************************************************
1306 * mmioRenameA [WINMM.@]
1308 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1309 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1311 struct IOProcList* ioProc = NULL;
1312 struct IOProcList tmp;
1315 TRACE("('%s', '%s', %p, %08lX);\n",
1316 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1318 /* If both params are NULL, then parse the file name */
1319 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1321 fcc = MMIO_ParseExtA(szFileName);
1322 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1325 /* Handle any unhandled/error case from above. Assume DOS file */
1326 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1327 ioProc = MMIO_FindProcNode(FOURCC_DOS);
1328 /* if just the four character code is present, look up IO proc */
1329 else if (lpmmioinfo->pIOProc == NULL)
1330 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1331 else /* use relevant ioProc */
1334 tmp.fourCC = lpmmioinfo->fccIOProc;
1335 tmp.pIOProc = lpmmioinfo->pIOProc;
1336 tmp.type = MMIO_PROC_32A;
1340 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1341 * or make a copy of it because it's const ???
1343 return send_message(ioProc, (LPMMIOINFO)lpmmioinfo, MMIOM_RENAME,
1344 (LPARAM)szFileName, (LPARAM)szNewFileName, MMIO_PROC_32A);
1347 /**************************************************************************
1348 * mmioRenameW [WINMM.@]
1350 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1351 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1360 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1361 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1362 if (!szFn) return MMSYSERR_NOMEM;
1363 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1367 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1368 sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1369 if (!sznFn) return MMSYSERR_NOMEM;
1370 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1373 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1375 HeapFree(GetProcessHeap(),0,szFn);
1376 HeapFree(GetProcessHeap(),0,sznFn);