1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <string.h> 27 28 #include "jni.h" 29 #include "jvm.h" 30 #include "jdk_internal_jimage_ImageNativeSubstrate.h" 31 32 JNIEXPORT jlong JNICALL 33 Java_jdk_internal_jimage_ImageNativeSubstrate_openImage(JNIEnv *env, 34 jclass cls, jstring path, jboolean big_endian) { 35 const char *nativePath; 36 jlong ret; 37 38 nativePath = (*env)->GetStringUTFChars(env, path, NULL); 39 ret = JVM_ImageOpen(env, nativePath, big_endian); 40 (*env)->ReleaseStringUTFChars(env, path, nativePath); 41 return ret; 42 } 43 44 JNIEXPORT void JNICALL 45 Java_jdk_internal_jimage_ImageNativeSubstrate_closeImage(JNIEnv *env, 46 jclass cls, jlong id) { 47 JVM_ImageClose(env, id); 48 } 49 50 JNIEXPORT jlong JNICALL 51 Java_jdk_internal_jimage_ImageNativeSubstrate_getIndexAddress(JNIEnv *env, 52 jclass cls, jlong id) { 53 return JVM_ImageGetIndexAddress(env, id); 54 } 55 56 JNIEXPORT jlong JNICALL 57 Java_jdk_internal_jimage_ImageNativeSubstrate_getDataAddress(JNIEnv *env, 58 jclass cls, jlong id) { 59 return JVM_ImageGetDataAddress(env, id); 60 } 61 62 JNIEXPORT jboolean JNICALL 63 Java_jdk_internal_jimage_ImageNativeSubstrate_read(JNIEnv *env, 64 jclass cls, jlong id, jlong offset, 65 jobject uncompressedBuffer, jlong uncompressed_size) { 66 unsigned char* uncompressedAddress; 67 68 uncompressedAddress = (unsigned char*) (*env)->GetDirectBufferAddress(env, uncompressedBuffer); 69 if (uncompressedBuffer == NULL) { 70 return JNI_FALSE; 71 } 72 return JVM_ImageRead(env, id, offset, uncompressedAddress, uncompressed_size); 73 } 74 75 JNIEXPORT jboolean JNICALL 76 Java_jdk_internal_jimage_ImageNativeSubstrate_readCompressed(JNIEnv *env, 77 jclass cls, jlong id, jlong offset, 78 jobject compressedBuffer, jlong compressed_size, 79 jobject uncompressedBuffer, jlong uncompressed_size) { 80 // Get address of read direct buffer. 81 unsigned char* compressedAddress; 82 unsigned char* uncompressedAddress; 83 84 compressedAddress = (unsigned char*) (*env)->GetDirectBufferAddress(env, compressedBuffer); 85 // Get address of decompression direct buffer. 86 uncompressedAddress = (unsigned char*) (*env)->GetDirectBufferAddress(env, uncompressedBuffer); 87 if (uncompressedBuffer == NULL || compressedBuffer == NULL) { 88 return JNI_FALSE; 89 } 90 return JVM_ImageReadCompressed(env, id, offset, compressedAddress, compressed_size, 91 uncompressedAddress, uncompressed_size); 92 } 93 94 JNIEXPORT jbyteArray JNICALL 95 Java_jdk_internal_jimage_ImageNativeSubstrate_getStringBytes(JNIEnv *env, 96 jclass cls, jlong id, jint offset) { 97 const char* data; 98 size_t size; 99 jbyteArray byteArray; 100 jbyte* rawBytes; 101 102 data = JVM_ImageGetStringBytes(env, id, offset); 103 // Determine String length. 104 size = strlen(data); 105 // Allocate byte array. 106 byteArray = (*env)->NewByteArray(env, (jsize) size); 107 // Get array base address. 108 rawBytes = (*env)->GetByteArrayElements(env, byteArray, NULL); 109 // Copy bytes from image string table. 110 memcpy(rawBytes, data, size); 111 // Release byte array base address. 112 (*env)->ReleaseByteArrayElements(env, byteArray, rawBytes, 0); 113 return byteArray; 114 } 115 116 JNIEXPORT jlongArray JNICALL 117 Java_jdk_internal_jimage_ImageNativeSubstrate_getAttributes(JNIEnv *env, 118 jclass cls, jlong id, jint offset) { 119 // Allocate a jlong large enough for all location attributes. 120 jlongArray attributes; 121 jlong* rawAttributes; 122 jlong* ret; 123 124 attributes = (*env)->NewLongArray(env, JVM_ImageGetAttributesCount(env)); 125 // Get base address for jlong array. 126 rawAttributes = (*env)->GetLongArrayElements(env, attributes, NULL); 127 ret = JVM_ImageGetAttributes(env, rawAttributes, id, offset); 128 // Release jlong array base address. 129 (*env)->ReleaseLongArrayElements(env, attributes, rawAttributes, 0); 130 return ret == NULL ? NULL : attributes; 131 } 132 133 JNIEXPORT jlongArray JNICALL 134 Java_jdk_internal_jimage_ImageNativeSubstrate_findAttributes(JNIEnv *env, 135 jclass cls, jlong id, jbyteArray utf8) { 136 // Allocate a jlong large enough for all location attributes. 137 jsize count; 138 jlongArray attributes; 139 jlong* rawAttributes; 140 jsize size; 141 jbyte* rawBytes; 142 jlong* ret; 143 144 count = JVM_ImageGetAttributesCount(env); 145 attributes = (*env)->NewLongArray(env, JVM_ImageGetAttributesCount(env)); 146 // Get base address for jlong array. 147 rawAttributes = (*env)->GetLongArrayElements(env, attributes, NULL); 148 size = (*env)->GetArrayLength(env, utf8); 149 rawBytes = (*env)->GetByteArrayElements(env, utf8, NULL); 150 ret = JVM_ImageFindAttributes(env, rawAttributes, rawBytes, size, id); 151 (*env)->ReleaseByteArrayElements(env, utf8, rawBytes, 0); 152 // Release jlong array base address. 153 (*env)->ReleaseLongArrayElements(env, attributes, rawAttributes, 0); 154 return ret == NULL ? NULL : attributes; 155 156 } 157 158 JNIEXPORT jintArray JNICALL 159 Java_jdk_internal_jimage_ImageNativeSubstrate_attributeOffsets(JNIEnv *env, 160 jclass cls, jlong id) { 161 unsigned int length; 162 jintArray offsets; 163 jint* rawOffsets; 164 jint* ret; 165 166 length = JVM_ImageAttributeOffsetsLength(env, id); 167 offsets = (*env)->NewIntArray(env, length); 168 // Get base address of result. 169 rawOffsets = (*env)->GetIntArrayElements(env, offsets, NULL); 170 ret = JVM_ImageAttributeOffsets(env, rawOffsets, length, id); 171 if (length == 0) { 172 return NULL; 173 } 174 // Release result base address. 175 (*env)->ReleaseIntArrayElements(env, offsets, rawOffsets, 0); 176 return ret == NULL ? NULL : offsets; 177 }