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 static WINE_MMIO *MMIOList;
50 /**************************************************************************
51 * mmioDosIOProc [internal]
53 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
54 LPARAM lParam1, LPARAM lParam2)
56 LRESULT ret = MMSYSERR_NOERROR;
58 TRACE("(%p, %X, 0x%lx, 0x%lx);\n", lpmmioinfo, uMessage, lParam1, lParam2);
64 * lParam1 = szFileName parameter from mmioOpen
66 * Returns: zero on success, error code on error
67 * NOTE: lDiskOffset automatically set to zero
69 LPCSTR szFileName = (LPCSTR)lParam1;
71 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
72 FIXME("MMIO_GETTEMP not implemented\n");
73 return MMIOERR_CANNOTOPEN;
76 /* if filename NULL, assume open file handle in adwInfo[0] */
79 lpmmioinfo->adwInfo[0] = OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
81 if (lpmmioinfo->adwInfo[0] == HFILE_ERROR)
82 ret = MMIOERR_FILENOTFOUND;
88 * lParam1 = wFlags parameter from mmioClose
90 * Returns: zero on success, error code on error
92 if (!(lParam1 & MMIO_FHOPEN))
93 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
98 * lParam1 = huge pointer to read buffer
99 * lParam2 = number of bytes to read
100 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
103 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
105 lpmmioinfo->lDiskOffset += ret;
110 case MMIOM_WRITEFLUSH:
111 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
114 * lParam1 = huge pointer to write buffer
115 * lParam2 = number of bytes to write
116 * Returns: number of bytes written, -1 for error (error code in
119 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
121 lpmmioinfo->lDiskOffset += ret;
126 * lParam1 = new position
127 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
128 * Returns: new file postion, -1 on error
130 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
132 lpmmioinfo->lDiskOffset = ret;
139 * Returns: zero on success, non-zero on failure
141 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
142 ret = MMIOERR_FILENOTFOUND;
146 FIXME("unexpected message %u\n", uMessage);
153 /**************************************************************************
154 * mmioMemIOProc [internal]
156 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
157 LPARAM lParam1, LPARAM lParam2)
159 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
165 * lParam1 = filename (must be NULL)
167 * Returns: zero on success, error code on error
168 * NOTE: lDiskOffset automatically set to zero
170 /* FIXME: io proc shouldn't change it */
171 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
172 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
173 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
178 * lParam1 = wFlags parameter from mmioClose
180 * Returns: zero on success, error code on error
186 * lParam1 = huge pointer to read buffer
187 * lParam2 = number of bytes to read
188 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
190 * NOTE: lDiskOffset should be updated
192 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
196 case MMIOM_WRITEFLUSH:
197 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
200 * lParam1 = huge pointer to write buffer
201 * lParam2 = number of bytes to write
202 * Returns: number of bytes written, -1 for error (error code in
204 * NOTE: lDiskOffset should be updated
206 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
211 * lParam1 = new position
212 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
213 * Returns: new file postion, -1 on error
214 * NOTE: lDiskOffset should be updated
216 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
220 FIXME("unexpected message %u\n", uMessage);
225 /* This array will be the entire list for most apps
226 * Note that temporary ioProcs will be stored with a 0 fourCC code
229 static struct IOProcList defaultProcs[] = {
230 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, FALSE, 0},
231 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, FALSE, 0},
234 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
236 /****************************************************************
237 * MMIO_FindProcNode [INTERNAL]
239 * Finds the ProcList node associated with a given FOURCC code.
241 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
243 struct IOProcList* pListNode;
245 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
246 if (pListNode->fourCC == fccIOProc) {
253 /****************************************************************
254 * MMIO_InstallIOProc [INTERNAL]
256 static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
257 DWORD dwFlags, BOOL is_unicode)
259 LPMMIOPROC lpProc = NULL;
260 struct IOProcList* pListNode;
261 struct IOProcList** ppListNode;
263 TRACE("(%08x, %p, %08X, %s)\n", fccIOProc, pIOProc, dwFlags, is_unicode ? "unicode" : "ansi");
265 if (dwFlags & MMIO_GLOBALPROC)
266 FIXME("Global procedures not implemented\n");
268 /* just handle the known procedures for now */
269 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
270 case MMIO_INSTALLPROC:
271 /* Create new entry for the IOProc list */
272 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
274 /* Fill in this node */
275 pListNode->fourCC = fccIOProc;
276 pListNode->pIOProc = pIOProc;
277 pListNode->is_unicode = is_unicode;
278 pListNode->count = 0;
280 /* Stick it on the end of the list */
281 pListNode->pNext = pIOProcListAnchor;
282 pIOProcListAnchor = pListNode;
284 /* Return this IOProc - that's how the caller knows we succeeded */
289 case MMIO_REMOVEPROC:
291 * Search for the node that we're trying to remove
292 * We search for a matching fourCC code if it's non null, or the proc
294 * note that this method won't find the first item on the list, but
295 * since the first two items on this list are ones we won't
296 * let the user delete anyway, that's okay
298 ppListNode = &pIOProcListAnchor;
299 while ((*ppListNode) &&
301 (*ppListNode)->fourCC != fccIOProc :
302 (*ppListNode)->pIOProc != pIOProc))
303 ppListNode = &((*ppListNode)->pNext);
305 if (*ppListNode) { /* found it */
306 /* FIXME: what should be done if an open mmio object uses this proc ?
307 * shall we return an error, nuke the mmio object ?
309 if ((*ppListNode)->count) {
310 ERR("Cannot remove a mmIOProc while in use\n");
313 /* remove it, but only if it isn't builtin */
314 if ((*ppListNode) >= defaultProcs &&
315 (*ppListNode) < defaultProcs + sizeof(defaultProcs) / sizeof(defaultProcs[0])) {
316 WARN("Tried to remove built-in mmio proc. Skipping\n");
319 struct IOProcList* ptmpNode = *ppListNode;
320 lpProc = (*ppListNode)->pIOProc;
321 *ppListNode = (*ppListNode)->pNext;
322 HeapFree(GetProcessHeap(), 0, ptmpNode);
328 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
329 lpProc = pListNode->pIOProc;
337 /****************************************************************
338 * send_message [INTERNAL]
340 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
341 DWORD wMsg, LPARAM lParam1,
342 LPARAM lParam2, BOOL is_unicode)
344 LRESULT result = MMSYSERR_ERROR;
345 LPARAM lp1 = lParam1, lp2 = lParam2;
348 ERR("ioProc NULL\n");
349 return MMSYSERR_INVALPARAM;
352 if (ioProc->is_unicode != is_unicode) {
353 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
354 FIXME("NIY 32 A<=>W mapping\n");
356 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
359 if (ioProc->is_unicode != is_unicode) {
360 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
367 /**************************************************************************
368 * MMIO_ParseExtA [internal]
370 * Parses a filename for the extension.
373 * The FOURCC code for the extension if found, else 0.
375 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
377 /* Filenames are of the form file.ext{+ABC}
378 For now, we take the last '+' if present */
382 /* Note that ext{Start,End} point to the . and + respectively */
388 TRACE("(%s)\n", debugstr_a(szFileName));
393 /* Find the last '+' */
394 extEnd = strrchr(szFileName,'+');
397 /* No + so just an extension */
400 /* Find the first '.' before '+' */
401 extStart = extEnd - 1;
402 while (extStart >= szFileName && *extStart != '.') {
405 if (extStart < szFileName) {
406 ERR("No extension in szFileName: %s\n", debugstr_a(szFileName));
411 if (extEnd - extStart - 1 > 4)
412 WARN("Extension length > 4\n");
413 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
415 TRACE("Got extension: %s\n", debugstr_a(ext));
417 /* FOURCC codes identifying file-extensions must be uppercase */
418 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
423 /**************************************************************************
424 * MMIO_Get [internal]
426 * Retrieves the mmio object from current process
428 static LPWINE_MMIO MMIO_Get(HMMIO h)
430 LPWINE_MMIO wm = NULL;
432 EnterCriticalSection(&WINMM_cs);
433 for (wm = MMIOList; wm; wm = wm->lpNext) {
434 if (wm->info.hmmio == h)
437 LeaveCriticalSection(&WINMM_cs);
441 /**************************************************************************
442 * MMIO_Create [internal]
444 * Creates an internal representation for a mmio instance
446 static LPWINE_MMIO MMIO_Create(void)
448 static WORD MMIO_counter = 0;
451 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
453 EnterCriticalSection(&WINMM_cs);
454 /* lookup next unallocated WORD handle, with a non NULL value */
455 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
456 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
457 wm->lpNext = MMIOList;
459 LeaveCriticalSection(&WINMM_cs);
464 /**************************************************************************
465 * MMIO_Destroy [internal]
467 * Destroys an internal representation for a mmio instance
469 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
473 EnterCriticalSection(&WINMM_cs);
474 /* search for the matching one... */
476 while (*m && *m != wm) m = &(*m)->lpNext;
480 HeapFree(GetProcessHeap(), 0, wm);
483 LeaveCriticalSection(&WINMM_cs);
484 return wm ? FALSE : TRUE;
487 /****************************************************************
488 * MMIO_Flush [INTERNAL]
490 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
492 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
493 /* not quite sure what to do here, but I'll guess */
494 if (wm->info.dwFlags & MMIO_DIRTY) {
495 /* FIXME: error handling */
496 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, wm->info.lBufOffset, SEEK_SET, FALSE);
497 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
498 (LPARAM)wm->info.pchBuffer,
499 wm->info.pchNext - wm->info.pchBuffer, FALSE);
501 if (uFlags & MMIO_EMPTYBUF)
502 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
504 wm->info.dwFlags &= ~MMIO_DIRTY;
506 return MMSYSERR_NOERROR;
509 /***************************************************************************
510 * MMIO_GrabNextBuffer [INTERNAL]
512 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
514 LONG size = wm->info.cchBuffer;
516 TRACE("bo=%x do=%x of=%lx\n",
517 wm->info.lBufOffset, wm->info.lDiskOffset,
518 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE));
520 wm->info.lBufOffset = wm->info.lDiskOffset;
521 wm->info.pchNext = wm->info.pchBuffer;
522 wm->info.pchEndRead = wm->info.pchBuffer;
523 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
525 wm->bBufferLoaded = TRUE;
527 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
528 (LPARAM)wm->info.pchBuffer, size, FALSE);
530 wm->info.pchEndRead += size;
532 wm->bBufferLoaded = FALSE;
538 /***************************************************************************
539 * MMIO_SetBuffer [INTERNAL]
541 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
544 TRACE("(%p %p %d %u)\n", wm, pchBuffer, cchBuffer, uFlags);
546 if (cchBuffer > 0xFFFF)
547 WARN("Untested handling of huge mmio buffers (%d >= 64k)\n", cchBuffer);
549 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
550 return MMIOERR_CANNOTWRITE;
552 /* free previous buffer if allocated */
553 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
554 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
555 wm->info.pchBuffer = NULL;
556 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
560 wm->info.pchBuffer = pchBuffer;
561 } else if (cchBuffer) {
562 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
563 return MMIOERR_OUTOFMEMORY;
564 wm->info.dwFlags |= MMIO_ALLOCBUF;
566 wm->info.pchBuffer = NULL;
569 wm->info.cchBuffer = cchBuffer;
570 wm->info.pchNext = wm->info.pchBuffer;
571 wm->info.pchEndRead = wm->info.pchBuffer;
572 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
573 wm->info.lBufOffset = wm->info.lDiskOffset;
574 wm->bBufferLoaded = FALSE;
576 return MMSYSERR_NOERROR;
579 /**************************************************************************
580 * MMIO_Open [internal]
582 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, BOOL is_unicode)
588 TRACE("(%s, %p, %08X, %s);\n", debugstr_a(szFileName), refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
591 refmminfo = &mmioinfo;
593 mmioinfo.fccIOProc = 0;
594 mmioinfo.pIOProc = NULL;
595 mmioinfo.pchBuffer = NULL;
596 mmioinfo.cchBuffer = 0;
600 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
601 char buffer[MAX_PATH];
605 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
607 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
609 strcpy(szFileName, buffer);
613 if ((wm = MMIO_Create()) == NULL)
616 /* If both params are NULL, then parse the file name if available */
617 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
618 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
619 /* Handle any unhandled/error case. Assume DOS file */
620 if (wm->info.fccIOProc == 0) {
621 wm->info.fccIOProc = FOURCC_DOS;
622 wm->ioProc = &defaultProcs[0];
624 else if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
625 /* If not found, assume DOS file */
626 wm->ioProc = &defaultProcs[0];
628 wm->bTmpIOProc = FALSE;
630 /* if just the four character code is present, look up IO proc */
631 else if (refmminfo->pIOProc == NULL) {
632 wm->info.fccIOProc = refmminfo->fccIOProc;
633 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
634 wm->bTmpIOProc = FALSE;
636 /* if IO proc specified, use it and specified four character code */
638 wm->info.fccIOProc = refmminfo->fccIOProc;
639 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
640 MMIO_INSTALLPROC, is_unicode);
641 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
642 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
643 wm->bTmpIOProc = TRUE;
647 wm->info.dwFlags = dwOpenFlags;
649 if (dwOpenFlags & MMIO_ALLOCBUF) {
650 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
651 refmminfo->cchBuffer ? refmminfo->cchBuffer : MMIO_DEFAULTBUFFER, 0);
652 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
655 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
656 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
660 if (wm->info.fccIOProc == FOURCC_MEM && !(wm->info.dwFlags & MMIO_ALLOCBUF))
661 wm->bBufferLoaded = TRUE;
663 /* see mmioDosIOProc for that one */
664 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
666 /* call IO proc to actually open file */
667 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
668 (LPARAM)szFileName, 0, FALSE);
670 /* grab file size, when possible */
671 if (wm->info.fccIOProc != FOURCC_MEM && (send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE)) != -1) {
672 pos = wm->info.lDiskOffset;
673 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_END, FALSE);
674 wm->dwFileSize = wm->info.lDiskOffset;
675 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, pos, SEEK_SET, FALSE);
677 else wm->dwFileSize = 0;
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 NULL;
702 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
705 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
707 HeapFree(GetProcessHeap(), 0, szFn);
711 /**************************************************************************
712 * mmioOpenA [WINMM.@]
714 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
717 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
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, uFlags, 0, FALSE);
738 MMIO_SetBuffer(wm, NULL, 0, 0);
743 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
744 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
751 /**************************************************************************
754 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
759 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
761 if ((wm = MMIO_Get(hmmio)) == NULL)
764 /* unbuffered case first */
765 if (!wm->info.pchBuffer)
766 return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
768 /* first try from current buffer */
769 if (wm->info.pchNext != wm->info.pchEndRead) {
770 count = wm->info.pchEndRead - wm->info.pchNext;
771 if (count > cch || count < 0) count = cch;
772 memcpy(pch, wm->info.pchNext, count);
773 wm->info.pchNext += count;
779 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
780 assert(wm->info.cchBuffer);
785 size = MMIO_GrabNextBuffer(wm, TRUE);
786 if (size <= 0) break;
787 if (size > cch) size = cch;
788 memcpy(pch, wm->info.pchBuffer, size);
789 wm->info.pchNext += size;
796 TRACE("count=%d\n", count);
800 /**************************************************************************
801 * mmioWrite [WINMM.@]
803 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
808 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
810 if ((wm = MMIO_Get(hmmio)) == NULL)
813 if (wm->info.cchBuffer) {
818 if (wm->info.pchNext != wm->info.pchEndWrite) {
819 count = wm->info.pchEndWrite - wm->info.pchNext;
820 if (count > cch || count < 0) count = cch;
821 memcpy(wm->info.pchNext, pch, count);
822 wm->info.pchNext += count;
826 wm->info.dwFlags |= MMIO_DIRTY;
828 if (wm->info.fccIOProc == FOURCC_MEM) {
829 if (wm->info.adwInfo[0]) {
830 /* from where would we get the memory handle? */
831 FIXME("memory file expansion not implemented!\n");
837 if (wm->info.pchNext == wm->info.pchEndWrite)
839 MMIO_Flush(wm, MMIO_EMPTYBUF);
840 MMIO_GrabNextBuffer(wm, FALSE);
846 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
847 wm->info.lBufOffset = wm->info.lDiskOffset;
850 TRACE("bytes written=%d\n", count);
854 /**************************************************************************
857 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
862 TRACE("(%p, %08X, %d);\n", hmmio, lOffset, iOrigin);
864 if ((wm = MMIO_Get(hmmio)) == NULL)
865 return MMSYSERR_INVALHANDLE;
867 /* not buffered, direct seek on file */
868 if (!wm->info.pchBuffer)
869 return send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, iOrigin, FALSE);
876 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
879 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
885 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
886 /* should check that write mode exists */
887 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
889 wm->info.lBufOffset = offset;
890 wm->info.pchEndRead = wm->info.pchBuffer;
891 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
892 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
893 wm->info.lDiskOffset = wm->dwFileSize;
895 } else if ((wm->info.cchBuffer > 0) &&
896 ((offset < wm->info.lBufOffset) ||
897 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
898 !wm->bBufferLoaded)) {
899 /* stay in same buffer ? */
900 /* some memory mapped buffers are defined with -1 as a size */
902 /* condition to change buffer */
903 if ((wm->info.fccIOProc == FOURCC_MEM) ||
904 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
905 /* this also sets the wm->info.lDiskOffset field */
906 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
907 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
908 SEEK_SET, FALSE) == -1)
910 MMIO_GrabNextBuffer(wm, TRUE);
913 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
915 TRACE("=> %d\n", offset);
919 /**************************************************************************
920 * mmioGetInfo [WINMM.@]
922 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
926 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
928 if ((wm = MMIO_Get(hmmio)) == NULL)
929 return MMSYSERR_INVALHANDLE;
931 *lpmmioinfo = wm->info;
933 return MMSYSERR_NOERROR;
936 /**************************************************************************
937 * mmioSetInfo [WINMM.@]
939 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
943 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
945 if ((wm = MMIO_Get(hmmio)) == NULL)
946 return MMSYSERR_INVALHANDLE;
948 /* check pointers coherence */
949 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
950 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
951 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
952 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
953 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
954 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
955 return MMSYSERR_INVALPARAM;
957 wm->info.pchNext = lpmmioinfo->pchNext;
958 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
960 return MMSYSERR_NOERROR;
963 /**************************************************************************
964 * mmioSetBuffer [WINMM.@]
966 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
970 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%d, uFlags=%#08x)\n",
971 hmmio, pchBuffer, cchBuffer, uFlags);
973 if ((wm = MMIO_Get(hmmio)) == NULL)
974 return MMSYSERR_INVALHANDLE;
976 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
979 /**************************************************************************
980 * mmioFlush [WINMM.@]
982 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
986 TRACE("(%p, %04X)\n", hmmio, uFlags);
988 if ((wm = MMIO_Get(hmmio)) == NULL)
989 return MMSYSERR_INVALHANDLE;
991 return MMIO_Flush(wm, uFlags);
994 /**************************************************************************
995 * mmioAdvance [WINMM.@]
997 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1001 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1003 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1004 * here. be sure if you change something here to check mmioAdvance16 as well
1006 if ((wm = MMIO_Get(hmmio)) == NULL)
1007 return MMSYSERR_INVALHANDLE;
1009 if (!wm->info.cchBuffer)
1010 return MMIOERR_UNBUFFERED;
1012 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1013 return MMSYSERR_INVALPARAM;
1015 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1017 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
1018 send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1019 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1020 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1022 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1023 return MMIOERR_CANNOTWRITE;
1026 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1027 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1029 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1032 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1033 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1034 (wm->info.pchEndRead - wm->info.pchBuffer);
1035 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1036 (wm->info.pchEndWrite - wm->info.pchBuffer);
1037 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1038 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1040 return MMSYSERR_NOERROR;
1043 /**************************************************************************
1044 * mmioStringToFOURCCA [WINMM.@]
1046 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1051 for (i = 0; i < 4 && sz[i]; i++) {
1052 if (uFlags & MMIO_TOUPPER) {
1053 cc[i] = toupper(sz[i]);
1059 /* Pad with spaces */
1060 while (i < 4) cc[i++] = ' ';
1062 TRACE("Got %s\n",debugstr_an(cc,4));
1063 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1066 /**************************************************************************
1067 * mmioStringToFOURCCW [WINMM.@]
1069 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1073 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1074 return mmioStringToFOURCCA(szA,uFlags);
1077 /**************************************************************************
1078 * mmioInstallIOProcA [WINMM.@]
1080 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1081 LPMMIOPROC pIOProc, DWORD dwFlags)
1083 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, FALSE);
1086 /**************************************************************************
1087 * mmioInstallIOProcW [WINMM.@]
1089 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1090 LPMMIOPROC pIOProc, DWORD dwFlags)
1092 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, TRUE);
1095 /******************************************************************
1100 static LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1101 LPARAM lParam2, BOOL is_unicode)
1105 TRACE("(%p, %u, %ld, %ld, %s)\n", hmmio, uMessage, lParam1, lParam2, is_unicode ? "unicode" : "ansi");
1107 if (uMessage < MMIOM_USER)
1108 return MMSYSERR_INVALPARAM;
1110 if ((wm = MMIO_Get(hmmio)) == NULL)
1111 return MMSYSERR_INVALHANDLE;
1113 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, is_unicode);
1116 /**************************************************************************
1117 * mmioSendMessage [WINMM.@]
1119 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1120 LPARAM lParam1, LPARAM lParam2)
1122 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, FALSE);
1125 /**************************************************************************
1126 * mmioDescend [WINMM.@]
1128 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1129 const MMCKINFO* lpckParent, UINT uFlags)
1135 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1138 return MMSYSERR_INVALPARAM;
1140 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1141 TRACE("dwOldPos=%d\n", dwOldPos);
1143 if (lpckParent != NULL) {
1144 TRACE("seek inside parent at %d !\n", lpckParent->dwDataOffset);
1145 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1146 if (dwOldPos < lpckParent->dwDataOffset ||
1147 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1148 WARN("outside parent chunk\n");
1149 return MMIOERR_CHUNKNOTFOUND;
1153 /* The SDK docu says 'ckid' is used for all cases. Real World
1154 * examples disagree -Marcus,990216.
1160 /* find_chunk looks for 'ckid' */
1161 if (uFlags & MMIO_FINDCHUNK)
1162 srchCkId = lpck->ckid;
1164 /* find_riff and find_list look for 'fccType' */
1165 if (uFlags & MMIO_FINDLIST)
1167 srchCkId = FOURCC_LIST;
1168 srchType = lpck->fccType;
1171 if (uFlags & MMIO_FINDRIFF)
1173 srchCkId = FOURCC_RIFF;
1174 srchType = lpck->fccType;
1177 TRACE("searching for %s.%s\n",
1178 debugstr_an((LPCSTR)&srchCkId, 4), srchType ? debugstr_an((LPCSTR)&srchType, 4) : "<any>");
1184 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1185 if (ix < 2*sizeof(DWORD))
1187 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1188 WARN("return ChunkNotFound\n");
1189 return MMIOERR_CHUNKNOTFOUND;
1192 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1193 TRACE("ckid=%s fcc=%s cksize=%08X !\n",
1194 debugstr_an((LPCSTR)&lpck->ckid, 4),
1195 srchType ? debugstr_an((LPCSTR)&lpck->fccType, 4) : "<na>",
1197 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1198 (!srchType || (srchType == lpck->fccType)) )
1201 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1202 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1206 /* If we were looking for RIFF/LIST chunks, the final file position
1207 * is after the chunkid. If we were just looking for the chunk
1208 * it is after the cksize. So add 4 in RIFF/LIST case.
1210 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1211 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1214 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1217 TRACE("lpck: ckid=%s, cksize=%d, dwDataOffset=%d fccType=%08X (%s)!\n",
1218 debugstr_an((LPSTR)&lpck->ckid, 4), lpck->cksize, lpck->dwDataOffset,
1219 lpck->fccType, srchType ? debugstr_an((LPSTR)&lpck->fccType, 4):"");
1220 return MMSYSERR_NOERROR;
1223 /**************************************************************************
1224 * mmioAscend [WINMM.@]
1226 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1228 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1230 if (lpck->dwFlags & MMIO_DIRTY) {
1231 DWORD dwOldPos, dwNewSize;
1233 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1234 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1235 TRACE("dwOldPos=%d lpck->dwDataOffset = %d\n", dwOldPos, lpck->dwDataOffset);
1236 dwNewSize = dwOldPos - lpck->dwDataOffset;
1237 if (dwNewSize != lpck->cksize) {
1238 TRACE("Nope: lpck->cksize=%d dwNewSize=%d\n", lpck->cksize, dwNewSize);
1239 lpck->cksize = dwNewSize;
1241 /* pad odd size with 0 */
1242 if (dwNewSize & 1) {
1244 mmioWrite(hmmio, &ch, 1);
1246 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1247 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1252 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1254 return MMSYSERR_NOERROR;
1257 /**************************************************************************
1258 * mmioCreateChunk [WINMM.@]
1260 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1266 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1268 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1269 TRACE("dwOldPos=%d\n", dwOldPos);
1271 if (uFlags == MMIO_CREATELIST)
1272 lpck->ckid = FOURCC_LIST;
1273 else if (uFlags == MMIO_CREATERIFF)
1274 lpck->ckid = FOURCC_RIFF;
1276 TRACE("ckid=%s\n", debugstr_an((LPSTR)&lpck->ckid, 4));
1278 size = 2 * sizeof(DWORD);
1279 lpck->dwDataOffset = dwOldPos + size;
1281 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1282 size += sizeof(DWORD);
1283 lpck->dwFlags = MMIO_DIRTY;
1285 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1286 TRACE("after mmioWrite ix = %d req = %d, errno = %d\n", ix, size, errno);
1288 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1289 WARN("return CannotWrite\n");
1290 return MMIOERR_CANNOTWRITE;
1293 return MMSYSERR_NOERROR;
1296 /**************************************************************************
1297 * mmioRenameA [WINMM.@]
1299 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1300 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1302 struct IOProcList* ioProc = NULL;
1303 struct IOProcList tmp;
1306 TRACE("(%s, %s, %p, %08X);\n",
1307 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1309 /* If both params are NULL, then parse the file name */
1310 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1312 fcc = MMIO_ParseExtA(szFileName);
1313 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1316 /* Handle any unhandled/error case from above. Assume DOS file */
1317 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1318 ioProc = &defaultProcs[0];
1319 /* if just the four character code is present, look up IO proc */
1320 else if (lpmmioinfo->pIOProc == NULL)
1321 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1322 else /* use relevant ioProc */
1325 tmp.fourCC = lpmmioinfo->fccIOProc;
1326 tmp.pIOProc = lpmmioinfo->pIOProc;
1327 tmp.is_unicode = FALSE;
1331 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1332 * or make a copy of it because it's const ???
1334 return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1335 (LPARAM)szFileName, (LPARAM)szNewFileName, FALSE);
1338 /**************************************************************************
1339 * mmioRenameW [WINMM.@]
1341 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1342 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1346 UINT ret = MMSYSERR_NOMEM;
1351 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1352 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1353 if (!szFn) goto done;
1354 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1358 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1359 sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1360 if (!sznFn) goto done;
1361 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1364 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1367 HeapFree(GetProcessHeap(),0,szFn);
1368 HeapFree(GetProcessHeap(),0,sznFn);