Path: blob/master/src/java.prefs/windows/native/libprefs/WindowsPreferences.c
41152 views
/*1* Copyright (c) 2000, 2018, 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#include "java_util_prefs_WindowsPreferences.h"3132#ifdef __cplusplus33extern "C" {34#endif3536/*37* Declare library specific JNI_Onload entry if static build38*/39DEF_STATIC_JNI_OnLoad4041JNIEXPORT jlongArray JNICALL42Java_java_util_prefs_WindowsPreferences_WindowsRegOpenKey(JNIEnv* env,43jclass this_class, jlong hKey, jbyteArray lpSubKey, jint securityMask) {44char* str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);45CHECK_NULL_RETURN(str, NULL);4647HKEY handle;48int errorCode = RegOpenKeyEx((HKEY) hKey, str, 0, securityMask, &handle);49(*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);5051__declspec(align(8)) jlong tmp[2];52tmp[0] = (jlong) handle;53tmp[1] = errorCode;54jlongArray result = (*env)->NewLongArray(env, 2);55if (result != NULL) {56(*env)->SetLongArrayRegion(env, result, 0, 2, tmp);57}58return result;59}6061JNIEXPORT jint JNICALL62Java_java_util_prefs_WindowsPreferences_WindowsRegCloseKey(JNIEnv* env,63jclass this_class, jlong hKey) {64return (jint) RegCloseKey((HKEY) hKey);65};6667JNIEXPORT jlongArray JNICALL68Java_java_util_prefs_WindowsPreferences_WindowsRegCreateKeyEx(JNIEnv* env,69jclass this_class, jlong hKey, jbyteArray lpSubKey) {70char* str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);71CHECK_NULL_RETURN(str, NULL);7273HKEY handle;74DWORD lpdwDisposition;75int errorCode = RegCreateKeyEx((HKEY) hKey, str, 0, NULL,76REG_OPTION_NON_VOLATILE, KEY_READ,77NULL, &handle, &lpdwDisposition);78(*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);7980__declspec(align(8)) jlong tmp[3];81tmp[0] = (jlong) handle;82tmp[1] = errorCode;83tmp[2] = lpdwDisposition;84jlongArray result = (*env)->NewLongArray(env, 3);85if (result != NULL) {86(*env)->SetLongArrayRegion(env, result, 0, 3, tmp);87}88return result;89}9091JNIEXPORT jint JNICALL92Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteKey(JNIEnv* env,93jclass this_class, jlong hKey, jbyteArray lpSubKey) {94char* str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);95CHECK_NULL_RETURN(str, -1);9697int result = RegDeleteKey((HKEY) hKey, str);98(*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);99return result;100101};102103JNIEXPORT jint JNICALL104Java_java_util_prefs_WindowsPreferences_WindowsRegFlushKey(JNIEnv* env,105jclass this_class, jlong hKey) {106return RegFlushKey((HKEY) hKey);107}108109JNIEXPORT jbyteArray JNICALL110Java_java_util_prefs_WindowsPreferences_WindowsRegQueryValueEx(JNIEnv* env,111jclass this_class, jlong hKey, jbyteArray valueName) {112char* valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);113CHECK_NULL_RETURN(valueNameStr, NULL);114115DWORD valueType;116DWORD valueSize;117if (RegQueryValueEx((HKEY) hKey, valueNameStr, NULL, &valueType, NULL,118&valueSize) != ERROR_SUCCESS) {119(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);120return NULL;121}122123char* buffer = (char*) malloc(valueSize);124if (buffer != NULL) {125if (RegQueryValueEx((HKEY) hKey, valueNameStr, NULL, &valueType, buffer,126&valueSize) != ERROR_SUCCESS) {127free(buffer);128(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);129return NULL;130}131} else {132JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");133(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);134return NULL;135}136137jbyteArray result;138if (valueType == REG_SZ) {139result = (*env)->NewByteArray(env, valueSize);140if (result != NULL) {141(*env)->SetByteArrayRegion(env, result, 0, valueSize, buffer);142}143} else {144result = NULL;145}146free(buffer);147(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);148return result;149}150151JNIEXPORT jint JNICALL152Java_java_util_prefs_WindowsPreferences_WindowsRegSetValueEx(JNIEnv* env,153jclass this_class, jlong hKey, jbyteArray valueName, jbyteArray data) {154if ((valueName == NULL) || (data == NULL)) {155return -1;156}157int size = (*env)->GetArrayLength(env, data);158char* dataStr = (*env)->GetByteArrayElements(env, data, NULL);159CHECK_NULL_RETURN(dataStr, -1);160161char* valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);162int error_code = -1;163if (valueNameStr != NULL) {164error_code = RegSetValueEx((HKEY) hKey, valueNameStr, 0,165REG_SZ, dataStr, size);166(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);167}168(*env)->ReleaseByteArrayElements(env, data, dataStr, 0);169return error_code;170}171172JNIEXPORT jint JNICALL173Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteValue(JNIEnv* env,174jclass this_class, jlong hKey, jbyteArray valueName) {175if (valueName == NULL) {176return -1;177}178char* valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);179CHECK_NULL_RETURN(valueNameStr, -1);180181int error_code = RegDeleteValue((HKEY) hKey, valueNameStr);182(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);183return error_code;184}185186JNIEXPORT jlongArray JNICALL187Java_java_util_prefs_WindowsPreferences_WindowsRegQueryInfoKey(JNIEnv* env,188jclass this_class, jlong hKey) {189int subKeysNumber;190int maxSubKeyLength;191int valuesNumber;192int maxValueNameLength;193int errorCode = RegQueryInfoKey((HKEY) hKey, NULL, NULL, NULL,194&subKeysNumber, &maxSubKeyLength, NULL,195&valuesNumber, &maxValueNameLength,196NULL, NULL, NULL);197198__declspec(align(8)) jlong tmp[5];199tmp[0] = subKeysNumber;200tmp[1] = errorCode;201tmp[2] = valuesNumber;202tmp[3] = maxSubKeyLength;203tmp[4] = maxValueNameLength;204jintArray result = (*env)->NewLongArray(env, 5);205if (result != NULL) {206(*env)->SetLongArrayRegion(env, result, 0, 5, tmp);207}208return result;209}210211JNIEXPORT jbyteArray JNICALL212Java_java_util_prefs_WindowsPreferences_WindowsRegEnumKeyEx(JNIEnv* env,213jclass this_class, jlong hKey, jint subKeyIndex, jint maxKeyLength) {214int size = maxKeyLength;215char* buffer = (char*) malloc(maxKeyLength);216if (buffer == NULL) {217JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");218return NULL;219}220if (RegEnumKeyEx((HKEY) hKey, subKeyIndex, buffer, &size, NULL, NULL,221NULL, NULL) != ERROR_SUCCESS) {222free(buffer);223return NULL;224}225226jbyteArray result = (*env)->NewByteArray(env, size + 1);227if (result != NULL) {228(*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);229}230free(buffer);231return result;232}233234JNIEXPORT jbyteArray JNICALL235Java_java_util_prefs_WindowsPreferences_WindowsRegEnumValue(JNIEnv* env,236jclass this_class, jlong hKey, jint valueIndex, jint maxValueNameLength) {237int size = maxValueNameLength;238char* buffer = (char*) malloc(maxValueNameLength);239if (buffer == NULL) {240JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");241return NULL;242}243244int error_code = RegEnumValue((HKEY) hKey, valueIndex, buffer,245&size, NULL, NULL, NULL, NULL);246if (error_code != ERROR_SUCCESS) {247free(buffer);248return NULL;249}250jbyteArray result = (*env)->NewByteArray(env, size + 1);251if (result != NULL) {252(*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);253}254free(buffer);255return result;256}257258259#ifdef __cplusplus260}261#endif262263264