container.c 39.9 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 21
 */

#include "dmloader_private.h"

22
WINE_DEFAULT_DEBUG_CHANNEL(dmloader);
23
WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24 25 26 27 28
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)
29

30 31 32 33
static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_AddRef (LPDIRECTMUSICCONTAINER iface);
static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface);
static ULONG WINAPI IDirectMusicContainerImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface);

34 35 36
/*****************************************************************************
 * IDirectMusicContainerImpl implementation
 */
37
/* IUnknown/IDirectMusicContainer part: */
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

static HRESULT DMUSIC_DestroyDirectMusicContainerImpl (LPDIRECTMUSICCONTAINER iface) {
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);
	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 */
71
	unlock_module();
72 73 74 75

	return S_OK;
}

76
static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface (LPDIRECTMUSICCONTAINER iface, REFIID riid, LPVOID *ppobj) {
77
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);
78
	
79
	TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
80 81
	if (IsEqualIID (riid, &IID_IUnknown) ||
		IsEqualIID (riid, &IID_IDirectMusicContainer)) {
82
		*ppobj = &This->ContainerVtbl;
83 84 85
		IDirectMusicContainerImpl_IDirectMusicContainer_AddRef ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
		return S_OK;
	} else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
86
		*ppobj = &This->ObjectVtbl;
87 88 89
		IDirectMusicContainerImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);		
		return S_OK;
	} else if (IsEqualIID (riid, &IID_IPersistStream)) {
90
		*ppobj = &This->PersistStreamVtbl;
91
		IDirectMusicContainerImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);		
92 93
		return S_OK;
	}
94
	
95
	WARN(": not found\n");
96 97 98
	return E_NOINTERFACE;
}

99
static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_AddRef (LPDIRECTMUSICCONTAINER iface) {
100
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);
101
	TRACE("(%p): AddRef from %d\n", This, This->dwRef);
102
	return InterlockedIncrement (&This->dwRef);
103 104
}

105
static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_Release (LPDIRECTMUSICCONTAINER iface) {
106
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);
107 108
	
	DWORD dwRef = InterlockedDecrement (&This->dwRef);
109
	TRACE("(%p): ReleaseRef to %d\n", This, dwRef);
110 111 112 113 114 115
	if (dwRef == 0) {
		DMUSIC_DestroyDirectMusicContainerImpl (iface);
		HeapFree(GetProcessHeap(), 0, This);
	}
	
	return dwRef;
116
}
117

118
static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicContainer_EnumObject (LPDIRECTMUSICCONTAINER iface, REFGUID rguidClass, DWORD dwIndex, LPDMUS_OBJECTDESC pDesc, WCHAR* pwszAlias) {
119 120 121 122
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ContainerVtbl, iface);	
	struct list *pEntry;
	LPWINE_CONTAINER_ENTRY pContainedObject;
	DWORD dwCount = 0;
123

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

126 127 128 129 130
	if (!pDesc)
		return E_POINTER;
	if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
		ERR(": invalid pDesc->dwSize %d\n", pDesc->dwSize);
		return E_INVALIDARG;
131
	}
132

133
	DM_STRUCT_INIT(pDesc);
134

135 136 137 138 139 140 141
	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
142
					lstrcpynW (pwszAlias, pContainedObject->wszAlias, DMUS_MAX_FILENAME);
143 144 145
					if (strlenW (pContainedObject->wszAlias) > DMUS_MAX_FILENAME)
						result = DMUS_S_STRING_TRUNCATED;
				}
146
				*pDesc = pContainedObject->Desc;
147 148 149 150 151 152 153
				return result;
			}
			dwCount++;
		}
	}		
	
	TRACE(": not found\n");
154
	return S_FALSE;
155
}
156

157
static const IDirectMusicContainerVtbl DirectMusicContainer_Container_Vtbl = {
158 159 160 161 162
	IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface,
	IDirectMusicContainerImpl_IDirectMusicContainer_AddRef,
	IDirectMusicContainerImpl_IDirectMusicContainer_Release,
	IDirectMusicContainerImpl_IDirectMusicContainer_EnumObject
};
163

