Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/util/zip/Deflater.c
38830 views
/*1* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Native method support for java.util.zip.Deflater27*/2829#include <stdio.h>30#include <stdlib.h>31#include "jlong.h"32#include "jni.h"33#include "jni_util.h"34#include <zlib.h>3536#include "java_util_zip_Deflater.h"3738#define DEF_MEM_LEVEL 83940static jfieldID levelID;41static jfieldID strategyID;42static jfieldID setParamsID;43static jfieldID finishID;44static jfieldID finishedID;45static jfieldID bufID, offID, lenID;4647JNIEXPORT void JNICALL48Java_java_util_zip_Deflater_initIDs(JNIEnv *env, jclass cls)49{50levelID = (*env)->GetFieldID(env, cls, "level", "I");51CHECK_NULL(levelID);52strategyID = (*env)->GetFieldID(env, cls, "strategy", "I");53CHECK_NULL(strategyID);54setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z");55CHECK_NULL(setParamsID);56finishID = (*env)->GetFieldID(env, cls, "finish", "Z");57CHECK_NULL(finishID);58finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");59CHECK_NULL(finishedID);60bufID = (*env)->GetFieldID(env, cls, "buf", "[B");61CHECK_NULL(bufID);62offID = (*env)->GetFieldID(env, cls, "off", "I");63CHECK_NULL(offID);64lenID = (*env)->GetFieldID(env, cls, "len", "I");65CHECK_NULL(lenID);66}6768JNIEXPORT jlong JNICALL69Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level,70jint strategy, jboolean nowrap)71{72z_stream *strm = calloc(1, sizeof(z_stream));7374if (strm == 0) {75JNU_ThrowOutOfMemoryError(env, 0);76return jlong_zero;77} else {78const char *msg;79int ret = deflateInit2(strm, level, Z_DEFLATED,80nowrap ? -MAX_WBITS : MAX_WBITS,81DEF_MEM_LEVEL, strategy);82switch (ret) {83case Z_OK:84return ptr_to_jlong(strm);85case Z_MEM_ERROR:86free(strm);87JNU_ThrowOutOfMemoryError(env, 0);88return jlong_zero;89case Z_STREAM_ERROR:90free(strm);91JNU_ThrowIllegalArgumentException(env, 0);92return jlong_zero;93default:94msg = ((strm->msg != NULL) ? strm->msg :95(ret == Z_VERSION_ERROR) ?96"zlib returned Z_VERSION_ERROR: "97"compile time and runtime zlib implementations differ" :98"unknown error initializing zlib library");99free(strm);100JNU_ThrowInternalError(env, msg);101return jlong_zero;102}103}104}105106JNIEXPORT void JNICALL107Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,108jarray b, jint off, jint len)109{110Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);111int res;112if (buf == 0) {/* out of memory */113return;114}115res = deflateSetDictionary((z_stream *)jlong_to_ptr(addr), buf + off, len);116(*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);117switch (res) {118case Z_OK:119break;120case Z_STREAM_ERROR:121JNU_ThrowIllegalArgumentException(env, 0);122break;123default:124JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);125break;126}127}128129JNIEXPORT jint JNICALL130Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr,131jarray b, jint off, jint len, jint flush)132{133z_stream *strm = jlong_to_ptr(addr);134135jarray this_buf = (*env)->GetObjectField(env, this, bufID);136jint this_off = (*env)->GetIntField(env, this, offID);137jint this_len = (*env)->GetIntField(env, this, lenID);138jbyte *in_buf;139jbyte *out_buf;140int res;141if ((*env)->GetBooleanField(env, this, setParamsID)) {142int level = (*env)->GetIntField(env, this, levelID);143int strategy = (*env)->GetIntField(env, this, strategyID);144in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);145if (in_buf == NULL) {146// Throw OOME only when length is not zero147if (this_len != 0 && (*env)->ExceptionOccurred(env) == NULL)148JNU_ThrowOutOfMemoryError(env, 0);149return 0;150}151out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);152if (out_buf == NULL) {153(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);154if (len != 0 && (*env)->ExceptionOccurred(env) == NULL)155JNU_ThrowOutOfMemoryError(env, 0);156return 0;157}158159strm->next_in = (Bytef *) (in_buf + this_off);160strm->next_out = (Bytef *) (out_buf + off);161strm->avail_in = this_len;162strm->avail_out = len;163res = deflateParams(strm, level, strategy);164(*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);165(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);166switch (res) {167case Z_OK:168(*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);169case Z_BUF_ERROR:170this_off += this_len - strm->avail_in;171(*env)->SetIntField(env, this, offID, this_off);172(*env)->SetIntField(env, this, lenID, strm->avail_in);173return (jint) (len - strm->avail_out);174default:175JNU_ThrowInternalError(env, strm->msg);176return 0;177}178} else {179jboolean finish = (*env)->GetBooleanField(env, this, finishID);180in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);181if (in_buf == NULL) {182if (this_len != 0)183JNU_ThrowOutOfMemoryError(env, 0);184return 0;185}186out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);187if (out_buf == NULL) {188(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);189if (len != 0)190JNU_ThrowOutOfMemoryError(env, 0);191192return 0;193}194195strm->next_in = (Bytef *) (in_buf + this_off);196strm->next_out = (Bytef *) (out_buf + off);197strm->avail_in = this_len;198strm->avail_out = len;199res = deflate(strm, finish ? Z_FINISH : flush);200(*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);201(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);202switch (res) {203case Z_STREAM_END:204(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);205/* fall through */206case Z_OK:207case Z_BUF_ERROR:208this_off += this_len - strm->avail_in;209(*env)->SetIntField(env, this, offID, this_off);210(*env)->SetIntField(env, this, lenID, strm->avail_in);211return len - strm->avail_out;212default:213JNU_ThrowInternalError(env, strm->msg);214return 0;215}216}217}218219JNIEXPORT jint JNICALL220Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)221{222return ((z_stream *)jlong_to_ptr(addr))->adler;223}224225JNIEXPORT void JNICALL226Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)227{228if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {229JNU_ThrowInternalError(env, 0);230}231}232233JNIEXPORT void JNICALL234Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr)235{236if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {237JNU_ThrowInternalError(env, 0);238} else {239free((z_stream *)jlong_to_ptr(addr));240}241}242243244