forked from mariadb-corporation/mariadb-connector-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ma_platform_win32.c
331 lines (269 loc) · 8.69 KB
/
ma_platform_win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/************************************************************************************
Copyright (C) 2014,2016 MariaDB Corporation AB
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
/* MariaDB ODBC driver Win32 specific helper functions */
/* NOTE If you change something in this program, please consider if other platform's version
of the function you are changing, needs to be changed accordingly */
#include <ma_odbc.h>
#include "Shlwapi.h"
extern Client_Charset utf8;
char LogFile[256];
char *strndup(const char *s, size_t n)
{
char *res= NULL;
if (s != NULL)
{
size_t len= MIN(strlen(s), n);
res= (char*)malloc(len + 1);
if (res != NULL)
{
memcpy(res, s, len);
res[len]= '\0';
}
}
return res;
}
const char* GetDefaultLogDir()
{
const char *DefaultLogDir= "c:";
char *tmp= getenv("USERPROFILE");
if (tmp)
{
DefaultLogDir= tmp;
}
tmp= getenv("TMP");
if (tmp)
{
DefaultLogDir= tmp;
}
_snprintf(LogFile, sizeof(LogFile), "%s\\MAODBC.LOG", DefaultLogDir);
return LogFile;
}
/* Connection is needed to set custom error */
int DSNPrompt_Lookup(MADB_Prompt *prompt, const char * SetupLibName)
{
if (!(prompt->LibraryHandle=(void*) LoadLibrary(SetupLibName)))
{
return MADB_PROMPT_COULDNT_LOAD;
}
if (!(prompt->Call= (PromptDSN)GetProcAddress((HMODULE)prompt->LibraryHandle, "DSNPrompt")))
{
return MADB_PROMPT_COULDNT_LOAD;
}
return SQL_SUCCESS;
}
int DSNPrompt_Free(MADB_Prompt *prompt)
{
if (prompt->LibraryHandle != NULL)
{
FreeLibrary((HMODULE)prompt->LibraryHandle);
}
prompt->LibraryHandle= NULL;
prompt->Call= NULL;
return 0;
}
SQLWCHAR *MADB_ConvertToWchar(const char *Ptr, SQLLEN PtrLength, Client_Charset* cc)
{
SQLWCHAR *WStr= NULL;
int Length;
if (!Ptr)
return WStr;
if (PtrLength == SQL_NTS)
PtrLength= -1;
if (!cc || !cc->CodePage)
cc= &utf8;
/* TODO: Check this
In fact MultiByteToWideChar does not copy terminating character by default
Thus +1 does not make sense
"If the function succeeds and cchWideChar is 0, the return value is the required size,
in characters, for the buffer indicated by lpWideCharStr...
MultiByteToWideChar does not null-terminate an output string if the input string length
is explicitly specified without a terminating null character. To null-terminate an output
string for this function, the application should pass in -1 or explicitly count the
terminating null character for the input string." */
if ((Length= MultiByteToWideChar(cc->CodePage, 0, Ptr, (int)PtrLength, NULL, 0)))
if ((WStr= (SQLWCHAR *)MADB_CALLOC(sizeof(SQLWCHAR) * Length + 1)))
MultiByteToWideChar(cc->CodePage, 0, Ptr, (int)PtrLength, WStr, Length);
return WStr;
}
/* {{{ MADB_ConvertFromWChar
Length gets number of written bytes including TN (if WstrCharLen == -1 or SQL_NTS or if WstrCharLen includes
TN in the Wstr) */
char *MADB_ConvertFromWChar(const SQLWCHAR *Wstr, SQLINTEGER WstrCharLen, SQLULEN *Length/*Bytes*/, Client_Charset *cc, BOOL *Error)
{
char *AscStr;
int AscLen, AllocLen;
if (Error)
*Error= 0;
if (cc == NULL || cc->CodePage < 1)
{
cc= &utf8;
}
if (WstrCharLen == SQL_NTS)
WstrCharLen= -1;
AllocLen= AscLen= WideCharToMultiByte(cc->CodePage, 0, Wstr, WstrCharLen, NULL, 0, NULL, NULL);
if (WstrCharLen != -1)
++AllocLen;
if (!(AscStr = (char *)MADB_CALLOC(AllocLen)))
return NULL;
AscLen= WideCharToMultiByte(cc->CodePage, 0, Wstr, WstrCharLen, AscStr, AscLen, NULL, (cc->CodePage != CP_UTF8) ? Error : NULL);
if (AscLen && WstrCharLen == -1)
--AscLen;
if (Length)
*Length= (SQLINTEGER)AscLen;
return AscStr;
}
/* }}} */
/* Required Length without or with TN(if IsNull is TRUE, or AnsiLength == -1 or SQL_NTS) is put to LenghtIndicator*/
int MADB_ConvertAnsi2Unicode(Client_Charset *cc, const char *AnsiString, SQLLEN AnsiLength,
SQLWCHAR *UnicodeString, SQLLEN UnicodeLength,
SQLLEN *LengthIndicator, BOOL IsNull, MADB_Error *Error)
{
SQLLEN RequiredLength;
SQLWCHAR *Tmp= UnicodeString;
int rc= 0;
if (LengthIndicator)
*LengthIndicator= 0;
if (Error)
MADB_CLEAR_ERROR(Error);
if (!AnsiLength || UnicodeLength < 0)
{
if (Error)
MADB_SetError(Error, MADB_ERR_HY090, NULL, 0);
return 1;
}
if (AnsiLength == SQL_NTS || AnsiLength == -1)
IsNull= 1;
/* calculate required length */
RequiredLength= MultiByteToWideChar(cc->CodePage, 0, AnsiString, IsNull ? -1 : (int)AnsiLength, NULL, 0);
/* Set LengthIndicator */
if (LengthIndicator)
*LengthIndicator= RequiredLength - IsNull;
if (!UnicodeLength)
return 0;
if (RequiredLength > UnicodeLength)
Tmp= (SQLWCHAR *)malloc(RequiredLength * sizeof(SQLWCHAR));
RequiredLength= MultiByteToWideChar(cc->CodePage, 0, AnsiString, IsNull ? -1 : (int)AnsiLength, Tmp, (int)RequiredLength);
if (RequiredLength < 1)
{
if (Error)
MADB_SetError(Error, MADB_ERR_HY000, "Ansi to Unicode conversion error occurred", GetLastError());
rc= 1;
goto end;
}
/* Truncation */
if (Tmp != UnicodeString)
{
wcsncpy(UnicodeString, L"", 1);
wcsncat(UnicodeString, Tmp, UnicodeLength- 1);
if (Error)
MADB_SetError(Error, MADB_ERR_01004, NULL, 0);
}
end:
if (Tmp != UnicodeString)
free(Tmp);
return rc;
}
/* Returns required length for result string with(if dest and dest length are provided)
or without terminating NULL(otherwise). If cc is NULL, or not initialized(CodePage is 0),
then simply SrcLength is returned.
If Dest is not NULL, and DestLenth is 0, then error */
SQLLEN MADB_SetString(Client_Charset* cc, void *Dest, SQLULEN DestLength,
const char *Src, SQLLEN SrcLength/*bytes*/, MADB_Error *Error)
{
char *p= (char *)Dest;
SQLLEN Length= 0;
if (SrcLength == SQL_NTS)
{
if (Src != NULL)
{
SrcLength= strlen(Src);
}
else
{
SrcLength= 0;
}
}
/* Not enough space */
if (!DestLength || !Dest)
{
if (Dest)
MADB_SetError(Error, MADB_ERR_01004, NULL, 0);
if (!cc || !cc->CodePage)
return SrcLength;
else
{
Length= MultiByteToWideChar(cc->CodePage, 0, Src, (int)SrcLength, NULL, 0);
return Length;
}
}
if (!Src || !strlen(Src) || !SrcLength)
{
memset(p, 0, cc && cc->CodePage ? sizeof(SQLWCHAR) : sizeof(SQLCHAR));
return 0;
}
if (!cc || !cc->CodePage)
{
strncpy_s((char *)Dest, DestLength, Src ? Src : "", _TRUNCATE);
if (Error && (SQLULEN)SrcLength >= DestLength)
MADB_SetError(Error, MADB_ERR_01004, NULL, 0);
return SrcLength;
}
else
{
MADB_ConvertAnsi2Unicode(cc, Src, -1, (SQLWCHAR *)Dest, DestLength, &Length, 1, Error);
return Length;
}
}
int GetSourceAnsiCs(Client_Charset *cc)
{
cc->CodePage= GetACP();
/* We don't need cs_info for this */
return cc->CodePage;
}
BOOL MADB_DirectoryExists(const char *Path)
{
DWORD FileAttributes = GetFileAttributes(Path);
return (FileAttributes != INVALID_FILE_ATTRIBUTES) && (FileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}
const char* MADB_GetDefaultPluginsDir(MADB_Dbc *Dbc)
{
HMODULE hModule = GetModuleHandle(MADB_DRIVER_NAME);
static char OurLocation[_MAX_PATH];
const char *PluginsSubDirName= "\\"MADB_DEFAULT_PLUGINS_SUBDIR;
GetModuleFileName(hModule, OurLocation, _MAX_PATH);
PathRemoveFileSpec(OurLocation);
if (strlen(OurLocation) < _MAX_PATH - strlen(PluginsSubDirName))
{
strcpy(OurLocation + strlen(OurLocation), PluginsSubDirName);
if (MADB_DirectoryExists(OurLocation) != FALSE)
{
return OurLocation;
}
}
return NULL;
}
/* {{{ MADB_DSN_PossibleConnect() */
BOOL MADB_DSN_PossibleConnect(MADB_Dsn *Dsn)
{
return Dsn->ServerName && (Dsn->IsNamedPipe || Dsn->Port > 0);
}
/* }}} */
/* {{{ strcasestr() */
char* strcasestr(const char* HayStack, const char* Needle)
{
return StrStrIA(HayStack, Needle);
}
/* }}} */