oledb32: Avoid leaking propsets on error paths (Coverity).
[wine/testsucceed.git] / dlls / oledb32 / datainit.c
bloba870bbc09ca8e6d66fefd365341b85aeebe47f80
1 /*
2 * Copyright (C) 2012 Alistair Leslie-Hughes
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.
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.
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
18 #include <stdarg.h>
20 #define COBJMACROS
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
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"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
38 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
40 typedef struct datainit
42 IDataInitialize IDataInitialize_iface;
44 LONG ref;
45 } datainit;
47 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
49 return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
52 typedef struct
54 IDBInitialize IDBInitialize_iface;
55 IDBProperties IDBProperties_iface;
57 LONG ref;
58 } dbinit;
60 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
62 return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
65 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
67 return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
70 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
72 dbinit *This = impl_from_IDBProperties(iface);
74 return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
77 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
79 dbinit *This = impl_from_IDBProperties(iface);
81 return IDBInitialize_AddRef(&This->IDBInitialize_iface);
84 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
86 dbinit *This = impl_from_IDBProperties(iface);
88 return IDBInitialize_Release(&This->IDBInitialize_iface);
91 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
92 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
94 dbinit *This = impl_from_IDBProperties(iface);
96 FIXME("(%p)->(%d %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
98 return E_NOTIMPL;
101 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
102 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
103 DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
105 dbinit *This = impl_from_IDBProperties(iface);
107 FIXME("(%p)->(%d %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
108 prgPropertyInfoSets, ppDescBuffer);
110 return E_NOTIMPL;
113 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
114 DBPROPSET rgPropertySets[])
116 dbinit *This = impl_from_IDBProperties(iface);
118 FIXME("(%p)->(%d %p)\n", This, cPropertySets, rgPropertySets);
120 return E_NOTIMPL;
123 static const struct IDBPropertiesVtbl dbprops_vtbl =
125 dbprops_QueryInterface,
126 dbprops_AddRef,
127 dbprops_Release,
128 dbprops_GetProperties,
129 dbprops_GetPropertyInfo,
130 dbprops_SetProperties
133 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
135 dbinit *This = impl_from_IDBInitialize(iface);
136 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
138 *obj = NULL;
140 if(IsEqualIID(riid, &IID_IUnknown) ||
141 IsEqualIID(riid, &IID_IDBInitialize))
143 *obj = iface;
145 else if(IsEqualIID(riid, &IID_IDBProperties))
147 *obj = &This->IDBProperties_iface;
149 else
151 FIXME("interface %s not implemented\n", debugstr_guid(riid));
152 return E_NOINTERFACE;
155 IDBInitialize_AddRef(iface);
156 return S_OK;
159 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
161 dbinit *This = impl_from_IDBInitialize(iface);
162 TRACE("(%p)\n", This);
164 return InterlockedIncrement(&This->ref);
167 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
169 dbinit *This = impl_from_IDBInitialize(iface);
170 LONG ref;
172 TRACE("(%p)\n", This);
174 ref = InterlockedDecrement(&This->ref);
175 if(ref == 0)
177 HeapFree(GetProcessHeap(), 0, This);
180 return ref;
183 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
185 dbinit *This = impl_from_IDBInitialize(iface);
187 FIXME("(%p) stub\n", This);
189 return S_OK;
192 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
194 dbinit *This = impl_from_IDBInitialize(iface);
196 FIXME("(%p) stub\n", This);
198 return S_OK;
201 static const IDBInitializeVtbl dbinit_vtbl =
203 dbinit_QueryInterface,
204 dbinit_AddRef,
205 dbinit_Release,
206 dbinit_Initialize,
207 dbinit_Uninitialize
210 static HRESULT create_db_init(void **obj)
212 dbinit *This;
214 TRACE("()\n");
216 *obj = NULL;
218 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
219 if(!This) return E_OUTOFMEMORY;
221 This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
222 This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
223 This->ref = 1;
225 *obj = &This->IDBInitialize_iface;
227 return S_OK;
230 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
232 datainit *This = impl_from_IDataInitialize(iface);
233 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
235 *obj = NULL;
237 if(IsEqualIID(riid, &IID_IUnknown) ||
238 IsEqualIID(riid, &IID_IDataInitialize))
240 *obj = &This->IDataInitialize_iface;
242 else
244 FIXME("interface %s not implemented\n", debugstr_guid(riid));
245 return E_NOINTERFACE;
248 IDataInitialize_AddRef(iface);
249 return S_OK;
252 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
254 datainit *This = impl_from_IDataInitialize(iface);
255 TRACE("(%p)\n", This);
257 return InterlockedIncrement(&This->ref);
260 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
262 datainit *This = impl_from_IDataInitialize(iface);
263 LONG ref;
265 TRACE("(%p)\n", This);
267 ref = InterlockedDecrement(&This->ref);
268 if(ref == 0)
270 HeapFree(GetProcessHeap(), 0, This);
273 return ref;
276 static void free_dbpropset(ULONG count, DBPROPSET *propset)
278 int i;
280 for (i = 0; i < count; i++)
282 int p;
284 for (p = 0; p < propset[i].cProperties; p++)
285 VariantClear(&propset[i].rgProperties[p].vValue);
287 CoTaskMemFree(propset[i].rgProperties);
289 CoTaskMemFree(propset);
292 static HRESULT set_dbpropset(BSTR name, BSTR value, DBPROPSET **propset)
294 static const WCHAR datasourceW[] = {'D','a','t','a',' ','S','o','u','r','c','e',0};
296 if (!strcmpW(datasourceW, name))
298 *propset = CoTaskMemAlloc(sizeof(DBPROPSET));
299 (*propset)->rgProperties = CoTaskMemAlloc(sizeof(DBPROP));
300 (*propset)->cProperties = 1;
301 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
302 (*propset)->rgProperties[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
303 (*propset)->rgProperties[0].dwOptions = DBPROPOPTIONS_REQUIRED;
304 (*propset)->rgProperties[0].dwStatus = 0;
305 memset(&(*propset)->rgProperties[0].colid, 0, sizeof(DBID));
306 V_VT(&(*propset)->rgProperties[0].vValue) = VT_BSTR;
307 V_BSTR(&(*propset)->rgProperties[0].vValue) = SysAllocString(value);
308 return S_OK;
310 else
312 *propset = NULL;
313 FIXME("unsupported property %s\n", debugstr_w(name));
314 return E_FAIL;
318 /*** IDataInitialize methods ***/
319 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
320 LPWSTR initstring, REFIID riid, IUnknown **datasource)
322 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r','=',0};
323 static const WCHAR msdasqlW[] = {'M','S','D','A','S','Q','L',0};
324 datainit *This = impl_from_IDataInitialize(iface);
325 IDBProperties *dbprops;
326 DBPROPSET *propset;
327 WCHAR *prov = NULL;
328 CLSID provclsid;
329 HRESULT hr;
331 TRACE("(%p)->(%p 0x%x %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
333 /* first get provider name */
334 provclsid = IID_NULL;
335 if (initstring && (prov = strstrW(initstring, providerW)))
337 WCHAR *start, *progid;
338 int len;
340 prov += sizeof(providerW)/sizeof(WCHAR)-1;
341 start = prov;
342 while (*prov && *prov != ';')
343 ++prov;
344 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
346 len = prov - start;
347 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
348 if (!progid) return E_OUTOFMEMORY;
350 memcpy(progid, start, len*sizeof(WCHAR));
351 progid[len] = 0;
353 hr = CLSIDFromProgID(progid, &provclsid);
354 CoTaskMemFree(progid);
355 if (FAILED(hr))
357 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
358 return hr;
361 else
363 hr = CLSIDFromProgID(msdasqlW, &provclsid);
364 if (FAILED(hr))
365 ERR("ODBC provider for OLE DB not registered\n");
368 /* check for provider mismatch if it was specified in init string */
369 if (*datasource && prov)
371 DBPROPIDSET propidset;
372 enum DBPROPENUM prop;
373 CLSID initprov;
374 ULONG count;
376 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
377 if (FAILED(hr))
379 WARN("provider doesn't support IDBProperties\n");
380 return hr;
383 prop = DBPROP_INIT_DATASOURCE;
384 propidset.rgPropertyIDs = &prop;
385 propidset.cPropertyIDs = 1;
386 propidset.guidPropertySet = DBPROPSET_DBINIT;
387 count = 0;
388 propset = NULL;
389 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
390 IDBProperties_Release(dbprops);
391 if (FAILED(hr))
393 WARN("GetProperties failed for datasource, 0x%08x\n", hr);
394 return hr;
397 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
398 initprov = IID_NULL;
399 CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
400 free_dbpropset(count, propset);
402 if (!IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
405 if (!*datasource)
407 if (!IsEqualIID(&provclsid, &IID_NULL))
408 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
410 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
411 hr = create_db_init((void**)datasource);
414 /* now set properties */
415 if (initstring)
417 static const WCHAR scolW[] = {';',0};
418 static const WCHAR eqW[] = {'=',0};
419 WCHAR *eq, *start;
421 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
422 if (FAILED(hr))
424 WARN("provider doesn't support IDBProperties\n");
425 return hr;
428 start = initstring;
429 while ((eq = strstrW(start, eqW)))
431 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r',0};
432 WCHAR *scol = strstrW(eq+1, scolW);
433 BSTR value, name;
435 name = SysAllocStringLen(start, eq - start);
436 /* skip equal sign to get value */
437 eq++;
438 value = SysAllocStringLen(eq, scol ? scol - eq : -1);
440 /* skip semicolon if present */
441 if (scol) scol++;
442 start = scol;
444 if (!strcmpW(name, providerW))
446 SysFreeString(name);
447 SysFreeString(value);
448 continue;
451 TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
453 hr = set_dbpropset(name, value, &propset);
454 SysFreeString(name);
455 SysFreeString(value);
456 if (FAILED(hr)) return hr;
458 hr = IDBProperties_SetProperties(dbprops, 1, propset);
459 free_dbpropset(1, propset);
460 TRACE("provider ret 0x%08x\n", hr);
463 IDBProperties_Release(dbprops);
466 return hr;
469 /* returns character length of string representation */
470 static int get_propvalue_length(DBPROP *prop)
472 VARIANT str;
473 HRESULT hr;
475 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
477 VariantInit(&str);
478 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
479 if (hr == S_OK)
481 int len = SysStringLen(V_BSTR(&str));
482 VariantClear(&str);
483 return len;
486 return 0;
489 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
491 VARIANT *v = &prop->vValue;
492 VARIANT vstr;
493 HRESULT hr;
495 if (V_VT(v) == VT_BSTR)
497 strcatW(str, V_BSTR(v));
498 return;
501 VariantInit(&vstr);
502 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
503 if (hr == S_OK)
505 strcatW(str, V_BSTR(&vstr));
506 VariantClear(&vstr);
510 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
512 int i;
514 for (i = 0; i < propinfoset->cPropertyInfos; i++)
515 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
516 return propinfoset->rgPropertyInfos[i].pwszDescription;
518 return NULL;
521 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
523 int i;
525 for (i = 0; i < count; i++)
527 int p;
529 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
530 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
532 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
534 CoTaskMemFree(propinfoset);
537 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
538 boolean include_pass, LPWSTR *init_string)
540 static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
541 static const WCHAR colW[] = {';',0};
542 datainit *This = impl_from_IDataInitialize(iface);
543 DBPROPINFOSET *propinfoset;
544 IDBProperties *props;
545 DBPROPIDSET propidset;
546 ULONG count, infocount;
547 WCHAR *progid, *desc;
548 DBPROPSET *propset;
549 IPersist *persist;
550 HRESULT hr;
551 CLSID clsid;
552 int i, len;
554 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
556 /* IPersist support is mandatory for data sources */
557 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
558 if (FAILED(hr)) return hr;
560 memset(&clsid, 0, sizeof(clsid));
561 hr = IPersist_GetClassID(persist, &clsid);
562 IPersist_Release(persist);
563 if (FAILED(hr)) return hr;
565 progid = NULL;
566 ProgIDFromCLSID(&clsid, &progid);
567 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
569 /* now get initialization properties */
570 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
571 if (FAILED(hr))
573 WARN("IDBProperties not supported\n");
574 CoTaskMemFree(progid);
575 return hr;
578 propidset.rgPropertyIDs = NULL;
579 propidset.cPropertyIDs = 0;
580 propidset.guidPropertySet = DBPROPSET_DBINIT;
581 propset = NULL;
582 count = 0;
583 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
584 if (FAILED(hr))
586 WARN("failed to get data source properties, 0x%08x\n", hr);
587 CoTaskMemFree(progid);
588 return hr;
591 infocount = 0;
592 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
593 IDBProperties_Release(props);
595 /* check if we need to skip password */
596 len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
597 for (i = 0; i < count; i++)
599 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
600 if (descr)
602 /* include '=' and ';' */
603 len += strlenW(descr) + 2;
604 len += get_propvalue_length(&propset->rgProperties[i]);
607 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
608 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
609 include_pass = FALSE;
612 len *= sizeof(WCHAR);
613 *init_string = CoTaskMemAlloc(len);
614 *init_string[0] = 0;
616 /* provider name */
617 strcatW(*init_string, provW);
618 strcatW(*init_string, progid);
619 strcatW(*init_string, colW);
620 CoTaskMemFree(progid);
622 for (i = 0; i < count; i++)
624 WCHAR *descr;
626 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
628 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
629 if (descr)
631 static const WCHAR eqW[] = {'=',0};
632 strcatW(*init_string, descr);
633 strcatW(*init_string, eqW);
634 write_propvalue_str(*init_string, &propset->rgProperties[i]);
635 strcatW(*init_string, colW);
639 free_dbpropset(count, propset);
640 free_dbpropinfoset(infocount, propinfoset);
641 CoTaskMemFree(desc);
643 if (!include_pass)
644 TRACE("%s\n", debugstr_w(*init_string));
645 return S_OK;
648 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
649 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
650 IUnknown **datasource)
652 datainit *This = impl_from_IDataInitialize(iface);
654 TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
655 debugstr_guid(riid), datasource);
657 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
660 static HRESULT WINAPI datainit_RemoteCreateDBInstanceEx(IDataInitialize *iface, REFCLSID clsidProvider,
661 IUnknown *pUnkOuter, DWORD dwClsCtx, LPWSTR pwszReserved, COSERVERINFO *pServerInfo,
662 DWORD cmq, GUID **rgpIID, IUnknown **rgpItf, HRESULT *rghr)
664 datainit *This = impl_from_IDataInitialize(iface);
666 FIXME("(%p)->()\n", This);
668 return E_NOTIMPL;
671 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
672 LPWSTR *ppwszInitializationString)
674 datainit *This = impl_from_IDataInitialize(iface);
676 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
678 return E_NOTIMPL;
681 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
682 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
684 datainit *This = impl_from_IDataInitialize(iface);
686 FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
688 return E_NOTIMPL;
692 static const struct IDataInitializeVtbl datainit_vtbl =
694 datainit_QueryInterface,
695 datainit_AddRef,
696 datainit_Release,
697 datainit_GetDataSource,
698 datainit_GetInitializationString,
699 datainit_CreateDBInstance,
700 datainit_RemoteCreateDBInstanceEx,
701 datainit_LoadStringFromStorage,
702 datainit_WriteStringToStorage
705 HRESULT create_data_init(IUnknown *outer, void **obj)
707 datainit *This;
709 TRACE("(%p)\n", obj);
711 if(outer) return CLASS_E_NOAGGREGATION;
713 *obj = NULL;
715 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
716 if(!This) return E_OUTOFMEMORY;
718 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
719 This->ref = 1;
721 *obj = &This->IDataInitialize_iface;
723 return S_OK;