Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/java/util/WindowsPreferences.c
32287 views
/*1* Copyright (c) 2000, 2014, 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 <windows.h>27#include "jni.h"28#include "jni_util.h"29#include "jvm.h"30#ifdef __cplusplus31extern "C" {32#endif33JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegOpenKey34(JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey, jint securityMask) {35HKEY handle;36char* str;37int tmp[2];38int errorCode=-1;39jintArray result;40str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);41CHECK_NULL_RETURN(str, NULL);42errorCode = RegOpenKeyEx((HKEY)hKey, str, 0, securityMask, &handle);43(*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);44tmp[0]= (int) handle;45tmp[1]= errorCode;46result = (*env)->NewIntArray(env,2);47if (result != NULL) {48(*env)->SetIntArrayRegion(env, result, 0, 2, tmp);49}50return result;51}5253JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegCloseKey54(JNIEnv* env, jclass this_class, jint hKey) {55return (jint) RegCloseKey((HKEY) hKey);56};5758JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegCreateKeyEx59(JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey) {60HKEY handle;61char* str;62int tmp[3];63DWORD lpdwDisposition;64int errorCode;65jintArray result = NULL;66str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);67CHECK_NULL_RETURN(str, NULL);68errorCode = RegCreateKeyEx((HKEY)hKey, str, 0, NULL,69REG_OPTION_NON_VOLATILE, KEY_READ,70NULL, &handle, &lpdwDisposition);71(*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);72tmp[0]= (int) handle;73tmp[1]= errorCode;74tmp[2]= lpdwDisposition;75result = (*env)->NewIntArray(env,3);76if (result != NULL) {77(*env)->SetIntArrayRegion(env, result, 0, 3, tmp);78}79return result;80}8182JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteKey83(JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey) {84char* str;85int result;86str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);87CHECK_NULL_RETURN(str, -1);88result = RegDeleteKey((HKEY)hKey, str);89(*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);90return result;9192};9394JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegFlushKey95(JNIEnv* env, jclass this_class, jint hKey) {96return RegFlushKey ((HKEY)hKey);97}9899JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryValueEx100(JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName) {101char* valueNameStr;102char* buffer;103jbyteArray result;104DWORD valueType;105DWORD valueSize;106valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);107CHECK_NULL_RETURN(valueNameStr, NULL);108if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, NULL,109&valueSize) != ERROR_SUCCESS) {110(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);111return NULL;112}113114buffer = (char*)malloc(valueSize);115116if (buffer != NULL) {117if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, buffer,118&valueSize) != ERROR_SUCCESS) {119free(buffer);120(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);121return NULL;122}123} else {124JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");125(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);126return NULL;127}128129if (valueType == REG_SZ) {130result = (*env)->NewByteArray(env, valueSize);131if (result != NULL) {132(*env)->SetByteArrayRegion(env, result, 0, valueSize, buffer);133}134} else {135result = NULL;136}137free(buffer);138(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);139return result;140}141142143144145JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegSetValueEx146(JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName, jbyteArray data) {147char* valueNameStr;148char* dataStr;149int size = -1;150int nameSize = -1;151int error_code = -1;152if ((valueName == NULL)||(data == NULL)) {return -1;}153size = (*env)->GetArrayLength(env, data);154dataStr = (*env)->GetByteArrayElements(env, data, NULL);155CHECK_NULL_RETURN(dataStr, -1);156valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);157CHECK_NULL_RETURN(valueNameStr, -1);158error_code = RegSetValueEx((HKEY)hKey, valueNameStr, 0,159REG_SZ, dataStr, size);160(*env)->ReleaseByteArrayElements(env, data, dataStr, 0);161(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);162return error_code;163}164165JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteValue166(JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName) {167char* valueNameStr;168int error_code = -1;169if (valueName == NULL) {return -1;}170valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);171CHECK_NULL_RETURN(valueNameStr, -1);172error_code = RegDeleteValue((HKEY)hKey, valueNameStr);173(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);174return error_code;175}176177JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryInfoKey178(JNIEnv* env, jclass this_class, jint hKey) {179jintArray result = NULL;180int tmp[5];181int valuesNumber = -1;182int maxValueNameLength = -1;183int maxSubKeyLength = -1;184int subKeysNumber = -1;185int errorCode = -1;186errorCode = RegQueryInfoKey((HKEY)hKey, NULL, NULL, NULL,187&subKeysNumber, &maxSubKeyLength, NULL,188&valuesNumber, &maxValueNameLength,189NULL, NULL, NULL);190tmp[0]= subKeysNumber;191tmp[1]= (int)errorCode;192tmp[2]= valuesNumber;193tmp[3]= maxSubKeyLength;194tmp[4]= maxValueNameLength;195result = (*env)->NewIntArray(env,5);196if (result != NULL) {197(*env)->SetIntArrayRegion(env, result, 0, 5, tmp);198}199return result;200}201202JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegEnumKeyEx203(JNIEnv* env, jclass this_class, jint hKey , jint subKeyIndex, jint maxKeyLength) {204int size = maxKeyLength;205jbyteArray result;206char* buffer = NULL;207buffer = (char*)malloc(maxKeyLength);208if (buffer == NULL) {209JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");210return NULL;211}212if (RegEnumKeyEx((HKEY) hKey, subKeyIndex, buffer, &size, NULL, NULL,213NULL, NULL) != ERROR_SUCCESS){214free(buffer);215return NULL;216}217result = (*env)->NewByteArray(env, size + 1);218if (result != NULL) {219(*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);220}221free(buffer);222return result;223}224225JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegEnumValue226(JNIEnv* env, jclass this_class, jint hKey , jint valueIndex, jint maxValueNameLength){227int size = maxValueNameLength;228jbyteArray result;229char* buffer = NULL;230int error_code;231buffer = (char*)malloc(maxValueNameLength);232if (buffer == NULL) {233JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");234return NULL;235}236error_code = RegEnumValue((HKEY) hKey, valueIndex, buffer,237&size, NULL, NULL, NULL, NULL);238if (error_code!= ERROR_SUCCESS){239free(buffer);240return NULL;241}242result = (*env)->NewByteArray(env, size + 1);243if (result != NULL) {244(*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);245}246free(buffer);247return result;248}249250251#ifdef __cplusplus252}253#endif254255256