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