container.c 34.8 KB
Newer Older
1
/* IDirectMusicContainerImpl
2
 *
3
 * Copyright (C) 2003-2004 Rok Mandeljc
4
 *
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
9 10 11
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
14
 *
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19 20
 */

#include "dmloader_private.h"
21
#include "dmobject.h"
22

23
WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
24
WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25 26 27 28 29
WINE_DECLARE_DEBUG_CHANNEL(dmdump);

#define DMUS_MAX_CATEGORY_SIZE DMUS_MAX_CATEGORY*sizeof(WCHAR)
#define DMUS_MAX_NAME_SIZE     DMUS_MAX_NAME*sizeof(WCHAR)
#define DMUS_MAX_FILENAME_SIZE DMUS_MAX_FILENAME*sizeof(WCHAR)
30

31 32 33
/*****************************************************************************
 * IDirectMusicContainerImpl implementation
 */
34 35
typedef struct IDirectMusicContainerImpl {
    IDirectMusicContainer IDirectMusicContainer_iface;
36
    struct dmobject dmobj;
37 38 39 40 41 42 43 44 45 46
    LONG ref;
    IStream *pStream;
    DMUS_IO_CONTAINER_HEADER Header; /* header */
    struct list *pContainedObjects;  /* data */
} IDirectMusicContainerImpl;

static inline IDirectMusicContainerImpl *impl_from_IDirectMusicContainer(IDirectMusicContainer *iface)
{
    return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, IDirectMusicContainer_iface);
}
47

48 49
static HRESULT destroy_dmcontainer(IDirectMusicContainerImpl *This)
{
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	LPDIRECTMUSICLOADER pLoader;
	LPDIRECTMUSICGETLOADER pGetLoader;
	struct list *pEntry;
	LPWINE_CONTAINER_ENTRY pContainedObject;

	/* get loader (from stream we loaded from) */
	TRACE(": getting loader\n");
	IStream_QueryInterface (This->pStream, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader);
	IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
	IDirectMusicGetLoader_Release (pGetLoader);

	/* release objects from loader's cache (if appropriate) */
	TRACE(": releasing objects from loader's cache\n");
	LIST_FOR_EACH (pEntry, This->pContainedObjects) {
		pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
		/* my tests indicate that container releases objects *only*
		   if they were loaded at its load-time (makes sense, it doesn't
		   have pointers to objects otherwise); BTW: native container seems
		   to ignore the flags (I won't) */
		if (pContainedObject->pObject && !(pContainedObject->dwFlags & DMUS_CONTAINED_OBJF_KEEP)) {
			/* flags say it shouldn't be kept in loader's cache */
			IDirectMusicLoader_ReleaseObject (pLoader, pContainedObject->pObject);
		}
	}
	IDirectMusicLoader_Release (pLoader);

	/* release stream we loaded from */
	IStream_Release (This->pStream);

	/* FIXME: release allocated entries */

	return S_OK;
}

84 85 86 87 88 89 90 91 92
static HRESULT WINAPI IDirectMusicContainerImpl_QueryInterface(IDirectMusicContainer *iface, REFIID riid, void **ret_iface)
{
    IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);

    TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);

    if (IsEqualIID (riid, &IID_IUnknown) || IsEqualIID (riid, &IID_IDirectMusicContainer))
        *ret_iface = &This->IDirectMusicContainer_iface;
    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 97 98 99 100 101 102 103
    else {
        WARN("Unknown interface %s\n", debugstr_dmguid(riid));
        *ret_iface = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ret_iface);
    return S_OK;
104 105
}

