forked from mariadb-corporation/mariadb-connector-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ma_platform_posix.c
404 lines (325 loc) · 9.86 KB
/
ma_platform_posix.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/************************************************************************************
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 helper functions for platforms other than windows(so far) */
/* 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 <stdarg.h>
extern MARIADB_CHARSET_INFO *DmUnicodeCs;
extern Client_Charset utf8;
char LogFile[256];
void InitializeCriticalSection(CRITICAL_SECTION *cs)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(cs, &attr);
}
int DSNPrompt_Lookup(MADB_Prompt *prompt, const char * SetupLibName)
{
return MADB_PROMPT_NOT_SUPPORTED;
}
int DSNPrompt_Free (MADB_Prompt *prompt)
{
prompt->LibraryHandle= NULL;
return 0;
}
/* Mimicking of VS' _snprintf */
int _snprintf(char *buffer, size_t count, const char *format, ...)
{
va_list list;
va_start(list, format);
int result= vsnprintf(buffer, count, format, list);
va_end(list);
/* _snprintf returns negative number if buffer is not big enough */
if (result > count)
{
return count - result - 1;
}
return result;
}
int strcpy_s(char *dest, size_t buffer_size, const char *src)
{
size_t src_len;
if (dest == NULL)
{
return EINVAL;
}
if (src == NULL)
{
*dest= '\0';
return EINVAL;
}
src_len= strlen(src);
if (buffer_size < src_len + 1)
{
*dest= 0;
return ERANGE;
}
memcpy((void*)dest, (void*)src, src_len + 1);
return 0;
}
const char* GetDefaultLogDir()
{
const char *DefaultLogDir="/tmp";
char *tmp= getenv("HOME");
if (tmp)
{
DefaultLogDir= tmp;
}
_snprintf(LogFile, sizeof(LogFile), "%s/maodbc.log", DefaultLogDir);
return LogFile;
}
/* CharLen < 0 - treat as NTS */
SQLINTEGER SqlwcsOctetLen(const SQLWCHAR *str, SQLINTEGER *CharLen)
{
SQLINTEGER result= 0, inChars= *CharLen;
if (str)
{
while (inChars > 0 || (inChars < 0 && *str))
{
result+= DmUnicodeCs->mb_charlen(*str);
--inChars;
str+= DmUnicodeCs->mb_charlen(*str)/sizeof(SQLWCHAR);
}
}
if (*CharLen < 0)
{
*CharLen-= inChars;
}
return result;
}
SQLWCHAR *MADB_ConvertToWchar(const char *Ptr, SQLLEN PtrLength, Client_Charset* cc)
{
SQLWCHAR *WStr= NULL;
size_t Length= 0;
if (!Ptr)
return WStr;
if (PtrLength == SQL_NTS)
{
PtrLength= -1;
/* To copy terminating null as well */
Length= 1;
}
if (!cc || !cc->CodePage)
cc= &utf8;
Length+= MbstrOctetLen(Ptr, &PtrLength, cc->cs_info);
if ((WStr= (SQLWCHAR *)MADB_CALLOC(sizeof(SQLWCHAR) * (PtrLength + 1))))
{
size_t wstr_octet_len= sizeof(SQLWCHAR) * (PtrLength + 1);
/* TODO: Need error processing. i.e. if mariadb_convert_string returns -1 */
mariadb_convert_string(Ptr, &Length, cc->cs_info, (char*)WStr, &wstr_octet_len, DmUnicodeCs, NULL);
}
return WStr;
}
/* {{{ MADB_ConvertFromWChar */
char *MADB_ConvertFromWChar(const SQLWCHAR *Ptr, SQLINTEGER PtrLength, SQLULEN *Length, Client_Charset *cc,
BOOL *Error)
{
char *AscStr;
size_t AscLen= PtrLength, PtrOctetLen;
BOOL dummyError= 0;
if (Error)
{
*Error= 0;
}
else
{
Error= &dummyError;
}
if (cc == NULL || cc->CodePage < 1)
{
cc= &utf8;
}
if (PtrLength == SQL_NTS)
{
/*-1 - to calculate length as of nts */
SQLINTEGER InCharLen= -1;
PtrOctetLen= SqlwcsOctetLen(Ptr, &InCharLen);
/* Allocating +1 character for terminating symbol */
AscLen= (InCharLen+1)*cc->cs_info->char_maxlen;
}
else
{
/* PtrLength is in characters. mariadb_convert_string(iconv) needs bytes */
PtrOctetLen= SqlwcsOctetLen(Ptr, &PtrLength);
AscLen= PtrLength*cc->cs_info->char_maxlen;
}
if (!(AscStr = (char *)MADB_CALLOC(AscLen)))
return NULL;
AscLen= mariadb_convert_string((char*)Ptr, &PtrOctetLen, DmUnicodeCs, AscStr, &AscLen, cc->cs_info, Error);
if (AscLen != (size_t)-1)
{
if (PtrLength == -1 && AscLen > 0)
{
--AscLen;
}
}
else
{
MADB_FREE(AscStr);
AscLen= 0;
}
if (Length)
*Length= (SQLINTEGER)AscLen;
return AscStr;
}
/* }}} */
/* {{{ MADB_ConvertAnsi2Unicode
@AnsiLength[in] - number of bytes to copy, negative if AnsiString is Null terminated and the terminating blank has to be copied
@UnicodeLength[in] - size of output buffer in chars, that effectively mean in SQLWCHAR units
@LengthIndicator[out] - number of available characters not counting terminating null(unless it was included in AnsiLength, and IsNull
is FALSE)
@IsNull[in] - whether to copy terminating blank. The value has to be 1 or 0(TRUE/FALSE)
If AnsiString is negative, its value is neglected(null is copied)
@returns 1 in case of error, 0 otherwise */
int MADB_ConvertAnsi2Unicode(Client_Charset *cc, const char *AnsiString, SQLLEN AnsiLength,
SQLWCHAR *UnicodeString, SQLLEN UnicodeLength,
SQLLEN *LengthIndicator, BOOL IsNull, MADB_Error *Error)
{
SQLINTEGER RequiredLength;
SQLWCHAR *Tmp= UnicodeString;
int rc= 0, error;
size_t SrcOctetLen, DestOctetLen;
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;
AnsiLength= strlen(AnsiString);
}
/* calculate required length */
RequiredLength= MbstrCharLen(AnsiString, AnsiLength, cc->cs_info) + IsNull;
/* Set LengthIndicator */
if (LengthIndicator)
*LengthIndicator= RequiredLength - IsNull;
/* No buffer length, no need to copy - got length and run */
if (!UnicodeLength)
return 0;
if (RequiredLength > UnicodeLength)
{
Tmp= (SQLWCHAR *)malloc(RequiredLength * sizeof(SQLWCHAR));
}
else
{
RequiredLength= UnicodeLength;
}
SrcOctetLen= AnsiLength + IsNull;
DestOctetLen= sizeof(SQLWCHAR) * RequiredLength;
RequiredLength= mariadb_convert_string(AnsiString, &SrcOctetLen, cc->cs_info,
(char*)Tmp, &DestOctetLen, DmUnicodeCs, &error);
if (RequiredLength < 1)
{
if (Error)
MADB_SetError(Error, MADB_ERR_HY000, "Ansi to Unicode conversion error occurred", error);
rc= 1;
goto end;
}
if (LengthIndicator)
*LengthIndicator= SqlwcsCharLen(Tmp, RequiredLength);
/* Truncation */
if (Tmp != UnicodeString)
{
memcpy((void*)UnicodeString, (void*)Tmp, (UnicodeLength-1)*sizeof(SQLWCHAR));
*(UnicodeString + UnicodeLength - 1)= 0;
if (Error)
MADB_SetError(Error, MADB_ERR_01004, NULL, 0);
}
end:
if (Tmp != UnicodeString)
free(Tmp);
return rc;
}
/* }}} */
/* {{{ MADB_ConvertAnsi2Unicode
@returns number of characters available at Src */
SQLLEN MADB_SetString(Client_Charset* cc, void *Dest, SQLULEN DestLength,
const char *Src, SQLLEN SrcLength/*bytes*/, MADB_Error *Error)
{
SQLLEN Length= 0;
if (SrcLength == SQL_NTS)
{
if (Src != NULL)
{
/* Thinking about utf8 - Should be probably len in characters */
SrcLength= strlen(Src);
}
else
{
SrcLength= 0;
}
}
/* Not enough space */
if (!DestLength || !Dest)
{
if (Dest)
MADB_SetError(Error, MADB_ERR_01004, NULL, 0);
if (!cc)
return SrcLength;
else
{
Length= MbstrCharLen(Src, SrcLength, cc->cs_info);
/* In case of !DestLength || !Dest(application didn't give buffer and probably wants to know required length)
* we most probably have empty Src, and Length will be equal 0 in this case.
* Taking source length as character length. MultiByteToWideChar on windows does that for us */
return Length == 0 && SrcLength > 0 ? SrcLength : Length;
}
}
if (!SrcLength || !Src || !strlen(Src))
{
memset((char *)Dest, 0, cc ? sizeof(SQLWCHAR) : sizeof(SQLCHAR));
return 0;
}
if (!cc)
{
strncpy_s((char *)Dest, DestLength, Src ? Src : "", _TRUNCATE);
/* strncpy does not write null at the end */
*((char *)Dest + MIN(SrcLength, DestLength - 1))= '\0';
if (Error && SrcLength >= DestLength)
MADB_SetError(Error, MADB_ERR_01004, NULL, 0);
return SrcLength;
}
else
{
MADB_ConvertAnsi2Unicode(cc, Src, -1, (SQLWCHAR *)Dest, DestLength, &Length, TRUE, Error);
return Length;
}
}
/* }}} */
/* Stub - needed on windows only */
int GetSourceAnsiCs(Client_Charset *cc)
{
CopyClientCharset(&utf8, cc);
return cc->CodePage;
}
/* {{{ MADB_DSN_PossibleConnect(MADB_Dsn *) */
BOOL MADB_DSN_PossibleConnect(MADB_Dsn *Dsn)
{
return Dsn->Socket || (Dsn->ServerName && Dsn->Port > 0 && Dsn->IsTcpIp);
}
/* Stub - atm it looks like we don't need to do anything here */
char* MADB_GetDefaultPluginsDir(char* Buffer, size_t Size)
{
return NULL;
}