ntoskrnl.exe/tests: Fix path buffer allocation size.
[wine.git] / dlls / dmband / band.c
blob4338791971b8c9c6e3420a866c17dec855a3a1fa
1 /* IDirectMusicBand Implementation
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 "dmband_private.h"
21 #include "dmobject.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmband);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
27 /*****************************************************************************
28 * IDirectMusicBandImpl implementation
30 typedef struct IDirectMusicBandImpl {
31 IDirectMusicBand IDirectMusicBand_iface;
32 struct dmobject dmobj;
33 LONG ref;
34 struct list Instruments;
35 } IDirectMusicBandImpl;
37 static inline IDirectMusicBandImpl *impl_from_IDirectMusicBand(IDirectMusicBand *iface)
39 return CONTAINING_RECORD(iface, IDirectMusicBandImpl, IDirectMusicBand_iface);
42 /* DirectMusicBandImpl IDirectMusicBand part: */
43 static HRESULT WINAPI IDirectMusicBandImpl_QueryInterface(IDirectMusicBand *iface, REFIID riid,
44 void **ret_iface)
46 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
48 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
50 *ret_iface = NULL;
52 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDirectMusicBand))
53 *ret_iface = iface;
54 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
55 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
56 else if (IsEqualIID(riid, &IID_IPersistStream))
57 *ret_iface = &This->dmobj.IPersistStream_iface;
58 else {
59 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
60 return E_NOINTERFACE;
63 IDirectMusicBand_AddRef((IUnknown*)*ret_iface);
64 return S_OK;
67 static ULONG WINAPI IDirectMusicBandImpl_AddRef(IDirectMusicBand *iface)
69 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
70 LONG ref = InterlockedIncrement(&This->ref);
72 TRACE("(%p) ref=%d\n", This, ref);
74 return ref;
77 static ULONG WINAPI IDirectMusicBandImpl_Release(IDirectMusicBand *iface)
79 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
80 LONG ref = InterlockedDecrement(&This->ref);
82 TRACE("(%p) ref=%d\n", This, ref);
84 if (!ref) {
85 HeapFree(GetProcessHeap(), 0, This);
86 DMBAND_UnlockModule();
89 return ref;
92 static HRESULT WINAPI IDirectMusicBandImpl_CreateSegment(IDirectMusicBand *iface,
93 IDirectMusicSegment **ppSegment)
95 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
96 FIXME("(%p, %p): stub\n", This, ppSegment);
97 return S_OK;
100 static HRESULT WINAPI IDirectMusicBandImpl_Download(IDirectMusicBand *iface,
101 IDirectMusicPerformance *pPerformance)
103 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
104 FIXME("(%p, %p): stub\n", This, pPerformance);
105 return S_OK;
108 static HRESULT WINAPI IDirectMusicBandImpl_Unload(IDirectMusicBand *iface,
109 IDirectMusicPerformance *pPerformance)
111 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
112 FIXME("(%p, %p): stub\n", This, pPerformance);
113 return S_OK;
116 static const IDirectMusicBandVtbl dmband_vtbl = {
117 IDirectMusicBandImpl_QueryInterface,
118 IDirectMusicBandImpl_AddRef,
119 IDirectMusicBandImpl_Release,
120 IDirectMusicBandImpl_CreateSegment,
121 IDirectMusicBandImpl_Download,
122 IDirectMusicBandImpl_Unload
125 /* IDirectMusicBandImpl IDirectMusicObject part: */
126 static HRESULT WINAPI band_IDirectMusicObject_ParseDescriptor(IDirectMusicObject *iface,
127 IStream *stream, DMUS_OBJECTDESC *desc)
129 struct chunk_entry riff = {0};
130 STATSTG stat;
131 HRESULT hr;
133 TRACE("(%p, %p, %p)\n", iface, stream, desc);
135 if (!stream || !desc)
136 return E_POINTER;
138 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
139 return hr;
140 if (riff.id != FOURCC_RIFF || riff.type != DMUS_FOURCC_BAND_FORM) {
141 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
142 stream_skip_chunk(stream, &riff);
143 return DMUS_E_INVALID_BAND;
146 hr = dmobj_parsedescriptor(stream, &riff, desc,
147 DMUS_OBJ_OBJECT|DMUS_OBJ_NAME|DMUS_OBJ_NAME_INAM|DMUS_OBJ_CATEGORY|DMUS_OBJ_VERSION);
148 if (FAILED(hr))
149 return hr;
151 desc->guidClass = CLSID_DirectMusicBand;
152 desc->dwValidData |= DMUS_OBJ_CLASS;
154 if (desc->dwValidData & DMUS_OBJ_CATEGORY) {
155 IStream_Stat(stream, &stat, STATFLAG_NONAME);
156 desc->ftDate = stat.mtime;
157 desc->dwValidData |= DMUS_OBJ_DATE;
160 TRACE("returning descriptor:\n");
161 debug_DMUS_OBJECTDESC(desc);
162 return S_OK;
165 static const IDirectMusicObjectVtbl dmobject_vtbl = {
166 dmobj_IDirectMusicObject_QueryInterface,
167 dmobj_IDirectMusicObject_AddRef,
168 dmobj_IDirectMusicObject_Release,
169 dmobj_IDirectMusicObject_GetDescriptor,
170 dmobj_IDirectMusicObject_SetDescriptor,
171 band_IDirectMusicObject_ParseDescriptor
174 /* IDirectMusicBandImpl IPersistStream part: */
175 static HRESULT parse_instrument(IDirectMusicBandImpl *This, DMUS_PRIVATE_CHUNK *pChunk,
176 IStream *pStm)
178 DMUS_PRIVATE_CHUNK Chunk;
179 DWORD ListSize[3], ListCount[3];
180 LARGE_INTEGER liMove; /* used when skipping chunks */
181 HRESULT hr;
183 DMUS_IO_INSTRUMENT inst;
184 LPDMUS_PRIVATE_INSTRUMENT pNewInstrument;
185 IDirectMusicObject* pObject = NULL;
187 if (pChunk->fccID != DMUS_FOURCC_INSTRUMENT_LIST) {
188 ERR_(dmfile)(": %s chunk should be an INSTRUMENT list\n", debugstr_fourcc (pChunk->fccID));
189 return E_FAIL;
192 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
193 ListCount[0] = 0;
195 do {
196 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
197 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
198 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
199 switch (Chunk.fccID) {
200 case DMUS_FOURCC_INSTRUMENT_CHUNK: {
201 TRACE_(dmfile)(": Instrument chunk\n");
202 if (Chunk.dwSize != sizeof(DMUS_IO_INSTRUMENT)) return E_FAIL;
203 IStream_Read (pStm, &inst, sizeof(DMUS_IO_INSTRUMENT), NULL);
204 TRACE_(dmfile)(" - dwPatch: %u\n", inst.dwPatch);
205 TRACE_(dmfile)(" - dwAssignPatch: %u\n", inst.dwAssignPatch);
206 TRACE_(dmfile)(" - dwNoteRanges[0]: %u\n", inst.dwNoteRanges[0]);
207 TRACE_(dmfile)(" - dwNoteRanges[1]: %u\n", inst.dwNoteRanges[1]);
208 TRACE_(dmfile)(" - dwNoteRanges[2]: %u\n", inst.dwNoteRanges[2]);
209 TRACE_(dmfile)(" - dwNoteRanges[3]: %u\n", inst.dwNoteRanges[3]);
210 TRACE_(dmfile)(" - dwPChannel: %u\n", inst.dwPChannel);
211 TRACE_(dmfile)(" - dwFlags: %x\n", inst.dwFlags);
212 TRACE_(dmfile)(" - bPan: %u\n", inst.bPan);
213 TRACE_(dmfile)(" - bVolume: %u\n", inst.bVolume);
214 TRACE_(dmfile)(" - nTranspose: %d\n", inst.nTranspose);
215 TRACE_(dmfile)(" - dwChannelPriority: %u\n", inst.dwChannelPriority);
216 TRACE_(dmfile)(" - nPitchBendRange: %d\n", inst.nPitchBendRange);
217 break;
219 case FOURCC_LIST: {
220 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
221 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
222 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
223 ListCount[1] = 0;
224 switch (Chunk.fccID) {
225 case DMUS_FOURCC_REF_LIST: {
226 FIXME_(dmfile)(": DMRF (DM References) list\n");
227 hr = IDirectMusicUtils_IPersistStream_ParseReference(&This->dmobj.IPersistStream_iface,
228 &Chunk, pStm, &pObject);
229 if (FAILED(hr)) {
230 ERR(": could not load Reference\n");
231 return hr;
233 break;
235 default: {
236 TRACE_(dmfile)(": unknown (skipping)\n");
237 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
238 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
239 break;
242 break;
244 default: {
245 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
246 liMove.QuadPart = Chunk.dwSize;
247 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
248 break;
251 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
252 } while (ListCount[0] < ListSize[0]);
255 * @TODO insert pNewInstrument into This
257 pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENT));
258 if (NULL == pNewInstrument) {
259 ERR(": no more memory\n");
260 return E_OUTOFMEMORY;
262 memcpy(&pNewInstrument->pInstrument, &inst, sizeof(DMUS_IO_INSTRUMENT));
263 pNewInstrument->ppReferenceCollection = NULL;
264 if (NULL != pObject) {
265 IDirectMusicCollection* pCol = NULL;
266 hr = IDirectMusicObject_QueryInterface (pObject, &IID_IDirectMusicCollection, (void**) &pCol);
267 if (FAILED(hr)) {
268 ERR(": failed to get IDirectMusicCollection Interface from DMObject\n");
269 HeapFree(GetProcessHeap(), 0, pNewInstrument);
271 return hr;
273 pNewInstrument->ppReferenceCollection = pCol;
274 IDirectMusicObject_Release(pObject);
276 list_add_tail (&This->Instruments, &pNewInstrument->entry);
278 return S_OK;
281 static HRESULT parse_instruments_list(IDirectMusicBandImpl *This, DMUS_PRIVATE_CHUNK *pChunk,
282 IStream *pStm)
284 HRESULT hr;
285 DMUS_PRIVATE_CHUNK Chunk;
286 DWORD ListSize[3], ListCount[3];
287 LARGE_INTEGER liMove; /* used when skipping chunks */
289 if (pChunk->fccID != DMUS_FOURCC_INSTRUMENTS_LIST) {
290 ERR_(dmfile)(": %s chunk should be an INSTRUMENTS list\n", debugstr_fourcc (pChunk->fccID));
291 return E_FAIL;
294 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
295 ListCount[0] = 0;
297 do {
298 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
299 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
300 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
301 switch (Chunk.fccID) {
302 case FOURCC_LIST: {
303 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
304 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
305 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
306 ListCount[1] = 0;
307 switch (Chunk.fccID) {
308 case DMUS_FOURCC_INSTRUMENT_LIST: {
309 TRACE_(dmfile)(": Instrument list\n");
310 hr = parse_instrument(This, &Chunk, pStm);
311 if (FAILED(hr)) return hr;
312 break;
314 default: {
315 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
316 liMove.QuadPart = ListSize[1];
317 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
318 break;
321 break;
323 default: {
324 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
325 liMove.QuadPart = Chunk.dwSize;
326 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
327 break;
330 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
331 } while (ListCount[0] < ListSize[0]);
333 return S_OK;
336 static HRESULT parse_band_form(IDirectMusicBandImpl *This, DMUS_PRIVATE_CHUNK *pChunk,
337 IStream *pStm)
339 HRESULT hr = E_FAIL;
340 DMUS_PRIVATE_CHUNK Chunk;
341 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
342 LARGE_INTEGER liMove; /* used when skipping chunks */
344 GUID tmp_guid;
346 if (pChunk->fccID != DMUS_FOURCC_BAND_FORM) {
347 ERR_(dmfile)(": %s chunk should be a BAND form\n", debugstr_fourcc (pChunk->fccID));
348 return E_FAIL;
351 StreamSize = pChunk->dwSize - sizeof(FOURCC);
352 StreamCount = 0;
354 do {
355 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
356 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
357 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
359 hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, &This->dmobj.desc);
360 if (FAILED(hr)) return hr;
362 if (hr == S_FALSE) {
363 switch (Chunk.fccID) {
364 case DMUS_FOURCC_GUID_CHUNK: {
365 TRACE_(dmfile)(": GUID\n");
366 IStream_Read (pStm, &tmp_guid, sizeof(GUID), NULL);
367 TRACE_(dmfile)(" - guid: %s\n", debugstr_dmguid(&tmp_guid));
368 break;
370 case FOURCC_LIST: {
371 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
372 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
373 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
374 ListCount[0] = 0;
375 switch (Chunk.fccID) {
376 case DMUS_FOURCC_UNFO_LIST: {
377 TRACE_(dmfile)(": UNFO list\n");
378 do {
379 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
380 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
381 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
383 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &This->dmobj.desc);
384 if (FAILED(hr)) return hr;
386 if (hr == S_FALSE) {
387 switch (Chunk.fccID) {
388 default: {
389 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
390 liMove.QuadPart = Chunk.dwSize;
391 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
392 break;
396 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
397 } while (ListCount[0] < ListSize[0]);
398 break;
400 case DMUS_FOURCC_INSTRUMENTS_LIST: {
401 TRACE_(dmfile)(": INSTRUMENTS list\n");
402 hr = parse_instruments_list(This, &Chunk, pStm);
403 if (FAILED(hr)) return hr;
404 break;
406 default: {
407 TRACE_(dmfile)(": unknown (skipping)\n");
408 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
409 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
410 break;
413 break;
415 default: {
416 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
417 liMove.QuadPart = Chunk.dwSize;
418 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
419 break;
423 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
424 } while (StreamCount < StreamSize);
426 return S_OK;
429 static inline IDirectMusicBandImpl *impl_from_IPersistStream(IPersistStream *iface)
431 return CONTAINING_RECORD(iface, IDirectMusicBandImpl, dmobj.IPersistStream_iface);
434 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
436 IDirectMusicBandImpl *This = impl_from_IPersistStream(iface);
437 DMUS_PRIVATE_CHUNK Chunk;
438 LARGE_INTEGER liMove;
439 HRESULT hr;
441 TRACE("(%p,%p): loading\n", This, pStm);
443 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
444 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
445 switch (Chunk.fccID) {
446 case FOURCC_RIFF: {
447 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
448 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
449 switch (Chunk.fccID) {
450 case DMUS_FOURCC_BAND_FORM: {
451 TRACE_(dmfile)(": Band form\n");
452 hr = parse_band_form(This, &Chunk, pStm);
453 if (FAILED(hr)) return hr;
454 break;
456 default: {
457 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
458 liMove.QuadPart = Chunk.dwSize;
459 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
460 return E_FAIL;
463 TRACE_(dmfile)(": reading finished\n");
464 break;
466 default: {
467 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
468 liMove.QuadPart = Chunk.dwSize;
469 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
470 return E_FAIL;
474 return S_OK;
477 static const IPersistStreamVtbl persiststream_vtbl = {
478 dmobj_IPersistStream_QueryInterface,
479 dmobj_IPersistStream_AddRef,
480 dmobj_IPersistStream_Release,
481 unimpl_IPersistStream_GetClassID,
482 unimpl_IPersistStream_IsDirty,
483 IPersistStreamImpl_Load,
484 unimpl_IPersistStream_Save,
485 unimpl_IPersistStream_GetSizeMax
488 /* for ClassFactory */
489 HRESULT WINAPI create_dmband(REFIID lpcGUID, void **ppobj)
491 IDirectMusicBandImpl* obj;
492 HRESULT hr;
494 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandImpl));
495 if (NULL == obj) {
496 *ppobj = NULL;
497 return E_OUTOFMEMORY;
499 obj->IDirectMusicBand_iface.lpVtbl = &dmband_vtbl;
500 obj->ref = 1;
501 dmobject_init(&obj->dmobj, &CLSID_DirectMusicBand, (IUnknown *)&obj->IDirectMusicBand_iface);
502 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
503 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
504 list_init (&obj->Instruments);
506 DMBAND_LockModule();
507 hr = IDirectMusicBand_QueryInterface(&obj->IDirectMusicBand_iface, lpcGUID, ppobj);
508 IDirectMusicBand_Release(&obj->IDirectMusicBand_iface);
510 return hr;