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 |
23 | * questions. |
24 | */ |
25 | |
26 | /* |
27 | * Native method support for java.util.zip.Inflater |
28 | */ |
29 | |
30 | #include <stddef.h> |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
33 | #include <errno.h> |
34 | #include <string.h> |
35 | #include "jlong.h" |
36 | #include "jni.h" |
37 | #include "jvm.h" |
38 | #include "jni_util.h" |
39 | #include <zlib.h> |
40 | #include "java_util_zip_Inflater.h" |
41 | |
42 | #define ThrowDataFormatException(env, msg) \ |
43 | JNU_ThrowByName(env, "java/util/zip/DataFormatException", msg) |
44 | |
45 | static jfieldID inputConsumedID; |
46 | static jfieldID outputConsumedID; |
47 | |
48 | JNIEXPORT void JNICALL |
49 | Java_java_util_zip_Inflater_initIDs(JNIEnv *env, jclass cls) |
50 | { |
51 | inputConsumedID = (*env)->GetFieldID(env, cls, "inputConsumed" , "I" ); |
52 | outputConsumedID = (*env)->GetFieldID(env, cls, "outputConsumed" , "I" ); |
53 | CHECK_NULL(inputConsumedID); |
54 | CHECK_NULL(outputConsumedID); |
55 | } |
56 | |
57 | JNIEXPORT jlong JNICALL |
58 | Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap) |
59 | { |
60 | z_stream *strm = calloc(1, sizeof(z_stream)); |
61 | |
62 | if (strm == NULL) { |
63 | JNU_ThrowOutOfMemoryError(env, 0); |
64 | return jlong_zero; |
65 | } else { |
66 | const char *msg; |
67 | int ret = inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS); |
68 | switch (ret) { |
69 | case Z_OK: |
70 | return ptr_to_jlong(strm); |
71 | case Z_MEM_ERROR: |
72 | free(strm); |
73 | JNU_ThrowOutOfMemoryError(env, 0); |
74 | return jlong_zero; |
75 | default: |
76 | msg = ((strm->msg != NULL) ? strm->msg : |
77 | (ret == Z_VERSION_ERROR) ? |
78 | "zlib returned Z_VERSION_ERROR: " |
79 | "compile time and runtime zlib implementations differ" : |
80 | (ret == Z_STREAM_ERROR) ? |
81 | "inflateInit2 returned Z_STREAM_ERROR" : |
82 | "unknown error initializing zlib library" ); |
83 | free(strm); |
84 | JNU_ThrowInternalError(env, msg); |
85 | return jlong_zero; |
86 | } |
87 | } |
88 | } |
89 | |
90 | static void checkSetDictionaryResult(JNIEnv *env, jlong addr, int res) |
91 | { |
92 | switch (res) { |
93 | case Z_OK: |
94 | break; |
95 | case Z_STREAM_ERROR: |
96 | case Z_DATA_ERROR: |
97 | JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg); |
98 | break; |
99 | default: |
100 | JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg); |
101 | break; |
102 | } |
103 | } |
104 | |
105 | JNIEXPORT void JNICALL |
106 | Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr, |
107 | jbyteArray b, jint off, jint len) |
108 | { |
109 | jint res; |
110 | Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); |
111 | if (buf == NULL) /* out of memory */ |
112 | return; |
113 | res = inflateSetDictionary(jlong_to_ptr(addr), buf + off, len); |
114 | (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0); |
115 | checkSetDictionaryResult(env, addr, res); |
116 | } |
117 | |
118 | JNIEXPORT void JNICALL |
119 | Java_java_util_zip_Inflater_setDictionaryBuffer(JNIEnv *env, jclass cls, jlong addr, |
120 | jlong bufferAddr, jint len) |
121 | { |
122 | jint res; |
123 | Bytef *buf = jlong_to_ptr(bufferAddr); |
124 | res = inflateSetDictionary(jlong_to_ptr(addr), buf, len); |
125 | checkSetDictionaryResult(env, addr, res); |
126 | } |
127 | |
128 | static jint doInflate(jlong addr, |
129 | jbyte *input, jint inputLen, |
130 | jbyte *output, jint outputLen) |
131 | { |
132 | jint ret; |
133 | z_stream *strm = jlong_to_ptr(addr); |
134 | |
135 | strm->next_in = (Bytef *) input; |
136 | strm->next_out = (Bytef *) output; |
137 | strm->avail_in = inputLen; |
138 | strm->avail_out = outputLen; |
139 | |
140 | ret = inflate(strm, Z_PARTIAL_FLUSH); |
141 | return ret; |
142 | } |
143 | |
144 | static jlong checkInflateStatus(JNIEnv *env, jobject this, jlong addr, |
145 | jint inputLen, jint outputLen, jint ret ) |
146 | { |
147 | z_stream *strm = jlong_to_ptr(addr); |
148 | jint inputUsed = 0, outputUsed = 0; |
149 | int finished = 0; |
150 | int needDict = 0; |
151 | |
152 | switch (ret) { |
153 | case Z_STREAM_END: |
154 | finished = 1; |
155 | /* fall through */ |
156 | case Z_OK: |
157 | inputUsed = inputLen - strm->avail_in; |
158 | outputUsed = outputLen - strm->avail_out; |
159 | break; |
160 | case Z_NEED_DICT: |
161 | needDict = 1; |
162 | /* Might have consumed some input here! */ |
163 | inputUsed = inputLen - strm->avail_in; |
164 | /* zlib is unclear about whether output may be produced */ |
165 | outputUsed = outputLen - strm->avail_out; |
166 | break; |
167 | case Z_BUF_ERROR: |
168 | break; |
169 | case Z_DATA_ERROR: |
170 | inputUsed = inputLen - strm->avail_in; |
171 | (*env)->SetIntField(env, this, inputConsumedID, inputUsed); |
172 | outputUsed = outputLen - strm->avail_out; |
173 | (*env)->SetIntField(env, this, outputConsumedID, outputUsed); |
174 | ThrowDataFormatException(env, strm->msg); |
175 | break; |
176 | case Z_MEM_ERROR: |
177 | JNU_ThrowOutOfMemoryError(env, 0); |
178 | break; |
179 | default: |
180 | JNU_ThrowInternalError(env, strm->msg); |
181 | break; |
182 | } |
183 | return ((jlong)inputUsed) | (((jlong)outputUsed) << 31) | (((jlong)finished) << 62) | (((jlong)needDict) << 63); |
184 | } |
185 | |
186 | JNIEXPORT jlong JNICALL |
187 | Java_java_util_zip_Inflater_inflateBytesBytes(JNIEnv *env, jobject this, jlong addr, |
188 | jbyteArray inputArray, jint inputOff, jint inputLen, |
189 | jbyteArray outputArray, jint outputOff, jint outputLen) |
190 | { |
191 | jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0); |
192 | jbyte *output; |
193 | jint ret; |
194 | jlong retVal; |
195 | |
196 | if (input == NULL) { |
197 | if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL) |
198 | JNU_ThrowOutOfMemoryError(env, 0); |
199 | return 0L; |
200 | } |
201 | output = (*env)->GetPrimitiveArrayCritical(env, outputArray, 0); |
202 | if (output == NULL) { |
203 | (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0); |
204 | if (outputLen != 0 && (*env)->ExceptionOccurred(env) == NULL) |
205 | JNU_ThrowOutOfMemoryError(env, 0); |
206 | return 0L; |
207 | } |
208 | |
209 | ret = doInflate(addr, input + inputOff, inputLen, output + outputOff, |
210 | outputLen); |
211 | |
212 | (*env)->ReleasePrimitiveArrayCritical(env, outputArray, output, 0); |
213 | (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0); |
214 | |
215 | retVal = checkInflateStatus(env, this, addr, inputLen, outputLen, ret ); |
216 | return retVal; |
217 | } |
218 | |
219 | JNIEXPORT jlong JNICALL |
220 | Java_java_util_zip_Inflater_inflateBytesBuffer(JNIEnv *env, jobject this, jlong addr, |
221 | jbyteArray inputArray, jint inputOff, jint inputLen, |
222 | jlong outputBuffer, jint outputLen) |
223 | { |
224 | jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0); |
225 | jbyte *output; |
226 | jint ret; |
227 | jlong retVal; |
228 | |
229 | if (input == NULL) { |
230 | if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL) |
231 | JNU_ThrowOutOfMemoryError(env, 0); |
232 | return 0L; |
233 | } |
234 | output = jlong_to_ptr(outputBuffer); |
235 | |
236 | ret = doInflate(addr, input + inputOff, inputLen, output, outputLen); |
237 | |
238 | (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0); |
239 | retVal = checkInflateStatus(env, this, addr, inputLen, outputLen, ret ); |
240 | |
241 | return retVal; |
242 | } |
243 | |
244 | JNIEXPORT jlong JNICALL |
245 | Java_java_util_zip_Inflater_inflateBufferBytes(JNIEnv *env, jobject this, jlong addr, |
246 | jlong inputBuffer, jint inputLen, |
247 | jbyteArray outputArray, jint outputOff, jint outputLen) |
248 | { |
249 | jbyte *input = jlong_to_ptr(inputBuffer); |
250 | jbyte *output = (*env)->GetPrimitiveArrayCritical(env, outputArray, 0); |
251 | jint ret; |
252 | jlong retVal; |
253 | |
254 | if (output == NULL) { |
255 | if (outputLen != 0 && (*env)->ExceptionOccurred(env) == NULL) |
256 | JNU_ThrowOutOfMemoryError(env, 0); |
257 | return 0L; |
258 | } |
259 | |
260 | ret = doInflate(addr, input, inputLen, output + outputOff, outputLen); |
261 | |
262 | (*env)->ReleasePrimitiveArrayCritical(env, outputArray, output, 0); |
263 | retVal = checkInflateStatus(env, this, addr, inputLen, outputLen, ret ); |
264 | |
265 | return retVal; |
266 | } |
267 | |
268 | JNIEXPORT jlong JNICALL |
269 | Java_java_util_zip_Inflater_inflateBufferBuffer(JNIEnv *env, jobject this, jlong addr, |
270 | jlong inputBuffer, jint inputLen, |
271 | jlong outputBuffer, jint outputLen) |
272 | { |
273 | jbyte *input = jlong_to_ptr(inputBuffer); |
274 | jbyte *output = jlong_to_ptr(outputBuffer); |
275 | jint ret; |
276 | jlong retVal; |
277 | |
278 | ret = doInflate(addr, input, inputLen, output, outputLen); |
279 | retVal = checkInflateStatus(env, this, addr, inputLen, outputLen, ret); |
280 | return retVal; |
281 | } |
282 | |
283 | JNIEXPORT jint JNICALL |
284 | Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr) |
285 | { |
286 | return ((z_stream *)jlong_to_ptr(addr))->adler; |
287 | } |
288 | |
289 | JNIEXPORT void JNICALL |
290 | Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr) |
291 | { |
292 | if (inflateReset(jlong_to_ptr(addr)) != Z_OK) { |
293 | JNU_ThrowInternalError(env, 0); |
294 | } |
295 | } |
296 | |
297 | JNIEXPORT void JNICALL |
298 | Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr) |
299 | { |
300 | if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) { |
301 | JNU_ThrowInternalError(env, 0); |
302 | } else { |
303 | free(jlong_to_ptr(addr)); |
304 | } |
305 | } |
306 | |