mmio.c 39.8 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * MMIO functions
 *
 * Copyright 1998 Andrew Taylor
5
 * Copyright 1998 Ove Kåven
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 49
static WINE_MMIO *MMIOList;

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
static struct IOProcList defaultProcs[] = {
230 231
    {&defaultProcs[1], FOURCC_DOS, (LPMMIOPROC)mmioDosIOProc, FALSE, 0},
    {NULL,             FOURCC_MEM, (LPMMIOPROC)mmioMemIOProc, FALSE, 0},
232
};
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
static LPMMIOPROC MMIO_InstallIOProc(FOURCC fccIOProc, LPMMIOPROC pIOProc,
257
                                     DWORD dwFlags, BOOL is_unicode)
258 259 260 261 262
{
    LPMMIOPROC	        lpProc = NULL;
    struct IOProcList*  pListNode;
    struct IOProcList** ppListNode;

263
    TRACE("(%08x, %p, %08X, %s)\n", fccIOProc, pIOProc, dwFlags, is_unicode ? "unicode" : "ansi");
264 265 266 267 268 269 270 271 272 273 274 275 276

    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;
277
	    pListNode->is_unicode = is_unicode;
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
		(*ppListNode) < defaultProcs + sizeof(defaultProcs) / sizeof(defaultProcs[0])) {
316 317 318
		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
static LRESULT	send_message(struct IOProcList* ioProc, LPMMIOINFO mmioinfo,
                             DWORD wMsg, LPARAM lParam1,
342
                             LPARAM lParam2, BOOL is_unicode)
343
{
344
    LRESULT 		result = MMSYSERR_ERROR;
345 346
    LPARAM		lp1 = lParam1, lp2 = lParam2;

347
    if (!ioProc) {
348 349
	ERR("ioProc NULL\n");
	return MMSYSERR_INVALPARAM;
350 351
    }

352 353 354 355 356
    if (ioProc->is_unicode != is_unicode) {
        /* map (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
        FIXME("NIY 32 A<=>W mapping\n");
    }
    result = (ioProc->pIOProc)((LPSTR)mmioinfo, wMsg, lp1, lp2);
357

358
#if 0
359
    if (ioProc->is_unicode != is_unicode) {
360
	    /* unmap (lParam1, lParam2) into (lp1, lp2) 32 A<=>W */
361 362 363 364
	}
#endif

    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
365 366
}

367
/**************************************************************************
368
 *      			MMIO_ParseExtA 		        [internal]
369 370 371 372 373 374
 *
 * Parses a filename for the extension.
 *
 * RETURNS
 *  The FOURCC code for the extension if found, else 0.
 */
375
static FOURCC MMIO_ParseExtA(LPCSTR szFileName)
376
{
377 378
    /* Filenames are of the form file.ext{+ABC}
       For now, we take the last '+' if present */
379

380
    FOURCC ret = 0;
381

382 383
    /* Note that ext{Start,End} point to the . and + respectively */
    LPSTR extEnd;
384
    LPSTR extStart;
385

386
    TRACE("(%s)\n", debugstr_a(szFileName));
387

388 389
    if (!szFileName)
	return ret;
390

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    /* 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);
418 419
    }
    return ret;
420 421
}

Alexandre Julliard's avatar
Alexandre Julliard committed
422
/**************************************************************************
423 424
 *				MMIO_Get			[internal]
 *
Andreas Mohr's avatar
Andreas Mohr committed
425
 * Retrieves the mmio object from current process
Alexandre Julliard's avatar
Alexandre Julliard committed
426
 */
427
static LPWINE_MMIO      MMIO_Get(HMMIO h)
Alexandre Julliard's avatar
Alexandre Julliard committed
428
{
429
    LPWINE_MMIO		wm = NULL;
430

431 432
    EnterCriticalSection(&WINMM_cs);
    for (wm = MMIOList; wm; wm = wm->lpNext) {
433 434 435
	if (wm->info.hmmio == h)
	    break;
    }
436
    LeaveCriticalSection(&WINMM_cs);
437 438
    return wm;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
439

440 441 442 443 444 445 446 447 448 449 450 451
/**************************************************************************
 *				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) {
452
	EnterCriticalSection(&WINMM_cs);
453
        /* lookup next unallocated WORD handle, with a non NULL value */
454 455
	while (++MMIO_counter == 0 || MMIO_Get((HMMIO)(ULONG_PTR)MMIO_counter));
	wm->info.hmmio = (HMMIO)(ULONG_PTR)MMIO_counter;
456 457 458
	wm->lpNext = MMIOList;
	MMIOList = wm;
	LeaveCriticalSection(&WINMM_cs);
459 460 461
    }
    return wm;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
462

463 464 465 466 467 468 469 470 471
/**************************************************************************
 *				MMIO_Destroy			[internal]
 *-
 * Destroys an internal representation for a mmio instance
 */
static	BOOL		MMIO_Destroy(LPWINE_MMIO wm)
{
    LPWINE_MMIO*	m;

472
    EnterCriticalSection(&WINMM_cs);
473
    /* search for the matching one... */
474
    m = &MMIOList;
475 476
    while (*m && *m != wm) m = &(*m)->lpNext;
    /* ...and destroy */
477 478
    if (*m) {
	*m = (*m)->lpNext;
479
	HeapFree(GetProcessHeap(), 0, wm);
480 481
	wm = NULL;
    }
482
    LeaveCriticalSection(&WINMM_cs);
483 484
    return wm ? FALSE : TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
485

486 487 488
/****************************************************************
 *       		MMIO_Flush 			[INTERNAL]
 */
489
static	MMRESULT MMIO_Flush(WINE_MMIO* wm, UINT uFlags)
490
{
491 492 493
    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) {
494
            /* FIXME: error handling */
495 496
	    send_message(wm->ioProc, &wm->info, MMIOM_SEEK, wm->info.lBufOffset, SEEK_SET, FALSE);
	    send_message(wm->ioProc, &wm->info, MMIOM_WRITE,
497
                         (LPARAM)wm->info.pchBuffer,
498
                         wm->info.pchNext - wm->info.pchBuffer, FALSE);
499
	}
500
	if (uFlags & MMIO_EMPTYBUF)
501
	    wm->info.pchNext = wm->info.pchEndRead = wm->info.pchBuffer;
502
    }
503
    wm->info.dwFlags &= ~MMIO_DIRTY;
504

505
    return MMSYSERR_NOERROR;
506
}
Alexandre Julliard's avatar
Alexandre Julliard committed
507

508 509 510 511 512 513 514
/***************************************************************************
 *       		MMIO_GrabNextBuffer			[INTERNAL]
 */
static LONG	MMIO_GrabNextBuffer(LPWINE_MMIO wm, int for_read)
{
    LONG	size = wm->info.cchBuffer;

515
    TRACE("bo=%x do=%x of=%lx\n",
516
	  wm->info.lBufOffset, wm->info.lDiskOffset,
517
	  send_message(wm->ioProc, &wm->info, MMIOM_SEEK, 0, SEEK_CUR, FALSE));
518

519 520 521
    wm->info.lBufOffset = wm->info.lDiskOffset;
    wm->info.pchNext = wm->info.pchBuffer;
    wm->info.pchEndRead = wm->info.pchBuffer;
522
    wm->info.pchEndWrite = wm->info.pchBuffer + wm->info.cchBuffer;
523

524
    wm->bBufferLoaded = TRUE;
525
    if (for_read) {
526 527
	size = send_message(wm->ioProc, &wm->info, MMIOM_READ,
                            (LPARAM)wm->info.pchBuffer, size, FALSE);
528 529
	if (size > 0)
	    wm->info.pchEndRead += size;
530 531
        else
            wm->bBufferLoaded = FALSE;
532
    }
533

534 535 536 537 538 539
    return size;
}

/***************************************************************************
 *       		MMIO_SetBuffer 				[INTERNAL]
 */
540
static MMRESULT MMIO_SetBuffer(WINE_MMIO* wm, void* pchBuffer, LONG cchBuffer,
541
			       UINT uFlags)
542
{
543
    TRACE("(%p %p %d %u)\n", wm, pchBuffer, cchBuffer, uFlags);
544

545
    if (cchBuffer > 0xFFFF)
546
	WARN("Untested handling of huge mmio buffers (%d >= 64k)\n", cchBuffer);
547

548
    if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
549
	return MMIOERR_CANNOTWRITE;
550

551 552 553 554
    /* free previous buffer if allocated */
    if (wm->info.dwFlags & MMIO_ALLOCBUF) {
        HeapFree(GetProcessHeap(), 0, wm->info.pchBuffer);
        wm->info.pchBuffer = NULL;
555 556
	wm->info.dwFlags &= ~MMIO_ALLOCBUF;
    }
557

558
    if (pchBuffer) {
559
        wm->info.pchBuffer = pchBuffer;
560
    } else if (cchBuffer) {
561
	if (!(wm->info.pchBuffer = HeapAlloc(GetProcessHeap(), 0, cchBuffer)))
562 563 564 565 566 567 568 569 570 571
	    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;
572
    wm->info.lBufOffset = wm->info.lDiskOffset;
573
    wm->bBufferLoaded = FALSE;
574

575
    return MMSYSERR_NOERROR;
576 577 578
}

/**************************************************************************
579
 * 			MMIO_Open       			[internal]
580
 */
581
static HMMIO MMIO_Open(LPSTR szFileName, MMIOINFO* refmminfo, DWORD dwOpenFlags, BOOL is_unicode)
582 583
{
    LPWINE_MMIO		wm;
584
    MMIOINFO    	mmioinfo;
585

586
    TRACE("('%s', %p, %08X, %s);\n", szFileName, refmminfo, dwOpenFlags, is_unicode ? "unicode" : "ansi");
587

588 589 590 591 592 593 594
    if (!refmminfo) {
        refmminfo = &mmioinfo;

	mmioinfo.fccIOProc = 0;
	mmioinfo.pIOProc = NULL;
	mmioinfo.pchBuffer = NULL;
	mmioinfo.cchBuffer = 0;
595
        is_unicode = FALSE;
596 597
    }

598 599
    if (dwOpenFlags & (MMIO_PARSE|MMIO_EXIST)) {
	char	buffer[MAX_PATH];
600

601 602
	if (!szFileName)
	    return (HMMIO)FALSE;
603
	if (GetFullPathNameA(szFileName, sizeof(buffer), buffer, NULL) >= sizeof(buffer))
604
	    return (HMMIO)FALSE;
605
	if ((dwOpenFlags & MMIO_EXIST) && (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES))
606 607 608 609
	    return (HMMIO)FALSE;
	strcpy(szFileName, buffer);
	return (HMMIO)TRUE;
    }
610

611 612 613
    if ((wm = MMIO_Create()) == NULL)
	return 0;

614
    /* If both params are NULL, then parse the file name if available */
615
    if (refmminfo->fccIOProc == 0 && refmminfo->pIOProc == NULL) {
616
	wm->info.fccIOProc = MMIO_ParseExtA(szFileName);
617 618 619
	/* Handle any unhandled/error case. Assume DOS file */
	if (wm->info.fccIOProc == 0)
	    wm->info.fccIOProc = FOURCC_DOS;
620 621 622 623 624 625
	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;
	}
626 627 628
	wm->bTmpIOProc = FALSE;
    }
    /* if just the four character code is present, look up IO proc */
629
    else if (refmminfo->pIOProc == NULL) {
630 631 632
	wm->info.fccIOProc = refmminfo->fccIOProc;
	if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
	wm->bTmpIOProc = FALSE;
633
    }
634 635 636
    /* if IO proc specified, use it and specified four character code */
    else {
	wm->info.fccIOProc = refmminfo->fccIOProc;
637
	MMIO_InstallIOProc(wm->info.fccIOProc, refmminfo->pIOProc,
638
                           MMIO_INSTALLPROC, is_unicode);
639 640 641 642
	if (!(wm->ioProc = MMIO_FindProcNode(wm->info.fccIOProc))) goto error2;
	assert(wm->ioProc->pIOProc == refmminfo->pIOProc);
	wm->bTmpIOProc = TRUE;
    }
643

644
    wm->ioProc->count++;
645
    wm->info.dwFlags = dwOpenFlags;
646

647
    if (dwOpenFlags & MMIO_ALLOCBUF) {
648 649 650
	refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer,
	    refmminfo->cchBuffer ? refmminfo->cchBuffer : MMIO_DEFAULTBUFFER, 0);
	if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
651
	    goto error1;
652
    } else {
653
        refmminfo->wErrorRet = MMIO_SetBuffer(wm, refmminfo->pchBuffer, refmminfo->cchBuffer, 0);
654 655
	if (refmminfo->wErrorRet != MMSYSERR_NOERROR)
	    goto error1;
656
    }
657

658 659 660
    if (wm->info.fccIOProc == FOURCC_MEM && !(wm->info.dwFlags & MMIO_ALLOCBUF))
        wm->bBufferLoaded = TRUE;

661 662
    /* see mmioDosIOProc for that one */
    wm->info.adwInfo[0] = refmminfo->adwInfo[0];
663

664
    /* call IO proc to actually open file */
665 666
    refmminfo->wErrorRet = send_message(wm->ioProc, &wm->info, MMIOM_OPEN,
                                        (LPARAM)szFileName, 0, FALSE);
667 668

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

671 672 673 674 675 676 677
    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
678 679
}

