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_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)
587 TRACE("(%s, %p, %08X, %s);\n", debugstr_a(szFileName), refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
590 refmminfo = &mmioinfo;
592 mmioinfo.fccIOProc = 0;
593 mmioinfo.pIOProc = NULL;
594 mmioinfo.pchBuffer = NULL;
595 mmioinfo.cchBuffer = 0;
599 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
600 char buffer[MAX_PATH];
604 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
606 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
608 strcpy(szFileName, buffer);
612 if ((wm = MMIO_Create()) == NULL)
615 /* If both params are NULL, then parse the file name if available */
616 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
617 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
618 /* Handle any unhandled/error case. Assume DOS file */
619 if (wm->info.fccIOProc == 0)
620 wm->info.fccIOProc = FOURCC_DOS;
621 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
622 /* If not found, retry with FOURCC_DOS */
623 wm->info.fccIOProc = FOURCC_DOS;
624 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc)))
627 wm->bTmpIOProc = FALSE;
629 /* if just the four character code is present, look up IO proc */
630 else if (refmminfo->pIOProc == NULL) {
631 wm->info.fccIOProc = refmminfo->fccIOProc;
632 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
633 wm->bTmpIOProc = FALSE;
635 /* if IO proc specified, use it and specified four character code */
637 wm->info.fccIOProc = refmminfo->fccIOProc;
638 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
639 MMIO_INSTALLPROC, is_unicode);
640 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
641 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
642 wm->bTmpIOProc = TRUE;
646 wm->info.dwFlags = dwOpenFlags;
648 if (dwOpenFlags & MMIO_ALLOCBUF) {
649 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
650 refmminfo->cchBuffer ? refmminfo->cchBuffer : MMIO_DEFAULTBUFFER, 0);
651 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
654 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
655 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
659 if (wm->info.fccIOProc == FOURCC_MEM && !(wm->info.dwFlags & MMIO_ALLOCBUF))
660 wm->bBufferLoaded = TRUE;
662 /* see mmioDosIOProc for that one */
663 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
665 /* call IO proc to actually open file */
666 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
667 (LPARAM)szFileName, 0, FALSE);
669 /* grab file size, when possible */
670 if (wm->info.fccIOProc != FOURCC_MEM && (send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE)) != -1) {
671 pos = wm->info.lDiskOffset;
672 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_END, FALSE);
673 wm->dwFileSize = wm->info.lDiskOffset;
674 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, pos, SEEK_SET, FALSE);
676 else wm->dwFileSize = 0;
678 if (refmminfo->wErrorRet == 0)
679 return wm->info.hmmio;
681 if (wm->ioProc) wm->ioProc->count--;
687 /**************************************************************************
688 * mmioOpenW [WINMM.@]
690 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
698 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
699 szFn = HeapAlloc( GetProcessHeap(), 0, len );
700 if (!szFn) return NULL;
701 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
704 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
706 HeapFree(GetProcessHeap(), 0, szFn);
710 /**************************************************************************
711 * mmioOpenA [WINMM.@]
713 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
716 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
719 /**************************************************************************
720 * mmioClose [WINMM.@]
722 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
727 TRACE("(%p, %04X);\n", hmmio, uFlags);
729 if ((wm = MMIO_Get(hmmio)) == NULL)
730 return MMSYSERR_INVALHANDLE;
732 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
735 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
737 MMIO_SetBuffer(wm, NULL, 0, 0);
742 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
743 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
750 /**************************************************************************
753 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
758 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
760 if ((wm = MMIO_Get(hmmio)) == NULL)
763 /* unbuffered case first */
764 if (!wm->info.pchBuffer)
765 return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
767 /* first try from current buffer */
768 if (wm->info.pchNext != wm->info.pchEndRead) {
769 count = wm->info.pchEndRead - wm->info.pchNext;
770 if (count > cch || count < 0) count = cch;
771 memcpy(pch, wm->info.pchNext, count);
772 wm->info.pchNext += count;
778 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
779 assert(wm->info.cchBuffer);
784 size = MMIO_GrabNextBuffer(wm, TRUE);
785 if (size <= 0) break;
786 if (size > cch) size = cch;
787 memcpy(pch, wm->info.pchBuffer, size);
788 wm->info.pchNext += size;
795 TRACE("count=%d\n", count);
799 /**************************************************************************
800 * mmioWrite [WINMM.@]
802 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
807 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
809 if ((wm = MMIO_Get(hmmio)) == NULL)
812 if (wm->info.cchBuffer) {
817 if (wm->info.pchNext != wm->info.pchEndWrite) {
818 count = wm->info.pchEndWrite - wm->info.pchNext;
819 if (count > cch || count < 0) count = cch;
820 memcpy(wm->info.pchNext, pch, count);
821 wm->info.pchNext += count;
825 wm->info.dwFlags |= MMIO_DIRTY;
827 if (wm->info.fccIOProc == FOURCC_MEM) {
828 if (wm->info.adwInfo[0]) {
829 /* from where would we get the memory handle? */
830 FIXME("memory file expansion not implemented!\n");
836 if (wm->info.pchNext == wm->info.pchEndWrite)
838 MMIO_Flush(wm, MMIO_EMPTYBUF);
839 MMIO_GrabNextBuffer(wm, FALSE);
845 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
846 wm->info.lBufOffset = wm->info.lDiskOffset;
849 TRACE("bytes written=%d\n", count);
853 /**************************************************************************
856 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
861 TRACE("(%p, %08X, %d);\n", hmmio, lOffset, iOrigin);
863 if ((wm = MMIO_Get(hmmio)) == NULL)
864 return MMSYSERR_INVALHANDLE;
866 /* not buffered, direct seek on file */
867 if (!wm->info.pchBuffer)
868 return send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, iOrigin, FALSE);
875 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
878 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
884 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
885 /* should check that write mode exists */
886 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
888 wm->info.lBufOffset = offset;
889 wm->info.pchEndRead = wm->info.pchBuffer;
890 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
891 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
892 wm->info.lDiskOffset = wm->dwFileSize;
894 } else if ((wm->info.cchBuffer > 0) &&
895 ((offset < wm->info.lBufOffset) ||
896 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
897 !wm->bBufferLoaded)) {
898 /* stay in same buffer ? */
899 /* some memory mapped buffers are defined with -1 as a size */
901 /* condition to change buffer */
902 if ((wm->info.fccIOProc == FOURCC_MEM) ||
903 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
904 /* this also sets the wm->info.lDiskOffset field */
905 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
906 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
907 SEEK_SET, FALSE) == -1)
909 MMIO_GrabNextBuffer(wm, TRUE);
912 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
914 TRACE("=> %d\n", offset);
918 /**************************************************************************
919 * mmioGetInfo [WINMM.@]
921 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
925 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
927 if ((wm = MMIO_Get(hmmio)) == NULL)
928 return MMSYSERR_INVALHANDLE;
930 *lpmmioinfo = wm->info;
932 return MMSYSERR_NOERROR;
935 /**************************************************************************
936 * mmioSetInfo [WINMM.@]
938 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
942 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
944 if ((wm = MMIO_Get(hmmio)) == NULL)
945 return MMSYSERR_INVALHANDLE;
947 /* check pointers coherence */
948 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
949 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
950 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
951 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
952 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
953 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
954 return MMSYSERR_INVALPARAM;
956 wm->info.pchNext = lpmmioinfo->pchNext;
957 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
959 return MMSYSERR_NOERROR;
962 /**************************************************************************
963 * mmioSetBuffer [WINMM.@]
965 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
969 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%d, uFlags=%#08x)\n",
970 hmmio, pchBuffer, cchBuffer, uFlags);
972 if ((wm = MMIO_Get(hmmio)) == NULL)
973 return MMSYSERR_INVALHANDLE;
975 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
978 /**************************************************************************
979 * mmioFlush [WINMM.@]
981 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
985 TRACE("(%p, %04X)\n", hmmio, uFlags);
987 if ((wm = MMIO_Get(hmmio)) == NULL)
988 return MMSYSERR_INVALHANDLE;
990 return MMIO_Flush(wm, uFlags);
993 /**************************************************************************
994 * mmioAdvance [WINMM.@]
996 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1000 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1002 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1003 * here. be sure if you change something here to check mmioAdvance16 as well
1005 if ((wm = MMIO_Get(hmmio)) == NULL)
1006 return MMSYSERR_INVALHANDLE;
1008 if (!wm->info.cchBuffer)
1009 return MMIOERR_UNBUFFERED;
1011 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1012 return MMSYSERR_INVALPARAM;
1014 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1016 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
1017 send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1018 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1019 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1021 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1022 return MMIOERR_CANNOTWRITE;
1025 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1026 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1028 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1031 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1032 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1033 (wm->info.pchEndRead - wm->info.pchBuffer);
1034 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1035 (wm->info.pchEndWrite - wm->info.pchBuffer);
1036 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1037 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1039 return MMSYSERR_NOERROR;
1042 /**************************************************************************
1043 * mmioStringToFOURCCA [WINMM.@]
1045 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1050 for (i = 0; i < 4 && sz[i]; i++) {
1051 if (uFlags & MMIO_TOUPPER) {
1052 cc[i] = toupper(sz[i]);
1058 /* Pad with spaces */
1059 while (i < 4) cc[i++] = ' ';
1061 TRACE("Got %s\n",debugstr_an(cc,4));
1062 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1065 /**************************************************************************
1066 * mmioStringToFOURCCW [WINMM.@]
1068 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1072 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1073 return mmioStringToFOURCCA(szA,uFlags);
1076 /**************************************************************************
1077 * mmioInstallIOProcA [WINMM.@]
1079 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1080 LPMMIOPROC pIOProc, DWORD dwFlags)
1082 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, FALSE);
1085 /**************************************************************************
1086 * mmioInstallIOProcW [WINMM.@]
1088 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1089 LPMMIOPROC pIOProc, DWORD dwFlags)
1091 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, TRUE);
1094 /******************************************************************
1099 static LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1100 LPARAM lParam2, BOOL is_unicode)
1104 TRACE("(%p, %u, %ld, %ld, %s)\n", hmmio, uMessage, lParam1, lParam2, is_unicode ? "unicode" : "ansi");
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, is_unicode);
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, FALSE);
1124 /**************************************************************************
1125 * mmioDescend [WINMM.@]
1127 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1128 const MMCKINFO* lpckParent, UINT uFlags)
1134 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1137 return MMSYSERR_INVALPARAM;
1139 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1140 TRACE("dwOldPos=%d\n", dwOldPos);
1142 if (lpckParent != NULL) {
1143 TRACE("seek inside parent at %d !\n", lpckParent->dwDataOffset);
1144 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1145 if (dwOldPos < lpckParent->dwDataOffset ||
1146 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1147 WARN("outside parent chunk\n");
1148 return MMIOERR_CHUNKNOTFOUND;
1152 /* The SDK docu says 'ckid' is used for all cases. Real World
1153 * examples disagree -Marcus,990216.
1159 /* find_chunk looks for 'ckid' */
1160 if (uFlags & MMIO_FINDCHUNK)
1161 srchCkId = lpck->ckid;
1163 /* find_riff and find_list look for 'fccType' */
1164 if (uFlags & MMIO_FINDLIST)
1166 srchCkId = FOURCC_LIST;
1167 srchType = lpck->fccType;
1170 if (uFlags & MMIO_FINDRIFF)
1172 srchCkId = FOURCC_RIFF;
1173 srchType = lpck->fccType;
1176 TRACE("searching for %s.%s\n",
1177 debugstr_an((LPCSTR)&srchCkId, 4), srchType ? debugstr_an((LPCSTR)&srchType, 4) : "<any>");
1183 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1184 if (ix < 2*sizeof(DWORD))
1186 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1187 WARN("return ChunkNotFound\n");
1188 return MMIOERR_CHUNKNOTFOUND;
1191 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1192 TRACE("ckid=%s fcc=%s cksize=%08X !\n",
1193 debugstr_an((LPCSTR)&lpck->ckid, 4),
1194 srchType ? debugstr_an((LPCSTR)&lpck->fccType, 4) : "<na>",
1196 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1197 (!srchType || (srchType == lpck->fccType)) )
1200 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1201 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1205 /* If we were looking for RIFF/LIST chunks, the final file position
1206 * is after the chunkid. If we were just looking for the chunk
1207 * it is after the cksize. So add 4 in RIFF/LIST case.
1209 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1210 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1213 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1216 TRACE("lpck: ckid=%s, cksize=%d, dwDataOffset=%d fccType=%08X (%s)!\n",
1217 debugstr_an((LPSTR)&lpck->ckid, 4), lpck->cksize, lpck->dwDataOffset,
1218 lpck->fccType, srchType ? debugstr_an((LPSTR)&lpck->fccType, 4):"");
1219 return MMSYSERR_NOERROR;
1222 /**************************************************************************
1223 * mmioAscend [WINMM.@]
1225 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1227 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1229 if (lpck->dwFlags & MMIO_DIRTY) {
1230 DWORD dwOldPos, dwNewSize;
1232 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1233 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1234 TRACE("dwOldPos=%d lpck->dwDataOffset = %d\n", dwOldPos, lpck->dwDataOffset);
1235 dwNewSize = dwOldPos - lpck->dwDataOffset;
1236 if (dwNewSize != lpck->cksize) {
1237 TRACE("Nope: lpck->cksize=%d dwNewSize=%d\n", lpck->cksize, dwNewSize);
1238 lpck->cksize = dwNewSize;
1240 /* pad odd size with 0 */
1241 if (dwNewSize & 1) {
1243 mmioWrite(hmmio, &ch, 1);
1245 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1246 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1251 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1253 return MMSYSERR_NOERROR;
1256 /**************************************************************************
1257 * mmioCreateChunk [WINMM.@]
1259 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1265 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1267 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1268 TRACE("dwOldPos=%d\n", dwOldPos);
1270 if (uFlags == MMIO_CREATELIST)
1271 lpck->ckid = FOURCC_LIST;
1272 else if (uFlags == MMIO_CREATERIFF)
1273 lpck->ckid = FOURCC_RIFF;
1275 TRACE("ckid=%s\n", debugstr_an((LPSTR)&lpck->ckid, 4));
1277 size = 2 * sizeof(DWORD);
1278 lpck->dwDataOffset = dwOldPos + size;
1280 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1281 size += sizeof(DWORD);
1282 lpck->dwFlags = MMIO_DIRTY;
1284 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1285 TRACE("after mmioWrite ix = %d req = %d, errno = %d\n", ix, size, errno);
1287 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1288 WARN("return CannotWrite\n");
1289 return MMIOERR_CANNOTWRITE;
1292 return MMSYSERR_NOERROR;
1295 /**************************************************************************
1296 * mmioRenameA [WINMM.@]
1298 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1299 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1301 struct IOProcList* ioProc = NULL;
1302 struct IOProcList tmp;
1305 TRACE("(%s, %s, %p, %08X);\n",
1306 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1308 /* If both params are NULL, then parse the file name */
1309 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1311 fcc = MMIO_ParseExtA(szFileName);
1312 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1315 /* Handle any unhandled/error case from above. Assume DOS file */
1316 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1317 ioProc = MMIO_FindProcNode(FOURCC_DOS);
1318 /* if just the four character code is present, look up IO proc */
1319 else if (lpmmioinfo->pIOProc == NULL)
1320 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1321 else /* use relevant ioProc */
1324 tmp.fourCC = lpmmioinfo->fccIOProc;
1325 tmp.pIOProc = lpmmioinfo->pIOProc;
1326 tmp.is_unicode = FALSE;
1330 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1331 * or make a copy of it because it's const ???
1333 return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1334 (LPARAM)szFileName, (LPARAM)szNewFileName, FALSE);
1337 /**************************************************************************
1338 * mmioRenameW [WINMM.@]
1340 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1341 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1345 UINT ret = MMSYSERR_NOMEM;
1350 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1351 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1352 if (!szFn) goto done;
1353 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1357 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1358 sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1359 if (!sznFn) goto done;
1360 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1363 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1366 HeapFree(GetProcessHeap(),0,szFn);
1367 HeapFree(GetProcessHeap(),0,sznFn);