Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/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 <stdio.h>26#include <stdlib.h>27#include <string.h>28#include <locale.h>29#ifndef __ANDROID__30# include <langinfo.h>31# include <iconv.h>32#else33# include "langinfo.h"34# include "iconv.h"35#endif3637#include "utf.h"3839/* Global variables */4041/*42* Initialize all utf processing.43*/44struct UtfInst *JNICALL45utfInitialize(char *options)46{47struct UtfInst *ui;48char *codeset;4950ui = (struct UtfInst*)calloc(sizeof(struct UtfInst), 1);51ui->iconvToPlatform = (void *)-1;52ui->iconvFromPlatform = (void *)-1;5354/* Set the locale from the environment */55(void)setlocale(LC_ALL, "");5657/* Get the codeset name */58codeset = (char*)nl_langinfo(CODESET);59if ( codeset == NULL || codeset[0] == 0 ) {60return ui;61}6263/* If we don't need this, skip it */64if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {65return ui;66}6768/* Open conversion descriptors */69ui->iconvToPlatform = iconv_open(codeset, "UTF-8");70if ( ui->iconvToPlatform == (void *)-1 ) {71UTF_ERROR("Failed to complete iconv_open() setup");72}73ui->iconvFromPlatform = iconv_open("UTF-8", codeset);74if ( ui->iconvFromPlatform == (void *)-1 ) {75UTF_ERROR("Failed to complete iconv_open() setup");76}77return ui;78}7980/*81* Terminate all utf processing82*/83void JNICALL84utfTerminate(struct UtfInst *ui, char *options)85{86if ( ui->iconvFromPlatform != (void *)-1 ) {87(void)iconv_close(ui->iconvFromPlatform);88}89if ( ui->iconvToPlatform != (void *)-1 ) {90(void)iconv_close(ui->iconvToPlatform);91}92ui->iconvToPlatform = (void *)-1;93ui->iconvFromPlatform = (void *)-1;94(void)free(ui);95}9697/*98* Do iconv() conversion.99* Returns length or -1 if output overflows.100*/101static int102iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)103{104int outputLen = 0;105106UTF_ASSERT(bytes);107UTF_ASSERT(len>=0);108UTF_ASSERT(output);109UTF_ASSERT(outputMaxLen>len);110111output[0] = 0;112outputLen = 0;113114if ( ic != (iconv_t)(void *)-1 ) {115int returnValue;116size_t inLeft;117size_t outLeft;118char *inbuf;119char *outbuf;120121inbuf = bytes;122outbuf = output;123inLeft = len;124outLeft = outputMaxLen;125returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);126if ( returnValue >= 0 && inLeft==0 ) {127outputLen = outputMaxLen-outLeft;128output[outputLen] = 0;129return outputLen;130}131132/* Failed to do the conversion */133return -1;134}135136/* Just copy bytes */137outputLen = len;138(void)memcpy(output, bytes, len);139output[len] = 0;140return outputLen;141}142143/*144* Convert UTF-8 to Platform Encoding.145* Returns length or -1 if output overflows.146*/147int JNICALL148utf8ToPlatform(struct UtfInst*ui, jbyte *utf8, int len, char *output, int outputMaxLen)149{150/* Negative length is an error */151if ( len < 0 ) {152return -1;153}154155/* Zero length is ok, but we don't need to do much */156if ( len == 0 ) {157output[0] = 0;158return 0;159}160161return iconvConvert(ui->iconvToPlatform, (char*)utf8, len, output, outputMaxLen);162}163164/*165* Convert Platform Encoding to UTF-8.166* Returns length or -1 if output overflows.167*/168int JNICALL169utf8FromPlatform(struct UtfInst*ui, char *str, int len, jbyte *output, int outputMaxLen)170{171/* Negative length is an error */172if ( len < 0 ) {173return -1;174}175176/* Zero length is ok, but we don't need to do much */177if ( len == 0 ) {178output[0] = 0;179return 0;180}181182return iconvConvert(ui->iconvFromPlatform, str, len, (char*)output, outputMaxLen);183}184185186