ntdll: Rename attach_dlls() to LdrInitializeThunk().
[wine.git] / dlls / dmloader / container.c
blob3232758b57dcf451dbe76820d11cd94ce3ffd67a
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"
21 #include "dmobject.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25 WINE_DECLARE_DEBUG_CHANNEL(dmdump);
27 #define DMUS_MAX_CATEGORY_SIZE DMUS_MAX_CATEGORY*sizeof(WCHAR)
28 #define DMUS_MAX_NAME_SIZE DMUS_MAX_NAME*sizeof(WCHAR)
29 #define DMUS_MAX_FILENAME_SIZE DMUS_MAX_FILENAME*sizeof(WCHAR)
31 typedef struct riff_chunk {
32 FOURCC fccID;
33 DWORD dwSize;
34 } WINE_CHUNK;
36 /* contained object entry */
37 typedef struct container_entry {
38 struct list entry;
39 DMUS_OBJECTDESC Desc;
40 BOOL bIsRIFF;
41 DWORD dwFlags; /* DMUS_CONTAINED_OBJF_KEEP: keep object in loader's cache, even when container is released */
42 WCHAR *wszAlias;
43 IDirectMusicObject *pObject; /* needed when releasing from loader's cache on container release */
44 } WINE_CONTAINER_ENTRY, *LPWINE_CONTAINER_ENTRY;
46 /*****************************************************************************
47 * IDirectMusicContainerImpl implementation
49 typedef struct IDirectMusicContainerImpl {
50 IDirectMusicContainer IDirectMusicContainer_iface;
51 struct dmobject dmobj;
52 LONG ref;
53 IStream *pStream;
54 DMUS_IO_CONTAINER_HEADER Header; /* header */
55 struct list *pContainedObjects; /* data */
56 } IDirectMusicContainerImpl;
58 static inline IDirectMusicContainerImpl *impl_from_IDirectMusicContainer(IDirectMusicContainer *iface)
60 return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, IDirectMusicContainer_iface);
63 static HRESULT destroy_dmcontainer(IDirectMusicContainerImpl *This)
65 LPDIRECTMUSICLOADER pLoader;
66 LPDIRECTMUSICGETLOADER pGetLoader;
67 struct list *pEntry;
68 LPWINE_CONTAINER_ENTRY pContainedObject;
70 /* get loader (from stream we loaded from) */
71 TRACE(": getting loader\n");
72 IStream_QueryInterface (This->pStream, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader);
73 IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
74 IDirectMusicGetLoader_Release (pGetLoader);
76 /* release objects from loader's cache (if appropriate) */
77 TRACE(": releasing objects from loader's cache\n");
78 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
79 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
80 /* my tests indicate that container releases objects *only*
81 if they were loaded at its load-time (makes sense, it doesn't
82 have pointers to objects otherwise); BTW: native container seems
83 to ignore the flags (I won't) */
84 if (pContainedObject->pObject && !(pContainedObject->dwFlags & DMUS_CONTAINED_OBJF_KEEP)) {
85 /* flags say it shouldn't be kept in loader's cache */
86 IDirectMusicLoader_ReleaseObject (pLoader, pContainedObject->pObject);
89 IDirectMusicLoader_Release (pLoader);
91 /* release stream we loaded from */
92 IStream_Release (This->pStream);
94 /* FIXME: release allocated entries */
96 return S_OK;
99 static HRESULT WINAPI IDirectMusicContainerImpl_QueryInterface(IDirectMusicContainer *iface, REFIID riid, void **ret_iface)
101 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
103 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
105 if (IsEqualIID (riid, &IID_IUnknown) || IsEqualIID (riid, &IID_IDirectMusicContainer))
106 *ret_iface = &This->IDirectMusicContainer_iface;
107 else if (IsEqualIID (riid, &IID_IDirectMusicObject))
108 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
109 else if (IsEqualIID (riid, &IID_IPersistStream))
110 *ret_iface = &This->dmobj.IPersistStream_iface;
111 else {
112 WARN("Unknown interface %s\n", debugstr_dmguid(riid));
113 *ret_iface = NULL;
114 return E_NOINTERFACE;
117 IUnknown_AddRef((IUnknown*)*ret_iface);
118 return S_OK;
121 static ULONG WINAPI IDirectMusicContainerImpl_AddRef(IDirectMusicContainer *iface)
123 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
124 LONG ref = InterlockedIncrement(&This->ref);
126 TRACE("(%p) ref=%d\n", This, ref);
128 return ref;
131 static ULONG WINAPI IDirectMusicContainerImpl_Release(IDirectMusicContainer *iface)
133 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
134 LONG ref = InterlockedDecrement(&This->ref);
136 TRACE("(%p) ref=%d\n", This, ref);
138 if (!ref) {
139 if (This->pStream)
140 destroy_dmcontainer(This);
141 HeapFree(GetProcessHeap(), 0, This);
142 unlock_module();
145 return ref;
148 static HRESULT WINAPI IDirectMusicContainerImpl_EnumObject(IDirectMusicContainer *iface,
149 REFGUID rguidClass, DWORD dwIndex, DMUS_OBJECTDESC *pDesc, WCHAR *pwszAlias)
151 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
152 struct list *pEntry;
153 LPWINE_CONTAINER_ENTRY pContainedObject;
154 DWORD dwCount = 0;
156 TRACE("(%p, %s, %d, %p, %p)\n", This, debugstr_dmguid(rguidClass), dwIndex, pDesc, pwszAlias);
158 if (!pDesc)
159 return E_POINTER;
160 if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
161 ERR(": invalid pDesc->dwSize %d\n", pDesc->dwSize);
162 return E_INVALIDARG;
165 DM_STRUCT_INIT(pDesc);
167 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
168 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
170 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pContainedObject->Desc.guidClass)) {
171 if (dwCount == dwIndex) {
172 HRESULT result = S_OK;
173 if (pwszAlias) {
174 lstrcpynW (pwszAlias, pContainedObject->wszAlias, DMUS_MAX_FILENAME);
175 if (strlenW (pContainedObject->wszAlias) > DMUS_MAX_FILENAME)
176 result = DMUS_S_STRING_TRUNCATED;
178 *pDesc = pContainedObject->Desc;
179 return result;
181 dwCount++;
185 TRACE(": not found\n");
186 return S_FALSE;
189 static const IDirectMusicContainerVtbl dmcontainer_vtbl = {
190 IDirectMusicContainerImpl_QueryInterface,
191 IDirectMusicContainerImpl_AddRef,
192 IDirectMusicContainerImpl_Release,
193 IDirectMusicContainerImpl_EnumObject
196 /* IDirectMusicObject part: */
197 static HRESULT WINAPI cont_IDirectMusicObject_ParseDescriptor(IDirectMusicObject *iface,
198 IStream *stream, DMUS_OBJECTDESC *desc)
200 struct chunk_entry riff = {0};
201 HRESULT hr;
203 TRACE("(%p, %p, %p)\n", iface, stream, desc);
205 if (!stream)
206 return E_POINTER;
207 if (!desc || desc->dwSize != sizeof(*desc))
208 return E_INVALIDARG;
210 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
211 return hr;
212 if (riff.id != FOURCC_RIFF || riff.type != DMUS_FOURCC_CONTAINER_FORM) {
213 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
214 stream_skip_chunk(stream, &riff);
215 return DMUS_E_DESCEND_CHUNK_FAIL;
218 hr = dmobj_parsedescriptor(stream, &riff, desc,
219 DMUS_OBJ_OBJECT|DMUS_OBJ_CLASS|DMUS_OBJ_NAME|DMUS_OBJ_CATEGORY|DMUS_OBJ_VERSION);
220 if (FAILED(hr))
221 return hr;
223 desc->guidClass = CLSID_DirectMusicContainer;
224 desc->dwValidData |= DMUS_OBJ_CLASS;
226 TRACE("returning descriptor:\n");
227 dump_DMUS_OBJECTDESC (desc);
228 return S_OK;
231 static const IDirectMusicObjectVtbl dmobject_vtbl = {
232 dmobj_IDirectMusicObject_QueryInterface,
233 dmobj_IDirectMusicObject_AddRef,
234 dmobj_IDirectMusicObject_Release,
235 dmobj_IDirectMusicObject_GetDescriptor,
236 dmobj_IDirectMusicObject_SetDescriptor,
237 cont_IDirectMusicObject_ParseDescriptor
240 /* IPersistStream part: */
241 static inline IDirectMusicContainerImpl *impl_from_IPersistStream(IPersistStream *iface)
243 return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, dmobj.IPersistStream_iface);
246 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
248 IDirectMusicContainerImpl *This = impl_from_IPersistStream(iface);
249 WINE_CHUNK Chunk;
250 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
251 LARGE_INTEGER liMove; /* used when skipping chunks */
252 ULARGE_INTEGER uliPos; /* needed when dealing with RIFF chunks */
253 LPDIRECTMUSICGETLOADER pGetLoader;
254 LPDIRECTMUSICLOADER pLoader;
255 HRESULT result = S_OK;
257 TRACE("(%p, %p):\n", This, pStm);
259 /* check whether pStm is valid read pointer */
260 if (IsBadReadPtr (pStm, sizeof(LPVOID))) {
261 ERR(": pStm bad read pointer\n");
262 return E_POINTER;
264 /* if stream is already set, this means the container is already loaded */
265 if (This->pStream) {
266 TRACE(": stream is already set, which means container is already loaded\n");
267 return DMUS_E_ALREADY_LOADED;
270 /* get loader since it will be needed later */
271 if (FAILED(IStream_QueryInterface (pStm, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader))) {
272 ERR(": stream not supported\n");
273 return DMUS_E_UNSUPPORTED_STREAM;
275 IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
276 IDirectMusicGetLoader_Release (pGetLoader);
278 This->pStream = pStm;
279 IStream_AddRef (pStm); /* add count for later references */
281 /* start with load */
282 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
283 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
284 switch (Chunk.fccID) {
285 case FOURCC_RIFF: {
286 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
287 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
288 StreamSize = Chunk.dwSize - sizeof(FOURCC);
289 StreamCount = 0;
290 switch (Chunk.fccID) {
291 case DMUS_FOURCC_CONTAINER_FORM: {
292 TRACE_(dmfile)(": container form\n");
293 This->dmobj.desc.guidClass = CLSID_DirectMusicContainer;
294 This->dmobj.desc.dwValidData |= DMUS_OBJ_CLASS;
295 do {
296 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
297 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
298 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
299 switch (Chunk.fccID) {
300 case DMUS_FOURCC_CONTAINER_CHUNK: {
301 TRACE_(dmfile)(": container header chunk\n");
302 IStream_Read (pStm, &This->Header, Chunk.dwSize, NULL);
303 TRACE_(dmdump)(": container header chunk:\n%s\n", debugstr_DMUS_IO_CONTAINER_HEADER(&This->Header));
304 break;
306 case DMUS_FOURCC_GUID_CHUNK: {
307 TRACE_(dmfile)(": GUID chunk\n");
308 IStream_Read (pStm, &This->dmobj.desc.guidObject, Chunk.dwSize, NULL);
309 This->dmobj.desc.dwValidData |= DMUS_OBJ_OBJECT;
310 TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&This->dmobj.desc.guidObject));
311 break;
313 case DMUS_FOURCC_VERSION_CHUNK: {
314 TRACE_(dmfile)(": version chunk\n");
315 IStream_Read (pStm, &This->dmobj.desc.vVersion, Chunk.dwSize, NULL);
316 This->dmobj.desc.dwValidData |= DMUS_OBJ_VERSION;
317 TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&This->dmobj.desc.vVersion));
318 break;
320 case DMUS_FOURCC_DATE_CHUNK: {
321 TRACE_(dmfile)(": date chunk\n");
322 IStream_Read (pStm, &This->dmobj.desc.ftDate, Chunk.dwSize, NULL);
323 This->dmobj.desc.dwValidData |= DMUS_OBJ_DATE;
324 TRACE_(dmdump)(": date: %s\n", debugstr_filetime(&This->dmobj.desc.ftDate));
325 break;
327 case DMUS_FOURCC_CATEGORY_CHUNK: {
328 TRACE_(dmfile)(": category chunk\n");
329 /* if it happens that string is too long,
330 read what we can and skip the rest*/
331 if (Chunk.dwSize > DMUS_MAX_CATEGORY_SIZE) {
332 IStream_Read (pStm, This->dmobj.desc.wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
333 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
334 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
335 } else {
336 IStream_Read (pStm, This->dmobj.desc.wszCategory, Chunk.dwSize, NULL);
338 This->dmobj.desc.dwValidData |= DMUS_OBJ_CATEGORY;
339 TRACE_(dmdump)(": category: %s\n", debugstr_w(This->dmobj.desc.wszCategory));
340 break;
342 case FOURCC_LIST: {
343 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
344 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
345 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
346 ListCount[0] = 0;
347 switch (Chunk.fccID) {
348 case DMUS_FOURCC_UNFO_LIST: {
349 TRACE_(dmfile)(": UNFO list\n");
350 do {
351 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
352 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
353 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
354 switch (Chunk.fccID) {
355 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
356 (though strings seem to be valid unicode) */
357 case mmioFOURCC('I','N','A','M'):
358 case DMUS_FOURCC_UNAM_CHUNK: {
359 TRACE_(dmfile)(": name chunk\n");
360 /* if it happens that string is too long,
361 read what we can and skip the rest*/
362 if (Chunk.dwSize > DMUS_MAX_NAME_SIZE) {
363 IStream_Read (pStm, This->dmobj.desc.wszName, DMUS_MAX_NAME_SIZE, NULL);
364 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
365 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
366 } else {
367 IStream_Read (pStm, This->dmobj.desc.wszName, Chunk.dwSize, NULL);
369 This->dmobj.desc.dwValidData |= DMUS_OBJ_NAME;
370 TRACE_(dmdump)(": name: %s\n", debugstr_w(This->dmobj.desc.wszName));
371 break;
373 default: {
374 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
375 liMove.QuadPart = Chunk.dwSize;
376 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
377 break;
380 TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
381 } while (ListCount[0] < ListSize[0]);
382 break;
384 case DMUS_FOURCC_CONTAINED_OBJECTS_LIST: {
385 TRACE_(dmfile)(": contained objects list\n");
386 do {
387 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
388 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
389 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
390 switch (Chunk.fccID) {
391 case FOURCC_LIST: {
392 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
393 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
394 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
395 ListCount[1] = 0;
396 switch (Chunk.fccID) {
397 case DMUS_FOURCC_CONTAINED_OBJECT_LIST: {
398 LPWINE_CONTAINER_ENTRY pNewEntry;
399 TRACE_(dmfile)(": contained object list\n");
400 pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_CONTAINER_ENTRY));
401 DM_STRUCT_INIT(&pNewEntry->Desc);
402 do {
403 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
404 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
405 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
406 switch (Chunk.fccID) {
407 case DMUS_FOURCC_CONTAINED_ALIAS_CHUNK: {
408 TRACE_(dmfile)(": alias chunk\n");
409 pNewEntry->wszAlias = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
410 IStream_Read (pStm, pNewEntry->wszAlias, Chunk.dwSize, NULL);
411 TRACE_(dmdump)(": alias: %s\n", debugstr_w(pNewEntry->wszAlias));
412 break;
414 case DMUS_FOURCC_CONTAINED_OBJECT_CHUNK: {
415 DMUS_IO_CONTAINED_OBJECT_HEADER tmpObjectHeader;
416 TRACE_(dmfile)(": contained object header chunk\n");
417 IStream_Read (pStm, &tmpObjectHeader, Chunk.dwSize, NULL);
418 TRACE_(dmdump)(": contained object header:\n%s\n", debugstr_DMUS_IO_CONTAINED_OBJECT_HEADER(&tmpObjectHeader));
419 /* copy guidClass */
420 pNewEntry->Desc.dwValidData |= DMUS_OBJ_CLASS;
421 pNewEntry->Desc.guidClass = tmpObjectHeader.guidClassID;
422 /* store flags */
423 pNewEntry->dwFlags = tmpObjectHeader.dwFlags;
424 break;
426 /* now read data... it may be safe to read everything after object header chunk,
427 but I'm not comfortable with MSDN's "the header is *normally* followed by ..." */
428 case FOURCC_LIST: {
429 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
430 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
431 ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
432 ListCount[2] = 0;
433 switch (Chunk.fccID) {
434 case DMUS_FOURCC_REF_LIST: {
435 TRACE_(dmfile)(": reference list\n");
436 pNewEntry->bIsRIFF = 0;
437 do {
438 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
439 ListCount[2] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
440 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
441 switch (Chunk.fccID) {
442 case DMUS_FOURCC_REF_CHUNK: {
443 DMUS_IO_REFERENCE tmpReferenceHeader; /* temporary structure */
444 TRACE_(dmfile)(": reference header chunk\n");
445 memset (&tmpReferenceHeader, 0, sizeof(DMUS_IO_REFERENCE));
446 IStream_Read (pStm, &tmpReferenceHeader, Chunk.dwSize, NULL);
447 /* copy retrieved data to DMUS_OBJECTDESC */
448 if (!IsEqualCLSID (&pNewEntry->Desc.guidClass, &tmpReferenceHeader.guidClassID)) ERR(": object header declares different CLSID than reference header?\n");
449 /* it shouldn't be necessary to copy guidClass, since it was set in contained object header already...
450 yet if they happen to be different, I'd rather stick to this one */
451 pNewEntry->Desc.guidClass = tmpReferenceHeader.guidClassID;
452 pNewEntry->Desc.dwValidData |= tmpReferenceHeader.dwValidData;
453 break;
455 case DMUS_FOURCC_GUID_CHUNK: {
456 TRACE_(dmfile)(": guid chunk\n");
457 /* no need to set flags since they were copied from reference header */
458 IStream_Read (pStm, &pNewEntry->Desc.guidObject, Chunk.dwSize, NULL);
459 break;
461 case DMUS_FOURCC_DATE_CHUNK: {
462 TRACE_(dmfile)(": file date chunk\n");
463 /* no need to set flags since they were copied from reference header */
464 IStream_Read (pStm, &pNewEntry->Desc.ftDate, Chunk.dwSize, NULL);
465 break;
467 case DMUS_FOURCC_NAME_CHUNK: {
468 TRACE_(dmfile)(": name chunk\n");
469 /* no need to set flags since they were copied from reference header */
470 IStream_Read (pStm, pNewEntry->Desc.wszName, Chunk.dwSize, NULL);
471 break;
473 case DMUS_FOURCC_FILE_CHUNK: {
474 TRACE_(dmfile)(": file name chunk\n");
475 /* no need to set flags since they were copied from reference header */
476 IStream_Read (pStm, pNewEntry->Desc.wszFileName, Chunk.dwSize, NULL);
477 break;
479 case DMUS_FOURCC_CATEGORY_CHUNK: {
480 TRACE_(dmfile)(": category chunk\n");
481 /* no need to set flags since they were copied from reference header */
482 IStream_Read (pStm, pNewEntry->Desc.wszCategory, Chunk.dwSize, NULL);
483 break;
485 case DMUS_FOURCC_VERSION_CHUNK: {
486 TRACE_(dmfile)(": version chunk\n");
487 /* no need to set flags since they were copied from reference header */
488 IStream_Read (pStm, &pNewEntry->Desc.vVersion, Chunk.dwSize, NULL);
489 break;
491 default: {
492 TRACE_(dmfile)(": unknown chunk (skipping)\n");
493 liMove.QuadPart = Chunk.dwSize;
494 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip this chunk */
495 break;
498 TRACE_(dmfile)(": ListCount[2] = 0x%08X < ListSize[2] = 0x%08X\n", ListCount[2], ListSize[2]);
499 } while (ListCount[2] < ListSize[2]);
500 break;
502 default: {
503 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
504 return E_FAIL;
507 break;
510 case FOURCC_RIFF: {
511 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
512 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
513 if (IS_VALID_DMFORM (Chunk.fccID)) {
514 TRACE_(dmfile)(": valid DMUSIC form\n");
515 pNewEntry->bIsRIFF = 1;
516 /* we'll have to skip whole RIFF chunk after SetObject is called */
517 liMove.QuadPart = 0;
518 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &uliPos);
519 uliPos.QuadPart += (Chunk.dwSize - sizeof(FOURCC)); /* set uliPos at the end of RIFF chunk */
520 /* move at the beginning of RIFF chunk */
521 liMove.QuadPart = 0;
522 liMove.QuadPart -= (sizeof(FOURCC)+sizeof(DWORD)+sizeof(FOURCC));
523 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
524 /* put pointer to stream in descriptor */
525 pNewEntry->Desc.dwValidData |= DMUS_OBJ_STREAM;
526 pNewEntry->Desc.pStream = pStm; /* we don't have to worry about cloning, since SetObject will perform it */
527 /* wait till we get on the end of object list */
528 } else {
529 TRACE_(dmfile)(": invalid DMUSIC form (skipping)\n");
530 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
531 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
532 /* FIXME: should we return E_FAIL? */
534 break;
536 default: {
537 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
538 liMove.QuadPart = Chunk.dwSize;
539 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
540 break;
543 TRACE_(dmfile)(": ListCount[1] = 0x%08X < ListSize[1] = 0x%08X\n", ListCount[1], ListSize[1]);
544 } while (ListCount[1] < ListSize[1]);
545 /* SetObject: this will fill descriptor with additional info and add alias in loader's cache */
546 IDirectMusicLoader_SetObject (pLoader, &pNewEntry->Desc);
547 /* now that SetObject collected appropriate info into descriptor we can live happily ever after;
548 or not, since we have to clean evidence of loading through stream... *sigh*
549 and we have to skip rest of the chunk, if we loaded through RIFF */
550 if (pNewEntry->bIsRIFF) {
551 liMove.QuadPart = uliPos.QuadPart;
552 IStream_Seek (pStm, liMove, STREAM_SEEK_SET, NULL);
553 pNewEntry->Desc.dwValidData &= ~DMUS_OBJ_STREAM; /* clear flag (and with bitwise complement) */
554 pNewEntry->Desc.pStream = NULL;
556 /* add entry to list of objects */
557 list_add_tail (This->pContainedObjects, &pNewEntry->entry);
558 break;
560 default: {
561 TRACE_(dmfile)(": unknown (skipping)\n");
562 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
563 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
564 break;
567 break;
569 default: {
570 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
571 liMove.QuadPart = Chunk.dwSize;
572 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
573 break;
576 TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
577 } while (ListCount[0] < ListSize[0]);
578 break;
580 default: {
581 TRACE_(dmfile)(": unknown (skipping)\n");
582 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
583 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
584 break;
587 break;
589 default: {
590 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
591 liMove.QuadPart = Chunk.dwSize;
592 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
593 break;
596 TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
597 } while (StreamCount < StreamSize);
598 break;
600 default: {
601 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
602 liMove.QuadPart = StreamSize;
603 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
604 return E_FAIL;
607 TRACE_(dmfile)(": reading finished\n");
608 This->dmobj.desc.dwValidData |= DMUS_OBJ_LOADED;
609 break;
611 default: {
612 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
613 liMove.QuadPart = Chunk.dwSize;
614 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
615 return E_FAIL;
619 /* now, if DMUS_CONTAINER_NOLOADS is not set, we are supposed to load contained objects;
620 so when we call GetObject later, they'll already be in cache */
621 if (!(This->Header.dwFlags & DMUS_CONTAINER_NOLOADS)) {
622 struct list *pEntry;
623 LPWINE_CONTAINER_ENTRY pContainedObject;
625 TRACE(": DMUS_CONTAINER_NOLOADS not set... load all objects\n");
627 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
628 IDirectMusicObject* pObject;
629 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
630 /* get object from loader and then release it */
631 if (SUCCEEDED(IDirectMusicLoader_GetObject (pLoader, &pContainedObject->Desc, &IID_IDirectMusicObject, (LPVOID*)&pObject))) {
632 pContainedObject->pObject = pObject; /* for final release */
633 IDirectMusicObject_Release (pObject); /* we don't really need this one */
634 } else {
635 WARN(": failed to load contained object\n");
636 result = DMUS_S_PARTIALLOAD;
641 IDirectMusicLoader_Release (pLoader); /* release loader */
643 #if 0
644 /* DEBUG: dumps whole container object tree: */
645 if (TRACE_ON(dmloader)) {
646 int r = 0;
647 LPWINE_CONTAINER_ENTRY tmpEntry;
648 struct list *listEntry;
650 TRACE("*** IDirectMusicContainer (%p) ***\n", This->ContainerVtbl);
651 TRACE(" - Objects:\n");
652 LIST_FOR_EACH (listEntry, This->pContainedObjects) {
653 tmpEntry = LIST_ENTRY( listEntry, WINE_CONTAINER_ENTRY, entry );
654 TRACE(" - Object[%i]:\n", r);
655 TRACE(" - wszAlias: %s\n", debugstr_w(tmpEntry->wszAlias));
656 TRACE(" - Object descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC(&tmpEntry->Desc));
657 r++;
660 #endif
662 return result;
665 static const IPersistStreamVtbl persiststream_vtbl = {
666 dmobj_IPersistStream_QueryInterface,
667 dmobj_IPersistStream_AddRef,
668 dmobj_IPersistStream_Release,
669 dmobj_IPersistStream_GetClassID,
670 unimpl_IPersistStream_IsDirty,
671 IPersistStreamImpl_Load,
672 unimpl_IPersistStream_Save,
673 unimpl_IPersistStream_GetSizeMax
676 /* for ClassFactory */
677 HRESULT WINAPI create_dmcontainer(REFIID lpcGUID, void **ppobj)
679 IDirectMusicContainerImpl* obj;
680 HRESULT hr;
682 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicContainerImpl));
683 if (NULL == obj) {
684 *ppobj = NULL;
685 return E_OUTOFMEMORY;
687 obj->IDirectMusicContainer_iface.lpVtbl = &dmcontainer_vtbl;
688 obj->ref = 1;
689 dmobject_init(&obj->dmobj, &CLSID_DirectMusicContainer,
690 (IUnknown*)&obj->IDirectMusicContainer_iface);
691 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
692 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
693 obj->pContainedObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
694 list_init (obj->pContainedObjects);
696 lock_module();
698 hr = IDirectMusicContainer_QueryInterface(&obj->IDirectMusicContainer_iface, lpcGUID, ppobj);
699 IDirectMusicContainer_Release(&obj->IDirectMusicContainer_iface);
701 return hr;