dxdiagn: Fix string copy behavior with an excessively short buffer in IDxDiagContaine...
[wine/multimedia.git] / dlls / dxdiagn / container.c
blobd1c023fa0c95bb94fde2e0cce572b3ff1adff17d
1 /*
2 * IDxDiagContainer Implementation
3 *
4 * Copyright 2004 Raphael Junqueira
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #define COBJMACROS
25 #include "dxdiag_private.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);
31 /* IDxDiagContainer IUnknown parts follow: */
32 HRESULT WINAPI IDxDiagContainerImpl_QueryInterface(PDXDIAGCONTAINER iface, REFIID riid, LPVOID *ppobj)
34 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
36 if (!ppobj) return E_INVALIDARG;
38 if (IsEqualGUID(riid, &IID_IUnknown)
39 || IsEqualGUID(riid, &IID_IDxDiagContainer)) {
40 IUnknown_AddRef(iface);
41 *ppobj = This;
42 return S_OK;
45 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
46 *ppobj = NULL;
47 return E_NOINTERFACE;
50 static ULONG WINAPI IDxDiagContainerImpl_AddRef(PDXDIAGCONTAINER iface) {
51 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
52 ULONG refCount = InterlockedIncrement(&This->ref);
54 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
56 DXDIAGN_LockModule();
58 return refCount;
61 static ULONG WINAPI IDxDiagContainerImpl_Release(PDXDIAGCONTAINER iface) {
62 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
63 ULONG refCount = InterlockedDecrement(&This->ref);
65 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
67 if (!refCount) {
68 HeapFree(GetProcessHeap(), 0, This);
71 DXDIAGN_UnlockModule();
73 return refCount;
76 /* IDxDiagContainer Interface follow: */
77 static HRESULT WINAPI IDxDiagContainerImpl_GetNumberOfChildContainers(PDXDIAGCONTAINER iface, DWORD* pdwCount) {
78 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
79 TRACE("(%p)\n", iface);
80 if (NULL == pdwCount) {
81 return E_INVALIDARG;
83 *pdwCount = This->nSubContainers;
84 return S_OK;
87 static HRESULT WINAPI IDxDiagContainerImpl_EnumChildContainerNames(PDXDIAGCONTAINER iface, DWORD dwIndex, LPWSTR pwszContainer, DWORD cchContainer) {
88 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
89 IDxDiagContainerImpl_SubContainer* p = NULL;
90 DWORD i = 0;
92 TRACE("(%p, %u, %p, %u)\n", iface, dwIndex, pwszContainer, cchContainer);
94 if (NULL == pwszContainer || 0 == cchContainer) {
95 return E_INVALIDARG;
98 p = This->subContainers;
99 while (NULL != p) {
100 if (dwIndex == i) {
101 TRACE("Found container name %s, copying string\n", debugstr_w(p->contName));
102 lstrcpynW(pwszContainer, p->contName, cchContainer);
103 return (cchContainer <= strlenW(p->contName)) ?
104 DXDIAG_E_INSUFFICIENT_BUFFER : S_OK;
106 p = p->next;
107 ++i;
110 TRACE("Failed to find container name at specified index\n");
111 *pwszContainer = '\0';
112 return E_INVALIDARG;
115 static HRESULT IDxDiagContainerImpl_GetChildContainerInternal(PDXDIAGCONTAINER iface, LPCWSTR pwszContainer, IDxDiagContainer** ppInstance) {
116 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
117 IDxDiagContainerImpl_SubContainer* p = NULL;
119 p = This->subContainers;
120 while (NULL != p) {
121 if (0 == lstrcmpW(p->contName, pwszContainer)) {
122 *ppInstance = p->pCont;
123 return S_OK;
125 p = p->next;
127 return E_INVALIDARG;
130 static HRESULT WINAPI IDxDiagContainerImpl_GetChildContainer(PDXDIAGCONTAINER iface, LPCWSTR pwszContainer, IDxDiagContainer** ppInstance) {
131 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
132 IDxDiagContainer* pContainer = NULL;
133 LPWSTR tmp, orig_tmp;
134 INT tmp_len;
135 WCHAR* cur;
136 HRESULT hr = E_INVALIDARG;
138 TRACE("(%p, %s, %p)\n", iface, debugstr_w(pwszContainer), ppInstance);
140 if (NULL == ppInstance || NULL == pwszContainer) {
141 return E_INVALIDARG;
144 pContainer = (PDXDIAGCONTAINER) This;
146 tmp_len = strlenW(pwszContainer) + 1;
147 orig_tmp = tmp = HeapAlloc(GetProcessHeap(), 0, tmp_len * sizeof(WCHAR));
148 if (NULL == tmp) return E_FAIL;
149 lstrcpynW(tmp, pwszContainer, tmp_len);
151 cur = strchrW(tmp, '.');
152 while (NULL != cur) {
153 *cur = '\0'; /* cut tmp string to '.' */
154 hr = IDxDiagContainerImpl_GetChildContainerInternal(pContainer, tmp, &pContainer);
155 if (FAILED(hr) || NULL == pContainer)
156 goto on_error;
157 cur++; /* go after '.' (just replaced by \0) */
158 tmp = cur;
159 cur = strchrW(tmp, '.');
162 hr = IDxDiagContainerImpl_GetChildContainerInternal(pContainer, tmp, ppInstance);
163 if (SUCCEEDED(hr)) {
164 IDxDiagContainerImpl_AddRef(*ppInstance);
167 on_error:
168 HeapFree(GetProcessHeap(), 0, orig_tmp);
169 return hr;
172 static HRESULT WINAPI IDxDiagContainerImpl_GetNumberOfProps(PDXDIAGCONTAINER iface, DWORD* pdwCount) {
173 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
174 TRACE("(%p)\n", iface);
175 if (NULL == pdwCount) {
176 return E_INVALIDARG;
178 *pdwCount = This->nProperties;
179 return S_OK;
182 static HRESULT WINAPI IDxDiagContainerImpl_EnumPropNames(PDXDIAGCONTAINER iface, DWORD dwIndex, LPWSTR pwszPropName, DWORD cchPropName) {
183 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
184 IDxDiagContainerImpl_Property* p = NULL;
185 DWORD i = 0;
187 TRACE("(%p, %u, %s, %u)\n", iface, dwIndex, debugstr_w(pwszPropName), cchPropName);
189 if (NULL == pwszPropName) {
190 return E_INVALIDARG;
192 if (256 > cchPropName) {
193 return DXDIAG_E_INSUFFICIENT_BUFFER;
196 p = This->properties;
197 while (NULL != p) {
198 if (dwIndex == i) {
199 if (cchPropName <= strlenW(p->vName)) {
200 return DXDIAG_E_INSUFFICIENT_BUFFER;
202 lstrcpynW(pwszPropName, p->vName, cchPropName);
203 return S_OK;
205 p = p->next;
206 ++i;
208 return E_INVALIDARG;
211 static HRESULT WINAPI IDxDiagContainerImpl_GetProp(PDXDIAGCONTAINER iface, LPCWSTR pwszPropName, VARIANT* pvarProp) {
212 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
213 IDxDiagContainerImpl_Property* p = NULL;
215 TRACE("(%p, %s, %p)\n", iface, debugstr_w(pwszPropName), pvarProp);
217 if (NULL == pvarProp || NULL == pwszPropName) {
218 return E_INVALIDARG;
221 p = This->properties;
222 while (NULL != p) {
223 if (0 == lstrcmpW(p->vName, pwszPropName)) {
224 VariantCopy(pvarProp, &p->v);
225 return S_OK;
227 p = p->next;
229 return S_OK;
232 HRESULT WINAPI IDxDiagContainerImpl_AddProp(PDXDIAGCONTAINER iface, LPCWSTR pwszPropName, VARIANT* pVarProp) {
233 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
234 IDxDiagContainerImpl_Property* p = NULL;
235 IDxDiagContainerImpl_Property* pNew = NULL;
237 TRACE("(%p, %s, %p)\n", iface, debugstr_w(pwszPropName), pVarProp);
239 if (NULL == pVarProp || NULL == pwszPropName) {
240 return E_INVALIDARG;
243 pNew = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagContainerImpl_Property));
244 if (NULL == pNew) {
245 return E_OUTOFMEMORY;
247 VariantInit(&pNew->v);
248 VariantCopy(&pNew->v, pVarProp);
249 pNew->vName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pwszPropName) + 1) * sizeof(WCHAR));
250 lstrcpyW(pNew->vName, pwszPropName);
251 pNew->next = NULL;
253 p = This->properties;
254 if (NULL == p) {
255 This->properties = pNew;
256 } else {
257 while (NULL != p->next) {
258 p = p->next;
260 p->next = pNew;
262 ++This->nProperties;
263 return S_OK;
266 HRESULT WINAPI IDxDiagContainerImpl_AddChildContainer(PDXDIAGCONTAINER iface, LPCWSTR pszContName, PDXDIAGCONTAINER pSubCont) {
267 IDxDiagContainerImpl *This = (IDxDiagContainerImpl *)iface;
268 IDxDiagContainerImpl_SubContainer* p = NULL;
269 IDxDiagContainerImpl_SubContainer* pNew = NULL;
271 TRACE("(%p, %s, %p)\n", iface, debugstr_w(pszContName), pSubCont);
273 if (NULL == pSubCont || NULL == pszContName) {
274 return E_INVALIDARG;
277 pNew = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagContainerImpl_SubContainer));
278 if (NULL == pNew) {
279 return E_OUTOFMEMORY;
281 pNew->pCont = pSubCont;
282 pNew->contName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlenW(pszContName) + 1) * sizeof(WCHAR));
283 lstrcpyW(pNew->contName, pszContName);
284 pNew->next = NULL;
286 p = This->subContainers;
287 if (NULL == p) {
288 This->subContainers = pNew;
289 } else {
290 while (NULL != p->next) {
291 p = p->next;
293 p->next = pNew;
295 ++This->nSubContainers;
296 return S_OK;
299 static const IDxDiagContainerVtbl DxDiagContainer_Vtbl =
301 IDxDiagContainerImpl_QueryInterface,
302 IDxDiagContainerImpl_AddRef,
303 IDxDiagContainerImpl_Release,
304 IDxDiagContainerImpl_GetNumberOfChildContainers,
305 IDxDiagContainerImpl_EnumChildContainerNames,
306 IDxDiagContainerImpl_GetChildContainer,
307 IDxDiagContainerImpl_GetNumberOfProps,
308 IDxDiagContainerImpl_EnumPropNames,
309 IDxDiagContainerImpl_GetProp
313 HRESULT DXDiag_CreateDXDiagContainer(REFIID riid, LPVOID *ppobj) {
314 IDxDiagContainerImpl* container;
316 TRACE("(%p, %p)\n", debugstr_guid(riid), ppobj);
318 container = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDxDiagContainerImpl));
319 if (NULL == container) {
320 *ppobj = NULL;
321 return E_OUTOFMEMORY;
323 container->lpVtbl = &DxDiagContainer_Vtbl;
324 container->ref = 0; /* will be inited with QueryInterface */
325 return IDxDiagContainerImpl_QueryInterface((PDXDIAGCONTAINER)container, riid, ppobj);