From a49a77a7ef9221b62be89e0e5cddc947bbb984a4 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 11 Sep 2020 01:46:59 -0400 Subject: [PATCH] Working on improvements to natives. Not done yet. --- native/src/main/c/jni_cipher.c | 6 +++--- native/src/main/c/jni_zlib_deflate.c | 6 +++--- native/src/main/c/jni_zlib_inflate.c | 6 +++--- .../compression/LibdeflateVelocityCompressor.java | 14 ++++++-------- .../natives/compression/NativeZlibDeflate.java | 6 +++--- .../natives/compression/NativeZlibInflate.java | 8 ++++---- .../natives/encryption/NativeVelocityCipher.java | 8 +++----- .../natives/encryption/OpenSslCipherImpl.java | 6 +++--- 8 files changed, 28 insertions(+), 32 deletions(-) diff --git a/native/src/main/c/jni_cipher.c b/native/src/main/c/jni_cipher.c index e0b0b84e8..1872865b7 100644 --- a/native/src/main/c/jni_cipher.c +++ b/native/src/main/c/jni_cipher.c @@ -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, diff --git a/native/src/main/c/jni_zlib_deflate.c b/native/src/main/c/jni_zlib_deflate.c index 7a84a5b04..9a954081c 100644 --- a/native/src/main/c/jni_zlib_deflate.c +++ b/native/src/main/c/jni_zlib_deflate.c @@ -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, diff --git a/native/src/main/c/jni_zlib_inflate.c b/native/src/main/c/jni_zlib_inflate.c index 9a81f0db8..d6a0c09df 100644 --- a/native/src/main/c/jni_zlib_inflate.c +++ b/native/src/main/c/jni_zlib_inflate.c @@ -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, diff --git a/native/src/main/java/com/velocitypowered/natives/compression/LibdeflateVelocityCompressor.java b/native/src/main/java/com/velocitypowered/natives/compression/LibdeflateVelocityCompressor.java index e748fd70b..ab3e79108 100644 --- a/native/src/main/java/com/velocitypowered/natives/compression/LibdeflateVelocityCompressor.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/LibdeflateVelocityCompressor.java @@ -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; } diff --git a/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibDeflate.java b/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibDeflate.java index eb89412cb..83cdc5cd1 100644 --- a/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibDeflate.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibDeflate.java @@ -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); } diff --git a/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibInflate.java b/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibInflate.java index fc6e9787f..1c6c42594 100644 --- a/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibInflate.java +++ b/native/src/main/java/com/velocitypowered/natives/compression/NativeZlibInflate.java @@ -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; } diff --git a/native/src/main/java/com/velocitypowered/natives/encryption/NativeVelocityCipher.java b/native/src/main/java/com/velocitypowered/natives/encryption/NativeVelocityCipher.java index bee59ac7a..814971c4e 100644 --- a/native/src/main/java/com/velocitypowered/natives/encryption/NativeVelocityCipher.java +++ b/native/src/main/java/com/velocitypowered/natives/encryption/NativeVelocityCipher.java @@ -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; } diff --git a/native/src/main/java/com/velocitypowered/natives/encryption/OpenSslCipherImpl.java b/native/src/main/java/com/velocitypowered/natives/encryption/OpenSslCipherImpl.java index da5fc6b50..f97eef3df 100644 --- a/native/src/main/java/com/velocitypowered/natives/encryption/OpenSslCipherImpl.java +++ b/native/src/main/java/com/velocitypowered/natives/encryption/OpenSslCipherImpl.java @@ -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); }