crypt32: Assign to structs instead of using memcpy.
[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 = (LPVOID)&This->UnknownVtbl;
41                 IDirectMusicStyle8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
42                 return S_OK;    
43         } else if (IsEqualIID (riid, &IID_IDirectMusicStyle)) {
44                 *ppobj = (LPVOID)&This->StyleVtbl;
45                 IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef ((LPDIRECTMUSICSTYLE8)&This->StyleVtbl);
46                 return S_OK;
47         } else if (IsEqualIID (riid, &IID_IDirectMusicStyle8)) {
48                 *ppobj = (LPVOID)&This->StyleVtbl;
49                 IDirectMusicStyle8Impl_IDirectMusicStyle8_AddRef ((LPDIRECTMUSICSTYLE8)&This->StyleVtbl);
50                 return S_OK;
51         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
52                 *ppobj = (LPVOID)&This->ObjectVtbl;
53                 IDirectMusicStyle8Impl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);              
54                 return S_OK;
55         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
56                 *ppobj = (LPVOID)&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                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
235         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
236                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (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                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
245         if (pDesc->dwValidData & DMUS_OBJ_DATE)
246                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (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         memcpy (&pDesc->guidClass, &CLSID_DirectMusicStyle, sizeof(CLSID));
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   memcpy(pClassID, &CLSID_DirectMusicStyle, sizeof(CLSID));
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 (NULL == 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           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewItem->desc);
527           if (FAILED(hr)) return hr;
528           
529           if (hr == S_FALSE) {
530             switch (Chunk.fccID) {
531             default: {
532               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
533               liMove.QuadPart = Chunk.dwSize;
534               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
535               break;                            
536             }
537             }
538           }  
539           TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
540         } while (ListCount[1] < ListSize[1]);
541         break;
542       }
543       default: {
544         TRACE_(dmfile)(": unknown chunk (skipping)\n");
545         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
546         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
547         break;                                          
548       }
549       }
550       break;
551     }
552     default: {
553       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
554       liMove.QuadPart = Chunk.dwSize;
555       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
556       break;                                            
557     }
558     }
559     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
560   } while (ListCount[0] < ListSize[0]);
561
562   return S_OK;
563 }
564
565 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParsePartList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
566
567   /*ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);*/
568   HRESULT hr = E_FAIL;
569   DMUS_PRIVATE_CHUNK Chunk;
570   DWORD ListSize[3], ListCount[3];
571   LARGE_INTEGER liMove; /* used when skipping chunks */
572
573   DMUS_OBJECTDESC desc;
574   DWORD dwSize = 0;
575   DWORD cnt = 0;
576
577   if (pChunk->fccID != DMUS_FOURCC_PART_LIST) {
578     ERR_(dmfile)(": %s chunk should be a PART list\n", debugstr_fourcc (pChunk->fccID));
579     return E_FAIL;
580   }  
581
582   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
583   ListCount[0] = 0;
584
585   do {
586     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
587     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
588     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
589     switch (Chunk.fccID) {
590     case DMUS_FOURCC_PART_CHUNK: {
591       TRACE_(dmfile)(": Part chunk (skipping for now)\n" );
592       liMove.QuadPart = Chunk.dwSize;
593       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
594       break;
595     }
596     case DMUS_FOURCC_NOTE_CHUNK: { 
597       TRACE_(dmfile)(": Note chunk (skipping for now)\n");
598       IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
599       cnt = (Chunk.dwSize - sizeof(DWORD));
600       TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
601       TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
602       if (cnt % dwSize != 0) {
603         ERR("Invalid Array Size\n");
604         return E_FAIL;
605       }
606       cnt /= dwSize;
607       /** skip for now */
608       liMove.QuadPart = cnt * dwSize;
609       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
610       break;
611     }
612     case DMUS_FOURCC_CURVE_CHUNK: { 
613       TRACE_(dmfile)(": Curve chunk (skipping for now)\n");
614       IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
615       cnt = (Chunk.dwSize - sizeof(DWORD));
616       TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
617       TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
618       if (cnt % dwSize != 0) {
619         ERR("Invalid Array Size\n");
620         return E_FAIL;
621       }
622       cnt /= dwSize;
623       /** skip for now */
624       liMove.QuadPart = cnt * dwSize;
625       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
626       break;
627     }
628     case DMUS_FOURCC_MARKER_CHUNK: { 
629       TRACE_(dmfile)(": Marker chunk (skipping for now)\n");
630       liMove.QuadPart = Chunk.dwSize;
631       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
632       break;
633     }
634     case DMUS_FOURCC_RESOLUTION_CHUNK: { 
635       TRACE_(dmfile)(": Resolution chunk (skipping for now)\n");
636       liMove.QuadPart = Chunk.dwSize;
637       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
638       break;
639     }
640     case DMUS_FOURCC_ANTICIPATION_CHUNK: { 
641       TRACE_(dmfile)(": Anticipation chunk (skipping for now)\n");
642       liMove.QuadPart = Chunk.dwSize;
643       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
644       break;
645     }
646     case FOURCC_LIST: {
647       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
648       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
649       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
650       ListCount[1] = 0;
651       switch (Chunk.fccID) { 
652       case DMUS_FOURCC_UNFO_LIST: { 
653         TRACE_(dmfile)(": UNFO list\n");
654         do {
655           IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
656           ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
657           TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
658           
659           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &desc);
660           if (FAILED(hr)) return hr;
661           
662           if (hr == S_FALSE) {
663             switch (Chunk.fccID) {
664             default: {
665               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
666               liMove.QuadPart = Chunk.dwSize;
667               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
668               break;                            
669             }
670             }
671           }  
672           TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
673         } while (ListCount[1] < ListSize[1]);
674         break;
675       }
676       default: {
677         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
678         liMove.QuadPart = Chunk.dwSize;
679         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
680         break;                                          
681       }
682       }
683     break;
684     }
685     default: {
686       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
687       liMove.QuadPart = Chunk.dwSize;
688       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
689       break;                                            
690     }
691     }
692     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
693   } while (ListCount[0] < ListSize[0]);
694
695   return S_OK;
696 }
697
698 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParsePatternList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
699
700   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
701   HRESULT hr = E_FAIL;
702   DMUS_PRIVATE_CHUNK Chunk;
703   DWORD ListSize[3], ListCount[3];
704   LARGE_INTEGER liMove; /* used when skipping chunks */
705
706   DMUS_OBJECTDESC desc;
707   IDirectMusicBand* pBand = NULL;
708   LPDMUS_PRIVATE_STYLE_MOTIF pNewMotif = NULL;
709
710   DM_STRUCT_INIT(&desc);
711
712   if (pChunk->fccID != DMUS_FOURCC_PATTERN_LIST) {
713     ERR_(dmfile)(": %s chunk should be a PATTERN list\n", debugstr_fourcc (pChunk->fccID));
714     return E_FAIL;
715   }  
716
717   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
718   ListCount[0] = 0;
719
720   do {
721     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
722     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
723     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
724     switch (Chunk.fccID) {
725     case DMUS_FOURCC_PATTERN_CHUNK: {
726       TRACE_(dmfile)(": Pattern chunk\n");
727       /** alloc new motif entry */
728       pNewMotif = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_MOTIF));
729       list_add_tail (&This->Motifs, &pNewMotif->entry);
730       if (NULL == pNewMotif) {
731         ERR(": no more memory\n");
732         return  E_OUTOFMEMORY;
733       }
734
735       IStream_Read (pStm, &pNewMotif->pattern, Chunk.dwSize, NULL);
736       /** TODO trace pattern */
737
738       /** reset all datas, as a new pattern begin */
739       DM_STRUCT_INIT(&pNewMotif->desc);
740       list_init (&pNewMotif->Items);
741       break;
742     }
743     case DMUS_FOURCC_RHYTHM_CHUNK: { 
744       TRACE_(dmfile)(": Rhythm chunk\n");
745       IStream_Read (pStm, &pNewMotif->dwRhythm, sizeof(DWORD), NULL);
746       TRACE_(dmfile)(" - dwRhythm: %u\n", pNewMotif->dwRhythm);
747       /** TODO understand why some Chunks have size > 4 */
748       liMove.QuadPart = Chunk.dwSize - sizeof(DWORD);
749       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
750       break;
751     }
752     case DMUS_FOURCC_MOTIFSETTINGS_CHUNK: {
753       TRACE_(dmfile)(": MotifSettings chunk (skipping for now)\n");
754       IStream_Read (pStm, &pNewMotif->settings, Chunk.dwSize, NULL);
755       /** TODO trace settings */
756       break;
757     }
758     case FOURCC_RIFF: {
759       /**
760        * sould be embededs Bands into pattern
761        */
762       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
763       TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
764       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
765       ListCount[1] = 0;
766       switch (Chunk.fccID) {
767       case DMUS_FOURCC_BAND_FORM: { 
768         LPSTREAM pClonedStream = NULL;
769         
770         TRACE_(dmfile)(": BAND RIFF\n");
771         
772         IStream_Clone (pStm, &pClonedStream);
773         
774         liMove.QuadPart = 0;
775         liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
776         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
777         
778         hr = IDirectMusicStyle8Impl_IPersistStream_LoadBand (iface, pClonedStream, &pBand);
779         if (FAILED(hr)) {
780           ERR(": could not load track\n");
781           return hr;
782         }
783         IStream_Release (pClonedStream);
784         
785         pNewMotif->pBand = pBand;
786         IDirectMusicBand_AddRef(pBand);
787
788         IDirectMusicTrack_Release(pBand); pBand = NULL;  /* now we can release at as it inserted */
789         
790         /** now safe move the cursor */
791         liMove.QuadPart = ListSize[1];
792         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
793         
794         break;
795       }
796       default: {
797         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
798         liMove.QuadPart = ListSize[1];
799         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
800         break;
801       }
802       }
803       break;
804     }
805     case FOURCC_LIST: {
806       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
807       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
808       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
809       ListCount[1] = 0;
810       switch (Chunk.fccID) {
811       case DMUS_FOURCC_UNFO_LIST: { 
812         TRACE_(dmfile)(": UNFO list\n");
813         do {
814           IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
815           ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
816           TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
817           
818           hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewMotif->desc);
819           if (FAILED(hr)) return hr;
820           
821           if (hr == S_FALSE) {
822             switch (Chunk.fccID) {
823             default: {
824               TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
825               liMove.QuadPart = Chunk.dwSize;
826               IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
827               break;                            
828             }
829             }
830           }  
831           TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
832         } while (ListCount[1] < ListSize[1]);
833         break;
834       }
835       case DMUS_FOURCC_PARTREF_LIST: {
836         TRACE_(dmfile)(": PartRef list\n");
837         hr = IDirectMusicStyle8Impl_IPersistStream_ParsePartRefList (iface, &Chunk, pStm, pNewMotif);
838         if (FAILED(hr)) return hr;
839         break;
840       }
841       default: {
842         TRACE_(dmfile)(": unknown (skipping)\n");
843         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
844         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
845         break;                                          
846       }
847       }
848       break;    
849     }
850     default: {
851       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
852       liMove.QuadPart = Chunk.dwSize;
853       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
854       break;                                            
855     }
856     }
857     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
858   } while (ListCount[0] < ListSize[0]);
859
860   return S_OK;
861 }
862
863 static HRESULT IDirectMusicStyle8Impl_IPersistStream_ParseStyleForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
864   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
865
866   HRESULT hr = E_FAIL;
867   DMUS_PRIVATE_CHUNK Chunk;
868   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
869   LARGE_INTEGER liMove; /* used when skipping chunks */
870
871   IDirectMusicBand* pBand = NULL;
872
873   if (pChunk->fccID != DMUS_FOURCC_STYLE_FORM) {
874     ERR_(dmfile)(": %s chunk should be a STYLE form\n", debugstr_fourcc (pChunk->fccID));
875     return E_FAIL;
876   }  
877
878   StreamSize = pChunk->dwSize - sizeof(FOURCC);
879   StreamCount = 0;
880
881   do {
882     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
883     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
884     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
885     
886     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
887     if (FAILED(hr)) return hr;
888
889     if (hr == S_FALSE) {
890       switch (Chunk.fccID) {
891       case DMUS_FOURCC_STYLE_CHUNK: {
892         TRACE_(dmfile)(": Style chunk\n");
893         IStream_Read (pStm, &This->style, sizeof(DMUS_IO_STYLE), NULL);
894         /** TODO dump DMUS_IO_TIMESIG style.timeSig */
895         TRACE_(dmfile)(" - dblTempo: %g\n", This->style.dblTempo);
896         break;
897       }   
898       case FOURCC_RIFF: {
899         /**
900          * sould be embededs Bands into style
901          */
902         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
903         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
904         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
905         ListCount[0] = 0;
906         switch (Chunk.fccID) {
907         case DMUS_FOURCC_BAND_FORM: { 
908           LPSTREAM pClonedStream = NULL;
909           LPDMUS_PRIVATE_STYLE_BAND pNewBand;
910
911           TRACE_(dmfile)(": BAND RIFF\n");
912           
913           IStream_Clone (pStm, &pClonedStream);
914             
915           liMove.QuadPart = 0;
916           liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
917           IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
918           
919           hr = IDirectMusicStyle8Impl_IPersistStream_LoadBand (iface, pClonedStream, &pBand);
920           if (FAILED(hr)) {
921             ERR(": could not load track\n");
922             return hr;
923           }
924           IStream_Release (pClonedStream);
925           
926           pNewBand = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_STYLE_BAND));
927           if (NULL == pNewBand) {
928             ERR(": no more memory\n");
929             return  E_OUTOFMEMORY;
930           }
931           pNewBand->pBand = pBand;
932           IDirectMusicBand_AddRef(pBand);
933           list_add_tail (&This->Bands, &pNewBand->entry);
934
935           IDirectMusicTrack_Release(pBand); pBand = NULL;  /* now we can release at as it inserted */
936         
937           /** now safe move the cursor */
938           liMove.QuadPart = ListSize[0];
939           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
940           
941           break;
942         }
943         default: {
944           TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
945           liMove.QuadPart = ListSize[0];
946           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
947           break;
948         }
949         }
950         break;
951       }
952       case FOURCC_LIST: {
953         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
954         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
955         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
956         ListCount[0] = 0;
957         switch (Chunk.fccID) {
958         case DMUS_FOURCC_UNFO_LIST: { 
959           TRACE_(dmfile)(": UNFO list\n");
960           do {
961             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
962             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
963             TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
964             
965             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
966             if (FAILED(hr)) return hr;
967             
968             if (hr == S_FALSE) {
969               switch (Chunk.fccID) {
970               default: {
971                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
972                 liMove.QuadPart = Chunk.dwSize;
973                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
974                 break;                          
975               }
976               }
977             }  
978             TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
979           } while (ListCount[0] < ListSize[0]);
980           break;
981         }
982         case DMUS_FOURCC_PART_LIST: {
983           TRACE_(dmfile)(": PART list\n");
984           hr = IDirectMusicStyle8Impl_IPersistStream_ParsePartList (iface, &Chunk, pStm);
985           if (FAILED(hr)) return hr;
986           break;
987         }
988         case  DMUS_FOURCC_PATTERN_LIST: {
989           TRACE_(dmfile)(": PATTERN list\n");
990           hr = IDirectMusicStyle8Impl_IPersistStream_ParsePatternList (iface, &Chunk, pStm);
991           if (FAILED(hr)) return hr;
992           break;
993         }
994         default: {
995           TRACE_(dmfile)(": unknown (skipping)\n");
996           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
997           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
998           break;                                                
999         }
1000         }
1001         break;
1002       }
1003       default: {
1004         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
1005         liMove.QuadPart = Chunk.dwSize;
1006         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1007         break;                                          
1008       }
1009       }
1010     }
1011     TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
1012   } while (StreamCount < StreamSize);  
1013
1014   return S_OK;
1015 }
1016
1017 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
1018   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1019   
1020   DMUS_PRIVATE_CHUNK Chunk;
1021   LARGE_INTEGER liMove; /* used when skipping chunks */
1022   HRESULT hr;
1023
1024   FIXME("(%p, %p): Loading\n", This, pStm);
1025
1026   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1027   TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1028   switch (Chunk.fccID) {
1029   case FOURCC_RIFF: {
1030     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
1031     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1032     switch (Chunk.fccID) {
1033     case DMUS_FOURCC_STYLE_FORM: {
1034       TRACE_(dmfile)(": Style form\n");
1035       hr = IDirectMusicStyle8Impl_IPersistStream_ParseStyleForm (iface, &Chunk, pStm);
1036       if (FAILED(hr)) return hr;
1037       break;
1038     }
1039     default: {
1040       TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
1041       liMove.QuadPart = Chunk.dwSize;
1042       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1043       return E_FAIL;
1044     }
1045     }
1046     TRACE_(dmfile)(": reading finished\n");
1047     break;
1048   }
1049   default: {
1050     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
1051     liMove.QuadPart = Chunk.dwSize;
1052     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1053     return E_FAIL;
1054   }
1055   }
1056   
1057   return S_OK;
1058 }
1059
1060 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
1061   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1062   FIXME("(%p): Saving not implemented yet\n", This);
1063   return E_NOTIMPL;
1064 }
1065
1066 static HRESULT WINAPI IDirectMusicStyle8Impl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
1067   ICOM_THIS_MULTI(IDirectMusicStyle8Impl, PersistStreamVtbl, iface);
1068   FIXME("(%p, %p): stub\n", This, pcbSize);
1069   return E_NOTIMPL;
1070
1071 }
1072
1073 static const IPersistStreamVtbl DirectMusicStyle8_PersistStream_Vtbl = {
1074   IDirectMusicStyle8Impl_IPersistStream_QueryInterface,
1075   IDirectMusicStyle8Impl_IPersistStream_AddRef,
1076   IDirectMusicStyle8Impl_IPersistStream_Release,
1077   IDirectMusicStyle8Impl_IPersistStream_GetClassID,
1078   IDirectMusicStyle8Impl_IPersistStream_IsDirty,
1079   IDirectMusicStyle8Impl_IPersistStream_Load,
1080   IDirectMusicStyle8Impl_IPersistStream_Save,
1081   IDirectMusicStyle8Impl_IPersistStream_GetSizeMax
1082 };
1083
1084 /* for ClassFactory */
1085 HRESULT WINAPI DMUSIC_CreateDirectMusicStyleImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
1086   IDirectMusicStyle8Impl* obj;
1087         
1088   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicStyle8Impl));
1089   if (NULL == obj) {
1090     *ppobj = NULL;
1091     return E_OUTOFMEMORY;
1092   }
1093   obj->UnknownVtbl = &DirectMusicStyle8_Unknown_Vtbl;
1094   obj->StyleVtbl = &DirectMusicStyle8_Style_Vtbl;
1095   obj->ObjectVtbl = &DirectMusicStyle8_Object_Vtbl;
1096   obj->PersistStreamVtbl = &DirectMusicStyle8_PersistStream_Vtbl;
1097   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
1098   DM_STRUCT_INIT(obj->pDesc);
1099   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
1100   memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicStyle, sizeof (CLSID));
1101   obj->ref = 0; /* will be inited by QueryInterface */
1102   list_init (&obj->Bands);
1103   list_init (&obj->Motifs);
1104
1105   return IDirectMusicStyle8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
1106 }