Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/npt/utf_md.c
32285 views
/*1* Copyright (c) 2004, 2005, 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*/2425#include "utf.h"2627#include <windows.h>28#include <stdlib.h>29#include <stdio.h>3031/*32* Initialize all utf processing.33*/34struct UtfInst * JNICALL35utfInitialize(char *options)36{37struct UtfInst *ui;38LANGID langID;39LCID localeID;40TCHAR strCodePage[7]; // ANSI code page id4142ui = (struct UtfInst*)calloc(sizeof(struct UtfInst), 1);4344/*45* Get the code page for this locale46*/47langID = LANGIDFROMLCID(GetUserDefaultLCID());48localeID = MAKELCID(langID, SORT_DEFAULT);49if (GetLocaleInfo(localeID, LOCALE_IDEFAULTANSICODEPAGE,50strCodePage, sizeof(strCodePage)/sizeof(TCHAR)) > 0 ) {51ui->platformCodePage = atoi(strCodePage);52} else {53ui->platformCodePage = GetACP();54}55return ui;56}5758/*59* Terminate all utf processing60*/61void JNICALL62utfTerminate(struct UtfInst *ui, char *options)63{64(void)free(ui);65}6667/*68* Get wide string (assumes len>0)69*/70static WCHAR*71getWideString(UINT codePage, char* str, int len, int *pwlen)72{73int wlen;74WCHAR* wstr;7576/* Convert the string to WIDE string */77wlen = MultiByteToWideChar(codePage, 0, str, len, NULL, 0);78*pwlen = wlen;79if (wlen <= 0) {80UTF_ERROR(("Can't get WIDE string length"));81return NULL;82}83wstr = (WCHAR*)malloc(wlen * sizeof(WCHAR));84if (wstr == NULL) {85UTF_ERROR(("Can't malloc() any space"));86return NULL;87}88if (MultiByteToWideChar(codePage, 0, str, len, wstr, wlen) == 0) {89UTF_ERROR(("Can't get WIDE string"));90return NULL;91}92return wstr;93}9495/*96* Convert UTF-8 to a platform string97*/98int JNICALL99utf8ToPlatform(struct UtfInst *ui, jbyte *utf8, int len, char* output, int outputMaxLen)100{101int wlen;102int plen;103WCHAR* wstr;104105/* Negative length is an error */106if ( len < 0 ) {107return -1;108}109110/* Zero length is ok, but we don't need to do much */111if ( len == 0 ) {112output[0] = 0;113return 0;114}115116/* Get WIDE string version (assumes len>0) */117wstr = getWideString(CP_UTF8, (char*)utf8, len, &wlen);118if ( wstr == NULL ) {119return -1;120}121122/* Convert WIDE string to MultiByte string */123plen = WideCharToMultiByte(ui->platformCodePage, 0, wstr, wlen,124output, outputMaxLen, NULL, NULL);125free(wstr);126if (plen <= 0) {127UTF_ERROR(("Can't convert WIDE string to multi-byte"));128return -1;129}130output[plen] = '\0';131return plen;132}133134/*135* Convert Platform Encoding to UTF-8.136*/137int JNICALL138utf8FromPlatform(struct UtfInst *ui, char *str, int len, jbyte *output, int outputMaxLen)139{140int wlen;141int plen;142WCHAR* wstr;143144/* Negative length is an error */145if ( len < 0 ) {146return -1;147}148149/* Zero length is ok, but we don't need to do much */150if ( len == 0 ) {151output[0] = 0;152return 0;153}154155/* Get WIDE string version (assumes len>0) */156wstr = getWideString(ui->platformCodePage, str, len, &wlen);157if ( wstr == NULL ) {158return -1;159}160161/* Convert WIDE string to UTF-8 string */162plen = WideCharToMultiByte(CP_UTF8, 0, wstr, wlen,163(char*)output, outputMaxLen, NULL, NULL);164free(wstr);165if (plen <= 0) {166UTF_ERROR(("Can't convert WIDE string to multi-byte"));167return -1;168}169output[plen] = '\0';170return plen;171}172173174