mmio.c 40.8 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * MMIO functions
 *
 * Copyright 1998 Andrew Taylor
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 * Copyright 1998 Ove Kven
6
 * Copyright 2000,2002 Eric Pouech
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
21 22
 */

23
/* Still to be done:
24
 * 	+ correct handling of global/local IOProcs (and temporary IOProcs)
25 26
 *	+ mode of mmio objects is not used (read vs write vs readwrite)
 *	+ thread safeness
27
 *      + 32 A <=> W message mapping
28
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
29

30

31
#include <ctype.h>
32
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
33 34 35
#include <stdlib.h>
#include <string.h>
#include <errno.h>
36
#include <assert.h>
37

38
#include "windef.h"
39
#include "winbase.h"
40
#include "winnls.h"
41
#include "mmsystem.h"
42
#include "winemm.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
43

44
#include "wine/debug.h"
45

46
WINE_DEFAULT_DEBUG_CHANNEL(mmio);
47

48
LRESULT         (*pFnMmioCallback16)(DWORD,LPMMIOINFO,UINT,LPARAM,LPARAM) /* = NULL */;
Eric Pouech's avatar
Eric Pouech committed
49

50
/**************************************************************************
51
 *               	mmioDosIOProc           		[internal]
52
 */
53 54
static LRESULT CALLBACK mmioDosIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
				      LPARAM lParam1, LPARAM lParam2)
