mmio.c 40.4 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
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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
static WINE_MMIO *MMIOList;

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

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

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

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

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

    case MMIOM_CLOSE:
89 90 91 92 93 94 95 96
	/* 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;
97 98

    case MMIOM_READ:
99 100 101 102 103 104 105 106 107
	/* 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;
108

109
	break;
110

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

115 116 117 118 119 120 121 122 123 124
	/* 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;
125 126

    case MMIOM_SEEK:
127 128 129 130 131 132 133 134 135
	/* 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;
136 137

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

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

152
    return ret;
153 154
}

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

163
    switch (uMessage) {
164 165

    case MMIOM_OPEN:
166 167
	/* Parameters:
	 * lParam1 = filename (must be NULL)
168
	 * lParam2 = reserved
169 170 171 172 173 174
	 * 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;
175
        lpmmioinfo->adwInfo[0] = HFILE_ERROR;
176
	return 0;
177 178

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

    case MMIOM_READ:
187 188 189 190 191 192 193 194 195
	/* 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;
196

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

201 202 203 204 205 206 207 208 209
	/* 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;
210 211

    case MMIOM_SEEK:
212 213 214 215 216 217 218 219
	/* 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;
220

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

227 228 229
/* 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
230

231 232 233 234
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
235

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

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

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

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

265
    TRACE("(%08x, %p, %08X, %i)\n", fccIOProc, pIOProc, dwFlags, type);
266 267 268 269 270 271 272 273 274 275 276 277 278 279

    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;
280 281
	    pListNode->count = 0;

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

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

291
    case MMIO_REMOVEPROC:
292
	/*
293 294 295 296
	 * 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
297 298
	 * since the first two items on this list are ones we won't
	 * let the user delete anyway, that's okay
299
	 */
300
	ppListNode = &pIOProcListAnchor;
301 302 303 304
	while ((*ppListNode) && 
               ((fccIOProc != 0) ? 
                (*ppListNode)->fourCC != fccIOProc : 
                (*ppListNode)->pIOProc != pIOProc))
305 306 307 308 309 310 311 312 313 314 315
	    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 */
316
	    if ((*ppListNode) >= defaultProcs &&
317 318 319 320
		(*ppListNode) < defaultProcs + sizeof(defaultProcs)) {
		WARN("Tried to remove built-in mmio proc. Skipping\n");
	    } else {
		/* Okay, nuke it */
321
		struct IOProcList*  ptmpNode = *ppListNode;
322 323
		lpProc = (*ppListNode)->pIOProc;
		*ppListNode = (*ppListNode)->pNext;
324
		HeapFree(GetProcessHeap(), 0, ptmpNode);
325 326 327
	    }
	}
	break;
328

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

336 337
    return lpProc;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
338

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

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

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

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

    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
379 380
}

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

394
    FOURCC ret = 0;
395

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

400
    TRACE("(%s)\n", debugstr_a(szFileName));
401

402 403
    if (!szFileName)
	return ret;
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 430 431
    /* 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);
432 433
    }
    return ret;
434 435
}

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

445 446
    EnterCriticalSection(&WINMM_cs);
    for (wm = MMIOList; wm; wm = wm->lpNext) {
447 448 449
	if (wm->info.hmmio == h)
	    break;
    }
450
    LeaveCriticalSection(&WINMM_cs);
451 452
    return wm;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
453

454 455 456 457 458 459 460 461 462 463 464 465
/**************************************************************************
 *				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) {
466
	EnterCriticalSection(&WINMM_cs);
467
        /* lookup next unallocated WORD handle, with a non NULL value */
468 469
	while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
	wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
470 471 472
	wm->lpNext = MMIOList;
	MMIOList = wm;
	LeaveCriticalSection(&WINMM_cs);
473 474 475
    }
    return wm;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
476

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

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

500 501 502
/****************************************************************
 *       		MMIO_Flush 			[INTERNAL]
 */
503
static	MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
504
{
505 506 507
    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) {
508
            /* FIXME: error handling */
509 510 511 512 513
	    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);
514
	}
515
	if (uFlags & MMIO_EMPTYBUF)
516
	    wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
517
    }
518
    wm->info.dwFlags &= ~MMIO_DIRTY;
519

520
    return MMSYSERR_NOERROR;
521
}
Alexandre Julliard's avatar
Alexandre Julliard committed
522

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

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

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

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

549 550 551 552 553 554
    return size;
}

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

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

564
    if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
565
	return MMIOERR_CANNOTWRITE;
566

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

574
    if (pchBuffer) {
575
        wm->info.pchBuffer = pchBuffer;
576
    } else if (cchBuffer) {
577
	if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
578 579 580 581 582 583 584 585 586 587 588
	    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;
589
    wm->bBufferLoaded = FALSE;
590

591
    return MMSYSERR_NOERROR;
592 593 594
}

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

603
    TRACE("('%s', %p, %08X, %d);\n", szFileName, refmminfo, dwOpenFlags, type);
604

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

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

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

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

626 627 628
    if ((wm = MMIO_Create()) == NULL)
	return 0;

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

