< prev index next >

src/java.base/share/native/libzip/Inflater.c

Print this page


   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


  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;


 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) {
   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


  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 doSetDictionary(JNIEnv *env, jlong addr, jbyte *buf, jint len)
  91 {
  92     int res = inflateSetDictionary(jlong_to_ptr(addr), (Bytef *) buf, len);
  93     switch (res) {
  94     case Z_OK:
  95         break;
  96     case Z_STREAM_ERROR:
  97     case Z_DATA_ERROR:
  98         JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg);
  99         break;
 100     default:
 101         JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
 102         break;
 103     }
 104 }
 105 
 106 JNIEXPORT void JNICALL
 107 Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
 108                                           jbyteArray b, jint off, jint len)
 109 {
 110     jbyte *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);

 111     if (buf == NULL) /* out of memory */
 112         return;
 113     doSetDictionary(env, addr, buf + off, len);
 114     (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);

 115 }
 116 
 117 JNIEXPORT void JNICALL
 118 Java_java_util_zip_Inflater_setDictionaryBuffer(JNIEnv *env, jclass cls, jlong addr,
 119                                           jlong bufferAddr, jint len)
 120 {
 121     jbyte *buf = jlong_to_ptr(bufferAddr);
 122     doSetDictionary(env, addr, buf, len);


 123 }
 124 
 125 static jlong doInflate(JNIEnv *env, jobject this, jlong addr,
 126                        jbyte *input, jint inputLen,
 127                        jbyte *output, jint outputLen)
 128 {

 129     z_stream *strm = jlong_to_ptr(addr);
 130     jint inputUsed = 0, outputUsed = 0;
 131     int finished = 0;
 132     int needDict = 0;
 133     int ret;
 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 
 142     switch (ret) {
 143     case Z_STREAM_END:
 144         finished = 1;
 145         /* fall through */
 146     case Z_OK:
 147         inputUsed = inputLen - strm->avail_in;
 148         outputUsed = outputLen - strm->avail_out;
 149         break;
 150     case Z_NEED_DICT:
 151         needDict = 1;
 152         /* Might have consumed some input here! */
 153         inputUsed = inputLen - strm->avail_in;
 154         /* zlib is unclear about whether output may be produced */
 155         outputUsed = outputLen - strm->avail_out;
 156         break;
 157     case Z_BUF_ERROR:
 158         break;
 159     case Z_DATA_ERROR:
 160         inputUsed = inputLen - strm->avail_in;


 163         (*env)->SetIntField(env, this, outputConsumedID, outputUsed);
 164         ThrowDataFormatException(env, strm->msg);
 165         break;
 166     case Z_MEM_ERROR:
 167         JNU_ThrowOutOfMemoryError(env, 0);
 168         break;
 169     default:
 170         JNU_ThrowInternalError(env, strm->msg);
 171         break;
 172     }
 173     return ((jlong)inputUsed) | (((jlong)outputUsed) << 31) | (((jlong)finished) << 62) | (((jlong)needDict) << 63);
 174 }
 175 
 176 JNIEXPORT jlong JNICALL
 177 Java_java_util_zip_Inflater_inflateBytesBytes(JNIEnv *env, jobject this, jlong addr,
 178                                          jbyteArray inputArray, jint inputOff, jint inputLen,
 179                                          jbyteArray outputArray, jint outputOff, jint outputLen)
 180 {
 181     jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0);
 182     jbyte *output;

 183     jlong retVal;
 184 
 185     if (input == NULL) {
 186         if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
 187             JNU_ThrowOutOfMemoryError(env, 0);
 188         return 0L;
 189     }
 190     output = (*env)->GetPrimitiveArrayCritical(env, outputArray, 0);
 191     if (output == NULL) {
 192         (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0);
 193         if (outputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
 194             JNU_ThrowOutOfMemoryError(env, 0);
 195         return 0L;
 196     }
 197 
 198     retVal = doInflate(env, this, addr,
 199             input + inputOff, inputLen,
 200             output + outputOff, outputLen);
 201 
 202     (*env)->ReleasePrimitiveArrayCritical(env, outputArray, output, 0);
 203     (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0);
 204 

 205     return retVal;
 206 }
 207 
 208 JNIEXPORT jlong JNICALL
 209 Java_java_util_zip_Inflater_inflateBytesBuffer(JNIEnv *env, jobject this, jlong addr,
 210                                          jbyteArray inputArray, jint inputOff, jint inputLen,
 211                                          jlong outputBuffer, jint outputLen)
 212 {
 213     jbyte *input = (*env)->GetPrimitiveArrayCritical(env, inputArray, 0);
 214     jbyte *output;

 215     jlong retVal;
 216 
 217     if (input == NULL) {
 218         if (inputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
 219             JNU_ThrowOutOfMemoryError(env, 0);
 220         return 0L;
 221     }
 222     output = jlong_to_ptr(outputBuffer);
 223 
 224     retVal = doInflate(env, this, addr,
 225             input + inputOff, inputLen,
 226             output, outputLen);
 227 
 228     (*env)->ReleasePrimitiveArrayCritical(env, inputArray, input, 0);

 229 
 230     return retVal;
 231 }
 232 
 233 JNIEXPORT jlong JNICALL
 234 Java_java_util_zip_Inflater_inflateBufferBytes(JNIEnv *env, jobject this, jlong addr,
 235                                          jlong inputBuffer, jint inputLen,
 236                                          jbyteArray outputArray, jint outputOff, jint outputLen)
 237 {
 238     jbyte *input = jlong_to_ptr(inputBuffer);
 239     jbyte *output = (*env)->GetPrimitiveArrayCritical(env, outputArray, 0);

 240     jlong retVal;
 241 
 242     if (output == NULL) {
 243         if (outputLen != 0 && (*env)->ExceptionOccurred(env) == NULL)
 244             JNU_ThrowOutOfMemoryError(env, 0);
 245         return 0L;
 246     }
 247 
 248     retVal = doInflate(env, this, addr,
 249             input, inputLen,
 250             output + outputOff, outputLen);
 251 
 252     (*env)->ReleasePrimitiveArrayCritical(env, outputArray, output, 0);

 253 
 254     return retVal;
 255 }
 256 
 257 JNIEXPORT jlong JNICALL
 258 Java_java_util_zip_Inflater_inflateBufferBuffer(JNIEnv *env, jobject this, jlong addr,
 259                                          jlong inputBuffer, jint inputLen,
 260                                          jlong outputBuffer, jint outputLen)
 261 {
 262     jbyte *input = jlong_to_ptr(inputBuffer);
 263     jbyte *output = jlong_to_ptr(outputBuffer);


 264 
 265     return doInflate(env, this, addr,
 266             input, inputLen,
 267             output, outputLen);
 268 }
 269 
 270 JNIEXPORT jint JNICALL
 271 Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
 272 {
 273     return ((z_stream *)jlong_to_ptr(addr))->adler;
 274 }
 275 
 276 JNIEXPORT void JNICALL
 277 Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
 278 {
 279     if (inflateReset(jlong_to_ptr(addr)) != Z_OK) {
 280         JNU_ThrowInternalError(env, 0);
 281     }
 282 }
 283 
 284 JNIEXPORT void JNICALL
 285 Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr)
 286 {
 287     if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
< prev index next >