1 | /* $Id: utf8-win.cpp 93640 2022-02-07 14:02:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - UTF8 helpers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_UTF8
|
---|
32 | #include <iprt/win/windows.h>
|
---|
33 |
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/utf16.h>
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | RTR3DECL(int) RTStrUtf8ToCurrentCPTag(char **ppszString, const char *pszString, const char *pszTag)
|
---|
43 | {
|
---|
44 | return RTStrUtf8ToCurrentCPExTag(ppszString, pszString, RTSTR_MAX, pszTag);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | RTR3DECL(int) RTStrUtf8ToCurrentCPExTag(char **ppszString, const char *pszString, size_t cchString, const char *pszTag)
|
---|
49 | {
|
---|
50 | Assert(ppszString);
|
---|
51 | Assert(pszString);
|
---|
52 | *ppszString = NULL;
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * If the ANSI codepage (CP_ACP) is UTF-8, no translation is needed.
|
---|
56 | * Same goes for empty strings.
|
---|
57 | */
|
---|
58 | if ( cchString == 0
|
---|
59 | || *pszString == '\0')
|
---|
60 | return RTStrDupNExTag(ppszString, pszString, 0, pszTag);
|
---|
61 | if (GetACP() == CP_UTF8)
|
---|
62 | {
|
---|
63 | int rc = RTStrValidateEncodingEx(pszString, cchString, 0);
|
---|
64 | AssertRCReturn(rc, rc);
|
---|
65 | return RTStrDupNExTag(ppszString, pszString, cchString, pszTag);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Convert to wide char first.
|
---|
70 | */
|
---|
71 | PRTUTF16 pwszString = NULL;
|
---|
72 | int rc = RTStrToUtf16Ex(pszString, cchString, &pwszString, 0, NULL);
|
---|
73 | if (RT_FAILURE(rc))
|
---|
74 | return rc;
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * First calc result string length.
|
---|
78 | */
|
---|
79 | int cbResult = WideCharToMultiByte(CP_ACP, 0, pwszString, -1, NULL, 0, NULL, NULL);
|
---|
80 | if (cbResult > 0)
|
---|
81 | {
|
---|
82 | /*
|
---|
83 | * Alloc space for result buffer.
|
---|
84 | */
|
---|
85 | LPSTR lpString = (LPSTR)RTMemTmpAllocTag(cbResult, pszTag);
|
---|
86 | if (lpString)
|
---|
87 | {
|
---|
88 | /*
|
---|
89 | * Do the translation.
|
---|
90 | */
|
---|
91 | if (WideCharToMultiByte(CP_ACP, 0, pwszString, -1, lpString, cbResult, NULL, NULL) > 0)
|
---|
92 | {
|
---|
93 | /* ok */
|
---|
94 | *ppszString = lpString;
|
---|
95 | RTMemTmpFree(pwszString);
|
---|
96 | return VINF_SUCCESS;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* translation error */
|
---|
100 | int iLastErr = GetLastError();
|
---|
101 | AssertMsgFailed(("Unicode to ACP translation failed. lasterr=%d\n", iLastErr));
|
---|
102 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
103 | }
|
---|
104 | else
|
---|
105 | rc = VERR_NO_TMP_MEMORY;
|
---|
106 | RTMemTmpFree(lpString);
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | /* translation error */
|
---|
111 | int iLastErr = GetLastError();
|
---|
112 | AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
|
---|
113 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
114 | }
|
---|
115 | RTMemTmpFree(pwszString);
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static int rtStrCPToUtf8Tag(char **ppszString, const char *pszString, uint32_t uCodePage, const char *pszTag)
|
---|
120 | {
|
---|
121 | Assert(ppszString);
|
---|
122 | Assert(pszString);
|
---|
123 | *ppszString = NULL;
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * If the ANSI codepage (CP_ACP) is UTF-8, no translation is needed.
|
---|
127 | * Same goes for empty strings.
|
---|
128 | */
|
---|
129 | if (*pszString == '\0')
|
---|
130 | return RTStrDupExTag(ppszString, pszString, pszTag);
|
---|
131 | if (GetACP() == CP_UTF8)
|
---|
132 | {
|
---|
133 | int rc = RTStrValidateEncoding(pszString);
|
---|
134 | AssertRCReturn(rc, rc);
|
---|
135 | return RTStrDupExTag(ppszString, pszString, pszTag);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** @todo is there a quicker way? Currently: ACP -> UTF-16 -> UTF-8 */
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * First calc result string length.
|
---|
142 | */
|
---|
143 | int rc;
|
---|
144 | int cwc = MultiByteToWideChar((UINT)uCodePage, 0, pszString, -1, NULL, 0);
|
---|
145 | if (cwc > 0)
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * Alloc space for result buffer.
|
---|
149 | */
|
---|
150 | PRTUTF16 pwszString = (PRTUTF16)RTMemTmpAlloc(cwc * sizeof(RTUTF16));
|
---|
151 | if (pwszString)
|
---|
152 | {
|
---|
153 | /*
|
---|
154 | * Do the translation.
|
---|
155 | */
|
---|
156 | if (MultiByteToWideChar((UINT)uCodePage, 0, pszString, -1, pwszString, cwc) > 0)
|
---|
157 | {
|
---|
158 | /*
|
---|
159 | * Now we got UTF-16, convert it to UTF-8
|
---|
160 | */
|
---|
161 | rc = RTUtf16ToUtf8(pwszString, ppszString);
|
---|
162 | RTMemTmpFree(pwszString);
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 | RTMemTmpFree(pwszString);
|
---|
166 | /* translation error */
|
---|
167 | int iLastErr = GetLastError();
|
---|
168 | AssertMsgFailed(("ACP to Unicode translation failed. lasterr=%d\n", iLastErr));
|
---|
169 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
170 | }
|
---|
171 | else
|
---|
172 | rc = VERR_NO_TMP_MEMORY;
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | /* translation error */
|
---|
177 | int iLastErr = GetLastError();
|
---|
178 | AssertMsgFailed(("Unicode to ACP translation failed lasterr=%d\n", iLastErr));
|
---|
179 | rc = RTErrConvertFromWin32(iLastErr);
|
---|
180 | }
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | RTR3DECL(int) RTStrCurrentCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag)
|
---|
186 | {
|
---|
187 | return rtStrCPToUtf8Tag(ppszString, pszString, CP_ACP, pszTag);
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | RTR3DECL(int) RTStrConsoleCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag)
|
---|
192 | {
|
---|
193 | return rtStrCPToUtf8Tag(ppszString, pszString, GetConsoleCP(), pszTag);
|
---|
194 | }
|
---|