dmstyle: Another NULL ptr check added (Coverity).
[wine] / dlls / dmstyle / style.c
1 /* IDirectMusicStyle8 Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  * Copyright (C) 2003-2004 Raphael Junqueira
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "dmstyle_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(dmstyle);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25
26 static ULONG WINAPI IDirectMusicStyle8Impl_IUnknown_AddRef (LPUNKNOWN iface);
27 static ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef (LPDIRECTMUSICSTYLE8 iface);
28 static ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface);
29 static ULONG WINAPI IDirectMusicStyle8Impl_IPersistStream_AddRef (LPPERSISTSTREAM iface);
30
31 /*****************************************************************************
32  * IDirectMusicStyleImpl implementation
33  */
34 /* IDirectMusicStyleImpl IUnknown part: */
35 static HRESULT WINAPI IDirectMusicStyle8Impl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
36         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, UnknownVtbl, iface);
37         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
38         
39         if (IsEqualIID (riid, &IID_IUnknown)) {
40                 *ppobj = &This->UnknownVtbl;
41                 IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
42                 return S_OK;    
43         } else if (IsEqualIID (riid, &IID_IDirectMusicStyle)) {
44                 *ppobj = &This->StyleVtbl;
45                 IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef ((LPDIRECTMUSICSTYLE8)&This->StyleVtbl);
46                 return S_OK;
47         } else if (IsEqualIID (riid, &IID_IDirectMusicStyle8)) {
48                 *ppobj = &This->StyleVtbl;
49                 IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef ((LPDIRECTMUSICSTYLE8)&This->StyleVtbl);
50                 return S_OK;
51         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
52                 *ppobj = &This->ObjectVtbl;
53                 IDirectMusicStyle8Impl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);              
54                 return S_OK;
55         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
56                 *ppobj = &This->PersistStreamVtbl;
57                 IDirectMusicStyle8Impl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);               
58                 return S_OK;
59         }
60         
61         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
62         return E_NOINTERFACE;
63 }
64
65 static ULONG WINAPI IDirectMusicStyle8Impl_IUnknown_AddRef (LPUNKNOWN iface) {
66         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, UnknownVtbl, iface);
67         ULONG ref = InterlockedIncrement(&This->ref);
68
69         TRACE("(%p): AddRef from %d\n", This, ref - 1);
70
71         DMSTYLE_LockModule();
72
73         return ref;
74 }
75
76 static ULONG WINAPI IDirectMusicStyle8Impl_IUnknown_Release (LPUNKNOWN iface) {
77         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, UnknownVtbl, iface);
78         ULONG ref = InterlockedDecrement(&This->ref);
79
80         TRACE("(%p): ReleaseRef to %d\n", This, ref);
81
82         if (ref == 0) {
83                 HeapFree(GetProcessHeap(), 0, This);
84         }
85         
86         DMSTYLE_UnlockModule();
87         
88         return ref;
89 }
90
91 static const IUnknownVtbl DirectMusicStyle8_Unknown_Vtbl = {
92         IDirectMusicStyle8Impl_IUnknown_QueryInterface,
93         IDirectMusicStyle8Impl_IUnknown_AddRef,
94         IDirectMusicStyle8Impl_IUnknown_Release
95 };
96
97 /* IDirectMusicStyle8Impl IDirectMusicStyle8 part: */
98 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_QueryInterface (LPDIRECTMUSICSTYLE8 iface, REFIID riid, LPVOID *ppobj) {
99         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
100         return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
101 }
102
103 static ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef (LPDIRECTMUSICSTYLE8 iface) {
104         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
105         return IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
106 }
107
108 static ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_Release (LPDIRECTMUSICSTYLE8 iface) {
109         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
110         return IDirectMusicStyle8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
111 }
112
113 /* IDirectMusicStyle8Impl IDirectMusicStyle(8) part: */
114 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetBand (LPDIRECTMUSICSTYLE8 iface, WCHAR* pwszName, IDirectMusicBand** ppBand) {
115         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
116         FIXME("(%p, %p, %p): stub\n", This, pwszName, ppBand);
117         return S_OK;
118 }
119
120 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumBand (LPDIRECTMUSICSTYLE8 iface, DWORD dwIndex, WCHAR* pwszName) {
121         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
122         FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
123         return S_OK;
124 }
125
126 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetDefaultBand (LPDIRECTMUSICSTYLE8 iface, IDirectMusicBand** ppBand) {
127         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
128         FIXME("(%p, %p): stub\n", This, ppBand);
129         return S_OK;
130 }
131
132 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumMotif (LPDIRECTMUSICSTYLE8 iface, DWORD dwIndex, WCHAR* pwszName) {
133         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
134         FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
135         return S_OK;
136 }
137
138 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetMotif (LPDIRECTMUSICSTYLE8 iface, WCHAR* pwszName, IDirectMusicSegment** ppSegment) {
139         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
140         FIXME("(%p, %p, %p): stub\n", This, pwszName, ppSegment);
141         return S_OK;
142 }
143
144 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetDefaultChordMap (LPDIRECTMUSICSTYLE8 iface, IDirectMusicChordMap** ppChordMap) {
145         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
146         FIXME("(%p, %p): stub\n", This, ppChordMap);
147         return S_OK;
148 }
149
150 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumChordMap (LPDIRECTMUSICSTYLE8 iface, DWORD dwIndex, WCHAR* pwszName) {
151         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
152         FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
153         return S_OK;
154 }
155
156 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetChordMap (LPDIRECTMUSICSTYLE8 iface, WCHAR* pwszName, IDirectMusicChordMap** ppChordMap) {
157         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
158         FIXME("(%p, %p, %p): stub\n", This, pwszName, ppChordMap);
159         return S_OK;
160 }
161
162 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetTimeSignature (LPDIRECTMUSICSTYLE8 iface, DMUS_TIMESIGNATURE* pTimeSig) {
163         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
164         FIXME("(%p, %p): stub\n", This, pTimeSig);
165         return S_OK;
166 }
167
168 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetEmbellishmentLength (LPDIRECTMUSICSTYLE8 iface, DWORD dwType, DWORD dwLevel, DWORD* pdwMin, DWORD* pdwMax) {
169         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
170         FIXME("(%p, %d, %d, %p, %p): stub\n", This, dwType, dwLevel, pdwMin, pdwMax);
171         return S_OK;
172 }
173
174 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_GetTempo (LPDIRECTMUSICSTYLE8 iface, double* pTempo) {
175         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
176         FIXME("(%p, %p): stub\n", This, pTempo);
177         return S_OK;
178 }
179
180 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumPattern (LPDIRECTMUSICSTYLE8 iface, DWORD dwIndex, DWORD dwPatternType, WCHAR* pwszName) {
181         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, StyleVtbl, iface);
182         FIXME("(%p, %d, %d, %p): stub\n", This, dwIndex, dwPatternType, pwszName);
183         return S_OK;
184 }
185
186 static const IDirectMusicStyle8Vtbl DirectMusicStyle8_Style_Vtbl = {
187         IDirectMusicStyle8Impl_IDirectMusicStyle8_QueryInterface,
188         IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef,
189         IDirectMusicStyle8Impl_IDirectMusicStyle8_Release,
190         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetBand,
191         IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumBand,
192         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetDefaultBand,
193         IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumMotif,
194         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetMotif,
195         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetDefaultChordMap,
196         IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumChordMap,
197         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetChordMap,
198         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetTimeSignature,
199         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetEmbellishmentLength,
200         IDirectMusicStyle8Impl_IDirectMusicStyle8_GetTempo,
201         IDirectMusicStyle8Impl_IDirectMusicStyle8_EnumPattern
202 };
203
204 /* IDirectMusicStyle8Impl IDirectMusicObject part: */
205 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
206         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
207         return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
208 }
209
210 static ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
211         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
212         return IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
213 }
214
215 static ULONG WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
216         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
217         return IDirectMusicStyle8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
218 }
219
220 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
221         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
222         TRACE("(%p, %p)\n", This, pDesc);
223         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
224         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
225         return S_OK;
226 }
227
228 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
229         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
230         TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
231
232         /* According to MSDN, we should copy only given values, not whole struct */     
233         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
234                 This->pDesc->guidObject = pDesc->guidObject;
235         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
236                 This->pDesc->guidClass = pDesc->guidClass;
237         if (pDesc->dwValidData & DMUS_OBJ_NAME)
238                 lstrcpynW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
239         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
240                 lstrcpynW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
241         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
242                 lstrcpynW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
243         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
244                 This->pDesc->vVersion = pDesc->vVersion;
245         if (pDesc->dwValidData & DMUS_OBJ_DATE)
246                 This->pDesc->ftDate = pDesc->ftDate;
247         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
248                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
249                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
250         }
251         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
252                 /* according to MSDN, we copy the stream */
253                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
254         }
255         
256         /* add new flags */
257         This->pDesc->dwValidData |= pDesc->dwValidData;
258
259         return S_OK;
260 }
261
262 static HRESULT WINAPI IDirectMusicStyle8Impl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
263         ICOM_THIS_MULTI(IDirectMusicStyle8Impl, ObjectVtbl, iface);
264         DMUS_PRIVATE_CHUNK Chunk;
265         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
266         LARGE_INTEGER liMove; /* used when skipping chunks */
267
268         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
269
270         /* FIXME: should this be determined from stream? */
271         pDesc->dwValidData |= DMUS_OBJ_CLASS;
272         pDesc->guidClass = CLSID_DirectMusicStyle;
273
274         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
275         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
276         switch (Chunk.fccID) {  
277                 case FOURCC_RIFF: {
278                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
279                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
280                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
281                         StreamCount = 0;
282                         if (Chunk.fccID == DMUS_FOURCC_STYLE_FORM) {
283                                 TRACE_(dmfile)(": style form\n");
284                                 do {
285                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
286                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
287                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
288                                         switch (Chunk.fccID) {
289                                                 case DMUS_FOURCC_GUID_CHUNK: {
290                                                         TRACE_(dmfile)(": GUID chunk\n");
291                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
292                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
293                                                         break;
294                                                 }
295                                                 case DMUS_FOURCC_VERSION_CHUNK: {
296                                                         TRACE_(dmfile)(": version chunk\n");
297                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
298                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
299                                                         break;
300                                                 }
301                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
302                                                         TRACE_(dmfile)(": category chunk\n");
303                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
304                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
305                                                         break;
306                                                 }
307                                                 case FOURCC_LIST: {
308                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
309                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
310                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
311                                                         ListCount[0] = 0;
312                                                         switch (Chunk.fccID) {
313                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
314                                                                 case DMUS_FOURCC_UNFO_LIST: {
315                                                                         TRACE_(dmfile)(": UNFO list\n");
316                                                                         do {
317                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
318                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
319                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
320                                                                                 switch (Chunk.fccID) {
321                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
322                                              (though strings seem to be valid unicode) */
323                                                                                         case mmioFOURCC('I','N','A','M'):
324                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
325                                                                                                 TRACE_(dmfile)(": name chunk\n");
326                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
327                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
328                                                                                                 break;
329                                                                                         }
330                                                                                         case mmioFOURCC('I','A','R','T'):
331                                                                                         case DMUS_FOURCC_UART_CHUNK: {
332                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
333                                                                                                 liMove.QuadPart = Chunk.dwSize;
334                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
335                                                                                                 break;
336                                                                                         }
337                                                                                         case mmioFOURCC('I','C','O','P'):
338                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
339                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
340                                                                                                 liMove.QuadPart = Chunk.dwSize;
341                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
342                                                                                                 break;
343                                                                                         }
344                                                                                         case mmioFOURCC('I','S','B','J'):
345                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
346                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
347                                                                                                 liMove.QuadPart = Chunk.dwSize;
348                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
349                                                                                                 break;
350                                                                                         }
351                                                                                         case mmioFOURCC('I','C','M','T'):
352                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
353                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
354                                                                                                 liMove.QuadPart = Chunk.dwSize;
355                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
356                                                                                                 break;
357                                                                                         }
358                                                                                         default: {
359                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
360                                                                                                 liMove.QuadPart = Chunk.dwSize;
361                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
362                                                                                                 break;                                          
363                                                                                         }
364                                                                                 }
365                                                                                 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
366                                                                         } while (ListCount[0] < ListSize[0]);
367                                                                         break;
368                                                                 }
369                                                                 default: {
370                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
371                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
372                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
373                                                                         break;                                          
374                                                                 }
375                                                         }
376                                                         break;
377                                                 }       
378                                                 default: {
379                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
380                                                         liMove.QuadPart = Chunk.dwSize;
381                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
382                                                         break;                                          
383                                                 }
384                                         }
385                                         TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
386                                 } while (StreamCount < StreamSize);
387                         } else {
388                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
389                                 liMove.QuadPart = StreamSize;
390                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
391                                 return E_FAIL;
392                         }
393                 
394                         TRACE_(dmfile)(": reading finished\n");
395                         break;
396                 }
397                 default: {
398                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
399                         liMove.QuadPart = Chunk.dwSize;
400                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
401                         return DMUS_E_INVALIDFILE;
402                 }
403         }       
404         
405         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
406         
407         return S_OK;
408 }
409
410 static const IDirectMusicObjectVtbl DirectMusicStyle8_Object_Vtbl = {
411   IDirectMusicStyle8Impl_IDirectMusicObject_QueryInterface,
412   IDirectMusicStyle8Impl_IDirectMusicObject_AddRef,
413   IDirectMusicStyle8Impl_IDirectMusicObject_Release,
414   IDirectMusicStyle8Impl_IDirectMusicObject_GetDescriptor,
415   IDirectMusicStyle8Impl_IDirectMusicObject_SetDescriptor,
416   IDirectMusicStyle8Impl_IDirectMusicObject_ParseDescriptor
417 };
418
419 /* IDirectMusicStyle8Impl IPersistStream part: */
420 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
421   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
422   return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
423 }
424
425 static ULONG WINAPI IDirectMusicStyle8Impl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
426   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
427   return IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
428 }
429
430 static ULONG WINAPI IDirectMusicStyle8Impl_IPersistStream_Release (LPPERSISTSTREAM iface) {
431   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
432   return IDirectMusicStyle8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
433 }
434
435 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
436   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
437   TRACE("(%p, %p)\n", This, pClassID);
438   *pClassID = CLSID_DirectMusicStyle;
439   return S_OK;
440 }
441
442 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
443   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
444   FIXME("(%p): stub, always S_FALSE\n", This);
445   return S_FALSE;
446 }
447
448 static HRESULT IDirectMusicStyle8Impl_IPersistStream_LoadBand (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicBand** ppBand) {
449
450   HRESULT hr = E_FAIL;
451   IPersistStream* pPersistStream = NULL;
452   
453   hr = CoCreateInstance (&CLSID_DirectMusicBand, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicBand, (LPVOID*) ppBand);
454   if (FAILED(hr)) {
455     ERR(": could not create object\n");
456     return hr;
457   }
458   /* acquire PersistStream interface */
459   hr = IDirectMusicBand_QueryInterface (*ppBand, &IID_IPersistStream, (LPVOID*) &pPersistStream);
460   if (FAILED(hr)) {
461     ERR(": could not acquire IPersistStream\n");
462     return hr;
463   }
464   /* load */
465   hr = IPersistStream_Load (pPersistStream, pClonedStream);
466   if (FAILED(hr)) {
467     ERR(": failed to load object\n");
468     return hr;
469   }
470   
471   /* release all loading-related stuff */
472   IPersistStream_Release (pPersistStream);
473
474   return S_OK;
475 }
476
477 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParsePartRefList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm, LPDMUS_PRIVATE_STYLE_MOTIF pNewMotif) {
478   /*ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);*/
479   HRESULT hr = E_FAIL;
480   DMUS_PRIVATE_CHUNK Chunk;
481   DWORD ListSize[3], ListCount[3];
482   LARGE_INTEGER liMove; /* used when skipping chunks */
483
484   LPDMUS_PRIVATE_STYLE_PARTREF_ITEM pNewItem = NULL;
485
486
487   if (pChunk->fccID != DMUS_FOURCC_PARTREF_LIST) {
488     ERR_(dmfile)(": %s chunk should be a PARTREF list\n", debugstr_fourcc (pChunk->fccID));
489     return E_FAIL;
490   }  
491
492   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
493   ListCount[0] = 0;
494
495   do {
496     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
497     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
498     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
499     switch (Chunk.fccID) {
500     case DMUS_FOURCC_PARTREF_CHUNK: {
501       TRACE_(dmfile)(": PartRef chunk\n");
502       pNewItem = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_PARTREF_ITEM));
503       if (!pNewItem) {
504         ERR(": no more memory\n");
505         return E_OUTOFMEMORY;
506       }
507       hr = IStream_Read (pStm, &pNewItem->part_ref, sizeof(DMUS_IO_PARTREF), NULL);
508       /*TRACE_(dmfile)(" - sizeof %lu\n",  sizeof(DMUS_IO_PARTREF));*/
509       list_add_tail (&pNewMotif->Items, &pNewItem->entry);      
510       DM_STRUCT_INIT(&pNewItem->desc);
511       break;
512     }    
513     case FOURCC_LIST: {
514       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
515       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
516       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
517       ListCount[1] = 0;
518       switch (Chunk.fccID) {  
519       case DMUS_FOURCC_UNFO_LIST: { 
520         TRACE_(dmfile)(": UNFO list\n");
521         do {
522           IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
523           ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
524           TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
525           
526           if (!pNewItem) {
527             ERR(": pNewItem not yet allocated, chunk order bad?\n");
528             return E_OUTOFMEMORY;
529           }
530           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewItem->desc);
531           if (FAILED(hr)) return hr;
532           
533           if (hr == S_FALSE) {
534             switch (Chunk.fccID) {
535             default: {
536               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
537               liMove.QuadPart = Chunk.dwSize;
538               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
539               break;                            
540             }
541             }
542           }  
543           TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
544         } while (ListCount[1] < ListSize[1]);
545         break;
546       }
547       default: {
548         TRACE_(dmfile)(": unknown chunk (skipping)\n");
549         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
550         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
551         break;                                          
552       }
553       }
554       break;
555     }
556     default: {
557       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
558       liMove.QuadPart = Chunk.dwSize;
559       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
560       break;                                            
561     }
562     }
563     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
564   } while (ListCount[0] < ListSize[0]);
565
566   return S_OK;
567 }
568
569 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParsePartList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
570
571   /*ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);*/
572   HRESULT hr = E_FAIL;
573   DMUS_PRIVATE_CHUNK Chunk;
574   DWORD ListSize[3], ListCount[3];
575   LARGE_INTEGER liMove; /* used when skipping chunks */
576
577   DMUS_OBJECTDESC desc;
578   DWORD dwSize = 0;
579   DWORD cnt = 0;
580
581   if (pChunk->fccID != DMUS_FOURCC_PART_LIST) {
582     ERR_(dmfile)(": %s chunk should be a PART list\n", debugstr_fourcc (pChunk->fccID));
583     return E_FAIL;
584   }  
585
586   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
587   ListCount[0] = 0;
588
589   do {
590     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
591     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
592     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
593     switch (Chunk.fccID) {
594     case DMUS_FOURCC_PART_CHUNK: {
595       TRACE_(dmfile)(": Part chunk (skipping for now)\n" );
596       liMove.QuadPart = Chunk.dwSize;
597       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
598       break;
599     }
600     case DMUS_FOURCC_NOTE_CHUNK: { 
601       TRACE_(dmfile)(": Note chunk (skipping for now)\n");
602       IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
603       cnt = (Chunk.dwSize - sizeof(DWORD));
604       TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
605       TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
606       if (cnt % dwSize != 0) {
607         ERR("Invalid Array Size\n");
608         return E_FAIL;
609       }
610       cnt /= dwSize;
611       /** skip for now */
612       liMove.QuadPart = cnt * dwSize;
613       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
614       break;
615     }
616     case DMUS_FOURCC_CURVE_CHUNK: { 
617       TRACE_(dmfile)(": Curve chunk (skipping for now)\n");
618       IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
619       cnt = (Chunk.dwSize - sizeof(DWORD));
620       TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
621       TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
622       if (cnt % dwSize != 0) {
623         ERR("Invalid Array Size\n");
624         return E_FAIL;
625       }
626       cnt /= dwSize;
627       /** skip for now */
628       liMove.QuadPart = cnt * dwSize;
629       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
630       break;
631     }
632     case DMUS_FOURCC_MARKER_CHUNK: { 
633       TRACE_(dmfile)(": Marker chunk (skipping for now)\n");
634       liMove.QuadPart = Chunk.dwSize;
635       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
636       break;
637     }
638     case DMUS_FOURCC_RESOLUTION_CHUNK: { 
639       TRACE_(dmfile)(": Resolution chunk (skipping for now)\n");
640       liMove.QuadPart = Chunk.dwSize;
641       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
642       break;
643     }
644     case DMUS_FOURCC_ANTICIPATION_CHUNK: { 
645       TRACE_(dmfile)(": Anticipation chunk (skipping for now)\n");
646       liMove.QuadPart = Chunk.dwSize;
647       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
648       break;
649     }
650     case FOURCC_LIST: {
651       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
652       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
653       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
654       ListCount[1] = 0;
655       switch (Chunk.fccID) { 
656       case DMUS_FOURCC_UNFO_LIST: { 
657         TRACE_(dmfile)(": UNFO list\n");
658         do {
659           IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
660           ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
661           TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
662           
663           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &desc);
664           if (FAILED(hr)) return hr;
665           
666           if (hr == S_FALSE) {
667             switch (Chunk.fccID) {
668             default: {
669               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
670               liMove.QuadPart = Chunk.dwSize;
671               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
672               break;                            
673             }
674             }
675           }  
676           TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
677         } while (ListCount[1] < ListSize[1]);
678         break;
679       }
680       default: {
681         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
682         liMove.QuadPart = Chunk.dwSize;
683         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
684         break;                                          
685       }
686       }
687     break;
688     }
689     default: {
690       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
691       liMove.QuadPart = Chunk.dwSize;
692       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
693       break;                                            
694     }
695     }
696     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
697   } while (ListCount[0] < ListSize[0]);
698
699   return S_OK;
700 }
701
702 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParsePatternList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
703
704   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
705   HRESULT hr = E_FAIL;
706   DMUS_PRIVATE_CHUNK Chunk;
707   DWORD ListSize[3], ListCount[3];
708   LARGE_INTEGER liMove; /* used when skipping chunks */
709
710   DMUS_OBJECTDESC desc;
711   IDirectMusicBand* pBand = NULL;
712   LPDMUS_PRIVATE_STYLE_MOTIF pNewMotif = NULL;
713
714   DM_STRUCT_INIT(&desc);
715
716   if (pChunk->fccID != DMUS_FOURCC_PATTERN_LIST) {
717     ERR_(dmfile)(": %s chunk should be a PATTERN list\n", debugstr_fourcc (pChunk->fccID));
718     return E_FAIL;
719   }  
720
721   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
722   ListCount[0] = 0;
723
724   do {
725     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
726     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
727     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
728     switch (Chunk.fccID) {
729     case DMUS_FOURCC_PATTERN_CHUNK: {
730       TRACE_(dmfile)(": Pattern chunk\n");
731       /** alloc new motif entry */
732       pNewMotif = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_MOTIF));
733       list_add_tail (&This->Motifs, &pNewMotif->entry);
734       if (NULL == pNewMotif) {
735         ERR(": no more memory\n");
736         return  E_OUTOFMEMORY;
737       }
738
739       IStream_Read (pStm, &pNewMotif->pattern, Chunk.dwSize, NULL);
740       /** TODO trace pattern */
741
742       /** reset all data, as a new pattern begin */
743       DM_STRUCT_INIT(&pNewMotif->desc);
744       list_init (&pNewMotif->Items);
745       break;
746     }
747     case DMUS_FOURCC_RHYTHM_CHUNK: { 
748       TRACE_(dmfile)(": Rhythm chunk\n");
749       IStream_Read (pStm, &pNewMotif->dwRhythm, sizeof(DWORD), NULL);
750       TRACE_(dmfile)(" - dwRhythm: %u\n", pNewMotif->dwRhythm);
751       /** TODO understand why some Chunks have size > 4 */
752       liMove.QuadPart = Chunk.dwSize - sizeof(DWORD);
753       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
754       break;
755     }
756     case DMUS_FOURCC_MOTIFSETTINGS_CHUNK: {
757       TRACE_(dmfile)(": MotifSettings chunk (skipping for now)\n");
758       IStream_Read (pStm, &pNewMotif->settings, Chunk.dwSize, NULL);
759       /** TODO trace settings */
760       break;
761     }
762     case FOURCC_RIFF: {
763       /**
764        * should be embedded Bands into pattern
765        */
766       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
767       TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
768       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
769       ListCount[1] = 0;
770       switch (Chunk.fccID) {
771       case DMUS_FOURCC_BAND_FORM: { 
772         LPSTREAM pClonedStream = NULL;
773         
774         TRACE_(dmfile)(": BAND RIFF\n");
775         
776         IStream_Clone (pStm, &pClonedStream);
777         
778         liMove.QuadPart = 0;
779         liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
780         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
781         
782         hr = IDirectMusicStyle8Impl_IPersistStream_LoadBand (iface, pClonedStream, &pBand);
783         if (FAILED(hr)) {
784           ERR(": could not load track\n");
785           return hr;
786         }
787         IStream_Release (pClonedStream);
788         
789         pNewMotif->pBand = pBand;
790         IDirectMusicBand_AddRef(pBand);
791
792         IDirectMusicTrack_Release(pBand); pBand = NULL;  /* now we can release it as it's inserted */
793         
794         /** now safe move the cursor */
795         liMove.QuadPart = ListSize[1];
796         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
797         
798         break;
799       }
800       default: {
801         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
802         liMove.QuadPart = ListSize[1];
803         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
804         break;
805       }
806       }
807       break;
808     }
809     case FOURCC_LIST: {
810       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
811       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
812       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
813       ListCount[1] = 0;
814       switch (Chunk.fccID) {
815       case DMUS_FOURCC_UNFO_LIST: { 
816         TRACE_(dmfile)(": UNFO list\n");
817         do {
818           IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
819           ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
820           TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
821           
822           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewMotif->desc);
823           if (FAILED(hr)) return hr;
824           
825           if (hr == S_FALSE) {
826             switch (Chunk.fccID) {
827             default: {
828               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
829               liMove.QuadPart = Chunk.dwSize;
830               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
831               break;                            
832             }
833             }
834           }  
835           TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
836         } while (ListCount[1] < ListSize[1]);
837         break;
838       }
839       case DMUS_FOURCC_PARTREF_LIST: {
840         TRACE_(dmfile)(": PartRef list\n");
841         hr = IDirectMusicStyle8Impl_IPersistStream_ParsePartRefList (iface, &Chunk, pStm, pNewMotif);
842         if (FAILED(hr)) return hr;
843         break;
844       }
845       default: {
846         TRACE_(dmfile)(": unknown (skipping)\n");
847         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
848         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
849         break;                                          
850       }
851       }
852       break;    
853     }
854     default: {
855       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
856       liMove.QuadPart = Chunk.dwSize;
857       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
858       break;                                            
859     }
860     }
861     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
862   } while (ListCount[0] < ListSize[0]);
863
864   return S_OK;
865 }
866
867 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParseStyleForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
868   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
869
870   HRESULT hr = E_FAIL;
871   DMUS_PRIVATE_CHUNK Chunk;
872   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
873   LARGE_INTEGER liMove; /* used when skipping chunks */
874
875   IDirectMusicBand* pBand = NULL;
876
877   if (pChunk->fccID != DMUS_FOURCC_STYLE_FORM) {
878     ERR_(dmfile)(": %s chunk should be a STYLE form\n", debugstr_fourcc (pChunk->fccID));
879     return E_FAIL;
880   }  
881
882   StreamSize = pChunk->dwSize - sizeof(FOURCC);
883   StreamCount = 0;
884
885   do {
886     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
887     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
888     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
889     
890     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
891     if (FAILED(hr)) return hr;
892
893     if (hr == S_FALSE) {
894       switch (Chunk.fccID) {
895       case DMUS_FOURCC_STYLE_CHUNK: {
896         TRACE_(dmfile)(": Style chunk\n");
897         IStream_Read (pStm, &This->style, sizeof(DMUS_IO_STYLE), NULL);
898         /** TODO dump DMUS_IO_TIMESIG style.timeSig */
899         TRACE_(dmfile)(" - dblTempo: %g\n", This->style.dblTempo);
900         break;
901       }   
902       case FOURCC_RIFF: {
903         /**
904          * should be embedded Bands into style
905          */
906         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
907         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
908         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
909         ListCount[0] = 0;
910         switch (Chunk.fccID) {
911         case DMUS_FOURCC_BAND_FORM: { 
912           LPSTREAM pClonedStream = NULL;
913           LPDMUS_PRIVATE_STYLE_BAND pNewBand;
914
915           TRACE_(dmfile)(": BAND RIFF\n");
916           
917           IStream_Clone (pStm, &pClonedStream);
918             
919           liMove.QuadPart = 0;
920           liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
921           IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
922           
923           hr = IDirectMusicStyle8Impl_IPersistStream_LoadBand (iface, pClonedStream, &pBand);
924           if (FAILED(hr)) {
925             ERR(": could not load track\n");
926             return hr;
927           }
928           IStream_Release (pClonedStream);
929           
930           pNewBand = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_BAND));
931           if (NULL == pNewBand) {
932             ERR(": no more memory\n");
933             return  E_OUTOFMEMORY;
934           }
935           pNewBand->pBand = pBand;
936           IDirectMusicBand_AddRef(pBand);
937           list_add_tail (&This->Bands, &pNewBand->entry);
938
939           IDirectMusicTrack_Release(pBand); pBand = NULL;  /* now we can release it as it's inserted */
940         
941           /** now safely move the cursor */
942           liMove.QuadPart = ListSize[0];
943           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
944           
945           break;
946         }
947         default: {
948           TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
949           liMove.QuadPart = ListSize[0];
950           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
951           break;
952         }
953         }
954         break;
955       }
956       case FOURCC_LIST: {
957         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
958         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
959         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
960         ListCount[0] = 0;
961         switch (Chunk.fccID) {
962         case DMUS_FOURCC_UNFO_LIST: { 
963           TRACE_(dmfile)(": UNFO list\n");
964           do {
965             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
966             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
967             TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
968             
969             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
970             if (FAILED(hr)) return hr;
971             
972             if (hr == S_FALSE) {
973               switch (Chunk.fccID) {
974               default: {
975                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
976                 liMove.QuadPart = Chunk.dwSize;
977                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
978                 break;                          
979               }
980               }
981             }  
982             TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
983           } while (ListCount[0] < ListSize[0]);
984           break;
985         }
986         case DMUS_FOURCC_PART_LIST: {
987           TRACE_(dmfile)(": PART list\n");
988           hr = IDirectMusicStyle8Impl_IPersistStream_ParsePartList (iface, &Chunk, pStm);
989           if (FAILED(hr)) return hr;
990           break;
991         }
992         case  DMUS_FOURCC_PATTERN_LIST: {
993           TRACE_(dmfile)(": PATTERN list\n");
994           hr = IDirectMusicStyle8Impl_IPersistStream_ParsePatternList (iface, &Chunk, pStm);
995           if (FAILED(hr)) return hr;
996           break;
997         }
998         default: {
999           TRACE_(dmfile)(": unknown (skipping)\n");
1000           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
1001           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1002           break;                                                
1003         }
1004         }
1005         break;
1006       }
1007       default: {
1008         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
1009         liMove.QuadPart = Chunk.dwSize;
1010         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1011         break;                                          
1012       }
1013       }
1014     }
1015     TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
1016   } while (StreamCount < StreamSize);  
1017
1018   return S_OK;
1019 }
1020
1021 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
1022   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1023   
1024   DMUS_PRIVATE_CHUNK Chunk;
1025   LARGE_INTEGER liMove; /* used when skipping chunks */
1026   HRESULT hr;
1027
1028   FIXME("(%p, %p): Loading\n", This, pStm);
1029
1030   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1031   TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1032   switch (Chunk.fccID) {
1033   case FOURCC_RIFF: {
1034     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
1035     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1036     switch (Chunk.fccID) {
1037     case DMUS_FOURCC_STYLE_FORM: {
1038       TRACE_(dmfile)(": Style form\n");
1039       hr = IDirectMusicStyle8Impl_IPersistStream_ParseStyleForm (iface, &Chunk, pStm);
1040       if (FAILED(hr)) return hr;
1041       break;
1042     }
1043     default: {
1044       TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
1045       liMove.QuadPart = Chunk.dwSize;
1046       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1047       return E_FAIL;
1048     }
1049     }
1050     TRACE_(dmfile)(": reading finished\n");
1051     break;
1052   }
1053   default: {
1054     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
1055     liMove.QuadPart = Chunk.dwSize;
1056     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1057     return E_FAIL;
1058   }
1059   }
1060   
1061   return S_OK;
1062 }
1063
1064 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
1065   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1066   FIXME("(%p): Saving not implemented yet\n", This);
1067   return E_NOTIMPL;
1068 }
1069
1070 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
1071   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1072   FIXME("(%p, %p): stub\n", This, pcbSize);
1073   return E_NOTIMPL;
1074
1075 }
1076
1077 static const IPersistStreamVtbl DirectMusicStyle8_PersistStream_Vtbl = {
1078   IDirectMusicStyle8Impl_IPersistStream_QueryInterface,
1079   IDirectMusicStyle8Impl_IPersistStream_AddRef,
1080   IDirectMusicStyle8Impl_IPersistStream_Release,
1081   IDirectMusicStyle8Impl_IPersistStream_GetClassID,
1082   IDirectMusicStyle8Impl_IPersistStream_IsDirty,
1083   IDirectMusicStyle8Impl_IPersistStream_Load,
1084   IDirectMusicStyle8Impl_IPersistStream_Save,
1085   IDirectMusicStyle8Impl_IPersistStream_GetSizeMax
1086 };
1087
1088 /* for ClassFactory */
1089 HRESULT WINAPI DMUSIC_CreateDirectMusicStyleImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
1090   IDirectMusicStyle8Impl* obj;
1091         
1092   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicStyle8Impl));
1093   if (NULL == obj) {
1094     *ppobj = NULL;
1095     return E_OUTOFMEMORY;
1096   }
1097   obj->UnknownVtbl = &DirectMusicStyle8_Unknown_Vtbl;
1098   obj->StyleVtbl = &DirectMusicStyle8_Style_Vtbl;
1099   obj->ObjectVtbl = &DirectMusicStyle8_Object_Vtbl;
1100   obj->PersistStreamVtbl = &DirectMusicStyle8_PersistStream_Vtbl;
1101   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
1102   DM_STRUCT_INIT(obj->pDesc);
1103   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
1104   obj->pDesc->guidClass = CLSID_DirectMusicStyle;
1105   obj->ref = 0; /* will be inited by QueryInterface */
1106   list_init (&obj->Bands);
1107   list_init (&obj->Motifs);
1108
1109   return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
1110 }