164
/* IDirectMusicObject part: */
165
static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
166
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
167
	return IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl, riid, ppobj);
168 169
}

170
static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
171
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
172
	return IDirectMusicContainerImpl_IDirectMusicContainer_AddRef ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
173 174
}

175
static ULONG WINAPI IDirectMusicContainerImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
176
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
177
	return IDirectMusicContainerImpl_IDirectMusicContainer_Release ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
178 179
}

180
static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
181
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
182 183
	TRACE("(%p, %p):\n", This, pDesc);
	
Austin English's avatar
Austin English committed
184
	/* check if we can write to whole pDesc */
185 186 187 188 189 190 191 192 193 194 195 196
	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;
	}
197

198
	DM_STRUCT_INIT(pDesc);
199 200
	*pDesc = This->Desc;

201 202 203
	return S_OK;
}

204
static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
205 206
	DWORD dwNewFlags = 0;
	DWORD dwFlagDifference;
207
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
208 209
	TRACE("(%p, %p):\n", This, pDesc);

Austin English's avatar
Austin English committed
210
	/* check if we can read whole pDesc */
211 212 213
	if (IsBadReadPtr (pDesc, sizeof(DWORD))) {
		ERR(": pDesc->dwSize bad read pointer\n");
		return E_POINTER;
214
	}
215 216 217 218 219 220 221
	if (pDesc->dwSize != sizeof(DMUS_OBJECTDESC)) {
		ERR(": invalid pDesc->dwSize\n");
		return E_INVALIDARG;
	}
	if (IsBadReadPtr (pDesc, sizeof(DMUS_OBJECTDESC))) {
		ERR(": pDesc bad read pointer\n");
		return E_POINTER;
222
	}
223

224
	if (pDesc->dwValidData & DMUS_OBJ_OBJECT) {
225
		This->Desc.guidObject = pDesc->guidObject;
226 227 228
		dwNewFlags |= DMUS_OBJ_OBJECT;
	}
	if (pDesc->dwValidData & DMUS_OBJ_NAME) {
Peter Berg Larsen's avatar
Peter Berg Larsen committed
229
		lstrcpynW (This->Desc.wszName, pDesc->wszName, DMUS_MAX_NAME);
230 231 232
		dwNewFlags |= DMUS_OBJ_NAME;
	}
	if (pDesc->dwValidData & DMUS_OBJ_CATEGORY) {
Peter Berg Larsen's avatar
Peter Berg Larsen committed
233
		lstrcpynW (This->Desc.wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
234 235 236
		dwNewFlags |= DMUS_OBJ_CATEGORY;
	}
	if (pDesc->dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH)) {
Peter Berg Larsen's avatar
Peter Berg Larsen committed
237
		lstrcpynW (This->Desc.wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		dwNewFlags |= (pDesc->dwValidData & (DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH));
	}
	if (pDesc->dwValidData & DMUS_OBJ_VERSION) {
		This->Desc.vVersion.dwVersionLS = pDesc->vVersion.dwVersionLS;
		This->Desc.vVersion.dwVersionMS = pDesc->vVersion.dwVersionMS;
		dwNewFlags |= DMUS_OBJ_VERSION;
	}
	if (pDesc->dwValidData & DMUS_OBJ_DATE) {
		This->Desc.ftDate.dwHighDateTime = pDesc->ftDate.dwHighDateTime;
		This->Desc.ftDate.dwLowDateTime = pDesc->ftDate.dwLowDateTime;
		dwNewFlags |= DMUS_OBJ_DATE;
	}
	/* set new flags */
	This->Desc.dwValidData |= dwNewFlags;
	
	dwFlagDifference = pDesc->dwValidData - dwNewFlags;
	if (dwFlagDifference) {
		pDesc->dwValidData &= ~dwFlagDifference; /* and with bitwise complement */
		return S_FALSE;
	} else return S_OK;
258 259
}

