1 /*
   2  * Copyright (c) 1997, 2009, 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.Deflater
  28  */
  29 
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include "jlong.h"
  33 #include "jni.h"
  34 #include "jni_util.h"
  35 #include "zlib.h"
  36 
  37 #include "java_util_zip_Deflater.h"
  38 
  39 #define DEF_MEM_LEVEL 8
  40 
  41 static jfieldID levelID;
  42 static jfieldID strategyID;
  43 static jfieldID setParamsID;
  44 static jfieldID finishID;
  45 static jfieldID finishedID;
  46 static jfieldID bufID, offID, lenID;
  47 
  48 JNIEXPORT void JNICALL
  49 Java_java_util_zip_Deflater_initIDs(JNIEnv *env, jclass cls)
  50 {
  51     levelID = (*env)->GetFieldID(env, cls, "level", "I");
  52     strategyID = (*env)->GetFieldID(env, cls, "strategy", "I");
  53     setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z");
  54     finishID = (*env)->GetFieldID(env, cls, "finish", "Z");
  55     finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
  56     bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
  57     offID = (*env)->GetFieldID(env, cls, "off", "I");
  58     lenID = (*env)->GetFieldID(env, cls, "len", "I");
  59 }
  60 
  61 JNIEXPORT jlong JNICALL
  62 Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level,
  63                                  jint strategy, jboolean nowrap)
  64 {
  65     z_stream *strm = calloc(1, sizeof(z_stream));
  66 
  67     if (strm == 0) {
  68         JNU_ThrowOutOfMemoryError(env, 0);
  69         return jlong_zero;
  70     } else {
  71         char *msg;
  72         switch (deflateInit2(strm, level, Z_DEFLATED,
  73                              nowrap ? -MAX_WBITS : MAX_WBITS,
  74                              DEF_MEM_LEVEL, strategy)) {
  75           case Z_OK:
  76             return ptr_to_jlong(strm);
  77           case Z_MEM_ERROR:
  78             free(strm);
  79             JNU_ThrowOutOfMemoryError(env, 0);
  80             return jlong_zero;
  81           case Z_STREAM_ERROR:
  82             free(strm);
  83             JNU_ThrowIllegalArgumentException(env, 0);
  84             return jlong_zero;
  85           default:
  86             msg = strm->msg;
  87             free(strm);
  88             JNU_ThrowInternalError(env, msg);
  89             return jlong_zero;
  90         }
  91     }
  92 }
  93 
  94 JNIEXPORT void JNICALL
  95 Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
  96                                           jarray b, jint off, jint len)
  97 {
  98     Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
  99     int res;
 100     if (buf == 0) {/* out of memory */
 101         return;
 102     }
 103     res = deflateSetDictionary((z_stream *)jlong_to_ptr(addr), buf + off, len);
 104     (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
 105     switch (res) {
 106     case Z_OK:
 107         break;
 108     case Z_STREAM_ERROR:
 109         JNU_ThrowIllegalArgumentException(env, 0);
 110         break;
 111     default:
 112         JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
 113         break;
 114     }
 115 }
 116 
 117 JNIEXPORT jint JNICALL
 118 Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr,
 119                                          jarray b, jint off, jint len, jint flush)
 120 {
 121     z_stream *strm = jlong_to_ptr(addr);
 122 
 123     jarray this_buf = (*env)->GetObjectField(env, this, bufID);
 124     jint this_off = (*env)->GetIntField(env, this, offID);
 125     jint this_len = (*env)->GetIntField(env, this, lenID);
 126     jbyte *in_buf;
 127     jbyte *out_buf;
 128     int res;
 129     if ((*env)->GetBooleanField(env, this, setParamsID)) {
 130         int level = (*env)->GetIntField(env, this, levelID);
 131         int strategy = (*env)->GetIntField(env, this, strategyID);
 132 
 133         in_buf = (jbyte *) malloc(this_len);
 134         if (in_buf == 0) {
 135             // Throw OOME only when length is not zero
 136             if (this_len != 0)
 137                 JNU_ThrowOutOfMemoryError(env, 0);
 138             return 0;
 139         }
 140         (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
 141         out_buf = (jbyte *) malloc(len);
 142         if (out_buf == 0) {
 143             free(in_buf);
 144             if (len != 0)
 145                 JNU_ThrowOutOfMemoryError(env, 0);
 146             return 0;
 147         }
 148 
 149         strm->next_in = (Bytef *) in_buf;
 150         strm->next_out = (Bytef *) out_buf;
 151         strm->avail_in = this_len;
 152         strm->avail_out = len;
 153         res = deflateParams(strm, level, strategy);
 154 
 155         if (res == Z_OK) {
 156             (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
 157         }
 158         free(out_buf);
 159         free(in_buf);
 160 
 161         switch (res) {
 162         case Z_OK:
 163             (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
 164             this_off += this_len - strm->avail_in;
 165             (*env)->SetIntField(env, this, offID, this_off);
 166             (*env)->SetIntField(env, this, lenID, strm->avail_in);
 167             return len - strm->avail_out;
 168         case Z_BUF_ERROR:
 169             (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
 170             return 0;
 171         default:
 172             JNU_ThrowInternalError(env, strm->msg);
 173             return 0;
 174         }
 175     } else {
 176         jboolean finish = (*env)->GetBooleanField(env, this, finishID);
 177         in_buf = (jbyte *) malloc(this_len);
 178         if (in_buf == 0) {
 179             if (this_len != 0)
 180                 JNU_ThrowOutOfMemoryError(env, 0);
 181             return 0;
 182         }
 183         (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
 184 
 185         out_buf = (jbyte *) malloc(len);
 186         if (out_buf == 0) {
 187             free(in_buf);
 188             if (len != 0)
 189                 JNU_ThrowOutOfMemoryError(env, 0);
 190             return 0;
 191         }
 192 
 193         strm->next_in = (Bytef *) in_buf;
 194         strm->next_out = (Bytef *) out_buf;
 195         strm->avail_in = this_len;
 196         strm->avail_out = len;
 197         res = deflate(strm, finish ? Z_FINISH : flush);
 198 
 199         if (res == Z_STREAM_END || res == Z_OK) {
 200             (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
 201         }
 202         free(out_buf);
 203         free(in_buf);
 204 
 205         switch (res) {
 206         case Z_STREAM_END:
 207             (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
 208             /* fall through */
 209         case Z_OK:
 210             this_off += this_len - strm->avail_in;
 211             (*env)->SetIntField(env, this, offID, this_off);
 212             (*env)->SetIntField(env, this, lenID, strm->avail_in);
 213             return len - strm->avail_out;
 214         case Z_BUF_ERROR:
 215             return 0;
 216             default:
 217             JNU_ThrowInternalError(env, strm->msg);
 218             return 0;
 219         }
 220     }
 221 }
 222 
 223 JNIEXPORT jint JNICALL
 224 Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
 225 {
 226     return ((z_stream *)jlong_to_ptr(addr))->adler;
 227 }
 228 
 229 JNIEXPORT jlong JNICALL
 230 Java_java_util_zip_Deflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
 231 {
 232     return ((z_stream *)jlong_to_ptr(addr))->total_in;
 233 }
 234 
 235 JNIEXPORT jlong JNICALL
 236 Java_java_util_zip_Deflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
 237 {
 238     return ((z_stream *)jlong_to_ptr(addr))->total_out;
 239 }
 240 
 241 JNIEXPORT void JNICALL
 242 Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
 243 {
 244     if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
 245         JNU_ThrowInternalError(env, 0);
 246     }
 247 }
 248 
 249 JNIEXPORT void JNICALL
 250 Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr)
 251 {
 252     if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
 253         JNU_ThrowInternalError(env, 0);
 254     } else {
 255         free((z_stream *)jlong_to_ptr(addr));
 256     }
 257 }