55 56 57
{
    LRESULT	ret = MMSYSERR_NOERROR;

58
    TRACE("(%p, %X, 0x%lx, 0x%lx);\n", lpmmioinfo, uMessage, lParam1, lParam2);
59

60
    switch (uMessage) {
61
    case MMIOM_OPEN:
62 63 64
	{
	    /* Parameters:
	     * lParam1 = szFileName parameter from mmioOpen
65
	     * lParam2 = reserved
66 67 68
	     * Returns: zero on success, error code on error
	     * NOTE: lDiskOffset automatically set to zero
	     */
69 70
	    LPCSTR      szFileName = (LPCSTR)lParam1;

71 72 73 74
	    if (lpmmioinfo->dwFlags & MMIO_GETTEMP) {
		FIXME("MMIO_GETTEMP not implemented\n");
		return MMIOERR_CANNOTOPEN;
	    }
75

76
	    /* if filename NULL, assume open file handle in adwInfo[0] */
Eric Pouech's avatar
Eric Pouech committed
77
	    if (szFileName) {
78 79 80
                OFSTRUCT    ofs;
                lpmmioinfo->adwInfo[0] = (DWORD)OpenFile(szFileName, &ofs, lpmmioinfo->dwFlags & 0xFFFF);
            }
81
	    if (lpmmioinfo->adwInfo[0] == (DWORD)HFILE_ERROR)
82 83 84
		ret = MMIOERR_CANNOTOPEN;
	}
	break;
85 86

    case MMIOM_CLOSE:
87 88 89 90 91 92 93 94
	/* Parameters:
	 * lParam1 = wFlags parameter from mmioClose
	 * lParam2 = unused
	 * Returns: zero on success, error code on error
	 */
	if (!(lParam1 & MMIO_FHOPEN))
	    _lclose((HFILE)lpmmioinfo->adwInfo[0]);
	break;
95 96

    case MMIOM_READ:
97 98 99 100 101 102 103 104 105
	/* Parameters:
	 * lParam1 = huge pointer to read buffer
	 * lParam2 = number of bytes to read
	 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
	 *	   in wErrorRet)
	 */
	ret = _lread((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
	if (ret != -1)
	    lpmmioinfo->lDiskOffset += ret;
106

107
	break;
108

109
    case MMIOM_WRITE:
110
    case MMIOM_WRITEFLUSH:
111
	/* no internal buffering, so WRITEFLUSH handled same as WRITE */
112

113 114 115 116 117 118 119 120 121 122
	/* Parameters:
	 * lParam1 = huge pointer to write buffer
	 * lParam2 = number of bytes to write
	 * Returns: number of bytes written, -1 for error (error code in
	 *		wErrorRet)
	 */
	ret = _hwrite((HFILE)lpmmioinfo->adwInfo[0], (HPSTR)lParam1, (LONG)lParam2);
	if (ret != -1)
	    lpmmioinfo->lDiskOffset += ret;
	break;
123 124

    case MMIOM_SEEK:
125 126 127 128 129 130 131 132 133
	/* Parameters:
	 * lParam1 = new position
	 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
	 * Returns: new file postion, -1 on error
	 */
	ret = _llseek((HFILE)lpmmioinfo->adwInfo[0], (LONG)lParam1, (LONG)lParam2);
	if (ret != -1)
	    lpmmioinfo->lDiskOffset = ret;
	return ret;
134 135

    case MMIOM_RENAME:
136 137 138 139 140
	/* Parameters:
	 * lParam1 = old name
	 * lParam2 = new name
	 * Returns: zero on success, non-zero on failure
	 */
141 142 143 144
 	 if (!MoveFileA((const char*)lParam1, (const char*)lParam2))
	     ret = MMIOERR_FILENOTFOUND;
	 break;

145 146 147 148
    default:
	FIXME("unexpected message %u\n", uMessage);
	return 0;
    }
149

150
    return ret;
151 152
}

Alexandre Julliard's avatar
Alexandre Julliard committed
153
/**************************************************************************
154
 *               	mmioMemIOProc           		[internal]
Alexandre Julliard's avatar
Alexandre Julliard committed
155
 */
156 157
static LRESULT CALLBACK mmioMemIOProc(LPMMIOINFO lpmmioinfo, UINT uMessage,
				      LPARAM lParam1, LPARAM lParam2)
158 159
{
    TRACE("(%p,0x%04x,0x%08lx,0x%08lx)\n", lpmmioinfo, uMessage, lParam1, lParam2);
Alexandre Julliard's avatar
Alexandre Julliard committed
160

161
    switch (uMessage) {
162 163

    case MMIOM_OPEN:
164 165
	/* Parameters:
	 * lParam1 = filename (must be NULL)
166
	 * lParam2 = reserved
167 168 169 170 171 172
	 * Returns: zero on success, error code on error
	 * NOTE: lDiskOffset automatically set to zero
	 */
	/* FIXME: io proc shouldn't change it */
	if (!(lpmmioinfo->dwFlags & MMIO_CREATE))
	    lpmmioinfo->pchEndRead = lpmmioinfo->pchEndWrite;
173
        lpmmioinfo->adwInfo[0] = HFILE_ERROR;
174
	return 0;
175 176

    case MMIOM_CLOSE:
177 178 179 180 181 182
	/* Parameters:
	 * lParam1 = wFlags parameter from mmioClose
	 * lParam2 = unused
	 * Returns: zero on success, error code on error
	 */
	return 0;
183 184

    case MMIOM_READ:
185 186 187 188 189 190 191 192 193
	/* Parameters:
	 * lParam1 = huge pointer to read buffer
	 * lParam2 = number of bytes to read
	 * Returns: number of bytes read, 0 for EOF, -1 for error (error code
	 *	   in wErrorRet)
	 * NOTE: lDiskOffset should be updated
	 */
	FIXME("MMIOM_READ on memory files should not occur, buffer may be lost!\n");
	return 0;
194

195
    case MMIOM_WRITE:
196
    case MMIOM_WRITEFLUSH:
197
	/* no internal buffering, so WRITEFLUSH handled same as WRITE */
198

199 200 201 202 203 204 205 206 207
	/* Parameters:
	 * lParam1 = huge pointer to write buffer
	 * lParam2 = number of bytes to write
	 * Returns: number of bytes written, -1 for error (error code in
	 *		wErrorRet)
	 * NOTE: lDiskOffset should be updated
	 */
	FIXME("MMIOM_WRITE on memory files should not occur, buffer may be lost!\n");
	return 0;
208 209

    case MMIOM_SEEK:
210 211 212 213 214 215 216 217
	/* Parameters:
	 * lParam1 = new position
	 * lParam2 = from whence to seek (SEEK_SET, SEEK_CUR, SEEK_END)
	 * Returns: new file postion, -1 on error
	 * NOTE: lDiskOffset should be updated
	 */
	FIXME("MMIOM_SEEK on memory files should not occur, buffer may be lost!\n");
	return -1;
218

219 220 221 222 223
    default:
	FIXME("unexpected message %u\n", uMessage);
	return 0;
    }
}
Alexandre Julliard's avatar
Alexandre Julliard committed
224

225 226 227
/* This array will be the entire list for most apps 
 * Note that temporary ioProcs will be stored with a 0 fourCC code
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
228

229 230 231 232
static struct IOProcList defaultProcs[] = {
    {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, MMIO_PROC_32A, 0},
    {NULL,             FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, MMIO_PROC_32A, 0},
};
Alexandre Julliard's avatar
Alexandre Julliard committed
233

234
static struct IOProcList*	pIOProcListAnchor = &defaultProcs[0];
Alexandre Julliard's avatar
Alexandre Julliard committed
235

236 237 238 239 240
/****************************************************************
 *       	MMIO_FindProcNode 			[INTERNAL]
 *
 * Finds the ProcList node associated with a given FOURCC code.
 */
241
static struct IOProcList*	MMIO_FindProcNode(FOURCC fccIOProc)
242 243
{
    struct IOProcList*	pListNode;
Alexandre Julliard's avatar
Alexandre Julliard committed
244

245 246 247
    for (pListNode = pIOProcListAnchor; pListNode; pListNode = pListNode->pNext) {
	if (pListNode->fourCC == fccIOProc) {
	    return pListNode;
Alexandre Julliard's avatar
Alexandre Julliard committed
248
	}
249 250
    }
    return NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
251 252
}

253 254 255
/****************************************************************
 *       	MMIO_InstallIOProc 			[INTERNAL]
 */
256 257
LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
                              DWORD dwFlags, enum mmioProcType type)
258 259 260 261 262
{
    LPMMIOPROC	        lpProc = NULL;
    struct IOProcList*  pListNode;
    struct IOProcList** ppListNode;

263
    TRACE("(%08lx, %p, %08lX, %i)\n", fccIOProc, pIOProc, dwFlags, type);
264 265 266 267 268 269 270 271 272 273 274 275 276 277

    if (dwFlags & MMIO_GLOBALPROC)
	FIXME("Global procedures not implemented\n");

    /* just handle the known procedures for now */
    switch (dwFlags & (MMIO_INSTALLPROC|MMIO_REMOVEPROC|MMIO_FINDPROC)) {
    case MMIO_INSTALLPROC:
	/* Create new entry for the IOProc list */
	pListNode = HeapAlloc(GetProcessHeap(), 0, sizeof(*pListNode));
	if (pListNode) {
	    /* Fill in this node */
	    pListNode->fourCC = fccIOProc;
	    pListNode->pIOProc = pIOProc;
	    pListNode->type = type;
278 279
	    pListNode->count = 0;

280 281 282
	    /* Stick it on the end of the list */
	    pListNode->pNext = pIOProcListAnchor;
	    pIOProcListAnchor = pListNode;
283

284 285
	    /* Return this IOProc - that's how the caller knows we succeeded */
	    lpProc = pIOProc;
286
	}
287
	break;
288

289
    case MMIO_REMOVEPROC:
290
	/*
291 292 293 294
	 * Search for the node that we're trying to remove
         * We search for a matching fourCC code if it's non null, or the proc
         * address otherwise
         * note that this method won't find the first item on the list, but
295 296
	 * since the first two items on this list are ones we won't
	 * let the user delete anyway, that's okay
297
	 */
298
	ppListNode = &pIOProcListAnchor;
299 300 301 302
	while ((*ppListNode) && 
               ((fccIOProc != 0) ? 
                (*ppListNode)->fourCC != fccIOProc : 
                (*ppListNode)->pIOProc != pIOProc))
303 304 305 306 307 308 309 310 311 312 313
	    ppListNode = &((*ppListNode)->pNext);

	if (*ppListNode) { /* found it */
	    /* FIXME: what should be done if an open mmio object uses this proc ?
	     * shall we return an error, nuke the mmio object ?
	     */
	    if ((*ppListNode)->count) {
		ERR("Cannot remove a mmIOProc while in use\n");
		break;
	    }
	    /* remove it, but only if it isn't builtin */
314
	    if ((*ppListNode) >= defaultProcs &&
315 316 317 318
		(*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
		WARN("Tried to remove built-in mmio proc. Skipping\n");
	    } else {
		/* Okay, nuke it */
319
		struct IOProcList*  ptmpNode = *ppListNode;
320 321
		lpProc = (*ppListNode)->pIOProc;
		*ppListNode = (*ppListNode)->pNext;
322
		HeapFree(GetProcessHeap(), 0, ptmpNode);
323 324 325
	    }
	}
	break;
326

327 328 329 330 331 332
    case MMIO_FINDPROC:
	if ((pListNode = MMIO_FindProcNode(fccIOProc))) {
	    lpProc = pListNode->pIOProc;
	}
	break;
    }
333

334 335
    return lpProc;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
336

337
/****************************************************************
338
 *       	send_message    			[INTERNAL]
339
 */
340 341 342
static LRESULT	send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
                             DWORD wMsg, LPARAM lParam1,
                             LPARAM lParam2, enum mmioProcType type)
343
{
344
    LRESULT 		result = MMSYSERR_ERROR;
345 346
    LPARAM		lp1 = lParam1, lp2 = lParam2;

347
    if (!ioProc) {
348 349 350 351
	ERR("brrr\n");
	result = MMSYSERR_INVALPARAM;
    }

352
    switch (ioProc->type) {
353
    case MMIO_PROC_16:
Eric Pouech's avatar
Eric Pouech committed
354
        if (pFnMmioCallback16)
355
            result = pFnMmioCallback16((DWORD)ioProc->pIOProc,
Eric Pouech's avatar
Eric Pouech committed
356
                                       mmioinfo, wMsg, lp1, lp2);
357
        break;
358 359
    case MMIO_PROC_32A:
    case MMIO_PROC_32W:
360 361 362
	if (ioProc->type != type) {
	    /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
	    FIXME("NIY 32 A<=>W mapping\n");
363
	}
364
	result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
365

366
#if 0
367 368
	if (ioProc->type != type) {
	    /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
369 370 371
	}
#endif
	break;
372
    default:
373 374 375 376
	FIXME("Internal error\n");
    }

    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
377 378
}

379
/**************************************************************************
380
 *      			MMIO_ParseExtA 		        [internal]
381 382 383 384 385 386
 *
 * Parses a filename for the extension.
 *
 * RETURNS
 *  The FOURCC code for the extension if found, else 0.
 */
387
static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
388
{
389 390
    /* Filenames are of the form file.ext{+ABC}
       For now, we take the last '+' if present */
391

392
    FOURCC ret = 0;
393

394 395
    /* Note that ext{Start,End} point to the . and + respectively */
    LPSTR extEnd;
396
    LPSTR extStart;
397

398
    TRACE("(%s)\n", debugstr_a(szFileName));
399

400 401
    if (!szFileName)
	return ret;
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    /* Find the last '.' */
    extStart = strrchr(szFileName,'.');

    if (!extStart) {
         ERR("No . in szFileName: %s\n", debugstr_a(szFileName));
    } else {
        CHAR ext[5];

        /* Find the '+' afterwards */
        extEnd = strchr(extStart,'+');
        if (extEnd) {

            if (extEnd - extStart - 1 > 4)
                WARN("Extension length > 4\n");
            lstrcpynA(ext, extStart + 1, min(extEnd-extStart,5));

        } else {
            /* No + so just an extension */
            if (strlen(extStart) > 4) {
                WARN("Extension length > 4\n");
            }
            lstrcpynA(ext, extStart + 1, 5);
        }
        TRACE("Got extension: %s\n", debugstr_a(ext));

        /* FOURCC codes identifying file-extensions must be uppercase */
        ret = mmioStringToFOURCCA(ext, MMIO_TOUPPER);
430 431
    }
    return ret;
432 433
}

Alexandre Julliard's avatar
Alexandre Julliard committed
434
/**************************************************************************
435 436
 *				MMIO_Get			[internal]
 *
Andreas Mohr's avatar
Andreas Mohr committed
437
 * Retrieves the mmio object from current process
Alexandre Julliard's avatar
Alexandre Julliard committed
438
 */
439
LPWINE_MMIO	MMIO_Get(HMMIO h)
Alexandre Julliard's avatar
Alexandre Julliard committed
440
{
441
    LPWINE_MMIO		wm = NULL;
442

443 444
    EnterCriticalSection(&WINMM_IData.cs);
    for (wm = WINMM_IData.lpMMIO; wm; wm = wm->lpNext) {
445 446 447
	if (wm->info.hmmio == h)
	    break;
    }
448
    LeaveCriticalSection(&WINMM_IData.cs);
449 450
    return wm;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
451

452 453 454 455 456 457 458 459 460 461 462 463
/**************************************************************************
 *				MMIO_Create			[internal]
 *
 * Creates an internal representation for a mmio instance
 */
static	LPWINE_MMIO		MMIO_Create(void)
{
    static	WORD	MMIO_counter = 0;
    LPWINE_MMIO		wm;

    wm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MMIO));
    if (wm) {
464
	EnterCriticalSection(&WINMM_IData.cs);
465
        /* lookup next unallocated WORD handle, with a non NULL value */
466 467
	while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
	wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
468 469 470
	wm->lpNext = WINMM_IData.lpMMIO;
	WINMM_IData.lpMMIO = wm;
	LeaveCriticalSection(&WINMM_IData.cs);
471 472 473
    }
    return wm;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
474

475 476 477 478 479 480 481 482 483
/**************************************************************************
 *				MMIO_Destroy			[internal]
 *-
 * Destroys an internal representation for a mmio instance
 */
static	BOOL		MMIO_Destroy(LPWINE_MMIO wm)
{
    LPWINE_MMIO*	m;

484
    EnterCriticalSection(&WINMM_IData.cs);
485
    /* search for the matching one... */
486
    m = &(WINMM_IData.lpMMIO);
487 488
    while (*m && *m != wm) m = &(*m)->lpNext;
    /* ...and destroy */
489 490
    if (*m) {
	*m = (*m)->lpNext;
491
	HeapFree(GetProcessHeap(), 0, wm);
492 493
	wm = NULL;
    }
494
    LeaveCriticalSection(&WINMM_IData.cs);
495 496
    return wm ? FALSE : TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
497

498 499 500
/****************************************************************
 *       		MMIO_Flush 			[INTERNAL]
 */
501
static	MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
502
{
503 504 505
    if (wm->info.cchBuffer && (wm->info.fccIOProc != FOURCC_MEM)) {
	/* not quite sure what to do here, but I'll guess */
	if (wm->info.dwFlags & MMIO_DIRTY) {
506
            /* FIXME: error handling */
507 508 509 510 511
	    send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
                         wm->info.lBufOffset, SEEK_SET, MMIO_PROC_32A);
	    send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
                         (LPARAM)wm->info.pchBuffer,
                         wm->info.pchNext - wm->info.pchBuffer, MMIO_PROC_32A);
512
	}
513
	if (uFlags & MMIO_EMPTYBUF)
514
	    wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
515
    }
516
    wm->info.dwFlags &= ~MMIO_DIRTY;
517

518
    return MMSYSERR_NOERROR;
519
}
Alexandre Julliard's avatar
Alexandre Julliard committed
520

521 522 523 524 525 526 527
/***************************************************************************
 *       		MMIO_GrabNextBuffer			[INTERNAL]
 */
static LONG	MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
{
    LONG	size = wm->info.cchBuffer;

528 529
    TRACE("bo=%lx do=%lx of=%lx\n",
	  wm->info.lBufOffset, wm->info.lDiskOffset,
530
	  send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, MMIO_PROC_32A));
531

532 533 534
    wm->info.lBufOffset = wm->info.lDiskOffset;
    wm->info.pchNext = wm->info.pchBuffer;
    wm->info.pchEndRead = wm->info.pchBuffer;
535
    wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
536

537
    wm->bBufferLoaded = TRUE;
538
    if (for_read) {
539 540
	size = send_message(wm->ioProc, &wm->info, MMIOM_READ, 
                            (LPARAM)wm->info.pchBuffer, size, MMIO_PROC_32A);
541 542
	if (size > 0)
	    wm->info.pchEndRead += size;
543 544
        else
            wm->bBufferLoaded = FALSE;
545
    }
546

547 548 549 550 551 552
    return size;
}

