Path: blob/master/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c
41149 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 */66#ifndef __ANDROID__67codeset = (char*)nl_langinfo(CODESET);68#else69codeset = "ASCII"; // (MB_CUR_MAX == 1) ? "ASCII" : "UTF-8";70#endif71if ( codeset == NULL || codeset[0] == 0 ) {72UTF_DEBUG(("NO codeset returned by nl_langinfo(CODESET)\n"));73return;74}7576UTF_DEBUG(("Codeset = %s\n", codeset));7778#ifdef MACOSX79/* On Mac, if US-ASCII, but with no env hints, use UTF-8 */80const char* env_lang = getenv("LANG");81const char* env_lc_all = getenv("LC_ALL");82const char* env_lc_ctype = getenv("LC_CTYPE");8384if (strcmp(codeset,"US-ASCII") == 0 &&85(env_lang == NULL || strlen(env_lang) == 0) &&86(env_lc_all == NULL || strlen(env_lc_all) == 0) &&87(env_lc_ctype == NULL || strlen(env_lc_ctype) == 0)) {88codeset = "UTF-8";89}90#endif9192/* If we don't need this, skip it */93if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {94UTF_DEBUG(("NO iconv() being used because it is not needed\n"));95return;96}9798/* Open conversion descriptors */99iconvToPlatform = iconv_open(codeset, "UTF-8");100if ( iconvToPlatform == (iconv_t)-1 ) {101UTF_ERROR("Failed to complete iconv_open() setup");102}103iconvFromPlatform = iconv_open("UTF-8", codeset);104if ( iconvFromPlatform == (iconv_t)-1 ) {105UTF_ERROR("Failed to complete iconv_open() setup");106}107}108109/*110* Terminate all utf processing111*/112static void113utfTerminate(void)114{115if ( iconvFromPlatform!=(iconv_t)-1 ) {116(void)iconv_close(iconvFromPlatform);117}118if ( iconvToPlatform!=(iconv_t)-1 ) {119(void)iconv_close(iconvToPlatform);120}121iconvToPlatform = (iconv_t)-1;122iconvFromPlatform = (iconv_t)-1;123}124125/*126* Do iconv() conversion.127* Returns length or -1 if output overflows.128*/129static int130iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)131{132int outputLen = 0;133134UTF_ASSERT(bytes);135UTF_ASSERT(len>=0);136UTF_ASSERT(output);137UTF_ASSERT(outputMaxLen>len);138139output[0] = 0;140outputLen = 0;141142if ( ic != (iconv_t)-1 ) {143int returnValue;144size_t inLeft;145size_t outLeft;146char *inbuf;147char *outbuf;148149inbuf = bytes;150outbuf = output;151inLeft = len;152outLeft = outputMaxLen;153returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);154if ( returnValue >= 0 && inLeft==0 ) {155outputLen = outputMaxLen-outLeft;156output[outputLen] = 0;157return outputLen;158}159160/* Failed to do the conversion */161UTF_DEBUG(("iconv() failed to do the conversion\n"));162return -1;163}164165/* Just copy bytes */166outputLen = len;167(void)memcpy(output, bytes, len);168output[len] = 0;169return outputLen;170}171172/*173* Convert UTF-8 to Platform Encoding.174* Returns length or -1 if output overflows.175*/176static int177utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen)178{179return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen);180}181182/*183* Convert Platform Encoding to UTF-8.184* Returns length or -1 if output overflows.185*/186static int187platformToUtf8(char *str, int len, char *output, int outputMaxLen)188{189return iconvConvert(iconvFromPlatform, str, len, output, outputMaxLen);190}191192int193convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {194if (iconvToPlatform == (iconv_t)-1) {195utfInitialize();196}197return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len);198}199200201