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 position, -1 on error
130 if (lParam2 == SEEK_END)
131 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], -(LONG)lParam1, (LONG)lParam2);
133 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
135 lpmmioinfo->lDiskOffset = ret;
142 * Returns: zero on success, non-zero on failure
144 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
145 ret = MMIOERR_FILENOTFOUND;
149 FIXME("unexpected message %u\n", uMessage);
156 /**************************************************************************
157 * mmioMemIOProc [internal]
159 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
160 LPARAM lParam1, LPARAM lParam2)
162 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
168 * lParam1 = filename (must be NULL)
170 * Returns: zero on success, error code on error
171 * NOTE: lDiskOffset automatically set to zero
173 /* FIXME: io proc shouldn't change it */
174 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
175 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
176 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
181 * lParam1 = wFlags parameter from mmioClose
183 * Returns: zero on success, error code on error
189 * lParam1 = huge pointer to read buffer
190 * lParam2 = number of bytes to read
191 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
193 * NOTE: lDiskOffset should be updated
195 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
199 case MMIOM_WRITEFLUSH:
200 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
203 * lParam1 = huge pointer to write buffer
204 * lParam2 = number of bytes to write
205 * Returns: number of bytes written, -1 for error (error code in
207 * NOTE: lDiskOffset should be updated
209 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
214 * lParam1 = new position
215 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
216 * Returns: new file position, -1 on error
217 * NOTE: lDiskOffset should be updated
219 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
223 FIXME("unexpected message %u\n", uMessage);
228 /* This array will be the entire list for most apps
229 * Note that temporary ioProcs will be stored with a 0 fourCC code
232 static struct IOProcList defaultProcs[] = {
233 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, FALSE, 0},
234 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, FALSE, 0},
237 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
239 /****************************************************************
240 * MMIO_FindProcNode [INTERNAL]
242 * Finds the ProcList node associated with a given FOURCC code.
244 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
246 struct IOProcList* pListNode;
248 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
249 if (pListNode->fourCC == fccIOProc) {
256 /****************************************************************
257 * MMIO_InstallIOProc [INTERNAL]
259 static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
260 DWORD dwFlags, BOOL is_unicode)
262 LPMMIOPROC lpProc = NULL;
263 struct IOProcList* pListNode;
264 struct IOProcList** ppListNode;
266 TRACE("(%08x, %p, %08X, %s)\n", fccIOProc, pIOProc, dwFlags, is_unicode ? "unicode" : "ansi");
268 if (dwFlags & MMIO_GLOBALPROC)
269 FIXME("Global procedures not implemented\n");
271 /* just handle the known procedures for now */
272 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
273 case MMIO_INSTALLPROC:
274 /* Create new entry for the IOProc list */
275 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
277 /* Fill in this node */
278 pListNode->fourCC = fccIOProc;
279 pListNode->pIOProc = pIOProc;
280 pListNode->is_unicode = is_unicode;
281 pListNode->count = 0;
283 /* Stick it on the end of the list */
284 pListNode->pNext = pIOProcListAnchor;
285 pIOProcListAnchor = pListNode;
287 /* Return this IOProc - that's how the caller knows we succeeded */
292 case MMIO_REMOVEPROC:
294 * Search for the node that we're trying to remove
295 * We search for a matching fourCC code if it's non null, or the proc
297 * note that this method won't find the first item on the list, but
298 * since the first two items on this list are ones we won't
299 * let the user delete anyway, that's okay
301 ppListNode = &pIOProcListAnchor;
302 while ((*ppListNode) &&
304 (*ppListNode)->fourCC != fccIOProc :
305 (*ppListNode)->pIOProc != pIOProc))
306 ppListNode = &((*ppListNode)->pNext);
308 if (*ppListNode) { /* found it */
309 /* FIXME: what should be done if an open mmio object uses this proc ?
310 * shall we return an error, nuke the mmio object ?
312 if ((*ppListNode)->count) {
313 ERR("Cannot remove a mmIOProc while in use\n");
316 /* remove it, but only if it isn't builtin */
317 if ((*ppListNode) >= defaultProcs &&
318 (*ppListNode) < defaultProcs + sizeof(defaultProcs) / sizeof(defaultProcs[0])) {
319 WARN("Tried to remove built-in mmio proc. Skipping\n");
322 struct IOProcList* ptmpNode = *ppListNode;
323 lpProc = (*ppListNode)->pIOProc;
324 *ppListNode = (*ppListNode)->pNext;
325 HeapFree(GetProcessHeap(), 0, ptmpNode);
331 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
332 lpProc = pListNode->pIOProc;
340 /****************************************************************
341 * send_message [INTERNAL]
343 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
344 DWORD wMsg, LPARAM lParam1,
345 LPARAM lParam2, BOOL is_unicode)
347 LRESULT result = MMSYSERR_ERROR;
348 LPARAM lp1 = lParam1, lp2 = lParam2;
351 ERR("ioProc NULL\n");
352 return MMSYSERR_INVALPARAM;
355 if (ioProc->is_unicode != is_unicode) {
356 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
357 FIXME("NIY 32 A<=>W mapping\n");
359 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
362 if (ioProc->is_unicode != is_unicode) {
363 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
370 /**************************************************************************
371 * MMIO_ParseExtA [internal]
373 * Parses a filename for the extension.
376 * The FOURCC code for the extension if found, else 0.
378 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
380 /* Filenames are of the form file.ext{+ABC}
381 For now, we take the last '+' if present */
385 /* Note that ext{Start,End} point to the . and + respectively */
391 TRACE("(%s)\n", debugstr_a(szFileName));
396 /* Find the last '+' */
397 extEnd = strrchr(szFileName,'+');
400 /* No + so just an extension */
403 /* Find the first '.' before '+' */
404 extStart = extEnd - 1;
405 while (extStart >= szFileName && *extStart != '.') {
408 if (extStart < szFileName) {
409 ERR("No extension in szFileName: %s\n", debugstr_a(szFileName));
414 if (extEnd - extStart - 1 > 4)
415 WARN("Extension length > 4\n");
416 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
418 TRACE("Got extension: %s\n", debugstr_a(ext));
420 /* FOURCC codes identifying file-extensions must be uppercase */
421 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
426 /**************************************************************************
427 * MMIO_Get [internal]
429 * Retrieves the mmio object from current process
431 static LPWINE_MMIO MMIO_Get(HMMIO h)
433 LPWINE_MMIO wm = NULL;
435 EnterCriticalSection(&WINMM_cs);
436 for (wm = MMIOList; wm; wm = wm->lpNext) {
437 if (wm->info.hmmio == h)
440 LeaveCriticalSection(&WINMM_cs);
444 /**************************************************************************
445 * MMIO_Create [internal]
447 * Creates an internal representation for a mmio instance
449 static LPWINE_MMIO MMIO_Create(void)
451 static WORD MMIO_counter = 0;
454 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
456 EnterCriticalSection(&WINMM_cs);
457 /* lookup next unallocated WORD handle, with a non NULL value */
458 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
459 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
460 wm->lpNext = MMIOList;
462 LeaveCriticalSection(&WINMM_cs);
467 /**************************************************************************
468 * MMIO_Destroy [internal]
470 * Destroys an internal representation for a mmio instance
472 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
476 EnterCriticalSection(&WINMM_cs);
477 /* search for the matching one... */
479 while (*m && *m != wm) m = &(*m)->lpNext;
483 HeapFree(GetProcessHeap(), 0, wm);
486 LeaveCriticalSection(&WINMM_cs);
490 /****************************************************************
491 * MMIO_Flush [INTERNAL]
493 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
495 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
496 /* not quite sure what to do here, but I'll guess */
497 if (wm->info.dwFlags & MMIO_DIRTY) {
498 /* FIXME: error handling */
499 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, wm->info.lBufOffset, SEEK_SET, FALSE);
500 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
501 (LPARAM)wm->info.pchBuffer,
502 wm->info.pchNext - wm->info.pchBuffer, FALSE);
504 if (uFlags & MMIO_EMPTYBUF)
505 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
507 wm->info.dwFlags &= ~MMIO_DIRTY;
509 return MMSYSERR_NOERROR;
512 /***************************************************************************
513 * MMIO_GrabNextBuffer [INTERNAL]
515 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
517 LONG size = wm->info.cchBuffer;
519 TRACE("bo=%x do=%x of=%lx\n",
520 wm->info.lBufOffset, wm->info.lDiskOffset,
521 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE));
523 wm->info.lBufOffset = wm->info.lDiskOffset;
524 wm->info.pchNext = wm->info.pchBuffer;
525 wm->info.pchEndRead = wm->info.pchBuffer;
526 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
528 wm->bBufferLoaded = TRUE;
530 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
531 (LPARAM)wm->info.pchBuffer, size, FALSE);
533 wm->info.pchEndRead += size;
535 wm->bBufferLoaded = FALSE;
541 /***************************************************************************
542 * MMIO_SetBuffer [INTERNAL]
544 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
547 TRACE("(%p %p %d %u)\n", wm, pchBuffer, cchBuffer, uFlags);
549 if (cchBuffer > 0xFFFF)
550 WARN("Untested handling of huge mmio buffers (%d >= 64k)\n", cchBuffer);
552 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
553 return MMIOERR_CANNOTWRITE;
555 /* free previous buffer if allocated */
556 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
557 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
558 wm->info.pchBuffer = NULL;
559 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
563 wm->info.pchBuffer = pchBuffer;
564 } else if (cchBuffer) {
565 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
566 return MMIOERR_OUTOFMEMORY;
567 wm->info.dwFlags |= MMIO_ALLOCBUF;
569 wm->info.pchBuffer = NULL;
572 wm->info.cchBuffer = cchBuffer;
573 wm->info.pchNext = wm->info.pchBuffer;
574 wm->info.pchEndRead = wm->info.pchBuffer;
575 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
576 wm->info.lBufOffset = wm->info.lDiskOffset;
577 wm->bBufferLoaded = FALSE;
579 return MMSYSERR_NOERROR;
582 /**************************************************************************
583 * MMIO_Open [internal]
585 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, BOOL is_unicode)
591 TRACE("(%s, %p, %08X, %s);\n", debugstr_a(szFileName), refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
594 refmminfo = &mmioinfo;
596 mmioinfo.fccIOProc = 0;
597 mmioinfo.pIOProc = NULL;
598 mmioinfo.pchBuffer = NULL;
599 mmioinfo.cchBuffer = 0;
603 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
604 char buffer[MAX_PATH];
608 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
610 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
612 strcpy(szFileName, buffer);
616 if ((wm = MMIO_Create()) == NULL)
619 /* If both params are NULL, then parse the file name if available */
620 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
621 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
622 /* Handle any unhandled/error case. Assume DOS file */
623 if (wm->info.fccIOProc == 0) {
624 wm->info.fccIOProc = FOURCC_DOS;
625 wm->ioProc = &defaultProcs[0];
627 else if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
628 /* If not found, assume DOS file */
629 wm->ioProc = &defaultProcs[0];
631 wm->bTmpIOProc = FALSE;
633 /* if just the four character code is present, look up IO proc */
634 else if (refmminfo->pIOProc == NULL) {
635 wm->info.fccIOProc = refmminfo->fccIOProc;
636 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
637 wm->bTmpIOProc = FALSE;
639 /* if IO proc specified, use it and specified four character code */
641 wm->info.fccIOProc = refmminfo->fccIOProc;
642 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
643 MMIO_INSTALLPROC, is_unicode);
644 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
645 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
646 wm->bTmpIOProc = TRUE;
650 wm->info.dwFlags = dwOpenFlags;
652 if (dwOpenFlags & MMIO_ALLOCBUF) {
653 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
654 refmminfo->cchBuffer ? refmminfo->cchBuffer : MMIO_DEFAULTBUFFER, 0);
655 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
658 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
659 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
663 if (wm->info.fccIOProc == FOURCC_MEM && !(wm->info.dwFlags & MMIO_ALLOCBUF))
664 wm->bBufferLoaded = TRUE;
666 /* see mmioDosIOProc for that one */
667 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
669 /* call IO proc to actually open file */
670 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
671 (LPARAM)szFileName, 0, FALSE);
673 /* grab file size, when possible */
674 if (wm->info.fccIOProc != FOURCC_MEM && (send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE)) != -1) {
675 pos = wm->info.lDiskOffset;
676 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_END, FALSE);
677 wm->dwFileSize = wm->info.lDiskOffset;
678 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, pos, SEEK_SET, FALSE);
680 else wm->dwFileSize = 0;
682 if (refmminfo->wErrorRet == 0)
683 return wm->info.hmmio;
685 if (wm->info.dwFlags & MMIO_ALLOCBUF)
686 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
687 if (wm->ioProc) wm->ioProc->count--;
693 /**************************************************************************
694 * mmioOpenW [WINMM.@]
696 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
704 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
705 szFn = HeapAlloc( GetProcessHeap(), 0, len );
706 if (!szFn) return NULL;
707 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
710 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
712 HeapFree(GetProcessHeap(), 0, szFn);
716 /**************************************************************************
717 * mmioOpenA [WINMM.@]
719 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
722 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
725 /**************************************************************************
726 * mmioClose [WINMM.@]
728 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
733 TRACE("(%p, %04X);\n", hmmio, uFlags);
735 if ((wm = MMIO_Get(hmmio)) == NULL)
736 return MMSYSERR_INVALHANDLE;
738 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
741 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
743 MMIO_SetBuffer(wm, NULL, 0, 0);
748 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
749 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
756 /**************************************************************************
759 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
764 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
766 if ((wm = MMIO_Get(hmmio)) == NULL)
769 /* unbuffered case first */
770 if (!wm->info.pchBuffer)
771 return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
773 /* first try from current buffer */
774 if (wm->info.pchNext != wm->info.pchEndRead) {
775 count = wm->info.pchEndRead - wm->info.pchNext;
776 if (count > cch || count < 0) count = cch;
777 memcpy(pch, wm->info.pchNext, count);
778 wm->info.pchNext += count;
784 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
785 assert(wm->info.cchBuffer);
790 size = MMIO_GrabNextBuffer(wm, TRUE);
791 if (size <= 0) break;
792 if (size > cch) size = cch;
793 memcpy(pch, wm->info.pchBuffer, size);
794 wm->info.pchNext += size;
801 TRACE("count=%d\n", count);
805 /**************************************************************************
806 * mmioWrite [WINMM.@]
808 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
813 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
815 if ((wm = MMIO_Get(hmmio)) == NULL)
818 if (wm->info.cchBuffer) {
823 if (wm->info.pchNext != wm->info.pchEndWrite) {
824 count = wm->info.pchEndWrite - wm->info.pchNext;
825 if (count > cch || count < 0) count = cch;
826 memcpy(wm->info.pchNext, pch, count);
827 wm->info.pchNext += count;
831 wm->info.dwFlags |= MMIO_DIRTY;
833 if (wm->info.fccIOProc == FOURCC_MEM) {
834 if (wm->info.adwInfo[0]) {
835 /* from where would we get the memory handle? */
836 FIXME("memory file expansion not implemented!\n");
842 if (wm->info.pchNext == wm->info.pchEndWrite)
844 MMIO_Flush(wm, MMIO_EMPTYBUF);
845 MMIO_GrabNextBuffer(wm, FALSE);
851 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
852 wm->info.lBufOffset = wm->info.lDiskOffset;
855 TRACE("bytes written=%d\n", count);
859 /**************************************************************************
862 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
867 TRACE("(%p, %08X, %d);\n", hmmio, lOffset, iOrigin);
869 if ((wm = MMIO_Get(hmmio)) == NULL)
870 return MMSYSERR_INVALHANDLE;
872 /* not buffered, direct seek on file */
873 if (!wm->info.pchBuffer)
874 return send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, iOrigin, FALSE);
881 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
884 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
890 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
891 /* should check that write mode exists */
892 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
894 wm->info.lBufOffset = offset;
895 wm->info.pchEndRead = wm->info.pchBuffer;
896 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
897 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
898 wm->info.lDiskOffset = wm->dwFileSize;
900 } else if ((wm->info.cchBuffer > 0) &&
901 ((offset < wm->info.lBufOffset) ||
902 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
903 !wm->bBufferLoaded)) {
904 /* stay in same buffer ? */
905 /* some memory mapped buffers are defined with -1 as a size */
907 /* condition to change buffer */
908 if ((wm->info.fccIOProc == FOURCC_MEM) ||
909 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
910 /* this also sets the wm->info.lDiskOffset field */
911 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
912 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
913 SEEK_SET, FALSE) == -1)
915 MMIO_GrabNextBuffer(wm, TRUE);
918 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
920 TRACE("=> %d\n", offset);
924 /**************************************************************************
925 * mmioGetInfo [WINMM.@]
927 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
931 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
933 if ((wm = MMIO_Get(hmmio)) == NULL)
934 return MMSYSERR_INVALHANDLE;
936 *lpmmioinfo = wm->info;
938 return MMSYSERR_NOERROR;
941 /**************************************************************************
942 * mmioSetInfo [WINMM.@]
944 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
948 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
950 if ((wm = MMIO_Get(hmmio)) == NULL)
951 return MMSYSERR_INVALHANDLE;
953 /* check pointers coherence */
954 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
955 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
956 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
957 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
958 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
959 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
960 return MMSYSERR_INVALPARAM;
962 wm->info.pchNext = lpmmioinfo->pchNext;
963 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
965 return MMSYSERR_NOERROR;
968 /**************************************************************************
969 * mmioSetBuffer [WINMM.@]
971 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
975 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%d, uFlags=%#08x)\n",
976 hmmio, pchBuffer, cchBuffer, uFlags);
978 if ((wm = MMIO_Get(hmmio)) == NULL)
979 return MMSYSERR_INVALHANDLE;
981 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
984 /**************************************************************************
985 * mmioFlush [WINMM.@]
987 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
991 TRACE("(%p, %04X)\n", hmmio, uFlags);
993 if ((wm = MMIO_Get(hmmio)) == NULL)
994 return MMSYSERR_INVALHANDLE;
996 return MMIO_Flush(wm, uFlags);
999 /**************************************************************************
1000 * mmioAdvance [WINMM.@]
1002 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1006 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1008 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1009 * here. be sure if you change something here to check mmioAdvance16 as well
1011 if ((wm = MMIO_Get(hmmio)) == NULL)
1012 return MMSYSERR_INVALHANDLE;
1014 if (!wm->info.cchBuffer)
1015 return MMIOERR_UNBUFFERED;
1017 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1018 return MMSYSERR_INVALPARAM;
1020 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1022 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
1023 send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1024 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1025 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1027 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1028 return MMIOERR_CANNOTWRITE;
1031 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1032 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1034 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1037 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1038 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1039 (wm->info.pchEndRead - wm->info.pchBuffer);
1040 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1041 (wm->info.pchEndWrite - wm->info.pchBuffer);
1042 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1043 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1045 return MMSYSERR_NOERROR;
1048 /**************************************************************************
1049 * mmioStringToFOURCCA [WINMM.@]
1051 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1056 for (i = 0; i < 4 && sz[i]; i++) {
1057 if (uFlags & MMIO_TOUPPER) {
1058 cc[i] = toupper(sz[i]);
1064 /* Pad with spaces */
1065 while (i < 4) cc[i++] = ' ';
1067 TRACE("Got %s\n",debugstr_an(cc,4));
1068 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1071 /**************************************************************************
1072 * mmioStringToFOURCCW [WINMM.@]
1074 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1078 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1079 return mmioStringToFOURCCA(szA,uFlags);
1082 /**************************************************************************
1083 * mmioInstallIOProcA [WINMM.@]
1085 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1086 LPMMIOPROC pIOProc, DWORD dwFlags)
1088 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, FALSE);
1091 /**************************************************************************
1092 * mmioInstallIOProcW [WINMM.@]
1094 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1095 LPMMIOPROC pIOProc, DWORD dwFlags)
1097 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, TRUE);
1100 /******************************************************************
1105 static LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1106 LPARAM lParam2, BOOL is_unicode)
1110 TRACE("(%p, %u, %ld, %ld, %s)\n", hmmio, uMessage, lParam1, lParam2, is_unicode ? "unicode" : "ansi");
1112 if (uMessage < MMIOM_USER)
1113 return MMSYSERR_INVALPARAM;
1115 if ((wm = MMIO_Get(hmmio)) == NULL)
1116 return MMSYSERR_INVALHANDLE;
1118 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, is_unicode);
1121 /**************************************************************************
1122 * mmioSendMessage [WINMM.@]
1124 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1125 LPARAM lParam1, LPARAM lParam2)
1127 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, FALSE);
1130 /**************************************************************************
1131 * mmioDescend [WINMM.@]
1133 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1134 const MMCKINFO* lpckParent, UINT uFlags)
1140 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1143 return MMSYSERR_INVALPARAM;
1145 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1146 TRACE("dwOldPos=%d\n", dwOldPos);
1148 if (lpckParent != NULL) {
1149 TRACE("seek inside parent at %d !\n", lpckParent->dwDataOffset);
1150 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1151 if (dwOldPos < lpckParent->dwDataOffset ||
1152 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1153 WARN("outside parent chunk\n");
1154 return MMIOERR_CHUNKNOTFOUND;
1158 /* The SDK docu says 'ckid' is used for all cases. Real World
1159 * examples disagree -Marcus,990216.
1165 /* find_chunk looks for 'ckid' */
1166 if (uFlags & MMIO_FINDCHUNK)
1167 srchCkId = lpck->ckid;
1169 /* find_riff and find_list look for 'fccType' */
1170 if (uFlags & MMIO_FINDLIST)
1172 srchCkId = FOURCC_LIST;
1173 srchType = lpck->fccType;
1176 if (uFlags & MMIO_FINDRIFF)
1178 srchCkId = FOURCC_RIFF;
1179 srchType = lpck->fccType;
1182 TRACE("searching for %s.%s\n",
1183 debugstr_an((LPCSTR)&srchCkId, 4), srchType ? debugstr_an((LPCSTR)&srchType, 4) : "<any>");
1189 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1190 if (ix < 2*sizeof(DWORD))
1192 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1193 WARN("return ChunkNotFound\n");
1194 return MMIOERR_CHUNKNOTFOUND;
1197 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1198 TRACE("ckid=%s fcc=%s cksize=%08X !\n",
1199 debugstr_an((LPCSTR)&lpck->ckid, 4),
1200 srchType ? debugstr_an((LPCSTR)&lpck->fccType, 4) : "<na>",
1202 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1203 (!srchType || (srchType == lpck->fccType)) )
1206 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1207 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1211 /* If we were looking for RIFF/LIST chunks, the final file position
1212 * is after the chunkid. If we were just looking for the chunk
1213 * it is after the cksize. So add 4 in RIFF/LIST case.
1215 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1216 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1219 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1222 TRACE("lpck: ckid=%s, cksize=%d, dwDataOffset=%d fccType=%08X (%s)!\n",
1223 debugstr_an((LPSTR)&lpck->ckid, 4), lpck->cksize, lpck->dwDataOffset,
1224 lpck->fccType, srchType ? debugstr_an((LPSTR)&lpck->fccType, 4):"");
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=%d lpck->dwDataOffset = %d\n", dwOldPos, lpck->dwDataOffset);
1241 dwNewSize = dwOldPos - lpck->dwDataOffset;
1242 if (dwNewSize != lpck->cksize) {
1243 TRACE("Nope: lpck->cksize=%d dwNewSize=%d\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=%d\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=%s\n", debugstr_an((LPSTR)&lpck->ckid, 4));
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 = %d req = %d, 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 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1307 struct IOProcList* ioProc = NULL;
1308 struct IOProcList tmp;
1311 TRACE("(%s, %s, %p, %08X);\n",
1312 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1314 /* If both params are NULL, then parse the file name */
1315 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1317 fcc = MMIO_ParseExtA(szFileName);
1318 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1321 /* Handle any unhandled/error case from above. Assume DOS file */
1322 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1323 ioProc = &defaultProcs[0];
1324 /* if just the four character code is present, look up IO proc */
1325 else if (lpmmioinfo->pIOProc == NULL)
1326 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1327 else /* use relevant ioProc */
1330 tmp.fourCC = lpmmioinfo->fccIOProc;
1331 tmp.pIOProc = lpmmioinfo->pIOProc;
1332 tmp.is_unicode = FALSE;
1336 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1337 * or make a copy of it because it's const ???
1339 return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1340 (LPARAM)szFileName, (LPARAM)szNewFileName, FALSE);
1343 /**************************************************************************
1344 * mmioRenameW [WINMM.@]
1346 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1347 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1351 UINT ret = MMSYSERR_NOMEM;
1356 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1357 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1358 if (!szFn) goto done;
1359 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1363 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1364 sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1365 if (!sznFn) goto done;
1366 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1369 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1372 HeapFree(GetProcessHeap(),0,szFn);
1373 HeapFree(GetProcessHeap(),0,sznFn);