659
    wm->bBufferLoaded = FALSE;
660
    wm->ioProc->count++;
661

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

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

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

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

683 684 685 686 687 688 689
    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
690 691
}

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

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

709
    ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, MMIO_PROC_32W);
710 711 712

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

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

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

732
    TRACE("(%p, %04X);\n", hmmio, uFlags);
733

734
    if ((wm = MMIO_Get(hmmio)) == NULL)
735
	return MMSYSERR_INVALHANDLE;
736

737
    if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
738
	return result;
739

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

743
    MMIO_SetBuffer(wm, NULL, 0, 0);
744

745
    wm->ioProc->count--;
Alexandre Julliard's avatar
Alexandre Julliard committed
746

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

751
    MMIO_Destroy(wm);
Alexandre Julliard's avatar
Alexandre Julliard committed
752

753
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
754 755 756
}

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

764
    TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
765

766
    if ((wm = MMIO_Get(hmmio)) == NULL)
767 768 769 770
	return -1;

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

    /* 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;
784

785 786 787 788 789 790 791 792 793 794 795 796 797 798
    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
799
	}
800
    }
801

802
    TRACE("count=%d\n", count);
803
    return count;
Alexandre Julliard's avatar
Alexandre Julliard committed
804 805 806
}

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

814
    TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
815

816
    if ((wm = MMIO_Get(hmmio)) == NULL)
817
	return -1;
818

819
    if (wm->info.cchBuffer) {
820 821 822 823 824 825 826 827 828 829
	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;
830
                cch -= count;
831
                bytesW += count;
832
                wm->info.dwFlags |= MMIO_DIRTY;
833 834 835 836
	    } else {
                if (wm->info.fccIOProc == FOURCC_MEM) {
                    if (wm->info.adwInfo[0]) {
                        /* from where would we get the memory handle? */
837
                        FIXME("memory file expansion not implemented!\n");
838
                        break;
839
		    } else break;
840
                }
841
            }
842

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

857
    TRACE("bytes written=%d\n", count);
858
    return count;
Alexandre Julliard's avatar
Alexandre Julliard committed
859 860
}

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

869
    TRACE("(%p, %08X, %d);\n", hmmio, lOffset, iOrigin);
870

871
    if ((wm = MMIO_Get(hmmio)) == NULL)
872 873
	return MMSYSERR_INVALHANDLE;

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

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

893 894 895 896 897 898 899 900 901 902 903
    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) &&
904
	((offset < wm->info.lBufOffset) ||
905 906
	 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
	 !wm->bBufferLoaded)) {
907 908
        /* stay in same buffer ? */
        /* some memory mapped buffers are defined with -1 as a size */
909 910

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

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

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

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

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

936
    if ((wm = MMIO_Get(hmmio)) == NULL)
937 938 939
	return MMSYSERR_INVALHANDLE;

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

944
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
945 946 947
}

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

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

956
    if ((wm = MMIO_Get(hmmio)) == NULL)
957
	return MMSYSERR_INVALHANDLE;
958

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

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

971
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
972 973 974
}

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

981
    TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%d, uFlags=%#08x)\n",
982
	  hmmio, pchBuffer, cchBuffer, uFlags);
983

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

987
    return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
988 989
}

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

997
    TRACE("(%p, %04X)\n", hmmio, uFlags);
998

999
    if ((wm = MMIO_Get(hmmio)) == NULL)
1000
	return MMSYSERR_INVALHANDLE;
1001

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

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

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

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

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

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

1026 1027
    if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
    {
1028 1029 1030 1031 1032
        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);
1033 1034 1035
        lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
    }
    if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1036 1037
	return MMIOERR_CANNOTWRITE;

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

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

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

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

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

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

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

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

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

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

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

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

    if (uMessage < MMIOM_USER)
	return MMSYSERR_INVALPARAM;
1122

1123
    if ((wm = MMIO_Get(hmmio)) == NULL)
1124
	return MMSYSERR_INVALHANDLE;
1125

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

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

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

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

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

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

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

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

1170
    srchCkId = 0;
1171
    srchType = 0;
1172

1173 1174 1175
    /* find_chunk looks for 'ckid' */
    if (uFlags & MMIO_FINDCHUNK)
	srchCkId = lpck->ckid;
1176

1177
    /* find_riff and find_list look for 'fccType' */
1178 1179
    if (uFlags & MMIO_FINDLIST)
    {
1180
	srchCkId = FOURCC_LIST;
1181
        srchType = lpck->fccType;
1182
    }
1183 1184 1185

    if (uFlags & MMIO_FINDRIFF)
    {
1186
	srchCkId = FOURCC_RIFF;
1187
        srchType = lpck->fccType;
1188
    }
1189

1190 1191
    TRACE("searching for %4.4s.%4.4s\n",
          (LPCSTR)&srchCkId, srchType ? (LPCSTR)&srchType : "any");
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    while (TRUE)
    {
        LONG ix;

        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;
        }
1204

