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
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
47 LRESULT (*pFnMmioCallback16)(SEGPTR,LPMMIOINFO,UINT,LPARAM,LPARAM) /* = NULL */;
49 /**************************************************************************
50 * mmioDosIOProc [internal]
52 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
53 LPARAM lParam1, LPARAM lParam2)
55 LRESULT ret = MMSYSERR_NOERROR;
57 TRACE("(%p, %X, 0x%lx, 0x%lx);\n", lpmmioinfo, uMessage, lParam1, lParam2);
63 * lParam1 = szFileName parameter from mmioOpen
65 * Returns: zero on success, error code on error
66 * NOTE: lDiskOffset automatically set to zero
68 LPCSTR szFileName = (LPCSTR)lParam1;
70 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
71 FIXME("MMIO_GETTEMP not implemented\n");
72 return MMIOERR_CANNOTOPEN;
75 /* if filename NULL, assume open file handle in adwInfo[0] */
78 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
80 if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
81 ret = MMIOERR_CANNOTOPEN;
87 * lParam1 = wFlags parameter from mmioClose
89 * Returns: zero on success, error code on error
91 if (!(lParam1 & MMIO_FHOPEN))
92 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
97 * lParam1 = huge pointer to read buffer
98 * lParam2 = number of bytes to read
99 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
102 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
104 lpmmioinfo->lDiskOffset += ret;
109 case MMIOM_WRITEFLUSH:
110 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
113 * lParam1 = huge pointer to write buffer
114 * lParam2 = number of bytes to write
115 * Returns: number of bytes written, -1 for error (error code in
118 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
120 lpmmioinfo->lDiskOffset += ret;
125 * lParam1 = new position
126 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
127 * Returns: new file postion, -1 on error
129 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
131 lpmmioinfo->lDiskOffset = ret;
138 * Returns: zero on success, non-zero on failure
140 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
141 ret = MMIOERR_FILENOTFOUND;
145 FIXME("unexpected message %u\n", uMessage);
152 /**************************************************************************
153 * mmioMemIOProc [internal]
155 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
156 LPARAM lParam1, LPARAM lParam2)
158 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
164 * lParam1 = filename (must be NULL)
166 * Returns: zero on success, error code on error
167 * NOTE: lDiskOffset automatically set to zero
169 /* FIXME: io proc shouldn't change it */
170 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
171 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
172 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
177 * lParam1 = wFlags parameter from mmioClose
179 * Returns: zero on success, error code on error
185 * lParam1 = huge pointer to read buffer
186 * lParam2 = number of bytes to read
187 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
189 * NOTE: lDiskOffset should be updated
191 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
195 case MMIOM_WRITEFLUSH:
196 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
199 * lParam1 = huge pointer to write buffer
200 * lParam2 = number of bytes to write
201 * Returns: number of bytes written, -1 for error (error code in
203 * NOTE: lDiskOffset should be updated
205 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
210 * lParam1 = new position
211 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
212 * Returns: new file postion, -1 on error
213 * NOTE: lDiskOffset should be updated
215 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
219 FIXME("unexpected message %u\n", uMessage);
226 /* This array will be the entire list for most apps
227 * Note that temporary ioProcs will be stored with a 0 fourCC code
230 static struct IOProcList defaultProcs[] = {
231 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
232 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
235 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
237 /****************************************************************
238 * MMIO_FindProcNode [INTERNAL]
240 * Finds the ProcList node associated with a given FOURCC code.
242 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
244 struct IOProcList* pListNode;
246 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
247 if (pListNode->fourCC == fccIOProc) {
254 /****************************************************************
255 * MMIO_InstallIOProc [INTERNAL]
257 LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
258 DWORD dwFlags, enum mmioProcType type)
260 LPMMIOPROC lpProc = NULL;
261 struct IOProcList* pListNode;
262 struct IOProcList** ppListNode;
264 TRACE("(%ld, %p, %08lX, %i)\n", fccIOProc, pIOProc, dwFlags, type);
266 if (dwFlags & MMIO_GLOBALPROC)
267 FIXME("Global procedures not implemented\n");
269 /* just handle the known procedures for now */
270 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
271 case MMIO_INSTALLPROC:
272 /* Create new entry for the IOProc list */
273 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
275 /* Fill in this node */
276 pListNode->fourCC = fccIOProc;
277 pListNode->pIOProc = pIOProc;
278 pListNode->type = type;
279 pListNode->count = 0;
281 /* Stick it on the end of the list */
282 pListNode->pNext = pIOProcListAnchor;
283 pIOProcListAnchor = pListNode;
285 /* Return this IOProc - that's how the caller knows we succeeded */
290 case MMIO_REMOVEPROC:
292 * Search for the node that we're trying to remove
293 * We search for a matching fourCC code if it's non null, or the proc
295 * note that this method won't find the first item on the list, but
296 * since the first two items on this list are ones we won't
297 * let the user delete anyway, that's okay
299 ppListNode = &pIOProcListAnchor;
300 while ((*ppListNode) &&
302 (*ppListNode)->fourCC != fccIOProc :
303 (*ppListNode)->pIOProc != pIOProc))
304 ppListNode = &((*ppListNode)->pNext);
306 if (*ppListNode) { /* found it */
307 /* FIXME: what should be done if an open mmio object uses this proc ?
308 * shall we return an error, nuke the mmio object ?
310 if ((*ppListNode)->count) {
311 ERR("Cannot remove a mmIOProc while in use\n");
314 /* remove it, but only if it isn't builtin */
315 if ((*ppListNode) >= defaultProcs &&
316 (*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
317 WARN("Tried to remove built-in mmio proc. Skipping\n");
320 struct IOProcList* ptmpNode = *ppListNode;
321 lpProc = (*ppListNode)->pIOProc;
322 *ppListNode = (*ppListNode)->pNext;
323 HeapFree(GetProcessHeap(), 0, ptmpNode);
329 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
330 lpProc = pListNode->pIOProc;
338 /****************************************************************
339 * send_message [INTERNAL]
341 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
342 DWORD wMsg, LPARAM lParam1,
343 LPARAM lParam2, enum mmioProcType type)
345 LRESULT result = MMSYSERR_ERROR;
346 LPARAM lp1 = lParam1, lp2 = lParam2;
350 result = MMSYSERR_INVALPARAM;
353 switch (ioProc->type) {
355 if (pFnMmioCallback16)
356 result = pFnMmioCallback16((SEGPTR)ioProc->pIOProc,
357 mmioinfo, wMsg, lp1, lp2);
361 if (ioProc->type != type) {
362 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
363 FIXME("NIY 32 A<=>W mapping\n");
365 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
368 if (ioProc->type != type) {
369 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
374 FIXME("Internal error\n");
380 /**************************************************************************
381 * MMIO_ParseExtA [internal]
383 * Parses a filename for the extension.
386 * The FOURCC code for the extension if found, else 0.
388 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
390 /* Filenames are of the form file.ext+ABC
391 FIXME: What if a '+' is part of the file name?
392 For now, we take the last '+' present */
396 /* Note that ext{Start,End} point to the . and + respectively */
399 TRACE("(%s)\n", debugstr_a(szFileName));
403 extEnd = strrchr(szFileName,'+');
405 /* Need to parse to find the extension */
409 while (extStart >= szFileName && extStart[0] != '.') {
413 if (extStart < szFileName) {
414 ERR("+ but no . in szFileName: %s\n", debugstr_a(szFileName));
418 if (extEnd - extStart - 1 > 4)
419 WARN("Extension length > 4\n");
420 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
421 TRACE("Got extension: %s\n", debugstr_a(ext));
422 /* FOURCC codes identifying file-extensions must be uppercase */
423 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
429 /**************************************************************************
430 * MMIO_Get [internal]
432 * Retrieves the mmio object from current process
434 LPWINE_MMIO MMIO_Get(HMMIO h)
436 LPWINE_MMIO wm = NULL;
438 EnterCriticalSection(&WINMM_IData->cs);
439 for (wm = WINMM_IData->lpMMIO; wm; wm = wm->lpNext) {
440 if (wm->info.hmmio == h)
443 LeaveCriticalSection(&WINMM_IData->cs);
447 /**************************************************************************
448 * MMIO_Create [internal]
450 * Creates an internal representation for a mmio instance
452 static LPWINE_MMIO MMIO_Create(void)
454 static WORD MMIO_counter = 0;
457 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
459 EnterCriticalSection(&WINMM_IData->cs);
460 /* lookup next unallocated WORD handle, with a non NULL value */
461 while (++MMIO_counter == 0 || MMIO_Get(HMMIO_32(MMIO_counter)));
462 wm->info.hmmio = HMMIO_32(MMIO_counter);
463 wm->lpNext = WINMM_IData->lpMMIO;
464 WINMM_IData->lpMMIO = wm;
465 LeaveCriticalSection(&WINMM_IData->cs);
470 /**************************************************************************
471 * MMIO_Destroy [internal]
473 * Destroys an internal representation for a mmio instance
475 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
479 EnterCriticalSection(&WINMM_IData->cs);
480 /* search for the matching one... */
481 m = &(WINMM_IData->lpMMIO);
482 while (*m && *m != wm) m = &(*m)->lpNext;
486 HeapFree(GetProcessHeap(), 0, wm);
489 LeaveCriticalSection(&WINMM_IData->cs);
490 return wm ? FALSE : TRUE;
493 /****************************************************************
494 * MMIO_Flush [INTERNAL]
496 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
498 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
499 /* not quite sure what to do here, but I'll guess */
500 if (wm->info.dwFlags & MMIO_DIRTY) {
501 /* FIXME: error handling */
502 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
503 wm->info.lBufOffset, SEEK_SET, MMIO_PROC_32A);
504 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
505 (LPARAM)wm->info.pchBuffer,
506 wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
508 if (uFlags & MMIO_EMPTYBUF)
509 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
511 wm->info.dwFlags &= ~MMIO_DIRTY;
513 return MMSYSERR_NOERROR;
516 /***************************************************************************
517 * MMIO_GrabNextBuffer [INTERNAL]
519 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
521 LONG size = wm->info.cchBuffer;
523 TRACE("bo=%lx do=%lx of=%lx\n",
524 wm->info.lBufOffset, wm->info.lDiskOffset,
525 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
527 wm->info.lBufOffset = wm->info.lDiskOffset;
528 wm->info.pchNext = wm->info.pchBuffer;
529 wm->info.pchEndRead = wm->info.pchBuffer;
530 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
533 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
534 (LPARAM)wm->info.pchBuffer, size, MMIO_PROC_32A);
536 wm->info.pchEndRead += size;
539 wm->bBufferLoaded = TRUE;
543 /***************************************************************************
544 * MMIO_SetBuffer [INTERNAL]
546 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
549 TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
551 if (uFlags) return MMSYSERR_INVALPARAM;
552 if (cchBuffer > 0xFFFF)
553 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
555 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
556 return MMIOERR_CANNOTWRITE;
558 /* free previous buffer if allocated */
559 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
560 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
561 wm->info.pchBuffer = NULL;
562 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
565 /* free segmented ptr mapping, if any */
566 if (wm->info.dwReserved1 != 0L)
568 UnMapLS(wm->info.dwReserved1);
569 wm->info.dwReserved1 = 0L;
573 wm->info.pchBuffer = pchBuffer;
574 } else if (cchBuffer) {
575 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
576 return MMIOERR_OUTOFMEMORY;
577 wm->info.dwFlags |= MMIO_ALLOCBUF;
579 wm->info.pchBuffer = NULL;
582 wm->info.cchBuffer = cchBuffer;
583 wm->info.pchNext = wm->info.pchBuffer;
584 wm->info.pchEndRead = wm->info.pchBuffer;
585 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
586 wm->info.lBufOffset = 0;
587 wm->bBufferLoaded = FALSE;
589 return MMSYSERR_NOERROR;
592 /**************************************************************************
593 * MMIO_Open [internal]
595 HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags,
596 enum mmioProcType type)
601 TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
604 refmminfo = &mmioinfo;
606 mmioinfo.fccIOProc = 0;
607 mmioinfo.pIOProc = NULL;
608 mmioinfo.pchBuffer = NULL;
609 mmioinfo.cchBuffer = 0;
610 type = MMIO_PROC_32A;
613 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
614 char buffer[MAX_PATH];
616 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
617 return (HMMIO16)FALSE;
618 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == -1))
620 strcpy(szFileName, buffer);
624 if ((wm = MMIO_Create()) == NULL)
627 /* If both params are NULL, then parse the file name if available */
628 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
629 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
630 /* Handle any unhandled/error case. Assume DOS file */
631 if (wm->info.fccIOProc == 0)
632 wm->info.fccIOProc = FOURCC_DOS;
633 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
634 wm->bTmpIOProc = FALSE;
636 /* if just the four character code is present, look up IO proc */
637 else if (refmminfo->pIOProc == NULL) {
638 wm->info.fccIOProc = refmminfo->fccIOProc;
639 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
640 wm->bTmpIOProc = FALSE;
642 /* if IO proc specified, use it and specified four character code */
644 wm->info.fccIOProc = refmminfo->fccIOProc;
645 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
646 MMIO_INSTALLPROC, type);
647 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
648 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
649 wm->bTmpIOProc = TRUE;
652 wm->bBufferLoaded = FALSE;
655 if (dwOpenFlags & MMIO_ALLOCBUF) {
656 if ((refmminfo->wErrorRet = MMIO_SetBuffer(wm, NULL, MMIO_DEFAULTBUFFER, 0)))
658 } else if (wm->info.fccIOProc == FOURCC_MEM) {
659 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
660 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
662 wm->bBufferLoaded = TRUE;
663 } /* else => unbuffered, wm->info.pchBuffer == NULL */
665 /* see mmioDosIOProc for that one */
666 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
667 wm->info.dwFlags = dwOpenFlags;
669 /* call IO proc to actually open file */
670 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
671 (LPARAM)szFileName, 0, MMIO_PROC_32A);
673 /* grab file size, when possible */
674 wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
676 if (refmminfo->wErrorRet == 0)
677 return wm->info.hmmio;
679 if (wm->ioProc) wm->ioProc->count--;
685 /**************************************************************************
686 * mmioOpenW [WINMM.@]
688 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
692 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
694 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
696 HeapFree(GetProcessHeap(), 0, szFn);
700 /**************************************************************************
701 * mmioOpenA [WINMM.@]
703 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
706 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
709 /**************************************************************************
710 * mmioClose [WINMM.@]
712 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
717 TRACE("(%p, %04X);\n", hmmio, uFlags);
719 if ((wm = MMIO_Get(hmmio)) == NULL)
720 return MMSYSERR_INVALHANDLE;
722 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
725 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE,
726 uFlags, 0, MMIO_PROC_32A);
728 MMIO_SetBuffer(wm, NULL, 0, 0);
733 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
734 MMIO_REMOVEPROC, wm->ioProc->type);
741 /**************************************************************************
744 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
749 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
751 if ((wm = MMIO_Get(hmmio)) == NULL)
754 /* unbuffered case first */
755 if (!wm->info.pchBuffer)
756 return send_message(wm->ioProc, &wm->info, MMIOM_READ,
757 (LPARAM)pch, cch, MMIO_PROC_32A);
759 /* first try from current buffer */
760 if (wm->info.pchNext != wm->info.pchEndRead) {
761 count = wm->info.pchEndRead - wm->info.pchNext;
762 if (count > cch || count < 0) count = cch;
763 memcpy(pch, wm->info.pchNext, count);
764 wm->info.pchNext += count;
770 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
771 assert(wm->info.cchBuffer);
776 size = MMIO_GrabNextBuffer(wm, TRUE);
777 if (size <= 0) break;
778 if (size > cch) size = cch;
779 memcpy(pch, wm->info.pchBuffer, size);
780 wm->info.pchNext += size;
787 TRACE("count=%ld\n", count);
791 /**************************************************************************
792 * mmioWrite [WINMM.@]
794 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
799 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
801 if ((wm = MMIO_Get(hmmio)) == NULL)
804 if (wm->info.cchBuffer) {
809 if (wm->info.pchNext != wm->info.pchEndWrite) {
810 count = wm->info.pchEndWrite - wm->info.pchNext;
811 if (count > cch || count < 0) count = cch;
812 memcpy(wm->info.pchNext, pch, count);
813 wm->info.pchNext += count;
817 wm->info.dwFlags |= MMIO_DIRTY;
819 if (wm->info.fccIOProc == FOURCC_MEM) {
820 if (wm->info.adwInfo[0]) {
821 /* from where would we get the memory handle? */
822 FIXME("memory file expansion not implemented!\n");
828 if (wm->info.pchNext == wm->info.pchEndWrite)
830 MMIO_Flush(wm, MMIO_EMPTYBUF);
831 MMIO_GrabNextBuffer(wm, FALSE);
837 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
838 (LPARAM)pch, cch, MMIO_PROC_32A);
839 wm->info.lBufOffset = wm->info.lDiskOffset;
842 TRACE("bytes written=%ld\n", count);
846 /**************************************************************************
849 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
854 TRACE("(%p, %08lX, %d);\n", hmmio, lOffset, iOrigin);
856 if ((wm = MMIO_Get(hmmio)) == NULL)
857 return MMSYSERR_INVALHANDLE;
859 /* not buffered, direct seek on file */
860 if (!wm->info.pchBuffer)
861 return send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
862 lOffset, iOrigin, MMIO_PROC_32A);
869 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
872 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
878 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
879 /* should check that write mode exists */
880 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
882 wm->info.lBufOffset = offset;
883 wm->info.pchEndRead = wm->info.pchBuffer;
884 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
885 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
886 wm->info.lDiskOffset = wm->dwFileSize;
888 } else if ((wm->info.cchBuffer > 0) &&
889 ((offset < wm->info.lBufOffset) ||
890 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
891 !wm->bBufferLoaded)) {
892 /* stay in same buffer ? */
893 /* some memory mapped buffers are defined with -1 as a size */
895 /* condition to change buffer */
896 if ((wm->info.fccIOProc == FOURCC_MEM) ||
897 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
898 /* this also sets the wm->info.lDiskOffset field */
899 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
900 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
901 SEEK_SET, MMIO_PROC_32A) == -1)
903 MMIO_GrabNextBuffer(wm, TRUE);
906 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
908 TRACE("=> %ld\n", offset);
912 /**************************************************************************
913 * mmioGetInfo [WINMM.@]
915 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
919 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
921 if ((wm = MMIO_Get(hmmio)) == NULL)
922 return MMSYSERR_INVALHANDLE;
924 memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
925 /* don't expose 16 bit ioproc:s */
926 if (wm->ioProc->type != MMIO_PROC_16)
927 lpmmioinfo->pIOProc = wm->ioProc->pIOProc;
929 return MMSYSERR_NOERROR;
932 /**************************************************************************
933 * mmioSetInfo [WINMM.@]
935 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
939 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
941 if ((wm = MMIO_Get(hmmio)) == NULL)
942 return MMSYSERR_INVALHANDLE;
944 /* check pointers coherence */
945 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
946 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
947 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
948 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
949 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
950 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
951 return MMSYSERR_INVALPARAM;
953 wm->info.pchNext = lpmmioinfo->pchNext;
954 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
956 return MMSYSERR_NOERROR;
959 /**************************************************************************
960 * mmioSetBuffer [WINMM.@]
962 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
966 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
967 hmmio, pchBuffer, cchBuffer, uFlags);
969 if ((wm = MMIO_Get(hmmio)) == NULL)
970 return MMSYSERR_INVALHANDLE;
972 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
975 /**************************************************************************
976 * mmioFlush [WINMM.@]
978 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
982 TRACE("(%p, %04X)\n", hmmio, uFlags);
984 if ((wm = MMIO_Get(hmmio)) == NULL)
985 return MMSYSERR_INVALHANDLE;
987 return MMIO_Flush(wm, uFlags);
990 /**************************************************************************
991 * mmioAdvance [WINMM.@]
993 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
997 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
999 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1000 * here. be sure if you change something here to check mmioAdvance16 as well
1002 if ((wm = MMIO_Get(hmmio)) == NULL)
1003 return MMSYSERR_INVALHANDLE;
1005 if (!wm->info.cchBuffer)
1006 return MMIOERR_UNBUFFERED;
1008 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1009 return MMSYSERR_INVALPARAM;
1011 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1013 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
1014 lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1015 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
1016 (LPARAM)lpmmioinfo->pchBuffer,
1017 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1018 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1020 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1021 return MMIOERR_CANNOTWRITE;
1024 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1025 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1027 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1030 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1031 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1032 (wm->info.pchEndRead - wm->info.pchBuffer);
1033 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1034 (wm->info.pchEndWrite - wm->info.pchBuffer);
1035 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1036 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1038 return MMSYSERR_NOERROR;
1041 /**************************************************************************
1042 * mmioStringToFOURCCA [WINMM.@]
1044 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1049 for (i = 0; i < 4 && sz[i]; i++) {
1050 if (uFlags & MMIO_TOUPPER) {
1051 cc[i] = toupper(sz[i]);
1057 /* Pad with spaces */
1058 while (i < 4) cc[i++] = ' ';
1060 TRACE("Got '%.4s'\n",cc);
1061 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1064 /**************************************************************************
1065 * mmioStringToFOURCCW [WINMM.@]
1067 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1069 LPSTR szA = HEAP_strdupWtoA(GetProcessHeap(),0,sz);
1070 FOURCC ret = mmioStringToFOURCCA(szA,uFlags);
1072 HeapFree(GetProcessHeap(), 0, szA);
1076 /**************************************************************************
1077 * mmioInstallIOProcA [WINMM.@]
1079 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1080 LPMMIOPROC pIOProc, DWORD dwFlags)
1082 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1085 /**************************************************************************
1086 * mmioInstallIOProcW [WINMM.@]
1088 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1089 LPMMIOPROC pIOProc, DWORD dwFlags)
1091 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1094 /******************************************************************
1099 LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1100 LPARAM lParam2, enum mmioProcType type)
1104 TRACE("(%p, %u, %ld, %ld, %d)\n", hmmio, uMessage, lParam1, lParam2, type);
1106 if (uMessage < MMIOM_USER)
1107 return MMSYSERR_INVALPARAM;
1109 if ((wm = MMIO_Get(hmmio)) == NULL)
1110 return MMSYSERR_INVALHANDLE;
1112 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, type);
1115 /**************************************************************************
1116 * mmioSendMessage [WINMM.@]
1118 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1119 LPARAM lParam1, LPARAM lParam2)
1121 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1124 /**************************************************************************
1125 * mmioDescend [WINMM.@]
1127 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1128 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=%ld\n", dwOldPos);
1143 if (lpckParent != NULL) {
1144 TRACE("seek inside parent at %ld !\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.
1158 /* find_chunk looks for 'ckid' */
1159 if (uFlags & MMIO_FINDCHUNK)
1160 srchCkId = lpck->ckid;
1161 /* find_riff and find_list look for 'fccType' */
1162 if (uFlags & MMIO_FINDLIST) {
1163 srchCkId = FOURCC_LIST;
1164 srchType = lpck->fccType;
1166 if (uFlags & MMIO_FINDRIFF) {
1167 srchCkId = FOURCC_RIFF;
1168 srchType = lpck->fccType;
1171 if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1172 TRACE("searching for %.4s.%.4s\n",
1174 srchType?(LPSTR)&srchType:"any ");
1179 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1180 if (ix < 2*sizeof(DWORD)) {
1181 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1182 WARN("return ChunkNotFound\n");
1183 return MMIOERR_CHUNKNOTFOUND;
1185 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1186 if (ix < lpck->dwDataOffset - dwOldPos) {
1187 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1188 WARN("return ChunkNotFound\n");
1189 return MMIOERR_CHUNKNOTFOUND;
1191 TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1193 srchType?(LPSTR)&lpck->fccType:"<na>",
1195 if ((srchCkId == lpck->ckid) &&
1196 (!srchType || (srchType == lpck->fccType))
1200 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1201 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1204 /* FIXME: unverified, does it do this? */
1205 /* NB: This part is used by WAVE_mciOpen, among others */
1206 if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1207 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1208 WARN("return ChunkNotFound 2nd\n");
1209 return MMIOERR_CHUNKNOTFOUND;
1211 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1214 /* If we were looking for RIFF/LIST chunks, the final file position
1215 * is after the chunkid. If we were just looking for the chunk
1216 * it is after the cksize. So add 4 in RIFF/LIST case.
1218 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1219 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1221 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1222 TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
1223 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1224 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1225 return MMSYSERR_NOERROR;
1228 /**************************************************************************
1229 * mmioAscend [WINMM.@]
1231 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1233 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1235 if (lpck->dwFlags & MMIO_DIRTY) {
1236 DWORD dwOldPos, dwNewSize;
1238 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1239 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1240 TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1241 dwNewSize = dwOldPos - lpck->dwDataOffset;
1242 if (dwNewSize != lpck->cksize) {
1243 TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1244 lpck->cksize = dwNewSize;
1246 /* pad odd size with 0 */
1247 if (dwNewSize & 1) {
1249 mmioWrite(hmmio, &ch, 1);
1251 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1252 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1257 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1259 return MMSYSERR_NOERROR;
1262 /**************************************************************************
1263 * mmioCreateChunk [WINMM.@]
1265 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1271 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1273 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1274 TRACE("dwOldPos=%ld\n", dwOldPos);
1276 if (uFlags == MMIO_CREATELIST)
1277 lpck->ckid = FOURCC_LIST;
1278 else if (uFlags == MMIO_CREATERIFF)
1279 lpck->ckid = FOURCC_RIFF;
1281 TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1283 size = 2 * sizeof(DWORD);
1284 lpck->dwDataOffset = dwOldPos + size;
1286 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1287 size += sizeof(DWORD);
1288 lpck->dwFlags = MMIO_DIRTY;
1290 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1291 TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
1293 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1294 WARN("return CannotWrite\n");
1295 return MMIOERR_CANNOTWRITE;
1298 return MMSYSERR_NOERROR;
1301 /**************************************************************************
1302 * mmioRenameA [WINMM.@]
1304 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1305 MMIOINFO* lpmmioinfo, DWORD dwFlags)
1307 struct IOProcList* ioProc = NULL;
1308 struct IOProcList tmp;
1310 TRACE("('%s', '%s', %p, %08lX);\n",
1311 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1313 /* If both params are NULL, then parse the file name */
1314 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1315 lpmmioinfo->fccIOProc = MMIO_ParseExtA(szFileName);
1317 /* Handle any unhandled/error case from above. Assume DOS file */
1318 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1319 ioProc = MMIO_FindProcNode(FOURCC_DOS);
1320 /* if just the four character code is present, look up IO proc */
1321 else if (lpmmioinfo->pIOProc == NULL)
1322 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1323 else /* use relevant ioProc */
1326 tmp.fourCC = lpmmioinfo->fccIOProc;
1327 tmp.pIOProc = lpmmioinfo->pIOProc;
1328 tmp.type = MMIO_PROC_32A;
1332 return send_message(ioProc, lpmmioinfo, MMIOM_RENAME,
1333 (LPARAM)szFileName, (LPARAM)szNewFileName, MMIO_PROC_32A);
1336 /**************************************************************************
1337 * mmioRenameW [WINMM.@]
1339 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1340 MMIOINFO* lpmmioinfo, DWORD dwFlags)
1342 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
1343 LPSTR sznFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szNewFileName);
1344 UINT ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1346 HeapFree(GetProcessHeap(),0,szFn);
1347 HeapFree(GetProcessHeap(),0,sznFn);