author | sherman |
Fri, 01 Dec 2017 22:04:03 -0800 | |
changeset 48045 | d66e420cc482 |
parent 47216 | 71c04702a3d5 |
child 49834 | 99644c75eaed |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* Native method support for java.util.zip.Deflater |
|
28 |
*/ |
|
29 |
||
30 |
#include <stdio.h> |
|
31 |
#include <stdlib.h> |
|
32 |
#include "jlong.h" |
|
33 |
#include "jni.h" |
|
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 | 36 |
|
37 |
#include "java_util_zip_Deflater.h" |
|
38 |
||
39 |
#define DEF_MEM_LEVEL 8 |
|
40 |
||
41 |
static jfieldID levelID; |
|
42 |
static jfieldID strategyID; |
|
43 |
static jfieldID setParamsID; |
|
44 |
static jfieldID finishID; |
|
45 |
static jfieldID finishedID; |
|
46 |
static jfieldID bufID, offID, lenID; |
|
47 |
||
48 |
JNIEXPORT void JNICALL |
|
49 |
Java_java_util_zip_Deflater_initIDs(JNIEnv *env, jclass cls) |
|
50 |
{ |
|
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 67 |
} |
68 |
||
69 |
JNIEXPORT jlong JNICALL |
|
70 |
Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level, |
|
71 |
jint strategy, jboolean nowrap) |
|
72 |
{ |
|
73 |
z_stream *strm = calloc(1, sizeof(z_stream)); |
|
74 |
||
75 |
if (strm == 0) { |
|
76 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
77 |
return jlong_zero; |
|
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 | 84 |
case Z_OK: |
85 |
return ptr_to_jlong(strm); |
|
86 |
case Z_MEM_ERROR: |
|
87 |
free(strm); |
|
88 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
89 |
return jlong_zero; |
|
90 |
case Z_STREAM_ERROR: |
|
91 |
free(strm); |
|
92 |
JNU_ThrowIllegalArgumentException(env, 0); |
|
93 |
return jlong_zero; |
|
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 | 100 |
free(strm); |
101 |
JNU_ThrowInternalError(env, msg); |
|
102 |
return jlong_zero; |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
JNIEXPORT void JNICALL |
|
5173 | 108 |
Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr, |
2 | 109 |
jarray b, jint off, jint len) |
110 |
{ |
|
111 |
Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); |
|
112 |
int res; |
|
113 |
if (buf == 0) {/* out of memory */ |
|
114 |
return; |
|
115 |
} |
|
5173 | 116 |
res = deflateSetDictionary((z_stream *)jlong_to_ptr(addr), buf + off, len); |
2 | 117 |
(*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0); |
118 |
switch (res) { |
|
119 |
case Z_OK: |
|
120 |
break; |
|
121 |
case Z_STREAM_ERROR: |
|
122 |
JNU_ThrowIllegalArgumentException(env, 0); |
|
123 |
break; |
|
124 |
default: |
|
5173 | 125 |
JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg); |
2 | 126 |
break; |
127 |
} |
|
128 |
} |
|
129 |
||
130 |
JNIEXPORT jint JNICALL |
|
5173 | 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 | 133 |
{ |
5173 | 134 |
z_stream *strm = jlong_to_ptr(addr); |
2 | 135 |
|
5173 | 136 |
jarray this_buf = (*env)->GetObjectField(env, this, bufID); |
137 |
jint this_off = (*env)->GetIntField(env, this, offID); |
|
138 |
jint this_len = (*env)->GetIntField(env, this, lenID); |
|
139 |
jbyte *in_buf; |
|
140 |
jbyte *out_buf; |
|
141 |
int res; |
|
142 |
if ((*env)->GetBooleanField(env, this, setParamsID)) { |
|
143 |
int level = (*env)->GetIntField(env, this, levelID); |
|
144 |
int strategy = (*env)->GetIntField(env, this, strategyID); |
|
9023 | 145 |
in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0); |
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 | 150 |
return 0; |
151 |
} |
|
9023 | 152 |
out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); |
153 |
if (out_buf == NULL) { |
|
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 | 157 |
return 0; |
158 |
} |
|
2 | 159 |
|
9023 | 160 |
strm->next_in = (Bytef *) (in_buf + this_off); |
161 |
strm->next_out = (Bytef *) (out_buf + off); |
|
5173 | 162 |
strm->avail_in = this_len; |
163 |
strm->avail_out = len; |
|
164 |
res = deflateParams(strm, level, strategy); |
|
9023 | 165 |
(*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0); |
166 |
(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); |
|
5173 | 167 |
switch (res) { |
168 |
case Z_OK: |
|
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 | 171 |
this_off += this_len - strm->avail_in; |
172 |
(*env)->SetIntField(env, this, offID, this_off); |
|
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 | 175 |
default: |
176 |
JNU_ThrowInternalError(env, strm->msg); |
|
177 |
return 0; |
|
178 |
} |
|
179 |
} else { |
|
180 |
jboolean finish = (*env)->GetBooleanField(env, this, finishID); |
|
9023 | 181 |
in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0); |
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 | 185 |
return 0; |
186 |
} |
|
9023 | 187 |
out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); |
188 |
if (out_buf == NULL) { |
|
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 | 192 |
|
5173 | 193 |
return 0; |
194 |
} |
|
2 | 195 |
|
9023 | 196 |
strm->next_in = (Bytef *) (in_buf + this_off); |
197 |
strm->next_out = (Bytef *) (out_buf + off); |
|
5173 | 198 |
strm->avail_in = this_len; |
199 |
strm->avail_out = len; |
|
200 |
res = deflate(strm, finish ? Z_FINISH : flush); |
|
9023 | 201 |
(*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0); |
202 |
(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); |
|
5173 | 203 |
switch (res) { |
204 |
case Z_STREAM_END: |
|
205 |
(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE); |
|
206 |
/* fall through */ |
|
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 | 209 |
this_off += this_len - strm->avail_in; |
210 |
(*env)->SetIntField(env, this, offID, this_off); |
|
211 |
(*env)->SetIntField(env, this, lenID, strm->avail_in); |
|
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 | 214 |
JNU_ThrowInternalError(env, strm->msg); |
215 |
return 0; |
|
2 | 216 |
} |
217 |
} |
|
218 |
} |
|
219 |
||
220 |
JNIEXPORT jint JNICALL |
|
5173 | 221 |
Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr) |
2 | 222 |
{ |
5173 | 223 |
return ((z_stream *)jlong_to_ptr(addr))->adler; |
2 | 224 |
} |
225 |
||
226 |
JNIEXPORT void JNICALL |
|
5173 | 227 |
Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr) |
2 | 228 |
{ |
5173 | 229 |
if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) { |
2 | 230 |
JNU_ThrowInternalError(env, 0); |
231 |
} |
|
232 |
} |
|
233 |
||
234 |
JNIEXPORT void JNICALL |
|
5173 | 235 |
Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr) |
2 | 236 |
{ |
5173 | 237 |
if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) { |
2 | 238 |
JNU_ThrowInternalError(env, 0); |
239 |
} else { |
|
5173 | 240 |
free((z_stream *)jlong_to_ptr(addr)); |
2 | 241 |
} |
242 |
} |