260
static HRESULT WINAPI IDirectMusicContainerImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
261
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, ObjectVtbl, iface);
262
	WINE_CHUNK Chunk;
263 264
	DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
	LARGE_INTEGER liMove; /* used when skipping chunks */
265

266
	TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
267
	
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	/* 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);
288
	
289
	/* here we go... */
290
	IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
291
	TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
292 293 294 295 296 297 298 299
	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");
300 301
				/* set guidClass */
				pDesc->dwValidData |= DMUS_OBJ_CLASS;
302
				pDesc->guidClass = CLSID_DirectMusicContainer;
303 304 305
				do {
					IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
					StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
306
					TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
307 308 309 310 311
					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);
312
							TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&pDesc->guidObject));
313 314 315 316 317 318
							break;
						}
						case DMUS_FOURCC_VERSION_CHUNK: {
							TRACE_(dmfile)(": version chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_VERSION;
							IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
319
							TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&pDesc->vVersion));
320 321
							break;
						}
322 323 324 325 326 327 328
						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;
						}								
329 330
						case DMUS_FOURCC_CATEGORY_CHUNK: {
							TRACE_(dmfile)(": category chunk\n");
331 332 333 334 335 336 337 338 339
							/* 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);
							}
340
							pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
341
							TRACE_(dmdump)(": category: %s\n", debugstr_w(pDesc->wszCategory));							
342 343 344 345 346 347 348 349 350 351 352 353 354 355
							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;
356
										TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
357 358 359 360 361 362
										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");
363 364 365 366 367 368 369 370 371
												/* 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);
												}
372
												pDesc->dwValidData |= DMUS_OBJ_NAME;
373
												TRACE_(dmdump)(": name: %s\n", debugstr_w(pDesc->wszName));
374 375 376
												break;
											}
											default: {
377
												TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
378 379 380 381 382
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;						
											}
										}
383
										TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
384 385 386 387 388 389 390 391 392 393 394 395 396
									} 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: {
397
							TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
398 399 400 401 402
							liMove.QuadPart = Chunk.dwSize;
							IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
							break;						
						}
					}
403
					TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
				} 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;
		}
	}	
422
	
423
	TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC(pDesc));
424
	return S_OK;	
425 426
}

427
static const IDirectMusicObjectVtbl DirectMusicContainer_Object_Vtbl = {
428 429 430 431 432 433 434 435
	IDirectMusicContainerImpl_IDirectMusicObject_QueryInterface,
	IDirectMusicContainerImpl_IDirectMusicObject_AddRef,
	IDirectMusicContainerImpl_IDirectMusicObject_Release,
	IDirectMusicContainerImpl_IDirectMusicObject_GetDescriptor,
	IDirectMusicContainerImpl_IDirectMusicObject_SetDescriptor,
	IDirectMusicContainerImpl_IDirectMusicObject_ParseDescriptor
};

436
/* IPersistStream part: */
437
static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
438
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
439
	return IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl, riid, ppobj);
440 441
}

442
static ULONG WINAPI IDirectMusicContainerImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
443
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
444
	return IDirectMusicContainerImpl_IDirectMusicContainer_AddRef ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
445 446
}

447
static ULONG WINAPI IDirectMusicContainerImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
448
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
449
	return IDirectMusicContainerImpl_IDirectMusicContainer_Release ((LPDIRECTMUSICCONTAINER)&This->ContainerVtbl);
450 451
}

452
static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
453 454 455 456 457 458 459 460 461
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
	
	TRACE("(%p, %p)\n", This, pClassID);
	if (IsBadWritePtr (pClassID, sizeof(CLSID))) {
		ERR(": pClassID bad write pointer\n");
		return E_POINTER;
	}
	
	*pClassID = CLSID_DirectMusicContainer;
462
	return S_OK;
463 464
}

465
static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
466
	/* FIXME: is implemented (somehow) */
467 468 469
	return E_NOTIMPL;
}

