-
Notifications
You must be signed in to change notification settings - Fork 15
/
win_unicode.c
1316 lines (1217 loc) · 29.1 KB
/
win_unicode.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
/*-------
* Module: win_unicode.c
*
* Description: This module contains utf8 <-> ucs2 conversion routines
* under WIndows
*
*-------
*/
#ifdef UNICODE_SUPPORT
#include "unicode_support.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef WIN32
#define FORMAT_SIZE_T "%Iu"
#else
#define FORMAT_SIZE_T "%zu"
#endif
#if (defined(__STDC_ISO_10646__) && defined(HAVE_MBSTOWCS) && defined(HAVE_WCSTOMBS)) || defined(WIN32)
#define __WCS_ISO10646__
static BOOL use_wcs = FALSE;
#endif
#if (defined(__STDC_UTF_16__) && defined(HAVE_UCHAR_H) && defined(HAVE_MBRTOC16) && defined(HAVE_C16RTOMB))
#define __CHAR16_UTF_16__
#include <uchar.h>
static BOOL use_c16 = FALSE;
#endif
static int convtype = -1;
int get_convtype(void)
{
const UCHAR *cdt;
#if defined(__WCS_ISO10646__)
if (convtype < 0)
{
wchar_t *wdt = L"a";
int sizeof_w = sizeof(wchar_t);
cdt = (UCHAR *) wdt;
switch (sizeof_w)
{
case 2:
if ('a' == cdt[0] &&
'\0' == cdt[1] &&
'\0' == cdt[2] &&
'\0' == cdt[3])
{
MYLOG(0, " UTF-16LE detected\n");
convtype = WCSTYPE_UTF16_LE;
use_wcs = TRUE;
}
break;
case 4:
if ('a' == cdt[0] &&
'\0' == cdt[1] &&
'\0' == cdt[2] &&
'\0' == cdt[3] &&
'\0' == cdt[4] &&
'\0' == cdt[5] &&
'\0' == cdt[6] &&
'\0' == cdt[7])
{
MYLOG(0, " UTF32-LE detected\n");
convtype = WCSTYPE_UTF32_LE;
use_wcs = TRUE;
}
break;
}
}
#endif /* __WCS_ISO10646__ */
#ifdef __CHAR16_UTF_16__
if (convtype < 0)
{
char16_t *c16dt = u"a";
cdt = (UCHAR *) c16dt;
if ('a' == cdt[0] &&
'\0' == cdt[1] &&
'\0' == cdt[2] &&
'\0' == cdt[3])
{
MYLOG(0, " C16_UTF-16LE detected\n");
convtype = C16TYPE_UTF16_LE;
use_c16 = TRUE;
}
}
#endif /* __CHAR16_UTF_16__ */
if (convtype < 0)
convtype = CONVTYPE_UNKNOWN; /* unknown */
return convtype;
}
#define byte3check 0xfffff800
#define byte2_base 0x80c0
#define byte2_mask1 0x07c0
#define byte2_mask2 0x003f
#define byte3_base 0x8080e0
#define byte3_mask1 0xf000
#define byte3_mask2 0x0fc0
#define byte3_mask3 0x003f
#define surrog_check 0xfc00
#define surrog1_bits 0xd800
#define surrog2_bits 0xdc00
#define byte4_base 0x808080f0
#define byte4_sr1_mask1 0x0700
#define byte4_sr1_mask2 0x00fc
#define byte4_sr1_mask3 0x0003
#define byte4_sr2_mask1 0x03c0
#define byte4_sr2_mask2 0x003f
#define surrogate_adjust (0x10000 >> 10)
static int little_endian = -1;
SQLULEN ucs2strlen(const SQLWCHAR *ucs2str)
{
SQLULEN len;
for (len = 0; ucs2str[len]; len++)
;
return len;
}
char *ucs2_to_utf8(const SQLWCHAR *ucs2str, SQLLEN ilen, SQLLEN *olen, BOOL lower_identifier)
{
char * utf8str;
int len = 0;
MYLOG(0, "%p ilen=" FORMAT_LEN " ", ucs2str, ilen);
if (!ucs2str)
{
if (olen)
*olen = SQL_NULL_DATA;
return NULL;
}
if (little_endian < 0)
{
int crt = 1;
little_endian = (0 != ((char *) &crt)[0]);
}
if (ilen < 0)
ilen = ucs2strlen(ucs2str);
MYPRINTF(0, " newlen=" FORMAT_LEN, ilen);
utf8str = (char *) malloc(ilen * 4 + 1);
if (utf8str)
{
int i = 0;
UInt2 byte2code;
Int4 byte4code, surrd1, surrd2;
const SQLWCHAR *wstr;
for (i = 0, wstr = ucs2str; i < ilen; i++, wstr++)
{
if (!*wstr)
break;
else if (0 == (*wstr & 0xffffff80)) /* ASCII */
{
if (lower_identifier)
utf8str[len++] = (char) tolower(*wstr);
else
utf8str[len++] = (char) *wstr;
}
else if ((*wstr & byte3check) == 0)
{
byte2code = byte2_base |
((byte2_mask1 & *wstr) >> 6) |
((byte2_mask2 & *wstr) << 8);
if (little_endian)
memcpy(utf8str + len, (char *) &byte2code, sizeof(byte2code));
else
{
utf8str[len] = ((char *) &byte2code)[1];
utf8str[len + 1] = ((char *) &byte2code)[0];
}
len += sizeof(byte2code);
}
/* surrogate pair check for non ucs-2 code */
else if (surrog1_bits == (*wstr & surrog_check))
{
surrd1 = (*wstr & ~surrog_check) + surrogate_adjust;
wstr++;
i++;
surrd2 = (*wstr & ~surrog_check);
byte4code = byte4_base |
((byte4_sr1_mask1 & surrd1) >> 8) |
((byte4_sr1_mask2 & surrd1) << 6) |
((byte4_sr1_mask3 & surrd1) << 20) |
((byte4_sr2_mask1 & surrd2) << 10) |
((byte4_sr2_mask2 & surrd2) << 24);
if (little_endian)
memcpy(utf8str + len, (char *) &byte4code, sizeof(byte4code));
else
{
utf8str[len] = ((char *) &byte4code)[3];
utf8str[len + 1] = ((char *) &byte4code)[2];
utf8str[len + 2] = ((char *) &byte4code)[1];
utf8str[len + 3] = ((char *) &byte4code)[0];
}
len += sizeof(byte4code);
}
else
{
byte4code = byte3_base |
((byte3_mask1 & *wstr) >> 12) |
((byte3_mask2 & *wstr) << 2) |
((byte3_mask3 & *wstr) << 16);
if (little_endian)
memcpy(utf8str + len, (char *) &byte4code, 3);
else
{
utf8str[len] = ((char *) &byte4code)[3];
utf8str[len + 1] = ((char *) &byte4code)[2];
utf8str[len + 2] = ((char *) &byte4code)[1];
}
len += 3;
}
}
utf8str[len] = '\0';
if (olen)
*olen = len;
}
MYPRINTF(0, " olen=%d utf8str=%s\n", len, utf8str ? utf8str : "");
return utf8str;
}
#define byte3_m1 0x0f
#define byte3_m2 0x3f
#define byte3_m3 0x3f
#define byte2_m1 0x1f
#define byte2_m2 0x3f
#define byte4_m1 0x07
#define byte4_m2 0x3f
#define byte4_m31 0x30
#define byte4_m32 0x0f
#define byte4_m4 0x3f
/*
* Convert a string from UTF-8 encoding to UCS-2.
*
* utf8str - input string in UTF-8
* ilen - length of input string in bytes (or minus)
* lfconv - TRUE if line feeds (LF) should be converted to CR + LF
* ucs2str - output buffer
* bufcount - size of output buffer
* errcheck - if TRUE, check for invalidly encoded input characters
*
* Returns the number of SQLWCHARs copied to output buffer. If the output
* buffer is too small, the output is truncated. The output string is
* NULL-terminated, except when the output is truncated.
*/
SQLULEN
utf8_to_ucs2_lf(const char *utf8str, SQLLEN ilen, BOOL lfconv,
SQLWCHAR *ucs2str, SQLULEN bufcount, BOOL errcheck)
{
int i;
SQLULEN rtn, ocount, wcode;
const UCHAR *str;
MYLOG(DETAIL_LOG_LEVEL, "ilen=" FORMAT_LEN " bufcount=" FORMAT_ULEN, ilen, bufcount);
if (!utf8str)
return 0;
MYPRINTF(DETAIL_LOG_LEVEL, " string=%s", utf8str);
if (!bufcount)
ucs2str = NULL;
else if (!ucs2str)
bufcount = 0;
if (ilen < 0)
ilen = strlen(utf8str);
for (i = 0, ocount = 0, str = (SQLCHAR *) utf8str; i < ilen && *str;)
{
if ((*str & 0x80) == 0)
{
if (lfconv && PG_LINEFEED == *str &&
(i == 0 || PG_CARRIAGE_RETURN != str[-1]))
{
if (ocount < bufcount)
ucs2str[ocount] = PG_CARRIAGE_RETURN;
ocount++;
}
if (ocount < bufcount)
ucs2str[ocount] = *str;
ocount++;
i++;
str++;
}
else if (0xf8 == (*str & 0xf8)) /* more than 5 byte code */
{
ocount = (SQLULEN) -1;
goto cleanup;
}
else if (0xf0 == (*str & 0xf8)) /* 4 byte code */
{
if (errcheck)
{
if (i + 4 > ilen ||
0 == (str[1] & 0x80) ||
0 == (str[2] & 0x80) ||
0 == (str[3] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = (surrog1_bits |
((((UInt4) *str) & byte4_m1) << 8) |
((((UInt4) str[1]) & byte4_m2) << 2) |
((((UInt4) str[2]) & byte4_m31) >> 4))
- surrogate_adjust;
ucs2str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
if (ocount < bufcount)
{
wcode = surrog2_bits |
((((UInt4) str[2]) & byte4_m32) << 6) |
(((UInt4) str[3]) & byte4_m4);
ucs2str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
i += 4;
str += 4;
}
else if (0xe0 == (*str & 0xf0)) /* 3 byte code */
{
if (errcheck)
{
if (i + 3 > ilen ||
0 == (str[1] & 0x80) ||
0 == (str[2] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = ((((UInt4) *str) & byte3_m1) << 12) |
((((UInt4) str[1]) & byte3_m2) << 6) |
(((UInt4) str[2]) & byte3_m3);
ucs2str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
i += 3;
str += 3;
}
else if (0xc0 == (*str & 0xe0)) /* 2 byte code */
{
if (errcheck)
{
if (i + 2 > ilen ||
0 == (str[1] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = ((((UInt4) *str) & byte2_m1) << 6) |
(((UInt4) str[1]) & byte2_m2);
ucs2str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
i += 2;
str += 2;
}
else
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
cleanup:
rtn = ocount;
if (ocount == (SQLULEN) -1)
{
if (!errcheck)
rtn = 0;
ocount = 0;
}
if (ocount < bufcount && ucs2str)
ucs2str[ocount] = 0;
MYPRINTF(DETAIL_LOG_LEVEL, " ocount=" FORMAT_ULEN "\n", ocount);
return rtn;
}
#ifdef __WCS_ISO10646__
/* UCS4 => utf8 */
#define byte4check 0xffff0000
#define byte4_check 0x10000
#define byte4_mask1 0x1c0000
#define byte4_mask2 0x3f000
#define byte4_mask3 0x0fc0
#define byte4_mask4 0x003f
#define byte4_m3 0x3f
static
SQLULEN ucs4strlen(const UInt4 *ucs4str)
{
SQLULEN len;
for (len = 0; ucs4str[len]; len++)
;
return len;
}
static
char *ucs4_to_utf8(const UInt4 *ucs4str, SQLLEN ilen, SQLLEN *olen, BOOL lower_identifier)
{
char * utf8str;
int len = 0;
MYLOG(0, " %p ilen=" FORMAT_LEN "\n", ucs4str, ilen);
if (!ucs4str)
{
if (olen)
*olen = SQL_NULL_DATA;
return NULL;
}
if (little_endian < 0)
{
int crt = 1;
little_endian = (0 != ((char *) &crt)[0]);
}
if (ilen < 0)
ilen = ucs4strlen(ucs4str);
MYLOG(0, " newlen=" FORMAT_LEN "\n", ilen);
utf8str = (char *) malloc(ilen * 4 + 1);
if (utf8str)
{
int i;
UInt2 byte2code;
Int4 byte4code;
const UInt4 *wstr;
for (i = 0, wstr = ucs4str; i < ilen; i++, wstr++)
{
if (!*wstr)
break;
else if (0 == (*wstr & 0xffffff80)) /* ASCII */
{
if (lower_identifier)
utf8str[len++] = (char) tolower(*wstr);
else
utf8str[len++] = (char) *wstr;
}
else if ((*wstr & byte3check) == 0)
{
byte2code = byte2_base |
((byte2_mask1 & *wstr) >> 6) |
((byte2_mask2 & *wstr) << 8);
if (little_endian)
memcpy(utf8str + len, (char *) &byte2code, sizeof(byte2code));
else
{
utf8str[len] = ((char *) &byte2code)[1];
utf8str[len + 1] = ((char *) &byte2code)[0];
}
len += sizeof(byte2code);
}
else if ((*wstr & byte4check) == 0)
{
byte4code = byte3_base |
((byte3_mask1 & *wstr) >> 12) |
((byte3_mask2 & *wstr) << 2) |
((byte3_mask3 & *wstr) << 16);
if (little_endian)
memcpy(utf8str + len, (char *) &byte4code, 3);
else
{
utf8str[len] = ((char *) &byte4code)[3];
utf8str[len + 1] = ((char *) &byte4code)[2];
utf8str[len + 2] = ((char *) &byte4code)[1];
}
len += 3;
}
else
{
byte4code = byte4_base |
((byte4_mask1 & *wstr) >> 18) |
((byte4_mask2 & *wstr) >> 4) |
((byte4_mask3 & *wstr) << 10) |
((byte4_mask4 & *wstr) << 24);
/* MYLOG(0, " %08x->%08x\n", *wstr, byte4code); */
if (little_endian)
memcpy(utf8str + len, (char *) &byte4code, sizeof(byte4code));
else
{
utf8str[len] = ((char *) &byte4code)[3];
utf8str[len + 1] = ((char *) &byte4code)[2];
utf8str[len + 2] = ((char *) &byte4code)[1];
utf8str[len + 3] = ((char *) &byte4code)[0];
}
len += sizeof(byte4code);
}
}
utf8str[len] = '\0';
if (olen)
*olen = len;
}
MYLOG(0, " olen=%d %s\n", len, utf8str ? utf8str : "");
return utf8str;
}
/*
* Convert a string from UTF-8 encoding to UTF-32.
*
* utf8str - input string in UTF-8
* ilen - length of input string in bytes (or minus)
* lfconv - TRUE if line feeds (LF) should be converted to CR + LF
* ucs4str - output buffer
* bufcount - size of output buffer
* errcheck - if TRUE, check for invalidly encoded input characters
*
* Returns the number of UInt4s copied to output buffer. If the output
* buffer is too small, the output is truncated. The output string is
* NULL-terminated, except when the output is truncated.
*/
static
SQLULEN utf8_to_ucs4_lf(const char *utf8str, SQLLEN ilen, BOOL lfconv,
UInt4 *ucs4str, SQLULEN bufcount, BOOL errcheck)
{
int i;
SQLULEN rtn, ocount, wcode;
const UCHAR *str;
MYLOG(0, " ilen=" FORMAT_LEN " bufcount=" FORMAT_ULEN "\n", ilen, bufcount);
if (!utf8str)
return 0;
MYLOG(99, " string=%s\n", utf8str);
if (!bufcount)
ucs4str = NULL;
else if (!ucs4str)
bufcount = 0;
if (ilen < 0)
ilen = strlen(utf8str);
for (i = 0, ocount = 0, str = (SQLCHAR *) utf8str; i < ilen && *str;)
{
if ((*str & 0x80) == 0)
{
if (lfconv && PG_LINEFEED == *str &&
(i == 0 || PG_CARRIAGE_RETURN != str[-1]))
{
if (ocount < bufcount)
ucs4str[ocount] = PG_CARRIAGE_RETURN;
ocount++;
}
if (ocount < bufcount)
ucs4str[ocount] = *str;
ocount++;
i++;
str++;
}
else if (0xf8 == (*str & 0xf8)) /* more than 5 byte code */
{
ocount = (SQLULEN) -1;
goto cleanup;
}
else if (0xf0 == (*str & 0xf8)) /* 4 byte code */
{
if (errcheck)
{
if (i + 4 > ilen ||
0 == (str[1] & 0x80) ||
0 == (str[2] & 0x80) ||
0 == (str[3] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = (((((UInt4) *str) & byte4_m1) << 18) |
((((UInt4) str[1]) & byte4_m2) << 12) |
((((UInt4) str[2]) & byte4_m3) << 6)) |
(((UInt4) str[3]) & byte4_m4);
ucs4str[ocount] = wcode;
}
ocount++;
i += 4;
str += 4;
}
else if (0xe0 == (*str & 0xf0)) /* 3 byte code */
{
if (errcheck)
{
if (i + 3 > ilen ||
0 == (str[1] & 0x80) ||
0 == (str[2] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = ((((UInt4) *str) & byte3_m1) << 12) |
((((UInt4) str[1]) & byte3_m2) << 6) |
(((UInt4) str[2]) & byte3_m3);
ucs4str[ocount] = wcode;
}
ocount++;
i += 3;
str += 3;
}
else if (0xc0 == (*str & 0xe0)) /* 2 byte code */
{
if (errcheck)
{
if (i + 2 > ilen ||
0 == (str[1] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = ((((UInt4) *str) & byte2_m1) << 6) |
(((UInt4) str[1]) & byte2_m2);
ucs4str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
i += 2;
str += 2;
}
else
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
cleanup:
rtn = ocount;
if (ocount == (SQLULEN) -1)
{
if (!errcheck)
rtn = 0;
ocount = 0;
}
if (ocount < bufcount && ucs4str)
ucs4str[ocount] = 0;
MYLOG(0, " ocount=" FORMAT_ULEN "\n", ocount);
return rtn;
}
#define SURROGATE_CHECK 0xfc
#define SURROG1_BYTE 0xd8
#define SURROG2_BYTE 0xdc
static
int ucs4_to_ucs2_lf(const unsigned int *ucs4str, SQLLEN ilen, SQLWCHAR *ucs2str, int bufcount, BOOL lfconv)
{
int outlen = 0, i;
UCHAR *ucdt;
SQLWCHAR *sqlwdt, dmy_wchar;
UCHAR * const udt = (UCHAR *) &dmy_wchar;
unsigned int uintdt;
MYLOG(0, " ilen=" FORMAT_LEN " bufcount=%d\n", ilen, bufcount);
if (ilen < 0)
ilen = ucs4strlen(ucs4str);
for (i = 0; i < ilen && (uintdt = ucs4str[i]); i++)
{
sqlwdt = (SQLWCHAR *)&uintdt;
ucdt = (UCHAR *)&uintdt;
if (0 == sqlwdt[1])
{
if (lfconv && PG_LINEFEED == ucdt[0] &&
(i == 0 ||
PG_CARRIAGE_RETURN != *((UCHAR *)&ucs4str[i - 1]))
)
{
if (outlen < bufcount)
{
udt[0] = PG_CARRIAGE_RETURN;
udt[1] = 0;
ucs2str[outlen] = *((SQLWCHAR *) udt);
}
outlen++;
}
if (outlen < bufcount)
ucs2str[outlen] = sqlwdt[0];
outlen++;
continue;
}
sqlwdt[1]--;
udt[0] = ((0xfc & ucdt[1]) >> 2) | ((0x3 & ucdt[2]) << 6);
// printf("%02x", udt[0]);
udt[1] = SURROG1_BYTE | ((0xc & ucdt[2]) >> 2);
// printf("%02x", udt[1]);
if (outlen < bufcount)
ucs2str[outlen] = *((SQLWCHAR *)udt);
outlen++;
udt[0] = ucdt[0];
// printf("%02x", udt[0]);
udt[1] = SURROG2_BYTE | (0x3 & ucdt[1]);
// printf("%02x\n", udt[1]);
if (outlen < bufcount)
ucs2str[outlen] = *((SQLWCHAR *)udt);
outlen++;
}
if (outlen < bufcount)
ucs2str[outlen] = 0;
return outlen;
}
static
int ucs2_to_ucs4(const SQLWCHAR *ucs2str, SQLLEN ilen, unsigned int *ucs4str, int bufcount)
{
int outlen = 0, i;
UCHAR *ucdt;
SQLWCHAR sqlwdt;
unsigned int dmy_uint;
UCHAR * const udt = (UCHAR *) &dmy_uint;
MYLOG(0, " ilen=" FORMAT_LEN " bufcount=%d\n", ilen, bufcount);
if (ilen < 0)
ilen = ucs2strlen(ucs2str);
udt[3] = 0; /* always */
for (i = 0; i < ilen && (sqlwdt = ucs2str[i]); i++)
{
ucdt = (UCHAR *)(ucs2str + i);
// printf("IN=%x\n", sqlwdt);
if ((ucdt[1] & SURROGATE_CHECK) != SURROG1_BYTE)
{
// printf("SURROG1=%2x\n", ucdt[1] & SURROG1_BYTE);
if (outlen < bufcount)
{
udt[0] = ucdt[0];
udt[1] = ucdt[1];
udt[2] = 0;
ucs4str[outlen] = *((unsigned int *)udt);
}
outlen++;
continue;
}
/* surrogate pair */
udt[0] = ucdt[2];
udt[1] = (ucdt[3] & 0x3) | ((ucdt[0] & 0x3f) << 2);
udt[2] = (((ucdt[0] & 0xc0) >> 6) | ((ucdt[1] & 0x3) << 2)) + 1;
// udt[3] = 0; needless
if (outlen < bufcount)
ucs4str[outlen] = *((unsigned int *)udt);
outlen++;
i++;
}
if (outlen < bufcount)
ucs4str[outlen] = 0;
return outlen;
}
#endif /* __WCS_ISO10646__ */
#if defined(__WCS_ISO10646__)
static SQLULEN
utf8_to_wcs_lf(const char *utf8str, SQLLEN ilen, BOOL lfconv,
wchar_t *wcsstr, SQLULEN bufcount, BOOL errcheck)
{
switch (get_convtype())
{
case WCSTYPE_UTF16_LE:
return utf8_to_ucs2_lf(utf8str, ilen, lfconv,
(SQLWCHAR *) wcsstr, bufcount, errcheck);
case WCSTYPE_UTF32_LE:
return utf8_to_ucs4_lf(utf8str, ilen, lfconv,
(UInt4 *) wcsstr, bufcount, errcheck);
}
return -1;
}
static
char *wcs_to_utf8(const wchar_t *wcsstr, SQLLEN ilen, SQLLEN *olen, BOOL lower_identifier)
{
switch (get_convtype())
{
case WCSTYPE_UTF16_LE:
return ucs2_to_utf8((const SQLWCHAR *) wcsstr, ilen, olen, lower_identifier);
case WCSTYPE_UTF32_LE:
return ucs4_to_utf8((const UInt4 *) wcsstr, ilen, olen, lower_identifier);
}
return NULL;
}
/*
* Input strings must be NULL terminated.
* Output wide character strings would be NULL terminated. The result
* outmsg would be truncated when the buflen is small.
*
* The output NULL terminator is counted as buflen.
* if outmsg is NULL or buflen is 0, only output length is returned.
* As for return values, NULL terminators aren't counted.
*/
static
int msgtowstr(const char *inmsg, wchar_t *outmsg, int buflen)
{
int outlen = -1;
MYLOG(0, " inmsg=%p buflen=%d\n", inmsg, buflen);
#ifdef WIN32
if (NULL == outmsg)
buflen = 0;
if ((outlen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED
| MB_ERR_INVALID_CHARS, inmsg, -1, outmsg, buflen)) > 0)
outlen--;
else if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
outlen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED
| MB_ERR_INVALID_CHARS, inmsg, -1, NULL, 0) - 1;
else
outlen = -1;
#else
if (0 == buflen)
outmsg = NULL;
outlen = mbstowcs((wchar_t *) outmsg, inmsg, buflen);
#endif /* WIN32 */
if (outmsg && outlen >= buflen)
{
outmsg[buflen - 1] = 0;
MYLOG(0, " out=%dchars truncated to %d\n", outlen, buflen - 1);
}
MYLOG(0, " buf=%dchars out=%dchars\n", buflen, outlen);
return outlen;
}
/*
* Input wide character strings must be NULL terminated.
* Output strings would be NULL terminated. The result outmsg would be
* truncated when the buflen is small.
*
* The output NULL terminator is counted as buflen.
* if outmsg is NULL or buflen is 0, only output length is returned.
* As for return values, NULL terminators aren't counted.
*/
static
int wstrtomsg(const wchar_t *wstr, char *outmsg, int buflen)
{
int outlen = -1;
MYLOG(0, " wstr=%p buflen=%d\n", wstr, buflen);
#ifdef WIN32
if (NULL == outmsg)
buflen = 0;
if ((outlen = WideCharToMultiByte(CP_ACP, 0, wstr, -1, outmsg, buflen, NULL, NULL)) > 0)
outlen--;
else if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
outlen = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL) - 1;
else
outlen = -1;
#else
if (0 == buflen)
outmsg = NULL;
outlen = wcstombs(outmsg, wstr, buflen);
#endif /* WIN32 */
if (outmsg && outlen >= buflen)
{
outmsg[buflen - 1] = 0;
MYLOG(0, " out=%dbytes truncated to %d\n", outlen, buflen - 1);
}
MYLOG(0, " buf=%dbytes outlen=%dbytes\n", buflen, outlen);
return outlen;
}
#endif /* __WCS_ISO10646__ */
#if defined(__CHAR16_UTF_16__)
static mbstate_t initial_state;
static
SQLLEN mbstoc16_lf(char16_t *c16dt, const char *c8dt, size_t n, BOOL lf_conv)
{
int i;
size_t brtn;
const char *cdt;
mbstate_t mbst = initial_state;
MYLOG(0, " c16dt=%p size=" FORMAT_SIZE_T "\n", c16dt, n);
for (i = 0, cdt = c8dt; i < n || (!c16dt); i++)
{
if (lf_conv && PG_LINEFEED == *cdt && i > 0 && PG_CARRIAGE_RETURN != cdt[-1])
{
if (c16dt)
c16dt[i] = PG_CARRIAGE_RETURN;
i++;
}
brtn = mbrtoc16(c16dt ? c16dt + i : NULL, cdt, 4, &mbst);
if (0 == brtn)
break;
if (brtn == (size_t) -1 ||
brtn == (size_t) -2)
return -1;
if (brtn == (size_t) -3)
continue;
cdt += brtn;
}
if (c16dt && i >= n)
c16dt[n - 1] = 0;
return i;
}
static
SQLLEN c16tombs(char *c8dt, const char16_t *c16dt, size_t n)
{
int i;
SQLLEN result = 0;
size_t brtn;
char *cdt, c4byte[4];
mbstate_t mbst = initial_state;
MYLOG(0, " c8dt=%p size=" FORMAT_SIZE_T "u\n", c8dt, n);
if (!c8dt)
n = 0;
for (i = 0, cdt = c8dt; c16dt[i] && (result < n || (!cdt)); i++)
{
if (NULL != cdt && result + 4 < n)
brtn = c16rtomb(cdt, c16dt[i], &mbst);
else
{
brtn = c16rtomb(c4byte, c16dt[i], &mbst);
if (brtn < 5)
{
SQLLEN result_n = result + brtn;
if (result_n < n)
memcpy(cdt, c4byte, brtn);
else
{
if (cdt && n > 0)
{
c8dt[result] = '\0'; /* truncate */
return result_n;
}
}
}
}
/*
printf("c16dt=%04X brtn=%lu result=%ld cdt=%02X%02X%02X%02X\n",
c16dt[i], brtn, result, (UCHAR) cdt[0], (UCHAR) cdt[1], (UCHAR) cdt[2], (UCHAR) cdt[3]);
*/
if (brtn == (size_t) -1)
{
if (n > 0)
c8dt[n - 1] = '\0';
return -1;
}
if (cdt)
cdt += brtn;
result += brtn;
}
if (cdt)
*cdt = '\0';
return result;
}
#endif /* __CHAR16_UTF_16__ */
//
// SQLBindParameter SQL_C_CHAR to UTF-8 case
// the current locale => UTF-8
//
SQLLEN bindpara_msg_to_utf8(const char *ldt, char **wcsbuf, SQLLEN used)
{
SQLLEN l = (-2);
char *utf8 = NULL, *ldt_nts, *alloc_nts = NULL, ntsbuf[128];
int count;
if (SQL_NTS == used)
{
count = strlen(ldt);
ldt_nts = (char *) ldt;
}
else if (used < 0)
{
return -1;
}
else
{
count = used;