dmobject.c 24.6 KB
Newer Older
1 2
/*
 * Base IDirectMusicObject Implementation
3
 * Keep in sync with the master in dlls/dmusic/dmobject.c
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright (C) 2003-2004 Rok Mandeljc
 * Copyright (C) 2014 Michael Stefaniuc
 *
 * This program 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 program 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 program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS
24
#include <assert.h>
25 26
#include "objbase.h"
#include "dmusici.h"
27
#include "dmusicf.h"
28
#include "dmusics.h"
29 30
#include "dmobject.h"
#include "wine/debug.h"
31
#include "wine/heap.h"
32 33

WINE_DEFAULT_DEBUG_CHANNEL(dmobj);
34 35
WINE_DECLARE_DEBUG_CHANNEL(dmfile);

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/* Debugging helpers */
const char *debugstr_dmguid(const GUID *id) {
    unsigned int i;
#define X(guid) { &guid, #guid }
    static const struct {
        const GUID *guid;
        const char *name;
    } guids[] = {
        /* CLSIDs */
        X(CLSID_AudioVBScript),
        X(CLSID_DirectMusic),
        X(CLSID_DirectMusicAudioPathConfig),
        X(CLSID_DirectMusicAuditionTrack),
        X(CLSID_DirectMusicBand),
        X(CLSID_DirectMusicBandTrack),
        X(CLSID_DirectMusicChordMapTrack),
        X(CLSID_DirectMusicChordMap),
        X(CLSID_DirectMusicChordTrack),
        X(CLSID_DirectMusicCollection),
        X(CLSID_DirectMusicCommandTrack),
        X(CLSID_DirectMusicComposer),
        X(CLSID_DirectMusicContainer),
        X(CLSID_DirectMusicGraph),
        X(CLSID_DirectMusicLoader),
        X(CLSID_DirectMusicLyricsTrack),
        X(CLSID_DirectMusicMarkerTrack),
        X(CLSID_DirectMusicMelodyFormulationTrack),
        X(CLSID_DirectMusicMotifTrack),
        X(CLSID_DirectMusicMuteTrack),
        X(CLSID_DirectMusicParamControlTrack),
        X(CLSID_DirectMusicPatternTrack),
        X(CLSID_DirectMusicPerformance),
        X(CLSID_DirectMusicScript),
        X(CLSID_DirectMusicScriptAutoImpSegment),
        X(CLSID_DirectMusicScriptAutoImpPerformance),
        X(CLSID_DirectMusicScriptAutoImpSegmentState),
        X(CLSID_DirectMusicScriptAutoImpAudioPathConfig),
        X(CLSID_DirectMusicScriptAutoImpAudioPath),
        X(CLSID_DirectMusicScriptAutoImpSong),
        X(CLSID_DirectMusicScriptSourceCodeLoader),
        X(CLSID_DirectMusicScriptTrack),
        X(CLSID_DirectMusicSection),
        X(CLSID_DirectMusicSegment),
        X(CLSID_DirectMusicSegmentState),
        X(CLSID_DirectMusicSegmentTriggerTrack),
        X(CLSID_DirectMusicSegTriggerTrack),
        X(CLSID_DirectMusicSeqTrack),
        X(CLSID_DirectMusicSignPostTrack),
        X(CLSID_DirectMusicSong),
        X(CLSID_DirectMusicStyle),
        X(CLSID_DirectMusicStyleTrack),
        X(CLSID_DirectMusicSynth),
        X(CLSID_DirectMusicSynthSink),
        X(CLSID_DirectMusicSysExTrack),
        X(CLSID_DirectMusicTemplate),
        X(CLSID_DirectMusicTempoTrack),
        X(CLSID_DirectMusicTimeSigTrack),
        X(CLSID_DirectMusicWaveTrack),
        X(CLSID_DirectSoundWave),
        /* IIDs */
        X(IID_IDirectMusic),
        X(IID_IDirectMusic2),
        X(IID_IDirectMusic8),
        X(IID_IDirectMusicAudioPath),
        X(IID_IDirectMusicBand),
        X(IID_IDirectMusicBuffer),
        X(IID_IDirectMusicChordMap),
        X(IID_IDirectMusicCollection),
        X(IID_IDirectMusicComposer),
        X(IID_IDirectMusicContainer),
        X(IID_IDirectMusicDownload),
        X(IID_IDirectMusicDownloadedInstrument),
        X(IID_IDirectMusicGetLoader),
        X(IID_IDirectMusicGraph),
        X(IID_IDirectMusicInstrument),
        X(IID_IDirectMusicLoader),
        X(IID_IDirectMusicLoader8),
        X(IID_IDirectMusicObject),
        X(IID_IDirectMusicPatternTrack),
        X(IID_IDirectMusicPerformance),
        X(IID_IDirectMusicPerformance2),
        X(IID_IDirectMusicPerformance8),
        X(IID_IDirectMusicPort),
        X(IID_IDirectMusicPortDownload),
        X(IID_IDirectMusicScript),
        X(IID_IDirectMusicSegment),
        X(IID_IDirectMusicSegment2),
        X(IID_IDirectMusicSegment8),
        X(IID_IDirectMusicSegmentState),
        X(IID_IDirectMusicSegmentState8),
        X(IID_IDirectMusicStyle),
        X(IID_IDirectMusicStyle8),
        X(IID_IDirectMusicSynth),
        X(IID_IDirectMusicSynth8),
        X(IID_IDirectMusicSynthSink),
        X(IID_IDirectMusicThru),
        X(IID_IDirectMusicTool),
        X(IID_IDirectMusicTool8),
        X(IID_IDirectMusicTrack),
        X(IID_IDirectMusicTrack8),
        X(IID_IUnknown),
        X(IID_IPersistStream),
        X(IID_IStream),
        X(IID_IClassFactory),
        /* GUIDs */
        X(GUID_DirectMusicAllTypes),
        X(GUID_NOTIFICATION_CHORD),
        X(GUID_NOTIFICATION_COMMAND),
        X(GUID_NOTIFICATION_MEASUREANDBEAT),
        X(GUID_NOTIFICATION_PERFORMANCE),
        X(GUID_NOTIFICATION_RECOMPOSE),
        X(GUID_NOTIFICATION_SEGMENT),
        X(GUID_BandParam),
        X(GUID_ChordParam),
        X(GUID_CommandParam),
        X(GUID_CommandParam2),
        X(GUID_CommandParamNext),
        X(GUID_IDirectMusicBand),
        X(GUID_IDirectMusicChordMap),
        X(GUID_IDirectMusicStyle),
        X(GUID_MuteParam),
        X(GUID_Play_Marker),
        X(GUID_RhythmParam),
        X(GUID_TempoParam),
        X(GUID_TimeSignature),
        X(GUID_Valid_Start_Time),
        X(GUID_Clear_All_Bands),
        X(GUID_ConnectToDLSCollection),
        X(GUID_Disable_Auto_Download),
        X(GUID_DisableTempo),
        X(GUID_DisableTimeSig),
        X(GUID_Download),
        X(GUID_DownloadToAudioPath),
        X(GUID_Enable_Auto_Download),
        X(GUID_EnableTempo),
        X(GUID_EnableTimeSig),
        X(GUID_IgnoreBankSelectForGM),
        X(GUID_SeedVariations),
        X(GUID_StandardMIDIFile),
        X(GUID_Unload),
        X(GUID_UnloadFromAudioPath),
        X(GUID_Variations),
        X(GUID_PerfMasterTempo),
        X(GUID_PerfMasterVolume),
        X(GUID_PerfMasterGrooveLevel),
        X(GUID_PerfAutoDownload),
        X(GUID_DefaultGMCollection),
        X(GUID_Synth_Default),
        X(GUID_Buffer_Reverb),
        X(GUID_Buffer_EnvReverb),
        X(GUID_Buffer_Stereo),
        X(GUID_Buffer_3D_Dry),
        X(GUID_Buffer_Mono),
        X(GUID_DMUS_PROP_GM_Hardware),
        X(GUID_DMUS_PROP_GS_Capable),
        X(GUID_DMUS_PROP_GS_Hardware),
        X(GUID_DMUS_PROP_DLS1),
        X(GUID_DMUS_PROP_DLS2),
        X(GUID_DMUS_PROP_Effects),
        X(GUID_DMUS_PROP_INSTRUMENT2),
        X(GUID_DMUS_PROP_LegacyCaps),
        X(GUID_DMUS_PROP_MemorySize),
        X(GUID_DMUS_PROP_SampleMemorySize),
        X(GUID_DMUS_PROP_SamplePlaybackRate),
        X(GUID_DMUS_PROP_SetSynthSink),
        X(GUID_DMUS_PROP_SinkUsesDSound),
        X(GUID_DMUS_PROP_SynthSink_DSOUND),
        X(GUID_DMUS_PROP_SynthSink_WAVE),
        X(GUID_DMUS_PROP_Volume),
        X(GUID_DMUS_PROP_WavesReverb),
        X(GUID_DMUS_PROP_WriteLatency),
        X(GUID_DMUS_PROP_WritePeriod),
        X(GUID_DMUS_PROP_XG_Capable),
        X(GUID_DMUS_PROP_XG_Hardware)
    };
#undef X

    if (!id)
        return "(null)";
215

216 217 218 219 220 221 222 223
    for (i = 0; i < ARRAY_SIZE(guids); i++)
        if (IsEqualGUID(id, guids[i].guid))
            return guids[i].name;

    return debugstr_guid(id);
}

