Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/instrument/EncodingSupport_md.c
32285 views
/*1* Copyright (c) 2004, 2012, 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 <stdio.h>25#include <stddef.h>26#include <stdlib.h>27#include <string.h>28#include <ctype.h>29#include <locale.h>30#include <langinfo.h>31#include <iconv.h>3233/* Routines to convert back and forth between Platform Encoding and UTF-8 */3435/* Use THIS_FILE when it is available. */36#ifndef THIS_FILE37#define THIS_FILE __FILE__38#endif3940/* Error and assert macros */41#define UTF_ERROR(m) utfError(THIS_FILE, __LINE__, m)42#define UTF_ASSERT(x) ( (x)==0 ? UTF_ERROR("ASSERT ERROR " #x) : (void)0 )43#define UTF_DEBUG(x)4445/* Global variables */46static iconv_t iconvToPlatform = (iconv_t)-1;47static iconv_t iconvFromPlatform = (iconv_t)-1;4849/*50* Error handler51*/52static void53utfError(char *file, int line, char *message)54{55(void)fprintf(stderr, "UTF ERROR [\"%s\":%d]: %s\n", file, line, message);56abort();57}5859/*60* Initialize all utf processing.61*/62static void63utfInitialize(void)64{65char *codeset;6667/* Set the locale from the environment */68(void)setlocale(LC_ALL, "");6970/* Get the codeset name */71codeset = (char*)nl_langinfo(CODESET);72if ( codeset == NULL || codeset[0] == 0 ) {73UTF_DEBUG(("NO codeset returned by nl_langinfo(CODESET)\n"));74return;75}7677UTF_DEBUG(("Codeset = %s\n", codeset));7879/* If we don't need this, skip it */80if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {81UTF_DEBUG(("NO iconv() being used because it is not needed\n"));82return;83}8485/* Open conversion descriptors */86iconvToPlatform = iconv_open(codeset, "UTF-8");87if ( iconvToPlatform == (iconv_t)-1 ) {88UTF_ERROR("Failed to complete iconv_open() setup");89}90iconvFromPlatform = iconv_open("UTF-8", codeset);91if ( iconvFromPlatform == (iconv_t)-1 ) {92UTF_ERROR("Failed to complete iconv_open() setup");93}94}9596/*97* Terminate all utf processing98*/99static void100utfTerminate(void)101{102if ( iconvFromPlatform!=(iconv_t)-1 ) {103(void)iconv_close(iconvFromPlatform);104}105if ( iconvToPlatform!=(iconv_t)-1 ) {106(void)iconv_close(iconvToPlatform);107}108iconvToPlatform = (iconv_t)-1;109iconvFromPlatform = (iconv_t)-1;110}111112/*113* Do iconv() conversion.114* Returns length or -1 if output overflows.115*/116static int117iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)118{119int outputLen = 0;120121UTF_ASSERT(bytes);122UTF_ASSERT(len>=0);123UTF_ASSERT(output);124UTF_ASSERT(outputMaxLen>len);125126output[0] = 0;127outputLen = 0;128129if ( ic != (iconv_t)-1 ) {130int returnValue;131size_t inLeft;132size_t outLeft;133char *inbuf;134char *outbuf;135136inbuf = bytes;137outbuf = output;138inLeft = len;139outLeft = outputMaxLen;140returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);141if ( returnValue >= 0 && inLeft==0 ) {142outputLen = outputMaxLen-outLeft;143output[outputLen] = 0;144return outputLen;145}146147/* Failed to do the conversion */148return -1;149}150151/* Just copy bytes */152outputLen = len;153(void)memcpy(output, bytes, len);154output[len] = 0;155return outputLen;156}157158/*159* Convert UTF-8 to Platform Encoding.160* Returns length or -1 if output overflows.161*/162static int163utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen)164{165return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen);166}167168/*169* Convert Platform Encoding to UTF-8.170* Returns length or -1 if output overflows.171*/172static int173platformToUtf8(char *str, int len, char *output, int outputMaxLen)174{175return iconvConvert(iconvFromPlatform, str, len, output, outputMaxLen);176}177178int179convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {180if (iconvToPlatform == (iconv_t)-1) {181utfInitialize();182}183return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len);184}185186187