aboutsummaryrefslogtreecommitdiff
path: root/src/share/native/com/sun/java/util/jar/pack/jni.cpp
blob: e9109cbec96a4a2e5bea23abcdb7d5f3aaad0bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
#include <sys/types.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>


#include <limits.h>

#include <com_sun_java_util_jar_pack_NativeUnpack.h>

#include "jni_util.h"

#include "defines.h"
#include "bytes.h"
#include "utils.h"
#include "coding.h"
#include "bands.h"
#include "constants.h"
#include "zip.h"
#include "unpack.h"


static jfieldID  unpackerPtrFID;
static jmethodID currentInstMID;
static jmethodID readInputMID;
static jclass    NIclazz;
static jmethodID getUnpackerPtrMID;

static char* dbg = null;

#define THROW_IOE(x) JNU_ThrowIOException(env,x)

#define CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(CERVTI_exception, CERVTI_message) \
    do { \
        if ((env)->ExceptionOccurred()) { \
            THROW_IOE(CERVTI_message); \
            return; \
        } \
        if ((CERVTI_exception) == NULL) { \
                THROW_IOE(CERVTI_message); \
                return; \
        } \
    } while (JNI_FALSE)


#define CHECK_EXCEPTION_RETURN_VALUE(CERL_exception, CERL_return_value) \
    do { \
        if ((env)->ExceptionOccurred()) { \
            return CERL_return_value; \
        } \
        if ((CERL_exception) == NULL) { \
            return CERL_return_value; \
        } \
    } while (JNI_FALSE)


// If these useful macros aren't defined in jni_util.h then define them here
#ifndef CHECK_NULL_RETURN
#define CHECK_NULL_RETURN(x, y) \
    do { \
        if ((x) == NULL) return (y); \
    } while (JNI_FALSE)
#endif

#ifndef CHECK_EXCEPTION_RETURN
#define CHECK_EXCEPTION_RETURN(env, y) \
    do { \
        if ((*env)->ExceptionCheck(env)) return (y); \
    } while (JNI_FALSE)
#endif

static jlong read_input_via_jni(unpacker* self,
                                void* buf, jlong minlen, jlong maxlen);

static unpacker* get_unpacker(JNIEnv *env, jobject pObj, bool noCreate=false) {
  unpacker* uPtr;
  jlong p = env->CallLongMethod(pObj, getUnpackerPtrMID);
  uPtr = (unpacker*)jlong2ptr(p);
  if (uPtr == null) {
    if (noCreate)  return null;
    uPtr = new unpacker();
    if (uPtr == null) {
      THROW_IOE(ERROR_ENOMEM);
      return null;
    }
    //fprintf(stderr, "get_unpacker(%p) uPtr=%p initializing\n", pObj, uPtr);
    uPtr->init(read_input_via_jni);
    uPtr->jniobj = (void*) env->NewGlobalRef(pObj);
    env->SetLongField(pObj, unpackerPtrFID, ptr2jlong(uPtr));
  }
  uPtr->jnienv = env;  // keep refreshing this in case of MT access
  return uPtr;
}

// This is the harder trick:  Pull the current state out of mid-air.
static unpacker* get_unpacker() {
  //fprintf(stderr, "get_unpacker()\n");
  JavaVM* vm = null;
  jsize nVM = 0;
  jint retval = JNI_GetCreatedJavaVMs(&vm, 1, &nVM);
  // other VM implements may differ, thus for correctness, we need these checks
  if (retval != JNI_OK || nVM != 1)
    return null;
  void* envRaw = null;
  vm->GetEnv(&envRaw, JNI_VERSION_1_1);
  JNIEnv* env = (JNIEnv*) envRaw;
  //fprintf(stderr, "get_unpacker() env=%p\n", env);
  CHECK_NULL_RETURN(env, NULL);
  jobject pObj = env->CallStaticObjectMethod(NIclazz, currentInstMID);
  // We should check upon the known non-null variable because here we want to check
  // only for pending exceptions. If pObj is null we'll deal with it later.
  CHECK_EXCEPTION_RETURN_VALUE(env, NULL);
  //fprintf(stderr, "get_unpacker0() pObj=%p\n", pObj);
  if (pObj != null) {
    // Got pObj and env; now do it the easy way.
    return get_unpacker(env, pObj);
  }
  // this should really not happen, if it does something is seriously
  // wrong throw an exception
  THROW_IOE(ERROR_INTERNAL);
  return null;
}

