ddraw/tests: Add a test for drawing to a flippable surface.
[wine.git] / dlls / winmm / mmio.c
blob7b7dbc3bf67ac8425eb90188149b67f3f7c06e65
1 /*
2 * MMIO functions
4 * Copyright 1998 Andrew Taylor
5 * Copyright 1998 Ove Kåven
6 * Copyright 2000,2002 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* Still to be done:
24 * + correct handling of global/local IOProcs (and temporary IOProcs)
25 * + mode of mmio objects is not used (read vs write vs readwrite)
26 * + thread safeness
27 * + 32 A <=> W message mapping
31 #include <ctype.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <assert.h>
38 #include "ntstatus.h"
39 #define WIN32_NO_STATUS
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winnls.h"
43 #include "winternl.h"
44 #include "ddk/ntddk.h"
45 #include "mmsystem.h"
46 #include "winemm.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
52 static WINE_MMIO *MMIOList;
54 /* From kernel32 */
55 static HANDLE create_file_OF( LPCSTR path, INT mode )
57 DWORD access, sharing, creation;
58 char full_path[MAX_PATH];
60 if (mode & OF_CREATE)
62 creation = CREATE_ALWAYS;
63 access = GENERIC_READ | GENERIC_WRITE;
65 else
67 creation = OPEN_EXISTING;
68 switch(mode & 0x03)
70 case OF_READ: access = GENERIC_READ; break;
71 case OF_WRITE: access = GENERIC_WRITE; break;
72 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
73 default: access = 0; break;
77 switch(mode & 0x70)
79 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
80 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
81 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
82 case OF_SHARE_DENY_NONE:
83 case OF_SHARE_COMPAT:
84 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
87 if (mode & OF_CREATE)
88 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
90 if (!SearchPathA( NULL, path, NULL, MAX_PATH, full_path, NULL ))
91 return INVALID_HANDLE_VALUE;
92 return CreateFileA( full_path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
95 /**************************************************************************
96 * mmioDosIOProc [internal]
98 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
99 LPARAM lParam1, LPARAM lParam2)
101 LRESULT ret = MMSYSERR_NOERROR;
103 TRACE("(%p, %X, 0x%Ix, 0x%Ix);\n", lpmmioinfo, uMessage, lParam1, lParam2);
105 switch (uMessage) {
106 case MMIOM_OPEN:
108 /* Parameters:
109 * lParam1 = szFileName parameter from mmioOpen
110 * lParam2 = reserved
111 * Returns: zero on success, error code on error
112 * NOTE: lDiskOffset automatically set to zero
114 LPCSTR szFileName = (LPCSTR)lParam1;
116 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
117 FIXME("MMIO_GETTEMP not implemented\n");
118 return MMIOERR_CANNOTOPEN;
121 /* if filename NULL, assume open file handle in adwInfo[0] */
122 if (szFileName) {
123 HANDLE file;
125 file = create_file_OF( szFileName, lpmmioinfo->dwFlags );
126 if (file != INVALID_HANDLE_VALUE)
127 lpmmioinfo->adwInfo[0] = HandleToLong(file);
128 else
129 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
131 if (lpmmioinfo->adwInfo[0] == HFILE_ERROR)
132 ret = MMIOERR_FILENOTFOUND;
134 break;
136 case MMIOM_CLOSE:
137 /* Parameters:
138 * lParam1 = wFlags parameter from mmioClose
139 * lParam2 = unused
140 * Returns: zero on success, error code on error
142 if (!(lParam1 & MMIO_FHOPEN))
143 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
144 break;
146 case MMIOM_READ:
147 /* Parameters:
148 * lParam1 = huge pointer to read buffer
149 * lParam2 = number of bytes to read
150 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
151 * in wErrorRet)
153 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
154 if (ret != -1)
155 lpmmioinfo->lDiskOffset += ret;
157 break;
159 case MMIOM_WRITE:
160 case MMIOM_WRITEFLUSH:
161 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
163 /* Parameters:
164 * lParam1 = huge pointer to write buffer
165 * lParam2 = number of bytes to write
166 * Returns: number of bytes written, -1 for error (error code in
167 * wErrorRet)
169 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
170 if (ret != -1)
171 lpmmioinfo->lDiskOffset += ret;
172 break;
174 case MMIOM_SEEK:
175 /* Parameters:
176 * lParam1 = new position
177 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
178 * Returns: new file position, -1 on error
180 if (lParam2 == SEEK_END)
181 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], -(LONG)lParam1, (LONG)lParam2);
182 else
183 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
184 if (ret != -1)
185 lpmmioinfo->lDiskOffset = ret;
186 return ret;
188 case MMIOM_RENAME:
189 /* Parameters:
190 * lParam1 = old name
191 * lParam2 = new name
192 * Returns: zero on success, non-zero on failure
194 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
195 ret = MMIOERR_FILENOTFOUND;
196 break;
198 default:
199 FIXME("unexpected message %u\n", uMessage);
200 return 0;
203 return ret;
206 /**************************************************************************
207 * mmioMemIOProc [internal]
209 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
210 LPARAM lParam1, LPARAM lParam2)
212 TRACE("(%p,0x%04x,0x%08Ix,0x%08Ix)\n", lpmmioinfo, uMessage, lParam1, lParam2);
214 switch (uMessage) {
216 case MMIOM_OPEN:
217 /* Parameters:
218 * lParam1 = filename (must be NULL)
219 * lParam2 = reserved
220 * Returns: zero on success, error code on error
221 * NOTE: lDiskOffset automatically set to zero
223 /* FIXME: io proc shouldn't change it */
224 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
225 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
226 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
227 return 0;
229 case MMIOM_CLOSE:
230 /* Parameters:
231 * lParam1 = wFlags parameter from mmioClose
232 * lParam2 = unused
233 * Returns: zero on success, error code on error
235 return 0;
237 case MMIOM_READ:
238 /* Parameters:
239 * lParam1 = huge pointer to read buffer
240 * lParam2 = number of bytes to read
241 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
242 * in wErrorRet)
243 * NOTE: lDiskOffset should be updated
245 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
246 return 0;
248 case MMIOM_WRITE:
249 case MMIOM_WRITEFLUSH:
250 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
252 /* Parameters:
253 * lParam1 = huge pointer to write buffer
254 * lParam2 = number of bytes to write
255 * Returns: number of bytes written, -1 for error (error code in
256 * wErrorRet)
257 * NOTE: lDiskOffset should be updated
259 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
260 return 0;
262 case MMIOM_SEEK:
263 /* Parameters:
264 * lParam1 = new position
265 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
266 * Returns: new file position, -1 on error
267 * NOTE: lDiskOffset should be updated
269 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
270 return -1;
272 default:
273 FIXME("unexpected message %u\n", uMessage);
274 return 0;
278 /* This array will be the entire list for most apps
279 * Note that temporary ioProcs will be stored with a 0 fourCC code
282 static struct IOProcList defaultProcs[] = {
283 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, FALSE, 0},
284 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, FALSE, 0},
287 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
289 /****************************************************************
290 * MMIO_FindProcNode [INTERNAL]
292 * Finds the ProcList node associated with a given FOURCC code.
294 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
296 struct IOProcList* pListNode;
298 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
299 if (pListNode->fourCC == fccIOProc) {
300 return pListNode;
303 return NULL;
306 /****************************************************************
307 * MMIO_InstallIOProc [INTERNAL]
309 static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
310 DWORD dwFlags, BOOL is_unicode)
312 LPMMIOPROC lpProc = NULL;
313 struct IOProcList* pListNode;
314 struct IOProcList** ppListNode;
316 TRACE("(%08lx, %p, %08lX, %s)\n", fccIOProc, pIOProc, dwFlags, is_unicode ? "unicode" : "ansi");
318 if (dwFlags & MMIO_GLOBALPROC)
319 FIXME("Global procedures not implemented\n");
321 /* just handle the known procedures for now */
322 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
323 case MMIO_INSTALLPROC:
324 /* Create new entry for the IOProc list */
325 pListNode = malloc(sizeof(*pListNode));
326 if (pListNode) {
327 /* Fill in this node */
328 pListNode->fourCC = fccIOProc;
329 pListNode->pIOProc = pIOProc;
330 pListNode->is_unicode = is_unicode;
331 pListNode->count = 0;
333 /* Stick it on the end of the list */
334 pListNode->pNext = pIOProcListAnchor;
335 pIOProcListAnchor = pListNode;
337 /* Return this IOProc - that's how the caller knows we succeeded */
338 lpProc = pIOProc;
340 break;
342 case MMIO_REMOVEPROC:
344 * Search for the node that we're trying to remove
345 * We search for a matching fourCC code if it's non null, or the proc
346 * address otherwise
347 * note that this method won't find the first item on the list, but
348 * since the first two items on this list are ones we won't
349 * let the user delete anyway, that's okay
351 ppListNode = &pIOProcListAnchor;
352 while ((*ppListNode) &&
353 ((fccIOProc != 0) ?
354 (*ppListNode)->fourCC != fccIOProc :
355 (*ppListNode)->pIOProc != pIOProc))
356 ppListNode = &((*ppListNode)->pNext);
358 if (*ppListNode) { /* found it */
359 /* FIXME: what should be done if an open mmio object uses this proc ?
360 * shall we return an error, nuke the mmio object ?
362 if ((*ppListNode)->count) {
363 ERR("Cannot remove a mmIOProc while in use\n");
364 break;
366 /* remove it, but only if it isn't builtin */
367 if ((*ppListNode) >= defaultProcs &&
368 (*ppListNode) < defaultProcs + ARRAY_SIZE(defaultProcs)) {
369 WARN("Tried to remove built-in mmio proc. Skipping\n");
370 } else {
371 /* Okay, nuke it */
372 struct IOProcList* ptmpNode = *ppListNode;
373 lpProc = (*ppListNode)->pIOProc;
374 *ppListNode = (*ppListNode)->pNext;
375 free(ptmpNode);
378 break;
380 case MMIO_FINDPROC:
381 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
382 lpProc = pListNode->pIOProc;
384 break;
387 return lpProc;
390 /****************************************************************
391 * send_message [INTERNAL]
393 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
394 DWORD wMsg, LPARAM lParam1,
395 LPARAM lParam2, BOOL is_unicode)
397 LRESULT result = MMSYSERR_ERROR;
398 LPARAM lp1 = lParam1, lp2 = lParam2;
400 if (!ioProc) {
401 ERR("ioProc NULL\n");
402 return MMSYSERR_INVALPARAM;
405 if (ioProc->is_unicode != is_unicode) {
406 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
407 FIXME("NIY 32 A<=>W mapping\n");
409 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
411 #if 0
412 if (ioProc->is_unicode != is_unicode) {
413 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
415 #endif
417 return result;
420 /**************************************************************************
421 * MMIO_ParseExtA [internal]
423 * Parses a filename for the extension.
425 * RETURNS
426 * The FOURCC code for the extension if found, else 0.
428 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
430 /* Filenames are of the form file.ext{+ABC}
431 For now, we take the last '+' if present */
433 FOURCC ret = 0;
435 /* Note that ext{Start,End} point to the . and + respectively */
436 LPCSTR extEnd;
437 LPCSTR extStart;
439 CHAR ext[5];
441 TRACE("(%s)\n", debugstr_a(szFileName));
443 if (!szFileName)
444 return ret;
446 /* Find the last '+' */
447 extEnd = strrchr(szFileName,'+');
449 if (!extEnd) {
450 /* No + so just an extension */
451 return ret;
452 } else {
453 /* Find the first '.' before '+' */
454 extStart = extEnd - 1;
455 while (extStart >= szFileName && *extStart != '.') {
456 extStart--;
458 if (extStart < szFileName) {
459 ERR("No extension in szFileName: %s\n", debugstr_a(szFileName));
460 return ret;
464 if (extEnd - extStart - 1 > 4)
465 WARN("Extension length > 4\n");
466 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
468 TRACE("Got extension: %s\n", debugstr_a(ext));
470 /* FOURCC codes identifying file-extensions must be uppercase */
471 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
473 return ret;
476 /**************************************************************************
477 * MMIO_Get [internal]
479 * Retrieves the mmio object from current process
481 static LPWINE_MMIO MMIO_Get(HMMIO h)
483 LPWINE_MMIO wm = NULL;
485 EnterCriticalSection(&WINMM_cs);
486 for (wm = MMIOList; wm; wm = wm->lpNext) {
487 if (wm->info.hmmio == h)
488 break;
490 LeaveCriticalSection(&WINMM_cs);
491 return wm;
494 /**************************************************************************
495 * MMIO_Create [internal]
497 * Creates an internal representation for a mmio instance
499 static LPWINE_MMIO MMIO_Create(void)
501 static WORD MMIO_counter = 0;
502 LPWINE_MMIO wm;
504 wm = calloc(1, sizeof(WINE_MMIO));
505 if (wm) {
506 EnterCriticalSection(&WINMM_cs);
507 /* lookup next unallocated WORD handle, with a non NULL value */
508 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
509 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
510 wm->lpNext = MMIOList;
511 MMIOList = wm;
512 LeaveCriticalSection(&WINMM_cs);
514 return wm;
517 /**************************************************************************
518 * MMIO_Destroy [internal]
520 * Destroys an internal representation for a mmio instance
522 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
524 LPWINE_MMIO* m;
526 EnterCriticalSection(&WINMM_cs);
527 /* search for the matching one... */
528 m = &MMIOList;
529 while (*m && *m != wm) m = &(*m)->lpNext;
530 /* ...and destroy */
531 if (*m) {
532 *m = (*m)->lpNext;
533 free(wm);
534 wm = NULL;
536 LeaveCriticalSection(&WINMM_cs);
537 return !wm;
540 /****************************************************************
541 * MMIO_Flush [INTERNAL]
543 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
545 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
546 /* not quite sure what to do here, but I'll guess */
547 if (wm->info.dwFlags & MMIO_DIRTY) {
548 /* FIXME: error handling */
549 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, wm->info.lBufOffset, SEEK_SET, FALSE);
550 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
551 (LPARAM)wm->info.pchBuffer,
552 wm->info.pchNext - wm->info.pchBuffer, FALSE);
554 if (uFlags & MMIO_EMPTYBUF)
555 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
557 wm->info.dwFlags &= ~MMIO_DIRTY;
559 return MMSYSERR_NOERROR;
562 /***************************************************************************
563 * MMIO_GrabNextBuffer [INTERNAL]
565 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
567 LONG size = wm->info.cchBuffer;
569 TRACE("bo=%lx do=%lx\n",
570 wm->info.lBufOffset, wm->info.lDiskOffset);
572 wm->info.lBufOffset = wm->info.lDiskOffset;
573 wm->info.pchNext = wm->info.pchBuffer;
574 wm->info.pchEndRead = wm->info.pchBuffer;
575 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
577 wm->bBufferLoaded = TRUE;
578 if (for_read) {
579 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
580 (LPARAM)wm->info.pchBuffer, size, FALSE);
581 if (size > 0)
582 wm->info.pchEndRead += size;
583 else
584 wm->bBufferLoaded = FALSE;
587 return size;
590 /***************************************************************************
591 * MMIO_SetBuffer [INTERNAL]
593 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
594 UINT uFlags)
596 TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
598 if (cchBuffer > 0xFFFF)
599 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
601 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
602 return MMIOERR_CANNOTWRITE;
604 /* free previous buffer if allocated */
605 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
606 free(wm->info.pchBuffer);
607 wm->info.pchBuffer = NULL;
608 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
611 if (pchBuffer) {
612 wm->info.pchBuffer = pchBuffer;
613 } else if (cchBuffer) {
614 if (!(wm->info.pchBuffer = malloc(cchBuffer)))
615 return MMIOERR_OUTOFMEMORY;
616 wm->info.dwFlags |= MMIO_ALLOCBUF;
617 } else {
618 wm->info.pchBuffer = NULL;
621 wm->info.cchBuffer = cchBuffer;
622 wm->info.pchNext = wm->info.pchBuffer;
623 wm->info.pchEndRead = wm->info.pchBuffer;
624 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
625 wm->info.lBufOffset = wm->info.lDiskOffset;
626 wm->bBufferLoaded = (wm->info.fccIOProc == FOURCC_MEM);
628 return MMSYSERR_NOERROR;
631 /**************************************************************************
632 * MMIO_Open [internal]
634 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, BOOL is_unicode)
636 LPWINE_MMIO wm;
637 MMIOINFO mmioinfo;
638 DWORD pos;
640 TRACE("(%s, %p, %08lX, %s);\n", debugstr_a(szFileName), refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
642 if (!refmminfo) {
643 refmminfo = &mmioinfo;
644 memset(&mmioinfo, 0, sizeof(mmioinfo));
645 is_unicode = FALSE;
648 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
649 char buffer[MAX_PATH];
651 if (!szFileName)
652 return (HMMIO)FALSE;
653 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
654 return (HMMIO)FALSE;
655 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
656 return (HMMIO)FALSE;
657 strcpy(szFileName, buffer);
658 return (HMMIO)TRUE;
661 if ((wm = MMIO_Create()) == NULL)
662 return 0;
664 /* If both params are NULL, then parse the file name if available */
665 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
666 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
667 /* Handle any unhandled/error case. Assume DOS file */
668 if (wm->info.fccIOProc == 0) {
669 wm->info.fccIOProc = FOURCC_DOS;
670 wm->ioProc = &defaultProcs[0];
672 else if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
673 /* If not found, assume DOS file */
674 wm->ioProc = &defaultProcs[0];
676 wm->bTmpIOProc = FALSE;
678 /* if just the four character code is present, look up IO proc */
679 else if (refmminfo->pIOProc == NULL) {
680 wm->info.fccIOProc = refmminfo->fccIOProc;
681 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
682 wm->bTmpIOProc = FALSE;
684 /* if IO proc specified, use it and specified four character code */
685 else {
686 wm->info.fccIOProc = refmminfo->fccIOProc;
687 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
688 MMIO_INSTALLPROC, is_unicode);
689 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
690 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
691 wm->bTmpIOProc = TRUE;
694 wm->ioProc->count++;
695 wm->info.dwFlags = dwOpenFlags;
697 if (dwOpenFlags & MMIO_ALLOCBUF) {
698 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
699 refmminfo->cchBuffer ? refmminfo->cchBuffer : MMIO_DEFAULTBUFFER, 0);
700 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
701 goto error1;
702 } else {
703 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
704 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
705 goto error1;
708 /* see mmioDosIOProc for that one */
709 memcpy( wm->info.adwInfo, refmminfo->adwInfo, sizeof(wm->info.adwInfo) );
711 /* call IO proc to actually open file */
712 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
713 (LPARAM)szFileName, 0, FALSE);
715 /* update offsets and grab file size, when possible */
716 if (wm->info.fccIOProc == FOURCC_DOS && (send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE)) != -1) {
717 pos = wm->info.lBufOffset = wm->info.lDiskOffset;
718 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_END, FALSE);
719 wm->dwFileSize = wm->info.lDiskOffset;
720 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, pos, SEEK_SET, FALSE);
722 else wm->dwFileSize = 0;
724 if (refmminfo->wErrorRet == 0)
725 return wm->info.hmmio;
726 error1:
727 if (wm->info.dwFlags & MMIO_ALLOCBUF)
728 free(wm->info.pchBuffer);
729 if (wm->ioProc) wm->ioProc->count--;
730 error2:
731 MMIO_Destroy(wm);
732 return 0;
735 /**************************************************************************
736 * mmioOpenW [WINMM.@]
738 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
739 DWORD dwOpenFlags)
741 HMMIO ret;
742 LPSTR szFn = NULL;
744 if (szFileName)
746 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
747 if (len < MAX_PATH) len = MAX_PATH;
748 szFn = malloc( len );
749 if (!szFn) return NULL;
750 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
753 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
755 free(szFn);
756 return ret;
759 /**************************************************************************
760 * mmioOpenA [WINMM.@]
762 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
763 DWORD dwOpenFlags)
765 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
768 /**************************************************************************
769 * mmioClose [WINMM.@]
771 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
773 LPWINE_MMIO wm;
774 MMRESULT result;
776 TRACE("(%p, %04X);\n", hmmio, uFlags);
778 if ((wm = MMIO_Get(hmmio)) == NULL)
779 return MMSYSERR_INVALHANDLE;
781 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
782 return result;
784 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
786 MMIO_SetBuffer(wm, NULL, 0, 0);
788 wm->ioProc->count--;
790 if (wm->bTmpIOProc)
791 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
792 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
794 MMIO_Destroy(wm);
796 return result;
799 /**************************************************************************
800 * mmioRead [WINMM.@]
802 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
804 LPWINE_MMIO wm;
805 LONG count;
807 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
809 if ((wm = MMIO_Get(hmmio)) == NULL)
810 return -1;
812 /* unbuffered case first */
813 if (!wm->info.pchBuffer)
814 return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
816 /* first try from current buffer */
817 if (cch && wm->info.fccIOProc != FOURCC_MEM && wm->info.pchNext == wm->info.pchEndRead)
818 MMIO_GrabNextBuffer(wm, TRUE);
819 if (wm->info.pchNext != wm->info.pchEndRead) {
820 count = wm->info.pchEndRead - wm->info.pchNext;
821 if (count > cch || count < 0) count = cch;
822 memcpy(pch, wm->info.pchNext, count);
823 wm->info.pchNext += count;
824 pch += count;
825 cch -= count;
826 } else
827 count = 0;
829 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
830 assert(wm->info.cchBuffer);
832 while (cch) {
833 LONG size;
835 size = MMIO_GrabNextBuffer(wm, TRUE);
836 if (size <= 0) break;
837 if (size > cch) size = cch;
838 memcpy(pch, wm->info.pchBuffer, size);
839 wm->info.pchNext += size;
840 pch += size;
841 cch -= size;
842 count += size;
844 wm->bBufferLoaded = FALSE;
845 mmioSeek(hmmio, 0, SEEK_CUR);
848 TRACE("count=%ld\n", count);
849 return count;
852 /**************************************************************************
853 * mmioWrite [WINMM.@]
855 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
857 LPWINE_MMIO wm;
858 LONG count;
860 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
862 if ((wm = MMIO_Get(hmmio)) == NULL)
863 return -1;
865 if (wm->info.cchBuffer) {
866 LONG bytesW = 0;
868 while (cch) {
869 if (wm->info.pchNext != wm->info.pchEndWrite) {
870 count = wm->info.pchEndWrite - wm->info.pchNext;
871 if (count > cch || count < 0) count = cch;
872 memcpy(wm->info.pchNext, pch, count);
873 wm->info.pchNext += count;
874 pch += count;
875 cch -= count;
876 bytesW += count;
877 wm->info.dwFlags |= MMIO_DIRTY;
878 } else {
879 if (wm->info.fccIOProc == FOURCC_MEM) {
880 if (wm->info.adwInfo[0]) {
881 /* from where would we get the memory handle? */
882 FIXME("memory file expansion not implemented!\n");
883 break;
884 } else break;
888 if (wm->info.pchNext == wm->info.pchEndWrite)
890 MMIO_Flush(wm, MMIO_EMPTYBUF);
891 MMIO_GrabNextBuffer(wm, FALSE);
893 else break;
895 count = bytesW;
896 } else {
897 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
898 wm->info.lBufOffset = wm->info.lDiskOffset;
901 TRACE("bytes written=%ld\n", count);
902 return count;
905 /**************************************************************************
906 * mmioSeek [WINMM.@]
908 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
910 LPWINE_MMIO wm;
911 LONG offset;
913 TRACE("(%p, %08lX, %d);\n", hmmio, lOffset, iOrigin);
915 if ((wm = MMIO_Get(hmmio)) == NULL)
916 return MMSYSERR_INVALHANDLE;
918 /* not buffered, direct seek on file */
919 if (!wm->info.pchBuffer && wm->info.fccIOProc != FOURCC_MEM) {
920 LRESULT ret = send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, iOrigin, FALSE);
921 if (ret != -1)
922 wm->info.lBufOffset = wm->info.lDiskOffset;
923 return ret;
926 switch (iOrigin) {
927 case SEEK_SET:
928 offset = lOffset;
929 break;
930 case SEEK_CUR:
931 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
932 break;
933 case SEEK_END:
934 switch (wm->info.fccIOProc) {
935 case FOURCC_MEM:
936 offset = wm->info.cchBuffer - lOffset;
937 break;
938 case FOURCC_DOS:
939 offset = wm->dwFileSize - lOffset;
940 break;
941 default:
942 offset = send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, SEEK_END, FALSE);
943 break;
945 break;
946 default:
947 return -1;
950 /* stay in same buffer ? */
951 /* some memory mapped buffers are defined with -1 as a size */
952 if ((wm->info.cchBuffer > 0) &&
953 ((offset < wm->info.lBufOffset) ||
954 (offset > wm->info.lBufOffset + wm->info.cchBuffer) ||
955 (offset > wm->dwFileSize && wm->info.fccIOProc == FOURCC_DOS) ||
956 !wm->bBufferLoaded)) {
958 /* condition to change buffer */
959 if ((wm->info.fccIOProc == FOURCC_MEM) ||
960 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
961 /* this also sets the wm->info.lDiskOffset field */
962 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
963 offset, SEEK_SET, FALSE) == -1)
964 return -1;
965 wm->info.lBufOffset = offset;
966 wm->bBufferLoaded = FALSE;
967 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
969 else
970 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
972 TRACE("=> %ld\n", offset);
973 return offset;
976 /**************************************************************************
977 * mmioGetInfo [WINMM.@]
979 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
981 LPWINE_MMIO wm;
983 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
985 if ((wm = MMIO_Get(hmmio)) == NULL)
986 return MMSYSERR_INVALHANDLE;
988 *lpmmioinfo = wm->info;
990 return MMSYSERR_NOERROR;
993 /**************************************************************************
994 * mmioSetInfo [WINMM.@]
996 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
998 LPWINE_MMIO wm;
1000 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
1002 if ((wm = MMIO_Get(hmmio)) == NULL)
1003 return MMSYSERR_INVALHANDLE;
1005 /* check pointers coherence */
1006 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
1007 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
1008 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
1009 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
1010 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
1011 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
1012 return MMSYSERR_INVALPARAM;
1014 wm->info = *lpmmioinfo;
1015 return MMSYSERR_NOERROR;
1018 /**************************************************************************
1019 * mmioSetBuffer [WINMM.@]
1021 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
1023 LPWINE_MMIO wm;
1025 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
1026 hmmio, pchBuffer, cchBuffer, uFlags);
1028 if ((wm = MMIO_Get(hmmio)) == NULL)
1029 return MMSYSERR_INVALHANDLE;
1031 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
1034 /**************************************************************************
1035 * mmioFlush [WINMM.@]
1037 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
1039 LPWINE_MMIO wm;
1041 TRACE("(%p, %04X)\n", hmmio, uFlags);
1043 if ((wm = MMIO_Get(hmmio)) == NULL)
1044 return MMSYSERR_INVALHANDLE;
1046 return MMIO_Flush(wm, uFlags);
1049 /**************************************************************************
1050 * mmioAdvance [WINMM.@]
1052 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1054 LPWINE_MMIO wm;
1056 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1058 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1059 * here. be sure if you change something here to check mmioAdvance16 as well
1061 if ((wm = MMIO_Get(hmmio)) == NULL)
1062 return MMSYSERR_INVALHANDLE;
1064 if (!wm->info.cchBuffer)
1065 return MMIOERR_UNBUFFERED;
1067 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1068 return MMSYSERR_INVALPARAM;
1070 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1071 return MMIOERR_CANNOTWRITE;
1072 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1074 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
1075 send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1076 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1077 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1080 if (lpmmioinfo && lpmmioinfo->fccIOProc == FOURCC_DOS) {
1081 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1082 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1084 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1086 if (lpmmioinfo) {
1087 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1088 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1089 (wm->info.pchEndRead - wm->info.pchBuffer);
1090 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1091 (wm->info.pchEndWrite - wm->info.pchBuffer);
1092 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1093 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1095 return MMSYSERR_NOERROR;
1098 /**************************************************************************
1099 * mmioStringToFOURCCA [WINMM.@]
1101 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1103 CHAR cc[4];
1104 int i = 0;
1106 for (i = 0; i < 4 && sz[i]; i++) {
1107 if (uFlags & MMIO_TOUPPER) {
1108 cc[i] = RtlUpperChar(sz[i]);
1109 } else {
1110 cc[i] = sz[i];
1114 /* Pad with spaces */
1115 while (i < 4) cc[i++] = ' ';
1117 TRACE("Got %s\n",debugstr_an(cc,4));
1118 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1121 /**************************************************************************
1122 * mmioStringToFOURCCW [WINMM.@]
1124 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1126 char szA[4];
1128 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1129 return mmioStringToFOURCCA(szA,uFlags);
1132 /**************************************************************************
1133 * mmioInstallIOProcA [WINMM.@]
1135 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1136 LPMMIOPROC pIOProc, DWORD dwFlags)
1138 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, FALSE);
1141 /**************************************************************************
1142 * mmioInstallIOProcW [WINMM.@]
1144 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1145 LPMMIOPROC pIOProc, DWORD dwFlags)
1147 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, TRUE);
1150 /******************************************************************
1151 * MMIO_SendMessage
1155 static LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1156 LPARAM lParam2, BOOL is_unicode)
1158 LPWINE_MMIO wm;
1160 TRACE("(%p, %u, %Id, %Id, %s)\n", hmmio, uMessage, lParam1, lParam2, is_unicode ? "unicode" : "ansi");
1162 if (uMessage < MMIOM_USER)
1163 return MMSYSERR_INVALPARAM;
1165 if ((wm = MMIO_Get(hmmio)) == NULL)
1166 return MMSYSERR_INVALHANDLE;
1168 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, is_unicode);
1171 /**************************************************************************
1172 * mmioSendMessage [WINMM.@]
1174 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1175 LPARAM lParam1, LPARAM lParam2)
1177 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, FALSE);
1180 /**************************************************************************
1181 * mmioDescend [WINMM.@]
1183 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1184 const MMCKINFO* lpckParent, UINT uFlags)
1186 DWORD dwOldPos;
1187 FOURCC srchCkId;
1188 FOURCC srchType;
1190 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1192 if (lpck == NULL)
1193 return MMSYSERR_INVALPARAM;
1195 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1196 TRACE("dwOldPos=%ld\n", dwOldPos);
1198 if (lpckParent != NULL) {
1199 TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1200 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1201 if (dwOldPos < lpckParent->dwDataOffset ||
1202 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1203 WARN("outside parent chunk\n");
1204 return MMIOERR_CHUNKNOTFOUND;
1208 /* The SDK docu says 'ckid' is used for all cases. Real World
1209 * examples disagree -Marcus,990216.
1212 srchCkId = 0;
1213 srchType = 0;
1215 /* find_chunk looks for 'ckid' */
1216 if (uFlags & MMIO_FINDCHUNK)
1217 srchCkId = lpck->ckid;
1219 /* find_riff and find_list look for 'fccType' */
1220 if (uFlags & MMIO_FINDLIST)
1222 srchCkId = FOURCC_LIST;
1223 srchType = lpck->fccType;
1226 if (uFlags & MMIO_FINDRIFF)
1228 srchCkId = FOURCC_RIFF;
1229 srchType = lpck->fccType;
1232 TRACE("searching for %s.%s\n",
1233 debugstr_an((LPCSTR)&srchCkId, 4), srchType ? debugstr_an((LPCSTR)&srchType, 4) : "<any>");
1235 while (TRUE)
1237 LONG ix;
1239 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1240 if (ix < 0 || ix < 2*sizeof(DWORD))
1242 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1243 WARN("return ChunkNotFound\n");
1244 return MMIOERR_CHUNKNOTFOUND;
1247 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1248 TRACE("ckid=%s fcc=%s cksize=%08lX !\n",
1249 debugstr_an((LPCSTR)&lpck->ckid, 4),
1250 srchType ? debugstr_an((LPCSTR)&lpck->fccType, 4) : "<na>",
1251 lpck->cksize);
1252 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1253 (!srchType || (srchType == lpck->fccType)) )
1254 break;
1256 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1257 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1260 lpck->dwFlags = 0;
1261 /* If we were looking for RIFF/LIST chunks, the final file position
1262 * is after the chunkid. If we were just looking for the chunk
1263 * it is after the cksize. So add 4 in RIFF/LIST case.
1265 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1266 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1267 else
1269 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1270 lpck->fccType = 0;
1272 TRACE("lpck: ckid=%s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%s)!\n",
1273 debugstr_an((LPSTR)&lpck->ckid, 4), lpck->cksize, lpck->dwDataOffset,
1274 lpck->fccType, srchType ? debugstr_an((LPSTR)&lpck->fccType, 4):"");
1275 return MMSYSERR_NOERROR;
1278 /**************************************************************************
1279 * mmioAscend [WINMM.@]
1281 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1283 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1285 if (lpck->dwFlags & MMIO_DIRTY) {
1286 DWORD dwOldPos, dwNewSize;
1288 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1289 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1290 TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1291 dwNewSize = dwOldPos - lpck->dwDataOffset;
1292 if (dwNewSize != lpck->cksize) {
1293 TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1294 lpck->cksize = dwNewSize;
1296 /* pad odd size with 0 */
1297 if (dwNewSize & 1) {
1298 char ch = 0;
1299 mmioWrite(hmmio, &ch, 1);
1301 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1302 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1304 lpck->dwFlags = 0;
1307 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1309 return MMSYSERR_NOERROR;
1312 /**************************************************************************
1313 * mmioCreateChunk [WINMM.@]
1315 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1317 DWORD dwOldPos;
1318 LONG size;
1319 LONG ix;
1321 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1323 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1324 TRACE("dwOldPos=%ld\n", dwOldPos);
1326 if (uFlags == MMIO_CREATELIST)
1327 lpck->ckid = FOURCC_LIST;
1328 else if (uFlags == MMIO_CREATERIFF)
1329 lpck->ckid = FOURCC_RIFF;
1331 TRACE("ckid=%s\n", debugstr_an((LPSTR)&lpck->ckid, 4));
1333 size = 2 * sizeof(DWORD);
1334 lpck->dwDataOffset = dwOldPos + size;
1336 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1337 size += sizeof(DWORD);
1338 lpck->dwFlags = MMIO_DIRTY;
1340 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1341 TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n", ix, size, errno);
1342 if (ix != size) {
1343 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1344 WARN("return CannotWrite\n");
1345 return MMIOERR_CANNOTWRITE;
1348 return MMSYSERR_NOERROR;
1351 /**************************************************************************
1352 * mmioRenameA [WINMM.@]
1354 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1355 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1357 struct IOProcList* ioProc = NULL;
1358 struct IOProcList tmp;
1359 FOURCC fcc;
1361 TRACE("(%s, %s, %p, %08lX);\n",
1362 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1364 /* If both params are NULL, then parse the file name */
1365 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1367 fcc = MMIO_ParseExtA(szFileName);
1368 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1371 /* Handle any unhandled/error case from above. Assume DOS file */
1372 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1373 ioProc = &defaultProcs[0];
1374 /* if just the four character code is present, look up IO proc */
1375 else if (lpmmioinfo->pIOProc == NULL)
1376 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1377 else /* use relevant ioProc */
1379 ioProc = &tmp;
1380 tmp.fourCC = lpmmioinfo->fccIOProc;
1381 tmp.pIOProc = lpmmioinfo->pIOProc;
1382 tmp.is_unicode = FALSE;
1383 tmp.count = 1;
1386 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1387 * or make a copy of it because it's const ???
1389 return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1390 (LPARAM)szFileName, (LPARAM)szNewFileName, FALSE);
1393 /**************************************************************************
1394 * mmioRenameW [WINMM.@]
1396 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1397 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1399 LPSTR szFn = NULL;
1400 LPSTR sznFn = NULL;
1401 UINT ret = MMSYSERR_NOMEM;
1402 INT len;
1404 if (szFileName)
1406 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1407 szFn = malloc( len );
1408 if (!szFn) goto done;
1409 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1411 if (szNewFileName)
1413 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1414 sznFn = malloc( len );
1415 if (!sznFn) goto done;
1416 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1419 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1421 done:
1422 free(szFn);
1423 free(sznFn);
1424 return ret;