Release 990613.
[wine.git] / multimedia / mmio.c
blob8c8e2e59345a1f367c901c24027ed6c4c55e7f52
1 /*
2 * MMIO functions
4 * Copyright 1998 Andrew Taylor
5 * Copyright 1998 Ove Kåven
7 */
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "windef.h"
14 #include "wine/winbase16.h"
15 #include "heap.h"
16 #include "file.h"
17 #include "mmsystem.h"
18 #include "debug.h"
19 #include "xmalloc.h"
21 DEFAULT_DEBUG_CHANNEL(mmio)
23 /**************************************************************************
24 * mmioDosIOProc [internal]
26 static LRESULT mmioDosIOProc(LPMMIOINFO16 lpmmioinfo, UINT16 uMessage, LPARAM lParam1, LPARAM lParam2) {
27 TRACE(mmio, "(%p, %X, %ld, %ld);\n", lpmmioinfo, uMessage, lParam1, lParam2);
29 switch (uMessage) {
31 case MMIOM_OPEN: {
32 /* Parameters:
33 * lParam1 = szFileName parameter from mmioOpen
34 * lParam2 = reserved (we use it for 16-bitness)
35 * Returns: zero on success, error code on error
36 * NOTE: lDiskOffset automatically set to zero
39 OFSTRUCT ofs;
40 LPSTR szFileName = (LPSTR) lParam1;
42 if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
43 FIXME(mmio, "MMIO_GETTEMP not implemented\n");
44 return MMIOERR_CANNOTOPEN;
47 /* if filename NULL, assume open file handle in adwInfo[0] */
48 if (!szFileName) {
49 if (lParam2) lpmmioinfo->adwInfo[0] =
50 FILE_GetHandle(lpmmioinfo->adwInfo[0]);
51 return 0;
54 lpmmioinfo->adwInfo[0] =
55 (DWORD) OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags);
56 if (lpmmioinfo->adwInfo[0] == -1)
57 return MMIOERR_CANNOTOPEN;
59 return 0;
62 case MMIOM_CLOSE: {
63 /* Parameters:
64 * lParam1 = wFlags parameter from mmioClose
65 * lParam2 = unused
66 * Returns: zero on success, error code on error
69 UINT16 uFlags = (UINT16) lParam1;
71 if (uFlags & MMIO_FHOPEN)
72 return 0;
74 _lclose((HFILE)lpmmioinfo->adwInfo[0]);
75 return 0;
79 case MMIOM_READ: {
80 /* Parameters:
81 * lParam1 = huge pointer to read buffer
82 * lParam2 = number of bytes to read
83 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
84 * in wErrorRet)
85 * NOTE: lDiskOffset should be updated
88 HPSTR pch = (HPSTR) lParam1;
89 LONG cch = (LONG) lParam2;
90 LONG count;
92 count = _lread((HFILE)lpmmioinfo->adwInfo[0], pch, cch);
93 if (count != -1)
94 lpmmioinfo->lDiskOffset += count;
96 return count;
99 case MMIOM_WRITE:
100 case MMIOM_WRITEFLUSH: {
101 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
103 /* Parameters:
104 * lParam1 = huge pointer to write buffer
105 * lParam2 = number of bytes to write
106 * Returns: number of bytes written, -1 for error (error code in
107 * wErrorRet)
108 * NOTE: lDiskOffset should be updated
111 HPSTR pch = (HPSTR) lParam1;
112 LONG cch = (LONG) lParam2;
113 LONG count;
115 count = _hwrite((HFILE)lpmmioinfo->adwInfo[0], pch, cch);
116 if (count != -1)
117 lpmmioinfo->lDiskOffset += count;
119 return count;
122 case MMIOM_SEEK: {
123 /* Parameters:
124 * lParam1 = new position
125 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
126 * Returns: new file postion, -1 on error
127 * NOTE: lDiskOffset should be updated
130 LONG Offset = (LONG) lParam1;
131 LONG Whence = (LONG) lParam2;
132 LONG pos;
134 pos = _llseek((HFILE)lpmmioinfo->adwInfo[0], Offset, Whence);
135 if (pos != -1)
136 lpmmioinfo->lDiskOffset = pos;
138 return pos;
141 case MMIOM_RENAME: {
142 /* Parameters:
143 * lParam1 = old name
144 * lParam2 = new name
145 * Returns: zero on success, non-zero on failure
148 FIXME(mmio, "MMIOM_RENAME unimplemented\n");
149 return MMIOERR_FILENOTFOUND;
152 default:
153 FIXME(mmio, "unexpected message %u\n", uMessage);
154 return 0;
157 return 0;
160 /**************************************************************************
161 * mmioMemIOProc [internal]
163 static LRESULT mmioMemIOProc(LPMMIOINFO16 lpmmioinfo, UINT16 uMessage, LPARAM lParam1, LPARAM lParam2) {
164 TRACE(mmio,"(%p,0x%04x,0x%08lx,0x%08lx)\n",lpmmioinfo,uMessage,lParam1,lParam2);
165 switch (uMessage) {
167 case MMIOM_OPEN: {
168 /* Parameters:
169 * lParam1 = filename (must be NULL)
170 * lParam2 = reserved (we use it for 16-bitness)
171 * Returns: zero on success, error code on error
172 * NOTE: lDiskOffset automatically set to zero
175 if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
176 lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
178 return 0;
181 case MMIOM_CLOSE: {
182 /* Parameters:
183 * lParam1 = wFlags parameter from mmioClose
184 * lParam2 = unused
185 * Returns: zero on success, error code on error
188 return 0;
192 case MMIOM_READ: {
193 /* Parameters:
194 * lParam1 = huge pointer to read buffer
195 * lParam2 = number of bytes to read
196 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
197 * in wErrorRet)
198 * NOTE: lDiskOffset should be updated
201 /* HPSTR pch = (HPSTR) lParam1; */
202 /* LONG cch = (LONG) lParam2; */
204 FIXME(mmio,"MMIOM_READ on memory files should not occur, buffer may be lost!\n");
205 return 0;
208 case MMIOM_WRITE:
209 case MMIOM_WRITEFLUSH: {
210 /* no internal buffering, so WRITEFLUSH handled same as WRITE */
212 /* Parameters:
213 * lParam1 = huge pointer to write buffer
214 * lParam2 = number of bytes to write
215 * Returns: number of bytes written, -1 for error (error code in
216 * wErrorRet)
217 * NOTE: lDiskOffset should be updated
220 /* HPSTR pch = (HPSTR) lParam1; */
221 /* LONG cch = (LONG) lParam2; */
223 FIXME(mmio,"MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
224 return 0;
227 case MMIOM_SEEK: {
228 /* Parameters:
229 * lParam1 = new position
230 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
231 * Returns: new file postion, -1 on error
232 * NOTE: lDiskOffset should be updated
235 /* LONG Offset = (LONG) lParam1; */
236 /* LONG Whence = (LONG) lParam2; */
238 FIXME(mmio,"MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
239 return -1;
242 default:
243 FIXME(mmio, "unexpected message %u\n", uMessage);
244 return 0;
247 return 0;
250 /**************************************************************************
251 * MMIO_Open [internal]
253 static HMMIO16 MMIO_Open(LPSTR szFileName, MMIOINFO16 * lpmmioinfo,
254 DWORD dwOpenFlags, int use16)
256 LPMMIOINFO16 lpmminfo;
257 HMMIO16 hmmio;
258 UINT16 result;
260 TRACE(mmio, "('%s', %p, %08lX);\n", szFileName, lpmmioinfo, dwOpenFlags);
262 if (dwOpenFlags & MMIO_PARSE) {
263 char buffer[MAX_PATH];
265 if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
266 return (HMMIO16)FALSE;
267 strcpy(szFileName, buffer);
268 return (HMMIO16)TRUE;
271 hmmio = GlobalAlloc16(GHND, sizeof(MMIOINFO16));
272 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
273 if (lpmminfo == NULL)
274 return 0;
275 memset(lpmminfo, 0, sizeof(MMIOINFO16));
277 /* assume DOS file if not otherwise specified */
278 if (!lpmmioinfo ||
279 (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)) {
281 lpmminfo->fccIOProc = FOURCC_DOS;
282 lpmminfo->pIOProc = (LPMMIOPROC16) mmioDosIOProc;
284 /* if just the four character code is present, look up IO proc */
285 else if (lpmmioinfo->pIOProc == NULL) {
287 lpmminfo->fccIOProc = lpmmioinfo->fccIOProc;
288 lpmminfo->pIOProc = mmioInstallIOProc16(lpmmioinfo->fccIOProc, NULL, MMIO_FINDPROC);
291 /* if IO proc specified, use it and specified four character code */
292 else {
294 lpmminfo->fccIOProc = lpmmioinfo->fccIOProc;
295 lpmminfo->pIOProc = lpmmioinfo->pIOProc;
298 if (dwOpenFlags & MMIO_ALLOCBUF) {
299 if ((result = mmioSetBuffer16(hmmio, NULL, MMIO_DEFAULTBUFFER, 0))) {
300 if (lpmmioinfo)
301 lpmmioinfo->wErrorRet = result;
302 return 0;
304 } else
305 if (lpmminfo->fccIOProc == FOURCC_MEM) {
306 if ((result = mmioSetBuffer16(hmmio, lpmmioinfo->pchBuffer, lpmmioinfo->cchBuffer, 0))) {
307 if (lpmmioinfo)
308 lpmmioinfo->wErrorRet = result;
309 return 0;
313 lpmminfo->dwFlags = dwOpenFlags;
314 lpmminfo->hmmio = hmmio;
316 /* call IO proc to actually open file */
317 result = (UINT16) mmioSendMessage(hmmio, MMIOM_OPEN, (LPARAM) szFileName, (LPARAM) use16);
319 GlobalUnlock16(hmmio);
321 if (result != 0) {
322 GlobalFree16(hmmio);
323 return 0;
326 return hmmio;
329 /**************************************************************************
330 * mmioOpenW [WINMM.123]
332 HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO * lpmmioinfo,
333 DWORD dwOpenFlags)
335 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(),0,szFileName);
336 HMMIO ret = MMIO_Open(szFn,(LPMMIOINFO16)lpmmioinfo,dwOpenFlags,FALSE);
338 HeapFree(GetProcessHeap(),0,szFn);
339 return ret;
342 /**************************************************************************
343 * mmioOpenA [WINMM.122]
345 HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO * lpmmioinfo,
346 DWORD dwOpenFlags)
348 return MMIO_Open(szFileName,(LPMMIOINFO16)lpmmioinfo,dwOpenFlags,FALSE);
351 /**************************************************************************
352 * mmioOpen [MMSYSTEM.1210]
354 HMMIO16 WINAPI mmioOpen16(LPSTR szFileName, MMIOINFO16 * lpmmioinfo,
355 DWORD dwOpenFlags)
357 return MMIO_Open(szFileName,(LPMMIOINFO16)lpmmioinfo,dwOpenFlags,TRUE);
361 /**************************************************************************
362 * mmioClose [WINMM.114]
364 MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
366 LPMMIOINFO16 lpmminfo;
367 MMRESULT result;
369 TRACE(mmio, "(%04X, %04X);\n", hmmio, uFlags);
371 lpmminfo = (LPMMIOINFO16) GlobalLock16(hmmio);
372 if (lpmminfo == NULL)
373 return 0;
375 /* flush the file - if error reported, ignore */
376 if (mmioFlush(hmmio, MMIO_EMPTYBUF) != 0)
377 lpmminfo->dwFlags &= ~MMIO_DIRTY;
379 result = mmioSendMessage(hmmio,MMIOM_CLOSE,(LPARAM)uFlags,(LPARAM)0);
381 mmioSetBuffer16(hmmio, NULL, 0, 0);
383 GlobalUnlock16(hmmio);
384 GlobalFree16(hmmio);
386 return result;
390 /**************************************************************************
391 * mmioClose [MMSYSTEM.1211]
393 MMRESULT16 WINAPI mmioClose16(HMMIO16 hmmio, UINT16 uFlags)
395 return mmioClose(hmmio,uFlags);
400 /**************************************************************************
401 * mmioRead [WINMM.124]
403 LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
405 LONG count;
406 LPMMIOINFO16 lpmminfo;
408 TRACE(mmio, "(%04X, %p, %ld);\n", hmmio, pch, cch);
410 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
411 if (lpmminfo == NULL)
412 return -1;
414 if (lpmminfo->pchNext != lpmminfo->pchEndRead) {
415 count = lpmminfo->pchEndRead - lpmminfo->pchNext;
416 if (count > cch || count < 0) count = cch;
417 memcpy(pch, lpmminfo->pchNext, count);
418 lpmminfo->pchNext += count;
419 pch += count;
420 cch -= count;
421 } else
422 count = 0;
424 if (cch&&(lpmminfo->fccIOProc!=FOURCC_MEM)) {
425 if (lpmminfo->cchBuffer) {
426 mmioFlush(hmmio, MMIO_EMPTYBUF);
428 while (cch) {
429 LONG size;
430 lpmminfo->lBufOffset = lpmminfo->lDiskOffset;
431 lpmminfo->pchNext = lpmminfo->pchBuffer;
432 lpmminfo->pchEndRead = lpmminfo->pchBuffer;
433 size = mmioSendMessage(hmmio, MMIOM_READ,
434 (LPARAM) lpmminfo->pchBuffer,
435 (LPARAM) lpmminfo->cchBuffer);
436 if (size<=0) break;
437 lpmminfo->pchEndRead = lpmminfo->pchBuffer + size;
438 if (size > cch) size = cch;
439 memcpy(pch, lpmminfo->pchNext, size);
440 lpmminfo->pchNext += size;
441 pch += size;
442 cch -= size;
443 count += size;
445 } else {
446 count += mmioSendMessage(hmmio, MMIOM_READ, (LPARAM) pch, (LPARAM) cch);
447 if (count>0) lpmminfo->lBufOffset += count;
451 GlobalUnlock16(hmmio);
452 TRACE(mmio, "count=%ld\n", count);
453 return count;
456 /**************************************************************************
457 * mmioRead [MMSYSTEM.1212]
459 LONG WINAPI mmioRead16(HMMIO16 hmmio, HPSTR pch, LONG cch)
461 return mmioRead(hmmio,pch,cch);
464 /**************************************************************************
465 * mmioWrite [WINMM.133]
467 LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
469 LONG count;
470 LPMMIOINFO16 lpmminfo;
472 TRACE(mmio, "(%04X, %p, %ld);\n", hmmio, pch, cch);
474 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
475 if (lpmminfo == NULL)
476 return -1;
478 if (lpmminfo->cchBuffer) {
479 count = 0;
480 while (cch) {
481 if (lpmminfo->pchNext != lpmminfo->pchEndWrite) {
482 count = lpmminfo->pchEndWrite - lpmminfo->pchNext;
483 if (count > cch || count < 0) count = cch;
484 memcpy(lpmminfo->pchNext, pch, count);
485 lpmminfo->pchNext += count;
486 pch += count;
487 cch -= count;
488 lpmminfo->dwFlags |= MMIO_DIRTY;
489 } else
490 if (lpmminfo->fccIOProc==FOURCC_MEM) {
491 if (lpmminfo->adwInfo[0]) {
492 /* from where would we get the memory handle? */
493 FIXME(mmio, "memory file expansion not implemented!\n");
494 } else break;
497 if (lpmminfo->pchNext == lpmminfo->pchEndWrite
498 && mmioFlush(hmmio, MMIO_EMPTYBUF)) break;
500 } else {
501 count = mmioSendMessage(hmmio, MMIOM_WRITE, (LPARAM) pch, (LPARAM) cch);
502 lpmminfo->lBufOffset = lpmminfo->lDiskOffset;
505 GlobalUnlock16(hmmio);
506 TRACE(mmio, "count=%ld\n", count);
507 return count;
510 /**************************************************************************
511 * mmioWrite [MMSYSTEM.1213]
513 LONG WINAPI mmioWrite16(HMMIO16 hmmio, HPCSTR pch, LONG cch)
515 return mmioWrite(hmmio,pch,cch);
518 /**************************************************************************
519 * mmioSeek [MMSYSTEM.1214]
521 LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
523 int offset;
524 LPMMIOINFO16 lpmminfo;
526 TRACE(mmio, "(%04X, %08lX, %d);\n", hmmio, lOffset, iOrigin);
528 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
529 if (lpmminfo == NULL)
530 return -1;
532 offset = (iOrigin==SEEK_SET)?(lOffset - lpmminfo->lBufOffset):
533 (iOrigin==SEEK_CUR)?(lOffset +
534 (lpmminfo->pchNext - lpmminfo->pchBuffer)):-1;
536 if ((lpmminfo->cchBuffer<0)||
537 ((offset>=0)&&(offset<=(lpmminfo->pchEndRead-lpmminfo->pchBuffer)))) {
538 lpmminfo->pchNext = lpmminfo->pchBuffer + offset;
539 GlobalUnlock16(hmmio);
540 return lpmminfo->lBufOffset + offset;
543 if ((lpmminfo->fccIOProc==FOURCC_MEM)||mmioFlush(hmmio, MMIO_EMPTYBUF)) {
544 GlobalUnlock16(hmmio);
545 return -1;
548 offset = mmioSendMessage(hmmio, MMIOM_SEEK, (LPARAM) lOffset, (LPARAM) iOrigin);
549 lpmminfo->lBufOffset = lpmminfo->lDiskOffset;
551 GlobalUnlock16(hmmio);
552 return offset;
555 /**************************************************************************
556 * mmioSeek [MMSYSTEM.1214]
558 LONG WINAPI mmioSeek16(HMMIO16 hmmio, LONG lOffset, INT16 iOrigin)
560 return mmioSeek(hmmio,lOffset,iOrigin);
563 /**************************************************************************
564 * mmioGetInfo [MMSYSTEM.1215]
566 UINT16 WINAPI mmioGetInfo16(HMMIO16 hmmio, MMIOINFO16 * lpmmioinfo, UINT16 uFlags)
568 LPMMIOINFO16 lpmminfo;
569 TRACE(mmio, "mmioGetInfo\n");
570 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
571 if (lpmminfo == NULL) return 0;
572 memcpy(lpmmioinfo, lpmminfo, sizeof(MMIOINFO16));
573 GlobalUnlock16(hmmio);
574 return 0;
577 /**************************************************************************
578 * mmioGetInfo [WINMM.118]
580 UINT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO*lpmmioinfo, UINT uFlags)
582 MMIOINFO16 mmioinfo;
583 LPMMIOINFO16 lpmminfo=&mmioinfo;
584 UINT16 ret;
586 TRACE(mmio, "(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
587 ret = mmioGetInfo16(hmmio,&mmioinfo,uFlags);
588 if (!ret)
589 return 0;
590 lpmmioinfo->dwFlags = lpmminfo->dwFlags;
591 lpmmioinfo->fccIOProc = lpmminfo->fccIOProc;
592 lpmmioinfo->pIOProc = (LPMMIOPROC)lpmminfo->pIOProc;
593 lpmmioinfo->wErrorRet = lpmminfo->wErrorRet;
594 lpmmioinfo->htask = lpmminfo->htask;
595 lpmmioinfo->cchBuffer = lpmminfo->cchBuffer;
596 lpmmioinfo->pchBuffer = lpmminfo->pchBuffer;
597 lpmmioinfo->pchNext = lpmminfo->pchNext;
598 lpmmioinfo->pchEndRead = lpmminfo->pchEndRead;
599 lpmmioinfo->pchEndWrite = lpmminfo->pchEndWrite;
600 lpmmioinfo->lBufOffset = lpmminfo->lBufOffset;
601 lpmmioinfo->lDiskOffset = lpmminfo->lDiskOffset;
602 memcpy(lpmmioinfo->adwInfo,lpmminfo->adwInfo,sizeof(lpmminfo->adwInfo));
603 lpmmioinfo->dwReserved1 = lpmminfo->dwReserved1;
604 lpmmioinfo->dwReserved2 = lpmminfo->dwReserved2;
605 lpmmioinfo->hmmio = lpmminfo->hmmio;
606 return 0;
609 /**************************************************************************
610 * mmioSetInfo [MMSYSTEM.1216]
612 UINT16 WINAPI mmioSetInfo16(HMMIO16 hmmio, const MMIOINFO16 * lpmmioinfo, UINT16 uFlags)
614 LPMMIOINFO16 lpmminfo;
615 TRACE(mmio, "mmioSetInfo\n");
616 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
617 if (lpmminfo == NULL) return 0;
618 lpmminfo->pchNext = lpmmioinfo->pchNext;
619 lpmminfo->pchEndRead = lpmmioinfo->pchEndRead;
620 GlobalUnlock16(hmmio);
621 return 0;
624 /**************************************************************************
625 * mmioSetInfo [WINMM.130]
627 UINT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO * lpmmioinfo, UINT uFlags)
629 LPMMIOINFO16 lpmminfo;
630 TRACE(mmio, "mmioSetInfo\n");
631 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
632 if (lpmminfo == NULL) return 0;
633 lpmminfo->pchNext = lpmmioinfo->pchNext;
634 lpmminfo->pchEndRead = lpmmioinfo->pchEndRead;
635 GlobalUnlock16(hmmio);
636 return 0;
639 /**************************************************************************
640 * mmioSetBuffer [WINMM.129]
642 UINT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer,
643 LONG cchBuffer, UINT uFlags)
645 LPMMIOINFO16 lpmminfo;
647 if (mmioFlush(hmmio, MMIO_EMPTYBUF) != 0)
648 return MMIOERR_CANNOTWRITE;
650 TRACE(mmio, "(hmmio=%04x, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
651 hmmio, pchBuffer, cchBuffer, uFlags);
653 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
654 if (lpmminfo == NULL) return 0;
655 if ((!cchBuffer || pchBuffer) && lpmminfo->dwFlags&MMIO_ALLOCBUF) {
656 GlobalUnlock16(lpmminfo->dwReserved1);
657 GlobalFree16(lpmminfo->dwReserved1);
658 lpmminfo->dwFlags &= ~MMIO_ALLOCBUF;
660 if (pchBuffer) {
661 lpmminfo->pchBuffer = pchBuffer;
662 } else if (lpmminfo->dwFlags&MMIO_ALLOCBUF) {
663 HGLOBAL16 hNewBuf;
664 GlobalUnlock16(lpmminfo->dwReserved1);
665 hNewBuf = GlobalReAlloc16(lpmminfo->dwReserved1, cchBuffer, 0);
666 if (!hNewBuf) {
667 /* FIXME: this assumes the memory block didn't move */
668 GlobalLock16(lpmminfo->dwReserved1);
669 GlobalUnlock16(hmmio);
670 return MMIOERR_OUTOFMEMORY;
672 lpmminfo->dwReserved1 = hNewBuf;
673 lpmminfo->pchBuffer = GlobalLock16(hNewBuf);
674 } else if (cchBuffer) {
675 HGLOBAL16 hNewBuf = GlobalAlloc16(GMEM_MOVEABLE, cchBuffer);
676 if (!hNewBuf) {
677 GlobalUnlock16(hmmio);
678 return MMIOERR_OUTOFMEMORY;
680 lpmminfo->dwReserved1 = hNewBuf;
681 lpmminfo->pchBuffer = GlobalLock16(hNewBuf);
682 lpmminfo->dwFlags |= MMIO_ALLOCBUF;
683 } else
684 lpmminfo->pchBuffer = NULL;
685 lpmminfo->cchBuffer = cchBuffer;
686 lpmminfo->pchNext = lpmminfo->pchBuffer;
687 lpmminfo->pchEndRead = lpmminfo->pchBuffer;
688 lpmminfo->pchEndWrite = lpmminfo->pchBuffer + cchBuffer;
689 lpmminfo->lBufOffset = 0;
691 GlobalUnlock16(hmmio);
692 return (UINT16) 0;
695 /**************************************************************************
696 * mmioSetBuffer [MMSYSTEM.1217]
698 UINT16 WINAPI mmioSetBuffer16(HMMIO16 hmmio, LPSTR pchBuffer,
699 LONG cchBuffer, UINT16 uFlags)
701 return mmioSetBuffer(hmmio, pchBuffer, cchBuffer, uFlags);
704 /**************************************************************************
705 * mmioFlush [WINMM.117]
707 UINT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
709 LPMMIOINFO16 lpmminfo;
710 TRACE(mmio, "(%04X, %04X)\n", hmmio, uFlags);
711 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
712 if (lpmminfo == NULL) return 0;
714 if ((!lpmminfo->cchBuffer)||(lpmminfo->fccIOProc==FOURCC_MEM)) {
715 GlobalUnlock16(hmmio);
716 return 0;
718 /* not quite sure what to do here, but I'll guess */
719 if (lpmminfo->dwFlags & MMIO_DIRTY) {
720 mmioSendMessage(hmmio, MMIOM_SEEK,
721 (LPARAM) lpmminfo->lBufOffset,
722 (LPARAM) SEEK_SET);
723 mmioSendMessage(hmmio, MMIOM_WRITE,
724 (LPARAM) lpmminfo->pchBuffer,
725 (LPARAM) (lpmminfo->pchNext - lpmminfo->pchBuffer) );
726 lpmminfo->dwFlags &= ~MMIO_DIRTY;
728 if (uFlags & MMIO_EMPTYBUF) {
729 /* seems Windows doesn't do any seeking here, hopefully this
730 won't matter, otherwise a slight rewrite is necessary */
731 mmioSendMessage(hmmio, MMIOM_SEEK,
732 (LPARAM) (lpmminfo->lBufOffset +
733 (lpmminfo->pchNext - lpmminfo->pchBuffer)),
734 (LPARAM) SEEK_SET);
735 lpmminfo->pchNext = lpmminfo->pchBuffer;
736 lpmminfo->pchEndRead = lpmminfo->pchBuffer;
737 lpmminfo->lBufOffset = lpmminfo->lDiskOffset;
740 GlobalUnlock16(hmmio);
741 return 0;
744 /**************************************************************************
745 * mmioFlush [MMSYSTEM.1218]
747 UINT16 WINAPI mmioFlush16(HMMIO16 hmmio, UINT16 uFlags)
749 return mmioFlush(hmmio,uFlags);
752 /**************************************************************************
753 * mmioAdvance [MMSYSTEM.1219]
755 UINT WINAPI mmioAdvance(HMMIO hmmio,MMIOINFO*lpmmioinfo,UINT uFlags)
757 LPMMIOINFO16 lpmminfo;
758 TRACE(mmio, "mmioAdvance\n");
759 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
760 if (lpmminfo == NULL) return 0;
761 if (!lpmminfo->cchBuffer) {
762 GlobalUnlock16(hmmio);
763 return MMIOERR_UNBUFFERED;
765 lpmminfo->pchNext = lpmmioinfo->pchNext;
766 if (mmioFlush(hmmio, MMIO_EMPTYBUF)) {
767 GlobalUnlock16(hmmio);
768 return MMIOERR_CANNOTWRITE;
770 if (uFlags == MMIO_READ)
771 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
772 mmioSendMessage(hmmio, MMIOM_READ,
773 (LPARAM) lpmmioinfo->pchBuffer,
774 (LPARAM) lpmmioinfo->cchBuffer);
775 #if 0 /* mmioFlush already did the writing */
776 if (uFlags == MMIO_WRITE)
777 mmioSendMessage(hmmio, MMIOM_WRITE,
778 (LPARAM) lpmmioinfo->pchBuffer,
779 (LPARAM) lpmmioinfo->cchBuffer);
780 #endif
781 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
782 GlobalUnlock16(hmmio);
783 return 0;
786 /**************************************************************************
787 * mmioAdvance [MMSYSTEM.1219]
789 UINT16 WINAPI mmioAdvance16(HMMIO16 hmmio,MMIOINFO16*lpmmioinfo,UINT16 uFlags)
791 LPMMIOINFO16 lpmminfo;
792 TRACE(mmio, "mmioAdvance\n");
793 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
794 if (lpmminfo == NULL) return 0;
795 if (!lpmminfo->cchBuffer) {
796 GlobalUnlock16(hmmio);
797 return MMIOERR_UNBUFFERED;
799 lpmminfo->pchNext = lpmmioinfo->pchNext;
800 if (mmioFlush(hmmio, MMIO_EMPTYBUF)) {
801 GlobalUnlock16(hmmio);
802 return MMIOERR_CANNOTWRITE;
804 if (uFlags == MMIO_READ)
805 lpmmioinfo->pchEndRead = lpmmioinfo->pchBuffer +
806 mmioSendMessage(hmmio, MMIOM_READ,
807 (LPARAM) lpmmioinfo->pchBuffer,
808 (LPARAM) lpmmioinfo->cchBuffer);
809 #if 0 /* mmioFlush already did the writing */
810 if (uFlags == MMIO_WRITE)
811 mmioSendMessage(hmmio, MMIOM_WRITE,
812 (LPARAM) lpmmioinfo->pchBuffer,
813 (LPARAM) lpmmioinfo->cchBuffer);
814 #endif
815 lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
816 GlobalUnlock16(hmmio);
817 return 0;
820 /**************************************************************************
821 * mmioStringToFOURCCA [WINMM.131]
823 FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
825 return mmioStringToFOURCC16(sz,uFlags);
828 /**************************************************************************
829 * mmioStringToFOURCCW [WINMM.132]
831 FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
833 LPSTR szA = HEAP_strdupWtoA(GetProcessHeap(),0,sz);
834 FOURCC ret = mmioStringToFOURCCA(szA,uFlags);
836 HeapFree(GetProcessHeap(),0,szA);
837 return ret;
840 /**************************************************************************
841 * mmioStringToFOURCC [MMSYSTEM.1220]
843 FOURCC WINAPI mmioStringToFOURCC16(LPCSTR sz, UINT16 uFlags)
845 return mmioFOURCC(sz[0],sz[1],sz[2],sz[3]);
848 /**************************************************************************
849 * mmioInstallIOProc16 [MMSYSTEM.1221]
851 LPMMIOPROC16 WINAPI mmioInstallIOProc16(FOURCC fccIOProc,
852 LPMMIOPROC16 pIOProc, DWORD dwFlags)
854 TRACE(mmio, "(%ld, %p, %08lX)\n",
855 fccIOProc, pIOProc, dwFlags);
857 if (dwFlags & MMIO_GLOBALPROC) {
858 FIXME(mmio, " global procedures not implemented\n");
861 /* just handle the known procedures for now */
862 switch(dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
863 case MMIO_INSTALLPROC:
864 return NULL;
865 case MMIO_REMOVEPROC:
866 return NULL;
867 case MMIO_FINDPROC:
868 if (fccIOProc == FOURCC_DOS)
869 return (LPMMIOPROC16) mmioDosIOProc;
870 else if (fccIOProc == FOURCC_MEM)
871 return (LPMMIOPROC16) mmioMemIOProc;
872 else
873 return NULL;
874 default:
875 return NULL;
879 /**************************************************************************
880 * mmioInstallIOProcA [WINMM.120]
882 LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
883 LPMMIOPROC pIOProc, DWORD dwFlags)
885 FIXME(mmio, "(%c%c%c%c,%p,0x%08lx) -- empty stub \n",
886 (char)((fccIOProc&0xff000000)>>24),
887 (char)((fccIOProc&0x00ff0000)>>16),
888 (char)((fccIOProc&0x0000ff00)>> 8),
889 (char)(fccIOProc&0x000000ff),
890 pIOProc, dwFlags );
891 return 0;
894 /**************************************************************************
895 * mmioSendMessage [MMSYSTEM.1222]
897 LRESULT WINAPI mmioSendMessage(HMMIO16 hmmio, UINT16 uMessage,
898 LPARAM lParam1, LPARAM lParam2)
900 LPMMIOINFO16 lpmminfo;
901 LRESULT result;
902 const char *msg = NULL;
904 #ifdef DEBUG_RUNTIME
905 switch (uMessage) {
906 #define msgname(x) case x: msg = #x; break;
907 msgname(MMIOM_OPEN);
908 msgname(MMIOM_CLOSE);
909 msgname(MMIOM_READ);
910 msgname(MMIOM_WRITE);
911 msgname(MMIOM_WRITEFLUSH);
912 msgname(MMIOM_SEEK);
913 msgname(MMIOM_RENAME);
914 #undef msgname
916 #endif
918 if (msg)
919 TRACE(mmio, "(%04X, %s, %ld, %ld)\n",
920 hmmio, msg, lParam1, lParam2);
921 else
922 TRACE(mmio, "(%04X, %u, %ld, %ld)\n",
923 hmmio, uMessage, lParam1, lParam2);
925 lpmminfo = (LPMMIOINFO16)GlobalLock16(hmmio);
927 if (lpmminfo && lpmminfo->pIOProc)
928 result = (*lpmminfo->pIOProc)((LPSTR)lpmminfo, uMessage, lParam1, lParam2);
929 else
930 result = MMSYSERR_INVALPARAM;
932 GlobalUnlock16(hmmio);
934 return result;
937 /**************************************************************************
938 * mmioDescend [MMSYSTEM.1223]
940 UINT16 WINAPI mmioDescend(HMMIO16 hmmio, LPMMCKINFO lpck,
941 const MMCKINFO * lpckParent, UINT16 uFlags)
943 DWORD dwOldPos;
944 MMCKINFO searchcki;
945 char ckid[5],fcc[5];
947 TRACE(mmio,"(%04X, %p, %p, %04X);\n",hmmio,lpck,lpckParent,uFlags);
949 if (lpck == NULL)
950 return MMSYSERR_INVALPARAM;
952 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
953 TRACE(mmio, "dwOldPos=%ld\n", dwOldPos);
955 if (lpckParent != NULL) {
956 TRACE(mmio, "seek inside parent at %ld !\n", lpckParent->dwDataOffset);
957 /* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
958 if (dwOldPos < lpckParent->dwDataOffset || dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
959 ERR(mmio, "outside parent chunk\n");
960 return MMIOERR_CHUNKNOTFOUND;
964 /* The SDK docu says 'ckid' is used for all cases. Real World
965 * examples disagree -Marcus,990216.
968 searchcki.fccType = 0;
969 /* find_chunk looks for 'ckid' */
970 if (uFlags & MMIO_FINDCHUNK)
971 searchcki.ckid = lpck->ckid;
972 /* find_riff and find_list look for 'fccType' */
973 if (uFlags & MMIO_FINDLIST) {
974 searchcki.ckid = FOURCC_LIST;
975 searchcki.fccType = lpck->fccType;
977 if (uFlags & MMIO_FINDRIFF) {
978 searchcki.ckid = FOURCC_RIFF;
979 searchcki.fccType = lpck->fccType;
981 memcpy(&fcc,&(searchcki.fccType),4);fcc[4]=0;
982 memcpy(&ckid,&(searchcki.ckid),4);ckid[4]=0;
983 TRACE(mmio,"searching for %s.%s\n",ckid,searchcki.fccType?fcc:"<any>");
985 if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
986 while (TRUE) {
987 LONG ix;
989 ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
990 if (ix < 2*sizeof(DWORD)) {
991 mmioSeek(hmmio, dwOldPos, SEEK_SET);
992 WARN(mmio, "return ChunkNotFound\n");
993 return MMIOERR_CHUNKNOTFOUND;
995 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
996 if (ix < lpck->dwDataOffset - dwOldPos) {
997 mmioSeek(hmmio, dwOldPos, SEEK_SET);
998 WARN(mmio, "return ChunkNotFound\n");
999 return MMIOERR_CHUNKNOTFOUND;
1001 memcpy(ckid,&lpck->ckid,4);
1002 memcpy(fcc,&lpck->fccType,4);
1003 TRACE(mmio, "ckid=%s fcc=%s cksize=%08lX !\n",
1004 ckid, searchcki.fccType?fcc:"<unused>",
1005 lpck->cksize
1007 if ((searchcki.ckid == lpck->ckid) &&
1008 (!searchcki.fccType ||
1009 (searchcki.fccType == lpck->fccType)
1012 break;
1014 dwOldPos = lpck->dwDataOffset + lpck->cksize;
1015 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1017 /* If we were looking for RIFF/LIST chunks, the final dataptr
1018 * is after the chunkid. If we were just looking for the chunk
1019 * it is after the cksize. So add 4 in RIFF/LIST case.
1021 if (uFlags & (MMIO_FINDLIST|MMIO_FINDRIFF))
1022 lpck->dwDataOffset+=sizeof(DWORD);
1023 } else {
1024 /* FIXME: unverified, does it do this? */
1025 if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
1026 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1027 WARN(mmio, "return ChunkNotFound 2nd\n");
1028 return MMIOERR_CHUNKNOTFOUND;
1030 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1031 lpck->dwFlags = 0;
1032 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1033 lpck->dwDataOffset += sizeof(DWORD);
1035 mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1036 memcpy(ckid,&(lpck->ckid),4);
1037 TRACE(mmio, "lpck->ckid=%s lpck->cksize=%ld !\n", ckid, lpck->cksize);
1038 memcpy(fcc,&(lpck->fccType),4);
1039 TRACE(mmio, "lpck->fccType=%08lX (%s)!\n", lpck->fccType,searchcki.fccType?fcc:"");
1040 return 0;
1043 /**************************************************************************
1044 * mmioAscend [WINMM.113]
1046 UINT WINAPI mmioAscend(HMMIO hmmio, MMCKINFO * lpck, UINT uFlags)
1048 TRACE(mmio, "(%04X, %p, %04X);\n",
1049 hmmio, lpck, uFlags);
1050 if (lpck->dwFlags&MMIO_DIRTY) {
1051 DWORD dwOldPos, dwNewSize, dwSizePos;
1053 TRACE(mmio, "chunk is marked MMIO_DIRTY, correcting chunk size\n");
1054 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1055 TRACE(mmio, "dwOldPos=%ld\n", dwOldPos);
1056 dwNewSize = dwOldPos - lpck->dwDataOffset;
1057 if (dwNewSize != lpck->cksize) {
1058 TRACE(mmio, "dwNewSize=%ld\n", dwNewSize);
1059 lpck->cksize = dwNewSize;
1061 dwSizePos = lpck->dwDataOffset - sizeof(DWORD);
1062 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1063 dwSizePos -= sizeof(DWORD);
1064 TRACE(mmio, "dwSizePos=%ld\n", dwSizePos);
1066 mmioSeek(hmmio, dwSizePos, SEEK_SET);
1067 mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
1070 mmioSeek(hmmio,lpck->dwDataOffset+lpck->cksize,SEEK_SET);
1072 return 0;
1075 /**************************************************************************
1076 * mmioAscend [MMSYSTEM.1224]
1078 UINT16 WINAPI mmioAscend16(HMMIO16 hmmio, MMCKINFO * lpck, UINT16 uFlags)
1080 return mmioAscend(hmmio,lpck,uFlags);
1083 /**************************************************************************
1084 * mmioCreateChunk [MMSYSTEM.1225]
1086 UINT16 WINAPI mmioCreateChunk16(HMMIO16 hmmio, MMCKINFO * lpck, UINT16 uFlags)
1088 DWORD dwOldPos;
1089 LONG ix;
1091 TRACE(mmio, "(%04X, %p, %04X);\n",
1092 hmmio, lpck, uFlags);
1094 dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1095 TRACE(mmio, "dwOldPos=%ld\n", dwOldPos);
1097 if (uFlags == MMIO_CREATELIST)
1098 lpck->ckid = FOURCC_LIST;
1099 else if (uFlags == MMIO_CREATERIFF)
1100 lpck->ckid = FOURCC_RIFF;
1102 TRACE(mmio, "ckid=%08lX\n", lpck->ckid);
1104 lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1105 if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1106 lpck->dwDataOffset += sizeof(DWORD);
1107 lpck->dwFlags = MMIO_DIRTY;
1109 ix = mmioWrite(hmmio, (LPSTR)lpck, lpck->dwDataOffset - dwOldPos);
1110 TRACE(mmio, "after mmioWrite ix = %ld req = %ld, errno = %d\n",ix,lpck->dwDataOffset - dwOldPos,errno);
1111 if (ix < lpck->dwDataOffset - dwOldPos) {
1113 mmioSeek(hmmio, dwOldPos, SEEK_SET);
1114 WARN(mmio, "return CannotWrite\n");
1115 return MMIOERR_CANNOTWRITE;
1118 return 0;
1121 /**************************************************************************
1122 * mmioCreateChunk [WINMM.115]
1124 UINT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO * lpck, UINT uFlags)
1126 return mmioCreateChunk16(hmmio, lpck, uFlags);
1129 /**************************************************************************
1130 * mmioRename [MMSYSTEM.1226]
1132 UINT16 WINAPI mmioRename16(LPCSTR szFileName, LPCSTR szNewFileName,
1133 MMIOINFO16 * lpmmioinfo, DWORD dwRenameFlags)
1135 UINT16 result;
1136 LPMMIOINFO16 lpmminfo;
1137 HMMIO16 hmmio;
1139 TRACE(mmio, "('%s', '%s', %p, %08lX);\n",
1140 szFileName, szNewFileName, lpmmioinfo, dwRenameFlags);
1142 hmmio = GlobalAlloc16(GHND, sizeof(MMIOINFO16));
1143 lpmminfo = (LPMMIOINFO16) GlobalLock16(hmmio);
1145 if (lpmmioinfo)
1146 memcpy(lpmminfo, lpmmioinfo, sizeof(MMIOINFO16));
1148 /* assume DOS file if not otherwise specified */
1149 if (lpmminfo->fccIOProc == 0 && lpmminfo->pIOProc == NULL) {
1151 lpmminfo->fccIOProc = mmioFOURCC('D', 'O', 'S', ' ');
1152 lpmminfo->pIOProc = (LPMMIOPROC16) mmioDosIOProc;
1155 /* if just the four character code is present, look up IO proc */
1156 else if (lpmminfo->pIOProc == NULL) {
1158 lpmminfo->pIOProc = mmioInstallIOProc16(lpmminfo->fccIOProc, NULL, MMIO_FINDPROC);
1161 /* (if IO proc specified, use it and specified four character code) */
1163 result = (UINT16) mmioSendMessage(hmmio, MMIOM_RENAME, (LPARAM) szFileName, (LPARAM) szNewFileName);
1165 GlobalUnlock16(hmmio);
1166 GlobalFree16(hmmio);
1168 return result;
1171 /**************************************************************************
1172 * mmioRenameA [WINMM.125]
1174 UINT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1175 MMIOINFO* lpmmioinfo, DWORD dwRenameFlags)
1177 FIXME(mmio, "This may fail\n");
1178 return mmioRename16(szFileName, szNewFileName, (MMIOINFO16*)lpmmioinfo, dwRenameFlags);
1181 /**************************************************************************
1182 * mmioRenameW [WINMM.126]
1184 UINT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1185 MMIOINFO* lpmmioinfo, DWORD dwRenameFlags)
1187 LPSTR szFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szFileName);
1188 LPSTR sznFn = HEAP_strdupWtoA(GetProcessHeap(), 0, szNewFileName);
1189 UINT ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwRenameFlags);
1191 HeapFree(GetProcessHeap(),0,szFn);
1192 HeapFree(GetProcessHeap(),0,sznFn);
1193 return ret;