d3d11: Implement d3d11_immediate_context_DSGetSamplers().
[wine.git] / dlls / dmloader / container.c
blobab43c6bc07c78b4f217cfe4bee82848a4948f5c6
1 /* IDirectMusicContainerImpl
3 * Copyright (C) 2003-2004 Rok Mandeljc
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "dmloader_private.h"
21 #include "dmobject.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25 WINE_DECLARE_DEBUG_CHANNEL(dmdump);
27 #define DMUS_MAX_CATEGORY_SIZE DMUS_MAX_CATEGORY*sizeof(WCHAR)
28 #define DMUS_MAX_NAME_SIZE DMUS_MAX_NAME*sizeof(WCHAR)
29 #define DMUS_MAX_FILENAME_SIZE DMUS_MAX_FILENAME*sizeof(WCHAR)
31 /*****************************************************************************
32 * IDirectMusicContainerImpl implementation
34 typedef struct IDirectMusicContainerImpl {
35 IDirectMusicContainer IDirectMusicContainer_iface;
36 struct dmobject dmobj;
37 LONG ref;
38 IStream *pStream;
39 DMUS_IO_CONTAINER_HEADER Header; /* header */
40 struct list *pContainedObjects; /* data */
41 } IDirectMusicContainerImpl;
43 static inline IDirectMusicContainerImpl *impl_from_IDirectMusicContainer(IDirectMusicContainer *iface)
45 return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, IDirectMusicContainer_iface);
48 static HRESULT destroy_dmcontainer(IDirectMusicContainerImpl *This)
50 LPDIRECTMUSICLOADER pLoader;
51 LPDIRECTMUSICGETLOADER pGetLoader;
52 struct list *pEntry;
53 LPWINE_CONTAINER_ENTRY pContainedObject;
55 /* get loader (from stream we loaded from) */
56 TRACE(": getting loader\n");
57 IStream_QueryInterface (This->pStream, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader);
58 IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
59 IDirectMusicGetLoader_Release (pGetLoader);
61 /* release objects from loader's cache (if appropriate) */
62 TRACE(": releasing objects from loader's cache\n");
63 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
64 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
65 /* my tests indicate that container releases objects *only*
66 if they were loaded at its load-time (makes sense, it doesn't
67 have pointers to objects otherwise); BTW: native container seems
68 to ignore the flags (I won't) */
69 if (pContainedObject->pObject && !(pContainedObject->dwFlags & DMUS_CONTAINED_OBJF_KEEP)) {
70 /* flags say it shouldn't be kept in loader's cache */
71 IDirectMusicLoader_ReleaseObject (pLoader, pContainedObject->pObject);
74 IDirectMusicLoader_Release (pLoader);
76 /* release stream we loaded from */
77 IStream_Release (This->pStream);
79 /* FIXME: release allocated entries */
81 return S_OK;
84 static HRESULT WINAPI IDirectMusicContainerImpl_QueryInterface(IDirectMusicContainer *iface, REFIID riid, void **ret_iface)
86 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
88 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
90 if (IsEqualIID (riid, &IID_IUnknown) || IsEqualIID (riid, &IID_IDirectMusicContainer))
91 *ret_iface = &This->IDirectMusicContainer_iface;
92 else if (IsEqualIID (riid, &IID_IDirectMusicObject))
93 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
94 else if (IsEqualIID (riid, &IID_IPersistStream))
95 *ret_iface = &This->dmobj.IPersistStream_iface;
96 else {
97 WARN("Unknown interface %s\n", debugstr_dmguid(riid));
98 *ret_iface = NULL;
99 return E_NOINTERFACE;
102 IUnknown_AddRef((IUnknown*)*ret_iface);
103 return S_OK;
106 static ULONG WINAPI IDirectMusicContainerImpl_AddRef(IDirectMusicContainer *iface)
108 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
109 LONG ref = InterlockedIncrement(&This->ref);
111 TRACE("(%p) ref=%d\n", This, ref);
113 return ref;
116 static ULONG WINAPI IDirectMusicContainerImpl_Release(IDirectMusicContainer *iface)
118 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
119 LONG ref = InterlockedDecrement(&This->ref);
121 TRACE("(%p) ref=%d\n", This, ref);
123 if (!ref) {
124 if (This->pStream)
125 destroy_dmcontainer(This);
126 HeapFree(GetProcessHeap(), 0, This);
127 unlock_module();
130 return ref;
133 static HRESULT WINAPI IDirectMusicContainerImpl_EnumObject(IDirectMusicContainer *iface,
134 REFGUID rguidClass, DWORD dwIndex, DMUS_OBJECTDESC *pDesc, WCHAR *pwszAlias)
136 IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
137 struct list *pEntry;
138 LPWINE_CONTAINER_ENTRY pContainedObject;
139 DWORD dwCount = 0;
141 TRACE("(%p, %s, %d, %p, %p)\n", This, debugstr_dmguid(rguidClass), dwIndex, pDesc, pwszAlias);
143 if (!pDesc)
144 return E_POINTER;
145 if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
146 ERR(": invalid pDesc->dwSize %d\n", pDesc->dwSize);
147 return E_INVALIDARG;
150 DM_STRUCT_INIT(pDesc);
152 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
153 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
155 if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pContainedObject->Desc.guidClass)) {
156 if (dwCount == dwIndex) {
157 HRESULT result = S_OK;
158 if (pwszAlias) {
159 lstrcpynW (pwszAlias, pContainedObject->wszAlias, DMUS_MAX_FILENAME);
160 if (strlenW (pContainedObject->wszAlias) > DMUS_MAX_FILENAME)
161 result = DMUS_S_STRING_TRUNCATED;
163 *pDesc = pContainedObject->Desc;
164 return result;
166 dwCount++;
170 TRACE(": not found\n");
171 return S_FALSE;
174 static const IDirectMusicContainerVtbl dmcontainer_vtbl = {
175 IDirectMusicContainerImpl_QueryInterface,
176 IDirectMusicContainerImpl_AddRef,
177 IDirectMusicContainerImpl_Release,
178 IDirectMusicContainerImpl_EnumObject
181 /* IDirectMusicObject part: */
182 static inline IDirectMusicContainerImpl *impl_from_IDirectMusicObject(IDirectMusicObject *iface)
184 return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, dmobj.IDirectMusicObject_iface);
187 static HRESULT WINAPI IDirectMusicObjectImpl_ParseDescriptor(IDirectMusicObject *iface,
188 IStream *pStream, DMUS_OBJECTDESC *pDesc)
190 IDirectMusicContainerImpl *This = impl_from_IDirectMusicObject(iface);
191 WINE_CHUNK Chunk;
192 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
193 LARGE_INTEGER liMove; /* used when skipping chunks */
195 TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
197 /* check whether arguments are OK */
198 if (IsBadReadPtr (pStream, sizeof(LPVOID))) {
199 ERR(": pStream bad read pointer\n");
200 return E_POINTER;
202 /* check whether pDesc is OK */
203 if (IsBadReadPtr (pDesc, sizeof(DWORD))) {
204 ERR(": pDesc->dwSize bad read pointer\n");
205 return E_POINTER;
207 if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
208 ERR(": invalid pDesc->dwSize\n");
209 return E_INVALIDARG;
211 if (IsBadWritePtr (pDesc, sizeof(DMUS_OBJECTDESC))) {
212 ERR(": pDesc bad write pointer\n");
213 return E_POINTER;
216 DM_STRUCT_INIT(pDesc);
218 /* here we go... */
219 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
220 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
221 switch (Chunk.fccID) {
222 case FOURCC_RIFF: {
223 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
224 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
225 StreamSize = Chunk.dwSize - sizeof(FOURCC);
226 StreamCount = 0;
227 if (Chunk.fccID == DMUS_FOURCC_CONTAINER_FORM) {
228 TRACE_(dmfile)(": container form\n");
229 /* set guidClass */
230 pDesc->dwValidData |= DMUS_OBJ_CLASS;
231 pDesc->guidClass = CLSID_DirectMusicContainer;
232 do {
233 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
234 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
235 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
236 switch (Chunk.fccID) {
237 case DMUS_FOURCC_GUID_CHUNK: {
238 TRACE_(dmfile)(": GUID chunk\n");
239 pDesc->dwValidData |= DMUS_OBJ_OBJECT;
240 IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
241 TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&pDesc->guidObject));
242 break;
244 case DMUS_FOURCC_VERSION_CHUNK: {
245 TRACE_(dmfile)(": version chunk\n");
246 pDesc->dwValidData |= DMUS_OBJ_VERSION;
247 IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
248 TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&pDesc->vVersion));
249 break;
251 case DMUS_FOURCC_DATE_CHUNK: {
252 TRACE_(dmfile)(": date chunk\n");
253 IStream_Read (pStream, &pDesc->ftDate, Chunk.dwSize, NULL);
254 pDesc->dwValidData |= DMUS_OBJ_DATE;
255 TRACE_(dmdump)(": date: %s\n", debugstr_filetime(&pDesc->ftDate));
256 break;
258 case DMUS_FOURCC_CATEGORY_CHUNK: {
259 TRACE_(dmfile)(": category chunk\n");
260 /* if it happens that string is too long,
261 read what we can and skip the rest*/
262 if (Chunk.dwSize > DMUS_MAX_CATEGORY_SIZE) {
263 IStream_Read (pStream, pDesc->wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
264 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
265 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
266 } else {
267 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
269 pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
270 TRACE_(dmdump)(": category: %s\n", debugstr_w(pDesc->wszCategory));
271 break;
273 case FOURCC_LIST: {
274 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
275 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
276 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
277 ListCount[0] = 0;
278 switch (Chunk.fccID) {
279 /* evil M$ UNFO list, which can (!?) contain INFO elements */
280 case DMUS_FOURCC_UNFO_LIST: {
281 TRACE_(dmfile)(": UNFO list\n");
282 do {
283 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
284 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
285 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
286 switch (Chunk.fccID) {
287 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
288 (though strings seem to be valid unicode) */
289 case mmioFOURCC('I','N','A','M'):
290 case DMUS_FOURCC_UNAM_CHUNK: {
291 TRACE_(dmfile)(": name chunk\n");
292 /* if it happens that string is too long,
293 read what we can and skip the rest*/
294 if (Chunk.dwSize > DMUS_MAX_NAME_SIZE) {
295 IStream_Read (pStream, pDesc->wszName, DMUS_MAX_NAME_SIZE, NULL);
296 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
297 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
298 } else {
299 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
301 pDesc->dwValidData |= DMUS_OBJ_NAME;
302 TRACE_(dmdump)(": name: %s\n", debugstr_w(pDesc->wszName));
303 break;
305 default: {
306 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
307 liMove.QuadPart = Chunk.dwSize;
308 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
309 break;
312 TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
313 } while (ListCount[0] < ListSize[0]);
314 break;
316 default: {
317 TRACE_(dmfile)(": unknown (skipping)\n");
318 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
319 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
320 break;
323 break;
325 default: {
326 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
327 liMove.QuadPart = Chunk.dwSize;
328 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
329 break;
332 TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
333 } while (StreamCount < StreamSize);
334 } else {
335 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
336 liMove.QuadPart = StreamSize;
337 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
338 return E_FAIL;
341 TRACE_(dmfile)(": reading finished\n");
342 break;
344 default: {
345 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
346 liMove.QuadPart = Chunk.dwSize;
347 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
348 return DMUS_E_INVALIDFILE;
352 TRACE(": returning descriptor:\n");
353 if (TRACE_ON(dmloader))
354 dump_DMUS_OBJECTDESC(pDesc);
355 return S_OK;
358 static const IDirectMusicObjectVtbl dmobject_vtbl = {
359 dmobj_IDirectMusicObject_QueryInterface,
360 dmobj_IDirectMusicObject_AddRef,
361 dmobj_IDirectMusicObject_Release,
362 dmobj_IDirectMusicObject_GetDescriptor,
363 dmobj_IDirectMusicObject_SetDescriptor,
364 IDirectMusicObjectImpl_ParseDescriptor
367 /* IPersistStream part: */
368 static inline IDirectMusicContainerImpl *impl_from_IPersistStream(IPersistStream *iface)
370 return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, dmobj.IPersistStream_iface);
373 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
375 IDirectMusicContainerImpl *This = impl_from_IPersistStream(iface);
376 WINE_CHUNK Chunk;
377 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
378 LARGE_INTEGER liMove; /* used when skipping chunks */
379 ULARGE_INTEGER uliPos; /* needed when dealing with RIFF chunks */
380 LPDIRECTMUSICGETLOADER pGetLoader;
381 LPDIRECTMUSICLOADER pLoader;
382 HRESULT result = S_OK;
384 TRACE("(%p, %p):\n", This, pStm);
386 /* check whether pStm is valid read pointer */
387 if (IsBadReadPtr (pStm, sizeof(LPVOID))) {
388 ERR(": pStm bad read pointer\n");
389 return E_POINTER;
391 /* if stream is already set, this means the container is already loaded */
392 if (This->pStream) {
393 TRACE(": stream is already set, which means container is already loaded\n");
394 return DMUS_E_ALREADY_LOADED;
397 /* get loader since it will be needed later */
398 if (FAILED(IStream_QueryInterface (pStm, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader))) {
399 ERR(": stream not supported\n");
400 return DMUS_E_UNSUPPORTED_STREAM;
402 IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
403 IDirectMusicGetLoader_Release (pGetLoader);
405 This->pStream = pStm;
406 IStream_AddRef (pStm); /* add count for later references */
408 /* start with load */
409 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
410 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
411 switch (Chunk.fccID) {
412 case FOURCC_RIFF: {
413 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
414 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
415 StreamSize = Chunk.dwSize - sizeof(FOURCC);
416 StreamCount = 0;
417 switch (Chunk.fccID) {
418 case DMUS_FOURCC_CONTAINER_FORM: {
419 TRACE_(dmfile)(": container form\n");
420 This->dmobj.desc.guidClass = CLSID_DirectMusicContainer;
421 This->dmobj.desc.dwValidData |= DMUS_OBJ_CLASS;
422 do {
423 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
424 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
425 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
426 switch (Chunk.fccID) {
427 case DMUS_FOURCC_CONTAINER_CHUNK: {
428 TRACE_(dmfile)(": container header chunk\n");
429 IStream_Read (pStm, &This->Header, Chunk.dwSize, NULL);
430 TRACE_(dmdump)(": container header chunk:\n%s\n", debugstr_DMUS_IO_CONTAINER_HEADER(&This->Header));
431 break;
433 case DMUS_FOURCC_GUID_CHUNK: {
434 TRACE_(dmfile)(": GUID chunk\n");
435 IStream_Read (pStm, &This->dmobj.desc.guidObject, Chunk.dwSize, NULL);
436 This->dmobj.desc.dwValidData |= DMUS_OBJ_OBJECT;
437 TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&This->dmobj.desc.guidObject));
438 break;
440 case DMUS_FOURCC_VERSION_CHUNK: {
441 TRACE_(dmfile)(": version chunk\n");
442 IStream_Read (pStm, &This->dmobj.desc.vVersion, Chunk.dwSize, NULL);
443 This->dmobj.desc.dwValidData |= DMUS_OBJ_VERSION;
444 TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&This->dmobj.desc.vVersion));
445 break;
447 case DMUS_FOURCC_DATE_CHUNK: {
448 TRACE_(dmfile)(": date chunk\n");
449 IStream_Read (pStm, &This->dmobj.desc.ftDate, Chunk.dwSize, NULL);
450 This->dmobj.desc.dwValidData |= DMUS_OBJ_DATE;
451 TRACE_(dmdump)(": date: %s\n", debugstr_filetime(&This->dmobj.desc.ftDate));
452 break;
454 case DMUS_FOURCC_CATEGORY_CHUNK: {
455 TRACE_(dmfile)(": category chunk\n");
456 /* if it happens that string is too long,
457 read what we can and skip the rest*/
458 if (Chunk.dwSize > DMUS_MAX_CATEGORY_SIZE) {
459 IStream_Read (pStm, This->dmobj.desc.wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
460 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
461 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
462 } else {
463 IStream_Read (pStm, This->dmobj.desc.wszCategory, Chunk.dwSize, NULL);
465 This->dmobj.desc.dwValidData |= DMUS_OBJ_CATEGORY;
466 TRACE_(dmdump)(": category: %s\n", debugstr_w(This->dmobj.desc.wszCategory));
467 break;
469 case FOURCC_LIST: {
470 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
471 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
472 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
473 ListCount[0] = 0;
474 switch (Chunk.fccID) {
475 case DMUS_FOURCC_UNFO_LIST: {
476 TRACE_(dmfile)(": UNFO list\n");
477 do {
478 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
479 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
480 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
481 switch (Chunk.fccID) {
482 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
483 (though strings seem to be valid unicode) */
484 case mmioFOURCC('I','N','A','M'):
485 case DMUS_FOURCC_UNAM_CHUNK: {
486 TRACE_(dmfile)(": name chunk\n");
487 /* if it happens that string is too long,
488 read what we can and skip the rest*/
489 if (Chunk.dwSize > DMUS_MAX_NAME_SIZE) {
490 IStream_Read (pStm, This->dmobj.desc.wszName, DMUS_MAX_NAME_SIZE, NULL);
491 liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
492 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
493 } else {
494 IStream_Read (pStm, This->dmobj.desc.wszName, Chunk.dwSize, NULL);
496 This->dmobj.desc.dwValidData |= DMUS_OBJ_NAME;
497 TRACE_(dmdump)(": name: %s\n", debugstr_w(This->dmobj.desc.wszName));
498 break;
500 default: {
501 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
502 liMove.QuadPart = Chunk.dwSize;
503 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
504 break;
507 TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
508 } while (ListCount[0] < ListSize[0]);
509 break;
511 case DMUS_FOURCC_CONTAINED_OBJECTS_LIST: {
512 TRACE_(dmfile)(": contained objects list\n");
513 do {
514 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
515 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
516 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
517 switch (Chunk.fccID) {
518 case FOURCC_LIST: {
519 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
520 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
521 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
522 ListCount[1] = 0;
523 switch (Chunk.fccID) {
524 case DMUS_FOURCC_CONTAINED_OBJECT_LIST: {
525 LPWINE_CONTAINER_ENTRY pNewEntry;
526 TRACE_(dmfile)(": contained object list\n");
527 pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_CONTAINER_ENTRY));
528 DM_STRUCT_INIT(&pNewEntry->Desc);
529 do {
530 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
531 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
532 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
533 switch (Chunk.fccID) {
534 case DMUS_FOURCC_CONTAINED_ALIAS_CHUNK: {
535 TRACE_(dmfile)(": alias chunk\n");
536 pNewEntry->wszAlias = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
537 IStream_Read (pStm, pNewEntry->wszAlias, Chunk.dwSize, NULL);
538 TRACE_(dmdump)(": alias: %s\n", debugstr_w(pNewEntry->wszAlias));
539 break;
541 case DMUS_FOURCC_CONTAINED_OBJECT_CHUNK: {
542 DMUS_IO_CONTAINED_OBJECT_HEADER tmpObjectHeader;
543 TRACE_(dmfile)(": contained object header chunk\n");
544 IStream_Read (pStm, &tmpObjectHeader, Chunk.dwSize, NULL);
545 TRACE_(dmdump)(": contained object header:\n%s\n", debugstr_DMUS_IO_CONTAINED_OBJECT_HEADER(&tmpObjectHeader));
546 /* copy guidClass */
547 pNewEntry->Desc.dwValidData |= DMUS_OBJ_CLASS;
548 pNewEntry->Desc.guidClass = tmpObjectHeader.guidClassID;
549 /* store flags */
550 pNewEntry->dwFlags = tmpObjectHeader.dwFlags;
551 break;
553 /* now read data... it may be safe to read everything after object header chunk,
554 but I'm not comfortable with MSDN's "the header is *normally* followed by ..." */
555 case FOURCC_LIST: {
556 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
557 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
558 ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
559 ListCount[2] = 0;
560 switch (Chunk.fccID) {
561 case DMUS_FOURCC_REF_LIST: {
562 TRACE_(dmfile)(": reference list\n");
563 pNewEntry->bIsRIFF = 0;
564 do {
565 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
566 ListCount[2] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
567 TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
568 switch (Chunk.fccID) {
569 case DMUS_FOURCC_REF_CHUNK: {
570 DMUS_IO_REFERENCE tmpReferenceHeader; /* temporary structure */
571 TRACE_(dmfile)(": reference header chunk\n");
572 memset (&tmpReferenceHeader, 0, sizeof(DMUS_IO_REFERENCE));
573 IStream_Read (pStm, &tmpReferenceHeader, Chunk.dwSize, NULL);
574 /* copy retrieved data to DMUS_OBJECTDESC */
575 if (!IsEqualCLSID (&pNewEntry->Desc.guidClass, &tmpReferenceHeader.guidClassID)) ERR(": object header declares different CLSID than reference header?\n");
576 /* it shouldn't be necessary to copy guidClass, since it was set in contained object header already...
577 yet if they happen to be different, I'd rather stick to this one */
578 pNewEntry->Desc.guidClass = tmpReferenceHeader.guidClassID;
579 pNewEntry->Desc.dwValidData |= tmpReferenceHeader.dwValidData;
580 break;
582 case DMUS_FOURCC_GUID_CHUNK: {
583 TRACE_(dmfile)(": guid chunk\n");
584 /* no need to set flags since they were copied from reference header */
585 IStream_Read (pStm, &pNewEntry->Desc.guidObject, Chunk.dwSize, NULL);
586 break;
588 case DMUS_FOURCC_DATE_CHUNK: {
589 TRACE_(dmfile)(": file date chunk\n");
590 /* no need to set flags since they were copied from reference header */
591 IStream_Read (pStm, &pNewEntry->Desc.ftDate, Chunk.dwSize, NULL);
592 break;
594 case DMUS_FOURCC_NAME_CHUNK: {
595 TRACE_(dmfile)(": name chunk\n");
596 /* no need to set flags since they were copied from reference header */
597 IStream_Read (pStm, pNewEntry->Desc.wszName, Chunk.dwSize, NULL);
598 break;
600 case DMUS_FOURCC_FILE_CHUNK: {
601 TRACE_(dmfile)(": file name chunk\n");
602 /* no need to set flags since they were copied from reference header */
603 IStream_Read (pStm, pNewEntry->Desc.wszFileName, Chunk.dwSize, NULL);
604 break;
606 case DMUS_FOURCC_CATEGORY_CHUNK: {
607 TRACE_(dmfile)(": category chunk\n");
608 /* no need to set flags since they were copied from reference header */
609 IStream_Read (pStm, pNewEntry->Desc.wszCategory, Chunk.dwSize, NULL);
610 break;
612 case DMUS_FOURCC_VERSION_CHUNK: {
613 TRACE_(dmfile)(": version chunk\n");
614 /* no need to set flags since they were copied from reference header */
615 IStream_Read (pStm, &pNewEntry->Desc.vVersion, Chunk.dwSize, NULL);
616 break;
618 default: {
619 TRACE_(dmfile)(": unknown chunk (skipping)\n");
620 liMove.QuadPart = Chunk.dwSize;
621 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip this chunk */
622 break;
625 TRACE_(dmfile)(": ListCount[2] = 0x%08X < ListSize[2] = 0x%08X\n", ListCount[2], ListSize[2]);
626 } while (ListCount[2] < ListSize[2]);
627 break;
629 default: {
630 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
631 return E_FAIL;
634 break;
637 case FOURCC_RIFF: {
638 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
639 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
640 if (IS_VALID_DMFORM (Chunk.fccID)) {
641 TRACE_(dmfile)(": valid DMUSIC form\n");
642 pNewEntry->bIsRIFF = 1;
643 /* we'll have to skip whole RIFF chunk after SetObject is called */
644 liMove.QuadPart = 0;
645 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &uliPos);
646 uliPos.QuadPart += (Chunk.dwSize - sizeof(FOURCC)); /* set uliPos at the end of RIFF chunk */
647 /* move at the beginning of RIFF chunk */
648 liMove.QuadPart = 0;
649 liMove.QuadPart -= (sizeof(FOURCC)+sizeof(DWORD)+sizeof(FOURCC));
650 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
651 /* put pointer to stream in descriptor */
652 pNewEntry->Desc.dwValidData |= DMUS_OBJ_STREAM;
653 pNewEntry->Desc.pStream = pStm; /* we don't have to worry about cloning, since SetObject will perform it */
654 /* wait till we get on the end of object list */
655 } else {
656 TRACE_(dmfile)(": invalid DMUSIC form (skipping)\n");
657 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
658 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
659 /* FIXME: should we return E_FAIL? */
661 break;
663 default: {
664 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
665 liMove.QuadPart = Chunk.dwSize;
666 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
667 break;
670 TRACE_(dmfile)(": ListCount[1] = 0x%08X < ListSize[1] = 0x%08X\n", ListCount[1], ListSize[1]);
671 } while (ListCount[1] < ListSize[1]);
672 /* SetObject: this will fill descriptor with additional info and add alias in loader's cache */
673 IDirectMusicLoader_SetObject (pLoader, &pNewEntry->Desc);
674 /* now that SetObject collected appropriate info into descriptor we can live happily ever after;
675 or not, since we have to clean evidence of loading through stream... *sigh*
676 and we have to skip rest of the chunk, if we loaded through RIFF */
677 if (pNewEntry->bIsRIFF) {
678 liMove.QuadPart = uliPos.QuadPart;
679 IStream_Seek (pStm, liMove, STREAM_SEEK_SET, NULL);
680 pNewEntry->Desc.dwValidData &= ~DMUS_OBJ_STREAM; /* clear flag (and with bitwise complement) */
681 pNewEntry->Desc.pStream = NULL;
683 /* add entry to list of objects */
684 list_add_tail (This->pContainedObjects, &pNewEntry->entry);
685 break;
687 default: {
688 TRACE_(dmfile)(": unknown (skipping)\n");
689 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
690 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
691 break;
694 break;
696 default: {
697 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
698 liMove.QuadPart = Chunk.dwSize;
699 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
700 break;
703 TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
704 } while (ListCount[0] < ListSize[0]);
705 break;
707 default: {
708 TRACE_(dmfile)(": unknown (skipping)\n");
709 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
710 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
711 break;
714 break;
716 default: {
717 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
718 liMove.QuadPart = Chunk.dwSize;
719 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
720 break;
723 TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
724 } while (StreamCount < StreamSize);
725 break;
727 default: {
728 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
729 liMove.QuadPart = StreamSize;
730 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
731 return E_FAIL;
734 TRACE_(dmfile)(": reading finished\n");
735 This->dmobj.desc.dwValidData |= DMUS_OBJ_LOADED;
736 break;
738 default: {
739 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
740 liMove.QuadPart = Chunk.dwSize;
741 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
742 return E_FAIL;
746 /* now, if DMUS_CONTAINER_NOLOADS is not set, we are supposed to load contained objects;
747 so when we call GetObject later, they'll already be in cache */
748 if (!(This->Header.dwFlags & DMUS_CONTAINER_NOLOADS)) {
749 struct list *pEntry;
750 LPWINE_CONTAINER_ENTRY pContainedObject;
752 TRACE(": DMUS_CONTAINER_NOLOADS not set... load all objects\n");
754 LIST_FOR_EACH (pEntry, This->pContainedObjects) {
755 IDirectMusicObject* pObject;
756 pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
757 /* get object from loader and then release it */
758 if (SUCCEEDED(IDirectMusicLoader_GetObject (pLoader, &pContainedObject->Desc, &IID_IDirectMusicObject, (LPVOID*)&pObject))) {
759 pContainedObject->pObject = pObject; /* for final release */
760 IDirectMusicObject_Release (pObject); /* we don't really need this one */
761 } else {
762 WARN(": failed to load contained object\n");
763 result = DMUS_S_PARTIALLOAD;
768 IDirectMusicLoader_Release (pLoader); /* release loader */
770 #if 0
771 /* DEBUG: dumps whole container object tree: */
772 if (TRACE_ON(dmloader)) {
773 int r = 0;
774 LPWINE_CONTAINER_ENTRY tmpEntry;
775 struct list *listEntry;
777 TRACE("*** IDirectMusicContainer (%p) ***\n", This->ContainerVtbl);
778 TRACE(" - Objects:\n");
779 LIST_FOR_EACH (listEntry, This->pContainedObjects) {
780 tmpEntry = LIST_ENTRY( listEntry, WINE_CONTAINER_ENTRY, entry );
781 TRACE(" - Object[%i]:\n", r);
782 TRACE(" - wszAlias: %s\n", debugstr_w(tmpEntry->wszAlias));
783 TRACE(" - Object descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC(&tmpEntry->Desc));
784 r++;
787 #endif
789 return result;
792 static const IPersistStreamVtbl persiststream_vtbl = {
793 dmobj_IPersistStream_QueryInterface,
794 dmobj_IPersistStream_AddRef,
795 dmobj_IPersistStream_Release,
796 dmobj_IPersistStream_GetClassID,
797 unimpl_IPersistStream_IsDirty,
798 IPersistStreamImpl_Load,
799 unimpl_IPersistStream_Save,
800 unimpl_IPersistStream_GetSizeMax
803 /* for ClassFactory */
804 HRESULT WINAPI create_dmcontainer(REFIID lpcGUID, void **ppobj)
806 IDirectMusicContainerImpl* obj;
807 HRESULT hr;
809 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicContainerImpl));
810 if (NULL == obj) {
811 *ppobj = NULL;
812 return E_OUTOFMEMORY;
814 obj->IDirectMusicContainer_iface.lpVtbl = &dmcontainer_vtbl;
815 obj->ref = 1;
816 dmobject_init(&obj->dmobj, &CLSID_DirectMusicContainer,
817 (IUnknown*)&obj->IDirectMusicContainer_iface);
818 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
819 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
820 obj->pContainedObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
821 list_init (obj->pContainedObjects);
823 lock_module();
825 hr = IDirectMusicContainer_QueryInterface(&obj->IDirectMusicContainer_iface, lpcGUID, ppobj);
826 IDirectMusicContainer_Release(&obj->IDirectMusicContainer_iface);
828 return hr;