2015-03-13 18:52:59 +00:00
|
|
|
/* Copyright (c) 2008-2015, Avian Contributors
|
2008-02-19 18:06:52 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-09-17 22:15:16 +00:00
|
|
|
#include "stdlib.h"
|
|
|
|
#include "string.h"
|
2013-02-27 20:25:50 +00:00
|
|
|
#include "avian/zlib-custom.h"
|
2007-09-17 22:15:16 +00:00
|
|
|
|
2011-06-01 19:56:03 +00:00
|
|
|
#include "jni.h"
|
2007-09-17 22:15:16 +00:00
|
|
|
#include "jni-util.h"
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT jlong JNICALL
|
2014-07-11 15:50:18 +00:00
|
|
|
Java_java_util_zip_Inflater_make(JNIEnv* e, jclass, jboolean nowrap)
|
2007-09-17 22:15:16 +00:00
|
|
|
{
|
|
|
|
z_stream* s = static_cast<z_stream*>(malloc(sizeof(z_stream)));
|
2009-12-05 22:51:12 +00:00
|
|
|
if (s == 0) {
|
|
|
|
throwNew(e, "java/lang/OutOfMemoryError", 0);
|
2014-07-11 15:50:18 +00:00
|
|
|
return 0;
|
2009-12-05 22:51:12 +00:00
|
|
|
}
|
|
|
|
|
2007-09-17 22:15:16 +00:00
|
|
|
memset(s, 0, sizeof(z_stream));
|
2014-07-11 15:50:18 +00:00
|
|
|
|
2007-09-17 22:15:16 +00:00
|
|
|
int r = inflateInit2(s, (nowrap ? -15 : 15));
|
|
|
|
if (r != Z_OK) {
|
|
|
|
free(s);
|
|
|
|
throwNew(e, "java/lang/RuntimeException", zError(r));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<jlong>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL
|
2014-07-11 15:50:18 +00:00
|
|
|
Java_java_util_zip_Inflater_dispose(JNIEnv*, jclass, jlong peer)
|
2007-09-17 22:15:16 +00:00
|
|
|
{
|
|
|
|
z_stream* s = reinterpret_cast<z_stream*>(peer);
|
|
|
|
inflateEnd(s);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL
|
2014-07-11 15:50:18 +00:00
|
|
|
Java_java_util_zip_Inflater_inflate(JNIEnv* e,
|
|
|
|
jclass,
|
|
|
|
jlong peer,
|
|
|
|
jbyteArray input,
|
|
|
|
jint inputOffset,
|
|
|
|
jint inputLength,
|
|
|
|
jbyteArray output,
|
|
|
|
jint outputOffset,
|
|
|
|
jint outputLength,
|
|
|
|
jintArray results)
|
2007-09-17 22:15:16 +00:00
|
|
|
{
|
|
|
|
z_stream* s = reinterpret_cast<z_stream*>(peer);
|
|
|
|
|
|
|
|
jbyte* in = static_cast<jbyte*>(malloc(inputLength));
|
|
|
|
if (in == 0) {
|
|
|
|
throwNew(e, "java/lang/OutOfMemoryError", 0);
|
2014-07-11 15:50:18 +00:00
|
|
|
return;
|
2007-09-17 22:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jbyte* out = static_cast<jbyte*>(malloc(outputLength));
|
|
|
|
if (out == 0) {
|
|
|
|
free(in);
|
|
|
|
throwNew(e, "java/lang/OutOfMemoryError", 0);
|
2014-07-11 15:50:18 +00:00
|
|
|
return;
|
2007-09-17 22:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e->GetByteArrayRegion(input, inputOffset, inputLength, in);
|
2014-07-11 15:50:18 +00:00
|
|
|
|
2007-09-17 22:15:16 +00:00
|
|
|
s->next_in = reinterpret_cast<Bytef*>(in);
|
|
|
|
s->avail_in = inputLength;
|
|
|
|
s->next_out = reinterpret_cast<Bytef*>(out);
|
|
|
|
s->avail_out = outputLength;
|
|
|
|
|
|
|
|
int r = inflate(s, Z_SYNC_FLUSH);
|
2014-07-11 15:50:18 +00:00
|
|
|
jint resultArray[3] = {r,
|
|
|
|
static_cast<jint>(inputLength - s->avail_in),
|
|
|
|
static_cast<jint>(outputLength - s->avail_out)};
|
2007-09-17 22:15:16 +00:00
|
|
|
|
|
|
|
free(in);
|
|
|
|
|
|
|
|
e->SetByteArrayRegion(output, outputOffset, resultArray[2], out);
|
|
|
|
free(out);
|
|
|
|
|
|
|
|
e->SetIntArrayRegion(results, 0, 3, resultArray);
|
|
|
|
}
|
2009-12-04 19:01:31 +00:00
|
|
|
|
|
|
|
extern "C" JNIEXPORT jlong JNICALL
|
2014-07-11 15:50:18 +00:00
|
|
|
Java_java_util_zip_Deflater_make(JNIEnv* e,
|
|
|
|
jclass,
|
|
|
|
jboolean nowrap,
|
|
|
|
jint level)
|
2009-12-04 19:01:31 +00:00
|
|
|
{
|
|
|
|
z_stream* s = static_cast<z_stream*>(malloc(sizeof(z_stream)));
|
2009-12-05 22:51:12 +00:00
|
|
|
if (s == 0) {
|
|
|
|
throwNew(e, "java/lang/OutOfMemoryError", 0);
|
2014-07-11 15:50:18 +00:00
|
|
|
return 0;
|
2009-12-05 22:51:12 +00:00
|
|
|
}
|
|
|
|
|
2009-12-04 19:01:31 +00:00
|
|
|
memset(s, 0, sizeof(z_stream));
|
2014-07-11 15:50:18 +00:00
|
|
|
|
2009-12-04 19:01:31 +00:00
|
|
|
int r = deflateInit2(s, level, (nowrap ? -15 : 15));
|
|
|
|
if (r != Z_OK) {
|
|
|
|
free(s);
|
|
|
|
throwNew(e, "java/lang/RuntimeException", zError(r));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<jlong>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL
|
2014-07-11 15:50:18 +00:00
|
|
|
Java_java_util_zip_Deflater_dispose(JNIEnv*, jclass, jlong peer)
|
2009-12-04 19:01:31 +00:00
|
|
|
{
|
|
|
|
z_stream* s = reinterpret_cast<z_stream*>(peer);
|
|
|
|
deflateEnd(s);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL
|
2014-07-11 15:50:18 +00:00
|
|
|
Java_java_util_zip_Deflater_deflate(JNIEnv* e,
|
|
|
|
jclass,
|
|
|
|
jlong peer,
|
|
|
|
jbyteArray input,
|
|
|
|
jint inputOffset,
|
|
|
|
jint inputLength,
|
|
|
|
jbyteArray output,
|
|
|
|
jint outputOffset,
|
|
|
|
jint outputLength,
|
|
|
|
jboolean finish,
|
|
|
|
jintArray results)
|
2009-12-04 19:01:31 +00:00
|
|
|
{
|
|
|
|
z_stream* s = reinterpret_cast<z_stream*>(peer);
|
|
|
|
|
|
|
|
jbyte* in = static_cast<jbyte*>(malloc(inputLength));
|
|
|
|
if (in == 0) {
|
|
|
|
throwNew(e, "java/lang/OutOfMemoryError", 0);
|
2014-07-11 15:50:18 +00:00
|
|
|
return;
|
2009-12-04 19:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jbyte* out = static_cast<jbyte*>(malloc(outputLength));
|
|
|
|
if (out == 0) {
|
|
|
|
free(in);
|
|
|
|
throwNew(e, "java/lang/OutOfMemoryError", 0);
|
2014-07-11 15:50:18 +00:00
|
|
|
return;
|
2009-12-04 19:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e->GetByteArrayRegion(input, inputOffset, inputLength, in);
|
2014-07-11 15:50:18 +00:00
|
|
|
|
2009-12-04 19:01:31 +00:00
|
|
|
s->next_in = reinterpret_cast<Bytef*>(in);
|
|
|
|
s->avail_in = inputLength;
|
|
|
|
s->next_out = reinterpret_cast<Bytef*>(out);
|
|
|
|
s->avail_out = outputLength;
|
|
|
|
|
2011-05-16 23:12:41 +00:00
|
|
|
int r = deflate(s, finish ? Z_FINISH : Z_NO_FLUSH);
|
2014-07-11 15:50:18 +00:00
|
|
|
jint resultArray[3] = {r,
|
|
|
|
static_cast<jint>(inputLength - s->avail_in),
|
|
|
|
static_cast<jint>(outputLength - s->avail_out)};
|
2009-12-04 19:01:31 +00:00
|
|
|
|
|
|
|
free(in);
|
|
|
|
|
|
|
|
e->SetByteArrayRegion(output, outputOffset, resultArray[2], out);
|
|
|
|
free(out);
|
|
|
|
|
|
|
|
e->SetIntArrayRegion(results, 0, 3, resultArray);
|
|
|
|
}
|