void dump_DMUS_OBJECTDESC(DMUS_OBJECTDESC *desc)
224
{
225 226 227 228
    if (!desc || !TRACE_ON(dmfile))
        return;

    TRACE_(dmfile)("DMUS_OBJECTDESC (%p):", desc);
229
    TRACE_(dmfile)(" - dwSize = %lu\n", desc->dwSize);
230 231

#define X(flag) if (desc->dwValidData & flag) TRACE_(dmfile)(#flag " ")
232
    TRACE_(dmfile)(" - dwValidData = %#08lx ( ", desc->dwValidData);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    X(DMUS_OBJ_OBJECT);
    X(DMUS_OBJ_CLASS);
    X(DMUS_OBJ_NAME);
    X(DMUS_OBJ_CATEGORY);
    X(DMUS_OBJ_FILENAME);
    X(DMUS_OBJ_FULLPATH);
    X(DMUS_OBJ_URL);
    X(DMUS_OBJ_VERSION);
    X(DMUS_OBJ_DATE);
    X(DMUS_OBJ_LOADED);
    X(DMUS_OBJ_MEMORY);
    X(DMUS_OBJ_STREAM);
    TRACE_(dmfile)(")\n");
#undef X

    if (desc->dwValidData & DMUS_OBJ_CLASS)
        TRACE_(dmfile)(" - guidClass = %s\n", debugstr_dmguid(&desc->guidClass));
    if (desc->dwValidData & DMUS_OBJ_OBJECT)
        TRACE_(dmfile)(" - guidObject = %s\n", debugstr_guid(&desc->guidObject));

    if (desc->dwValidData & DMUS_OBJ_DATE) {
        SYSTEMTIME time;
        FileTimeToSystemTime(&desc->ftDate, &time);
        TRACE_(dmfile)(" - ftDate = \'%04u-%02u-%02u %02u:%02u:%02u\'\n",
                time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
    }
    if (desc->dwValidData & DMUS_OBJ_VERSION)
        TRACE_(dmfile)(" - vVersion = \'%u,%u,%u,%u\'\n",
                HIWORD(desc->vVersion.dwVersionMS), LOWORD(desc->vVersion.dwVersionMS),
                HIWORD(desc->vVersion.dwVersionLS), LOWORD(desc->vVersion.dwVersionLS));
    if (desc->dwValidData & DMUS_OBJ_NAME)
        TRACE_(dmfile)(" - wszName = %s\n", debugstr_w(desc->wszName));
    if (desc->dwValidData & DMUS_OBJ_CATEGORY)
        TRACE_(dmfile)(" - wszCategory = %s\n", debugstr_w(desc->wszCategory));
    if (desc->dwValidData & DMUS_OBJ_FILENAME)
        TRACE_(dmfile)(" - wszFileName = %s\n", debugstr_w(desc->wszFileName));
    if (desc->dwValidData & DMUS_OBJ_MEMORY)
        TRACE_(dmfile)(" - llMemLength = 0x%s - pbMemData = %p\n",
                wine_dbgstr_longlong(desc->llMemLength), desc->pbMemData);
    if (desc->dwValidData & DMUS_OBJ_STREAM)
        TRACE_(dmfile)(" - pStream = %p\n", desc->pStream);
274 275
}

276 277 278 279

/* RIFF format parsing */
#define CHUNK_HDR_SIZE (sizeof(FOURCC) + sizeof(DWORD))

280 281 282 283 284 285 286 287
const char *debugstr_chunk(const struct chunk_entry *chunk)
{
    const char *type = "";

    if (!chunk)
        return "(null)";
    if (chunk->id == FOURCC_RIFF || chunk->id == FOURCC_LIST)
        type = wine_dbg_sprintf("type %s, ", debugstr_fourcc(chunk->type));
288
    return wine_dbg_sprintf("%s chunk, %ssize %lu", debugstr_fourcc(chunk->id), type, chunk->size);
289 290 291 292 293 294 295 296 297
}

static HRESULT stream_read(IStream *stream, void *data, ULONG size)
{
    ULONG read;
    HRESULT hr;

    hr = IStream_Read(stream, data, size, &read);
    if (FAILED(hr))
298
        TRACE_(dmfile)("IStream_Read failed: %#lx\n", hr);
299 300
    else if (!read && read < size) {
        /* All or nothing: Handle a partial read due to end of stream as an error */
301
        TRACE_(dmfile)("Short read: %lu < %lu\n", read, size);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
        return E_FAIL;
    }

    return hr;
}

HRESULT stream_get_chunk(IStream *stream, struct chunk_entry *chunk)
{
    static const LARGE_INTEGER zero;
    ULONGLONG ck_end = 0, p_end = 0;
    HRESULT hr;

    hr = IStream_Seek(stream, zero, STREAM_SEEK_CUR, &chunk->offset);
    if (FAILED(hr))
        return hr;
    assert(!(chunk->offset.QuadPart & 1));
    if (chunk->parent) {
        p_end = chunk->parent->offset.QuadPart + CHUNK_HDR_SIZE + ((chunk->parent->size + 1) & ~1);
        if (chunk->offset.QuadPart == p_end)
            return S_FALSE;
        ck_end = chunk->offset.QuadPart + CHUNK_HDR_SIZE;
        if (ck_end > p_end) {
            WARN_(dmfile)("No space for sub-chunk header in parent chunk: ends at offset %s > %s\n",
                    wine_dbgstr_longlong(ck_end), wine_dbgstr_longlong(p_end));
            return E_FAIL;
        }
    }

    hr = stream_read(stream, chunk, CHUNK_HDR_SIZE);
    if (hr != S_OK)
        return hr;
    if (chunk->parent) {
        ck_end += (chunk->size + 1) & ~1;
        if (ck_end > p_end) {
            WARN_(dmfile)("No space for sub-chunk data in parent chunk: ends at offset %s > %s\n",
                    wine_dbgstr_longlong(ck_end), wine_dbgstr_longlong(p_end));
            return E_FAIL;
        }
    }

    if (chunk->id == FOURCC_LIST || chunk->id == FOURCC_RIFF) {
        hr = stream_read(stream, &chunk->type, sizeof(FOURCC));
        if (hr != S_OK)
            return hr != S_FALSE ? hr : E_FAIL;
    }

    TRACE_(dmfile)("Returning %s\n", debugstr_chunk(chunk));

    return S_OK;
}

353
HRESULT stream_skip_chunk(IStream *stream, const struct chunk_entry *chunk)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
{
    LARGE_INTEGER end;

    end.QuadPart = (chunk->offset.QuadPart + CHUNK_HDR_SIZE + chunk->size + 1) & ~(ULONGLONG)1;

    return IStream_Seek(stream, end, STREAM_SEEK_SET, NULL);
}

HRESULT stream_next_chunk(IStream *stream, struct chunk_entry *chunk)
{
    HRESULT hr;

    if (chunk->id) {
        hr = stream_skip_chunk(stream, chunk);
        if (FAILED(hr))
            return hr;
    }

    return stream_get_chunk(stream, chunk);
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
/* Reads chunk data of the form:
   DWORD     - size of array element
   element[] - Array of elements
   The caller needs to heap_free() the array.
*/
HRESULT stream_chunk_get_array(IStream *stream, const struct chunk_entry *chunk, void **array,
        unsigned int *count, DWORD elem_size)
{
    DWORD size;
    HRESULT hr;

    *array = NULL;
    *count = 0;

    if (chunk->size < sizeof(DWORD)) {
        WARN_(dmfile)("%s: Too short to read element size\n", debugstr_chunk(chunk));
        return E_FAIL;
    }
    if (FAILED(hr = stream_read(stream, &size, sizeof(DWORD))))
        return hr;
    if (size != elem_size) {
396
        WARN_(dmfile)("%s: Array element size mismatch: got %lu, expected %lu\n",
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
                debugstr_chunk(chunk), size, elem_size);
        return DMUS_E_UNSUPPORTED_STREAM;
    }

    *count = (chunk->size - sizeof(DWORD)) / elem_size;
    size = *count * elem_size;
    if (!(*array = heap_alloc(size)))
        return E_OUTOFMEMORY;
    if (FAILED(hr = stream_read(stream, *array, size))) {
        heap_free(*array);
        *array = NULL;
        return hr;
    }

    if (chunk->size > size + sizeof(DWORD)) {
        WARN_(dmfile)("%s: Extraneous data at end of array\n", debugstr_chunk(chunk));
        stream_skip_chunk(stream, chunk);
        return S_FALSE;
    }
    return S_OK;
}

419 420 421 422
HRESULT stream_chunk_get_data(IStream *stream, const struct chunk_entry *chunk, void *data,
        ULONG size)
{
    if (chunk->size != size) {
423
        WARN_(dmfile)("Chunk %s (size %lu, offset %s) doesn't contains the expected data size %lu\n",
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
                debugstr_fourcc(chunk->id), chunk->size,
                wine_dbgstr_longlong(chunk->offset.QuadPart), size);
        return E_FAIL;
    }
    return stream_read(stream, data, size);
}

HRESULT stream_chunk_get_wstr(IStream *stream, const struct chunk_entry *chunk, WCHAR *str,
        ULONG size)
{
    ULONG len;
    HRESULT hr;

    hr = IStream_Read(stream, str, min(chunk->size, size), &len);
    if (FAILED(hr))
        return hr;

    /* Don't assume the string is properly zero terminated */
    str[min(len, size - 1)] = 0;

    if (len < chunk->size)
        return S_FALSE;
    return S_OK;
}


450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

/* Generic IDirectMusicObject methods */
static inline struct dmobject *impl_from_IDirectMusicObject(IDirectMusicObject *iface)
{
    return CONTAINING_RECORD(iface, struct dmobject, IDirectMusicObject_iface);
}

HRESULT WINAPI dmobj_IDirectMusicObject_QueryInterface(IDirectMusicObject *iface, REFIID riid,
        void **ret_iface)
{
    struct dmobject *This = impl_from_IDirectMusicObject(iface);
    return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface);
}

ULONG WINAPI dmobj_IDirectMusicObject_AddRef(IDirectMusicObject *iface)
{
    struct dmobject *This = impl_from_IDirectMusicObject(iface);
    return IUnknown_AddRef(This->outer_unk);
}

ULONG WINAPI dmobj_IDirectMusicObject_Release(IDirectMusicObject *iface)
{
    struct dmobject *This = impl_from_IDirectMusicObject(iface);
    return IUnknown_Release(This->outer_unk);
}

HRESULT WINAPI dmobj_IDirectMusicObject_GetDescriptor(IDirectMusicObject *iface,
        DMUS_OBJECTDESC *desc)
{
    struct dmobject *This = impl_from_IDirectMusicObject(iface);

    TRACE("(%p/%p)->(%p)\n", iface, This, desc);

    if (!desc)
        return E_POINTER;

    memcpy(desc, &This->desc, This->desc.dwSize);

    return S_OK;
}

HRESULT WINAPI dmobj_IDirectMusicObject_SetDescriptor(IDirectMusicObject *iface,
        DMUS_OBJECTDESC *desc)
{
    struct dmobject *This = impl_from_IDirectMusicObject(iface);
    HRESULT ret = S_OK;

    TRACE("(%p, %p)\n", iface, desc);

    if (!desc)
        return E_POINTER;

    /* Immutable property */
    if (desc->dwValidData & DMUS_OBJ_CLASS)
    {
        desc->dwValidData &= ~DMUS_OBJ_CLASS;
        ret = S_FALSE;
    }
    /* Set only valid fields */
    if (desc->dwValidData & DMUS_OBJ_OBJECT)
        This->desc.guidObject = desc->guidObject;
    if (desc->dwValidData & DMUS_OBJ_NAME)
        lstrcpynW(This->desc.wszName, desc->wszName, DMUS_MAX_NAME);
    if (desc->dwValidData & DMUS_OBJ_CATEGORY)
        lstrcpynW(This->desc.wszCategory, desc->wszCategory, DMUS_MAX_CATEGORY);
    if (desc->dwValidData & DMUS_OBJ_FILENAME)
        lstrcpynW(This->desc.wszFileName, desc->wszFileName, DMUS_MAX_FILENAME);
    if (desc->dwValidData & DMUS_OBJ_VERSION)
        This->desc.vVersion = desc->vVersion;
    if (desc->dwValidData & DMUS_OBJ_DATE)
        This->desc.ftDate = desc->ftDate;
    if (desc->dwValidData & DMUS_OBJ_MEMORY) {
        This->desc.llMemLength = desc->llMemLength;
        memcpy(This->desc.pbMemData, desc->pbMemData, desc->llMemLength);
    }
    if (desc->dwValidData & DMUS_OBJ_STREAM)
        IStream_Clone(desc->pStream, &This->desc.pStream);

    This->desc.dwValidData |= desc->dwValidData;

    return ret;
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
/* Helper for IDirectMusicObject::ParseDescriptor */
static inline void info_get_name(IStream *stream, const struct chunk_entry *info,
        DMUS_OBJECTDESC *desc)
{
    struct chunk_entry chunk = {.parent = info};
    char name[DMUS_MAX_NAME];
    ULONG len;
    HRESULT hr = E_FAIL;

    while (stream_next_chunk(stream, &chunk) == S_OK)
        if (chunk.id == mmioFOURCC('I','N','A','M'))
            hr = IStream_Read(stream, name, min(chunk.size, sizeof(name)), &len);

    if (SUCCEEDED(hr)) {
        len = MultiByteToWideChar(CP_ACP, 0, name, len, desc->wszName, sizeof(desc->wszName));
        desc->wszName[min(len, sizeof(desc->wszName) - 1)] = 0;
        desc->dwValidData |= DMUS_OBJ_NAME;
    }
}

static inline void unfo_get_name(IStream *stream, const struct chunk_entry *unfo,
        DMUS_OBJECTDESC *desc, BOOL inam)
{
    struct chunk_entry chunk = {.parent = unfo};

    while (stream_next_chunk(stream, &chunk) == S_OK)
        if (chunk.id == DMUS_FOURCC_UNAM_CHUNK || (inam && chunk.id == mmioFOURCC('I','N','A','M')))
            if (stream_chunk_get_wstr(stream, &chunk, desc->wszName, sizeof(desc->wszName)) == S_OK)
                desc->dwValidData |= DMUS_OBJ_NAME;
}

HRESULT dmobj_parsedescriptor(IStream *stream, const struct chunk_entry *riff,
        DMUS_OBJECTDESC *desc, DWORD supported)
{
    struct chunk_entry chunk = {.parent = riff};
    HRESULT hr;

570
    TRACE("Looking for %#lx in %p: %s\n", supported, stream, debugstr_chunk(riff));
571 572 573 574 575 576

    desc->dwValidData = 0;
    desc->dwSize = sizeof(*desc);

    while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
        switch (chunk.id) {
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
            case DMUS_FOURCC_CATEGORY_CHUNK:
                if ((supported & DMUS_OBJ_CATEGORY) && stream_chunk_get_wstr(stream, &chunk,
                            desc->wszCategory, sizeof(desc->wszCategory)) == S_OK)
                    desc->dwValidData |= DMUS_OBJ_CATEGORY;
                break;
            case DMUS_FOURCC_DATE_CHUNK:
                if ((supported & DMUS_OBJ_DATE) && stream_chunk_get_data(stream, &chunk,
                            &desc->ftDate, sizeof(desc->ftDate)) == S_OK)
                    desc->dwValidData |= DMUS_OBJ_DATE;
                break;
            case DMUS_FOURCC_FILE_CHUNK:
                if ((supported & DMUS_OBJ_FILENAME) && stream_chunk_get_wstr(stream, &chunk,
                            desc->wszFileName, sizeof(desc->wszFileName)) == S_OK)
                    desc->dwValidData |= DMUS_OBJ_FILENAME;
                break;
592 593 594 595 596
            case DMUS_FOURCC_GUID_CHUNK:
                if ((supported & DMUS_OBJ_OBJECT) && stream_chunk_get_data(stream, &chunk,
                            &desc->guidObject, sizeof(desc->guidObject)) == S_OK)
                    desc->dwValidData |= DMUS_OBJ_OBJECT;
                break;
597 598 599 600
            case DMUS_FOURCC_NAME_CHUNK:
                if ((supported & DMUS_OBJ_NAME) && stream_chunk_get_wstr(stream, &chunk,
                            desc->wszName, sizeof(desc->wszName)) == S_OK)
                    desc->dwValidData |= DMUS_OBJ_NAME;
601 602 603 604 605 606 607 608 609 610 611 612 613 614
                break;
            case DMUS_FOURCC_VERSION_CHUNK:
                if ((supported & DMUS_OBJ_VERSION) && stream_chunk_get_data(stream, &chunk,
                            &desc->vVersion, sizeof(desc->vVersion)) == S_OK)
                    desc->dwValidData |= DMUS_OBJ_VERSION;
                break;
            case FOURCC_LIST:
                if (chunk.type == DMUS_FOURCC_UNFO_LIST && (supported & DMUS_OBJ_NAME))
                    unfo_get_name(stream, &chunk, desc, supported & DMUS_OBJ_NAME_INAM);
                else if (chunk.type == DMUS_FOURCC_INFO_LIST && (supported & DMUS_OBJ_NAME_INFO))
                    info_get_name(stream, &chunk, desc);
                break;
        }
    }
615
    TRACE("Found %#lx\n", desc->dwValidData);
616 617 618 619

    return hr;
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
HRESULT dmobj_parsereference(IStream *stream, const struct chunk_entry *list,
        IDirectMusicObject **dmobj)
{
    struct chunk_entry chunk = {.parent = list};
    IDirectMusicGetLoader *getloader;
    IDirectMusicLoader *loader;
    DMUS_OBJECTDESC desc;
    DMUS_IO_REFERENCE reference;
    HRESULT hr;

    if (FAILED(hr = stream_next_chunk(stream, &chunk)))
        return hr;
    if (chunk.id != DMUS_FOURCC_REF_CHUNK)
        return DMUS_E_UNSUPPORTED_STREAM;

    if (FAILED(hr = stream_chunk_get_data(stream, &chunk, &reference, sizeof(reference)))) {
        WARN("Failed to read data of %s\n", debugstr_chunk(&chunk));
        return hr;
    }
639
    TRACE("REFERENCE guidClassID %s, dwValidData %#lx\n", debugstr_dmguid(&reference.guidClassID),
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
            reference.dwValidData);

    if (FAILED(hr = dmobj_parsedescriptor(stream, list, &desc, reference.dwValidData)))
        return hr;
    desc.guidClass = reference.guidClassID;
    desc.dwValidData |= DMUS_OBJ_CLASS;
    dump_DMUS_OBJECTDESC(&desc);

    if (FAILED(hr = IStream_QueryInterface(stream, &IID_IDirectMusicGetLoader, (void**)&getloader)))
        return hr;
    hr = IDirectMusicGetLoader_GetLoader(getloader, &loader);
    IDirectMusicGetLoader_Release(getloader);
    if (FAILED(hr))
        return hr;

    hr = IDirectMusicLoader_GetObject(loader, &desc, &IID_IDirectMusicObject, (void**)dmobj);
    IDirectMusicLoader_Release(loader);

    return hr;
}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
/* Generic IPersistStream methods */
static inline struct dmobject *impl_from_IPersistStream(IPersistStream *iface)
{
    return CONTAINING_RECORD(iface, struct dmobject, IPersistStream_iface);
}

HRESULT WINAPI dmobj_IPersistStream_QueryInterface(IPersistStream *iface, REFIID riid,
        void **ret_iface)
{
    struct dmobject *This = impl_from_IPersistStream(iface);
    return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface);
}

ULONG WINAPI dmobj_IPersistStream_AddRef(IPersistStream *iface)
{
    struct dmobject *This = impl_from_IPersistStream(iface);
    return IUnknown_AddRef(This->outer_unk);
}

ULONG WINAPI dmobj_IPersistStream_Release(IPersistStream *iface)
{
    struct dmobject *This = impl_from_IPersistStream(iface);
    return IUnknown_Release(This->outer_unk);
}

686 687 688 689 690 691 692 693 694 695 696 697 698 699
HRESULT WINAPI dmobj_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
{
    struct dmobject *This = impl_from_IPersistStream(iface);

    TRACE("(%p, %p)\n", This, class);

    if (!class)
        return E_POINTER;

    *class = This->desc.guidClass;

    return S_OK;
}

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
/* IPersistStream methods not implemented in native */
HRESULT WINAPI unimpl_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
{
    TRACE("(%p, %p): method not implemented\n", iface, class);
    return E_NOTIMPL;
}

HRESULT WINAPI unimpl_IPersistStream_IsDirty(IPersistStream *iface)
{
    TRACE("(%p): method not implemented, always returning S_FALSE\n", iface);
    return S_FALSE;
}

HRESULT WINAPI unimpl_IPersistStream_Save(IPersistStream *iface, IStream *stream,
        BOOL clear_dirty)
{
    TRACE("(%p, %p, %d): method not implemented\n", iface, stream, clear_dirty);
    return E_NOTIMPL;
}

HRESULT WINAPI unimpl_IPersistStream_GetSizeMax(IPersistStream *iface, ULARGE_INTEGER *size)
{
    TRACE("(%p, %p): method not implemented\n", iface, size);
    return E_NOTIMPL;
}


void dmobject_init(struct dmobject *dmobj, const GUID *class, IUnknown *outer_unk)
{
    dmobj->outer_unk = outer_unk;
    dmobj->desc.dwSize = sizeof(dmobj->desc);
    dmobj->desc.dwValidData = DMUS_OBJ_CLASS;
    dmobj->desc.guidClass = *class;
}