8
8
#if defined(TS_USE_WIDE ) && defined(WIN32 )
9
9
10
10
size_t
11
- wchar2char ( const char * to , const wchar_t * from , size_t len ) {
11
+ wchar2char ( char * to , const wchar_t * from , size_t len ) {
12
12
if (GetDatabaseEncoding () == PG_UTF8 ) {
13
- int r ;
13
+ int r , nbytes ;
14
14
15
15
if (len == 0 )
16
16
return 0 ;
17
17
18
+ /* in any case, *to should be allocated with enough space */
19
+ nbytes = WideCharToMultiByte (CP_UTF8 , 0 , from , len , NULL , 0 , NULL , NULL );
20
+ if ( nbytes == 0 )
21
+ ereport (ERROR ,
22
+ (errcode (ERRCODE_CHARACTER_NOT_IN_REPERTOIRE ),
23
+ errmsg ("UTF-16 to UTF-8 translation failed: %lu" ,
24
+ GetLastError ())));
25
+
18
26
r = WideCharToMultiByte (CP_UTF8 , 0 , from , len , to , nbytes ,
19
27
NULL , NULL );
20
28
21
-
22
29
if ( r == 0 )
23
30
ereport (ERROR ,
24
31
(errcode (ERRCODE_CHARACTER_NOT_IN_REPERTOIRE ),
25
32
errmsg ("UTF-16 to UTF-8 translation failed: %lu" ,
26
33
GetLastError ())));
27
-
28
34
return r ;
29
35
}
30
36
31
37
return wcstombs (to , from , len );
32
38
}
33
39
34
40
size_t
35
- char2wchar ( const wchar_t * to , const char * from , size_t len ) {
41
+ char2wchar ( wchar_t * to , const char * from , size_t len ) {
36
42
if (GetDatabaseEncoding () == PG_UTF8 ) {
37
43
int r ;
38
44
39
45
if (len == 0 )
40
46
return 0 ;
41
47
42
- r = MultiByteToWideChar (CP_UTF8 , 0 , from , len ,
43
- to , len );
48
+ r = MultiByteToWideChar (CP_UTF8 , 0 , from , len , to , len );
44
49
45
50
if (!r ) {
46
51
pg_verifymbstr (from , len , false);
@@ -50,7 +55,7 @@ char2wchar( const wchar_t *to, const char *from, size_t len ) {
50
55
errhint ("The server's LC_CTYPE locale is probably incompatible with the database encoding." )));
51
56
}
52
57
53
- Assert (r <= nbytes );
58
+ Assert ( r <= len );
54
59
55
60
return r ;
56
61
}
0 commit comments