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