/***************************************************************************
 *       		MMIO_SetBuffer 				[INTERNAL]
 */
553
static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
554
			       UINT uFlags)
555
{
556
    TRACE("(%p %p %ld %u)\n", wm, pchBuffer, cchBuffer, uFlags);
557 558

    if (uFlags)			return MMSYSERR_INVALPARAM;
559 560
    if (cchBuffer > 0xFFFF)
	WARN("Untested handling of huge mmio buffers (%ld >= 64k)\n", cchBuffer);
561

562
    if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
563
	return MMIOERR_CANNOTWRITE;
564

565 566 567 568
    /* free previous buffer if allocated */
    if (wm->info.dwFlags & MMIO_ALLOCBUF) {
        HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
        wm->info.pchBuffer = NULL;
569 570
	wm->info.dwFlags &= ~MMIO_ALLOCBUF;
    }
571

572
    if (pchBuffer) {
573
        wm->info.pchBuffer = pchBuffer;
574
    } else if (cchBuffer) {
575
	if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
576 577 578 579 580 581 582 583 584 585 586
	    return MMIOERR_OUTOFMEMORY;
	wm->info.dwFlags |= MMIO_ALLOCBUF;
    } else {
	wm->info.pchBuffer = NULL;
    }

    wm->info.cchBuffer = cchBuffer;
    wm->info.pchNext = wm->info.pchBuffer;
    wm->info.pchEndRead = wm->info.pchBuffer;
    wm->info.pchEndWrite = wm->info.pchBuffer + cchBuffer;
    wm->info.lBufOffset = 0;
587
    wm->bBufferLoaded = FALSE;
588

589
    return MMSYSERR_NOERROR;
590 591 592
}

/**************************************************************************
593
 * 			MMIO_Open       			[internal]
594
 */
595 596
HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, 
                enum mmioProcType type)
597 598
{
    LPWINE_MMIO		wm;
599
    MMIOINFO    	mmioinfo;
600 601

    TRACE("('%s', %p, %08lX, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
602

603 604 605 606 607 608 609 610 611 612
    if (!refmminfo) {
        refmminfo = &mmioinfo;

	mmioinfo.fccIOProc = 0;
	mmioinfo.pIOProc = NULL;
	mmioinfo.pchBuffer = NULL;
	mmioinfo.cchBuffer = 0;
        type = MMIO_PROC_32A;
    }

613 614
    if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
	char	buffer[MAX_PATH];
615

616
	if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
617
	    return (HMMIO)FALSE;
618
	if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
619 620 621 622
	    return (HMMIO)FALSE;
	strcpy(szFileName, buffer);
	return (HMMIO)TRUE;
    }
623

624 625 626
    if ((wm = MMIO_Create()) == NULL)
	return 0;

627
    /* If both params are NULL, then parse the file name if available */
628
    if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
629
	wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
630 631 632
	/* Handle any unhandled/error case. Assume DOS file */
	if (wm->info.fccIOProc == 0)
	    wm->info.fccIOProc = FOURCC_DOS;
633 634 635 636 637 638
	if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) {
	    /* If not found, retry with FOURCC_DOS */
	    wm->info.fccIOProc = FOURCC_DOS;
	    if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc)))
		goto error2;
	}
639 640 641
	wm->bTmpIOProc = FALSE;
    }
    /* if just the four character code is present, look up IO proc */
642
    else if (refmminfo->pIOProc == NULL) {
643 644 645
	wm->info.fccIOProc = refmminfo->fccIOProc;
	if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
	wm->bTmpIOProc = FALSE;
646
    }
647 648 649
    /* if IO proc specified, use it and specified four character code */
    else {
	wm->info.fccIOProc = refmminfo->fccIOProc;
650 651
	MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
                           MMIO_INSTALLPROC, type);
652 653 654 655
	if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
	assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
	wm->bTmpIOProc = TRUE;
    }
656

657
    wm->bBufferLoaded = FALSE;
658
    wm->ioProc->count++;
659

660
    if (dwOpenFlags & MMIO_ALLOCBUF) {
661
	if ((refmminfo->wErrorRet = MMIO_SetBuffer(wm, NULL, MMIO_DEFAULTBUFFER, 0)))
662 663
	    goto error1;
    } else if (wm->info.fccIOProc == FOURCC_MEM) {
664
        refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
665 666
	if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
	    goto error1;
667
	wm->bBufferLoaded = TRUE;
668
    } /* else => unbuffered, wm->info.pchBuffer == NULL */
669

670 671 672
    /* see mmioDosIOProc for that one */
    wm->info.adwInfo[0] = refmminfo->adwInfo[0];
    wm->info.dwFlags = dwOpenFlags;
673

674
    /* call IO proc to actually open file */
675 676
    refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN, 
                                        (LPARAM)szFileName, 0, MMIO_PROC_32A);
677 678

    /* grab file size, when possible */
679
    wm->dwFileSize = GetFileSize((HANDLE)wm->info.adwInfo[0], NULL);
