1 /*
   2  * Copyright (c) 1997, 2018, 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 <stddef.h>
  31 #include <stdio.h>
  32 #include <stdlib.h>
  33 #include <errno.h>
  34 #include <string.h>
  35 #include "jlong.h"
  36 #include "jni.h"
  37 #include "jvm.h"
  38 #include "jni_util.h"
  39 #include <zlib.h>
  40 #include "java_util_zip_Inflater.h"
  41 
  42 #define ThrowDataFormatException(env, msg) \
  43         JNU_ThrowByName(env, "java/util/zip/DataFormatException", msg)
  44 
  45 static jfieldID inputConsumedID;
  46 static jfieldID outputConsumedID;
  47 
  48 JNIEXPORT void JNICALL
  49 Java_java_util_zip_Inflater_initIDs(JNIEnv *env, jclass cls)
  50 {
  51     inputConsumedID = (*env)->GetFieldID(env, cls, "inputConsumed", "I");
  52     outputConsumedID = (*env)->GetFieldID(env, cls, "outputConsumed", "I");
  53     CHECK_NULL(inputConsumedID);
  54     CHECK_NULL(outputConsumedID);
  55 }
  56 
  57 JNIEXPORT jlong JNICALL
  58 Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap)
  59 {
  60     z_stream *strm = calloc(1, sizeof(z_stream));
  61 
  62     if (strm == NULL) {
  63         JNU_ThrowOutOfMemoryError(env, 0);
  64         return jlong_zero;
  65     } else {
  66         const char *msg;
  67         int ret = inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS);
  68         switch (ret) {
  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 != NULL) ? strm->msg :
  77                    (ret == Z_VERSION_ERROR) ?
  78                    "zlib returned Z_VERSION_ERROR: "
  79                    "compile time and runtime zlib implementations differ" :
  80                    (ret == Z_STREAM_ERROR) ?
  81                    "inflateInit2 returned Z_STREAM_ERROR" :
  82                    "unknown error initializing zlib library");
  83             free(strm);
  84             JNU_ThrowInternalError(env, msg);
  85             return jlong_zero;
  86         }
  87     }
  88 }
  89 
  90 static void doSetDictionary(JNIEnv *env, jlong addr, jbyte *buf, jint len)
  91 {
  92     int res = inflateSetDictionary(jlong_to_ptr(addr), (Bytef *) buf, len);
  93     switch (res) {
  94     case Z_OK:
  95         break;
  96     case Z_STREAM_ERROR:
  97     case Z_DATA_ERROR:
  98         JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg);
  99         break;
 100     default:
 101         JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
 102         break;
 103     }
 104 }
 105 
 106 JNIEXPORT void JNICALL
 107 Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
 108                                           jbyteArray b, jint off, jint len)
 109 {
 110     jbyte *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
 111     if (buf == NULL) /* out of memory */
 112         return;
 113     doSetDictionary(env, addr, buf + off, len);
 114     (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
 115 }
 116 
 117 JNIEXPORT void JNICALL
 118 Java_java_util_zip_Inflater_setDictionaryBuffer(JNIEnv *env, jclass cls, jlong addr,
 119                                           jlong bufferAddr, jint len)
 120 {
 121     jbyte *buf = jlong_to_ptr(bufferAddr);
 122     doSetDictionary(env, addr, buf, len);
 123 }
 124 
 125 static jlong doInflate(JNIEnv *env, jobject this, jlong addr,
 126                        jbyte *input, jint inputLen,
 127                        jbyte *output, jint outputLen)
 128 {
 129     z_stream *strm = jlong_to_ptr(addr);
 130     jint inputUsed = 0, outputUsed = 0;
 131     int finished = 0;
 132     int needDict = 0;
 133     int ret;
 134 
 135     strm->next_in  = (Bytef *) input;
 136     strm->next_out = (Bytef *) output;
 137     strm->avail_in  = inputLen;
 138     strm->avail_out = outputLen;
 139 
 140     ret = inflate(strm, Z_PARTIAL_FLUSH);
 141 
 142     switch (ret) {
 143     case Z_STREAM_END:
 144         finished = 1;
 145         /* fall through */
 146     case Z_OK:
 147         inputUsed = inputLen - strm->avail_in;
 148         outputUsed = outputLen - strm->avail_out;
 149         break;
 150     case Z_NEED_DICT:
 151         needDict = 1;
 152         /* Might have consumed some input here! */
 153         inputUsed = inputLen - strm->avail_in;
 154         /* zlib is unclear about whether output may be produced */
 155         outputUsed = outputLen - strm->avail_out;
 156         break;
 157     case Z_BUF_ERROR:
 158         break;
 159     case Z_DATA_ERROR:
 160         inputUsed = inputLen - strm->avail_in;
 161         (*env)->SetIntField(env, this, inputConsumedID, inputUsed);
 162         outputUsed = outputLen - strm->avail_out;
 163         (*env)->SetIntField(env, this, outputConsumedID, outputUsed);
 164         ThrowDataFormatException(env, strm->msg);
 165         break;
 166     case Z_MEM_ERROR:
 167         JNU_ThrowOutOfMemoryError(env, 0);
 168         break;
 169     default:
 170         JNU_ThrowInternalError(env, strm->msg);
 171         break;
 172     }
 173     return ((jlong)inputUsed) | (((jlong)outputUsed) << 31) | (((jlong)finished) << 62) | (((jlong)needDict) << 63);
 174 }
 175 
 176 JNIEXPORT jlong JNICALL
 177 Java_java_util_zip_Inflater_inflateBytesBytes(JNIEnv *env, jobject this, jlong addr,
 178                                          jbyteArray inputArray, jint inputOff, jint inputLen,
 179                                          jbyteArray outputArray, jint outputOff, jint outputLen)
 180 {
 181     jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0);
 182     jbyte *output;
 183     jlong retVal;
 184 
 185     if (input == NULL) {
 186         if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
 187             JNU_ThrowOutOfMemoryError(env, 0);
 188         return 0L;
 189     }
 190     output = (*env)->GetPrimitiveArrayCritical(env, outputArray, 0);
 191     if (output == NULL) {
 192         (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0);
 193         if (outputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
 194             JNU_ThrowOutOfMemoryError(env, 0);
 195         return 0L;
 196     }
 197 
 198     retVal = doInflate(env, this, addr,
 199             input + inputOff, inputLen,
 200             output + outputOff, outputLen);
 201 
 202     (*env)->ReleasePrimitiveArrayCritical(env, outputArray, output, 0);
 203     (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0);
 204 
 205     return retVal;
 206 }
 207 
 208 JNIEXPORT jlong JNICALL
 209 Java_java_util_zip_Inflater_inflateBytesBuffer(JNIEnv *env, jobject this, jlong addr,
 210                                          jbyteArray inputArray, jint inputOff, jint inputLen,
 211                                          jlong outputBuffer, jint outputLen)
 212 {
 213     jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0);
 214     jbyte *output;
 215     jlong retVal;
 216 
 217     if (input == NULL) {
 218         if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
 219             JNU_ThrowOutOfMemoryError(env, 0);
 220         return 0L;
 221     }
 222     output = jlong_to_ptr(outputBuffer);
 223 
 224     retVal = doInflate(env, this, addr,
 225             input + inputOff, inputLen,
 226             output, outputLen);
 227 
 228     (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0);
 229 
 230     return retVal;
 231 }
 232 
 233 JNIEXPORT jlong JNICALL
 234 Java_java_util_zip_Inflater_inflateBufferBytes(JNIEnv *env, jobject this, jlong addr,
 235                                          jlong inputBuffer, jint inputLen,
 236                                          jbyteArray outputArray, jint outputOff, jint outputLen)
 237 {
 238     jbyte *input = jlong_to_ptr(inputBuffer);
 239     jbyte *output = (*env)->GetPrimitiveArrayCritical(env, outputArray, 0);
 240     jlong retVal;
 241 
 242     if (output == NULL) {
 243         if (outputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
 244             JNU_ThrowOutOfMemoryError(env, 0);
 245         return 0L;
 246     }
 247 
 248     retVal = doInflate(env, this, addr,
 249             input, inputLen,
 250             output + outputOff, outputLen);
 251 
 252     (*env)->ReleasePrimitiveArrayCritical(env, outputArray, output, 0);
 253 
 254     return retVal;
 255 }
 256 
 257 JNIEXPORT jlong JNICALL
 258 Java_java_util_zip_Inflater_inflateBufferBuffer(JNIEnv *env, jobject this, jlong addr,
 259                                          jlong inputBuffer, jint inputLen,
 260                                          jlong outputBuffer, jint outputLen)
 261 {
 262     jbyte *input = jlong_to_ptr(inputBuffer);
 263     jbyte *output = jlong_to_ptr(outputBuffer);
 264 
 265     return doInflate(env, this, addr,
 266             input, inputLen,
 267             output, outputLen);
 268 }
 269 
 270 JNIEXPORT jint JNICALL
 271 Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
 272 {
 273     return ((z_stream *)jlong_to_ptr(addr))->adler;
 274 }
 275 
 276 JNIEXPORT void JNICALL
 277 Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
 278 {
 279     if (inflateReset(jlong_to_ptr(addr)) != Z_OK) {
 280         JNU_ThrowInternalError(env, 0);
 281     }
 282 }
 283 
 284 JNIEXPORT void JNICALL
 285 Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr)
 286 {
 287     if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
 288         JNU_ThrowInternalError(env, 0);
 289     } else {
 290         free(jlong_to_ptr(addr));
 291     }
 292 }