13
0
geforkt von Mirrors/Velocity

Prettify JNI stuff.

Dieser Commit ist enthalten in:
Andrew Steinborn 2019-01-11 14:13:13 -05:00
Ursprung 1ddeb85f60
Commit 9daa6c3a64
9 geänderte Dateien mit 87 neuen und 76 gelöschten Zeilen

Datei anzeigen

@ -6,7 +6,7 @@ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Hom
CFLAGS="-O3 -I$JAVA_HOME/include/ -I$JAVA_HOME/include/darwin/ -fPIC -shared"
clang $CFLAGS -lz src/main/c/jni_util.c src/main/c/jni_zlib_deflate.c src/main/c/jni_zlib_inflate.c \
-o src/main/resources/macosx/velocity-compress.dylib
src/main/c/jni_zlib_common.c -o src/main/resources/macosx/velocity-compress.dylib
clang $CFLAGS -I $MBEDTLS_ROOT/include -shared $MBEDTLS_ROOT/library/aes.c $MBEDTLS_ROOT/library/aesni.c \
$MBEDTLS_ROOT/library/platform.c $MBEDTLS_ROOT/library/platform_util.c src/main/c/jni_util.c src/main/c/jni_cipher.c \
-o src/main/resources/macosx/velocity-cipher.dylib

Datei anzeigen

@ -1,7 +1,7 @@
#include <jni.h>
#include "jni_util.h"
JNIEXPORT void JNICALL
void JNICALL
throwException(JNIEnv *env, const char *type, const char *msg)
{
jclass klazz = (*env)->FindClass(env, type);

Datei anzeigen

@ -0,0 +1,29 @@
#include <jni.h>
#include <stdbool.h>
#include <stdlib.h>
#include <zlib.h>
#include "jni_util.h"
void JNICALL
check_zlib_free(JNIEnv *env, z_stream *stream, bool deflate)
{
int ret = deflate ? deflateEnd(stream) : inflateEnd(stream);
char *msg = stream->msg;
free((void*) stream);
switch (ret) {
case Z_OK:
break;
case Z_STREAM_ERROR:
if (msg == NULL) {
msg = "stream state inconsistent";
}
// fall-through
case Z_DATA_ERROR:
if (msg == NULL) {
msg = "data was discarded";
}
throwException(env, "java/lang/IllegalArgumentException", msg);
break;
}
}

Datei anzeigen

@ -0,0 +1,6 @@
#include <jni.h>
#include <stdbool.h>
#include <zlib.h>
void JNICALL
check_zlib_free(JNIEnv *env, z_stream *stream, bool deflate);

Datei anzeigen

@ -1,8 +1,10 @@
#include <assert.h>
#include <jni.h>
#include <stdbool.h>
#include <stdlib.h>
#include <zlib.h>
#include "jni_util.h"
#include "jni_zlib_common.h"
static jfieldID finishedID;
static jfieldID consumedID;
@ -27,27 +29,28 @@ Java_com_velocitypowered_natives_compression_NativeZlibDeflate_init(JNIEnv *env,
return 0;
}
char *msg;
int ret = deflateInit(stream, level);
switch (ret) {
case Z_OK:
return (jlong) stream;
case Z_MEM_ERROR:
free(stream);
throwException(env, "java/lang/OutOfMemoryError", "zlib init");
return 0;
case Z_STREAM_ERROR:
free(stream);
char message[32];
snprintf(message, 32, "invalid level %d", level);
throwException(env, "java/lang/IllegalArgumentException", message);
return 0;
default:
msg = stream->msg;
free(stream);
throwException(env, "java/util/zip/DataFormatException", msg);
return 0;
if (ret == Z_OK) {
return (jlong) stream;
} else {
char *zlib_msg = stream->msg;
free(stream);
switch (ret) {
case Z_MEM_ERROR:
throwException(env, "java/lang/OutOfMemoryError", "zlib init");
break;
case Z_STREAM_ERROR: {
// Thanks Ken and Ritchie!
char message[32];
snprintf(message, 32, "invalid level %d", level);
throwException(env, "java/lang/IllegalArgumentException", message);
break;
}
default:
throwException(env, "java/util/zip/DataFormatException", zlib_msg);
break;
}
return 0;
}
}
@ -57,24 +60,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibDeflate_free(JNIEnv *env,
jlong ctx)
{
z_stream* stream = (z_stream*) ctx;
int ret = deflateEnd(stream);
char *msg = stream->msg;
free((void*) ctx);
switch (ret) {
case Z_OK:
break;
case Z_STREAM_ERROR:
if (msg == NULL) {
msg = "stream state inconsistent";
}
case Z_DATA_ERROR:
if (msg == NULL) {
msg = "data was discarded";
}
throwException(env, "java/lang/IllegalArgumentException", msg);
break;
}
check_zlib_free(env, stream, true);
}
JNIEXPORT int JNICALL

Datei anzeigen

@ -1,8 +1,10 @@
#include <assert.h>
#include <jni.h>
#include <stdbool.h>
#include <stdlib.h>
#include <zlib.h>
#include "jni_util.h"
#include "jni_zlib_common.h"
static jfieldID finishedID;
static jfieldID consumedID;
@ -26,25 +28,23 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_init(JNIEnv *env,
return 0;
}
char *msg;
int ret = inflateInit(stream);
switch (ret) {
case Z_OK:
return (jlong) stream;
case Z_MEM_ERROR:
free(stream);
throwException(env, "java/lang/OutOfMemoryError", "zlib init");
return 0;
case Z_STREAM_ERROR:
free(stream);
throwException(env, "java/lang/IllegalArgumentException", "stream clobbered?");
return 0;
default:
msg = stream->msg;
free(stream);
throwException(env, "java/util/zip/DataFormatException", msg);
return 0;
if (ret == Z_OK) {
return (jlong) stream;
} else {
char *zlib_msg = stream->msg;
free(stream);
switch (ret) {
case Z_MEM_ERROR:
throwException(env, "java/lang/OutOfMemoryError", "zlib init");
return 0;
case Z_STREAM_ERROR:
throwException(env, "java/lang/IllegalArgumentException", "stream clobbered?");
return 0;
default:
throwException(env, "java/util/zip/DataFormatException", zlib_msg);
return 0;
}
}
}
@ -54,24 +54,7 @@ Java_com_velocitypowered_natives_compression_NativeZlibInflate_free(JNIEnv *env,
jlong ctx)
{
z_stream* stream = (z_stream*) ctx;
int ret = inflateEnd(stream);
char *msg = stream->msg;
free((void*) ctx);
switch (ret) {
case Z_OK:
break;
case Z_STREAM_ERROR:
if (msg == NULL) {
msg = "stream state inconsistent";
}
case Z_DATA_ERROR:
if (msg == NULL) {
msg = "data was discarded";
}
throwException(env, "java/lang/IllegalArgumentException", msg);
break;
}
check_zlib_free(env, stream, false);
}
JNIEXPORT int JNICALL

Datei anzeigen

@ -1,5 +1,6 @@
package com.velocitypowered.natives.compression;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.condition.OS.LINUX;
@ -28,6 +29,12 @@ class VelocityCompressorTest {
new Random(1).nextBytes(TEST_DATA);
}
@Test
@EnabledOnOs({MAC, LINUX})
void sanityCheckNative() {
assertThrows(IllegalArgumentException.class, () -> Natives.compress.get().create(-42));
}
@Test
@EnabledOnOs({MAC, LINUX})
void nativeIntegrityCheck() throws DataFormatException {