oleaut32: Add a test for loading/saving an empty picture.
[wine] / dlls / oledb32 / datainit.c
1 /*
2  * Copyright (C) 2012 Alistair Leslie-Hughes
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 #include <stdarg.h>
19
20 #define COBJMACROS
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "ole2.h"
28 #include "msdasc.h"
29 #include "oledberr.h"
30 #include "initguid.h"
31 #include "oledb_private.h"
32
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
37
38 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
39
40 typedef struct datainit
41 {
42     IDataInitialize IDataInitialize_iface;
43
44     LONG ref;
45 } datainit;
46
47 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
48 {
49     return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
50 }
51
52 typedef struct
53 {
54     IDBInitialize IDBInitialize_iface;
55     IDBProperties IDBProperties_iface;
56
57     LONG ref;
58 } dbinit;
59
60 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
61 {
62     return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
63 }
64
65 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
66 {
67     return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
68 }
69
70 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
71 {
72     dbinit *This = impl_from_IDBProperties(iface);
73
74     return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
75 }
76
77 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
78 {
79     dbinit *This = impl_from_IDBProperties(iface);
80
81     return IDBInitialize_AddRef(&This->IDBInitialize_iface);
82 }
83
84 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
85 {
86     dbinit *This = impl_from_IDBProperties(iface);
87
88     return IDBInitialize_Release(&This->IDBInitialize_iface);
89 }
90
91 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
92             const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
93 {
94     dbinit *This = impl_from_IDBProperties(iface);
95
96     FIXME("(%p)->(%d %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
97
98     return E_NOTIMPL;
99 }
100
101 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
102             const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
103             DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
104 {
105     dbinit *This = impl_from_IDBProperties(iface);
106
107     FIXME("(%p)->(%d %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
108                 prgPropertyInfoSets, ppDescBuffer);
109
110     return E_NOTIMPL;
111 }
112
113 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
114             DBPROPSET rgPropertySets[])
115 {
116     dbinit *This = impl_from_IDBProperties(iface);
117
118     FIXME("(%p)->(%d %p)\n", This, cPropertySets, rgPropertySets);
119
120     return E_NOTIMPL;
121 }
122
123 static const struct IDBPropertiesVtbl dbprops_vtbl =
124 {
125     dbprops_QueryInterface,
126     dbprops_AddRef,
127     dbprops_Release,
128     dbprops_GetProperties,
129     dbprops_GetPropertyInfo,
130     dbprops_SetProperties
131 };
132
133 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
134 {
135     dbinit *This = impl_from_IDBInitialize(iface);
136     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
137
138     *obj = NULL;
139
140     if(IsEqualIID(riid, &IID_IUnknown) ||
141        IsEqualIID(riid, &IID_IDBInitialize))
142     {
143         *obj = iface;
144     }
145     else if(IsEqualIID(riid, &IID_IDBProperties))
146     {
147         *obj = &This->IDBProperties_iface;
148     }
149     else
150     {
151         FIXME("interface %s not implemented\n", debugstr_guid(riid));
152         return E_NOINTERFACE;
153     }
154
155     IDBInitialize_AddRef(iface);
156     return S_OK;
157 }
158
159 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
160 {
161     dbinit *This = impl_from_IDBInitialize(iface);
162     TRACE("(%p)\n", This);
163
164     return InterlockedIncrement(&This->ref);
165 }
166
167 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
168 {
169     dbinit *This = impl_from_IDBInitialize(iface);
170     LONG ref;
171
172     TRACE("(%p)\n", This);
173
174     ref = InterlockedDecrement(&This->ref);
175     if(ref == 0)
176     {
177         HeapFree(GetProcessHeap(), 0, This);
178     }
179
180     return ref;
181 }
182
183 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
184 {
185     dbinit *This = impl_from_IDBInitialize(iface);
186
187     FIXME("(%p) stub\n", This);
188
189     return S_OK;
190 }
191
192 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
193 {
194     dbinit *This = impl_from_IDBInitialize(iface);
195
196     FIXME("(%p) stub\n", This);
197
198     return S_OK;
199 }
200
201 static const IDBInitializeVtbl dbinit_vtbl =
202 {
203     dbinit_QueryInterface,
204     dbinit_AddRef,
205     dbinit_Release,
206     dbinit_Initialize,
207     dbinit_Uninitialize
208 };
209
210 static HRESULT create_db_init(void **obj)
211 {
212     dbinit *This;
213
214     TRACE("()\n");
215
216     *obj = NULL;
217
218     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
219     if(!This) return E_OUTOFMEMORY;
220
221     This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
222     This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
223     This->ref = 1;
224
225     *obj = &This->IDBInitialize_iface;
226
227     return S_OK;
228 }
229
230 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
231 {
232     datainit *This = impl_from_IDataInitialize(iface);
233     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
234
235     *obj = NULL;
236
237     if(IsEqualIID(riid, &IID_IUnknown) ||
238        IsEqualIID(riid, &IID_IDataInitialize))
239     {
240         *obj = &This->IDataInitialize_iface;
241     }
242     else
243     {
244         FIXME("interface %s not implemented\n", debugstr_guid(riid));
245         return E_NOINTERFACE;
246     }
247
248     IDataInitialize_AddRef(iface);
249     return S_OK;
250 }
251
252 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
253 {
254     datainit *This = impl_from_IDataInitialize(iface);
255     TRACE("(%p)\n", This);
256
257     return InterlockedIncrement(&This->ref);
258 }
259
260 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
261 {
262     datainit *This = impl_from_IDataInitialize(iface);
263     LONG ref;
264
265     TRACE("(%p)\n", This);
266
267     ref = InterlockedDecrement(&This->ref);
268     if(ref == 0)
269     {
270         HeapFree(GetProcessHeap(), 0, This);
271     }
272
273     return ref;
274 }
275
276 /*** IDataInitialize methods ***/
277 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *pUnkOuter, DWORD dwClsCtx,
278                                 LPWSTR pwszInitializationString, REFIID riid, IUnknown **ppDataSource)
279 {
280     datainit *This = impl_from_IDataInitialize(iface);
281
282     FIXME("(%p)->(%p %d %s %s %p)\n", This, pUnkOuter, dwClsCtx, debugstr_w(pwszInitializationString),
283             debugstr_guid(riid), ppDataSource);
284
285     if(IsEqualIID(riid, &IID_IDBInitialize))
286     {
287         return create_db_init( (LPVOID*)ppDataSource);
288     }
289
290     return E_NOTIMPL;
291 }
292
293 /* returns character length of string representation */
294 static int get_propvalue_length(DBPROP *prop)
295 {
296     VARIANT str;
297     HRESULT hr;
298
299     if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
300
301     VariantInit(&str);
302     hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
303     if (hr == S_OK)
304     {
305         int len = SysStringLen(V_BSTR(&str));
306         VariantClear(&str);
307         return len;
308     }
309
310     return 0;
311 }
312
313 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
314 {
315     VARIANT *v = &prop->vValue;
316     VARIANT vstr;
317     HRESULT hr;
318
319     if (V_VT(v) == VT_BSTR)
320     {
321         strcatW(str, V_BSTR(v));
322         return;
323     }
324
325     VariantInit(&vstr);
326     hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
327     if (hr == S_OK)
328     {
329         strcatW(str, V_BSTR(&vstr));
330         VariantClear(&vstr);
331     }
332 }
333
334 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
335 {
336     int i;
337
338     for (i = 0; i < propinfoset->cPropertyInfos; i++)
339         if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
340             return propinfoset->rgPropertyInfos[i].pwszDescription;
341
342     return NULL;
343 }
344
345 static void free_dbpropset(ULONG count, DBPROPSET *propset)
346 {
347     int i;
348
349     for (i = 0; i < count; i++)
350     {
351         int p;
352
353         for (p = 0; p < propset[i].cProperties; p++)
354         {
355             VariantClear(&propset[i].rgProperties[p].vValue);
356             CoTaskMemFree(&propset[i].rgProperties[p]);
357         }
358         CoTaskMemFree(&propset[i]);
359     }
360 }
361
362 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
363 {
364     int i;
365
366     for (i = 0; i < count; i++)
367     {
368         int p;
369
370         for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
371         {
372             VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
373             CoTaskMemFree(&propinfoset[i].rgPropertyInfos[p]);
374         }
375         CoTaskMemFree(&propinfoset[i]);
376     }
377 }
378
379 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
380                                 boolean include_pass, LPWSTR *init_string)
381 {
382     static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
383     static const WCHAR colW[] = {';',0};
384     datainit *This = impl_from_IDataInitialize(iface);
385     DBPROPINFOSET *propinfoset;
386     IDBProperties *props;
387     DBPROPIDSET propidset;
388     ULONG count, infocount;
389     WCHAR *progid, *desc;
390     DBPROPSET *propset;
391     IPersist *persist;
392     HRESULT hr;
393     CLSID clsid;
394     int i, len;
395
396     TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
397
398     /* IPersist support is mandatory for data sources */
399     hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
400     if (FAILED(hr)) return hr;
401
402     memset(&clsid, 0, sizeof(clsid));
403     hr = IPersist_GetClassID(persist, &clsid);
404     IPersist_Release(persist);
405     if (FAILED(hr)) return hr;
406
407     progid = NULL;
408     ProgIDFromCLSID(&clsid, &progid);
409     TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
410
411     /* now get initialization properties */
412     hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
413     if (FAILED(hr))
414     {
415         WARN("IDBProperties not supported\n");
416         CoTaskMemFree(progid);
417         return hr;
418     }
419
420     propidset.rgPropertyIDs = NULL;
421     propidset.cPropertyIDs = 0;
422     propidset.guidPropertySet = DBPROPSET_DBINIT;
423     propset = NULL;
424     count = 0;
425     hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
426     if (FAILED(hr))
427     {
428         WARN("failed to get data source properties, 0x%08x\n", hr);
429         CoTaskMemFree(progid);
430         return hr;
431     }
432
433     infocount = 0;
434     IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
435     IDBProperties_Release(props);
436
437     /* check if we need to skip password */
438     len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
439     for (i = 0; i < count; i++)
440     {
441         WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
442         if (descr)
443         {
444             /* include '=' and ';' */
445             len += strlenW(descr) + 2;
446             len += get_propvalue_length(&propset->rgProperties[i]);
447         }
448
449         if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
450             (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
451            include_pass = FALSE;
452     }
453
454     len *= sizeof(WCHAR);
455     *init_string = CoTaskMemAlloc(len);
456     *init_string[0] = 0;
457
458     /* provider name */
459     strcatW(*init_string, provW);
460     strcatW(*init_string, progid);
461     strcatW(*init_string, colW);
462     CoTaskMemFree(progid);
463
464     for (i = 0; i < count; i++)
465     {
466         WCHAR *descr;
467
468         if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
469
470         descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
471         if (descr)
472         {
473             static const WCHAR eqW[] = {'=',0};
474             strcatW(*init_string, descr);
475             strcatW(*init_string, eqW);
476             write_propvalue_str(*init_string, &propset->rgProperties[i]);
477             strcatW(*init_string, colW);
478         }
479     }
480
481     free_dbpropset(count, propset);
482     free_dbpropinfoset(infocount, propinfoset);
483     CoTaskMemFree(desc);
484
485     if (!include_pass)
486         TRACE("%s\n", debugstr_w(*init_string));
487     return S_OK;
488 }
489
490 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
491                                 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
492                                 IUnknown **datasource)
493 {
494     datainit *This = impl_from_IDataInitialize(iface);
495
496     TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
497         debugstr_guid(riid), datasource);
498
499     return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
500 }
501
502 static HRESULT WINAPI datainit_RemoteCreateDBInstanceEx(IDataInitialize *iface, REFCLSID clsidProvider,
503                                 IUnknown *pUnkOuter, DWORD dwClsCtx, LPWSTR pwszReserved, COSERVERINFO *pServerInfo,
504                                 DWORD cmq, GUID **rgpIID, IUnknown **rgpItf, HRESULT *rghr)
505 {
506     datainit *This = impl_from_IDataInitialize(iface);
507
508     FIXME("(%p)->()\n", This);
509
510     return E_NOTIMPL;
511 }
512
513 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
514                                 LPWSTR *ppwszInitializationString)
515 {
516     datainit *This = impl_from_IDataInitialize(iface);
517
518     FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
519
520     return E_NOTIMPL;
521 }
522
523 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
524                                 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
525 {
526     datainit *This = impl_from_IDataInitialize(iface);
527
528     FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
529
530     return E_NOTIMPL;
531 }
532
533
534 static const struct IDataInitializeVtbl datainit_vtbl =
535 {
536     datainit_QueryInterface,
537     datainit_AddRef,
538     datainit_Release,
539     datainit_GetDataSource,
540     datainit_GetInitializationString,
541     datainit_CreateDBInstance,
542     datainit_RemoteCreateDBInstanceEx,
543     datainit_LoadStringFromStorage,
544     datainit_WriteStringToStorage
545 };
546
547 HRESULT create_data_init(IUnknown *outer, void **obj)
548 {
549     datainit *This;
550
551     TRACE("(%p)\n", obj);
552
553     if(outer) return CLASS_E_NOAGGREGATION;
554
555     *obj = NULL;
556
557     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
558     if(!This) return E_OUTOFMEMORY;
559
560     This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
561     This->ref = 1;
562
563     *obj = &This->IDataInitialize_iface;
564
565     return S_OK;
566 }