mmsystem.dll16: Check for NULL ptr in mmioRename (Coverity).
[wine/multimedia.git] / dlls / winmm / mmio.c
blobb774003abb81f016b98deac17febc0af2cfd45af
1 /*
2 * MMIO functions
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
23 /* Still to be done:
24 * + correct handling of global/local IOProcs (and temporary IOProcs)
25 * + mode of mmio objects is not used (read vs write vs readwrite)
26 * + thread safeness
27 * + 32 A <=> W message mapping
31 #include <ctype.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <assert.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "mmsystem.h"
42 #include "winemm.h"
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);
60 switch (uMessage) {
61 case MMIOM_OPEN:
63 /* Parameters:
64 * lParam1 = szFileName parameter from mmioOpen
65 * lParam2 = reserved
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] */
77 if (szFileName) {
78 OFSTRUCT ofs;
79 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
81 if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
82 ret = MMIOERR_CANNOTOPEN;
84 break;
86 case MMIOM_CLOSE:
87 /* Parameters:
88 * lParam1 = wFlags parameter from mmioClose
89 * lParam2 = unused
90 * Returns: zero on success, error code on error
92 if (!(lParam1 & MMIO_FHOPEN))
93 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
94 break;
96 case MMIOM_READ:
97 /* Parameters:
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
101 * in wErrorRet)
103 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
104 if (ret != -1)
105 lpmmioinfo->lDiskOffset += ret;
107 break;
109 case MMIOM_WRITE:
110 case MMIOM_WRITEFLUSH:
111 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
113 /* Parameters:
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
117 * wErrorRet)
119 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
120 if (ret != -1)
121 lpmmioinfo->lDiskOffset += ret;
122 break;
124 case MMIOM_SEEK:
125 /* Parameters:
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);
131 if (ret != -1)
132 lpmmioinfo->lDiskOffset = ret;
133 return ret;
135 case MMIOM_RENAME:
136 /* Parameters:
137 * lParam1 = old name
138 * lParam2 = new name
139 * Returns: zero on success, non-zero on failure
141 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
142 ret = MMIOERR_FILENOTFOUND;
143 break;
145 default:
146 FIXME("unexpected message %u\n", uMessage);
147 return 0;
150 return ret;
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);
161 switch (uMessage) {
163 case MMIOM_OPEN:
164 /* Parameters:
165 * lParam1 = filename (must be NULL)
166 * lParam2 = reserved
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;
174 return 0;
176 case MMIOM_CLOSE:
177 /* Parameters:
178 * lParam1 = wFlags parameter from mmioClose
179 * lParam2 = unused
180 * Returns: zero on success, error code on error
182 return 0;
184 case MMIOM_READ:
185 /* Parameters:
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
189 * in wErrorRet)
190 * NOTE: lDiskOffset should be updated
192 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
193 return 0;
195 case MMIOM_WRITE:
196 case MMIOM_WRITEFLUSH:
197 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
199 /* Parameters:
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
203 * wErrorRet)
204 * NOTE: lDiskOffset should be updated
206 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
207 return 0;
209 case MMIOM_SEEK:
210 /* Parameters:
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");
217 return -1;
219 default:
220 FIXME("unexpected message %u\n", uMessage);
221 return 0;
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) {
247 return pListNode;
250 return NULL;
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));
273 if (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 */
285 lpProc = pIOProc;
287 break;
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
293 * address otherwise
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) &&
300 ((fccIOProc != 0) ?
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");
311 break;
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");
317 } else {
318 /* Okay, nuke it */
319 struct IOProcList* ptmpNode = *ppListNode;
320 lpProc = (*ppListNode)->pIOProc;
321 *ppListNode = (*ppListNode)->pNext;
322 HeapFree(GetProcessHeap(), 0, ptmpNode);
325 break;
327 case MMIO_FINDPROC:
328 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
329 lpProc = pListNode->pIOProc;
331 break;
334 return lpProc;
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;
347 if (!ioProc) {
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);
358 #if 0
359 if (ioProc->is_unicode != is_unicode) {
360 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
362 #endif
364 return result;
367 /**************************************************************************
368 * MMIO_ParseExtA [internal]
370 * Parses a filename for the extension.
372 * RETURNS
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 */
380 FOURCC ret = 0;
382 /* Note that ext{Start,End} point to the . and + respectively */
383 LPSTR extEnd;
384 LPSTR extStart;
386 TRACE("(%s)\n", debugstr_a(szFileName));
388 if (!szFileName)
389 return ret;
391 /* Find the last '.' */
392 extStart = strrchr(szFileName,'.');
394 if (!extStart) {
395 ERR("No . in szFileName: %s\n", debugstr_a(szFileName));
396 } else {
397 CHAR ext[5];
399 /* Find the '+' afterwards */
400 extEnd = strchr(extStart,'+');
401 if (extEnd) {
403 if (extEnd - extStart - 1 > 4)
404 WARN("Extension length > 4\n");
405 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
407 } else {
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);
419 return ret;
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)
434 break;
436 LeaveCriticalSection(&WINMM_cs);
437 return wm;
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;
448 LPWINE_MMIO wm;
450 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
451 if (wm) {
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;
457 MMIOList = wm;
458 LeaveCriticalSection(&WINMM_cs);
460 return wm;
463 /**************************************************************************
464 * MMIO_Destroy [internal]
466 * Destroys an internal representation for a mmio instance
468 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
470 LPWINE_MMIO* m;
472 EnterCriticalSection(&WINMM_cs);
473 /* search for the matching one... */
474 m = &MMIOList;
475 while (*m && *m != wm) m = &(*m)->lpNext;
476 /* ...and destroy */
477 if (*m) {
478 *m = (*m)->lpNext;
479 HeapFree(GetProcessHeap(), 0, wm);
480 wm = NULL;
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;
525 if (for_read) {
526 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
527 (LPARAM)wm->info.pchBuffer, size, FALSE);
528 if (size > 0)
529 wm->info.pchEndRead += size;
530 else
531 wm->bBufferLoaded = FALSE;
534 return size;
537 /***************************************************************************
538 * MMIO_SetBuffer [INTERNAL]
540 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
541 UINT uFlags)
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;
558 if (pchBuffer) {
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;
564 } else {
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)
583 LPWINE_MMIO wm;
584 MMIOINFO mmioinfo;
586 TRACE("('%s', %p, %08X, %s);\n", szFileName, refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
588 if (!refmminfo) {
589 refmminfo = &mmioinfo;
591 mmioinfo.fccIOProc = 0;
592 mmioinfo.pIOProc = NULL;
593 mmioinfo.pchBuffer = NULL;
594 mmioinfo.cchBuffer = 0;
595 is_unicode = FALSE;
598 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
599 char buffer[MAX_PATH];
601 if (!szFileName)
602 return (HMMIO)FALSE;
603 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
604 return (HMMIO)FALSE;
605 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
606 return (HMMIO)FALSE;
607 strcpy(szFileName, buffer);
608 return (HMMIO)TRUE;
611 if ((wm = MMIO_Create()) == NULL)
612 return 0;
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)))
624 goto error2;
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 */
635 else {
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;
644 wm->ioProc->count++;
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)
651 goto error1;
652 } else {
653 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
654 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
655 goto error1;
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;
673 error1:
674 if (wm->ioProc) wm->ioProc->count--;
675 error2:
676 MMIO_Destroy(wm);
677 return 0;
680 /**************************************************************************
681 * mmioOpenW [WINMM.@]
683 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
684 DWORD dwOpenFlags)
686 HMMIO ret;
687 LPSTR szFn = NULL;
689 if (szFileName)
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);
700 return ret;
703 /**************************************************************************
704 * mmioOpenA [WINMM.@]
706 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
707 DWORD dwOpenFlags)
709 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
712 /**************************************************************************
713 * mmioClose [WINMM.@]
715 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
717 LPWINE_MMIO wm;
718 MMRESULT result;
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)
726 return result;
728 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
730 MMIO_SetBuffer(wm, NULL, 0, 0);
732 wm->ioProc->count--;
734 if (wm->bTmpIOProc)
735 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
736 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
738 MMIO_Destroy(wm);
740 return result;
743 /**************************************************************************
744 * mmioRead [WINMM.@]
746 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
748 LPWINE_MMIO wm;
749 LONG count;
751 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
753 if ((wm = MMIO_Get(hmmio)) == NULL)
754 return -1;
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;
766 pch += count;
767 cch -= count;
768 } else
769 count = 0;
771 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
772 assert(wm->info.cchBuffer);
774 while (cch) {
775 LONG size;
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;
782 pch += size;
783 cch -= size;
784 count += size;
788 TRACE("count=%d\n", count);
789 return count;
792 /**************************************************************************
793 * mmioWrite [WINMM.@]
795 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
797 LPWINE_MMIO wm;
798 LONG count;
800 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
802 if ((wm = MMIO_Get(hmmio)) == NULL)
803 return -1;
805 if (wm->info.cchBuffer) {
806 LONG bytesW = 0;
808 count = 0;
809 while (cch) {
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;
815 pch += count;
816 cch -= count;
817 bytesW += count;
818 wm->info.dwFlags |= MMIO_DIRTY;
819 } else {
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");
824 break;
825 } else break;
829 if (wm->info.pchNext == wm->info.pchEndWrite)
831 MMIO_Flush(wm, MMIO_EMPTYBUF);
832 MMIO_GrabNextBuffer(wm, FALSE);
834 else break;
836 count = bytesW;
837 } else {
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);
843 return count;
846 /**************************************************************************
847 * mmioSeek [WINMM.@]
849 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
851 LPWINE_MMIO wm;
852 LONG offset;
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);
863 switch (iOrigin) {
864 case SEEK_SET:
865 offset = lOffset;
866 break;
867 case SEEK_CUR:
868 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
869 break;
870 case SEEK_END:
871 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
872 break;
873 default:
874 return -1;
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)
880 return -1;
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)
901 return -1;
902 MMIO_GrabNextBuffer(wm, TRUE);
905 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
907 TRACE("=> %d\n", offset);
908 return offset;
911 /**************************************************************************
912 * mmioGetInfo [WINMM.@]
914 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
916 LPWINE_MMIO wm;
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)
933 LPWINE_MMIO wm;
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)
960 LPWINE_MMIO wm;
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)
976 LPWINE_MMIO wm;
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)
991 LPWINE_MMIO wm;
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;
1017 if (lpmmioinfo) {
1018 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1019 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1021 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1023 if (lpmmioinfo) {
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)
1040 CHAR cc[4];
1041 int i = 0;
1043 for (i = 0; i < 4 && sz[i]; i++) {
1044 if (uFlags & MMIO_TOUPPER) {
1045 cc[i] = toupper(sz[i]);
1046 } else {
1047 cc[i] = 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)
1063 char szA[4];
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 /******************************************************************
1088 * MMIO_SendMessage
1092 static LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1093 LPARAM lParam2, BOOL is_unicode)
1095 LPWINE_MMIO wm;
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)
1123 DWORD dwOldPos;
1124 FOURCC srchCkId;
1125 FOURCC srchType;
1127 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1129 if (lpck == NULL)
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.
1149 srchCkId = 0;
1150 srchType = 0;
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");
1172 while (TRUE)
1174 LONG ix;
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>",
1188 lpck->cksize);
1189 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1190 (!srchType || (srchType == lpck->fccType)) )
1191 break;
1193 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1194 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1197 lpck->dwFlags = 0;
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);
1204 else
1206 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1207 lpck->fccType = 0;
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) {
1235 char ch = 0;
1236 mmioWrite(hmmio, &ch, 1);
1238 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1239 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1241 lpck->dwFlags = 0;
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)
1254 DWORD dwOldPos;
1255 LONG size;
1256 LONG ix;
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);
1279 if (ix < size) {
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;
1296 FOURCC fcc;
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 */
1316 ioProc = &tmp;
1317 tmp.fourCC = lpmmioinfo->fccIOProc;
1318 tmp.pIOProc = lpmmioinfo->pIOProc;
1319 tmp.is_unicode = FALSE;
1320 tmp.count = 1;
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)
1336 LPSTR szFn = NULL;
1337 LPSTR sznFn = NULL;
1338 UINT ret = MMSYSERR_NOMEM;
1339 INT len;
1341 if (szFileName)
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 );
1348 if (szNewFileName)
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);
1358 done:
1359 HeapFree(GetProcessHeap(),0,szFn);
1360 HeapFree(GetProcessHeap(),0,sznFn);
1361 return ret;