Inflater.c 6.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * 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
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29
 */

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

30
#include <stddef.h>
D
duke 已提交
31 32 33 34 35 36 37 38
#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"
39
#include <zlib.h>
D
duke 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
#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");
53
    CHECK_NULL(needDictID);
D
duke 已提交
54
    finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
55
    CHECK_NULL(finishedID);
D
duke 已提交
56
    bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
57
    CHECK_NULL(bufID);
D
duke 已提交
58
    offID = (*env)->GetFieldID(env, cls, "off", "I");
59
    CHECK_NULL(offID);
D
duke 已提交
60
    lenID = (*env)->GetFieldID(env, cls, "len", "I");
61
    CHECK_NULL(lenID);
D
duke 已提交
62 63 64 65 66 67 68
}

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

69
    if (strm == NULL) {
D
duke 已提交
70 71 72
        JNU_ThrowOutOfMemoryError(env, 0);
        return jlong_zero;
    } else {
73 74 75
        const char *msg;
        int ret = inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS);
        switch (ret) {
D
duke 已提交
76 77 78 79 80 81 82
          case Z_OK:
            return ptr_to_jlong(strm);
          case Z_MEM_ERROR:
            free(strm);
            JNU_ThrowOutOfMemoryError(env, 0);
            return jlong_zero;
          default:
83 84 85 86 87 88 89
            msg = ((strm->msg != NULL) ? strm->msg :
                   (ret == Z_VERSION_ERROR) ?
                   "zlib returned Z_VERSION_ERROR: "
                   "compile time and runtime zlib implementations differ" :
                   (ret == Z_STREAM_ERROR) ?
                   "inflateInit2 returned Z_STREAM_ERROR" :
                   "unknown error initializing zlib library");
D
duke 已提交
90 91 92 93 94 95 96 97
            free(strm);
            JNU_ThrowInternalError(env, msg);
            return jlong_zero;
        }
    }
}

JNIEXPORT void JNICALL
S
sherman 已提交
98
Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
D
duke 已提交
99 100 101 102 103 104
                                          jarray b, jint off, jint len)
{
    Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
    int res;
    if (buf == 0) /* out of memory */
        return;
S
sherman 已提交
105
    res = inflateSetDictionary(jlong_to_ptr(addr), buf + off, len);
D
duke 已提交
106 107 108 109 110 111
    (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
    switch (res) {
    case Z_OK:
        break;
    case Z_STREAM_ERROR:
    case Z_DATA_ERROR:
S
sherman 已提交
112
        JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg);
D
duke 已提交
113 114
        break;
    default:
S
sherman 已提交
115
        JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
D
duke 已提交
116 117 118 119 120
        break;
    }
}

JNIEXPORT jint JNICALL
S
sherman 已提交
121
Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,
D
duke 已提交
122 123
                                         jarray b, jint off, jint len)
{
S
sherman 已提交
124 125 126 127
    z_stream *strm = jlong_to_ptr(addr);
    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);
128

S
sherman 已提交
129 130 131 132
    jbyte *in_buf;
    jbyte *out_buf;
    int ret;

133 134
    in_buf  = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
    if (in_buf == NULL) {
135
        if (this_len != 0 && (*env)->ExceptionOccurred(env) == NULL)
136
            JNU_ThrowOutOfMemoryError(env, 0);
D
duke 已提交
137
        return 0;
S
sherman 已提交
138
    }
139 140 141
    out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
    if (out_buf == NULL) {
        (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
142
        if (len != 0 && (*env)->ExceptionOccurred(env) == NULL)
143
            JNU_ThrowOutOfMemoryError(env, 0);
S
sherman 已提交
144 145
        return 0;
    }
146 147 148
    strm->next_in  = (Bytef *) (in_buf + this_off);
    strm->next_out = (Bytef *) (out_buf + off);
    strm->avail_in  = this_len;
S
sherman 已提交
149 150
    strm->avail_out = len;
    ret = inflate(strm, Z_PARTIAL_FLUSH);
151 152
    (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
    (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
D
duke 已提交
153

S
sherman 已提交
154 155 156 157 158
    switch (ret) {
    case Z_STREAM_END:
        (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
        /* fall through */
    case Z_OK:
159 160 161
        this_off += this_len - strm->avail_in;
        (*env)->SetIntField(env, this, offID, this_off);
        (*env)->SetIntField(env, this, lenID, strm->avail_in);
162
        return (jint) (len - strm->avail_out);
S
sherman 已提交
163 164 165
    case Z_NEED_DICT:
        (*env)->SetBooleanField(env, this, needDictID, JNI_TRUE);
        /* Might have consumed some input here! */
166 167 168
        this_off += this_len - strm->avail_in;
        (*env)->SetIntField(env, this, offID, this_off);
        (*env)->SetIntField(env, this, lenID, strm->avail_in);
S
sherman 已提交
169 170 171 172 173 174 175 176 177 178 179 180
        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 已提交
181 182 183 184
    }
}

JNIEXPORT jint JNICALL
S
sherman 已提交
185
Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
D
duke 已提交
186
{
S
sherman 已提交
187
    return ((z_stream *)jlong_to_ptr(addr))->adler;
D
duke 已提交
188 189 190
}

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

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