Fixed a compile error on non-i386.
[wine] / dlls / winmm / mmio.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3  * MMIO functions
4  *
5  * Copyright 1998 Andrew Taylor
6  * Copyright 1998 Ove Kåven
7  * Copyright 2000,2002 Eric Pouech
8  *
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.
13  *
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.
18  *
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
22  */
23
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
29  */
30
31
32 #include <ctype.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <assert.h>
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winnls.h"
42 #include "mmsystem.h"
43 #include "winemm.h"
44
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
48
49 LRESULT         (*pFnMmioCallback16)(DWORD,LPMMIOINFO,UINT,LPARAM,LPARAM) /* = NULL */;
50
51 /**************************************************************************
52  *                      mmioDosIOProc                           [internal]
53  */
54 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
55                                       LPARAM lParam1, LPARAM lParam2)
56 {
57     LRESULT     ret = MMSYSERR_NOERROR;
58
59     TRACE("(%p, %X, 0x%lx, 0x%lx);\n", lpmmioinfo, uMessage, lParam1, lParam2);
60
61     switch (uMessage) {
62     case MMIOM_OPEN:
63         {
64             /* Parameters:
65              * lParam1 = szFileName parameter from mmioOpen
66              * lParam2 = reserved
67              * Returns: zero on success, error code on error
68              * NOTE: lDiskOffset automatically set to zero
69              */
70             LPCSTR      szFileName = (LPCSTR)lParam1;
71
72             if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
73                 FIXME("MMIO_GETTEMP not implemented\n");
74                 return MMIOERR_CANNOTOPEN;
75             }
76
77             /* if filename NULL, assume open file handle in adwInfo[0] */
78             if (szFileName) {
79                 OFSTRUCT    ofs;
80                 lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
81             }
82             if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
83                 ret = MMIOERR_CANNOTOPEN;
84         }
85         break;
86
87     case MMIOM_CLOSE:
88         /* Parameters:
89          * lParam1 = wFlags parameter from mmioClose
90          * lParam2 = unused
91          * Returns: zero on success, error code on error
92          */
93         if (!(lParam1 & MMIO_FHOPEN))
94             _lclose((HFILE)lpmmioinfo->adwInfo[0]);
95         break;
96
97     case MMIOM_READ:
98         /* Parameters:
99          * lParam1 = huge pointer to read buffer
100          * lParam2 = number of bytes to read
101          * Returns: number of bytes read, 0 for EOF, -1 for error (error code
102          *         in wErrorRet)
103          */
104         ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
105         if (ret != -1)
106             lpmmioinfo->lDiskOffset += ret;
107
108         break;
109
110     case MMIOM_WRITE:
111     case MMIOM_WRITEFLUSH:
112         /* no internal buffering, so WRITEFLUSH handled same as WRITE */
113
114         /* Parameters:
115          * lParam1 = huge pointer to write buffer
116          * lParam2 = number of bytes to write
117          * Returns: number of bytes written, -1 for error (error code in
118          *              wErrorRet)
119          */
120         ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
121         if (ret != -1)
122             lpmmioinfo->lDiskOffset += ret;
123         break;
124
125     case MMIOM_SEEK:
126         /* Parameters:
127          * lParam1 = new position
128          * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
129          * Returns: new file postion, -1 on error
130          */
131         ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
132         if (ret != -1)
133             lpmmioinfo->lDiskOffset = ret;
134         return ret;
135
136     case MMIOM_RENAME:
137         /* Parameters:
138          * lParam1 = old name
139          * lParam2 = new name
140          * Returns: zero on success, non-zero on failure
141          */
142          if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
143              ret = MMIOERR_FILENOTFOUND;
144          break;
145
146     default:
147         FIXME("unexpected message %u\n", uMessage);
148         return 0;
149     }
150
151     return ret;
152 }
153
154 /**************************************************************************
155  *                      mmioMemIOProc                           [internal]
156  */
157 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
158                                       LPARAM lParam1, LPARAM lParam2)
159 {
160     TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
161
162     switch (uMessage) {
163
164     case MMIOM_OPEN:
165         /* Parameters:
166          * lParam1 = filename (must be NULL)
167          * lParam2 = reserved
168          * Returns: zero on success, error code on error
169          * NOTE: lDiskOffset automatically set to zero
170          */
171         /* FIXME: io proc shouldn't change it */
172         if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
173             lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
174         lpmmioinfo->adwInfo[0] = HFILE_ERROR;
175         return 0;
176
177     case MMIOM_CLOSE:
178         /* Parameters:
179          * lParam1 = wFlags parameter from mmioClose
180          * lParam2 = unused
181          * Returns: zero on success, error code on error
182          */
183         return 0;
184
185     case MMIOM_READ:
186         /* Parameters:
187          * lParam1 = huge pointer to read buffer
188          * lParam2 = number of bytes to read
189          * Returns: number of bytes read, 0 for EOF, -1 for error (error code
190          *         in wErrorRet)
191          * NOTE: lDiskOffset should be updated
192          */
193         FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
194         return 0;
195
196     case MMIOM_WRITE:
197     case MMIOM_WRITEFLUSH:
198         /* no internal buffering, so WRITEFLUSH handled same as WRITE */
199
200         /* Parameters:
201          * lParam1 = huge pointer to write buffer
202          * lParam2 = number of bytes to write
203          * Returns: number of bytes written, -1 for error (error code in
204          *              wErrorRet)
205          * NOTE: lDiskOffset should be updated
206          */
207         FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
208         return 0;
209
210     case MMIOM_SEEK:
211         /* Parameters:
212          * lParam1 = new position
213          * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
214          * Returns: new file postion, -1 on error
215          * NOTE: lDiskOffset should be updated
216          */
217         FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
218         return -1;
219
220     default:
221         FIXME("unexpected message %u\n", uMessage);
222         return 0;
223     }
224
225     return 0;
226 }
227
228 /* This array will be the entire list for most apps 
229  * Note that temporary ioProcs will be stored with a 0 fourCC code
230  */
231
232 static struct IOProcList defaultProcs[] = {
233     {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
234     {NULL,             FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
235 };
236
237 static struct IOProcList*       pIOProcListAnchor = &defaultProcs[0];
238
239 /****************************************************************
240  *              MMIO_FindProcNode                       [INTERNAL]
241  *
242  * Finds the ProcList node associated with a given FOURCC code.
243  */
244 static struct IOProcList*       MMIO_FindProcNode(FOURCC fccIOProc)
245 {
246     struct IOProcList*  pListNode;
247
248     for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
249         if (pListNode->fourCC == fccIOProc) {
250             return pListNode;
251         }
252     }
253     return NULL;
254 }
255
256 /****************************************************************
257  *              MMIO_InstallIOProc                      [INTERNAL]
258  */
259 LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
260                               DWORD dwFlags, enum mmioProcType type)
261 {
262     LPMMIOPROC          lpProc = NULL;
263     struct IOProcList*  pListNode;
264     struct IOProcList** ppListNode;
265
266     TRACE("(%08lx, %p, %08lX, %i)\n", fccIOProc, pIOProc, dwFlags, type);
267
268     if (dwFlags & MMIO_GLOBALPROC)
269         FIXME("Global procedures not implemented\n");
270
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->type = type;
281             pListNode->count = 0;
282
283             /* Stick it on the end of the list */
284             pListNode->pNext = pIOProcListAnchor;
285             pIOProcListAnchor = pListNode;
286
287             /* Return this IOProc - that's how the caller knows we succeeded */
288             lpProc = pIOProc;
289         }
290         break;
291
292     case MMIO_REMOVEPROC:
293         /*
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
300          */
301         ppListNode = &pIOProcListAnchor;
302         while ((*ppListNode) && 
303                ((fccIOProc != 0) ? 
304                 (*ppListNode)->fourCC != fccIOProc : 
305                 (*ppListNode)->pIOProc != pIOProc))
306             ppListNode = &((*ppListNode)->pNext);
307
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 ?
311              */
312             if ((*ppListNode)->count) {
313                 ERR("Cannot remove a mmIOProc while in use\n");
314                 break;
315             }
316             /* remove it, but only if it isn't builtin */
317             if ((*ppListNode) >= defaultProcs &&
318                 (*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
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);
326             }
327         }
328         break;
329
330     case MMIO_FINDPROC:
331         if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
332             lpProc = pListNode->pIOProc;
333         }
334         break;
335     }
336
337     return lpProc;
338 }
339
340 /****************************************************************
341  *              send_message                            [INTERNAL]
342  */
343 static LRESULT  send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
344                              DWORD wMsg, LPARAM lParam1,
345                              LPARAM lParam2, enum mmioProcType type)
346 {
347     LRESULT             result = MMSYSERR_ERROR;
348     LPARAM              lp1 = lParam1, lp2 = lParam2;
349
350     if (!ioProc) {
351         ERR("brrr\n");
352         result = MMSYSERR_INVALPARAM;
353     }
354
355     switch (ioProc->type) {
356     case MMIO_PROC_16:
357         if (pFnMmioCallback16)
358             result = pFnMmioCallback16((DWORD)ioProc->pIOProc,
359                                        mmioinfo, wMsg, lp1, lp2);
360         break;
361     case MMIO_PROC_32A:
362     case MMIO_PROC_32W:
363         if (ioProc->type != type) {
364             /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
365             FIXME("NIY 32 A<=>W mapping\n");
366         }
367         result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
368
369 #if 0
370         if (ioProc->type != type) {
371             /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
372         }
373 #endif
374         break;
375     default:
376         FIXME("Internal error\n");
377     }
378
379     return result;
380 }
381
382 /**************************************************************************
383  *                              MMIO_ParseExtA                  [internal]
384  *
385  * Parses a filename for the extension.
386  *
387  * RETURNS
388  *  The FOURCC code for the extension if found, else 0.
389  */
390 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
391 {
392     /* Filenames are of the form file.ext{+ABC}
393        For now, we take the last '+' if present */
394
395     FOURCC ret = 0;
396
397     /* Note that ext{Start,End} point to the . and + respectively */
398     LPSTR extEnd;
399     LPSTR extStart;
400
401     TRACE("(%s)\n", debugstr_a(szFileName));
402
403     if (!szFileName)
404         return ret;
405
406     /* Find the last '.' */
407     extStart = strrchr(szFileName,'.');
408
409     if (!extStart) {
410          ERR("No . in szFileName: %s\n", debugstr_a(szFileName));
411     } else {
412         CHAR ext[5];
413
414         /* Find the '+' afterwards */
415         extEnd = strchr(extStart,'+');
416         if (extEnd) {
417
418             if (extEnd - extStart - 1 > 4)
419                 WARN("Extension length > 4\n");
420             lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
421
422         } else {
423             /* No + so just an extension */
424             if (strlen(extStart) > 4) {
425                 WARN("Extension length > 4\n");
426             }
427             lstrcpynA(ext, extStart + 1, 5);
428         }
429         TRACE("Got extension: %s\n", debugstr_a(ext));
430
431         /* FOURCC codes identifying file-extensions must be uppercase */
432         ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
433     }
434     return ret;
435 }
436
437 /**************************************************************************
438  *                              MMIO_Get                        [internal]
439  *
440  * Retrieves the mmio object from current process
441  */
442 LPWINE_MMIO     MMIO_Get(HMMIO h)
443 {
444     LPWINE_MMIO         wm = NULL;
445
446     EnterCriticalSection(&WINMM_IData->cs);
447     for (wm = WINMM_IData->lpMMIO; wm; wm = wm->lpNext) {
448         if (wm->info.hmmio == h)
449             break;
450     }
451     LeaveCriticalSection(&WINMM_IData->cs);
452     return wm;
453 }
454
455 /**************************************************************************
456  *                              MMIO_Create                     [internal]
457  *
458  * Creates an internal representation for a mmio instance
459  */
460 static  LPWINE_MMIO             MMIO_Create(void)
461 {
462     static      WORD    MMIO_counter = 0;
463     LPWINE_MMIO         wm;
464
465     wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
466     if (wm) {
467         EnterCriticalSection(&WINMM_IData->cs);
468         /* lookup next unallocated WORD handle, with a non NULL value */
469         while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
470         wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
471         wm->lpNext = WINMM_IData->lpMMIO;
472         WINMM_IData->lpMMIO = wm;
473         LeaveCriticalSection(&WINMM_IData->cs);
474     }
475     return wm;
476 }
477
478 /**************************************************************************
479  *                              MMIO_Destroy                    [internal]
480  *-
481  * Destroys an internal representation for a mmio instance
482  */
483 static  BOOL            MMIO_Destroy(LPWINE_MMIO wm)
484 {
485     LPWINE_MMIO*        m;
486
487     EnterCriticalSection(&WINMM_IData->cs);
488     /* search for the matching one... */
489     m = &(WINMM_IData->lpMMIO);
490     while (*m && *m != wm) m = &(*m)->lpNext;
491     /* ...and destroy */
492     if (*m) {
493         *m = (*m)->lpNext;
494         HeapFree(GetProcessHeap(), 0, wm);
495         wm = NULL;
496     }
497     LeaveCriticalSection(&WINMM_IData->cs);
498     return wm ? FALSE : TRUE;
499 }
500
501 /****************************************************************
502  *                      MMIO_Flush                      [INTERNAL]
503  */
504 static  MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
505 {
506     if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
507         /* not quite sure what to do here, but I'll guess */
508         if (wm->info.dwFlags & MMIO_DIRTY) {
509             /* FIXME: error handling */
510             send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
511                          wm->info.lBufOffset, SEEK_SET, MMIO_PROC_32A);
512             send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
513                          (LPARAM)wm->info.pchBuffer,
514                          wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
515         }
516         if (uFlags & MMIO_EMPTYBUF)
517             wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
518     }
519     wm->info.dwFlags &= ~MMIO_DIRTY;
520
521     return MMSYSERR_NOERROR;
522 }
523
524 /***************************************************************************
525  *                      MMIO_GrabNextBuffer                     [INTERNAL]
526  */
527 static LONG     MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
528 {
529     LONG        size = wm->info.cchBuffer;
530
531     TRACE("bo=%lx do=%lx of=%lx\n",
532           wm->info.lBufOffset, wm->info.lDiskOffset,
533           send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
534
535     wm->info.lBufOffset = wm->info.lDiskOffset;
536     wm->info.pchNext = wm->info.pchBuffer;
537     wm->info.pchEndRead = wm->info.pchBuffer;
538     wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
539
540     wm->bBufferLoaded = TRUE;
541     if (for_read) {
542         size = send_message(wm->ioProc, &wm->info, MMIOM_READ, 
543                             (LPARAM)wm->info.pchBuffer, size, MMIO_PROC_32A);
544         if (size > 0)
545             wm->info.pchEndRead += size;
546         else
547             wm->bBufferLoaded = FALSE;
548     }
549
550     return size;
551 }
552
553 /***************************************************************************
554  *                      MMIO_SetBuffer                          [INTERNAL]
555  */
556 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
557                                UINT uFlags)
558 {
559     TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
560
561     if (uFlags)                 return MMSYSERR_INVALPARAM;
562     if (cchBuffer > 0xFFFF)
563         WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
564
565     if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
566         return MMIOERR_CANNOTWRITE;
567
568     /* free previous buffer if allocated */
569     if (wm->info.dwFlags & MMIO_ALLOCBUF) {
570         HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
571         wm->info.pchBuffer = NULL;
572         wm->info.dwFlags &= ~MMIO_ALLOCBUF;
573     }
574
575     if (pchBuffer) {
576         wm->info.pchBuffer = pchBuffer;
577     } else if (cchBuffer) {
578         if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
579             return MMIOERR_OUTOFMEMORY;
580         wm->info.dwFlags |= MMIO_ALLOCBUF;
581     } else {
582         wm->info.pchBuffer = NULL;
583     }
584
585     wm->info.cchBuffer = cchBuffer;
586     wm->info.pchNext = wm->info.pchBuffer;
587     wm->info.pchEndRead = wm->info.pchBuffer;
588     wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
589     wm->info.lBufOffset = 0;
590     wm->bBufferLoaded = FALSE;
591
592     return MMSYSERR_NOERROR;
593 }
594
595 /**************************************************************************
596  *                      MMIO_Open                               [internal]
597  */
598 HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, 
599                 enum mmioProcType type)
600 {
601     LPWINE_MMIO         wm;
602     MMIOINFO            mmioinfo;
603
604     TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
605
606     if (!refmminfo) {
607         refmminfo = &mmioinfo;
608
609         mmioinfo.fccIOProc = 0;
610         mmioinfo.pIOProc = NULL;
611         mmioinfo.pchBuffer = NULL;
612         mmioinfo.cchBuffer = 0;
613         type = MMIO_PROC_32A;
614     }
615
616     if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
617         char    buffer[MAX_PATH];
618
619         if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
620             return (HMMIO)FALSE;
621         if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
622             return (HMMIO)FALSE;
623         strcpy(szFileName, buffer);
624         return (HMMIO)TRUE;
625     }
626
627     if ((wm = MMIO_Create()) == NULL)
628         return 0;
629
630     /* If both params are NULL, then parse the file name if available */
631     if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
632         wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
633         /* Handle any unhandled/error case. Assume DOS file */
634         if (wm->info.fccIOProc == 0)
635             wm->info.fccIOProc = FOURCC_DOS;
636         if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
637             /* If not found, retry with FOURCC_DOS */
638             wm->info.fccIOProc = FOURCC_DOS;
639             if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc)))
640                 goto error2;
641         }
642         wm->bTmpIOProc = FALSE;
643     }
644     /* if just the four character code is present, look up IO proc */
645     else if (refmminfo->pIOProc == NULL) {
646         wm->info.fccIOProc = refmminfo->fccIOProc;
647         if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
648         wm->bTmpIOProc = FALSE;
649     }
650     /* if IO proc specified, use it and specified four character code */
651     else {
652         wm->info.fccIOProc = refmminfo->fccIOProc;
653         MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
654                            MMIO_INSTALLPROC, type);
655         if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
656         assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
657         wm->bTmpIOProc = TRUE;
658     }
659
660     wm->bBufferLoaded = FALSE;
661     wm->ioProc->count++;
662
663     if (dwOpenFlags & MMIO_ALLOCBUF) {
664         if ((refmminfo->wErrorRet = MMIO_SetBuffer(wm, NULL, MMIO_DEFAULTBUFFER, 0)))
665             goto error1;
666     } else if (wm->info.fccIOProc == FOURCC_MEM) {
667         refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
668         if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
669             goto error1;
670         wm->bBufferLoaded = TRUE;
671     } /* else => unbuffered, wm->info.pchBuffer == NULL */
672
673     /* see mmioDosIOProc for that one */
674     wm->info.adwInfo[0] = refmminfo->adwInfo[0];
675     wm->info.dwFlags = dwOpenFlags;
676
677     /* call IO proc to actually open file */
678     refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN, 
679                                         (LPARAM)szFileName, 0, MMIO_PROC_32A);
680
681     /* grab file size, when possible */
682     wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
683
684     if (refmminfo->wErrorRet == 0)
685         return wm->info.hmmio;
686  error1:
687     if (wm->ioProc) wm->ioProc->count--;
688  error2:
689     MMIO_Destroy(wm);
690     return 0;
691 }
692
693 /**************************************************************************
694  *                              mmioOpenW                       [WINMM.@]
695  */
696 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
697                        DWORD dwOpenFlags)
698 {
699     HMMIO       ret;
700     LPSTR       szFn = NULL;
701
702     if (szFileName)
703     {
704         INT     len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
705         szFn = HeapAlloc( GetProcessHeap(), 0, len );
706         if (!szFn) return (HMMIO)NULL;
707         WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
708     }
709
710     ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
711
712     HeapFree(GetProcessHeap(), 0, szFn);
713     return ret;
714 }
715
716 /**************************************************************************
717  *                              mmioOpenA                       [WINMM.@]
718  */
719 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
720                        DWORD dwOpenFlags)
721 {
722     return  MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
723 }
724
725 /**************************************************************************
726  *                              mmioClose               [WINMM.@]
727  */
728 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
729 {
730     LPWINE_MMIO wm;
731     MMRESULT    result;
732
733     TRACE("(%p, %04X);\n", hmmio, uFlags);
734
735     if ((wm = MMIO_Get(hmmio)) == NULL)
736         return MMSYSERR_INVALHANDLE;
737
738     if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
739         return result;
740
741     result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, 
742                           uFlags, 0, MMIO_PROC_32A);
743
744     MMIO_SetBuffer(wm, NULL, 0, 0);
745
746     wm->ioProc->count--;
747
748     if (wm->bTmpIOProc)
749         MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
750                            MMIO_REMOVEPROC, wm->ioProc->type);
751
752     MMIO_Destroy(wm);
753
754     return result;
755 }
756
757 /**************************************************************************
758  *                              mmioRead                [WINMM.@]
759  */
760 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
761 {
762     LPWINE_MMIO wm;
763     LONG        count;
764
765     TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
766
767     if ((wm = MMIO_Get(hmmio)) == NULL)
768         return -1;
769
770     /* unbuffered case first */
771     if (!wm->info.pchBuffer)
772         return send_message(wm->ioProc, &wm->info, MMIOM_READ, 
773                             (LPARAM)pch, cch, MMIO_PROC_32A);
774
775     /* first try from current buffer */
776     if (wm->info.pchNext != wm->info.pchEndRead) {
777         count = wm->info.pchEndRead - wm->info.pchNext;
778         if (count > cch || count < 0) count = cch;
779         memcpy(pch, wm->info.pchNext, count);
780         wm->info.pchNext += count;
781         pch += count;
782         cch -= count;
783     } else
784         count = 0;
785
786     if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
787         assert(wm->info.cchBuffer);
788
789         while (cch) {
790             LONG size;
791
792             size = MMIO_GrabNextBuffer(wm, TRUE);
793             if (size <= 0) break;
794             if (size > cch) size = cch;
795             memcpy(pch, wm->info.pchBuffer, size);
796             wm->info.pchNext += size;
797             pch += size;
798             cch -= size;
799             count += size;
800         }
801     }
802
803     TRACE("count=%ld\n", count);
804     return count;
805 }
806
807 /**************************************************************************
808  *                              mmioWrite               [WINMM.@]
809  */
810 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
811 {
812     LPWINE_MMIO wm;
813     LONG        count;
814
815     TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
816
817     if ((wm = MMIO_Get(hmmio)) == NULL)
818         return -1;
819
820     if (wm->info.cchBuffer) {
821         LONG    bytesW = 0;
822
823         count = 0;
824         while (cch) {
825             if (wm->info.pchNext != wm->info.pchEndWrite) {
826                 count = wm->info.pchEndWrite - wm->info.pchNext;
827                 if (count > cch || count < 0) count = cch;
828                 memcpy(wm->info.pchNext, pch, count);
829                 wm->info.pchNext += count;
830                 pch += count;
831                 cch -= count;
832                 bytesW += count;
833                 wm->info.dwFlags |= MMIO_DIRTY;
834             } else {
835                 if (wm->info.fccIOProc == FOURCC_MEM) {
836                     if (wm->info.adwInfo[0]) {
837                         /* from where would we get the memory handle? */
838                         FIXME("memory file expansion not implemented!\n");
839                         break;
840                     } else break;
841                 }
842             }
843
844             if (wm->info.pchNext == wm->info.pchEndWrite)
845             {
846                 MMIO_Flush(wm, MMIO_EMPTYBUF);
847                 MMIO_GrabNextBuffer(wm, FALSE);
848             }
849             else break;
850         }
851         count = bytesW;
852     } else {
853         count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
854                              (LPARAM)pch, cch, MMIO_PROC_32A);
855         wm->info.lBufOffset = wm->info.lDiskOffset;
856     }
857
858     TRACE("bytes written=%ld\n", count);
859     return count;
860 }
861
862 /**************************************************************************
863  *                              mmioSeek                [WINMM.@]
864  */
865 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
866 {
867     LPWINE_MMIO wm;
868     LONG        offset;
869
870     TRACE("(%p, %08lX, %d);\n", hmmio, lOffset, iOrigin);
871
872     if ((wm = MMIO_Get(hmmio)) == NULL)
873         return MMSYSERR_INVALHANDLE;
874
875     /* not buffered, direct seek on file */
876     if (!wm->info.pchBuffer)
877         return send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
878                             lOffset, iOrigin, MMIO_PROC_32A);
879
880     switch (iOrigin) {
881     case SEEK_SET:
882         offset = lOffset;
883         break;
884     case SEEK_CUR:
885         offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
886         break;
887     case SEEK_END:
888         offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
889         break;
890     default:
891         return -1;
892     }
893
894     if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
895         /* should check that write mode exists */
896         if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
897             return -1;
898         wm->info.lBufOffset = offset;
899         wm->info.pchEndRead = wm->info.pchBuffer;
900         wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
901         if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
902             wm->info.lDiskOffset = wm->dwFileSize;
903         }
904     } else if ((wm->info.cchBuffer > 0) &&
905         ((offset < wm->info.lBufOffset) ||
906          (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
907          !wm->bBufferLoaded)) {
908         /* stay in same buffer ? */
909         /* some memory mapped buffers are defined with -1 as a size */
910
911         /* condition to change buffer */
912         if ((wm->info.fccIOProc == FOURCC_MEM) ||
913             MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
914             /* this also sets the wm->info.lDiskOffset field */
915             send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
916                          (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
917                          SEEK_SET, MMIO_PROC_32A) == -1)
918             return -1;
919         MMIO_GrabNextBuffer(wm, TRUE);
920     }
921
922     wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
923
924     TRACE("=> %ld\n", offset);
925     return offset;
926 }
927
928 /**************************************************************************
929  *                              mmioGetInfo             [WINMM.@]
930  */
931 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
932 {
933     LPWINE_MMIO         wm;
934
935     TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
936
937     if ((wm = MMIO_Get(hmmio)) == NULL)
938         return MMSYSERR_INVALHANDLE;
939
940     memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
941     /* don't expose 16 bit ioproc:s */
942     if (wm->ioProc->type != MMIO_PROC_16)
943         lpmmioinfo->pIOProc = wm->ioProc->pIOProc;
944
945     return MMSYSERR_NOERROR;
946 }
947
948 /**************************************************************************
949  *                              mmioSetInfo             [WINMM.@]
950  */
951 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
952 {
953     LPWINE_MMIO         wm;
954
955     TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
956
957     if ((wm = MMIO_Get(hmmio)) == NULL)
958         return MMSYSERR_INVALHANDLE;
959
960     /* check pointers coherence */
961     if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
962         lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
963         lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
964         lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
965         lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
966         lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
967         return MMSYSERR_INVALPARAM;
968
969     wm->info.pchNext = lpmmioinfo->pchNext;
970     wm->info.pchEndRead = lpmmioinfo->pchEndRead;
971
972     return MMSYSERR_NOERROR;
973 }
974
975 /**************************************************************************
976 *                               mmioSetBuffer           [WINMM.@]
977 */
978 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
979 {
980     LPWINE_MMIO         wm;
981
982     TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
983           hmmio, pchBuffer, cchBuffer, uFlags);
984
985     if ((wm = MMIO_Get(hmmio)) == NULL)
986         return MMSYSERR_INVALHANDLE;
987
988     return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
989 }
990
991 /**************************************************************************
992  *                              mmioFlush               [WINMM.@]
993  */
994 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
995 {
996     LPWINE_MMIO         wm;
997
998     TRACE("(%p, %04X)\n", hmmio, uFlags);
999
1000     if ((wm = MMIO_Get(hmmio)) == NULL)
1001         return MMSYSERR_INVALHANDLE;
1002
1003     return MMIO_Flush(wm, uFlags);
1004 }
1005
1006 /**************************************************************************
1007  *                              mmioAdvance             [WINMM.@]
1008  */
1009 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1010 {
1011     LPWINE_MMIO         wm;
1012
1013     TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1014
1015     /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1016      * here. be sure if you change something here to check mmioAdvance16 as well
1017      */
1018     if ((wm = MMIO_Get(hmmio)) == NULL)
1019         return MMSYSERR_INVALHANDLE;
1020
1021     if (!wm->info.cchBuffer)
1022         return MMIOERR_UNBUFFERED;
1023
1024     if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1025         return MMSYSERR_INVALPARAM;
1026
1027     if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1028     {
1029         send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
1030                      lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1031         send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
1032                      (LPARAM)lpmmioinfo->pchBuffer,
1033                      lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1034         lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1035     }
1036     if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1037         return MMIOERR_CANNOTWRITE;
1038
1039     if (lpmmioinfo) {
1040         wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset + 
1041                              (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1042     }
1043     MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1044
1045     if (lpmmioinfo) {
1046         lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1047         lpmmioinfo->pchEndRead  = lpmmioinfo->pchBuffer +
1048             (wm->info.pchEndRead - wm->info.pchBuffer);
1049         lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1050             (wm->info.pchEndWrite - wm->info.pchBuffer);
1051         lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1052         lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1053     }
1054     return MMSYSERR_NOERROR;
1055 }
1056
1057 /**************************************************************************
1058  *                              mmioStringToFOURCCA     [WINMM.@]
1059  */
1060 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1061 {
1062     CHAR cc[4];
1063     int i = 0;
1064
1065     for (i = 0; i < 4 && sz[i]; i++) {
1066         if (uFlags & MMIO_TOUPPER) {
1067             cc[i] = toupper(sz[i]);
1068         } else {
1069             cc[i] = sz[i];
1070         }
1071     }
1072
1073     /* Pad with spaces */
1074     while (i < 4) cc[i++] = ' ';
1075
1076     TRACE("Got '%.4s'\n",cc);
1077     return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1078 }
1079
1080 /**************************************************************************
1081  *                              mmioStringToFOURCCW     [WINMM.@]
1082  */
1083 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1084 {
1085     char        szA[4];
1086
1087     WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1088     return mmioStringToFOURCCA(szA,uFlags);
1089 }
1090
1091 /**************************************************************************
1092  *                              mmioInstallIOProcA         [WINMM.@]
1093  */
1094 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1095                                      LPMMIOPROC pIOProc, DWORD dwFlags)
1096 {
1097     return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1098 }
1099
1100 /**************************************************************************
1101  *                              mmioInstallIOProcW         [WINMM.@]
1102  */
1103 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1104                                      LPMMIOPROC pIOProc, DWORD dwFlags)
1105 {
1106     return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1107 }
1108
1109 /******************************************************************
1110  *              MMIO_SendMessage
1111  *
1112  *
1113  */
1114 LRESULT         MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1, 
1115                                  LPARAM lParam2, enum mmioProcType type)
1116 {
1117     LPWINE_MMIO         wm;
1118
1119     TRACE("(%p, %u, %ld, %ld, %d)\n", hmmio, uMessage, lParam1, lParam2, type);
1120
1121     if (uMessage < MMIOM_USER)
1122         return MMSYSERR_INVALPARAM;
1123
1124     if ((wm = MMIO_Get(hmmio)) == NULL)
1125         return MMSYSERR_INVALHANDLE;
1126
1127     return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, type);
1128 }
1129
1130 /**************************************************************************
1131  *                              mmioSendMessage         [WINMM.@]
1132  */
1133 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1134                                LPARAM lParam1, LPARAM lParam2)
1135 {
1136     return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1137 }
1138
1139 /**************************************************************************
1140  *                              mmioDescend             [WINMM.@]
1141  */
1142 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1143                             const MMCKINFO* lpckParent, UINT uFlags)
1144 {
1145     DWORD               dwOldPos;
1146     FOURCC              srchCkId;
1147     FOURCC              srchType;
1148
1149
1150     TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1151
1152     if (lpck == NULL)
1153         return MMSYSERR_INVALPARAM;
1154
1155     dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1156     TRACE("dwOldPos=%ld\n", dwOldPos);
1157
1158     if (lpckParent != NULL) {
1159         TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1160         /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1161         if (dwOldPos < lpckParent->dwDataOffset ||
1162             dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1163             WARN("outside parent chunk\n");
1164             return MMIOERR_CHUNKNOTFOUND;
1165         }
1166     }
1167
1168     /* The SDK docu says 'ckid' is used for all cases. Real World
1169      * examples disagree -Marcus,990216.
1170      */
1171
1172     srchType = 0;
1173     /* find_chunk looks for 'ckid' */
1174     if (uFlags & MMIO_FINDCHUNK)
1175         srchCkId = lpck->ckid;
1176     /* find_riff and find_list look for 'fccType' */
1177     if (uFlags & MMIO_FINDLIST) {
1178         srchCkId = FOURCC_LIST;
1179         srchType = lpck->fccType;
1180     }
1181     if (uFlags & MMIO_FINDRIFF) {
1182         srchCkId = FOURCC_RIFF;
1183         srchType = lpck->fccType;
1184     }
1185
1186     if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1187         TRACE("searching for %.4s.%.4s\n",
1188               (LPSTR)&srchCkId,
1189               srchType?(LPSTR)&srchType:"any ");
1190
1191         while (TRUE) {
1192             LONG ix;
1193
1194             ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1195             if (ix < 2*sizeof(DWORD)) {
1196                 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1197                 WARN("return ChunkNotFound\n");
1198                 return MMIOERR_CHUNKNOTFOUND;
1199             }
1200             lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1201             if (ix < lpck->dwDataOffset - dwOldPos) {
1202                 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1203                 WARN("return ChunkNotFound\n");
1204                 return MMIOERR_CHUNKNOTFOUND;
1205             }
1206             TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1207                   (LPSTR)&lpck->ckid,
1208                   srchType?(LPSTR)&lpck->fccType:"<na>",
1209                   lpck->cksize);
1210             if ((srchCkId == lpck->ckid) &&
1211                 (!srchType || (srchType == lpck->fccType))
1212                 )
1213                 break;
1214
1215             dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1216             mmioSeek(hmmio, dwOldPos, SEEK_SET);
1217         }
1218     } else {
1219         /* FIXME: unverified, does it do this? */
1220         /* NB: This part is used by WAVE_mciOpen, among others */
1221         if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1222             mmioSeek(hmmio, dwOldPos, SEEK_SET);
1223             WARN("return ChunkNotFound 2nd\n");
1224             return MMIOERR_CHUNKNOTFOUND;
1225         }
1226         lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1227     }
1228     lpck->dwFlags = 0;
1229     /* If we were looking for RIFF/LIST chunks, the final file position
1230      * is after the chunkid. If we were just looking for the chunk
1231      * it is after the cksize. So add 4 in RIFF/LIST case.
1232      */
1233     if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1234         mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1235     else
1236         mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1237     TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
1238           (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1239           lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1240     return MMSYSERR_NOERROR;
1241 }
1242
1243 /**************************************************************************
1244  *                              mmioAscend              [WINMM.@]
1245  */
1246 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1247 {
1248     TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1249
1250     if (lpck->dwFlags & MMIO_DIRTY) {
1251         DWORD   dwOldPos, dwNewSize;
1252
1253         TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1254         dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1255         TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1256         dwNewSize = dwOldPos - lpck->dwDataOffset;
1257         if (dwNewSize != lpck->cksize) {
1258             TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1259             lpck->cksize = dwNewSize;
1260
1261             /* pad odd size with 0 */
1262             if (dwNewSize & 1) {
1263                 char ch = 0;
1264                 mmioWrite(hmmio, &ch, 1);
1265             }
1266             mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1267             mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1268         }
1269         lpck->dwFlags = 0;
1270     }
1271
1272     mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1273
1274     return MMSYSERR_NOERROR;
1275 }
1276
1277 /**************************************************************************
1278  *                      mmioCreateChunk                         [WINMM.@]
1279  */
1280 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1281 {
1282     DWORD       dwOldPos;
1283     LONG        size;
1284     LONG        ix;
1285
1286     TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1287
1288     dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1289     TRACE("dwOldPos=%ld\n", dwOldPos);
1290
1291     if (uFlags == MMIO_CREATELIST)
1292         lpck->ckid = FOURCC_LIST;
1293     else if (uFlags == MMIO_CREATERIFF)
1294         lpck->ckid = FOURCC_RIFF;
1295
1296     TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1297
1298     size = 2 * sizeof(DWORD);
1299     lpck->dwDataOffset = dwOldPos + size;
1300
1301     if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1302         size += sizeof(DWORD);
1303     lpck->dwFlags = MMIO_DIRTY;
1304
1305     ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1306     TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
1307     if (ix < size) {
1308         mmioSeek(hmmio, dwOldPos, SEEK_SET);
1309         WARN("return CannotWrite\n");
1310         return MMIOERR_CANNOTWRITE;
1311     }
1312
1313     return MMSYSERR_NOERROR;
1314 }
1315
1316 /**************************************************************************
1317  *                              mmioRenameA                     [WINMM.@]
1318  */
1319 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1320                             const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1321 {
1322     struct IOProcList*  ioProc = NULL;
1323     struct IOProcList   tmp;
1324     FOURCC              fcc;
1325
1326     TRACE("('%s', '%s', %p, %08lX);\n",
1327           debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1328
1329     /* If both params are NULL, then parse the file name */
1330     if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1331     {
1332         fcc = MMIO_ParseExtA(szFileName);
1333         if (fcc) ioProc = MMIO_FindProcNode(fcc);
1334     }
1335
1336     /* Handle any unhandled/error case from above. Assume DOS file */
1337     if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1338         ioProc = MMIO_FindProcNode(FOURCC_DOS);
1339     /* if just the four character code is present, look up IO proc */
1340     else if (lpmmioinfo->pIOProc == NULL)
1341         ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1342     else /* use relevant ioProc */
1343     {
1344         ioProc = &tmp;
1345         tmp.fourCC = lpmmioinfo->fccIOProc;
1346         tmp.pIOProc = lpmmioinfo->pIOProc;
1347         tmp.type = MMIO_PROC_32A;
1348         tmp.count = 1;
1349     }
1350
1351     /* FIXME: should we actually pass lpmmioinfo down the drain ???
1352      * or make a copy of it because it's const ???
1353      */
1354     return send_message(ioProc, (LPMMIOINFO)lpmmioinfo, MMIOM_RENAME,
1355                         (LPARAM)szFileName, (LPARAM)szNewFileName, MMIO_PROC_32A);
1356 }
1357
1358 /**************************************************************************
1359  *                              mmioRenameW                     [WINMM.@]
1360  */
1361 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1362                             const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1363 {
1364     LPSTR       szFn = NULL;
1365     LPSTR       sznFn = NULL;
1366     UINT        ret;
1367     INT         len;
1368
1369     if (szFileName)
1370     {
1371         len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1372         szFn = HeapAlloc( GetProcessHeap(), 0, len );
1373         if (!szFn) return MMSYSERR_NOMEM;
1374         WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1375     }
1376     if (szNewFileName)
1377     {
1378         len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1379         sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1380         if (!sznFn) return MMSYSERR_NOMEM;
1381         WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1382     }
1383
1384     ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1385
1386     HeapFree(GetProcessHeap(),0,szFn);
1387     HeapFree(GetProcessHeap(),0,sznFn);
1388     return ret;
1389 }