Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/instrument/EncodingSupport_md.c
32285 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24#include <windows.h>25#include <stdlib.h>26#include <stdio.h>272829/*30* Convert UTF-8 to a platform string31*/32int33convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {34LANGID langID;35LCID localeID;36TCHAR strCodePage[7]; // ANSI code page id37UINT codePage;38int wlen, plen;39WCHAR* wstr;4041/*42* Get the code page for this locale43*/44langID = LANGIDFROMLCID(GetUserDefaultLCID());45localeID = MAKELCID(langID, SORT_DEFAULT);46if (GetLocaleInfo(localeID, LOCALE_IDEFAULTANSICODEPAGE,47strCodePage, sizeof(strCodePage)/sizeof(TCHAR)) > 0 ) {48codePage = atoi(strCodePage);49} else {50codePage = GetACP();51}5253/*54* To convert the string to platform encoding we must first convert55* to unicode, and then convert to the platform encoding56*/57plen = -1;58wlen = MultiByteToWideChar(CP_UTF8, 0, utf8_str, utf8_len, NULL, 0);59if (wlen > 0) {60wstr = (WCHAR*)malloc(wlen * sizeof(WCHAR));61if (wstr != NULL) {62if (MultiByteToWideChar(CP_UTF8,630,64utf8_str,65utf8_len,66wstr, wlen) > 0) {67plen = WideCharToMultiByte(codePage,680,69wstr,70wlen,71platform_str,72platform_len,73NULL,74NULL);75if (plen >= 0) {76platform_str[plen] = '\0';77}78free(wstr);79}80}81}82return plen;83}848586