Inflater.c 6.4 KB
Newer Older
D
duke 已提交
1
/*
S
sherman 已提交
2
 * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * Native method support for java.util.zip.Inflater
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "jlong.h"
#include "jni.h"
#include "jvm.h"
#include "jni_util.h"
#include "zlib.h"
#include "java_util_zip_Inflater.h"

#define ThrowDataFormatException(env, msg) \
        JNU_ThrowByName(env, "java/util/zip/DataFormatException", msg)

static jfieldID needDictID;
static jfieldID finishedID;
static jfieldID bufID, offID, lenID;

JNIEXPORT void JNICALL
Java_java_util_zip_Inflater_initIDs(JNIEnv *env, jclass cls)
{
    needDictID = (*env)->GetFieldID(env, cls, "needDict", "Z");
    finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
    bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
    offID = (*env)->GetFieldID(env, cls, "off", "I");
    lenID = (*env)->GetFieldID(env, cls, "len", "I");
}

JNIEXPORT jlong JNICALL
Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap)
{
    z_stream *strm = calloc(1, sizeof(z_stream));

    if (strm == 0) {
        JNU_ThrowOutOfMemoryError(env, 0);
        return jlong_zero;
    } else {
        char *msg;
        switch (inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS)) {
          case Z_OK:
            return ptr_to_jlong(strm);
          case Z_MEM_ERROR:
            free(strm);
            JNU_ThrowOutOfMemoryError(env, 0);
            return jlong_zero;
          default:
            msg = strm->msg;
            free(strm);
            JNU_ThrowInternalError(env, msg);
            return jlong_zero;
        }
    }
}

JNIEXPORT void JNICALL
S
sherman 已提交
85
Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
D
duke 已提交
86 87 88 89 90 91
                                          jarray b, jint off, jint len)
{
    Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
    int res;
    if (buf == 0) /* out of memory */
        return;
S
sherman 已提交
92
    res = inflateSetDictionary(jlong_to_ptr(addr), buf + off, len);
D
duke 已提交
93 94 95 96 97 98
    (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
    switch (res) {
    case Z_OK:
        break;
    case Z_STREAM_ERROR:
    case Z_DATA_ERROR:
S
sherman 已提交
99
        JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg);
D
duke 已提交
100 101
        break;
    default:
S
sherman 已提交
102
        JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
D
duke 已提交
103 104 105 106 107
        break;
    }
}

JNIEXPORT jint JNICALL
S
sherman 已提交
108
Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,
D
duke 已提交
109 110
                                         jarray b, jint off, jint len)
{
S
sherman 已提交
111
    z_stream *strm = jlong_to_ptr(addr);
D
duke 已提交
112

S
sherman 已提交
113 114 115 116 117 118 119 120 121 122
    jarray this_buf = (jarray)(*env)->GetObjectField(env, this, bufID);
    jint this_off = (*env)->GetIntField(env, this, offID);
    jint this_len = (*env)->GetIntField(env, this, lenID);
    jbyte *in_buf;
    jbyte *out_buf;
    int ret;

    in_buf = (jbyte *) malloc(this_len);
    if (in_buf == 0) {
        JNU_ThrowOutOfMemoryError(env, 0);
D
duke 已提交
123
        return 0;
S
sherman 已提交
124 125
    }
    (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
D
duke 已提交
126

S
sherman 已提交
127 128 129 130 131 132
    out_buf = (jbyte *) malloc(len);
    if (out_buf == 0) {
        free(in_buf);
        JNU_ThrowOutOfMemoryError(env, 0);
        return 0;
    }
D
duke 已提交
133

S
sherman 已提交
134 135 136 137 138
    strm->next_in  = (Bytef *) in_buf;
    strm->next_out = (Bytef *) out_buf;
    strm->avail_in  = this_len;
    strm->avail_out = len;
    ret = inflate(strm, Z_PARTIAL_FLUSH);
D
duke 已提交
139

S
sherman 已提交
140 141 142 143 144
    if (ret == Z_STREAM_END || ret == Z_OK) {
        (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
    }
    free(out_buf);
    free(in_buf);
D
duke 已提交
145

S
sherman 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    switch (ret) {
    case Z_STREAM_END:
        (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
        /* fall through */
    case Z_OK:
        this_off += this_len - strm->avail_in;
        (*env)->SetIntField(env, this, offID, this_off);
        (*env)->SetIntField(env, this, lenID, strm->avail_in);
        return len - strm->avail_out;
    case Z_NEED_DICT:
        (*env)->SetBooleanField(env, this, needDictID, JNI_TRUE);
        /* Might have consumed some input here! */
        this_off += this_len - strm->avail_in;
        (*env)->SetIntField(env, this, offID, this_off);
        (*env)->SetIntField(env, this, lenID, strm->avail_in);
        return 0;
    case Z_BUF_ERROR:
        return 0;
    case Z_DATA_ERROR:
        ThrowDataFormatException(env, strm->msg);
        return 0;
    case Z_MEM_ERROR:
        JNU_ThrowOutOfMemoryError(env, 0);
        return 0;
    default:
        JNU_ThrowInternalError(env, strm->msg);
        return 0;
D
duke 已提交
173 174 175 176
    }
}

JNIEXPORT jint JNICALL
S
sherman 已提交
177
Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
D
duke 已提交
178
{
S
sherman 已提交
179
    return ((z_stream *)jlong_to_ptr(addr))->adler;
D
duke 已提交
180 181 182
}

JNIEXPORT jlong JNICALL
S
sherman 已提交
183
Java_java_util_zip_Inflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
D
duke 已提交
184
{
S
sherman 已提交
185
    return ((z_stream *)jlong_to_ptr(addr))->total_in;
D
duke 已提交
186 187 188
}

JNIEXPORT jlong JNICALL
S
sherman 已提交
189
Java_java_util_zip_Inflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
D
duke 已提交
190
{
S
sherman 已提交
191
    return ((z_stream *)jlong_to_ptr(addr))->total_out;
D
duke 已提交
192 193 194
}

JNIEXPORT void JNICALL
S
sherman 已提交
195
Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
D
duke 已提交
196
{
S
sherman 已提交
197
    if (inflateReset(jlong_to_ptr(addr)) != Z_OK) {
D
duke 已提交
198 199 200 201 202
        JNU_ThrowInternalError(env, 0);
    }
}

JNIEXPORT void JNICALL
S
sherman 已提交
203
Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr)
D
duke 已提交
204
{
S
sherman 已提交
205
    if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
D
duke 已提交
206 207
        JNU_ThrowInternalError(env, 0);
    } else {
S
sherman 已提交
208
        free(jlong_to_ptr(addr));
D
duke 已提交
209 210
    }
}