msvcrt: Remove non-needed defines from msvcrt.h.
[wine.git] / dlls / dmloader / container.c
blob005f0d7e9ed0b88a64223f67e223b2c67405c5ba
1 /* IDirectMusicContainerImpl
3 * Copyright (C) 2003-2004 Rok Mandeljc
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "dmloader_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25 #define DMUS_MAX_CATEGORY_SIZE DMUS_MAX_CATEGORY*sizeof(WCHAR)
26 #define DMUS_MAX_NAME_SIZE DMUS_MAX_NAME*sizeof(WCHAR)
27 #define DMUS_MAX_FILENAME_SIZE DMUS_MAX_FILENAME*sizeof(WCHAR)
29 typedef struct riff_chunk {
30 FOURCC fccID;
31 DWORD dwSize;
32 } WINE_CHUNK;
34 /* contained object entry */
35 typedef struct container_entry {
36 struct list entry;
37 DMUS_OBJECTDESC Desc;
38 BOOL bIsRIFF;
39 DWORD dwFlags; /* DMUS_CONTAINED_OBJF_KEEP: keep object in loader's cache, even when container is released */
40 WCHAR *wszAlias;
41 IDirectMusicObject *pObject; /* needed when releasing from loader's cache on container release */
42 } WINE_CONTAINER_ENTRY, *LPWINE_CONTAINER_ENTRY;
44 /*****************************************************************************
45 * IDirectMusicContainerImpl implementation
47 typedef struct IDirectMusicContainerImpl {
48 IDirectMusicContainer IDirectMusicContainer_iface;
49 struct dmobject dmobj;
50 LONG ref;
51 IStream *pStream;
52 DMUS_IO_CONTAINER_HEADER Header; /* header */
53 struct list *pContainedObjects; /* data */
54 } IDirectMusicContainerImpl;
56 static inline IDirectMusicContainerImpl *impl_from_IDirectMusicContainer(IDirectMusicContainer *iface)
58 return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, IDirectMusicContainer_iface);
61 static HRESULT destroy_dmcontainer(IDirectMusicContainerImpl *This)
63 LPDIRECTMUSICLOADER pLoader;
64 LPDIRECTMUSICGETLOADER pGetLoader;
65 struct list *pEntry;
66 LPWINE_CONTAINER_ENTRY pContainedObject;
68 /* get loader (from stream we loaded from) */
69 TRACE(": getting loader\n");
70 IStream_QueryInterface (This->pStream, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader);
71 IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
72 IDirectMusicGetLoader_Release (pGetLoader);
74 /* release objects from loader's cache (if appropriate) */
75 TRACE(": releasing objects from loader's cache\n");
76 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
77 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
78 /* my tests indicate that container releases objects *only*
79 if they were loaded at its load-time (makes sense, it doesn't
80 have pointers to objects otherwise); BTW: native container seems
81 to ignore the flags (I won't) */
82 if (pContainedObject->pObject && !(pContainedObject->dwFlags & DMUS_CONTAINED_OBJF_KEEP)) {
83 /* flags say it shouldn't be kept in loader's cache */
84 IDirectMusicLoader_ReleaseObject (pLoader, pContainedObject->pObject);
87 IDirectMusicLoader_Release (pLoader);
89 /* release stream we loaded from */
90 IStream_Release (This->pStream);
92 /* FIXME: release allocated entries */
94 return S_OK;
97 static HRESULT WINAPI IDirectMusicContainerImpl_QueryInterface(IDirectMusicContainer *iface, REFIID riid, void **ret_iface)
99 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
101 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
103 if (IsEqualIID (riid, &IID_IUnknown) || IsEqualIID (riid, &IID_IDirectMusicContainer))
104 *ret_iface = &This->IDirectMusicContainer_iface;
105 else if (IsEqualIID (riid, &IID_IDirectMusicObject))
106 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
107 else if (IsEqualIID (riid, &IID_IPersistStream))
108 *ret_iface = &This->dmobj.IPersistStream_iface;
109 else {
110 WARN("Unknown interface %s\n", debugstr_dmguid(riid));
111 *ret_iface = NULL;
112 return E_NOINTERFACE;
115 IUnknown_AddRef((IUnknown*)*ret_iface);
116 return S_OK;
119 static ULONG WINAPI IDirectMusicContainerImpl_AddRef(IDirectMusicContainer *iface)
121 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
122 LONG ref = InterlockedIncrement(&This->ref);
124 TRACE("(%p) ref=%d\n", This, ref);
126 return ref;
129 static ULONG WINAPI IDirectMusicContainerImpl_Release(IDirectMusicContainer *iface)
131 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
132 LONG ref = InterlockedDecrement(&This->ref);
134 TRACE("(%p) ref=%d\n", This, ref);
136 if (!ref) {
137 if (This->pStream)
138 destroy_dmcontainer(This);
139 HeapFree(GetProcessHeap(), 0, This);
140 unlock_module();
143 return ref;
146 static HRESULT WINAPI IDirectMusicContainerImpl_EnumObject(IDirectMusicContainer *iface,
147 REFGUID rguidClass, DWORD dwIndex, DMUS_OBJECTDESC *pDesc, WCHAR *pwszAlias)
149 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
150 struct list *pEntry;
151 LPWINE_CONTAINER_ENTRY pContainedObject;
152 DWORD dwCount = 0;
154 TRACE("(%p, %s, %d, %p, %p)\n", This, debugstr_dmguid(rguidClass), dwIndex, pDesc, pwszAlias);
156 if (!pDesc)
157 return E_POINTER;
158 if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
159 ERR(": invalid pDesc->dwSize %d\n", pDesc->dwSize);
160 return E_INVALIDARG;
163 DM_STRUCT_INIT(pDesc);
165 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
166 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
168 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pContainedObject->Desc.guidClass)) {
169 if (dwCount == dwIndex) {
170 HRESULT result = S_OK;
171 if (pwszAlias) {
172 lstrcpynW (pwszAlias, pContainedObject->wszAlias, DMUS_MAX_FILENAME);
173 if (lstrlenW (pContainedObject->wszAlias) > DMUS_MAX_FILENAME)
174 result = DMUS_S_STRING_TRUNCATED;
176 *pDesc = pContainedObject->Desc;
177 return result;
179 dwCount++;
183 TRACE(": not found\n");
184 return S_FALSE;
187 static const IDirectMusicContainerVtbl dmcontainer_vtbl = {
188 IDirectMusicContainerImpl_QueryInterface,
189 IDirectMusicContainerImpl_AddRef,
190 IDirectMusicContainerImpl_Release,
191 IDirectMusicContainerImpl_EnumObject
194 /* IDirectMusicObject part: */
195 static HRESULT WINAPI cont_IDirectMusicObject_ParseDescriptor(IDirectMusicObject *iface,
196 IStream *stream, DMUS_OBJECTDESC *desc)
198 struct chunk_entry riff = {0};
199 HRESULT hr;
201 TRACE("(%p, %p, %p)\n", iface, stream, desc);
203 if (!stream)
204 return E_POINTER;
205 if (!desc || desc->dwSize != sizeof(*desc))
206 return E_INVALIDARG;
208 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
209 return hr;
210 if (riff.id != FOURCC_RIFF || riff.type != DMUS_FOURCC_CONTAINER_FORM) {
211 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
212 stream_skip_chunk(stream, &riff);
213 return DMUS_E_DESCEND_CHUNK_FAIL;
216 hr = dmobj_parsedescriptor(stream, &riff, desc,
217 DMUS_OBJ_OBJECT|DMUS_OBJ_CLASS|DMUS_OBJ_NAME|DMUS_OBJ_CATEGORY|DMUS_OBJ_VERSION);
218 if (FAILED(hr))
219 return hr;
221 desc->guidClass = CLSID_DirectMusicContainer;
222 desc->dwValidData |= DMUS_OBJ_CLASS;
224 TRACE("returning descriptor:\n");
225 dump_DMUS_OBJECTDESC (desc);
226 return S_OK;
229 static const IDirectMusicObjectVtbl dmobject_vtbl = {
230 dmobj_IDirectMusicObject_QueryInterface,
231 dmobj_IDirectMusicObject_AddRef,
232 dmobj_IDirectMusicObject_Release,
233 dmobj_IDirectMusicObject_GetDescriptor,
234 dmobj_IDirectMusicObject_SetDescriptor,
235 cont_IDirectMusicObject_ParseDescriptor
238 /* IPersistStream part: */
239 static inline IDirectMusicContainerImpl *impl_from_IPersistStream(IPersistStream *iface)
241 return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, dmobj.IPersistStream_iface);
244 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
246 IDirectMusicContainerImpl *This = impl_from_IPersistStream(iface);
247 WINE_CHUNK Chunk;
248 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
249 LARGE_INTEGER liMove; /* used when skipping chunks */
250 ULARGE_INTEGER uliPos; /* needed when dealing with RIFF chunks */
251 LPDIRECTMUSICGETLOADER pGetLoader;
252 LPDIRECTMUSICLOADER pLoader;
253 HRESULT result = S_OK;
255 TRACE("(%p, %p):\n", This, pStm);
257 /* check whether pStm is valid read pointer */
258 if (IsBadReadPtr (pStm, sizeof(LPVOID))) {
259 ERR(": pStm bad read pointer\n");
260 return E_POINTER;
262 /* if stream is already set, this means the container is already loaded */
263 if (This->pStream) {
264 TRACE(": stream is already set, which means container is already loaded\n");
265 return DMUS_E_ALREADY_LOADED;
268 /* get loader since it will be needed later */
269 if (FAILED(IStream_QueryInterface (pStm, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader))) {
270 ERR(": stream not supported\n");
271 return DMUS_E_UNSUPPORTED_STREAM;
273 IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
274 IDirectMusicGetLoader_Release (pGetLoader);
276 This->pStream = pStm;
277 IStream_AddRef (pStm); /* add count for later references */
279 /* start with load */
280 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
281 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
282 switch (Chunk.fccID) {
283 case FOURCC_RIFF: {
284 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
285 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
286 StreamSize = Chunk.dwSize - sizeof(FOURCC);
287 StreamCount = 0;
288 switch (Chunk.fccID) {
289 case DMUS_FOURCC_CONTAINER_FORM: {
290 TRACE_(dmfile)(": container form\n");
291 This->dmobj.desc.guidClass = CLSID_DirectMusicContainer;
292 This->dmobj.desc.dwValidData |= DMUS_OBJ_CLASS;
293 do {
294 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
295 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
296 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
297 switch (Chunk.fccID) {
298 case DMUS_FOURCC_CONTAINER_CHUNK: {
299 IStream_Read (pStm, &This->Header, Chunk.dwSize, NULL);
300 TRACE_(dmfile)(": container header chunk:\n%s\n", debugstr_DMUS_IO_CONTAINER_HEADER(&This->Header));
301 break;
303 case DMUS_FOURCC_GUID_CHUNK: {
304 TRACE_(dmfile)(": GUID chunk\n");
305 IStream_Read (pStm, &This->dmobj.desc.guidObject, Chunk.dwSize, NULL);
306 This->dmobj.desc.dwValidData |= DMUS_OBJ_OBJECT;
307 break;
309 case DMUS_FOURCC_VERSION_CHUNK: {
310 TRACE_(dmfile)(": version chunk\n");
311 IStream_Read (pStm, &This->dmobj.desc.vVersion, Chunk.dwSize, NULL);
312 This->dmobj.desc.dwValidData |= DMUS_OBJ_VERSION;
313 break;
315 case DMUS_FOURCC_DATE_CHUNK: {
316 TRACE_(dmfile)(": date chunk\n");
317 IStream_Read (pStm, &This->dmobj.desc.ftDate, Chunk.dwSize, NULL);
318 This->dmobj.desc.dwValidData |= DMUS_OBJ_DATE;
319 break;
321 case DMUS_FOURCC_CATEGORY_CHUNK: {
322 TRACE_(dmfile)(": category chunk\n");
323 /* if it happens that string is too long,
324 read what we can and skip the rest*/
325 if (Chunk.dwSize > DMUS_MAX_CATEGORY_SIZE) {
326 IStream_Read (pStm, This->dmobj.desc.wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
327 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
328 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
329 } else {
330 IStream_Read (pStm, This->dmobj.desc.wszCategory, Chunk.dwSize, NULL);
332 This->dmobj.desc.dwValidData |= DMUS_OBJ_CATEGORY;
333 break;
335 case FOURCC_LIST: {
336 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
337 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
338 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
339 ListCount[0] = 0;
340 switch (Chunk.fccID) {
341 case DMUS_FOURCC_UNFO_LIST: {
342 TRACE_(dmfile)(": UNFO list\n");
343 do {
344 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
345 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
346 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
347 switch (Chunk.fccID) {
348 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
349 (though strings seem to be valid unicode) */
350 case mmioFOURCC('I','N','A','M'):
351 case DMUS_FOURCC_UNAM_CHUNK: {
352 TRACE_(dmfile)(": name chunk\n");
353 /* if it happens that string is too long,
354 read what we can and skip the rest*/
355 if (Chunk.dwSize > DMUS_MAX_NAME_SIZE) {
356 IStream_Read (pStm, This->dmobj.desc.wszName, DMUS_MAX_NAME_SIZE, NULL);
357 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
358 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
359 } else {
360 IStream_Read (pStm, This->dmobj.desc.wszName, Chunk.dwSize, NULL);
362 This->dmobj.desc.dwValidData |= DMUS_OBJ_NAME;
363 break;
365 default: {
366 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
367 liMove.QuadPart = Chunk.dwSize;
368 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
369 break;
372 TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
373 } while (ListCount[0] < ListSize[0]);
374 break;
376 case DMUS_FOURCC_CONTAINED_OBJECTS_LIST: {
377 TRACE_(dmfile)(": contained objects list\n");
378 do {
379 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
380 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
381 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
382 switch (Chunk.fccID) {
383 case FOURCC_LIST: {
384 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
385 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
386 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
387 ListCount[1] = 0;
388 switch (Chunk.fccID) {
389 case DMUS_FOURCC_CONTAINED_OBJECT_LIST: {
390 LPWINE_CONTAINER_ENTRY pNewEntry;
391 TRACE_(dmfile)(": contained object list\n");
392 pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_CONTAINER_ENTRY));
393 DM_STRUCT_INIT(&pNewEntry->Desc);
394 do {
395 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
396 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
397 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
398 switch (Chunk.fccID) {
399 case DMUS_FOURCC_CONTAINED_ALIAS_CHUNK: {
400 TRACE_(dmfile)(": alias chunk\n");
401 pNewEntry->wszAlias = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
402 IStream_Read (pStm, pNewEntry->wszAlias, Chunk.dwSize, NULL);
403 TRACE_(dmfile)(": alias: %s\n", debugstr_w(pNewEntry->wszAlias));
404 break;
406 case DMUS_FOURCC_CONTAINED_OBJECT_CHUNK: {
407 DMUS_IO_CONTAINED_OBJECT_HEADER tmpObjectHeader;
408 IStream_Read (pStm, &tmpObjectHeader, Chunk.dwSize, NULL);
409 TRACE_(dmfile)(": contained object header:\n%s\n", debugstr_DMUS_IO_CONTAINED_OBJECT_HEADER(&tmpObjectHeader));
410 /* copy guidClass */
411 pNewEntry->Desc.dwValidData |= DMUS_OBJ_CLASS;
412 pNewEntry->Desc.guidClass = tmpObjectHeader.guidClassID;
413 /* store flags */
414 pNewEntry->dwFlags = tmpObjectHeader.dwFlags;
415 break;
417 /* now read data... it may be safe to read everything after object header chunk,
418 but I'm not comfortable with MSDN's "the header is *normally* followed by ..." */
419 case FOURCC_LIST: {
420 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
421 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
422 ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
423 ListCount[2] = 0;
424 switch (Chunk.fccID) {
425 case DMUS_FOURCC_REF_LIST: {
426 TRACE_(dmfile)(": reference list\n");
427 pNewEntry->bIsRIFF = 0;
428 do {
429 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
430 ListCount[2] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
431 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
432 switch (Chunk.fccID) {
433 case DMUS_FOURCC_REF_CHUNK: {
434 DMUS_IO_REFERENCE tmpReferenceHeader; /* temporary structure */
435 TRACE_(dmfile)(": reference header chunk\n");
436 memset (&tmpReferenceHeader, 0, sizeof(DMUS_IO_REFERENCE));
437 IStream_Read (pStm, &tmpReferenceHeader, Chunk.dwSize, NULL);
438 /* copy retrieved data to DMUS_OBJECTDESC */
439 if (!IsEqualCLSID (&pNewEntry->Desc.guidClass, &tmpReferenceHeader.guidClassID)) ERR(": object header declares different CLSID than reference header?\n");
440 /* it shouldn't be necessary to copy guidClass, since it was set in contained object header already...
441 yet if they happen to be different, I'd rather stick to this one */
442 pNewEntry->Desc.guidClass = tmpReferenceHeader.guidClassID;
443 pNewEntry->Desc.dwValidData |= tmpReferenceHeader.dwValidData;
444 break;
446 case DMUS_FOURCC_GUID_CHUNK: {
447 TRACE_(dmfile)(": guid chunk\n");
448 /* no need to set flags since they were copied from reference header */
449 IStream_Read (pStm, &pNewEntry->Desc.guidObject, Chunk.dwSize, NULL);
450 break;
452 case DMUS_FOURCC_DATE_CHUNK: {
453 TRACE_(dmfile)(": file date chunk\n");
454 /* no need to set flags since they were copied from reference header */
455 IStream_Read (pStm, &pNewEntry->Desc.ftDate, Chunk.dwSize, NULL);
456 break;
458 case DMUS_FOURCC_NAME_CHUNK: {
459 TRACE_(dmfile)(": name chunk\n");
460 /* no need to set flags since they were copied from reference header */
461 IStream_Read (pStm, pNewEntry->Desc.wszName, Chunk.dwSize, NULL);
462 break;
464 case DMUS_FOURCC_FILE_CHUNK: {
465 TRACE_(dmfile)(": file name chunk\n");
466 /* no need to set flags since they were copied from reference header */
467 IStream_Read (pStm, pNewEntry->Desc.wszFileName, Chunk.dwSize, NULL);
468 break;
470 case DMUS_FOURCC_CATEGORY_CHUNK: {
471 TRACE_(dmfile)(": category chunk\n");
472 /* no need to set flags since they were copied from reference header */
473 IStream_Read (pStm, pNewEntry->Desc.wszCategory, Chunk.dwSize, NULL);
474 break;
476 case DMUS_FOURCC_VERSION_CHUNK: {
477 TRACE_(dmfile)(": version chunk\n");
478 /* no need to set flags since they were copied from reference header */
479 IStream_Read (pStm, &pNewEntry->Desc.vVersion, Chunk.dwSize, NULL);
480 break;
482 default: {
483 TRACE_(dmfile)(": unknown chunk (skipping)\n");
484 liMove.QuadPart = Chunk.dwSize;
485 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip this chunk */
486 break;
489 TRACE_(dmfile)(": ListCount[2] = 0x%08X < ListSize[2] = 0x%08X\n", ListCount[2], ListSize[2]);
490 } while (ListCount[2] < ListSize[2]);
491 break;
493 default: {
494 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
495 return E_FAIL;
498 break;
501 case FOURCC_RIFF: {
502 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
503 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
504 if (IS_VALID_DMFORM (Chunk.fccID)) {
505 TRACE_(dmfile)(": valid DMUSIC form\n");
506 pNewEntry->bIsRIFF = 1;
507 /* we'll have to skip whole RIFF chunk after SetObject is called */
508 liMove.QuadPart = 0;
509 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &uliPos);
510 uliPos.QuadPart += (Chunk.dwSize - sizeof(FOURCC)); /* set uliPos at the end of RIFF chunk */
511 /* move at the beginning of RIFF chunk */
512 liMove.QuadPart = 0;
513 liMove.QuadPart -= (sizeof(FOURCC)+sizeof(DWORD)+sizeof(FOURCC));
514 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
515 /* put pointer to stream in descriptor */
516 pNewEntry->Desc.dwValidData |= DMUS_OBJ_STREAM;
517 pNewEntry->Desc.pStream = pStm; /* we don't have to worry about cloning, since SetObject will perform it */
518 /* wait till we get on the end of object list */
519 } else {
520 TRACE_(dmfile)(": invalid DMUSIC form (skipping)\n");
521 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
522 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
523 /* FIXME: should we return E_FAIL? */
525 break;
527 default: {
528 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
529 liMove.QuadPart = Chunk.dwSize;
530 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
531 break;
534 TRACE_(dmfile)(": ListCount[1] = 0x%08X < ListSize[1] = 0x%08X\n", ListCount[1], ListSize[1]);
535 } while (ListCount[1] < ListSize[1]);
536 /* SetObject: this will fill descriptor with additional info and add alias in loader's cache */
537 IDirectMusicLoader_SetObject (pLoader, &pNewEntry->Desc);
538 /* now that SetObject collected appropriate info into descriptor we can live happily ever after;
539 or not, since we have to clean evidence of loading through stream... *sigh*
540 and we have to skip rest of the chunk, if we loaded through RIFF */
541 if (pNewEntry->bIsRIFF) {
542 liMove.QuadPart = uliPos.QuadPart;
543 IStream_Seek (pStm, liMove, STREAM_SEEK_SET, NULL);
544 pNewEntry->Desc.dwValidData &= ~DMUS_OBJ_STREAM; /* clear flag (and with bitwise complement) */
545 pNewEntry->Desc.pStream = NULL;
547 /* add entry to list of objects */
548 list_add_tail (This->pContainedObjects, &pNewEntry->entry);
549 break;
551 default: {
552 TRACE_(dmfile)(": unknown (skipping)\n");
553 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
554 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
555 break;
558 break;
560 default: {
561 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
562 liMove.QuadPart = Chunk.dwSize;
563 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
564 break;
567 TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
568 } while (ListCount[0] < ListSize[0]);
569 break;
571 default: {
572 TRACE_(dmfile)(": unknown (skipping)\n");
573 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
574 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
575 break;
578 break;
580 default: {
581 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
582 liMove.QuadPart = Chunk.dwSize;
583 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
584 break;
587 TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
588 } while (StreamCount < StreamSize);
589 break;
591 default: {
592 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
593 liMove.QuadPart = StreamSize;
594 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
595 return E_FAIL;
598 TRACE_(dmfile)(": reading finished\n");
599 This->dmobj.desc.dwValidData |= DMUS_OBJ_LOADED;
600 dump_DMUS_OBJECTDESC(&This->dmobj.desc);
601 break;
603 default: {
604 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
605 liMove.QuadPart = Chunk.dwSize;
606 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
607 return E_FAIL;
611 /* now, if DMUS_CONTAINER_NOLOADS is not set, we are supposed to load contained objects;
612 so when we call GetObject later, they'll already be in cache */
613 if (!(This->Header.dwFlags & DMUS_CONTAINER_NOLOADS)) {
614 struct list *pEntry;
615 LPWINE_CONTAINER_ENTRY pContainedObject;
617 TRACE(": DMUS_CONTAINER_NOLOADS not set... load all objects\n");
619 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
620 IDirectMusicObject* pObject;
621 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
622 /* get object from loader and then release it */
623 if (SUCCEEDED(IDirectMusicLoader_GetObject (pLoader, &pContainedObject->Desc, &IID_IDirectMusicObject, (LPVOID*)&pObject))) {
624 pContainedObject->pObject = pObject; /* for final release */
625 IDirectMusicObject_Release (pObject); /* we don't really need this one */
626 } else {
627 WARN(": failed to load contained object\n");
628 result = DMUS_S_PARTIALLOAD;
633 IDirectMusicLoader_Release (pLoader); /* release loader */
635 return result;
638 static const IPersistStreamVtbl persiststream_vtbl = {
639 dmobj_IPersistStream_QueryInterface,
640 dmobj_IPersistStream_AddRef,
641 dmobj_IPersistStream_Release,
642 dmobj_IPersistStream_GetClassID,
643 unimpl_IPersistStream_IsDirty,
644 IPersistStreamImpl_Load,
645 unimpl_IPersistStream_Save,
646 unimpl_IPersistStream_GetSizeMax
649 /* for ClassFactory */
650 HRESULT WINAPI create_dmcontainer(REFIID lpcGUID, void **ppobj)
652 IDirectMusicContainerImpl* obj;
653 HRESULT hr;
655 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicContainerImpl));
656 if (NULL == obj) {
657 *ppobj = NULL;
658 return E_OUTOFMEMORY;
660 obj->IDirectMusicContainer_iface.lpVtbl = &dmcontainer_vtbl;
661 obj->ref = 1;
662 dmobject_init(&obj->dmobj, &CLSID_DirectMusicContainer,
663 (IUnknown*)&obj->IDirectMusicContainer_iface);
664 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
665 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
666 obj->pContainedObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
667 list_init (obj->pContainedObjects);
669 lock_module();
671 hr = IDirectMusicContainer_QueryInterface(&obj->IDirectMusicContainer_iface, lpcGUID, ppobj);
672 IDirectMusicContainer_Release(&obj->IDirectMusicContainer_iface);
674 return hr;