0N/A/*
3261N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * Native method support for java.util.zip.Deflater
0N/A */
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include "jlong.h"
0N/A#include "jni.h"
0N/A#include "jni_util.h"
5353N/A#include <zlib.h>
0N/A
0N/A#include "java_util_zip_Deflater.h"
0N/A
0N/A#define DEF_MEM_LEVEL 8
0N/A
0N/Astatic jfieldID levelID;
0N/Astatic jfieldID strategyID;
0N/Astatic jfieldID setParamsID;
0N/Astatic jfieldID finishID;
0N/Astatic jfieldID finishedID;
0N/Astatic jfieldID bufID, offID, lenID;
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_util_zip_Deflater_initIDs(JNIEnv *env, jclass cls)
0N/A{
0N/A levelID = (*env)->GetFieldID(env, cls, "level", "I");
0N/A strategyID = (*env)->GetFieldID(env, cls, "strategy", "I");
0N/A setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z");
0N/A finishID = (*env)->GetFieldID(env, cls, "finish", "Z");
0N/A finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
0N/A bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
0N/A offID = (*env)->GetFieldID(env, cls, "off", "I");
0N/A lenID = (*env)->GetFieldID(env, cls, "len", "I");
0N/A}
0N/A
0N/AJNIEXPORT jlong JNICALL
0N/AJava_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level,
0N/A jint strategy, jboolean nowrap)
0N/A{
0N/A z_stream *strm = calloc(1, sizeof(z_stream));
0N/A
0N/A if (strm == 0) {
0N/A JNU_ThrowOutOfMemoryError(env, 0);
0N/A return jlong_zero;
0N/A } else {
0N/A char *msg;
0N/A switch (deflateInit2(strm, level, Z_DEFLATED,
0N/A nowrap ? -MAX_WBITS : MAX_WBITS,
0N/A DEF_MEM_LEVEL, strategy)) {
0N/A case Z_OK:
0N/A return ptr_to_jlong(strm);
0N/A case Z_MEM_ERROR:
0N/A free(strm);
0N/A JNU_ThrowOutOfMemoryError(env, 0);
0N/A return jlong_zero;
0N/A case Z_STREAM_ERROR:
0N/A free(strm);
0N/A JNU_ThrowIllegalArgumentException(env, 0);
0N/A return jlong_zero;
0N/A default:
0N/A msg = strm->msg;
0N/A free(strm);
0N/A JNU_ThrowInternalError(env, msg);
0N/A return jlong_zero;
0N/A }
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
2252N/AJava_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
0N/A jarray b, jint off, jint len)
0N/A{
0N/A Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
0N/A int res;
0N/A if (buf == 0) {/* out of memory */
0N/A return;
0N/A }
2252N/A res = deflateSetDictionary((z_stream *)jlong_to_ptr(addr), buf + off, len);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
0N/A switch (res) {
0N/A case Z_OK:
0N/A break;
0N/A case Z_STREAM_ERROR:
0N/A JNU_ThrowIllegalArgumentException(env, 0);
0N/A break;
0N/A default:
2252N/A JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
0N/A break;
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
2252N/AJava_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr,
1796N/A jarray b, jint off, jint len, jint flush)
0N/A{
2252N/A z_stream *strm = jlong_to_ptr(addr);
0N/A
2252N/A jarray this_buf = (*env)->GetObjectField(env, this, bufID);
2252N/A jint this_off = (*env)->GetIntField(env, this, offID);
2252N/A jint this_len = (*env)->GetIntField(env, this, lenID);
2252N/A jbyte *in_buf;
2252N/A jbyte *out_buf;
2252N/A int res;
2252N/A if ((*env)->GetBooleanField(env, this, setParamsID)) {
2252N/A int level = (*env)->GetIntField(env, this, levelID);
2252N/A int strategy = (*env)->GetIntField(env, this, strategyID);
3897N/A in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
3897N/A if (in_buf == NULL) {
3128N/A // Throw OOME only when length is not zero
3128N/A if (this_len != 0)
3128N/A JNU_ThrowOutOfMemoryError(env, 0);
2252N/A return 0;
2252N/A }
3897N/A out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
3897N/A if (out_buf == NULL) {
3897N/A (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
3128N/A if (len != 0)
3128N/A JNU_ThrowOutOfMemoryError(env, 0);
2252N/A return 0;
2252N/A }
0N/A
3897N/A strm->next_in = (Bytef *) (in_buf + this_off);
3897N/A strm->next_out = (Bytef *) (out_buf + off);
2252N/A strm->avail_in = this_len;
2252N/A strm->avail_out = len;
2252N/A res = deflateParams(strm, level, strategy);
3897N/A (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
3897N/A (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
0N/A
2252N/A switch (res) {
2252N/A case Z_OK:
2252N/A (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
2252N/A this_off += this_len - strm->avail_in;
2252N/A (*env)->SetIntField(env, this, offID, this_off);
2252N/A (*env)->SetIntField(env, this, lenID, strm->avail_in);
2252N/A return len - strm->avail_out;
2252N/A case Z_BUF_ERROR:
2252N/A (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
2252N/A return 0;
2252N/A default:
2252N/A JNU_ThrowInternalError(env, strm->msg);
2252N/A return 0;
2252N/A }
2252N/A } else {
2252N/A jboolean finish = (*env)->GetBooleanField(env, this, finishID);
3897N/A in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
3897N/A if (in_buf == NULL) {
3128N/A if (this_len != 0)
3128N/A JNU_ThrowOutOfMemoryError(env, 0);
2252N/A return 0;
2252N/A }
3897N/A out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
3897N/A if (out_buf == NULL) {
3897N/A (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
3128N/A if (len != 0)
3128N/A JNU_ThrowOutOfMemoryError(env, 0);
3897N/A
2252N/A return 0;
2252N/A }
0N/A
3897N/A strm->next_in = (Bytef *) (in_buf + this_off);
3897N/A strm->next_out = (Bytef *) (out_buf + off);
2252N/A strm->avail_in = this_len;
2252N/A strm->avail_out = len;
2252N/A res = deflate(strm, finish ? Z_FINISH : flush);
3897N/A (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
3897N/A (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
0N/A
2252N/A switch (res) {
2252N/A case Z_STREAM_END:
2252N/A (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
2252N/A /* fall through */
2252N/A case Z_OK:
2252N/A this_off += this_len - strm->avail_in;
2252N/A (*env)->SetIntField(env, this, offID, this_off);
2252N/A (*env)->SetIntField(env, this, lenID, strm->avail_in);
2252N/A return len - strm->avail_out;
2252N/A case Z_BUF_ERROR:
2252N/A return 0;
0N/A default:
2252N/A JNU_ThrowInternalError(env, strm->msg);
2252N/A return 0;
0N/A }
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
2252N/AJava_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
0N/A{
2252N/A return ((z_stream *)jlong_to_ptr(addr))->adler;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
2252N/AJava_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
0N/A{
2252N/A if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
0N/A JNU_ThrowInternalError(env, 0);
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
2252N/AJava_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr)
0N/A{
2252N/A if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
0N/A JNU_ThrowInternalError(env, 0);
0N/A } else {
2252N/A free((z_stream *)jlong_to_ptr(addr));
0N/A }
0N/A}