112 JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg); 113 break; 114 } 115 } 116 117 JNIEXPORT jint JNICALL 118 Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr, 119 jarray b, jint off, jint len, jint flush) 120 { 121 z_stream *strm = jlong_to_ptr(addr); 122 123 jarray this_buf = (*env)->GetObjectField(env, this, bufID); 124 jint this_off = (*env)->GetIntField(env, this, offID); 125 jint this_len = (*env)->GetIntField(env, this, lenID); 126 jbyte *in_buf; 127 jbyte *out_buf; 128 int res; 129 if ((*env)->GetBooleanField(env, this, setParamsID)) { 130 int level = (*env)->GetIntField(env, this, levelID); 131 int strategy = (*env)->GetIntField(env, this, strategyID); 132 133 in_buf = (jbyte *) malloc(this_len); 134 if (in_buf == 0) { 135 // Throw OOME only when length is not zero 136 if (this_len != 0) 137 JNU_ThrowOutOfMemoryError(env, 0); 138 return 0; 139 } 140 (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf); 141 out_buf = (jbyte *) malloc(len); 142 if (out_buf == 0) { 143 free(in_buf); 144 if (len != 0) 145 JNU_ThrowOutOfMemoryError(env, 0); 146 return 0; 147 } 148 149 strm->next_in = (Bytef *) in_buf; 150 strm->next_out = (Bytef *) out_buf; 151 strm->avail_in = this_len; 152 strm->avail_out = len; 153 res = deflateParams(strm, level, strategy); 154 155 if (res == Z_OK) { 156 (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf); 157 } 158 free(out_buf); 159 free(in_buf); 160 161 switch (res) { 162 case Z_OK: 163 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE); 164 this_off += this_len - strm->avail_in; 165 (*env)->SetIntField(env, this, offID, this_off); 166 (*env)->SetIntField(env, this, lenID, strm->avail_in); 167 return len - strm->avail_out; 168 case Z_BUF_ERROR: 169 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE); 170 return 0; 171 default: 172 JNU_ThrowInternalError(env, strm->msg); 173 return 0; 174 } 175 } else { 176 jboolean finish = (*env)->GetBooleanField(env, this, finishID); 177 in_buf = (jbyte *) malloc(this_len); 178 if (in_buf == 0) { 179 if (this_len != 0) 180 JNU_ThrowOutOfMemoryError(env, 0); 181 return 0; 182 } 183 (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf); 184 185 out_buf = (jbyte *) malloc(len); 186 if (out_buf == 0) { 187 free(in_buf); 188 if (len != 0) 189 JNU_ThrowOutOfMemoryError(env, 0); 190 return 0; 191 } 192 193 strm->next_in = (Bytef *) in_buf; 194 strm->next_out = (Bytef *) out_buf; 195 strm->avail_in = this_len; 196 strm->avail_out = len; 197 res = deflate(strm, finish ? Z_FINISH : flush); 198 199 if (res == Z_STREAM_END || res == Z_OK) { 200 (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf); 201 } 202 free(out_buf); 203 free(in_buf); 204 205 switch (res) { 206 case Z_STREAM_END: 207 (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE); 208 /* fall through */ 209 case Z_OK: 210 this_off += this_len - strm->avail_in; 211 (*env)->SetIntField(env, this, offID, this_off); 212 (*env)->SetIntField(env, this, lenID, strm->avail_in); 213 return len - strm->avail_out; 214 case Z_BUF_ERROR: 215 return 0; 216 default: 217 JNU_ThrowInternalError(env, strm->msg); 218 return 0; 219 } 220 } 221 } 222 223 JNIEXPORT jint JNICALL | 112 JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg); 113 break; 114 } 115 } 116 117 JNIEXPORT jint JNICALL 118 Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr, 119 jarray b, jint off, jint len, jint flush) 120 { 121 z_stream *strm = jlong_to_ptr(addr); 122 123 jarray this_buf = (*env)->GetObjectField(env, this, bufID); 124 jint this_off = (*env)->GetIntField(env, this, offID); 125 jint this_len = (*env)->GetIntField(env, this, lenID); 126 jbyte *in_buf; 127 jbyte *out_buf; 128 int res; 129 if ((*env)->GetBooleanField(env, this, setParamsID)) { 130 int level = (*env)->GetIntField(env, this, levelID); 131 int strategy = (*env)->GetIntField(env, this, strategyID); 132 in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0); 133 if (in_buf == NULL) { 134 // Throw OOME only when length is not zero 135 if (this_len != 0) 136 JNU_ThrowOutOfMemoryError(env, 0); 137 return 0; 138 } 139 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); 140 if (out_buf == NULL) { 141 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); 142 if (len != 0) 143 JNU_ThrowOutOfMemoryError(env, 0); 144 return 0; 145 } 146 147 strm->next_in = (Bytef *) (in_buf + this_off); 148 strm->next_out = (Bytef *) (out_buf + off); 149 strm->avail_in = this_len; 150 strm->avail_out = len; 151 res = deflateParams(strm, level, strategy); 152 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0); 153 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); 154 155 switch (res) { 156 case Z_OK: 157 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE); 158 this_off += this_len - strm->avail_in; 159 (*env)->SetIntField(env, this, offID, this_off); 160 (*env)->SetIntField(env, this, lenID, strm->avail_in); 161 return len - strm->avail_out; 162 case Z_BUF_ERROR: 163 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE); 164 return 0; 165 default: 166 JNU_ThrowInternalError(env, strm->msg); 167 return 0; 168 } 169 } else { 170 jboolean finish = (*env)->GetBooleanField(env, this, finishID); 171 in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0); 172 if (in_buf == NULL) { 173 if (this_len != 0) 174 JNU_ThrowOutOfMemoryError(env, 0); 175 return 0; 176 } 177 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); 178 if (out_buf == NULL) { 179 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); 180 if (len != 0) 181 JNU_ThrowOutOfMemoryError(env, 0); 182 183 return 0; 184 } 185 186 strm->next_in = (Bytef *) (in_buf + this_off); 187 strm->next_out = (Bytef *) (out_buf + off); 188 strm->avail_in = this_len; 189 strm->avail_out = len; 190 res = deflate(strm, finish ? Z_FINISH : flush); 191 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0); 192 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0); 193 194 switch (res) { 195 case Z_STREAM_END: 196 (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE); 197 /* fall through */ 198 case Z_OK: 199 this_off += this_len - strm->avail_in; 200 (*env)->SetIntField(env, this, offID, this_off); 201 (*env)->SetIntField(env, this, lenID, strm->avail_in); 202 return len - strm->avail_out; 203 case Z_BUF_ERROR: 204 return 0; 205 default: 206 JNU_ThrowInternalError(env, strm->msg); 207 return 0; 208 } 209 } 210 } 211 212 JNIEXPORT jint JNICALL |