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] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
81 if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
82 ret = MMIOERR_CANNOTOPEN;
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 */
386 TRACE("(%s)\n", debugstr_a(szFileName));
391 /* Find the last '.' */
392 extStart = strrchr(szFileName,'.');
395 ERR("No . in szFileName: %s\n", debugstr_a(szFileName));
399 /* Find the '+' afterwards */
400 extEnd = strchr(extStart,'+');
403 if (extEnd - extStart - 1 > 4)
404 WARN("Extension length > 4\n");
405 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
408 /* No + so just an extension */
409 if (strlen(extStart) > 4) {
410 WARN("Extension length > 4\n");
412 lstrcpynA(ext, extStart + 1, 5);
414 TRACE("Got extension: %s\n", debugstr_a(ext));
416 /* FOURCC codes identifying file-extensions must be uppercase */
417 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
422 /**************************************************************************
423 * MMIO_Get [internal]
425 * Retrieves the mmio object from current process
427 static LPWINE_MMIO MMIO_Get(HMMIO h)
429 LPWINE_MMIO wm = NULL;
431 EnterCriticalSection(&WINMM_cs);
432 for (wm = MMIOList; wm; wm = wm->lpNext) {
433 if (wm->info.hmmio == h)
436 LeaveCriticalSection(&WINMM_cs);
440 /**************************************************************************
441 * MMIO_Create [internal]
443 * Creates an internal representation for a mmio instance
445 static LPWINE_MMIO MMIO_Create(void)
447 static WORD MMIO_counter = 0;
450 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
452 EnterCriticalSection(&WINMM_cs);
453 /* lookup next unallocated WORD handle, with a non NULL value */
454 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
455 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
456 wm->lpNext = MMIOList;
458 LeaveCriticalSection(&WINMM_cs);
463 /**************************************************************************
464 * MMIO_Destroy [internal]
466 * Destroys an internal representation for a mmio instance
468 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
472 EnterCriticalSection(&WINMM_cs);
473 /* search for the matching one... */
475 while (*m && *m != wm) m = &(*m)->lpNext;
479 HeapFree(GetProcessHeap(), 0, wm);
482 LeaveCriticalSection(&WINMM_cs);
483 return wm ? FALSE : TRUE;
486 /****************************************************************
487 * MMIO_Flush [INTERNAL]
489 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
491 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
492 /* not quite sure what to do here, but I'll guess */
493 if (wm->info.dwFlags & MMIO_DIRTY) {
494 /* FIXME: error handling */
495 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, wm->info.lBufOffset, SEEK_SET, FALSE);
496 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
497 (LPARAM)wm->info.pchBuffer,
498 wm->info.pchNext - wm->info.pchBuffer, FALSE);
500 if (uFlags & MMIO_EMPTYBUF)
501 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
503 wm->info.dwFlags &= ~MMIO_DIRTY;
505 return MMSYSERR_NOERROR;
508 /***************************************************************************
509 * MMIO_GrabNextBuffer [INTERNAL]
511 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
513 LONG size = wm->info.cchBuffer;
515 TRACE("bo=%x do=%x of=%lx\n",
516 wm->info.lBufOffset, wm->info.lDiskOffset,
517 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE));
519 wm->info.lBufOffset = wm->info.lDiskOffset;
520 wm->info.pchNext = wm->info.pchBuffer;
521 wm->info.pchEndRead = wm->info.pchBuffer;
522 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
524 wm->bBufferLoaded = TRUE;
526 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
527 (LPARAM)wm->info.pchBuffer, size, FALSE);
529 wm->info.pchEndRead += size;
531 wm->bBufferLoaded = FALSE;
537 /***************************************************************************
538 * MMIO_SetBuffer [INTERNAL]
540 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
543 TRACE("(%p %p %d %u)\n", wm, pchBuffer, cchBuffer, uFlags);
545 if (cchBuffer > 0xFFFF)
546 WARN("Untested handling of huge mmio buffers (%d >= 64k)\n", cchBuffer);
548 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
549 return MMIOERR_CANNOTWRITE;
551 /* free previous buffer if allocated */
552 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
553 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
554 wm->info.pchBuffer = NULL;
555 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
559 wm->info.pchBuffer = pchBuffer;
560 } else if (cchBuffer) {
561 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
562 return MMIOERR_OUTOFMEMORY;
563 wm->info.dwFlags |= MMIO_ALLOCBUF;
565 wm->info.pchBuffer = NULL;
568 wm->info.cchBuffer = cchBuffer;
569 wm->info.pchNext = wm->info.pchBuffer;
570 wm->info.pchEndRead = wm->info.pchBuffer;
571 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
572 wm->info.lBufOffset = wm->info.lDiskOffset;
573 wm->bBufferLoaded = FALSE;
575 return MMSYSERR_NOERROR;
578 /**************************************************************************
579 * MMIO_Open [internal]
581 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, BOOL is_unicode)
586 TRACE("('%s', %p, %08X, %s);\n", szFileName, refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
589 refmminfo = &mmioinfo;
591 mmioinfo.fccIOProc = 0;
592 mmioinfo.pIOProc = NULL;
593 mmioinfo.pchBuffer = NULL;
594 mmioinfo.cchBuffer = 0;
598 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
599 char buffer[MAX_PATH];
603 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
605 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
607 strcpy(szFileName, buffer);
611 if ((wm = MMIO_Create()) == NULL)
614 /* If both params are NULL, then parse the file name if available */
615 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
616 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
617 /* Handle any unhandled/error case. Assume DOS file */
618 if (wm->info.fccIOProc == 0)
619 wm->info.fccIOProc = FOURCC_DOS;
620 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
621 /* If not found, retry with FOURCC_DOS */
622 wm->info.fccIOProc = FOURCC_DOS;
623 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc)))
626 wm->bTmpIOProc = FALSE;
628 /* if just the four character code is present, look up IO proc */
629 else if (refmminfo->pIOProc == NULL) {
630 wm->info.fccIOProc = refmminfo->fccIOProc;
631 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
632 wm->bTmpIOProc = FALSE;
634 /* if IO proc specified, use it and specified four character code */
636 wm->info.fccIOProc = refmminfo->fccIOProc;
637 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
638 MMIO_INSTALLPROC, is_unicode);
639 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
640 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
641 wm->bTmpIOProc = TRUE;
645 wm->info.dwFlags = dwOpenFlags;
647 if (dwOpenFlags & MMIO_ALLOCBUF) {
648 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
649 refmminfo->cchBuffer ? refmminfo->cchBuffer : MMIO_DEFAULTBUFFER, 0);
650 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
653 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
654 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
658 if (wm->info.fccIOProc == FOURCC_MEM && !(wm->info.dwFlags & MMIO_ALLOCBUF))
659 wm->bBufferLoaded = TRUE;
661 /* see mmioDosIOProc for that one */
662 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
664 /* call IO proc to actually open file */
665 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
666 (LPARAM)szFileName, 0, FALSE);
668 /* grab file size, when possible */
669 wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
671 if (refmminfo->wErrorRet == 0)
672 return wm->info.hmmio;
674 if (wm->ioProc) wm->ioProc->count--;
680 /**************************************************************************
681 * mmioOpenW [WINMM.@]
683 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
691 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
692 szFn = HeapAlloc( GetProcessHeap(), 0, len );
693 if (!szFn) return NULL;
694 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
697 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
699 HeapFree(GetProcessHeap(), 0, szFn);
703 /**************************************************************************
704 * mmioOpenA [WINMM.@]
706 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
709 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
712 /**************************************************************************
713 * mmioClose [WINMM.@]
715 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
720 TRACE("(%p, %04X);\n", hmmio, uFlags);
722 if ((wm = MMIO_Get(hmmio)) == NULL)
723 return MMSYSERR_INVALHANDLE;
725 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
728 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
730 MMIO_SetBuffer(wm, NULL, 0, 0);
735 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
736 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
743 /**************************************************************************
746 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
751 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
753 if ((wm = MMIO_Get(hmmio)) == NULL)
756 /* unbuffered case first */
757 if (!wm->info.pchBuffer)
758 return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
760 /* first try from current buffer */
761 if (wm->info.pchNext != wm->info.pchEndRead) {
762 count = wm->info.pchEndRead - wm->info.pchNext;
763 if (count > cch || count < 0) count = cch;
764 memcpy(pch, wm->info.pchNext, count);
765 wm->info.pchNext += count;
771 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
772 assert(wm->info.cchBuffer);
777 size = MMIO_GrabNextBuffer(wm, TRUE);
778 if (size <= 0) break;
779 if (size > cch) size = cch;
780 memcpy(pch, wm->info.pchBuffer, size);
781 wm->info.pchNext += size;
788 TRACE("count=%d\n", count);
792 /**************************************************************************
793 * mmioWrite [WINMM.@]
795 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
800 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
802 if ((wm = MMIO_Get(hmmio)) == NULL)
805 if (wm->info.cchBuffer) {
810 if (wm->info.pchNext != wm->info.pchEndWrite) {
811 count = wm->info.pchEndWrite - wm->info.pchNext;
812 if (count > cch || count < 0) count = cch;
813 memcpy(wm->info.pchNext, pch, count);
814 wm->info.pchNext += count;
818 wm->info.dwFlags |= MMIO_DIRTY;
820 if (wm->info.fccIOProc == FOURCC_MEM) {
821 if (wm->info.adwInfo[0]) {
822 /* from where would we get the memory handle? */
823 FIXME("memory file expansion not implemented!\n");
829 if (wm->info.pchNext == wm->info.pchEndWrite)
831 MMIO_Flush(wm, MMIO_EMPTYBUF);
832 MMIO_GrabNextBuffer(wm, FALSE);
838 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
839 wm->info.lBufOffset = wm->info.lDiskOffset;
842 TRACE("bytes written=%d\n", count);
846 /**************************************************************************
849 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
854 TRACE("(%p, %08X, %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, lOffset, iOrigin, FALSE);
868 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
871 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
877 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
878 /* should check that write mode exists */
879 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
881 wm->info.lBufOffset = offset;
882 wm->info.pchEndRead = wm->info.pchBuffer;
883 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
884 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
885 wm->info.lDiskOffset = wm->dwFileSize;
887 } else if ((wm->info.cchBuffer > 0) &&
888 ((offset < wm->info.lBufOffset) ||
889 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
890 !wm->bBufferLoaded)) {
891 /* stay in same buffer ? */
892 /* some memory mapped buffers are defined with -1 as a size */
894 /* condition to change buffer */
895 if ((wm->info.fccIOProc == FOURCC_MEM) ||
896 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
897 /* this also sets the wm->info.lDiskOffset field */
898 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
899 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
900 SEEK_SET, FALSE) == -1)
902 MMIO_GrabNextBuffer(wm, TRUE);
905 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
907 TRACE("=> %d\n", offset);
911 /**************************************************************************
912 * mmioGetInfo [WINMM.@]
914 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
918 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
920 if ((wm = MMIO_Get(hmmio)) == NULL)
921 return MMSYSERR_INVALHANDLE;
923 *lpmmioinfo = wm->info;
925 return MMSYSERR_NOERROR;
928 /**************************************************************************
929 * mmioSetInfo [WINMM.@]
931 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
935 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
937 if ((wm = MMIO_Get(hmmio)) == NULL)
938 return MMSYSERR_INVALHANDLE;
940 /* check pointers coherence */
941 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
942 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
943 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
944 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
945 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
946 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
947 return MMSYSERR_INVALPARAM;
949 wm->info.pchNext = lpmmioinfo->pchNext;
950 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
952 return MMSYSERR_NOERROR;
955 /**************************************************************************
956 * mmioSetBuffer [WINMM.@]
958 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
962 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%d, uFlags=%#08x)\n",
963 hmmio, pchBuffer, cchBuffer, uFlags);
965 if ((wm = MMIO_Get(hmmio)) == NULL)
966 return MMSYSERR_INVALHANDLE;
968 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
971 /**************************************************************************
972 * mmioFlush [WINMM.@]
974 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
978 TRACE("(%p, %04X)\n", hmmio, uFlags);
980 if ((wm = MMIO_Get(hmmio)) == NULL)
981 return MMSYSERR_INVALHANDLE;
983 return MMIO_Flush(wm, uFlags);
986 /**************************************************************************
987 * mmioAdvance [WINMM.@]
989 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
993 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
995 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
996 * here. be sure if you change something here to check mmioAdvance16 as well
998 if ((wm = MMIO_Get(hmmio)) == NULL)
999 return MMSYSERR_INVALHANDLE;
1001 if (!wm->info.cchBuffer)
1002 return MMIOERR_UNBUFFERED;
1004 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1005 return MMSYSERR_INVALPARAM;
1007 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1009 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
1010 send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1011 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1012 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1014 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1015 return MMIOERR_CANNOTWRITE;
1018 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1019 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1021 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1024 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1025 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1026 (wm->info.pchEndRead - wm->info.pchBuffer);
1027 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1028 (wm->info.pchEndWrite - wm->info.pchBuffer);
1029 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1030 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1032 return MMSYSERR_NOERROR;
1035 /**************************************************************************
1036 * mmioStringToFOURCCA [WINMM.@]
1038 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1043 for (i = 0; i < 4 && sz[i]; i++) {
1044 if (uFlags & MMIO_TOUPPER) {
1045 cc[i] = toupper(sz[i]);
1051 /* Pad with spaces */
1052 while (i < 4) cc[i++] = ' ';
1054 TRACE("Got '%.4s'\n",cc);
1055 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1058 /**************************************************************************
1059 * mmioStringToFOURCCW [WINMM.@]
1061 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1065 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1066 return mmioStringToFOURCCA(szA,uFlags);
1069 /**************************************************************************
1070 * mmioInstallIOProcA [WINMM.@]
1072 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1073 LPMMIOPROC pIOProc, DWORD dwFlags)
1075 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, FALSE);
1078 /**************************************************************************
1079 * mmioInstallIOProcW [WINMM.@]
1081 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1082 LPMMIOPROC pIOProc, DWORD dwFlags)
1084 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, TRUE);
1087 /******************************************************************
1092 static LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1093 LPARAM lParam2, BOOL is_unicode)
1097 TRACE("(%p, %u, %ld, %ld, %s)\n", hmmio, uMessage, lParam1, lParam2, is_unicode ? "unicode" : "ansi");
1099 if (uMessage < MMIOM_USER)
1100 return MMSYSERR_INVALPARAM;
1102 if ((wm = MMIO_Get(hmmio)) == NULL)
1103 return MMSYSERR_INVALHANDLE;
1105 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, is_unicode);
1108 /**************************************************************************
1109 * mmioSendMessage [WINMM.@]
1111 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1112 LPARAM lParam1, LPARAM lParam2)
1114 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, FALSE);
1117 /**************************************************************************
1118 * mmioDescend [WINMM.@]
1120 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1121 const MMCKINFO* lpckParent, UINT uFlags)
1127 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1130 return MMSYSERR_INVALPARAM;
1132 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1133 TRACE("dwOldPos=%d\n", dwOldPos);
1135 if (lpckParent != NULL) {
1136 TRACE("seek inside parent at %d !\n", lpckParent->dwDataOffset);
1137 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1138 if (dwOldPos < lpckParent->dwDataOffset ||
1139 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1140 WARN("outside parent chunk\n");
1141 return MMIOERR_CHUNKNOTFOUND;
1145 /* The SDK docu says 'ckid' is used for all cases. Real World
1146 * examples disagree -Marcus,990216.
1152 /* find_chunk looks for 'ckid' */
1153 if (uFlags & MMIO_FINDCHUNK)
1154 srchCkId = lpck->ckid;
1156 /* find_riff and find_list look for 'fccType' */
1157 if (uFlags & MMIO_FINDLIST)
1159 srchCkId = FOURCC_LIST;
1160 srchType = lpck->fccType;
1163 if (uFlags & MMIO_FINDRIFF)
1165 srchCkId = FOURCC_RIFF;
1166 srchType = lpck->fccType;
1169 TRACE("searching for %4.4s.%4.4s\n",
1170 (LPCSTR)&srchCkId, srchType ? (LPCSTR)&srchType : "any");
1176 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1177 if (ix < 2*sizeof(DWORD))
1179 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1180 WARN("return ChunkNotFound\n");
1181 return MMIOERR_CHUNKNOTFOUND;
1184 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1185 TRACE("ckid=%4.4s fcc=%4.4s cksize=%08X !\n",
1186 (LPCSTR)&lpck->ckid,
1187 srchType ? (LPCSTR)&lpck->fccType:"<na>",
1189 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1190 (!srchType || (srchType == lpck->fccType)) )
1193 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1194 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1198 /* If we were looking for RIFF/LIST chunks, the final file position
1199 * is after the chunkid. If we were just looking for the chunk
1200 * it is after the cksize. So add 4 in RIFF/LIST case.
1202 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1203 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1206 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1209 TRACE("lpck: ckid=%.4s, cksize=%d, dwDataOffset=%d fccType=%08X (%.4s)!\n",
1210 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1211 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1212 return MMSYSERR_NOERROR;
1215 /**************************************************************************
1216 * mmioAscend [WINMM.@]
1218 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1220 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1222 if (lpck->dwFlags & MMIO_DIRTY) {
1223 DWORD dwOldPos, dwNewSize;
1225 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1226 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1227 TRACE("dwOldPos=%d lpck->dwDataOffset = %d\n", dwOldPos, lpck->dwDataOffset);
1228 dwNewSize = dwOldPos - lpck->dwDataOffset;
1229 if (dwNewSize != lpck->cksize) {
1230 TRACE("Nope: lpck->cksize=%d dwNewSize=%d\n", lpck->cksize, dwNewSize);
1231 lpck->cksize = dwNewSize;
1233 /* pad odd size with 0 */
1234 if (dwNewSize & 1) {
1236 mmioWrite(hmmio, &ch, 1);
1238 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1239 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1244 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1246 return MMSYSERR_NOERROR;
1249 /**************************************************************************
1250 * mmioCreateChunk [WINMM.@]
1252 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1258 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1260 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1261 TRACE("dwOldPos=%d\n", dwOldPos);
1263 if (uFlags == MMIO_CREATELIST)
1264 lpck->ckid = FOURCC_LIST;
1265 else if (uFlags == MMIO_CREATERIFF)
1266 lpck->ckid = FOURCC_RIFF;
1268 TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1270 size = 2 * sizeof(DWORD);
1271 lpck->dwDataOffset = dwOldPos + size;
1273 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1274 size += sizeof(DWORD);
1275 lpck->dwFlags = MMIO_DIRTY;
1277 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1278 TRACE("after mmioWrite ix = %d req = %d, errno = %d\n", ix, size, errno);
1280 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1281 WARN("return CannotWrite\n");
1282 return MMIOERR_CANNOTWRITE;
1285 return MMSYSERR_NOERROR;
1288 /**************************************************************************
1289 * mmioRenameA [WINMM.@]
1291 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1292 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1294 struct IOProcList* ioProc = NULL;
1295 struct IOProcList tmp;
1298 TRACE("('%s', '%s', %p, %08X);\n",
1299 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1301 /* If both params are NULL, then parse the file name */
1302 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1304 fcc = MMIO_ParseExtA(szFileName);
1305 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1308 /* Handle any unhandled/error case from above. Assume DOS file */
1309 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1310 ioProc = MMIO_FindProcNode(FOURCC_DOS);
1311 /* if just the four character code is present, look up IO proc */
1312 else if (lpmmioinfo->pIOProc == NULL)
1313 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1314 else /* use relevant ioProc */
1317 tmp.fourCC = lpmmioinfo->fccIOProc;
1318 tmp.pIOProc = lpmmioinfo->pIOProc;
1319 tmp.is_unicode = FALSE;
1323 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1324 * or make a copy of it because it's const ???
1326 return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1327 (LPARAM)szFileName, (LPARAM)szNewFileName, FALSE);
1330 /**************************************************************************
1331 * mmioRenameW [WINMM.@]
1333 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1334 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1338 UINT ret = MMSYSERR_NOMEM;
1343 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1344 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1345 if (!szFn) goto done;
1346 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1350 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1351 sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1352 if (!sznFn) goto done;
1353 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1356 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1359 HeapFree(GetProcessHeap(),0,szFn);
1360 HeapFree(GetProcessHeap(),0,sznFn);