Implement A->W call for GetNamedSecurityInfo.
[wine] / dlls / dmband / band.c
1 /* IDirectMusicBand Implementation
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 "dmband_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmband);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25 const GUID IID_IDirectMusicBandPRIVATE = {0xda54db81,0x837d,0x11d1,{0x86,0xbc,0x00,0xc0,0x4f,0xbf,0x8f,0xef}};
26
27 /*****************************************************************************
28  * IDirectMusicBandImpl implementation
29  */
30 /* IDirectMusicBandImpl IUnknown part: */
31 HRESULT WINAPI IDirectMusicBandImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
32         ICOM_THIS_MULTI(IDirectMusicBandImpl, UnknownVtbl, iface);
33         
34         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
35         if (IsEqualIID (riid, &IID_IUnknown)) {
36                 *ppobj = (LPVOID)&This->UnknownVtbl;
37                 IDirectMusicBandImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
38                 return S_OK;    
39         } else if (IsEqualIID (riid, &IID_IDirectMusicBand)) {
40                 *ppobj = (LPVOID)&This->BandVtbl;
41                 IDirectMusicBandImpl_IDirectMusicBand_AddRef ((LPDIRECTMUSICBAND)&This->BandVtbl);
42                 return S_OK;
43         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
44                 *ppobj = (LPVOID)&This->ObjectVtbl;
45                 IDirectMusicBandImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);                
46                 return S_OK;
47         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
48                 *ppobj = (LPVOID)&This->PersistStreamVtbl;
49                 IDirectMusicBandImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);         
50                 return S_OK;
51         }
52         
53         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
54         return E_NOINTERFACE;
55 }
56
57 ULONG WINAPI IDirectMusicBandImpl_IUnknown_AddRef (LPUNKNOWN iface) {
58         ICOM_THIS_MULTI(IDirectMusicBandImpl, UnknownVtbl, iface);
59         TRACE("(%p) : AddRef from %ld\n", This, This->ref);
60         return ++(This->ref);
61 }
62
63 ULONG WINAPI IDirectMusicBandImpl_IUnknown_Release (LPUNKNOWN iface) {
64         ICOM_THIS_MULTI(IDirectMusicBandImpl, UnknownVtbl, iface);
65         ULONG ref = --This->ref;
66         TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
67         if (ref == 0) {
68                 HeapFree(GetProcessHeap(), 0, This);
69         }
70         return ref;
71 }
72
73 ICOM_VTABLE(IUnknown) DirectMusicBand_Unknown_Vtbl = {
74     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
75         IDirectMusicBandImpl_IUnknown_QueryInterface,
76         IDirectMusicBandImpl_IUnknown_AddRef,
77         IDirectMusicBandImpl_IUnknown_Release
78 };
79
80 /* IDirectMusicBandImpl IDirectMusicBand part: */
81 HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicBand_QueryInterface (LPDIRECTMUSICBAND iface, REFIID riid, LPVOID *ppobj) {
82         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
83         return IDirectMusicBandImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
84 }
85
86 ULONG WINAPI IDirectMusicBandImpl_IDirectMusicBand_AddRef (LPDIRECTMUSICBAND iface) {
87         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
88         return IDirectMusicBandImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
89 }
90
91 ULONG WINAPI IDirectMusicBandImpl_IDirectMusicBand_Release (LPDIRECTMUSICBAND iface) {
92         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
93         return IDirectMusicBandImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
94 }
95
96 HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicBand_CreateSegment (LPDIRECTMUSICBAND iface, IDirectMusicSegment** ppSegment) {
97         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
98         FIXME("(%p, %p): stub\n", This, ppSegment);
99         return S_OK;
100 }
101
102 HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicBand_Download (LPDIRECTMUSICBAND iface, IDirectMusicPerformance* pPerformance) {
103         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
104         FIXME("(%p, %p): stub\n", This, pPerformance);
105         return S_OK;
106 }
107
108 HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicBand_Unload (LPDIRECTMUSICBAND iface, IDirectMusicPerformance* pPerformance) {
109         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
110         FIXME("(%p, %p): stub\n", This, pPerformance);
111         return S_OK;
112 }
113
114 ICOM_VTABLE(IDirectMusicBand) DirectMusicBand_Band_Vtbl = {
115     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
116         IDirectMusicBandImpl_IDirectMusicBand_QueryInterface,
117         IDirectMusicBandImpl_IDirectMusicBand_AddRef,
118         IDirectMusicBandImpl_IDirectMusicBand_Release,
119         IDirectMusicBandImpl_IDirectMusicBand_CreateSegment,
120         IDirectMusicBandImpl_IDirectMusicBand_Download,
121         IDirectMusicBandImpl_IDirectMusicBand_Unload
122 };
123
124 /* IDirectMusicBandImpl IDirectMusicObject part: */
125 HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
126         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
127         return IDirectMusicBandImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
128 }
129
130 ULONG WINAPI IDirectMusicBandImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
131         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
132         return IDirectMusicBandImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
133 }
134
135 ULONG WINAPI IDirectMusicBandImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
136         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
137         return IDirectMusicBandImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
138 }
139
140 HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
141         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
142         TRACE("(%p, %p)\n", This, pDesc);
143         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
144         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
145         return S_OK;
146 }
147
148 HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
149         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
150         TRACE("(%p, %p): setting descriptor:\n", This, pDesc); debug_DMUS_OBJECTDESC (pDesc);
151         
152         /* According to MSDN, we should copy only given values, not whole struct */     
153         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
154                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
155         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
156                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));         
157         if (pDesc->dwValidData & DMUS_OBJ_NAME)
158                 strncpyW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
159         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
160                 strncpyW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);             
161         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
162                 strncpyW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);             
163         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
164                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
165         if (pDesc->dwValidData & DMUS_OBJ_DATE)
166                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                          
167         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
168                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
169                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
170         }
171         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
172                 /* according to MSDN, we copy the stream */
173                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
174         }
175         
176         /* add new flags */
177         This->pDesc->dwValidData |= pDesc->dwValidData;
178
179         return S_OK;
180 }
181
182 HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
183         DMUS_PRIVATE_CHUNK Chunk;
184         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
185         LARGE_INTEGER liMove; /* used when skipping chunks */
186
187         TRACE("(%p, %p)\n", pStream, pDesc);
188         
189         /* FIXME: should this be determined from stream? */
190         pDesc->dwValidData |= DMUS_OBJ_CLASS;
191         memcpy (&pDesc->guidClass, &CLSID_DirectMusicBand, sizeof(CLSID));
192         
193         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
194         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
195         switch (Chunk.fccID) {  
196                 case FOURCC_RIFF: {
197                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
198                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
199                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
200                         StreamCount = 0;
201                         if (Chunk.fccID == DMUS_FOURCC_BAND_FORM) {
202                                 TRACE_(dmfile)(": band form\n");
203                                 do {
204                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
205                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
206                                         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
207                                         switch (Chunk.fccID) {
208                                                 case DMUS_FOURCC_GUID_CHUNK: {
209                                                         TRACE_(dmfile)(": GUID chunk\n");
210                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
211                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
212                                                         break;
213                                                 }
214                                                 case DMUS_FOURCC_VERSION_CHUNK: {
215                                                         TRACE_(dmfile)(": version chunk\n");
216                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
217                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
218                                                         break;
219                                                 }
220                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
221                                                         TRACE_(dmfile)(": category chunk\n");
222                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
223                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
224                                                         break;
225                                                 }
226                                                 case FOURCC_LIST: {
227                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
228                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
229                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
230                                                         ListCount[0] = 0;
231                                                         switch (Chunk.fccID) {
232                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
233                                                                 case DMUS_FOURCC_UNFO_LIST: {
234                                                                         TRACE_(dmfile)(": UNFO list\n");
235                                                                         do {
236                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
237                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
238                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
239                                                                                 switch (Chunk.fccID) {
240                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
241                                              (though strings seem to be valid unicode) */
242                                                                                         case mmioFOURCC('I','N','A','M'):
243                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
244                                                                                                 TRACE_(dmfile)(": name chunk\n");
245                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
246                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
247                                                                                                 break;
248                                                                                         }
249                                                                                         case mmioFOURCC('I','A','R','T'):
250                                                                                         case DMUS_FOURCC_UART_CHUNK: {
251                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
252                                                                                                 liMove.QuadPart = Chunk.dwSize;
253                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
254                                                                                                 break;
255                                                                                         }
256                                                                                         case mmioFOURCC('I','C','O','P'):
257                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
258                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
259                                                                                                 liMove.QuadPart = Chunk.dwSize;
260                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
261                                                                                                 break;
262                                                                                         }
263                                                                                         case mmioFOURCC('I','S','B','J'):
264                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
265                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
266                                                                                                 liMove.QuadPart = Chunk.dwSize;
267                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
268                                                                                                 break;
269                                                                                         }
270                                                                                         case mmioFOURCC('I','C','M','T'):
271                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
272                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
273                                                                                                 liMove.QuadPart = Chunk.dwSize;
274                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
275                                                                                                 break;
276                                                                                         }
277                                                                                         default: {
278                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
279                                                                                                 liMove.QuadPart = Chunk.dwSize;
280                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
281                                                                                                 break;                                          
282                                                                                         }
283                                                                                 }
284                                                                                 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
285                                                                         } while (ListCount[0] < ListSize[0]);
286                                                                         break;
287                                                                 }
288                                                                 default: {
289                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
290                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
291                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
292                                                                         break;                                          
293                                                                 }
294                                                         }
295                                                         break;
296                                                 }       
297                                                 default: {
298                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
299                                                         liMove.QuadPart = Chunk.dwSize;
300                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
301                                                         break;                                          
302                                                 }
303                                         }
304                                         TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
305                                 } while (StreamCount < StreamSize);
306                                 break;
307                         } else {
308                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
309                                 liMove.QuadPart = StreamSize;
310                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
311                                 return E_FAIL;
312                         }
313                 
314                         TRACE_(dmfile)(": reading finished\n");
315                         break;
316                 }
317                 default: {
318                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
319                         liMove.QuadPart = Chunk.dwSize;
320                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
321                         return DMUS_E_INVALIDFILE;
322                 }
323         }       
324         
325         TRACE(": returning descriptor:\n"); debug_DMUS_OBJECTDESC (pDesc);
326         
327         return S_OK;
328 }
329
330 ICOM_VTABLE(IDirectMusicObject) DirectMusicBand_Object_Vtbl = {
331   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
332   IDirectMusicBandImpl_IDirectMusicObject_QueryInterface,
333   IDirectMusicBandImpl_IDirectMusicObject_AddRef,
334   IDirectMusicBandImpl_IDirectMusicObject_Release,
335   IDirectMusicBandImpl_IDirectMusicObject_GetDescriptor,
336   IDirectMusicBandImpl_IDirectMusicObject_SetDescriptor,
337   IDirectMusicBandImpl_IDirectMusicObject_ParseDescriptor
338 };
339
340 /* IDirectMusicBandImpl IPersistStream part: */
341 HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
342   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
343   return IDirectMusicBandImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
344 }
345
346 ULONG WINAPI IDirectMusicBandImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
347   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
348   return IDirectMusicBandImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
349 }
350
351 ULONG WINAPI IDirectMusicBandImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
352   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
353   return IDirectMusicBandImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl); 
354 }
355
356 HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
357   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
358   TRACE("(%p, %p)\n", This, pClassID);
359   memcpy(pClassID, &CLSID_DirectMusicBand, sizeof(CLSID));
360   return S_OK;
361 }
362
363 HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
364   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
365   FIXME("(%p): stub, always S_FALSE\n", This);
366   return S_FALSE;
367 }
368
369 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseInstrument (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
370   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
371   DMUS_PRIVATE_CHUNK Chunk;
372   DWORD ListSize[3], ListCount[3];
373   LARGE_INTEGER liMove; /* used when skipping chunks */
374   HRESULT hr;
375
376   DMUS_IO_INSTRUMENT inst;
377   LPDMUS_PRIVATE_INSTRUMENT pNewInstrument;
378   IDirectMusicObject* pObject = NULL;
379
380   if (pChunk->fccID != DMUS_FOURCC_INSTRUMENT_LIST) {
381     ERR_(dmfile)(": %s chunk should be a INSTRUMENT list\n", debugstr_fourcc (pChunk->fccID));
382     return E_FAIL;
383   }
384
385   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
386   ListCount[0] = 0;
387
388   do {
389     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
390     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
391     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
392     switch (Chunk.fccID) { 
393     case DMUS_FOURCC_INSTRUMENT_CHUNK: {
394       TRACE_(dmfile)(": Instrument chunk\n");
395       if (Chunk.dwSize != sizeof(DMUS_IO_INSTRUMENT)) return E_FAIL;
396       IStream_Read (pStm, &inst, sizeof(DMUS_IO_INSTRUMENT), NULL);
397       TRACE_(dmfile)(" - dwPatch: %lu\n", inst.dwPatch);
398       TRACE_(dmfile)(" - dwAssignPatch: %lu\n", inst.dwAssignPatch);
399       TRACE_(dmfile)(" - dwNoteRanges[0]: %lu\n", inst.dwNoteRanges[0]);
400       TRACE_(dmfile)(" - dwNoteRanges[1]: %lu\n", inst.dwNoteRanges[1]);
401       TRACE_(dmfile)(" - dwNoteRanges[2]: %lu\n", inst.dwNoteRanges[2]);
402       TRACE_(dmfile)(" - dwNoteRanges[3]: %lu\n", inst.dwNoteRanges[3]);
403       TRACE_(dmfile)(" - dwPChannel: %lu\n", inst.dwPChannel);
404       TRACE_(dmfile)(" - dwFlags: %lu\n", inst.dwFlags);
405       TRACE_(dmfile)(" - bPan: %u\n", inst.bPan);
406       TRACE_(dmfile)(" - bVolume: %u\n", inst.bVolume);
407       TRACE_(dmfile)(" - nTranspose: %d\n", inst.nTranspose);
408       TRACE_(dmfile)(" - dwChannelPriority: %lu\n", inst.dwChannelPriority);
409       TRACE_(dmfile)(" - nPitchBendRange: %d\n", inst.nPitchBendRange);
410       break;
411     } 
412     case FOURCC_LIST: {
413       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
414       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
415       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
416       ListCount[1] = 0;
417       switch (Chunk.fccID) { 
418       case DMUS_FOURCC_REF_LIST: {
419         FIXME_(dmfile)(": DMRF (DM References) list\n");
420         hr = IDirectMusicUtils_IPersistStream_ParseReference (iface,  &Chunk, pStm, &pObject);
421         if (FAILED(hr)) {
422           ERR(": could not load Reference\n");
423           return hr;
424         }
425         break;                                          
426       }
427       default: {
428         TRACE_(dmfile)(": unknown (skipping)\n");
429         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
430         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
431         break;                                          
432       }
433       } 
434       break;
435     }
436     default: {
437       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
438       liMove.QuadPart = Chunk.dwSize;
439       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
440       break;                                            
441     }
442     }
443     TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
444   } while (ListCount[0] < ListSize[0]);
445   
446   /*
447    * @TODO insert pNewInstrument into This
448    */
449   pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENT));
450   if (NULL == pNewInstrument) {
451     ERR(": no more memory\n");
452     return  E_OUTOFMEMORY;
453   }
454   memcpy(&pNewInstrument->pInstrument, &inst, sizeof(DMUS_IO_INSTRUMENT));
455   pNewInstrument->ppReferenceCollection = NULL;
456   if (NULL != pObject) {
457     IDirectMusicCollection* pCol = NULL;
458     hr = IDirectMusicObject_QueryInterface (pObject, &IID_IDirectMusicCollection, (void**) &pCol);
459     if (FAILED(hr)) {
460       ERR(": failed to get IDirectMusicCollection Interface from DMObject\n");
461       return hr;
462     }
463     pNewInstrument->ppReferenceCollection = pCol;
464     IDirectMusicObject_Release(pObject);
465   }
466   list_add_tail (&This->Instruments, &pNewInstrument->entry);
467
468   return S_OK;
469 }
470
471 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseInstrumentsList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
472   /*ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);*/
473   HRESULT hr;
474   DMUS_PRIVATE_CHUNK Chunk;
475   DWORD ListSize[3], ListCount[3];
476   LARGE_INTEGER liMove; /* used when skipping chunks */
477
478   if (pChunk->fccID != DMUS_FOURCC_INSTRUMENTS_LIST) {
479     ERR_(dmfile)(": %s chunk should be a INSTRUMENTS list\n", debugstr_fourcc (pChunk->fccID));
480     return E_FAIL;
481   }  
482
483   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
484   ListCount[0] = 0;
485
486   do {
487     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
488     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
489     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
490     switch (Chunk.fccID) {
491     case FOURCC_LIST: {
492       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
493       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
494       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
495       ListCount[1] = 0;
496       switch (Chunk.fccID) { 
497       case DMUS_FOURCC_INSTRUMENT_LIST: {
498         TRACE_(dmfile)(": Instrument list\n");
499         hr = IDirectMusicBandImpl_IPersistStream_ParseInstrument (iface, &Chunk, pStm);
500         if (FAILED(hr)) return hr;
501         break;
502       }
503       default: {
504         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
505         liMove.QuadPart = ListSize[1];
506         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
507         break;                                          
508       }
509       }
510       break;
511     }
512     default: {
513       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
514       liMove.QuadPart = Chunk.dwSize;
515       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
516       break;                                            
517     }
518     }
519     TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
520   } while (ListCount[0] < ListSize[0]);
521
522   return S_OK;
523 }
524
525 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseBandForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
526
527   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
528   HRESULT hr = E_FAIL;
529   DMUS_PRIVATE_CHUNK Chunk;
530   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
531   LARGE_INTEGER liMove; /* used when skipping chunks */
532
533   GUID tmp_guid;
534
535   if (pChunk->fccID != DMUS_FOURCC_BAND_FORM) {
536     ERR_(dmfile)(": %s chunk should be a BAND form\n", debugstr_fourcc (pChunk->fccID));
537     return E_FAIL;
538   }  
539
540   StreamSize = pChunk->dwSize - sizeof(FOURCC);
541   StreamCount = 0;
542
543   do {
544     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
545     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
546     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
547
548     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
549     if (FAILED(hr)) return hr;
550     
551     if (hr == S_FALSE) {
552       switch (Chunk.fccID) {
553       case DMUS_FOURCC_GUID_CHUNK: {
554         TRACE_(dmfile)(": GUID\n");
555         IStream_Read (pStm, &tmp_guid, sizeof(GUID), NULL);
556         TRACE_(dmfile)(" - guid: %s\n", debugstr_dmguid(&tmp_guid));
557         break;
558       }
559       case FOURCC_LIST: {
560         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
561         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
562         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
563         ListCount[0] = 0;
564         switch (Chunk.fccID) {
565         case DMUS_FOURCC_UNFO_LIST: { 
566           TRACE_(dmfile)(": UNFO list\n");
567           do {
568             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
569             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
570             TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
571             
572             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
573             if (FAILED(hr)) return hr;
574             
575             if (hr == S_FALSE) {
576               switch (Chunk.fccID) {
577               default: {
578                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
579                 liMove.QuadPart = Chunk.dwSize;
580                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
581                 break;                                          
582               }
583               }
584             }
585             TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
586           } while (ListCount[0] < ListSize[0]);
587           break;
588         }
589         case DMUS_FOURCC_INSTRUMENTS_LIST: {
590           TRACE_(dmfile)(": INSTRUMENTS list\n");
591           hr = IDirectMusicBandImpl_IPersistStream_ParseInstrumentsList (iface, &Chunk, pStm);
592           if (FAILED(hr)) return hr;
593           break;        
594         }
595         default: {
596           TRACE_(dmfile)(": unknown (skipping)\n");
597           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
598           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
599           break;                                                
600         }
601         }
602         break;
603       }
604       default: {
605         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
606         liMove.QuadPart = Chunk.dwSize;
607         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
608         break;                                          
609       }
610       }
611     }
612     TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
613   } while (StreamCount < StreamSize);  
614
615   return S_OK;
616 }
617
618 HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
619   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
620
621   DMUS_PRIVATE_CHUNK Chunk;
622   LARGE_INTEGER liMove;
623   HRESULT hr;
624   
625   TRACE("(%p,%p): loading\n", This, pStm);
626   
627   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
628   TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
629   switch (Chunk.fccID) {
630   case FOURCC_RIFF: {
631     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
632     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
633     switch (Chunk.fccID) {
634     case DMUS_FOURCC_BAND_FORM: {
635       TRACE_(dmfile)(": Band form\n");
636       hr = IDirectMusicBandImpl_IPersistStream_ParseBandForm (iface, &Chunk, pStm);
637       if (FAILED(hr)) return hr;
638       break;    
639     }
640     default: {
641       TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
642       liMove.QuadPart = Chunk.dwSize;
643       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
644       return E_FAIL;
645     }
646     }
647     TRACE_(dmfile)(": reading finished\n");
648     break;
649   }
650   default: {
651     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
652     liMove.QuadPart = Chunk.dwSize;
653     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
654     return E_FAIL;
655   }
656   }
657   
658   return S_OK;
659 }
660
661 HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
662   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
663   FIXME("(%p): Saving not implemented yet\n", This);
664   return E_NOTIMPL;
665 }
666
667 HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
668   return E_NOTIMPL;
669 }
670
671 ICOM_VTABLE(IPersistStream) DirectMusicBand_PersistStream_Vtbl = {
672   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
673   IDirectMusicBandImpl_IPersistStream_QueryInterface,
674   IDirectMusicBandImpl_IPersistStream_AddRef,
675   IDirectMusicBandImpl_IPersistStream_Release,
676   IDirectMusicBandImpl_IPersistStream_GetClassID,
677   IDirectMusicBandImpl_IPersistStream_IsDirty,
678   IDirectMusicBandImpl_IPersistStream_Load,
679   IDirectMusicBandImpl_IPersistStream_Save,
680   IDirectMusicBandImpl_IPersistStream_GetSizeMax
681 };
682
683 /* for ClassFactory */
684 HRESULT WINAPI DMUSIC_CreateDirectMusicBandImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
685   IDirectMusicBandImpl* obj;
686         
687   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandImpl));
688   if (NULL == obj) {
689     *ppobj = (LPVOID) NULL;
690     return E_OUTOFMEMORY;
691   }
692   obj->UnknownVtbl = &DirectMusicBand_Unknown_Vtbl;
693   obj->BandVtbl = &DirectMusicBand_Band_Vtbl;
694   obj->ObjectVtbl = &DirectMusicBand_Object_Vtbl;
695   obj->PersistStreamVtbl = &DirectMusicBand_PersistStream_Vtbl;
696   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
697   DM_STRUCT_INIT(obj->pDesc);
698   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
699   memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicBand, sizeof (CLSID));
700   obj->ref = 0; /* will be inited by QueryInterface */
701   list_init (&obj->Instruments);
702   
703   return IDirectMusicBandImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
704 }