forked from mariadb-corporation/mariadb-connector-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ma_dsn.c
708 lines (632 loc) · 21.5 KB
/
ma_dsn.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/************************************************************************************
Copyright (C) 2013,2019 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
*************************************************************************************/
#include <ma_odbc.h>
#define DSNKEY_OPTIONS_INDEX 3
#define DSNKEY_OPTION_INDEX 4
#define DSNKEY_NAMEDPIPE_INDEX 5
#define DSNKEY_TCPIP_INDEX 6
#define DSNKEY_SERVER_INDEX 7
#define DSNKEY_UID_INDEX 8
#define DSNKEY_PWD_INDEX 9
#define DSNKEY_DATABASE_INDEX 10
#define DSNKEY_FP_INDEX 25
#define DSNKEY_FPLIST_INDEX 26
MADB_DsnKey DsnKeys[]=
{
{"DSN", offsetof(MADB_Dsn, DSNName), DSN_TYPE_STRING, 0, 0}, /* 0 */
{"DESCRIPTION", offsetof(MADB_Dsn, Description), DSN_TYPE_STRING, 0, 0},
{"DRIVER", offsetof(MADB_Dsn, Driver), DSN_TYPE_STRING, 0, 0},
/* OPTIONS should go above all DSN_TYPE_OPTION. They are not saved in DSN separately, and then DSN is read, corresponding
properties are filled from OPTIONS. Also putting its alias here - it should not appear on Windows(unless somebody edits
registry manually), but on *nix we can expect everything. Array index used in some places to decide if the key is OPTIONS */
{"OPTIONS", offsetof(MADB_Dsn, Options), DSN_TYPE_INT, 0, 0}, /* DSNKEY_OPTIONS_INDEX */
{"OPTION", DSNKEY_OPTIONS_INDEX, DSN_TYPE_INT, 0, 1}, /* DSNKEY_OPTION_INDEX */
{"NamedPipe", offsetof(MADB_Dsn, IsNamedPipe), DSN_TYPE_OPTION, MADB_OPT_FLAG_NAMED_PIPE, 0}, /* MADB_DSNKEY_NAMEDPIPE_INDEX */
{"TCPIP", offsetof(MADB_Dsn, IsTcpIp), DSN_TYPE_BOOL, 0, 0}, /* DSNKEY_TCPIP_INDEX */
{"SERVER", offsetof(MADB_Dsn, ServerName), DSN_TYPE_STRING, 0, 0}, /* DSNKEY_SERVER_INDEX */
{"UID", offsetof(MADB_Dsn, UserName), DSN_TYPE_STRING, 0, 0}, /* DSNKEY_UID_INDEX */
{"PWD", offsetof(MADB_Dsn, Password), DSN_TYPE_STRING, 0, 0}, /* DSNKEY_PWD_INDEX */
{"DATABASE", offsetof(MADB_Dsn, Catalog), DSN_TYPE_COMBO, 0, 0}, /* 10 DSNKEY_DATABASE_INDEX */
{"PORT", offsetof(MADB_Dsn, Port), DSN_TYPE_INT, 0, 0},
{"INITSTMT", offsetof(MADB_Dsn, InitCommand), DSN_TYPE_STRING, 0, 0},
{"CONN_TIMEOUT", offsetof(MADB_Dsn, ConnectionTimeout), DSN_TYPE_INT, 0, 0},
{"AUTO_RECONNECT", offsetof(MADB_Dsn, Reconnect), DSN_TYPE_OPTION, MADB_OPT_FLAG_AUTO_RECONNECT,0},
{"NO_PROMPT", offsetof(MADB_Dsn, ConnectPrompt), DSN_TYPE_OPTION, MADB_OPT_FLAG_NO_PROMPT,0},
{"CHARSET", offsetof(MADB_Dsn, CharacterSet), DSN_TYPE_COMBO, 0, 0},
{"TRACE", offsetof(MADB_Dsn, TraceFile), DSN_TYPE_STRING, 0, 0},
{"PLUGIN_DIR", offsetof(MADB_Dsn, ConnCPluginsDir), DSN_TYPE_STRING, 0, 0},
/* SSL */
{"SSLKEY", offsetof(MADB_Dsn, SslKey), DSN_TYPE_STRING, 0, 0},
{"SSLCERT", offsetof(MADB_Dsn, SslCert), DSN_TYPE_STRING, 0, 0}, /* 20 */
{"SSLCA", offsetof(MADB_Dsn, SslCa), DSN_TYPE_STRING, 0, 0},
{"SSLCAPATH", offsetof(MADB_Dsn, SslCaPath), DSN_TYPE_STRING, 0, 0},
{"SSLCIPHER", offsetof(MADB_Dsn, SslCipher), DSN_TYPE_STRING, 0, 0},
{"SSLVERIFY", offsetof(MADB_Dsn, SslVerify), DSN_TYPE_BOOL, 0, 0},
{"TLSPEERFP", offsetof(MADB_Dsn, TlsPeerFp), DSN_TYPE_STRING, 0, 0},
{"TLSPEERFPLIST", offsetof(MADB_Dsn, TlsPeerFpList), DSN_TYPE_STRING, 0, 0},
{"SSLCRL", offsetof(MADB_Dsn, SslCrl), DSN_TYPE_STRING, 0, 0},
{"SSLCRLPATH", offsetof(MADB_Dsn, SslCrlPath), DSN_TYPE_STRING, 0, 0},
{"SOCKET", offsetof(MADB_Dsn, Socket), DSN_TYPE_STRING, 0, 0},
{"SAVEFILE", offsetof(MADB_Dsn, SaveFile), DSN_TYPE_STRING, 0, 0}, /* 30 */
{"USE_MYCNF", offsetof(MADB_Dsn, ReadMycnf), DSN_TYPE_OPTION, MADB_OPT_FLAG_USE_CNF, 0},
{"TLSVERSION", offsetof(MADB_Dsn, TlsVersion), DSN_TYPE_CBOXGROUP, 0, 0},
{"FORCETLS", offsetof(MADB_Dsn, ForceTls), DSN_TYPE_BOOL, 0, 0},
{"SERVERKEY", offsetof(MADB_Dsn, ServerKey), DSN_TYPE_STRING, 0, 0},
{"TLSKEYPWD", offsetof(MADB_Dsn, TlsKeyPwd), DSN_TYPE_STRING, 0, 0},
/* Aliases. Here offset is index of aliased key */
{"SERVERNAME", DSNKEY_SERVER_INDEX, DSN_TYPE_STRING, 0, 1},
{"USER", DSNKEY_UID_INDEX, DSN_TYPE_STRING, 0, 1},
{"PASSWORD", DSNKEY_PWD_INDEX, DSN_TYPE_STRING, 0, 1},
{"DB", DSNKEY_DATABASE_INDEX, DSN_TYPE_COMBO, 0, 1},
{"SSLFP", DSNKEY_FP_INDEX, DSN_TYPE_STRING, 0, 1},
{"SSLFPLIST", DSNKEY_FPLIST_INDEX, DSN_TYPE_STRING, 0, 1},
/* Terminating Null */
{NULL, 0, DSN_TYPE_BOOL,0,0}
};
/* TODO: Shouln't 2nd be DSNKEY_OPTIONS_INDEX? But I seemed to remember it was removed for a reason... */
#define IS_OPTIONS_BITMAP(key_index) (key_index == DSNKEY_OPTIONS_INDEX || key_index == DSNKEY_OPTIONS_INDEX)
typedef struct
{
unsigned int Key;
unsigned int Dependent;
BOOL Same; /* Should dependent be switched same way, or in reverse */
} MADB_DsnKeyDep;
/* Define pairs of keys that are switches, i.e. setting one should reset the other.
Transitive dependencies have to be defined as direct dependencies here as well */
const MADB_DsnKeyDep DsnKeysSwitch[]=
{
{DSNKEY_NAMEDPIPE_INDEX, DSNKEY_TCPIP_INDEX, 0},
{DSNKEY_TCPIP_INDEX, DSNKEY_NAMEDPIPE_INDEX, 0}
};
const char TlsVersionName[][8]= {"TLSv1.1", "TLSv1.2", "TLSv1.3"};
const char TlsVersionBits[]= {MADB_TLSV11, MADB_TLSV12, MADB_TLSV13};
/* {{{ MADB_DSN_SetDefaults() */
void MADB_DSN_SetDefaults(MADB_Dsn *Dsn)
{
Dsn->IsTcpIp= 1;
}
/* }}} */
/* {{{ MADB_Dsn_Init() */
MADB_Dsn *MADB_DSN_Init()
{
MADB_Dsn *Dsn;
if ((Dsn= (MADB_Dsn *)MADB_CALLOC(sizeof(MADB_Dsn))))
{
Dsn->FreeMe= TRUE;
Dsn->Keys= (MADB_DsnKey *)&DsnKeys;
}
return Dsn;
}
/* }}} */
/* {{{ MADB_Dsn_Free */
void MADB_DSN_Free(MADB_Dsn *Dsn)
{
if (!Dsn)
return;
MADB_FREE(Dsn->DSNName);
MADB_FREE(Dsn->Driver);
MADB_FREE(Dsn->Description);
MADB_FREE(Dsn->ServerName);
MADB_FREE(Dsn->UserName);
MADB_FREE(Dsn->Password);
MADB_FREE(Dsn->Catalog);
MADB_FREE(Dsn->CharacterSet);
MADB_FREE(Dsn->InitCommand);
MADB_FREE(Dsn->TraceFile);
MADB_FREE(Dsn->Socket);
MADB_FREE(Dsn->ConnCPluginsDir);
MADB_FREE(Dsn->SslKey);
MADB_FREE(Dsn->SslCert);
MADB_FREE(Dsn->SslCa);
MADB_FREE(Dsn->SslCaPath);
MADB_FREE(Dsn->SslCipher);
MADB_FREE(Dsn->SslCrl);
MADB_FREE(Dsn->SslCrlPath);
MADB_FREE(Dsn->TlsPeerFp);
MADB_FREE(Dsn->TlsPeerFpList);
MADB_FREE(Dsn->SaveFile);
MADB_FREE(Dsn->ServerKey);
MADB_FREE(Dsn->TlsKeyPwd);
if (Dsn->FreeMe)
MADB_FREE(Dsn);
}
/* }}} */
void MADB_SetOptionValue(MADB_Dsn *Dsn, MADB_DsnKey *DsnKey, my_bool value)
{
*GET_FIELD_PTR(Dsn, DsnKey, my_bool)= value;
if (value)
{
Dsn->Options |= DsnKey->FlagValue;
}
else
{
Dsn->Options &= ~DsnKey->FlagValue;
}
}
my_bool MADB_DsnStoreValue(MADB_Dsn *Dsn, unsigned int DsnKeyIdx, char *Value, my_bool OverWrite);
/* {{{ MADB_DsnSwitchDependents */
/* If TCPIP selected, we have to reset NAMEDPIPE */
BOOL MADB_DsnSwitchDependents(MADB_Dsn *Dsn, unsigned int Changed)
{
int i;
for (i= 0; i < sizeof(DsnKeysSwitch)/sizeof(MADB_DsnKeyDep); ++i)
{
if (DsnKeysSwitch[i].Key == Changed)
{
my_bool KeySet;
switch (DsnKeys[Changed].Type)
{
case DSN_TYPE_STRING:
case DSN_TYPE_COMBO:
{
char *str= *GET_FIELD_PTR(Dsn, &DsnKeys[Changed], char*);
KeySet= str && *str;
}
break;
case DSN_TYPE_OPTION:
case DSN_TYPE_BOOL:
case DSN_TYPE_CBOXGROUP:
{
KeySet= *GET_FIELD_PTR(Dsn, &DsnKeys[Changed], my_bool);
}
break;
case DSN_TYPE_INT:
{
KeySet= *GET_FIELD_PTR(Dsn, &DsnKeys[Changed], int) != 0;
}
}
/* No problem to deal with aliases here as well, but let's keep things simple */
if (DsnKeys[DsnKeysSwitch[i].Dependent].IsAlias != 0)
{
return FALSE;
}
switch(DsnKeys[DsnKeysSwitch[i].Dependent].Type)
{
case DSN_TYPE_BOOL:
*GET_FIELD_PTR(Dsn, &DsnKeys[DsnKeysSwitch[i].Dependent], my_bool)= DsnKeysSwitch[i].Same == KeySet ? 1 : 0;
break;
case DSN_TYPE_OPTION:
MADB_SetOptionValue(Dsn, &DsnKeys[DsnKeysSwitch[i].Dependent], DsnKeysSwitch[i].Same == KeySet ? 1 : 0);
break;
default:
return FALSE; /* Only boolean fields are supported as dependent atm */
}
}
}
return TRUE;
}
/* }}} */
/* {{{ MADB_DsnStoreValue */
my_bool MADB_DsnStoreValue(MADB_Dsn *Dsn, unsigned int DsnKeyIdx, char *Value, my_bool OverWrite)
{
MADB_DsnKey *DsnKey= &DsnKeys[DsnKeyIdx];
if (!Dsn || DsnKey->IsAlias)
return FALSE;
switch(DsnKey->Type) {
case DSN_TYPE_STRING:
case DSN_TYPE_COMBO:
{
char **p= GET_FIELD_PTR(Dsn, DsnKey, char*);
if (*p && OverWrite == FALSE)
break;
/* For the case of making copy of currently stored values */
MADB_RESET(*p, Value);
}
break;
case DSN_TYPE_BOOL:
/* If value is not set or we may overwrite it */
if (!(*GET_FIELD_PTR(Dsn, DsnKey, my_bool) && OverWrite == FALSE))
{
*GET_FIELD_PTR(Dsn, DsnKey, my_bool)= atoi(Value);
}
break;
case DSN_TYPE_CBOXGROUP:
/* If value is not set or we may overwrite it */
if (! (*GET_FIELD_PTR(Dsn, DsnKey, char) && OverWrite == FALSE))
{
char IntValue= atoi(Value);
/* Atm we have only one DSN_TYPE_CBOXGROUP!!!, and following works only for it. If sometime another such field is added,
we will need another data structure array, that will bind DSN field with string values and bits for this field.
So far we use hardcoded arrays for the singe such field we have atm */
if (IntValue == '\0')
{
unsigned int i;
IntValue= 0;
for (i= 0; i < sizeof(TlsVersionBits); ++i)
{
if (strcasestr(Value, TlsVersionName[i]) != NULL)
{
IntValue|= TlsVersionBits[i];
}
}
}
*GET_FIELD_PTR(Dsn, DsnKey, char)= IntValue;
}
break;
case DSN_TYPE_INT:
if (*GET_FIELD_PTR(Dsn, DsnKey, int) && OverWrite == FALSE)
break;
*GET_FIELD_PTR(Dsn, DsnKey, int)= strtoul(Value, NULL, 10);
break;
case DSN_TYPE_OPTION:
if (*GET_FIELD_PTR(Dsn, DsnKey, my_bool) && OverWrite == FALSE)
break;
MADB_SetOptionValue(Dsn, DsnKey, strtoul(Value, NULL, 10) != 0 ? 1 : 0);
break;
}
return MADB_DsnSwitchDependents(Dsn, DsnKeyIdx);
}
/* }}} */
/* {{{ MADB_DsnUpdateOptionsFields */
void MADB_DsnUpdateOptionsFields(MADB_Dsn *Dsn)
{
int i= 0;
while (DsnKeys[i].DsnKey != NULL)
{
if (DsnKeys[i].IsAlias == 0)
{
if (DsnKeys[i].Type == DSN_TYPE_OPTION)
{
*GET_FIELD_PTR(Dsn, &DsnKeys[i], my_bool)= (my_bool)(DSN_OPTION(Dsn, DsnKeys[i].FlagValue) ? 1 : 0);
MADB_DsnSwitchDependents(Dsn, i);
}
}
++i;
}
}
/* }}} */
/* {{{ MADB_ReadDSN */
my_bool MADB_ReadDSN(MADB_Dsn *Dsn, const char *KeyValue, my_bool OverWrite)
{
char *Value;
/* if no key/value pair was specified, we will try to read Dsn->DSNName */
if (!KeyValue)
{
Value= Dsn->DSNName;
}
else
{
if ((Value= strchr(KeyValue, '=')) != NULL)
{
++Value;
MADB_RESET(Dsn->DSNName, Value);
}
}
if (Value)
{
int i= 1;
char KeyVal[1024];
while (DsnKeys[i].DsnKey)
{
unsigned int KeyIdx= DsnKeys[i].IsAlias ? DsnKeys[i].DsnOffset : i;
if (SQLGetPrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey, "", KeyVal, 1024, "ODBC.INI") > 0)
{
if (!MADB_DsnStoreValue(Dsn, KeyIdx, KeyVal, OverWrite))
return FALSE;
}
else if (DsnKeys[i].Type == DSN_TYPE_OPTION)
{
*GET_FIELD_PTR(Dsn, &DsnKeys[KeyIdx], my_bool)= (my_bool)(DSN_OPTION(Dsn, DsnKeys[KeyIdx].FlagValue) ? 1 : 0);
}
++i;
}
return TRUE;
}
return FALSE;
}
/* }}} */
my_bool MADB_DSN_Exists(const char *DsnName)
{
my_bool ret;
char buffer[1024];
char *p= "";
if (!DsnName)
return FALSE;
ret= (SQLGetPrivateProfileString(DsnName, NULL, p, buffer, 1024, "ODBC.INI") > 0);
return ret;
}
/* {{{ MADB_SaveDSN */
my_bool MADB_SaveDSN(MADB_Dsn *Dsn)
{
int i= 1;
char Value[32];
my_bool ret;
DWORD ErrNum;
if (!SQLValidDSN(Dsn->DSNName))
{
strcpy_s(Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, "Invalid Data Source Name");
return FALSE;
}
if (!SQLRemoveDSNFromIni(Dsn->DSNName))
{
SQLInstallerError(1,&ErrNum, Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, NULL);
return FALSE;
}
if (!SQLWriteDSNToIni(Dsn->DSNName, Dsn->Driver))
{
SQLInstallerError(1,&ErrNum, Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, NULL);
return FALSE;
}
while(DsnKeys[i].DsnKey)
{
/* Skipping aliases - options are saved by primary name only */
if (!DsnKeys[i].IsAlias)
{
ret= TRUE;
/* We do not save DSN_TYPE_OPTION - they are saved as OPTIONS bits */
switch (DsnKeys[i].Type) {
case DSN_TYPE_BOOL:
ret= SQLWritePrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey,
*GET_FIELD_PTR(Dsn, &DsnKeys[i], my_bool) ? "1" : "0", "ODBC.INI");
break;
case DSN_TYPE_INT:
{
_snprintf(Value ,32, "%d", *(int *)((char *)Dsn + DsnKeys[i].DsnOffset));
ret= SQLWritePrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey, Value, "ODBC.INI");
}
break;
case DSN_TYPE_CBOXGROUP:
{
_snprintf(Value, 32, "%hu", (short)*GET_FIELD_PTR(Dsn, &DsnKeys[i], char));
ret= SQLWritePrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey, Value, "ODBC.INI");
}
break;
case DSN_TYPE_STRING:
case DSN_TYPE_COMBO:
{
char *Val= *GET_FIELD_PTR(Dsn, &DsnKeys[i], char*);
if (Val && Val[0])
ret= SQLWritePrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey, Val, "ODBC.INI");
}
default:
/* To avoid warning with some compilers */
break;
} /* switch */
if (!ret)
{
SQLInstallerError(1,&ErrNum, Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, NULL);
return FALSE;
}
}
i++;
}
/* Save Options */
_snprintf(Value ,32, "%d", Dsn->Options);
if (!(ret= SQLWritePrivateProfileString(Dsn->DSNName, "OPTIONS", Value, "ODBC.INI")))
{
SQLInstallerError(1,&ErrNum, Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, NULL);
return FALSE;
}
return TRUE;
}
/* }}} */
size_t ConnStringLength(const char * String, char Delimiter)
{
size_t result= strlen(String);
const char *p= String + result + 1;
if (Delimiter != '\0')
{
return result;
}
/* else - we have string with null terminated key=value pairs with additional NULL after last pair */
while (*p)
{
p+= strlen(p) + 1;
}
return p - String /* Length without ending NULL */;
}
/* {{{ MADB_ParseConnString */
my_bool MADB_ParseConnString(MADB_Dsn *Dsn, const char *String, size_t Length, char Delimiter)
{
char *Buffer, *Key, *Value, *ValueBuf;
my_bool ret= TRUE;
if (!String)
return FALSE;
if (Length == SQL_NTS)
{
Length= ConnStringLength(String, Delimiter);
}
Buffer= MADB_ALLOC(Length + 1);
Buffer= memcpy(Buffer, String, Length + 1);
Key= Buffer;
ValueBuf= MADB_ALLOC(Length - 4); /*DSN=<value> - DSN or DRIVER must be in */
while (Key && Key < ((char *)Buffer + Length))
{
int i= 0;
/* The case of ;; - "empty key/value pair. Probably that shouldn't be allowed. But parser uset to digest this, so leaving this as a feature so far
TODO: check and maybe remove for the next version */
if (Delimiter != '\0' && *Key == Delimiter)
{
++Key;
continue;
}
if (!(Value= strchr(Key, '=')))
{
ret= FALSE;
break;
}
*Value= 0;
++Value;
Key= trim(Key);
while (DsnKeys[i].DsnKey)
{
if (_stricmp(DsnKeys[i].DsnKey, Key) == 0)
{
char *p= NULL;
if (DsnKeys[i].IsAlias)
{
i= DsnKeys[i].DsnOffset; /* For aliases DsnOffset is index of aliased "main" key */
}
Value= ltrim(Value);
if (Value[0] == '{')
{
char *valueBufPtr= ValueBuf;
char *prev= ++Value;
*valueBufPtr= '\0';
while ((p = strchr(prev, '}')) != NULL )
{
memcpy(valueBufPtr, prev, p - prev);
valueBufPtr+= p - prev;
if (*(p + 1) == '}')
{
*(valueBufPtr++)= '}';
*valueBufPtr= '\0';
prev= p + 2;
}
else
{
*valueBufPtr= '\0';
++p;
break;
}
}
Value= ValueBuf;
}
else if ((p= strchr(Value, Delimiter)))
{
*p= 0;
}
/* TODO: 3.2 we should not trim enclosed in braces, I think */
Value= trim(Value);
/* Overwriting here - if an option repeated more than once in the string, its last entrance will determine the value */
if (!MADB_DsnStoreValue(Dsn, i, Value, TRUE))
{
ret= FALSE;
goto end;
}
if (IS_OPTIONS_BITMAP(i))
{
MADB_DsnUpdateOptionsFields(Dsn);
}
if (p)
{
Key= p + 1;
}
else
{
Key= NULL;
}
break;
}
++i;
}
/* Unknown keyword */
if (DsnKeys[i].DsnKey == NULL)
{
//TODO: shouldn't some error/warning be thrown?
Key= strchr(Value, Delimiter);
if (Key != NULL)
{
++Key;
}
}
}
end:
MADB_FREE(Buffer);
MADB_FREE(ValueBuf);
return ret;
}
/* }}} */
/* {{{ MADB_ReadConnString */
/* Like ParseConnString, but expands DSN if needed, preserving connection string values precedence.
Or in other words - it is combination of ReadDsn and ParseConnString */
BOOL MADB_ReadConnString(MADB_Dsn *Dsn, const char *String, size_t Length, char Delimiter)
{
/* Basically at this point we need DSN name only */
if (!MADB_ParseConnString(Dsn, String, Length, Delimiter))
{
return FALSE;
}
/* "If the connection string contains the DRIVER keyword, the driver cannot retrieve information about the data source
from the system information." https://msdn.microsoft.com/en-us/library/ms715433%28v=vs.85%29.aspx */
if (Dsn->DSNName && MADB_IS_EMPTY(Dsn->Driver))
{
MADB_ReadDSN(Dsn, NULL, FALSE);
/* This redundancy is needed to be able to reset options set in the DSN, e.g. if DSN has Reconnect option selected, and
connection string has AUTO_RECONNECT=0. Connection string should have precedence */
MADB_ParseConnString(Dsn, String, Length, Delimiter);
}
return TRUE;
}
/* }}} */
/* {{{ MADB_DsnToString */
SQLULEN MADB_DsnToString(MADB_Dsn *Dsn, char *OutString, SQLULEN OutLength)
{
int i= 0;
SQLULEN TotalLength= 0;
char *p= OutString;
char *Value= NULL;
char TmpStr[1024]= { '\0' };
char IntVal[12];
int CpyLength;
if (OutLength && OutString)
OutString[0]= '\0';
while (DsnKeys[i].DsnKey)
{
Value= NULL;
if (!DsnKeys[i].IsAlias)
{
switch (DsnKeys[i].Type) {
case DSN_TYPE_STRING:
case DSN_TYPE_COMBO:
Value= *GET_FIELD_PTR(Dsn, &DsnKeys[i], char*);
if (MADB_IS_EMPTY(Value))
{
++i;
continue;
}
break;
case DSN_TYPE_INT:
if (*GET_FIELD_PTR(Dsn, &DsnKeys[i], int))
{
_snprintf(IntVal, sizeof(IntVal), "%d", *GET_FIELD_PTR(Dsn, &DsnKeys[i], int));
Value= IntVal;
}
break;
case DSN_TYPE_BOOL:
if (*GET_FIELD_PTR(Dsn, &DsnKeys[i], my_bool))
{
Value= "1";
}
break;
case DSN_TYPE_CBOXGROUP:
if (*GET_FIELD_PTR(Dsn, &DsnKeys[i], char))
{
_snprintf(IntVal, sizeof(IntVal), "%hu", (short)*GET_FIELD_PTR(Dsn, &DsnKeys[i], char));
Value= IntVal;
}
break;
default:
/* To avoid warning with some compilers */
break;
}
}
if (Value)
{
my_bool isSpecial= (strchr(Value, ' ') || strchr(Value, ';') || strchr(Value, '@'));
CpyLength= _snprintf(TmpStr + TotalLength, 1024 - TotalLength, "%s%s=%s%s%s", (TotalLength) ? ";" : "",
DsnKeys[i].DsnKey, isSpecial ? "{" : "", Value, isSpecial ? "}" : "");
TotalLength+= CpyLength;
}
++i;
}
if (OutLength && OutString)
{
strncpy_s(OutString, OutLength, TmpStr, TotalLength);
}
return TotalLength;
}
/* }}} */