Alexandre Julliard's avatar
Alexandre Julliard committed
680
/**************************************************************************
681
 * 				mmioOpenW       		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
682
 */
683
HMMIO WINAPI mmioOpenW(LPWSTR szFileName, MMIOINFO* lpmmioinfo,
684
		       DWORD dwOpenFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
685
{
686
    HMMIO 	ret;
687 688 689 690 691 692
    LPSTR	szFn = NULL;

    if (szFileName)
    {
        INT     len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
        szFn = HeapAlloc( GetProcessHeap(), 0, len );
693
        if (!szFn) return NULL;
694 695
        WideCharToMultiByte( CP_ACP, 0, szFileName, -1, szFn, len, NULL, NULL );
    }
696

697
    ret = MMIO_Open(szFn, lpmmioinfo, dwOpenFlags, TRUE);
698 699 700

    HeapFree(GetProcessHeap(), 0, szFn);
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
701 702 703
}

/**************************************************************************
704
 * 				mmioOpenA       		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
705
 */
706
HMMIO WINAPI mmioOpenA(LPSTR szFileName, MMIOINFO* lpmmioinfo,
707
		       DWORD dwOpenFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
708
{
709
    return  MMIO_Open(szFileName, lpmmioinfo, dwOpenFlags, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
710 711
}

Alexandre Julliard's avatar
Alexandre Julliard committed
712
/**************************************************************************
713
 * 				mmioClose      		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
714
 */
715
MMRESULT WINAPI mmioClose(HMMIO hmmio, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
716
{
717 718
    LPWINE_MMIO	wm;
    MMRESULT 	result;
719

720
    TRACE("(%p, %04X);\n", hmmio, uFlags);
721

722
    if ((wm = MMIO_Get(hmmio)) == NULL)
723
	return MMSYSERR_INVALHANDLE;
724

725
    if ((result = MMIO_Flush(wm, 0)) != MMSYSERR_NOERROR)
726
	return result;
727

728
    result = send_message(wm->ioProc, &wm->info, MMIOM_CLOSE, uFlags, 0, FALSE);
729

730
    MMIO_SetBuffer(wm, NULL, 0, 0);
731

732
    wm->ioProc->count--;
Alexandre Julliard's avatar
Alexandre Julliard committed
733

734
    if (wm->bTmpIOProc)
735
	MMIO_InstallIOProc(wm->info.fccIOProc, wm->ioProc->pIOProc,
736
                           MMIO_REMOVEPROC, wm->ioProc->is_unicode);
Alexandre Julliard's avatar
Alexandre Julliard committed
737

738
    MMIO_Destroy(wm);
Alexandre Julliard's avatar
Alexandre Julliard committed
739

740
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
741 742 743
}

/**************************************************************************
744
 * 				mmioRead	       	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
745
 */
746
LONG WINAPI mmioRead(HMMIO hmmio, HPSTR pch, LONG cch)
Alexandre Julliard's avatar
Alexandre Julliard committed
747
{
748 749
    LPWINE_MMIO	wm;
    LONG 	count;
750

751
    TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
752

753
    if ((wm = MMIO_Get(hmmio)) == NULL)
754 755 756 757
	return -1;

    /* unbuffered case first */
    if (!wm->info.pchBuffer)
758
	return send_message(wm->ioProc, &wm->info, MMIOM_READ, (LPARAM)pch, cch, FALSE);
759 760 761 762 763 764 765 766 767 768 769

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

771 772 773 774 775 776 777 778 779 780 781 782 783 784
    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
785
	}
786
    }
787

788
    TRACE("count=%d\n", count);
789
    return count;
Alexandre Julliard's avatar
Alexandre Julliard committed
790 791 792
}

/**************************************************************************
793
 * 				mmioWrite      		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
794
 */
795
LONG WINAPI mmioWrite(HMMIO hmmio, HPCSTR pch, LONG cch)
Alexandre Julliard's avatar
Alexandre Julliard committed
796
{
797
    LPWINE_MMIO	wm;
798
    LONG	count;
799

800
    TRACE("(%p, %p, %d);\n", hmmio, pch, cch);
801

802
    if ((wm = MMIO_Get(hmmio)) == NULL)
803
	return -1;
804

805
    if (wm->info.cchBuffer) {
806 807 808 809 810 811 812 813 814 815
	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;
816
                cch -= count;
817
                bytesW += count;
818
                wm->info.dwFlags |= MMIO_DIRTY;
819 820 821 822
	    } else {
                if (wm->info.fccIOProc == FOURCC_MEM) {
                    if (wm->info.adwInfo[0]) {
                        /* from where would we get the memory handle? */
823
                        FIXME("memory file expansion not implemented!\n");
824
                        break;
825
		    } else break;
826
                }
827
            }
828

829 830 831 832 833
            if (wm->info.pchNext == wm->info.pchEndWrite)
            {
                MMIO_Flush(wm, MMIO_EMPTYBUF);
                MMIO_GrabNextBuffer(wm, FALSE);
            }
834
            else break;
835 836
        }
	count = bytesW;
837
    } else {
838
	count = send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)pch, cch, FALSE);
