ReactOS  0.4.14-dev-317-g96040ec
dmobject.c
Go to the documentation of this file.
1 /*
2  * Base IDirectMusicObject Implementation
3  * Keep in sync with the master in dlls/dmusic/dmobject.c
4  *
5  * Copyright (C) 2003-2004 Rok Mandeljc
6  * Copyright (C) 2014 Michael Stefaniuc
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 
23 #define COBJMACROS
24 #include <assert.h>
25 #include "objbase.h"
26 #include "dmusici.h"
27 #include "dmusicf.h"
28 #include "dmobject.h"
29 #include "wine/debug.h"
30 
31 #include "dmusic_private.h"
32 
35 
36 /* RIFF format parsing */
37 #define CHUNK_HDR_SIZE (sizeof(FOURCC) + sizeof(DWORD))
38 
39 #ifndef __REACTOS__
40 static inline const char *debugstr_fourcc(DWORD fourcc)
41 {
42  if (!fourcc) return "''";
43  return wine_dbg_sprintf("'%c%c%c%c'", (char)(fourcc), (char)(fourcc >> 8),
44  (char)(fourcc >> 16), (char)(fourcc >> 24));
45 }
46 #endif
47 
48 const char *debugstr_chunk(const struct chunk_entry *chunk)
49 {
50  const char *type = "";
51 
52  if (!chunk)
53  return "(null)";
54  if (chunk->id == FOURCC_RIFF || chunk->id == FOURCC_LIST)
55  type = wine_dbg_sprintf("type %s, ", debugstr_fourcc(chunk->type));
56  return wine_dbg_sprintf("%s chunk, %ssize %u", debugstr_fourcc(chunk->id), type, chunk->size);
57 }
58 
60 {
61  ULONG read;
62  HRESULT hr;
63 
64  hr = IStream_Read(stream, data, size, &read);
65  if (FAILED(hr))
66  TRACE_(dmfile)("IStream_Read failed: %08x\n", hr);
67  else if (!read && read < size) {
68  /* All or nothing: Handle a partial read due to end of stream as an error */
69  TRACE_(dmfile)("Short read: %u < %u\n", read, size);
70  return E_FAIL;
71  }
72 
73  return hr;
74 }
75 
77 {
78  static const LARGE_INTEGER zero;
79  ULONGLONG ck_end = 0, p_end = 0;
80  HRESULT hr;
81 
82  hr = IStream_Seek(stream, zero, STREAM_SEEK_CUR, &chunk->offset);
83  if (FAILED(hr))
84  return hr;
85  assert(!(chunk->offset.QuadPart & 1));
86  if (chunk->parent) {
87  p_end = chunk->parent->offset.QuadPart + CHUNK_HDR_SIZE + ((chunk->parent->size + 1) & ~1);
88  if (chunk->offset.QuadPart == p_end)
89  return S_FALSE;
90  ck_end = chunk->offset.QuadPart + CHUNK_HDR_SIZE;
91  if (ck_end > p_end) {
92  WARN_(dmfile)("No space for sub-chunk header in parent chunk: ends at offset %s > %s\n",
94  return E_FAIL;
95  }
96  }
97 
99  if (hr != S_OK)
100  return hr;
101  if (chunk->parent) {
102  ck_end += (chunk->size + 1) & ~1;
103  if (ck_end > p_end) {
104  WARN_(dmfile)("No space for sub-chunk data in parent chunk: ends at offset %s > %s\n",
106  return E_FAIL;
107  }
108  }
109 
110  if (chunk->id == FOURCC_LIST || chunk->id == FOURCC_RIFF) {
111  hr = stream_read(stream, &chunk->type, sizeof(FOURCC));
112  if (hr != S_OK)
113  return hr != S_FALSE ? hr : E_FAIL;
114  }
115 
116  TRACE_(dmfile)("Returning %s\n", debugstr_chunk(chunk));
117 
118  return S_OK;
119 }
120 
122 {
124 
125  end.QuadPart = (chunk->offset.QuadPart + CHUNK_HDR_SIZE + chunk->size + 1) & ~(ULONGLONG)1;
126 
127  return IStream_Seek(stream, end, STREAM_SEEK_SET, NULL);
128 }
129 
131 {
132  HRESULT hr;
133 
134  if (chunk->id) {
136  if (FAILED(hr))
137  return hr;
138  }
139 
140  return stream_get_chunk(stream, chunk);
141 }
142 
144  ULONG size)
145 {
146  if (chunk->size != size) {
147  WARN_(dmfile)("Chunk %s (size %u, offset %s) doesn't contains the expected data size %u\n",
149  wine_dbgstr_longlong(chunk->offset.QuadPart), size);
150  return E_FAIL;
151  }
152  return stream_read(stream, data, size);
153 }
154 
156  ULONG size)
157 {
158  ULONG len;
159  HRESULT hr;
160 
161  hr = IStream_Read(stream, str, min(chunk->size, size), &len);
162  if (FAILED(hr))
163  return hr;
164 
165  /* Don't assume the string is properly zero terminated */
166  str[min(len, size - 1)] = 0;
167 
168  if (len < chunk->size)
169  return S_FALSE;
170  return S_OK;
171 }
172 
173 
174 
175 /* Generic IDirectMusicObject methods */
176 static inline struct dmobject *impl_from_IDirectMusicObject(IDirectMusicObject *iface)
177 {
178  return CONTAINING_RECORD(iface, struct dmobject, IDirectMusicObject_iface);
179 }
180 
182  void **ret_iface)
183 {
184  struct dmobject *This = impl_from_IDirectMusicObject(iface);
185  return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface);
186 }
187 
188 ULONG WINAPI dmobj_IDirectMusicObject_AddRef(IDirectMusicObject *iface)
189 {
190  struct dmobject *This = impl_from_IDirectMusicObject(iface);
191  return IUnknown_AddRef(This->outer_unk);
192 }
193 
195 {
196  struct dmobject *This = impl_from_IDirectMusicObject(iface);
197  return IUnknown_Release(This->outer_unk);
198 }
199 
202 {
203  struct dmobject *This = impl_from_IDirectMusicObject(iface);
204 
205  TRACE("(%p/%p)->(%p)\n", iface, This, desc);
206 
207  if (!desc)
208  return E_POINTER;
209 
210  memcpy(desc, &This->desc, This->desc.dwSize);
211 
212  return S_OK;
213 }
214 
217 {
218  struct dmobject *This = impl_from_IDirectMusicObject(iface);
219  HRESULT ret = S_OK;
220 
221  TRACE("(%p, %p)\n", iface, desc);
222 
223  if (!desc)
224  return E_POINTER;
225 
226  /* Immutable property */
227  if (desc->dwValidData & DMUS_OBJ_CLASS)
228  {
229  desc->dwValidData &= ~DMUS_OBJ_CLASS;
230  ret = S_FALSE;
231  }
232  /* Set only valid fields */
233  if (desc->dwValidData & DMUS_OBJ_OBJECT)
234  This->desc.guidObject = desc->guidObject;
235  if (desc->dwValidData & DMUS_OBJ_NAME)
236  lstrcpynW(This->desc.wszName, desc->wszName, DMUS_MAX_NAME);
237  if (desc->dwValidData & DMUS_OBJ_CATEGORY)
238  lstrcpynW(This->desc.wszCategory, desc->wszCategory, DMUS_MAX_CATEGORY);
239  if (desc->dwValidData & DMUS_OBJ_FILENAME)
240  lstrcpynW(This->desc.wszFileName, desc->wszFileName, DMUS_MAX_FILENAME);
241  if (desc->dwValidData & DMUS_OBJ_VERSION)
242  This->desc.vVersion = desc->vVersion;
243  if (desc->dwValidData & DMUS_OBJ_DATE)
244  This->desc.ftDate = desc->ftDate;
245  if (desc->dwValidData & DMUS_OBJ_MEMORY) {
246  This->desc.llMemLength = desc->llMemLength;
247  memcpy(This->desc.pbMemData, desc->pbMemData, desc->llMemLength);
248  }
249  if (desc->dwValidData & DMUS_OBJ_STREAM)
250  IStream_Clone(desc->pStream, &This->desc.pStream);
251 
252  This->desc.dwValidData |= desc->dwValidData;
253 
254  return ret;
255 }
256 
257 /* Helper for IDirectMusicObject::ParseDescriptor */
258 static inline void info_get_name(IStream *stream, const struct chunk_entry *info,
260 {
261 #ifndef __REACTOS__
262  struct chunk_entry chunk = {.parent = info};
263 #else
264  struct chunk_entry chunk = { 0, 0, 0, {{0}}, info };
265 #endif
266  char name[DMUS_MAX_NAME];
267  ULONG len;
268  HRESULT hr = E_FAIL;
269 
270  while (stream_next_chunk(stream, &chunk) == S_OK)
271  if (chunk.id == mmioFOURCC('I','N','A','M'))
272  hr = IStream_Read(stream, name, min(chunk.size, sizeof(name)), &len);
273 
274  if (SUCCEEDED(hr)) {
275  len = MultiByteToWideChar(CP_ACP, 0, name, len, desc->wszName, sizeof(desc->wszName));
276  desc->wszName[min(len, sizeof(desc->wszName) - 1)] = 0;
277  desc->dwValidData |= DMUS_OBJ_NAME;
278  }
279 }
280 
281 static inline void unfo_get_name(IStream *stream, const struct chunk_entry *unfo,
282  DMUS_OBJECTDESC *desc, BOOL inam)
283 {
284 #ifndef __REACTOS__
285  struct chunk_entry chunk = {.parent = unfo};
286 #else
287  struct chunk_entry chunk = { 0, 0, 0, {{0}}, unfo };
288 #endif
289 
290  while (stream_next_chunk(stream, &chunk) == S_OK)
291  if (chunk.id == DMUS_FOURCC_UNAM_CHUNK || (inam && chunk.id == mmioFOURCC('I','N','A','M')))
292  if (stream_chunk_get_wstr(stream, &chunk, desc->wszName, sizeof(desc->wszName)) == S_OK)
293  desc->dwValidData |= DMUS_OBJ_NAME;
294 }
295 
297  DMUS_OBJECTDESC *desc, DWORD supported)
298 {
299 #ifndef __REACTOS__
300  struct chunk_entry chunk = {.parent = riff};
301 #else
302  struct chunk_entry chunk = { 0, 0, 0, {{0}}, riff };
303 #endif
304  HRESULT hr;
305 
306  TRACE("Looking for %#x in %p: %s\n", supported, stream, debugstr_chunk(riff));
307 
308  desc->dwValidData = 0;
309  desc->dwSize = sizeof(*desc);
310 
311  while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
312  switch (chunk.id) {
314  if ((supported & DMUS_OBJ_OBJECT) && stream_chunk_get_data(stream, &chunk,
315  &desc->guidObject, sizeof(desc->guidObject)) == S_OK)
316  desc->dwValidData |= DMUS_OBJ_OBJECT;
317  break;
319  if ((supported & DMUS_OBJ_CATEGORY) && stream_chunk_get_wstr(stream, &chunk,
320  desc->wszCategory, sizeof(desc->wszCategory)) == S_OK)
321  desc->dwValidData |= DMUS_OBJ_CATEGORY;
322  break;
324  if ((supported & DMUS_OBJ_VERSION) && stream_chunk_get_data(stream, &chunk,
325  &desc->vVersion, sizeof(desc->vVersion)) == S_OK)
326  desc->dwValidData |= DMUS_OBJ_VERSION;
327  break;
328  case FOURCC_LIST:
329  if (chunk.type == DMUS_FOURCC_UNFO_LIST && (supported & DMUS_OBJ_NAME))
331  else if (chunk.type == DMUS_FOURCC_INFO_LIST && (supported & DMUS_OBJ_NAME_INFO))
333  break;
334  }
335  }
336  TRACE("Found %#x\n", desc->dwValidData);
337 
338  return hr;
339 }
340 
341 /* Generic IPersistStream methods */
342 static inline struct dmobject *impl_from_IPersistStream(IPersistStream *iface)
343 {
344  return CONTAINING_RECORD(iface, struct dmobject, IPersistStream_iface);
345 }
346 
348  void **ret_iface)
349 {
350  struct dmobject *This = impl_from_IPersistStream(iface);
351  return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface);
352 }
353 
355 {
356  struct dmobject *This = impl_from_IPersistStream(iface);
357  return IUnknown_AddRef(This->outer_unk);
358 }
359 
361 {
362  struct dmobject *This = impl_from_IPersistStream(iface);
363  return IUnknown_Release(This->outer_unk);
364 }
365 
367 {
368  struct dmobject *This = impl_from_IPersistStream(iface);
369 
370  TRACE("(%p, %p)\n", This, class);
371 
372  if (!class)
373  return E_POINTER;
374 
375  *class = This->desc.guidClass;
376 
377  return S_OK;
378 }
379 
380 /* IPersistStream methods not implemented in native */
382 {
383  TRACE("(%p, %p): method not implemented\n", iface, class);
384  return E_NOTIMPL;
385 }
386 
388 {
389  TRACE("(%p): method not implemented, always returning S_FALSE\n", iface);
390  return S_FALSE;
391 }
392 
394  BOOL clear_dirty)
395 {
396  TRACE("(%p, %p, %d): method not implemented\n", iface, stream, clear_dirty);
397  return E_NOTIMPL;
398 }
399 
401 {
402  TRACE("(%p, %p): method not implemented\n", iface, size);
403  return E_NOTIMPL;
404 }
405 
406 
407 void dmobject_init(struct dmobject *dmobj, const GUID *class, IUnknown *outer_unk)
408 {
409  dmobj->outer_unk = outer_unk;
410  dmobj->desc.dwSize = sizeof(dmobj->desc);
412  dmobj->desc.guidClass = *class;
413 }
HRESULT WINAPI unimpl_IPersistStream_IsDirty(IPersistStream *iface)
Definition: dmobject.c:387
HRESULT WINAPI dmobj_IDirectMusicObject_SetDescriptor(IDirectMusicObject *iface, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:215
HRESULT dmobj_parsedescriptor(IStream *stream, const struct chunk_entry *riff, DMUS_OBJECTDESC *desc, DWORD supported)
Definition: dmobject.c:296
#define REFIID
Definition: guiddef.h:118
#define DMUS_FOURCC_VERSION_CHUNK
Definition: dmusicf.h:51
uint16_t size
Definition: btrfs_drv.h:556
HRESULT stream_next_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:130
HRESULT hr
Definition: shlfolder.c:183
HRESULT WINAPI dmobj_IPersistStream_QueryInterface(IPersistStream *iface, REFIID riid, void **ret_iface)
Definition: dmobject.c:347
WINE_DEFAULT_DEBUG_CHANNEL(dmobj)
REFIID riid
Definition: precomp.h:44
#define CP_ACP
Definition: compat.h:99
uint64_t offset
Definition: btrfs_drv.h:557
#define DMUS_OBJ_NAME
Definition: dmusici.h:267
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
#define assert(x)
Definition: debug.h:53
#define CHUNK_HDR_SIZE
Definition: dmobject.c:37
GLuint GLuint end
Definition: gl.h:1545
#define E_FAIL
Definition: ddrawi.h:102
#define DMUS_MAX_CATEGORY
Definition: dmusici.h:246
#define lstrcpynW
Definition: compat.h:397
HRESULT WINAPI unimpl_IPersistStream_Save(IPersistStream *iface, IStream *stream, BOOL clear_dirty)
Definition: dmobject.c:393
#define DMUS_FOURCC_UNFO_LIST
Definition: dmusicf.h:44
const char * wine_dbg_sprintf(const char *format,...)
Definition: compat.c:271
#define DMUS_OBJ_FILENAME
Definition: dmusici.h:269
#define DMUS_OBJ_NAME_INAM
Definition: dmobject.h:93
unsigned int BOOL
Definition: ntddk_ex.h:94
ULONG WINAPI dmobj_IDirectMusicObject_Release(IDirectMusicObject *iface)
Definition: dmobject.c:194
static const WCHAR desc[]
Definition: protectdata.c:36
static struct dmobject * impl_from_IPersistStream(IPersistStream *iface)
Definition: dmobject.c:342
#define DMUS_FOURCC_CATEGORY_CHUNK
Definition: dmusicf.h:50
#define S_FALSE
Definition: winerror.h:2357
WINE_DECLARE_DEBUG_CHANNEL(dmfile)
const WCHAR * str
const char * debugstr_chunk(const struct chunk_entry *chunk)
Definition: dmobject.c:48
smooth NULL
Definition: ftsmooth.c:416
#define DMUS_FOURCC_GUID_CHUNK
Definition: dmusicf.h:42
ULONG WINAPI dmobj_IDirectMusicObject_AddRef(IDirectMusicObject *iface)
Definition: dmobject.c:188
IPersistStream IPersistStream_iface
Definition: dmobject.h:71
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
DMUS_OBJECTDESC desc
Definition: dmobject.h:73
void dmobject_init(struct dmobject *dmobj, const GUID *class, IUnknown *outer_unk)
Definition: dmobject.c:407
#define TRACE_(x)
Definition: compat.h:66
DWORD dwValidData
Definition: dmusici.h:743
#define TRACE(s)
Definition: solgame.cpp:4
GLsizeiptr size
Definition: glext.h:5919
#define DMUS_OBJ_NAME_INFO
Definition: dmobject.h:94
#define FOURCC_LIST
Definition: mmsystem.h:565
__wchar_t WCHAR
Definition: xmlstorage.h:180
LONG HRESULT
Definition: typedefs.h:77
static const char * debugstr_fourcc(DWORD fourcc)
Definition: dmobject.c:40
uint64_t ULONGLONG
Definition: typedefs.h:65
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
Definition: dmobject.h:28
#define DMUS_OBJ_OBJECT
Definition: dmusici.h:265
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
HRESULT WINAPI dmobj_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
Definition: dmobject.c:366
int ret
static double zero
Definition: j0_y0.c:96
#define DMUS_OBJ_MEMORY
Definition: dmusici.h:275
#define FOURCC_RIFF
Definition: mmsystem.h:564
Definition: parse.h:22
#define mmioFOURCC(c0, c1, c2, c3)
Definition: mmsystem.h:38
IDirectMusicObject IDirectMusicObject_iface
Definition: dmobject.h:70
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
HRESULT stream_chunk_get_wstr(IStream *stream, const struct chunk_entry *chunk, WCHAR *str, ULONG size)
Definition: dmobject.c:155
#define DMUS_OBJ_DATE
Definition: dmusici.h:273
DWORD FOURCC
Definition: dmdls.h:25
#define DMUS_OBJ_CATEGORY
Definition: dmusici.h:268
#define S_OK
Definition: intsafe.h:59
#define DMUS_FOURCC_INFO_LIST
Definition: dmusicf.h:43
ULONG WINAPI dmobj_IPersistStream_Release(IPersistStream *iface)
Definition: dmobject.c:360
#define E_NOTIMPL
Definition: ddrawi.h:99
HRESULT stream_skip_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:121
#define min(a, b)
Definition: monoChain.cc:55
static void unfo_get_name(IStream *stream, const struct chunk_entry *unfo, DMUS_OBJECTDESC *desc, BOOL inam)
Definition: dmobject.c:281
#define MultiByteToWideChar
Definition: compat.h:100
Definition: name.c:36
HRESULT WINAPI unimpl_IPersistStream_GetSizeMax(IPersistStream *iface, ULARGE_INTEGER *size)
Definition: dmobject.c:400
HRESULT WINAPI dmobj_IDirectMusicObject_GetDescriptor(IDirectMusicObject *iface, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:200
ULONG WINAPI dmobj_IPersistStream_AddRef(IPersistStream *iface)
Definition: dmobject.c:354
#define DMUS_FOURCC_UNAM_CHUNK
Definition: dmusicf.h:45
static void info_get_name(IStream *stream, const struct chunk_entry *info, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:258
unsigned int ULONG
Definition: retypes.h:1
#define DMUS_MAX_FILENAME
Definition: dmusici.h:247
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:41
HRESULT stream_chunk_get_data(IStream *stream, const struct chunk_entry *chunk, void *data, ULONG size)
Definition: dmobject.c:143
#define DMUS_OBJ_VERSION
Definition: dmusici.h:272
#define E_POINTER
Definition: winerror.h:2365
static struct dmobject * impl_from_IDirectMusicObject(IDirectMusicObject *iface)
Definition: dmobject.c:176
HRESULT WINAPI unimpl_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
Definition: dmobject.c:381
#define WARN_(ch,...)
Definition: debug.h:157
#define DMUS_OBJ_CLASS
Definition: dmusici.h:266
#define DMUS_OBJ_STREAM
Definition: dmusici.h:276
#define DMUS_MAX_NAME
Definition: dmusici.h:245
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
#define SUCCEEDED(hr)
Definition: intsafe.h:57
static HRESULT stream_read(IStream *stream, void *data, ULONG size)
Definition: dmobject.c:59
HRESULT WINAPI dmobj_IDirectMusicObject_QueryInterface(IDirectMusicObject *iface, REFIID riid, void **ret_iface)
Definition: dmobject.c:181
IUnknown * outer_unk
Definition: dmobject.h:72
HRESULT stream_get_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:76