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 #if !defined(zip_h)
19 #define zip_h
20
21 #include "JNIHelp.h"
22 #include "JniException.h"
23 #include "UniquePtr.h"
24 #include "jni.h"
25 #include "zlib.h"
26 #include "zutil.h"
27
throwExceptionForZlibError(JNIEnv * env,const char * exceptionClassName,int error)28 static void throwExceptionForZlibError(JNIEnv* env, const char* exceptionClassName, int error) {
29 if (error == Z_MEM_ERROR) {
30 jniThrowOutOfMemoryError(env, NULL);
31 } else {
32 jniThrowException(env, exceptionClassName, zError(error));
33 }
34 }
35
36 class NativeZipStream {
37 public:
38 UniquePtr<jbyte[]> input;
39 int inCap;
40 z_stream stream;
41
NativeZipStream()42 NativeZipStream() : input(NULL), inCap(0), mDict(NULL) {
43 // Let zlib use its default allocator.
44 stream.opaque = Z_NULL;
45 stream.zalloc = Z_NULL;
46 stream.zfree = Z_NULL;
47 }
48
~NativeZipStream()49 ~NativeZipStream() {
50 }
51
setDictionary(JNIEnv * env,jbyteArray javaDictionary,int off,int len,bool inflate)52 void setDictionary(JNIEnv* env, jbyteArray javaDictionary, int off, int len, bool inflate) {
53 UniquePtr<jbyte[]> dictionaryBytes(new jbyte[len]);
54 if (dictionaryBytes.get() == NULL) {
55 jniThrowOutOfMemoryError(env, NULL);
56 return;
57 }
58 env->GetByteArrayRegion(javaDictionary, off, len, &dictionaryBytes[0]);
59 const Bytef* dictionary = reinterpret_cast<const Bytef*>(&dictionaryBytes[0]);
60 int err;
61 if (inflate) {
62 err = inflateSetDictionary(&stream, dictionary, len);
63 } else {
64 err = deflateSetDictionary(&stream, dictionary, len);
65 }
66 if (err != Z_OK) {
67 throwExceptionForZlibError(env, "java/lang/IllegalArgumentException", err);
68 return;
69 }
70 mDict.reset(dictionaryBytes.release());
71 }
72
setInput(JNIEnv * env,jbyteArray buf,jint off,jint len)73 void setInput(JNIEnv* env, jbyteArray buf, jint off, jint len) {
74 input.reset(new jbyte[len]);
75 if (input.get() == NULL) {
76 inCap = 0;
77 jniThrowOutOfMemoryError(env, NULL);
78 return;
79 }
80 inCap = len;
81 if (buf != NULL) {
82 env->GetByteArrayRegion(buf, off, len, &input[0]);
83 }
84 stream.next_in = reinterpret_cast<Bytef*>(&input[0]);
85 stream.avail_in = len;
86 }
87
88 private:
89 UniquePtr<jbyte[]> mDict;
90
91 // Disallow copy and assignment.
92 NativeZipStream(const NativeZipStream&);
93 void operator=(const NativeZipStream&);
94 };
95
toNativeZipStream(jlong address)96 static NativeZipStream* toNativeZipStream(jlong address) {
97 return reinterpret_cast<NativeZipStream*>(static_cast<uintptr_t>(address));
98 }
99
100 #endif /* zip_h */
101