static void free_unpacker(JNIEnv *env, jobject pObj, unpacker* uPtr) {
  if (uPtr != null) {
    //fprintf(stderr, "free_unpacker(%p) uPtr=%p\n", pObj, uPtr);
    env->DeleteGlobalRef((jobject) uPtr->jniobj);
    uPtr->jniobj = null;
    uPtr->free();
    delete uPtr;
    env->SetLongField(pObj, unpackerPtrFID, (jlong)null);
   }
}

unpacker* unpacker::current() {
  return get_unpacker();
}

// Callback for fetching data, Java style.  Calls NativeUnpack.readInputFn().
static jlong read_input_via_jni(unpacker* self,
                                void* buf, jlong minlen, jlong maxlen) {
  JNIEnv* env = (JNIEnv*) self->jnienv;
  jobject pbuf = env->NewDirectByteBuffer(buf, maxlen);
  return env->CallLongMethod((jobject) self->jniobj, readInputMID,
                             pbuf, minlen);
}

JNIEXPORT void JNICALL
Java_com_sun_java_util_jar_pack_NativeUnpack_initIDs(JNIEnv *env, jclass clazz) {
#ifndef PRODUCT
  dbg = getenv("DEBUG_ATTACH");
  while( dbg != null) { sleep(10); }
#endif
  NIclazz = (jclass) env->NewGlobalRef(clazz);

  unpackerPtrFID = env->GetFieldID(clazz, "unpackerPtr", "J");
  CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(unpackerPtrFID, ERROR_INIT);

  currentInstMID = env->GetStaticMethodID(clazz, "currentInstance",
                                          "()Ljava/lang/Object;");
  CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(currentInstMID, ERROR_INIT);

  readInputMID = env->GetMethodID(clazz, "readInputFn",
                                  "(Ljava/nio/ByteBuffer;J)J");
  CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(readInputMID, ERROR_INIT);

  getUnpackerPtrMID = env->GetMethodID(clazz, "getUnpackerPtr", "()J");
  CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(getUnpackerPtrMID, ERROR_INIT);
}

JNIEXPORT jlong JNICALL
Java_com_sun_java_util_jar_pack_NativeUnpack_start(JNIEnv *env, jobject pObj,
                                   jobject pBuf, jlong offset) {
  // try to get the unpacker pointer the hard way first, we do this to ensure
  // valid object pointers and env is intact, if not now is good time to bail.
  unpacker* uPtr = get_unpacker();
  //fprintf(stderr, "start(%p) uPtr=%p initializing\n", pObj, uPtr);
  CHECK_EXCEPTION_RETURN_VALUE(uPtr, -1);
  // redirect our io to the default log file or whatever.
  uPtr->redirect_stdio();

  void*  buf    = null;
  size_t buflen = 0;
  if (pBuf != null) {
    buf    = env->GetDirectBufferAddress(pBuf);
    buflen = (size_t)env->GetDirectBufferCapacity(pBuf);
    if (buflen == 0)  buf = null;
    if (buf == null) { THROW_IOE(ERROR_INTERNAL); return 0; }
    if ((size_t)offset >= buflen)
      { buf = null; buflen = 0; }
    else
      { buf = (char*)buf + (size_t)offset; buflen -= (size_t)offset; }
  }
  // before we start off we make sure there is no other error by the time we
  // get here
  if (uPtr->aborting()) {
    THROW_IOE(uPtr->get_abort_message());
    return 0;
  }
  uPtr->start(buf, buflen);
  if (uPtr->aborting()) {
    THROW_IOE(uPtr->get_abort_message());
    return 0;
  }

  return ((jlong)
          uPtr->get_segments_remaining() << 32)
    + uPtr->get_files_remaining();
}

