advapi32: Test for OpenProcessToken with MAXIMUM_ALLOWED access.
[wine] / dlls / dmband / bandtrack.c
1 /* IDirectMusicBandTrack Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include "dmband_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmband);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25 /*****************************************************************************
26  * IDirectMusicBandTrack implementation
27  */
28 /* IDirectMusicBandTrack IUnknown part: */
29 static HRESULT WINAPI IDirectMusicBandTrack_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
30         ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
31         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
32
33         if (IsEqualIID (riid, &IID_IUnknown)) {
34                 *ppobj = (LPUNKNOWN)&This->UnknownVtbl;
35                 IUnknown_AddRef (iface);
36                 return S_OK;
37         } else if (IsEqualIID (riid, &IID_IDirectMusicTrack)
38           || IsEqualIID (riid, &IID_IDirectMusicTrack8)) {
39                 *ppobj = (LPDIRECTMUSICTRACK8)&This->TrackVtbl;
40                 IUnknown_AddRef (iface);
41                 return S_OK;
42         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
43                 *ppobj = (LPPERSISTSTREAM)&This->PersistStreamVtbl;
44                 IUnknown_AddRef (iface);
45                 return S_OK;
46         }
47         
48         WARN("(%p, %s,%p): not found\n", This, debugstr_dmguid(riid), ppobj);
49         return E_NOINTERFACE;
50 }
51
52 static ULONG WINAPI IDirectMusicBandTrack_IUnknown_AddRef (LPUNKNOWN iface) {
53   ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
54   ULONG ref = InterlockedIncrement(&This->ref);
55
56   TRACE("(%p) : AddRef from %d\n", This, ref - 1);
57
58   DMBAND_LockModule();
59
60   return ref;
61 }
62
63 static ULONG WINAPI IDirectMusicBandTrack_IUnknown_Release (LPUNKNOWN iface) {
64   ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
65   ULONG ref = InterlockedDecrement(&This->ref);
66
67   TRACE("(%p) : ReleaseRef to %d\n", This, ref);
68
69   if (ref == 0) {
70     HeapFree(GetProcessHeap(), 0, This);
71   }
72
73   DMBAND_UnlockModule();
74   
75   return ref;
76 }
77
78 static const IUnknownVtbl DirectMusicBandTrack_Unknown_Vtbl = {
79   IDirectMusicBandTrack_IUnknown_QueryInterface,
80   IDirectMusicBandTrack_IUnknown_AddRef,
81   IDirectMusicBandTrack_IUnknown_Release
82 };
83
84 /* IDirectMusicBandTrack IDirectMusicTrack8 part: */
85 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_QueryInterface (LPDIRECTMUSICTRACK8 iface, REFIID riid, LPVOID *ppobj) {
86   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
87   return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
88 }
89
90 static ULONG WINAPI IDirectMusicBandTrack_IDirectMusicTrack_AddRef (LPDIRECTMUSICTRACK8 iface) {
91   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
92   return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
93 }
94
95 static ULONG WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Release (LPDIRECTMUSICTRACK8 iface) {
96   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
97   return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
98 }
99
100 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Init (LPDIRECTMUSICTRACK8 iface, IDirectMusicSegment* pSegment) {
101   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
102   FIXME("(%p, %p): stub\n", This, pSegment);
103   return S_OK;
104 }
105
106 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_InitPlay (LPDIRECTMUSICTRACK8 iface, IDirectMusicSegmentState* pSegmentState, IDirectMusicPerformance* pPerformance, void** ppStateData, DWORD dwVirtualTrack8ID, DWORD dwFlags) {
107   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
108   FIXME("(%p, %p, %p, %p, %d, %d): stub\n", This, pSegmentState, pPerformance, ppStateData, dwVirtualTrack8ID, dwFlags);
109   return S_OK;
110 }
111
112 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_EndPlay (LPDIRECTMUSICTRACK8 iface, void* pStateData) {
113   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
114   FIXME("(%p, %p): stub\n", This, pStateData);
115   return S_OK;
116 }
117
118 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Play (LPDIRECTMUSICTRACK8 iface, void* pStateData, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, MUSIC_TIME mtOffset, DWORD dwFlags, IDirectMusicPerformance* pPerf, IDirectMusicSegmentState* pSegSt, DWORD dwVirtualID) {
119   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
120   
121   FIXME("(%p, %p, %ld, %ld, %ld, %d, %p, %p, %d): semi-stub\n", This, pStateData, mtStart, mtEnd, mtOffset, dwFlags, pPerf, pSegSt, dwVirtualID);
122   /* Sends following pMSG:
123      - DMUS_PATCH_PMSG
124      - DMUS_TRANSPOSE_PMSG
125      - DMUS_CHANNEL_PRIORITY_PMSG
126      - DMUS_MIDI_PMSG
127   */
128
129   return S_OK;
130 }
131
132 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_GetParam (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
133   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
134   FIXME("(%p, %s, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), mtTime, pmtNext, pParam);
135   return S_OK;
136 }
137
138 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_SetParam (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, MUSIC_TIME mtTime, void* pParam) {
139   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
140   FIXME("(%p, %s, %ld, %p): stub\n", This, debugstr_dmguid(rguidType), mtTime, pParam);
141   return S_OK;
142 }
143
144 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_IsParamSupported (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType) {
145   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
146   
147   TRACE("(%p, %s)\n", This, debugstr_dmguid(rguidType));
148   if (IsEqualGUID (rguidType, &GUID_BandParam)
149       || IsEqualGUID (rguidType, &GUID_Clear_All_Bands)
150       || IsEqualGUID (rguidType, &GUID_ConnectToDLSCollection)
151       || IsEqualGUID (rguidType, &GUID_Disable_Auto_Download)
152       || IsEqualGUID (rguidType, &GUID_Download)
153       || IsEqualGUID (rguidType, &GUID_DownloadToAudioPath)
154       || IsEqualGUID (rguidType, &GUID_Enable_Auto_Download)
155       || IsEqualGUID (rguidType, &GUID_IDirectMusicBand)
156       || IsEqualGUID (rguidType, &GUID_StandardMIDIFile)
157       || IsEqualGUID (rguidType, &GUID_Unload)
158       || IsEqualGUID (rguidType, &GUID_UnloadFromAudioPath)) {
159     TRACE("param supported\n");
160     return S_OK;
161   }
162   
163   TRACE("param unsupported\n");
164   return DMUS_E_TYPE_UNSUPPORTED;
165 }
166
167 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_AddNotificationType (LPDIRECTMUSICTRACK8 iface, REFGUID rguidNotificationType) {
168   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
169   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
170   return S_OK;
171 }
172
173 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_RemoveNotificationType (LPDIRECTMUSICTRACK8 iface, REFGUID rguidNotificationType) {
174   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
175   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
176   return S_OK;
177 }
178
179 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Clone (LPDIRECTMUSICTRACK8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, IDirectMusicTrack** ppTrack) {
180   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
181   FIXME("(%p, %ld, %ld, %p): stub\n", This, mtStart, mtEnd, ppTrack);
182   return S_OK;
183 }
184
185 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_PlayEx (LPDIRECTMUSICTRACK8 iface, void* pStateData, REFERENCE_TIME rtStart, REFERENCE_TIME rtEnd, REFERENCE_TIME rtOffset, DWORD dwFlags, IDirectMusicPerformance* pPerf, IDirectMusicSegmentState* pSegSt, DWORD dwVirtualID) {
186   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
187   FIXME("(%p, %p, 0x%s, 0x%s, 0x%s, %d, %p, %p, %d): stub\n", This, pStateData, wine_dbgstr_longlong(rtStart),
188       wine_dbgstr_longlong(rtEnd), wine_dbgstr_longlong(rtOffset), dwFlags, pPerf, pSegSt, dwVirtualID);
189   return S_OK;
190 }
191
192 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_GetParamEx (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, REFERENCE_TIME rtTime, REFERENCE_TIME* prtNext, void* pParam, void* pStateData, DWORD dwFlags) {
193   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
194   FIXME("(%p, %s, 0x%s, %p, %p, %p, %d): stub\n", This, debugstr_dmguid(rguidType),
195       wine_dbgstr_longlong(rtTime), prtNext, pParam, pStateData, dwFlags);
196   return S_OK;
197 }
198
199 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_SetParamEx (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, REFERENCE_TIME rtTime, void* pParam, void* pStateData, DWORD dwFlags) {
200   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
201   FIXME("(%p, %s, 0x%s, %p, %p, %d): stub\n", This, debugstr_dmguid(rguidType),
202       wine_dbgstr_longlong(rtTime), pParam, pStateData, dwFlags);
203   return S_OK;
204 }
205
206 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Compose (LPDIRECTMUSICTRACK8 iface, IUnknown* pContext, DWORD dwTrackGroup, IDirectMusicTrack** ppResultTrack) {
207   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
208   FIXME("(%p, %p, %d, %p): stub\n", This, pContext, dwTrackGroup, ppResultTrack);
209   return S_OK;
210 }
211
212 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Join (LPDIRECTMUSICTRACK8 iface, IDirectMusicTrack* pNewTrack, MUSIC_TIME mtJoin, IUnknown* pContext, DWORD dwTrackGroup, IDirectMusicTrack** ppResultTrack) {
213   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
214   FIXME("(%p, %p, %ld, %p, %d, %p): stub\n", This, pNewTrack, mtJoin, pContext, dwTrackGroup, ppResultTrack);
215   return S_OK;
216 }
217
218 static const IDirectMusicTrack8Vtbl DirectMusicBandTrack_DirectMusicTrack_Vtbl = {
219   IDirectMusicBandTrack_IDirectMusicTrack_QueryInterface,
220   IDirectMusicBandTrack_IDirectMusicTrack_AddRef,
221   IDirectMusicBandTrack_IDirectMusicTrack_Release,
222   IDirectMusicBandTrack_IDirectMusicTrack_Init,
223   IDirectMusicBandTrack_IDirectMusicTrack_InitPlay,
224   IDirectMusicBandTrack_IDirectMusicTrack_EndPlay,
225   IDirectMusicBandTrack_IDirectMusicTrack_Play,
226   IDirectMusicBandTrack_IDirectMusicTrack_GetParam,
227   IDirectMusicBandTrack_IDirectMusicTrack_SetParam,
228   IDirectMusicBandTrack_IDirectMusicTrack_IsParamSupported,
229   IDirectMusicBandTrack_IDirectMusicTrack_AddNotificationType,
230   IDirectMusicBandTrack_IDirectMusicTrack_RemoveNotificationType,
231   IDirectMusicBandTrack_IDirectMusicTrack_Clone,
232   IDirectMusicBandTrack_IDirectMusicTrack_PlayEx,
233   IDirectMusicBandTrack_IDirectMusicTrack_GetParamEx,
234   IDirectMusicBandTrack_IDirectMusicTrack_SetParamEx,
235   IDirectMusicBandTrack_IDirectMusicTrack_Compose,
236   IDirectMusicBandTrack_IDirectMusicTrack_Join
237 };
238
239 /* IDirectMusicBandTrack IPersistStream part: */
240 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
241   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
242   return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
243 }
244
245 static ULONG WINAPI IDirectMusicBandTrack_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
246   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
247   return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
248 }
249
250 static ULONG WINAPI IDirectMusicBandTrack_IPersistStream_Release (LPPERSISTSTREAM iface) {
251   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
252   return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
253 }
254
255 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
256   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
257   TRACE("(%p, %p)\n", This, pClassID);
258   memcpy(pClassID, &CLSID_DirectMusicBandTrack, sizeof(CLSID));
259   return S_OK;
260 }
261
262 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
263   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
264   FIXME("(%p): stub, always S_FALSE\n", This);
265   return S_FALSE;
266 }
267
268 static HRESULT IDirectMusicBandTrack_IPersistStream_LoadBand (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicBand** ppBand,
269                                                               DMUS_PRIVATE_BAND_ITEM_HEADER* pHeader) {
270
271   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
272   HRESULT hr = E_FAIL;
273   IPersistStream* pPersistStream = NULL;
274   
275   hr = CoCreateInstance (&CLSID_DirectMusicBand, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicBand, (LPVOID*) ppBand);
276   if (FAILED(hr)) {
277     ERR(": could not create object\n");
278     return hr;
279   }
280   /* acquire PersistStream interface */
281   hr = IDirectMusicBand_QueryInterface (*ppBand, &IID_IPersistStream, (LPVOID*) &pPersistStream);
282   if (FAILED(hr)) {
283     ERR(": could not acquire IPersistStream\n");
284     return hr;
285   }
286   /* load */
287   hr = IPersistStream_Load (pPersistStream, pClonedStream);
288   if (FAILED(hr)) {
289     ERR(": failed to load object\n");
290     return hr;
291   }
292   
293   /* release all loading-related stuff */
294   IPersistStream_Release (pPersistStream);
295
296   /*
297    * @TODO insert pBand into This
298    */
299   if (SUCCEEDED(hr)) {
300     LPDMUS_PRIVATE_BAND pNewBand = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_BAND));
301     if (NULL == pNewBand) {
302       ERR(": no more memory\n");
303       return  E_OUTOFMEMORY;
304     }
305     memcpy(&pNewBand->pBandHeader, pHeader, sizeof(DMUS_PRIVATE_BAND_ITEM_HEADER));
306     pNewBand->ppBand = (IDirectMusicBandImpl*)((char*)(*ppBand) - offsetof(IDirectMusicBandImpl,BandVtbl));
307     IDirectMusicBand_AddRef(*ppBand);
308     list_add_tail (&This->Bands, &pNewBand->entry);
309   }
310
311   return S_OK;
312 }
313
314 static HRESULT IDirectMusicBandTrack_IPersistStream_ParseBandsList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
315
316   /*ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);*/
317   HRESULT hr = E_FAIL;
318   DMUS_PRIVATE_CHUNK Chunk;
319   DWORD StreamSize, ListSize[3], ListCount[3];
320   LARGE_INTEGER liMove; /* used when skipping chunks */
321
322   IDirectMusicBand* pBand = NULL;
323   DMUS_PRIVATE_BAND_ITEM_HEADER header;
324
325   if (pChunk->fccID != DMUS_FOURCC_BANDS_LIST) {
326     ERR_(dmfile)(": %s chunk should be a BANDS list\n", debugstr_fourcc (pChunk->fccID));
327     return E_FAIL;
328   }  
329
330   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
331   ListCount[0] = 0;
332
333   do {
334     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
335     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
336     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
337     switch (Chunk.fccID) {
338     case FOURCC_LIST: {
339       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
340       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
341       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
342       ListCount[1] = 0;
343       do {
344         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
345         ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
346         TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
347         switch (Chunk.fccID) { 
348         case DMUS_FOURCC_BANDITEM_CHUNK: {
349           DMUS_IO_BAND_ITEM_HEADER tmp_header;
350           TRACE_(dmfile)(": Band Item chunk v1\n");
351           
352           IStream_Read (pStm, &tmp_header, sizeof(DMUS_IO_BAND_ITEM_HEADER), NULL);
353           TRACE_(dmfile)(" - lBandTime: %lu\n", tmp_header.lBandTime);
354           
355           header.dwVersion = 1;
356           header.lBandTime = tmp_header.lBandTime;
357           break;
358         }
359         case DMUS_FOURCC_BANDITEM_CHUNK2: { 
360           DMUS_IO_BAND_ITEM_HEADER2 tmp_header2;
361           TRACE_(dmfile)(": Band Item chunk v2\n");
362           
363           IStream_Read (pStm, &tmp_header2, sizeof(DMUS_IO_BAND_ITEM_HEADER2), NULL);
364           TRACE_(dmfile)(" - lBandTimeLogical: %lu\n", tmp_header2.lBandTimeLogical);
365           TRACE_(dmfile)(" - lBandTimePhysical: %lu\n", tmp_header2.lBandTimePhysical);
366           
367           header.dwVersion = 2;
368           header.lBandTimeLogical = tmp_header2.lBandTimeLogical;
369           header.lBandTimePhysical = tmp_header2.lBandTimePhysical;
370           break;
371         }
372         case FOURCC_RIFF: { 
373           IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
374           TRACE_(dmfile)(": RIFF chunk of type %s\n", debugstr_fourcc(Chunk.fccID));
375           StreamSize = Chunk.dwSize - sizeof(FOURCC);
376           switch (Chunk.fccID) {
377           case DMUS_FOURCC_BAND_FORM: {
378             LPSTREAM pClonedStream = NULL;
379             TRACE_(dmfile)(": BAND RIFF\n");
380             
381             IStream_Clone (pStm, &pClonedStream);
382             
383             liMove.QuadPart = 0;
384             liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
385             IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
386             
387             hr = IDirectMusicBandTrack_IPersistStream_LoadBand (iface, pClonedStream, &pBand, &header);
388             if (FAILED(hr)) {
389               ERR(": could not load track\n");
390               return hr;
391             }
392             IStream_Release (pClonedStream);
393             
394             IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release at as it inserted */
395             
396             /** now safe move the cursor */
397             liMove.QuadPart = StreamSize;
398             IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
399             break;
400           }
401           default: {
402             TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
403             liMove.QuadPart = StreamSize;
404             IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
405             break;                                              
406           }
407           }
408           break;
409         }
410         default: {
411           TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
412           liMove.QuadPart = Chunk.dwSize;
413           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
414           break;                                                
415          }
416         }
417         TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
418       } while (ListCount[1] < ListSize[1]);
419       break;
420     }
421     default: {
422       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
423       liMove.QuadPart = Chunk.dwSize;
424       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
425       break;                                            
426     }
427     }
428     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
429   } while (ListCount[0] < ListSize[0]);
430
431   return S_OK;
432 }
433
434 static HRESULT IDirectMusicBandTrack_IPersistStream_ParseBandTrackForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
435
436   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
437   HRESULT hr = E_FAIL;
438   DMUS_PRIVATE_CHUNK Chunk;
439   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
440   LARGE_INTEGER liMove; /* used when skipping chunks */
441
442   if (pChunk->fccID != DMUS_FOURCC_BANDTRACK_FORM) {
443     ERR_(dmfile)(": %s chunk should be a BANDTRACK form\n", debugstr_fourcc (pChunk->fccID));
444     return E_FAIL;
445   }  
446
447   StreamSize = pChunk->dwSize - sizeof(FOURCC);
448   StreamCount = 0;
449
450   do {
451     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
452     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
453     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
454     
455     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
456     if (FAILED(hr)) return hr;
457
458     if (hr == S_FALSE) {
459       switch (Chunk.fccID) {
460       case DMUS_FOURCC_BANDTRACK_CHUNK: {
461         TRACE_(dmfile)(": BandTrack chunk\n");
462         IStream_Read (pStm, &This->header, sizeof(DMUS_IO_BAND_TRACK_HEADER), NULL);
463         TRACE_(dmfile)(" - bAutoDownload: %u\n", This->header.bAutoDownload);
464         break;
465       }   
466       case FOURCC_LIST: {
467         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
468         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
469         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
470         ListCount[0] = 0;
471         switch (Chunk.fccID) {
472         case DMUS_FOURCC_UNFO_LIST: { 
473           TRACE_(dmfile)(": UNFO list\n");
474           do {
475             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
476             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
477             TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
478             
479             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
480             if (FAILED(hr)) return hr;
481             
482             if (hr == S_FALSE) {
483               switch (Chunk.fccID) {
484               default: {
485                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
486                 liMove.QuadPart = Chunk.dwSize;
487                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
488                 break;                          
489               }
490               }
491             }  
492             TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
493           } while (ListCount[0] < ListSize[0]);
494           break;
495         }
496         case DMUS_FOURCC_BANDS_LIST: {
497           TRACE_(dmfile)(": TRACK list\n");
498           hr = IDirectMusicBandTrack_IPersistStream_ParseBandsList (iface, &Chunk, pStm);
499           if (FAILED(hr)) return hr;
500           break;
501         }
502         default: {
503           TRACE_(dmfile)(": unknown (skipping)\n");
504           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
505           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
506           break;                                                
507         }
508         }
509         break;
510       }
511       default: {
512         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
513         liMove.QuadPart = Chunk.dwSize;
514         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
515         break;                                          
516       }
517       }
518     }
519     TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
520   } while (StreamCount < StreamSize);  
521
522   return S_OK;
523 }
524
525
526 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
527   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
528
529   DMUS_PRIVATE_CHUNK Chunk;
530   LARGE_INTEGER liMove;
531   HRESULT hr;
532
533   TRACE("(%p, %p): Loading\n", This, pStm);
534
535   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
536   TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
537   switch (Chunk.fccID) {
538   case FOURCC_RIFF: {
539     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
540     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
541     switch (Chunk.fccID) {
542     case DMUS_FOURCC_BANDTRACK_FORM: {
543       TRACE_(dmfile)(": Band track form\n");
544       hr = IDirectMusicBandTrack_IPersistStream_ParseBandTrackForm (iface, &Chunk, pStm);
545       if (FAILED(hr)) return hr;
546       break;    
547     }
548     default: {
549       TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
550       liMove.QuadPart = Chunk.dwSize;
551       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
552       return E_FAIL;
553     }
554     }
555     TRACE_(dmfile)(": reading finished\n");
556     break;
557   }
558   default: {
559     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
560     liMove.QuadPart = Chunk.dwSize;
561     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
562     return E_FAIL;
563   }
564   }
565
566   return S_OK;
567 }
568
569 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
570   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
571   FIXME("(%p): Saving not implemented yet\n", This);
572   return E_NOTIMPL;
573 }
574
575 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
576   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
577   FIXME("(%p, %p): stub\n", This, pcbSize);
578   return E_NOTIMPL;
579 }
580
581 static const IPersistStreamVtbl DirectMusicBandTrack_PerststStream_Vtbl = {
582   IDirectMusicBandTrack_IPersistStream_QueryInterface,
583   IDirectMusicBandTrack_IPersistStream_AddRef,
584   IDirectMusicBandTrack_IPersistStream_Release,
585   IDirectMusicBandTrack_IPersistStream_GetClassID,
586   IDirectMusicBandTrack_IPersistStream_IsDirty,
587   IDirectMusicBandTrack_IPersistStream_Load,
588   IDirectMusicBandTrack_IPersistStream_Save,
589   IDirectMusicBandTrack_IPersistStream_GetSizeMax
590 };
591
592 /* for ClassFactory */
593 HRESULT WINAPI DMUSIC_CreateDirectMusicBandTrack (LPCGUID lpcGUID, LPVOID *ppobj, LPUNKNOWN pUnkOuter) {
594   IDirectMusicBandTrack* track;
595         
596   track = HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandTrack));
597   if (NULL == track) {
598     *ppobj = NULL;
599     return E_OUTOFMEMORY;
600   }
601   track->UnknownVtbl = &DirectMusicBandTrack_Unknown_Vtbl;
602   track->TrackVtbl = &DirectMusicBandTrack_DirectMusicTrack_Vtbl;
603   track->PersistStreamVtbl = &DirectMusicBandTrack_PerststStream_Vtbl;
604   track->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
605   DM_STRUCT_INIT(track->pDesc);
606   track->pDesc->dwValidData |= DMUS_OBJ_CLASS;
607   memcpy (&track->pDesc->guidClass, &CLSID_DirectMusicBandTrack, sizeof (CLSID));
608   track->ref = 0; /* will be inited by QueryInterface */
609   list_init (&track->Bands);
610   
611   return IDirectMusicBandTrack_IUnknown_QueryInterface ((LPUNKNOWN)&track->UnknownVtbl, lpcGUID, ppobj);
612 }