106 107 108 109 110 111 112 113
static ULONG WINAPI IDirectMusicContainerImpl_AddRef(IDirectMusicContainer *iface)
{
    IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static ULONG WINAPI IDirectMusicContainerImpl_Release(IDirectMusicContainer *iface)
{
    IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    if (!ref) {
        if (This->pStream)
            destroy_dmcontainer(This);
        HeapFree(GetProcessHeap(), 0, This);
        unlock_module();
    }

    return ref;
131
}
132

133 134 135 136
static HRESULT WINAPI IDirectMusicContainerImpl_EnumObject(IDirectMusicContainer *iface,
        REFGUID rguidClass, DWORD dwIndex, DMUS_OBJECTDESC *pDesc, WCHAR *pwszAlias)
{
	IDirectMusicContainerImpl *This = impl_from_IDirectMusicContainer(iface);
137 138 139
	struct list *pEntry;
	LPWINE_CONTAINER_ENTRY pContainedObject;
	DWORD dwCount = 0;
140

141
	TRACE("(%p, %s, %d, %p, %p)\n", This, debugstr_dmguid(rguidClass), dwIndex, pDesc, pwszAlias);
142

143 144 145 146 147
	if (!pDesc)
		return E_POINTER;
	if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
		ERR(": invalid pDesc->dwSize %d\n", pDesc->dwSize);
		return E_INVALIDARG;
148
	}
149

150
	DM_STRUCT_INIT(pDesc);
151

152 153 154 155 156 157 158
	LIST_FOR_EACH (pEntry, This->pContainedObjects) {
		pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);
		
		if (IsEqualGUID (rguidClass, &GUID_DirectMusicAllTypes) || IsEqualGUID (rguidClass, &pContainedObject->Desc.guidClass)) {
			if (dwCount == dwIndex) {
				HRESULT result = S_OK;
				if (pwszAlias) {
Peter Berg Larsen's avatar
Peter Berg Larsen committed
159
					lstrcpynW (pwszAlias, pContainedObject->wszAlias, DMUS_MAX_FILENAME);
160 161 162
					if (strlenW (pContainedObject->wszAlias) > DMUS_MAX_FILENAME)
						result = DMUS_S_STRING_TRUNCATED;
				}
163
				*pDesc = pContainedObject->Desc;
164 165 166 167 168 169 170
				return result;
			}
			dwCount++;
		}
	}		
	
	TRACE(": not found\n");
171
	return S_FALSE;
172
}
173

174 175 176 177 178
static const IDirectMusicContainerVtbl dmcontainer_vtbl = {
    IDirectMusicContainerImpl_QueryInterface,
    IDirectMusicContainerImpl_AddRef,
    IDirectMusicContainerImpl_Release,
    IDirectMusicContainerImpl_EnumObject
179
};
180

181
/* IDirectMusicObject part: */
182 183 184
static inline IDirectMusicContainerImpl *impl_from_IDirectMusicObject(IDirectMusicObject *iface)
{
    return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, dmobj.IDirectMusicObject_iface);
185 186
}

187 188 189
static HRESULT WINAPI IDirectMusicObjectImpl_ParseDescriptor(IDirectMusicObject *iface,
        IStream *pStream, DMUS_OBJECTDESC *pDesc)
{
190
        IDirectMusicContainerImpl *This = impl_from_IDirectMusicObject(iface);
191
	WINE_CHUNK Chunk;
192 193
	DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
	LARGE_INTEGER liMove; /* used when skipping chunks */
194

195
	TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
196
	
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	/* check whether arguments are OK */
	if (IsBadReadPtr (pStream, sizeof(LPVOID))) {
		ERR(": pStream bad read pointer\n");
		return E_POINTER;
	}
	/* check whether pDesc is OK */
	if (IsBadReadPtr (pDesc, sizeof(DWORD))) {
		ERR(": pDesc->dwSize bad read pointer\n");
		return E_POINTER;
	}
	if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
		ERR(": invalid pDesc->dwSize\n");
		return E_INVALIDARG;
	}
	if (IsBadWritePtr (pDesc, sizeof(DMUS_OBJECTDESC))) {
		ERR(": pDesc bad write pointer\n");
		return E_POINTER;
	}

	DM_STRUCT_INIT(pDesc);
