-
Notifications
You must be signed in to change notification settings - Fork 36
/
sqlite4odbc.c
20108 lines (18960 loc) · 482 KB
/
sqlite4odbc.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file sqlite4odbc.c
* SQLite4 ODBC Driver main module.
*
* $Id: sqlite4odbc.c,v 1.26 2023/10/23 13:33:58 chw Exp chw $
*
* Copyright (c) 2014-2023 Christian Werner <[email protected]>
*
* See the file "license.terms" for information on usage
* and redistribution of this file and for a
* DISCLAIMER OF ALL WARRANTIES.
*/
#if defined(SQLITE_HAS_CODEC) && defined(SQLITE_API)
#undef WITH_SQLITE_DLLS
#undef SQLITE_DYNLOAD
#include "sqlite4.c"
#endif
#if defined(WITH_SQLITE_DLLS) && (WITH_SQLITE_DLLS > 1)
#define SQLITE_DYNLOAD 1
#endif
#include "sqlite4odbc.h"
#ifdef SQLITE_DYNLOAD
#undef MEMORY_DEBUG
#if defined(_WIN32) || defined(_WIN64)
static void dls_init(void);
static void dls_fini(void);
#else
void dls_init(void);
void dls_fini(void);
#endif
static struct dl_sqlite4_funcs {
int (*bind_blob)(sqlite4_stmt *p0, int p1, const void *p2, int p3,
void (*p4)(void *, void *), void *p5);
int (*bind_double)(sqlite4_stmt *p0, int p1, double p2);
int (*bind_int)(sqlite4_stmt *p0, int p1, int p2);
int (*bind_int64)(sqlite4_stmt *p0, int p1, sqlite4_int64 p2);
int (*bind_null)(sqlite4_stmt *p0, int p1);
int (*bind_parameter_count)(sqlite4_stmt *p0);
int (*bind_text)(sqlite4_stmt *p0, int p1, const char *p2, int p3,
void (*p4)(void *, void *), void *p5);
int (*changes)(sqlite4 *p0);
int (*close)(sqlite4 *p0, unsigned int p1);
const void * (*column_blob)(sqlite4_stmt *p0, int p1, int p2);
int (*column_bytes)(sqlite4_stmt *p0, int p1);
int (*column_count)(sqlite4_stmt *p0);
const char * (*column_database_name)(sqlite4_stmt *p0, int p1);
const char * (*column_decltype)(sqlite4_stmt *p0, int p1);
double (*column_double)(sqlite4_stmt *p0);
const char * (*column_name)(sqlite4_stmt *p0, int p1);
const char * (*column_origin_name)(sqlite4_stmt *p0, int p1);
const char * (*column_table_name)(sqlite4_stmt *p0, int p1);
const char * (*column_text)(sqlite4_stmt *p0, int p1, p2);
int (*column_type)(sqlite4_stmt *p0, int p1);
int (*create_function)(sqlite4 *p0, const char *p1, int p2,
void *p4,
void (*p5)(sqlite4_context *, int, sqlite4_value **),
void (*p6)(sqlite4_context *, int, sqlite4_value **),
void (*p7)(sqlite4_context *),
void (*p8)(void *));
int (*errcode)(sqlite4 *p0);
const char * (*errmsg)(sqlite4 *p0);
int (*exec)(sqlite4 *p0, const char *p1,
int (*p2)(void *, int, char **, char **),
void *p3);
int (*finalize)(sqlite4_stmt *p0);
void (*free)(sqlite4_env *p0, void *p1);
void (*interrupt)(sqlite4 *p0);
#if 0
sqlite4_int64 (*last_insert_rowid)(sqlite4 *p0);
#endif
const char * (*libversion)(void);
#if 0
int (*load_extension)(sqlite4 *p0, const char *p1, const char *p2,
char **p3);
#endif
void * (*malloc)(sqlite4_env *p0, int p1);
char * (*mprintf)(sqlite4_env *p0, const char *p1, ...);
int (*open)(sqlite4_env *p0, const char *p1, sqlite4 **p2, ...);
int (*prepare)(sqlite4 *p0, const char *p1, int p2, sqlite4_stmt **p3,
int *p4);
void (*profile)(sqlite4 *p0, void *p1,
void (*p2)(void *, const char *, sqlite4_uint64),
void *p3);
void * (*realloc)(sqlite4_env *p0, void *p1, int p2);
int (*reset)(sqlite4_stmt *p0);
void (*result_blob)(sqlite4_context *p0, const void *p1,
int p2, void (*p3)(void *, void *), void *p4);
void (*result_error)(sqlite4_context *p0, const char *p1, int p2);
void (*result_int)(sqlite4_context *p0, int p1);
void (*result_null)(sqlite4_context *p0);
int (*step)(sqlite4_stmt *p0);
int (*xstrnicmp)(const char *p0, const char *p1, int p2);
#if 0
int (*table_column_metadata)(sqlite4 *p0, const char *p1,
const char *p2, const char *p3,
char const **p4, char const **p5,
int *p6, int *p7, int *p8);
#endif
void (*trace)(sqlite4 *p0, void *p1, void (*p2)(void *, const char *),
void *p2);
void * (*user_data)(sqlite4_context *p0);
const void * (*value_blob)(sqlite4_value *p0, int *p1);
int (*value_bytes)(sqlite4_value *p0);
const char * (*value_text)(sqlite4_value *p0, int *p1);
int (*value_type)(sqlite4_value *p0);
} dls_funcs;
#define sqlite4_bind_blob dls_funcs.bind_blob
#define sqlite4_bind_double dls_funcs.bind_double
#define sqlite4_bind_int dls_funcs.bind_int
#define sqlite4_bind_int64 dls_funcs.bind_int64
#define sqlite4_bind_null dls_funcs.bind_null
#define sqlite4_bind_parameter_count dls_funcs.bind_parameter_count
#define sqlite4_bind_text dls_funcs.bind_text
#define sqlite4_changes dls_funcs.changes
#define sqlite4_close dls_funcs.close
#define sqlite4_column_blob dls_funcs.column_blob
#define sqlite4_column_bytes dls_funcs.column_bytes
#define sqlite4_column_count dls_funcs.column_count
#define sqlite4_column_database_name dls_funcs.column_database_name
#define sqlite4_column_decltype dls_funcs.column_decltype
#define sqlite4_column_double dls_funcs.column_double
#define sqlite4_column_name dls_funcs.column_name
#define sqlite4_column_origin_name dls_funcs.column_origin_name
#define sqlite4_column_table_name dls_funcs.column_table_name
#define sqlite4_column_text dls_funcs.column_text
#define sqlite4_column_type dls_funcs.column_type
#define sqlite4_create_function dls_funcs.create_function
#define sqlite4_errcode dls_funcs.errcode
#define sqlite4_errmsg dls_funcs.errmsg
#define sqlite4_exec dls_funcs.exec
#define sqlite4_finalize dls_funcs.finalize
#define sqlite4_free dls_funcs.free
#define sqlite4_interrupt dls_funcs.interrupt
#if 0
#define sqlite4_last_insert_rowid dls_funcs.last_insert_rowid
#endif
#define sqlite4_libversion dls_funcs.libversion
#if 0
#define sqlite4_load_extension dls_funcs.load_extension
#endif
#define sqlite4_malloc dls_funcs.malloc
#define sqlite4_mprintf dls_funcs.mprintf
#define sqlite4_open dls_funcs.open
#define sqlite4_prepare dls_funcs.prepare
#define sqlite4_profile dls_funcs.profile
#define sqlite4_realloc dls_funcs.realloc
#define sqlite4_reset dls_funcs.reset
#define sqlite4_result_blob dls_funcs.result_blob
#define sqlite4_result_error dls_funcs.result_error
#define sqlite4_result_int dls_funcs.result_int
#define sqlite4_result_null dls_funcs.result_null
#define sqlite4_step dls_funcs.step
#define sqlite4_strnicmp dls_funcs.xstrnicmp
#if 0
#define sqlite4_table_column_metadata dls_funcs.table_column_metadata
#endif
#define sqlite4_trace dls_funcs.trace
#define sqlite4_user_data dls_funcs.user_data
#define sqlite4_value_blob dls_funcs.value_blob
#define sqlite4_value_bytes dls_funcs.value_bytes
#define sqlite4_value_text dls_funcs.value_text
#define sqlite4_value_type dls_funcs.value_type
#endif
#ifndef WITHOUT_WINTERFACE
#define WINTERFACE
#define WCHARSUPPORT
#endif
#if !defined(_WIN32) && !defined(_WIN64)
#if !defined(WCHARSUPPORT) && defined(HAVE_SQLWCHAR) && (HAVE_SQLWCHAR)
#define WCHARSUPPORT
#endif
#endif
#if defined(WINTERFACE)
#include <sqlucode.h>
#endif
#if defined(_WIN32) || defined(_WIN64)
#include "resource3.h"
#define ODBC_INI "ODBC.INI"
#ifndef DRIVER_VER_INFO
#define DRIVER_VER_INFO VERSION
#endif
#else
#define ODBC_INI ".odbc.ini"
#endif
#ifndef DRIVER_VER_INFO
#define DRIVER_VER_INFO "0.0"
#endif
#ifndef COLATTRIBUTE_LAST_ARG_TYPE
#ifdef _WIN64
#define COLATTRIBUTE_LAST_ARG_TYPE SQLLEN *
#else
#define COLATTRIBUTE_LAST_ARG_TYPE SQLPOINTER
#endif
#endif
#ifndef SETSTMTOPTION_LAST_ARG_TYPE
#define SETSTMTOPTION_LAST_ARG_TYPE SQLROWCOUNT
#endif
#undef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#undef max
#define max(a, b) ((a) < (b) ? (b) : (a))
#ifndef PTRDIFF_T
#define PTRDIFF_T int
#endif
#define array_size(x) (sizeof (x) / sizeof (x[0]))
#define stringify1(s) #s
#define stringify(s) stringify1(s)
#define verinfo(maj, min, lev) ((maj) << 16 | (min) << 8 | (lev))
/* Column types for static string column descriptions (SQLTables etc.) */
#if defined(WINTERFACE) && !defined(_WIN32) && !defined(_WIN64)
#define SCOL_VARCHAR SQL_WVARCHAR
#define SCOL_CHAR SQL_WCHAR
#else
#define SCOL_VARCHAR SQL_VARCHAR
#define SCOL_CHAR SQL_CHAR
#endif
#define ENV_MAGIC 0x53544145
#define DBC_MAGIC 0x53544144
#define DEAD_MAGIC 0xdeadbeef
/**
* @typedef dstr
* @struct dstr
* Internal structure representing dynamic strings.
*/
typedef struct dstr {
int len; /**< Current length. */
int max; /**< Maximum length of buffer. */
int oom; /**< True when out of memory. */
char buffer[1]; /**< String buffer. */
} dstr;
static const char *xdigits = "0123456789ABCDEFabcdef";
#ifdef MEMORY_DEBUG
static void *
xmalloc_(int n, char *file, int line)
{
int nn = n + 4 * sizeof (long);
long *p;
p = malloc(nn);
if (!p) {
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "malloc\t%d\tNULL\t%s:%d\n", n, file, line);
#endif
return NULL;
}
p[0] = 0xdead1234;
nn = nn / sizeof (long) - 1;
p[1] = n;
p[nn] = 0xdead5678;
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "malloc\t%d\t%p\t%s:%d\n", n, &p[2], file, line);
#endif
return (void *) &p[2];
}
static void *
xrealloc_(void *old, int n, char *file, int line)
{
int nn = n + 4 * sizeof (long), nnn;
long *p, *pp;
if (n == 0 || !old) {
return xmalloc_(n, file, line);
}
p = &((long *) old)[-2];
if (p[0] != 0xdead1234) {
fprintf(stderr, "*** low end corruption @ %p\n", old);
abort();
}
nnn = p[1] + 4 * sizeof (long);
nnn = nnn / sizeof (long) - 1;
if (p[nnn] != 0xdead5678) {
fprintf(stderr, "*** high end corruption @ %p\n", old);
abort();
}
pp = realloc(p, nn);
if (!pp) {
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "realloc\t%p,%d\tNULL\t%s:%d\n", old, n, file, line);
#endif
return NULL;
}
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "realloc\t%p,%d\t%p\t%s:%d\n", old, n, &pp[2], file, line);
#endif
p = pp;
p[1] = n;
nn = nn / sizeof (long) - 1;
p[nn] = 0xdead5678;
return (void *) &p[2];
}
static void
xfree_(void *x, char *file, int line)
{
long *p;
int n;
if (!x) {
return;
}
p = &((long *) x)[-2];
if (p[0] != 0xdead1234) {
fprintf(stderr, "*** low end corruption @ %p\n", x);
abort();
}
n = p[1] + 4 * sizeof (long);
n = n / sizeof (long) - 1;
if (p[n] != 0xdead5678) {
fprintf(stderr, "*** high end corruption @ %p\n", x);
abort();
}
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "free\t%p\t\t%s:%d\n", x, file, line);
#endif
free(p);
}
static void
xfree__(void *x)
{
xfree_(x, "unknown location", 0);
}
static char *
xstrdup_(const char *str, char *file, int line)
{
char *p;
if (!str) {
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "strdup\tNULL\tNULL\t%s:%d\n", file, line);
#endif
return NULL;
}
p = xmalloc_(strlen(str) + 1, file, line);
if (p) {
strcpy(p, str);
}
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "strdup\t%p\t%p\t%s:%d\n", str, p, file, line);
#endif
return p;
}
#define xmalloc(x) xmalloc_(x, __FILE__, __LINE__)
#define xrealloc(x,y) xrealloc_(x, y, __FILE__, __LINE__)
#define xfree(x) xfree_(x, __FILE__, __LINE__)
#define xstrdup(x) xstrdup_(x, __FILE__, __LINE__)
#else
#define xmalloc(x) sqlite4_malloc(0, x)
#define xrealloc(x,y) sqlite4_realloc(0, x, y)
#define xfree(x) sqlite4_free(0, x)
#define xstrdup(x) strdup_(x)
#endif
#if defined(_WIN32) || defined(_WIN64)
#define vsnprintf _vsnprintf
#define snprintf _snprintf
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#ifdef _MSC_VER
#define strtoll _strtoi64
#define strtoull _strtoui64
#endif
static HINSTANCE NEAR hModule; /* Saved module handle for resources */
#endif
#undef strncasecmp
#define strncasecmp(A,B,C) sqlite4_strnicmp(A,B,C)
#undef strcasecmp
#define strcasecmp(A,B) strcasecmp_(A,B)
#if defined(__GNUC__) && (__GNUC__ >= 2)
static int strcasecmp_(const char *a, const char *b)
__attribute__((__unused__));
#endif
static int strcasecmp_(const char *a, const char *b)
{
int c = strlen(a), d = strlen(b);
if (c > d) {
return strncasecmp(a, b, c);
}
return strncasecmp(a, b, d);
}
#if defined(_WIN32) || defined(_WIN64)
/*
* SQLHENV, SQLHDBC, and SQLHSTMT synchronization
* is done using a critical section in ENV and DBC
* structures.
*/
#define HDBC_LOCK(hdbc) \
{ \
DBC *d; \
\
if ((hdbc) == SQL_NULL_HDBC) { \
return SQL_INVALID_HANDLE; \
} \
d = (DBC *) (hdbc); \
if (d->magic != DBC_MAGIC) { \
return SQL_INVALID_HANDLE; \
} \
EnterCriticalSection(&d->cs); \
d->owner = GetCurrentThreadId(); \
}
#define HDBC_UNLOCK(hdbc) \
if ((hdbc) != SQL_NULL_HDBC) { \
DBC *d; \
\
d = (DBC *) (hdbc); \
if (d->magic == DBC_MAGIC) { \
LeaveCriticalSection(&d->cs); \
} \
}
#define HSTMT_LOCK(hstmt) \
{ \
DBC *d; \
\
if ((hstmt) == SQL_NULL_HSTMT) { \
return SQL_INVALID_HANDLE; \
} \
d = (DBC *) ((STMT *) (hstmt))->dbc; \
if (d->magic != DBC_MAGIC) { \
return SQL_INVALID_HANDLE; \
} \
if (d->env->magic != ENV_MAGIC) { \
return SQL_INVALID_HANDLE; \
} \
EnterCriticalSection(&d->cs); \
d->owner = GetCurrentThreadId(); \
}
#define HSTMT_UNLOCK(hstmt) \
if ((hstmt) != SQL_NULL_HSTMT) { \
DBC *d; \
\
d = (DBC *) ((STMT *) (hstmt))->dbc; \
if (d->magic == DBC_MAGIC) { \
LeaveCriticalSection(&d->cs); \
} \
}
#else
/*
* On UN*X assume that we are single-threaded or
* the driver manager provides serialization for us.
*
* In iODBC (3.52.x) serialization can be turned
* on using the DSN property "ThreadManager=yes".
*
* In unixODBC that property is named
* "Threading=0-3" and takes one of these values:
*
* 0 - no protection
* 1 - statement level protection
* 2 - connection level protection
* 3 - environment level protection
*
* unixODBC 2.2.11 uses environment level protection
* by default when it has been built with pthread
* support.
*/
#define HDBC_LOCK(hdbc)
#define HDBC_UNLOCK(hdbc)
#define HSTMT_LOCK(hdbc)
#define HSTMT_UNLOCK(hdbc)
#endif
/*
* tolower() replacement w/o locale
*/
static const char upper_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char lower_chars[] = "abcdefghijklmnopqrstuvwxyz";
static int
TOLOWER(int c)
{
if (c) {
char *p = strchr(upper_chars, c);
if (p) {
c = lower_chars[p - upper_chars];
}
}
return c;
}
/*
* isdigit() replacement w/o ctype.h
*/
static const char digit_chars[] = "0123456789";
#define ISDIGIT(c) \
((c) && strchr(digit_chars, (c)) != NULL)
/*
* isspace() replacement w/o ctype.h
*/
static const char space_chars[] = " \f\n\r\t\v";
#define ISSPACE(c) \
((c) && strchr(space_chars, (c)) != NULL)
/*
* Forward declarations of static functions.
*/
static void dbtraceapi(DBC *d, char *fn, const char *sql);
static void freedyncols(STMT *s);
static void freeresult(STMT *s, int clrcols);
static void freerows(char **rowp);
static void unbindcols(STMT *s);
static void s4stmt_drop(STMT *s);
static SQLRETURN drvexecute(SQLHSTMT stmt, int initial);
static SQLRETURN freestmt(HSTMT stmt);
static SQLRETURN mkbindcols(STMT *s, int ncols);
static SQLRETURN setupdyncols(STMT *s, sqlite4_stmt *s4stmt, int *ncolsp);
static SQLRETURN setupparbuf(STMT *s, BINDPARM *p);
static SQLRETURN starttran(STMT *s);
static SQLRETURN setupparam(STMT *s, char *sql, int pnum);
static SQLRETURN getrowdata(STMT *s, SQLUSMALLINT col, SQLSMALLINT otype,
SQLPOINTER val, SQLINTEGER len, SQLLEN *lenp,
int partial);
#if (defined(_WIN32) || defined(_WIN64)) && defined(WINTERFACE)
/* MS Access hack part 1 (reserved error -7748) */
static COL *statSpec2P, *statSpec3P;
#endif
#if (MEMORY_DEBUG < 1)
/**
* Duplicate string using xmalloc().
* @param str string to be duplicated
* @result pointer to new string or NULL
*/
static char *
strdup_(const char *str)
{
char *p = NULL;
if (str) {
p = xmalloc(strlen(str) + 1);
if (p) {
strcpy(p, str);
}
}
return p;
}
#endif
/**
* Append string to dynamic string.
* @param dsp dstr pointer
* @param str string to append
* @result dsp result dstr pointer or NULL.
*/
static dstr *
dsappend(dstr *dsp, const char *str)
{
int len;
if (!str) {
return dsp;
}
len = strlen(str);
if (!dsp) {
int max = 256;
if (max < len) {
max += len;
}
dsp = xmalloc(max + sizeof (*dsp));
if (dsp) {
dsp->max = max;
dsp->len = dsp->oom = 0;
goto copy;
}
return dsp;
}
if (dsp->oom) {
return dsp;
}
if (dsp->len + len > dsp->max) {
int max = dsp->max + len + 256;
dstr *ndsp = xrealloc(dsp, max + sizeof (*dsp));
if (!ndsp) {
strcpy(dsp->buffer, "OUT OF MEMORY");
dsp->max = dsp->len = 13;
dsp->oom = 1;
return dsp;
}
dsp = ndsp;
dsp->max = max;
}
copy:
strcpy(dsp->buffer + dsp->len, str);
dsp->len += len;
return dsp;
}
/**
* Append a string double quoted to dynamic string.
* @param dsp dstr pointer
* @param str string to append
* @result dsp result dstr pointer or NULL.
*/
static dstr *
dsappendq(dstr *dsp, const char *str)
{
int len;
const char *p;
char *q;
if (!str) {
return dsp;
}
len = strlen(str);
for (p = str; *p; ++p) {
if (p[0] == '"') {
++len;
}
}
len += 2;
if (!dsp) {
int max = 256;
if (max < len) {
max += len;
}
dsp = xmalloc(max + sizeof (*dsp));
if (dsp) {
dsp->max = max;
dsp->len = dsp->oom = 0;
goto copy;
}
return dsp;
}
if (dsp->oom) {
return dsp;
}
if (dsp->len + len > dsp->max) {
int max = dsp->max + len + 256;
dstr *ndsp = xrealloc(dsp, max + sizeof (*dsp));
if (!ndsp) {
strcpy(dsp->buffer, "OUT OF MEMORY");
dsp->max = dsp->len = 13;
dsp->oom = 1;
return dsp;
}
dsp = ndsp;
dsp->max = max;
}
copy:
q = dsp->buffer + dsp->len;
*q++ = '"';
for (p = str; *p; ++p) {
*q++ = *p;
if (p[0] == '"') {
*q++ = '"';
}
}
*q++ = '"';
*q = '\0';
dsp->len += len;
return dsp;
}
/**
* Return dynamic string's value.
* @param dsp dstr pointer
* @result string value
*/
static const char *
dsval(dstr *dsp)
{
if (dsp) {
return (const char *) dsp->buffer;
}
return "ERROR";
}
/**
* Check error on dynamic string.
* @param dsp dstr pointer
* @result true when error pending
*/
static int
dserr(dstr *dsp)
{
return !dsp || dsp->oom;
}
/**
* Free dynamic string.
* @param dsp dstr pointer
*/
static void
dsfree(dstr *dsp)
{
if (dsp) {
xfree(dsp);
}
}
#ifdef WCHARSUPPORT
/**
* Return length of UNICODE string.
* @param str UNICODE string
* @result length of string in characters
*/
static int
uc_strlen(SQLWCHAR *str)
{
int len = 0;
if (str) {
while (*str) {
++len;
++str;
}
}
return len;
}
/**
* Copy UNICODE string like strncpy().
* @param dest destination area
* @param src source area
* @param len length of source area in characters
* @return pointer to destination area
*/
static SQLWCHAR *
uc_strncpy(SQLWCHAR *dest, SQLWCHAR *src, int len)
{
int i = 0;
while (i < len) {
if (!src[i]) {
break;
}
dest[i] = src[i];
++i;
}
if (i < len) {
dest[i] = 0;
}
return dest;
}
/**
* Make UNICODE string from UTF8 string into buffer.
* @param str UTF8 string to be converted
* @param len length in characters of str or -1
* @param uc destination area to receive UNICODE string
* @param ucLen byte length of destination area
*/
static void
uc_from_utf_buf(unsigned char *str, int len, SQLWCHAR *uc, int ucLen)
{
ucLen = ucLen / sizeof (SQLWCHAR);
if (!uc || ucLen < 0) {
return;
}
if (len < 0) {
len = ucLen * 5;
}
uc[0] = 0;
if (str) {
int i = 0;
while (i < len && *str && i < ucLen) {
unsigned char c = str[0];
if (c < 0x80) {
uc[i++] = c;
++str;
} else if (c <= 0xc1 || c >= 0xf5) {
/* illegal, ignored */
++str;
} else if (c < 0xe0) {
if ((str[1] & 0xc0) == 0x80) {
unsigned long t = ((c & 0x1f) << 6) | (str[1] & 0x3f);
uc[i++] = t;
str += 2;
} else {
uc[i++] = c;
++str;
}
} else if (c < 0xf0) {
if ((str[1] & 0xc0) == 0x80 && (str[2] & 0xc0) == 0x80) {
unsigned long t = ((c & 0x0f) << 12) |
((str[1] & 0x3f) << 6) | (str[2] & 0x3f);
uc[i++] = t;
str += 3;
} else {
uc[i++] = c;
++str;
}
} else if (c < 0xf8) {
if ((str[1] & 0xc0) == 0x80 && (str[2] & 0xc0) == 0x80 &&
(str[3] & 0xc0) == 0x80) {
unsigned long t = ((c & 0x03) << 18) |
((str[1] & 0x3f) << 12) | ((str[2] & 0x3f) << 6) |
(str[3] & 0x3f);
if (sizeof (SQLWCHAR) == 2 * sizeof (char) &&
t >= 0x10000) {
t -= 0x10000;
uc[i++] = 0xd800 | ((t >> 10) & 0x3ff);
if (i >= ucLen) {
break;
}
t = 0xdc00 | (t & 0x3ff);
}
uc[i++] = t;
str += 4;
} else {
uc[i++] = c;
++str;
}
}
}
if (i < ucLen) {
uc[i] = 0;
}
}
}
/**
* Make UNICODE string from UTF8 string.
* @param str UTF8 string to be converted
* @param len length of UTF8 string
* @return alloc'ed UNICODE string to be free'd by uc_free()
*/
static SQLWCHAR *
uc_from_utf(unsigned char *str, int len)
{
SQLWCHAR *uc = NULL;
int ucLen;
if (str) {
if (len == SQL_NTS) {
len = strlen((char *) str);
}
ucLen = sizeof (SQLWCHAR) * (len + 1);
uc = xmalloc(ucLen);
if (uc) {
uc_from_utf_buf(str, len, uc, ucLen);
}
}
return uc;
}
/**
* Make UTF8 string from UNICODE string.
* @param str UNICODE string to be converted
* @param len length of UNICODE string in bytes
* @return alloc'ed UTF8 string to be free'd by uc_free()
*/
static char *
uc_to_utf(SQLWCHAR *str, int len)
{
int i;
char *cp, *ret = NULL;
if (!str) {
return ret;
}
if (len == SQL_NTS) {
len = uc_strlen(str);
} else {
len = len / sizeof (SQLWCHAR);
}
cp = xmalloc(len * 6 + 1);
if (!cp) {
return ret;
}
ret = cp;
for (i = 0; i < len; i++) {
unsigned long c = str[i];
if (sizeof (SQLWCHAR) == 2 * sizeof (char)) {
c &= 0xffff;
}
if (c < 0x80) {
*cp++ = c;
} else if (c < 0x800) {
*cp++ = 0xc0 | ((c >> 6) & 0x1f);
*cp++ = 0x80 | (c & 0x3f);
} else if (c < 0x10000) {
if (sizeof (SQLWCHAR) == 2 * sizeof (char) &&
c >= 0xd800 && c <= 0xdbff && i + 1 < len) {
unsigned long c2 = str[i + 1] & 0xffff;
if (c2 >= 0xdc00 && c2 <= 0xdfff) {
c = (((c & 0x3ff) << 10) | (c2 & 0x3ff)) + 0x10000;
*cp++ = 0xf0 | ((c >> 18) & 0x07);
*cp++ = 0x80 | ((c >> 12) & 0x3f);
*cp++ = 0x80 | ((c >> 6) & 0x3f);
*cp++ = 0x80 | (c & 0x3f);
++i;
continue;
}
}
*cp++ = 0xe0 | ((c >> 12) & 0x0f);
*cp++ = 0x80 | ((c >> 6) & 0x3f);
*cp++ = 0x80 | (c & 0x3f);
} else if (c <= 0x10ffff) {
*cp++ = 0xf0 | ((c >> 18) & 0x07);
*cp++ = 0x80 | ((c >> 12) & 0x3f);
*cp++ = 0x80 | ((c >> 6) & 0x3f);
*cp++ = 0x80 | (c & 0x3f);
}
}
*cp = '\0';
return ret;
}
#endif
#ifdef WINTERFACE
/**
* Make UTF8 string from UNICODE string.
* @param str UNICODE string to be converted
* @param len length of UNICODE string in characters
* @return alloc'ed UTF8 string to be free'd by uc_free()
*/
static char *
uc_to_utf_c(SQLWCHAR *str, int len)
{
if (len != SQL_NTS) {