680

681 682 683 684 685 686 687
    if (refmminfo->wErrorRet == 0)
	return wm->info.hmmio;
 error1:
    if (wm->ioProc) wm->ioProc->count--;
 error2:
    MMIO_Destroy(wm);
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
688 689
}

Alexandre Julliard's avatar
Alexandre Julliard committed
690
/**************************************************************************
691
 * 				mmioOpenW       		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
692
 */
693
HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
694
		       DWORD dwOpenFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
695
{
696
    HMMIO 	ret;
697 698 699 700 701 702
    LPSTR	szFn = NULL;

    if (szFileName)
    {
        INT     len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
        szFn = HeapAlloc( GetProcessHeap(), 0, len );
703
        if (!szFn) return NULL;
704 705
        WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
    }
706

707
    ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
708 709 710

    HeapFree(GetProcessHeap(), 0, szFn);
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
711 712 713
}

/**************************************************************************
714
 * 				mmioOpenA       		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
715
 */
716
HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
717
		       DWORD dwOpenFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
718
{
719
    return  MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, MMIO_PROC_32A);
Alexandre Julliard's avatar
Alexandre Julliard committed
720 721
}

Alexandre Julliard's avatar
Alexandre Julliard committed
722
/**************************************************************************
723
 * 				mmioClose      		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
724
 */
725
MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
726
{
727 728
    LPWINE_MMIO	wm;
    MMRESULT 	result;
729

730
    TRACE("(%p, %04X);\n", hmmio, uFlags);
731

732
    if ((wm = MMIO_Get(hmmio)) == NULL)
733
	return MMSYSERR_INVALHANDLE;
734

735
    if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
736
	return result;
737

738 739
    result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, 
                          uFlags, 0, MMIO_PROC_32A);
740

741
    MMIO_SetBuffer(wm, NULL, 0, 0);
742

743
    wm->ioProc->count--;
Alexandre Julliard's avatar
Alexandre Julliard committed
744

745
    if (wm->bTmpIOProc)
746 747
	MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
                           MMIO_REMOVEPROC, wm->ioProc->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
748

749
    MMIO_Destroy(wm);
Alexandre Julliard's avatar
Alexandre Julliard committed
750

751
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
752 753 754
}

/**************************************************************************
755
 * 				mmioRead	       	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
756
 */
757
LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
Alexandre Julliard's avatar
Alexandre Julliard committed
758
{
759 760
    LPWINE_MMIO	wm;
    LONG 	count;
761

762
    TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
763

764
    if ((wm = MMIO_Get(hmmio)) == NULL)
765 766 767 768
	return -1;

    /* unbuffered case first */
    if (!wm->info.pchBuffer)
769 770
	return send_message(wm->ioProc, &wm->info, MMIOM_READ, 
                            (LPARAM)pch, cch, MMIO_PROC_32A);
771 772 773 774 775 776 777 778 779 780 781

    /* first try from current buffer */
    if (wm->info.pchNext != wm->info.pchEndRead) {
	count = wm->info.pchEndRead - wm->info.pchNext;
	if (count > cch || count < 0) count = cch;
	memcpy(pch, wm->info.pchNext, count);
	wm->info.pchNext += count;
	pch += count;
	cch -= count;
    } else
	count = 0;
782

783 784 785 786 787 788 789 790 791 792 793 794 795 796
    if (cch && (wm->info.fccIOProc != FOURCC_MEM)) {
	assert(wm->info.cchBuffer);

	while (cch) {
	    LONG size;

	    size = MMIO_GrabNextBuffer(wm, TRUE);
	    if (size <= 0) break;
	    if (size > cch) size = cch;
	    memcpy(pch, wm->info.pchBuffer, size);
	    wm->info.pchNext += size;
	    pch += size;
	    cch -= size;
	    count += size;
Alexandre Julliard's avatar
Alexandre Julliard committed
797
	}
798
    }
799

800 801
    TRACE("count=%ld\n", count);
    return count;
Alexandre Julliard's avatar
Alexandre Julliard committed
802 803 804
}

/**************************************************************************
805
 * 				mmioWrite      		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
806
 */
807
LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
Alexandre Julliard's avatar
Alexandre Julliard committed
808
{
809
    LPWINE_MMIO	wm;
810
    LONG	count;
811

812
    TRACE("(%p, %p, %ld);\n", hmmio, pch, cch);
813

814
    if ((wm = MMIO_Get(hmmio)) == NULL)
815
	return -1;
816

817
    if (wm->info.cchBuffer) {
818 819 820 821 822 823 824 825 826 827
	LONG	bytesW = 0;

        count = 0;
        while (cch) {
            if (wm->info.pchNext != wm->info.pchEndWrite) {
                count = wm->info.pchEndWrite - wm->info.pchNext;
                if (count > cch || count < 0) count = cch;
                memcpy(wm->info.pchNext, pch, count);
                wm->info.pchNext += count;
                pch += count;
828
                cch -= count;
829
                bytesW += count;
830
                wm->info.dwFlags |= MMIO_DIRTY;
831 832 833 834
	    } else {
                if (wm->info.fccIOProc == FOURCC_MEM) {
                    if (wm->info.adwInfo[0]) {
                        /* from where would we get the memory handle? */
835
                        FIXME("memory file expansion not implemented!\n");
836
                        break;
837
		    } else break;
838
                }
839
            }
840

841 842 843 844 845
            if (wm->info.pchNext == wm->info.pchEndWrite)
            {
                MMIO_Flush(wm, MMIO_EMPTYBUF);
                MMIO_GrabNextBuffer(wm, FALSE);
            }
846
            else break;
847 848
        }
	count = bytesW;
849
    } else {
850 851
	count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
                             (LPARAM)pch, cch, MMIO_PROC_32A);
852 853
	wm->info.lBufOffset = wm->info.lDiskOffset;
    }
854

855 856
    TRACE("bytes written=%ld\n", count);
    return count;
Alexandre Julliard's avatar
Alexandre Julliard committed
857 858
}

Alexandre Julliard's avatar
Alexandre Julliard committed
859
/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
860
 * 				mmioSeek		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
861
 */
862
LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
Alexandre Julliard's avatar
Alexandre Julliard committed
863
{
864 865
    LPWINE_MMIO	wm;
    LONG 	offset;
Alexandre Julliard's avatar
Alexandre Julliard committed
866

867
    TRACE("(%p, %08lX, %d);\n", hmmio, lOffset, iOrigin);
868

869
    if ((wm = MMIO_Get(hmmio)) == NULL)
870 871
	return MMSYSERR_INVALHANDLE;

872
    /* not buffered, direct seek on file */
873
    if (!wm->info.pchBuffer)
874 875
	return send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
                            lOffset, iOrigin, MMIO_PROC_32A);
