4 * Copyright 1998 Andrew Taylor
5 * Copyright 1998 Ove Kåven
6 * Copyright 2000,2002 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * + correct handling of global/local IOProcs (and temporary IOProcs)
25 * + mode of mmio objects is not used (read vs write vs readwrite)
27 * + 32 A <=> W message mapping
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
48 LRESULT (*pFnMmioCallback16)(DWORD,LPMMIOINFO,UINT,LPARAM,LPARAM) /* = NULL */;
50 static WINE_MMIO *MMIOList;
52 /**************************************************************************
53 * mmioDosIOProc [internal]
55 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
56 LPARAM lParam1, LPARAM lParam2)
58 LRESULT ret = MMSYSERR_NOERROR;
60 TRACE("(%p, %X, 0x%lx, 0x%lx);\n", lpmmioinfo, uMessage, lParam1, lParam2);
66 * lParam1 = szFileName parameter from mmioOpen
68 * Returns: zero on success, error code on error
69 * NOTE: lDiskOffset automatically set to zero
71 LPCSTR szFileName = (LPCSTR)lParam1;
73 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
74 FIXME("MMIO_GETTEMP not implemented\n");
75 return MMIOERR_CANNOTOPEN;
78 /* if filename NULL, assume open file handle in adwInfo[0] */
81 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
83 if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
84 ret = MMIOERR_CANNOTOPEN;
90 * lParam1 = wFlags parameter from mmioClose
92 * Returns: zero on success, error code on error
94 if (!(lParam1 & MMIO_FHOPEN))
95 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
100 * lParam1 = huge pointer to read buffer
101 * lParam2 = number of bytes to read
102 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
105 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
107 lpmmioinfo->lDiskOffset += ret;
112 case MMIOM_WRITEFLUSH:
113 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
116 * lParam1 = huge pointer to write buffer
117 * lParam2 = number of bytes to write
118 * Returns: number of bytes written, -1 for error (error code in
121 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
123 lpmmioinfo->lDiskOffset += ret;
128 * lParam1 = new position
129 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
130 * Returns: new file postion, -1 on error
132 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
134 lpmmioinfo->lDiskOffset = ret;
141 * Returns: zero on success, non-zero on failure
143 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
144 ret = MMIOERR_FILENOTFOUND;
148 FIXME("unexpected message %u\n", uMessage);
155 /**************************************************************************
156 * mmioMemIOProc [internal]
158 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
159 LPARAM lParam1, LPARAM lParam2)
161 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
167 * lParam1 = filename (must be NULL)
169 * Returns: zero on success, error code on error
170 * NOTE: lDiskOffset automatically set to zero
172 /* FIXME: io proc shouldn't change it */
173 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
174 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
175 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
180 * lParam1 = wFlags parameter from mmioClose
182 * Returns: zero on success, error code on error
188 * lParam1 = huge pointer to read buffer
189 * lParam2 = number of bytes to read
190 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
192 * NOTE: lDiskOffset should be updated
194 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
198 case MMIOM_WRITEFLUSH:
199 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
202 * lParam1 = huge pointer to write buffer
203 * lParam2 = number of bytes to write
204 * Returns: number of bytes written, -1 for error (error code in
206 * NOTE: lDiskOffset should be updated
208 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
213 * lParam1 = new position
214 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
215 * Returns: new file postion, -1 on error
216 * NOTE: lDiskOffset should be updated
218 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
222 FIXME("unexpected message %u\n", uMessage);
227 /* This array will be the entire list for most apps
228 * Note that temporary ioProcs will be stored with a 0 fourCC code
231 static struct IOProcList defaultProcs[] = {
232 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
233 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
236 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
238 /****************************************************************
239 * MMIO_FindProcNode [INTERNAL]
241 * Finds the ProcList node associated with a given FOURCC code.
243 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
245 struct IOProcList* pListNode;
247 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
248 if (pListNode->fourCC == fccIOProc) {
255 /****************************************************************
256 * MMIO_InstallIOProc [INTERNAL]
258 LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
259 DWORD dwFlags, enum mmioProcType type)
261 LPMMIOPROC lpProc = NULL;
262 struct IOProcList* pListNode;
263 struct IOProcList** ppListNode;
265 TRACE("(%08x, %p, %08X, %i)\n", fccIOProc, pIOProc, dwFlags, type);
267 if (dwFlags & MMIO_GLOBALPROC)
268 FIXME("Global procedures not implemented\n");
270 /* just handle the known procedures for now */
271 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
272 case MMIO_INSTALLPROC:
273 /* Create new entry for the IOProc list */
274 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
276 /* Fill in this node */
277 pListNode->fourCC = fccIOProc;
278 pListNode->pIOProc = pIOProc;
279 pListNode->type = type;
280 pListNode->count = 0;
282 /* Stick it on the end of the list */
283 pListNode->pNext = pIOProcListAnchor;
284 pIOProcListAnchor = pListNode;
286 /* Return this IOProc - that's how the caller knows we succeeded */
291 case MMIO_REMOVEPROC:
293 * Search for the node that we're trying to remove
294 * We search for a matching fourCC code if it's non null, or the proc
296 * note that this method won't find the first item on the list, but
297 * since the first two items on this list are ones we won't
298 * let the user delete anyway, that's okay
300 ppListNode = &pIOProcListAnchor;
301 while ((*ppListNode) &&
303 (*ppListNode)->fourCC != fccIOProc :
304 (*ppListNode)->pIOProc != pIOProc))
305 ppListNode = &((*ppListNode)->pNext);
307 if (*ppListNode) { /* found it */
308 /* FIXME: what should be done if an open mmio object uses this proc ?
309 * shall we return an error, nuke the mmio object ?
311 if ((*ppListNode)->count) {
312 ERR("Cannot remove a mmIOProc while in use\n");
315 /* remove it, but only if it isn't builtin */
316 if ((*ppListNode) >= defaultProcs &&
317 (*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
318 WARN("Tried to remove built-in mmio proc. Skipping\n");
321 struct IOProcList* ptmpNode = *ppListNode;
322 lpProc = (*ppListNode)->pIOProc;
323 *ppListNode = (*ppListNode)->pNext;
324 HeapFree(GetProcessHeap(), 0, ptmpNode);
330 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
331 lpProc = pListNode->pIOProc;
339 /****************************************************************
340 * send_message [INTERNAL]
342 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
343 DWORD wMsg, LPARAM lParam1,
344 LPARAM lParam2, enum mmioProcType type)
346 LRESULT result = MMSYSERR_ERROR;
347 LPARAM lp1 = lParam1, lp2 = lParam2;
351 result = MMSYSERR_INVALPARAM;
354 switch (ioProc->type) {
356 if (pFnMmioCallback16)
357 result = pFnMmioCallback16((DWORD)ioProc->pIOProc,
358 mmioinfo, wMsg, lp1, lp2);
362 if (ioProc->type != type) {
363 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
364 FIXME("NIY 32 A<=>W mapping\n");
366 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
369 if (ioProc->type != type) {
370 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
375 FIXME("Internal error\n");
381 /**************************************************************************
382 * MMIO_ParseExtA [internal]
384 * Parses a filename for the extension.
387 * The FOURCC code for the extension if found, else 0.
389 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
391 /* Filenames are of the form file.ext{+ABC}
392 For now, we take the last '+' if present */
396 /* Note that ext{Start,End} point to the . and + respectively */
400 TRACE("(%s)\n", debugstr_a(szFileName));
405 /* Find the last '.' */
406 extStart = strrchr(szFileName,'.');
409 ERR("No . in szFileName: %s\n", debugstr_a(szFileName));
413 /* Find the '+' afterwards */
414 extEnd = strchr(extStart,'+');
417 if (extEnd - extStart - 1 > 4)
418 WARN("Extension length > 4\n");
419 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
422 /* No + so just an extension */
423 if (strlen(extStart) > 4) {
424 WARN("Extension length > 4\n");
426 lstrcpynA(ext, extStart + 1, 5);
428 TRACE("Got extension: %s\n", debugstr_a(ext));
430 /* FOURCC codes identifying file-extensions must be uppercase */
431 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
436 /**************************************************************************
437 * MMIO_Get [internal]
439 * Retrieves the mmio object from current process
441 LPWINE_MMIO MMIO_Get(HMMIO h)
443 LPWINE_MMIO wm = NULL;
445 EnterCriticalSection(&WINMM_cs);
446 for (wm = MMIOList; wm; wm = wm->lpNext) {
447 if (wm->info.hmmio == h)
450 LeaveCriticalSection(&WINMM_cs);
454 /**************************************************************************
455 * MMIO_Create [internal]
457 * Creates an internal representation for a mmio instance
459 static LPWINE_MMIO MMIO_Create(void)
461 static WORD MMIO_counter = 0;
464 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
466 EnterCriticalSection(&WINMM_cs);
467 /* lookup next unallocated WORD handle, with a non NULL value */
468 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
469 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
470 wm->lpNext = MMIOList;
472 LeaveCriticalSection(&WINMM_cs);
477 /**************************************************************************
478 * MMIO_Destroy [internal]
480 * Destroys an internal representation for a mmio instance
482 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
486 EnterCriticalSection(&WINMM_cs);
487 /* search for the matching one... */
489 while (*m && *m != wm) m = &(*m)->lpNext;
493 HeapFree(GetProcessHeap(), 0, wm);
496 LeaveCriticalSection(&WINMM_cs);
497 return wm ? FALSE : TRUE;
500 /****************************************************************
501 * MMIO_Flush [INTERNAL]
503 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
505 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
506 /* not quite sure what to do here, but I'll guess */
507 if (wm->info.dwFlags & MMIO_DIRTY) {
508 /* FIXME: error handling */
509 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
510 wm->info.lBufOffset, SEEK_SET, MMIO_PROC_32A);
511 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
512 (LPARAM)wm->info.pchBuffer,
513 wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
515 if (uFlags & MMIO_EMPTYBUF)
516 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
518 wm->info.dwFlags &= ~MMIO_DIRTY;
520 return MMSYSERR_NOERROR;
523 /***************************************************************************
524 * MMIO_GrabNextBuffer [INTERNAL]
526 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
528 LONG size = wm->info.cchBuffer;
530 TRACE("bo=%x do=%x of=%lx\n",
531 wm->info.lBufOffset, wm->info.lDiskOffset,
532 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
534 wm->info.lBufOffset = wm->info.lDiskOffset;
535 wm->info.pchNext = wm->info.pchBuffer;
536 wm->info.pchEndRead = wm->info.pchBuffer;
537 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
539 wm->bBufferLoaded = TRUE;
541 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
542 (LPARAM)wm->info.pchBuffer, size, MMIO_PROC_32A);
544 wm->info.pchEndRead += size;
546 wm->bBufferLoaded = FALSE;
552 /***************************************************************************
553 * MMIO_SetBuffer [INTERNAL]
555 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
558 TRACE("(%p %p %d %u)\n", wm, pchBuffer, cchBuffer, uFlags);
560 if (cchBuffer > 0xFFFF)
561 WARN("Untested handling of huge mmio buffers (%d >= 64k)\n", cchBuffer);
563 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
564 return MMIOERR_CANNOTWRITE;
566 /* free previous buffer if allocated */
567 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
568 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
569 wm->info.pchBuffer = NULL;
570 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
574 wm->info.pchBuffer = pchBuffer;
575 } else if (cchBuffer) {
576 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
577 return MMIOERR_OUTOFMEMORY;
578 wm->info.dwFlags |= MMIO_ALLOCBUF;
580 wm->info.pchBuffer = NULL;
583 wm->info.cchBuffer = cchBuffer;
584 wm->info.pchNext = wm->info.pchBuffer;
585 wm->info.pchEndRead = wm->info.pchBuffer;
586 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
587 wm->info.lBufOffset = wm->info.lDiskOffset;
588 wm->bBufferLoaded = FALSE;
590 return MMSYSERR_NOERROR;
593 /**************************************************************************
594 * MMIO_Open [internal]
596 HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags,
597 enum mmioProcType type)
602 TRACE("('%s', %p, %08X, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
605 refmminfo = &mmioinfo;
607 mmioinfo.fccIOProc = 0;
608 mmioinfo.pIOProc = NULL;
609 mmioinfo.pchBuffer = NULL;
610 mmioinfo.cchBuffer = 0;
611 type = MMIO_PROC_32A;
614 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
615 char buffer[MAX_PATH];
617 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
619 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
621 strcpy(szFileName, buffer);
625 if ((wm = MMIO_Create()) == NULL)
628 /* If both params are NULL, then parse the file name if available */
629 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
630 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
631 /* Handle any unhandled/error case. Assume DOS file */
632 if (wm->info.fccIOProc == 0)
633 wm->info.fccIOProc = FOURCC_DOS;
634 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
635 /* If not found, retry with FOURCC_DOS */
636 wm->info.fccIOProc = FOURCC_DOS;
637 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc)))
640 wm->bTmpIOProc = FALSE;
642 /* if just the four character code is present, look up IO proc */
643 else if (refmminfo->pIOProc == NULL) {
644 wm->info.fccIOProc = refmminfo->fccIOProc;
645 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
646 wm->bTmpIOProc = FALSE;
648 /* if IO proc specified, use it and specified four character code */
650 wm->info.fccIOProc = refmminfo->fccIOProc;
651 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
652 MMIO_INSTALLPROC, type);
653 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
654 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
655 wm->bTmpIOProc = TRUE;
659 wm->info.dwFlags = dwOpenFlags;
661 if (dwOpenFlags & MMIO_ALLOCBUF) {
662 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
663 refmminfo->cchBuffer ? refmminfo->cchBuffer : MMIO_DEFAULTBUFFER, 0);
664 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
667 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
668 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
672 if (wm->info.fccIOProc == FOURCC_MEM && !(wm->info.dwFlags & MMIO_ALLOCBUF))
673 wm->bBufferLoaded = TRUE;
675 /* see mmioDosIOProc for that one */
676 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
678 /* call IO proc to actually open file */
679 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
680 (LPARAM)szFileName, 0, MMIO_PROC_32A);
682 /* grab file size, when possible */
683 wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
685 if (refmminfo->wErrorRet == 0)
686 return wm->info.hmmio;
688 if (wm->ioProc) wm->ioProc->count--;
694 /**************************************************************************
695 * mmioOpenW [WINMM.@]
697 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
705 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
706 szFn = HeapAlloc( GetProcessHeap(), 0, len );
707 if (!szFn) return NULL;
708 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
711 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
713 HeapFree(GetProcessHeap(), 0, szFn);
717 /**************************************************************************
718 * mmioOpenA [WINMM.@]
720 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
723 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
726 /**************************************************************************
727 * mmioClose [WINMM.@]
729 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
734 TRACE("(%p, %04X);\n", hmmio, uFlags);
736 if ((wm = MMIO_Get(hmmio)) == NULL)
737 return MMSYSERR_INVALHANDLE;
739 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
742 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE,
743 uFlags, 0, MMIO_PROC_32A);
745 MMIO_SetBuffer(wm, NULL, 0, 0);
750 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
751 MMIO_REMOVEPROC, wm->ioProc->type);
758 /**************************************************************************
761 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
766 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
768 if ((wm = MMIO_Get(hmmio)) == NULL)
771 /* unbuffered case first */
772 if (!wm->info.pchBuffer)
773 return send_message(wm->ioProc, &wm->info, MMIOM_READ,
774 (LPARAM)pch, cch, MMIO_PROC_32A);
776 /* first try from current buffer */
777 if (wm->info.pchNext != wm->info.pchEndRead) {
778 count = wm->info.pchEndRead - wm->info.pchNext;
779 if (count > cch || count < 0) count = cch;
780 memcpy(pch, wm->info.pchNext, count);
781 wm->info.pchNext += count;
787 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
788 assert(wm->info.cchBuffer);
793 size = MMIO_GrabNextBuffer(wm, TRUE);
794 if (size <= 0) break;
795 if (size > cch) size = cch;
796 memcpy(pch, wm->info.pchBuffer, size);
797 wm->info.pchNext += size;
804 TRACE("count=%d\n", count);
808 /**************************************************************************
809 * mmioWrite [WINMM.@]
811 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
816 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
818 if ((wm = MMIO_Get(hmmio)) == NULL)
821 if (wm->info.cchBuffer) {
826 if (wm->info.pchNext != wm->info.pchEndWrite) {
827 count = wm->info.pchEndWrite - wm->info.pchNext;
828 if (count > cch || count < 0) count = cch;
829 memcpy(wm->info.pchNext, pch, count);
830 wm->info.pchNext += count;
834 wm->info.dwFlags |= MMIO_DIRTY;
836 if (wm->info.fccIOProc == FOURCC_MEM) {
837 if (wm->info.adwInfo[0]) {
838 /* from where would we get the memory handle? */
839 FIXME("memory file expansion not implemented!\n");
845 if (wm->info.pchNext == wm->info.pchEndWrite)
847 MMIO_Flush(wm, MMIO_EMPTYBUF);
848 MMIO_GrabNextBuffer(wm, FALSE);
854 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
855 (LPARAM)pch, cch, MMIO_PROC_32A);
856 wm->info.lBufOffset = wm->info.lDiskOffset;
859 TRACE("bytes written=%d\n", count);
863 /**************************************************************************
866 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
871 TRACE("(%p, %08X, %d);\n", hmmio, lOffset, iOrigin);
873 if ((wm = MMIO_Get(hmmio)) == NULL)
874 return MMSYSERR_INVALHANDLE;
876 /* not buffered, direct seek on file */
877 if (!wm->info.pchBuffer)
878 return send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
879 lOffset, iOrigin, MMIO_PROC_32A);
886 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
889 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
895 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
896 /* should check that write mode exists */
897 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
899 wm->info.lBufOffset = offset;
900 wm->info.pchEndRead = wm->info.pchBuffer;
901 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
902 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
903 wm->info.lDiskOffset = wm->dwFileSize;
905 } else if ((wm->info.cchBuffer > 0) &&
906 ((offset < wm->info.lBufOffset) ||
907 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
908 !wm->bBufferLoaded)) {
909 /* stay in same buffer ? */
910 /* some memory mapped buffers are defined with -1 as a size */
912 /* condition to change buffer */
913 if ((wm->info.fccIOProc == FOURCC_MEM) ||
914 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
915 /* this also sets the wm->info.lDiskOffset field */
916 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
917 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
918 SEEK_SET, MMIO_PROC_32A) == -1)
920 MMIO_GrabNextBuffer(wm, TRUE);
923 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
925 TRACE("=> %d\n", offset);
929 /**************************************************************************
930 * mmioGetInfo [WINMM.@]
932 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
936 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
938 if ((wm = MMIO_Get(hmmio)) == NULL)
939 return MMSYSERR_INVALHANDLE;
941 *lpmmioinfo = wm->info;
942 /* don't expose 16 bit ioproc:s */
943 if (wm->ioProc->type != MMIO_PROC_16)
944 lpmmioinfo->pIOProc = wm->ioProc->pIOProc;
946 return MMSYSERR_NOERROR;
949 /**************************************************************************
950 * mmioSetInfo [WINMM.@]
952 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
956 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
958 if ((wm = MMIO_Get(hmmio)) == NULL)
959 return MMSYSERR_INVALHANDLE;
961 /* check pointers coherence */
962 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
963 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
964 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
965 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
966 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
967 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
968 return MMSYSERR_INVALPARAM;
970 wm->info.pchNext = lpmmioinfo->pchNext;
971 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
973 return MMSYSERR_NOERROR;
976 /**************************************************************************
977 * mmioSetBuffer [WINMM.@]
979 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
983 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%d, uFlags=%#08x)\n",
984 hmmio, pchBuffer, cchBuffer, uFlags);
986 if ((wm = MMIO_Get(hmmio)) == NULL)
987 return MMSYSERR_INVALHANDLE;
989 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
992 /**************************************************************************
993 * mmioFlush [WINMM.@]
995 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
999 TRACE("(%p, %04X)\n", hmmio, uFlags);
1001 if ((wm = MMIO_Get(hmmio)) == NULL)
1002 return MMSYSERR_INVALHANDLE;
1004 return MMIO_Flush(wm, uFlags);
1007 /**************************************************************************
1008 * mmioAdvance [WINMM.@]
1010 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1014 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1016 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1017 * here. be sure if you change something here to check mmioAdvance16 as well
1019 if ((wm = MMIO_Get(hmmio)) == NULL)
1020 return MMSYSERR_INVALHANDLE;
1022 if (!wm->info.cchBuffer)
1023 return MMIOERR_UNBUFFERED;
1025 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1026 return MMSYSERR_INVALPARAM;
1028 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1030 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
1031 lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1032 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
1033 (LPARAM)lpmmioinfo->pchBuffer,
1034 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1035 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1037 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1038 return MMIOERR_CANNOTWRITE;
1041 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1042 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1044 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1047 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1048 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1049 (wm->info.pchEndRead - wm->info.pchBuffer);
1050 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1051 (wm->info.pchEndWrite - wm->info.pchBuffer);
1052 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1053 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1055 return MMSYSERR_NOERROR;
1058 /**************************************************************************
1059 * mmioStringToFOURCCA [WINMM.@]
1061 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1066 for (i = 0; i < 4 && sz[i]; i++) {
1067 if (uFlags & MMIO_TOUPPER) {
1068 cc[i] = toupper(sz[i]);
1074 /* Pad with spaces */
1075 while (i < 4) cc[i++] = ' ';
1077 TRACE("Got '%.4s'\n",cc);
1078 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1081 /**************************************************************************
1082 * mmioStringToFOURCCW [WINMM.@]
1084 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1088 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1089 return mmioStringToFOURCCA(szA,uFlags);
1092 /**************************************************************************
1093 * mmioInstallIOProcA [WINMM.@]
1095 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1096 LPMMIOPROC pIOProc, DWORD dwFlags)
1098 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1101 /**************************************************************************
1102 * mmioInstallIOProcW [WINMM.@]
1104 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1105 LPMMIOPROC pIOProc, DWORD dwFlags)
1107 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1110 /******************************************************************
1115 LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1116 LPARAM lParam2, enum mmioProcType type)
1120 TRACE("(%p, %u, %ld, %ld, %d)\n", hmmio, uMessage, lParam1, lParam2, type);
1122 if (uMessage < MMIOM_USER)
1123 return MMSYSERR_INVALPARAM;
1125 if ((wm = MMIO_Get(hmmio)) == NULL)
1126 return MMSYSERR_INVALHANDLE;
1128 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, type);
1131 /**************************************************************************
1132 * mmioSendMessage [WINMM.@]
1134 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1135 LPARAM lParam1, LPARAM lParam2)
1137 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1140 /**************************************************************************
1141 * mmioDescend [WINMM.@]
1143 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1144 const MMCKINFO* lpckParent, UINT uFlags)
1150 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1153 return MMSYSERR_INVALPARAM;
1155 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1156 TRACE("dwOldPos=%d\n", dwOldPos);
1158 if (lpckParent != NULL) {
1159 TRACE("seek inside parent at %d !\n", lpckParent->dwDataOffset);
1160 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1161 if (dwOldPos < lpckParent->dwDataOffset ||
1162 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1163 WARN("outside parent chunk\n");
1164 return MMIOERR_CHUNKNOTFOUND;
1168 /* The SDK docu says 'ckid' is used for all cases. Real World
1169 * examples disagree -Marcus,990216.
1175 /* find_chunk looks for 'ckid' */
1176 if (uFlags & MMIO_FINDCHUNK)
1177 srchCkId = lpck->ckid;
1179 /* find_riff and find_list look for 'fccType' */
1180 if (uFlags & MMIO_FINDLIST)
1182 srchCkId = FOURCC_LIST;
1183 srchType = lpck->fccType;
1186 if (uFlags & MMIO_FINDRIFF)
1188 srchCkId = FOURCC_RIFF;
1189 srchType = lpck->fccType;
1192 TRACE("searching for %4.4s.%4.4s\n",
1193 (LPCSTR)&srchCkId, srchType ? (LPCSTR)&srchType : "any");
1199 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1200 if (ix < 2*sizeof(DWORD))
1202 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1203 WARN("return ChunkNotFound\n");
1204 return MMIOERR_CHUNKNOTFOUND;
1207 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1208 TRACE("ckid=%4.4s fcc=%4.4s cksize=%08X !\n",
1209 (LPCSTR)&lpck->ckid,
1210 srchType ? (LPCSTR)&lpck->fccType:"<na>",
1212 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1213 (!srchType || (srchType == lpck->fccType)) )
1216 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1217 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1221 /* If we were looking for RIFF/LIST chunks, the final file position
1222 * is after the chunkid. If we were just looking for the chunk
1223 * it is after the cksize. So add 4 in RIFF/LIST case.
1225 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1226 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1229 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1232 TRACE("lpck: ckid=%.4s, cksize=%d, dwDataOffset=%d fccType=%08X (%.4s)!\n",
1233 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1234 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1235 return MMSYSERR_NOERROR;
1238 /**************************************************************************
1239 * mmioAscend [WINMM.@]
1241 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1243 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1245 if (lpck->dwFlags & MMIO_DIRTY) {
1246 DWORD dwOldPos, dwNewSize;
1248 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1249 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1250 TRACE("dwOldPos=%d lpck->dwDataOffset = %d\n", dwOldPos, lpck->dwDataOffset);
1251 dwNewSize = dwOldPos - lpck->dwDataOffset;
1252 if (dwNewSize != lpck->cksize) {
1253 TRACE("Nope: lpck->cksize=%d dwNewSize=%d\n", lpck->cksize, dwNewSize);
1254 lpck->cksize = dwNewSize;
1256 /* pad odd size with 0 */
1257 if (dwNewSize & 1) {
1259 mmioWrite(hmmio, &ch, 1);
1261 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1262 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1267 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1269 return MMSYSERR_NOERROR;
1272 /**************************************************************************
1273 * mmioCreateChunk [WINMM.@]
1275 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1281 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1283 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1284 TRACE("dwOldPos=%d\n", dwOldPos);
1286 if (uFlags == MMIO_CREATELIST)
1287 lpck->ckid = FOURCC_LIST;
1288 else if (uFlags == MMIO_CREATERIFF)
1289 lpck->ckid = FOURCC_RIFF;
1291 TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1293 size = 2 * sizeof(DWORD);
1294 lpck->dwDataOffset = dwOldPos + size;
1296 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1297 size += sizeof(DWORD);
1298 lpck->dwFlags = MMIO_DIRTY;
1300 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1301 TRACE("after mmioWrite ix = %d req = %d, errno = %d\n", ix, size, errno);
1303 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1304 WARN("return CannotWrite\n");
1305 return MMIOERR_CANNOTWRITE;
1308 return MMSYSERR_NOERROR;
1311 /**************************************************************************
1312 * mmioRenameA [WINMM.@]
1314 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1315 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1317 struct IOProcList* ioProc = NULL;
1318 struct IOProcList tmp;
1321 TRACE("('%s', '%s', %p, %08X);\n",
1322 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1324 /* If both params are NULL, then parse the file name */
1325 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1327 fcc = MMIO_ParseExtA(szFileName);
1328 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1331 /* Handle any unhandled/error case from above. Assume DOS file */
1332 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1333 ioProc = MMIO_FindProcNode(FOURCC_DOS);
1334 /* if just the four character code is present, look up IO proc */
1335 else if (lpmmioinfo->pIOProc == NULL)
1336 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1337 else /* use relevant ioProc */
1340 tmp.fourCC = lpmmioinfo->fccIOProc;
1341 tmp.pIOProc = lpmmioinfo->pIOProc;
1342 tmp.type = MMIO_PROC_32A;
1346 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1347 * or make a copy of it because it's const ???
1349 return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1350 (LPARAM)szFileName, (LPARAM)szNewFileName, MMIO_PROC_32A);
1353 /**************************************************************************
1354 * mmioRenameW [WINMM.@]
1356 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1357 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1361 UINT ret = MMSYSERR_NOMEM;
1366 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1367 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1368 if (!szFn) goto done;
1369 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1373 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1374 sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1375 if (!sznFn) goto done;
1376 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1379 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1382 HeapFree(GetProcessHeap(),0,szFn);
1383 HeapFree(GetProcessHeap(),0,sznFn);