470
static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
471
	ICOM_THIS_MULTI(IDirectMusicContainerImpl, PersistStreamVtbl, iface);
472
	WINE_CHUNK Chunk;
473 474 475 476 477
	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;
478 479 480
	HRESULT result = S_OK;

	TRACE("(%p, %p):\n", This, pStm);
481
	
482 483 484 485 486
	/* 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
487
	/* if stream is already set, this means the container is already loaded */
488 489 490 491 492
	if (This->pStream) {
		TRACE(": stream is already set, which means container is already loaded\n");
		return DMUS_E_ALREADY_LOADED;
	}

493
	/* get loader since it will be needed later */
494 495 496 497
	if (FAILED(IStream_QueryInterface (pStm, &IID_IDirectMusicGetLoader, (LPVOID*)&pGetLoader))) {
		ERR(": stream not supported\n");
		return DMUS_E_UNSUPPORTED_STREAM;
	}
498 499 500
	IDirectMusicGetLoader_GetLoader (pGetLoader, &pLoader);
	IDirectMusicGetLoader_Release (pGetLoader);
	
501
	This->pStream = pStm;
502
	IStream_AddRef (pStm); /* add count for later references */
503 504
	
	/* start with load */
505
	IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
506
	TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
507 508 509 510 511 512 513 514 515
	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");
516
					This->Desc.guidClass = CLSID_DirectMusicContainer;
517
					This->Desc.dwValidData |= DMUS_OBJ_CLASS;
