vbscript: Handle index read access to array properties.
[wine.git] / dlls / winmm / mmio.c
blobd61b3c90c499b547bbc3cd9bbef62f2e6b8add79
1 /*
2 * MMIO functions
4 * Copyright 1998 Andrew Taylor
5 * Copyright 1998 Ove Kåven
6 * Copyright 2000,2002 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* Still to be done:
24 * + correct handling of global/local IOProcs (and temporary IOProcs)
25 * + mode of mmio objects is not used (read vs write vs readwrite)
26 * + thread safeness
27 * + 32 A <=> W message mapping
31 #include <ctype.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <assert.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "winternl.h"
42 #include "mmsystem.h"
43 #include "winemm.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(mmio);
49 static WINE_MMIO *MMIOList;
51 /* From kernel32 */
52 static HANDLE create_file_OF( LPCSTR path, INT mode )
54 DWORD access, sharing, creation;
55 char full_path[MAX_PATH];
57 if (mode & OF_CREATE)
59 creation = CREATE_ALWAYS;
60 access = GENERIC_READ | GENERIC_WRITE;
62 else
64 creation = OPEN_EXISTING;
65 switch(mode & 0x03)
67 case OF_READ: access = GENERIC_READ; break;
68 case OF_WRITE: access = GENERIC_WRITE; break;
69 case OF_READWRITE: access = GENERIC_READ | GENERIC_WRITE; break;
70 default: access = 0; break;
74 switch(mode & 0x70)
76 case OF_SHARE_EXCLUSIVE: sharing = 0; break;
77 case OF_SHARE_DENY_WRITE: sharing = FILE_SHARE_READ; break;
78 case OF_SHARE_DENY_READ: sharing = FILE_SHARE_WRITE; break;
79 case OF_SHARE_DENY_NONE:
80 case OF_SHARE_COMPAT:
81 default: sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
84 if (mode & OF_CREATE)
85 return CreateFileA( path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
87 if (!SearchPathA( NULL, path, NULL, MAX_PATH, full_path, NULL ))
88 return INVALID_HANDLE_VALUE;
89 return CreateFileA( full_path, access, sharing, NULL, creation, FILE_ATTRIBUTE_NORMAL, 0 );
92 /**************************************************************************
93 * mmioDosIOProc [internal]
95 static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
96 LPARAM lParam1, LPARAM lParam2)
98 LRESULT ret = MMSYSERR_NOERROR;
100 TRACE("(%p, %X, 0x%Ix, 0x%Ix);\n", lpmmioinfo, uMessage, lParam1, lParam2);
102 switch (uMessage) {
103 case MMIOM_OPEN:
105 /* Parameters:
106 * lParam1 = szFileName parameter from mmioOpen
107 * lParam2 = reserved
108 * Returns: zero on success, error code on error
109 * NOTE: lDiskOffset automatically set to zero
111 LPCSTR szFileName = (LPCSTR)lParam1;
113 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
114 FIXME("MMIO_GETTEMP not implemented\n");
115 return MMIOERR_CANNOTOPEN;
118 /* if filename NULL, assume open file handle in adwInfo[0] */
119 if (szFileName) {
120 HANDLE file;
122 file = create_file_OF( szFileName, lpmmioinfo->dwFlags );
123 if (file != INVALID_HANDLE_VALUE)
124 lpmmioinfo->adwInfo[0] = HandleToLong(file);
125 else
126 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
128 if (lpmmioinfo->adwInfo[0] == HFILE_ERROR)
129 ret = MMIOERR_FILENOTFOUND;
131 break;
133 case MMIOM_CLOSE:
134 /* Parameters:
135 * lParam1 = wFlags parameter from mmioClose
136 * lParam2 = unused
137 * Returns: zero on success, error code on error
139 if (!(lParam1 & MMIO_FHOPEN))
140 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
141 break;
143 case MMIOM_READ:
144 /* Parameters:
145 * lParam1 = huge pointer to read buffer
146 * lParam2 = number of bytes to read
147 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
148 * in wErrorRet)
150 ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
151 if (ret != -1)
152 lpmmioinfo->lDiskOffset += ret;
154 break;
156 case MMIOM_WRITE:
157 case MMIOM_WRITEFLUSH:
158 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
160 /* Parameters:
161 * lParam1 = huge pointer to write buffer
162 * lParam2 = number of bytes to write
163 * Returns: number of bytes written, -1 for error (error code in
164 * wErrorRet)
166 ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
167 if (ret != -1)
168 lpmmioinfo->lDiskOffset += ret;
169 break;
171 case MMIOM_SEEK:
172 /* Parameters:
173 * lParam1 = new position
174 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
175 * Returns: new file position, -1 on error
177 if (lParam2 == SEEK_END)
178 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], -(LONG)lParam1, (LONG)lParam2);
179 else
180 ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
181 if (ret != -1)
182 lpmmioinfo->lDiskOffset = ret;
183 return ret;
185 case MMIOM_RENAME:
186 /* Parameters:
187 * lParam1 = old name
188 * lParam2 = new name
189 * Returns: zero on success, non-zero on failure
191 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
192 ret = MMIOERR_FILENOTFOUND;
193 break;
195 default:
196 FIXME("unexpected message %u\n", uMessage);
197 return 0;
200 return ret;
203 /**************************************************************************
204 * mmioMemIOProc [internal]
206 static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
207 LPARAM lParam1, LPARAM lParam2)
209 TRACE("(%p,0x%04x,0x%08Ix,0x%08Ix)\n", lpmmioinfo, uMessage, lParam1, lParam2);
211 switch (uMessage) {
213 case MMIOM_OPEN:
214 /* Parameters:
215 * lParam1 = filename (must be NULL)
216 * lParam2 = reserved
217 * Returns: zero on success, error code on error
218 * NOTE: lDiskOffset automatically set to zero
220 /* FIXME: io proc shouldn't change it */
221 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
222 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
223 lpmmioinfo->adwInfo[0] = HFILE_ERROR;
224 return 0;
226 case MMIOM_CLOSE:
227 /* Parameters:
228 * lParam1 = wFlags parameter from mmioClose
229 * lParam2 = unused
230 * Returns: zero on success, error code on error
232 return 0;
234 case MMIOM_READ:
235 /* Parameters:
236 * lParam1 = huge pointer to read buffer
237 * lParam2 = number of bytes to read
238 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
239 * in wErrorRet)
240 * NOTE: lDiskOffset should be updated
242 FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
243 return 0;
245 case MMIOM_WRITE:
246 case MMIOM_WRITEFLUSH:
247 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
249 /* Parameters:
250 * lParam1 = huge pointer to write buffer
251 * lParam2 = number of bytes to write
252 * Returns: number of bytes written, -1 for error (error code in
253 * wErrorRet)
254 * NOTE: lDiskOffset should be updated
256 FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
257 return 0;
259 case MMIOM_SEEK:
260 /* Parameters:
261 * lParam1 = new position
262 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
263 * Returns: new file position, -1 on error
264 * NOTE: lDiskOffset should be updated
266 FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
267 return -1;
269 default:
270 FIXME("unexpected message %u\n", uMessage);
271 return 0;
275 /* This array will be the entire list for most apps
276 * Note that temporary ioProcs will be stored with a 0 fourCC code
279 static struct IOProcList defaultProcs[] = {
280 {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, FALSE, 0},
281 {NULL, FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, FALSE, 0},
284 static struct IOProcList* pIOProcListAnchor = &defaultProcs[0];
286 /****************************************************************
287 * MMIO_FindProcNode [INTERNAL]
289 * Finds the ProcList node associated with a given FOURCC code.
291 static struct IOProcList* MMIO_FindProcNode(FOURCC fccIOProc)
293 struct IOProcList* pListNode;
295 for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
296 if (pListNode->fourCC == fccIOProc) {
297 return pListNode;
300 return NULL;
303 /****************************************************************
304 * MMIO_InstallIOProc [INTERNAL]
306 static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
307 DWORD dwFlags, BOOL is_unicode)
309 LPMMIOPROC lpProc = NULL;
310 struct IOProcList* pListNode;
311 struct IOProcList** ppListNode;
313 TRACE("(%08lx, %p, %08lX, %s)\n", fccIOProc, pIOProc, dwFlags, is_unicode ? "unicode" : "ansi");
315 if (dwFlags & MMIO_GLOBALPROC)
316 FIXME("Global procedures not implemented\n");
318 /* just handle the known procedures for now */
319 switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
320 case MMIO_INSTALLPROC:
321 /* Create new entry for the IOProc list */
322 pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
323 if (pListNode) {
324 /* Fill in this node */
325 pListNode->fourCC = fccIOProc;
326 pListNode->pIOProc = pIOProc;
327 pListNode->is_unicode = is_unicode;
328 pListNode->count = 0;
330 /* Stick it on the end of the list */
331 pListNode->pNext = pIOProcListAnchor;
332 pIOProcListAnchor = pListNode;
334 /* Return this IOProc - that's how the caller knows we succeeded */
335 lpProc = pIOProc;
337 break;
339 case MMIO_REMOVEPROC:
341 * Search for the node that we're trying to remove
342 * We search for a matching fourCC code if it's non null, or the proc
343 * address otherwise
344 * note that this method won't find the first item on the list, but
345 * since the first two items on this list are ones we won't
346 * let the user delete anyway, that's okay
348 ppListNode = &pIOProcListAnchor;
349 while ((*ppListNode) &&
350 ((fccIOProc != 0) ?
351 (*ppListNode)->fourCC != fccIOProc :
352 (*ppListNode)->pIOProc != pIOProc))
353 ppListNode = &((*ppListNode)->pNext);
355 if (*ppListNode) { /* found it */
356 /* FIXME: what should be done if an open mmio object uses this proc ?
357 * shall we return an error, nuke the mmio object ?
359 if ((*ppListNode)->count) {
360 ERR("Cannot remove a mmIOProc while in use\n");
361 break;
363 /* remove it, but only if it isn't builtin */
364 if ((*ppListNode) >= defaultProcs &&
365 (*ppListNode) < defaultProcs + ARRAY_SIZE(defaultProcs)) {
366 WARN("Tried to remove built-in mmio proc. Skipping\n");
367 } else {
368 /* Okay, nuke it */
369 struct IOProcList* ptmpNode = *ppListNode;
370 lpProc = (*ppListNode)->pIOProc;
371 *ppListNode = (*ppListNode)->pNext;
372 HeapFree(GetProcessHeap(), 0, ptmpNode);
375 break;
377 case MMIO_FINDPROC:
378 if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
379 lpProc = pListNode->pIOProc;
381 break;
384 return lpProc;
387 /****************************************************************
388 * send_message [INTERNAL]
390 static LRESULT send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
391 DWORD wMsg, LPARAM lParam1,
392 LPARAM lParam2, BOOL is_unicode)
394 LRESULT result = MMSYSERR_ERROR;
395 LPARAM lp1 = lParam1, lp2 = lParam2;
397 if (!ioProc) {
398 ERR("ioProc NULL\n");
399 return MMSYSERR_INVALPARAM;
402 if (ioProc->is_unicode != is_unicode) {
403 /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
404 FIXME("NIY 32 A<=>W mapping\n");
406 result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
408 #if 0
409 if (ioProc->is_unicode != is_unicode) {
410 /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
412 #endif
414 return result;
417 /**************************************************************************
418 * MMIO_ParseExtA [internal]
420 * Parses a filename for the extension.
422 * RETURNS
423 * The FOURCC code for the extension if found, else 0.
425 static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
427 /* Filenames are of the form file.ext{+ABC}
428 For now, we take the last '+' if present */
430 FOURCC ret = 0;
432 /* Note that ext{Start,End} point to the . and + respectively */
433 LPCSTR extEnd;
434 LPCSTR extStart;
436 CHAR ext[5];
438 TRACE("(%s)\n", debugstr_a(szFileName));
440 if (!szFileName)
441 return ret;
443 /* Find the last '+' */
444 extEnd = strrchr(szFileName,'+');
446 if (!extEnd) {
447 /* No + so just an extension */
448 return ret;
449 } else {
450 /* Find the first '.' before '+' */
451 extStart = extEnd - 1;
452 while (extStart >= szFileName && *extStart != '.') {
453 extStart--;
455 if (extStart < szFileName) {
456 ERR("No extension in szFileName: %s\n", debugstr_a(szFileName));
457 return ret;
461 if (extEnd - extStart - 1 > 4)
462 WARN("Extension length > 4\n");
463 lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));
465 TRACE("Got extension: %s\n", debugstr_a(ext));
467 /* FOURCC codes identifying file-extensions must be uppercase */
468 ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
470 return ret;
473 /**************************************************************************
474 * MMIO_Get [internal]
476 * Retrieves the mmio object from current process
478 static LPWINE_MMIO MMIO_Get(HMMIO h)
480 LPWINE_MMIO wm = NULL;
482 EnterCriticalSection(&WINMM_cs);
483 for (wm = MMIOList; wm; wm = wm->lpNext) {
484 if (wm->info.hmmio == h)
485 break;
487 LeaveCriticalSection(&WINMM_cs);
488 return wm;
491 /**************************************************************************
492 * MMIO_Create [internal]
494 * Creates an internal representation for a mmio instance
496 static LPWINE_MMIO MMIO_Create(void)
498 static WORD MMIO_counter = 0;
499 LPWINE_MMIO wm;
501 wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
502 if (wm) {
503 EnterCriticalSection(&WINMM_cs);
504 /* lookup next unallocated WORD handle, with a non NULL value */
505 while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
506 wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
507 wm->lpNext = MMIOList;
508 MMIOList = wm;
509 LeaveCriticalSection(&WINMM_cs);
511 return wm;
514 /**************************************************************************
515 * MMIO_Destroy [internal]
517 * Destroys an internal representation for a mmio instance
519 static BOOL MMIO_Destroy(LPWINE_MMIO wm)
521 LPWINE_MMIO* m;
523 EnterCriticalSection(&WINMM_cs);
524 /* search for the matching one... */
525 m = &MMIOList;
526 while (*m && *m != wm) m = &(*m)->lpNext;
527 /* ...and destroy */
528 if (*m) {
529 *m = (*m)->lpNext;
530 HeapFree(GetProcessHeap(), 0, wm);
531 wm = NULL;
533 LeaveCriticalSection(&WINMM_cs);
534 return !wm;
537 /****************************************************************
538 * MMIO_Flush [INTERNAL]
540 static MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
542 if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
543 /* not quite sure what to do here, but I'll guess */
544 if (wm->info.dwFlags & MMIO_DIRTY) {
545 /* FIXME: error handling */
546 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, wm->info.lBufOffset, SEEK_SET, FALSE);
547 send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
548 (LPARAM)wm->info.pchBuffer,
549 wm->info.pchNext - wm->info.pchBuffer, FALSE);
551 if (uFlags & MMIO_EMPTYBUF)
552 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
554 wm->info.dwFlags &= ~MMIO_DIRTY;
556 return MMSYSERR_NOERROR;
559 /***************************************************************************
560 * MMIO_GrabNextBuffer [INTERNAL]
562 static LONG MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
564 LONG size = wm->info.cchBuffer;
566 TRACE("bo=%lx do=%lx\n",
567 wm->info.lBufOffset, wm->info.lDiskOffset);
569 wm->info.lBufOffset = wm->info.lDiskOffset;
570 wm->info.pchNext = wm->info.pchBuffer;
571 wm->info.pchEndRead = wm->info.pchBuffer;
572 wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
574 wm->bBufferLoaded = TRUE;
575 if (for_read) {
576 size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
577 (LPARAM)wm->info.pchBuffer, size, FALSE);
578 if (size > 0)
579 wm->info.pchEndRead += size;
580 else
581 wm->bBufferLoaded = FALSE;
584 return size;
587 /***************************************************************************
588 * MMIO_SetBuffer [INTERNAL]
590 static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
591 UINT uFlags)
593 TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
595 if (cchBuffer > 0xFFFF)
596 WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
598 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
599 return MMIOERR_CANNOTWRITE;
601 /* free previous buffer if allocated */
602 if (wm->info.dwFlags & MMIO_ALLOCBUF) {
603 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
604 wm->info.pchBuffer = NULL;
605 wm->info.dwFlags &= ~MMIO_ALLOCBUF;
608 if (pchBuffer) {
609 wm->info.pchBuffer = pchBuffer;
610 } else if (cchBuffer) {
611 if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
612 return MMIOERR_OUTOFMEMORY;
613 wm->info.dwFlags |= MMIO_ALLOCBUF;
614 } else {
615 wm->info.pchBuffer = NULL;
618 wm->info.cchBuffer = cchBuffer;
619 wm->info.pchNext = wm->info.pchBuffer;
620 wm->info.pchEndRead = wm->info.pchBuffer;
621 wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
622 wm->info.lBufOffset = wm->info.lDiskOffset;
623 wm->bBufferLoaded = (wm->info.fccIOProc == FOURCC_MEM);
625 return MMSYSERR_NOERROR;
628 /**************************************************************************
629 * MMIO_Open [internal]
631 static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, BOOL is_unicode)
633 LPWINE_MMIO wm;
634 MMIOINFO mmioinfo;
635 DWORD pos;
637 TRACE("(%s, %p, %08lX, %s);\n", debugstr_a(szFileName), refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
639 if (!refmminfo) {
640 refmminfo = &mmioinfo;
641 memset(&mmioinfo, 0, sizeof(mmioinfo));
642 is_unicode = FALSE;
645 if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
646 char buffer[MAX_PATH];
648 if (!szFileName)
649 return (HMMIO)FALSE;
650 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
651 return (HMMIO)FALSE;
652 if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
653 return (HMMIO)FALSE;
654 strcpy(szFileName, buffer);
655 return (HMMIO)TRUE;
658 if ((wm = MMIO_Create()) == NULL)
659 return 0;
661 /* If both params are NULL, then parse the file name if available */
662 if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
663 wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
664 /* Handle any unhandled/error case. Assume DOS file */
665 if (wm->info.fccIOProc == 0) {
666 wm->info.fccIOProc = FOURCC_DOS;
667 wm->ioProc = &defaultProcs[0];
669 else if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
670 /* If not found, assume DOS file */
671 wm->ioProc = &defaultProcs[0];
673 wm->bTmpIOProc = FALSE;
675 /* if just the four character code is present, look up IO proc */
676 else if (refmminfo->pIOProc == NULL) {
677 wm->info.fccIOProc = refmminfo->fccIOProc;
678 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
679 wm->bTmpIOProc = FALSE;
681 /* if IO proc specified, use it and specified four character code */
682 else {
683 wm->info.fccIOProc = refmminfo->fccIOProc;
684 MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
685 MMIO_INSTALLPROC, is_unicode);
686 if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
687 assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
688 wm->bTmpIOProc = TRUE;
691 wm->ioProc->count++;
692 wm->info.dwFlags = dwOpenFlags;
694 if (dwOpenFlags & MMIO_ALLOCBUF) {
695 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
696 refmminfo->cchBuffer ? refmminfo->cchBuffer : MMIO_DEFAULTBUFFER, 0);
697 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
698 goto error1;
699 } else {
700 refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
701 if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
702 goto error1;
705 /* see mmioDosIOProc for that one */
706 memcpy( wm->info.adwInfo, refmminfo->adwInfo, sizeof(wm->info.adwInfo) );
708 /* call IO proc to actually open file */
709 refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
710 (LPARAM)szFileName, 0, FALSE);
712 /* update offsets and grab file size, when possible */
713 if (wm->info.fccIOProc == FOURCC_DOS && (send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE)) != -1) {
714 pos = wm->info.lBufOffset = wm->info.lDiskOffset;
715 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_END, FALSE);
716 wm->dwFileSize = wm->info.lDiskOffset;
717 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, pos, SEEK_SET, FALSE);
719 else wm->dwFileSize = 0;
721 if (refmminfo->wErrorRet == 0)
722 return wm->info.hmmio;
723 error1:
724 if (wm->info.dwFlags & MMIO_ALLOCBUF)
725 HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
726 if (wm->ioProc) wm->ioProc->count--;
727 error2:
728 MMIO_Destroy(wm);
729 return 0;
732 /**************************************************************************
733 * mmioOpenW [WINMM.@]
735 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
736 DWORD dwOpenFlags)
738 HMMIO ret;
739 LPSTR szFn = NULL;
741 if (szFileName)
743 INT len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
744 if (len < MAX_PATH) len = MAX_PATH;
745 szFn = HeapAlloc( GetProcessHeap(), 0, len );
746 if (!szFn) return NULL;
747 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
750 ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
752 HeapFree(GetProcessHeap(), 0, szFn);
753 return ret;
756 /**************************************************************************
757 * mmioOpenA [WINMM.@]
759 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
760 DWORD dwOpenFlags)
762 return MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
765 /**************************************************************************
766 * mmioClose [WINMM.@]
768 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
770 LPWINE_MMIO wm;
771 MMRESULT result;
773 TRACE("(%p, %04X);\n", hmmio, uFlags);
775 if ((wm = MMIO_Get(hmmio)) == NULL)
776 return MMSYSERR_INVALHANDLE;
778 if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
779 return result;
781 result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
783 MMIO_SetBuffer(wm, NULL, 0, 0);
785 wm->ioProc->count--;
787 if (wm->bTmpIOProc)
788 MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
789 MMIO_REMOVEPROC, wm->ioProc->is_unicode);
791 MMIO_Destroy(wm);
793 return result;
796 /**************************************************************************
797 * mmioRead [WINMM.@]
799 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
801 LPWINE_MMIO wm;
802 LONG count;
804 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
806 if ((wm = MMIO_Get(hmmio)) == NULL)
807 return -1;
809 /* unbuffered case first */
810 if (!wm->info.pchBuffer)
811 return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
813 /* first try from current buffer */
814 if (cch && wm->info.fccIOProc != FOURCC_MEM && wm->info.pchNext == wm->info.pchEndRead)
815 MMIO_GrabNextBuffer(wm, TRUE);
816 if (wm->info.pchNext != wm->info.pchEndRead) {
817 count = wm->info.pchEndRead - wm->info.pchNext;
818 if (count > cch || count < 0) count = cch;
819 memcpy(pch, wm->info.pchNext, count);
820 wm->info.pchNext += count;
821 pch += count;
822 cch -= count;
823 } else
824 count = 0;
826 if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
827 assert(wm->info.cchBuffer);
829 while (cch) {
830 LONG size;
832 size = MMIO_GrabNextBuffer(wm, TRUE);
833 if (size <= 0) break;
834 if (size > cch) size = cch;
835 memcpy(pch, wm->info.pchBuffer, size);
836 wm->info.pchNext += size;
837 pch += size;
838 cch -= size;
839 count += size;
841 wm->bBufferLoaded = FALSE;
842 mmioSeek(hmmio, 0, SEEK_CUR);
845 TRACE("count=%ld\n", count);
846 return count;
849 /**************************************************************************
850 * mmioWrite [WINMM.@]
852 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
854 LPWINE_MMIO wm;
855 LONG count;
857 TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
859 if ((wm = MMIO_Get(hmmio)) == NULL)
860 return -1;
862 if (wm->info.cchBuffer) {
863 LONG bytesW = 0;
865 while (cch) {
866 if (wm->info.pchNext != wm->info.pchEndWrite) {
867 count = wm->info.pchEndWrite - wm->info.pchNext;
868 if (count > cch || count < 0) count = cch;
869 memcpy(wm->info.pchNext, pch, count);
870 wm->info.pchNext += count;
871 pch += count;
872 cch -= count;
873 bytesW += count;
874 wm->info.dwFlags |= MMIO_DIRTY;
875 } else {
876 if (wm->info.fccIOProc == FOURCC_MEM) {
877 if (wm->info.adwInfo[0]) {
878 /* from where would we get the memory handle? */
879 FIXME("memory file expansion not implemented!\n");
880 break;
881 } else break;
885 if (wm->info.pchNext == wm->info.pchEndWrite)
887 MMIO_Flush(wm, MMIO_EMPTYBUF);
888 MMIO_GrabNextBuffer(wm, FALSE);
890 else break;
892 count = bytesW;
893 } else {
894 count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
895 wm->info.lBufOffset = wm->info.lDiskOffset;
898 TRACE("bytes written=%ld\n", count);
899 return count;
902 /**************************************************************************
903 * mmioSeek [WINMM.@]
905 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
907 LPWINE_MMIO wm;
908 LONG offset;
910 TRACE("(%p, %08lX, %d);\n", hmmio, lOffset, iOrigin);
912 if ((wm = MMIO_Get(hmmio)) == NULL)
913 return MMSYSERR_INVALHANDLE;
915 /* not buffered, direct seek on file */
916 if (!wm->info.pchBuffer && wm->info.fccIOProc != FOURCC_MEM) {
917 LRESULT ret = send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, iOrigin, FALSE);
918 if (ret != -1)
919 wm->info.lBufOffset = wm->info.lDiskOffset;
920 return ret;
923 switch (iOrigin) {
924 case SEEK_SET:
925 offset = lOffset;
926 break;
927 case SEEK_CUR:
928 offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
929 break;
930 case SEEK_END:
931 switch (wm->info.fccIOProc) {
932 case FOURCC_MEM:
933 offset = wm->info.cchBuffer - lOffset;
934 break;
935 case FOURCC_DOS:
936 offset = wm->dwFileSize - lOffset;
937 break;
938 default:
939 offset = send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, SEEK_END, FALSE);
940 break;
942 break;
943 default:
944 return -1;
947 /* stay in same buffer ? */
948 /* some memory mapped buffers are defined with -1 as a size */
949 if ((wm->info.cchBuffer > 0) &&
950 ((offset < wm->info.lBufOffset) ||
951 (offset > wm->info.lBufOffset + wm->info.cchBuffer) ||
952 (offset > wm->dwFileSize && wm->info.fccIOProc == FOURCC_DOS) ||
953 !wm->bBufferLoaded)) {
955 /* condition to change buffer */
956 if ((wm->info.fccIOProc == FOURCC_MEM) ||
957 MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
958 /* this also sets the wm->info.lDiskOffset field */
959 send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
960 offset, SEEK_SET, FALSE) == -1)
961 return -1;
962 wm->info.lBufOffset = offset;
963 wm->bBufferLoaded = FALSE;
964 wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
966 else
967 wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);
969 TRACE("=> %ld\n", offset);
970 return offset;
973 /**************************************************************************
974 * mmioGetInfo [WINMM.@]
976 MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
978 LPWINE_MMIO wm;
980 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
982 if ((wm = MMIO_Get(hmmio)) == NULL)
983 return MMSYSERR_INVALHANDLE;
985 *lpmmioinfo = wm->info;
987 return MMSYSERR_NOERROR;
990 /**************************************************************************
991 * mmioSetInfo [WINMM.@]
993 MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
995 LPWINE_MMIO wm;
997 TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
999 if ((wm = MMIO_Get(hmmio)) == NULL)
1000 return MMSYSERR_INVALHANDLE;
1002 /* check pointers coherence */
1003 if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
1004 lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
1005 lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
1006 lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
1007 lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
1008 lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
1009 return MMSYSERR_INVALPARAM;
1011 wm->info = *lpmmioinfo;
1012 return MMSYSERR_NOERROR;
1015 /**************************************************************************
1016 * mmioSetBuffer [WINMM.@]
1018 MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
1020 LPWINE_MMIO wm;
1022 TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
1023 hmmio, pchBuffer, cchBuffer, uFlags);
1025 if ((wm = MMIO_Get(hmmio)) == NULL)
1026 return MMSYSERR_INVALHANDLE;
1028 return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
1031 /**************************************************************************
1032 * mmioFlush [WINMM.@]
1034 MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
1036 LPWINE_MMIO wm;
1038 TRACE("(%p, %04X)\n", hmmio, uFlags);
1040 if ((wm = MMIO_Get(hmmio)) == NULL)
1041 return MMSYSERR_INVALHANDLE;
1043 return MMIO_Flush(wm, uFlags);
1046 /**************************************************************************
1047 * mmioAdvance [WINMM.@]
1049 MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
1051 LPWINE_MMIO wm;
1053 TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1055 /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
1056 * here. be sure if you change something here to check mmioAdvance16 as well
1058 if ((wm = MMIO_Get(hmmio)) == NULL)
1059 return MMSYSERR_INVALHANDLE;
1061 if (!wm->info.cchBuffer)
1062 return MMIOERR_UNBUFFERED;
1064 if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
1065 return MMSYSERR_INVALPARAM;
1067 if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1068 return MMIOERR_CANNOTWRITE;
1069 if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
1071 send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
1072 send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
1073 lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1074 lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
1077 if (lpmmioinfo && lpmmioinfo->fccIOProc == FOURCC_DOS) {
1078 wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset +
1079 (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1081 MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);
1083 if (lpmmioinfo) {
1084 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1085 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
1086 (wm->info.pchEndRead - wm->info.pchBuffer);
1087 lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1088 (wm->info.pchEndWrite - wm->info.pchBuffer);
1089 lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
1090 lpmmioinfo->lBufOffset = wm->info.lBufOffset;
1092 return MMSYSERR_NOERROR;
1095 /**************************************************************************
1096 * mmioStringToFOURCCA [WINMM.@]
1098 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
1100 CHAR cc[4];
1101 int i = 0;
1103 for (i = 0; i < 4 && sz[i]; i++) {
1104 if (uFlags & MMIO_TOUPPER) {
1105 cc[i] = RtlUpperChar(sz[i]);
1106 } else {
1107 cc[i] = sz[i];
1111 /* Pad with spaces */
1112 while (i < 4) cc[i++] = ' ';
1114 TRACE("Got %s\n",debugstr_an(cc,4));
1115 return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
1118 /**************************************************************************
1119 * mmioStringToFOURCCW [WINMM.@]
1121 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
1123 char szA[4];
1125 WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
1126 return mmioStringToFOURCCA(szA,uFlags);
1129 /**************************************************************************
1130 * mmioInstallIOProcA [WINMM.@]
1132 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1133 LPMMIOPROC pIOProc, DWORD dwFlags)
1135 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, FALSE);
1138 /**************************************************************************
1139 * mmioInstallIOProcW [WINMM.@]
1141 LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1142 LPMMIOPROC pIOProc, DWORD dwFlags)
1144 return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, TRUE);
1147 /******************************************************************
1148 * MMIO_SendMessage
1152 static LRESULT MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1153 LPARAM lParam2, BOOL is_unicode)
1155 LPWINE_MMIO wm;
1157 TRACE("(%p, %u, %Id, %Id, %s)\n", hmmio, uMessage, lParam1, lParam2, is_unicode ? "unicode" : "ansi");
1159 if (uMessage < MMIOM_USER)
1160 return MMSYSERR_INVALPARAM;
1162 if ((wm = MMIO_Get(hmmio)) == NULL)
1163 return MMSYSERR_INVALHANDLE;
1165 return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, is_unicode);
1168 /**************************************************************************
1169 * mmioSendMessage [WINMM.@]
1171 LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
1172 LPARAM lParam1, LPARAM lParam2)
1174 return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, FALSE);
1177 /**************************************************************************
1178 * mmioDescend [WINMM.@]
1180 MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
1181 const MMCKINFO* lpckParent, UINT uFlags)
1183 DWORD dwOldPos;
1184 FOURCC srchCkId;
1185 FOURCC srchType;
1187 TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1189 if (lpck == NULL)
1190 return MMSYSERR_INVALPARAM;
1192 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1193 TRACE("dwOldPos=%ld\n", dwOldPos);
1195 if (lpckParent != NULL) {
1196 TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
1197 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1198 if (dwOldPos < lpckParent->dwDataOffset ||
1199 dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
1200 WARN("outside parent chunk\n");
1201 return MMIOERR_CHUNKNOTFOUND;
1205 /* The SDK docu says 'ckid' is used for all cases. Real World
1206 * examples disagree -Marcus,990216.
1209 srchCkId = 0;
1210 srchType = 0;
1212 /* find_chunk looks for 'ckid' */
1213 if (uFlags & MMIO_FINDCHUNK)
1214 srchCkId = lpck->ckid;
1216 /* find_riff and find_list look for 'fccType' */
1217 if (uFlags & MMIO_FINDLIST)
1219 srchCkId = FOURCC_LIST;
1220 srchType = lpck->fccType;
1223 if (uFlags & MMIO_FINDRIFF)
1225 srchCkId = FOURCC_RIFF;
1226 srchType = lpck->fccType;
1229 TRACE("searching for %s.%s\n",
1230 debugstr_an((LPCSTR)&srchCkId, 4), srchType ? debugstr_an((LPCSTR)&srchType, 4) : "<any>");
1232 while (TRUE)
1234 LONG ix;
1236 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
1237 if (ix < 0 || ix < 2*sizeof(DWORD))
1239 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1240 WARN("return ChunkNotFound\n");
1241 return MMIOERR_CHUNKNOTFOUND;
1244 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1245 TRACE("ckid=%s fcc=%s cksize=%08lX !\n",
1246 debugstr_an((LPCSTR)&lpck->ckid, 4),
1247 srchType ? debugstr_an((LPCSTR)&lpck->fccType, 4) : "<na>",
1248 lpck->cksize);
1249 if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
1250 (!srchType || (srchType == lpck->fccType)) )
1251 break;
1253 dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
1254 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1257 lpck->dwFlags = 0;
1258 /* If we were looking for RIFF/LIST chunks, the final file position
1259 * is after the chunkid. If we were just looking for the chunk
1260 * it is after the cksize. So add 4 in RIFF/LIST case.
1262 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1263 mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
1264 else
1266 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1267 lpck->fccType = 0;
1269 TRACE("lpck: ckid=%s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%s)!\n",
1270 debugstr_an((LPSTR)&lpck->ckid, 4), lpck->cksize, lpck->dwDataOffset,
1271 lpck->fccType, srchType ? debugstr_an((LPSTR)&lpck->fccType, 4):"");
1272 return MMSYSERR_NOERROR;
1275 /**************************************************************************
1276 * mmioAscend [WINMM.@]
1278 MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1280 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1282 if (lpck->dwFlags & MMIO_DIRTY) {
1283 DWORD dwOldPos, dwNewSize;
1285 TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1286 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1287 TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1288 dwNewSize = dwOldPos - lpck->dwDataOffset;
1289 if (dwNewSize != lpck->cksize) {
1290 TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1291 lpck->cksize = dwNewSize;
1293 /* pad odd size with 0 */
1294 if (dwNewSize & 1) {
1295 char ch = 0;
1296 mmioWrite(hmmio, &ch, 1);
1298 mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1299 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1301 lpck->dwFlags = 0;
1304 mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1306 return MMSYSERR_NOERROR;
1309 /**************************************************************************
1310 * mmioCreateChunk [WINMM.@]
1312 MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
1314 DWORD dwOldPos;
1315 LONG size;
1316 LONG ix;
1318 TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1320 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1321 TRACE("dwOldPos=%ld\n", dwOldPos);
1323 if (uFlags == MMIO_CREATELIST)
1324 lpck->ckid = FOURCC_LIST;
1325 else if (uFlags == MMIO_CREATERIFF)
1326 lpck->ckid = FOURCC_RIFF;
1328 TRACE("ckid=%s\n", debugstr_an((LPSTR)&lpck->ckid, 4));
1330 size = 2 * sizeof(DWORD);
1331 lpck->dwDataOffset = dwOldPos + size;
1333 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1334 size += sizeof(DWORD);
1335 lpck->dwFlags = MMIO_DIRTY;
1337 ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1338 TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n", ix, size, errno);
1339 if (ix != size) {
1340 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1341 WARN("return CannotWrite\n");
1342 return MMIOERR_CANNOTWRITE;
1345 return MMSYSERR_NOERROR;
1348 /**************************************************************************
1349 * mmioRenameA [WINMM.@]
1351 MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1352 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1354 struct IOProcList* ioProc = NULL;
1355 struct IOProcList tmp;
1356 FOURCC fcc;
1358 TRACE("(%s, %s, %p, %08lX);\n",
1359 debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1361 /* If both params are NULL, then parse the file name */
1362 if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1364 fcc = MMIO_ParseExtA(szFileName);
1365 if (fcc) ioProc = MMIO_FindProcNode(fcc);
1368 /* Handle any unhandled/error case from above. Assume DOS file */
1369 if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1370 ioProc = &defaultProcs[0];
1371 /* if just the four character code is present, look up IO proc */
1372 else if (lpmmioinfo->pIOProc == NULL)
1373 ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1374 else /* use relevant ioProc */
1376 ioProc = &tmp;
1377 tmp.fourCC = lpmmioinfo->fccIOProc;
1378 tmp.pIOProc = lpmmioinfo->pIOProc;
1379 tmp.is_unicode = FALSE;
1380 tmp.count = 1;
1383 /* FIXME: should we actually pass lpmmioinfo down the drain ???
1384 * or make a copy of it because it's const ???
1386 return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1387 (LPARAM)szFileName, (LPARAM)szNewFileName, FALSE);
1390 /**************************************************************************
1391 * mmioRenameW [WINMM.@]
1393 MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1394 const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1396 LPSTR szFn = NULL;
1397 LPSTR sznFn = NULL;
1398 UINT ret = MMSYSERR_NOMEM;
1399 INT len;
1401 if (szFileName)
1403 len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
1404 szFn = HeapAlloc( GetProcessHeap(), 0, len );
1405 if (!szFn) goto done;
1406 WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
1408 if (szNewFileName)
1410 len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
1411 sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1412 if (!sznFn) goto done;
1413 WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
1416 ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1418 done:
1419 HeapFree(GetProcessHeap(),0,szFn);
1420 HeapFree(GetProcessHeap(),0,sznFn);
1421 return ret;