876 877

    switch (iOrigin) {
878
    case SEEK_SET:
879 880
	offset = lOffset;
	break;
881
    case SEEK_CUR:
882 883
	offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
	break;
884
    case SEEK_END:
885
	offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
886 887 888 889 890
	break;
    default:
	return -1;
    }

891 892 893 894 895 896 897 898 899 900 901
    if (offset && offset >= wm->dwFileSize && wm->info.fccIOProc != FOURCC_MEM) {
        /* should check that write mode exists */
        if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
            return -1;
        wm->info.lBufOffset = offset;
        wm->info.pchEndRead = wm->info.pchBuffer;
        wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
        if ((wm->info.dwFlags & MMIO_RWMODE) == MMIO_READ) {
            wm->info.lDiskOffset = wm->dwFileSize;
        }
    } else if ((wm->info.cchBuffer > 0) &&
902
	((offset < wm->info.lBufOffset) ||
903 904
	 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
	 !wm->bBufferLoaded)) {
905 906
        /* stay in same buffer ? */
        /* some memory mapped buffers are defined with -1 as a size */
907 908

	/* condition to change buffer */
909
	if ((wm->info.fccIOProc == FOURCC_MEM) ||
910
	    MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
911
	    /* this also sets the wm->info.lDiskOffset field */
912 913 914
	    send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
                         (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
                         SEEK_SET, MMIO_PROC_32A) == -1)
915 916 917 918 919 920 921 922
	    return -1;
	MMIO_GrabNextBuffer(wm, TRUE);
    }

    wm->info.pchNext = wm->info.pchBuffer + (offset - wm->info.lBufOffset);

    TRACE("=> %ld\n", offset);
    return offset;
Alexandre Julliard's avatar
Alexandre Julliard committed
923 924
}

Alexandre Julliard's avatar
Alexandre Julliard committed
925
/**************************************************************************
926
 * 				mmioGetInfo	       	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
927
 */
928
MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
929
{
930
    LPWINE_MMIO		wm;
931

932
    TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
933

934
    if ((wm = MMIO_Get(hmmio)) == NULL)
935 936 937
	return MMSYSERR_INVALHANDLE;

    memcpy(lpmmioinfo, &wm->info, sizeof(MMIOINFO));
938 939 940
    /* don't expose 16 bit ioproc:s */
    if (wm->ioProc->type != MMIO_PROC_16)
        lpmmioinfo->pIOProc = wm->ioProc->pIOProc;
941

942
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
943 944 945
}

/**************************************************************************
946
 * 				mmioSetInfo    		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
947
 */
948
MMRESULT WINAPI mmioSetInfo(HMMIO hmmio, const MMIOINFO* lpmmioinfo, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
949
{
950 951
    LPWINE_MMIO		wm;

952
    TRACE("(%p,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
953

954
    if ((wm = MMIO_Get(hmmio)) == NULL)
955
	return MMSYSERR_INVALHANDLE;
956

957
    /* check pointers coherence */
958
    if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
959
	lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
960
	lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
961
	lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
962
	lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
963 964 965 966 967 968
	lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
	return MMSYSERR_INVALPARAM;

    wm->info.pchNext = lpmmioinfo->pchNext;
    wm->info.pchEndRead = lpmmioinfo->pchEndRead;

969
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
970 971 972
}

/**************************************************************************
973
* 				mmioSetBuffer		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
974
*/
975
MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
976
{
977
    LPWINE_MMIO		wm;
Alexandre Julliard's avatar
Alexandre Julliard committed
978

979
    TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%ld, uFlags=%#08x)\n",
980
	  hmmio, pchBuffer, cchBuffer, uFlags);
981

982
    if ((wm = MMIO_Get(hmmio)) == NULL)
983
	return MMSYSERR_INVALHANDLE;
Alexandre Julliard's avatar
Alexandre Julliard committed
984

985
    return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
986 987
}

Alexandre Julliard's avatar
Alexandre Julliard committed
988
/**************************************************************************
989
 * 				mmioFlush      		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
990
 */
991
MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
992
{
993
    LPWINE_MMIO		wm;
Alexandre Julliard's avatar
Alexandre Julliard committed
994

995
    TRACE("(%p, %04X)\n", hmmio, uFlags);
996

997
    if ((wm = MMIO_Get(hmmio)) == NULL)
998
	return MMSYSERR_INVALHANDLE;
999

1000
    return MMIO_Flush(wm, uFlags);
Alexandre Julliard's avatar
Alexandre Julliard committed
1001 1002
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1003
/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1004
 * 				mmioAdvance      	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1005
 */
1006
MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1007
{
1008
    LPWINE_MMIO		wm;
1009

1010
    TRACE("hmmio=%p, lpmmioinfo=%p, uFlags=%04X\n", hmmio, lpmmioinfo, uFlags);
1011

1012 1013 1014
    /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
     * here. be sure if you change something here to check mmioAdvance16 as well
     */
1015
    if ((wm = MMIO_Get(hmmio)) == NULL)
1016 1017 1018 1019 1020 1021 1022 1023
	return MMSYSERR_INVALHANDLE;

    if (!wm->info.cchBuffer)
	return MMIOERR_UNBUFFERED;

    if (uFlags != MMIO_READ && uFlags != MMIO_WRITE)
	return MMSYSERR_INVALPARAM;

1024 1025
    if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
    {
1026 1027 1028 1029 1030
        send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 
                     lpmmioinfo->lBufOffset, SEEK_SET, MMIO_PROC_32A);
        send_message(wm->ioProc, &wm->info, MMIOM_WRITE, 
                     (LPARAM)lpmmioinfo->pchBuffer,
                     lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, MMIO_PROC_32A);
1031 1032 1033
        lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
    }
    if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1034 1035
	return MMIOERR_CANNOTWRITE;

1036
    if (lpmmioinfo) {
1037 1038
	wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset + 
                             (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1039
    }
1040 1041
    MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);

1042 1043
    if (lpmmioinfo) {
	lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1044
	lpmmioinfo->pchEndRead  = lpmmioinfo->pchBuffer +
1045
	    (wm->info.pchEndRead - wm->info.pchBuffer);
1046
	lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1047 1048 1049 1050
	    (wm->info.pchEndWrite - wm->info.pchBuffer);
	lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
	lpmmioinfo->lBufOffset = wm->info.lBufOffset;
    }
1051
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1052 1053
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1054
/**************************************************************************
1055
 * 				mmioStringToFOURCCA	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1056
 */
1057
FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1058
{
1059 1060
    CHAR cc[4];
    int i = 0;
1061

1062 1063 1064 1065 1066 1067 1068
    for (i = 0; i < 4 && sz[i]; i++) {
	if (uFlags & MMIO_TOUPPER) {
	    cc[i] = toupper(sz[i]);
	} else {
	    cc[i] = sz[i];
	}
    }
1069

1070
    /* Pad with spaces */
1071
    while (i < 4) cc[i++] = ' ';
1072

1073
    TRACE("Got '%.4s'\n",cc);
1074
    return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
Alexandre Julliard's avatar
Alexandre Julliard committed
1075 1076 1077
}

/**************************************************************************
1078
 * 				mmioStringToFOURCCW	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1079
 */
1080
FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1081
{
1082
    char	szA[4];
1083

1084 1085
    WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
    return mmioStringToFOURCCA(szA,uFlags);
Alexandre Julliard's avatar
Alexandre Julliard committed
1086 1087
}

1088
/**************************************************************************
1089
 * 				mmioInstallIOProcA	   [WINMM.@]
1090
 */
1091
LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1092
				     LPMMIOPROC pIOProc, DWORD dwFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1093
{
1094
    return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32A);
Alexandre Julliard's avatar
Alexandre Julliard committed
1095 1096
}

1097
/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1098
 * 				mmioInstallIOProcW	   [WINMM.@]
1099
 */
1100
LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1101
				     LPMMIOPROC pIOProc, DWORD dwFlags)