217
	
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 222 223 224 225 226 227 228
	switch (Chunk.fccID) {	
		case FOURCC_RIFF: {
			IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);				
			TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
			StreamSize = Chunk.dwSize - sizeof(FOURCC);
			StreamCount = 0;
			if (Chunk.fccID == DMUS_FOURCC_CONTAINER_FORM) {
				TRACE_(dmfile)(": container form\n");
229 230
				/* set guidClass */
				pDesc->dwValidData |= DMUS_OBJ_CLASS;
231
				pDesc->guidClass = CLSID_DirectMusicContainer;
232 233 234
				do {
					IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
					StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
235
					TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
236 237 238 239 240
					switch (Chunk.fccID) {
						case DMUS_FOURCC_GUID_CHUNK: {
							TRACE_(dmfile)(": GUID chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_OBJECT;
							IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
241
							TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&pDesc->guidObject));
242 243 244 245 246 247
							break;
						}
						case DMUS_FOURCC_VERSION_CHUNK: {
							TRACE_(dmfile)(": version chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_VERSION;
							IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
248
							TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&pDesc->vVersion));
249 250
							break;
						}
251 252 253 254 255 256 257
						case DMUS_FOURCC_DATE_CHUNK: {
							TRACE_(dmfile)(": date chunk\n");
							IStream_Read (pStream, &pDesc->ftDate, Chunk.dwSize, NULL);
							pDesc->dwValidData |= DMUS_OBJ_DATE;
							TRACE_(dmdump)(": date: %s\n", debugstr_filetime(&pDesc->ftDate));
							break;
						}								
258 259
						case DMUS_FOURCC_CATEGORY_CHUNK: {
							TRACE_(dmfile)(": category chunk\n");
260 261 262 263 264 265 266 267 268
							/* if it happens that string is too long,
							   read what we can and skip the rest*/
							if (Chunk.dwSize > DMUS_MAX_CATEGORY_SIZE) {
								IStream_Read (pStream, pDesc->wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
								liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
								IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
							} else {
								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 272 273 274 275 276 277 278 279 280 281 282 283 284
							break;
						}
						case FOURCC_LIST: {
							IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);				
							TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
							ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
							ListCount[0] = 0;
							switch (Chunk.fccID) {
								/* evil M$ UNFO list, which can (!?) contain INFO elements */
								case DMUS_FOURCC_UNFO_LIST: {
									TRACE_(dmfile)(": UNFO list\n");
									do {
										IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
										ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
285
										TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
286 287 288 289 290 291
										switch (Chunk.fccID) {
											/* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
                                             (though strings seem to be valid unicode) */
											case mmioFOURCC('I','N','A','M'):
											case DMUS_FOURCC_UNAM_CHUNK: {
												TRACE_(dmfile)(": name chunk\n");
292 293 294 295 296 297 298 299 300
												/* if it happens that string is too long,
													   read what we can and skip the rest*/
												if (Chunk.dwSize > DMUS_MAX_NAME_SIZE) {
													IStream_Read (pStream, pDesc->wszName, DMUS_MAX_NAME_SIZE, NULL);
													liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
													IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												} else {
													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 304 305
												break;
											}
											default: {
306
												TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
307 308 309 310 311
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;						
											}
										}
312
										TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
313 314 315 316 317 318 319 320 321 322 323 324 325
									} while (ListCount[0] < ListSize[0]);
									break;
								}
								default: {
									TRACE_(dmfile)(": unknown (skipping)\n");
									liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
									IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
									break;						
								}
							}
							break;
						}	
						default: {
326
							TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
327 328 329 330 331
							liMove.QuadPart = Chunk.dwSize;
							IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
							break;						
						}
					}
332
					TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
				} while (StreamCount < StreamSize);
			} else {
				TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
				liMove.QuadPart = StreamSize;
				IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
				return E_FAIL;
			}
		
			TRACE_(dmfile)(": reading finished\n");
			break;
		}
		default: {
			TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
			liMove.QuadPart = Chunk.dwSize;
			IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
			return DMUS_E_INVALIDFILE;
		}
	}	
351
	
352 353 354 355
	TRACE(": returning descriptor:\n");
	if (TRACE_ON(dmloader))
		dump_DMUS_OBJECTDESC(pDesc);
	return S_OK;
356 357
}

