Sync with trunk.
[reactos.git] / dll / win32 / windowscodecs / propertybag.c
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2013 Ludger Sprenker
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #define WIN32_NO_STATUS
21 #define _INC_WINDOWS
22 #define COM_NO_WINDOWS_H
23
24 #include <config.h>
25
26 #include <stdarg.h>
27
28 #define COBJMACROS
29
30 #include <windef.h>
31 #include <winbase.h>
32 #include <ole2.h>
33 #include <wincodec.h>
34 #include <wine/unicode.h>
35
36 //#include "wincodecs_private.h"
37
38 #include <wine/debug.h>
39
40 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
41
42 typedef struct PropertyBag {
43 IPropertyBag2 IPropertyBag2_iface;
44 LONG ref;
45 UINT prop_count;
46 PROPBAG2 *properties;
47 VARIANT *values;
48 } PropertyBag;
49
50 static inline PropertyBag *impl_from_IPropertyBag2(IPropertyBag2 *iface)
51 {
52 return CONTAINING_RECORD(iface, PropertyBag, IPropertyBag2_iface);
53 }
54
55 static HRESULT WINAPI PropertyBag_QueryInterface(IPropertyBag2 *iface, REFIID iid,
56 void **ppv)
57 {
58 PropertyBag *This = impl_from_IPropertyBag2(iface);
59 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
60
61 if (!ppv) return E_INVALIDARG;
62
63 if (IsEqualIID(&IID_IUnknown, iid) ||
64 IsEqualIID(&IID_IPropertyBag2, iid))
65 {
66 *ppv = &This->IPropertyBag2_iface;
67 }
68 else
69 {
70 *ppv = NULL;
71 return E_NOINTERFACE;
72 }
73
74 IUnknown_AddRef((IUnknown*)*ppv);
75 return S_OK;
76 }
77
78 static ULONG WINAPI PropertyBag_AddRef(IPropertyBag2 *iface)
79 {
80 PropertyBag *This = impl_from_IPropertyBag2(iface);
81 ULONG ref = InterlockedIncrement(&This->ref);
82
83 TRACE("(%p) refcount=%u\n", iface, ref);
84
85 return ref;
86 }
87
88 static ULONG WINAPI PropertyBag_Release(IPropertyBag2 *iface)
89 {
90 PropertyBag *This = impl_from_IPropertyBag2(iface);
91 ULONG ref = InterlockedDecrement(&This->ref);
92
93 TRACE("(%p) refcount=%u\n", iface, ref);
94
95 if (ref == 0)
96 {
97 ULONG i;
98 if (This->properties && This->values)
99 {
100 for (i=0; i < This->prop_count; i++)
101 {
102 HeapFree(GetProcessHeap(), 0, This->properties[i].pstrName);
103 VariantClear( This->values+i );
104 }
105 }
106
107 HeapFree(GetProcessHeap(), 0, This->properties);
108 HeapFree(GetProcessHeap(), 0, This->values);
109 HeapFree(GetProcessHeap(), 0, This);
110 }
111
112 return ref;
113 }
114
115 static LONG find_item(PropertyBag *This, LPCOLESTR name)
116 {
117 LONG i;
118 if (!This->properties)
119 return -1;
120 if (!name)
121 return -1;
122
123 for (i=0; i < This->prop_count; i++)
124 {
125 if (strcmpW(name, This->properties[i].pstrName) == 0)
126 return i;
127 }
128
129 return -1;
130 }
131
132 static HRESULT WINAPI PropertyBag_Read(IPropertyBag2 *iface, ULONG cProperties,
133 PROPBAG2 *pPropBag, IErrorLog *pErrLog, VARIANT *pvarValue, HRESULT *phrError)
134 {
135 HRESULT res = S_OK;
136 ULONG i;
137 PropertyBag *This = impl_from_IPropertyBag2(iface);
138
139 TRACE("(%p,%u,%p,%p,%p,%p)\n", iface, cProperties, pPropBag, pErrLog, pvarValue, phrError);
140
141 for (i=0; i < cProperties; i++)
142 {
143 LONG idx;
144 if (pPropBag[i].dwHint && pPropBag[i].dwHint <= This->prop_count)
145 idx = pPropBag[i].dwHint-1;
146 else
147 idx = find_item(This, pPropBag[i].pstrName);
148
149 if (idx > -1)
150 {
151 VariantInit(pvarValue+i);
152 res = VariantCopy(pvarValue+i, This->values+idx);
153 if (FAILED(res))
154 break;
155 phrError[i] = res;
156 }
157 else
158 {
159 res = E_FAIL;
160 break;
161 }
162 }
163
164 return res;
165 }
166
167 static HRESULT WINAPI PropertyBag_Write(IPropertyBag2 *iface, ULONG cProperties,
168 PROPBAG2 *pPropBag, VARIANT *pvarValue)
169 {
170 HRESULT res = S_OK;
171 ULONG i;
172 PropertyBag *This = impl_from_IPropertyBag2(iface);
173
174 TRACE("(%p,%u,%p,%p)\n", iface, cProperties, pPropBag, pvarValue);
175
176 for (i=0; i < cProperties; i++)
177 {
178 LONG idx;
179 if (pPropBag[i].dwHint && pPropBag[i].dwHint <= This->prop_count)
180 idx = pPropBag[i].dwHint-1;
181 else
182 idx = find_item(This, pPropBag[i].pstrName);
183
184 if (idx > -1)
185 {
186 if (This->properties[idx].vt != V_VT(pvarValue+i))
187 return WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE;
188 res = VariantCopy(This->values+idx, pvarValue+i);
189 if (FAILED(res))
190 return E_FAIL;
191 }
192 else
193 {
194 if (pPropBag[i].pstrName)
195 FIXME("Application tried to set the unknown option %s.\n",
196 debugstr_w(pPropBag[i].pstrName));
197
198 /* FIXME: Function is not atomar on error, but MSDN does not say anything about it
199 * (no reset of items between 0 and i-1) */
200 return E_FAIL;
201 }
202 }
203
204 return res;
205 }
206
207 static HRESULT WINAPI PropertyBag_CountProperties(IPropertyBag2 *iface, ULONG *pcProperties)
208 {
209 PropertyBag *This = impl_from_IPropertyBag2(iface);
210
211 TRACE("(%p,%p)\n", iface, pcProperties);
212
213 if (!pcProperties)
214 return E_INVALIDARG;
215
216 *pcProperties = This->prop_count;
217
218 return S_OK;
219 }
220
221 static HRESULT copy_propbag2(PROPBAG2 *dest, PROPBAG2 *src, BOOL useCoAlloc)
222 {
223 dest->cfType = src->cfType;
224 dest->clsid = src->clsid;
225 dest->dwHint = src->dwHint;
226 dest->dwType = src->dwType;
227 dest->vt = src->vt;
228 if(useCoAlloc)
229 dest->pstrName = CoTaskMemAlloc((strlenW(src->pstrName)+1) * sizeof(WCHAR));
230 else
231 dest->pstrName = HeapAlloc(GetProcessHeap(), 0, (strlenW(src->pstrName)+1) * sizeof(WCHAR));
232
233 if(!dest->pstrName)
234 return E_OUTOFMEMORY;
235
236 strcpyW(dest->pstrName, src->pstrName);
237
238 return S_OK;
239 }
240
241 static HRESULT WINAPI PropertyBag_GetPropertyInfo(IPropertyBag2 *iface, ULONG iProperty,
242 ULONG cProperties, PROPBAG2 *pPropBag, ULONG *pcProperties)
243 {
244 HRESULT res = S_OK;
245 ULONG i;
246 PropertyBag *This = impl_from_IPropertyBag2(iface);
247
248 TRACE("(%p,%u,%u,%p,%p)\n", iface, iProperty, cProperties, pPropBag, pcProperties);
249
250 if (iProperty >= This->prop_count && iProperty > 0)
251 return WINCODEC_ERR_VALUEOUTOFRANGE;
252 if (iProperty+cProperties > This->prop_count )
253 return WINCODEC_ERR_VALUEOUTOFRANGE;
254
255 *pcProperties = max(cProperties, This->prop_count-iProperty);
256
257 for (i=0; i < *pcProperties; i++)
258 {
259 res = copy_propbag2(pPropBag+i, This->properties+iProperty+i, TRUE);
260 if (FAILED(res))
261 {
262 do {
263 CoTaskMemFree( pPropBag[--i].pstrName );
264 } while (i);
265 break;
266 }
267 }
268
269 return res;
270 }
271
272 static HRESULT WINAPI PropertyBag_LoadObject(IPropertyBag2 *iface, LPCOLESTR pstrName,
273 DWORD dwHint, IUnknown *pUnkObject, IErrorLog *pErrLog)
274 {
275 FIXME("(%p,%s,%u,%p,%p): stub\n", iface, debugstr_w(pstrName), dwHint, pUnkObject, pErrLog);
276 return E_NOTIMPL;
277 }
278
279 static const IPropertyBag2Vtbl PropertyBag_Vtbl = {
280 PropertyBag_QueryInterface,
281 PropertyBag_AddRef,
282 PropertyBag_Release,
283 PropertyBag_Read,
284 PropertyBag_Write,
285 PropertyBag_CountProperties,
286 PropertyBag_GetPropertyInfo,
287 PropertyBag_LoadObject
288 };
289
290 HRESULT CreatePropertyBag2(PROPBAG2 *options, UINT count,
291 IPropertyBag2 **ppPropertyBag2)
292 {
293 UINT i;
294 HRESULT res = S_OK;
295 PropertyBag *This;
296
297 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PropertyBag));
298 if (!This) return E_OUTOFMEMORY;
299
300 This->IPropertyBag2_iface.lpVtbl = &PropertyBag_Vtbl;
301 This->ref = 1;
302 This->prop_count = count;
303
304 if (count == 0)
305 {
306 This->properties = NULL;
307 This->values = NULL;
308 }
309 else
310 {
311 This->properties = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PROPBAG2)*count);
312 This->values = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VARIANT)*count);
313
314 if (!This->properties || !This->values)
315 res = E_OUTOFMEMORY;
316 else
317 for (i=0; i < count; i++)
318 {
319 res = copy_propbag2(This->properties+i, options+i, FALSE);
320 if (FAILED(res))
321 break;
322 This->properties[i].dwHint = i+1; /* 0 means unset, so we start with 1 */
323 }
324 }
325
326 if (FAILED(res))
327 {
328 PropertyBag_Release(&This->IPropertyBag2_iface);
329 *ppPropertyBag2 = NULL;
330 }
331 else
332 *ppPropertyBag2 = &This->IPropertyBag2_iface;
333
334 return res;
335 }