1102
{
1103
    return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, MMIO_PROC_32W);
1104 1105
}

1106 1107 1108 1109
/******************************************************************
 *		MMIO_SendMessage
 *
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1110
 */
1111 1112
LRESULT         MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1, 
                                 LPARAM lParam2, enum mmioProcType type)
Alexandre Julliard's avatar
Alexandre Julliard committed
1113
{
1114
    LPWINE_MMIO		wm;
1115

1116
    TRACE("(%p, %u, %ld, %ld, %d)\n", hmmio, uMessage, lParam1, lParam2, type);
1117 1118 1119

    if (uMessage < MMIOM_USER)
	return MMSYSERR_INVALPARAM;
1120

1121
    if ((wm = MMIO_Get(hmmio)) == NULL)
1122
	return MMSYSERR_INVALHANDLE;
1123

1124
    return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, type);
Alexandre Julliard's avatar
Alexandre Julliard committed
1125 1126 1127
}

/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1128
 * 				mmioSendMessage		[WINMM.@]
1129 1130 1131
 */
LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
			       LPARAM lParam1, LPARAM lParam2)
Alexandre Julliard's avatar
Alexandre Julliard committed
1132
{
1133
    return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, MMIO_PROC_32A);
Alexandre Julliard's avatar
Alexandre Julliard committed
1134 1135 1136
}

/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1137
 * 				mmioDescend         	[WINMM.@]
1138
 */
