jdk/src/jdk.runtime/share/native/libunpack/jni.cpp
changeset 29455 e451c01a5747
parent 29454 e5e9478e2ddb
parent 29433 c97e2d1bad97
child 29478 6637277d28cc
equal deleted inserted replaced
29454:e5e9478e2ddb 29455:e451c01a5747
     1 /*
       
     2  * Copyright (c) 2003, 2014, 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 #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 static jmethodID getUnpackerPtrMID;
       
    54 
       
    55 static char* dbg = null;
       
    56 
       
    57 #define THROW_IOE(x) JNU_ThrowIOException(env,x)
       
    58 
       
    59 #define CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(CERVTI_exception, CERVTI_message) \
       
    60     do { \
       
    61         if ((env)->ExceptionOccurred()) { \
       
    62             THROW_IOE(CERVTI_message); \
       
    63             return; \
       
    64         } \
       
    65         if ((CERVTI_exception) == NULL) { \
       
    66                 THROW_IOE(CERVTI_message); \
       
    67                 return; \
       
    68         } \
       
    69     } while (JNI_FALSE)
       
    70 
       
    71 
       
    72 #define CHECK_EXCEPTION_RETURN_VALUE(CERL_exception, CERL_return_value) \
       
    73     do { \
       
    74         if ((env)->ExceptionOccurred()) { \
       
    75             return CERL_return_value; \
       
    76         } \
       
    77         if ((CERL_exception) == NULL) { \
       
    78             return CERL_return_value; \
       
    79         } \
       
    80     } while (JNI_FALSE)
       
    81 
       
    82 
       
    83 // If these useful macros aren't defined in jni_util.h then define them here
       
    84 #ifndef CHECK_NULL_RETURN
       
    85 #define CHECK_NULL_RETURN(x, y) \
       
    86     do { \
       
    87         if ((x) == NULL) return (y); \
       
    88     } while (JNI_FALSE)
       
    89 #endif
       
    90 
       
    91 #ifndef CHECK_EXCEPTION_RETURN
       
    92 #define CHECK_EXCEPTION_RETURN(env, y) \
       
    93     do { \
       
    94         if ((*env)->ExceptionCheck(env)) return (y); \
       
    95     } while (JNI_FALSE)
       
    96 #endif
       
    97 
       
    98 static jlong read_input_via_jni(unpacker* self,
       
    99                                 void* buf, jlong minlen, jlong maxlen);
       
   100 
       
   101 static unpacker* get_unpacker(JNIEnv *env, jobject pObj, bool noCreate=false) {
       
   102   unpacker* uPtr;
       
   103   jlong p = env->CallLongMethod(pObj, getUnpackerPtrMID);
       
   104   uPtr = (unpacker*)jlong2ptr(p);
       
   105   if (uPtr == null) {
       
   106     if (noCreate)  return null;
       
   107     uPtr = new unpacker();
       
   108     if (uPtr == null) {
       
   109       THROW_IOE(ERROR_ENOMEM);
       
   110       return null;
       
   111     }
       
   112     //fprintf(stderr, "get_unpacker(%p) uPtr=%p initializing\n", pObj, uPtr);
       
   113     uPtr->init(read_input_via_jni);
       
   114     uPtr->jniobj = (void*) env->NewGlobalRef(pObj);
       
   115     env->SetLongField(pObj, unpackerPtrFID, ptr2jlong(uPtr));
       
   116   }
       
   117   uPtr->jnienv = env;  // keep refreshing this in case of MT access
       
   118   return uPtr;
       
   119 }
       
   120 
       
   121 // This is the harder trick:  Pull the current state out of mid-air.
       
   122 static unpacker* get_unpacker() {
       
   123   //fprintf(stderr, "get_unpacker()\n");
       
   124   JavaVM* vm = null;
       
   125   jsize nVM = 0;
       
   126   jint retval = JNI_GetCreatedJavaVMs(&vm, 1, &nVM);
       
   127   // other VM implements may differ, thus for correctness, we need these checks
       
   128   if (retval != JNI_OK || nVM != 1)
       
   129     return null;
       
   130   void* envRaw = null;
       
   131   vm->GetEnv(&envRaw, JNI_VERSION_1_1);
       
   132   JNIEnv* env = (JNIEnv*) envRaw;
       
   133   //fprintf(stderr, "get_unpacker() env=%p\n", env);
       
   134   CHECK_NULL_RETURN(env, NULL);
       
   135   jobject pObj = env->CallStaticObjectMethod(NIclazz, currentInstMID);
       
   136   // We should check upon the known non-null variable because here we want to check
       
   137   // only for pending exceptions. If pObj is null we'll deal with it later.
       
   138   CHECK_EXCEPTION_RETURN_VALUE(env, NULL);
       
   139   //fprintf(stderr, "get_unpacker0() pObj=%p\n", pObj);
       
   140   if (pObj != null) {
       
   141     // Got pObj and env; now do it the easy way.
       
   142     return get_unpacker(env, pObj);
       
   143   }
       
   144   // this should really not happen, if it does something is seriously
       
   145   // wrong throw an exception
       
   146   THROW_IOE(ERROR_INTERNAL);
       
   147   return null;
       
   148 }
       
   149 
       
   150 static void free_unpacker(JNIEnv *env, jobject pObj, unpacker* uPtr) {
       
   151   if (uPtr != null) {
       
   152     //fprintf(stderr, "free_unpacker(%p) uPtr=%p\n", pObj, uPtr);
       
   153     env->DeleteGlobalRef((jobject) uPtr->jniobj);
       
   154     uPtr->jniobj = null;
       
   155     uPtr->free();
       
   156     delete uPtr;
       
   157     env->SetLongField(pObj, unpackerPtrFID, (jlong)null);
       
   158    }
       
   159 }
       
   160 
       
   161 unpacker* unpacker::current() {
       
   162   return get_unpacker();
       
   163 }
       
   164 
       
   165 // Callback for fetching data, Java style.  Calls NativeUnpack.readInputFn().
       
   166 static jlong read_input_via_jni(unpacker* self,
       
   167                                 void* buf, jlong minlen, jlong maxlen) {
       
   168   JNIEnv* env = (JNIEnv*) self->jnienv;
       
   169   jobject pbuf = env->NewDirectByteBuffer(buf, maxlen);
       
   170   return env->CallLongMethod((jobject) self->jniobj, readInputMID,
       
   171                              pbuf, minlen);
       
   172 }
       
   173 
       
   174 JNIEXPORT void JNICALL
       
   175 Java_com_sun_java_util_jar_pack_NativeUnpack_initIDs(JNIEnv *env, jclass clazz) {
       
   176 #ifndef PRODUCT
       
   177   dbg = getenv("DEBUG_ATTACH");
       
   178   while( dbg != null) { sleep(10); }
       
   179 #endif
       
   180   NIclazz = (jclass) env->NewGlobalRef(clazz);
       
   181 
       
   182   unpackerPtrFID = env->GetFieldID(clazz, "unpackerPtr", "J");
       
   183   CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(unpackerPtrFID, ERROR_INIT);
       
   184 
       
   185   currentInstMID = env->GetStaticMethodID(clazz, "currentInstance",
       
   186                                           "()Ljava/lang/Object;");
       
   187   CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(currentInstMID, ERROR_INIT);
       
   188 
       
   189   readInputMID = env->GetMethodID(clazz, "readInputFn",
       
   190                                   "(Ljava/nio/ByteBuffer;J)J");
       
   191   CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(readInputMID, ERROR_INIT);
       
   192 
       
   193   getUnpackerPtrMID = env->GetMethodID(clazz, "getUnpackerPtr", "()J");
       
   194   CHECK_EXCEPTION_RETURN_VOID_THROW_IOE(getUnpackerPtrMID, ERROR_INIT);
       
   195 }
       
   196 
       
   197 JNIEXPORT jlong JNICALL
       
   198 Java_com_sun_java_util_jar_pack_NativeUnpack_start(JNIEnv *env, jobject pObj,
       
   199                                    jobject pBuf, jlong offset) {
       
   200   // try to get the unpacker pointer the hard way first, we do this to ensure
       
   201   // valid object pointers and env is intact, if not now is good time to bail.
       
   202   unpacker* uPtr = get_unpacker();
       
   203   //fprintf(stderr, "start(%p) uPtr=%p initializing\n", pObj, uPtr);
       
   204   CHECK_EXCEPTION_RETURN_VALUE(uPtr, -1);
       
   205   // redirect our io to the default log file or whatever.
       
   206   uPtr->redirect_stdio();
       
   207 
       
   208   void*  buf    = null;
       
   209   size_t buflen = 0;
       
   210   if (pBuf != null) {
       
   211     buf    = env->GetDirectBufferAddress(pBuf);
       
   212     buflen = (size_t)env->GetDirectBufferCapacity(pBuf);
       
   213     if (buflen == 0)  buf = null;
       
   214     if (buf == null) { THROW_IOE(ERROR_INTERNAL); return 0; }
       
   215     if ((size_t)offset >= buflen)
       
   216       { buf = null; buflen = 0; }
       
   217     else
       
   218       { buf = (char*)buf + (size_t)offset; buflen -= (size_t)offset; }
       
   219   }
       
   220   // before we start off we make sure there is no other error by the time we
       
   221   // get here
       
   222   if (uPtr->aborting()) {
       
   223     THROW_IOE(uPtr->get_abort_message());
       
   224     return 0;
       
   225   }
       
   226   uPtr->start(buf, buflen);
       
   227   if (uPtr->aborting()) {
       
   228     THROW_IOE(uPtr->get_abort_message());
       
   229     return 0;
       
   230   }
       
   231 
       
   232   return ((jlong)
       
   233           uPtr->get_segments_remaining() << 32)
       
   234     + uPtr->get_files_remaining();
       
   235 }
       
   236 
       
   237 JNIEXPORT jboolean JNICALL
       
   238 Java_com_sun_java_util_jar_pack_NativeUnpack_getNextFile(JNIEnv *env, jobject pObj,
       
   239                                          jobjectArray pParts) {
       
   240 
       
   241   unpacker* uPtr = get_unpacker(env, pObj);
       
   242   CHECK_EXCEPTION_RETURN_VALUE(uPtr, false);
       
   243   unpacker::file* filep = uPtr->get_next_file();
       
   244 
       
   245   if (uPtr->aborting()) {
       
   246     THROW_IOE(uPtr->get_abort_message());
       
   247     return false;
       
   248   }
       
   249 
       
   250   CHECK_NULL_RETURN(filep, false);
       
   251   assert(filep == &uPtr->cur_file);
       
   252 
       
   253   int pidx = 0, iidx = 0;
       
   254   jintArray pIntParts = (jintArray) env->GetObjectArrayElement(pParts, pidx++);
       
   255   CHECK_EXCEPTION_RETURN_VALUE(pIntParts, false);
       
   256   jint*     intParts  = env->GetIntArrayElements(pIntParts, null);
       
   257   intParts[iidx++] = (jint)( (julong)filep->size >> 32 );
       
   258   intParts[iidx++] = (jint)( (julong)filep->size >>  0 );
       
   259   intParts[iidx++] = filep->modtime;
       
   260   intParts[iidx++] = filep->deflate_hint() ? 1 : 0;
       
   261   env->ReleaseIntArrayElements(pIntParts, intParts, JNI_COMMIT);
       
   262   jstring filename = env->NewStringUTF(filep->name);
       
   263   CHECK_EXCEPTION_RETURN_VALUE(filename, false);
       
   264   env->SetObjectArrayElement(pParts, pidx++, filename);
       
   265   CHECK_EXCEPTION_RETURN_VALUE(uPtr, false);
       
   266   jobject pDataBuf = null;
       
   267   if (filep->data[0].len > 0) {
       
   268     pDataBuf = env->NewDirectByteBuffer(filep->data[0].ptr,
       
   269                                         filep->data[0].len);
       
   270     CHECK_EXCEPTION_RETURN_VALUE(pDataBuf, false);
       
   271   }
       
   272   env->SetObjectArrayElement(pParts, pidx++, pDataBuf);
       
   273   CHECK_EXCEPTION_RETURN_VALUE(uPtr, false);
       
   274   pDataBuf = null;
       
   275   if (filep->data[1].len > 0) {
       
   276     pDataBuf = env->NewDirectByteBuffer(filep->data[1].ptr,
       
   277                                         filep->data[1].len);
       
   278     CHECK_EXCEPTION_RETURN_VALUE(pDataBuf, false);
       
   279   }
       
   280   env->SetObjectArrayElement(pParts, pidx++, pDataBuf);
       
   281   CHECK_EXCEPTION_RETURN_VALUE(uPtr, false);
       
   282 
       
   283   return true;
       
   284 }
       
   285 
       
   286 
       
   287 JNIEXPORT jobject JNICALL
       
   288 Java_com_sun_java_util_jar_pack_NativeUnpack_getUnusedInput(JNIEnv *env, jobject pObj) {
       
   289   unpacker* uPtr = get_unpacker(env, pObj);
       
   290   CHECK_EXCEPTION_RETURN_VALUE(uPtr, NULL);
       
   291   unpacker::file* filep = &uPtr->cur_file;
       
   292 
       
   293   if (uPtr->aborting()) {
       
   294     THROW_IOE(uPtr->get_abort_message());
       
   295     return false;
       
   296   }
       
   297 
       
   298   // We have fetched all the files.
       
   299   // Now swallow up any remaining input.
       
   300   if (uPtr->input_remaining() == 0) {
       
   301     return null;
       
   302   } else {
       
   303     bytes remaining_bytes;
       
   304     remaining_bytes.malloc(uPtr->input_remaining());
       
   305     remaining_bytes.copyFrom(uPtr->input_scan(), uPtr->input_remaining());
       
   306     return env->NewDirectByteBuffer(remaining_bytes.ptr, remaining_bytes.len);
       
   307   }
       
   308 }
       
   309 
       
   310 JNIEXPORT jlong JNICALL
       
   311 Java_com_sun_java_util_jar_pack_NativeUnpack_finish(JNIEnv *env, jobject pObj) {
       
   312   unpacker* uPtr = get_unpacker(env, pObj, false);
       
   313   CHECK_EXCEPTION_RETURN_VALUE(uPtr, NULL);
       
   314   size_t consumed = uPtr->input_consumed();
       
   315   free_unpacker(env, pObj, uPtr);
       
   316   return consumed;
       
   317 }
       
   318 
       
   319 JNIEXPORT jboolean JNICALL
       
   320 Java_com_sun_java_util_jar_pack_NativeUnpack_setOption(JNIEnv *env, jobject pObj,
       
   321                                        jstring pProp, jstring pValue) {
       
   322   unpacker*   uPtr  = get_unpacker(env, pObj);
       
   323   const char* prop  = env->GetStringUTFChars(pProp, JNI_FALSE);
       
   324   CHECK_EXCEPTION_RETURN_VALUE(prop, false);
       
   325   const char* value = env->GetStringUTFChars(pValue, JNI_FALSE);
       
   326   CHECK_EXCEPTION_RETURN_VALUE(value, false);
       
   327   jboolean   retval = uPtr->set_option(prop, value);
       
   328   env->ReleaseStringUTFChars(pProp,  prop);
       
   329   env->ReleaseStringUTFChars(pValue, value);
       
   330   return retval;
       
   331 }
       
   332 
       
   333 JNIEXPORT jstring JNICALL
       
   334 Java_com_sun_java_util_jar_pack_NativeUnpack_getOption(JNIEnv *env, jobject pObj,
       
   335                                        jstring pProp) {
       
   336 
       
   337   unpacker*   uPtr  = get_unpacker(env, pObj);
       
   338   CHECK_EXCEPTION_RETURN_VALUE(uPtr, NULL);
       
   339   const char* prop  = env->GetStringUTFChars(pProp, JNI_FALSE);
       
   340   CHECK_EXCEPTION_RETURN_VALUE(prop, NULL);
       
   341   const char* value = uPtr->get_option(prop);
       
   342   CHECK_EXCEPTION_RETURN_VALUE(value, NULL);
       
   343   env->ReleaseStringUTFChars(pProp, prop);
       
   344   return env->NewStringUTF(value);
       
   345 }