open Sdiff src/java.base/share/native/libzip (original) (raw)

1 /* 2 * Copyright (c) 1997, 2017, 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.Deflater 28 */ 29 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include "jlong.h" 33 #include "jni.h" 34 #include "jni_util.h" 35 #include <zlib.h> 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"); 52 CHECK_NULL(levelID); 53 strategyID = (*env)->GetFieldID(env, cls, "strategy", "I"); 54 CHECK_NULL(strategyID); 55 setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z"); 56 CHECK_NULL(setParamsID); 57 finishID = (*env)->GetFieldID(env, cls, "finish", "Z"); 58 CHECK_NULL(finishID); 59 finishedID = (*env)->GetFieldID(env, cls, "finished", "Z"); 60 CHECK_NULL(finishedID); 61 bufID = (*env)->GetFieldID(env, cls, "buf", "[B"); 62 CHECK_NULL(bufID); 63 offID = (*env)->GetFieldID(env, cls, "off", "I"); 64 CHECK_NULL(offID); 65 lenID = (*env)->GetFieldID(env, cls, "len", "I"); 66 CHECK_NULL(lenID); 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 { 79 const char *msg; 80 int ret = deflateInit2(strm, level, Z_DEFLATED, 81 nowrap ? -MAX_WBITS : MAX_WBITS, 82 DEF_MEM_LEVEL, strategy); 83 switch (ret) { 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: 95 msg = ((strm->msg != NULL) ? strm->msg : 96 (ret == Z_VERSION_ERROR) ? 97 "zlib returned Z_VERSION_ERROR: " 98 "compile time and runtime zlib implementations differ" : 99 "unknown error initializing zlib library"); 100 free(strm); 101 JNU_ThrowInternalError(env, msg); 102 return jlong_zero; 103 } 104 } 105 } 106 107 JNIEXPORT void JNICALL 108 Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr, 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 } 116 res = deflateSetDictionary((z_stream *)jlong_to_ptr(addr), buf + off, len); 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: 125 JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg); 126 break; 127 } 128 } 129 130 JNIEXPORT jint JNICALL 131 Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr, 132 jarray b, jint off, jint len, jint flush) 133 { 134 z_stream *strm = jlong_to_ptr(addr);

135 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); 145 in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0); 146 if (in_buf == NULL) { 147 // Throw OOME only when length is not zero 148 if (this_len != 0 && (*env)->ExceptionOccurred(env) == NULL) 149 JNU_ThrowOutOfMemoryError(env, 0); 150 return 0; 151 } 152 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); 153 if (out_buf == NULL) { 154 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); 155 if (len != 0 && (*env)->ExceptionOccurred(env) == NULL) 156 JNU_ThrowOutOfMemoryError(env, 0); 157 return 0; 158 } 159 160 strm->next_in = (Bytef *) (in_buf + this_off); 161 strm->next_out = (Bytef *) (out_buf + off); 162 strm->avail_in = this_len; 163 strm->avail_out = len; 164 res = deflateParams(strm, level, strategy); 165 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0); 166 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);

167 switch (res) { 168 case Z_OK: 169 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);

170 case Z_BUF_ERROR: 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); 174 return (jint) (len - strm->avail_out); 175 default: 176 JNU_ThrowInternalError(env, strm->msg); 177 return 0; 178 } 179 } else { 180 jboolean finish = (*env)->GetBooleanField(env, this, finishID); 181 in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0); 182 if (in_buf == NULL) { 183 if (this_len != 0) 184 JNU_ThrowOutOfMemoryError(env, 0); 185 return 0; 186 } 187 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); 188 if (out_buf == NULL) { 189 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); 190 if (len != 0) 191 JNU_ThrowOutOfMemoryError(env, 0); 192 193 return 0; 194 } 195 196 strm->next_in = (Bytef *) (in_buf + this_off); 197 strm->next_out = (Bytef *) (out_buf + off); 198 strm->avail_in = this_len; 199 strm->avail_out = len; 200 res = deflate(strm, finish ? Z_FINISH : flush); 201 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0); 202 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); 203 switch (res) { 204 case Z_STREAM_END: 205 (env)->SetBooleanField(env, this, finishedID, JNI_TRUE); 206 / fall through */ 207 case Z_OK: 208 case Z_BUF_ERROR: 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; 213 default: 214 JNU_ThrowInternalError(env, strm->msg); 215 return 0; 216 } 217 }