358 359 360 361 362 363
static const IDirectMusicObjectVtbl dmobject_vtbl = {
    dmobj_IDirectMusicObject_QueryInterface,
    dmobj_IDirectMusicObject_AddRef,
    dmobj_IDirectMusicObject_Release,
    dmobj_IDirectMusicObject_GetDescriptor,
    dmobj_IDirectMusicObject_SetDescriptor,
364
    IDirectMusicObjectImpl_ParseDescriptor
365 366
};

367
/* IPersistStream part: */
368 369 370
static inline IDirectMusicContainerImpl *impl_from_IPersistStream(IPersistStream *iface)
{
    return CONTAINING_RECORD(iface, IDirectMusicContainerImpl, dmobj.IPersistStream_iface);
371 372
}

373 374
static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
{
375
        IDirectMusicContainerImpl *This = impl_from_IPersistStream(iface);
376
	WINE_CHUNK Chunk;
377 378 379 380 381
	DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
	LARGE_INTEGER liMove; /* used when skipping chunks */
	ULARGE_INTEGER uliPos; /* needed when dealing with RIFF chunks */
	LPDIRECTMUSICGETLOADER pGetLoader;
	LPDIRECTMUSICLOADER pLoader;
382 383 384
	HRESULT result = S_OK;

	TRACE("(%p, %p):\n", This, pStm);
385
	
386 387 388 389 390
	/* check whether pStm is valid read pointer */
	if (IsBadReadPtr (pStm, sizeof(LPVOID))) {
		ERR(": pStm bad read pointer\n");
		return E_POINTER;
	}
Austin English's avatar
Austin English committed
391
	/* if stream is already set, this means the container is already loaded */
392 393 394 395 396
	if (This->pStream) {
		TRACE(": stream is already set, which means container is already loaded\n");
		return DMUS_E_ALREADY_LOADED;
	}

397
	/* get loader since it will be needed later */
398 399 400 401
	if (FAILED(IStream_QueryInterface (pStm, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader))) {
		ERR(": stream not supported\n");
		return DMUS_E_UNSUPPORTED_STREAM;
	}
402 403 404
	IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
	IDirectMusicGetLoader_Release (pGetLoader);
	
405
	This->pStream = pStm;
406
	IStream_AddRef (pStm); /* add count for later references */
407 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 412 413 414 415 416 417 418 419
	switch (Chunk.fccID) {	
		case FOURCC_RIFF: {
			IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
			TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
			StreamSize = Chunk.dwSize - sizeof(FOURCC);
			StreamCount = 0;
			switch (Chunk.fccID) {
				case DMUS_FOURCC_CONTAINER_FORM: {
					TRACE_(dmfile)(": container form\n");
420 421
					This->dmobj.desc.guidClass = CLSID_DirectMusicContainer;
					This->dmobj.desc.dwValidData |= DMUS_OBJ_CLASS;
422 423 424
					do {
						IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
						StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
425
						TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
426 427 428
						switch (Chunk.fccID) {
							case DMUS_FOURCC_CONTAINER_CHUNK: {
								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 432 433 434
								break;	
							}
							case DMUS_FOURCC_GUID_CHUNK: {
								TRACE_(dmfile)(": GUID chunk\n");
435 436 437
								IStream_Read (pStm, &This->dmobj.desc.guidObject, Chunk.dwSize, NULL);
								This->dmobj.desc.dwValidData |= DMUS_OBJ_OBJECT;
								TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&This->dmobj.desc.guidObject));
438 439 440 441
								break;
							}
							case DMUS_FOURCC_VERSION_CHUNK: {
								TRACE_(dmfile)(": version chunk\n");
442 443 444
								IStream_Read (pStm, &This->dmobj.desc.vVersion, Chunk.dwSize, NULL);
								This->dmobj.desc.dwValidData |= DMUS_OBJ_VERSION;
								TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&This->dmobj.desc.vVersion));
445 446
								break;
							}
447 448
							case DMUS_FOURCC_DATE_CHUNK: {
								TRACE_(dmfile)(": date chunk\n");
449 450 451
								IStream_Read (pStm, &This->dmobj.desc.ftDate, Chunk.dwSize, NULL);
								This->dmobj.desc.dwValidData |= DMUS_OBJ_DATE;
								TRACE_(dmdump)(": date: %s\n", debugstr_filetime(&This->dmobj.desc.ftDate));
452 453
								break;
							}							
454 455
							case DMUS_FOURCC_CATEGORY_CHUNK: {
								TRACE_(dmfile)(": category chunk\n");
456 457 458
								/* if it happens that string is too long,
								   read what we can and skip the rest*/
								if (Chunk.dwSize > DMUS_MAX_CATEGORY_SIZE) {
459
									IStream_Read (pStm, This->dmobj.desc.wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
460 461 462
									liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
									IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
								} else {
463
									IStream_Read (pStm, This->dmobj.desc.wszCategory, Chunk.dwSize, NULL);
464
								}
465 466
								This->dmobj.desc.dwValidData |= DMUS_OBJ_CATEGORY;
								TRACE_(dmdump)(": category: %s\n", debugstr_w(This->dmobj.desc.wszCategory));
467 468 469 470 471 472 473 474 475 476 477 478 479
								break;
							}
							case FOURCC_LIST: {
								IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
								TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
								ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
								ListCount[0] = 0;
								switch (Chunk.fccID) {
									case DMUS_FOURCC_UNFO_LIST: {
										TRACE_(dmfile)(": UNFO list\n");
										do {
											IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
											ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
480
											TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
481 482 483 484 485 486
											switch (Chunk.fccID) {
												/* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
                                              (though strings seem to be valid unicode) */
												case mmioFOURCC('I','N','A','M'):
												case DMUS_FOURCC_UNAM_CHUNK: {
													TRACE_(dmfile)(": name chunk\n");
487 488 489
													/* if it happens that string is too long,
													   read what we can and skip the rest*/
													if (Chunk.dwSize > DMUS_MAX_NAME_SIZE) {
490
														IStream_Read (pStm, This->dmobj.desc.wszName, DMUS_MAX_NAME_SIZE, NULL);
491 492 493
														liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
														IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													} else {
494
														IStream_Read (pStm, This->dmobj.desc.wszName, Chunk.dwSize, NULL);
495
													}
496 497
													This->dmobj.desc.dwValidData |= DMUS_OBJ_NAME;
													TRACE_(dmdump)(": name: %s\n", debugstr_w(This->dmobj.desc.wszName));
498 499 500
													break;
												}
												default: {
501
													TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
502 503 504 505 506
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;						
												}
											}
507
											TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
508 509 510 511 512 513 514 515
										} while (ListCount[0] < ListSize[0]);
										break;
									}
									case DMUS_FOURCC_CONTAINED_OBJECTS_LIST: {
										TRACE_(dmfile)(": contained objects list\n");
										do {
											IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
											ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
516
											TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
517 518 519 520 521 522 523 524
											switch (Chunk.fccID) {
												case FOURCC_LIST: {
													IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
													TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
													ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
													ListCount[1] = 0;
													switch (Chunk.fccID) {
														case DMUS_FOURCC_CONTAINED_OBJECT_LIST: {
525
															LPWINE_CONTAINER_ENTRY pNewEntry;
526
															TRACE_(dmfile)(": contained object list\n");
527 528
															pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_CONTAINER_ENTRY));
															DM_STRUCT_INIT(&pNewEntry->Desc);
529 530 531
															do {
																IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
																ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
532
																TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
533 534 535
																switch (Chunk.fccID) {
																	case DMUS_FOURCC_CONTAINED_ALIAS_CHUNK: {
																		TRACE_(dmfile)(": alias chunk\n");
536 537 538
																		pNewEntry->wszAlias = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
																		IStream_Read (pStm, pNewEntry->wszAlias, Chunk.dwSize, NULL);
																		TRACE_(dmdump)(": alias: %s\n", debugstr_w(pNewEntry->wszAlias));
539 540 541
																		break;
																	}
																	case DMUS_FOURCC_CONTAINED_OBJECT_CHUNK: {
542
																		DMUS_IO_CONTAINED_OBJECT_HEADER tmpObjectHeader;
543 544
																		TRACE_(dmfile)(": contained object header chunk\n");
																		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 550
																		/* store flags */
																		pNewEntry->dwFlags = tmpObjectHeader.dwFlags;
551 552 553 554 555 556 557 558 559 560 561 562
																		break;
																	}
																	/* now read data... it may be safe to read everything after object header chunk, 
																		but I'm not comfortable with MSDN's "the header is *normally* followed by ..." */
																	case FOURCC_LIST: {
																		IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
																		TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
																		ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
																		ListCount[2] = 0;
																		switch (Chunk.fccID) {
																			case DMUS_FOURCC_REF_LIST: {
																				TRACE_(dmfile)(": reference list\n");
563
																				pNewEntry->bIsRIFF = 0;
564 565 566
																				do {
																					IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
																					ListCount[2] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
567
																					TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
568 569
																					switch (Chunk.fccID) {
																						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 574
																							IStream_Read (pStm, &tmpReferenceHeader, Chunk.dwSize, NULL);
																							/* copy retrieved data to DMUS_OBJECTDESC */
575 576 577
																							if (!IsEqualCLSID (&pNewEntry->Desc.guidClass, &tmpReferenceHeader.guidClassID)) ERR(": object header declares different CLSID than reference header?\n");
																							/* it shouldn't be necessary to copy guidClass, since it was set in contained object header already...
																							   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 581 582 583 584
																							break;																	
																						}
																						case DMUS_FOURCC_GUID_CHUNK: {
																							TRACE_(dmfile)(": guid chunk\n");
																							/* no need to set flags since they were copied from reference header */
585
																							IStream_Read (pStm, &pNewEntry->Desc.guidObject, Chunk.dwSize, NULL);
586 587 588 589 590
																							break;
																						}
																						case DMUS_FOURCC_DATE_CHUNK: {
																							TRACE_(dmfile)(": file date chunk\n");
																							/* no need to set flags since they were copied from reference header */
591
																							IStream_Read (pStm, &pNewEntry->Desc.ftDate, Chunk.dwSize, NULL);
592 593 594 595 596
																							break;
																						}
																						case DMUS_FOURCC_NAME_CHUNK: {
																							TRACE_(dmfile)(": name chunk\n");
																							/* no need to set flags since they were copied from reference header */
597
																							IStream_Read (pStm, pNewEntry->Desc.wszName, Chunk.dwSize, NULL);
598 599 600 601 602
																							break;
																						}
																						case DMUS_FOURCC_FILE_CHUNK: {
																							TRACE_(dmfile)(": file name chunk\n");
																							/* no need to set flags since they were copied from reference header */
603
																							IStream_Read (pStm, pNewEntry->Desc.wszFileName, Chunk.dwSize, NULL);
604 605 606 607 608
																							break;
																						}
																						case DMUS_FOURCC_CATEGORY_CHUNK: {
																							TRACE_(dmfile)(": category chunk\n");
																							/* no need to set flags since they were copied from reference header */
609
																							IStream_Read (pStm, pNewEntry->Desc.wszCategory, Chunk.dwSize, NULL);
610 611 612 613 614
																							break;
																						}
																						case DMUS_FOURCC_VERSION_CHUNK: {
																							TRACE_(dmfile)(": version chunk\n");
																							/* no need to set flags since they were copied from reference header */
615
																							IStream_Read (pStm, &pNewEntry->Desc.vVersion, Chunk.dwSize, NULL);
616 617 618 619 620 621 622 623 624
																							break;
																						}
																						default: {
																							TRACE_(dmfile)(": unknown chunk (skipping)\n");
																							liMove.QuadPart = Chunk.dwSize;
																							IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip this chunk */
																							break;
																						}
																					}
625
																					TRACE_(dmfile)(": ListCount[2] = 0x%08X < ListSize[2] = 0x%08X\n", ListCount[2], ListSize[2]);
626 627 628 629 630 631 632 633 634 635
																				} while (ListCount[2] < ListSize[2]);
																				break;
																			}
																			default: {
																				TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
																				return E_FAIL;
																			}
																		}
																		break;
																	}
636
																	
637 638 639 640 641
																	case FOURCC_RIFF: {
																		IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
																		TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
																		if (IS_VALID_DMFORM (Chunk.fccID)) {
																			TRACE_(dmfile)(": valid DMUSIC form\n");
642 643
																			pNewEntry->bIsRIFF = 1;
																			/* we'll have to skip whole RIFF chunk after SetObject is called */
644 645 646 647 648 649 650 651
																			liMove.QuadPart = 0;
																			IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &uliPos);
																			uliPos.QuadPart += (Chunk.dwSize - sizeof(FOURCC)); /* set uliPos at the end of RIFF chunk */																			
																			/* move at the beginning of RIFF chunk */
																			liMove.QuadPart = 0;
																			liMove.QuadPart -= (sizeof(FOURCC)+sizeof(DWORD)+sizeof(FOURCC));
																			IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
																			/* put pointer to stream in descriptor */
652 653
																			pNewEntry->Desc.dwValidData |= DMUS_OBJ_STREAM;
																			pNewEntry->Desc.pStream = pStm; /* we don't have to worry about cloning, since SetObject will perform it */
654 655 656 657 658 659 660 661 662 663
																			/* wait till we get on the end of object list */
																		} else {
																			TRACE_(dmfile)(": invalid DMUSIC form (skipping)\n");
																			liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
																			IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
																			/* FIXME: should we return E_FAIL? */
																		}
																		break;
																	}
																	default: {
664
																		TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
665 666 667 668 669
																		liMove.QuadPart = Chunk.dwSize;
																		IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
																		break;						
																	}
																}
670
																TRACE_(dmfile)(": ListCount[1] = 0x%08X < ListSize[1] = 0x%08X\n", ListCount[1], ListSize[1]);
671
															} while (ListCount[1] < ListSize[1]);
672 673 674 675 676 677
															/* SetObject: this will fill descriptor with additional info	and add alias in loader's cache */
															IDirectMusicLoader_SetObject (pLoader, &pNewEntry->Desc);
															/* now that SetObject collected appropriate info into descriptor we can live happily ever after;
															   or not, since we have to clean evidence of loading through stream... *sigh*
															   and we have to skip rest of the chunk, if we loaded through RIFF */
															if (pNewEntry->bIsRIFF) {
678 679
																liMove.QuadPart = uliPos.QuadPart;
																IStream_Seek (pStm, liMove, STREAM_SEEK_SET, NULL);
680 681
																pNewEntry->Desc.dwValidData &= ~DMUS_OBJ_STREAM; /* clear flag (and with bitwise complement) */
																pNewEntry->Desc.pStream = NULL;											
682
															}
683 684
															/* add entry to list of objects */
															list_add_tail (This->pContainedObjects, &pNewEntry->entry);
685 686 687 688 689 690 691 692 693 694 695 696
															break;
														}
														default: {
															TRACE_(dmfile)(": unknown (skipping)\n");
															liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
															IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
															break;						
														}
													}
													break;
												}
												default: {
697
													TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
698 699 700 701 702
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;						
												}
											}
703
											TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
704 705 706 707 708 709 710 711 712 713 714 715 716
										} while (ListCount[0] < ListSize[0]);
										break;
									}									
									default: {
										TRACE_(dmfile)(": unknown (skipping)\n");
										liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
										IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
										break;						
									}
								}
								break;
							}	
							default: {
717
								TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
718 719 720 721 722
								liMove.QuadPart = Chunk.dwSize;
								IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
								break;						
							}
						}
