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