src/java.base/share/native/libzip/Deflater.c
author sherman
Fri, 01 Dec 2017 22:04:03 -0800
changeset 48045 d66e420cc482
parent 47216 71c04702a3d5
child 49834 99644c75eaed
permissions -rw-r--r--
8191918: tomcat gzip-compressed response bodies appear to be broken in update 151 Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48045
d66e420cc482 8191918: tomcat gzip-compressed response bodies appear to be broken in update 151
sherman
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5173
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5173
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5173
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5173
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5173
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Native method support for java.util.zip.Deflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "jni_util.h"
13567
b124faa29aef 7110151: Use underlying platform's zlib library for Java zlib support
andrew
parents: 13409
diff changeset
    35
#include <zlib.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "java_util_zip_Deflater.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#define DEF_MEM_LEVEL 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
static jfieldID levelID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static jfieldID strategyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static jfieldID setParamsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static jfieldID finishID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static jfieldID finishedID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static jfieldID bufID, offID, lenID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
Java_java_util_zip_Deflater_initIDs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    levelID = (*env)->GetFieldID(env, cls, "level", "I");
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
    52
    CHECK_NULL(levelID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    strategyID = (*env)->GetFieldID(env, cls, "strategy", "I");
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
    54
    CHECK_NULL(strategyID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z");
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
    56
    CHECK_NULL(setParamsID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    finishID = (*env)->GetFieldID(env, cls, "finish", "Z");
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
    58
    CHECK_NULL(finishID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
    60
    CHECK_NULL(finishedID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
    62
    CHECK_NULL(bufID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    offID = (*env)->GetFieldID(env, cls, "off", "I");
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
    64
    CHECK_NULL(offID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    lenID = (*env)->GetFieldID(env, cls, "len", "I");
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
    66
    CHECK_NULL(lenID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                 jint strategy, jboolean nowrap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    z_stream *strm = calloc(1, sizeof(z_stream));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    if (strm == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    } else {
28258
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    79
        const char *msg;
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    80
        int ret = deflateInit2(strm, level, Z_DEFLATED,
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    81
                               nowrap ? -MAX_WBITS : MAX_WBITS,
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    82
                               DEF_MEM_LEVEL, strategy);
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    83
        switch (ret) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
          case Z_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return ptr_to_jlong(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
          case Z_MEM_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            free(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
          case Z_STREAM_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            free(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            JNU_ThrowIllegalArgumentException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
          default:
28258
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    95
            msg = ((strm->msg != NULL) ? strm->msg :
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    96
                   (ret == Z_VERSION_ERROR) ?
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    97
                   "zlib returned Z_VERSION_ERROR: "
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    98
                   "compile time and runtime zlib implementations differ" :
381e960842b6 8068338: Better message about incompatible zlib in Deflater.init
igerasim
parents: 25859
diff changeset
    99
                   "unknown error initializing zlib library");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            free(strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            JNU_ThrowInternalError(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
JNIEXPORT void JNICALL
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   108
Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                          jarray b, jint off, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    int res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if (buf == 0) {/* out of memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   116
    res = deflateSetDictionary((z_stream *)jlong_to_ptr(addr), buf + off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    switch (res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    case Z_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    case Z_STREAM_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        JNU_ThrowIllegalArgumentException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    default:
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   125
        JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
JNIEXPORT jint JNICALL
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   131
Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr,
4162
425328b81201 4206909: want java.util.zip to work for interactive use (Z_SYNC_FLUSH)
sherman
parents: 2915
diff changeset
   132
                                         jarray b, jint off, jint len, jint flush)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
{
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   134
    z_stream *strm = jlong_to_ptr(addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   136
    jarray this_buf = (*env)->GetObjectField(env, this, bufID);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   137
    jint this_off = (*env)->GetIntField(env, this, offID);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   138
    jint this_len = (*env)->GetIntField(env, this, lenID);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   139
    jbyte *in_buf;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   140
    jbyte *out_buf;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   141
    int res;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   142
    if ((*env)->GetBooleanField(env, this, setParamsID)) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   143
        int level = (*env)->GetIntField(env, this, levelID);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   144
        int strategy = (*env)->GetIntField(env, this, strategyID);
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   145
        in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   146
        if (in_buf == NULL) {
7294
5616c35fa811 6858865: Fix for 6728376 causes regression if the size of "data" is 0 and malloc returns Null for 0-length
sherman
parents: 5506
diff changeset
   147
            // Throw OOME only when length is not zero
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
   148
            if (this_len != 0 && (*env)->ExceptionOccurred(env) == NULL)
7294
5616c35fa811 6858865: Fix for 6728376 causes regression if the size of "data" is 0 and malloc returns Null for 0-length
sherman
parents: 5506
diff changeset
   149
                JNU_ThrowOutOfMemoryError(env, 0);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   150
            return 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   151
        }
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   152
        out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   153
        if (out_buf == NULL) {
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   154
            (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
   155
            if (len != 0 && (*env)->ExceptionOccurred(env) == NULL)
7294
5616c35fa811 6858865: Fix for 6728376 causes regression if the size of "data" is 0 and malloc returns Null for 0-length
sherman
parents: 5506
diff changeset
   156
                JNU_ThrowOutOfMemoryError(env, 0);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   157
            return 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   158
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   160
        strm->next_in = (Bytef *) (in_buf + this_off);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   161
        strm->next_out = (Bytef *) (out_buf + off);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   162
        strm->avail_in = this_len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   163
        strm->avail_out = len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   164
        res = deflateParams(strm, level, strategy);
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   165
        (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   166
        (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   167
        switch (res) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   168
        case Z_OK:
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   169
            (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
48045
d66e420cc482 8191918: tomcat gzip-compressed response bodies appear to be broken in update 151
sherman
parents: 47216
diff changeset
   170
        case Z_BUF_ERROR:
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   171
            this_off += this_len - strm->avail_in;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   172
            (*env)->SetIntField(env, this, offID, this_off);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   173
            (*env)->SetIntField(env, this, lenID, strm->avail_in);
22643
ba2891b3f866 8029020: Check src/share/native/java/util/zip code for JNI pending exceptions
coffeys
parents: 14342
diff changeset
   174
            return (jint) (len - strm->avail_out);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   175
        default:
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   176
            JNU_ThrowInternalError(env, strm->msg);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   177
            return 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   178
        }
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   179
    } else {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   180
        jboolean finish = (*env)->GetBooleanField(env, this, finishID);
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   181
        in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   182
        if (in_buf == NULL) {
7294
5616c35fa811 6858865: Fix for 6728376 causes regression if the size of "data" is 0 and malloc returns Null for 0-length
sherman
parents: 5506
diff changeset
   183
            if (this_len != 0)
5616c35fa811 6858865: Fix for 6728376 causes regression if the size of "data" is 0 and malloc returns Null for 0-length
sherman
parents: 5506
diff changeset
   184
                JNU_ThrowOutOfMemoryError(env, 0);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   185
            return 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   186
        }
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   187
        out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   188
        if (out_buf == NULL) {
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   189
            (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
7294
5616c35fa811 6858865: Fix for 6728376 causes regression if the size of "data" is 0 and malloc returns Null for 0-length
sherman
parents: 5506
diff changeset
   190
            if (len != 0)
5616c35fa811 6858865: Fix for 6728376 causes regression if the size of "data" is 0 and malloc returns Null for 0-length
sherman
parents: 5506
diff changeset
   191
                JNU_ThrowOutOfMemoryError(env, 0);
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   192
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   193
            return 0;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   194
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   196
        strm->next_in = (Bytef *) (in_buf + this_off);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   197
        strm->next_out = (Bytef *) (out_buf + off);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   198
        strm->avail_in = this_len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   199
        strm->avail_out = len;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   200
        res = deflate(strm, finish ? Z_FINISH : flush);
9023
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   201
        (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
6175922846a4 6751338: ZIP inflater/deflater performance
sherman
parents: 7668
diff changeset
   202
        (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   203
        switch (res) {
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   204
        case Z_STREAM_END:
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   205
            (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   206
            /* fall through */
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   207
        case Z_OK:
48045
d66e420cc482 8191918: tomcat gzip-compressed response bodies appear to be broken in update 151
sherman
parents: 47216
diff changeset
   208
        case Z_BUF_ERROR:
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   209
            this_off += this_len - strm->avail_in;
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   210
            (*env)->SetIntField(env, this, offID, this_off);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   211
            (*env)->SetIntField(env, this, lenID, strm->avail_in);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   212
            return len - strm->avail_out;
48045
d66e420cc482 8191918: tomcat gzip-compressed response bodies appear to be broken in update 151
sherman
parents: 47216
diff changeset
   213
        default:
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   214
            JNU_ThrowInternalError(env, strm->msg);
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   215
            return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
JNIEXPORT jint JNICALL
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   221
Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
{
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   223
    return ((z_stream *)jlong_to_ptr(addr))->adler;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
JNIEXPORT void JNICALL
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   227
Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
{
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   229
    if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        JNU_ThrowInternalError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
JNIEXPORT void JNICALL
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   235
Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
{
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   237
    if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        JNU_ThrowInternalError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    } else {
5173
36ad2c5fbb51 6745393: Inflater/Deflater clone issue
sherman
parents: 4162
diff changeset
   240
        free((z_stream *)jlong_to_ptr(addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
}