1 /*
   2  * Copyright (c) 1997, 2010, 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 /*
  27  * Native method support for java.util.zip.Inflater
  28  */
  29 
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <errno.h>
  33 #include <string.h>
  34 #include "jlong.h"
  35 #include "jni.h"
  36 #include "jvm.h"
  37 #include "jni_util.h"
  38 #include "zlib.h"
  39 #include "java_util_zip_Inflater.h"
  40 
  41 #define ThrowDataFormatException(env, msg) \
  42         JNU_ThrowByName(env, "java/util/zip/DataFormatException", msg)
  43 
  44 static jfieldID needDictID;
  45 static jfieldID finishedID;
  46 static jfieldID bufID, offID, lenID;
  47 
  48 JNIEXPORT void JNICALL
  49 Java_java_util_zip_Inflater_initIDs(JNIEnv *env, jclass cls)
  50 {
  51     needDictID = (*env)->GetFieldID(env, cls, "needDict", "Z");
  52     finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
  53     bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
  54     offID = (*env)->GetFieldID(env, cls, "off", "I");
  55     lenID = (*env)->GetFieldID(env, cls, "len", "I");
  56 }
  57 
  58 JNIEXPORT jlong JNICALL
  59 Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap)
  60 {
  61     z_stream *strm = calloc(1, sizeof(z_stream));
  62 
  63     if (strm == 0) {
  64         JNU_ThrowOutOfMemoryError(env, 0);
  65         return jlong_zero;
  66     } else {
  67         char *msg;
  68         switch (inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS)) {
  69           case Z_OK:
  70             return ptr_to_jlong(strm);
  71           case Z_MEM_ERROR:
  72             free(strm);
  73             JNU_ThrowOutOfMemoryError(env, 0);
  74             return jlong_zero;
  75           default:
  76             msg = strm->msg;
  77             free(strm);
  78             JNU_ThrowInternalError(env, msg);
  79             return jlong_zero;
  80         }
  81     }
  82 }
  83 
  84 JNIEXPORT void JNICALL
  85 Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
  86                                           jarray b, jint off, jint len)
  87 {
  88     Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
  89     int res;
  90     if (buf == 0) /* out of memory */
  91         return;
  92     res = inflateSetDictionary(jlong_to_ptr(addr), buf + off, len);
  93     (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
  94     switch (res) {
  95     case Z_OK:
  96         break;
  97     case Z_STREAM_ERROR:
  98     case Z_DATA_ERROR:
  99         JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg);
 100         break;
 101     default:
 102         JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
 103         break;
 104     }
 105 }
 106 
 107 JNIEXPORT jint JNICALL
 108 Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,
 109                                          jarray b, jint off, jint len)
 110 {
 111     z_stream *strm = jlong_to_ptr(addr);
 112     jarray this_buf = (jarray)(*env)->GetObjectField(env, this, bufID);
 113     jint this_off = (*env)->GetIntField(env, this, offID);
 114     jint this_len = (*env)->GetIntField(env, this, lenID);
 115 
 116     jbyte *in_buf;
 117     jbyte *out_buf;
 118     int ret;
 119 
 120     in_buf  = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
 121     if (in_buf == NULL) {
 122         if (this_len != 0)
 123             JNU_ThrowOutOfMemoryError(env, 0);
 124         return 0;
 125     }
 126     out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
 127     if (out_buf == NULL) {
 128         (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
 129         if (len != 0)
 130             JNU_ThrowOutOfMemoryError(env, 0);
 131         return 0;
 132     }
 133     strm->next_in  = (Bytef *) (in_buf + this_off);
 134     strm->next_out = (Bytef *) (out_buf + off);
 135     strm->avail_in  = this_len;
 136     strm->avail_out = len;
 137     ret = inflate(strm, Z_PARTIAL_FLUSH);
 138     (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
 139     (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
 140 
 141     switch (ret) {
 142     case Z_STREAM_END:
 143         (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
 144         /* fall through */
 145     case Z_OK:
 146         this_off += this_len - strm->avail_in;
 147         (*env)->SetIntField(env, this, offID, this_off);
 148         (*env)->SetIntField(env, this, lenID, strm->avail_in);
 149         return len - strm->avail_out;
 150     case Z_NEED_DICT:
 151         (*env)->SetBooleanField(env, this, needDictID, JNI_TRUE);
 152         /* Might have consumed some input here! */
 153         this_off += this_len - strm->avail_in;
 154         (*env)->SetIntField(env, this, offID, this_off);
 155         (*env)->SetIntField(env, this, lenID, strm->avail_in);
 156         return 0;
 157     case Z_BUF_ERROR:
 158         return 0;
 159     case Z_DATA_ERROR:
 160         ThrowDataFormatException(env, strm->msg);
 161         return 0;
 162     case Z_MEM_ERROR:
 163         JNU_ThrowOutOfMemoryError(env, 0);
 164         return 0;
 165     default:
 166         JNU_ThrowInternalError(env, strm->msg);
 167         return 0;
 168     }
 169 }
 170 
 171 JNIEXPORT jint JNICALL
 172 Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
 173 {
 174     return ((z_stream *)jlong_to_ptr(addr))->adler;
 175 }
 176 
 177 JNIEXPORT jlong JNICALL
 178 Java_java_util_zip_Inflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
 179 {
 180     return ((z_stream *)jlong_to_ptr(addr))->total_in;
 181 }
 182 
 183 JNIEXPORT jlong JNICALL
 184 Java_java_util_zip_Inflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
 185 {
 186     return ((z_stream *)jlong_to_ptr(addr))->total_out;
 187 }
 188 
 189 JNIEXPORT void JNICALL
 190 Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
 191 {
 192     if (inflateReset(jlong_to_ptr(addr)) != Z_OK) {
 193         JNU_ThrowInternalError(env, 0);
 194     }
 195 }
 196 
 197 JNIEXPORT void JNICALL
 198 Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr)
 199 {
 200     if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
 201         JNU_ThrowInternalError(env, 0);
 202     } else {
 203         free(jlong_to_ptr(addr));
 204     }
 205 }