ReactOS 0.4.15-dev-7704-gc07eb9f
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__
40static 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
48const 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
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",
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 */
176static inline struct dmobject *impl_from_IDirectMusicObject(IDirectMusicObject *iface)
177{
179}
180
182 void **ret_iface)
183{
185 return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface);
186}
187
189{
191 return IUnknown_AddRef(This->outer_unk);
192}
193
195{
197 return IUnknown_Release(This->outer_unk);
198}
199
202{
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{
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 */
258static 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
281static inline void unfo_get_name(IStream *stream, const struct chunk_entry *unfo,
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;
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 */
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
407void 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}
#define read
Definition: acwin.h:96
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define E_NOTIMPL
Definition: ddrawi.h:99
#define E_FAIL
Definition: ddrawi.h:102
#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(x)
Definition: debug.h:53
DWORD FOURCC
Definition: dmdls.h:25
static struct dmobject * impl_from_IPersistStream(IPersistStream *iface)
Definition: dmobject.c:342
HRESULT WINAPI dmobj_IDirectMusicObject_GetDescriptor(IDirectMusicObject *iface, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:200
void dmobject_init(struct dmobject *dmobj, const GUID *class, IUnknown *outer_unk)
Definition: dmobject.c:407
HRESULT stream_chunk_get_data(IStream *stream, const struct chunk_entry *chunk, void *data, ULONG size)
Definition: dmobject.c:143
HRESULT stream_next_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:130
static const char * debugstr_fourcc(DWORD fourcc)
Definition: dmobject.c:40
ULONG WINAPI dmobj_IPersistStream_Release(IPersistStream *iface)
Definition: dmobject.c:360
#define CHUNK_HDR_SIZE
Definition: dmobject.c:37
ULONG WINAPI dmobj_IDirectMusicObject_Release(IDirectMusicObject *iface)
Definition: dmobject.c:194
HRESULT dmobj_parsedescriptor(IStream *stream, const struct chunk_entry *riff, DMUS_OBJECTDESC *desc, DWORD supported)
Definition: dmobject.c:296
HRESULT WINAPI dmobj_IDirectMusicObject_QueryInterface(IDirectMusicObject *iface, REFIID riid, void **ret_iface)
Definition: dmobject.c:181
static void info_get_name(IStream *stream, const struct chunk_entry *info, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:258
HRESULT stream_skip_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:121
static void unfo_get_name(IStream *stream, const struct chunk_entry *unfo, DMUS_OBJECTDESC *desc, BOOL inam)
Definition: dmobject.c:281
HRESULT WINAPI unimpl_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
Definition: dmobject.c:381
HRESULT WINAPI unimpl_IPersistStream_GetSizeMax(IPersistStream *iface, ULARGE_INTEGER *size)
Definition: dmobject.c:400
HRESULT WINAPI dmobj_IDirectMusicObject_SetDescriptor(IDirectMusicObject *iface, DMUS_OBJECTDESC *desc)
Definition: dmobject.c:215
HRESULT stream_chunk_get_wstr(IStream *stream, const struct chunk_entry *chunk, WCHAR *str, ULONG size)
Definition: dmobject.c:155
const char * debugstr_chunk(const struct chunk_entry *chunk)
Definition: dmobject.c:48
HRESULT WINAPI dmobj_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
Definition: dmobject.c:366
ULONG WINAPI dmobj_IPersistStream_AddRef(IPersistStream *iface)
Definition: dmobject.c:354
HRESULT WINAPI dmobj_IPersistStream_QueryInterface(IPersistStream *iface, REFIID riid, void **ret_iface)
Definition: dmobject.c:347
ULONG WINAPI dmobj_IDirectMusicObject_AddRef(IDirectMusicObject *iface)
Definition: dmobject.c:188
static struct dmobject * impl_from_IDirectMusicObject(IDirectMusicObject *iface)
Definition: dmobject.c:176
HRESULT stream_get_chunk(IStream *stream, struct chunk_entry *chunk)
Definition: dmobject.c:76
HRESULT WINAPI unimpl_IPersistStream_IsDirty(IPersistStream *iface)
Definition: dmobject.c:387
HRESULT WINAPI unimpl_IPersistStream_Save(IPersistStream *iface, IStream *stream, BOOL clear_dirty)
Definition: dmobject.c:393
#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
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
static const WCHAR desc[]
Definition: protectdata.c:36
static int stream_read
Definition: htmldoc.c:205
#define min(a, b)
Definition: monoChain.cc:55
#define REFIID
Definition: guiddef.h:118
const WCHAR * str
#define WARN_(ch,...)
Definition: debug.h:157
int zero
Definition: sehframes.cpp:29
HRESULT hr
Definition: shlfolder.c:183
#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
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
int ret
#define WINAPI
Definition: msvc.h:6
#define S_FALSE
Definition: winerror.h:2357
#define E_POINTER
Definition: winerror.h:2365
__wchar_t WCHAR
Definition: xmlstorage.h:180