[1] | 1 | /* $Id: utf8-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - UTF8 helpers.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_UTF8
|
---|
[62592] | 42 | #include <iprt/win/windows.h>
|
---|
[1] | 43 |
|
---|
| 44 | #include <iprt/string.h>
|
---|
| 45 | #include <iprt/alloc.h>
|
---|
| 46 | #include <iprt/assert.h>
|
---|
| 47 | #include <iprt/err.h>
|
---|
[76409] | 48 | #include <iprt/utf16.h>
|
---|
[1] | 49 |
|
---|
| 50 |
|
---|
[80764] | 51 |
|
---|
[31157] | 52 | RTR3DECL(int) RTStrUtf8ToCurrentCPTag(char **ppszString, const char *pszString, const char *pszTag)
|
---|
[1] | 53 | {
|
---|
[80764] | 54 | return RTStrUtf8ToCurrentCPExTag(ppszString, pszString, RTSTR_MAX, pszTag);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | RTR3DECL(int) RTStrUtf8ToCurrentCPExTag(char **ppszString, const char *pszString, size_t cchString, const char *pszTag)
|
---|
| 59 | {
|
---|
[1] | 60 | Assert(ppszString);
|
---|
| 61 | Assert(pszString);
|
---|
[93059] | 62 | *ppszString = NULL;
|
---|
[1] | 63 |
|
---|
| 64 | /*
|
---|
[93059] | 65 | * If the ANSI codepage (CP_ACP) is UTF-8, no translation is needed.
|
---|
| 66 | * Same goes for empty strings.
|
---|
[1] | 67 | */
|
---|
[93059] | 68 | if ( cchString == 0
|
---|
| 69 | || *pszString == '\0')
|
---|
| 70 | return RTStrDupNExTag(ppszString, pszString, 0, pszTag);
|
---|
| 71 | if (GetACP() == CP_UTF8)
|
---|
[1] | 72 | {
|
---|
[93059] | 73 | int rc = RTStrValidateEncodingEx(pszString, cchString, 0);
|
---|
| 74 | AssertRCReturn(rc, rc);
|
---|
| 75 | return RTStrDupNExTag(ppszString, pszString, cchString, pszTag);
|
---|
[1] | 76 | }
|
---|
| 77 |
|
---|
| 78 | /*
|
---|
| 79 | * Convert to wide char first.
|
---|
| 80 | */
|
---|
[7418] | 81 | PRTUTF16 pwszString = NULL;
|
---|
[80764] | 82 | int rc = RTStrToUtf16Ex(pszString, cchString, &pwszString, 0, NULL);
|
---|
[1] | 83 | if (RT_FAILURE(rc))
|
---|
| 84 | return rc;
|
---|
| 85 |
|
---|
| 86 | /*
|
---|
| 87 | * First calc result string length.
|
---|
| 88 | */
|
---|
[7418] | 89 | int cbResult = WideCharToMultiByte(CP_ACP, 0, pwszString, -1, NULL, 0, NULL, NULL);
|
---|
[1] | 90 | if (cbResult > 0)
|
---|
| 91 | {
|
---|
| 92 | /*
|
---|
| 93 | * Alloc space for result buffer.
|
---|
| 94 | */
|
---|
[31157] | 95 | LPSTR lpString = (LPSTR)RTMemTmpAllocTag(cbResult, pszTag);
|
---|
[1] | 96 | if (lpString)
|
---|
| 97 | {
|
---|
| 98 | /*
|
---|
| 99 | * Do the translation.
|
---|
| 100 | */
|
---|
[7418] | 101 | if (WideCharToMultiByte(CP_ACP, 0, pwszString, -1, lpString, cbResult, NULL, NULL) > 0)
|
---|
[1] | 102 | {
|
---|
| 103 | /* ok */
|
---|
| 104 | *ppszString = lpString;
|
---|
[7418] | 105 | RTMemTmpFree(pwszString);
|
---|
[1] | 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 | }
|
---|
[7418] | 125 | RTMemTmpFree(pwszString);
|
---|
[1] | 126 | return rc;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[93640] | 129 | static int rtStrCPToUtf8Tag(char **ppszString, const char *pszString, uint32_t uCodePage, const char *pszTag)
|
---|
[1] | 130 | {
|
---|
| 131 | Assert(ppszString);
|
---|
| 132 | Assert(pszString);
|
---|
| 133 | *ppszString = NULL;
|
---|
| 134 |
|
---|
[93059] | 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)
|
---|
[1] | 142 | {
|
---|
[93059] | 143 | int rc = RTStrValidateEncoding(pszString);
|
---|
| 144 | AssertRCReturn(rc, rc);
|
---|
| 145 | return RTStrDupExTag(ppszString, pszString, pszTag);
|
---|
[1] | 146 | }
|
---|
| 147 |
|
---|
[93059] | 148 | /** @todo is there a quicker way? Currently: ACP -> UTF-16 -> UTF-8 */
|
---|
| 149 |
|
---|
[1] | 150 | /*
|
---|
| 151 | * First calc result string length.
|
---|
| 152 | */
|
---|
| 153 | int rc;
|
---|
[93640] | 154 | int cwc = MultiByteToWideChar((UINT)uCodePage, 0, pszString, -1, NULL, 0);
|
---|
[7418] | 155 | if (cwc > 0)
|
---|
[1] | 156 | {
|
---|
| 157 | /*
|
---|
| 158 | * Alloc space for result buffer.
|
---|
| 159 | */
|
---|
[7418] | 160 | PRTUTF16 pwszString = (PRTUTF16)RTMemTmpAlloc(cwc * sizeof(RTUTF16));
|
---|
| 161 | if (pwszString)
|
---|
[1] | 162 | {
|
---|
| 163 | /*
|
---|
| 164 | * Do the translation.
|
---|
| 165 | */
|
---|
[93640] | 166 | if (MultiByteToWideChar((UINT)uCodePage, 0, pszString, -1, pwszString, cwc) > 0)
|
---|
[1] | 167 | {
|
---|
| 168 | /*
|
---|
[7418] | 169 | * Now we got UTF-16, convert it to UTF-8
|
---|
[1] | 170 | */
|
---|
[7418] | 171 | rc = RTUtf16ToUtf8(pwszString, ppszString);
|
---|
| 172 | RTMemTmpFree(pwszString);
|
---|
[1] | 173 | return rc;
|
---|
| 174 | }
|
---|
[7418] | 175 | RTMemTmpFree(pwszString);
|
---|
[1] | 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 |
|
---|
[93640] | 194 |
|
---|
| 195 | RTR3DECL(int) RTStrCurrentCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag)
|
---|
| 196 | {
|
---|
| 197 | return rtStrCPToUtf8Tag(ppszString, pszString, CP_ACP, pszTag);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | RTR3DECL(int) RTStrConsoleCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag)
|
---|
| 202 | {
|
---|
| 203 | return rtStrCPToUtf8Tag(ppszString, pszString, GetConsoleCP(), pszTag);
|
---|
| 204 | }
|
---|