518 519 520
					do {
						IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
						StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
521
						TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
522 523 524
						switch (Chunk.fccID) {
							case DMUS_FOURCC_CONTAINER_CHUNK: {
								TRACE_(dmfile)(": container header chunk\n");
525
								IStream_Read (pStm, &This->Header, Chunk.dwSize, NULL);
526
								TRACE_(dmdump)(": container header chunk:\n%s\n", debugstr_DMUS_IO_CONTAINER_HEADER(&This->Header));
527 528 529 530
								break;	
							}
							case DMUS_FOURCC_GUID_CHUNK: {
								TRACE_(dmfile)(": GUID chunk\n");
531 532 533
								IStream_Read (pStm, &This->Desc.guidObject, Chunk.dwSize, NULL);
								This->Desc.dwValidData |= DMUS_OBJ_OBJECT;
								TRACE_(dmdump)(": GUID: %s\n", debugstr_guid(&This->Desc.guidObject));
534 535 536 537
								break;
							}
							case DMUS_FOURCC_VERSION_CHUNK: {
								TRACE_(dmfile)(": version chunk\n");
538 539 540
								IStream_Read (pStm, &This->Desc.vVersion, Chunk.dwSize, NULL);
								This->Desc.dwValidData |= DMUS_OBJ_VERSION;
								TRACE_(dmdump)(": version: %s\n", debugstr_dmversion(&This->Desc.vVersion));
541 542
								break;
							}
543 544 545 546 547 548 549
							case DMUS_FOURCC_DATE_CHUNK: {
								TRACE_(dmfile)(": date chunk\n");
								IStream_Read (pStm, &This->Desc.ftDate, Chunk.dwSize, NULL);
								This->Desc.dwValidData |= DMUS_OBJ_DATE;
								TRACE_(dmdump)(": date: %s\n", debugstr_filetime(&This->Desc.ftDate));
								break;
							}							
550 551
							case DMUS_FOURCC_CATEGORY_CHUNK: {
								TRACE_(dmfile)(": category chunk\n");
552 553 554 555 556 557 558 559 560 561 562
								/* 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 (pStm, This->Desc.wszCategory, DMUS_MAX_CATEGORY_SIZE, NULL);
									liMove.QuadPart = Chunk.dwSize - DMUS_MAX_CATEGORY_SIZE;
									IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
								} else {
									IStream_Read (pStm, This->Desc.wszCategory, Chunk.dwSize, NULL);
								}
								This->Desc.dwValidData |= DMUS_OBJ_CATEGORY;
								TRACE_(dmdump)(": category: %s\n", debugstr_w(This->Desc.wszCategory));								
563 564 565 566 567 568 569 570 571 572 573 574 575
								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;
576
											TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
577 578 579 580 581 582
											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");
583 584 585 586 587 588 589 590 591 592 593
													/* 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 (pStm, This->Desc.wszName, DMUS_MAX_NAME_SIZE, NULL);
														liMove.QuadPart = Chunk.dwSize - DMUS_MAX_NAME_SIZE;
														IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													} else {
														IStream_Read (pStm, This->Desc.wszName, Chunk.dwSize, NULL);
													}
													This->Desc.dwValidData |= DMUS_OBJ_NAME;
													TRACE_(dmdump)(": name: %s\n", debugstr_w(This->Desc.wszName));
594 595 596
													break;
												}
												default: {
597
													TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
598 599 600 601 602
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;						
												}
											}
603
											TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
604 605 606 607 608 609 610 611
										} 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;
612
											TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
613 614 615 616 617 618 619 620
											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: {
621
															LPWINE_CONTAINER_ENTRY pNewEntry;
622
															TRACE_(dmfile)(": contained object list\n");
623 624
															pNewEntry = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(WINE_CONTAINER_ENTRY));
															DM_STRUCT_INIT(&pNewEntry->Desc);
625 626 627
															do {
																IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
																ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
628
																TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
629 630 631
																switch (Chunk.fccID) {
																	case DMUS_FOURCC_CONTAINED_ALIAS_CHUNK: {
																		TRACE_(dmfile)(": alias chunk\n");
632 633 634
																		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));
635 636 637
																		break;
																	}
																	case DMUS_FOURCC_CONTAINED_OBJECT_CHUNK: {
638
																		DMUS_IO_CONTAINED_OBJECT_HEADER tmpObjectHeader;
639 640
																		TRACE_(dmfile)(": contained object header chunk\n");
																		IStream_Read (pStm, &tmpObjectHeader, Chunk.dwSize, NULL);
641
																		TRACE_(dmdump)(": contained object header:\n%s\n", debugstr_DMUS_IO_CONTAINED_OBJECT_HEADER(&tmpObjectHeader));
642
																		/* copy guidClass */
643
																		pNewEntry->Desc.dwValidData |= DMUS_OBJ_CLASS;
644
																		pNewEntry->Desc.guidClass = tmpObjectHeader.guidClassID;
645 646
																		/* store flags */
																		pNewEntry->dwFlags = tmpObjectHeader.dwFlags;
647 648 649 650 651 652 653 654 655 656 657 658
																		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");
659
																				pNewEntry->bIsRIFF = 0;
660 661 662
																				do {
																					IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
																					ListCount[2] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
663
																					TRACE_(dmfile)(": %s chunk (size = 0x%08X)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
664 665
																					switch (Chunk.fccID) {
																						case DMUS_FOURCC_REF_CHUNK: {
666
																							DMUS_IO_REFERENCE tmpReferenceHeader; /* temporary structure */
667
																							TRACE_(dmfile)(": reference header chunk\n");
668
																							memset (&tmpReferenceHeader, 0, sizeof(DMUS_IO_REFERENCE));
669 670
																							IStream_Read (pStm, &tmpReferenceHeader, Chunk.dwSize, NULL);
																							/* copy retrieved data to DMUS_OBJECTDESC */
671 672 673
																							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 */
674
																							pNewEntry->Desc.guidClass = tmpReferenceHeader.guidClassID;
675
																							pNewEntry->Desc.dwValidData |= tmpReferenceHeader.dwValidData;
676 677 678 679 680
																							break;																	
																						}
																						case DMUS_FOURCC_GUID_CHUNK: {
																							TRACE_(dmfile)(": guid chunk\n");
																							/* no need to set flags since they were copied from reference header */
681
																							IStream_Read (pStm, &pNewEntry->Desc.guidObject, Chunk.dwSize, NULL);
682 683 684 685 686
																							break;
																						}
																						case DMUS_FOURCC_DATE_CHUNK: {
																							TRACE_(dmfile)(": file date chunk\n");
																							/* no need to set flags since they were copied from reference header */
687
																							IStream_Read (pStm, &pNewEntry->Desc.ftDate, Chunk.dwSize, NULL);
688 689 690 691 692
																							break;
																						}
																						case DMUS_FOURCC_NAME_CHUNK: {
																							TRACE_(dmfile)(": name chunk\n");
																							/* no need to set flags since they were copied from reference header */
693
																							IStream_Read (pStm, pNewEntry->Desc.wszName, Chunk.dwSize, NULL);
694 695 696 697 698
																							break;
																						}
																						case DMUS_FOURCC_FILE_CHUNK: {
																							TRACE_(dmfile)(": file name chunk\n");
																							/* no need to set flags since they were copied from reference header */
699
																							IStream_Read (pStm, pNewEntry->Desc.wszFileName, Chunk.dwSize, NULL);
700 701 702 703 704
																							break;
																						}
																						case DMUS_FOURCC_CATEGORY_CHUNK: {
																							TRACE_(dmfile)(": category chunk\n");
																							/* no need to set flags since they were copied from reference header */
705
																							IStream_Read (pStm, pNewEntry->Desc.wszCategory, Chunk.dwSize, NULL);
706 707 708 709 710
																							break;
																						}
																						case DMUS_FOURCC_VERSION_CHUNK: {
																							TRACE_(dmfile)(": version chunk\n");
																							/* no need to set flags since they were copied from reference header */
711
																							IStream_Read (pStm, &pNewEntry->Desc.vVersion, Chunk.dwSize, NULL);
712 713 714 715 716 717 718 719 720
																							break;
																						}
																						default: {
																							TRACE_(dmfile)(": unknown chunk (skipping)\n");
																							liMove.QuadPart = Chunk.dwSize;
																							IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip this chunk */
																							break;
																						}
																					}
721
																					TRACE_(dmfile)(": ListCount[2] = 0x%08X < ListSize[2] = 0x%08X\n", ListCount[2], ListSize[2]);
722 723 724 725 726 727 728 729 730 731
																				} while (ListCount[2] < ListSize[2]);
																				break;
																			}
																			default: {
																				TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
																				return E_FAIL;
																			}
																		}
																		break;
																	}
732
																	
733 734 735 736 737
																	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");
738 739
																			pNewEntry->bIsRIFF = 1;
																			/* we'll have to skip whole RIFF chunk after SetObject is called */
740 741 742 743 744 745 746 747
																			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 */
748 749
																			pNewEntry->Desc.dwValidData |= DMUS_OBJ_STREAM;
																			pNewEntry->Desc.pStream = pStm; /* we don't have to worry about cloning, since SetObject will perform it */
750 751 752 753 754 755 756 757 758 759
																			/* 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: {
760
																		TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
761 762 763 764 765
																		liMove.QuadPart = Chunk.dwSize;
																		IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
																		break;						
																	}
																}
766
																TRACE_(dmfile)(": ListCount[1] = 0x%08X < ListSize[1] = 0x%08X\n", ListCount[1], ListSize[1]);
767
															} while (ListCount[1] < ListSize[1]);
768 769 770 771 772 773
															/* 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) {
774 775
																liMove.QuadPart = uliPos.QuadPart;
																IStream_Seek (pStm, liMove, STREAM_SEEK_SET, NULL);
776 777
																pNewEntry->Desc.dwValidData &= ~DMUS_OBJ_STREAM; /* clear flag (and with bitwise complement) */
																pNewEntry->Desc.pStream = NULL;											
778
															}
779 780
															/* add entry to list of objects */
															list_add_tail (This->pContainedObjects, &pNewEntry->entry);
781 782 783 784 785 786 787 788 789 790 791 792
															break;
														}
														default: {
															TRACE_(dmfile)(": unknown (skipping)\n");
															liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
															IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
															break;						
														}
													}
													break;
												}
												default: {
793
													TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
794 795 796 797 798
													liMove.QuadPart = Chunk.dwSize;
													IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
													break;						
												}
											}
799
											TRACE_(dmfile)(": ListCount[0] = 0x%08X < ListSize[0] = 0x%08X\n", ListCount[0], ListSize[0]);
800 801 802 803 804 805 806 807 808 809 810 811 812
										} 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: {
813
								TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
814 815 816 817 818
								liMove.QuadPart = Chunk.dwSize;
								IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
								break;						
							}
						}
819
						TRACE_(dmfile)(": StreamCount[0] = 0x%08X < StreamSize[0] = 0x%08X\n", StreamCount, StreamSize);
820 821 822 823 824 825 826 827 828 829 830
					} 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");
831
			This->Desc.dwValidData |= DMUS_OBJ_LOADED;
832 833 834 835 836 837 838 839 840
			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;
		}
	}
841 842 843 844 845 846
	
	/* 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;
847

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
		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;
			}
		}
	}
863
	
864 865 866
	IDirectMusicLoader_Release (pLoader); /* release loader */