723
						TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
724 725 726 727 728 729 730 731 732 733 734
					} while (StreamCount < StreamSize);
					break;
				}
				default: {
					TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
					liMove.QuadPart = StreamSize;
					IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
					return E_FAIL;
				}
			}
			TRACE_(dmfile)(": reading finished\n");
735
			This->dmobj.desc.dwValidData |= DMUS_OBJ_LOADED;
736 737 738 739 740 741 742 743 744
			break;
		}
		default: {
			TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
			liMove.QuadPart = Chunk.dwSize;
			IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
			return E_FAIL;
		}
	}
745 746 747 748 749 750
	
	/* now, if DMUS_CONTAINER_NOLOADS is not set, we are supposed to load contained objects;
	   so when we call GetObject later, they'll already be in cache */
	if (!(This->Header.dwFlags & DMUS_CONTAINER_NOLOADS)) {
		struct list *pEntry;
		LPWINE_CONTAINER_ENTRY pContainedObject;
751

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
		TRACE(": DMUS_CONTAINER_NOLOADS not set... load all objects\n");
		
		LIST_FOR_EACH (pEntry, This->pContainedObjects) {
			IDirectMusicObject* pObject;
			pContainedObject = LIST_ENTRY (pEntry, WINE_CONTAINER_ENTRY, entry);		
			/* get object from loader and then release it */
			if (SUCCEEDED(IDirectMusicLoader_GetObject (pLoader, &pContainedObject->Desc, &IID_IDirectMusicObject, (LPVOID*)&pObject))) {
				pContainedObject->pObject = pObject; /* for final release */
				IDirectMusicObject_Release (pObject); /* we don't really need this one */
			} else {
				WARN(": failed to load contained object\n");
				result = DMUS_S_PARTIALLOAD;
			}
		}
	}
