|
| 1 | +#include "com_intel_qat_jni_CarbondataQatJNI.h" |
| 2 | +#include "qatzip.h" |
| 3 | + |
| 4 | +#include <sys/syscall.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <dlfcn.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <stdint.h> |
| 10 | + |
| 11 | +#define QAT_ZIP_LIBRARY_NAME "libqatzip.so" |
| 12 | + |
| 13 | +/* A helper macro to 'throw' a java exception. */ |
| 14 | +#define THROW(env, exception_name, message) \ |
| 15 | +{ \ |
| 16 | + jclass ecls = (*env)->FindClass(env, exception_name); \ |
| 17 | + if (ecls) { \ |
| 18 | + (*env)->ThrowNew(env, ecls, message); \ |
| 19 | + (*env)->DeleteLocalRef(env, ecls); \ |
| 20 | + } \ |
| 21 | +} |
| 22 | + |
| 23 | +typedef int (*dlsym_qzCompress)(QzSession_T *sess, const unsigned char* src, |
| 24 | + unsigned int* src_len, unsigned char* dest, unsigned int* dest_len, |
| 25 | + unsigned int last); |
| 26 | +typedef int (*dlsym_qzDecompress)(QzSession_T *sess, const unsigned char* src, |
| 27 | + unsigned int* compressed_buf_len, unsigned char* dest, |
| 28 | + unsigned int* uncompressed_buffer_len); |
| 29 | +typedef int (*dlsym_qzGetDefaults)(QzSessionParams_T *defaults); |
| 30 | +typedef unsigned int (*dlsym_qzMaxCompressedLength)(unsigned int src_sz); |
| 31 | + |
| 32 | + |
| 33 | +typedef struct qat_wrapper_context { |
| 34 | + dlsym_qzCompress compress; |
| 35 | + dlsym_qzDecompress decompress; |
| 36 | + dlsym_qzGetDefaults getDefaults; |
| 37 | + dlsym_qzMaxCompressedLength maxCompressedLength; |
| 38 | +} qat_wrapper_context_t; |
| 39 | + |
| 40 | +qat_wrapper_context_t g_qat_wrapper_context; |
| 41 | + |
| 42 | + |
| 43 | +__thread QzSession_T g_qzSession = { |
| 44 | + .internal = NULL, |
| 45 | +}; |
| 46 | + |
| 47 | +JNIEXPORT void JNICALL Java_com_intel_qat_jni_CarbondataQatJNI_init |
| 48 | + (JNIEnv *env, jclass cls) { |
| 49 | + |
| 50 | + qat_wrapper_context_t *qat_wrapper_context = &g_qat_wrapper_context; |
| 51 | + void *lib = dlopen(QAT_ZIP_LIBRARY_NAME, RTLD_LAZY | RTLD_GLOBAL); |
| 52 | + if (!lib){ |
| 53 | + char msg[128]; |
| 54 | + snprintf(msg, 128, "Can't load %s due to %s", QAT_ZIP_LIBRARY_NAME, dlerror()); |
| 55 | + THROW(env, "java/lang/UnsatisfiedLinkError", msg); |
| 56 | + } |
| 57 | + |
| 58 | + dlerror(); // Clear any existing error |
| 59 | + |
| 60 | + qat_wrapper_context->compress = dlsym(lib, "qzCompress"); |
| 61 | + if (qat_wrapper_context->compress == NULL) { |
| 62 | + THROW(env, "java/lang/UnsatisfiedLinkError", "Failed to load qzCompress"); |
| 63 | + } |
| 64 | + |
| 65 | + qat_wrapper_context->decompress = dlsym(lib, "qzDecompress"); |
| 66 | + if (qat_wrapper_context->compress == NULL) { |
| 67 | + THROW(env, "java/lang/UnsatisfiedLinkError", "Failed to load qzDecompress"); |
| 68 | + } |
| 69 | + |
| 70 | + qat_wrapper_context->getDefaults = dlsym(lib, "qzGetDefaults"); |
| 71 | + if (qat_wrapper_context->getDefaults == NULL) { |
| 72 | + THROW(env, "java/lang/UnsatisfiedLinkError", "Failed to load qzGetDefaults"); |
| 73 | + } |
| 74 | + |
| 75 | + qat_wrapper_context->maxCompressedLength = dlsym(lib, "qzMaxCompressedLength"); |
| 76 | + if (qat_wrapper_context->maxCompressedLength == NULL) { |
| 77 | + THROW(env, "java/lang/UnsatisfiedLinkError", "why Failed to load qzMaxCompressedLength"); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +JNIEXPORT jint JNICALL Java_com_intel_qat_jni_CarbondataQatJNI_compress |
| 83 | + (JNIEnv *env, jclass cls, jbyteArray src, jint srcLen, jbyteArray des){ |
| 84 | + |
| 85 | + jbyte *in = (*env)->GetByteArrayElements(env, src, 0); |
| 86 | + if (in == NULL) { |
| 87 | + THROW(env, "java/lang/OutOfMemoryError", "Can't get compressor input buffer"); |
| 88 | + } |
| 89 | + |
| 90 | + jbyte *out = (*env)->GetByteArrayElements(env, des, 0); |
| 91 | + if (out == NULL) { |
| 92 | + THROW(env, "java/lang/OutOfMemoryError", "Can't get compressor output buffer"); |
| 93 | + } |
| 94 | + |
| 95 | + uint32_t uncompressed_size = srcLen; |
| 96 | + uint32_t compressed_size = srcLen+1000; |
| 97 | + qat_wrapper_context_t *qat_wrapper_context = &g_qat_wrapper_context; |
| 98 | + int ret = qat_wrapper_context->compress(&g_qzSession, in, &uncompressed_size, out, &compressed_size, 1); |
| 99 | + if (ret == QZ_OK) { |
| 100 | + } |
| 101 | + else if (ret == QZ_PARAMS) { |
| 102 | + THROW(env, "java/lang/InternalError", "Could not compress data. *sess is NULL or member of params is invalid."); |
| 103 | + } |
| 104 | + else if (ret == QZ_FAIL) { |
| 105 | + THROW(env, "java/lang/InternalError", "Could not compress data. Function did not succeed."); |
| 106 | + } |
| 107 | + else { |
| 108 | + char temp[256]; |
| 109 | + sprintf(temp, "Could not compress data. Return error code %d", ret); |
| 110 | + THROW(env, "java/lang/InternalError", temp); |
| 111 | + } |
| 112 | + |
| 113 | + (*env)->ReleaseByteArrayElements(env, src, in, JNI_COMMIT); |
| 114 | + (*env)->ReleaseByteArrayElements(env, des, out, JNI_COMMIT); |
| 115 | + |
| 116 | + return compressed_size; |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +JNIEXPORT jint JNICALL Java_com_intel_qat_jni_CarbondataQatJNI_decompress |
| 121 | + (JNIEnv *env, jclass cls, jbyteArray src, jint srcOff, jint srcLen, jbyteArray des){ |
| 122 | + |
| 123 | + jbyte *in = (*env)->GetByteArrayElements(env, src, 0); |
| 124 | + if (in == NULL) { |
| 125 | + THROW(env, "java/lang/OutOfMemoryError", "Can't get decompressor input buffer"); |
| 126 | + } |
| 127 | + in += srcOff; |
| 128 | + |
| 129 | + jbyte *out = (*env)->GetByteArrayElements(env, des, 0); |
| 130 | + if (out == NULL) { |
| 131 | + THROW(env, "java/lang/OutOfMemoryError", "Can't get decompressor output buffer"); |
| 132 | + } |
| 133 | + |
| 134 | + uint32_t compressed_size = srcLen; |
| 135 | + uint32_t uncompressed_size = srcLen*2; |
| 136 | + |
| 137 | + qat_wrapper_context_t *qat_wrapper_context = &g_qat_wrapper_context; |
| 138 | + int ret = qat_wrapper_context->decompress(&g_qzSession, in, &compressed_size, out, &uncompressed_size); |
| 139 | + if (ret == QZ_OK) { |
| 140 | + } |
| 141 | + else if (ret == QZ_PARAMS) { |
| 142 | + THROW(env, "java/lang/InternalError", "Could not decompress data. *sess is NULL or member of params is invalid"); |
| 143 | + } |
| 144 | + else if (ret == QZ_FAIL) { |
| 145 | + THROW(env, "java/lang/InternalError", "Could not decompress data. Function did not succeed."); |
| 146 | + } |
| 147 | + else { |
| 148 | + char temp[256]; |
| 149 | + sprintf(temp, "Could not decompress data. Return error code %d", ret); |
| 150 | + THROW(env, "java/lang/InternalError", temp); |
| 151 | + } |
| 152 | + |
| 153 | + (*env)->ReleaseByteArrayElements(env, src, in, JNI_COMMIT); |
| 154 | + (*env)->ReleaseByteArrayElements(env, des, out, JNI_COMMIT); |
| 155 | + // if ((*env)->ExceptionCheck(env)){ |
| 156 | + // THROW(env, "java/lang/InternalError", "Could not release array. Function did not succeed."); |
| 157 | + // } |
| 158 | + return uncompressed_size; |
| 159 | +} |
| 160 | + |
| 161 | + |
| 162 | +JNIEXPORT jint JNICALL Java_com_intel_qat_jni_CarbondataQatJNI_maxCompressedLength |
| 163 | + (JNIEnv *env, jclass cls, jint length){ |
| 164 | + |
| 165 | + qat_wrapper_context_t *qat_wrapper_context = &g_qat_wrapper_context; |
| 166 | + uint32_t ret = qat_wrapper_context->maxCompressedLength((uint32_t)length); |
| 167 | + return ret; |
| 168 | +} |
| 169 | + |
0 commit comments