839 840
	wm->info.lBufOffset = wm->info.lDiskOffset;
    }
841

842
    TRACE("bytes written=%d\n", count);
843
    return count;
Alexandre Julliard's avatar
Alexandre Julliard committed
844 845
}

Alexandre Julliard's avatar
Alexandre Julliard committed
846
/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
847
 * 				mmioSeek		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
848
 */
849
LONG WINAPI mmioSeek(HMMIO hmmio, LONG lOffset, INT iOrigin)
Alexandre Julliard's avatar
Alexandre Julliard committed
850
{
851 852
    LPWINE_MMIO	wm;
    LONG 	offset;
Alexandre Julliard's avatar
Alexandre Julliard committed
853

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

856
    if ((wm = MMIO_Get(hmmio)) == NULL)
857 858
	return MMSYSERR_INVALHANDLE;

859
    /* not buffered, direct seek on file */
860
    if (!wm->info.pchBuffer)
861
	return send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lOffset, iOrigin, FALSE);
862 863

    switch (iOrigin) {
864
    case SEEK_SET:
865 866
	offset = lOffset;
	break;
867
    case SEEK_CUR:
868 869
	offset = wm->info.lBufOffset + (wm->info.pchNext - wm->info.pchBuffer) + lOffset;
	break;
870
    case SEEK_END:
871
	offset = ((wm->info.fccIOProc == FOURCC_MEM)? wm->info.cchBuffer : wm->dwFileSize) - lOffset;
872 873 874 875 876
	break;
    default:
	return -1;
    }

