author | ksrini |
Wed, 09 May 2012 07:28:12 -0700 | |
changeset 12680 | 289f842c0175 |
parent 5506 | 202f599c92aa |
child 16074 | 97593512057a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12680
289f842c0175
7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
#include <sys/types.h> |
|
26 |
||
27 |
#include <stdio.h> |
|
28 |
#include <string.h> |
|
29 |
#include <stdlib.h> |
|
30 |
#include <stdarg.h> |
|
31 |
||
32 |
||
33 |
#include <limits.h> |
|
34 |
||
35 |
#include <com_sun_java_util_jar_pack_NativeUnpack.h> |
|
36 |
||
37 |
#include "jni_util.h" |
|
38 |
||
39 |
#include "defines.h" |
|
40 |
#include "bytes.h" |
|
41 |
#include "utils.h" |
|
42 |
#include "coding.h" |
|
43 |
#include "bands.h" |
|
44 |
#include "constants.h" |
|
45 |
#include "zip.h" |
|
46 |
#include "unpack.h" |
|
47 |
||
48 |
||
49 |
static jfieldID unpackerPtrFID; |
|
50 |
static jmethodID currentInstMID; |
|
51 |
static jmethodID readInputMID; |
|
52 |
static jclass NIclazz; |
|
53 |
||
54 |
static char* dbg = null; |
|
55 |
||
56 |
#define THROW_IOE(x) JNU_ThrowIOException(env,x) |
|
57 |
||
58 |
static jlong read_input_via_jni(unpacker* self, |
|
59 |
void* buf, jlong minlen, jlong maxlen); |
|
60 |
||
61 |
static unpacker* get_unpacker(JNIEnv *env, jobject pObj, bool noCreate=false) { |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
62 |
unpacker* uPtr; |
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
63 |
uPtr = (unpacker*)jlong2ptr(env->GetLongField(pObj, unpackerPtrFID)); |
2 | 64 |
//fprintf(stderr, "get_unpacker(%p) uPtr=%p\n", pObj, uPtr); |
65 |
if (uPtr == null) { |
|
66 |
if (noCreate) return null; |
|
67 |
uPtr = new unpacker(); |
|
68 |
if (uPtr == null) { |
|
69 |
THROW_IOE(ERROR_ENOMEM); |
|
70 |
return null; |
|
71 |
} |
|
72 |
//fprintf(stderr, "get_unpacker(%p) uPtr=%p initializing\n", pObj, uPtr); |
|
73 |
uPtr->init(read_input_via_jni); |
|
74 |
uPtr->jniobj = (void*) env->NewGlobalRef(pObj); |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
75 |
env->SetLongField(pObj, unpackerPtrFID, ptr2jlong(uPtr)); |
2 | 76 |
} |
77 |
uPtr->jnienv = env; // keep refreshing this in case of MT access |
|
78 |
return uPtr; |
|
79 |
} |
|
80 |
||
81 |
// This is the harder trick: Pull the current state out of mid-air. |
|
82 |
static unpacker* get_unpacker() { |
|
83 |
//fprintf(stderr, "get_unpacker()\n"); |
|
84 |
JavaVM* vm = null; |
|
12680
289f842c0175
7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents:
5506
diff
changeset
|
85 |
jsize nVM = 0; |
289f842c0175
7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents:
5506
diff
changeset
|
86 |
jint retval = JNI_GetCreatedJavaVMs(&vm, 1, &nVM); |
289f842c0175
7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents:
5506
diff
changeset
|
87 |
// other VM implements may differ, thus for correctness, we need these checks |
289f842c0175
7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents:
5506
diff
changeset
|
88 |
if (retval != JNI_OK || nVM != 1) |
289f842c0175
7166955: (pack200) JNI_GetCreatedJavaVMs needs additional checking
ksrini
parents:
5506
diff
changeset
|
89 |
return null; |
2 | 90 |
void* envRaw = null; |
91 |
vm->GetEnv(&envRaw, JNI_VERSION_1_1); |
|
92 |
JNIEnv* env = (JNIEnv*) envRaw; |
|
93 |
//fprintf(stderr, "get_unpacker() env=%p\n", env); |
|
94 |
if (env == null) |
|
95 |
return null; |
|
96 |
jobject pObj = env->CallStaticObjectMethod(NIclazz, currentInstMID); |
|
97 |
//fprintf(stderr, "get_unpacker() pObj=%p\n", pObj); |
|
98 |
if (pObj == null) |
|
99 |
return null; |
|
100 |
// Got pObj and env; now do it the easy way. |
|
101 |
return get_unpacker(env, pObj); |
|
102 |
} |
|
103 |
||
104 |
static void free_unpacker(JNIEnv *env, jobject pObj, unpacker* uPtr) { |
|
105 |
if (uPtr != null) { |
|
106 |
//fprintf(stderr, "free_unpacker(%p) uPtr=%p\n", pObj, uPtr); |
|
107 |
env->DeleteGlobalRef((jobject) uPtr->jniobj); |
|
108 |
uPtr->jniobj = null; |
|
109 |
uPtr->free(); |
|
110 |
delete uPtr; |
|
111 |
env->SetLongField(pObj, unpackerPtrFID, (jlong)null); |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
unpacker* unpacker::current() { |
|
116 |
return get_unpacker(); |
|
117 |
} |
|
118 |
||
119 |
// Callback for fetching data, Java style. Calls NativeUnpack.readInputFn(). |
|
120 |
static jlong read_input_via_jni(unpacker* self, |
|
121 |
void* buf, jlong minlen, jlong maxlen) { |
|
122 |
JNIEnv* env = (JNIEnv*) self->jnienv; |
|
123 |
jobject pbuf = env->NewDirectByteBuffer(buf, maxlen); |
|
124 |
return env->CallLongMethod((jobject) self->jniobj, readInputMID, |
|
125 |
pbuf, minlen); |
|
126 |
} |
|
127 |
||
128 |
JNIEXPORT void JNICALL |
|
129 |
Java_com_sun_java_util_jar_pack_NativeUnpack_initIDs(JNIEnv *env, jclass clazz) { |
|
130 |
dbg = getenv("DEBUG_ATTACH"); |
|
131 |
while( dbg != null) { sleep(10); } |
|
132 |
NIclazz = (jclass) env->NewGlobalRef(clazz); |
|
133 |
unpackerPtrFID = env->GetFieldID(clazz, "unpackerPtr", "J"); |
|
134 |
currentInstMID = env->GetStaticMethodID(clazz, "currentInstance", |
|
135 |
"()Ljava/lang/Object;"); |
|
136 |
readInputMID = env->GetMethodID(clazz, "readInputFn", |
|
137 |
"(Ljava/nio/ByteBuffer;J)J"); |
|
138 |
if (unpackerPtrFID == null || |
|
139 |
currentInstMID == null || |
|
140 |
readInputMID == null || |
|
141 |
NIclazz == null) { |
|
142 |
THROW_IOE("cannot init class members"); |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
JNIEXPORT jlong JNICALL |
|
147 |
Java_com_sun_java_util_jar_pack_NativeUnpack_start(JNIEnv *env, jobject pObj, |
|
148 |
jobject pBuf, jlong offset) { |
|
149 |
unpacker* uPtr = get_unpacker(env, pObj); |
|
150 |
||
151 |
// redirect our io to the default log file or whatever. |
|
152 |
uPtr->redirect_stdio(); |
|
153 |
||
154 |
void* buf = null; |
|
155 |
size_t buflen = 0; |
|
156 |
if (pBuf != null) { |
|
157 |
buf = env->GetDirectBufferAddress(pBuf); |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
158 |
buflen = (size_t)env->GetDirectBufferCapacity(pBuf); |
2 | 159 |
if (buflen == 0) buf = null; |
160 |
if (buf == null) { THROW_IOE(ERROR_INTERNAL); return 0; } |
|
161 |
if ((size_t)offset >= buflen) |
|
162 |
{ buf = null; buflen = 0; } |
|
163 |
else |
|
164 |
{ buf = (char*)buf + (size_t)offset; buflen -= (size_t)offset; } |
|
165 |
} |
|
166 |
||
167 |
uPtr->start(buf, buflen); |
|
168 |
if (uPtr->aborting()) { |
|
169 |
THROW_IOE(uPtr->get_abort_message()); |
|
170 |
return 0; |
|
171 |
} |
|
172 |
||
173 |
return ((jlong) |
|
174 |
uPtr->get_segments_remaining() << 32) |
|
175 |
+ uPtr->get_files_remaining(); |
|
176 |
} |
|
177 |
||
178 |
JNIEXPORT jboolean JNICALL |
|
179 |
Java_com_sun_java_util_jar_pack_NativeUnpack_getNextFile(JNIEnv *env, jobject pObj, |
|
180 |
jobjectArray pParts) { |
|
181 |
||
182 |
unpacker* uPtr = get_unpacker(env, pObj); |
|
183 |
unpacker::file* filep = uPtr->get_next_file(); |
|
184 |
||
185 |
if (uPtr->aborting()) { |
|
186 |
THROW_IOE(uPtr->get_abort_message()); |
|
187 |
return false; |
|
188 |
} |
|
189 |
||
190 |
if (filep == null) { |
|
191 |
return false; // end of the sequence |
|
192 |
} |
|
193 |
assert(filep == &uPtr->cur_file); |
|
194 |
||
195 |
int pidx = 0, iidx = 0; |
|
196 |
jintArray pIntParts = (jintArray) env->GetObjectArrayElement(pParts, pidx++); |
|
197 |
jint* intParts = env->GetIntArrayElements(pIntParts, null); |
|
198 |
intParts[iidx++] = (jint)( (julong)filep->size >> 32 ); |
|
199 |
intParts[iidx++] = (jint)( (julong)filep->size >> 0 ); |
|
200 |
intParts[iidx++] = filep->modtime; |
|
201 |
intParts[iidx++] = filep->deflate_hint() ? 1 : 0; |
|
202 |
env->ReleaseIntArrayElements(pIntParts, intParts, JNI_COMMIT); |
|
203 |
||
204 |
env->SetObjectArrayElement(pParts, pidx++, env->NewStringUTF(filep->name)); |
|
205 |
||
206 |
jobject pDataBuf = null; |
|
207 |
if (filep->data[0].len > 0) |
|
208 |
pDataBuf = env->NewDirectByteBuffer(filep->data[0].ptr, |
|
209 |
filep->data[0].len); |
|
210 |
env->SetObjectArrayElement(pParts, pidx++, pDataBuf); |
|
211 |
pDataBuf = null; |
|
212 |
if (filep->data[1].len > 0) |
|
213 |
pDataBuf = env->NewDirectByteBuffer(filep->data[1].ptr, |
|
214 |
filep->data[1].len); |
|
215 |
env->SetObjectArrayElement(pParts, pidx++, pDataBuf); |
|
216 |
||
217 |
return true; |
|
218 |
} |
|
219 |
||
220 |
||
221 |
JNIEXPORT jobject JNICALL |
|
222 |
Java_com_sun_java_util_jar_pack_NativeUnpack_getUnusedInput(JNIEnv *env, jobject pObj) { |
|
223 |
unpacker* uPtr = get_unpacker(env, pObj); |
|
224 |
unpacker::file* filep = &uPtr->cur_file; |
|
225 |
||
226 |
if (uPtr->aborting()) { |
|
227 |
THROW_IOE(uPtr->get_abort_message()); |
|
228 |
return false; |
|
229 |
} |
|
230 |
||
231 |
// We have fetched all the files. |
|
232 |
// Now swallow up any remaining input. |
|
233 |
if (uPtr->input_remaining() == 0) |
|
234 |
return null; |
|
235 |
else |
|
236 |
return env->NewDirectByteBuffer(uPtr->input_scan(), |
|
237 |
uPtr->input_remaining()); |
|
238 |
} |
|
239 |
||
240 |
JNIEXPORT jlong JNICALL |
|
241 |
Java_com_sun_java_util_jar_pack_NativeUnpack_finish(JNIEnv *env, jobject pObj) { |
|
242 |
unpacker* uPtr = get_unpacker(env, pObj, false); |
|
243 |
if (uPtr == null) return 0; |
|
244 |
size_t consumed = uPtr->input_consumed(); |
|
245 |
free_unpacker(env, pObj, uPtr); |
|
246 |
return consumed; |
|
247 |
} |
|
248 |
||
249 |
JNIEXPORT jboolean JNICALL |
|
250 |
Java_com_sun_java_util_jar_pack_NativeUnpack_setOption(JNIEnv *env, jobject pObj, |
|
251 |
jstring pProp, jstring pValue) { |
|
252 |
unpacker* uPtr = get_unpacker(env, pObj); |
|
253 |
const char* prop = env->GetStringUTFChars(pProp, JNI_FALSE); |
|
254 |
const char* value = env->GetStringUTFChars(pValue, JNI_FALSE); |
|
255 |
jboolean retval = uPtr->set_option(prop, value); |
|
256 |
env->ReleaseStringUTFChars(pProp, prop); |
|
257 |
env->ReleaseStringUTFChars(pValue, value); |
|
258 |
return retval; |
|
259 |
} |
|
260 |
||
261 |
JNIEXPORT jstring JNICALL |
|
262 |
Java_com_sun_java_util_jar_pack_NativeUnpack_getOption(JNIEnv *env, jobject pObj, |
|
263 |
jstring pProp) { |
|
264 |
||
265 |
unpacker* uPtr = get_unpacker(env, pObj); |
|
266 |
const char* prop = env->GetStringUTFChars(pProp, JNI_FALSE); |
|
267 |
const char* value = uPtr->get_option(prop); |
|
268 |
env->ReleaseStringUTFChars(pProp, prop); |
|
269 |
if (value == null) return null; |
|
270 |
return env->NewStringUTF(value); |
|
271 |
} |