3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

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 JNIEXPORT jlong JNICALL
Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env, Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
jobject obj, jclass clazz,
jbyteArray key, jbyteArray key,
jboolean encrypt) jboolean encrypt)
{ {
@ -41,7 +41,7 @@ Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_init(JNIEnv *env,
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_free(JNIEnv *env, Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_free(JNIEnv *env,
jobject obj, jclass clazz,
jlong ptr) jlong ptr)
{ {
EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *) 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 JNIEXPORT void JNICALL
Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_process(JNIEnv *env, Java_com_velocitypowered_natives_encryption_OpenSslCipherImpl_process(JNIEnv *env,
jobject obj, jclass clazz,
jlong ptr, jlong ptr,
jlong source, jlong source,
jint len, jint len,

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

@ -5,10 +5,10 @@ package com.velocitypowered.natives.compression;
*/ */
class NativeZlibDeflate { 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); int destinationLength);
} }

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

@ -4,9 +4,9 @@ import java.security.GeneralSecurityException;
class OpenSslCipherImpl { 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);
} }