877 878 879 880 881 882 883 884 885 886 887
    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) &&
888
	((offset < wm->info.lBufOffset) ||
889 890
	 (offset >= wm->info.lBufOffset + wm->info.cchBuffer) ||
	 !wm->bBufferLoaded)) {
891 892
        /* stay in same buffer ? */
        /* some memory mapped buffers are defined with -1 as a size */
893 894

	/* condition to change buffer */
895
	if ((wm->info.fccIOProc == FOURCC_MEM) ||
896
	    MMIO_Flush(wm, 0) != MMSYSERR_NOERROR ||
897
	    /* this also sets the wm->info.lDiskOffset field */
898 899
	    send_message(wm->ioProc, &wm->info, MMIOM_SEEK,
                         (offset / wm->info.cchBuffer) * wm->info.cchBuffer,
900
                         SEEK_SET, FALSE) == -1)
901 902 903 904 905 906
	    return -1;
	MMIO_GrabNextBuffer(wm, TRUE);
    }

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

907
    TRACE("=> %d\n", offset);
908
    return offset;
Alexandre Julliard's avatar
Alexandre Julliard committed
909 910
}

Alexandre Julliard's avatar
Alexandre Julliard committed
911
/**************************************************************************
912
 * 				mmioGetInfo	       	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
913
 */
