msvcrt: Remove bogus code.
[wine] / dlls / dmscript / script.c
1 /* IDirectMusicScript
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 modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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
14  * GNU Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #include "dmscript_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(dmscript);
31 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
32
33
34 /*****************************************************************************
35  * IDirectMusicScriptImpl implementation
36  */
37 /* IDirectMusicScriptImpl IUnknown part: */
38 HRESULT WINAPI IDirectMusicScriptImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
39   ICOM_THIS_MULTI(IDirectMusicScriptImpl, UnknownVtbl, iface);
40   TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
41   
42   if (IsEqualIID (riid, &IID_IUnknown)) {
43     *ppobj = (LPVOID)&This->UnknownVtbl;
44     IDirectMusicScriptImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
45     return S_OK;        
46   } else if (IsEqualIID (riid, &IID_IDirectMusicScript)) {
47     *ppobj = (LPVOID)&This->ScriptVtbl;
48     IDirectMusicScriptImpl_IDirectMusicScript_AddRef ((LPDIRECTMUSICSCRIPT)&This->ScriptVtbl);
49     return S_OK;
50   } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
51     *ppobj = (LPVOID)&This->ObjectVtbl;
52     IDirectMusicScriptImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);          
53     return S_OK;
54   } else if (IsEqualIID (riid, &IID_IPersistStream)) {
55     *ppobj = (LPVOID)&This->PersistStreamVtbl;
56     IDirectMusicScriptImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);           
57     return S_OK;
58   }
59   
60   WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
61   return E_NOINTERFACE;
62 }
63
64 ULONG WINAPI IDirectMusicScriptImpl_IUnknown_AddRef (LPUNKNOWN iface) {
65   ICOM_THIS_MULTI(IDirectMusicScriptImpl, UnknownVtbl, iface);
66   ULONG ref = InterlockedIncrement(&This->ref);
67
68   TRACE("(%p): AddRef from %ld\n", This, ref - 1);
69
70   DMSCRIPT_LockModule();
71
72   return ref;
73 }
74
75 ULONG WINAPI IDirectMusicScriptImpl_IUnknown_Release (LPUNKNOWN iface) {
76   ICOM_THIS_MULTI(IDirectMusicScriptImpl, UnknownVtbl, iface);
77   ULONG ref = InterlockedDecrement(&This->ref);
78
79   TRACE("(%p): ReleaseRef to %ld\n", This, ref);
80
81   if (ref == 0) {
82     HeapFree(GetProcessHeap(), 0, This->pHeader);
83     HeapFree(GetProcessHeap(), 0, This->pVersion);
84     HeapFree(GetProcessHeap(), 0, This->pwzLanguage);
85     HeapFree(GetProcessHeap(), 0, This->pwzSource);
86     HeapFree(GetProcessHeap(), 0, This);
87   }
88
89   DMSCRIPT_UnlockModule();
90   
91   return ref;
92 }
93
94 static const IUnknownVtbl DirectMusicScript_Unknown_Vtbl = {
95   IDirectMusicScriptImpl_IUnknown_QueryInterface,
96   IDirectMusicScriptImpl_IUnknown_AddRef,
97   IDirectMusicScriptImpl_IUnknown_Release
98 };
99
100 /* IDirectMusicScriptImpl IDirectMusicScript part: */
101 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_QueryInterface (LPDIRECTMUSICSCRIPT iface, REFIID riid, LPVOID *ppobj) {
102   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
103   return IDirectMusicScriptImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
104 }
105
106 ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicScript_AddRef (LPDIRECTMUSICSCRIPT iface) {
107   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
108   return IDirectMusicScriptImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
109 }
110
111 ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicScript_Release (LPDIRECTMUSICSCRIPT iface) {
112   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
113   return IDirectMusicScriptImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
114 }
115
116 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_Init (LPDIRECTMUSICSCRIPT iface, IDirectMusicPerformance* pPerformance, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
117   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
118   FIXME("(%p, %p, %p): stub\n", This, pPerformance, pErrorInfo);
119   This->pPerformance = pPerformance;
120   return S_OK;
121 }
122
123 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_CallRoutine (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszRoutineName, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
124   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
125   FIXME("(%p, %s, %p): stub\n", This, debugstr_w(pwszRoutineName), pErrorInfo);
126   /*return E_NOTIMPL;*/
127   return S_OK;
128   /*return E_FAIL;*/
129 }
130
131 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_SetVariableVariant (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, VARIANT varValue, BOOL fSetRef, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
132   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
133   FIXME("(%p, %s, FIXME, %d, %p): stub\n", This, debugstr_w(pwszVariableName),/* varValue,*/ fSetRef, pErrorInfo);
134   return S_OK;
135 }
136
137 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_GetVariableVariant (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, VARIANT* pvarValue, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
138   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
139   FIXME("(%p, %s, %p, %p): stub\n", This, debugstr_w(pwszVariableName), pvarValue, pErrorInfo);
140   return S_OK;
141 }
142
143 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_SetVariableNumber (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, LONG lValue, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
144   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
145   FIXME("(%p, %s, %li, %p): stub\n", This, debugstr_w(pwszVariableName), lValue, pErrorInfo);
146   return S_OK;
147 }
148
149 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_GetVariableNumber (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, LONG* plValue, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
150   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
151   FIXME("(%p, %s, %p, %p): stub\n", This, debugstr_w(pwszVariableName), plValue, pErrorInfo);
152   return S_OK;
153 }
154
155 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_SetVariableObject (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, IUnknown* punkValue, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
156   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
157   FIXME("(%p, %s, %p, %p): stub\n", This, debugstr_w(pwszVariableName), punkValue, pErrorInfo);
158   return S_OK;
159 }
160
161 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_GetVariableObject (LPDIRECTMUSICSCRIPT iface, WCHAR* pwszVariableName, REFIID riid, LPVOID* ppv, DMUS_SCRIPT_ERRORINFO* pErrorInfo) {
162   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
163   FIXME("(%p, %s, %s, %p, %p): stub\n", This, debugstr_w(pwszVariableName), debugstr_dmguid(riid), ppv, pErrorInfo);
164   return S_OK;
165 }
166
167 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_EnumRoutine (LPDIRECTMUSICSCRIPT iface, DWORD dwIndex, WCHAR* pwszName) {
168   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
169   FIXME("(%p, %ld, %p): stub\n", This, dwIndex, pwszName);
170   return S_OK;
171 }
172
173 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicScript_EnumVariable (LPDIRECTMUSICSCRIPT iface, DWORD dwIndex, WCHAR* pwszName) {
174   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ScriptVtbl, iface);
175   FIXME("(%p, %ld, %p): stub\n", This, dwIndex, pwszName);
176   return S_OK;
177 }
178
179 static const IDirectMusicScriptVtbl DirectMusicScript_Script_Vtbl = {
180   IDirectMusicScriptImpl_IDirectMusicScript_QueryInterface,
181   IDirectMusicScriptImpl_IDirectMusicScript_AddRef,
182   IDirectMusicScriptImpl_IDirectMusicScript_Release,
183   IDirectMusicScriptImpl_IDirectMusicScript_Init,
184   IDirectMusicScriptImpl_IDirectMusicScript_CallRoutine,
185   IDirectMusicScriptImpl_IDirectMusicScript_SetVariableVariant,
186   IDirectMusicScriptImpl_IDirectMusicScript_GetVariableVariant,
187   IDirectMusicScriptImpl_IDirectMusicScript_SetVariableNumber,
188   IDirectMusicScriptImpl_IDirectMusicScript_GetVariableNumber,
189   IDirectMusicScriptImpl_IDirectMusicScript_SetVariableObject,
190   IDirectMusicScriptImpl_IDirectMusicScript_GetVariableObject,
191   IDirectMusicScriptImpl_IDirectMusicScript_EnumRoutine,
192   IDirectMusicScriptImpl_IDirectMusicScript_EnumVariable
193 };
194
195 /* IDirectMusicScriptImpl IDirectMusicObject part: */
196 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
197   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
198   return IDirectMusicScriptImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
199 }
200
201 ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
202   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
203   return IDirectMusicScriptImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
204 }
205
206 ULONG WINAPI IDirectMusicScriptImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
207   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
208   return IDirectMusicScriptImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
209 }
210
211 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
212   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
213   TRACE("(%p, %p)\n", This, pDesc);
214   /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
215   memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
216   return S_OK;
217 }
218
219 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
220   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
221   TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
222         
223   /* According to MSDN, we should copy only given values, not whole struct */   
224   if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
225     memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
226   if (pDesc->dwValidData & DMUS_OBJ_CLASS)
227     memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));             
228   if (pDesc->dwValidData & DMUS_OBJ_NAME)
229     lstrcpynW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
230   if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
231     lstrcpynW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
232   if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
233     lstrcpynW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
234   if (pDesc->dwValidData & DMUS_OBJ_VERSION)
235     memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                                
236   if (pDesc->dwValidData & DMUS_OBJ_DATE)
237     memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                              
238   if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
239     memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                               
240     memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
241   }
242   if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
243     /* according to MSDN, we copy the stream */
244     IStream_Clone (pDesc->pStream, &This->pDesc->pStream);      
245   }
246   
247   /* add new flags */
248   This->pDesc->dwValidData |= pDesc->dwValidData;
249   
250   return S_OK;
251 }
252
253 HRESULT WINAPI IDirectMusicScriptImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
254   ICOM_THIS_MULTI(IDirectMusicScriptImpl, ObjectVtbl, iface);
255   DMUS_PRIVATE_CHUNK Chunk;
256   DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
257   LARGE_INTEGER liMove; /* used when skipping chunks */
258   
259         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
260         
261         /* FIXME: should this be determined from stream? */
262         pDesc->dwValidData |= DMUS_OBJ_CLASS;
263         memcpy (&pDesc->guidClass, &CLSID_DirectMusicScript, sizeof(CLSID));
264         
265         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
266         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
267         switch (Chunk.fccID) {  
268                 case FOURCC_RIFF: {
269                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
270                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
271                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
272                         StreamCount = 0;
273                         if (Chunk.fccID == DMUS_FOURCC_SCRIPT_FORM) {
274                                 TRACE_(dmfile)(": script form\n");
275                                 do {
276                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
277                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
278                                         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
279                                         switch (Chunk.fccID) {
280                                                 case DMUS_FOURCC_GUID_CHUNK: {
281                                                         TRACE_(dmfile)(": GUID chunk\n");
282                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
283                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
284                                                         break;
285                                                 }
286                                                 case DMUS_FOURCC_VERSION_CHUNK: {
287                                                         TRACE_(dmfile)(": version chunk\n");
288                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
289                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
290                                                         break;
291                                                 }
292                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
293                                                         TRACE_(dmfile)(": category chunk\n");
294                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
295                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
296                                                         break;
297                                                 }
298                                                 case FOURCC_LIST: {
299                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
300                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
301                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
302                                                         ListCount[0] = 0;
303                                                         switch (Chunk.fccID) {
304                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
305                                                                 case DMUS_FOURCC_UNFO_LIST: {
306                                                                         TRACE_(dmfile)(": UNFO list\n");
307                                                                         do {
308                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
309                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
310                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
311                                                                                 switch (Chunk.fccID) {
312                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
313                                              (though strings seem to be valid unicode) */
314                                                                                         case mmioFOURCC('I','N','A','M'):
315                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
316                                                                                                 TRACE_(dmfile)(": name chunk\n");
317                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
318                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
319                                                                                                 break;
320                                                                                         }
321                                                                                         case mmioFOURCC('I','A','R','T'):
322                                                                                         case DMUS_FOURCC_UART_CHUNK: {
323                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
324                                                                                                 liMove.QuadPart = Chunk.dwSize;
325                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
326                                                                                                 break;
327                                                                                         }
328                                                                                         case mmioFOURCC('I','C','O','P'):
329                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
330                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
331                                                                                                 liMove.QuadPart = Chunk.dwSize;
332                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
333                                                                                                 break;
334                                                                                         }
335                                                                                         case mmioFOURCC('I','S','B','J'):
336                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
337                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
338                                                                                                 liMove.QuadPart = Chunk.dwSize;
339                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
340                                                                                                 break;
341                                                                                         }
342                                                                                         case mmioFOURCC('I','C','M','T'):
343                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
344                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
345                                                                                                 liMove.QuadPart = Chunk.dwSize;
346                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
347                                                                                                 break;
348                                                                                         }
349                                                                                         default: {
350                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
351                                                                                                 liMove.QuadPart = Chunk.dwSize;
352                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
353                                                                                                 break;                                          
354                                                                                         }
355                                                                                 }
356                                                                                 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
357                                                                         } while (ListCount[0] < ListSize[0]);
358                                                                         break;
359                                                                 }
360                                                                 default: {
361                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
362                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
363                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
364                                                                         break;                                          
365                                                                 }
366                                                         }
367                                                         break;
368                                                 }       
369                                                 default: {
370                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
371                                                         liMove.QuadPart = Chunk.dwSize;
372                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
373                                                         break;                                          
374                                                 }
375                                         }
376                                         TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
377                                 } while (StreamCount < StreamSize);
378                         } else {
379                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
380                                 liMove.QuadPart = StreamSize;
381                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
382                                 return E_FAIL;
383                         }
384                 
385                         TRACE_(dmfile)(": reading finished\n");
386                         break;
387                 }
388                 default: {
389                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
390                         liMove.QuadPart = Chunk.dwSize;
391                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
392                         return DMUS_E_INVALIDFILE;
393                 }
394         }       
395         
396         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
397         
398         return S_OK;
399 }
400
401 static const IDirectMusicObjectVtbl DirectMusicScript_Object_Vtbl = {
402   IDirectMusicScriptImpl_IDirectMusicObject_QueryInterface,
403   IDirectMusicScriptImpl_IDirectMusicObject_AddRef,
404   IDirectMusicScriptImpl_IDirectMusicObject_Release,
405   IDirectMusicScriptImpl_IDirectMusicObject_GetDescriptor,
406   IDirectMusicScriptImpl_IDirectMusicObject_SetDescriptor,
407   IDirectMusicScriptImpl_IDirectMusicObject_ParseDescriptor
408 };
409
410 /* IDirectMusicScriptImpl IPersistStream part: */
411 HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
412   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
413   return IDirectMusicScriptImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
414 }
415
416 ULONG WINAPI IDirectMusicScriptImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
417   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
418   return IDirectMusicScriptImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
419 }
420
421 ULONG WINAPI IDirectMusicScriptImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
422   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
423   return IDirectMusicScriptImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
424 }
425
426 HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
427   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
428   TRACE("(%p, %p)\n", This, pClassID);
429   memcpy(pClassID, &CLSID_DirectMusicScript, sizeof(CLSID));
430   return S_OK;
431 }
432
433 HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
434   ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);  
435   FIXME("(%p): stub, always S_FALSE\n", This);
436   return S_FALSE;
437 }
438
439 HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
440         ICOM_THIS_MULTI(IDirectMusicScriptImpl, PersistStreamVtbl, iface);
441
442         DMUS_PRIVATE_CHUNK Chunk;
443         DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
444         LARGE_INTEGER liMove; /* used when skipping chunks */
445         LPDIRECTMUSICGETLOADER pGetLoader = NULL;
446         LPDIRECTMUSICLOADER pLoader = NULL;
447
448         FIXME("(%p, %p): Loading not implemented yet\n", This, pStm);
449         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
450         TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
451         switch (Chunk.fccID) {  
452                 case FOURCC_RIFF: {
453                         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
454                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
455                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
456                         StreamCount = 0;
457                         switch (Chunk.fccID) {
458                                 case DMUS_FOURCC_SCRIPT_FORM: {
459                                         TRACE_(dmfile)(": script form\n");
460                                         do {
461                                                 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
462                                                 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
463                                                 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
464                                                 switch (Chunk.fccID) { 
465                                                         case DMUS_FOURCC_SCRIPT_CHUNK: {
466                                                                 TRACE_(dmfile)(": script header chunk\n");
467                                                                 This->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
468                                                                 IStream_Read (pStm, This->pHeader, Chunk.dwSize, NULL);
469                                                                 break;
470                                                         }
471                                                         case DMUS_FOURCC_SCRIPTVERSION_CHUNK: {
472                                                                 TRACE_(dmfile)(": script version chunk\n");
473                                                                 This->pVersion = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
474                                                                 IStream_Read (pStm, This->pVersion, Chunk.dwSize, NULL); 
475                                                                 TRACE_(dmfile)("version: 0x%08lx.0x%08lx\n", This->pVersion->dwVersionMS, This->pVersion->dwVersionLS);
476                                                                 break;
477                                                         }
478                                                         case DMUS_FOURCC_SCRIPTLANGUAGE_CHUNK: {
479                                                                 TRACE_(dmfile)(": script language chunk\n");
480                                                                 This->pwzLanguage = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
481                                                                 IStream_Read (pStm, This->pwzLanguage, Chunk.dwSize, NULL); 
482                                                                 TRACE_(dmfile)("using language: %s\n", debugstr_w(This->pwzLanguage));
483                                                                 break;
484                                                         }
485                                                         case DMUS_FOURCC_SCRIPTSOURCE_CHUNK: {
486                                                                 TRACE_(dmfile)(": script source chunk\n");
487                                                                 This->pwzSource = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
488                                                                 IStream_Read (pStm, This->pwzSource, Chunk.dwSize, NULL); 
489                                                                 if (TRACE_ON(dmscript)) {
490                                                                     int count = WideCharToMultiByte(CP_ACP, 0, This->pwzSource, -1, NULL, 0, NULL, NULL);
491                                                                     LPSTR str = HeapAlloc(GetProcessHeap (), 0, count);
492                                                                     WideCharToMultiByte(CP_ACP, 0, This->pwzSource, -1, str, count, NULL, NULL);
493                                                                     str[count-1] = '\n';
494                                                                     TRACE("source:\n");
495                                                                     write( 2, str, count );
496                                                                     HeapFree(GetProcessHeap(), 0, str);
497                                                                 }
498                                                                 break;
499                                                         }
500                                                         case DMUS_FOURCC_GUID_CHUNK: {
501                                                                 TRACE_(dmfile)(": GUID chunk\n");
502                                                                 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
503                                                                 IStream_Read (pStm, &This->pDesc->guidObject, Chunk.dwSize, NULL);
504                                                                 break;
505                                                         }
506                                                         case DMUS_FOURCC_VERSION_CHUNK: {
507                                                                 TRACE_(dmfile)(": version chunk\n");
508                                                                 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
509                                                                 IStream_Read (pStm, &This->pDesc->vVersion, Chunk.dwSize, NULL);
510                                                                 break;
511                                                         }
512                                                         case DMUS_FOURCC_CATEGORY_CHUNK: {
513                                                                 TRACE_(dmfile)(": category chunk\n");
514                                                                 This->pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
515                                                                 IStream_Read (pStm, This->pDesc->wszCategory, Chunk.dwSize, NULL);
516                                                                 break;
517                                                         }
518                                                         case FOURCC_RIFF: {
519                                                                 IDirectMusicObject* pObject = NULL;
520                                                                 DMUS_OBJECTDESC desc;
521
522                                                                 ZeroMemory ((LPVOID)&desc, sizeof(DMUS_OBJECTDESC));
523                                                                 desc.dwSize = sizeof(DMUS_OBJECTDESC);
524                                                                 desc.dwValidData = DMUS_OBJ_STREAM | DMUS_OBJ_CLASS;
525                                                                 desc.guidClass = CLSID_DirectMusicContainer;
526                                                                 desc.pStream = NULL;
527                                                                 IStream_Clone (pStm, &desc.pStream);
528
529                                                                 liMove.QuadPart = 0;
530                                                                 liMove.QuadPart -= (sizeof(FOURCC) + sizeof(DWORD));
531                                                                 IStream_Seek (desc.pStream, liMove, STREAM_SEEK_CUR, NULL);
532
533                                                                 IStream_QueryInterface (pStm, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader);
534                                                                 IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
535                                                                 IDirectMusicGetLoader_Release (pGetLoader);
536
537                                                                 if (SUCCEEDED(IDirectMusicLoader_GetObject (pLoader, &desc, &IID_IDirectMusicObject, (LPVOID*) &pObject))) {
538                                                                   IDirectMusicObject_Release (pObject);
539                                                                 } else {
540                                                                   ERR_(dmfile)("Error on GetObject while trying to load Scrip SubContainer\n");
541                                                                 }
542
543                                                                 IDirectMusicLoader_Release (pLoader); pLoader = NULL; /* release loader */
544                                                                 IStream_Release(desc.pStream); desc.pStream = NULL; /* release cloned stream */
545
546                                                                 liMove.QuadPart = Chunk.dwSize;
547                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
548                                                                 /*
549                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
550                                                                 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
551                                                                 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
552                                                                 ListCount[0] = 0;
553
554                                                                 switch (Chunk.fccID) {
555                                                                         default: {
556                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
557                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
558                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
559                                                                                 break;                                          
560                                                                         }
561                                                                 }
562                                                                 */
563                                                                 break;
564                                                         }
565                                                         case FOURCC_LIST: {
566                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
567                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
568                                                                 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
569                                                                 ListCount[0] = 0;
570                                                                 switch (Chunk.fccID) {
571                                                                         case DMUS_FOURCC_UNFO_LIST: {
572                                                                                 TRACE_(dmfile)(": UNFO list\n");
573                                                                                 do {
574                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
575                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
576                                                                                         TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
577                                                                                         switch (Chunk.fccID) {
578                                                                                                 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
579                                               (though strings seem to be valid unicode) */
580                                                                                                 case mmioFOURCC('I','N','A','M'):
581                                                                                                 case DMUS_FOURCC_UNAM_CHUNK: {
582                                                                                                         TRACE_(dmfile)(": name chunk\n");
583                                                                                                         This->pDesc->dwValidData |= DMUS_OBJ_NAME;
584                                                                                                         IStream_Read (pStm, This->pDesc->wszName, Chunk.dwSize, NULL);
585                                                                                                         break;
586                                                                                                 }
587                                                                                                 case mmioFOURCC('I','A','R','T'):
588                                                                                                 case DMUS_FOURCC_UART_CHUNK: {
589                                                                                                         TRACE_(dmfile)(": artist chunk (ignored)\n");
590                                                                                                         liMove.QuadPart = Chunk.dwSize;
591                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
592                                                                                                         break;
593                                                                                                 }
594                                                                                                 case mmioFOURCC('I','C','O','P'):
595                                                                                                 case DMUS_FOURCC_UCOP_CHUNK: {
596                                                                                                         TRACE_(dmfile)(": copyright chunk (ignored)\n");
597                                                                                                         liMove.QuadPart = Chunk.dwSize;
598                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
599                                                                                                         break;
600                                                                                                 }
601                                                                                                 case mmioFOURCC('I','S','B','J'):
602                                                                                                 case DMUS_FOURCC_USBJ_CHUNK: {
603                                                                                                         TRACE_(dmfile)(": subject chunk (ignored)\n");
604                                                                                                         liMove.QuadPart = Chunk.dwSize;
605                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
606                                                                                                         break;
607                                                                                                 }
608                                                                                                 case mmioFOURCC('I','C','M','T'):
609                                                                                                 case DMUS_FOURCC_UCMT_CHUNK: {
610                                                                                                         TRACE_(dmfile)(": comment chunk (ignored)\n");
611                                                                                                         liMove.QuadPart = Chunk.dwSize;
612                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
613                                                                                                         break;
614                                                                                                 }
615                                                                                                 default: {
616                                                                                                         TRACE_(dmfile)(": unknown sub-chunk (irrevelant & skipping)\n");
617                                                                                                         liMove.QuadPart = Chunk.dwSize;
618                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
619                                                                                                         break;                                          
620                                                                                                 }
621                                                                                         }
622                                                                                         TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
623                                                                                 } while (ListCount[0] < ListSize[0]);
624                                                                                 break;
625                                                                         }
626                                                                         default: {
627                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
628                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
629                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
630                                                                                 break;                                          
631                                                                         }
632                                                                 }
633                                                                 break;
634                                                         }       
635                                                         default: {
636                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
637                                                                 liMove.QuadPart = Chunk.dwSize;
638                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
639                                                                 break;                                          
640                                                         }
641                                                 }
642                                                 TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
643                                         } while (StreamCount < StreamSize);
644                                         break;
645                                 }
646                                 default: {
647                                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
648                                         liMove.QuadPart = StreamSize;
649                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
650                                         return E_FAIL;
651                                 }
652                         }
653                         TRACE_(dmfile)(": reading finished\n");
654                         break;
655                 }
656                 default: {
657                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
658                         liMove.QuadPart = Chunk.dwSize;
659                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
660                         return E_FAIL;
661                 }
662         }
663
664         return S_OK;
665 }
666
667 HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
668   return E_NOTIMPL;
669 }
670
671 HRESULT WINAPI IDirectMusicScriptImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
672   return E_NOTIMPL;
673 }
674
675 static const IPersistStreamVtbl DirectMusicScript_PersistStream_Vtbl = {
676   IDirectMusicScriptImpl_IPersistStream_QueryInterface,
677   IDirectMusicScriptImpl_IPersistStream_AddRef,
678   IDirectMusicScriptImpl_IPersistStream_Release,
679   IDirectMusicScriptImpl_IPersistStream_GetClassID,
680   IDirectMusicScriptImpl_IPersistStream_IsDirty,
681   IDirectMusicScriptImpl_IPersistStream_Load,
682   IDirectMusicScriptImpl_IPersistStream_Save,
683   IDirectMusicScriptImpl_IPersistStream_GetSizeMax
684 };
685
686 /* for ClassFactory */
687 HRESULT WINAPI DMUSIC_CreateDirectMusicScriptImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
688   IDirectMusicScriptImpl* obj;
689   
690   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicScriptImpl));
691   if (NULL == obj) {
692     *ppobj = NULL;
693     return E_OUTOFMEMORY;
694   }
695   obj->UnknownVtbl = &DirectMusicScript_Unknown_Vtbl;
696   obj->ScriptVtbl = &DirectMusicScript_Script_Vtbl;
697   obj->ObjectVtbl = &DirectMusicScript_Object_Vtbl;
698   obj->PersistStreamVtbl = &DirectMusicScript_PersistStream_Vtbl;
699   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
700   DM_STRUCT_INIT(obj->pDesc);
701   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
702   memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicScript, sizeof (CLSID));
703   obj->ref = 0; /* will be inited by QueryInterface */
704   
705   return IDirectMusicScriptImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
706 }