release 0.1.15
[liba.git] / java / src / crc16.c
blobbe492a6c8219e752592fbd31b564211a043908eb
1 #include "a.h"
2 #include "a/crc.h"
3 #include "liba_crc16.h"
5 #undef L
6 #define L Java_liba_crc16
7 static struct
9 jmethodID New;
10 jfieldID ctx;
11 } L = {NULL, NULL};
13 JNIEXPORT void JNICALL Java_liba_crc16_clinit(JNIEnv *Env, jclass Cls)
15 jclass Nbb = (*Env)->FindClass(Env, "Ljava/nio/ByteBuffer;");
16 L.New = (*Env)->GetStaticMethodID(Env, Nbb, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
17 L.ctx = (*Env)->GetFieldID(Env, Cls, "ctx", "Ljava/nio/ByteBuffer;");
20 struct crc16
22 a_u16 table[0x100];
23 a_u16 (*eval)(a_u16 const table[0x100], void const *pdata, a_size nbyte, a_u16 value);
26 JNIEXPORT void JNICALL Java_liba_crc16_init(JNIEnv *Env, jobject Obj, jshort poly, jboolean reversed)
28 jobject Ctx = (*Env)->CallObjectMethod(Env, Obj, L.New, (jint)sizeof(struct crc16));
29 struct crc16 *ctx = (struct crc16 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
30 (*Env)->SetObjectField(Env, Obj, L.ctx, Ctx);
31 if (reversed)
33 a_crc16l_init(ctx->table, (a_u16)poly);
34 ctx->eval = a_crc16l;
36 else
38 a_crc16m_init(ctx->table, (a_u16)poly);
39 ctx->eval = a_crc16m;
43 JNIEXPORT jshortArray JNICALL Java_liba_crc16_table(JNIEnv *Env, jobject Obj)
45 jobject Ctx = (*Env)->GetObjectField(Env, Obj, L.ctx);
46 struct crc16 *ctx = (struct crc16 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
47 jshortArray table = (*Env)->NewShortArray(Env, A_LEN(ctx->table));
48 (*Env)->SetShortArrayRegion(Env, table, 0, A_LEN(ctx->table), (jshort *)ctx->table);
49 return table;
52 JNIEXPORT jobject JNICALL Java_liba_crc16_gen(JNIEnv *Env, jobject Obj, jshort poly, jboolean reversed)
54 jobject Ctx = (*Env)->GetObjectField(Env, Obj, L.ctx);
55 struct crc16 *ctx = (struct crc16 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
56 if (reversed)
58 a_crc16l_init(ctx->table, (a_u16)poly);
59 ctx->eval = a_crc16l;
61 else
63 a_crc16m_init(ctx->table, (a_u16)poly);
64 ctx->eval = a_crc16m;
66 return Obj;
69 JNIEXPORT jshort JNICALL Java_liba_crc16_eval(JNIEnv *Env, jobject Obj, jbyteArray block, jshort value)
71 jobject Ctx = (*Env)->GetObjectField(Env, Obj, L.ctx);
72 struct crc16 *ctx = (struct crc16 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
73 jsize n = (*Env)->GetArrayLength(Env, block);
74 jbyte *p = (*Env)->GetByteArrayElements(Env, block, NULL);
75 value = (jshort)ctx->eval(ctx->table, p, (a_size)n, (a_u16)value);
76 (*Env)->ReleaseByteArrayElements(Env, block, p, JNI_ABORT);
77 return value;
80 JNIEXPORT jbyteArray JNICALL Java_liba_crc16_pack(JNIEnv *Env, jobject Obj, jbyteArray block, jshort value)
82 jobject Ctx = (*Env)->GetObjectField(Env, Obj, L.ctx);
83 struct crc16 *ctx = (struct crc16 *)(*Env)->GetDirectBufferAddress(Env, Ctx);
84 jsize block_n = (*Env)->GetArrayLength(Env, block);
85 jbyteArray res = (*Env)->NewByteArray(Env, block_n + 2);
86 jbyte *block_p = (*Env)->GetByteArrayElements(Env, block, NULL);
87 value = (jshort)ctx->eval(ctx->table, block_p, (a_size)block_n, (a_u16)value);
88 ctx->eval == a_crc16m ? a_u16_setb(&value, (a_u16)value) : a_u16_setl(&value, (a_u16)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, 2, (jbyte *)&value);
92 return res;