1139 1140
MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
                            const MMCKINFO* lpckParent, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1141
{
1142 1143 1144
    DWORD		dwOldPos;
    FOURCC		srchCkId;
    FOURCC		srchType;
1145 1146


1147
    TRACE("(%p, %p, %p, %04X);\n", hmmio, lpck, lpckParent, uFlags);
1148

1149 1150
    if (lpck == NULL)
	return MMSYSERR_INVALPARAM;
1151

1152 1153
    dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
    TRACE("dwOldPos=%ld\n", dwOldPos);
1154

1155 1156 1157
    if (lpckParent != NULL) {
	TRACE("seek inside parent at %ld !\n", lpckParent->dwDataOffset);
	/* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1158
	if (dwOldPos < lpckParent->dwDataOffset ||
1159 1160 1161
	    dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
	    WARN("outside parent chunk\n");
	    return MMIOERR_CHUNKNOTFOUND;
1162
	}
1163
    }
1164

1165
    /* The SDK docu says 'ckid' is used for all cases. Real World
1166
     * examples disagree -Marcus,990216.
1167
     */
1168

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
    srchType = 0;
    /* find_chunk looks for 'ckid' */
    if (uFlags & MMIO_FINDCHUNK)
	srchCkId = lpck->ckid;
    /* find_riff and find_list look for 'fccType' */
    if (uFlags & MMIO_FINDLIST) {
	srchCkId = FOURCC_LIST;
	srchType = lpck->fccType;
    }
    if (uFlags & MMIO_FINDRIFF) {
	srchCkId = FOURCC_RIFF;
	srchType = lpck->fccType;
    }
1182

1183
    if (uFlags & (MMIO_FINDCHUNK|MMIO_FINDLIST|MMIO_FINDRIFF)) {
1184
	TRACE("searching for %.4s.%.4s\n",
1185
	      (LPSTR)&srchCkId,
1186
	      srchType?(LPSTR)&srchType:"any ");
1187

1188 1189
	while (TRUE) {
	    LONG ix;
1190

1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
	    ix = mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD));
	    if (ix < 2*sizeof(DWORD)) {
		mmioSeek(hmmio, dwOldPos, SEEK_SET);
		WARN("return ChunkNotFound\n");
		return MMIOERR_CHUNKNOTFOUND;
	    }
	    lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
	    if (ix < lpck->dwDataOffset - dwOldPos) {
		mmioSeek(hmmio, dwOldPos, SEEK_SET);
		WARN("return ChunkNotFound\n");
		return MMIOERR_CHUNKNOTFOUND;
	    }
	    TRACE("ckid=%.4s fcc=%.4s cksize=%08lX !\n",
1204
		  (LPSTR)&lpck->ckid,
1205 1206 1207 1208 1209 1210
		  srchType?(LPSTR)&lpck->fccType:"<na>",
		  lpck->cksize);
	    if ((srchCkId == lpck->ckid) &&
		(!srchType || (srchType == lpck->fccType))
		)
		break;
1211

1212 1213
	    dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
	    mmioSeek(hmmio, dwOldPos, SEEK_SET);
Alexandre Julliard's avatar
Alexandre Julliard committed
1214
	}
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
    } else {
	/* FIXME: unverified, does it do this? */
	/* NB: This part is used by WAVE_mciOpen, among others */
	if (mmioRead(hmmio, (LPSTR)lpck, 3 * sizeof(DWORD)) < 3 * sizeof(DWORD)) {
	    mmioSeek(hmmio, dwOldPos, SEEK_SET);
	    WARN("return ChunkNotFound 2nd\n");
	    return MMIOERR_CHUNKNOTFOUND;
	}
	lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
    }
    lpck->dwFlags = 0;
    /* If we were looking for RIFF/LIST chunks, the final file position
     * is after the chunkid. If we were just looking for the chunk
     * it is after the cksize. So add 4 in RIFF/LIST case.
     */
    if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
	mmioSeek(hmmio, lpck->dwDataOffset + sizeof(DWORD), SEEK_SET);
    else
	mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1234 1235
    TRACE("lpck: ckid=%.4s, cksize=%ld, dwDataOffset=%ld fccType=%08lX (%.4s)!\n",
	  (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1236
	  lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1237
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1238 1239
}

1240
/**************************************************************************
1241
 * 				mmioAscend     		[WINMM.@]
1242
 */
1243
MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1244
{
1245
    TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1246

1247
    if (lpck->dwFlags & MMIO_DIRTY) {
1248
	DWORD	dwOldPos, dwNewSize;
1249

1250
	TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1251
	dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1252
	TRACE("dwOldPos=%ld lpck->dwDataOffset = %ld\n", dwOldPos, lpck->dwDataOffset);
1253 1254
	dwNewSize = dwOldPos - lpck->dwDataOffset;
	if (dwNewSize != lpck->cksize) {
1255
	    TRACE("Nope: lpck->cksize=%ld dwNewSize=%ld\n", lpck->cksize, dwNewSize);
1256
	    lpck->cksize = dwNewSize;
1257

1258 1259 1260 1261 1262 1263
	    /* pad odd size with 0 */
	    if (dwNewSize & 1) {
		char ch = 0;
		mmioWrite(hmmio, &ch, 1);
	    }
	    mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1264 1265
	    mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
	}
1266
	lpck->dwFlags = 0;
1267
    }
1268

1269
    mmioSeek(hmmio, lpck->dwDataOffset + ((lpck->cksize + 1) & ~1), SEEK_SET);
1270

1271
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1272 1273
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1274
/**************************************************************************
1275
 * 			mmioCreateChunk				[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1276
 */
1277
MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1278
{
1279 1280 1281
    DWORD	dwOldPos;
    LONG 	size;
    LONG 	ix;
1282

1283
    TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1284

1285 1286
    dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
    TRACE("dwOldPos=%ld\n", dwOldPos);
1287

1288 1289 1290 1291
    if (uFlags == MMIO_CREATELIST)
	lpck->ckid = FOURCC_LIST;
    else if (uFlags == MMIO_CREATERIFF)
	lpck->ckid = FOURCC_RIFF;
1292

1293
    TRACE("ckid=%.4s\n", (LPSTR)&lpck->ckid);
1294

1295 1296
    size = 2 * sizeof(DWORD);
    lpck->dwDataOffset = dwOldPos + size;
1297 1298

    if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1299 1300
	size += sizeof(DWORD);
    lpck->dwFlags = MMIO_DIRTY;
1301

1302 1303 1304 1305 1306 1307 1308
    ix = mmioWrite(hmmio, (LPSTR)lpck, size);
    TRACE("after mmioWrite ix = %ld req = %ld, errno = %d\n",ix, size, errno);
    if (ix < size) {
	mmioSeek(hmmio, dwOldPos, SEEK_SET);
	WARN("return CannotWrite\n");
	return MMIOERR_CANNOTWRITE;
    }
1309

1310
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1311 1312
}

1313
/**************************************************************************
1314
 * 				mmioRenameA    			[WINMM.@]
1315
 */
1316
MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1317
                            const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1318
{
1319 1320
    struct IOProcList*  ioProc = NULL;
    struct IOProcList   tmp;
1321
    FOURCC              fcc;
1322 1323

    TRACE("('%s', '%s', %p, %08lX);\n",
1324
	  debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1325

1326
    /* If both params are NULL, then parse the file name */
1327
    if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1328 1329 1330 1331
    {
	fcc = MMIO_ParseExtA(szFileName);
        if (fcc) ioProc = MMIO_FindProcNode(fcc);
    }
1332 1333

    /* Handle any unhandled/error case from above. Assume DOS file */
1334
    if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1335
	ioProc = MMIO_FindProcNode(FOURCC_DOS);
1336 1337
    /* if just the four character code is present, look up IO proc */
    else if (lpmmioinfo->pIOProc == NULL)
1338
        ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1339
    else /* use relevant ioProc */
1340 1341 1342 1343 1344 1345 1346
    {
        ioProc = &tmp;
        tmp.fourCC = lpmmioinfo->fccIOProc;
        tmp.pIOProc = lpmmioinfo->pIOProc;
        tmp.type = MMIO_PROC_32A;
        tmp.count = 1;
    }
1347

1348 1349 1350
    /* FIXME: should we actually pass lpmmioinfo down the drain ???
     * or make a copy of it because it's const ???
     */
1351
    return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1352
                        (LPARAM)szFileName, (LPARAM)szNewFileName, MMIO_PROC_32A);
1353 1354 1355
}

/**************************************************************************
1356
 * 				mmioRenameW    			[WINMM.@]
1357
 */
1358
MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1359
                            const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1360
{
1361 1362
    LPSTR	szFn = NULL;
    LPSTR	sznFn = NULL;
1363
    UINT	ret = MMSYSERR_NOMEM;
1364 1365 1366 1367 1368 1369
    INT         len;

    if (szFileName)
    {
        len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
        szFn = HeapAlloc( GetProcessHeap(), 0, len );
1370
        if (!szFn) goto done;
1371 1372 1373 1374 1375 1376
        WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
    }
    if (szNewFileName)
    {
        len = WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, NULL, 0, NULL, NULL );
        sznFn = HeapAlloc( GetProcessHeap(), 0, len );
1377
        if (!sznFn) goto done;
1378 1379 1380 1381
        WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
    }

    ret = mmioRenameA(szFn, sznFn, lpmmioinfo, dwFlags);
1382

1383
done:
1384 1385 1386
    HeapFree(GetProcessHeap(),0,szFn);
    HeapFree(GetProcessHeap(),0,sznFn);
    return ret;
1387
}