ReactOS 0.4.17-dev-804-g023d8af
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
39const char *debugstr_chunk(const struct chunk_entry *chunk)
40{
41 const char *type = "";
42
43 if (!chunk)
44 return "(null)";
45 if (chunk->id == FOURCC_RIFF || chunk->id == FOURCC_LIST)
46 type = wine_dbg_sprintf("type %s, ", debugstr_fourcc(chunk->type));
47 return wine_dbg_sprintf("%s chunk, %ssize %u", debugstr_fourcc(chunk->id), type, chunk->size);
48}
49
51{
52 ULONG read;
53 HRESULT hr;
54
55 hr = IStream_Read(stream, data, size, &read);
56 if (FAILED(hr))
57 TRACE_(dmfile)("IStream_Read failed: %08x\n", hr);
58 else if (!read && read < size) {
59 /* All or nothing: Handle a partial read due to end of stream as an error */
60 TRACE_(dmfile)("Short read: %u < %u\n", read, size);
61 return E_FAIL;
62 }
63
64 return hr;
65}
66
68{
69 static const LARGE_INTEGER zero;
70 ULONGLONG ck_end = 0, p_end = 0;
71 HRESULT hr;
72
73 hr = IStream_Seek(stream, zero, STREAM_SEEK_CUR, &chunk->offset);
74 if (FAILED(hr))
75 return hr;
76 assert(!(chunk->offset.QuadPart & 1));
77 if (chunk->parent) {
78 p_end = chunk->parent->offset.QuadPart + CHUNK_HDR_SIZE + ((chunk->parent->size + 1) & ~1);
79 if (chunk->offset.QuadPart == p_end)
80 return S_FALSE;
81 ck_end = chunk->offset.QuadPart + CHUNK_HDR_SIZE;
82 if (ck_end > p_end) {
83 WARN_(dmfile)("No space for sub-chunk header in parent chunk: ends at offset %s > %s\n",
85 return E_FAIL;
86 }
87 }
88
90 if (hr != S_OK)
91 return hr;
92 if (chunk->parent) {
93 ck_end += (chunk->size + 1) & ~1;
94 if (ck_end > p_end) {
95 WARN_(dmfile)("No space for sub-chunk data in parent chunk: ends at offset %s > %s\n",
97 return E_FAIL;
98 }
99 }
100
101 if (chunk->id == FOURCC_LIST || chunk->id == FOURCC_RIFF) {
102 hr = stream_read(stream, &chunk->type, sizeof(FOURCC));
103 if (hr != S_OK)
104 return hr != S_FALSE ? hr : E_FAIL;
105 }
106
107 TRACE_(dmfile)("Returning %s\n", debugstr_chunk(chunk));
108
109 return S_OK;
110}
111
113{
115
116 end.QuadPart = (chunk->offset.QuadPart + CHUNK_HDR_SIZE + chunk->size + 1) & ~(ULONGLONG)1;
117
118 return IStream_Seek(stream, end, STREAM_SEEK_SET, NULL);
119}
120
122{
123 HRESULT hr;
124
125 if (chunk->id) {
127 if (FAILED(hr))
128 return hr;
129 }
130
132}
133
135 ULONG size)
136{
137 if (chunk->size != size) {
138 WARN_(dmfile)("Chunk %s (size %u, offset %s) doesn't contains the expected data size %u\n",
139 debugstr_fourcc(chunk->id), chunk->size,
141 return E_FAIL;
142 }
143 return stream_read(stream, data, size);
144}
145
147 ULONG size)
148{
149 ULONG len;
150 HRESULT hr;
151
152 hr = IStream_Read(stream, str, min(chunk->size, size), &len);
153 if (FAILED(hr))
154 return hr;
155
156 /* Don't assume the string is properly zero terminated */
157 str[min(len, size - 1)] = 0;
158
159 if (len < chunk->size)
160 return S_FALSE;
161 return S_OK;
162}
163
164
165
166/* Generic IDirectMusicObject methods */
167static inline struct dmobject *impl_from_IDirectMusicObject(IDirectMusicObject *iface)
168{
170}
171
173 void **ret_iface)
174{
176 return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface);
177}
178
180{
182 return IUnknown_AddRef(This->outer_unk);
183}
184
186{
188 return IUnknown_Release(This->outer_unk);
189}
190
193{
195
196 TRACE("(%p/%p)->(%p)\n", iface, This, desc);
197
198 if (!desc)
199 return E_POINTER;
200
201 memcpy(desc, &This->desc, This->desc.dwSize);
202
203 return S_OK;
204}
205
208{
210 HRESULT ret = S_OK;
211
212 TRACE("(%p, %p)\n", iface, desc);
213
214 if (!desc)
215 return E_POINTER;
216
217 /* Immutable property */
218 if (desc->dwValidData & DMUS_OBJ_CLASS)
219 {
220 desc->dwValidData &= ~DMUS_OBJ_CLASS;
221 ret = S_FALSE;
222 }
223 /* Set only valid fields */
224 if (desc->dwValidData & DMUS_OBJ_OBJECT)
225 This->desc.guidObject = desc->guidObject;
226 if (desc->dwValidData & DMUS_OBJ_NAME)
227 lstrcpynW(This->desc.wszName, desc->wszName, DMUS_MAX_NAME);
228 if (desc->dwValidData & DMUS_OBJ_CATEGORY)
229 lstrcpynW(This->desc.wszCategory, desc->wszCategory, DMUS_MAX_CATEGORY);
230 if (desc->dwValidData & DMUS_OBJ_FILENAME)
231 lstrcpynW(This->desc.wszFileName, desc->wszFileName, DMUS_MAX_FILENAME);
232 if (desc->dwValidData & DMUS_OBJ_VERSION)
233 This->desc.vVersion = desc->vVersion;
234 if (desc->dwValidData & DMUS_OBJ_DATE)
235 This->desc.ftDate = desc->ftDate;
236 if (desc->dwValidData & DMUS_OBJ_MEMORY) {
237 This->desc.llMemLength = desc->llMemLength;
238 memcpy(This->desc.pbMemData, desc->pbMemData, desc->llMemLength);
239 }
240 if (desc->dwValidData & DMUS_OBJ_STREAM)
241 IStream_Clone(desc->pStream, &This->desc.pStream);
242
243 This->desc.dwValidData |= desc->dwValidData;
244
245 return ret;
246}
247
248/* Helper for IDirectMusicObject::ParseDescriptor */
249static inline void info_get_name(IStream *stream, const struct chunk_entry *info,
251{
252#ifndef __REACTOS__
253 struct chunk_entry chunk = {.parent = info};
254#else
255 struct chunk_entry chunk = { 0, 0, 0, {{0}}, info };
256#endif
257 char name[DMUS_MAX_NAME];
258 ULONG len;
259 HRESULT hr = E_FAIL;
260
261 while (stream_next_chunk(stream, &chunk) == S_OK)
262 if (chunk.id == mmioFOURCC('I','N','A','M'))
263 hr = IStream_Read(stream, name, min(chunk.size, sizeof(name)), &len);
264
265 if (SUCCEEDED(hr)) {
266 len = MultiByteToWideChar(CP_ACP, 0, name, len, desc->wszName, sizeof(desc->wszName));
267 desc->wszName[min(len, sizeof(desc->wszName) - 1)] = 0;
268 desc->dwValidData |= DMUS_OBJ_NAME;
269 }
270}
271
272static inline void unfo_get_name(IStream *stream, const struct chunk_entry *unfo,
274{
275#ifndef __REACTOS__
276 struct chunk_entry chunk = {.parent = unfo};
277#else
278 struct chunk_entry chunk = { 0, 0, 0, {{0}}, unfo };
279#endif
280
281 while (stream_next_chunk(stream, &chunk) == S_OK)
282 if (chunk.id == DMUS_FOURCC_UNAM_CHUNK || (inam && chunk.id == mmioFOURCC('I','N','A','M')))
283 if (stream_chunk_get_wstr(stream, &chunk, desc->wszName, sizeof(desc->wszName)) == S_OK)
284 desc->dwValidData |= DMUS_OBJ_NAME;
285}
286
288 DMUS_OBJECTDESC *desc, DWORD supported)
289{
290#ifndef __REACTOS__
291 struct chunk_entry chunk = {.parent = riff};
292#else
293 struct chunk_entry chunk = { 0, 0, 0, {{0}}, riff };
294#endif
295 HRESULT hr;
296
297 TRACE("Looking for %#x in %p: %s\n", supported, stream, debugstr_chunk(riff));
298
299 desc->dwValidData = 0;
300 desc->dwSize = sizeof(*desc);
301
302 while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
303 switch (chunk.id) {
305 if ((supported & DMUS_OBJ_OBJECT) && stream_chunk_get_data(stream, &chunk,
306 &desc->guidObject, sizeof(desc->guidObject)) == S_OK)
307 desc->dwValidData |= DMUS_OBJ_OBJECT;
308 break;
311 desc->wszCategory, sizeof(desc->wszCategory)) == S_OK)
312 desc->dwValidData |= DMUS_OBJ_CATEGORY;
313 break;
315 if ((supported & DMUS_OBJ_VERSION) && stream_chunk_get_data(stream, &chunk,
316 &desc->vVersion, sizeof(desc->vVersion)) == S_OK)
317 desc->dwValidData |= DMUS_OBJ_VERSION;
318 break;
319 case FOURCC_LIST:
320 if (chunk.type == DMUS_FOURCC_UNFO_LIST && (supported & DMUS_OBJ_NAME))
322 else if (chunk.type == DMUS_FOURCC_INFO_LIST && (supported & DMUS_OBJ_NAME_INFO))
324 break;
325 }
326 }
327 TRACE("Found %#x\n", desc->dwValidData);
328
329 return hr;
330}
331
332/* Generic IPersistStream methods */
334{
335 return CONTAINING_RECORD(iface, struct dmobject, IPersistStream_iface);
336}
337
339 void **ret_iface)
340{
341 struct dmobject *This = impl_from_IPersistStream(iface);
342 return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface);
343}
344
346{
347 struct dmobject *This = impl_from_IPersistStream(iface);
348 return IUnknown_AddRef(This->outer_unk);
349}
350
352{
353 struct dmobject *This = impl_from_IPersistStream(iface);
354 return IUnknown_Release(This->outer_unk);
355}
356
358{
359 struct dmobject *This = impl_from_IPersistStream(iface);
360
361 TRACE("(%p, %p)\n", This, class);
362
363 if (!class)
364 return E_POINTER;
365
366 *class = This->desc.guidClass;
367
368 return S_OK;
369}
370
371/* IPersistStream methods not implemented in native */
373{
374 TRACE("(%p, %p): method not implemented\n", iface, class);
375 return E_NOTIMPL;
376}
377
379{
380 TRACE("(%p): method not implemented, always returning S_FALSE\n", iface);
381 return S_FALSE;
382}
383
385 BOOL clear_dirty)
386{
387 TRACE("(%p, %p, %d): method not implemented\n", iface, stream, clear_dirty);
388 return E_NOTIMPL;
389}
390
392{
393 TRACE("(%p, %p): method not implemented\n", iface, size);
394 return E_NOTIMPL;
395}
396
397
398void dmobject_init(struct dmobject *dmobj, const GUID *class, IUnknown *outer_unk)
399{
400 dmobj->outer_unk = outer_unk;
401 dmobj->desc.dwSize = sizeof(dmobj->desc);
403 dmobj->desc.guidClass = *class;
404}
#define read
Definition: acwin.h:97
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define E_NOTIMPL
Definition: ddrawi.h:99
#define E_FAIL
Definition: ddrawi.h:102
HRESULT hr
Definition: delayimp.cpp:582
#define NULL
Definition: types.h:112
const char * wine_dbg_sprintf(const char *format,...)
Definition: compat.c:296
#define CP_ACP
Definition: compat.h:109
#define TRACE_(x)
Definition: compat.h:76
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:49
#define WINE_DECLARE_DEBUG_CHANNEL(x)
Definition: compat.h:45
#define MultiByteToWideChar
Definition: compat.h:110
#define lstrcpynW
Definition: compat.h:738
#define assert(_expr)
Definition: assert.h:32
DWORD FOURCC
Definition: dmdls.h:25
static struct dmobject * impl_from_IPersistStream(IPersistStream *iface)
Definition: dmobject.c:333
HRESULT WINAPI dmobj_IDirectMusicObject_GetDescriptor(IDirectMusicObject *iface, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:191
void dmobject_init(struct dmobject *dmobj, const GUID *class, IUnknown *outer_unk)
Definition: dmobject.c:398
HRESULT stream_chunk_get_data(IStream *stream, const struct chunk_entry *chunk, void *data, ULONG size)
Definition: dmobject.c:134
HRESULT stream_next_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:121
ULONG WINAPI dmobj_IPersistStream_Release(IPersistStream *iface)
Definition: dmobject.c:351
#define CHUNK_HDR_SIZE
Definition: dmobject.c:37
ULONG WINAPI dmobj_IDirectMusicObject_Release(IDirectMusicObject *iface)
Definition: dmobject.c:185
HRESULT dmobj_parsedescriptor(IStream *stream, const struct chunk_entry *riff, DMUS_OBJECTDESC *desc, DWORD supported)
Definition: dmobject.c:287
HRESULT WINAPI dmobj_IDirectMusicObject_QueryInterface(IDirectMusicObject *iface, REFIID riid, void **ret_iface)
Definition: dmobject.c:172
static void info_get_name(IStream *stream, const struct chunk_entry *info, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:249
HRESULT stream_skip_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:112
static void unfo_get_name(IStream *stream, const struct chunk_entry *unfo, DMUS_OBJECTDESC *desc, BOOL inam)
Definition: dmobject.c:272
HRESULT WINAPI unimpl_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
Definition: dmobject.c:372
HRESULT WINAPI unimpl_IPersistStream_GetSizeMax(IPersistStream *iface, ULARGE_INTEGER *size)
Definition: dmobject.c:391
HRESULT WINAPI dmobj_IDirectMusicObject_SetDescriptor(IDirectMusicObject *iface, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:206
HRESULT stream_chunk_get_wstr(IStream *stream, const struct chunk_entry *chunk, WCHAR *str, ULONG size)
Definition: dmobject.c:146
const char * debugstr_chunk(const struct chunk_entry *chunk)
Definition: dmobject.c:39
HRESULT WINAPI dmobj_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
Definition: dmobject.c:357
ULONG WINAPI dmobj_IPersistStream_AddRef(IPersistStream *iface)
Definition: dmobject.c:345
HRESULT WINAPI dmobj_IPersistStream_QueryInterface(IPersistStream *iface, REFIID riid, void **ret_iface)
Definition: dmobject.c:338
ULONG WINAPI dmobj_IDirectMusicObject_AddRef(IDirectMusicObject *iface)
Definition: dmobject.c:179
static struct dmobject * impl_from_IDirectMusicObject(IDirectMusicObject *iface)
Definition: dmobject.c:167
HRESULT stream_get_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:67
HRESULT WINAPI unimpl_IPersistStream_IsDirty(IPersistStream *iface)
Definition: dmobject.c:378
HRESULT WINAPI unimpl_IPersistStream_Save(IPersistStream *iface, IStream *stream, BOOL clear_dirty)
Definition: dmobject.c:384
#define DMUS_OBJ_NAME_INAM
Definition: dmobject.h:93
#define DMUS_OBJ_NAME_INFO
Definition: dmobject.h:94
#define DMUS_FOURCC_GUID_CHUNK
Definition: dmusicf.h:42
#define DMUS_FOURCC_UNAM_CHUNK
Definition: dmusicf.h:45
#define DMUS_FOURCC_VERSION_CHUNK
Definition: dmusicf.h:51
#define DMUS_FOURCC_INFO_LIST
Definition: dmusicf.h:43
#define DMUS_FOURCC_UNFO_LIST
Definition: dmusicf.h:44
#define DMUS_FOURCC_CATEGORY_CHUNK
Definition: dmusicf.h:50
#define DMUS_OBJ_DATE
Definition: dmusici.h:273
#define DMUS_MAX_CATEGORY
Definition: dmusici.h:246
#define DMUS_OBJ_MEMORY
Definition: dmusici.h:275
#define DMUS_OBJ_NAME
Definition: dmusici.h:267
#define DMUS_OBJ_STREAM
Definition: dmusici.h:276
#define DMUS_OBJ_FILENAME
Definition: dmusici.h:269
#define DMUS_OBJ_CLASS
Definition: dmusici.h:266
#define DMUS_MAX_NAME
Definition: dmusici.h:245
#define DMUS_MAX_FILENAME
Definition: dmusici.h:247
#define DMUS_OBJ_VERSION
Definition: dmusici.h:272
#define DMUS_OBJ_CATEGORY
Definition: dmusici.h:268
#define DMUS_OBJ_OBJECT
Definition: dmusici.h:265
return ret
Definition: mutex.c:147
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint GLuint end
Definition: gl.h:1545
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLsizeiptr size
Definition: glext.h:5919
GLenum GLsizei len
Definition: glext.h:6722
REFIID riid
Definition: atlbase.h:39
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define FAILED(hr)
Definition: intsafe.h:51
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define FOURCC_RIFF
Definition: mmsystem.h:564
#define FOURCC_LIST
Definition: mmsystem.h:565
#define mmioFOURCC(c0, c1, c2, c3)
Definition: mmsystem.h:38
D3D11_SHADER_VARIABLE_DESC desc
Definition: reflection.c:1204
static int stream_read
Definition: htmldoc.c:205
#define min(a, b)
Definition: monoChain.cc:55
short WCHAR
Definition: pedump.c:58
#define REFIID
Definition: guiddef.h:118
const WCHAR * str
#define WARN_(ch,...)
Definition: debug.h:157
int zero
Definition: sehframes.cpp:29
#define TRACE(s)
Definition: solgame.cpp:4
DWORD dwValidData
Definition: dmusici.h:743
Definition: dmobject.h:28
uint16_t size
Definition: btrfs_drv.h:563
uint64_t offset
Definition: btrfs_drv.h:564
IPersistStream IPersistStream_iface
Definition: dmobject.h:71
DMUS_OBJECTDESC desc
Definition: dmobject.h:73
IUnknown * outer_unk
Definition: dmobject.h:72
IDirectMusicObject IDirectMusicObject_iface
Definition: dmobject.h:70
Definition: name.c:39
Definition: parse.h:23
uint64_t ULONGLONG
Definition: typedefs.h:67
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
#define WINAPI
Definition: msvc.h:6
#define S_FALSE
Definition: winerror.h:3451
#define E_POINTER
Definition: winerror.h:3480