-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwin_unicode.c
730 lines (694 loc) · 16.9 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
/*-------
* Module: win_unicode.c
*
* Description: This module contains utf8 <-> ucs2 conversion routines
* under WIndows
*
*-------
*/
#ifdef UNICODE_SUPPORT
#include "psqlodbc.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#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;
/*mylog("ucs2_to_utf8 %p ilen=%d ", ucs2str, ilen);*/
if (!ucs2str)
{
*olen = SQL_NULL_DATA;
return NULL;
}
if (little_endian < 0)
{
int crt = 1;
little_endian = (0 != ((char *) &crt)[0]);
}
if (SQL_NTS == ilen)
ilen = ucs2strlen(ucs2str);
mylog(" newlen=%d", ilen);
utf8str = (char *) malloc(ilen * 4 + 1);
if (utf8str)
{
int i, len = 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;
}
mylog(" %s:olen=%d utf8str=%s\n", __FUNCTION__, *olen, 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 SQL_NTS)
* 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("utf8_to_ucs2 ilen=%d bufcount=%d", ilen, bufcount);*/
if (!utf8str)
return 0;
/*mylog(" string=%s\n", 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;
/*mylog(" ocount=%d\n", ocount);*/
return rtn;
}
/* 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(" %s:%p ilen=%d\n", __FUNCTION__, 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 (SQL_NTS == ilen)
ilen = ucs4strlen(ucs4str);
mylog(" newlen=%d\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(" %s:%08x->%08x\n", __FUNCTION__, *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(" %s:olen=%d %s\n", __FUNCTION__, 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 SQL_NTS)
* 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(" %s:ilen=%d bufcount=%d\n", __FUNCTION__, ilen, bufcount);
if (!utf8str)
return 0;
/*mylog(" 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(" %s:ocount=%d\n", __FUNCTION__, ocount);
return rtn;
}
static int wcstype = -1;
int get_wcstype(void)
{
wchar_t *wdt;
int sizeof_w = sizeof(wchar_t);
const UCHAR *cdt;
if (wcstype >= 0)
return wcstype;
#if defined(__STDC_ISO_10646__) || defined(WIN32)
wdt = L"a";
cdt = (UCHAR *) wdt;
switch (sizeof_w)
{
case 2:
if ('a' == cdt[0] &&
'\0' == cdt[1] &&
'\0' == cdt[2] &&
'\0' == cdt[3])
{
mylog(" %s:UTF-16LE detected\n", __FUNCTION__);
wcstype = WCSTYPE_UTF16_LE;
}
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(" %s:UTF32-LE detected\n", __FUNCTION__);
wcstype = WCSTYPE_UTF32_LE;
}
break;
}
#endif /* __STDC_ISO_10646__ */
if (wcstype < 0)
wcstype = WCSTYPE_UNKNOWN; /* unknown */
return wcstype;
}
SQLULEN
utf8_to_wcs_lf(const char *utf8str, SQLLEN ilen, BOOL lfconv,
wchar_t *wcsstr, SQLULEN bufcount, BOOL errcheck)
{
switch (get_wcstype())
{
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;
}
char *wcs_to_utf8(const wchar_t *wcsstr, SQLLEN ilen, SQLLEN *olen, BOOL lower_identifier)
{
switch (get_wcstype())
{
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.
*/
int msgtowstr(const char *inmsg, wchar_t *outmsg, int buflen)
{
int outlen = -1;
mylog(" %s:inmsg=%p buflen=%d\n", __FUNCTION__, 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
#ifdef HAVE_MBSTOWCS
#ifdef __STDC_ISO_10646__
if (0 == buflen)
outmsg = NULL;
outlen = mbstowcs((wchar_t *) outmsg, inmsg, buflen);
#endif /* __STDC_ISO_10646__ */
#endif /* HAVE_MBSTOWCS */
#endif /* WIN32 */
if (outmsg && outlen >= buflen)
{
outmsg[buflen - 1] = 0;
mylog(" %s:out=%dchars truncated to %d\n", __FUNCTION__, outlen, buflen - 1);
}
mylog(" %s in=%dchars out=%dchars\n", __FUNCTION__, 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.
*/
int wstrtomsg(const wchar_t *wstr, char *outmsg, int buflen)
{
int outlen = -1;
mylog(" %s:wstr=%p buflen=%d\n", __FUNCTION__, 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
#ifdef HAVE_MBSTOWCS
#ifdef __STDC_ISO_10646__
if (0 == buflen)
outmsg = NULL;
outlen = wcstombs(outmsg, wstr, buflen);
#endif /* __STDC_ISO_10646__ */
#endif /* HAVE_MBSTOWCS */
#endif /* WIN32 */
if (outmsg && outlen >= buflen)
{
outmsg[buflen - 1] = 0;
mylog(" %s:out=%dbytes truncated to %d\n", __FUNCTION__, outlen, buflen - 1);
}
mylog(" %s buf=%dbytes outlen=%dbytes\n", __FUNCTION__, buflen, outlen);
return outlen;
}
#endif /* UNICODE_SUPPORT */