1 /*
2  * Copyright (c) 1997, 2013, 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 <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <nativehelper/JNIHelp.h>
35 #include "jlong.h"
36 #include "jni.h"
37 #include "jvm.h"
38 #include "jni_util.h"
39 #include <zlib.h>
40 
41 #define NATIVE_METHOD(className, functionName, signature) \
42 { #functionName, signature, (void*)(className ## _ ## functionName) }
43 
44 #define ThrowDataFormatException(env, msg) \
45         JNU_ThrowByName(env, "java/util/zip/DataFormatException", msg)
46 
47 static jfieldID needDictID;
48 static jfieldID finishedID;
49 static jfieldID bufID, offID, lenID;
50 
Inflater_initIDs(JNIEnv * env)51 static void Inflater_initIDs(JNIEnv *env) {
52     jclass cls = (*env)->FindClass(env, "java/util/zip/Inflater");
53     needDictID = (*env)->GetFieldID(env, cls, "needDict", "Z");
54     finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
55     bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
56     offID = (*env)->GetFieldID(env, cls, "off", "I");
57     lenID = (*env)->GetFieldID(env, cls, "len", "I");
58 }
59 
60 JNIEXPORT jlong JNICALL
Inflater_init(JNIEnv * env,jclass cls,jboolean nowrap)61 Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap)
62 {
63     z_stream *strm = calloc(1, sizeof(z_stream));
64 
65     if (strm == NULL) {
66         JNU_ThrowOutOfMemoryError(env, 0);
67         return jlong_zero;
68     } else {
69         const char *msg;
70         int ret = inflateInit2(strm, nowrap ? -MAX_WBITS : MAX_WBITS);
71         switch (ret) {
72           case Z_OK:
73             return ptr_to_jlong(strm);
74           case Z_MEM_ERROR:
75             free(strm);
76             JNU_ThrowOutOfMemoryError(env, 0);
77             return jlong_zero;
78           default:
79             msg = ((strm->msg != NULL) ? strm->msg :
80                    (ret == Z_VERSION_ERROR) ?
81                    "zlib returned Z_VERSION_ERROR: "
82                    "compile time and runtime zlib implementations differ" :
83                    (ret == Z_STREAM_ERROR) ?
84                    "inflateInit2 returned Z_STREAM_ERROR" :
85                    "unknown error initializing zlib library");
86             free(strm);
87             JNU_ThrowInternalError(env, msg);
88             return jlong_zero;
89         }
90     }
91 }
92 
93 JNIEXPORT void JNICALL
Inflater_setDictionary(JNIEnv * env,jclass cls,jlong addr,jarray b,jint off,jint len)94 Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
95                                           jarray b, jint off, jint len)
96 {
97     Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
98     int res;
99     if (buf == 0) /* out of memory */
100         return;
101     res = inflateSetDictionary(jlong_to_ptr(addr), buf + off, len);
102     (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
103     switch (res) {
104     case Z_OK:
105         break;
106     case Z_STREAM_ERROR:
107     case Z_DATA_ERROR:
108         JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg);
109         break;
110     default:
111         JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
112         break;
113     }
114 }
115 
116 JNIEXPORT jint JNICALL
Inflater_inflateBytes(JNIEnv * env,jobject this,jlong addr,jarray b,jint off,jint len)117 Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,
118                                          jarray b, jint off, jint len)
119 {
120     z_stream *strm = jlong_to_ptr(addr);
121     jarray this_buf = (jarray)(*env)->GetObjectField(env, this, bufID);
122     jint this_off = (*env)->GetIntField(env, this, offID);
123     jint this_len = (*env)->GetIntField(env, this, lenID);
124 
125     jbyte *in_buf;
126     jbyte *out_buf;
127     int ret;
128 
129     in_buf  = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
130     if (in_buf == NULL) {
131         if (this_len != 0)
132             JNU_ThrowOutOfMemoryError(env, 0);
133         return 0;
134     }
135     out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
136     if (out_buf == NULL) {
137         (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
138         if (len != 0)
139             JNU_ThrowOutOfMemoryError(env, 0);
140         return 0;
141     }
142     strm->next_in  = (Bytef *) (in_buf + this_off);
143     strm->next_out = (Bytef *) (out_buf + off);
144     strm->avail_in  = this_len;
145     strm->avail_out = len;
146     ret = inflate(strm, Z_PARTIAL_FLUSH);
147     (*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
148     (*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
149 
150     switch (ret) {
151     case Z_STREAM_END:
152         (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
153         /* fall through */
154     case Z_OK:
155         this_off += this_len - strm->avail_in;
156         (*env)->SetIntField(env, this, offID, this_off);
157         (*env)->SetIntField(env, this, lenID, strm->avail_in);
158         return len - strm->avail_out;
159     case Z_NEED_DICT:
160         (*env)->SetBooleanField(env, this, needDictID, JNI_TRUE);
161         /* Might have consumed some input here! */
162         this_off += this_len - strm->avail_in;
163         (*env)->SetIntField(env, this, offID, this_off);
164         (*env)->SetIntField(env, this, lenID, strm->avail_in);
165         return 0;
166     case Z_BUF_ERROR:
167         return 0;
168     case Z_DATA_ERROR:
169         ThrowDataFormatException(env, strm->msg);
170         return 0;
171     case Z_MEM_ERROR:
172         JNU_ThrowOutOfMemoryError(env, 0);
173         return 0;
174     default:
175         JNU_ThrowInternalError(env, strm->msg);
176         return 0;
177     }
178 }
179 
180 JNIEXPORT jint JNICALL
Inflater_getAdler(JNIEnv * env,jclass cls,jlong addr)181 Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
182 {
183     return ((z_stream *)jlong_to_ptr(addr))->adler;
184 }
185 
186 JNIEXPORT void JNICALL
Inflater_reset(JNIEnv * env,jclass cls,jlong addr)187 Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
188 {
189     if (inflateReset(jlong_to_ptr(addr)) != Z_OK) {
190         JNU_ThrowInternalError(env, 0);
191     }
192 }
193 
194 JNIEXPORT void JNICALL
Inflater_end(JNIEnv * env,jclass cls,jlong addr)195 Inflater_end(JNIEnv *env, jclass cls, jlong addr)
196 {
197     if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
198         JNU_ThrowInternalError(env, 0);
199     } else {
200         free(jlong_to_ptr(addr));
201     }
202 }
203 
204 static JNINativeMethod gMethods[] = {
205   NATIVE_METHOD(Inflater, init, "(Z)J"),
206   NATIVE_METHOD(Inflater, setDictionary, "(J[BII)V"),
207   NATIVE_METHOD(Inflater, inflateBytes, "(J[BII)I"),
208   NATIVE_METHOD(Inflater, getAdler, "(J)I"),
209   NATIVE_METHOD(Inflater, reset, "(J)V"),
210   NATIVE_METHOD(Inflater, end, "(J)V"),
211 };
212 
register_java_util_zip_Inflater(JNIEnv * env)213 void register_java_util_zip_Inflater(JNIEnv* env) {
214     jniRegisterNativeMethods(env, "java/util/zip/Inflater", gMethods, NELEM(gMethods));
215 
216     Inflater_initIDs(env);
217 }
218