767
	
768 769 770
	IDirectMusicLoader_Release (pLoader); /* release loader */

#if 0
771 772 773
	/* DEBUG: dumps whole container object tree: */
	if (TRACE_ON(dmloader)) {
		int r = 0;
774
		LPWINE_CONTAINER_ENTRY tmpEntry;
775 776 777 778
		struct list *listEntry;

		TRACE("*** IDirectMusicContainer (%p) ***\n", This->ContainerVtbl);
		TRACE(" - Objects:\n");
779 780
		LIST_FOR_EACH (listEntry, This->pContainedObjects) {
			tmpEntry = LIST_ENTRY( listEntry, WINE_CONTAINER_ENTRY, entry );
781 782
			TRACE("    - Object[%i]:\n", r);
			TRACE("       - wszAlias: %s\n", debugstr_w(tmpEntry->wszAlias));
783
			TRACE("       - Object descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC(&tmpEntry->Desc));
784 785 786 787
			r++;
		}
	}
#endif
788 789

	return result;
790 791
}

792 793 794 795
static const IPersistStreamVtbl persiststream_vtbl = {
    dmobj_IPersistStream_QueryInterface,
    dmobj_IPersistStream_AddRef,
    dmobj_IPersistStream_Release,
796
    dmobj_IPersistStream_GetClassID,
797
    unimpl_IPersistStream_IsDirty,
798
    IPersistStreamImpl_Load,
799 800
    unimpl_IPersistStream_Save,
    unimpl_IPersistStream_GetSizeMax
801
};
802 803

/* for ClassFactory */
804 805
HRESULT WINAPI create_dmcontainer(REFIID lpcGUID, void **ppobj)
{
806
	IDirectMusicContainerImpl* obj;
807
        HRESULT hr;
808

809 810
	obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicContainerImpl));
	if (NULL == obj) {
811
		*ppobj = NULL;
812 813
		return E_OUTOFMEMORY;
	}
814 815
        obj->IDirectMusicContainer_iface.lpVtbl = &dmcontainer_vtbl;
        obj->ref = 1;
816 817 818 819
        dmobject_init(&obj->dmobj, &CLSID_DirectMusicContainer,
                        (IUnknown*)&obj->IDirectMusicContainer_iface);
        obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
        obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
820 821 822
	obj->pContainedObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
	list_init (obj->pContainedObjects);

823 824
	lock_module();

825 826 827 828
        hr = IDirectMusicContainer_QueryInterface(&obj->IDirectMusicContainer_iface, lpcGUID, ppobj);
        IDirectMusicContainer_Release(&obj->IDirectMusicContainer_iface);

        return hr;
829
}