1365N/A/*
3261N/A * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
1365N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1365N/A *
1365N/A * This code is free software; you can redistribute it and/or modify it
1365N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1365N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1365N/A *
1365N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1365N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1365N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1365N/A * version 2 for more details (a copy is included in the LICENSE file that
1365N/A * accompanied this code).
1365N/A *
1365N/A * You should have received a copy of the GNU General Public License version
1365N/A * 2 along with this work; if not, write to the Free Software Foundation,
1365N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1365N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1365N/A */
1365N/A
1365N/A#include <stdlib.h>
1365N/A#include <string.h>
1365N/A#include <windows.h>
1365N/A#include <locale.h>
1365N/A
1365N/A#include "jni.h"
1365N/A#include "jni_util.h"
1365N/A
1365N/Astatic void getParent(const TCHAR *path, TCHAR *dest) {
1365N/A char* lastSlash = max(strrchr(path, '\\'), strrchr(path, '/'));
1365N/A if (lastSlash == NULL) {
1365N/A *dest = 0;
1365N/A return;
1365N/A }
1365N/A if (path != dest)
1365N/A strcpy(dest, path);
1365N/A *lastSlash = 0;
1365N/A}
1365N/A
1365N/ABOOL useNativeConverter(JNIEnv *env) {
1365N/A static BOOL initialized;
1365N/A static BOOL useNative;
1365N/A if (!initialized) {
1365N/A HMODULE jvm = GetModuleHandle("jvm");
1365N/A useNative = FALSE;
1365N/A if (jvm != NULL) {
1365N/A TCHAR *jvmPath = NULL;
1365N/A int bufferSize = MAX_PATH;
1365N/A while (jvmPath == NULL) {
1365N/A DWORD result;
1365N/A jvmPath = malloc(bufferSize);
1365N/A if (jvmPath == NULL)
1365N/A return FALSE;
1365N/A result = GetModuleFileName(jvm, jvmPath, bufferSize);
1365N/A if (result == 0)
1365N/A return FALSE;
1365N/A if (result == bufferSize) { // didn't fit
1365N/A bufferSize += MAX_PATH; // increase buffer size, try again
1365N/A free(jvmPath);
1365N/A jvmPath = NULL;
1365N/A }
1365N/A }
1365N/A
1365N/A getParent(jvmPath, jvmPath);
1365N/A useNative = (!strcmp("kernel", jvmPath + strlen(jvmPath) -
1365N/A strlen("kernel"))); // true if jvm.dll lives in "kernel"
1365N/A if (useNative)
1365N/A setlocale(LC_ALL, "");
1365N/A free(jvmPath);
1365N/A }
1365N/A initialized = TRUE;
1365N/A }
1365N/A return useNative;
1365N/A}
1365N/A
1365N/Ajstring nativeNewStringPlatform(JNIEnv *env, const char *str) {
2884N/A static jmethodID String_char_constructor;
1365N/A if (useNativeConverter(env)) {
1365N/A // use native Unicode conversion so Kernel isn't required during
1365N/A // System.initProperties
1365N/A jcharArray chars = 0;
1365N/A wchar_t *utf16;
1365N/A int len;
1365N/A jstring result = NULL;
1365N/A
1365N/A if (getFastEncoding() == NO_ENCODING_YET)
1365N/A initializeEncoding(env);
1365N/A
1365N/A len = mbstowcs(NULL, str, strlen(str));
1365N/A if (len == -1)
1365N/A return NULL;
1365N/A utf16 = calloc(len + 1, 2);
1365N/A if (mbstowcs(utf16, str, len) == -1)
1365N/A return NULL;
1365N/A chars = (*env)->NewCharArray(env, len);
1365N/A if (chars == NULL)
1365N/A return NULL;
1365N/A (*env)->SetCharArrayRegion(env, chars, 0, len, utf16);
1365N/A if (String_char_constructor == NULL)
1365N/A String_char_constructor = (*env)->GetMethodID(env,
1365N/A JNU_ClassString(env), "<init>", "([C)V");
1365N/A result = (*env)->NewObject(env, JNU_ClassString(env),
1365N/A String_char_constructor, chars);
1365N/A free(utf16);
1365N/A return result;
1365N/A }
1365N/A else
1365N/A return NULL;
1365N/A}
1365N/A
1365N/A
1365N/Achar* nativeGetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy) {
1365N/A if (useNativeConverter(env)) {
1365N/A // use native Unicode conversion so Kernel isn't required during
1365N/A // System.initProperties
1365N/A char *result = NULL;
1365N/A size_t len;
1365N/A const jchar* utf16 = (*env)->GetStringChars(env, jstr, NULL);
1365N/A len = wcstombs(NULL, utf16, (*env)->GetStringLength(env, jstr) * 4) + 1;
1365N/A if (len == -1)
1365N/A return NULL;
1365N/A result = (char*) malloc(len);
1365N/A if (result != NULL) {
1365N/A if (wcstombs(result, utf16, len) == -1)
1365N/A return NULL;
1365N/A (*env)->ReleaseStringChars(env, jstr, utf16);
1365N/A if (isCopy)
1365N/A *isCopy = JNI_TRUE;
1365N/A }
1365N/A return result;
1365N/A }
1365N/A else
1365N/A return NULL;
1365N/A}