12e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root/* 22e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * Licensed to the Apache Software Foundation (ASF) under one or more 32e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * contributor license agreements. See the NOTICE file distributed with 42e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * this work for additional information regarding copyright ownership. 52e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * The ASF licenses this file to You under the Apache License, Version 2.0 62e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * (the "License"); you may not use this file except in compliance with 72e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * the License. You may obtain a copy of the License at 82e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * 92e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * http://www.apache.org/licenses/LICENSE-2.0 102e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * 112e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * Unless required by applicable law or agreed to in writing, software 122e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * distributed under the License is distributed on an "AS IS" BASIS, 132e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 142e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * See the License for the specific language governing permissions and 152e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root * limitations under the License. 162e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root */ 172e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root 18b3aacde3d8af759ee4a7b395c636ea360547d92dIan Rogers#include <memory> 19b3aacde3d8af759ee4a7b395c636ea360547d92dIan Rogers 202e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root#include "JniConstants.h" 212e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root#include "JniException.h" 222e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root#include "ZipUtilities.h" 232e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root 242e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Rootvoid throwExceptionForZlibError(JNIEnv* env, const char* exceptionClassName, int error, 252e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root NativeZipStream* stream) { 262e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root if (error == Z_MEM_ERROR) { 272e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root jniThrowOutOfMemoryError(env, NULL); 282e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } else if (stream != NULL && stream->stream.msg != NULL) { 292e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root jniThrowException(env, exceptionClassName, stream->stream.msg); 302e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } else { 312e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root jniThrowException(env, exceptionClassName, zError(error)); 322e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } 332e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root} 342e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root 355ccb2b3621f633708eb3ae1fd0f27f88bdb32a9dNarayan KamathNativeZipStream::NativeZipStream() : inCap(0), totalIn(0), totalOut(0) { 362e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root // Let zlib use its default allocator. 372e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root stream.opaque = Z_NULL; 382e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root stream.zalloc = Z_NULL; 392e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root stream.zfree = Z_NULL; 402e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root} 412e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root 422e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny RootNativeZipStream::~NativeZipStream() { 432e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root} 442e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root 452e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Rootvoid NativeZipStream::setDictionary(JNIEnv* env, jbyteArray javaDictionary, int off, int len, 462e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root bool inflate) { 47b3aacde3d8af759ee4a7b395c636ea360547d92dIan Rogers std::unique_ptr<jbyte[]> dictionaryBytes(new jbyte[len]); 482e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root if (dictionaryBytes.get() == NULL) { 492e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root jniThrowOutOfMemoryError(env, NULL); 502e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root return; 512e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } 522e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root env->GetByteArrayRegion(javaDictionary, off, len, &dictionaryBytes[0]); 532e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root const Bytef* dictionary = reinterpret_cast<const Bytef*>(&dictionaryBytes[0]); 542e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root int err; 552e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root if (inflate) { 562e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root err = inflateSetDictionary(&stream, dictionary, len); 572e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } else { 582e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root err = deflateSetDictionary(&stream, dictionary, len); 592e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } 602e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root if (err != Z_OK) { 612e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root throwExceptionForZlibError(env, "java/lang/IllegalArgumentException", err, NULL); 622e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root return; 632e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } 642e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root mDict.reset(dictionaryBytes.release()); 652e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root} 662e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root 672e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Rootvoid NativeZipStream::setInput(JNIEnv* env, jbyteArray buf, jint off, jint len) { 682e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root input.reset(new jbyte[len]); 692e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root if (input.get() == NULL) { 702e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root inCap = 0; 712e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root jniThrowOutOfMemoryError(env, NULL); 722e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root return; 732e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } 742e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root inCap = len; 752e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root if (buf != NULL) { 762e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root env->GetByteArrayRegion(buf, off, len, &input[0]); 772e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root } 782e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root stream.next_in = reinterpret_cast<Bytef*>(&input[0]); 792e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root stream.avail_in = len; 802e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root} 812e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root 822e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny RootNativeZipStream* toNativeZipStream(jlong address) { 832e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root return reinterpret_cast<NativeZipStream*>(static_cast<uintptr_t>(address)); 842e6a64542c52490d89476bfd1b10b1938a8a0e99Kenny Root} 85