Fix the case of product and company names.
[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 "mmsystem.h"
42 #include "heap.h"
43 #include "winemm.h"
44
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
48
49 LRESULT         (*pFnMmioCallback16)(SEGPTR,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((SEGPTR)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        FIXME: What if a '+' is part of the file name?
394        For now, we take the last '+' present */
395
396     FOURCC ret = 0;
397
398     /* Note that ext{Start,End} point to the . and + respectively */
399     LPSTR extEnd;
400
401     TRACE("(%s)\n", debugstr_a(szFileName));
402
403     if (!szFileName)
404         return ret;
405     extEnd = strrchr(szFileName,'+');
406     if (extEnd) {
407         /* Need to parse to find the extension */
408         LPSTR extStart;
409
410         extStart = extEnd;
411         while (extStart >= szFileName && extStart[0] != '.') {
412             extStart--;
413         }
414
415         if (extStart < szFileName) {
416             ERR("+ but no . in szFileName: %s\n", debugstr_a(szFileName));
417         } else {
418             CHAR ext[5];
419
420             if (extEnd - extStart - 1 > 4)
421                 WARN("Extension length > 4\n");
422             lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
423             TRACE("Got extension: %s\n", debugstr_a(ext));
424             /* FOURCC codes identifying file-extensions must be uppercase */
425             ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
426         }
427     }
428     return ret;
429 }
430
431 /**************************************************************************
432  *                              MMIO_Get                        [internal]
433  *
434  * Retrieves the mmio object from current process
435  */
436 LPWINE_MMIO     MMIO_Get(HMMIO h)
437 {
438     LPWINE_MMIO         wm = NULL;
439
440     EnterCriticalSection(&WINMM_IData->cs);
441     for (wm = WINMM_IData->lpMMIO; wm; wm = wm->lpNext) {
442         if (wm->info.hmmio == h)
443             break;
444     }
445     LeaveCriticalSection(&WINMM_IData->cs);
446     return wm;
447 }
448
449 /**************************************************************************
450  *                              MMIO_Create                     [internal]
451  *
452  * Creates an internal representation for a mmio instance
453  */
454 static  LPWINE_MMIO             MMIO_Create(void)
455 {
456     static      WORD    MMIO_counter = 0;
457     LPWINE_MMIO         wm;
458
459     wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
460     if (wm) {
461         EnterCriticalSection(&WINMM_IData->cs);
462         /* lookup next unallocated WORD handle, with a non NULL value */
463         while (++MMIO_counter == 0 || MMIO_Get(HMMIO_32(MMIO_counter)));
464         wm->info.hmmio = HMMIO_32(MMIO_counter);
465         wm->lpNext = WINMM_IData->lpMMIO;
466         WINMM_IData->lpMMIO = wm;
467         LeaveCriticalSection(&WINMM_IData->cs);
468     }
469     return wm;
470 }
471
472 /**************************************************************************
473  *                              MMIO_Destroy                    [internal]
474  *-
475  * Destroys an internal representation for a mmio instance
476  */
477 static  BOOL            MMIO_Destroy(LPWINE_MMIO wm)
478 {
479     LPWINE_MMIO*        m;
480
481     EnterCriticalSection(&WINMM_IData->cs);
482     /* search for the matching one... */
483     m = &(WINMM_IData->lpMMIO);
484     while (*m && *m != wm) m = &(*m)->lpNext;
485     /* ...and destroy */
486     if (*m) {
487         *m = (*m)->lpNext;
488         HeapFree(GetProcessHeap(), 0, wm);
489         wm = NULL;
490     }
491     LeaveCriticalSection(&WINMM_IData->cs);
492     return wm ? FALSE : TRUE;
493 }
494
495 /****************************************************************
496  *                      MMIO_Flush                      [INTERNAL]
497  */
498 static  MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
499 {
500     if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
501         /* not quite sure what to do here, but I'll guess */
502         if (wm->info.dwFlags & MMIO_DIRTY) {
503             /* FIXME: error handling */
504             send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
505                          wm->info.lBufOffset, SEEK_SET, MMIO_PROC_32A);
506             send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
507                          (LPARAM)wm->info.pchBuffer,
508                          wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
509         }
510         if (uFlags & MMIO_EMPTYBUF)
511             wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
512     }
513     wm->info.dwFlags &= ~MMIO_DIRTY;
514
515     return MMSYSERR_NOERROR;
516 }
517
518 /***************************************************************************
519  *                      MMIO_GrabNextBuffer                     [INTERNAL]
520  */
521 static LONG     MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
522 {
523     LONG        size = wm->info.cchBuffer;
524
525     TRACE("bo=%lx do=%lx of=%lx\n",
526           wm->info.lBufOffset, wm->info.lDiskOffset,
527           send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
528
529     wm->info.lBufOffset = wm->info.lDiskOffset;
530     wm->info.pchNext = wm->info.pchBuffer;
531     wm->info.pchEndRead = wm->info.pchBuffer;
532     wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
533
534     wm->bBufferLoaded = TRUE;
535     if (for_read) {
536         size = send_message(wm->ioProc, &wm->info, MMIOM_READ, 
537                             (LPARAM)wm->info.pchBuffer, size, MMIO_PROC_32A);
538         if (size > 0)
539             wm->info.pchEndRead += size;
540         else
541             wm->bBufferLoaded = FALSE;
542     }
543
544     return size;
545 }
546
547 /***************************************************************************
548  *                      MMIO_SetBuffer                          [INTERNAL]
549  */
550 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
551                                UINT uFlags)
552 {
553     TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
554
555     if (uFlags)                 return MMSYSERR_INVALPARAM;
556     if (cchBuffer > 0xFFFF)
557         WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
558
559     if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
560         return MMIOERR_CANNOTWRITE;
561
562     /* free previous buffer if allocated */
563     if (wm->info.dwFlags & MMIO_ALLOCBUF) {
564         HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
565         wm->info.pchBuffer = NULL;
566         wm->info.dwFlags &= ~MMIO_ALLOCBUF;
567     }
568
569     if (pchBuffer) {
570         wm->info.pchBuffer = pchBuffer;
571     } else if (cchBuffer) {
572         if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
573             return MMIOERR_OUTOFMEMORY;
574         wm->info.dwFlags |= MMIO_ALLOCBUF;
575     } else {
576         wm->info.pchBuffer = NULL;
577     }
578
579     wm->info.cchBuffer = cchBuffer;
580     wm->info.pchNext = wm->info.pchBuffer;
581     wm->info.pchEndRead = wm->info.pchBuffer;
582     wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
583     wm->info.lBufOffset = 0;
584     wm->bBufferLoaded = FALSE;
585
586     return MMSYSERR_NOERROR;
587 }
588
589 /**************************************************************************
590  *                      MMIO_Open                               [internal]
591  */
592 HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, 
593                 enum mmioProcType type)
594 {
595     LPWINE_MMIO         wm;
596     MMIOINFO            mmioinfo;
597
598     TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
599
600     if (!refmminfo) {
601         refmminfo = &mmioinfo;
602
603         mmioinfo.fccIOProc = 0;
604         mmioinfo.pIOProc = NULL;
605         mmioinfo.pchBuffer = NULL;
606         mmioinfo.cchBuffer = 0;
607         type = MMIO_PROC_32A;
608     }
609
610     if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
611         char    buffer[MAX_PATH];
612
613         if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
614             return (HMMIO16)FALSE;
615         if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == -1))
616             return (HMMIO)FALSE;
617         strcpy(szFileName, buffer);
618         return (HMMIO)TRUE;
619     }
620
621     if ((wm = MMIO_Create()) == NULL)
622         return 0;
623
624     /* If both params are NULL, then parse the file name if available */
625     if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
626         wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
627         /* Handle any unhandled/error case. Assume DOS file */
628         if (wm->info.fccIOProc == 0)
629             wm->info.fccIOProc = FOURCC_DOS;
630         if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
631         wm->bTmpIOProc = FALSE;
632     }
633     /* if just the four character code is present, look up IO proc */
634     else if (refmminfo->pIOProc == NULL) {
635         wm->info.fccIOProc = refmminfo->fccIOProc;
636         if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
637         wm->bTmpIOProc = FALSE;
638     }
639     /* if IO proc specified, use it and specified four character code */
640     else {
641         wm->info.fccIOProc = refmminfo->fccIOProc;
642         MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
643                            MMIO_INSTALLPROC, type);
644         if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
645         assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
646         wm->bTmpIOProc = TRUE;
647     }
648
649     wm->bBufferLoaded = FALSE;
650     wm->ioProc->count++;
651
652     if (dwOpenFlags & MMIO_ALLOCBUF) {
653         if ((refmminfo->wErrorRet = MMIO_SetBuffer(wm, NULL, MMIO_DEFAULTBUFFER, 0)))
654             goto error1;
655     } else if (wm->info.fccIOProc == FOURCC_MEM) {
656         refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
657         if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
658             goto error1;
659         wm->bBufferLoaded = TRUE;
660     } /* else => unbuffered, wm->info.pchBuffer == NULL */
661
662     /* see mmioDosIOProc for that one */
663     wm->info.adwInfo[0] = refmminfo->adwInfo[0];
664     wm->info.dwFlags = dwOpenFlags;
665
666     /* call IO proc to actually open file */
667     refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN, 
668                                         (LPARAM)szFileName, 0, MMIO_PROC_32A);
669
670     /* grab file size, when possible */
671     wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
672
673     if (refmminfo->wErrorRet == 0)
674         return wm->info.hmmio;
675  error1:
676     if (wm->ioProc) wm->ioProc->count--;
677  error2:
678     MMIO_Destroy(wm);
679     return 0;
680 }
681
682 /**************************************************************************
683  *                              mmioOpenW                       [WINMM.@]
684  */
685 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
686                        DWORD dwOpenFlags)
687 {
688     HMMIO       ret;
689     LPSTR       szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
690
691     ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
692
693     HeapFree(GetProcessHeap(), 0, szFn);
694     return ret;
695 }
696
697 /**************************************************************************
698  *                              mmioOpenA                       [WINMM.@]
699  */
700 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
701                        DWORD dwOpenFlags)
702 {
703     return  MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
704 }
705
706 /**************************************************************************
707  *                              mmioClose               [WINMM.@]
708  */
709 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
710 {
711     LPWINE_MMIO wm;
712     MMRESULT    result;
713
714     TRACE("(%p, %04X);\n", hmmio, uFlags);
715
716     if ((wm = MMIO_Get(hmmio)) == NULL)
717         return MMSYSERR_INVALHANDLE;
718
719     if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
720         return result;
721
722     result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, 
723                           uFlags, 0, MMIO_PROC_32A);
724
725     MMIO_SetBuffer(wm, NULL, 0, 0);
726
727     wm->ioProc->count--;
728
729     if (wm->bTmpIOProc)
730         MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
731                            MMIO_REMOVEPROC, wm->ioProc->type);
732
733     MMIO_Destroy(wm);
734
735     return result;
736 }
737
738 /**************************************************************************
739  *                              mmioRead                [WINMM.@]
740  */
741 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
742 {
743     LPWINE_MMIO wm;
744     LONG        count;
745
746     TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
747
748     if ((wm = MMIO_Get(hmmio)) == NULL)
749         return -1;
750
751     /* unbuffered case first */
752     if (!wm->info.pchBuffer)
753         return send_message(wm->ioProc, &wm->info, MMIOM_READ, 
754                             (LPARAM)pch, cch, MMIO_PROC_32A);
755
756     /* first try from current buffer */
757     if (wm->info.pchNext != wm->info.pchEndRead) {
758         count = wm->info.pchEndRead - wm->info.pchNext;
759         if (count > cch || count < 0) count = cch;
760         memcpy(pch, wm->info.pchNext, count);
761         wm->info.pchNext += count;
762         pch += count;
763         cch -= count;
764     } else
765         count = 0;
766
767     if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
768         assert(wm->info.cchBuffer);
769
770         while (cch) {
771             LONG size;
772
773             size = MMIO_GrabNextBuffer(wm, TRUE);
774             if (size <= 0) break;
775             if (size > cch) size = cch;
776             memcpy(pch, wm->info.pchBuffer, size);
777             wm->info.pchNext += size;
778             pch += size;
779             cch -= size;
780             count += size;
781         }
782     }
783
784     TRACE("count=%ld\n", count);
785     return count;
786 }
787
788 /**************************************************************************
789  *                              mmioWrite               [WINMM.@]
790  */
791 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
792 {
793     LPWINE_MMIO wm;
794     LONG        count;
795
796     TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
797
798     if ((wm = MMIO_Get(hmmio)) == NULL)
799         return -1;
800
801     if (wm->info.cchBuffer) {
802         LONG    bytesW = 0;
803
804         count = 0;
805         while (cch) {
806             if (wm->info.pchNext != wm->info.pchEndWrite) {
807                 count = wm->info.pchEndWrite - wm->info.pchNext;
808                 if (count > cch || count < 0) count = cch;
809                 memcpy(wm->info.pchNext, pch, count);
810                 wm->info.pchNext += count;
811                 pch += count;
812                 cch -= count;
813                 bytesW += count;
814                 wm->info.dwFlags |= MMIO_DIRTY;
815             } else {
816                 if (wm->info.fccIOProc == FOURCC_MEM) {
817                     if (wm->info.adwInfo[0]) {
818                         /* from where would we get the memory handle? */
819                         FIXME("memory file expansion not implemented!\n");
820                         break;
821                     } else break;
822                 }
823             }
824
825             if (wm->info.pchNext == wm->info.pchEndWrite)
826             {
827                 MMIO_Flush(wm, MMIO_EMPTYBUF);
828                 MMIO_GrabNextBuffer(wm, FALSE);
829             }
830             else break;
831         }
832         count = bytesW;
833     } else {
834         count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
835                              (LPARAM)pch, cch, MMIO_PROC_32A);
836         wm->info.lBufOffset = wm->info.lDiskOffset;
837     }
838
839     TRACE("bytes written=%ld\n", count);
840     return count;
841 }
842
843 /**************************************************************************
844  *                              mmioSeek                [WINMM.@]
845  */
846 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
847 {
848     LPWINE_MMIO wm;
849     LONG        offset;
850
851     TRACE("(%p, %08lX, %d);\n", hmmio, lOffset, iOrigin);
852
853     if ((wm = MMIO_Get(hmmio)) == NULL)
854         return MMSYSERR_INVALHANDLE;
855
856     /* not buffered, direct seek on file */
857     if (!wm->info.pchBuffer)
858         return send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
859                             lOffset, iOrigin, MMIO_PROC_32A);
860
861     switch (iOrigin) {
862     case SEEK_SET:
863         offset = lOffset;
864         break;
865     case SEEK_CUR:
866         offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
867         break;
868     case SEEK_END:
869         offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
870         break;
871     default:
872         return -1;
873     }
874
875     if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
876         /* should check that write mode exists */
877         if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
878             return -1;
879         wm->info.lBufOffset = offset;
880         wm->info.pchEndRead = wm->info.pchBuffer;
881         wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
882         if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
883             wm->info.lDiskOffset = wm->dwFileSize;
884         }
885     } else if ((wm->info.cchBuffer > 0) &&
886         ((offset < wm->info.lBufOffset) ||
887          (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
888          !wm->bBufferLoaded)) {
889         /* stay in same buffer ? */
890         /* some memory mapped buffers are defined with -1 as a size */
891
892         /* condition to change buffer */
893         if ((wm->info.fccIOProc == FOURCC_MEM) ||
894             MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
895             /* this also sets the wm->info.lDiskOffset field */
896             send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
897                          (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
898                          SEEK_SET, MMIO_PROC_32A) == -1)
899             return -1;
900         MMIO_GrabNextBuffer(wm, TRUE);
901     }
902
903     wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
904
905     TRACE("=> %ld\n", offset);
906     return offset;
907 }
908
909 /**************************************************************************
910  *                              mmioGetInfo             [WINMM.@]
911  */
912 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
913 {
914     LPWINE_MMIO         wm;
915
916     TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
917
918     if ((wm = MMIO_Get(hmmio)) == NULL)
919         return MMSYSERR_INVALHANDLE;
920
921     memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
922     /* don't expose 16 bit ioproc:s */
923     if (wm->ioProc->type != MMIO_PROC_16)
924         lpmmioinfo->pIOProc = wm->ioProc->pIOProc;
925
926     return MMSYSERR_NOERROR;
927 }
928
929 /**************************************************************************
930  *                              mmioSetInfo             [WINMM.@]
931  */
932 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
933 {
934     LPWINE_MMIO         wm;
935
936     TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
937
938     if ((wm = MMIO_Get(hmmio)) == NULL)
939         return MMSYSERR_INVALHANDLE;
940
941     /* check pointers coherence */
942     if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
943         lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
944         lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
945         lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
946         lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
947         lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
948         return MMSYSERR_INVALPARAM;
949
950     wm->info.pchNext = lpmmioinfo->pchNext;
951     wm->info.pchEndRead = lpmmioinfo->pchEndRead;
952
953     return MMSYSERR_NOERROR;
954 }
955
956 /**************************************************************************
957 *                               mmioSetBuffer           [WINMM.@]
958 */
959 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
960 {
961     LPWINE_MMIO         wm;
962
963     TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
964           hmmio, pchBuffer, cchBuffer, uFlags);
965
966     if ((wm = MMIO_Get(hmmio)) == NULL)
967         return MMSYSERR_INVALHANDLE;
968
969     return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
970 }
971
972 /**************************************************************************
973  *                              mmioFlush               [WINMM.@]
974  */
975 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
976 {
977     LPWINE_MMIO         wm;
978
979     TRACE("(%p, %04X)\n", hmmio, uFlags);
980
981     if ((wm = MMIO_Get(hmmio)) == NULL)
982         return MMSYSERR_INVALHANDLE;
983
984     return MMIO_Flush(wm, uFlags);
985 }
986
987 /**************************************************************************
988  *                              mmioAdvance             [WINMM.@]
989  */
990 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
991 {
992     LPWINE_MMIO         wm;
993
994     TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
995
996     /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
997      * here. be sure if you change something here to check mmioAdvance16 as well
998      */
999     if ((wm = MMIO_Get(hmmio)) == NULL)
1000         return MMSYSERR_INVALHANDLE;
1001
1002     if (!wm->info.cchBuffer)
1003         return MMIOERR_UNBUFFERED;
1004
1005     if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1006         return MMSYSERR_INVALPARAM;
1007
1008     if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1009     {
1010         send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
1011                      lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
1012         send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
1013                      (LPARAM)lpmmioinfo->pchBuffer,
1014                      lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1015         lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1016     }
1017     if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1018         return MMIOERR_CANNOTWRITE;
1019
1020     if (lpmmioinfo) {
1021         wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset + 
1022                              (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1023     }
1024     MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1025
1026     if (lpmmioinfo) {
1027         lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1028         lpmmioinfo->pchEndRead  = lpmmioinfo->pchBuffer +
1029             (wm->info.pchEndRead - wm->info.pchBuffer);
1030         lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1031             (wm->info.pchEndWrite - wm->info.pchBuffer);
1032         lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1033         lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1034     }
1035     return MMSYSERR_NOERROR;
1036 }
1037
1038 /**************************************************************************
1039  *                              mmioStringToFOURCCA     [WINMM.@]
1040  */
1041 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1042 {
1043     CHAR cc[4];
1044     int i = 0;
1045
1046     for (i = 0; i < 4 && sz[i]; i++) {
1047         if (uFlags & MMIO_TOUPPER) {
1048             cc[i] = toupper(sz[i]);
1049         } else {
1050             cc[i] = sz[i];
1051         }
1052     }
1053
1054     /* Pad with spaces */
1055     while (i < 4) cc[i++] = ' ';
1056
1057     TRACE("Got '%.4s'\n",cc);
1058     return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1059 }
1060
1061 /**************************************************************************
1062  *                              mmioStringToFOURCCW     [WINMM.@]
1063  */
1064 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1065 {
1066     LPSTR       szA = HEAP_strdupWtoA(GetProcessHeap(),0,sz);
1067     FOURCC      ret = mmioStringToFOURCCA(szA,uFlags);
1068
1069     HeapFree(GetProcessHeap(), 0, szA);
1070     return ret;
1071 }
1072
1073 /**************************************************************************
1074  *                              mmioInstallIOProcA         [WINMM.@]
1075  */
1076 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1077                                      LPMMIOPROC pIOProc, DWORD dwFlags)
1078 {
1079     return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
1080 }
1081
1082 /**************************************************************************
1083  *                              mmioInstallIOProcW         [WINMM.@]
1084  */
1085 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1086                                      LPMMIOPROC pIOProc, DWORD dwFlags)
1087 {
1088     return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1089 }
1090
1091 /******************************************************************
1092  *              MMIO_SendMessage
1093  *
1094  *
1095  */
1096 LRESULT         MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1, 
1097                                  LPARAM lParam2, enum mmioProcType type)
1098 {
1099     LPWINE_MMIO         wm;
1100
1101     TRACE("(%p, %u, %ld, %ld, %d)\n", hmmio, uMessage, lParam1, lParam2, type);
1102
1103     if (uMessage < MMIOM_USER)
1104         return MMSYSERR_INVALPARAM;
1105
1106     if ((wm = MMIO_Get(hmmio)) == NULL)
1107         return MMSYSERR_INVALHANDLE;
1108
1109     return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, type);
1110 }
1111
1112 /**************************************************************************
1113  *                              mmioSendMessage         [WINMM.@]
1114  */
1115 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1116                                LPARAM lParam1, LPARAM lParam2)
1117 {
1118     return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, MMIO_PROC_32A);
1119 }
1120
1121 /**************************************************************************
1122  *                              mmioDescend             [WINMM.@]
1123  */
1124 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1125                             const MMCKINFO* lpckParent, UINT uFlags)
1126 {
1127     DWORD               dwOldPos;
1128     FOURCC              srchCkId;
1129     FOURCC              srchType;
1130
1131
1132     TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1133
1134     if (lpck == NULL)
1135         return MMSYSERR_INVALPARAM;
1136
1137     dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1138     TRACE("dwOldPos=%ld\n", dwOldPos);
1139
1140     if (lpckParent != NULL) {
1141         TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1142         /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1143         if (dwOldPos < lpckParent->dwDataOffset ||
1144             dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1145             WARN("outside parent chunk\n");
1146             return MMIOERR_CHUNKNOTFOUND;
1147         }
1148     }
1149
1150     /* The SDK docu says 'ckid' is used for all cases. Real World
1151      * examples disagree -Marcus,990216.
1152      */
1153
1154     srchType = 0;
1155     /* find_chunk looks for 'ckid' */
1156     if (uFlags & MMIO_FINDCHUNK)
1157         srchCkId = lpck->ckid;
1158     /* find_riff and find_list look for 'fccType' */
1159     if (uFlags & MMIO_FINDLIST) {
1160         srchCkId = FOURCC_LIST;
1161         srchType = lpck->fccType;
1162     }
1163     if (uFlags & MMIO_FINDRIFF) {
1164         srchCkId = FOURCC_RIFF;
1165         srchType = lpck->fccType;
1166     }
1167
1168     if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1169         TRACE("searching for %.4s.%.4s\n",
1170               (LPSTR)&srchCkId,
1171               srchType?(LPSTR)&srchType:"any ");
1172
1173         while (TRUE) {
1174             LONG ix;
1175
1176             ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1177             if (ix < 2*sizeof(DWORD)) {
1178                 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1179                 WARN("return ChunkNotFound\n");
1180                 return MMIOERR_CHUNKNOTFOUND;
1181             }
1182             lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1183             if (ix < lpck->dwDataOffset - dwOldPos) {
1184                 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1185                 WARN("return ChunkNotFound\n");
1186                 return MMIOERR_CHUNKNOTFOUND;
1187             }
1188             TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1189                   (LPSTR)&lpck->ckid,
1190                   srchType?(LPSTR)&lpck->fccType:"<na>",
1191                   lpck->cksize);
1192             if ((srchCkId == lpck->ckid) &&
1193                 (!srchType || (srchType == lpck->fccType))
1194                 )
1195                 break;
1196
1197             dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1198             mmioSeek(hmmio, dwOldPos, SEEK_SET);
1199         }
1200     } else {
1201         /* FIXME: unverified, does it do this? */
1202         /* NB: This part is used by WAVE_mciOpen, among others */
1203         if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1204             mmioSeek(hmmio, dwOldPos, SEEK_SET);
1205             WARN("return ChunkNotFound 2nd\n");
1206             return MMIOERR_CHUNKNOTFOUND;
1207         }
1208         lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1209     }
1210     lpck->dwFlags = 0;
1211     /* If we were looking for RIFF/LIST chunks, the final file position
1212      * is after the chunkid. If we were just looking for the chunk
1213      * it is after the cksize. So add 4 in RIFF/LIST case.
1214      */
1215     if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1216         mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1217     else
1218         mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1219     TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
1220           (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1221           lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1222     return MMSYSERR_NOERROR;
1223 }
1224
1225 /**************************************************************************
1226  *                              mmioAscend              [WINMM.@]
1227  */
1228 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1229 {
1230     TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1231
1232     if (lpck->dwFlags & MMIO_DIRTY) {
1233         DWORD   dwOldPos, dwNewSize;
1234
1235         TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1236         dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1237         TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1238         dwNewSize = dwOldPos - lpck->dwDataOffset;
1239         if (dwNewSize != lpck->cksize) {
1240             TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1241             lpck->cksize = dwNewSize;
1242
1243             /* pad odd size with 0 */
1244             if (dwNewSize & 1) {
1245                 char ch = 0;
1246                 mmioWrite(hmmio, &ch, 1);
1247             }
1248             mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1249             mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1250         }
1251         lpck->dwFlags = 0;
1252     }
1253
1254     mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1255
1256     return MMSYSERR_NOERROR;
1257 }
1258
1259 /**************************************************************************
1260  *                      mmioCreateChunk                         [WINMM.@]
1261  */
1262 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1263 {
1264     DWORD       dwOldPos;
1265     LONG        size;
1266     LONG        ix;
1267
1268     TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1269
1270     dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1271     TRACE("dwOldPos=%ld\n", dwOldPos);
1272
1273     if (uFlags == MMIO_CREATELIST)
1274         lpck->ckid = FOURCC_LIST;
1275     else if (uFlags == MMIO_CREATERIFF)
1276         lpck->ckid = FOURCC_RIFF;
1277
1278     TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1279
1280     size = 2 * sizeof(DWORD);
1281     lpck->dwDataOffset = dwOldPos + size;
1282
1283     if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1284         size += sizeof(DWORD);
1285     lpck->dwFlags = MMIO_DIRTY;
1286
1287     ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1288     TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
1289     if (ix < size) {
1290         mmioSeek(hmmio, dwOldPos, SEEK_SET);
1291         WARN("return CannotWrite\n");
1292         return MMIOERR_CANNOTWRITE;
1293     }
1294
1295     return MMSYSERR_NOERROR;
1296 }
1297
1298 /**************************************************************************
1299  *                              mmioRenameA                     [WINMM.@]
1300  */
1301 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1302                             MMIOINFO* lpmmioinfo, DWORD dwFlags)
1303 {
1304     struct IOProcList*  ioProc = NULL;
1305     struct IOProcList   tmp;
1306
1307     TRACE("('%s', '%s', %p, %08lX);\n",
1308           debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1309
1310     /* If both params are NULL, then parse the file name */
1311     if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1312         lpmmioinfo->fccIOProc = MMIO_ParseExtA(szFileName);
1313
1314     /* Handle any unhandled/error case from above. Assume DOS file */
1315     if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL))
1316         ioProc = MMIO_FindProcNode(FOURCC_DOS);
1317     /* if just the four character code is present, look up IO proc */
1318     else if (lpmmioinfo->pIOProc == NULL)
1319         ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1320     else /* use relevant ioProc */
1321     {
1322         ioProc = &tmp;
1323         tmp.fourCC = lpmmioinfo->fccIOProc;
1324         tmp.pIOProc = lpmmioinfo->pIOProc;
1325         tmp.type = MMIO_PROC_32A;
1326         tmp.count = 1;
1327     }
1328
1329     return send_message(ioProc, lpmmioinfo, MMIOM_RENAME,
1330                         (LPARAM)szFileName, (LPARAM)szNewFileName, MMIO_PROC_32A);
1331 }
1332
1333 /**************************************************************************
1334  *                              mmioRenameW                     [WINMM.@]
1335  */
1336 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1337                             MMIOINFO* lpmmioinfo, DWORD dwFlags)
1338 {
1339     LPSTR       szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
1340     LPSTR       sznFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szNewFileName);
1341     UINT        ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1342
1343     HeapFree(GetProcessHeap(),0,szFn);
1344     HeapFree(GetProcessHeap(),0,sznFn);
1345     return ret;
1346 }