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 For now, we take the last '+' if present */
397 /* Note that ext{Start,End} point to the . and + respectively */
401 TRACE("(%s)\n", debugstr_a(szFileName));
406 /* Find the last '.' */
407 extStart = strrchr(szFileName,'.');
410 ERR("No . in szFileName: %s\n", debugstr_a(szFileName));
414 /* Find the '+' afterwards */
415 extEnd = strchr(extStart,'+');
418 if (extEnd - extStart - 1 > 4)
419 WARN("Extension length > 4\n");
420 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
423 /* No + so just an extension */
424 if (strlen(extStart) > 4) {
425 WARN("Extension length > 4\n");
427 lstrcpynA(ext, extStart + 1, 5);
429 TRACE("Got extension: %s\n", debugstr_a(ext));
431 /* FOURCC codes identifying file-extensions must be uppercase */
432 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
437 /**************************************************************************
438 * MMIO_Get [internal]
440 * Retrieves the mmio object from current process
442 LPWINE_MMIO MMIO_Get(HMMIO h)
444 LPWINE_MMIO wm = NULL;
446 EnterCriticalSection(&WINMM_IData->cs);
447 for (wm = WINMM_IData->lpMMIO; wm; wm = wm->lpNext) {
448 if (wm->info.hmmio == h)
451 LeaveCriticalSection(&WINMM_IData->cs);
455 /**************************************************************************
456 * MMIO_Create [internal]
458 * Creates an internal representation for a mmio instance
460 static LPWINE_MMIO MMIO_Create(void)
462 static WORD MMIO_counter = 0;
465 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
467 EnterCriticalSection(&WINMM_IData->cs);
468 /* lookup next unallocated WORD handle, with a non NULL value */
469 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
470 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
471 wm->lpNext = WINMM_IData->lpMMIO;
472 WINMM_IData->lpMMIO = wm;
473 LeaveCriticalSection(&WINMM_IData->cs);
478 /**************************************************************************
479 * MMIO_Destroy [internal]
481 * Destroys an internal representation for a mmio instance
483 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
487 EnterCriticalSection(&WINMM_IData->cs);
488 /* search for the matching one... */
489 m = &(WINMM_IData->lpMMIO);
490 while (*m && *m != wm) m = &(*m)->lpNext;
494 HeapFree(GetProcessHeap(), 0, wm);
497 LeaveCriticalSection(&WINMM_IData->cs);
498 return wm ? FALSE : TRUE;
501 /****************************************************************
502 * MMIO_Flush [INTERNAL]
504 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
506 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
507 /* not quite sure what to do here, but I'll guess */
508 if (wm->info.dwFlags & MMIO_DIRTY) {
509 /* FIXME: error handling */
510 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
511 wm->info.lBufOffset, SEEK_SET, MMIO_PROC_32A);
512 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
513 (LPARAM)wm->info.pchBuffer,
514 wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
516 if (uFlags & MMIO_EMPTYBUF)
517 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
519 wm->info.dwFlags &= ~MMIO_DIRTY;
521 return MMSYSERR_NOERROR;
524 /***************************************************************************
525 * MMIO_GrabNextBuffer [INTERNAL]
527 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
529 LONG size = wm->info.cchBuffer;
531 TRACE("bo=%lx do=%lx of=%lx\n",
532 wm->info.lBufOffset, wm->info.lDiskOffset,
533 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
535 wm->info.lBufOffset = wm->info.lDiskOffset;
536 wm->info.pchNext = wm->info.pchBuffer;
537 wm->info.pchEndRead = wm->info.pchBuffer;
538 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
540 wm->bBufferLoaded = TRUE;
542 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
543 (LPARAM)wm->info.pchBuffer, size, MMIO_PROC_32A);
545 wm->info.pchEndRead += size;
547 wm->bBufferLoaded = FALSE;
553 /***************************************************************************
554 * MMIO_SetBuffer [INTERNAL]
556 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
559 TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
561 if (uFlags) return MMSYSERR_INVALPARAM;
562 if (cchBuffer > 0xFFFF)
563 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
565 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
566 return MMIOERR_CANNOTWRITE;
568 /* free previous buffer if allocated */
569 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
570 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
571 wm->info.pchBuffer = NULL;
572 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
576 wm->info.pchBuffer = pchBuffer;
577 } else if (cchBuffer) {
578 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
579 return MMIOERR_OUTOFMEMORY;
580 wm->info.dwFlags |= MMIO_ALLOCBUF;
582 wm->info.pchBuffer = NULL;
585 wm->info.cchBuffer = cchBuffer;
586 wm->info.pchNext = wm->info.pchBuffer;
587 wm->info.pchEndRead = wm->info.pchBuffer;
588 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
589 wm->info.lBufOffset = 0;
590 wm->bBufferLoaded = FALSE;
592 return MMSYSERR_NOERROR;
595 /**************************************************************************
596 * MMIO_Open [internal]
598 HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags,
599 enum mmioProcType type)
604 TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
607 refmminfo = &mmioinfo;
609 mmioinfo.fccIOProc = 0;
610 mmioinfo.pIOProc = NULL;
611 mmioinfo.pchBuffer = NULL;
612 mmioinfo.cchBuffer = 0;
613 type = MMIO_PROC_32A;
616 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
617 char buffer[MAX_PATH];
619 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
621 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
623 strcpy(szFileName, buffer);
627 if ((wm = MMIO_Create()) == NULL)
630 /* If both params are NULL, then parse the file name if available */
631 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
632 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
633 /* Handle any unhandled/error case. Assume DOS file */
634 if (wm->info.fccIOProc == 0)
635 wm->info.fccIOProc = FOURCC_DOS;
636 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
637 wm->bTmpIOProc = FALSE;
639 /* if just the four character code is present, look up IO proc */
640 else if (refmminfo->pIOProc == NULL) {
641 wm->info.fccIOProc = refmminfo->fccIOProc;
642 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
643 wm->bTmpIOProc = FALSE;
645 /* if IO proc specified, use it and specified four character code */
647 wm->info.fccIOProc = refmminfo->fccIOProc;
648 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
649 MMIO_INSTALLPROC, type);
650 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
651 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
652 wm->bTmpIOProc = TRUE;
655 wm->bBufferLoaded = FALSE;
658 if (dwOpenFlags & MMIO_ALLOCBUF) {
659 if ((refmminfo->wErrorRet = MMIO_SetBuffer(wm, NULL, MMIO_DEFAULTBUFFER, 0)))
661 } else if (wm->info.fccIOProc == FOURCC_MEM) {
662 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
663 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
665 wm->bBufferLoaded = TRUE;
666 } /* else => unbuffered, wm->info.pchBuffer == NULL */
668 /* see mmioDosIOProc for that one */
669 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
670 wm->info.dwFlags = dwOpenFlags;
672 /* call IO proc to actually open file */
673 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
674 (LPARAM)szFileName, 0, MMIO_PROC_32A);
676 /* grab file size, when possible */
677 wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
679 if (refmminfo->wErrorRet == 0)
680 return wm->info.hmmio;
682 if (wm->ioProc) wm->ioProc->count--;
688 /**************************************************************************
689 * mmioOpenW [WINMM.@]
691 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
699 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
700 szFn = HeapAlloc( GetProcessHeap(), 0, len );
701 if (!szFn) return (HMMIO)NULL;
702 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
705 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
707 HeapFree(GetProcessHeap(), 0, szFn);
711 /**************************************************************************
712 * mmioOpenA [WINMM.@]
714 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
717 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
720 /**************************************************************************
721 * mmioClose [WINMM.@]
723 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
728 TRACE("(%p, %04X);\n", hmmio, uFlags);
730 if ((wm = MMIO_Get(hmmio)) == NULL)
731 return MMSYSERR_INVALHANDLE;
733 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
736 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE,
737 uFlags, 0, MMIO_PROC_32A);
739 MMIO_SetBuffer(wm, NULL, 0, 0);
744 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
745 MMIO_REMOVEPROC, wm->ioProc->type);
752 /**************************************************************************
755 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
760 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
762 if ((wm = MMIO_Get(hmmio)) == NULL)
765 /* unbuffered case first */
766 if (!wm->info.pchBuffer)
767 return send_message(wm->ioProc, &wm->info, MMIOM_READ,
768 (LPARAM)pch, cch, MMIO_PROC_32A);
770 /* first try from current buffer */
771 if (wm->info.pchNext != wm->info.pchEndRead) {
772 count = wm->info.pchEndRead - wm->info.pchNext;
773 if (count > cch || count < 0) count = cch;
774 memcpy(pch, wm->info.pchNext, count);
775 wm->info.pchNext += count;
781 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
782 assert(wm->info.cchBuffer);
787 size = MMIO_GrabNextBuffer(wm, TRUE);
788 if (size <= 0) break;
789 if (size > cch) size = cch;
790 memcpy(pch, wm->info.pchBuffer, size);
791 wm->info.pchNext += size;
798 TRACE("count=%ld\n", count);
802 /**************************************************************************
803 * mmioWrite [WINMM.@]
805 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
810 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
812 if ((wm = MMIO_Get(hmmio)) == NULL)
815 if (wm->info.cchBuffer) {
820 if (wm->info.pchNext != wm->info.pchEndWrite) {
821 count = wm->info.pchEndWrite - wm->info.pchNext;
822 if (count > cch || count < 0) count = cch;
823 memcpy(wm->info.pchNext, pch, count);
824 wm->info.pchNext += count;
828 wm->info.dwFlags |= MMIO_DIRTY;
830 if (wm->info.fccIOProc == FOURCC_MEM) {
831 if (wm->info.adwInfo[0]) {
832 /* from where would we get the memory handle? */
833 FIXME("memory file expansion not implemented!\n");
839 if (wm->info.pchNext == wm->info.pchEndWrite)
841 MMIO_Flush(wm, MMIO_EMPTYBUF);
842 MMIO_GrabNextBuffer(wm, FALSE);
848 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
849 (LPARAM)pch, cch, MMIO_PROC_32A);
850 wm->info.lBufOffset = wm->info.lDiskOffset;
853 TRACE("bytes written=%ld\n", count);
857 /**************************************************************************
860 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
865 TRACE("(%p, %08lX, %d);\n", hmmio, lOffset, iOrigin);
867 if ((wm = MMIO_Get(hmmio)) == NULL)
868 return MMSYSERR_INVALHANDLE;
870 /* not buffered, direct seek on file */
871 if (!wm->info.pchBuffer)
872 return send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
873 lOffset, iOrigin, MMIO_PROC_32A);
880 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
883 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
889 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
890 /* should check that write mode exists */
891 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
893 wm->info.lBufOffset = offset;
894 wm->info.pchEndRead = wm->info.pchBuffer;
895 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
896 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
897 wm->info.lDiskOffset = wm->dwFileSize;
899 } else if ((wm->info.cchBuffer > 0) &&
900 ((offset < wm->info.lBufOffset) ||
901 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
902 !wm->bBufferLoaded)) {
903 /* stay in same buffer ? */
904 /* some memory mapped buffers are defined with -1 as a size */
906 /* condition to change buffer */
907 if ((wm->info.fccIOProc == FOURCC_MEM) ||
908 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
909 /* this also sets the wm->info.lDiskOffset field */
910 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
911 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
912 SEEK_SET, MMIO_PROC_32A) == -1)
914 MMIO_GrabNextBuffer(wm, TRUE);
917 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
919 TRACE("=> %ld\n", offset);
923 /**************************************************************************
924 * mmioGetInfo [WINMM.@]
926 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
930 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
932 if ((wm = MMIO_Get(hmmio)) == NULL)
933 return MMSYSERR_INVALHANDLE;
935 memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
936 /* don't expose 16 bit ioproc:s */
937 if (wm->ioProc->type != MMIO_PROC_16)
938 lpmmioinfo->pIOProc = wm->ioProc->pIOProc;
940 return MMSYSERR_NOERROR;
943 /**************************************************************************
944 * mmioSetInfo [WINMM.@]
946 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
950 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
952 if ((wm = MMIO_Get(hmmio)) == NULL)
953 return MMSYSERR_INVALHANDLE;
955 /* check pointers coherence */
956 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
957 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
958 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
959 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
960 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
961 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
962 return MMSYSERR_INVALPARAM;
964 wm->info.pchNext = lpmmioinfo->pchNext;
965 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
967 return MMSYSERR_NOERROR;
970 /**************************************************************************
971 * mmioSetBuffer [WINMM.@]
973 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
977 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
978 hmmio, pchBuffer, cchBuffer, uFlags);
980 if ((wm = MMIO_Get(hmmio)) == NULL)
981 return MMSYSERR_INVALHANDLE;
983 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
986 /**************************************************************************
987 * mmioFlush [WINMM.@]
989 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
993 TRACE("(%p, %04X)\n", hmmio, uFlags);
995 if ((wm = MMIO_Get(hmmio)) == NULL)
996 return MMSYSERR_INVALHANDLE;
998 return MMIO_Flush(wm, uFlags);
1001 /**************************************************************************
1002 * mmioAdvance [WINMM.@]
1004 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1008 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1010 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1011 * here. be sure if you change something here to check mmioAdvance16 as well
1013 if ((wm = MMIO_Get(hmmio)) == NULL)
1014 return MMSYSERR_INVALHANDLE;
1016 if (!wm->info.cchBuffer)
1017 return MMIOERR_UNBUFFERED;
1019 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1020 return MMSYSERR_INVALPARAM;
1022 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1024 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
1025 lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1026 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
1027 (LPARAM)lpmmioinfo->pchBuffer,
1028 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1029 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1031 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1032 return MMIOERR_CANNOTWRITE;
1035 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1036 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1038 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1041 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1042 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1043 (wm->info.pchEndRead - wm->info.pchBuffer);
1044 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1045 (wm->info.pchEndWrite - wm->info.pchBuffer);
1046 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1047 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1049 return MMSYSERR_NOERROR;
1052 /**************************************************************************
1053 * mmioStringToFOURCCA [WINMM.@]
1055 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1060 for (i = 0; i < 4 && sz[i]; i++) {
1061 if (uFlags & MMIO_TOUPPER) {
1062 cc[i] = toupper(sz[i]);
1068 /* Pad with spaces */
1069 while (i < 4) cc[i++] = ' ';
1071 TRACE("Got '%.4s'\n",cc);
1072 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1075 /**************************************************************************
1076 * mmioStringToFOURCCW [WINMM.@]
1078 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1082 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1083 return mmioStringToFOURCCA(szA,uFlags);
1086 /**************************************************************************
1087 * mmioInstallIOProcA [WINMM.@]
1089 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1090 LPMMIOPROC pIOProc, DWORD dwFlags)
1092 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1095 /**************************************************************************
1096 * mmioInstallIOProcW [WINMM.@]
1098 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1099 LPMMIOPROC pIOProc, DWORD dwFlags)
1101 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1104 /******************************************************************
1109 LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1110 LPARAM lParam2, enum mmioProcType type)
1114 TRACE("(%p, %u, %ld, %ld, %d)\n", hmmio, uMessage, lParam1, lParam2, type);
1116 if (uMessage < MMIOM_USER)
1117 return MMSYSERR_INVALPARAM;
1119 if ((wm = MMIO_Get(hmmio)) == NULL)
1120 return MMSYSERR_INVALHANDLE;
1122 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, type);
1125 /**************************************************************************
1126 * mmioSendMessage [WINMM.@]
1128 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1129 LPARAM lParam1, LPARAM lParam2)
1131 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1134 /**************************************************************************
1135 * mmioDescend [WINMM.@]
1137 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1138 const MMCKINFO* lpckParent, UINT uFlags)
1145 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1148 return MMSYSERR_INVALPARAM;
1150 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1151 TRACE("dwOldPos=%ld\n", dwOldPos);
1153 if (lpckParent != NULL) {
1154 TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1155 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1156 if (dwOldPos < lpckParent->dwDataOffset ||
1157 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1158 WARN("outside parent chunk\n");
1159 return MMIOERR_CHUNKNOTFOUND;
1163 /* The SDK docu says 'ckid' is used for all cases. Real World
1164 * examples disagree -Marcus,990216.
1168 /* find_chunk looks for 'ckid' */
1169 if (uFlags & MMIO_FINDCHUNK)
1170 srchCkId = lpck->ckid;
1171 /* find_riff and find_list look for 'fccType' */
1172 if (uFlags & MMIO_FINDLIST) {
1173 srchCkId = FOURCC_LIST;
1174 srchType = lpck->fccType;
1176 if (uFlags & MMIO_FINDRIFF) {
1177 srchCkId = FOURCC_RIFF;
1178 srchType = lpck->fccType;
1181 if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1182 TRACE("searching for %.4s.%.4s\n",
1184 srchType?(LPSTR)&srchType:"any ");
1189 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1190 if (ix < 2*sizeof(DWORD)) {
1191 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1192 WARN("return ChunkNotFound\n");
1193 return MMIOERR_CHUNKNOTFOUND;
1195 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1196 if (ix < lpck->dwDataOffset - dwOldPos) {
1197 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1198 WARN("return ChunkNotFound\n");
1199 return MMIOERR_CHUNKNOTFOUND;
1201 TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1203 srchType?(LPSTR)&lpck->fccType:"<na>",
1205 if ((srchCkId == lpck->ckid) &&
1206 (!srchType || (srchType == lpck->fccType))
1210 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1211 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1214 /* FIXME: unverified, does it do this? */
1215 /* NB: This part is used by WAVE_mciOpen, among others */
1216 if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1217 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1218 WARN("return ChunkNotFound 2nd\n");
1219 return MMIOERR_CHUNKNOTFOUND;
1221 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1224 /* If we were looking for RIFF/LIST chunks, the final file position
1225 * is after the chunkid. If we were just looking for the chunk
1226 * it is after the cksize. So add 4 in RIFF/LIST case.
1228 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1229 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1231 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1232 TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.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=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1251 dwNewSize = dwOldPos - lpck->dwDataOffset;
1252 if (dwNewSize != lpck->cksize) {
1253 TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\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=%ld\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 = %ld req = %ld, 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, %08lX);\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, (LPMMIOINFO)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)
1366 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1367 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1368 if (!szFn) return MMSYSERR_NOMEM;
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) return MMSYSERR_NOMEM;
1376 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1379 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1381 HeapFree(GetProcessHeap(),0,szFn);
1382 HeapFree(GetProcessHeap(),0,sznFn);