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 if (byteArray == NULL) { |
|
108 return NULL; |
|
109 } |
|
110 // Get array base address. |
|
111 rawBytes = (*env)->GetByteArrayElements(env, byteArray, NULL); |
|
112 // Copy bytes from image string table. |
|
113 memcpy(rawBytes, data, size); |
|
114 // Release byte array base address. |
|
115 (*env)->ReleaseByteArrayElements(env, byteArray, rawBytes, 0); |
|
116 return byteArray; |
|
117 } |
|
118 |
|
119 JNIEXPORT jlongArray JNICALL |
|
120 Java_jdk_internal_jimage_ImageNativeSubstrate_getAttributes(JNIEnv *env, |
|
121 jclass cls, jlong id, jint offset) { |
|
122 // Allocate a jlong large enough for all location attributes. |
|
123 jlongArray attributes; |
|
124 jlong* rawAttributes; |
|
125 jlong* ret; |
|
126 |
|
127 attributes = (*env)->NewLongArray(env, JVM_ImageGetAttributesCount(env)); |
|
128 if (attributes == NULL) { |
|
129 return NULL; |
|
130 } |
|
131 // Get base address for jlong array. |
|
132 rawAttributes = (*env)->GetLongArrayElements(env, attributes, NULL); |
|
133 ret = JVM_ImageGetAttributes(env, rawAttributes, id, offset); |
|
134 // Release jlong array base address. |
|
135 (*env)->ReleaseLongArrayElements(env, attributes, rawAttributes, 0); |
|
136 return ret == NULL ? NULL : attributes; |
|
137 } |
|
138 |
|
139 JNIEXPORT jlongArray JNICALL |
|
140 Java_jdk_internal_jimage_ImageNativeSubstrate_findAttributes(JNIEnv *env, |
|
141 jclass cls, jlong id, jbyteArray utf8) { |
|
142 // Allocate a jlong large enough for all location attributes. |
|
143 jsize count; |
|
144 jlongArray attributes; |
|
145 jlong* rawAttributes; |
|
146 jsize size; |
|
147 jbyte* rawBytes; |
|
148 jlong* ret; |
|
149 |
|
150 count = JVM_ImageGetAttributesCount(env); |
|
151 attributes = (*env)->NewLongArray(env, JVM_ImageGetAttributesCount(env)); |
|
152 if (attributes == NULL) { |
|
153 return NULL; |
|
154 } |
|
155 // Get base address for jlong array. |
|
156 rawAttributes = (*env)->GetLongArrayElements(env, attributes, NULL); |
|
157 size = (*env)->GetArrayLength(env, utf8); |
|
158 rawBytes = (*env)->GetByteArrayElements(env, utf8, NULL); |
|
159 ret = JVM_ImageFindAttributes(env, rawAttributes, rawBytes, size, id); |
|
160 (*env)->ReleaseByteArrayElements(env, utf8, rawBytes, 0); |
|
161 // Release jlong array base address. |
|
162 (*env)->ReleaseLongArrayElements(env, attributes, rawAttributes, 0); |
|
163 return ret == NULL ? NULL : attributes; |
|
164 |
|
165 } |
|
166 |
|
167 JNIEXPORT jintArray JNICALL |
|
168 Java_jdk_internal_jimage_ImageNativeSubstrate_attributeOffsets(JNIEnv *env, |
|
169 jclass cls, jlong id) { |
|
170 unsigned int length; |
|
171 jintArray offsets; |
|
172 jint* rawOffsets; |
|
173 jint* ret; |
|
174 |
|
175 length = JVM_ImageAttributeOffsetsLength(env, id); |
|
176 offsets = (*env)->NewIntArray(env, length); |
|
177 if (offsets == NULL) { |
|
178 return NULL; |
|
179 } |
|
180 // Get base address of result. |
|
181 rawOffsets = (*env)->GetIntArrayElements(env, offsets, NULL); |
|
182 ret = JVM_ImageAttributeOffsets(env, rawOffsets, length, id); |
|
183 if (length == 0) { |
|
184 return NULL; |
|
185 } |
|
186 // Release result base address. |
|
187 (*env)->ReleaseIntArrayElements(env, offsets, rawOffsets, 0); |
|
188 return ret == NULL ? NULL : offsets; |
|
189 } |
|