wined3d: read_from_framebuffer_texture() isn't suitable for readback of onscreen...
[wine/testsucceed.git] / dlls / winmm / mmio.c
blobf13e090926f927ee0601c049eb021c4f489dd221
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] = OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
81 if (lpmmioinfo->adwInfo[0] == 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;
585 DWORD pos;
587 TRACE("('%s', %p, %08X, %s);\n", szFileName, refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
589 if (!refmminfo) {
590 refmminfo = &mmioinfo;
592 mmioinfo.fccIOProc = 0;
593 mmioinfo.pIOProc = NULL;
594 mmioinfo.pchBuffer = NULL;
595 mmioinfo.cchBuffer = 0;
596 is_unicode = FALSE;
599 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
600 char buffer[MAX_PATH];
602 if (!szFileName)
603 return (HMMIO)FALSE;
604 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
605 return (HMMIO)FALSE;
606 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
607 return (HMMIO)FALSE;
608 strcpy(szFileName, buffer);
609 return (HMMIO)TRUE;
612 if ((wm = MMIO_Create()) == NULL)
613 return 0;
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)))
625 goto error2;
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 */
636 else {
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;
645 wm->ioProc->count++;
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)
652 goto error1;
653 } else {
654 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
655 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
656 goto error1;
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 /* FIXME: the code around dwFileSize is:
671 * - probably broken
672 * - needs to be tested when the app installs its own IOproc
673 * - likely to be removed
674 * So, restrict dwFileSize usage to DOS ioprocs
676 if (wm->info.fccIOProc == FOURCC_DOS && (pos = _llseek((HFILE)wm->info.adwInfo[0], 0, SEEK_CUR)) != -1)
678 wm->dwFileSize = _llseek((HFILE)wm->info.adwInfo[0], 0, SEEK_END);
679 _llseek((HFILE)wm->info.adwInfo[0], pos, SEEK_SET);
681 else wm->dwFileSize = 0;
683 if (refmminfo->wErrorRet == 0)
684 return wm->info.hmmio;
685 error1:
686 if (wm->ioProc) wm->ioProc->count--;
687 error2:
688 MMIO_Destroy(wm);
689 return 0;
692 /**************************************************************************
693 * mmioOpenW [WINMM.@]
695 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
696 DWORD dwOpenFlags)
698 HMMIO ret;
699 LPSTR szFn = NULL;
701 if (szFileName)
703 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
704 szFn = HeapAlloc( GetProcessHeap(), 0, len );
705 if (!szFn) return NULL;
706 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
709 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
711 HeapFree(GetProcessHeap(), 0, szFn);
712 return ret;
715 /**************************************************************************
716 * mmioOpenA [WINMM.@]
718 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
719 DWORD dwOpenFlags)
721 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
724 /**************************************************************************
725 * mmioClose [WINMM.@]
727 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
729 LPWINE_MMIO wm;
730 MMRESULT result;
732 TRACE("(%p, %04X);\n", hmmio, uFlags);
734 if ((wm = MMIO_Get(hmmio)) == NULL)
735 return MMSYSERR_INVALHANDLE;
737 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
738 return result;
740 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
742 MMIO_SetBuffer(wm, NULL, 0, 0);
744 wm->ioProc->count--;
746 if (wm->bTmpIOProc)
747 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
748 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
750 MMIO_Destroy(wm);
752 return result;
755 /**************************************************************************
756 * mmioRead [WINMM.@]
758 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
760 LPWINE_MMIO wm;
761 LONG count;
763 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
765 if ((wm = MMIO_Get(hmmio)) == NULL)
766 return -1;
768 /* unbuffered case first */
769 if (!wm->info.pchBuffer)
770 return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
772 /* first try from current buffer */
773 if (wm->info.pchNext != wm->info.pchEndRead) {
774 count = wm->info.pchEndRead - wm->info.pchNext;
775 if (count > cch || count < 0) count = cch;
776 memcpy(pch, wm->info.pchNext, count);
777 wm->info.pchNext += count;
778 pch += count;
779 cch -= count;
780 } else
781 count = 0;
783 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
784 assert(wm->info.cchBuffer);
786 while (cch) {
787 LONG size;
789 size = MMIO_GrabNextBuffer(wm, TRUE);
790 if (size <= 0) break;
791 if (size > cch) size = cch;
792 memcpy(pch, wm->info.pchBuffer, size);
793 wm->info.pchNext += size;
794 pch += size;
795 cch -= size;
796 count += size;
800 TRACE("count=%d\n", count);
801 return count;
804 /**************************************************************************
805 * mmioWrite [WINMM.@]
807 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
809 LPWINE_MMIO wm;
810 LONG count;
812 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
814 if ((wm = MMIO_Get(hmmio)) == NULL)
815 return -1;
817 if (wm->info.cchBuffer) {
818 LONG bytesW = 0;
820 count = 0;
821 while (cch) {
822 if (wm->info.pchNext != wm->info.pchEndWrite) {
823 count = wm->info.pchEndWrite - wm->info.pchNext;
824 if (count > cch || count < 0) count = cch;
825 memcpy(wm->info.pchNext, pch, count);
826 wm->info.pchNext += count;
827 pch += count;
828 cch -= count;
829 bytesW += count;
830 wm->info.dwFlags |= MMIO_DIRTY;
831 } else {
832 if (wm->info.fccIOProc == FOURCC_MEM) {
833 if (wm->info.adwInfo[0]) {
834 /* from where would we get the memory handle? */
835 FIXME("memory file expansion not implemented!\n");
836 break;
837 } else break;
841 if (wm->info.pchNext == wm->info.pchEndWrite)
843 MMIO_Flush(wm, MMIO_EMPTYBUF);
844 MMIO_GrabNextBuffer(wm, FALSE);
846 else break;
848 count = bytesW;
849 } else {
850 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
851 wm->info.lBufOffset = wm->info.lDiskOffset;
854 TRACE("bytes written=%d\n", count);
855 return count;
858 /**************************************************************************
859 * mmioSeek [WINMM.@]
861 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
863 LPWINE_MMIO wm;
864 LONG offset;
866 TRACE("(%p, %08X, %d);\n", hmmio, lOffset, iOrigin);
868 if ((wm = MMIO_Get(hmmio)) == NULL)
869 return MMSYSERR_INVALHANDLE;
871 /* not buffered, direct seek on file */
872 if (!wm->info.pchBuffer)
873 return send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, iOrigin, FALSE);
875 switch (iOrigin) {
876 case SEEK_SET:
877 offset = lOffset;
878 break;
879 case SEEK_CUR:
880 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
881 break;
882 case SEEK_END:
883 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
884 break;
885 default:
886 return -1;
889 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
890 /* should check that write mode exists */
891 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
892 return -1;
893 wm->info.lBufOffset = offset;
894 wm->info.pchEndRead = wm->info.pchBuffer;
895 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
896 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
897 wm->info.lDiskOffset = wm->dwFileSize;
899 } else if ((wm->info.cchBuffer > 0) &&
900 ((offset < wm->info.lBufOffset) ||
901 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
902 !wm->bBufferLoaded)) {
903 /* stay in same buffer ? */
904 /* some memory mapped buffers are defined with -1 as a size */
906 /* condition to change buffer */
907 if ((wm->info.fccIOProc == FOURCC_MEM) ||
908 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
909 /* this also sets the wm->info.lDiskOffset field */
910 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
911 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
912 SEEK_SET, FALSE) == -1)
913 return -1;
914 MMIO_GrabNextBuffer(wm, TRUE);
917 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
919 TRACE("=> %d\n", offset);
920 return offset;
923 /**************************************************************************
924 * mmioGetInfo [WINMM.@]
926 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
928 LPWINE_MMIO wm;
930 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
932 if ((wm = MMIO_Get(hmmio)) == NULL)
933 return MMSYSERR_INVALHANDLE;
935 *lpmmioinfo = wm->info;
937 return MMSYSERR_NOERROR;
940 /**************************************************************************
941 * mmioSetInfo [WINMM.@]
943 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
945 LPWINE_MMIO wm;
947 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
949 if ((wm = MMIO_Get(hmmio)) == NULL)
950 return MMSYSERR_INVALHANDLE;
952 /* check pointers coherence */
953 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
954 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
955 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
956 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
957 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
958 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
959 return MMSYSERR_INVALPARAM;
961 wm->info.pchNext = lpmmioinfo->pchNext;
962 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
964 return MMSYSERR_NOERROR;
967 /**************************************************************************
968 * mmioSetBuffer [WINMM.@]
970 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
972 LPWINE_MMIO wm;
974 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%d, uFlags=%#08x)\n",
975 hmmio, pchBuffer, cchBuffer, uFlags);
977 if ((wm = MMIO_Get(hmmio)) == NULL)
978 return MMSYSERR_INVALHANDLE;
980 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
983 /**************************************************************************
984 * mmioFlush [WINMM.@]
986 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
988 LPWINE_MMIO wm;
990 TRACE("(%p, %04X)\n", hmmio, uFlags);
992 if ((wm = MMIO_Get(hmmio)) == NULL)
993 return MMSYSERR_INVALHANDLE;
995 return MMIO_Flush(wm, uFlags);
998 /**************************************************************************
999 * mmioAdvance [WINMM.@]
1001 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1003 LPWINE_MMIO wm;
1005 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1007 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1008 * here. be sure if you change something here to check mmioAdvance16 as well
1010 if ((wm = MMIO_Get(hmmio)) == NULL)
1011 return MMSYSERR_INVALHANDLE;
1013 if (!wm->info.cchBuffer)
1014 return MMIOERR_UNBUFFERED;
1016 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1017 return MMSYSERR_INVALPARAM;
1019 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1021 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
1022 send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1023 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1024 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1026 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1027 return MMIOERR_CANNOTWRITE;
1029 if (lpmmioinfo) {
1030 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1031 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1033 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1035 if (lpmmioinfo) {
1036 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1037 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1038 (wm->info.pchEndRead - wm->info.pchBuffer);
1039 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1040 (wm->info.pchEndWrite - wm->info.pchBuffer);
1041 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1042 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1044 return MMSYSERR_NOERROR;
1047 /**************************************************************************
1048 * mmioStringToFOURCCA [WINMM.@]
1050 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1052 CHAR cc[4];
1053 int i = 0;
1055 for (i = 0; i < 4 && sz[i]; i++) {
1056 if (uFlags & MMIO_TOUPPER) {
1057 cc[i] = toupper(sz[i]);
1058 } else {
1059 cc[i] = sz[i];
1063 /* Pad with spaces */
1064 while (i < 4) cc[i++] = ' ';
1066 TRACE("Got '%.4s'\n",cc);
1067 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1070 /**************************************************************************
1071 * mmioStringToFOURCCW [WINMM.@]
1073 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1075 char szA[4];
1077 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1078 return mmioStringToFOURCCA(szA,uFlags);
1081 /**************************************************************************
1082 * mmioInstallIOProcA [WINMM.@]
1084 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1085 LPMMIOPROC pIOProc, DWORD dwFlags)
1087 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, FALSE);
1090 /**************************************************************************
1091 * mmioInstallIOProcW [WINMM.@]
1093 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1094 LPMMIOPROC pIOProc, DWORD dwFlags)
1096 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, TRUE);
1099 /******************************************************************
1100 * MMIO_SendMessage
1104 static LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1105 LPARAM lParam2, BOOL is_unicode)
1107 LPWINE_MMIO wm;
1109 TRACE("(%p, %u, %ld, %ld, %s)\n", hmmio, uMessage, lParam1, lParam2, is_unicode ? "unicode" : "ansi");
1111 if (uMessage < MMIOM_USER)
1112 return MMSYSERR_INVALPARAM;
1114 if ((wm = MMIO_Get(hmmio)) == NULL)
1115 return MMSYSERR_INVALHANDLE;
1117 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, is_unicode);
1120 /**************************************************************************
1121 * mmioSendMessage [WINMM.@]
1123 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1124 LPARAM lParam1, LPARAM lParam2)
1126 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, FALSE);
1129 /**************************************************************************
1130 * mmioDescend [WINMM.@]
1132 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1133 const MMCKINFO* lpckParent, UINT uFlags)
1135 DWORD dwOldPos;
1136 FOURCC srchCkId;
1137 FOURCC srchType;
1139 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1141 if (lpck == NULL)
1142 return MMSYSERR_INVALPARAM;
1144 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1145 TRACE("dwOldPos=%d\n", dwOldPos);
1147 if (lpckParent != NULL) {
1148 TRACE("seek inside parent at %d !\n", lpckParent->dwDataOffset);
1149 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1150 if (dwOldPos < lpckParent->dwDataOffset ||
1151 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1152 WARN("outside parent chunk\n");
1153 return MMIOERR_CHUNKNOTFOUND;
1157 /* The SDK docu says 'ckid' is used for all cases. Real World
1158 * examples disagree -Marcus,990216.
1161 srchCkId = 0;
1162 srchType = 0;
1164 /* find_chunk looks for 'ckid' */
1165 if (uFlags & MMIO_FINDCHUNK)
1166 srchCkId = lpck->ckid;
1168 /* find_riff and find_list look for 'fccType' */
1169 if (uFlags & MMIO_FINDLIST)
1171 srchCkId = FOURCC_LIST;
1172 srchType = lpck->fccType;
1175 if (uFlags & MMIO_FINDRIFF)
1177 srchCkId = FOURCC_RIFF;
1178 srchType = lpck->fccType;
1181 TRACE("searching for %4.4s.%4.4s\n",
1182 (LPCSTR)&srchCkId, srchType ? (LPCSTR)&srchType : "any");
1184 while (TRUE)
1186 LONG ix;
1188 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1189 if (ix < 2*sizeof(DWORD))
1191 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1192 WARN("return ChunkNotFound\n");
1193 return MMIOERR_CHUNKNOTFOUND;
1196 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1197 TRACE("ckid=%4.4s fcc=%4.4s cksize=%08X !\n",
1198 (LPCSTR)&lpck->ckid,
1199 srchType ? (LPCSTR)&lpck->fccType:"<na>",
1200 lpck->cksize);
1201 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1202 (!srchType || (srchType == lpck->fccType)) )
1203 break;
1205 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1206 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1209 lpck->dwFlags = 0;
1210 /* If we were looking for RIFF/LIST chunks, the final file position
1211 * is after the chunkid. If we were just looking for the chunk
1212 * it is after the cksize. So add 4 in RIFF/LIST case.
1214 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1215 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1216 else
1218 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1219 lpck->fccType = 0;
1221 TRACE("lpck: ckid=%.4s, cksize=%d, dwDataOffset=%d fccType=%08X (%.4s)!\n",
1222 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1223 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1224 return MMSYSERR_NOERROR;
1227 /**************************************************************************
1228 * mmioAscend [WINMM.@]
1230 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1232 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1234 if (lpck->dwFlags & MMIO_DIRTY) {
1235 DWORD dwOldPos, dwNewSize;
1237 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1238 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1239 TRACE("dwOldPos=%d lpck->dwDataOffset = %d\n", dwOldPos, lpck->dwDataOffset);
1240 dwNewSize = dwOldPos - lpck->dwDataOffset;
1241 if (dwNewSize != lpck->cksize) {
1242 TRACE("Nope: lpck->cksize=%d dwNewSize=%d\n", lpck->cksize, dwNewSize);
1243 lpck->cksize = dwNewSize;
1245 /* pad odd size with 0 */
1246 if (dwNewSize & 1) {
1247 char ch = 0;
1248 mmioWrite(hmmio, &ch, 1);
1250 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1251 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1253 lpck->dwFlags = 0;
1256 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1258 return MMSYSERR_NOERROR;
1261 /**************************************************************************
1262 * mmioCreateChunk [WINMM.@]
1264 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1266 DWORD dwOldPos;
1267 LONG size;
1268 LONG ix;
1270 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1272 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1273 TRACE("dwOldPos=%d\n", dwOldPos);
1275 if (uFlags == MMIO_CREATELIST)
1276 lpck->ckid = FOURCC_LIST;
1277 else if (uFlags == MMIO_CREATERIFF)
1278 lpck->ckid = FOURCC_RIFF;
1280 TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1282 size = 2 * sizeof(DWORD);
1283 lpck->dwDataOffset = dwOldPos + size;
1285 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1286 size += sizeof(DWORD);
1287 lpck->dwFlags = MMIO_DIRTY;
1289 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1290 TRACE("after mmioWrite ix = %d req = %d, errno = %d\n", ix, size, errno);
1291 if (ix < size) {
1292 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1293 WARN("return CannotWrite\n");
1294 return MMIOERR_CANNOTWRITE;
1297 return MMSYSERR_NOERROR;
1300 /**************************************************************************
1301 * mmioRenameA [WINMM.@]
1303 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1304 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1306 struct IOProcList* ioProc = NULL;
1307 struct IOProcList tmp;
1308 FOURCC fcc;
1310 TRACE("('%s', '%s', %p, %08X);\n",
1311 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1313 /* If both params are NULL, then parse the file name */
1314 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1316 fcc = MMIO_ParseExtA(szFileName);
1317 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1320 /* Handle any unhandled/error case from above. Assume DOS file */
1321 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1322 ioProc = MMIO_FindProcNode(FOURCC_DOS);
1323 /* if just the four character code is present, look up IO proc */
1324 else if (lpmmioinfo->pIOProc == NULL)
1325 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1326 else /* use relevant ioProc */
1328 ioProc = &tmp;
1329 tmp.fourCC = lpmmioinfo->fccIOProc;
1330 tmp.pIOProc = lpmmioinfo->pIOProc;
1331 tmp.is_unicode = FALSE;
1332 tmp.count = 1;
1335 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1336 * or make a copy of it because it's const ???
1338 return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1339 (LPARAM)szFileName, (LPARAM)szNewFileName, FALSE);
1342 /**************************************************************************
1343 * mmioRenameW [WINMM.@]
1345 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1346 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1348 LPSTR szFn = NULL;
1349 LPSTR sznFn = NULL;
1350 UINT ret = MMSYSERR_NOMEM;
1351 INT len;
1353 if (szFileName)
1355 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1356 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1357 if (!szFn) goto done;
1358 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1360 if (szNewFileName)
1362 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1363 sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1364 if (!sznFn) goto done;
1365 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1368 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1370 done:
1371 HeapFree(GetProcessHeap(),0,szFn);
1372 HeapFree(GetProcessHeap(),0,sznFn);
1373 return ret;