#if 0
867 868 869
	/* DEBUG: dumps whole container object tree: */
	if (TRACE_ON(dmloader)) {
		int r = 0;
870
		LPWINE_CONTAINER_ENTRY tmpEntry;
871 872 873 874
		struct list *listEntry;

		TRACE("*** IDirectMusicContainer (%p) ***\n", This->ContainerVtbl);
		TRACE(" - Objects:\n");
875 876
		LIST_FOR_EACH (listEntry, This->pContainedObjects) {
			tmpEntry = LIST_ENTRY( listEntry, WINE_CONTAINER_ENTRY, entry );
877 878
			TRACE("    - Object[%i]:\n", r);
			TRACE("       - wszAlias: %s\n", debugstr_w(tmpEntry->wszAlias));
879
			TRACE("       - Object descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC(&tmpEntry->Desc));
880 881 882 883
			r++;
		}
	}
#endif
884 885

	return result;
886 887
}

888
static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
889
	ERR(": should not be needed\n");
890 891 892
	return E_NOTIMPL;
}

893
static HRESULT WINAPI IDirectMusicContainerImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
894
	ERR(": should not be needed\n");
895 896 897
	return E_NOTIMPL;
}

898
static const IPersistStreamVtbl DirectMusicContainer_PersistStream_Vtbl = {
899 900 901 902 903 904 905 906
	IDirectMusicContainerImpl_IPersistStream_QueryInterface,
	IDirectMusicContainerImpl_IPersistStream_AddRef,
	IDirectMusicContainerImpl_IPersistStream_Release,
	IDirectMusicContainerImpl_IPersistStream_GetClassID,
	IDirectMusicContainerImpl_IPersistStream_IsDirty,
	IDirectMusicContainerImpl_IPersistStream_Load,
	IDirectMusicContainerImpl_IPersistStream_Save,
	IDirectMusicContainerImpl_IPersistStream_GetSizeMax
907
};
908 909 910 911

/* for ClassFactory */
HRESULT WINAPI DMUSIC_CreateDirectMusicContainerImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
	IDirectMusicContainerImpl* obj;
912

913 914
	obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicContainerImpl));
	if (NULL == obj) {
915
		*ppobj = NULL;
916 917 918 919 920
		return E_OUTOFMEMORY;
	}
	obj->ContainerVtbl = &DirectMusicContainer_Container_Vtbl;
	obj->ObjectVtbl = &DirectMusicContainer_Object_Vtbl;
	obj->PersistStreamVtbl = &DirectMusicContainer_PersistStream_Vtbl;
921 922 923 924
	obj->dwRef = 0; /* will be inited by QueryInterface */
	obj->pContainedObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list));
	list_init (obj->pContainedObjects);

925 926
	lock_module();

927 928
	return IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface ((LPDIRECTMUSICCONTAINER)&obj->ContainerVtbl, lpcGUID, ppobj);
}