13
0
geforkt von Mirrors/Velocity

Working on improvements to natives. Not done yet.

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-09-11 01:46:59 -04:00
Ursprung 7650eedb7a
Commit a49a77a7ef
8 geänderte Dateien mit 28 neuen und 32 gelöschten Zeilen

Datei anzeigen

@ -8,7 +8,7 @@ typedef unsigned char byte;
JNIEXPORT jlong JNICALL
Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
jobject obj,
jclass clazz,
jbyteArray key,
jboolean encrypt)
{
@ -41,7 +41,7 @@ Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
JNIEXPORT void JNICALL
Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_free(JNIEnv *env,
jobject obj,
jclass clazz,
jlong ptr)
{
EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *) ptr);
@ -49,7 +49,7 @@ Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_free(JNIEnv *env,
JNIEXPORT void JNICALL
Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_process(JNIEnv *env,
jobject obj,
jclass clazz,
jlong ptr,
jlong source,
jint len,

Datei anzeigen

@ -7,7 +7,7 @@
JNIEXPORT jlong JNICALL
Java_com_velocitypowered_natives_compression_NativeZlibDeflate_init(JNIEnv *env,
jobject obj,
jclass clazz,
jint level)
{
struct libdeflate_compressor *compressor = libdeflate_alloc_compressor(level);
@ -21,7 +21,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibDeflate_init(JNIEnv *env,
JNIEXPORT void JNICALL
Java_com_velocitypowered_natives_compression_NativeZlibDeflate_free(JNIEnv *env,
jobject obj,
jclass clazz,
jlong ctx)
{
libdeflate_free_compressor((struct libdeflate_compressor *) ctx);
@ -29,7 +29,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibDeflate_free(JNIEnv *env,
JNIEXPORT jboolean JNICALL
Java_com_velocitypowered_natives_compression_NativeZlibDeflate_process(JNIEnv *env,
jobject obj,
jclass clazz,
jlong ctx,
jlong sourceAddress,
jint sourceLength,

Datei anzeigen

@ -7,7 +7,7 @@
JNIEXPORT jlong JNICALL
Java_com_velocitypowered_natives_compression_NativeZlibInflate_init(JNIEnv *env,
jobject obj)
jclass clazz)
{
struct libdeflate_decompressor *decompress = libdeflate_alloc_decompressor();
if (decompress == NULL) {
@ -21,7 +21,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_init(JNIEnv *env,
JNIEXPORT void JNICALL
Java_com_velocitypowered_natives_compression_NativeZlibInflate_free(JNIEnv *env,
jobject obj,
jclass clazz,
jlong ctx)
{
libdeflate_free_decompressor((struct libdeflate_decompressor *) ctx);
@ -29,7 +29,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_free(JNIEnv *env,
JNIEXPORT jboolean JNICALL
Java_com_velocitypowered_natives_compression_NativeZlibInflate_process(JNIEnv *env,
jobject obj,
jclass clazz,
jlong ctx,
jlong sourceAddress,
jint sourceLength,

Datei anzeigen

@ -9,9 +9,7 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
public static final VelocityCompressorFactory FACTORY = LibdeflateVelocityCompressor::new;
private final NativeZlibInflate inflate = new NativeZlibInflate();
private final long inflateCtx;
private final NativeZlibDeflate deflate = new NativeZlibDeflate();
private final long deflateCtx;
private boolean disposed = false;
@ -21,8 +19,8 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
throw new IllegalArgumentException("Invalid compression level " + level);
}
this.inflateCtx = inflate.init();
this.deflateCtx = deflate.init(correctedLevel);
this.inflateCtx = NativeZlibInflate.init();
this.deflateCtx = NativeZlibDeflate.init(correctedLevel);
}
@Override
@ -40,7 +38,7 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
long sourceAddress = source.memoryAddress() + source.readerIndex();
long destinationAddress = destination.memoryAddress() + destination.writerIndex();
inflate.process(inflateCtx, sourceAddress, source.readableBytes(), destinationAddress,
NativeZlibInflate.process(inflateCtx, sourceAddress, source.readableBytes(), destinationAddress,
uncompressedSize);
destination.writerIndex(destination.writerIndex() + uncompressedSize);
}
@ -53,7 +51,7 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
long sourceAddress = source.memoryAddress() + source.readerIndex();
long destinationAddress = destination.memoryAddress() + destination.writerIndex();
int produced = deflate.process(deflateCtx, sourceAddress, source.readableBytes(),
int produced = NativeZlibDeflate.process(deflateCtx, sourceAddress, source.readableBytes(),
destinationAddress, destination.writableBytes());
if (produced > 0) {
destination.writerIndex(destination.writerIndex() + produced);
@ -72,8 +70,8 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
@Override
public void close() {
if (!disposed) {
inflate.free(inflateCtx);
deflate.free(deflateCtx);
NativeZlibInflate.free(inflateCtx);
NativeZlibDeflate.free(deflateCtx);
}
disposed = true;
}

Datei anzeigen

@ -5,10 +5,10 @@ package com.velocitypowered.natives.compression;
*/
class NativeZlibDeflate {
native long init(int level);
static native long init(int level);
native long free(long ctx);
static native long free(long ctx);
native int process(long ctx, long sourceAddress, int sourceLength, long destinationAddress,
static native int process(long ctx, long sourceAddress, int sourceLength, long destinationAddress,
int destinationLength);
}

Datei anzeigen

@ -7,10 +7,10 @@ import java.util.zip.DataFormatException;
*/
class NativeZlibInflate {
native long init();
static native long init();
native long free(long ctx);
static native long free(long ctx);
native boolean process(long ctx, long sourceAddress, int sourceLength, long destinationAddress,
int destinationLength) throws DataFormatException;
static native boolean process(long ctx, long sourceAddress, int sourceLength,
long destinationAddress, int destinationLength) throws DataFormatException;
}

Datei anzeigen

@ -19,13 +19,11 @@ public class NativeVelocityCipher implements VelocityCipher {
return new NativeVelocityCipher(false, key);
}
};
private static final OpenSslCipherImpl impl = new OpenSslCipherImpl();
private final long ctx;
private boolean disposed = false;
private NativeVelocityCipher(boolean encrypt, SecretKey key) throws GeneralSecurityException {
this.ctx = impl.init(key.getEncoded(), encrypt);
this.ctx = OpenSslCipherImpl.init(key.getEncoded(), encrypt);
}
@Override
@ -35,13 +33,13 @@ public class NativeVelocityCipher implements VelocityCipher {
long base = source.memoryAddress() + source.readerIndex();
int len = source.readableBytes();
impl.process(ctx, base, len, base);
OpenSslCipherImpl.process(ctx, base, len, base);
}
@Override
public void close() {
if (!disposed) {
impl.free(ctx);
OpenSslCipherImpl.free(ctx);
}
disposed = true;
}

Datei anzeigen

@ -4,9 +4,9 @@ import java.security.GeneralSecurityException;
class OpenSslCipherImpl {
native long init(byte[] key, boolean encrypt) throws GeneralSecurityException;
static native long init(byte[] key, boolean encrypt) throws GeneralSecurityException;
native void process(long ctx, long source, int len, long dest);
static native void process(long ctx, long source, int len, long dest);
native void free(long ptr);
static native void free(long ptr);
}