3 #include "liba_crc64.h"
6 #define L Java_liba_crc64
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
);
33 a_crc64l_init(ctx
->table
, (a_u64
)poly
);
38 a_crc64m_init(ctx
->table
, (a_u64
)poly
);
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
);
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
);
58 a_crc64l_init(ctx
->table
, (a_u64
)poly
);
63 a_crc64m_init(ctx
->table
, (a_u64
)poly
);
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
);
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
);