1 /*
2 * Copyright (c) 1997, 2012, 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
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 strategyID = (*env)->GetFieldID(env, cls, "strategy", "I");
53 setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z");
54 finishID = (*env)->GetFieldID(env, cls, "finish", "Z");
55 finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
56 bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
57 offID = (*env)->GetFieldID(env, cls, "off", "I");
58 lenID = (*env)->GetFieldID(env, cls, "len", "I");
59 }
60
61 JNIEXPORT jlong JNICALL
62 Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level,
63 jint strategy, jboolean nowrap)
64 {
65 z_stream *strm = calloc(1, sizeof(z_stream));
66
67 if (strm == 0) {
68 JNU_ThrowOutOfMemoryError(env, 0);
69 return jlong_zero;
70 } else {
71 char *msg;
72 switch (deflateInit2(strm, level, Z_DEFLATED,
73 nowrap ? -MAX_WBITS : MAX_WBITS,
74 DEF_MEM_LEVEL, strategy)) {
75 case Z_OK:
76 return ptr_to_jlong(strm);
77 case Z_MEM_ERROR:
78 free(strm);
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);
|
1 /*
2 * Copyright (c) 1997, 2014, 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
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 char *msg;
80 switch (deflateInit2(strm, level, Z_DEFLATED,
81 nowrap ? -MAX_WBITS : MAX_WBITS,
82 DEF_MEM_LEVEL, strategy)) {
83 case Z_OK:
84 return ptr_to_jlong(strm);
85 case Z_MEM_ERROR:
86 free(strm);
123 }
124
125 JNIEXPORT jint JNICALL
126 Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr,
127 jarray b, jint off, jint len, jint flush)
128 {
129 z_stream *strm = jlong_to_ptr(addr);
130
131 jarray this_buf = (*env)->GetObjectField(env, this, bufID);
132 jint this_off = (*env)->GetIntField(env, this, offID);
133 jint this_len = (*env)->GetIntField(env, this, lenID);
134 jbyte *in_buf;
135 jbyte *out_buf;
136 int res;
137 if ((*env)->GetBooleanField(env, this, setParamsID)) {
138 int level = (*env)->GetIntField(env, this, levelID);
139 int strategy = (*env)->GetIntField(env, this, strategyID);
140 in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
141 if (in_buf == NULL) {
142 // Throw OOME only when length is not zero
143 if (this_len != 0 && (*env)->ExceptionOccurred(env) == NULL)
144 JNU_ThrowOutOfMemoryError(env, 0);
145 return 0;
146 }
147 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
148 if (out_buf == NULL) {
149 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
150 if (len != 0 && (*env)->ExceptionOccurred(env) == NULL)
151 JNU_ThrowOutOfMemoryError(env, 0);
152 return 0;
153 }
154
155 strm->next_in = (Bytef *) (in_buf + this_off);
156 strm->next_out = (Bytef *) (out_buf + off);
157 strm->avail_in = this_len;
158 strm->avail_out = len;
159 res = deflateParams(strm, level, strategy);
160 (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
161 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
162
163 switch (res) {
164 case Z_OK:
165 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
166 this_off += this_len - strm->avail_in;
167 (*env)->SetIntField(env, this, offID, this_off);
168 (*env)->SetIntField(env, this, lenID, strm->avail_in);
169 return (jint) (len - strm->avail_out);
170 case Z_BUF_ERROR:
171 (*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
172 return 0;
173 default:
174 JNU_ThrowInternalError(env, strm->msg);
175 return 0;
176 }
177 } else {
178 jboolean finish = (*env)->GetBooleanField(env, this, finishID);
179 in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
180 if (in_buf == NULL) {
181 if (this_len != 0)
182 JNU_ThrowOutOfMemoryError(env, 0);
183 return 0;
184 }
185 out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
186 if (out_buf == NULL) {
187 (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
188 if (len != 0)
189 JNU_ThrowOutOfMemoryError(env, 0);
|