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