VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/utf8-win.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette