1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
59 free(strm);
60 JNU_ThrowOutOfMemoryError(env, 0);
61 return jlong_zero;
62 case Z_STREAM_ERROR:
63 free(strm);
64 JNU_ThrowIllegalArgumentException(env, 0);
65 return jlong_zero;
66 default:
67 msg = ((strm->msg != NULL) ? strm->msg :
68 (ret == Z_VERSION_ERROR) ?
69 "zlib returned Z_VERSION_ERROR: "
70 "compile time and runtime zlib implementations differ" :
71 "unknown error initializing zlib library");
72 free(strm);
73 JNU_ThrowInternalError(env, msg);
74 return jlong_zero;
75 }
76 }
77 }
78
79 static void checkSetDictionaryResult(JNIEnv *env, jlong addr, jint res)
80 {
81 switch (res) {
82 case Z_OK:
83 break;
84 case Z_STREAM_ERROR:
85 JNU_ThrowIllegalArgumentException(env, 0);
86 break;
87 default:
88 JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
89 break;
90 }
91 }
92
93 JNIEXPORT void JNICALL
94 Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
95 jbyteArray b, jint off, jint len)
96 {
97 int res;
98 Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
99 if (buf == NULL) /* out of memory */
100 return;
101 res = deflateSetDictionary(jlong_to_ptr(addr), buf, len);
102 (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
103 checkSetDictionaryResult(env, addr, res);
104 }
105
106 JNIEXPORT void JNICALL
107 Java_java_util_zip_Deflater_setDictionaryBuffer(JNIEnv *env, jclass cls, jlong addr,
108 jlong bufferAddr, jint len)
140 static jlong checkDeflateStatus(JNIEnv *env, jlong addr,
141 jint inputLen,
142 jint outputLen,
143 jint params, int res)
144 {
145 z_stream *strm = jlong_to_ptr(addr);
146 jint inputUsed = 0, outputUsed = 0;
147 int finished = 0;
148 int setParams = params & 1;
149
150 if (setParams) {
151 switch (res) {
152 case Z_OK:
153 setParams = 0;
154 /* fall through */
155 case Z_BUF_ERROR:
156 inputUsed = inputLen - strm->avail_in;
157 outputUsed = outputLen - strm->avail_out;
158 break;
159 default:
160 JNU_ThrowInternalError(env, strm->msg);
161 return 0;
162 }
163 } else {
164 switch (res) {
165 case Z_STREAM_END:
166 finished = 1;
167 /* fall through */
168 case Z_OK:
169 case Z_BUF_ERROR:
170 inputUsed = inputLen - strm->avail_in;
171 outputUsed = outputLen - strm->avail_out;
172 break;
173 default:
174 JNU_ThrowInternalError(env, strm->msg);
175 return 0;
176 }
177 }
178 return ((jlong)inputUsed) | (((jlong)outputUsed) << 31) | (((jlong)finished) << 62) | (((jlong)setParams) << 63);
179 }
180
181 JNIEXPORT jlong JNICALL
182 Java_java_util_zip_Deflater_deflateBytesBytes(JNIEnv *env, jobject this, jlong addr,
183 jbyteArray inputArray, jint inputOff, jint inputLen,
184 jbyteArray outputArray, jint outputOff, jint outputLen,
185 jint flush, jint params)
186 {
187 jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0);
188 jbyte *output;
189 jlong retVal;
190 jint res;
191
192 if (input == NULL) {
193 if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
194 JNU_ThrowOutOfMemoryError(env, 0);
272 jbyte *input = jlong_to_ptr(inputBuffer);
273 jbyte *output = jlong_to_ptr(outputBuffer);
274 jlong retVal;
275 jint res;
276
277 res = doDeflate(env, addr, input, inputLen, output, outputLen, flush, params);
278 retVal = checkDeflateStatus(env, addr, inputLen, outputLen, params, res);
279 return retVal;
280 }
281
282 JNIEXPORT jint JNICALL
283 Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
284 {
285 return ((z_stream *)jlong_to_ptr(addr))->adler;
286 }
287
288 JNIEXPORT void JNICALL
289 Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
290 {
291 if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
292 JNU_ThrowInternalError(env, 0);
293 }
294 }
295
296 JNIEXPORT void JNICALL
297 Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr)
298 {
299 if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
300 JNU_ThrowInternalError(env, 0);
301 } else {
302 free((z_stream *)jlong_to_ptr(addr));
303 }
304 }
|
1 /*
2 * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
59 free(strm);
60 JNU_ThrowOutOfMemoryError(env, 0);
61 return jlong_zero;
62 case Z_STREAM_ERROR:
63 free(strm);
64 JNU_ThrowIllegalArgumentException(env, 0);
65 return jlong_zero;
66 default:
67 msg = ((strm->msg != NULL) ? strm->msg :
68 (ret == Z_VERSION_ERROR) ?
69 "zlib returned Z_VERSION_ERROR: "
70 "compile time and runtime zlib implementations differ" :
71 "unknown error initializing zlib library");
72 free(strm);
73 JNU_ThrowInternalError(env, msg);
74 return jlong_zero;
75 }
76 }
77 }
78
79 static void throwInternalErrorHelper(JNIEnv *env, z_stream *strm, const char *fixmsg) {
80 const char *msg = NULL;
81 msg = (strm->msg != NULL) ? strm->msg : fixmsg;
82 JNU_ThrowInternalError(env, msg);
83 }
84
85 static void checkSetDictionaryResult(JNIEnv *env, jlong addr, jint res)
86 {
87 z_stream *strm = (z_stream *) jlong_to_ptr(addr);
88 switch (res) {
89 case Z_OK:
90 break;
91 case Z_STREAM_ERROR:
92 JNU_ThrowIllegalArgumentException(env, 0);
93 break;
94 default:
95 throwInternalErrorHelper(env, strm, "unknown error in checkSetDictionaryResult");
96 break;
97 }
98 }
99
100 JNIEXPORT void JNICALL
101 Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
102 jbyteArray b, jint off, jint len)
103 {
104 int res;
105 Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
106 if (buf == NULL) /* out of memory */
107 return;
108 res = deflateSetDictionary(jlong_to_ptr(addr), buf, len);
109 (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
110 checkSetDictionaryResult(env, addr, res);
111 }
112
113 JNIEXPORT void JNICALL
114 Java_java_util_zip_Deflater_setDictionaryBuffer(JNIEnv *env, jclass cls, jlong addr,
115 jlong bufferAddr, jint len)
147 static jlong checkDeflateStatus(JNIEnv *env, jlong addr,
148 jint inputLen,
149 jint outputLen,
150 jint params, int res)
151 {
152 z_stream *strm = jlong_to_ptr(addr);
153 jint inputUsed = 0, outputUsed = 0;
154 int finished = 0;
155 int setParams = params & 1;
156
157 if (setParams) {
158 switch (res) {
159 case Z_OK:
160 setParams = 0;
161 /* fall through */
162 case Z_BUF_ERROR:
163 inputUsed = inputLen - strm->avail_in;
164 outputUsed = outputLen - strm->avail_out;
165 break;
166 default:
167 throwInternalErrorHelper(env, strm, "unknown error in checkDeflateStatus, setParams case");
168 return 0;
169 }
170 } else {
171 switch (res) {
172 case Z_STREAM_END:
173 finished = 1;
174 /* fall through */
175 case Z_OK:
176 case Z_BUF_ERROR:
177 inputUsed = inputLen - strm->avail_in;
178 outputUsed = outputLen - strm->avail_out;
179 break;
180 default:
181 throwInternalErrorHelper(env, strm, "unknown error in checkDeflateStatus");
182 return 0;
183 }
184 }
185 return ((jlong)inputUsed) | (((jlong)outputUsed) << 31) | (((jlong)finished) << 62) | (((jlong)setParams) << 63);
186 }
187
188 JNIEXPORT jlong JNICALL
189 Java_java_util_zip_Deflater_deflateBytesBytes(JNIEnv *env, jobject this, jlong addr,
190 jbyteArray inputArray, jint inputOff, jint inputLen,
191 jbyteArray outputArray, jint outputOff, jint outputLen,
192 jint flush, jint params)
193 {
194 jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0);
195 jbyte *output;
196 jlong retVal;
197 jint res;
198
199 if (input == NULL) {
200 if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
201 JNU_ThrowOutOfMemoryError(env, 0);
279 jbyte *input = jlong_to_ptr(inputBuffer);
280 jbyte *output = jlong_to_ptr(outputBuffer);
281 jlong retVal;
282 jint res;
283
284 res = doDeflate(env, addr, input, inputLen, output, outputLen, flush, params);
285 retVal = checkDeflateStatus(env, addr, inputLen, outputLen, params, res);
286 return retVal;
287 }
288
289 JNIEXPORT jint JNICALL
290 Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
291 {
292 return ((z_stream *)jlong_to_ptr(addr))->adler;
293 }
294
295 JNIEXPORT void JNICALL
296 Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
297 {
298 if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
299 JNU_ThrowInternalError(env, "deflateReset failed");
300 }
301 }
302
303 JNIEXPORT void JNICALL
304 Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr)
305 {
306 if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
307 JNU_ThrowInternalError(env, "deflateEnd failed");
308 } else {
309 free((z_stream *)jlong_to_ptr(addr));
310 }
311 }
|