218 } 219 220 JNIEXPORT jint JNICALL 221 Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr) 222 { 223 return ((z_stream *)jlong_to_ptr(addr))->adler; 224 } 225 226 JNIEXPORT void JNICALL 227 Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr) 228 { 229 if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) { 230 JNU_ThrowInternalError(env, 0); 231 } 232 } 233 234 JNIEXPORT void JNICALL 235 Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr) 236 { 237 if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {

1 /* 2 * Copyright (c) 1997, 2018, 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.Deflater 28 */ 29 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include "jlong.h" 33 #include "jni.h" 34 #include "jni_util.h" 35 #include <zlib.h> 36 37 #include "java_util_zip_Deflater.h" 38 39 #define DEF_MEM_LEVEL 8 40

41 JNIEXPORT jlong JNICALL 42 Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level, 43 jint strategy, jboolean nowrap) 44 { 45 z_stream *strm = calloc(1, sizeof(z_stream)); 46 47 if (strm == 0) { 48 JNU_ThrowOutOfMemoryError(env, 0); 49 return jlong_zero; 50 } else { 51 const char *msg; 52 int ret = deflateInit2(strm, level, Z_DEFLATED, 53 nowrap ? -MAX_WBITS : MAX_WBITS, 54 DEF_MEM_LEVEL, strategy); 55 switch (ret) { 56 case Z_OK: 57 return ptr_to_jlong(strm); 58 case Z_MEM_ERROR: 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 doSetDictionary(JNIEnv *env, jlong addr, jbyte *buf, jint len)

80 { 81 int res = deflateSetDictionary(jlong_to_ptr(addr), (Bytef *) buf, len);

82 switch (res) { 83 case Z_OK: 84 break; 85 case Z_STREAM_ERROR: 86 JNU_ThrowIllegalArgumentException(env, 0); 87 break; 88 default: 89 JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg); 90 break; 91 } 92 } 93 94 JNIEXPORT void JNICALL 95 Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr, 96 jbyteArray b, jint off, jint len) 97 { 98 jbyte *buf = (env)->GetPrimitiveArrayCritical(env, b, 0); 99 if (buf == NULL) / out of memory */ 100 return; 101 doSetDictionary(env, addr, buf + off, len); 102 (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0); 103 } 104 105 JNIEXPORT void JNICALL 106 Java_java_util_zip_Deflater_setDictionaryBuffer(JNIEnv *env, jclass cls, jlong addr, 107 jlong bufferAddr, jint len) 108 { 109 jbyte *buf = jlong_to_ptr(bufferAddr); 110 doSetDictionary(env, addr, buf, len); 111 }

112 113 static jlong doDeflate(JNIEnv *env, jobject this, jlong addr, 114 jbyte *input, jint inputLen, 115 jbyte *output, jint outputLen, 116 jint flush, jint params) 117 { 118 z_stream *strm = jlong_to_ptr(addr); 119 jint inputUsed = 0, outputUsed = 0; 120 int finished = 0; 121 int setParams = params & 1; 122 123 strm->next_in = (Bytef *) input; 124 strm->next_out = (Bytef ) output; 125 strm->avail_in = inputLen; 126 strm->avail_out = outputLen; 127 128 if (setParams) { 129 int strategy = (params >> 1) & 3; 130 int level = params >> 3; 131 int res = deflateParams(strm, level, strategy); 132 switch (res) { 133 case Z_OK: 134 setParams = 0; 135 / fall through */ 136 case Z_BUF_ERROR: 137 inputUsed = inputLen - strm->avail_in; 138 outputUsed = outputLen - strm->avail_out; 139 break;

140 default: 141 JNU_ThrowInternalError(env, strm->msg); 142 return 0; 143 } 144 } else { 145 int res = deflate(strm, flush);

146 switch (res) { 147 case Z_STREAM_END: 148 finished = 1; 149 /* fall through */ 150 case Z_OK: 151 case Z_BUF_ERROR: 152 inputUsed = inputLen - strm->avail_in; 153 outputUsed = outputLen - strm->avail_out; 154 break;

155 default: 156 JNU_ThrowInternalError(env, strm->msg); 157 return 0; 158 } 159 } 160 return ((jlong)inputUsed) | (((jlong)outputUsed) << 31) | (((jlong)finished) << 62) | (((jlong)setParams) << 63); 161 } 162 163 JNIEXPORT jlong JNICALL 164 Java_java_util_zip_Deflater_deflateBytesBytes(JNIEnv *env, jobject this, jlong addr, 165 jbyteArray inputArray, jint inputOff, jint inputLen, 166 jbyteArray outputArray, jint outputOff, jint outputLen, 167 jint flush, jint params) 168 { 169 jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0); 170 jbyte *output; 171 jlong retVal; 172 if (input == NULL) { 173 if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL) 174 JNU_ThrowOutOfMemoryError(env, 0); 175 return 0L; 176 } 177 output = (*env)->GetPrimitiveArrayCritical(env, outputArray, 0); 178 if (output == NULL) { 179 (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0); 180 if (outputLen != 0 && (*env)->ExceptionOccurred(env) == NULL) 181 JNU_ThrowOutOfMemoryError(env, 0); 182 return 0L; 183 } 184 185 retVal = doDeflate(env, this, addr, 186 input + inputOff, inputLen, 187 output + outputOff, outputLen, 188 flush, params); 189 190 (*env)->ReleasePrimitiveArrayCritical(env, outputArray, output, 0); 191 (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0); 192 193 return retVal; 194 } 195 196 197 JNIEXPORT jlong JNICALL 198 Java_java_util_zip_Deflater_deflateBytesBuffer(JNIEnv *env, jobject this, jlong addr, 199 jbyteArray inputArray, jint inputOff, jint inputLen, 200 jlong outputBuffer, jint outputLen, 201 jint flush, jint params) 202 { 203 jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0); 204 jbyte *output; 205 jlong retVal; 206 if (input == NULL) { 207 if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL) 208 JNU_ThrowOutOfMemoryError(env, 0); 209 return 0L; 210 } 211 output = jlong_to_ptr(outputBuffer); 212 213 retVal = doDeflate(env, this, addr, 214 input + inputOff, inputLen, 215 output, outputLen, 216 flush, params); 217 218 (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0); 219 220 return retVal; 221 } 222 223 JNIEXPORT jlong JNICALL 224 Java_java_util_zip_Deflater_deflateBufferBytes(JNIEnv *env, jobject this, jlong addr, 225 jlong inputBuffer, jint inputLen, 226 jbyteArray outputArray, jint outputOff, jint outputLen, 227 jint flush, jint params) 228 { 229 jbyte *input = jlong_to_ptr(inputBuffer); 230 jbyte *output = (*env)->GetPrimitiveArrayCritical(env, outputArray, 0); 231 jlong retVal; 232 if (output == NULL) { 233 if (outputLen != 0 && (*env)->ExceptionOccurred(env) == NULL) 234 JNU_ThrowOutOfMemoryError(env, 0); 235 return 0L; 236 } 237 238 retVal = doDeflate(env, this, addr, 239 input, inputLen, 240 output + outputOff, outputLen, 241 flush, params); 242 243 (*env)->ReleasePrimitiveArrayCritical(env, outputArray, input, 0); 244 245 return retVal; 246 } 247 248 JNIEXPORT jlong JNICALL 249 Java_java_util_zip_Deflater_deflateBufferBuffer(JNIEnv *env, jobject this, jlong addr, 250 jlong inputBuffer, jint inputLen, 251 jlong outputBuffer, jint outputLen, 252 jint flush, jint params) 253 { 254 jbyte *input = jlong_to_ptr(inputBuffer); 255 jbyte *output = jlong_to_ptr(outputBuffer); 256 257 return doDeflate(env, this, addr, 258 input, inputLen, 259 output, outputLen, 260 flush, params); 261 } 262 263 JNIEXPORT jint JNICALL 264 Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr) 265 { 266 return ((z_stream *)jlong_to_ptr(addr))->adler; 267 } 268 269 JNIEXPORT void JNICALL 270 Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr) 271 { 272 if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) { 273 JNU_ThrowInternalError(env, 0); 274 } 275 } 276 277 JNIEXPORT void JNICALL 278 Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr) 279 { 280 if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {