Path: blob/master/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c
66644 views
/*1* Copyright (c) 2004, 2018, 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/* Error and assert macros */36#define UTF_ERROR(m) utfError(__FILE__, __LINE__, m)37#define UTF_ASSERT(x) ( (x)==0 ? UTF_ERROR("ASSERT ERROR " #x) : (void)0 )38#define UTF_DEBUG(x)3940/* Global variables */41static iconv_t iconvToPlatform = (iconv_t)-1;42static iconv_t iconvFromPlatform = (iconv_t)-1;4344/*45* Error handler46*/47static void48utfError(char *file, int line, char *message)49{50(void)fprintf(stderr, "UTF ERROR [\"%s\":%d]: %s\n", file, line, message);51abort();52}5354/*55* Initialize all utf processing.56*/57static void58utfInitialize(void)59{60const char* codeset;6162/* Set the locale from the environment */63(void)setlocale(LC_ALL, "");6465/* Get the codeset name */66codeset = (char*)nl_langinfo(CODESET);67if ( codeset == NULL || codeset[0] == 0 ) {68UTF_DEBUG(("NO codeset returned by nl_langinfo(CODESET)\n"));69return;70}7172UTF_DEBUG(("Codeset = %s\n", codeset));7374#ifdef MACOSX75/* On Mac, if US-ASCII, but with no env hints, use UTF-8 */76const char* env_lang = getenv("LANG");77const char* env_lc_all = getenv("LC_ALL");78const char* env_lc_ctype = getenv("LC_CTYPE");7980if (strcmp(codeset,"US-ASCII") == 0 &&81(env_lang == NULL || strlen(env_lang) == 0) &&82(env_lc_all == NULL || strlen(env_lc_all) == 0) &&83(env_lc_ctype == NULL || strlen(env_lc_ctype) == 0)) {84codeset = "UTF-8";85}86#endif8788/* If we don't need this, skip it */89if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {90UTF_DEBUG(("NO iconv() being used because it is not needed\n"));91return;92}9394/* Open conversion descriptors */95iconvToPlatform = iconv_open(codeset, "UTF-8");96if ( iconvToPlatform == (iconv_t)-1 ) {97UTF_ERROR("Failed to complete iconv_open() setup");98}99iconvFromPlatform = iconv_open("UTF-8", codeset);100if ( iconvFromPlatform == (iconv_t)-1 ) {101UTF_ERROR("Failed to complete iconv_open() setup");102}103}104105/*106* Terminate all utf processing107*/108static void109utfTerminate(void)110{111if ( iconvFromPlatform!=(iconv_t)-1 ) {112(void)iconv_close(iconvFromPlatform);113}114if ( iconvToPlatform!=(iconv_t)-1 ) {115(void)iconv_close(iconvToPlatform);116}117iconvToPlatform = (iconv_t)-1;118iconvFromPlatform = (iconv_t)-1;119}120121/*122* Do iconv() conversion.123* Returns length or -1 if output overflows.124*/125static int126iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)127{128int outputLen = 0;129130UTF_ASSERT(bytes);131UTF_ASSERT(len>=0);132UTF_ASSERT(output);133UTF_ASSERT(outputMaxLen>len);134135output[0] = 0;136outputLen = 0;137138if ( ic != (iconv_t)-1 ) {139int returnValue;140size_t inLeft;141size_t outLeft;142char *inbuf;143char *outbuf;144145inbuf = bytes;146outbuf = output;147inLeft = len;148outLeft = outputMaxLen;149returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);150if ( returnValue >= 0 && inLeft==0 ) {151outputLen = outputMaxLen-outLeft;152output[outputLen] = 0;153return outputLen;154}155156/* Failed to do the conversion */157UTF_DEBUG(("iconv() failed to do the conversion\n"));158return -1;159}160161/* Just copy bytes */162outputLen = len;163(void)memcpy(output, bytes, len);164output[len] = 0;165return outputLen;166}167168/*169* Convert UTF-8 to Platform Encoding.170* Returns length or -1 if output overflows.171*/172static int173utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen)174{175return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen);176}177178/*179* Convert Platform Encoding to UTF-8.180* Returns length or -1 if output overflows.181*/182static int183platformToUtf8(char *str, int len, char *output, int outputMaxLen)184{185return iconvConvert(iconvFromPlatform, str, len, output, outputMaxLen);186}187188int189convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {190if (iconvToPlatform == (iconv_t)-1) {191utfInitialize();192}193return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len);194}195196197