/* gnu_java_nio_charset_iconv_IconvDecoder.c -- Copyright (C) 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include #include #include #include #include "gnu_java_nio_charset_iconv_IconvDecoder.h" static void createRawData (JNIEnv * env, jobject obj, void *ptr); static void *getData (JNIEnv * env, jobject obj); JNIEXPORT void JNICALL Java_gnu_java_nio_charset_iconv_IconvDecoder_openIconv (JNIEnv * env, jobject obj, jstring jname) { jclass exception; iconv_t iconv_object; const char *name = (*env)->GetStringUTFChars (env, jname, 0); /* to java from "name", native java format depends on endianness */ #ifdef WORDS_BIGENDIAN iconv_object = iconv_open ("UTF-16BE", name); #else iconv_object = iconv_open ("UTF-16LE", name); #endif (*env)->ReleaseStringUTFChars (env, jname, name); if ((long) iconv_object == -1L) { /* Throw an exception if charset not available */ (*env)->ExceptionDescribe (env); (*env)->ExceptionClear (env); exception = (*env)->FindClass (env, "java/lang/IllegalArgumentException"); assert (exception != 0); (*env)->ThrowNew (env, exception, "Charset not available."); return; } createRawData (env, obj, (void *) iconv_object); } JNIEXPORT jint JNICALL Java_gnu_java_nio_charset_iconv_IconvDecoder_decode (JNIEnv * env, jobject obj, jbyteArray inArr, jcharArray outArr, jint posIn, jint remIn, jint posOut, jint remOut) { iconv_t iconv_object = getData (env, obj); jclass cls; jfieldID fid; size_t retval; char **in, **out; jbyte *input, *inputcopy; jchar *output, *outputcopy; size_t lenIn = (size_t) remIn; size_t lenOut = (size_t) remOut * 2; inputcopy = input = (*env)->GetByteArrayElements (env, inArr, 0); outputcopy = output = (*env)->GetCharArrayElements (env, outArr, 0); input += posIn; output += posOut * 2; in = (char **) &input; out = (char **) &output; retval = iconv (iconv_object, in, &lenIn, out, &lenOut); /* XXX: Do we need to relase the input array? It's not modified. */ (*env)->ReleaseByteArrayElements (env, inArr, inputcopy, 0); (*env)->ReleaseCharArrayElements (env, outArr, outputcopy, 0); if (retval == (size_t) (-1)) { if (errno == EILSEQ || errno == EINVAL) retval = 1; else retval = 0; } else retval = 0; cls = (*env)->GetObjectClass (env, obj); fid = (*env)->GetFieldID (env, cls, "inremaining", "I"); assert (fid != 0); (*env)->SetIntField (env, obj, fid, (jint) lenIn); fid = (*env)->GetFieldID (env, cls, "outremaining", "I"); assert (fid != 0); (*env)->SetIntField (env, obj, fid, (jint) (lenOut >> 1)); return (jint) retval; } JNIEXPORT void JNICALL Java_gnu_java_nio_charset_iconv_IconvDecoder_closeIconv (JNIEnv * env, jobject obj) { iconv_t iconv_object; iconv_object = getData (env, obj); iconv_close (iconv_object); } static void createRawData (JNIEnv * env, jobject obj, void *ptr) { jclass cls; jmethodID method; jobject data; jfieldID data_fid; cls = (*env)->GetObjectClass (env, obj); data_fid = (*env)->GetFieldID (env, cls, "data", "Lgnu/classpath/RawData;"); assert (data_fid != 0); #ifdef POINTERS_ARE_64BIT cls = (*env)->FindClass (env, "gnu/classpath/RawData64"); method = (*env)->GetMethodID (env, cls, "", "(J)V"); data = (*env)->NewObject (env, cls, method, (jlong) ptr); #else cls = (*env)->FindClass (env, "gnu/classpath/RawData32"); method = (*env)->GetMethodID (env, cls, "", "(I)V"); data = (*env)->NewObject (env, cls, method, (jint) ptr); #endif (*env)->SetObjectField (env, obj, data_fid, data); } static void * getData (JNIEnv * env, jobject obj) { jclass cls; jfieldID field; jfieldID data_fid; jobject data; cls = (*env)->GetObjectClass (env, obj); data_fid = (*env)->GetFieldID (env, cls, "data", "Lgnu/classpath/RawData;"); assert (data_fid != 0); data = (*env)->GetObjectField (env, obj, data_fid); #if SIZEOF_VOID_P == 8 cls = (*env)->FindClass (env, "gnu/classpath/RawData64"); field = (*env)->GetFieldID (env, cls, "data", "J"); return (void *) (*env)->GetLongField (env, data, field); #else cls = (*env)->FindClass (env, "gnu/classpath/RawData32"); field = (*env)->GetFieldID (env, cls, "data", "I"); return (void *) (*env)->GetIntField (env, data, field); #endif }