1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 17:59:33 +02:00
FBReaderJ/native/sources/DeflatingDecompressor/DeflatingDecompressor.cpp
Nikolay Pultsin 4d7ea131fe cleanup
git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@991 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
2009-06-30 08:19:12 +00:00

71 lines
1.7 KiB
C++

#include <jni.h>
#include <string.h>
#include <zlib.h>
#include <new>
#define SIZE 10
static jobject keys[SIZE] = { 0 };
static z_stream* values[SIZE] = { 0 };
extern "C"
jboolean Java_org_amse_ys_zip_NativeDeflatingDecompressor_startInflating(JNIEnv *env, jobject thiz) {
int i;
for (i = 0; i < SIZE; ++i) {
if (keys[i] == 0) {
keys[i] = thiz;
values[i] = new z_stream;
memset(values[i], 0, sizeof(z_stream));
inflateInit2(values[i], -MAX_WBITS);
return 1;
}
}
return 0;
}
extern "C"
void Java_org_amse_ys_zip_NativeDeflatingDecompressor_endInflating(JNIEnv *env, jobject thiz) {
int i;
for (i = 0; i < SIZE; ++i) {
if (keys[i] == thiz) {
keys[i] = 0;
inflateEnd(values[i]);
delete values[i];
values[i] = 0;
break;
}
}
}
// returns ((used inLength) << 16) + utLength
extern "C"
jint Java_org_amse_ys_zip_NativeDeflatingDecompressor_inflate(JNIEnv *env, jobject thiz, jbyteArray in, jint inOffset, jint inLength, jbyteArray out) {
int i;
z_stream *stream = 0;
for (i = 0; i < SIZE; ++i) {
if (keys[i] == thiz) {
stream = values[i];
break;
}
}
if (stream == 0) {
return 0;
}
jbyte* inStart = env->GetByteArrayElements(in, 0);
jbyte* outStart = env->GetByteArrayElements(out, 0);
stream->next_in = (Bytef*)inStart + inOffset;
stream->avail_in = inLength;
stream->next_out = (Bytef*)outStart;
const int outLength = env->GetArrayLength(out);
stream->avail_out = outLength;
const int code = inflate(stream, Z_SYNC_FLUSH);
env->ReleaseByteArrayElements(in, inStart, 0);
env->ReleaseByteArrayElements(out, outStart, 0);
if ((code == Z_OK) || (code == Z_STREAM_END)) {
return ((inLength - stream->avail_in) << 16) + outLength - stream->avail_out;
}
return 0;
}