quartz: Use nAvgBytesPerSec to calculate length.
[wine.git] / dlls / winmm / mmio.c
blobd8af7874f3229334c3d117177f7c5c27a8a49298
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_FILENOTFOUND;
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 position, -1 on error
130 if (lParam2 == SEEK_END)
131 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], -(LONG)lParam1, (LONG)lParam2);
132 else
133 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
134 if (ret != -1)
135 lpmmioinfo->lDiskOffset = ret;
136 return ret;
138 case MMIOM_RENAME:
139 /* Parameters:
140 * lParam1 = old name
141 * lParam2 = new name
142 * Returns: zero on success, non-zero on failure
144 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
145 ret = MMIOERR_FILENOTFOUND;
146 break;
148 default:
149 FIXME("unexpected message %u\n", uMessage);
150 return 0;
153 return ret;
156 /**************************************************************************
157 * mmioMemIOProc [internal]
159 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
160 LPARAM lParam1, LPARAM lParam2)
162 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
164 switch (uMessage) {
166 case MMIOM_OPEN:
167 /* Parameters:
168 * lParam1 = filename (must be NULL)
169 * lParam2 = reserved
170 * Returns: zero on success, error code on error
171 * NOTE: lDiskOffset automatically set to zero
173 /* FIXME: io proc shouldn't change it */
174 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
175 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
176 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
177 return 0;
179 case MMIOM_CLOSE:
180 /* Parameters:
181 * lParam1 = wFlags parameter from mmioClose
182 * lParam2 = unused
183 * Returns: zero on success, error code on error
185 return 0;
187 case MMIOM_READ:
188 /* Parameters:
189 * lParam1 = huge pointer to read buffer
190 * lParam2 = number of bytes to read
191 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
192 * in wErrorRet)
193 * NOTE: lDiskOffset should be updated
195 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
196 return 0;
198 case MMIOM_WRITE:
199 case MMIOM_WRITEFLUSH:
200 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
202 /* Parameters:
203 * lParam1 = huge pointer to write buffer
204 * lParam2 = number of bytes to write
205 * Returns: number of bytes written, -1 for error (error code in
206 * wErrorRet)
207 * NOTE: lDiskOffset should be updated
209 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
210 return 0;
212 case MMIOM_SEEK:
213 /* Parameters:
214 * lParam1 = new position
215 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
216 * Returns: new file position, -1 on error
217 * NOTE: lDiskOffset should be updated
219 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
220 return -1;
222 default:
223 FIXME("unexpected message %u\n", uMessage);
224 return 0;
228 /* This array will be the entire list for most apps
229 * Note that temporary ioProcs will be stored with a 0 fourCC code
232 static struct IOProcList defaultProcs[] = {
233 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, FALSE, 0},
234 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, FALSE, 0},
237 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
239 /****************************************************************
240 * MMIO_FindProcNode [INTERNAL]
242 * Finds the ProcList node associated with a given FOURCC code.
244 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
246 struct IOProcList* pListNode;
248 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
249 if (pListNode->fourCC == fccIOProc) {
250 return pListNode;
253 return NULL;
256 /****************************************************************
257 * MMIO_InstallIOProc [INTERNAL]
259 static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
260 DWORD dwFlags, BOOL is_unicode)
262 LPMMIOPROC lpProc = NULL;
263 struct IOProcList* pListNode;
264 struct IOProcList** ppListNode;
266 TRACE("(%08x, %p, %08X, %s)\n", fccIOProc, pIOProc, dwFlags, is_unicode ? "unicode" : "ansi");
268 if (dwFlags & MMIO_GLOBALPROC)
269 FIXME("Global procedures not implemented\n");
271 /* just handle the known procedures for now */
272 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
273 case MMIO_INSTALLPROC:
274 /* Create new entry for the IOProc list */
275 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
276 if (pListNode) {
277 /* Fill in this node */
278 pListNode->fourCC = fccIOProc;
279 pListNode->pIOProc = pIOProc;
280 pListNode->is_unicode = is_unicode;
281 pListNode->count = 0;
283 /* Stick it on the end of the list */
284 pListNode->pNext = pIOProcListAnchor;
285 pIOProcListAnchor = pListNode;
287 /* Return this IOProc - that's how the caller knows we succeeded */
288 lpProc = pIOProc;
290 break;
292 case MMIO_REMOVEPROC:
294 * Search for the node that we're trying to remove
295 * We search for a matching fourCC code if it's non null, or the proc
296 * address otherwise
297 * note that this method won't find the first item on the list, but
298 * since the first two items on this list are ones we won't
299 * let the user delete anyway, that's okay
301 ppListNode = &pIOProcListAnchor;
302 while ((*ppListNode) &&
303 ((fccIOProc != 0) ?
304 (*ppListNode)->fourCC != fccIOProc :
305 (*ppListNode)->pIOProc != pIOProc))
306 ppListNode = &((*ppListNode)->pNext);
308 if (*ppListNode) { /* found it */
309 /* FIXME: what should be done if an open mmio object uses this proc ?
310 * shall we return an error, nuke the mmio object ?
312 if ((*ppListNode)->count) {
313 ERR("Cannot remove a mmIOProc while in use\n");
314 break;
316 /* remove it, but only if it isn't builtin */
317 if ((*ppListNode) >= defaultProcs &&
318 (*ppListNode) < defaultProcs + sizeof(defaultProcs) / sizeof(defaultProcs[0])) {
319 WARN("Tried to remove built-in mmio proc. Skipping\n");
320 } else {
321 /* Okay, nuke it */
322 struct IOProcList* ptmpNode = *ppListNode;
323 lpProc = (*ppListNode)->pIOProc;
324 *ppListNode = (*ppListNode)->pNext;
325 HeapFree(GetProcessHeap(), 0, ptmpNode);
328 break;
330 case MMIO_FINDPROC:
331 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
332 lpProc = pListNode->pIOProc;
334 break;
337 return lpProc;
340 /****************************************************************
341 * send_message [INTERNAL]
343 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
344 DWORD wMsg, LPARAM lParam1,
345 LPARAM lParam2, BOOL is_unicode)
347 LRESULT result = MMSYSERR_ERROR;
348 LPARAM lp1 = lParam1, lp2 = lParam2;
350 if (!ioProc) {
351 ERR("ioProc NULL\n");
352 return MMSYSERR_INVALPARAM;
355 if (ioProc->is_unicode != is_unicode) {
356 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
357 FIXME("NIY 32 A<=>W mapping\n");
359 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
361 #if 0
362 if (ioProc->is_unicode != is_unicode) {
363 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
365 #endif
367 return result;
370 /**************************************************************************
371 * MMIO_ParseExtA [internal]
373 * Parses a filename for the extension.
375 * RETURNS
376 * The FOURCC code for the extension if found, else 0.
378 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
380 /* Filenames are of the form file.ext{+ABC}
381 For now, we take the last '+' if present */
383 FOURCC ret = 0;
385 /* Note that ext{Start,End} point to the . and + respectively */
386 LPCSTR extEnd;
387 LPCSTR extStart;
389 CHAR ext[5];
391 TRACE("(%s)\n", debugstr_a(szFileName));
393 if (!szFileName)
394 return ret;
396 /* Find the last '+' */
397 extEnd = strrchr(szFileName,'+');
399 if (!extEnd) {
400 /* No + so just an extension */
401 return ret;
402 } else {
403 /* Find the first '.' before '+' */
404 extStart = extEnd - 1;
405 while (extStart >= szFileName && *extStart != '.') {
406 extStart--;
408 if (extStart < szFileName) {
409 ERR("No extension in szFileName: %s\n", debugstr_a(szFileName));
410 return ret;
414 if (extEnd - extStart - 1 > 4)
415 WARN("Extension length > 4\n");
416 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
418 TRACE("Got extension: %s\n", debugstr_a(ext));
420 /* FOURCC codes identifying file-extensions must be uppercase */
421 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
423 return ret;
426 /**************************************************************************
427 * MMIO_Get [internal]
429 * Retrieves the mmio object from current process
431 static LPWINE_MMIO MMIO_Get(HMMIO h)
433 LPWINE_MMIO wm = NULL;
435 EnterCriticalSection(&WINMM_cs);
436 for (wm = MMIOList; wm; wm = wm->lpNext) {
437 if (wm->info.hmmio == h)
438 break;
440 LeaveCriticalSection(&WINMM_cs);
441 return wm;
444 /**************************************************************************
445 * MMIO_Create [internal]
447 * Creates an internal representation for a mmio instance
449 static LPWINE_MMIO MMIO_Create(void)
451 static WORD MMIO_counter = 0;
452 LPWINE_MMIO wm;
454 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
455 if (wm) {
456 EnterCriticalSection(&WINMM_cs);
457 /* lookup next unallocated WORD handle, with a non NULL value */
458 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
459 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
460 wm->lpNext = MMIOList;
461 MMIOList = wm;
462 LeaveCriticalSection(&WINMM_cs);
464 return wm;
467 /**************************************************************************
468 * MMIO_Destroy [internal]
470 * Destroys an internal representation for a mmio instance
472 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
474 LPWINE_MMIO* m;
476 EnterCriticalSection(&WINMM_cs);
477 /* search for the matching one... */
478 m = &MMIOList;
479 while (*m && *m != wm) m = &(*m)->lpNext;
480 /* ...and destroy */
481 if (*m) {
482 *m = (*m)->lpNext;
483 HeapFree(GetProcessHeap(), 0, wm);
484 wm = NULL;
486 LeaveCriticalSection(&WINMM_cs);
487 return !wm;
490 /****************************************************************
491 * MMIO_Flush [INTERNAL]
493 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
495 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
496 /* not quite sure what to do here, but I'll guess */
497 if (wm->info.dwFlags & MMIO_DIRTY) {
498 /* FIXME: error handling */
499 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, wm->info.lBufOffset, SEEK_SET, FALSE);
500 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
501 (LPARAM)wm->info.pchBuffer,
502 wm->info.pchNext - wm->info.pchBuffer, FALSE);
504 if (uFlags & MMIO_EMPTYBUF)
505 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
507 wm->info.dwFlags &= ~MMIO_DIRTY;
509 return MMSYSERR_NOERROR;
512 /***************************************************************************
513 * MMIO_GrabNextBuffer [INTERNAL]
515 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
517 LONG size = wm->info.cchBuffer;
519 TRACE("bo=%x do=%x\n",
520 wm->info.lBufOffset, wm->info.lDiskOffset);
522 wm->info.lBufOffset = wm->info.lDiskOffset;
523 wm->info.pchNext = wm->info.pchBuffer;
524 wm->info.pchEndRead = wm->info.pchBuffer;
525 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
527 wm->bBufferLoaded = TRUE;
528 if (for_read) {
529 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
530 (LPARAM)wm->info.pchBuffer, size, FALSE);
531 if (size > 0)
532 wm->info.pchEndRead += size;
533 else
534 wm->bBufferLoaded = FALSE;
537 return size;
540 /***************************************************************************
541 * MMIO_SetBuffer [INTERNAL]
543 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
544 UINT uFlags)
546 TRACE("(%p %p %d %u)\n", wm, pchBuffer, cchBuffer, uFlags);
548 if (cchBuffer > 0xFFFF)
549 WARN("Untested handling of huge mmio buffers (%d >= 64k)\n", cchBuffer);
551 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
552 return MMIOERR_CANNOTWRITE;
554 /* free previous buffer if allocated */
555 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
556 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
557 wm->info.pchBuffer = NULL;
558 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
561 if (pchBuffer) {
562 wm->info.pchBuffer = pchBuffer;
563 } else if (cchBuffer) {
564 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
565 return MMIOERR_OUTOFMEMORY;
566 wm->info.dwFlags |= MMIO_ALLOCBUF;
567 } else {
568 wm->info.pchBuffer = NULL;
571 wm->info.cchBuffer = cchBuffer;
572 wm->info.pchNext = wm->info.pchBuffer;
573 wm->info.pchEndRead = wm->info.pchBuffer;
574 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
575 wm->info.lBufOffset = wm->info.lDiskOffset;
576 wm->bBufferLoaded = (wm->info.fccIOProc == FOURCC_MEM);
578 return MMSYSERR_NOERROR;
581 /**************************************************************************
582 * MMIO_Open [internal]
584 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, BOOL is_unicode)
586 LPWINE_MMIO wm;
587 MMIOINFO mmioinfo;
588 DWORD pos;
590 TRACE("(%s, %p, %08X, %s);\n", debugstr_a(szFileName), refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
592 if (!refmminfo) {
593 refmminfo = &mmioinfo;
594 memset(&mmioinfo, 0, sizeof(mmioinfo));
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 wm->ioProc = &defaultProcs[0];
622 else if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
623 /* If not found, assume DOS file */
624 wm->ioProc = &defaultProcs[0];
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 /* see mmioDosIOProc for that one */
659 memcpy( wm->info.adwInfo, refmminfo->adwInfo, sizeof(wm->info.adwInfo) );
661 /* call IO proc to actually open file */
662 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
663 (LPARAM)szFileName, 0, FALSE);
665 /* update offsets and grab file size, when possible */
666 if (wm->info.fccIOProc == FOURCC_DOS && (send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE)) != -1) {
667 pos = wm->info.lBufOffset = wm->info.lDiskOffset;
668 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_END, FALSE);
669 wm->dwFileSize = wm->info.lDiskOffset;
670 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, pos, SEEK_SET, FALSE);
672 else wm->dwFileSize = 0;
674 if (refmminfo->wErrorRet == 0)
675 return wm->info.hmmio;
676 error1:
677 if (wm->info.dwFlags & MMIO_ALLOCBUF)
678 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
679 if (wm->ioProc) wm->ioProc->count--;
680 error2:
681 MMIO_Destroy(wm);
682 return 0;
685 /**************************************************************************
686 * mmioOpenW [WINMM.@]
688 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
689 DWORD dwOpenFlags)
691 HMMIO ret;
692 LPSTR szFn = NULL;
694 if (szFileName)
696 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
697 szFn = HeapAlloc( GetProcessHeap(), 0, len );
698 if (!szFn) return NULL;
699 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
702 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
704 HeapFree(GetProcessHeap(), 0, szFn);
705 return ret;
708 /**************************************************************************
709 * mmioOpenA [WINMM.@]
711 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
712 DWORD dwOpenFlags)
714 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
717 /**************************************************************************
718 * mmioClose [WINMM.@]
720 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
722 LPWINE_MMIO wm;
723 MMRESULT result;
725 TRACE("(%p, %04X);\n", hmmio, uFlags);
727 if ((wm = MMIO_Get(hmmio)) == NULL)
728 return MMSYSERR_INVALHANDLE;
730 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
731 return result;
733 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
735 MMIO_SetBuffer(wm, NULL, 0, 0);
737 wm->ioProc->count--;
739 if (wm->bTmpIOProc)
740 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
741 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
743 MMIO_Destroy(wm);
745 return result;
748 /**************************************************************************
749 * mmioRead [WINMM.@]
751 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
753 LPWINE_MMIO wm;
754 LONG count;
756 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
758 if ((wm = MMIO_Get(hmmio)) == NULL)
759 return -1;
761 /* unbuffered case first */
762 if (!wm->info.pchBuffer)
763 return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
765 /* first try from current buffer */
766 if (cch && wm->info.fccIOProc != FOURCC_MEM && wm->info.pchNext == wm->info.pchEndRead)
767 MMIO_GrabNextBuffer(wm, TRUE);
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;
773 pch += count;
774 cch -= count;
775 } else
776 count = 0;
778 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
779 assert(wm->info.cchBuffer);
781 while (cch) {
782 LONG size;
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;
789 pch += size;
790 cch -= size;
791 count += size;
793 wm->bBufferLoaded = FALSE;
794 mmioSeek(hmmio, 0, SEEK_CUR);
797 TRACE("count=%d\n", count);
798 return count;
801 /**************************************************************************
802 * mmioWrite [WINMM.@]
804 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
806 LPWINE_MMIO wm;
807 LONG count;
809 TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
811 if ((wm = MMIO_Get(hmmio)) == NULL)
812 return -1;
814 if (wm->info.cchBuffer) {
815 LONG bytesW = 0;
817 while (cch) {
818 if (wm->info.pchNext != wm->info.pchEndWrite) {
819 count = wm->info.pchEndWrite - wm->info.pchNext;
820 if (count > cch || count < 0) count = cch;
821 memcpy(wm->info.pchNext, pch, count);
822 wm->info.pchNext += count;
823 pch += count;
824 cch -= count;
825 bytesW += count;
826 wm->info.dwFlags |= MMIO_DIRTY;
827 } else {
828 if (wm->info.fccIOProc == FOURCC_MEM) {
829 if (wm->info.adwInfo[0]) {
830 /* from where would we get the memory handle? */
831 FIXME("memory file expansion not implemented!\n");
832 break;
833 } else break;
837 if (wm->info.pchNext == wm->info.pchEndWrite)
839 MMIO_Flush(wm, MMIO_EMPTYBUF);
840 MMIO_GrabNextBuffer(wm, FALSE);
842 else break;
844 count = bytesW;
845 } else {
846 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
847 wm->info.lBufOffset = wm->info.lDiskOffset;
850 TRACE("bytes written=%d\n", count);
851 return count;
854 /**************************************************************************
855 * mmioSeek [WINMM.@]
857 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
859 LPWINE_MMIO wm;
860 LONG offset;
862 TRACE("(%p, %08X, %d);\n", hmmio, lOffset, iOrigin);
864 if ((wm = MMIO_Get(hmmio)) == NULL)
865 return MMSYSERR_INVALHANDLE;
867 /* not buffered, direct seek on file */
868 if (!wm->info.pchBuffer && wm->info.fccIOProc != FOURCC_MEM) {
869 LRESULT ret = send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, iOrigin, FALSE);
870 if (ret != -1)
871 wm->info.lBufOffset = wm->info.lDiskOffset;
872 return ret;
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 switch (wm->info.fccIOProc) {
884 case FOURCC_MEM:
885 offset = wm->info.cchBuffer - lOffset;
886 break;
887 case FOURCC_DOS:
888 offset = wm->dwFileSize - lOffset;
889 break;
890 default:
891 offset = send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, SEEK_END, FALSE);
892 break;
894 break;
895 default:
896 return -1;
899 /* stay in same buffer ? */
900 /* some memory mapped buffers are defined with -1 as a size */
901 if ((wm->info.cchBuffer > 0) &&
902 ((offset < wm->info.lBufOffset) ||
903 (offset > wm->info.lBufOffset + wm->info.cchBuffer) ||
904 (offset > wm->dwFileSize && wm->info.fccIOProc == FOURCC_DOS) ||
905 !wm->bBufferLoaded)) {
907 /* condition to change buffer */
908 if ((wm->info.fccIOProc == FOURCC_MEM) ||
909 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
910 /* this also sets the wm->info.lDiskOffset field */
911 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
912 offset, SEEK_SET, FALSE) == -1)
913 return -1;
914 wm->info.lBufOffset = offset;
915 wm->bBufferLoaded = FALSE;
916 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
918 else
919 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
921 TRACE("=> %d\n", offset);
922 return offset;
925 /**************************************************************************
926 * mmioGetInfo [WINMM.@]
928 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
930 LPWINE_MMIO wm;
932 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
934 if ((wm = MMIO_Get(hmmio)) == NULL)
935 return MMSYSERR_INVALHANDLE;
937 *lpmmioinfo = wm->info;
939 return MMSYSERR_NOERROR;
942 /**************************************************************************
943 * mmioSetInfo [WINMM.@]
945 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
947 LPWINE_MMIO wm;
949 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
951 if ((wm = MMIO_Get(hmmio)) == NULL)
952 return MMSYSERR_INVALHANDLE;
954 /* check pointers coherence */
955 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
956 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
957 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
958 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
959 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
960 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
961 return MMSYSERR_INVALPARAM;
963 wm->info = *lpmmioinfo;
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 (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1020 return MMIOERR_CANNOTWRITE;
1021 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1023 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
1024 send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1025 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1026 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1029 if (lpmmioinfo && lpmmioinfo->fccIOProc == FOURCC_DOS) {
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 %s\n",debugstr_an(cc,4));
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 %s.%s\n",
1182 debugstr_an((LPCSTR)&srchCkId, 4), srchType ? debugstr_an((LPCSTR)&srchType, 4) : "<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=%s fcc=%s cksize=%08X !\n",
1198 debugstr_an((LPCSTR)&lpck->ckid, 4),
1199 srchType ? debugstr_an((LPCSTR)&lpck->fccType, 4) : "<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=%s, cksize=%d, dwDataOffset=%d fccType=%08X (%s)!\n",
1222 debugstr_an((LPSTR)&lpck->ckid, 4), lpck->cksize, lpck->dwDataOffset,
1223 lpck->fccType, srchType ? debugstr_an((LPSTR)&lpck->fccType, 4):"");
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=%s\n", debugstr_an((LPSTR)&lpck->ckid, 4));
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 = &defaultProcs[0];
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;