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