Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/common/jni_util_md.c
32285 views
/*1* Copyright (c) 2004, 2013, 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 <stdlib.h>26#include <string.h>27#include <windows.h>28#include <locale.h>2930#include "jni.h"31#include "jni_util.h"3233static void getParent(const TCHAR *path, TCHAR *dest) {34char* lastSlash = max(strrchr(path, '\\'), strrchr(path, '/'));35if (lastSlash == NULL) {36*dest = 0;37return;38}39if (path != dest)40strcpy(dest, path);41*lastSlash = 0;42}4344BOOL useNativeConverter(JNIEnv *env) {45static BOOL initialized;46static BOOL useNative;47if (!initialized) {48HMODULE jvm = GetModuleHandle("jvm");49useNative = FALSE;50if (jvm != NULL) {51TCHAR *jvmPath = NULL;52int bufferSize = MAX_PATH;53while (jvmPath == NULL) {54DWORD result;55jvmPath = malloc(bufferSize);56if (jvmPath == NULL)57return FALSE;58result = GetModuleFileName(jvm, jvmPath, bufferSize);59if (result == 0)60return FALSE;61if (result == bufferSize) { // didn't fit62bufferSize += MAX_PATH; // increase buffer size, try again63free(jvmPath);64jvmPath = NULL;65}66}6768getParent(jvmPath, jvmPath);69useNative = (!strcmp("kernel", jvmPath + strlen(jvmPath) -70strlen("kernel"))); // true if jvm.dll lives in "kernel"71if (useNative)72setlocale(LC_ALL, "");73free(jvmPath);74}75initialized = TRUE;76}77return useNative;78}7980jstring nativeNewStringPlatform(JNIEnv *env, const char *str) {81static jmethodID String_char_constructor;82if (useNativeConverter(env)) {83// use native Unicode conversion so Kernel isn't required during84// System.initProperties85jcharArray chars = 0;86wchar_t *utf16;87int len;88jstring result = NULL;8990if (getFastEncoding() == NO_ENCODING_YET)91initializeEncoding(env);9293len = mbstowcs(NULL, str, strlen(str));94if (len == -1)95return NULL;96utf16 = calloc(len + 1, 2);97if (mbstowcs(utf16, str, len) == -1)98return NULL;99chars = (*env)->NewCharArray(env, len);100if (chars == NULL)101return NULL;102(*env)->SetCharArrayRegion(env, chars, 0, len, utf16);103if (String_char_constructor == NULL)104String_char_constructor = (*env)->GetMethodID(env,105JNU_ClassString(env), "<init>", "([C)V");106result = (*env)->NewObject(env, JNU_ClassString(env),107String_char_constructor, chars);108free(utf16);109return result;110}111else112return NULL;113}114115116char* nativeGetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy) {117if (useNativeConverter(env)) {118// use native Unicode conversion so Kernel isn't required during119// System.initProperties120char *result = NULL;121size_t len;122const jchar* utf16 = (*env)->GetStringChars(env, jstr, NULL);123len = wcstombs(NULL, utf16, (*env)->GetStringLength(env, jstr) * 4) + 1;124if (len == -1)125return NULL;126result = (char*) malloc(len);127if (result != NULL) {128if (wcstombs(result, utf16, len) == -1)129return NULL;130(*env)->ReleaseStringChars(env, jstr, utf16);131if (isCopy)132*isCopy = JNI_TRUE;133}134return result;135}136else137return NULL;138}139140void* getProcessHandle() {141return (void*)GetModuleHandle(NULL);142}143144int145getErrorString(int err, char *buf, size_t len)146{147int ret = 0;148if (err == 0 || len < 1) return 0;149ret = strerror_s(buf, len, err);150return ret;151}152153/*154* Windows symbols can be simple like JNI_OnLoad or __stdcall format155* like _JNI_OnLoad@8. We need to handle both.156*/157void buildJniFunctionName(const char *sym, const char *cname,158char *jniEntryName) {159if (cname != NULL) {160char *p = strrchr(sym, '@');161if (p != NULL && p != sym) {162// sym == _JNI_OnLoad@8163strncpy(jniEntryName, sym, (p - sym));164jniEntryName[(p-sym)] = '\0';165// jniEntryName == _JNI_OnLoad166strcat(jniEntryName, "_");167strcat(jniEntryName, cname);168strcat(jniEntryName, p);169//jniEntryName == _JNI_OnLoad_cname@8170} else {171strcpy(jniEntryName, sym);172strcat(jniEntryName, "_");173strcat(jniEntryName, cname);174}175} else {176strcpy(jniEntryName, sym);177}178return;179}180181182