• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <memory>
19 
20 #include <nativehelper/JniConstants.h>
21 
22 #include "JniException.h"
23 #include "ZipUtilities.h"
24 
throwExceptionForZlibError(JNIEnv * env,const char * exceptionClassName,int error,NativeZipStream * stream)25 void throwExceptionForZlibError(JNIEnv* env, const char* exceptionClassName, int error,
26     NativeZipStream* stream) {
27   if (error == Z_MEM_ERROR) {
28     jniThrowOutOfMemoryError(env, NULL);
29   } else if (stream != NULL && stream->stream.msg != NULL) {
30     jniThrowException(env, exceptionClassName, stream->stream.msg);
31   } else {
32     jniThrowException(env, exceptionClassName, zError(error));
33   }
34 }
35 
NativeZipStream()36 NativeZipStream::NativeZipStream() : inCap(0), totalIn(0), totalOut(0) {
37   // Let zlib use its default allocator.
38   stream.opaque = Z_NULL;
39   stream.zalloc = Z_NULL;
40   stream.zfree = Z_NULL;
41 }
42 
~NativeZipStream()43 NativeZipStream::~NativeZipStream() {
44 }
45 
setDictionary(JNIEnv * env,jbyteArray javaDictionary,int off,int len,bool inflate)46 void NativeZipStream::setDictionary(JNIEnv* env, jbyteArray javaDictionary, int off, int len,
47     bool inflate) {
48   std::unique_ptr<jbyte[]> dictionaryBytes(new jbyte[len]);
49   if (dictionaryBytes.get() == NULL) {
50     jniThrowOutOfMemoryError(env, NULL);
51     return;
52   }
53   env->GetByteArrayRegion(javaDictionary, off, len, &dictionaryBytes[0]);
54   const Bytef* dictionary = reinterpret_cast<const Bytef*>(&dictionaryBytes[0]);
55   int err;
56   if (inflate) {
57     err = inflateSetDictionary(&stream, dictionary, len);
58   } else {
59     err = deflateSetDictionary(&stream, dictionary, len);
60   }
61   if (err != Z_OK) {
62     throwExceptionForZlibError(env, "java/lang/IllegalArgumentException", err, NULL);
63     return;
64   }
65   mDict.reset(dictionaryBytes.release());
66 }
67 
setInput(JNIEnv * env,jbyteArray buf,jint off,jint len)68 void NativeZipStream::setInput(JNIEnv* env, jbyteArray buf, jint off, jint len) {
69   input.reset(new jbyte[len]);
70   if (input.get() == NULL) {
71     inCap = 0;
72     jniThrowOutOfMemoryError(env, NULL);
73     return;
74   }
75   inCap = len;
76   if (buf != NULL) {
77     env->GetByteArrayRegion(buf, off, len, &input[0]);
78   }
79   stream.next_in = reinterpret_cast<Bytef*>(&input[0]);
80   stream.avail_in = len;
81 }
82 
toNativeZipStream(jlong address)83 NativeZipStream* toNativeZipStream(jlong address) {
84   return reinterpret_cast<NativeZipStream*>(static_cast<uintptr_t>(address));
85 }
86