JNIEXPORT jboolean JNICALL
Java_com_sun_java_util_jar_pack_NativeUnpack_getNextFile(JNIEnv *env, jobject pObj,
                                         jobjectArray pParts) {

  unpacker* uPtr = get_unpacker(env, pObj);
  CHECK_EXCEPTION_RETURN_VALUE(uPtr, false);
  unpacker::file* filep = uPtr->get_next_file();

  if (uPtr->aborting()) {
    THROW_IOE(uPtr->get_abort_message());
    return false;
  }

  CHECK_NULL_RETURN(filep, false);
  assert(filep == &uPtr->cur_file);

  int pidx = 0, iidx = 0;
  jintArray pIntParts = (jintArray) env->GetObjectArrayElement(pParts, pidx++);
  CHECK_EXCEPTION_RETURN_VALUE(pIntParts, false);
  jint*     intParts  = env->GetIntArrayElements(pIntParts, null);
  intParts[iidx++] = (jint)( (julong)filep->size >> 32 );
  intParts[iidx++] = (jint)( (julong)filep->size >>  0 );
  intParts[iidx++] = filep->modtime;
  intParts[iidx++] = filep->deflate_hint() ? 1 : 0;
  env->ReleaseIntArrayElements(pIntParts, intParts, JNI_COMMIT);
  jstring filename = env->NewStringUTF(filep->name);
  CHECK_EXCEPTION_RETURN_VALUE(filename, false);
  env->SetObjectArrayElement(pParts, pidx++, filename);
  CHECK_EXCEPTION_RETURN_VALUE(uPtr, false);
  jobject pDataBuf = null;
  if (filep->data[0].len > 0) {
    pDataBuf = env->NewDirectByteBuffer(filep->data[0].ptr,
                                        filep->data[0].len);
    CHECK_EXCEPTION_RETURN_VALUE(pDataBuf, false);
  }
  env->SetObjectArrayElement(pParts, pidx++, pDataBuf);
  CHECK_EXCEPTION_RETURN_VALUE(uPtr, false);
  pDataBuf = null;
  if (filep->data[1].len > 0) {
    pDataBuf = env->NewDirectByteBuffer(filep->data[1].ptr,
                                        filep->data[1].len);
    CHECK_EXCEPTION_RETURN_VALUE(pDataBuf, false);
  }
  env->SetObjectArrayElement(pParts, pidx++, pDataBuf);
  CHECK_EXCEPTION_RETURN_VALUE(uPtr, false);

  return true;
}


JNIEXPORT jobject JNICALL
Java_com_sun_java_util_jar_pack_NativeUnpack_getUnusedInput(JNIEnv *env, jobject pObj) {
  unpacker* uPtr = get_unpacker(env, pObj);
  CHECK_EXCEPTION_RETURN_VALUE(uPtr, NULL);
  unpacker::file* filep = &uPtr->cur_file;

  if (uPtr->aborting()) {
    THROW_IOE(uPtr->get_abort_message());
    return false;
  }

  // We have fetched all the files.
  // Now swallow up any remaining input.
  if (uPtr->input_remaining() == 0) {
    return null;
  } else {
    bytes remaining_bytes;
    remaining_bytes.malloc(uPtr->input_remaining());
    remaining_bytes.copyFrom(uPtr->input_scan(), uPtr->input_remaining());
    return env->NewDirectByteBuffer(remaining_bytes.ptr, remaining_bytes.len);
  }
}

JNIEXPORT jlong JNICALL
Java_com_sun_java_util_jar_pack_NativeUnpack_finish(JNIEnv *env, jobject pObj) {
  unpacker* uPtr = get_unpacker(env, pObj, false);
  CHECK_EXCEPTION_RETURN_VALUE(uPtr, NULL);
  size_t consumed = uPtr->input_consumed();
  free_unpacker(env, pObj, uPtr);
  return consumed;
}

JNIEXPORT jboolean JNICALL
Java_com_sun_java_util_jar_pack_NativeUnpack_setOption(JNIEnv *env, jobject pObj,
                                       jstring pProp, jstring pValue) {
  unpacker*   uPtr  = get_unpacker(env, pObj);
  const char* prop  = env->GetStringUTFChars(pProp, JNI_FALSE);
  CHECK_EXCEPTION_RETURN_VALUE(prop, false);
  const char* value = env->GetStringUTFChars(pValue, JNI_FALSE);
  CHECK_EXCEPTION_RETURN_VALUE(value, false);
  jboolean   retval = uPtr->set_option(prop, value);
  env->ReleaseStringUTFChars(pProp,  prop);
  env->ReleaseStringUTFChars(pValue, value);
  return retval;
}

JNIEXPORT jstring JNICALL
Java_com_sun_java_util_jar_pack_NativeUnpack_getOption(JNIEnv *env, jobject pObj,
                                       jstring pProp) {

  unpacker*   uPtr  = get_unpacker(env, pObj);
  CHECK_EXCEPTION_RETURN_VALUE(uPtr, NULL);
  const char* prop  = env->GetStringUTFChars(pProp, JNI_FALSE);
  CHECK_EXCEPTION_RETURN_VALUE(prop, NULL);
  const char* value = uPtr->get_option(prop);
  CHECK_EXCEPTION_RETURN_VALUE(value, NULL);
  env->ReleaseStringUTFChars(pProp, prop);
  return env->NewStringUTF(value);
}