d3d9: Surfaces always have resource type WINED3DRTYPE_SURFACE.
[wine] / dlls / dmloader / container.c
1 /* IDirectMusicContainerImpl
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "dmloader_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24 WINE_DECLARE_DEBUG_CHANNEL(dmdump);
25
26 #define DMUS_MAX_CATEGORY_SIZE DMUS_MAX_CATEGORY*sizeof(WCHAR)
27 #define DMUS_MAX_NAME_SIZE     DMUS_MAX_NAME*sizeof(WCHAR)
28 #define DMUS_MAX_FILENAME_SIZE DMUS_MAX_FILENAME*sizeof(WCHAR)
29
30 static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_AddRef (LPDIRECTMUSICCONTAINER iface);
31 static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface);
32 static ULONG WINAPI IDirectMusicContainerImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface);
33
34 /*****************************************************************************
35  * IDirectMusicContainerImpl implementation
36  */
37 /* IUnknown/IDirectMusicContainer part: */
38
39 static HRESULT DMUSIC_DestroyDirectMusicContainerImpl (LPDIRECTMUSICCONTAINER iface) {
40         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);
41         LPDIRECTMUSICLOADER pLoader;
42         LPDIRECTMUSICGETLOADER pGetLoader;
43         struct list *pEntry;
44         LPWINE_CONTAINER_ENTRY pContainedObject;
45
46         /* get loader (from stream we loaded from) */
47         TRACE(": getting loader\n");
48         IStream_QueryInterface (This->pStream, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader);
49         IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
50         IDirectMusicGetLoader_Release (pGetLoader);
51
52         /* release objects from loader's cache (if appropriate) */
53         TRACE(": releasing objects from loader's cache\n");
54         LIST_FOR_EACH (pEntry, This->pContainedObjects) {
55                 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
56                 /* my tests indicate that container releases objects *only*
57                    if they were loaded at its load-time (makes sense, it doesn't
58                    have pointers to objects otherwise); BTW: native container seems
59                    to ignore the flags (I won't) */
60                 if (pContainedObject->pObject && !(pContainedObject->dwFlags & DMUS_CONTAINED_OBJF_KEEP)) {
61                         /* flags say it shouldn't be kept in loader's cache */
62                         IDirectMusicLoader_ReleaseObject (pLoader, pContainedObject->pObject);
63                 }
64         }
65         IDirectMusicLoader_Release (pLoader);
66
67         /* release stream we loaded from */
68         IStream_Release (This->pStream);
69
70         /* FIXME: release allocated entries */
71
72         /* decrease number of instances */
73         InterlockedDecrement (&dwDirectMusicContainer);
74
75         return S_OK;
76 }
77
78 static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface (LPDIRECTMUSICCONTAINER iface, REFIID riid, LPVOID *ppobj) {
79         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);
80         
81         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
82         if (IsEqualIID (riid, &IID_IUnknown) ||
83                 IsEqualIID (riid, &IID_IDirectMusicContainer)) {
84                 *ppobj = &This->ContainerVtbl;
85                 IDirectMusicContainerImpl_IDirectMusicContainer_AddRef ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
86                 return S_OK;
87         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
88                 *ppobj = &This->ObjectVtbl;
89                 IDirectMusicContainerImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);           
90                 return S_OK;
91         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
92                 *ppobj = &This->PersistStreamVtbl;
93                 IDirectMusicContainerImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);            
94                 return S_OK;
95         }
96         
97         WARN(": not found\n");
98         return E_NOINTERFACE;
99 }
100
101 static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_AddRef (LPDIRECTMUSICCONTAINER iface) {
102         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);
103         TRACE("(%p): AddRef from %d\n", This, This->dwRef);
104         return InterlockedIncrement (&This->dwRef);
105 }
106
107 static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_Release (LPDIRECTMUSICCONTAINER iface) {
108         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);
109         
110         DWORD dwRef = InterlockedDecrement (&This->dwRef);
111         TRACE("(%p): ReleaseRef to %d\n", This, dwRef);
112         if (dwRef == 0) {
113                 DMUSIC_DestroyDirectMusicContainerImpl (iface);
114                 HeapFree(GetProcessHeap(), 0, This);
115         }
116         
117         return dwRef;
118 }
119
120 static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_EnumObject (LPDIRECTMUSICCONTAINER iface, REFGUID rguidClass, DWORD dwIndex, LPDMUS_OBJECTDESC pDesc, WCHAR* pwszAlias) {
121         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);       
122         struct list *pEntry;
123         LPWINE_CONTAINER_ENTRY pContainedObject;
124         DWORD dwCount = 0;
125
126         TRACE("(%p, %s, %d, %p, %p)\n", This, debugstr_dmguid(rguidClass), dwIndex, pDesc, pwszAlias);
127
128         if (!pDesc)
129                 return E_POINTER;
130         if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
131                 ERR(": invalid pDesc->dwSize %d\n", pDesc->dwSize);
132                 return E_INVALIDARG;
133         }
134
135         DM_STRUCT_INIT(pDesc);
136
137         LIST_FOR_EACH (pEntry, This->pContainedObjects) {
138                 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
139                 
140                 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pContainedObject->Desc.guidClass)) {
141                         if (dwCount == dwIndex) {
142                                 HRESULT result = S_OK;
143                                 if (pwszAlias) {
144                                         lstrcpynW (pwszAlias, pContainedObject->wszAlias, DMUS_MAX_FILENAME);
145                                         if (strlenW (pContainedObject->wszAlias) > DMUS_MAX_FILENAME)
146                                                 result = DMUS_S_STRING_TRUNCATED;
147                                 }
148                                 *pDesc = pContainedObject->Desc;
149                                 return result;
150                         }
151                         dwCount++;
152                 }
153         }               
154         
155         TRACE(": not found\n");
156         return S_FALSE;
157 }
158
159 static const IDirectMusicContainerVtbl DirectMusicContainer_Container_Vtbl = {
160         IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface,
161         IDirectMusicContainerImpl_IDirectMusicContainer_AddRef,
162         IDirectMusicContainerImpl_IDirectMusicContainer_Release,
163         IDirectMusicContainerImpl_IDirectMusicContainer_EnumObject
164 };
165
166 /* IDirectMusicObject part: */
167 static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
168         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
169         return IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl, riid, ppobj);
170 }
171
172 static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
173         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
174         return IDirectMusicContainerImpl_IDirectMusicContainer_AddRef ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
175 }
176
177 static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
178         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
179         return IDirectMusicContainerImpl_IDirectMusicContainer_Release ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
180 }
181
182 static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
183         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
184         TRACE("(%p, %p):\n", This, pDesc);
185         
186         /* check if we can write to whole pDesc */
187         if (IsBadReadPtr (pDesc, sizeof(DWORD))) {
188                 ERR(": pDesc->dwSize bad read pointer\n");
189                 return E_POINTER;
190         }
191         if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
192                 ERR(": invalid pDesc->dwSize\n");
193                 return E_INVALIDARG;
194         }
195         if (IsBadWritePtr (pDesc, sizeof(DMUS_OBJECTDESC))) {
196                 ERR(": pDesc bad write pointer\n");
197                 return E_POINTER;
198         }
199
200         DM_STRUCT_INIT(pDesc);
201         *pDesc = This->Desc;
202
203         return S_OK;
204 }
205
206 static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
207         DWORD dwNewFlags = 0;
208         DWORD dwFlagDifference;
209         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
210         TRACE("(%p, %p):\n", This, pDesc);
211
212         /* check if we can read whole pDesc */
213         if (IsBadReadPtr (pDesc, sizeof(DWORD))) {
214                 ERR(": pDesc->dwSize bad read pointer\n");
215                 return E_POINTER;
216         }
217         if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
218                 ERR(": invalid pDesc->dwSize\n");
219                 return E_INVALIDARG;
220         }
221         if (IsBadReadPtr (pDesc, sizeof(DMUS_OBJECTDESC))) {
222                 ERR(": pDesc bad read pointer\n");
223                 return E_POINTER;
224         }
225
226         if (pDesc->dwValidData & DMUS_OBJ_OBJECT) {
227                 This->Desc.guidObject = pDesc->guidObject;
228                 dwNewFlags |= DMUS_OBJ_OBJECT;
229         }
230         if (pDesc->dwValidData & DMUS_OBJ_NAME) {
231                 lstrcpynW (This->Desc.wszName, pDesc->wszName, DMUS_MAX_NAME);
232                 dwNewFlags |= DMUS_OBJ_NAME;
233         }
234         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY) {
235                 lstrcpynW (This->Desc.wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
236                 dwNewFlags |= DMUS_OBJ_CATEGORY;
237         }
238         if (pDesc->dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) {
239                 lstrcpynW (This->Desc.wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
240                 dwNewFlags |= (pDesc->dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH));
241         }
242         if (pDesc->dwValidData & DMUS_OBJ_VERSION) {
243                 This->Desc.vVersion.dwVersionLS = pDesc->vVersion.dwVersionLS;
244                 This->Desc.vVersion.dwVersionMS = pDesc->vVersion.dwVersionMS;
245                 dwNewFlags |= DMUS_OBJ_VERSION;
246         }
247         if (pDesc->dwValidData & DMUS_OBJ_DATE) {
248                 This->Desc.ftDate.dwHighDateTime = pDesc->ftDate.dwHighDateTime;
249                 This->Desc.ftDate.dwLowDateTime = pDesc->ftDate.dwLowDateTime;
250                 dwNewFlags |= DMUS_OBJ_DATE;
251         }
252         /* set new flags */
253         This->Desc.dwValidData |= dwNewFlags;
254         
255         dwFlagDifference = pDesc->dwValidData - dwNewFlags;
256         if (dwFlagDifference) {
257                 pDesc->dwValidData &= ~dwFlagDifference; /* and with bitwise complement */
258                 return S_FALSE;
259         } else return S_OK;
260 }
261
262 static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
263         ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
264         WINE_CHUNK Chunk;
265         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
266         LARGE_INTEGER liMove; /* used when skipping chunks */
267
268         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
269         
270         /* check whether arguments are OK */
271         if (IsBadReadPtr (pStream, sizeof(LPVOID))) {
272                 ERR(": pStream bad read pointer\n");
273                 return E_POINTER;
274         }
275         /* check whether pDesc is OK */
276         if (IsBadReadPtr (pDesc, sizeof(DWORD))) {
277                 ERR(": pDesc->dwSize bad read pointer\n");
278                 return E_POINTER;
279         }
280         if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
281                 ERR(": invalid pDesc->dwSize\n");
282                 return E_INVALIDARG;
283         }
284         if (IsBadWritePtr (pDesc, sizeof(DMUS_OBJECTDESC))) {
285                 ERR(": pDesc bad write pointer\n");
286                 return E_POINTER;
287         }
288
289         DM_STRUCT_INIT(pDesc);
290         
291         /* here we go... */
292         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
293         TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
294         switch (Chunk.fccID) {  
295                 case FOURCC_RIFF: {
296                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
297                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
298                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
299                         StreamCount = 0;
300                         if (Chunk.fccID == DMUS_FOURCC_CONTAINER_FORM) {
301                                 TRACE_(dmfile)(": container form\n");
302                                 /* set guidClass */
303                                 pDesc->dwValidData |= DMUS_OBJ_CLASS;
304                                 pDesc->guidClass = CLSID_DirectMusicContainer;
305                                 do {
306                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
307                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
308                                         TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
309                                         switch (Chunk.fccID) {
310                                                 case DMUS_FOURCC_GUID_CHUNK: {
311                                                         TRACE_(dmfile)(": GUID chunk\n");
312                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
313                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
314                                                         TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&pDesc->guidObject));
315                                                         break;
316                                                 }
317                                                 case DMUS_FOURCC_VERSION_CHUNK: {
318                                                         TRACE_(dmfile)(": version chunk\n");
319                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
320                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
321                                                         TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&pDesc->vVersion));
322                                                         break;
323                                                 }
324                                                 case DMUS_FOURCC_DATE_CHUNK: {
325                                                         TRACE_(dmfile)(": date chunk\n");
326                                                         IStream_Read (pStream, &pDesc->ftDate, Chunk.dwSize, NULL);
327                                                         pDesc->dwValidData |= DMUS_OBJ_DATE;
328                                                         TRACE_(dmdump)(": date: %s\n", debugstr_filetime(&pDesc->ftDate));
329                                                         break;
330                                                 }                                                               
331                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
332                                                         TRACE_(dmfile)(": category chunk\n");
333                                                         /* if it happens that string is too long,
334                                                            read what we can and skip the rest*/
335                                                         if (Chunk.dwSize > DMUS_MAX_CATEGORY_SIZE) {
336                                                                 IStream_Read (pStream, pDesc->wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
337                                                                 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
338                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
339                                                         } else {
340                                                                 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
341                                                         }
342                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
343                                                         TRACE_(dmdump)(": category: %s\n", debugstr_w(pDesc->wszCategory));                                                     
344                                                         break;
345                                                 }
346                                                 case FOURCC_LIST: {
347                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
348                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
349                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
350                                                         ListCount[0] = 0;
351                                                         switch (Chunk.fccID) {
352                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
353                                                                 case DMUS_FOURCC_UNFO_LIST: {
354                                                                         TRACE_(dmfile)(": UNFO list\n");
355                                                                         do {
356                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
357                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
358                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
359                                                                                 switch (Chunk.fccID) {
360                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
361                                              (though strings seem to be valid unicode) */
362                                                                                         case mmioFOURCC('I','N','A','M'):
363                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
364                                                                                                 TRACE_(dmfile)(": name chunk\n");
365                                                                                                 /* if it happens that string is too long,
366                                                                                                            read what we can and skip the rest*/
367                                                                                                 if (Chunk.dwSize > DMUS_MAX_NAME_SIZE) {
368                                                                                                         IStream_Read (pStream, pDesc->wszName, DMUS_MAX_NAME_SIZE, NULL);
369                                                                                                         liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
370                                                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
371                                                                                                 } else {
372                                                                                                         IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
373                                                                                                 }
374                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
375                                                                                                 TRACE_(dmdump)(": name: %s\n", debugstr_w(pDesc->wszName));
376                                                                                                 break;
377                                                                                         }
378                                                                                         default: {
379                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
380                                                                                                 liMove.QuadPart = Chunk.dwSize;
381                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
382                                                                                                 break;                                          
383                                                                                         }
384                                                                                 }
385                                                                                 TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
386                                                                         } while (ListCount[0] < ListSize[0]);
387                                                                         break;
388                                                                 }
389                                                                 default: {
390                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
391                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
392                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
393                                                                         break;                                          
394                                                                 }
395                                                         }
396                                                         break;
397                                                 }       
398                                                 default: {
399                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
400                                                         liMove.QuadPart = Chunk.dwSize;
401                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
402                                                         break;                                          
403                                                 }
404                                         }
405                                         TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
406                                 } while (StreamCount < StreamSize);
407                         } else {
408                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
409                                 liMove.QuadPart = StreamSize;
410                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
411                                 return E_FAIL;
412                         }
413                 
414                         TRACE_(dmfile)(": reading finished\n");
415                         break;
416                 }
417                 default: {
418                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
419                         liMove.QuadPart = Chunk.dwSize;
420                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
421                         return DMUS_E_INVALIDFILE;
422                 }
423         }       
424         
425         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC(pDesc));
426         return S_OK;    
427 }
428
429 static const IDirectMusicObjectVtbl DirectMusicContainer_Object_Vtbl = {
430         IDirectMusicContainerImpl_IDirectMusicObject_QueryInterface,
431         IDirectMusicContainerImpl_IDirectMusicObject_AddRef,
432         IDirectMusicContainerImpl_IDirectMusicObject_Release,
433         IDirectMusicContainerImpl_IDirectMusicObject_GetDescriptor,
434         IDirectMusicContainerImpl_IDirectMusicObject_SetDescriptor,
435         IDirectMusicContainerImpl_IDirectMusicObject_ParseDescriptor
436 };
437
438 /* IPersistStream part: */
439 static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
440         ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
441         return IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl, riid, ppobj);
442 }
443
444 static ULONG WINAPI IDirectMusicContainerImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
445         ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
446         return IDirectMusicContainerImpl_IDirectMusicContainer_AddRef ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
447 }
448
449 static ULONG WINAPI IDirectMusicContainerImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
450         ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
451         return IDirectMusicContainerImpl_IDirectMusicContainer_Release ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
452 }
453
454 static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
455         ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
456         
457         TRACE("(%p, %p)\n", This, pClassID);
458         if (IsBadWritePtr (pClassID, sizeof(CLSID))) {
459                 ERR(": pClassID bad write pointer\n");
460                 return E_POINTER;
461         }
462         
463         *pClassID = CLSID_DirectMusicContainer;
464         return S_OK;
465 }
466
467 static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
468         /* FIXME: is implemented (somehow) */
469         return E_NOTIMPL;
470 }
471
472 static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
473         ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
474         WINE_CHUNK Chunk;
475         DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
476         LARGE_INTEGER liMove; /* used when skipping chunks */
477         ULARGE_INTEGER uliPos; /* needed when dealing with RIFF chunks */
478         LPDIRECTMUSICGETLOADER pGetLoader;
479         LPDIRECTMUSICLOADER pLoader;
480         HRESULT result = S_OK;
481
482         TRACE("(%p, %p):\n", This, pStm);
483         
484         /* check whether pStm is valid read pointer */
485         if (IsBadReadPtr (pStm, sizeof(LPVOID))) {
486                 ERR(": pStm bad read pointer\n");
487                 return E_POINTER;
488         }
489         /* if stream is already set, this means the container is already loaded */
490         if (This->pStream) {
491                 TRACE(": stream is already set, which means container is already loaded\n");
492                 return DMUS_E_ALREADY_LOADED;
493         }
494
495         /* get loader since it will be needed later */
496         if (FAILED(IStream_QueryInterface (pStm, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader))) {
497                 ERR(": stream not supported\n");
498                 return DMUS_E_UNSUPPORTED_STREAM;
499         }
500         IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
501         IDirectMusicGetLoader_Release (pGetLoader);
502         
503         This->pStream = pStm;
504         IStream_AddRef (pStm); /* add count for later references */
505         
506         /* start with load */
507         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
508         TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
509         switch (Chunk.fccID) {  
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                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
514                         StreamCount = 0;
515                         switch (Chunk.fccID) {
516                                 case DMUS_FOURCC_CONTAINER_FORM: {
517                                         TRACE_(dmfile)(": container form\n");
518                                         This->Desc.guidClass = CLSID_DirectMusicContainer;
519                                         This->Desc.dwValidData |= DMUS_OBJ_CLASS;
520                                         do {
521                                                 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
522                                                 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
523                                                 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
524                                                 switch (Chunk.fccID) {
525                                                         case DMUS_FOURCC_CONTAINER_CHUNK: {
526                                                                 TRACE_(dmfile)(": container header chunk\n");
527                                                                 IStream_Read (pStm, &This->Header, Chunk.dwSize, NULL);
528                                                                 TRACE_(dmdump)(": container header chunk:\n%s\n", debugstr_DMUS_IO_CONTAINER_HEADER(&This->Header));
529                                                                 break;  
530                                                         }
531                                                         case DMUS_FOURCC_GUID_CHUNK: {
532                                                                 TRACE_(dmfile)(": GUID chunk\n");
533                                                                 IStream_Read (pStm, &This->Desc.guidObject, Chunk.dwSize, NULL);
534                                                                 This->Desc.dwValidData |= DMUS_OBJ_OBJECT;
535                                                                 TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&This->Desc.guidObject));
536                                                                 break;
537                                                         }
538                                                         case DMUS_FOURCC_VERSION_CHUNK: {
539                                                                 TRACE_(dmfile)(": version chunk\n");
540                                                                 IStream_Read (pStm, &This->Desc.vVersion, Chunk.dwSize, NULL);
541                                                                 This->Desc.dwValidData |= DMUS_OBJ_VERSION;
542                                                                 TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&This->Desc.vVersion));
543                                                                 break;
544                                                         }
545                                                         case DMUS_FOURCC_DATE_CHUNK: {
546                                                                 TRACE_(dmfile)(": date chunk\n");
547                                                                 IStream_Read (pStm, &This->Desc.ftDate, Chunk.dwSize, NULL);
548                                                                 This->Desc.dwValidData |= DMUS_OBJ_DATE;
549                                                                 TRACE_(dmdump)(": date: %s\n", debugstr_filetime(&This->Desc.ftDate));
550                                                                 break;
551                                                         }                                                       
552                                                         case DMUS_FOURCC_CATEGORY_CHUNK: {
553                                                                 TRACE_(dmfile)(": category chunk\n");
554                                                                 /* if it happens that string is too long,
555                                                                    read what we can and skip the rest*/
556                                                                 if (Chunk.dwSize > DMUS_MAX_CATEGORY_SIZE) {
557                                                                         IStream_Read (pStm, This->Desc.wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
558                                                                         liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
559                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
560                                                                 } else {
561                                                                         IStream_Read (pStm, This->Desc.wszCategory, Chunk.dwSize, NULL);
562                                                                 }
563                                                                 This->Desc.dwValidData |= DMUS_OBJ_CATEGORY;
564                                                                 TRACE_(dmdump)(": category: %s\n", debugstr_w(This->Desc.wszCategory));                                                         
565                                                                 break;
566                                                         }
567                                                         case FOURCC_LIST: {
568                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
569                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
570                                                                 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
571                                                                 ListCount[0] = 0;
572                                                                 switch (Chunk.fccID) {
573                                                                         case DMUS_FOURCC_UNFO_LIST: {
574                                                                                 TRACE_(dmfile)(": UNFO list\n");
575                                                                                 do {
576                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
577                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
578                                                                                         TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
579                                                                                         switch (Chunk.fccID) {
580                                                                                                 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
581                                               (though strings seem to be valid unicode) */
582                                                                                                 case mmioFOURCC('I','N','A','M'):
583                                                                                                 case DMUS_FOURCC_UNAM_CHUNK: {
584                                                                                                         TRACE_(dmfile)(": name chunk\n");
585                                                                                                         /* if it happens that string is too long,
586                                                                                                            read what we can and skip the rest*/
587                                                                                                         if (Chunk.dwSize > DMUS_MAX_NAME_SIZE) {
588                                                                                                                 IStream_Read (pStm, This->Desc.wszName, DMUS_MAX_NAME_SIZE, NULL);
589                                                                                                                 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
590                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
591                                                                                                         } else {
592                                                                                                                 IStream_Read (pStm, This->Desc.wszName, Chunk.dwSize, NULL);
593                                                                                                         }
594                                                                                                         This->Desc.dwValidData |= DMUS_OBJ_NAME;
595                                                                                                         TRACE_(dmdump)(": name: %s\n", debugstr_w(This->Desc.wszName));
596                                                                                                         break;
597                                                                                                 }
598                                                                                                 default: {
599                                                                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
600                                                                                                         liMove.QuadPart = Chunk.dwSize;
601                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
602                                                                                                         break;                                          
603                                                                                                 }
604                                                                                         }
605                                                                                         TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
606                                                                                 } while (ListCount[0] < ListSize[0]);
607                                                                                 break;
608                                                                         }
609                                                                         case DMUS_FOURCC_CONTAINED_OBJECTS_LIST: {
610                                                                                 TRACE_(dmfile)(": contained objects list\n");
611                                                                                 do {
612                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
613                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
614                                                                                         TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
615                                                                                         switch (Chunk.fccID) {
616                                                                                                 case FOURCC_LIST: {
617                                                                                                         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
618                                                                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
619                                                                                                         ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
620                                                                                                         ListCount[1] = 0;
621                                                                                                         switch (Chunk.fccID) {
622                                                                                                                 case DMUS_FOURCC_CONTAINED_OBJECT_LIST: {
623                                                                                                                         LPWINE_CONTAINER_ENTRY pNewEntry;
624                                                                                                                         TRACE_(dmfile)(": contained object list\n");
625                                                                                                                         pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_CONTAINER_ENTRY));
626                                                                                                                         DM_STRUCT_INIT(&pNewEntry->Desc);
627                                                                                                                         do {
628                                                                                                                                 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
629                                                                                                                                 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
630                                                                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
631                                                                                                                                 switch (Chunk.fccID) {
632                                                                                                                                         case DMUS_FOURCC_CONTAINED_ALIAS_CHUNK: {
633                                                                                                                                                 TRACE_(dmfile)(": alias chunk\n");
634                                                                                                                                                 pNewEntry->wszAlias = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
635                                                                                                                                                 IStream_Read (pStm, pNewEntry->wszAlias, Chunk.dwSize, NULL);
636                                                                                                                                                 TRACE_(dmdump)(": alias: %s\n", debugstr_w(pNewEntry->wszAlias));
637                                                                                                                                                 break;
638                                                                                                                                         }
639                                                                                                                                         case DMUS_FOURCC_CONTAINED_OBJECT_CHUNK: {
640                                                                                                                                                 DMUS_IO_CONTAINED_OBJECT_HEADER tmpObjectHeader;
641                                                                                                                                                 TRACE_(dmfile)(": contained object header chunk\n");
642                                                                                                                                                 IStream_Read (pStm, &tmpObjectHeader, Chunk.dwSize, NULL);
643                                                                                                                                                 TRACE_(dmdump)(": contained object header:\n%s\n", debugstr_DMUS_IO_CONTAINED_OBJECT_HEADER(&tmpObjectHeader));
644                                                                                                                                                 /* copy guidClass */
645                                                                                                                                                 pNewEntry->Desc.dwValidData |= DMUS_OBJ_CLASS;
646                                                                                                                                                 pNewEntry->Desc.guidClass = tmpObjectHeader.guidClassID;
647                                                                                                                                                 /* store flags */
648                                                                                                                                                 pNewEntry->dwFlags = tmpObjectHeader.dwFlags;
649                                                                                                                                                 break;
650                                                                                                                                         }
651                                                                                                                                         /* now read data... it may be safe to read everything after object header chunk, 
652                                                                                                                                                 but I'm not comfortable with MSDN's "the header is *normally* followed by ..." */
653                                                                                                                                         case FOURCC_LIST: {
654                                                                                                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
655                                                                                                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
656                                                                                                                                                 ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
657                                                                                                                                                 ListCount[2] = 0;
658                                                                                                                                                 switch (Chunk.fccID) {
659                                                                                                                                                         case DMUS_FOURCC_REF_LIST: {
660                                                                                                                                                                 TRACE_(dmfile)(": reference list\n");
661                                                                                                                                                                 pNewEntry->bIsRIFF = 0;
662                                                                                                                                                                 do {
663                                                                                                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
664                                                                                                                                                                         ListCount[2] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
665                                                                                                                                                                         TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
666                                                                                                                                                                         switch (Chunk.fccID) {
667                                                                                                                                                                                 case DMUS_FOURCC_REF_CHUNK: {
668                                                                                                                                                                                         DMUS_IO_REFERENCE tmpReferenceHeader; /* temporary structure */
669                                                                                                                                                                                         TRACE_(dmfile)(": reference header chunk\n");
670                                                                                                                                                                                         memset (&tmpReferenceHeader, 0, sizeof(DMUS_IO_REFERENCE));
671                                                                                                                                                                                         IStream_Read (pStm, &tmpReferenceHeader, Chunk.dwSize, NULL);
672                                                                                                                                                                                         /* copy retrieved data to DMUS_OBJECTDESC */
673                                                                                                                                                                                         if (!IsEqualCLSID (&pNewEntry->Desc.guidClass, &tmpReferenceHeader.guidClassID)) ERR(": object header declares different CLSID than reference header?\n");
674                                                                                                                                                                                         /* it shouldn't be necessary to copy guidClass, since it was set in contained object header already...
675                                                                                                                                                                                            yet if they happen to be different, I'd rather stick to this one */
676                                                                                                                                                                                         pNewEntry->Desc.guidClass = tmpReferenceHeader.guidClassID;
677                                                                                                                                                                                         pNewEntry->Desc.dwValidData |= tmpReferenceHeader.dwValidData;
678                                                                                                                                                                                         break;                                                                                                                                  
679                                                                                                                                                                                 }
680                                                                                                                                                                                 case DMUS_FOURCC_GUID_CHUNK: {
681                                                                                                                                                                                         TRACE_(dmfile)(": guid chunk\n");
682                                                                                                                                                                                         /* no need to set flags since they were copied from reference header */
683                                                                                                                                                                                         IStream_Read (pStm, &pNewEntry->Desc.guidObject, Chunk.dwSize, NULL);
684                                                                                                                                                                                         break;
685                                                                                                                                                                                 }
686                                                                                                                                                                                 case DMUS_FOURCC_DATE_CHUNK: {
687                                                                                                                                                                                         TRACE_(dmfile)(": file date chunk\n");
688                                                                                                                                                                                         /* no need to set flags since they were copied from reference header */
689                                                                                                                                                                                         IStream_Read (pStm, &pNewEntry->Desc.ftDate, Chunk.dwSize, NULL);
690                                                                                                                                                                                         break;
691                                                                                                                                                                                 }
692                                                                                                                                                                                 case DMUS_FOURCC_NAME_CHUNK: {
693                                                                                                                                                                                         TRACE_(dmfile)(": name chunk\n");
694                                                                                                                                                                                         /* no need to set flags since they were copied from reference header */
695                                                                                                                                                                                         IStream_Read (pStm, pNewEntry->Desc.wszName, Chunk.dwSize, NULL);
696                                                                                                                                                                                         break;
697                                                                                                                                                                                 }
698                                                                                                                                                                                 case DMUS_FOURCC_FILE_CHUNK: {
699                                                                                                                                                                                         TRACE_(dmfile)(": file name chunk\n");
700                                                                                                                                                                                         /* no need to set flags since they were copied from reference header */
701                                                                                                                                                                                         IStream_Read (pStm, pNewEntry->Desc.wszFileName, Chunk.dwSize, NULL);
702                                                                                                                                                                                         break;
703                                                                                                                                                                                 }
704                                                                                                                                                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
705                                                                                                                                                                                         TRACE_(dmfile)(": category chunk\n");
706                                                                                                                                                                                         /* no need to set flags since they were copied from reference header */
707                                                                                                                                                                                         IStream_Read (pStm, pNewEntry->Desc.wszCategory, Chunk.dwSize, NULL);
708                                                                                                                                                                                         break;
709                                                                                                                                                                                 }
710                                                                                                                                                                                 case DMUS_FOURCC_VERSION_CHUNK: {
711                                                                                                                                                                                         TRACE_(dmfile)(": version chunk\n");
712                                                                                                                                                                                         /* no need to set flags since they were copied from reference header */
713                                                                                                                                                                                         IStream_Read (pStm, &pNewEntry->Desc.vVersion, Chunk.dwSize, NULL);
714                                                                                                                                                                                         break;
715                                                                                                                                                                                 }
716                                                                                                                                                                                 default: {
717                                                                                                                                                                                         TRACE_(dmfile)(": unknown chunk (skipping)\n");
718                                                                                                                                                                                         liMove.QuadPart = Chunk.dwSize;
719                                                                                                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip this chunk */
720                                                                                                                                                                                         break;
721                                                                                                                                                                                 }
722                                                                                                                                                                         }
723                                                                                                                                                                         TRACE_(dmfile)(": ListCount[2] = 0x%08X < ListSize[2] = 0x%08X\n", ListCount[2], ListSize[2]);
724                                                                                                                                                                 } while (ListCount[2] < ListSize[2]);
725                                                                                                                                                                 break;
726                                                                                                                                                         }
727                                                                                                                                                         default: {
728                                                                                                                                                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
729                                                                                                                                                                 return E_FAIL;
730                                                                                                                                                         }
731                                                                                                                                                 }
732                                                                                                                                                 break;
733                                                                                                                                         }
734                                                                                                                                         
735                                                                                                                                         case FOURCC_RIFF: {
736                                                                                                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
737                                                                                                                                                 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
738                                                                                                                                                 if (IS_VALID_DMFORM (Chunk.fccID)) {
739                                                                                                                                                         TRACE_(dmfile)(": valid DMUSIC form\n");
740                                                                                                                                                         pNewEntry->bIsRIFF = 1;
741                                                                                                                                                         /* we'll have to skip whole RIFF chunk after SetObject is called */
742                                                                                                                                                         liMove.QuadPart = 0;
743                                                                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &uliPos);
744                                                                                                                                                         uliPos.QuadPart += (Chunk.dwSize - sizeof(FOURCC)); /* set uliPos at the end of RIFF chunk */                                                                                                                                                   
745                                                                                                                                                         /* move at the beginning of RIFF chunk */
746                                                                                                                                                         liMove.QuadPart = 0;
747                                                                                                                                                         liMove.QuadPart -= (sizeof(FOURCC)+sizeof(DWORD)+sizeof(FOURCC));
748                                                                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
749                                                                                                                                                         /* put pointer to stream in descriptor */
750                                                                                                                                                         pNewEntry->Desc.dwValidData |= DMUS_OBJ_STREAM;
751                                                                                                                                                         pNewEntry->Desc.pStream = pStm; /* we don't have to worry about cloning, since SetObject will perform it */
752                                                                                                                                                         /* wait till we get on the end of object list */
753                                                                                                                                                 } else {
754                                                                                                                                                         TRACE_(dmfile)(": invalid DMUSIC form (skipping)\n");
755                                                                                                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
756                                                                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
757                                                                                                                                                         /* FIXME: should we return E_FAIL? */
758                                                                                                                                                 }
759                                                                                                                                                 break;
760                                                                                                                                         }
761                                                                                                                                         default: {
762                                                                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
763                                                                                                                                                 liMove.QuadPart = Chunk.dwSize;
764                                                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
765                                                                                                                                                 break;                                          
766                                                                                                                                         }
767                                                                                                                                 }
768                                                                                                                                 TRACE_(dmfile)(": ListCount[1] = 0x%08X < ListSize[1] = 0x%08X\n", ListCount[1], ListSize[1]);
769                                                                                                                         } while (ListCount[1] < ListSize[1]);
770                                                                                                                         /* SetObject: this will fill descriptor with additional info    and add alias in loader's cache */
771                                                                                                                         IDirectMusicLoader_SetObject (pLoader, &pNewEntry->Desc);
772                                                                                                                         /* now that SetObject collected appropriate info into descriptor we can live happily ever after;
773                                                                                                                            or not, since we have to clean evidence of loading through stream... *sigh*
774                                                                                                                            and we have to skip rest of the chunk, if we loaded through RIFF */
775                                                                                                                         if (pNewEntry->bIsRIFF) {
776                                                                                                                                 liMove.QuadPart = uliPos.QuadPart;
777                                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_SET, NULL);
778                                                                                                                                 pNewEntry->Desc.dwValidData &= ~DMUS_OBJ_STREAM; /* clear flag (and with bitwise complement) */
779                                                                                                                                 pNewEntry->Desc.pStream = NULL;                                                                                 
780                                                                                                                         }
781                                                                                                                         /* add entry to list of objects */
782                                                                                                                         list_add_tail (This->pContainedObjects, &pNewEntry->entry);
783                                                                                                                         break;
784                                                                                                                 }
785                                                                                                                 default: {
786                                                                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
787                                                                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
788                                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
789                                                                                                                         break;                                          
790                                                                                                                 }
791                                                                                                         }
792                                                                                                         break;
793                                                                                                 }
794                                                                                                 default: {
795                                                                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
796                                                                                                         liMove.QuadPart = Chunk.dwSize;
797                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
798                                                                                                         break;                                          
799                                                                                                 }
800                                                                                         }
801                                                                                         TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
802                                                                                 } while (ListCount[0] < ListSize[0]);
803                                                                                 break;
804                                                                         }                                                                       
805                                                                         default: {
806                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
807                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
808                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
809                                                                                 break;                                          
810                                                                         }
811                                                                 }
812                                                                 break;
813                                                         }       
814                                                         default: {
815                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
816                                                                 liMove.QuadPart = Chunk.dwSize;
817                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
818                                                                 break;                                          
819                                                         }
820                                                 }
821                                                 TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
822                                         } while (StreamCount < StreamSize);
823                                         break;
824                                 }
825                                 default: {
826                                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
827                                         liMove.QuadPart = StreamSize;
828                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
829                                         return E_FAIL;
830                                 }
831                         }
832                         TRACE_(dmfile)(": reading finished\n");
833                         This->Desc.dwValidData |= DMUS_OBJ_LOADED;
834                         break;
835                 }
836                 default: {
837                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
838                         liMove.QuadPart = Chunk.dwSize;
839                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
840                         return E_FAIL;
841                 }
842         }
843         
844         /* now, if DMUS_CONTAINER_NOLOADS is not set, we are supposed to load contained objects;
845            so when we call GetObject later, they'll already be in cache */
846         if (!(This->Header.dwFlags & DMUS_CONTAINER_NOLOADS)) {
847                 struct list *pEntry;
848                 LPWINE_CONTAINER_ENTRY pContainedObject;
849
850                 TRACE(": DMUS_CONTAINER_NOLOADS not set... load all objects\n");
851                 
852                 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
853                         IDirectMusicObject* pObject;
854                         pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);            
855                         /* get object from loader and then release it */
856                         if (SUCCEEDED(IDirectMusicLoader_GetObject (pLoader, &pContainedObject->Desc, &IID_IDirectMusicObject, (LPVOID*)&pObject))) {
857                                 pContainedObject->pObject = pObject; /* for final release */
858                                 IDirectMusicObject_Release (pObject); /* we don't really need this one */
859                         } else {
860                                 WARN(": failed to load contained object\n");
861                                 result = DMUS_S_PARTIALLOAD;
862                         }
863                 }
864         }
865         
866         IDirectMusicLoader_Release (pLoader); /* release loader */
867
868 #if 0
869         /* DEBUG: dumps whole container object tree: */
870         if (TRACE_ON(dmloader)) {
871                 int r = 0;
872                 LPWINE_CONTAINER_ENTRY tmpEntry;
873                 struct list *listEntry;
874
875                 TRACE("*** IDirectMusicContainer (%p) ***\n", This->ContainerVtbl);
876                 TRACE(" - Objects:\n");
877                 LIST_FOR_EACH (listEntry, This->pContainedObjects) {
878                         tmpEntry = LIST_ENTRY( listEntry, WINE_CONTAINER_ENTRY, entry );
879                         TRACE("    - Object[%i]:\n", r);
880                         TRACE("       - wszAlias: %s\n", debugstr_w(tmpEntry->wszAlias));
881                         TRACE("       - Object descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC(&tmpEntry->Desc));
882                         r++;
883                 }
884         }
885 #endif
886
887         return result;
888 }
889
890 static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
891         ERR(": should not be needed\n");
892         return E_NOTIMPL;
893 }
894
895 static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
896         ERR(": should not be needed\n");
897         return E_NOTIMPL;
898 }
899
900 static const IPersistStreamVtbl DirectMusicContainer_PersistStream_Vtbl = {
901         IDirectMusicContainerImpl_IPersistStream_QueryInterface,
902         IDirectMusicContainerImpl_IPersistStream_AddRef,
903         IDirectMusicContainerImpl_IPersistStream_Release,
904         IDirectMusicContainerImpl_IPersistStream_GetClassID,
905         IDirectMusicContainerImpl_IPersistStream_IsDirty,
906         IDirectMusicContainerImpl_IPersistStream_Load,
907         IDirectMusicContainerImpl_IPersistStream_Save,
908         IDirectMusicContainerImpl_IPersistStream_GetSizeMax
909 };
910
911 /* for ClassFactory */
912 HRESULT WINAPI DMUSIC_CreateDirectMusicContainerImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
913         IDirectMusicContainerImpl* obj;
914
915         obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicContainerImpl));
916         if (NULL == obj) {
917                 *ppobj = NULL;
918                 return E_OUTOFMEMORY;
919         }
920         obj->ContainerVtbl = &DirectMusicContainer_Container_Vtbl;
921         obj->ObjectVtbl = &DirectMusicContainer_Object_Vtbl;
922         obj->PersistStreamVtbl = &DirectMusicContainer_PersistStream_Vtbl;
923         obj->dwRef = 0; /* will be inited by QueryInterface */
924         obj->pContainedObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
925         list_init (obj->pContainedObjects);
926
927         /* increase number of instances */
928         InterlockedIncrement (&dwDirectMusicContainer);
929         
930         return IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface ((LPDIRECTMUSICCONTAINER)&obj->ContainerVtbl, lpcGUID, ppobj);
931 }