914
MMRESULT WINAPI mmioGetInfo(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
915
{
916
    LPWINE_MMIO		wm;
917

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

920
    if ((wm = MMIO_Get(hmmio)) == NULL)
921 922
	return MMSYSERR_INVALHANDLE;

923
    *lpmmioinfo = wm->info;
924

925
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
926 927 928
}

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

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

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

940
    /* check pointers coherence */
941
    if (lpmmioinfo->pchNext < wm->info.pchBuffer ||
942
	lpmmioinfo->pchNext > wm->info.pchBuffer + wm->info.cchBuffer ||
943
	lpmmioinfo->pchEndRead < wm->info.pchBuffer ||
944
	lpmmioinfo->pchEndRead > wm->info.pchBuffer + wm->info.cchBuffer ||
945
	lpmmioinfo->pchEndWrite < wm->info.pchBuffer ||
946 947 948 949 950 951
	lpmmioinfo->pchEndWrite > wm->info.pchBuffer + wm->info.cchBuffer)
	return MMSYSERR_INVALPARAM;

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

952
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
953 954 955
}

/**************************************************************************
956
* 				mmioSetBuffer		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
957
*/
958
MMRESULT WINAPI mmioSetBuffer(HMMIO hmmio, LPSTR pchBuffer, LONG cchBuffer, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
959
{
960
    LPWINE_MMIO		wm;
Alexandre Julliard's avatar
Alexandre Julliard committed
961

962
    TRACE("(hmmio=%p, pchBuf=%p, cchBuf=%d, uFlags=%#08x)\n",
963
	  hmmio, pchBuffer, cchBuffer, uFlags);
964

965
    if ((wm = MMIO_Get(hmmio)) == NULL)
966
	return MMSYSERR_INVALHANDLE;
Alexandre Julliard's avatar
Alexandre Julliard committed
967

968
    return MMIO_SetBuffer(wm, pchBuffer, cchBuffer, uFlags);
969 970
}

Alexandre Julliard's avatar
Alexandre Julliard committed
971
/**************************************************************************
972
 * 				mmioFlush      		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
973
 */
974
MMRESULT WINAPI mmioFlush(HMMIO hmmio, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
975
{
976
    LPWINE_MMIO		wm;
Alexandre Julliard's avatar
Alexandre Julliard committed
977

978
    TRACE("(%p, %04X)\n", hmmio, uFlags);
979

980
    if ((wm = MMIO_Get(hmmio)) == NULL)
981
	return MMSYSERR_INVALHANDLE;
982

983
    return MMIO_Flush(wm, uFlags);
Alexandre Julliard's avatar
Alexandre Julliard committed
984 985
}

Alexandre Julliard's avatar
Alexandre Julliard committed
986
/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
987
 * 				mmioAdvance      	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
988
 */
989
MMRESULT WINAPI mmioAdvance(HMMIO hmmio, MMIOINFO* lpmmioinfo, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
990
{
991
    LPWINE_MMIO		wm;
992

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

995 996 997
    /* NOTE: mmioAdvance16 heavily relies on parameters from lpmmioinfo we're using
     * here. be sure if you change something here to check mmioAdvance16 as well
     */
998
    if ((wm = MMIO_Get(hmmio)) == NULL)
999 1000 1001 1002 1003 1004 1005 1006
	return MMSYSERR_INVALHANDLE;

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

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

1007 1008
    if (uFlags == MMIO_WRITE && (lpmmioinfo->dwFlags & MMIO_DIRTY))
    {
1009 1010 1011
        send_message(wm->ioProc, &wm->info, MMIOM_SEEK, lpmmioinfo->lBufOffset, SEEK_SET, FALSE);
        send_message(wm->ioProc, &wm->info, MMIOM_WRITE, (LPARAM)lpmmioinfo->pchBuffer,
                     lpmmioinfo->pchNext - lpmmioinfo->pchBuffer, FALSE);
1012 1013 1014
        lpmmioinfo->dwFlags &= ~MMIO_DIRTY;
    }
    if (MMIO_Flush(wm, 0) != MMSYSERR_NOERROR)
1015 1016
	return MMIOERR_CANNOTWRITE;

1017
    if (lpmmioinfo) {
1018 1019
	wm->dwFileSize = max(wm->dwFileSize, lpmmioinfo->lBufOffset + 
                             (lpmmioinfo->pchNext - lpmmioinfo->pchBuffer));
1020
    }
1021 1022
    MMIO_GrabNextBuffer(wm, uFlags == MMIO_READ);

1023 1024
    if (lpmmioinfo) {
	lpmmioinfo->pchNext = lpmmioinfo->pchBuffer;
1025
	lpmmioinfo->pchEndRead  = lpmmioinfo->pchBuffer +
1026
	    (wm->info.pchEndRead - wm->info.pchBuffer);
1027
	lpmmioinfo->pchEndWrite = lpmmioinfo->pchBuffer +
1028 1029 1030 1031
	    (wm->info.pchEndWrite - wm->info.pchBuffer);
	lpmmioinfo->lDiskOffset = wm->info.lDiskOffset;
	lpmmioinfo->lBufOffset = wm->info.lBufOffset;
    }
1032
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1033 1034
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1035
/**************************************************************************
1036
 * 				mmioStringToFOURCCA	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1037
 */
1038
FOURCC WINAPI mmioStringToFOURCCA(LPCSTR sz, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1039
{
1040 1041
    CHAR cc[4];
    int i = 0;
1042

1043 1044 1045 1046 1047 1048 1049
    for (i = 0; i < 4 && sz[i]; i++) {
	if (uFlags & MMIO_TOUPPER) {
	    cc[i] = toupper(sz[i]);
	} else {
	    cc[i] = sz[i];
	}
    }
1050

1051
    /* Pad with spaces */
1052
    while (i < 4) cc[i++] = ' ';
1053

1054
    TRACE("Got '%.4s'\n",cc);
1055
    return mmioFOURCC(cc[0],cc[1],cc[2],cc[3]);
Alexandre Julliard's avatar
Alexandre Julliard committed
1056 1057 1058
}

/**************************************************************************
1059
 * 				mmioStringToFOURCCW	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1060
 */
1061
FOURCC WINAPI mmioStringToFOURCCW(LPCWSTR sz, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1062
{
1063
    char	szA[4];
1064

1065 1066
    WideCharToMultiByte( CP_ACP, 0, sz, 4, szA, sizeof(szA), NULL, NULL );
    return mmioStringToFOURCCA(szA,uFlags);
Alexandre Julliard's avatar
Alexandre Julliard committed
1067 1068
}

1069
/**************************************************************************
1070
 * 				mmioInstallIOProcA	   [WINMM.@]
1071
 */
1072
LPMMIOPROC WINAPI mmioInstallIOProcA(FOURCC fccIOProc,
1073
				     LPMMIOPROC pIOProc, DWORD dwFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1074
{
1075
    return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1076 1077
}

1078
/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1079
 * 				mmioInstallIOProcW	   [WINMM.@]
1080
 */
1081
LPMMIOPROC WINAPI mmioInstallIOProcW(FOURCC fccIOProc,
1082
				     LPMMIOPROC pIOProc, DWORD dwFlags)
1083
{
1084
    return MMIO_InstallIOProc(fccIOProc, pIOProc, dwFlags, TRUE);
1085 1086
}

1087 1088 1089 1090
/******************************************************************
 *		MMIO_SendMessage
 *
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1091
 */
1092
static LRESULT  MMIO_SendMessage(HMMIO hmmio, UINT uMessage, LPARAM lParam1,
1093
                                 LPARAM lParam2, BOOL is_unicode)
Alexandre Julliard's avatar
Alexandre Julliard committed
1094
{
1095
    LPWINE_MMIO		wm;
1096

1097
    TRACE("(%p, %u, %ld, %ld, %s)\n", hmmio, uMessage, lParam1, lParam2, is_unicode ? "unicode" : "ansi");
1098 1099 1100

    if (uMessage < MMIOM_USER)
	return MMSYSERR_INVALPARAM;
1101

1102
    if ((wm = MMIO_Get(hmmio)) == NULL)
1103
	return MMSYSERR_INVALHANDLE;
1104

1105
    return send_message(wm->ioProc, &wm->info, uMessage, lParam1, lParam2, is_unicode);
Alexandre Julliard's avatar
Alexandre Julliard committed
1106 1107 1108
}

/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1109
 * 				mmioSendMessage		[WINMM.@]
1110 1111 1112
 */
LRESULT WINAPI mmioSendMessage(HMMIO hmmio, UINT uMessage,
			       LPARAM lParam1, LPARAM lParam2)
Alexandre Julliard's avatar
Alexandre Julliard committed
1113
{
1114
    return MMIO_SendMessage(hmmio, uMessage, lParam1, lParam2, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1115 1116 1117
}

/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1118
 * 				mmioDescend         	[WINMM.@]
1119
 */
1120 1121
MMRESULT WINAPI mmioDescend(HMMIO hmmio, LPMMCKINFO lpck,
                            const MMCKINFO* lpckParent, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1122
{
1123 1124 1125
    DWORD		dwOldPos;
    FOURCC		srchCkId;
    FOURCC		srchType;
1126

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

1129 1130
    if (lpck == NULL)
	return MMSYSERR_INVALPARAM;
1131

1132
    dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1133
    TRACE("dwOldPos=%d\n", dwOldPos);
1134

1135
    if (lpckParent != NULL) {
1136
	TRACE("seek inside parent at %d !\n", lpckParent->dwDataOffset);
1137
	/* EPP: was dwOldPos = mmioSeek(hmmio,lpckParent->dwDataOffset,SEEK_SET); */
1138
	if (dwOldPos < lpckParent->dwDataOffset ||
1139 1140 1141
	    dwOldPos >= lpckParent->dwDataOffset + lpckParent->cksize) {
	    WARN("outside parent chunk\n");
	    return MMIOERR_CHUNKNOTFOUND;
1142
	}
1143
    }
1144

1145
    /* The SDK docu says 'ckid' is used for all cases. Real World
1146
     * examples disagree -Marcus,990216.
1147
     */
1148

1149
    srchCkId = 0;
1150
    srchType = 0;
1151

1152 1153 1154
    /* find_chunk looks for 'ckid' */
    if (uFlags & MMIO_FINDCHUNK)
	srchCkId = lpck->ckid;
1155

1156
    /* find_riff and find_list look for 'fccType' */
1157 1158
    if (uFlags & MMIO_FINDLIST)
    {
1159
	srchCkId = FOURCC_LIST;
1160
        srchType = lpck->fccType;
1161
    }
1162 1163 1164

    if (uFlags & MMIO_FINDRIFF)
    {
1165
	srchCkId = FOURCC_RIFF;
1166
        srchType = lpck->fccType;
1167
    }
1168

1169 1170
    TRACE("searching for %4.4s.%4.4s\n",
          (LPCSTR)&srchCkId, srchType ? (LPCSTR)&srchType : "any");
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    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;
        }
1183

1184
        lpck->dwDataOffset = dwOldPos + 2 * sizeof(DWORD);
1185
        TRACE("ckid=%4.4s fcc=%4.4s cksize=%08X !\n",
1186 1187 1188 1189 1190 1191 1192 1193 1194
              (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);
1195
    }
1196

1197 1198 1199 1200 1201 1202 1203 1204
    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
1205
    {
1206
	mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET);
1207 1208
	lpck->fccType = 0;
    }
1209
    TRACE("lpck: ckid=%.4s, cksize=%d, dwDataOffset=%d fccType=%08X (%.4s)!\n",
1210
	  (LPSTR)&lpck->ckid, lpck->cksize, lpck->dwDataOffset,
1211
	  lpck->fccType, srchType?(LPSTR)&lpck->fccType:"");
1212
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1213 1214
}

1215
/**************************************************************************
1216
 * 				mmioAscend     		[WINMM.@]
1217
 */
1218
MMRESULT WINAPI mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT uFlags)
1219
{
1220
    TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1221

1222
    if (lpck->dwFlags & MMIO_DIRTY) {
1223
	DWORD	dwOldPos, dwNewSize;
1224

1225
	TRACE("Chunk is dirty, checking if chunk's size is correct\n");
1226
	dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1227
	TRACE("dwOldPos=%d lpck->dwDataOffset = %d\n", dwOldPos, lpck->dwDataOffset);
1228 1229
	dwNewSize = dwOldPos - lpck->dwDataOffset;
	if (dwNewSize != lpck->cksize) {
1230
	    TRACE("Nope: lpck->cksize=%d dwNewSize=%d\n", lpck->cksize, dwNewSize);
1231
	    lpck->cksize = dwNewSize;
1232

1233 1234 1235 1236 1237 1238
	    /* pad odd size with 0 */
	    if (dwNewSize & 1) {
		char ch = 0;
		mmioWrite(hmmio, &ch, 1);
	    }
	    mmioSeek(hmmio, lpck->dwDataOffset - sizeof(DWORD), SEEK_SET);
1239 1240
	    mmioWrite(hmmio, (LPSTR)&dwNewSize, sizeof(DWORD));
	}
1241
	lpck->dwFlags = 0;
1242
    }
1243

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

1246
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1247 1248
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1249
/**************************************************************************
1250
 * 			mmioCreateChunk				[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1251
 */
1252
MMRESULT WINAPI mmioCreateChunk(HMMIO hmmio, MMCKINFO* lpck, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1253
{
1254 1255 1256
    DWORD	dwOldPos;
    LONG 	size;
    LONG 	ix;
1257

1258
    TRACE("(%p, %p, %04X);\n", hmmio, lpck, uFlags);
1259

1260
    dwOldPos = mmioSeek(hmmio, 0, SEEK_CUR);
1261
    TRACE("dwOldPos=%d\n", dwOldPos);
1262

1263 1264 1265 1266
    if (uFlags == MMIO_CREATELIST)
	lpck->ckid = FOURCC_LIST;
    else if (uFlags == MMIO_CREATERIFF)
	lpck->ckid = FOURCC_RIFF;
1267

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

1270 1271
    size = 2 * sizeof(DWORD);
    lpck->dwDataOffset = dwOldPos + size;
1272 1273

    if (lpck->ckid == FOURCC_RIFF || lpck->ckid == FOURCC_LIST)
1274 1275
	size += sizeof(DWORD);
    lpck->dwFlags = MMIO_DIRTY;
1276

1277
    ix = mmioWrite(hmmio, (LPSTR)lpck, size);
1278
    TRACE("after mmioWrite ix = %d req = %d, errno = %d\n", ix, size, errno);
1279 1280 1281 1282 1283
    if (ix < size) {
	mmioSeek(hmmio, dwOldPos, SEEK_SET);
	WARN("return CannotWrite\n");
	return MMIOERR_CANNOTWRITE;
    }
1284

1285
    return MMSYSERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
1286 1287
}

1288
/**************************************************************************
1289
 * 				mmioRenameA    			[WINMM.@]
1290
 */
1291
MMRESULT WINAPI mmioRenameA(LPCSTR szFileName, LPCSTR szNewFileName,
1292
                            const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1293
{
1294 1295
    struct IOProcList*  ioProc = NULL;
    struct IOProcList   tmp;
1296
    FOURCC              fcc;
1297

1298
    TRACE("('%s', '%s', %p, %08X);\n",
1299
	  debugstr_a(szFileName), debugstr_a(szNewFileName), lpmmioinfo, dwFlags);
1300

1301
    /* If both params are NULL, then parse the file name */
1302
    if (lpmmioinfo && lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL)
1303 1304 1305 1306
    {
	fcc = MMIO_ParseExtA(szFileName);
        if (fcc) ioProc = MMIO_FindProcNode(fcc);
    }
1307 1308

    /* Handle any unhandled/error case from above. Assume DOS file */
1309
    if (!lpmmioinfo || (lpmmioinfo->fccIOProc == 0 && lpmmioinfo->pIOProc == NULL && ioProc == NULL))
1310
	ioProc = MMIO_FindProcNode(FOURCC_DOS);
1311 1312
    /* if just the four character code is present, look up IO proc */
    else if (lpmmioinfo->pIOProc == NULL)
1313
        ioProc = MMIO_FindProcNode(lpmmioinfo->fccIOProc);
1314
    else /* use relevant ioProc */
1315 1316 1317 1318
    {
        ioProc = &tmp;
        tmp.fourCC = lpmmioinfo->fccIOProc;
        tmp.pIOProc = lpmmioinfo->pIOProc;
1319
        tmp.is_unicode = FALSE;
1320 1321
        tmp.count = 1;
    }
1322

1323 1324 1325
    /* FIXME: should we actually pass lpmmioinfo down the drain ???
     * or make a copy of it because it's const ???
     */
1326
    return send_message(ioProc, (MMIOINFO*)lpmmioinfo, MMIOM_RENAME,
1327
                        (LPARAM)szFileName, (LPARAM)szNewFileName, FALSE);
1328 1329 1330
}

/**************************************************************************
1331
 * 				mmioRenameW    			[WINMM.@]
1332
 */
1333
MMRESULT WINAPI mmioRenameW(LPCWSTR szFileName, LPCWSTR szNewFileName,
1334
                            const MMIOINFO* lpmmioinfo, DWORD dwFlags)
1335
{
1336 1337
    LPSTR	szFn = NULL;
    LPSTR	sznFn = NULL;
1338
    UINT	ret = MMSYSERR_NOMEM;
1339 1340 1341 1342 1343 1344
    INT         len;

    if (szFileName)
    {
        len = WideCharToMultiByte( CP_ACP, 0, szFileName, -1, NULL, 0, NULL, NULL );
        szFn = HeapAlloc( GetProcessHeap(), 0, len );
1345
        if (!szFn) goto done;
1346 1347 1348 1349 1350 1351
        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 );
1352
        if (!sznFn) goto done;
1353 1354 1355 1356
        WideCharToMultiByte( CP_ACP, 0, szNewFileName, -1, sznFn, len, NULL, NULL );
    }

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

1358
done:
1359 1360 1361
    HeapFree(GetProcessHeap(),0,szFn);
    HeapFree(GetProcessHeap(),0,sznFn);
    return ret;
1362
}