xref: /reactos/dll/directx/wine/dmusic/dmobject.c (revision 4561998a)
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 
33 WINE_DEFAULT_DEBUG_CHANNEL(dmobj);
34 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
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 
59 static HRESULT stream_read(IStream *stream, void *data, ULONG size)
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 
76 HRESULT stream_get_chunk(IStream *stream, struct chunk_entry *chunk)
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",
93                     wine_dbgstr_longlong(ck_end), wine_dbgstr_longlong(p_end));
94             return E_FAIL;
95         }
96     }
97 
98     hr = stream_read(stream, chunk, CHUNK_HDR_SIZE);
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",
105                     wine_dbgstr_longlong(ck_end), wine_dbgstr_longlong(p_end));
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 
121 HRESULT stream_skip_chunk(IStream *stream, struct chunk_entry *chunk)
122 {
123     LARGE_INTEGER end;
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 
130 HRESULT stream_next_chunk(IStream *stream, struct chunk_entry *chunk)
131 {
132     HRESULT hr;
133 
134     if (chunk->id) {
135         hr = stream_skip_chunk(stream, chunk);
136         if (FAILED(hr))
137             return hr;
138     }
139 
140     return stream_get_chunk(stream, chunk);
141 }
142 
143 HRESULT stream_chunk_get_data(IStream *stream, const struct chunk_entry *chunk, void *data,
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",
148                 debugstr_fourcc(chunk->id), chunk->size,
149                 wine_dbgstr_longlong(chunk->offset.QuadPart), size);
150         return E_FAIL;
151     }
152     return stream_read(stream, data, size);
153 }
154 
155 HRESULT stream_chunk_get_wstr(IStream *stream, const struct chunk_entry *chunk, WCHAR *str,
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 
181 HRESULT WINAPI dmobj_IDirectMusicObject_QueryInterface(IDirectMusicObject *iface, REFIID riid,
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 
194 ULONG WINAPI dmobj_IDirectMusicObject_Release(IDirectMusicObject *iface)
195 {
196     struct dmobject *This = impl_from_IDirectMusicObject(iface);
197     return IUnknown_Release(This->outer_unk);
198 }
199 
200 HRESULT WINAPI dmobj_IDirectMusicObject_GetDescriptor(IDirectMusicObject *iface,
201         DMUS_OBJECTDESC *desc)
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 
215 HRESULT WINAPI dmobj_IDirectMusicObject_SetDescriptor(IDirectMusicObject *iface,
216         DMUS_OBJECTDESC *desc)
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,
259         DMUS_OBJECTDESC *desc)
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 
296 HRESULT dmobj_parsedescriptor(IStream *stream, const struct chunk_entry *riff,
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) {
313             case DMUS_FOURCC_GUID_CHUNK:
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;
318             case DMUS_FOURCC_CATEGORY_CHUNK:
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;
323             case DMUS_FOURCC_VERSION_CHUNK:
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))
330                     unfo_get_name(stream, &chunk, desc, supported & DMUS_OBJ_NAME_INAM);
331                 else if (chunk.type == DMUS_FOURCC_INFO_LIST && (supported & DMUS_OBJ_NAME_INFO))
332                     info_get_name(stream, &chunk, desc);
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 
347 HRESULT WINAPI dmobj_IPersistStream_QueryInterface(IPersistStream *iface, REFIID riid,
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 
354 ULONG WINAPI dmobj_IPersistStream_AddRef(IPersistStream *iface)
355 {
356     struct dmobject *This = impl_from_IPersistStream(iface);
357     return IUnknown_AddRef(This->outer_unk);
358 }
359 
360 ULONG WINAPI dmobj_IPersistStream_Release(IPersistStream *iface)
361 {
362     struct dmobject *This = impl_from_IPersistStream(iface);
363     return IUnknown_Release(This->outer_unk);
364 }
365 
366 HRESULT WINAPI dmobj_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
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 */
381 HRESULT WINAPI unimpl_IPersistStream_GetClassID(IPersistStream *iface, CLSID *class)
382 {
383     TRACE("(%p, %p): method not implemented\n", iface, class);
384     return E_NOTIMPL;
385 }
386 
387 HRESULT WINAPI unimpl_IPersistStream_IsDirty(IPersistStream *iface)
388 {
389     TRACE("(%p): method not implemented, always returning S_FALSE\n", iface);
390     return S_FALSE;
391 }
392 
393 HRESULT WINAPI unimpl_IPersistStream_Save(IPersistStream *iface, IStream *stream,
394         BOOL clear_dirty)
395 {
396     TRACE("(%p, %p, %d): method not implemented\n", iface, stream, clear_dirty);
397     return E_NOTIMPL;
398 }
399 
400 HRESULT WINAPI unimpl_IPersistStream_GetSizeMax(IPersistStream *iface, ULARGE_INTEGER *size)
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);
411     dmobj->desc.dwValidData = DMUS_OBJ_CLASS;
412     dmobj->desc.guidClass = *class;
413 }
414