1205
        lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1206
        TRACE("ckid=%4.4s fcc=%4.4s cksize=%08X !\n",
1207 1208 1209 1210 1211 1212 1213 1214 1215
              (LPCSTR)&lpck->ckid,
              srchType ? (LPCSTR)&lpck->fccType:"<na>",
              lpck->cksize);
        if ( (!srchCkId || (srchCkId == lpck->ckid)) &&
             (!srchType || (srchType == lpck->fccType)) )
            break;

        dwOldPos = lpck->dwDataOffset + ((lpck->cksize + 1) & ~1);
        mmioSeek(hmmio, dwOldPos, SEEK_SET);
1216
    }
1217

1218 1219 1220 1221 1222 1223 1224 1225 1226
    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);
1227
    TRACE("lpck: ckid=%.4s, cksize=%d, dwDataOffset=%d fccType=%08X (%.4s)!\n",
1228
	  (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1229
	  lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1230
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1231 1232
}

1233
/**************************************************************************
1234
 * 				mmioAscend     		[WINMM.@]
1235
 */
1236
MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1237
{
1238
    TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1239

1240
    if (lpck->dwFlags & MMIO_DIRTY) {
1241
	DWORD	dwOldPos, dwNewSize;
1242

1243
	TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1244
	dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1245
	TRACE("dwOldPos=%d lpck->dwDataOffset = %d\n", dwOldPos, lpck->dwDataOffset);
1246 1247
	dwNewSize = dwOldPos - lpck->dwDataOffset;
	if (dwNewSize != lpck->cksize) {
1248
	    TRACE("Nope: lpck->cksize=%d dwNewSize=%d\n", lpck->cksize, dwNewSize);
1249
	    lpck->cksize = dwNewSize;
1250

1251 1252 1253 1254 1255 1256
	    /* pad odd size with 0 */
	    if (dwNewSize & 1) {
		char ch = 0;
		mmioWrite(hmmio, &ch, 1);
	    }
	    mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1257 1258
	    mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
	}
1259
	lpck->dwFlags = 0;
1260
    }
1261

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

1264
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1265 1266
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1267
/**************************************************************************
1268
 * 			mmioCreateChunk				[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1269
 */
1270
MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1271
{
1272 1273 1274
    DWORD	dwOldPos;
    LONG 	size;
    LONG 	ix;
1275

1276
    TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1277

1278
    dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1279
    TRACE("dwOldPos=%d\n", dwOldPos);
1280

1281 1282 1283 1284
    if (uFlags == MMIO_CREATELIST)
	lpck->ckid = FOURCC_LIST;
    else if (uFlags == MMIO_CREATERIFF)
	lpck->ckid = FOURCC_RIFF;
1285

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

1288 1289
    size = 2 * sizeof(DWORD);
    lpck->dwDataOffset = dwOldPos + size;
1290 1291

    if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1292 1293
	size += sizeof(DWORD);
    lpck->dwFlags = MMIO_DIRTY;
1294

1295
    ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1296
    TRACE("after mmioWrite ix = %d req = %d, errno = %d\n", ix, size, errno);
1297 1298 1299 1300 1301
    if (ix < size) {
	mmioSeek(hmmio, dwOldPos, SEEK_SET);
	WARN("return CannotWrite\n");
	return MMIOERR_CANNOTWRITE;
    }
1302

1303
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1304 1305
}

1306
/**************************************************************************
1307
 * 				mmioRenameA    			[WINMM.@]
1308
 */
1309
MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1310
                            const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1311
{
1312 1313
    struct IOProcList*  ioProc = NULL;
    struct IOProcList   tmp;
1314
    FOURCC              fcc;
1315

1316
    TRACE("('%s', '%s', %p, %08X);\n",
1317
	  debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1318

1319
    /* If both params are NULL, then parse the file name */
1320
    if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1321 1322 1323 1324
    {
	fcc = MMIO_ParseExtA(szFileName);
        if (fcc) ioProc = MMIO_FindProcNode(fcc);
    }
1325 1326

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

1341 1342 1343
    /* FIXME: should we actually pass lpmmioinfo down the drain ???
     * or make a copy of it because it's const ???
     */
1344
    return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1345
                        (LPARAM)szFileName, (LPARAM)szNewFileName, MMIO_PROC_32A);
1346 1347 1348
}

/**************************************************************************
1349
 * 				mmioRenameW    			[WINMM.@]
1350
 */
1351
MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1352
                            const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1353
{
1354 1355
    LPSTR	szFn = NULL;
    LPSTR	sznFn = NULL;
1356
    UINT	ret = MMSYSERR_NOMEM;
1357 1358 1359 1360 1361 1362
    INT         len;

    if (szFileName)
    {
        len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
        szFn = HeapAlloc( GetProcessHeap(), 0, len );
1363
        if (!szFn) goto done;
1364 1365 1366 1367 1368 1369
        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 );
1370
        if (!sznFn) goto done;
1371 1372 1373 1374
        WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
    }

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

1376
done:
1377 1378 1379
    HeapFree(GetProcessHeap(),0,szFn);
    HeapFree(GetProcessHeap(),0,sznFn);
    return ret;
1380
}