Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/util/zip/Inflater.c
38830 views
/*1* Copyright (c) 1997, 2015, 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.Inflater27*/2829#include <stddef.h>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"4041#define ThrowDataFormatException(env, msg) \42JNU_ThrowByName(env, "java/util/zip/DataFormatException", msg)4344static jfieldID needDictID;45static jfieldID finishedID;46static jfieldID bufID, offID, lenID;4748JNIEXPORT void JNICALL49Java_java_util_zip_Inflater_initIDs(JNIEnv *env, jclass cls)50{51needDictID = (*env)->GetFieldID(env, cls, "needDict", "Z");52CHECK_NULL(needDictID);53finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");54CHECK_NULL(finishedID);55bufID = (*env)->GetFieldID(env, cls, "buf", "[B");56CHECK_NULL(bufID);57offID = (*env)->GetFieldID(env, cls, "off", "I");58CHECK_NULL(offID);59lenID = (*env)->GetFieldID(env, cls, "len", "I");60CHECK_NULL(lenID);61}6263JNIEXPORT jlong JNICALL64Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap)65{66z_stream *strm = calloc(1, sizeof(z_stream));6768if (strm == NULL) {69JNU_ThrowOutOfMemoryError(env, 0);70return jlong_zero;71} else {72const char *msg;73int ret = inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS);74switch (ret) {75case Z_OK:76return ptr_to_jlong(strm);77case Z_MEM_ERROR:78free(strm);79JNU_ThrowOutOfMemoryError(env, 0);80return jlong_zero;81default:82msg = ((strm->msg != NULL) ? strm->msg :83(ret == Z_VERSION_ERROR) ?84"zlib returned Z_VERSION_ERROR: "85"compile time and runtime zlib implementations differ" :86(ret == Z_STREAM_ERROR) ?87"inflateInit2 returned Z_STREAM_ERROR" :88"unknown error initializing zlib library");89free(strm);90JNU_ThrowInternalError(env, msg);91return jlong_zero;92}93}94}9596JNIEXPORT void JNICALL97Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,98jarray b, jint off, jint len)99{100Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);101int res;102if (buf == 0) /* out of memory */103return;104res = inflateSetDictionary(jlong_to_ptr(addr), buf + off, len);105(*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);106switch (res) {107case Z_OK:108break;109case Z_STREAM_ERROR:110case Z_DATA_ERROR:111JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg);112break;113default:114JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);115break;116}117}118119JNIEXPORT jint JNICALL120Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,121jarray b, jint off, jint len)122{123z_stream *strm = jlong_to_ptr(addr);124jarray this_buf = (jarray)(*env)->GetObjectField(env, this, bufID);125jint this_off = (*env)->GetIntField(env, this, offID);126jint this_len = (*env)->GetIntField(env, this, lenID);127128jbyte *in_buf;129jbyte *out_buf;130int ret;131132in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);133if (in_buf == NULL) {134if (this_len != 0 && (*env)->ExceptionOccurred(env) == NULL)135JNU_ThrowOutOfMemoryError(env, 0);136return 0;137}138out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);139if (out_buf == NULL) {140(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);141if (len != 0 && (*env)->ExceptionOccurred(env) == NULL)142JNU_ThrowOutOfMemoryError(env, 0);143return 0;144}145strm->next_in = (Bytef *) (in_buf + this_off);146strm->next_out = (Bytef *) (out_buf + off);147strm->avail_in = this_len;148strm->avail_out = len;149ret = inflate(strm, Z_PARTIAL_FLUSH);150(*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);151(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);152153switch (ret) {154case Z_STREAM_END:155(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);156/* fall through */157case Z_OK:158this_off += this_len - strm->avail_in;159(*env)->SetIntField(env, this, offID, this_off);160(*env)->SetIntField(env, this, lenID, strm->avail_in);161return (jint) (len - strm->avail_out);162case Z_NEED_DICT:163(*env)->SetBooleanField(env, this, needDictID, JNI_TRUE);164/* Might have consumed some input here! */165this_off += this_len - strm->avail_in;166(*env)->SetIntField(env, this, offID, this_off);167(*env)->SetIntField(env, this, lenID, strm->avail_in);168return 0;169case Z_BUF_ERROR:170return 0;171case Z_DATA_ERROR:172ThrowDataFormatException(env, strm->msg);173return 0;174case Z_MEM_ERROR:175JNU_ThrowOutOfMemoryError(env, 0);176return 0;177default:178JNU_ThrowInternalError(env, strm->msg);179return 0;180}181}182183JNIEXPORT jint JNICALL184Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)185{186return ((z_stream *)jlong_to_ptr(addr))->adler;187}188189JNIEXPORT void JNICALL190Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)191{192if (inflateReset(jlong_to_ptr(addr)) != Z_OK) {193JNU_ThrowInternalError(env, 0);194}195}196197JNIEXPORT void JNICALL198Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr)199{200if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {201JNU_ThrowInternalError(env, 0);202} else {203free(jlong_to_ptr(addr));204}205}206207208