Release 20030408.
[wine/gsoc-2012-control.git] / dlls / winmm / mmio.c
blobf4300baa7376bb5f2f5ce09d444db6f6167c8aad
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * MMIO functions
5 * Copyright 1998 Andrew Taylor
6 * Copyright 1998 Ove Kåven
7 * Copyright 2000,2002 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* Still to be done:
25 * + correct handling of global/local IOProcs (and temporary IOProcs)
26 * + mode of mmio objects is not used (read vs write vs readwrite)
27 * + thread safeness
28 * + 32 A <=> W message mapping
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <assert.h>
38 #include "mmsystem.h"
39 #include "windef.h"
40 #include "heap.h"
41 #include "winemm.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
47 LRESULT (*pFnMmioCallback16)(SEGPTR,LPMMIOINFO,UINT,LPARAM,LPARAM) /* = NULL */;
49 /**************************************************************************
50 * mmioDosIOProc [internal]
52 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
53 LPARAM lParam1, LPARAM lParam2)
55 LRESULT ret = MMSYSERR_NOERROR;
57 TRACE("(%p, %X, 0x%lx, 0x%lx);\n", lpmmioinfo, uMessage, lParam1, lParam2);
59 switch (uMessage) {
60 case MMIOM_OPEN:
62 /* Parameters:
63 * lParam1 = szFileName parameter from mmioOpen
64 * lParam2 = reserved
65 * Returns: zero on success, error code on error
66 * NOTE: lDiskOffset automatically set to zero
68 LPCSTR szFileName = (LPCSTR)lParam1;
70 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
71 FIXME("MMIO_GETTEMP not implemented\n");
72 return MMIOERR_CANNOTOPEN;
75 /* if filename NULL, assume open file handle in adwInfo[0] */
76 if (szFileName) {
77 OFSTRUCT ofs;
78 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
80 if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
81 ret = MMIOERR_CANNOTOPEN;
83 break;
85 case MMIOM_CLOSE:
86 /* Parameters:
87 * lParam1 = wFlags parameter from mmioClose
88 * lParam2 = unused
89 * Returns: zero on success, error code on error
91 if (!(lParam1 & MMIO_FHOPEN))
92 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
93 break;
95 case MMIOM_READ:
96 /* Parameters:
97 * lParam1 = huge pointer to read buffer
98 * lParam2 = number of bytes to read
99 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
100 * in wErrorRet)
102 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
103 if (ret != -1)
104 lpmmioinfo->lDiskOffset += ret;
106 break;
108 case MMIOM_WRITE:
109 case MMIOM_WRITEFLUSH:
110 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
112 /* Parameters:
113 * lParam1 = huge pointer to write buffer
114 * lParam2 = number of bytes to write
115 * Returns: number of bytes written, -1 for error (error code in
116 * wErrorRet)
118 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
119 if (ret != -1)
120 lpmmioinfo->lDiskOffset += ret;
121 break;
123 case MMIOM_SEEK:
124 /* Parameters:
125 * lParam1 = new position
126 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
127 * Returns: new file postion, -1 on error
129 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
130 if (ret != -1)
131 lpmmioinfo->lDiskOffset = ret;
132 return ret;
134 case MMIOM_RENAME:
135 /* Parameters:
136 * lParam1 = old name
137 * lParam2 = new name
138 * Returns: zero on success, non-zero on failure
140 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
141 ret = MMIOERR_FILENOTFOUND;
142 break;
144 default:
145 FIXME("unexpected message %u\n", uMessage);
146 return 0;
149 return ret;
152 /**************************************************************************
153 * mmioMemIOProc [internal]
155 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
156 LPARAM lParam1, LPARAM lParam2)
158 TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
160 switch (uMessage) {
162 case MMIOM_OPEN:
163 /* Parameters:
164 * lParam1 = filename (must be NULL)
165 * lParam2 = reserved
166 * Returns: zero on success, error code on error
167 * NOTE: lDiskOffset automatically set to zero
169 /* FIXME: io proc shouldn't change it */
170 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
171 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
172 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
173 return 0;
175 case MMIOM_CLOSE:
176 /* Parameters:
177 * lParam1 = wFlags parameter from mmioClose
178 * lParam2 = unused
179 * Returns: zero on success, error code on error
181 return 0;
183 case MMIOM_READ:
184 /* Parameters:
185 * lParam1 = huge pointer to read buffer
186 * lParam2 = number of bytes to read
187 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
188 * in wErrorRet)
189 * NOTE: lDiskOffset should be updated
191 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
192 return 0;
194 case MMIOM_WRITE:
195 case MMIOM_WRITEFLUSH:
196 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
198 /* Parameters:
199 * lParam1 = huge pointer to write buffer
200 * lParam2 = number of bytes to write
201 * Returns: number of bytes written, -1 for error (error code in
202 * wErrorRet)
203 * NOTE: lDiskOffset should be updated
205 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
206 return 0;
208 case MMIOM_SEEK:
209 /* Parameters:
210 * lParam1 = new position
211 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
212 * Returns: new file postion, -1 on error
213 * NOTE: lDiskOffset should be updated
215 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
216 return -1;
218 default:
219 FIXME("unexpected message %u\n", uMessage);
220 return 0;
223 return 0;
226 /* This array will be the entire list for most apps
227 * Note that temporary ioProcs will be stored with a 0 fourCC code
230 static struct IOProcList defaultProcs[] = {
231 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
232 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
235 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
237 /****************************************************************
238 * MMIO_FindProcNode [INTERNAL]
240 * Finds the ProcList node associated with a given FOURCC code.
242 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
244 struct IOProcList* pListNode;
246 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
247 if (pListNode->fourCC == fccIOProc) {
248 return pListNode;
251 return NULL;
254 /****************************************************************
255 * MMIO_InstallIOProc [INTERNAL]
257 LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
258 DWORD dwFlags, enum mmioProcType type)
260 LPMMIOPROC lpProc = NULL;
261 struct IOProcList* pListNode;
262 struct IOProcList** ppListNode;
264 TRACE("(%ld, %p, %08lX, %i)\n", fccIOProc, pIOProc, dwFlags, type);
266 if (dwFlags & MMIO_GLOBALPROC)
267 FIXME("Global procedures not implemented\n");
269 /* just handle the known procedures for now */
270 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
271 case MMIO_INSTALLPROC:
272 /* Create new entry for the IOProc list */
273 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
274 if (pListNode) {
275 /* Fill in this node */
276 pListNode->fourCC = fccIOProc;
277 pListNode->pIOProc = pIOProc;
278 pListNode->type = type;
279 pListNode->count = 0;
281 /* Stick it on the end of the list */
282 pListNode->pNext = pIOProcListAnchor;
283 pIOProcListAnchor = pListNode;
285 /* Return this IOProc - that's how the caller knows we succeeded */
286 lpProc = pIOProc;
288 break;
290 case MMIO_REMOVEPROC:
292 * Search for the node that we're trying to remove
293 * We search for a matching fourCC code if it's non null, or the proc
294 * address otherwise
295 * note that this method won't find the first item on the list, but
296 * since the first two items on this list are ones we won't
297 * let the user delete anyway, that's okay
299 ppListNode = &pIOProcListAnchor;
300 while ((*ppListNode) &&
301 ((fccIOProc != 0) ?
302 (*ppListNode)->fourCC != fccIOProc :
303 (*ppListNode)->pIOProc != pIOProc))
304 ppListNode = &((*ppListNode)->pNext);
306 if (*ppListNode) { /* found it */
307 /* FIXME: what should be done if an open mmio object uses this proc ?
308 * shall we return an error, nuke the mmio object ?
310 if ((*ppListNode)->count) {
311 ERR("Cannot remove a mmIOProc while in use\n");
312 break;
314 /* remove it, but only if it isn't builtin */
315 if ((*ppListNode) >= defaultProcs &&
316 (*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
317 WARN("Tried to remove built-in mmio proc. Skipping\n");
318 } else {
319 /* Okay, nuke it */
320 struct IOProcList* ptmpNode = *ppListNode;
321 lpProc = (*ppListNode)->pIOProc;
322 *ppListNode = (*ppListNode)->pNext;
323 HeapFree(GetProcessHeap(), 0, ptmpNode);
326 break;
328 case MMIO_FINDPROC:
329 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
330 lpProc = pListNode->pIOProc;
332 break;
335 return lpProc;
338 /****************************************************************
339 * send_message [INTERNAL]
341 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
342 DWORD wMsg, LPARAM lParam1,
343 LPARAM lParam2, enum mmioProcType type)
345 LRESULT result = MMSYSERR_ERROR;
346 LPARAM lp1 = lParam1, lp2 = lParam2;
348 if (!ioProc) {
349 ERR("brrr\n");
350 result = MMSYSERR_INVALPARAM;
353 switch (ioProc->type) {
354 case MMIO_PROC_16:
355 if (pFnMmioCallback16)
356 result = pFnMmioCallback16((SEGPTR)ioProc->pIOProc,
357 mmioinfo, wMsg, lp1, lp2);
358 break;
359 case MMIO_PROC_32A:
360 case MMIO_PROC_32W:
361 if (ioProc->type != type) {
362 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
363 FIXME("NIY 32 A<=>W mapping\n");
365 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
367 #if 0
368 if (ioProc->type != type) {
369 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
371 #endif
372 break;
373 default:
374 FIXME("Internal error\n");
377 return result;
380 /**************************************************************************
381 * MMIO_ParseExtA [internal]
383 * Parses a filename for the extension.
385 * RETURNS
386 * The FOURCC code for the extension if found, else 0.
388 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
390 /* Filenames are of the form file.ext+ABC
391 FIXME: What if a '+' is part of the file name?
392 For now, we take the last '+' present */
394 FOURCC ret = 0;
396 /* Note that ext{Start,End} point to the . and + respectively */
397 LPSTR extEnd;
399 TRACE("(%s)\n", debugstr_a(szFileName));
401 if (!szFileName)
402 return ret;
403 extEnd = strrchr(szFileName,'+');
404 if (extEnd) {
405 /* Need to parse to find the extension */
406 LPSTR extStart;
408 extStart = extEnd;
409 while (extStart >= szFileName && extStart[0] != '.') {
410 extStart--;
413 if (extStart < szFileName) {
414 ERR("+ but no . in szFileName: %s\n", debugstr_a(szFileName));
415 } else {
416 CHAR ext[5];
418 if (extEnd - extStart - 1 > 4)
419 WARN("Extension length > 4\n");
420 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
421 TRACE("Got extension: %s\n", debugstr_a(ext));
422 /* FOURCC codes identifying file-extensions must be uppercase */
423 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
426 return ret;
429 /**************************************************************************
430 * MMIO_Get [internal]
432 * Retrieves the mmio object from current process
434 LPWINE_MMIO MMIO_Get(HMMIO h)
436 LPWINE_MMIO wm = NULL;
438 EnterCriticalSection(&WINMM_IData->cs);
439 for (wm = WINMM_IData->lpMMIO; wm; wm = wm->lpNext) {
440 if (wm->info.hmmio == h)
441 break;
443 LeaveCriticalSection(&WINMM_IData->cs);
444 return wm;
447 /**************************************************************************
448 * MMIO_Create [internal]
450 * Creates an internal representation for a mmio instance
452 static LPWINE_MMIO MMIO_Create(void)
454 static WORD MMIO_counter = 0;
455 LPWINE_MMIO wm;
457 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
458 if (wm) {
459 EnterCriticalSection(&WINMM_IData->cs);
460 /* lookup next unallocated WORD handle, with a non NULL value */
461 while (++MMIO_counter == 0 || MMIO_Get(HMMIO_32(MMIO_counter)));
462 wm->info.hmmio = HMMIO_32(MMIO_counter);
463 wm->lpNext = WINMM_IData->lpMMIO;
464 WINMM_IData->lpMMIO = wm;
465 LeaveCriticalSection(&WINMM_IData->cs);
467 return wm;
470 /**************************************************************************
471 * MMIO_Destroy [internal]
473 * Destroys an internal representation for a mmio instance
475 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
477 LPWINE_MMIO* m;
479 EnterCriticalSection(&WINMM_IData->cs);
480 /* search for the matching one... */
481 m = &(WINMM_IData->lpMMIO);
482 while (*m && *m != wm) m = &(*m)->lpNext;
483 /* ...and destroy */
484 if (*m) {
485 *m = (*m)->lpNext;
486 HeapFree(GetProcessHeap(), 0, wm);
487 wm = NULL;
489 LeaveCriticalSection(&WINMM_IData->cs);
490 return wm ? FALSE : TRUE;
493 /****************************************************************
494 * MMIO_Flush [INTERNAL]
496 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
498 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
499 /* not quite sure what to do here, but I'll guess */
500 if (wm->info.dwFlags & MMIO_DIRTY) {
501 /* FIXME: error handling */
502 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
503 wm->info.lBufOffset, SEEK_SET, MMIO_PROC_32A);
504 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
505 (LPARAM)wm->info.pchBuffer,
506 wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
508 if (uFlags & MMIO_EMPTYBUF)
509 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
511 wm->info.dwFlags &= ~MMIO_DIRTY;
513 return MMSYSERR_NOERROR;
516 /***************************************************************************
517 * MMIO_GrabNextBuffer [INTERNAL]
519 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
521 LONG size = wm->info.cchBuffer;
523 TRACE("bo=%lx do=%lx of=%lx\n",
524 wm->info.lBufOffset, wm->info.lDiskOffset,
525 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
527 wm->info.lBufOffset = wm->info.lDiskOffset;
528 wm->info.pchNext = wm->info.pchBuffer;
529 wm->info.pchEndRead = wm->info.pchBuffer;
530 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
532 if (for_read) {
533 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
534 (LPARAM)wm->info.pchBuffer, size, MMIO_PROC_32A);
535 if (size > 0)
536 wm->info.pchEndRead += size;
539 wm->bBufferLoaded = TRUE;
540 return size;
543 /***************************************************************************
544 * MMIO_SetBuffer [INTERNAL]
546 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
547 UINT uFlags)
549 TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
551 if (uFlags) return MMSYSERR_INVALPARAM;
552 if (cchBuffer > 0xFFFF)
553 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
555 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
556 return MMIOERR_CANNOTWRITE;
558 /* free previous buffer if allocated */
559 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
560 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
561 wm->info.pchBuffer = NULL;
562 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
565 /* free segmented ptr mapping, if any */
566 if (wm->info.dwReserved1 != 0L)
568 UnMapLS(wm->info.dwReserved1);
569 wm->info.dwReserved1 = 0L;
572 if (pchBuffer) {
573 wm->info.pchBuffer = pchBuffer;
574 } else if (cchBuffer) {
575 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
576 return MMIOERR_OUTOFMEMORY;
577 wm->info.dwFlags |= MMIO_ALLOCBUF;
578 } else {
579 wm->info.pchBuffer = NULL;
582 wm->info.cchBuffer = cchBuffer;
583 wm->info.pchNext = wm->info.pchBuffer;
584 wm->info.pchEndRead = wm->info.pchBuffer;
585 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
586 wm->info.lBufOffset = 0;
587 wm->bBufferLoaded = FALSE;
589 return MMSYSERR_NOERROR;
592 /**************************************************************************
593 * MMIO_Open [internal]
595 HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags,
596 enum mmioProcType type)
598 LPWINE_MMIO wm;
599 MMIOINFO mmioinfo;
601 TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
603 if (!refmminfo) {
604 refmminfo = &mmioinfo;
606 mmioinfo.fccIOProc = 0;
607 mmioinfo.pIOProc = NULL;
608 mmioinfo.pchBuffer = NULL;
609 mmioinfo.cchBuffer = 0;
610 type = MMIO_PROC_32A;
613 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
614 char buffer[MAX_PATH];
616 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
617 return (HMMIO16)FALSE;
618 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == -1))
619 return (HMMIO)FALSE;
620 strcpy(szFileName, buffer);
621 return (HMMIO)TRUE;
624 if ((wm = MMIO_Create()) == NULL)
625 return 0;
627 /* If both params are NULL, then parse the file name if available */
628 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
629 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
630 /* Handle any unhandled/error case. Assume DOS file */
631 if (wm->info.fccIOProc == 0)
632 wm->info.fccIOProc = FOURCC_DOS;
633 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
634 wm->bTmpIOProc = FALSE;
636 /* if just the four character code is present, look up IO proc */
637 else if (refmminfo->pIOProc == NULL) {
638 wm->info.fccIOProc = refmminfo->fccIOProc;
639 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
640 wm->bTmpIOProc = FALSE;
642 /* if IO proc specified, use it and specified four character code */
643 else {
644 wm->info.fccIOProc = refmminfo->fccIOProc;
645 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
646 MMIO_INSTALLPROC, type);
647 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
648 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
649 wm->bTmpIOProc = TRUE;
652 wm->bBufferLoaded = FALSE;
653 wm->ioProc->count++;
655 if (dwOpenFlags & MMIO_ALLOCBUF) {
656 if ((refmminfo->wErrorRet = MMIO_SetBuffer(wm, NULL, MMIO_DEFAULTBUFFER, 0)))
657 goto error1;
658 } else if (wm->info.fccIOProc == FOURCC_MEM) {
659 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
660 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
661 goto error1;
662 wm->bBufferLoaded = TRUE;
663 } /* else => unbuffered, wm->info.pchBuffer == NULL */
665 /* see mmioDosIOProc for that one */
666 wm->info.adwInfo[0] = refmminfo->adwInfo[0];
667 wm->info.dwFlags = dwOpenFlags;
669 /* call IO proc to actually open file */
670 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
671 (LPARAM)szFileName, 0, MMIO_PROC_32A);
673 /* grab file size, when possible */
674 wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
676 if (refmminfo->wErrorRet == 0)
677 return wm->info.hmmio;
678 error1:
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 = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
694 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
696 HeapFree(GetProcessHeap(), 0, szFn);
697 return ret;
700 /**************************************************************************
701 * mmioOpenA [WINMM.@]
703 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
704 DWORD dwOpenFlags)
706 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
709 /**************************************************************************
710 * mmioClose [WINMM.@]
712 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
714 LPWINE_MMIO wm;
715 MMRESULT result;
717 TRACE("(%p, %04X);\n", hmmio, uFlags);
719 if ((wm = MMIO_Get(hmmio)) == NULL)
720 return MMSYSERR_INVALHANDLE;
722 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
723 return result;
725 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE,
726 uFlags, 0, MMIO_PROC_32A);
728 MMIO_SetBuffer(wm, NULL, 0, 0);
730 wm->ioProc->count--;
732 if (wm->bTmpIOProc)
733 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
734 MMIO_REMOVEPROC, wm->ioProc->type);
736 MMIO_Destroy(wm);
738 return result;
741 /**************************************************************************
742 * mmioRead [WINMM.@]
744 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
746 LPWINE_MMIO wm;
747 LONG count;
749 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
751 if ((wm = MMIO_Get(hmmio)) == NULL)
752 return -1;
754 /* unbuffered case first */
755 if (!wm->info.pchBuffer)
756 return send_message(wm->ioProc, &wm->info, MMIOM_READ,
757 (LPARAM)pch, cch, MMIO_PROC_32A);
759 /* first try from current buffer */
760 if (wm->info.pchNext != wm->info.pchEndRead) {
761 count = wm->info.pchEndRead - wm->info.pchNext;
762 if (count > cch || count < 0) count = cch;
763 memcpy(pch, wm->info.pchNext, count);
764 wm->info.pchNext += count;
765 pch += count;
766 cch -= count;
767 } else
768 count = 0;
770 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
771 assert(wm->info.cchBuffer);
773 while (cch) {
774 LONG size;
776 size = MMIO_GrabNextBuffer(wm, TRUE);
777 if (size <= 0) break;
778 if (size > cch) size = cch;
779 memcpy(pch, wm->info.pchBuffer, size);
780 wm->info.pchNext += size;
781 pch += size;
782 cch -= size;
783 count += size;
787 TRACE("count=%ld\n", count);
788 return count;
791 /**************************************************************************
792 * mmioWrite [WINMM.@]
794 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
796 LPWINE_MMIO wm;
797 LONG count;
799 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
801 if ((wm = MMIO_Get(hmmio)) == NULL)
802 return -1;
804 if (wm->info.cchBuffer) {
805 LONG bytesW = 0;
807 count = 0;
808 while (cch) {
809 if (wm->info.pchNext != wm->info.pchEndWrite) {
810 count = wm->info.pchEndWrite - wm->info.pchNext;
811 if (count > cch || count < 0) count = cch;
812 memcpy(wm->info.pchNext, pch, count);
813 wm->info.pchNext += count;
814 pch += count;
815 cch -= count;
816 bytesW += count;
817 wm->info.dwFlags |= MMIO_DIRTY;
818 } else {
819 if (wm->info.fccIOProc == FOURCC_MEM) {
820 if (wm->info.adwInfo[0]) {
821 /* from where would we get the memory handle? */
822 FIXME("memory file expansion not implemented!\n");
823 break;
824 } else break;
828 if (wm->info.pchNext == wm->info.pchEndWrite)
830 MMIO_Flush(wm, MMIO_EMPTYBUF);
831 MMIO_GrabNextBuffer(wm, FALSE);
833 else break;
835 count = bytesW;
836 } else {
837 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
838 (LPARAM)pch, cch, MMIO_PROC_32A);
839 wm->info.lBufOffset = wm->info.lDiskOffset;
842 TRACE("bytes written=%ld\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, %08lX, %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,
862 lOffset, iOrigin, MMIO_PROC_32A);
864 switch (iOrigin) {
865 case SEEK_SET:
866 offset = lOffset;
867 break;
868 case SEEK_CUR:
869 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
870 break;
871 case SEEK_END:
872 offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
873 break;
874 default:
875 return -1;
878 if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
879 /* should check that write mode exists */
880 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
881 return -1;
882 wm->info.lBufOffset = offset;
883 wm->info.pchEndRead = wm->info.pchBuffer;
884 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
885 if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
886 wm->info.lDiskOffset = wm->dwFileSize;
888 } else if ((wm->info.cchBuffer > 0) &&
889 ((offset < wm->info.lBufOffset) ||
890 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
891 !wm->bBufferLoaded)) {
892 /* stay in same buffer ? */
893 /* some memory mapped buffers are defined with -1 as a size */
895 /* condition to change buffer */
896 if ((wm->info.fccIOProc == FOURCC_MEM) ||
897 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
898 /* this also sets the wm->info.lDiskOffset field */
899 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
900 (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
901 SEEK_SET, MMIO_PROC_32A) == -1)
902 return -1;
903 MMIO_GrabNextBuffer(wm, TRUE);
906 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
908 TRACE("=> %ld\n", offset);
909 return offset;
912 /**************************************************************************
913 * mmioGetInfo [WINMM.@]
915 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
917 LPWINE_MMIO wm;
919 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
921 if ((wm = MMIO_Get(hmmio)) == NULL)
922 return MMSYSERR_INVALHANDLE;
924 memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
925 /* don't expose 16 bit ioproc:s */
926 if (wm->ioProc->type != MMIO_PROC_16)
927 lpmmioinfo->pIOProc = wm->ioProc->pIOProc;
929 return MMSYSERR_NOERROR;
932 /**************************************************************************
933 * mmioSetInfo [WINMM.@]
935 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
937 LPWINE_MMIO wm;
939 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
941 if ((wm = MMIO_Get(hmmio)) == NULL)
942 return MMSYSERR_INVALHANDLE;
944 /* check pointers coherence */
945 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
946 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
947 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
948 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
949 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
950 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
951 return MMSYSERR_INVALPARAM;
953 wm->info.pchNext = lpmmioinfo->pchNext;
954 wm->info.pchEndRead = lpmmioinfo->pchEndRead;
956 return MMSYSERR_NOERROR;
959 /**************************************************************************
960 * mmioSetBuffer [WINMM.@]
962 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
964 LPWINE_MMIO wm;
966 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
967 hmmio, pchBuffer, cchBuffer, uFlags);
969 if ((wm = MMIO_Get(hmmio)) == NULL)
970 return MMSYSERR_INVALHANDLE;
972 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
975 /**************************************************************************
976 * mmioFlush [WINMM.@]
978 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
980 LPWINE_MMIO wm;
982 TRACE("(%p, %04X)\n", hmmio, uFlags);
984 if ((wm = MMIO_Get(hmmio)) == NULL)
985 return MMSYSERR_INVALHANDLE;
987 return MMIO_Flush(wm, uFlags);
990 /**************************************************************************
991 * mmioAdvance [WINMM.@]
993 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
995 LPWINE_MMIO wm;
997 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
999 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1000 * here. be sure if you change something here to check mmioAdvance16 as well
1002 if ((wm = MMIO_Get(hmmio)) == NULL)
1003 return MMSYSERR_INVALHANDLE;
1005 if (!wm->info.cchBuffer)
1006 return MMIOERR_UNBUFFERED;
1008 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1009 return MMSYSERR_INVALPARAM;
1011 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1013 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
1014 lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1015 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
1016 (LPARAM)lpmmioinfo->pchBuffer,
1017 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1018 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1020 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1021 return MMIOERR_CANNOTWRITE;
1023 if (lpmmioinfo) {
1024 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1025 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1027 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1029 if (lpmmioinfo) {
1030 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1031 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1032 (wm->info.pchEndRead - wm->info.pchBuffer);
1033 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1034 (wm->info.pchEndWrite - wm->info.pchBuffer);
1035 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1036 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1038 return MMSYSERR_NOERROR;
1041 /**************************************************************************
1042 * mmioStringToFOURCCA [WINMM.@]
1044 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1046 CHAR cc[4];
1047 int i = 0;
1049 for (i = 0; i < 4 && sz[i]; i++) {
1050 if (uFlags & MMIO_TOUPPER) {
1051 cc[i] = toupper(sz[i]);
1052 } else {
1053 cc[i] = sz[i];
1057 /* Pad with spaces */
1058 while (i < 4) cc[i++] = ' ';
1060 TRACE("Got '%.4s'\n",cc);
1061 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1064 /**************************************************************************
1065 * mmioStringToFOURCCW [WINMM.@]
1067 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1069 LPSTR szA = HEAP_strdupWtoA(GetProcessHeap(),0,sz);
1070 FOURCC ret = mmioStringToFOURCCA(szA,uFlags);
1072 HeapFree(GetProcessHeap(), 0, szA);
1073 return ret;
1076 /**************************************************************************
1077 * mmioInstallIOProcA [WINMM.@]
1079 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1080 LPMMIOPROC pIOProc, DWORD dwFlags)
1082 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1085 /**************************************************************************
1086 * mmioInstallIOProcW [WINMM.@]
1088 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1089 LPMMIOPROC pIOProc, DWORD dwFlags)
1091 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1094 /******************************************************************
1095 * MMIO_SendMessage
1099 LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1100 LPARAM lParam2, enum mmioProcType type)
1102 LPWINE_MMIO wm;
1104 TRACE("(%p, %u, %ld, %ld, %d)\n", hmmio, uMessage, lParam1, lParam2, type);
1106 if (uMessage < MMIOM_USER)
1107 return MMSYSERR_INVALPARAM;
1109 if ((wm = MMIO_Get(hmmio)) == NULL)
1110 return MMSYSERR_INVALHANDLE;
1112 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, type);
1115 /**************************************************************************
1116 * mmioSendMessage [WINMM.@]
1118 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1119 LPARAM lParam1, LPARAM lParam2)
1121 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1124 /**************************************************************************
1125 * mmioDescend [WINMM.@]
1127 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1128 const MMCKINFO* lpckParent, UINT uFlags)
1130 DWORD dwOldPos;
1131 FOURCC srchCkId;
1132 FOURCC srchType;
1135 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1137 if (lpck == NULL)
1138 return MMSYSERR_INVALPARAM;
1140 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1141 TRACE("dwOldPos=%ld\n", dwOldPos);
1143 if (lpckParent != NULL) {
1144 TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1145 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1146 if (dwOldPos < lpckParent->dwDataOffset ||
1147 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1148 WARN("outside parent chunk\n");
1149 return MMIOERR_CHUNKNOTFOUND;
1153 /* The SDK docu says 'ckid' is used for all cases. Real World
1154 * examples disagree -Marcus,990216.
1157 srchType = 0;
1158 /* find_chunk looks for 'ckid' */
1159 if (uFlags & MMIO_FINDCHUNK)
1160 srchCkId = lpck->ckid;
1161 /* find_riff and find_list look for 'fccType' */
1162 if (uFlags & MMIO_FINDLIST) {
1163 srchCkId = FOURCC_LIST;
1164 srchType = lpck->fccType;
1166 if (uFlags & MMIO_FINDRIFF) {
1167 srchCkId = FOURCC_RIFF;
1168 srchType = lpck->fccType;
1171 if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1172 TRACE("searching for %.4s.%.4s\n",
1173 (LPSTR)&srchCkId,
1174 srchType?(LPSTR)&srchType:"any ");
1176 while (TRUE) {
1177 LONG ix;
1179 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1180 if (ix < 2*sizeof(DWORD)) {
1181 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1182 WARN("return ChunkNotFound\n");
1183 return MMIOERR_CHUNKNOTFOUND;
1185 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1186 if (ix < lpck->dwDataOffset - dwOldPos) {
1187 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1188 WARN("return ChunkNotFound\n");
1189 return MMIOERR_CHUNKNOTFOUND;
1191 TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1192 (LPSTR)&lpck->ckid,
1193 srchType?(LPSTR)&lpck->fccType:"<na>",
1194 lpck->cksize);
1195 if ((srchCkId == lpck->ckid) &&
1196 (!srchType || (srchType == lpck->fccType))
1198 break;
1200 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1201 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1203 } else {
1204 /* FIXME: unverified, does it do this? */
1205 /* NB: This part is used by WAVE_mciOpen, among others */
1206 if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1207 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1208 WARN("return ChunkNotFound 2nd\n");
1209 return MMIOERR_CHUNKNOTFOUND;
1211 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1213 lpck->dwFlags = 0;
1214 /* If we were looking for RIFF/LIST chunks, the final file position
1215 * is after the chunkid. If we were just looking for the chunk
1216 * it is after the cksize. So add 4 in RIFF/LIST case.
1218 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1219 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1220 else
1221 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1222 TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
1223 (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1224 lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1225 return MMSYSERR_NOERROR;
1228 /**************************************************************************
1229 * mmioAscend [WINMM.@]
1231 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1233 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1235 if (lpck->dwFlags & MMIO_DIRTY) {
1236 DWORD dwOldPos, dwNewSize;
1238 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1239 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1240 TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1241 dwNewSize = dwOldPos - lpck->dwDataOffset;
1242 if (dwNewSize != lpck->cksize) {
1243 TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1244 lpck->cksize = dwNewSize;
1246 /* pad odd size with 0 */
1247 if (dwNewSize & 1) {
1248 char ch = 0;
1249 mmioWrite(hmmio, &ch, 1);
1251 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1252 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1254 lpck->dwFlags = 0;
1257 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1259 return MMSYSERR_NOERROR;
1262 /**************************************************************************
1263 * mmioCreateChunk [WINMM.@]
1265 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1267 DWORD dwOldPos;
1268 LONG size;
1269 LONG ix;
1271 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1273 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1274 TRACE("dwOldPos=%ld\n", dwOldPos);
1276 if (uFlags == MMIO_CREATELIST)
1277 lpck->ckid = FOURCC_LIST;
1278 else if (uFlags == MMIO_CREATERIFF)
1279 lpck->ckid = FOURCC_RIFF;
1281 TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1283 size = 2 * sizeof(DWORD);
1284 lpck->dwDataOffset = dwOldPos + size;
1286 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1287 size += sizeof(DWORD);
1288 lpck->dwFlags = MMIO_DIRTY;
1290 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1291 TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
1292 if (ix < size) {
1293 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1294 WARN("return CannotWrite\n");
1295 return MMIOERR_CANNOTWRITE;
1298 return MMSYSERR_NOERROR;
1301 /**************************************************************************
1302 * mmioRenameA [WINMM.@]
1304 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1305 MMIOINFO* lpmmioinfo, DWORD dwFlags)
1307 struct IOProcList* ioProc = NULL;
1308 struct IOProcList tmp;
1310 TRACE("('%s', '%s', %p, %08lX);\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)
1315 lpmmioinfo->fccIOProc = MMIO_ParseExtA(szFileName);
1317 /* Handle any unhandled/error case from above. Assume DOS file */
1318 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1319 ioProc = MMIO_FindProcNode(FOURCC_DOS);
1320 /* if just the four character code is present, look up IO proc */
1321 else if (lpmmioinfo->pIOProc == NULL)
1322 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1323 else /* use relevant ioProc */
1325 ioProc = &tmp;
1326 tmp.fourCC = lpmmioinfo->fccIOProc;
1327 tmp.pIOProc = lpmmioinfo->pIOProc;
1328 tmp.type = MMIO_PROC_32A;
1329 tmp.count = 1;
1332 return send_message(ioProc, lpmmioinfo, MMIOM_RENAME,
1333 (LPARAM)szFileName, (LPARAM)szNewFileName, MMIO_PROC_32A);
1336 /**************************************************************************
1337 * mmioRenameW [WINMM.@]
1339 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1340 MMIOINFO* lpmmioinfo, DWORD dwFlags)
1342 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
1343 LPSTR sznFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szNewFileName);
1344 UINT ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1346 HeapFree(GetProcessHeap(),0,szFn);
1347 HeapFree(GetProcessHeap(),0,sznFn);
1348 return ret;