release 0.1.15
[liba.git] / java / src / crc64.c
blob0476b2a9d26cc8b7ab9b1bb022cff5f776e6af92
1 #include "a.h"
2 #include "a/crc.h"
3 #include "liba_crc64.h"
5 #undef L
6 #define L Java_liba_crc64
7 static struct
9 jmethodID New;
10 jfieldID ctx;
11 } L = {NULL, NULL};
13 struct crc64
15 a_u64 table[0x100];
16 a_u64 (*eval)(a_u64 const table[0x100], void const *pdata, a_size nbyte, a_u64 value);
19 JNIEXPORT void JNICALL Java_liba_crc64_clinit(JNIEnv *Env, jclass Cls)
21 jclass Nbb = (*Env)->FindClass(Env, "Ljava/nio/ByteBuffer;");
22 L.New = (*Env)->GetStaticMethodID(Env, Nbb, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
23 L.ctx = (*Env)->GetFieldID(Env, Cls, "ctx", "Ljava/nio/ByteBuffer;");
26 JNIEXPORT void JNICALL Java_liba_crc64_init(JNIEnv *Env, jobject Obj, jlong poly, jboolean reversed)
28 jobject Ctx = (*Env)->CallObjectMethod(Env, Obj, L.New, (jint)sizeof(struct crc64));
29 struct crc64 *ctx = (struct crc64 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
30 (*Env)->SetObjectField(Env, Obj, L.ctx, Ctx);
31 if (reversed)
33 a_crc64l_init(ctx->table, (a_u64)poly);
34 ctx->eval = a_crc64l;
36 else
38 a_crc64m_init(ctx->table, (a_u64)poly);
39 ctx->eval = a_crc64m;
43 JNIEXPORT jlongArray JNICALL Java_liba_crc64_table(JNIEnv *Env, jobject Obj)
45 jobject Ctx = (*Env)->GetObjectField(Env, Obj, L.ctx);
46 struct crc64 *ctx = (struct crc64 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
47 jlongArray table = (*Env)->NewLongArray(Env, A_LEN(ctx->table));
48 (*Env)->SetLongArrayRegion(Env, table, 0, A_LEN(ctx->table), (jlong *)ctx->table);
49 return table;
52 JNIEXPORT jobject JNICALL Java_liba_crc64_gen(JNIEnv *Env, jobject Obj, jlong poly, jboolean reversed)
54 jobject Ctx = (*Env)->GetObjectField(Env, Obj, L.ctx);
55 struct crc64 *ctx = (struct crc64 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
56 if (reversed)
58 a_crc64l_init(ctx->table, (a_u64)poly);
59 ctx->eval = a_crc64l;
61 else
63 a_crc64m_init(ctx->table, (a_u64)poly);
64 ctx->eval = a_crc64m;
66 return Obj;
69 JNIEXPORT jlong JNICALL Java_liba_crc64_eval(JNIEnv *Env, jobject Obj, jbyteArray block, jlong value)
71 jobject Ctx = (*Env)->GetObjectField(Env, Obj, L.ctx);
72 struct crc64 *ctx = (struct crc64 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
73 jsize n = (*Env)->GetArrayLength(Env, block);
74 jbyte *p = (*Env)->GetByteArrayElements(Env, block, NULL);
75 value = (jlong)ctx->eval(ctx->table, p, (a_size)n, (a_u64)value);
76 (*Env)->ReleaseByteArrayElements(Env, block, p, JNI_ABORT);
77 return value;
80 JNIEXPORT jbyteArray JNICALL Java_liba_crc64_pack(JNIEnv *Env, jobject Obj, jbyteArray block, jlong value)
82 jobject Ctx = (*Env)->GetObjectField(Env, Obj, L.ctx);
83 struct crc64 *ctx = (struct crc64 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
84 jsize block_n = (*Env)->GetArrayLength(Env, block);
85 jbyteArray res = (*Env)->NewByteArray(Env, block_n + 8);
86 jbyte *block_p = (*Env)->GetByteArrayElements(Env, block, NULL);
87 value = (jlong)ctx->eval(ctx->table, block_p, (a_size)block_n, (a_u64)value);
88 ctx->eval == a_crc64m ? a_u64_setb(&value, (a_u64)value) : a_u64_setl(&value, (a_u64)value);
89 (*Env)->SetByteArrayRegion(Env, res, 0, block_n, block_p);
90 (*Env)->ReleaseByteArrayElements(Env, block, block_p, JNI_ABORT);
91 (*Env)->SetByteArrayRegion(Env, res, block_n, 8, (jbyte *)&value);
92 return res;